capnproto-c++-0.8.0/0000755000175000017500000000000013650320027014746 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/build-aux/0000755000175000017500000000000013650320027016640 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/build-aux/compile0000755000175000017500000001632713650317755020245 0ustar00kentonkenton00000000000000#! /bin/sh # Wrapper for compilers which do not understand '-c -o'. scriptversion=2018-03-07.03; # UTC # Copyright (C) 1999-2018 Free Software Foundation, Inc. # Written by Tom Tromey . # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # As a special exception to the GNU General Public License, if you # distribute this file as part of a program that contains a # configuration script generated by Autoconf, you may include it under # the same distribution terms that you use for the rest of that program. # This file is maintained in Automake, please report # bugs to or send patches to # . nl=' ' # We need space, tab and new line, in precisely that order. Quoting is # there to prevent tools from complaining about whitespace usage. IFS=" "" $nl" file_conv= # func_file_conv build_file lazy # Convert a $build file to $host form and store it in $file # Currently only supports Windows hosts. If the determined conversion # type is listed in (the comma separated) LAZY, no conversion will # take place. func_file_conv () { file=$1 case $file in / | /[!/]*) # absolute file, and not a UNC file if test -z "$file_conv"; then # lazily determine how to convert abs files case `uname -s` in MINGW*) file_conv=mingw ;; CYGWIN*) file_conv=cygwin ;; *) file_conv=wine ;; esac fi case $file_conv/,$2, in *,$file_conv,*) ;; mingw/*) file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'` ;; cygwin/*) file=`cygpath -m "$file" || echo "$file"` ;; wine/*) file=`winepath -w "$file" || echo "$file"` ;; esac ;; esac } # func_cl_dashL linkdir # Make cl look for libraries in LINKDIR func_cl_dashL () { func_file_conv "$1" if test -z "$lib_path"; then lib_path=$file else lib_path="$lib_path;$file" fi linker_opts="$linker_opts -LIBPATH:$file" } # func_cl_dashl library # Do a library search-path lookup for cl func_cl_dashl () { lib=$1 found=no save_IFS=$IFS IFS=';' for dir in $lib_path $LIB do IFS=$save_IFS if $shared && test -f "$dir/$lib.dll.lib"; then found=yes lib=$dir/$lib.dll.lib break fi if test -f "$dir/$lib.lib"; then found=yes lib=$dir/$lib.lib break fi if test -f "$dir/lib$lib.a"; then found=yes lib=$dir/lib$lib.a break fi done IFS=$save_IFS if test "$found" != yes; then lib=$lib.lib fi } # func_cl_wrapper cl arg... # Adjust compile command to suit cl func_cl_wrapper () { # Assume a capable shell lib_path= shared=: linker_opts= for arg do if test -n "$eat"; then eat= else case $1 in -o) # configure might choose to run compile as 'compile cc -o foo foo.c'. eat=1 case $2 in *.o | *.[oO][bB][jJ]) func_file_conv "$2" set x "$@" -Fo"$file" shift ;; *) func_file_conv "$2" set x "$@" -Fe"$file" shift ;; esac ;; -I) eat=1 func_file_conv "$2" mingw set x "$@" -I"$file" shift ;; -I*) func_file_conv "${1#-I}" mingw set x "$@" -I"$file" shift ;; -l) eat=1 func_cl_dashl "$2" set x "$@" "$lib" shift ;; -l*) func_cl_dashl "${1#-l}" set x "$@" "$lib" shift ;; -L) eat=1 func_cl_dashL "$2" ;; -L*) func_cl_dashL "${1#-L}" ;; -static) shared=false ;; -Wl,*) arg=${1#-Wl,} save_ifs="$IFS"; IFS=',' for flag in $arg; do IFS="$save_ifs" linker_opts="$linker_opts $flag" done IFS="$save_ifs" ;; -Xlinker) eat=1 linker_opts="$linker_opts $2" ;; -*) set x "$@" "$1" shift ;; *.cc | *.CC | *.cxx | *.CXX | *.[cC]++) func_file_conv "$1" set x "$@" -Tp"$file" shift ;; *.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO]) func_file_conv "$1" mingw set x "$@" "$file" shift ;; *) set x "$@" "$1" shift ;; esac fi shift done if test -n "$linker_opts"; then linker_opts="-link$linker_opts" fi exec "$@" $linker_opts exit 1 } eat= case $1 in '') echo "$0: No command. Try '$0 --help' for more information." 1>&2 exit 1; ;; -h | --h*) cat <<\EOF Usage: compile [--help] [--version] PROGRAM [ARGS] Wrapper for compilers which do not understand '-c -o'. Remove '-o dest.o' from ARGS, run PROGRAM with the remaining arguments, and rename the output as expected. If you are trying to build a whole package this is not the right script to run: please start by reading the file 'INSTALL'. Report bugs to . EOF exit $? ;; -v | --v*) echo "compile $scriptversion" exit $? ;; cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \ icl | *[/\\]icl | icl.exe | *[/\\]icl.exe ) func_cl_wrapper "$@" # Doesn't return... ;; esac ofile= cfile= for arg do if test -n "$eat"; then eat= else case $1 in -o) # configure might choose to run compile as 'compile cc -o foo foo.c'. # So we strip '-o arg' only if arg is an object. eat=1 case $2 in *.o | *.obj) ofile=$2 ;; *) set x "$@" -o "$2" shift ;; esac ;; *.c) cfile=$1 set x "$@" "$1" shift ;; *) set x "$@" "$1" shift ;; esac fi shift done if test -z "$ofile" || test -z "$cfile"; then # If no '-o' option was seen then we might have been invoked from a # pattern rule where we don't need one. That is ok -- this is a # normal compilation that the losing compiler can handle. If no # '.c' file was seen then we are probably linking. That is also # ok. exec "$@" fi # Name of file we expect compiler to create. cofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'` # Create the lock directory. # Note: use '[/\\:.-]' here to ensure that we don't use the same name # that we are using for the .o file. Also, base the name on the expected # object file name, since that is what matters with a parallel build. lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d while true; do if mkdir "$lockdir" >/dev/null 2>&1; then break fi sleep 1 done # FIXME: race condition here if user kills between mkdir and trap. trap "rmdir '$lockdir'; exit 1" 1 2 15 # Run the compile. "$@" ret=$? if test -f "$cofile"; then test "$cofile" = "$ofile" || mv "$cofile" "$ofile" elif test -f "${cofile}bj"; then test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile" fi rmdir "$lockdir" exit $ret # Local Variables: # mode: shell-script # sh-indentation: 2 # eval: (add-hook 'before-save-hook 'time-stamp) # time-stamp-start: "scriptversion=" # time-stamp-format: "%:y-%02m-%02d.%02H" # time-stamp-time-zone: "UTC0" # time-stamp-end: "; # UTC" # End: capnproto-c++-0.8.0/build-aux/config.guess0000755000175000017500000012637313650317755021212 0ustar00kentonkenton00000000000000#! /bin/sh # Attempt to guess a canonical system name. # Copyright 1992-2018 Free Software Foundation, Inc. timestamp='2018-02-24' # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . # # As a special exception to the GNU General Public License, if you # distribute this file as part of a program that contains a # configuration script generated by Autoconf, you may include it under # the same distribution terms that you use for the rest of that # program. This Exception is an additional permission under section 7 # of the GNU General Public License, version 3 ("GPLv3"). # # Originally written by Per Bothner; maintained since 2000 by Ben Elliston. # # You can get the latest version of this script from: # https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess # # Please send patches to . me=`echo "$0" | sed -e 's,.*/,,'` usage="\ Usage: $0 [OPTION] Output the configuration name of the system \`$me' is run on. Options: -h, --help print this help, then exit -t, --time-stamp print date of last modification, then exit -v, --version print version number, then exit Report bugs and patches to ." version="\ GNU config.guess ($timestamp) Originally written by Per Bothner. Copyright 1992-2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." help=" Try \`$me --help' for more information." # Parse command line while test $# -gt 0 ; do case $1 in --time-stamp | --time* | -t ) echo "$timestamp" ; exit ;; --version | -v ) echo "$version" ; exit ;; --help | --h* | -h ) echo "$usage"; exit ;; -- ) # Stop option processing shift; break ;; - ) # Use stdin as input. break ;; -* ) echo "$me: invalid option $1$help" >&2 exit 1 ;; * ) break ;; esac done if test $# != 0; then echo "$me: too many arguments$help" >&2 exit 1 fi trap 'exit 1' 1 2 15 # CC_FOR_BUILD -- compiler used by this script. Note that the use of a # compiler to aid in system detection is discouraged as it requires # temporary files to be created and, as you can see below, it is a # headache to deal with in a portable fashion. # Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still # use `HOST_CC' if defined, but it is deprecated. # Portable tmp directory creation inspired by the Autoconf team. set_cc_for_build=' trap "exitcode=\$?; (rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null) && exit \$exitcode" 0 ; trap "rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null; exit 1" 1 2 13 15 ; : ${TMPDIR=/tmp} ; { tmp=`(umask 077 && mktemp -d "$TMPDIR/cgXXXXXX") 2>/dev/null` && test -n "$tmp" && test -d "$tmp" ; } || { test -n "$RANDOM" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } || { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir $tmp) && echo "Warning: creating insecure temp directory" >&2 ; } || { echo "$me: cannot create a temporary directory in $TMPDIR" >&2 ; exit 1 ; } ; dummy=$tmp/dummy ; tmpfiles="$dummy.c $dummy.o $dummy.rel $dummy" ; case $CC_FOR_BUILD,$HOST_CC,$CC in ,,) echo "int x;" > "$dummy.c" ; for c in cc gcc c89 c99 ; do if ($c -c -o "$dummy.o" "$dummy.c") >/dev/null 2>&1 ; then CC_FOR_BUILD="$c"; break ; fi ; done ; if test x"$CC_FOR_BUILD" = x ; then CC_FOR_BUILD=no_compiler_found ; fi ;; ,,*) CC_FOR_BUILD=$CC ;; ,*,*) CC_FOR_BUILD=$HOST_CC ;; esac ; set_cc_for_build= ;' # This is needed to find uname on a Pyramid OSx when run in the BSD universe. # (ghazi@noc.rutgers.edu 1994-08-24) if (test -f /.attbin/uname) >/dev/null 2>&1 ; then PATH=$PATH:/.attbin ; export PATH fi UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown case "$UNAME_SYSTEM" in Linux|GNU|GNU/*) # If the system lacks a compiler, then just pick glibc. # We could probably try harder. LIBC=gnu eval "$set_cc_for_build" cat <<-EOF > "$dummy.c" #include #if defined(__UCLIBC__) LIBC=uclibc #elif defined(__dietlibc__) LIBC=dietlibc #else LIBC=gnu #endif EOF eval "`$CC_FOR_BUILD -E "$dummy.c" 2>/dev/null | grep '^LIBC' | sed 's, ,,g'`" # If ldd exists, use it to detect musl libc. if command -v ldd >/dev/null && \ ldd --version 2>&1 | grep -q ^musl then LIBC=musl fi ;; esac # Note: order is significant - the case branches are not exclusive. case "$UNAME_MACHINE:$UNAME_SYSTEM:$UNAME_RELEASE:$UNAME_VERSION" in *:NetBSD:*:*) # NetBSD (nbsd) targets should (where applicable) match one or # more of the tuples: *-*-netbsdelf*, *-*-netbsdaout*, # *-*-netbsdecoff* and *-*-netbsd*. For targets that recently # switched to ELF, *-*-netbsd* would select the old # object file format. This provides both forward # compatibility and a consistent mechanism for selecting the # object file format. # # Note: NetBSD doesn't particularly care about the vendor # portion of the name. We always set it to "unknown". sysctl="sysctl -n hw.machine_arch" UNAME_MACHINE_ARCH=`(uname -p 2>/dev/null || \ "/sbin/$sysctl" 2>/dev/null || \ "/usr/sbin/$sysctl" 2>/dev/null || \ echo unknown)` case "$UNAME_MACHINE_ARCH" in armeb) machine=armeb-unknown ;; arm*) machine=arm-unknown ;; sh3el) machine=shl-unknown ;; sh3eb) machine=sh-unknown ;; sh5el) machine=sh5le-unknown ;; earmv*) arch=`echo "$UNAME_MACHINE_ARCH" | sed -e 's,^e\(armv[0-9]\).*$,\1,'` endian=`echo "$UNAME_MACHINE_ARCH" | sed -ne 's,^.*\(eb\)$,\1,p'` machine="${arch}${endian}"-unknown ;; *) machine="$UNAME_MACHINE_ARCH"-unknown ;; esac # The Operating System including object format, if it has switched # to ELF recently (or will in the future) and ABI. case "$UNAME_MACHINE_ARCH" in earm*) os=netbsdelf ;; arm*|i386|m68k|ns32k|sh3*|sparc|vax) eval "$set_cc_for_build" if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \ | grep -q __ELF__ then # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout). # Return netbsd for either. FIX? os=netbsd else os=netbsdelf fi ;; *) os=netbsd ;; esac # Determine ABI tags. case "$UNAME_MACHINE_ARCH" in earm*) expr='s/^earmv[0-9]/-eabi/;s/eb$//' abi=`echo "$UNAME_MACHINE_ARCH" | sed -e "$expr"` ;; esac # The OS release # Debian GNU/NetBSD machines have a different userland, and # thus, need a distinct triplet. However, they do not need # kernel version information, so it can be replaced with a # suitable tag, in the style of linux-gnu. case "$UNAME_VERSION" in Debian*) release='-gnu' ;; *) release=`echo "$UNAME_RELEASE" | sed -e 's/[-_].*//' | cut -d. -f1,2` ;; esac # Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM: # contains redundant information, the shorter form: # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used. echo "$machine-${os}${release}${abi}" exit ;; *:Bitrig:*:*) UNAME_MACHINE_ARCH=`arch | sed 's/Bitrig.//'` echo "$UNAME_MACHINE_ARCH"-unknown-bitrig"$UNAME_RELEASE" exit ;; *:OpenBSD:*:*) UNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'` echo "$UNAME_MACHINE_ARCH"-unknown-openbsd"$UNAME_RELEASE" exit ;; *:LibertyBSD:*:*) UNAME_MACHINE_ARCH=`arch | sed 's/^.*BSD\.//'` echo "$UNAME_MACHINE_ARCH"-unknown-libertybsd"$UNAME_RELEASE" exit ;; *:MidnightBSD:*:*) echo "$UNAME_MACHINE"-unknown-midnightbsd"$UNAME_RELEASE" exit ;; *:ekkoBSD:*:*) echo "$UNAME_MACHINE"-unknown-ekkobsd"$UNAME_RELEASE" exit ;; *:SolidBSD:*:*) echo "$UNAME_MACHINE"-unknown-solidbsd"$UNAME_RELEASE" exit ;; macppc:MirBSD:*:*) echo powerpc-unknown-mirbsd"$UNAME_RELEASE" exit ;; *:MirBSD:*:*) echo "$UNAME_MACHINE"-unknown-mirbsd"$UNAME_RELEASE" exit ;; *:Sortix:*:*) echo "$UNAME_MACHINE"-unknown-sortix exit ;; *:Redox:*:*) echo "$UNAME_MACHINE"-unknown-redox exit ;; mips:OSF1:*.*) echo mips-dec-osf1 exit ;; alpha:OSF1:*:*) case $UNAME_RELEASE in *4.0) UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'` ;; *5.*) UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'` ;; esac # According to Compaq, /usr/sbin/psrinfo has been available on # OSF/1 and Tru64 systems produced since 1995. I hope that # covers most systems running today. This code pipes the CPU # types through head -n 1, so we only detect the type of CPU 0. ALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^ The alpha \(.*\) processor.*$/\1/p' | head -n 1` case "$ALPHA_CPU_TYPE" in "EV4 (21064)") UNAME_MACHINE=alpha ;; "EV4.5 (21064)") UNAME_MACHINE=alpha ;; "LCA4 (21066/21068)") UNAME_MACHINE=alpha ;; "EV5 (21164)") UNAME_MACHINE=alphaev5 ;; "EV5.6 (21164A)") UNAME_MACHINE=alphaev56 ;; "EV5.6 (21164PC)") UNAME_MACHINE=alphapca56 ;; "EV5.7 (21164PC)") UNAME_MACHINE=alphapca57 ;; "EV6 (21264)") UNAME_MACHINE=alphaev6 ;; "EV6.7 (21264A)") UNAME_MACHINE=alphaev67 ;; "EV6.8CB (21264C)") UNAME_MACHINE=alphaev68 ;; "EV6.8AL (21264B)") UNAME_MACHINE=alphaev68 ;; "EV6.8CX (21264D)") UNAME_MACHINE=alphaev68 ;; "EV6.9A (21264/EV69A)") UNAME_MACHINE=alphaev69 ;; "EV7 (21364)") UNAME_MACHINE=alphaev7 ;; "EV7.9 (21364A)") UNAME_MACHINE=alphaev79 ;; esac # A Pn.n version is a patched version. # A Vn.n version is a released version. # A Tn.n version is a released field test version. # A Xn.n version is an unreleased experimental baselevel. # 1.2 uses "1.2" for uname -r. echo "$UNAME_MACHINE"-dec-osf"`echo "$UNAME_RELEASE" | sed -e 's/^[PVTX]//' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`" # Reset EXIT trap before exiting to avoid spurious non-zero exit code. exitcode=$? trap '' 0 exit $exitcode ;; Amiga*:UNIX_System_V:4.0:*) echo m68k-unknown-sysv4 exit ;; *:[Aa]miga[Oo][Ss]:*:*) echo "$UNAME_MACHINE"-unknown-amigaos exit ;; *:[Mm]orph[Oo][Ss]:*:*) echo "$UNAME_MACHINE"-unknown-morphos exit ;; *:OS/390:*:*) echo i370-ibm-openedition exit ;; *:z/VM:*:*) echo s390-ibm-zvmoe exit ;; *:OS400:*:*) echo powerpc-ibm-os400 exit ;; arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*) echo arm-acorn-riscix"$UNAME_RELEASE" exit ;; arm*:riscos:*:*|arm*:RISCOS:*:*) echo arm-unknown-riscos exit ;; SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*) echo hppa1.1-hitachi-hiuxmpp exit ;; Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*) # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE. if test "`(/bin/universe) 2>/dev/null`" = att ; then echo pyramid-pyramid-sysv3 else echo pyramid-pyramid-bsd fi exit ;; NILE*:*:*:dcosx) echo pyramid-pyramid-svr4 exit ;; DRS?6000:unix:4.0:6*) echo sparc-icl-nx6 exit ;; DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*) case `/usr/bin/uname -p` in sparc) echo sparc-icl-nx7; exit ;; esac ;; s390x:SunOS:*:*) echo "$UNAME_MACHINE"-ibm-solaris2"`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'`" exit ;; sun4H:SunOS:5.*:*) echo sparc-hal-solaris2"`echo "$UNAME_RELEASE"|sed -e 's/[^.]*//'`" exit ;; sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*) echo sparc-sun-solaris2"`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'`" exit ;; i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*) echo i386-pc-auroraux"$UNAME_RELEASE" exit ;; i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*) eval "$set_cc_for_build" SUN_ARCH=i386 # If there is a compiler, see if it is configured for 64-bit objects. # Note that the Sun cc does not turn __LP64__ into 1 like gcc does. # This test works for both compilers. if [ "$CC_FOR_BUILD" != no_compiler_found ]; then if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \ (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ grep IS_64BIT_ARCH >/dev/null then SUN_ARCH=x86_64 fi fi echo "$SUN_ARCH"-pc-solaris2"`echo "$UNAME_RELEASE"|sed -e 's/[^.]*//'`" exit ;; sun4*:SunOS:6*:*) # According to config.sub, this is the proper way to canonicalize # SunOS6. Hard to guess exactly what SunOS6 will be like, but # it's likely to be more like Solaris than SunOS4. echo sparc-sun-solaris3"`echo "$UNAME_RELEASE"|sed -e 's/[^.]*//'`" exit ;; sun4*:SunOS:*:*) case "`/usr/bin/arch -k`" in Series*|S4*) UNAME_RELEASE=`uname -v` ;; esac # Japanese Language versions have a version number like `4.1.3-JL'. echo sparc-sun-sunos"`echo "$UNAME_RELEASE"|sed -e 's/-/_/'`" exit ;; sun3*:SunOS:*:*) echo m68k-sun-sunos"$UNAME_RELEASE" exit ;; sun*:*:4.2BSD:*) UNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null` test "x$UNAME_RELEASE" = x && UNAME_RELEASE=3 case "`/bin/arch`" in sun3) echo m68k-sun-sunos"$UNAME_RELEASE" ;; sun4) echo sparc-sun-sunos"$UNAME_RELEASE" ;; esac exit ;; aushp:SunOS:*:*) echo sparc-auspex-sunos"$UNAME_RELEASE" exit ;; # The situation for MiNT is a little confusing. The machine name # can be virtually everything (everything which is not # "atarist" or "atariste" at least should have a processor # > m68000). The system name ranges from "MiNT" over "FreeMiNT" # to the lowercase version "mint" (or "freemint"). Finally # the system name "TOS" denotes a system which is actually not # MiNT. But MiNT is downward compatible to TOS, so this should # be no problem. atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*) echo m68k-atari-mint"$UNAME_RELEASE" exit ;; atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*) echo m68k-atari-mint"$UNAME_RELEASE" exit ;; *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*) echo m68k-atari-mint"$UNAME_RELEASE" exit ;; milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*) echo m68k-milan-mint"$UNAME_RELEASE" exit ;; hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*) echo m68k-hades-mint"$UNAME_RELEASE" exit ;; *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*) echo m68k-unknown-mint"$UNAME_RELEASE" exit ;; m68k:machten:*:*) echo m68k-apple-machten"$UNAME_RELEASE" exit ;; powerpc:machten:*:*) echo powerpc-apple-machten"$UNAME_RELEASE" exit ;; RISC*:Mach:*:*) echo mips-dec-mach_bsd4.3 exit ;; RISC*:ULTRIX:*:*) echo mips-dec-ultrix"$UNAME_RELEASE" exit ;; VAX*:ULTRIX*:*:*) echo vax-dec-ultrix"$UNAME_RELEASE" exit ;; 2020:CLIX:*:* | 2430:CLIX:*:*) echo clipper-intergraph-clix"$UNAME_RELEASE" exit ;; mips:*:*:UMIPS | mips:*:*:RISCos) eval "$set_cc_for_build" sed 's/^ //' << EOF > "$dummy.c" #ifdef __cplusplus #include /* for printf() prototype */ int main (int argc, char *argv[]) { #else int main (argc, argv) int argc; char *argv[]; { #endif #if defined (host_mips) && defined (MIPSEB) #if defined (SYSTYPE_SYSV) printf ("mips-mips-riscos%ssysv\\n", argv[1]); exit (0); #endif #if defined (SYSTYPE_SVR4) printf ("mips-mips-riscos%ssvr4\\n", argv[1]); exit (0); #endif #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD) printf ("mips-mips-riscos%sbsd\\n", argv[1]); exit (0); #endif #endif exit (-1); } EOF $CC_FOR_BUILD -o "$dummy" "$dummy.c" && dummyarg=`echo "$UNAME_RELEASE" | sed -n 's/\([0-9]*\).*/\1/p'` && SYSTEM_NAME=`"$dummy" "$dummyarg"` && { echo "$SYSTEM_NAME"; exit; } echo mips-mips-riscos"$UNAME_RELEASE" exit ;; Motorola:PowerMAX_OS:*:*) echo powerpc-motorola-powermax exit ;; Motorola:*:4.3:PL8-*) echo powerpc-harris-powermax exit ;; Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*) echo powerpc-harris-powermax exit ;; Night_Hawk:Power_UNIX:*:*) echo powerpc-harris-powerunix exit ;; m88k:CX/UX:7*:*) echo m88k-harris-cxux7 exit ;; m88k:*:4*:R4*) echo m88k-motorola-sysv4 exit ;; m88k:*:3*:R3*) echo m88k-motorola-sysv3 exit ;; AViiON:dgux:*:*) # DG/UX returns AViiON for all architectures UNAME_PROCESSOR=`/usr/bin/uname -p` if [ "$UNAME_PROCESSOR" = mc88100 ] || [ "$UNAME_PROCESSOR" = mc88110 ] then if [ "$TARGET_BINARY_INTERFACE"x = m88kdguxelfx ] || \ [ "$TARGET_BINARY_INTERFACE"x = x ] then echo m88k-dg-dgux"$UNAME_RELEASE" else echo m88k-dg-dguxbcs"$UNAME_RELEASE" fi else echo i586-dg-dgux"$UNAME_RELEASE" fi exit ;; M88*:DolphinOS:*:*) # DolphinOS (SVR3) echo m88k-dolphin-sysv3 exit ;; M88*:*:R3*:*) # Delta 88k system running SVR3 echo m88k-motorola-sysv3 exit ;; XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3) echo m88k-tektronix-sysv3 exit ;; Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD) echo m68k-tektronix-bsd exit ;; *:IRIX*:*:*) echo mips-sgi-irix"`echo "$UNAME_RELEASE"|sed -e 's/-/_/g'`" exit ;; ????????:AIX?:[12].1:2) # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX. echo romp-ibm-aix # uname -m gives an 8 hex-code CPU id exit ;; # Note that: echo "'`uname -s`'" gives 'AIX ' i*86:AIX:*:*) echo i386-ibm-aix exit ;; ia64:AIX:*:*) if [ -x /usr/bin/oslevel ] ; then IBM_REV=`/usr/bin/oslevel` else IBM_REV="$UNAME_VERSION.$UNAME_RELEASE" fi echo "$UNAME_MACHINE"-ibm-aix"$IBM_REV" exit ;; *:AIX:2:3) if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then eval "$set_cc_for_build" sed 's/^ //' << EOF > "$dummy.c" #include main() { if (!__power_pc()) exit(1); puts("powerpc-ibm-aix3.2.5"); exit(0); } EOF if $CC_FOR_BUILD -o "$dummy" "$dummy.c" && SYSTEM_NAME=`"$dummy"` then echo "$SYSTEM_NAME" else echo rs6000-ibm-aix3.2.5 fi elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then echo rs6000-ibm-aix3.2.4 else echo rs6000-ibm-aix3.2 fi exit ;; *:AIX:*:[4567]) IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'` if /usr/sbin/lsattr -El "$IBM_CPU_ID" | grep ' POWER' >/dev/null 2>&1; then IBM_ARCH=rs6000 else IBM_ARCH=powerpc fi if [ -x /usr/bin/lslpp ] ; then IBM_REV=`/usr/bin/lslpp -Lqc bos.rte.libc | awk -F: '{ print $3 }' | sed s/[0-9]*$/0/` else IBM_REV="$UNAME_VERSION.$UNAME_RELEASE" fi echo "$IBM_ARCH"-ibm-aix"$IBM_REV" exit ;; *:AIX:*:*) echo rs6000-ibm-aix exit ;; ibmrt:4.4BSD:*|romp-ibm:4.4BSD:*) echo romp-ibm-bsd4.4 exit ;; ibmrt:*BSD:*|romp-ibm:BSD:*) # covers RT/PC BSD and echo romp-ibm-bsd"$UNAME_RELEASE" # 4.3 with uname added to exit ;; # report: romp-ibm BSD 4.3 *:BOSX:*:*) echo rs6000-bull-bosx exit ;; DPX/2?00:B.O.S.:*:*) echo m68k-bull-sysv3 exit ;; 9000/[34]??:4.3bsd:1.*:*) echo m68k-hp-bsd exit ;; hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*) echo m68k-hp-bsd4.4 exit ;; 9000/[34678]??:HP-UX:*:*) HPUX_REV=`echo "$UNAME_RELEASE"|sed -e 's/[^.]*.[0B]*//'` case "$UNAME_MACHINE" in 9000/31?) HP_ARCH=m68000 ;; 9000/[34]??) HP_ARCH=m68k ;; 9000/[678][0-9][0-9]) if [ -x /usr/bin/getconf ]; then sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null` sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null` case "$sc_cpu_version" in 523) HP_ARCH=hppa1.0 ;; # CPU_PA_RISC1_0 528) HP_ARCH=hppa1.1 ;; # CPU_PA_RISC1_1 532) # CPU_PA_RISC2_0 case "$sc_kernel_bits" in 32) HP_ARCH=hppa2.0n ;; 64) HP_ARCH=hppa2.0w ;; '') HP_ARCH=hppa2.0 ;; # HP-UX 10.20 esac ;; esac fi if [ "$HP_ARCH" = "" ]; then eval "$set_cc_for_build" sed 's/^ //' << EOF > "$dummy.c" #define _HPUX_SOURCE #include #include int main () { #if defined(_SC_KERNEL_BITS) long bits = sysconf(_SC_KERNEL_BITS); #endif long cpu = sysconf (_SC_CPU_VERSION); switch (cpu) { case CPU_PA_RISC1_0: puts ("hppa1.0"); break; case CPU_PA_RISC1_1: puts ("hppa1.1"); break; case CPU_PA_RISC2_0: #if defined(_SC_KERNEL_BITS) switch (bits) { case 64: puts ("hppa2.0w"); break; case 32: puts ("hppa2.0n"); break; default: puts ("hppa2.0"); break; } break; #else /* !defined(_SC_KERNEL_BITS) */ puts ("hppa2.0"); break; #endif default: puts ("hppa1.0"); break; } exit (0); } EOF (CCOPTS="" $CC_FOR_BUILD -o "$dummy" "$dummy.c" 2>/dev/null) && HP_ARCH=`"$dummy"` test -z "$HP_ARCH" && HP_ARCH=hppa fi ;; esac if [ "$HP_ARCH" = hppa2.0w ] then eval "$set_cc_for_build" # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating # 32-bit code. hppa64-hp-hpux* has the same kernel and a compiler # generating 64-bit code. GNU and HP use different nomenclature: # # $ CC_FOR_BUILD=cc ./config.guess # => hppa2.0w-hp-hpux11.23 # $ CC_FOR_BUILD="cc +DA2.0w" ./config.guess # => hppa64-hp-hpux11.23 if echo __LP64__ | (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | grep -q __LP64__ then HP_ARCH=hppa2.0w else HP_ARCH=hppa64 fi fi echo "$HP_ARCH"-hp-hpux"$HPUX_REV" exit ;; ia64:HP-UX:*:*) HPUX_REV=`echo "$UNAME_RELEASE"|sed -e 's/[^.]*.[0B]*//'` echo ia64-hp-hpux"$HPUX_REV" exit ;; 3050*:HI-UX:*:*) eval "$set_cc_for_build" sed 's/^ //' << EOF > "$dummy.c" #include int main () { long cpu = sysconf (_SC_CPU_VERSION); /* The order matters, because CPU_IS_HP_MC68K erroneously returns true for CPU_PA_RISC1_0. CPU_IS_PA_RISC returns correct results, however. */ if (CPU_IS_PA_RISC (cpu)) { switch (cpu) { case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break; case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break; case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break; default: puts ("hppa-hitachi-hiuxwe2"); break; } } else if (CPU_IS_HP_MC68K (cpu)) puts ("m68k-hitachi-hiuxwe2"); else puts ("unknown-hitachi-hiuxwe2"); exit (0); } EOF $CC_FOR_BUILD -o "$dummy" "$dummy.c" && SYSTEM_NAME=`"$dummy"` && { echo "$SYSTEM_NAME"; exit; } echo unknown-hitachi-hiuxwe2 exit ;; 9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:*) echo hppa1.1-hp-bsd exit ;; 9000/8??:4.3bsd:*:*) echo hppa1.0-hp-bsd exit ;; *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*) echo hppa1.0-hp-mpeix exit ;; hp7??:OSF1:*:* | hp8?[79]:OSF1:*:*) echo hppa1.1-hp-osf exit ;; hp8??:OSF1:*:*) echo hppa1.0-hp-osf exit ;; i*86:OSF1:*:*) if [ -x /usr/sbin/sysversion ] ; then echo "$UNAME_MACHINE"-unknown-osf1mk else echo "$UNAME_MACHINE"-unknown-osf1 fi exit ;; parisc*:Lites*:*:*) echo hppa1.1-hp-lites exit ;; C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*) echo c1-convex-bsd exit ;; C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*) if getsysinfo -f scalar_acc then echo c32-convex-bsd else echo c2-convex-bsd fi exit ;; C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*) echo c34-convex-bsd exit ;; C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*) echo c38-convex-bsd exit ;; C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*) echo c4-convex-bsd exit ;; CRAY*Y-MP:*:*:*) echo ymp-cray-unicos"$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/' exit ;; CRAY*[A-Z]90:*:*:*) echo "$UNAME_MACHINE"-cray-unicos"$UNAME_RELEASE" \ | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \ -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \ -e 's/\.[^.]*$/.X/' exit ;; CRAY*TS:*:*:*) echo t90-cray-unicos"$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/' exit ;; CRAY*T3E:*:*:*) echo alphaev5-cray-unicosmk"$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/' exit ;; CRAY*SV1:*:*:*) echo sv1-cray-unicos"$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/' exit ;; *:UNICOS/mp:*:*) echo craynv-cray-unicosmp"$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/' exit ;; F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*) FUJITSU_PROC=`uname -m | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz` FUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\///'` FUJITSU_REL=`echo "$UNAME_RELEASE" | sed -e 's/ /_/'` echo "${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" exit ;; 5000:UNIX_System_V:4.*:*) FUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\///'` FUJITSU_REL=`echo "$UNAME_RELEASE" | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/ /_/'` echo "sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" exit ;; i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\ Embedded/OS:*:*) echo "$UNAME_MACHINE"-pc-bsdi"$UNAME_RELEASE" exit ;; sparc*:BSD/OS:*:*) echo sparc-unknown-bsdi"$UNAME_RELEASE" exit ;; *:BSD/OS:*:*) echo "$UNAME_MACHINE"-unknown-bsdi"$UNAME_RELEASE" exit ;; *:FreeBSD:*:*) UNAME_PROCESSOR=`/usr/bin/uname -p` case "$UNAME_PROCESSOR" in amd64) UNAME_PROCESSOR=x86_64 ;; i386) UNAME_PROCESSOR=i586 ;; esac echo "$UNAME_PROCESSOR"-unknown-freebsd"`echo "$UNAME_RELEASE"|sed -e 's/[-(].*//'`" exit ;; i*:CYGWIN*:*) echo "$UNAME_MACHINE"-pc-cygwin exit ;; *:MINGW64*:*) echo "$UNAME_MACHINE"-pc-mingw64 exit ;; *:MINGW*:*) echo "$UNAME_MACHINE"-pc-mingw32 exit ;; *:MSYS*:*) echo "$UNAME_MACHINE"-pc-msys exit ;; i*:PW*:*) echo "$UNAME_MACHINE"-pc-pw32 exit ;; *:Interix*:*) case "$UNAME_MACHINE" in x86) echo i586-pc-interix"$UNAME_RELEASE" exit ;; authenticamd | genuineintel | EM64T) echo x86_64-unknown-interix"$UNAME_RELEASE" exit ;; IA64) echo ia64-unknown-interix"$UNAME_RELEASE" exit ;; esac ;; i*:UWIN*:*) echo "$UNAME_MACHINE"-pc-uwin exit ;; amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*) echo x86_64-unknown-cygwin exit ;; prep*:SunOS:5.*:*) echo powerpcle-unknown-solaris2"`echo "$UNAME_RELEASE"|sed -e 's/[^.]*//'`" exit ;; *:GNU:*:*) # the GNU system echo "`echo "$UNAME_MACHINE"|sed -e 's,[-/].*$,,'`-unknown-$LIBC`echo "$UNAME_RELEASE"|sed -e 's,/.*$,,'`" exit ;; *:GNU/*:*:*) # other systems with GNU libc and userland echo "$UNAME_MACHINE-unknown-`echo "$UNAME_SYSTEM" | sed 's,^[^/]*/,,' | tr "[:upper:]" "[:lower:]"``echo "$UNAME_RELEASE"|sed -e 's/[-(].*//'`-$LIBC" exit ;; i*86:Minix:*:*) echo "$UNAME_MACHINE"-pc-minix exit ;; aarch64:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; aarch64_be:Linux:*:*) UNAME_MACHINE=aarch64_be echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; alpha:Linux:*:*) case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in EV5) UNAME_MACHINE=alphaev5 ;; EV56) UNAME_MACHINE=alphaev56 ;; PCA56) UNAME_MACHINE=alphapca56 ;; PCA57) UNAME_MACHINE=alphapca56 ;; EV6) UNAME_MACHINE=alphaev6 ;; EV67) UNAME_MACHINE=alphaev67 ;; EV68*) UNAME_MACHINE=alphaev68 ;; esac objdump --private-headers /bin/sh | grep -q ld.so.1 if test "$?" = 0 ; then LIBC=gnulibc1 ; fi echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; arc:Linux:*:* | arceb:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; arm*:Linux:*:*) eval "$set_cc_for_build" if echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \ | grep -q __ARM_EABI__ then echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" else if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \ | grep -q __ARM_PCS_VFP then echo "$UNAME_MACHINE"-unknown-linux-"$LIBC"eabi else echo "$UNAME_MACHINE"-unknown-linux-"$LIBC"eabihf fi fi exit ;; avr32*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; cris:Linux:*:*) echo "$UNAME_MACHINE"-axis-linux-"$LIBC" exit ;; crisv32:Linux:*:*) echo "$UNAME_MACHINE"-axis-linux-"$LIBC" exit ;; e2k:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; frv:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; hexagon:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; i*86:Linux:*:*) echo "$UNAME_MACHINE"-pc-linux-"$LIBC" exit ;; ia64:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; k1om:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; m32r*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; m68*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; mips:Linux:*:* | mips64:Linux:*:*) eval "$set_cc_for_build" sed 's/^ //' << EOF > "$dummy.c" #undef CPU #undef ${UNAME_MACHINE} #undef ${UNAME_MACHINE}el #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL) CPU=${UNAME_MACHINE}el #else #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) CPU=${UNAME_MACHINE} #else CPU= #endif #endif EOF eval "`$CC_FOR_BUILD -E "$dummy.c" 2>/dev/null | grep '^CPU'`" test "x$CPU" != x && { echo "$CPU-unknown-linux-$LIBC"; exit; } ;; mips64el:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; openrisc*:Linux:*:*) echo or1k-unknown-linux-"$LIBC" exit ;; or32:Linux:*:* | or1k*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; padre:Linux:*:*) echo sparc-unknown-linux-"$LIBC" exit ;; parisc64:Linux:*:* | hppa64:Linux:*:*) echo hppa64-unknown-linux-"$LIBC" exit ;; parisc:Linux:*:* | hppa:Linux:*:*) # Look for CPU level case `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in PA7*) echo hppa1.1-unknown-linux-"$LIBC" ;; PA8*) echo hppa2.0-unknown-linux-"$LIBC" ;; *) echo hppa-unknown-linux-"$LIBC" ;; esac exit ;; ppc64:Linux:*:*) echo powerpc64-unknown-linux-"$LIBC" exit ;; ppc:Linux:*:*) echo powerpc-unknown-linux-"$LIBC" exit ;; ppc64le:Linux:*:*) echo powerpc64le-unknown-linux-"$LIBC" exit ;; ppcle:Linux:*:*) echo powerpcle-unknown-linux-"$LIBC" exit ;; riscv32:Linux:*:* | riscv64:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; s390:Linux:*:* | s390x:Linux:*:*) echo "$UNAME_MACHINE"-ibm-linux-"$LIBC" exit ;; sh64*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; sh*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; sparc:Linux:*:* | sparc64:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; tile*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; vax:Linux:*:*) echo "$UNAME_MACHINE"-dec-linux-"$LIBC" exit ;; x86_64:Linux:*:*) if objdump -f /bin/sh | grep -q elf32-x86-64; then echo "$UNAME_MACHINE"-pc-linux-"$LIBC"x32 else echo "$UNAME_MACHINE"-pc-linux-"$LIBC" fi exit ;; xtensa*:Linux:*:*) echo "$UNAME_MACHINE"-unknown-linux-"$LIBC" exit ;; i*86:DYNIX/ptx:4*:*) # ptx 4.0 does uname -s correctly, with DYNIX/ptx in there. # earlier versions are messed up and put the nodename in both # sysname and nodename. echo i386-sequent-sysv4 exit ;; i*86:UNIX_SV:4.2MP:2.*) # Unixware is an offshoot of SVR4, but it has its own version # number series starting with 2... # I am not positive that other SVR4 systems won't match this, # I just have to hope. -- rms. # Use sysv4.2uw... so that sysv4* matches it. echo "$UNAME_MACHINE"-pc-sysv4.2uw"$UNAME_VERSION" exit ;; i*86:OS/2:*:*) # If we were able to find `uname', then EMX Unix compatibility # is probably installed. echo "$UNAME_MACHINE"-pc-os2-emx exit ;; i*86:XTS-300:*:STOP) echo "$UNAME_MACHINE"-unknown-stop exit ;; i*86:atheos:*:*) echo "$UNAME_MACHINE"-unknown-atheos exit ;; i*86:syllable:*:*) echo "$UNAME_MACHINE"-pc-syllable exit ;; i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*) echo i386-unknown-lynxos"$UNAME_RELEASE" exit ;; i*86:*DOS:*:*) echo "$UNAME_MACHINE"-pc-msdosdjgpp exit ;; i*86:*:4.*:*) UNAME_REL=`echo "$UNAME_RELEASE" | sed 's/\/MP$//'` if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then echo "$UNAME_MACHINE"-univel-sysv"$UNAME_REL" else echo "$UNAME_MACHINE"-pc-sysv"$UNAME_REL" fi exit ;; i*86:*:5:[678]*) # UnixWare 7.x, OpenUNIX and OpenServer 6. case `/bin/uname -X | grep "^Machine"` in *486*) UNAME_MACHINE=i486 ;; *Pentium) UNAME_MACHINE=i586 ;; *Pent*|*Celeron) UNAME_MACHINE=i686 ;; esac echo "$UNAME_MACHINE-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}{$UNAME_VERSION}" exit ;; i*86:*:3.2:*) if test -f /usr/options/cb.name; then UNAME_REL=`sed -n 's/.*Version //p' /dev/null >/dev/null ; then UNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')` (/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486 (/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \ && UNAME_MACHINE=i586 (/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \ && UNAME_MACHINE=i686 (/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \ && UNAME_MACHINE=i686 echo "$UNAME_MACHINE"-pc-sco"$UNAME_REL" else echo "$UNAME_MACHINE"-pc-sysv32 fi exit ;; pc:*:*:*) # Left here for compatibility: # uname -m prints for DJGPP always 'pc', but it prints nothing about # the processor, so we play safe by assuming i586. # Note: whatever this is, it MUST be the same as what config.sub # prints for the "djgpp" host, or else GDB configure will decide that # this is a cross-build. echo i586-pc-msdosdjgpp exit ;; Intel:Mach:3*:*) echo i386-pc-mach3 exit ;; paragon:*:*:*) echo i860-intel-osf1 exit ;; i860:*:4.*:*) # i860-SVR4 if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then echo i860-stardent-sysv"$UNAME_RELEASE" # Stardent Vistra i860-SVR4 else # Add other i860-SVR4 vendors below as they are discovered. echo i860-unknown-sysv"$UNAME_RELEASE" # Unknown i860-SVR4 fi exit ;; mini*:CTIX:SYS*5:*) # "miniframe" echo m68010-convergent-sysv exit ;; mc68k:UNIX:SYSTEM5:3.51m) echo m68k-convergent-sysv exit ;; M680?0:D-NIX:5.3:*) echo m68k-diab-dnix exit ;; M68*:*:R3V[5678]*:*) test -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;; 3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0) OS_REL='' test -r /etc/.relid \ && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ && { echo i486-ncr-sysv4.3"$OS_REL"; exit; } /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ && { echo i586-ncr-sysv4.3"$OS_REL"; exit; } ;; 3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*) /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ && { echo i486-ncr-sysv4; exit; } ;; NCR*:*:4.2:* | MPRAS*:*:4.2:*) OS_REL='.3' test -r /etc/.relid \ && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ && { echo i486-ncr-sysv4.3"$OS_REL"; exit; } /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ && { echo i586-ncr-sysv4.3"$OS_REL"; exit; } /bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \ && { echo i586-ncr-sysv4.3"$OS_REL"; exit; } ;; m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*) echo m68k-unknown-lynxos"$UNAME_RELEASE" exit ;; mc68030:UNIX_System_V:4.*:*) echo m68k-atari-sysv4 exit ;; TSUNAMI:LynxOS:2.*:*) echo sparc-unknown-lynxos"$UNAME_RELEASE" exit ;; rs6000:LynxOS:2.*:*) echo rs6000-unknown-lynxos"$UNAME_RELEASE" exit ;; PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*) echo powerpc-unknown-lynxos"$UNAME_RELEASE" exit ;; SM[BE]S:UNIX_SV:*:*) echo mips-dde-sysv"$UNAME_RELEASE" exit ;; RM*:ReliantUNIX-*:*:*) echo mips-sni-sysv4 exit ;; RM*:SINIX-*:*:*) echo mips-sni-sysv4 exit ;; *:SINIX-*:*:*) if uname -p 2>/dev/null >/dev/null ; then UNAME_MACHINE=`(uname -p) 2>/dev/null` echo "$UNAME_MACHINE"-sni-sysv4 else echo ns32k-sni-sysv fi exit ;; PENTIUM:*:4.0*:*) # Unisys `ClearPath HMP IX 4000' SVR4/MP effort # says echo i586-unisys-sysv4 exit ;; *:UNIX_System_V:4*:FTX*) # From Gerald Hewes . # How about differentiating between stratus architectures? -djm echo hppa1.1-stratus-sysv4 exit ;; *:*:*:FTX*) # From seanf@swdc.stratus.com. echo i860-stratus-sysv4 exit ;; i*86:VOS:*:*) # From Paul.Green@stratus.com. echo "$UNAME_MACHINE"-stratus-vos exit ;; *:VOS:*:*) # From Paul.Green@stratus.com. echo hppa1.1-stratus-vos exit ;; mc68*:A/UX:*:*) echo m68k-apple-aux"$UNAME_RELEASE" exit ;; news*:NEWS-OS:6*:*) echo mips-sony-newsos6 exit ;; R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*) if [ -d /usr/nec ]; then echo mips-nec-sysv"$UNAME_RELEASE" else echo mips-unknown-sysv"$UNAME_RELEASE" fi exit ;; BeBox:BeOS:*:*) # BeOS running on hardware made by Be, PPC only. echo powerpc-be-beos exit ;; BeMac:BeOS:*:*) # BeOS running on Mac or Mac clone, PPC only. echo powerpc-apple-beos exit ;; BePC:BeOS:*:*) # BeOS running on Intel PC compatible. echo i586-pc-beos exit ;; BePC:Haiku:*:*) # Haiku running on Intel PC compatible. echo i586-pc-haiku exit ;; x86_64:Haiku:*:*) echo x86_64-unknown-haiku exit ;; SX-4:SUPER-UX:*:*) echo sx4-nec-superux"$UNAME_RELEASE" exit ;; SX-5:SUPER-UX:*:*) echo sx5-nec-superux"$UNAME_RELEASE" exit ;; SX-6:SUPER-UX:*:*) echo sx6-nec-superux"$UNAME_RELEASE" exit ;; SX-7:SUPER-UX:*:*) echo sx7-nec-superux"$UNAME_RELEASE" exit ;; SX-8:SUPER-UX:*:*) echo sx8-nec-superux"$UNAME_RELEASE" exit ;; SX-8R:SUPER-UX:*:*) echo sx8r-nec-superux"$UNAME_RELEASE" exit ;; SX-ACE:SUPER-UX:*:*) echo sxace-nec-superux"$UNAME_RELEASE" exit ;; Power*:Rhapsody:*:*) echo powerpc-apple-rhapsody"$UNAME_RELEASE" exit ;; *:Rhapsody:*:*) echo "$UNAME_MACHINE"-apple-rhapsody"$UNAME_RELEASE" exit ;; *:Darwin:*:*) UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown eval "$set_cc_for_build" if test "$UNAME_PROCESSOR" = unknown ; then UNAME_PROCESSOR=powerpc fi if test "`echo "$UNAME_RELEASE" | sed -e 's/\..*//'`" -le 10 ; then if [ "$CC_FOR_BUILD" != no_compiler_found ]; then if (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \ (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ grep IS_64BIT_ARCH >/dev/null then case $UNAME_PROCESSOR in i386) UNAME_PROCESSOR=x86_64 ;; powerpc) UNAME_PROCESSOR=powerpc64 ;; esac fi # On 10.4-10.6 one might compile for PowerPC via gcc -arch ppc if (echo '#ifdef __POWERPC__'; echo IS_PPC; echo '#endif') | \ (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ grep IS_PPC >/dev/null then UNAME_PROCESSOR=powerpc fi fi elif test "$UNAME_PROCESSOR" = i386 ; then # Avoid executing cc on OS X 10.9, as it ships with a stub # that puts up a graphical alert prompting to install # developer tools. Any system running Mac OS X 10.7 or # later (Darwin 11 and later) is required to have a 64-bit # processor. This is not true of the ARM version of Darwin # that Apple uses in portable devices. UNAME_PROCESSOR=x86_64 fi echo "$UNAME_PROCESSOR"-apple-darwin"$UNAME_RELEASE" exit ;; *:procnto*:*:* | *:QNX:[0123456789]*:*) UNAME_PROCESSOR=`uname -p` if test "$UNAME_PROCESSOR" = x86; then UNAME_PROCESSOR=i386 UNAME_MACHINE=pc fi echo "$UNAME_PROCESSOR"-"$UNAME_MACHINE"-nto-qnx"$UNAME_RELEASE" exit ;; *:QNX:*:4*) echo i386-pc-qnx exit ;; NEO-*:NONSTOP_KERNEL:*:*) echo neo-tandem-nsk"$UNAME_RELEASE" exit ;; NSE-*:NONSTOP_KERNEL:*:*) echo nse-tandem-nsk"$UNAME_RELEASE" exit ;; NSR-*:NONSTOP_KERNEL:*:*) echo nsr-tandem-nsk"$UNAME_RELEASE" exit ;; NSV-*:NONSTOP_KERNEL:*:*) echo nsv-tandem-nsk"$UNAME_RELEASE" exit ;; NSX-*:NONSTOP_KERNEL:*:*) echo nsx-tandem-nsk"$UNAME_RELEASE" exit ;; *:NonStop-UX:*:*) echo mips-compaq-nonstopux exit ;; BS2000:POSIX*:*:*) echo bs2000-siemens-sysv exit ;; DS/*:UNIX_System_V:*:*) echo "$UNAME_MACHINE"-"$UNAME_SYSTEM"-"$UNAME_RELEASE" exit ;; *:Plan9:*:*) # "uname -m" is not consistent, so use $cputype instead. 386 # is converted to i386 for consistency with other x86 # operating systems. if test "$cputype" = 386; then UNAME_MACHINE=i386 else UNAME_MACHINE="$cputype" fi echo "$UNAME_MACHINE"-unknown-plan9 exit ;; *:TOPS-10:*:*) echo pdp10-unknown-tops10 exit ;; *:TENEX:*:*) echo pdp10-unknown-tenex exit ;; KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*) echo pdp10-dec-tops20 exit ;; XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*) echo pdp10-xkl-tops20 exit ;; *:TOPS-20:*:*) echo pdp10-unknown-tops20 exit ;; *:ITS:*:*) echo pdp10-unknown-its exit ;; SEI:*:*:SEIUX) echo mips-sei-seiux"$UNAME_RELEASE" exit ;; *:DragonFly:*:*) echo "$UNAME_MACHINE"-unknown-dragonfly"`echo "$UNAME_RELEASE"|sed -e 's/[-(].*//'`" exit ;; *:*VMS:*:*) UNAME_MACHINE=`(uname -p) 2>/dev/null` case "$UNAME_MACHINE" in A*) echo alpha-dec-vms ; exit ;; I*) echo ia64-dec-vms ; exit ;; V*) echo vax-dec-vms ; exit ;; esac ;; *:XENIX:*:SysV) echo i386-pc-xenix exit ;; i*86:skyos:*:*) echo "$UNAME_MACHINE"-pc-skyos"`echo "$UNAME_RELEASE" | sed -e 's/ .*$//'`" exit ;; i*86:rdos:*:*) echo "$UNAME_MACHINE"-pc-rdos exit ;; i*86:AROS:*:*) echo "$UNAME_MACHINE"-pc-aros exit ;; x86_64:VMkernel:*:*) echo "$UNAME_MACHINE"-unknown-esx exit ;; amd64:Isilon\ OneFS:*:*) echo x86_64-unknown-onefs exit ;; esac echo "$0: unable to guess system type" >&2 case "$UNAME_MACHINE:$UNAME_SYSTEM" in mips:Linux | mips64:Linux) # If we got here on MIPS GNU/Linux, output extra information. cat >&2 <&2 </dev/null || echo unknown` uname -r = `(uname -r) 2>/dev/null || echo unknown` uname -s = `(uname -s) 2>/dev/null || echo unknown` uname -v = `(uname -v) 2>/dev/null || echo unknown` /usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null` /bin/uname -X = `(/bin/uname -X) 2>/dev/null` hostinfo = `(hostinfo) 2>/dev/null` /bin/universe = `(/bin/universe) 2>/dev/null` /usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null` /bin/arch = `(/bin/arch) 2>/dev/null` /usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null` /usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null` UNAME_MACHINE = "$UNAME_MACHINE" UNAME_RELEASE = "$UNAME_RELEASE" UNAME_SYSTEM = "$UNAME_SYSTEM" UNAME_VERSION = "$UNAME_VERSION" EOF exit 1 # Local variables: # eval: (add-hook 'write-file-functions 'time-stamp) # time-stamp-start: "timestamp='" # time-stamp-format: "%:y-%02m-%02d" # time-stamp-end: "'" # End: capnproto-c++-0.8.0/build-aux/config.sub0000755000175000017500000010645013650317755020647 0ustar00kentonkenton00000000000000#! /bin/sh # Configuration validation subroutine script. # Copyright 1992-2018 Free Software Foundation, Inc. timestamp='2018-02-22' # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . # # As a special exception to the GNU General Public License, if you # distribute this file as part of a program that contains a # configuration script generated by Autoconf, you may include it under # the same distribution terms that you use for the rest of that # program. This Exception is an additional permission under section 7 # of the GNU General Public License, version 3 ("GPLv3"). # Please send patches to . # # Configuration subroutine to validate and canonicalize a configuration type. # Supply the specified configuration type as an argument. # If it is invalid, we print an error message on stderr and exit with code 1. # Otherwise, we print the canonical config type on stdout and succeed. # You can get the latest version of this script from: # https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub # This file is supposed to be the same for all GNU packages # and recognize all the CPU types, system types and aliases # that are meaningful with *any* GNU software. # Each package is responsible for reporting which valid configurations # it does not support. The user should be able to distinguish # a failure to support a valid configuration from a meaningless # configuration. # The goal of this file is to map all the various variations of a given # machine specification into a single specification in the form: # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM # or in some cases, the newer four-part form: # CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM # It is wrong to echo any other type of specification. me=`echo "$0" | sed -e 's,.*/,,'` usage="\ Usage: $0 [OPTION] CPU-MFR-OPSYS or ALIAS Canonicalize a configuration name. Options: -h, --help print this help, then exit -t, --time-stamp print date of last modification, then exit -v, --version print version number, then exit Report bugs and patches to ." version="\ GNU config.sub ($timestamp) Copyright 1992-2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." help=" Try \`$me --help' for more information." # Parse command line while test $# -gt 0 ; do case $1 in --time-stamp | --time* | -t ) echo "$timestamp" ; exit ;; --version | -v ) echo "$version" ; exit ;; --help | --h* | -h ) echo "$usage"; exit ;; -- ) # Stop option processing shift; break ;; - ) # Use stdin as input. break ;; -* ) echo "$me: invalid option $1$help" exit 1 ;; *local*) # First pass through any local machine types. echo "$1" exit ;; * ) break ;; esac done case $# in 0) echo "$me: missing argument$help" >&2 exit 1;; 1) ;; *) echo "$me: too many arguments$help" >&2 exit 1;; esac # Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any). # Here we must recognize all the valid KERNEL-OS combinations. maybe_os=`echo "$1" | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\2/'` case $maybe_os in nto-qnx* | linux-gnu* | linux-android* | linux-dietlibc | linux-newlib* | \ linux-musl* | linux-uclibc* | uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | \ knetbsd*-gnu* | netbsd*-gnu* | netbsd*-eabi* | \ kopensolaris*-gnu* | cloudabi*-eabi* | \ storm-chaos* | os2-emx* | rtmk-nova*) os=-$maybe_os basic_machine=`echo "$1" | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'` ;; android-linux) os=-linux-android basic_machine=`echo "$1" | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'`-unknown ;; *) basic_machine=`echo "$1" | sed 's/-[^-]*$//'` if [ "$basic_machine" != "$1" ] then os=`echo "$1" | sed 's/.*-/-/'` else os=; fi ;; esac ### Let's recognize common machines as not being operating systems so ### that things like config.sub decstation-3100 work. We also ### recognize some manufacturers as not being operating systems, so we ### can provide default operating systems below. case $os in -sun*os*) # Prevent following clause from handling this invalid input. ;; -dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \ -att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \ -unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \ -convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\ -c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \ -harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \ -apple | -axis | -knuth | -cray | -microblaze*) os= basic_machine=$1 ;; -bluegene*) os=-cnk ;; -sim | -cisco | -oki | -wec | -winbond) os= basic_machine=$1 ;; -scout) ;; -wrs) os=-vxworks basic_machine=$1 ;; -chorusos*) os=-chorusos basic_machine=$1 ;; -chorusrdb) os=-chorusrdb basic_machine=$1 ;; -hiux*) os=-hiuxwe2 ;; -sco6) os=-sco5v6 basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -sco5) os=-sco3.2v5 basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -sco4) os=-sco3.2v4 basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -sco3.2.[4-9]*) os=`echo $os | sed -e 's/sco3.2./sco3.2v/'` basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -sco3.2v[4-9]*) # Don't forget version if it is 3.2v4 or newer. basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -sco5v6*) # Don't forget version if it is 3.2v4 or newer. basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -sco*) os=-sco3.2v2 basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -udk*) basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -isc) os=-isc2.2 basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -clix*) basic_machine=clipper-intergraph ;; -isc*) basic_machine=`echo "$1" | sed -e 's/86-.*/86-pc/'` ;; -lynx*178) os=-lynxos178 ;; -lynx*5) os=-lynxos5 ;; -lynx*) os=-lynxos ;; -ptx*) basic_machine=`echo "$1" | sed -e 's/86-.*/86-sequent/'` ;; -psos*) os=-psos ;; -mint | -mint[0-9]*) basic_machine=m68k-atari os=-mint ;; esac # Decode aliases for certain CPU-COMPANY combinations. case $basic_machine in # Recognize the basic CPU types without company name. # Some are omitted here because they have special meanings below. 1750a | 580 \ | a29k \ | aarch64 | aarch64_be \ | alpha | alphaev[4-8] | alphaev56 | alphaev6[78] | alphapca5[67] \ | alpha64 | alpha64ev[4-8] | alpha64ev56 | alpha64ev6[78] | alpha64pca5[67] \ | am33_2.0 \ | arc | arceb \ | arm | arm[bl]e | arme[lb] | armv[2-8] | armv[3-8][lb] | armv7[arm] \ | avr | avr32 \ | ba \ | be32 | be64 \ | bfin \ | c4x | c8051 | clipper \ | d10v | d30v | dlx | dsp16xx \ | e2k | epiphany \ | fido | fr30 | frv | ft32 \ | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \ | hexagon \ | i370 | i860 | i960 | ia16 | ia64 \ | ip2k | iq2000 \ | k1om \ | le32 | le64 \ | lm32 \ | m32c | m32r | m32rle | m68000 | m68k | m88k \ | maxq | mb | microblaze | microblazeel | mcore | mep | metag \ | mips | mipsbe | mipseb | mipsel | mipsle \ | mips16 \ | mips64 | mips64el \ | mips64octeon | mips64octeonel \ | mips64orion | mips64orionel \ | mips64r5900 | mips64r5900el \ | mips64vr | mips64vrel \ | mips64vr4100 | mips64vr4100el \ | mips64vr4300 | mips64vr4300el \ | mips64vr5000 | mips64vr5000el \ | mips64vr5900 | mips64vr5900el \ | mipsisa32 | mipsisa32el \ | mipsisa32r2 | mipsisa32r2el \ | mipsisa32r6 | mipsisa32r6el \ | mipsisa64 | mipsisa64el \ | mipsisa64r2 | mipsisa64r2el \ | mipsisa64r6 | mipsisa64r6el \ | mipsisa64sb1 | mipsisa64sb1el \ | mipsisa64sr71k | mipsisa64sr71kel \ | mipsr5900 | mipsr5900el \ | mipstx39 | mipstx39el \ | mn10200 | mn10300 \ | moxie \ | mt \ | msp430 \ | nds32 | nds32le | nds32be \ | nios | nios2 | nios2eb | nios2el \ | ns16k | ns32k \ | open8 | or1k | or1knd | or32 \ | pdp10 | pj | pjl \ | powerpc | powerpc64 | powerpc64le | powerpcle \ | pru \ | pyramid \ | riscv32 | riscv64 \ | rl78 | rx \ | score \ | sh | sh[1234] | sh[24]a | sh[24]aeb | sh[23]e | sh[234]eb | sheb | shbe | shle | sh[1234]le | sh3ele \ | sh64 | sh64le \ | sparc | sparc64 | sparc64b | sparc64v | sparc86x | sparclet | sparclite \ | sparcv8 | sparcv9 | sparcv9b | sparcv9v \ | spu \ | tahoe | tic4x | tic54x | tic55x | tic6x | tic80 | tron \ | ubicom32 \ | v850 | v850e | v850e1 | v850e2 | v850es | v850e2v3 \ | visium \ | wasm32 \ | x86 | xc16x | xstormy16 | xtensa \ | z8k | z80) basic_machine=$basic_machine-unknown ;; c54x) basic_machine=tic54x-unknown ;; c55x) basic_machine=tic55x-unknown ;; c6x) basic_machine=tic6x-unknown ;; leon|leon[3-9]) basic_machine=sparc-$basic_machine ;; m6811 | m68hc11 | m6812 | m68hc12 | m68hcs12x | nvptx | picochip) basic_machine=$basic_machine-unknown os=-none ;; m88110 | m680[12346]0 | m683?2 | m68360 | m5200 | v70 | w65) ;; ms1) basic_machine=mt-unknown ;; strongarm | thumb | xscale) basic_machine=arm-unknown ;; xgate) basic_machine=$basic_machine-unknown os=-none ;; xscaleeb) basic_machine=armeb-unknown ;; xscaleel) basic_machine=armel-unknown ;; # We use `pc' rather than `unknown' # because (1) that's what they normally are, and # (2) the word "unknown" tends to confuse beginning users. i*86 | x86_64) basic_machine=$basic_machine-pc ;; # Object if more than one company name word. *-*-*) echo Invalid configuration \`"$1"\': machine \`"$basic_machine"\' not recognized 1>&2 exit 1 ;; # Recognize the basic CPU types with company name. 580-* \ | a29k-* \ | aarch64-* | aarch64_be-* \ | alpha-* | alphaev[4-8]-* | alphaev56-* | alphaev6[78]-* \ | alpha64-* | alpha64ev[4-8]-* | alpha64ev56-* | alpha64ev6[78]-* \ | alphapca5[67]-* | alpha64pca5[67]-* | arc-* | arceb-* \ | arm-* | armbe-* | armle-* | armeb-* | armv*-* \ | avr-* | avr32-* \ | ba-* \ | be32-* | be64-* \ | bfin-* | bs2000-* \ | c[123]* | c30-* | [cjt]90-* | c4x-* \ | c8051-* | clipper-* | craynv-* | cydra-* \ | d10v-* | d30v-* | dlx-* \ | e2k-* | elxsi-* \ | f30[01]-* | f700-* | fido-* | fr30-* | frv-* | fx80-* \ | h8300-* | h8500-* \ | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \ | hexagon-* \ | i*86-* | i860-* | i960-* | ia16-* | ia64-* \ | ip2k-* | iq2000-* \ | k1om-* \ | le32-* | le64-* \ | lm32-* \ | m32c-* | m32r-* | m32rle-* \ | m68000-* | m680[012346]0-* | m68360-* | m683?2-* | m68k-* \ | m88110-* | m88k-* | maxq-* | mcore-* | metag-* \ | microblaze-* | microblazeel-* \ | mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \ | mips16-* \ | mips64-* | mips64el-* \ | mips64octeon-* | mips64octeonel-* \ | mips64orion-* | mips64orionel-* \ | mips64r5900-* | mips64r5900el-* \ | mips64vr-* | mips64vrel-* \ | mips64vr4100-* | mips64vr4100el-* \ | mips64vr4300-* | mips64vr4300el-* \ | mips64vr5000-* | mips64vr5000el-* \ | mips64vr5900-* | mips64vr5900el-* \ | mipsisa32-* | mipsisa32el-* \ | mipsisa32r2-* | mipsisa32r2el-* \ | mipsisa32r6-* | mipsisa32r6el-* \ | mipsisa64-* | mipsisa64el-* \ | mipsisa64r2-* | mipsisa64r2el-* \ | mipsisa64r6-* | mipsisa64r6el-* \ | mipsisa64sb1-* | mipsisa64sb1el-* \ | mipsisa64sr71k-* | mipsisa64sr71kel-* \ | mipsr5900-* | mipsr5900el-* \ | mipstx39-* | mipstx39el-* \ | mmix-* \ | mt-* \ | msp430-* \ | nds32-* | nds32le-* | nds32be-* \ | nios-* | nios2-* | nios2eb-* | nios2el-* \ | none-* | np1-* | ns16k-* | ns32k-* \ | open8-* \ | or1k*-* \ | orion-* \ | pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \ | powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* \ | pru-* \ | pyramid-* \ | riscv32-* | riscv64-* \ | rl78-* | romp-* | rs6000-* | rx-* \ | sh-* | sh[1234]-* | sh[24]a-* | sh[24]aeb-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \ | shle-* | sh[1234]le-* | sh3ele-* | sh64-* | sh64le-* \ | sparc-* | sparc64-* | sparc64b-* | sparc64v-* | sparc86x-* | sparclet-* \ | sparclite-* \ | sparcv8-* | sparcv9-* | sparcv9b-* | sparcv9v-* | sv1-* | sx*-* \ | tahoe-* \ | tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \ | tile*-* \ | tron-* \ | ubicom32-* \ | v850-* | v850e-* | v850e1-* | v850es-* | v850e2-* | v850e2v3-* \ | vax-* \ | visium-* \ | wasm32-* \ | we32k-* \ | x86-* | x86_64-* | xc16x-* | xps100-* \ | xstormy16-* | xtensa*-* \ | ymp-* \ | z8k-* | z80-*) ;; # Recognize the basic CPU types without company name, with glob match. xtensa*) basic_machine=$basic_machine-unknown ;; # Recognize the various machine names and aliases which stand # for a CPU type and a company and sometimes even an OS. 386bsd) basic_machine=i386-pc os=-bsd ;; 3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc) basic_machine=m68000-att ;; 3b*) basic_machine=we32k-att ;; a29khif) basic_machine=a29k-amd os=-udi ;; abacus) basic_machine=abacus-unknown ;; adobe68k) basic_machine=m68010-adobe os=-scout ;; alliant | fx80) basic_machine=fx80-alliant ;; altos | altos3068) basic_machine=m68k-altos ;; am29k) basic_machine=a29k-none os=-bsd ;; amd64) basic_machine=x86_64-pc ;; amd64-*) basic_machine=x86_64-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; amdahl) basic_machine=580-amdahl os=-sysv ;; amiga | amiga-*) basic_machine=m68k-unknown ;; amigaos | amigados) basic_machine=m68k-unknown os=-amigaos ;; amigaunix | amix) basic_machine=m68k-unknown os=-sysv4 ;; apollo68) basic_machine=m68k-apollo os=-sysv ;; apollo68bsd) basic_machine=m68k-apollo os=-bsd ;; aros) basic_machine=i386-pc os=-aros ;; asmjs) basic_machine=asmjs-unknown ;; aux) basic_machine=m68k-apple os=-aux ;; balance) basic_machine=ns32k-sequent os=-dynix ;; blackfin) basic_machine=bfin-unknown os=-linux ;; blackfin-*) basic_machine=bfin-`echo "$basic_machine" | sed 's/^[^-]*-//'` os=-linux ;; bluegene*) basic_machine=powerpc-ibm os=-cnk ;; c54x-*) basic_machine=tic54x-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; c55x-*) basic_machine=tic55x-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; c6x-*) basic_machine=tic6x-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; c90) basic_machine=c90-cray os=-unicos ;; cegcc) basic_machine=arm-unknown os=-cegcc ;; convex-c1) basic_machine=c1-convex os=-bsd ;; convex-c2) basic_machine=c2-convex os=-bsd ;; convex-c32) basic_machine=c32-convex os=-bsd ;; convex-c34) basic_machine=c34-convex os=-bsd ;; convex-c38) basic_machine=c38-convex os=-bsd ;; cray | j90) basic_machine=j90-cray os=-unicos ;; craynv) basic_machine=craynv-cray os=-unicosmp ;; cr16 | cr16-*) basic_machine=cr16-unknown os=-elf ;; crds | unos) basic_machine=m68k-crds ;; crisv32 | crisv32-* | etraxfs*) basic_machine=crisv32-axis ;; cris | cris-* | etrax*) basic_machine=cris-axis ;; crx) basic_machine=crx-unknown os=-elf ;; da30 | da30-*) basic_machine=m68k-da30 ;; decstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn) basic_machine=mips-dec ;; decsystem10* | dec10*) basic_machine=pdp10-dec os=-tops10 ;; decsystem20* | dec20*) basic_machine=pdp10-dec os=-tops20 ;; delta | 3300 | motorola-3300 | motorola-delta \ | 3300-motorola | delta-motorola) basic_machine=m68k-motorola ;; delta88) basic_machine=m88k-motorola os=-sysv3 ;; dicos) basic_machine=i686-pc os=-dicos ;; djgpp) basic_machine=i586-pc os=-msdosdjgpp ;; dpx20 | dpx20-*) basic_machine=rs6000-bull os=-bosx ;; dpx2*) basic_machine=m68k-bull os=-sysv3 ;; e500v[12]) basic_machine=powerpc-unknown os=$os"spe" ;; e500v[12]-*) basic_machine=powerpc-`echo "$basic_machine" | sed 's/^[^-]*-//'` os=$os"spe" ;; ebmon29k) basic_machine=a29k-amd os=-ebmon ;; elxsi) basic_machine=elxsi-elxsi os=-bsd ;; encore | umax | mmax) basic_machine=ns32k-encore ;; es1800 | OSE68k | ose68k | ose | OSE) basic_machine=m68k-ericsson os=-ose ;; fx2800) basic_machine=i860-alliant ;; genix) basic_machine=ns32k-ns ;; gmicro) basic_machine=tron-gmicro os=-sysv ;; go32) basic_machine=i386-pc os=-go32 ;; h3050r* | hiux*) basic_machine=hppa1.1-hitachi os=-hiuxwe2 ;; h8300hms) basic_machine=h8300-hitachi os=-hms ;; h8300xray) basic_machine=h8300-hitachi os=-xray ;; h8500hms) basic_machine=h8500-hitachi os=-hms ;; harris) basic_machine=m88k-harris os=-sysv3 ;; hp300-*) basic_machine=m68k-hp ;; hp300bsd) basic_machine=m68k-hp os=-bsd ;; hp300hpux) basic_machine=m68k-hp os=-hpux ;; hp3k9[0-9][0-9] | hp9[0-9][0-9]) basic_machine=hppa1.0-hp ;; hp9k2[0-9][0-9] | hp9k31[0-9]) basic_machine=m68000-hp ;; hp9k3[2-9][0-9]) basic_machine=m68k-hp ;; hp9k6[0-9][0-9] | hp6[0-9][0-9]) basic_machine=hppa1.0-hp ;; hp9k7[0-79][0-9] | hp7[0-79][0-9]) basic_machine=hppa1.1-hp ;; hp9k78[0-9] | hp78[0-9]) # FIXME: really hppa2.0-hp basic_machine=hppa1.1-hp ;; hp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893) # FIXME: really hppa2.0-hp basic_machine=hppa1.1-hp ;; hp9k8[0-9][13679] | hp8[0-9][13679]) basic_machine=hppa1.1-hp ;; hp9k8[0-9][0-9] | hp8[0-9][0-9]) basic_machine=hppa1.0-hp ;; hppaosf) basic_machine=hppa1.1-hp os=-osf ;; hppro) basic_machine=hppa1.1-hp os=-proelf ;; i370-ibm* | ibm*) basic_machine=i370-ibm ;; i*86v32) basic_machine=`echo "$1" | sed -e 's/86.*/86-pc/'` os=-sysv32 ;; i*86v4*) basic_machine=`echo "$1" | sed -e 's/86.*/86-pc/'` os=-sysv4 ;; i*86v) basic_machine=`echo "$1" | sed -e 's/86.*/86-pc/'` os=-sysv ;; i*86sol2) basic_machine=`echo "$1" | sed -e 's/86.*/86-pc/'` os=-solaris2 ;; i386mach) basic_machine=i386-mach os=-mach ;; vsta) basic_machine=i386-unknown os=-vsta ;; iris | iris4d) basic_machine=mips-sgi case $os in -irix*) ;; *) os=-irix4 ;; esac ;; isi68 | isi) basic_machine=m68k-isi os=-sysv ;; leon-*|leon[3-9]-*) basic_machine=sparc-`echo "$basic_machine" | sed 's/-.*//'` ;; m68knommu) basic_machine=m68k-unknown os=-linux ;; m68knommu-*) basic_machine=m68k-`echo "$basic_machine" | sed 's/^[^-]*-//'` os=-linux ;; magnum | m3230) basic_machine=mips-mips os=-sysv ;; merlin) basic_machine=ns32k-utek os=-sysv ;; microblaze*) basic_machine=microblaze-xilinx ;; mingw64) basic_machine=x86_64-pc os=-mingw64 ;; mingw32) basic_machine=i686-pc os=-mingw32 ;; mingw32ce) basic_machine=arm-unknown os=-mingw32ce ;; miniframe) basic_machine=m68000-convergent ;; *mint | -mint[0-9]* | *MiNT | *MiNT[0-9]*) basic_machine=m68k-atari os=-mint ;; mips3*-*) basic_machine=`echo "$basic_machine" | sed -e 's/mips3/mips64/'` ;; mips3*) basic_machine=`echo "$basic_machine" | sed -e 's/mips3/mips64/'`-unknown ;; monitor) basic_machine=m68k-rom68k os=-coff ;; morphos) basic_machine=powerpc-unknown os=-morphos ;; moxiebox) basic_machine=moxie-unknown os=-moxiebox ;; msdos) basic_machine=i386-pc os=-msdos ;; ms1-*) basic_machine=`echo "$basic_machine" | sed -e 's/ms1-/mt-/'` ;; msys) basic_machine=i686-pc os=-msys ;; mvs) basic_machine=i370-ibm os=-mvs ;; nacl) basic_machine=le32-unknown os=-nacl ;; ncr3000) basic_machine=i486-ncr os=-sysv4 ;; netbsd386) basic_machine=i386-unknown os=-netbsd ;; netwinder) basic_machine=armv4l-rebel os=-linux ;; news | news700 | news800 | news900) basic_machine=m68k-sony os=-newsos ;; news1000) basic_machine=m68030-sony os=-newsos ;; news-3600 | risc-news) basic_machine=mips-sony os=-newsos ;; necv70) basic_machine=v70-nec os=-sysv ;; next | m*-next) basic_machine=m68k-next case $os in -nextstep* ) ;; -ns2*) os=-nextstep2 ;; *) os=-nextstep3 ;; esac ;; nh3000) basic_machine=m68k-harris os=-cxux ;; nh[45]000) basic_machine=m88k-harris os=-cxux ;; nindy960) basic_machine=i960-intel os=-nindy ;; mon960) basic_machine=i960-intel os=-mon960 ;; nonstopux) basic_machine=mips-compaq os=-nonstopux ;; np1) basic_machine=np1-gould ;; neo-tandem) basic_machine=neo-tandem ;; nse-tandem) basic_machine=nse-tandem ;; nsr-tandem) basic_machine=nsr-tandem ;; nsv-tandem) basic_machine=nsv-tandem ;; nsx-tandem) basic_machine=nsx-tandem ;; op50n-* | op60c-*) basic_machine=hppa1.1-oki os=-proelf ;; openrisc | openrisc-*) basic_machine=or32-unknown ;; os400) basic_machine=powerpc-ibm os=-os400 ;; OSE68000 | ose68000) basic_machine=m68000-ericsson os=-ose ;; os68k) basic_machine=m68k-none os=-os68k ;; pa-hitachi) basic_machine=hppa1.1-hitachi os=-hiuxwe2 ;; paragon) basic_machine=i860-intel os=-osf ;; parisc) basic_machine=hppa-unknown os=-linux ;; parisc-*) basic_machine=hppa-`echo "$basic_machine" | sed 's/^[^-]*-//'` os=-linux ;; pbd) basic_machine=sparc-tti ;; pbb) basic_machine=m68k-tti ;; pc532 | pc532-*) basic_machine=ns32k-pc532 ;; pc98) basic_machine=i386-pc ;; pc98-*) basic_machine=i386-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; pentium | p5 | k5 | k6 | nexgen | viac3) basic_machine=i586-pc ;; pentiumpro | p6 | 6x86 | athlon | athlon_*) basic_machine=i686-pc ;; pentiumii | pentium2 | pentiumiii | pentium3) basic_machine=i686-pc ;; pentium4) basic_machine=i786-pc ;; pentium-* | p5-* | k5-* | k6-* | nexgen-* | viac3-*) basic_machine=i586-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; pentiumpro-* | p6-* | 6x86-* | athlon-*) basic_machine=i686-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; pentiumii-* | pentium2-* | pentiumiii-* | pentium3-*) basic_machine=i686-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; pentium4-*) basic_machine=i786-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; pn) basic_machine=pn-gould ;; power) basic_machine=power-ibm ;; ppc | ppcbe) basic_machine=powerpc-unknown ;; ppc-* | ppcbe-*) basic_machine=powerpc-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; ppcle | powerpclittle) basic_machine=powerpcle-unknown ;; ppcle-* | powerpclittle-*) basic_machine=powerpcle-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; ppc64) basic_machine=powerpc64-unknown ;; ppc64-*) basic_machine=powerpc64-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; ppc64le | powerpc64little) basic_machine=powerpc64le-unknown ;; ppc64le-* | powerpc64little-*) basic_machine=powerpc64le-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; ps2) basic_machine=i386-ibm ;; pw32) basic_machine=i586-unknown os=-pw32 ;; rdos | rdos64) basic_machine=x86_64-pc os=-rdos ;; rdos32) basic_machine=i386-pc os=-rdos ;; rom68k) basic_machine=m68k-rom68k os=-coff ;; rm[46]00) basic_machine=mips-siemens ;; rtpc | rtpc-*) basic_machine=romp-ibm ;; s390 | s390-*) basic_machine=s390-ibm ;; s390x | s390x-*) basic_machine=s390x-ibm ;; sa29200) basic_machine=a29k-amd os=-udi ;; sb1) basic_machine=mipsisa64sb1-unknown ;; sb1el) basic_machine=mipsisa64sb1el-unknown ;; sde) basic_machine=mipsisa32-sde os=-elf ;; sei) basic_machine=mips-sei os=-seiux ;; sequent) basic_machine=i386-sequent ;; sh5el) basic_machine=sh5le-unknown ;; simso-wrs) basic_machine=sparclite-wrs os=-vxworks ;; sps7) basic_machine=m68k-bull os=-sysv2 ;; spur) basic_machine=spur-unknown ;; st2000) basic_machine=m68k-tandem ;; stratus) basic_machine=i860-stratus os=-sysv4 ;; strongarm-* | thumb-*) basic_machine=arm-`echo "$basic_machine" | sed 's/^[^-]*-//'` ;; sun2) basic_machine=m68000-sun ;; sun2os3) basic_machine=m68000-sun os=-sunos3 ;; sun2os4) basic_machine=m68000-sun os=-sunos4 ;; sun3os3) basic_machine=m68k-sun os=-sunos3 ;; sun3os4) basic_machine=m68k-sun os=-sunos4 ;; sun4os3) basic_machine=sparc-sun os=-sunos3 ;; sun4os4) basic_machine=sparc-sun os=-sunos4 ;; sun4sol2) basic_machine=sparc-sun os=-solaris2 ;; sun3 | sun3-*) basic_machine=m68k-sun ;; sun4) basic_machine=sparc-sun ;; sun386 | sun386i | roadrunner) basic_machine=i386-sun ;; sv1) basic_machine=sv1-cray os=-unicos ;; symmetry) basic_machine=i386-sequent os=-dynix ;; t3e) basic_machine=alphaev5-cray os=-unicos ;; t90) basic_machine=t90-cray os=-unicos ;; tile*) basic_machine=$basic_machine-unknown os=-linux-gnu ;; tx39) basic_machine=mipstx39-unknown ;; tx39el) basic_machine=mipstx39el-unknown ;; toad1) basic_machine=pdp10-xkl os=-tops20 ;; tower | tower-32) basic_machine=m68k-ncr ;; tpf) basic_machine=s390x-ibm os=-tpf ;; udi29k) basic_machine=a29k-amd os=-udi ;; ultra3) basic_machine=a29k-nyu os=-sym1 ;; v810 | necv810) basic_machine=v810-nec os=-none ;; vaxv) basic_machine=vax-dec os=-sysv ;; vms) basic_machine=vax-dec os=-vms ;; vpp*|vx|vx-*) basic_machine=f301-fujitsu ;; vxworks960) basic_machine=i960-wrs os=-vxworks ;; vxworks68) basic_machine=m68k-wrs os=-vxworks ;; vxworks29k) basic_machine=a29k-wrs os=-vxworks ;; w65*) basic_machine=w65-wdc os=-none ;; w89k-*) basic_machine=hppa1.1-winbond os=-proelf ;; x64) basic_machine=x86_64-pc ;; xbox) basic_machine=i686-pc os=-mingw32 ;; xps | xps100) basic_machine=xps100-honeywell ;; xscale-* | xscalee[bl]-*) basic_machine=`echo "$basic_machine" | sed 's/^xscale/arm/'` ;; ymp) basic_machine=ymp-cray os=-unicos ;; none) basic_machine=none-none os=-none ;; # Here we handle the default manufacturer of certain CPU types. It is in # some cases the only manufacturer, in others, it is the most popular. w89k) basic_machine=hppa1.1-winbond ;; op50n) basic_machine=hppa1.1-oki ;; op60c) basic_machine=hppa1.1-oki ;; romp) basic_machine=romp-ibm ;; mmix) basic_machine=mmix-knuth ;; rs6000) basic_machine=rs6000-ibm ;; vax) basic_machine=vax-dec ;; pdp11) basic_machine=pdp11-dec ;; we32k) basic_machine=we32k-att ;; sh[1234] | sh[24]a | sh[24]aeb | sh[34]eb | sh[1234]le | sh[23]ele) basic_machine=sh-unknown ;; cydra) basic_machine=cydra-cydrome ;; orion) basic_machine=orion-highlevel ;; orion105) basic_machine=clipper-highlevel ;; mac | mpw | mac-mpw) basic_machine=m68k-apple ;; pmac | pmac-mpw) basic_machine=powerpc-apple ;; *-unknown) # Make sure to match an already-canonicalized machine name. ;; *) echo Invalid configuration \`"$1"\': machine \`"$basic_machine"\' not recognized 1>&2 exit 1 ;; esac # Here we canonicalize certain aliases for manufacturers. case $basic_machine in *-digital*) basic_machine=`echo "$basic_machine" | sed 's/digital.*/dec/'` ;; *-commodore*) basic_machine=`echo "$basic_machine" | sed 's/commodore.*/cbm/'` ;; *) ;; esac # Decode manufacturer-specific aliases for certain operating systems. if [ x"$os" != x"" ] then case $os in # First match some system type aliases that might get confused # with valid system types. # -solaris* is a basic system type, with this one exception. -auroraux) os=-auroraux ;; -solaris1 | -solaris1.*) os=`echo $os | sed -e 's|solaris1|sunos4|'` ;; -solaris) os=-solaris2 ;; -unixware*) os=-sysv4.2uw ;; -gnu/linux*) os=`echo $os | sed -e 's|gnu/linux|linux-gnu|'` ;; # es1800 is here to avoid being matched by es* (a different OS) -es1800*) os=-ose ;; # Now accept the basic system types. # The portable systems comes first. # Each alternative MUST end in a * to match a version number. # -sysv* is not here because it comes later, after sysvr4. -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \ | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\ | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \ | -sym* | -kopensolaris* | -plan9* \ | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \ | -aos* | -aros* | -cloudabi* | -sortix* \ | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \ | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \ | -hiux* | -knetbsd* | -mirbsd* | -netbsd* \ | -bitrig* | -openbsd* | -solidbsd* | -libertybsd* \ | -ekkobsd* | -kfreebsd* | -freebsd* | -riscix* | -lynxos* \ | -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \ | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \ | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \ | -chorusos* | -chorusrdb* | -cegcc* | -glidix* \ | -cygwin* | -msys* | -pe* | -psos* | -moss* | -proelf* | -rtems* \ | -midipix* | -mingw32* | -mingw64* | -linux-gnu* | -linux-android* \ | -linux-newlib* | -linux-musl* | -linux-uclibc* \ | -uxpv* | -beos* | -mpeix* | -udk* | -moxiebox* \ | -interix* | -uwin* | -mks* | -rhapsody* | -darwin* \ | -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \ | -storm-chaos* | -tops10* | -tenex* | -tops20* | -its* \ | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \ | -morphos* | -superux* | -rtmk* | -windiss* \ | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \ | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es* \ | -onefs* | -tirtos* | -phoenix* | -fuchsia* | -redox* | -bme* \ | -midnightbsd*) # Remember, each alternative MUST END IN *, to match a version number. ;; -qnx*) case $basic_machine in x86-* | i*86-*) ;; *) os=-nto$os ;; esac ;; -nto-qnx*) ;; -nto*) os=`echo $os | sed -e 's|nto|nto-qnx|'` ;; -sim | -xray | -os68k* | -v88r* \ | -windows* | -osx | -abug | -netware* | -os9* \ | -macos* | -mpw* | -magic* | -mmixware* | -mon960* | -lnews*) ;; -mac*) os=`echo "$os" | sed -e 's|mac|macos|'` ;; -linux-dietlibc) os=-linux-dietlibc ;; -linux*) os=`echo $os | sed -e 's|linux|linux-gnu|'` ;; -sunos5*) os=`echo "$os" | sed -e 's|sunos5|solaris2|'` ;; -sunos6*) os=`echo "$os" | sed -e 's|sunos6|solaris3|'` ;; -opened*) os=-openedition ;; -os400*) os=-os400 ;; -wince*) os=-wince ;; -utek*) os=-bsd ;; -dynix*) os=-bsd ;; -acis*) os=-aos ;; -atheos*) os=-atheos ;; -syllable*) os=-syllable ;; -386bsd) os=-bsd ;; -ctix* | -uts*) os=-sysv ;; -nova*) os=-rtmk-nova ;; -ns2) os=-nextstep2 ;; -nsk*) os=-nsk ;; # Preserve the version number of sinix5. -sinix5.*) os=`echo $os | sed -e 's|sinix|sysv|'` ;; -sinix*) os=-sysv4 ;; -tpf*) os=-tpf ;; -triton*) os=-sysv3 ;; -oss*) os=-sysv3 ;; -svr4*) os=-sysv4 ;; -svr3) os=-sysv3 ;; -sysvr4) os=-sysv4 ;; # This must come after -sysvr4. -sysv*) ;; -ose*) os=-ose ;; -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*) os=-mint ;; -zvmoe) os=-zvmoe ;; -dicos*) os=-dicos ;; -pikeos*) # Until real need of OS specific support for # particular features comes up, bare metal # configurations are quite functional. case $basic_machine in arm*) os=-eabi ;; *) os=-elf ;; esac ;; -nacl*) ;; -ios) ;; -none) ;; *) # Get rid of the `-' at the beginning of $os. os=`echo $os | sed 's/[^-]*-//'` echo Invalid configuration \`"$1"\': system \`"$os"\' not recognized 1>&2 exit 1 ;; esac else # Here we handle the default operating systems that come with various machines. # The value should be what the vendor currently ships out the door with their # machine or put another way, the most popular os provided with the machine. # Note that if you're going to try to match "-MANUFACTURER" here (say, # "-sun"), then you have to tell the case statement up towards the top # that MANUFACTURER isn't an operating system. Otherwise, code above # will signal an error saying that MANUFACTURER isn't an operating # system, and we'll never get to this point. case $basic_machine in score-*) os=-elf ;; spu-*) os=-elf ;; *-acorn) os=-riscix1.2 ;; arm*-rebel) os=-linux ;; arm*-semi) os=-aout ;; c4x-* | tic4x-*) os=-coff ;; c8051-*) os=-elf ;; hexagon-*) os=-elf ;; tic54x-*) os=-coff ;; tic55x-*) os=-coff ;; tic6x-*) os=-coff ;; # This must come before the *-dec entry. pdp10-*) os=-tops20 ;; pdp11-*) os=-none ;; *-dec | vax-*) os=-ultrix4.2 ;; m68*-apollo) os=-domain ;; i386-sun) os=-sunos4.0.2 ;; m68000-sun) os=-sunos3 ;; m68*-cisco) os=-aout ;; mep-*) os=-elf ;; mips*-cisco) os=-elf ;; mips*-*) os=-elf ;; or32-*) os=-coff ;; *-tti) # must be before sparc entry or we get the wrong os. os=-sysv3 ;; sparc-* | *-sun) os=-sunos4.1.1 ;; pru-*) os=-elf ;; *-be) os=-beos ;; *-ibm) os=-aix ;; *-knuth) os=-mmixware ;; *-wec) os=-proelf ;; *-winbond) os=-proelf ;; *-oki) os=-proelf ;; *-hp) os=-hpux ;; *-hitachi) os=-hiux ;; i860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent) os=-sysv ;; *-cbm) os=-amigaos ;; *-dg) os=-dgux ;; *-dolphin) os=-sysv3 ;; m68k-ccur) os=-rtu ;; m88k-omron*) os=-luna ;; *-next) os=-nextstep ;; *-sequent) os=-ptx ;; *-crds) os=-unos ;; *-ns) os=-genix ;; i370-*) os=-mvs ;; *-gould) os=-sysv ;; *-highlevel) os=-bsd ;; *-encore) os=-bsd ;; *-sgi) os=-irix ;; *-siemens) os=-sysv4 ;; *-masscomp) os=-rtu ;; f30[01]-fujitsu | f700-fujitsu) os=-uxpv ;; *-rom68k) os=-coff ;; *-*bug) os=-coff ;; *-apple) os=-macos ;; *-atari*) os=-mint ;; *) os=-none ;; esac fi # Here we handle the case where we know the os, and the CPU type, but not the # manufacturer. We pick the logical manufacturer. vendor=unknown case $basic_machine in *-unknown) case $os in -riscix*) vendor=acorn ;; -sunos*) vendor=sun ;; -cnk*|-aix*) vendor=ibm ;; -beos*) vendor=be ;; -hpux*) vendor=hp ;; -mpeix*) vendor=hp ;; -hiux*) vendor=hitachi ;; -unos*) vendor=crds ;; -dgux*) vendor=dg ;; -luna*) vendor=omron ;; -genix*) vendor=ns ;; -mvs* | -opened*) vendor=ibm ;; -os400*) vendor=ibm ;; -ptx*) vendor=sequent ;; -tpf*) vendor=ibm ;; -vxsim* | -vxworks* | -windiss*) vendor=wrs ;; -aux*) vendor=apple ;; -hms*) vendor=hitachi ;; -mpw* | -macos*) vendor=apple ;; -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*) vendor=atari ;; -vos*) vendor=stratus ;; esac basic_machine=`echo "$basic_machine" | sed "s/unknown/$vendor/"` ;; esac echo "$basic_machine$os" exit # Local variables: # eval: (add-hook 'write-file-functions 'time-stamp) # time-stamp-start: "timestamp='" # time-stamp-format: "%:y-%02m-%02d" # time-stamp-end: "'" # End: capnproto-c++-0.8.0/build-aux/depcomp0000755000175000017500000005602013650317755020236 0ustar00kentonkenton00000000000000#! /bin/sh # depcomp - compile a program generating dependencies as side-effects scriptversion=2018-03-07.03; # UTC # Copyright (C) 1999-2018 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # As a special exception to the GNU General Public License, if you # distribute this file as part of a program that contains a # configuration script generated by Autoconf, you may include it under # the same distribution terms that you use for the rest of that program. # Originally written by Alexandre Oliva . case $1 in '') echo "$0: No command. Try '$0 --help' for more information." 1>&2 exit 1; ;; -h | --h*) cat <<\EOF Usage: depcomp [--help] [--version] PROGRAM [ARGS] Run PROGRAMS ARGS to compile a file, generating dependencies as side-effects. Environment variables: depmode Dependency tracking mode. source Source file read by 'PROGRAMS ARGS'. object Object file output by 'PROGRAMS ARGS'. DEPDIR directory where to store dependencies. depfile Dependency file to output. tmpdepfile Temporary file to use when outputting dependencies. libtool Whether libtool is used (yes/no). Report bugs to . EOF exit $? ;; -v | --v*) echo "depcomp $scriptversion" exit $? ;; esac # Get the directory component of the given path, and save it in the # global variables '$dir'. Note that this directory component will # be either empty or ending with a '/' character. This is deliberate. set_dir_from () { case $1 in */*) dir=`echo "$1" | sed -e 's|/[^/]*$|/|'`;; *) dir=;; esac } # Get the suffix-stripped basename of the given path, and save it the # global variable '$base'. set_base_from () { base=`echo "$1" | sed -e 's|^.*/||' -e 's/\.[^.]*$//'` } # If no dependency file was actually created by the compiler invocation, # we still have to create a dummy depfile, to avoid errors with the # Makefile "include basename.Plo" scheme. make_dummy_depfile () { echo "#dummy" > "$depfile" } # Factor out some common post-processing of the generated depfile. # Requires the auxiliary global variable '$tmpdepfile' to be set. aix_post_process_depfile () { # If the compiler actually managed to produce a dependency file, # post-process it. if test -f "$tmpdepfile"; then # Each line is of the form 'foo.o: dependency.h'. # Do two passes, one to just change these to # $object: dependency.h # and one to simply output # dependency.h: # which is needed to avoid the deleted-header problem. { sed -e "s,^.*\.[$lower]*:,$object:," < "$tmpdepfile" sed -e "s,^.*\.[$lower]*:[$tab ]*,," -e 's,$,:,' < "$tmpdepfile" } > "$depfile" rm -f "$tmpdepfile" else make_dummy_depfile fi } # A tabulation character. tab=' ' # A newline character. nl=' ' # Character ranges might be problematic outside the C locale. # These definitions help. upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ lower=abcdefghijklmnopqrstuvwxyz digits=0123456789 alpha=${upper}${lower} if test -z "$depmode" || test -z "$source" || test -z "$object"; then echo "depcomp: Variables source, object and depmode must be set" 1>&2 exit 1 fi # Dependencies for sub/bar.o or sub/bar.obj go into sub/.deps/bar.Po. depfile=${depfile-`echo "$object" | sed 's|[^\\/]*$|'${DEPDIR-.deps}'/&|;s|\.\([^.]*\)$|.P\1|;s|Pobj$|Po|'`} tmpdepfile=${tmpdepfile-`echo "$depfile" | sed 's/\.\([^.]*\)$/.T\1/'`} rm -f "$tmpdepfile" # Avoid interferences from the environment. gccflag= dashmflag= # Some modes work just like other modes, but use different flags. We # parameterize here, but still list the modes in the big case below, # to make depend.m4 easier to write. Note that we *cannot* use a case # here, because this file can only contain one case statement. if test "$depmode" = hp; then # HP compiler uses -M and no extra arg. gccflag=-M depmode=gcc fi if test "$depmode" = dashXmstdout; then # This is just like dashmstdout with a different argument. dashmflag=-xM depmode=dashmstdout fi cygpath_u="cygpath -u -f -" if test "$depmode" = msvcmsys; then # This is just like msvisualcpp but w/o cygpath translation. # Just convert the backslash-escaped backslashes to single forward # slashes to satisfy depend.m4 cygpath_u='sed s,\\\\,/,g' depmode=msvisualcpp fi if test "$depmode" = msvc7msys; then # This is just like msvc7 but w/o cygpath translation. # Just convert the backslash-escaped backslashes to single forward # slashes to satisfy depend.m4 cygpath_u='sed s,\\\\,/,g' depmode=msvc7 fi if test "$depmode" = xlc; then # IBM C/C++ Compilers xlc/xlC can output gcc-like dependency information. gccflag=-qmakedep=gcc,-MF depmode=gcc fi case "$depmode" in gcc3) ## gcc 3 implements dependency tracking that does exactly what ## we want. Yay! Note: for some reason libtool 1.4 doesn't like ## it if -MD -MP comes after the -MF stuff. Hmm. ## Unfortunately, FreeBSD c89 acceptance of flags depends upon ## the command line argument order; so add the flags where they ## appear in depend2.am. Note that the slowdown incurred here ## affects only configure: in makefiles, %FASTDEP% shortcuts this. for arg do case $arg in -c) set fnord "$@" -MT "$object" -MD -MP -MF "$tmpdepfile" "$arg" ;; *) set fnord "$@" "$arg" ;; esac shift # fnord shift # $arg done "$@" stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile" exit $stat fi mv "$tmpdepfile" "$depfile" ;; gcc) ## Note that this doesn't just cater to obsosete pre-3.x GCC compilers. ## but also to in-use compilers like IMB xlc/xlC and the HP C compiler. ## (see the conditional assignment to $gccflag above). ## There are various ways to get dependency output from gcc. Here's ## why we pick this rather obscure method: ## - Don't want to use -MD because we'd like the dependencies to end ## up in a subdir. Having to rename by hand is ugly. ## (We might end up doing this anyway to support other compilers.) ## - The DEPENDENCIES_OUTPUT environment variable makes gcc act like ## -MM, not -M (despite what the docs say). Also, it might not be ## supported by the other compilers which use the 'gcc' depmode. ## - Using -M directly means running the compiler twice (even worse ## than renaming). if test -z "$gccflag"; then gccflag=-MD, fi "$@" -Wp,"$gccflag$tmpdepfile" stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile" exit $stat fi rm -f "$depfile" echo "$object : \\" > "$depfile" # The second -e expression handles DOS-style file names with drive # letters. sed -e 's/^[^:]*: / /' \ -e 's/^['$alpha']:\/[^:]*: / /' < "$tmpdepfile" >> "$depfile" ## This next piece of magic avoids the "deleted header file" problem. ## The problem is that when a header file which appears in a .P file ## is deleted, the dependency causes make to die (because there is ## typically no way to rebuild the header). We avoid this by adding ## dummy dependencies for each header file. Too bad gcc doesn't do ## this for us directly. ## Some versions of gcc put a space before the ':'. On the theory ## that the space means something, we add a space to the output as ## well. hp depmode also adds that space, but also prefixes the VPATH ## to the object. Take care to not repeat it in the output. ## Some versions of the HPUX 10.20 sed can't process this invocation ## correctly. Breaking it into two sed invocations is a workaround. tr ' ' "$nl" < "$tmpdepfile" \ | sed -e 's/^\\$//' -e '/^$/d' -e "s|.*$object$||" -e '/:$/d' \ | sed -e 's/$/ :/' >> "$depfile" rm -f "$tmpdepfile" ;; hp) # This case exists only to let depend.m4 do its work. It works by # looking at the text of this script. This case will never be run, # since it is checked for above. exit 1 ;; sgi) if test "$libtool" = yes; then "$@" "-Wp,-MDupdate,$tmpdepfile" else "$@" -MDupdate "$tmpdepfile" fi stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile" exit $stat fi rm -f "$depfile" if test -f "$tmpdepfile"; then # yes, the sourcefile depend on other files echo "$object : \\" > "$depfile" # Clip off the initial element (the dependent). Don't try to be # clever and replace this with sed code, as IRIX sed won't handle # lines with more than a fixed number of characters (4096 in # IRIX 6.2 sed, 8192 in IRIX 6.5). We also remove comment lines; # the IRIX cc adds comments like '#:fec' to the end of the # dependency line. tr ' ' "$nl" < "$tmpdepfile" \ | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' \ | tr "$nl" ' ' >> "$depfile" echo >> "$depfile" # The second pass generates a dummy entry for each header file. tr ' ' "$nl" < "$tmpdepfile" \ | sed -e 's/^.*\.o://' -e 's/#.*$//' -e '/^$/ d' -e 's/$/:/' \ >> "$depfile" else make_dummy_depfile fi rm -f "$tmpdepfile" ;; xlc) # This case exists only to let depend.m4 do its work. It works by # looking at the text of this script. This case will never be run, # since it is checked for above. exit 1 ;; aix) # The C for AIX Compiler uses -M and outputs the dependencies # in a .u file. In older versions, this file always lives in the # current directory. Also, the AIX compiler puts '$object:' at the # start of each line; $object doesn't have directory information. # Version 6 uses the directory in both cases. set_dir_from "$object" set_base_from "$object" if test "$libtool" = yes; then tmpdepfile1=$dir$base.u tmpdepfile2=$base.u tmpdepfile3=$dir.libs/$base.u "$@" -Wc,-M else tmpdepfile1=$dir$base.u tmpdepfile2=$dir$base.u tmpdepfile3=$dir$base.u "$@" -M fi stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" exit $stat fi for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" do test -f "$tmpdepfile" && break done aix_post_process_depfile ;; tcc) # tcc (Tiny C Compiler) understand '-MD -MF file' since version 0.9.26 # FIXME: That version still under development at the moment of writing. # Make that this statement remains true also for stable, released # versions. # It will wrap lines (doesn't matter whether long or short) with a # trailing '\', as in: # # foo.o : \ # foo.c \ # foo.h \ # # It will put a trailing '\' even on the last line, and will use leading # spaces rather than leading tabs (at least since its commit 0394caf7 # "Emit spaces for -MD"). "$@" -MD -MF "$tmpdepfile" stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile" exit $stat fi rm -f "$depfile" # Each non-empty line is of the form 'foo.o : \' or ' dep.h \'. # We have to change lines of the first kind to '$object: \'. sed -e "s|.*:|$object :|" < "$tmpdepfile" > "$depfile" # And for each line of the second kind, we have to emit a 'dep.h:' # dummy dependency, to avoid the deleted-header problem. sed -n -e 's|^ *\(.*\) *\\$|\1:|p' < "$tmpdepfile" >> "$depfile" rm -f "$tmpdepfile" ;; ## The order of this option in the case statement is important, since the ## shell code in configure will try each of these formats in the order ## listed in this file. A plain '-MD' option would be understood by many ## compilers, so we must ensure this comes after the gcc and icc options. pgcc) # Portland's C compiler understands '-MD'. # Will always output deps to 'file.d' where file is the root name of the # source file under compilation, even if file resides in a subdirectory. # The object file name does not affect the name of the '.d' file. # pgcc 10.2 will output # foo.o: sub/foo.c sub/foo.h # and will wrap long lines using '\' : # foo.o: sub/foo.c ... \ # sub/foo.h ... \ # ... set_dir_from "$object" # Use the source, not the object, to determine the base name, since # that's sadly what pgcc will do too. set_base_from "$source" tmpdepfile=$base.d # For projects that build the same source file twice into different object # files, the pgcc approach of using the *source* file root name can cause # problems in parallel builds. Use a locking strategy to avoid stomping on # the same $tmpdepfile. lockdir=$base.d-lock trap " echo '$0: caught signal, cleaning up...' >&2 rmdir '$lockdir' exit 1 " 1 2 13 15 numtries=100 i=$numtries while test $i -gt 0; do # mkdir is a portable test-and-set. if mkdir "$lockdir" 2>/dev/null; then # This process acquired the lock. "$@" -MD stat=$? # Release the lock. rmdir "$lockdir" break else # If the lock is being held by a different process, wait # until the winning process is done or we timeout. while test -d "$lockdir" && test $i -gt 0; do sleep 1 i=`expr $i - 1` done fi i=`expr $i - 1` done trap - 1 2 13 15 if test $i -le 0; then echo "$0: failed to acquire lock after $numtries attempts" >&2 echo "$0: check lockdir '$lockdir'" >&2 exit 1 fi if test $stat -ne 0; then rm -f "$tmpdepfile" exit $stat fi rm -f "$depfile" # Each line is of the form `foo.o: dependent.h', # or `foo.o: dep1.h dep2.h \', or ` dep3.h dep4.h \'. # Do two passes, one to just change these to # `$object: dependent.h' and one to simply `dependent.h:'. sed "s,^[^:]*:,$object :," < "$tmpdepfile" > "$depfile" # Some versions of the HPUX 10.20 sed can't process this invocation # correctly. Breaking it into two sed invocations is a workaround. sed 's,^[^:]*: \(.*\)$,\1,;s/^\\$//;/^$/d;/:$/d' < "$tmpdepfile" \ | sed -e 's/$/ :/' >> "$depfile" rm -f "$tmpdepfile" ;; hp2) # The "hp" stanza above does not work with aCC (C++) and HP's ia64 # compilers, which have integrated preprocessors. The correct option # to use with these is +Maked; it writes dependencies to a file named # 'foo.d', which lands next to the object file, wherever that # happens to be. # Much of this is similar to the tru64 case; see comments there. set_dir_from "$object" set_base_from "$object" if test "$libtool" = yes; then tmpdepfile1=$dir$base.d tmpdepfile2=$dir.libs/$base.d "$@" -Wc,+Maked else tmpdepfile1=$dir$base.d tmpdepfile2=$dir$base.d "$@" +Maked fi stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile1" "$tmpdepfile2" exit $stat fi for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" do test -f "$tmpdepfile" && break done if test -f "$tmpdepfile"; then sed -e "s,^.*\.[$lower]*:,$object:," "$tmpdepfile" > "$depfile" # Add 'dependent.h:' lines. sed -ne '2,${ s/^ *// s/ \\*$// s/$/:/ p }' "$tmpdepfile" >> "$depfile" else make_dummy_depfile fi rm -f "$tmpdepfile" "$tmpdepfile2" ;; tru64) # The Tru64 compiler uses -MD to generate dependencies as a side # effect. 'cc -MD -o foo.o ...' puts the dependencies into 'foo.o.d'. # At least on Alpha/Redhat 6.1, Compaq CCC V6.2-504 seems to put # dependencies in 'foo.d' instead, so we check for that too. # Subdirectories are respected. set_dir_from "$object" set_base_from "$object" if test "$libtool" = yes; then # Libtool generates 2 separate objects for the 2 libraries. These # two compilations output dependencies in $dir.libs/$base.o.d and # in $dir$base.o.d. We have to check for both files, because # one of the two compilations can be disabled. We should prefer # $dir$base.o.d over $dir.libs/$base.o.d because the latter is # automatically cleaned when .libs/ is deleted, while ignoring # the former would cause a distcleancheck panic. tmpdepfile1=$dir$base.o.d # libtool 1.5 tmpdepfile2=$dir.libs/$base.o.d # Likewise. tmpdepfile3=$dir.libs/$base.d # Compaq CCC V6.2-504 "$@" -Wc,-MD else tmpdepfile1=$dir$base.d tmpdepfile2=$dir$base.d tmpdepfile3=$dir$base.d "$@" -MD fi stat=$? if test $stat -ne 0; then rm -f "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" exit $stat fi for tmpdepfile in "$tmpdepfile1" "$tmpdepfile2" "$tmpdepfile3" do test -f "$tmpdepfile" && break done # Same post-processing that is required for AIX mode. aix_post_process_depfile ;; msvc7) if test "$libtool" = yes; then showIncludes=-Wc,-showIncludes else showIncludes=-showIncludes fi "$@" $showIncludes > "$tmpdepfile" stat=$? grep -v '^Note: including file: ' "$tmpdepfile" if test $stat -ne 0; then rm -f "$tmpdepfile" exit $stat fi rm -f "$depfile" echo "$object : \\" > "$depfile" # The first sed program below extracts the file names and escapes # backslashes for cygpath. The second sed program outputs the file # name when reading, but also accumulates all include files in the # hold buffer in order to output them again at the end. This only # works with sed implementations that can handle large buffers. sed < "$tmpdepfile" -n ' /^Note: including file: *\(.*\)/ { s//\1/ s/\\/\\\\/g p }' | $cygpath_u | sort -u | sed -n ' s/ /\\ /g s/\(.*\)/'"$tab"'\1 \\/p s/.\(.*\) \\/\1:/ H $ { s/.*/'"$tab"'/ G p }' >> "$depfile" echo >> "$depfile" # make sure the fragment doesn't end with a backslash rm -f "$tmpdepfile" ;; msvc7msys) # This case exists only to let depend.m4 do its work. It works by # looking at the text of this script. This case will never be run, # since it is checked for above. exit 1 ;; #nosideeffect) # This comment above is used by automake to tell side-effect # dependency tracking mechanisms from slower ones. dashmstdout) # Important note: in order to support this mode, a compiler *must* # always write the preprocessed file to stdout, regardless of -o. "$@" || exit $? # Remove the call to Libtool. if test "$libtool" = yes; then while test "X$1" != 'X--mode=compile'; do shift done shift fi # Remove '-o $object'. IFS=" " for arg do case $arg in -o) shift ;; $object) shift ;; *) set fnord "$@" "$arg" shift # fnord shift # $arg ;; esac done test -z "$dashmflag" && dashmflag=-M # Require at least two characters before searching for ':' # in the target name. This is to cope with DOS-style filenames: # a dependency such as 'c:/foo/bar' could be seen as target 'c' otherwise. "$@" $dashmflag | sed "s|^[$tab ]*[^:$tab ][^:][^:]*:[$tab ]*|$object: |" > "$tmpdepfile" rm -f "$depfile" cat < "$tmpdepfile" > "$depfile" # Some versions of the HPUX 10.20 sed can't process this sed invocation # correctly. Breaking it into two sed invocations is a workaround. tr ' ' "$nl" < "$tmpdepfile" \ | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \ | sed -e 's/$/ :/' >> "$depfile" rm -f "$tmpdepfile" ;; dashXmstdout) # This case only exists to satisfy depend.m4. It is never actually # run, as this mode is specially recognized in the preamble. exit 1 ;; makedepend) "$@" || exit $? # Remove any Libtool call if test "$libtool" = yes; then while test "X$1" != 'X--mode=compile'; do shift done shift fi # X makedepend shift cleared=no eat=no for arg do case $cleared in no) set ""; shift cleared=yes ;; esac if test $eat = yes; then eat=no continue fi case "$arg" in -D*|-I*) set fnord "$@" "$arg"; shift ;; # Strip any option that makedepend may not understand. Remove # the object too, otherwise makedepend will parse it as a source file. -arch) eat=yes ;; -*|$object) ;; *) set fnord "$@" "$arg"; shift ;; esac done obj_suffix=`echo "$object" | sed 's/^.*\././'` touch "$tmpdepfile" ${MAKEDEPEND-makedepend} -o"$obj_suffix" -f"$tmpdepfile" "$@" rm -f "$depfile" # makedepend may prepend the VPATH from the source file name to the object. # No need to regex-escape $object, excess matching of '.' is harmless. sed "s|^.*\($object *:\)|\1|" "$tmpdepfile" > "$depfile" # Some versions of the HPUX 10.20 sed can't process the last invocation # correctly. Breaking it into two sed invocations is a workaround. sed '1,2d' "$tmpdepfile" \ | tr ' ' "$nl" \ | sed -e 's/^\\$//' -e '/^$/d' -e '/:$/d' \ | sed -e 's/$/ :/' >> "$depfile" rm -f "$tmpdepfile" "$tmpdepfile".bak ;; cpp) # Important note: in order to support this mode, a compiler *must* # always write the preprocessed file to stdout. "$@" || exit $? # Remove the call to Libtool. if test "$libtool" = yes; then while test "X$1" != 'X--mode=compile'; do shift done shift fi # Remove '-o $object'. IFS=" " for arg do case $arg in -o) shift ;; $object) shift ;; *) set fnord "$@" "$arg" shift # fnord shift # $arg ;; esac done "$@" -E \ | sed -n -e '/^# [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \ -e '/^#line [0-9][0-9]* "\([^"]*\)".*/ s:: \1 \\:p' \ | sed '$ s: \\$::' > "$tmpdepfile" rm -f "$depfile" echo "$object : \\" > "$depfile" cat < "$tmpdepfile" >> "$depfile" sed < "$tmpdepfile" '/^$/d;s/^ //;s/ \\$//;s/$/ :/' >> "$depfile" rm -f "$tmpdepfile" ;; msvisualcpp) # Important note: in order to support this mode, a compiler *must* # always write the preprocessed file to stdout. "$@" || exit $? # Remove the call to Libtool. if test "$libtool" = yes; then while test "X$1" != 'X--mode=compile'; do shift done shift fi IFS=" " for arg do case "$arg" in -o) shift ;; $object) shift ;; "-Gm"|"/Gm"|"-Gi"|"/Gi"|"-ZI"|"/ZI") set fnord "$@" shift shift ;; *) set fnord "$@" "$arg" shift shift ;; esac done "$@" -E 2>/dev/null | sed -n '/^#line [0-9][0-9]* "\([^"]*\)"/ s::\1:p' | $cygpath_u | sort -u > "$tmpdepfile" rm -f "$depfile" echo "$object : \\" > "$depfile" sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::'"$tab"'\1 \\:p' >> "$depfile" echo "$tab" >> "$depfile" sed < "$tmpdepfile" -n -e 's% %\\ %g' -e '/^\(.*\)$/ s::\1\::p' >> "$depfile" rm -f "$tmpdepfile" ;; msvcmsys) # This case exists only to let depend.m4 do its work. It works by # looking at the text of this script. This case will never be run, # since it is checked for above. exit 1 ;; none) exec "$@" ;; *) echo "Unknown depmode $depmode" 1>&2 exit 1 ;; esac exit 0 # Local Variables: # mode: shell-script # sh-indentation: 2 # eval: (add-hook 'before-save-hook 'time-stamp) # time-stamp-start: "scriptversion=" # time-stamp-format: "%:y-%02m-%02d.%02H" # time-stamp-time-zone: "UTC0" # time-stamp-end: "; # UTC" # End: capnproto-c++-0.8.0/build-aux/install-sh0000755000175000017500000003601013650317755020662 0ustar00kentonkenton00000000000000#!/bin/sh # install - install a program, script, or datafile scriptversion=2018-03-11.20; # UTC # This originates from X11R5 (mit/util/scripts/install.sh), which was # later released in X11R6 (xc/config/util/install.sh) with the # following copyright and license. # # Copyright (C) 1994 X Consortium # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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 # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # Except as contained in this notice, the name of the X Consortium shall not # be used in advertising or otherwise to promote the sale, use or other deal- # ings in this Software without prior written authorization from the X Consor- # tium. # # # FSF changes to this file are in the public domain. # # Calling this script install-sh is preferred over install.sh, to prevent # 'make' implicit rules from creating a file called install from it # when there is no Makefile. # # This script is compatible with the BSD install script, but was written # from scratch. tab=' ' nl=' ' IFS=" $tab$nl" # Set DOITPROG to "echo" to test this script. doit=${DOITPROG-} doit_exec=${doit:-exec} # Put in absolute file names if you don't have them in your path; # or use environment vars. chgrpprog=${CHGRPPROG-chgrp} chmodprog=${CHMODPROG-chmod} chownprog=${CHOWNPROG-chown} cmpprog=${CMPPROG-cmp} cpprog=${CPPROG-cp} mkdirprog=${MKDIRPROG-mkdir} mvprog=${MVPROG-mv} rmprog=${RMPROG-rm} stripprog=${STRIPPROG-strip} posix_mkdir= # Desired mode of installed file. mode=0755 chgrpcmd= chmodcmd=$chmodprog chowncmd= mvcmd=$mvprog rmcmd="$rmprog -f" stripcmd= src= dst= dir_arg= dst_arg= copy_on_change=false is_target_a_directory=possibly usage="\ Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE or: $0 [OPTION]... SRCFILES... DIRECTORY or: $0 [OPTION]... -t DIRECTORY SRCFILES... or: $0 [OPTION]... -d DIRECTORIES... In the 1st form, copy SRCFILE to DSTFILE. In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. In the 4th, create DIRECTORIES. Options: --help display this help and exit. --version display version info and exit. -c (ignored) -C install only if different (preserve the last data modification time) -d create directories instead of installing files. -g GROUP $chgrpprog installed files to GROUP. -m MODE $chmodprog installed files to MODE. -o USER $chownprog installed files to USER. -s $stripprog installed files. -t DIRECTORY install into DIRECTORY. -T report an error if DSTFILE is a directory. Environment variables override the default commands: CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG " while test $# -ne 0; do case $1 in -c) ;; -C) copy_on_change=true;; -d) dir_arg=true;; -g) chgrpcmd="$chgrpprog $2" shift;; --help) echo "$usage"; exit $?;; -m) mode=$2 case $mode in *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) echo "$0: invalid mode: $mode" >&2 exit 1;; esac shift;; -o) chowncmd="$chownprog $2" shift;; -s) stripcmd=$stripprog;; -t) is_target_a_directory=always dst_arg=$2 # Protect names problematic for 'test' and other utilities. case $dst_arg in -* | [=\(\)!]) dst_arg=./$dst_arg;; esac shift;; -T) is_target_a_directory=never;; --version) echo "$0 $scriptversion"; exit $?;; --) shift break;; -*) echo "$0: invalid option: $1" >&2 exit 1;; *) break;; esac shift done # We allow the use of options -d and -T together, by making -d # take the precedence; this is for compatibility with GNU install. if test -n "$dir_arg"; then if test -n "$dst_arg"; then echo "$0: target directory not allowed when installing a directory." >&2 exit 1 fi fi if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then # When -d is used, all remaining arguments are directories to create. # When -t is used, the destination is already specified. # Otherwise, the last argument is the destination. Remove it from $@. for arg do if test -n "$dst_arg"; then # $@ is not empty: it contains at least $arg. set fnord "$@" "$dst_arg" shift # fnord fi shift # arg dst_arg=$arg # Protect names problematic for 'test' and other utilities. case $dst_arg in -* | [=\(\)!]) dst_arg=./$dst_arg;; esac done fi if test $# -eq 0; then if test -z "$dir_arg"; then echo "$0: no input file specified." >&2 exit 1 fi # It's OK to call 'install-sh -d' without argument. # This can happen when creating conditional directories. exit 0 fi if test -z "$dir_arg"; then if test $# -gt 1 || test "$is_target_a_directory" = always; then if test ! -d "$dst_arg"; then echo "$0: $dst_arg: Is not a directory." >&2 exit 1 fi fi fi if test -z "$dir_arg"; then do_exit='(exit $ret); exit $ret' trap "ret=129; $do_exit" 1 trap "ret=130; $do_exit" 2 trap "ret=141; $do_exit" 13 trap "ret=143; $do_exit" 15 # Set umask so as not to create temps with too-generous modes. # However, 'strip' requires both read and write access to temps. case $mode in # Optimize common cases. *644) cp_umask=133;; *755) cp_umask=22;; *[0-7]) if test -z "$stripcmd"; then u_plus_rw= else u_plus_rw='% 200' fi cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; *) if test -z "$stripcmd"; then u_plus_rw= else u_plus_rw=,u+rw fi cp_umask=$mode$u_plus_rw;; esac fi for src do # Protect names problematic for 'test' and other utilities. case $src in -* | [=\(\)!]) src=./$src;; esac if test -n "$dir_arg"; then dst=$src dstdir=$dst test -d "$dstdir" dstdir_status=$? else # Waiting for this to be detected by the "$cpprog $src $dsttmp" command # might cause directories to be created, which would be especially bad # if $src (and thus $dsttmp) contains '*'. if test ! -f "$src" && test ! -d "$src"; then echo "$0: $src does not exist." >&2 exit 1 fi if test -z "$dst_arg"; then echo "$0: no destination specified." >&2 exit 1 fi dst=$dst_arg # If destination is a directory, append the input filename. if test -d "$dst"; then if test "$is_target_a_directory" = never; then echo "$0: $dst_arg: Is a directory" >&2 exit 1 fi dstdir=$dst dstbase=`basename "$src"` case $dst in */) dst=$dst$dstbase;; *) dst=$dst/$dstbase;; esac dstdir_status=0 else dstdir=`dirname "$dst"` test -d "$dstdir" dstdir_status=$? fi fi case $dstdir in */) dstdirslash=$dstdir;; *) dstdirslash=$dstdir/;; esac obsolete_mkdir_used=false if test $dstdir_status != 0; then case $posix_mkdir in '') # Create intermediate dirs using mode 755 as modified by the umask. # This is like FreeBSD 'install' as of 1997-10-28. umask=`umask` case $stripcmd.$umask in # Optimize common cases. *[2367][2367]) mkdir_umask=$umask;; .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; *[0-7]) mkdir_umask=`expr $umask + 22 \ - $umask % 100 % 40 + $umask % 20 \ - $umask % 10 % 4 + $umask % 2 `;; *) mkdir_umask=$umask,go-w;; esac # With -d, create the new directory with the user-specified mode. # Otherwise, rely on $mkdir_umask. if test -n "$dir_arg"; then mkdir_mode=-m$mode else mkdir_mode= fi posix_mkdir=false case $umask in *[123567][0-7][0-7]) # POSIX mkdir -p sets u+wx bits regardless of umask, which # is incompatible with FreeBSD 'install' when (umask & 300) != 0. ;; *) # Note that $RANDOM variable is not portable (e.g. dash); Use it # here however when possible just to lower collision chance. tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0 # Because "mkdir -p" follows existing symlinks and we likely work # directly in world-writeable /tmp, make sure that the '$tmpdir' # directory is successfully created first before we actually test # 'mkdir -p' feature. if (umask $mkdir_umask && $mkdirprog $mkdir_mode "$tmpdir" && exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 then if test -z "$dir_arg" || { # Check for POSIX incompatibilities with -m. # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or # other-writable bit of parent directory when it shouldn't. # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. test_tmpdir="$tmpdir/a" ls_ld_tmpdir=`ls -ld "$test_tmpdir"` case $ls_ld_tmpdir in d????-?r-*) different_mode=700;; d????-?--*) different_mode=755;; *) false;; esac && $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" } } then posix_mkdir=: fi rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" else # Remove any dirs left behind by ancient mkdir implementations. rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null fi trap '' 0;; esac;; esac if $posix_mkdir && ( umask $mkdir_umask && $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" ) then : else # The umask is ridiculous, or mkdir does not conform to POSIX, # or it failed possibly due to a race condition. Create the # directory the slow way, step by step, checking for races as we go. case $dstdir in /*) prefix='/';; [-=\(\)!]*) prefix='./';; *) prefix='';; esac oIFS=$IFS IFS=/ set -f set fnord $dstdir shift set +f IFS=$oIFS prefixes= for d do test X"$d" = X && continue prefix=$prefix$d if test -d "$prefix"; then prefixes= else if $posix_mkdir; then (umask=$mkdir_umask && $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break # Don't fail if two instances are running concurrently. test -d "$prefix" || exit 1 else case $prefix in *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; *) qprefix=$prefix;; esac prefixes="$prefixes '$qprefix'" fi fi prefix=$prefix/ done if test -n "$prefixes"; then # Don't fail if two instances are running concurrently. (umask $mkdir_umask && eval "\$doit_exec \$mkdirprog $prefixes") || test -d "$dstdir" || exit 1 obsolete_mkdir_used=true fi fi fi if test -n "$dir_arg"; then { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 else # Make a couple of temp file names in the proper directory. dsttmp=${dstdirslash}_inst.$$_ rmtmp=${dstdirslash}_rm.$$_ # Trap to clean up those temp files at exit. trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 # Copy the file name to the temp name. (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && # and set any options; do chmod last to preserve setuid bits. # # If any of these fail, we abort the whole thing. If we want to # ignore errors from any of these, just make sure not to ignore # errors from the above "$doit $cpprog $src $dsttmp" command. # { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && # If -C, don't bother to copy if it wouldn't change the file. if $copy_on_change && old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && set -f && set X $old && old=:$2:$4:$5:$6 && set X $new && new=:$2:$4:$5:$6 && set +f && test "$old" = "$new" && $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 then rm -f "$dsttmp" else # Rename the file to the real destination. $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || # The rename failed, perhaps because mv can't rename something else # to itself, or perhaps because mv is so ancient that it does not # support -f. { # Now remove or move aside any old file at destination location. # We try this two ways since rm can't unlink itself on some # systems and the destination file might be busy for other # reasons. In this case, the final cleanup might fail but the new # file should still install successfully. { test ! -f "$dst" || $doit $rmcmd -f "$dst" 2>/dev/null || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } } || { echo "$0: cannot unlink or rename $dst" >&2 (exit 1); exit 1 } } && # Now rename the file to the real destination. $doit $mvcmd "$dsttmp" "$dst" } fi || exit 1 trap '' 0 fi done # Local variables: # eval: (add-hook 'before-save-hook 'time-stamp) # time-stamp-start: "scriptversion=" # time-stamp-format: "%:y-%02m-%02d.%02H" # time-stamp-time-zone: "UTC0" # time-stamp-end: "; # UTC" # End: capnproto-c++-0.8.0/build-aux/ltmain.sh0000644000175000017500000117671413650317750020512 0ustar00kentonkenton00000000000000#! /bin/sh ## DO NOT EDIT - This file generated from ./build-aux/ltmain.in ## by inline-source v2014-01-03.01 # libtool (GNU libtool) 2.4.6 # Provide generalized library-building support services. # Written by Gordon Matzigkeit , 1996 # Copyright (C) 1996-2015 Free Software Foundation, Inc. # This is free software; see the source for copying conditions. There is NO # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # GNU Libtool is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # As a special exception to the GNU General Public License, # if you distribute this file as part of a program or library that # is built using GNU Libtool, you may include this file under the # same distribution terms that you use for the rest of that program. # # GNU Libtool is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . PROGRAM=libtool PACKAGE=libtool VERSION="2.4.6 Debian-2.4.6-10" package_revision=2.4.6 ## ------ ## ## Usage. ## ## ------ ## # Run './libtool --help' for help with using this script from the # command line. ## ------------------------------- ## ## User overridable command paths. ## ## ------------------------------- ## # After configure completes, it has a better idea of some of the # shell tools we need than the defaults used by the functions shared # with bootstrap, so set those here where they can still be over- # ridden by the user, but otherwise take precedence. : ${AUTOCONF="autoconf"} : ${AUTOMAKE="automake"} ## -------------------------- ## ## Source external libraries. ## ## -------------------------- ## # Much of our low-level functionality needs to be sourced from external # libraries, which are installed to $pkgauxdir. # Set a version string for this script. scriptversion=2015-01-20.17; # UTC # General shell script boiler plate, and helper functions. # Written by Gary V. Vaughan, 2004 # Copyright (C) 2004-2015 Free Software Foundation, Inc. # This is free software; see the source for copying conditions. There is NO # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # As a special exception to the GNU General Public License, if you distribute # this file as part of a program or library that is built using GNU Libtool, # you may include this file under the same distribution terms that you use # for the rest of that program. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNES FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # Please report bugs or propose patches to gary@gnu.org. ## ------ ## ## Usage. ## ## ------ ## # Evaluate this file near the top of your script to gain access to # the functions and variables defined here: # # . `echo "$0" | ${SED-sed} 's|[^/]*$||'`/build-aux/funclib.sh # # If you need to override any of the default environment variable # settings, do that before evaluating this file. ## -------------------- ## ## Shell normalisation. ## ## -------------------- ## # Some shells need a little help to be as Bourne compatible as possible. # Before doing anything else, make sure all that help has been provided! DUALCASE=1; export DUALCASE # for MKS sh if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then : emulate sh NULLCMD=: # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which # is contrary to our usage. Disable this feature. alias -g '${1+"$@"}'='"$@"' setopt NO_GLOB_SUBST else case `(set -o) 2>/dev/null` in *posix*) set -o posix ;; esac fi # NLS nuisances: We save the old values in case they are required later. _G_user_locale= _G_safe_locale= for _G_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES do eval "if test set = \"\${$_G_var+set}\"; then save_$_G_var=\$$_G_var $_G_var=C export $_G_var _G_user_locale=\"$_G_var=\\\$save_\$_G_var; \$_G_user_locale\" _G_safe_locale=\"$_G_var=C; \$_G_safe_locale\" fi" done # CDPATH. (unset CDPATH) >/dev/null 2>&1 && unset CDPATH # Make sure IFS has a sensible default sp=' ' nl=' ' IFS="$sp $nl" # There are apparently some retarded systems that use ';' as a PATH separator! if test "${PATH_SEPARATOR+set}" != set; then PATH_SEPARATOR=: (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 || PATH_SEPARATOR=';' } fi ## ------------------------- ## ## Locate command utilities. ## ## ------------------------- ## # func_executable_p FILE # ---------------------- # Check that FILE is an executable regular file. func_executable_p () { test -f "$1" && test -x "$1" } # func_path_progs PROGS_LIST CHECK_FUNC [PATH] # -------------------------------------------- # Search for either a program that responds to --version with output # containing "GNU", or else returned by CHECK_FUNC otherwise, by # trying all the directories in PATH with each of the elements of # PROGS_LIST. # # CHECK_FUNC should accept the path to a candidate program, and # set $func_check_prog_result if it truncates its output less than # $_G_path_prog_max characters. func_path_progs () { _G_progs_list=$1 _G_check_func=$2 _G_PATH=${3-"$PATH"} _G_path_prog_max=0 _G_path_prog_found=false _G_save_IFS=$IFS; IFS=${PATH_SEPARATOR-:} for _G_dir in $_G_PATH; do IFS=$_G_save_IFS test -z "$_G_dir" && _G_dir=. for _G_prog_name in $_G_progs_list; do for _exeext in '' .EXE; do _G_path_prog=$_G_dir/$_G_prog_name$_exeext func_executable_p "$_G_path_prog" || continue case `"$_G_path_prog" --version 2>&1` in *GNU*) func_path_progs_result=$_G_path_prog _G_path_prog_found=: ;; *) $_G_check_func $_G_path_prog func_path_progs_result=$func_check_prog_result ;; esac $_G_path_prog_found && break 3 done done done IFS=$_G_save_IFS test -z "$func_path_progs_result" && { echo "no acceptable sed could be found in \$PATH" >&2 exit 1 } } # We want to be able to use the functions in this file before configure # has figured out where the best binaries are kept, which means we have # to search for them ourselves - except when the results are already set # where we skip the searches. # Unless the user overrides by setting SED, search the path for either GNU # sed, or the sed that truncates its output the least. test -z "$SED" && { _G_sed_script=s/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ for _G_i in 1 2 3 4 5 6 7; do _G_sed_script=$_G_sed_script$nl$_G_sed_script done echo "$_G_sed_script" 2>/dev/null | sed 99q >conftest.sed _G_sed_script= func_check_prog_sed () { _G_path_prog=$1 _G_count=0 printf 0123456789 >conftest.in while : do cat conftest.in conftest.in >conftest.tmp mv conftest.tmp conftest.in cp conftest.in conftest.nl echo '' >> conftest.nl "$_G_path_prog" -f conftest.sed conftest.out 2>/dev/null || break diff conftest.out conftest.nl >/dev/null 2>&1 || break _G_count=`expr $_G_count + 1` if test "$_G_count" -gt "$_G_path_prog_max"; then # Best one so far, save it but keep looking for a better one func_check_prog_result=$_G_path_prog _G_path_prog_max=$_G_count fi # 10*(2^10) chars as input seems more than enough test 10 -lt "$_G_count" && break done rm -f conftest.in conftest.tmp conftest.nl conftest.out } func_path_progs "sed gsed" func_check_prog_sed $PATH:/usr/xpg4/bin rm -f conftest.sed SED=$func_path_progs_result } # Unless the user overrides by setting GREP, search the path for either GNU # grep, or the grep that truncates its output the least. test -z "$GREP" && { func_check_prog_grep () { _G_path_prog=$1 _G_count=0 _G_path_prog_max=0 printf 0123456789 >conftest.in while : do cat conftest.in conftest.in >conftest.tmp mv conftest.tmp conftest.in cp conftest.in conftest.nl echo 'GREP' >> conftest.nl "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' conftest.out 2>/dev/null || break diff conftest.out conftest.nl >/dev/null 2>&1 || break _G_count=`expr $_G_count + 1` if test "$_G_count" -gt "$_G_path_prog_max"; then # Best one so far, save it but keep looking for a better one func_check_prog_result=$_G_path_prog _G_path_prog_max=$_G_count fi # 10*(2^10) chars as input seems more than enough test 10 -lt "$_G_count" && break done rm -f conftest.in conftest.tmp conftest.nl conftest.out } func_path_progs "grep ggrep" func_check_prog_grep $PATH:/usr/xpg4/bin GREP=$func_path_progs_result } ## ------------------------------- ## ## User overridable command paths. ## ## ------------------------------- ## # All uppercase variable names are used for environment variables. These # variables can be overridden by the user before calling a script that # uses them if a suitable command of that name is not already available # in the command search PATH. : ${CP="cp -f"} : ${ECHO="printf %s\n"} : ${EGREP="$GREP -E"} : ${FGREP="$GREP -F"} : ${LN_S="ln -s"} : ${MAKE="make"} : ${MKDIR="mkdir"} : ${MV="mv -f"} : ${RM="rm -f"} : ${SHELL="${CONFIG_SHELL-/bin/sh}"} ## -------------------- ## ## Useful sed snippets. ## ## -------------------- ## sed_dirname='s|/[^/]*$||' sed_basename='s|^.*/||' # Sed substitution that helps us do robust quoting. It backslashifies # metacharacters that are still active within double-quoted strings. sed_quote_subst='s|\([`"$\\]\)|\\\1|g' # Same as above, but do not quote variable references. sed_double_quote_subst='s/\(["`\\]\)/\\\1/g' # Sed substitution that turns a string into a regex matching for the # string literally. sed_make_literal_regex='s|[].[^$\\*\/]|\\&|g' # Sed substitution that converts a w32 file name or path # that contains forward slashes, into one that contains # (escaped) backslashes. A very naive implementation. sed_naive_backslashify='s|\\\\*|\\|g;s|/|\\|g;s|\\|\\\\|g' # Re-'\' parameter expansions in output of sed_double_quote_subst that # were '\'-ed in input to the same. If an odd number of '\' preceded a # '$' in input to sed_double_quote_subst, that '$' was protected from # expansion. Since each input '\' is now two '\'s, look for any number # of runs of four '\'s followed by two '\'s and then a '$'. '\' that '$'. _G_bs='\\' _G_bs2='\\\\' _G_bs4='\\\\\\\\' _G_dollar='\$' sed_double_backslash="\ s/$_G_bs4/&\\ /g s/^$_G_bs2$_G_dollar/$_G_bs&/ s/\\([^$_G_bs]\\)$_G_bs2$_G_dollar/\\1$_G_bs2$_G_bs$_G_dollar/g s/\n//g" ## ----------------- ## ## Global variables. ## ## ----------------- ## # Except for the global variables explicitly listed below, the following # functions in the '^func_' namespace, and the '^require_' namespace # variables initialised in the 'Resource management' section, sourcing # this file will not pollute your global namespace with anything # else. There's no portable way to scope variables in Bourne shell # though, so actually running these functions will sometimes place # results into a variable named after the function, and often use # temporary variables in the '^_G_' namespace. If you are careful to # avoid using those namespaces casually in your sourcing script, things # should continue to work as you expect. And, of course, you can freely # overwrite any of the functions or variables defined here before # calling anything to customize them. EXIT_SUCCESS=0 EXIT_FAILURE=1 EXIT_MISMATCH=63 # $? = 63 is used to indicate version mismatch to missing. EXIT_SKIP=77 # $? = 77 is used to indicate a skipped test to automake. # Allow overriding, eg assuming that you follow the convention of # putting '$debug_cmd' at the start of all your functions, you can get # bash to show function call trace with: # # debug_cmd='eval echo "${FUNCNAME[0]} $*" >&2' bash your-script-name debug_cmd=${debug_cmd-":"} exit_cmd=: # By convention, finish your script with: # # exit $exit_status # # so that you can set exit_status to non-zero if you want to indicate # something went wrong during execution without actually bailing out at # the point of failure. exit_status=$EXIT_SUCCESS # Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh # is ksh but when the shell is invoked as "sh" and the current value of # the _XPG environment variable is not equal to 1 (one), the special # positional parameter $0, within a function call, is the name of the # function. progpath=$0 # The name of this program. progname=`$ECHO "$progpath" |$SED "$sed_basename"` # Make sure we have an absolute progpath for reexecution: case $progpath in [\\/]*|[A-Za-z]:\\*) ;; *[\\/]*) progdir=`$ECHO "$progpath" |$SED "$sed_dirname"` progdir=`cd "$progdir" && pwd` progpath=$progdir/$progname ;; *) _G_IFS=$IFS IFS=${PATH_SEPARATOR-:} for progdir in $PATH; do IFS=$_G_IFS test -x "$progdir/$progname" && break done IFS=$_G_IFS test -n "$progdir" || progdir=`pwd` progpath=$progdir/$progname ;; esac ## ----------------- ## ## Standard options. ## ## ----------------- ## # The following options affect the operation of the functions defined # below, and should be set appropriately depending on run-time para- # meters passed on the command line. opt_dry_run=false opt_quiet=false opt_verbose=false # Categories 'all' and 'none' are always available. Append any others # you will pass as the first argument to func_warning from your own # code. warning_categories= # By default, display warnings according to 'opt_warning_types'. Set # 'warning_func' to ':' to elide all warnings, or func_fatal_error to # treat the next displayed warning as a fatal error. warning_func=func_warn_and_continue # Set to 'all' to display all warnings, 'none' to suppress all # warnings, or a space delimited list of some subset of # 'warning_categories' to display only the listed warnings. opt_warning_types=all ## -------------------- ## ## Resource management. ## ## -------------------- ## # This section contains definitions for functions that each ensure a # particular resource (a file, or a non-empty configuration variable for # example) is available, and if appropriate to extract default values # from pertinent package files. Call them using their associated # 'require_*' variable to ensure that they are executed, at most, once. # # It's entirely deliberate that calling these functions can set # variables that don't obey the namespace limitations obeyed by the rest # of this file, in order that that they be as useful as possible to # callers. # require_term_colors # ------------------- # Allow display of bold text on terminals that support it. require_term_colors=func_require_term_colors func_require_term_colors () { $debug_cmd test -t 1 && { # COLORTERM and USE_ANSI_COLORS environment variables take # precedence, because most terminfo databases neglect to describe # whether color sequences are supported. test -n "${COLORTERM+set}" && : ${USE_ANSI_COLORS="1"} if test 1 = "$USE_ANSI_COLORS"; then # Standard ANSI escape sequences tc_reset='' tc_bold=''; tc_standout='' tc_red=''; tc_green='' tc_blue=''; tc_cyan='' else # Otherwise trust the terminfo database after all. test -n "`tput sgr0 2>/dev/null`" && { tc_reset=`tput sgr0` test -n "`tput bold 2>/dev/null`" && tc_bold=`tput bold` tc_standout=$tc_bold test -n "`tput smso 2>/dev/null`" && tc_standout=`tput smso` test -n "`tput setaf 1 2>/dev/null`" && tc_red=`tput setaf 1` test -n "`tput setaf 2 2>/dev/null`" && tc_green=`tput setaf 2` test -n "`tput setaf 4 2>/dev/null`" && tc_blue=`tput setaf 4` test -n "`tput setaf 5 2>/dev/null`" && tc_cyan=`tput setaf 5` } fi } require_term_colors=: } ## ----------------- ## ## Function library. ## ## ----------------- ## # This section contains a variety of useful functions to call in your # scripts. Take note of the portable wrappers for features provided by # some modern shells, which will fall back to slower equivalents on # less featureful shells. # func_append VAR VALUE # --------------------- # Append VALUE onto the existing contents of VAR. # We should try to minimise forks, especially on Windows where they are # unreasonably slow, so skip the feature probes when bash or zsh are # being used: if test set = "${BASH_VERSION+set}${ZSH_VERSION+set}"; then : ${_G_HAVE_ARITH_OP="yes"} : ${_G_HAVE_XSI_OPS="yes"} # The += operator was introduced in bash 3.1 case $BASH_VERSION in [12].* | 3.0 | 3.0*) ;; *) : ${_G_HAVE_PLUSEQ_OP="yes"} ;; esac fi # _G_HAVE_PLUSEQ_OP # Can be empty, in which case the shell is probed, "yes" if += is # useable or anything else if it does not work. test -z "$_G_HAVE_PLUSEQ_OP" \ && (eval 'x=a; x+=" b"; test "a b" = "$x"') 2>/dev/null \ && _G_HAVE_PLUSEQ_OP=yes if test yes = "$_G_HAVE_PLUSEQ_OP" then # This is an XSI compatible shell, allowing a faster implementation... eval 'func_append () { $debug_cmd eval "$1+=\$2" }' else # ...otherwise fall back to using expr, which is often a shell builtin. func_append () { $debug_cmd eval "$1=\$$1\$2" } fi # func_append_quoted VAR VALUE # ---------------------------- # Quote VALUE and append to the end of shell variable VAR, separated # by a space. if test yes = "$_G_HAVE_PLUSEQ_OP"; then eval 'func_append_quoted () { $debug_cmd func_quote_for_eval "$2" eval "$1+=\\ \$func_quote_for_eval_result" }' else func_append_quoted () { $debug_cmd func_quote_for_eval "$2" eval "$1=\$$1\\ \$func_quote_for_eval_result" } fi # func_append_uniq VAR VALUE # -------------------------- # Append unique VALUE onto the existing contents of VAR, assuming # entries are delimited by the first character of VALUE. For example: # # func_append_uniq options " --another-option option-argument" # # will only append to $options if " --another-option option-argument " # is not already present somewhere in $options already (note spaces at # each end implied by leading space in second argument). func_append_uniq () { $debug_cmd eval _G_current_value='`$ECHO $'$1'`' _G_delim=`expr "$2" : '\(.\)'` case $_G_delim$_G_current_value$_G_delim in *"$2$_G_delim"*) ;; *) func_append "$@" ;; esac } # func_arith TERM... # ------------------ # Set func_arith_result to the result of evaluating TERMs. test -z "$_G_HAVE_ARITH_OP" \ && (eval 'test 2 = $(( 1 + 1 ))') 2>/dev/null \ && _G_HAVE_ARITH_OP=yes if test yes = "$_G_HAVE_ARITH_OP"; then eval 'func_arith () { $debug_cmd func_arith_result=$(( $* )) }' else func_arith () { $debug_cmd func_arith_result=`expr "$@"` } fi # func_basename FILE # ------------------ # Set func_basename_result to FILE with everything up to and including # the last / stripped. if test yes = "$_G_HAVE_XSI_OPS"; then # If this shell supports suffix pattern removal, then use it to avoid # forking. Hide the definitions single quotes in case the shell chokes # on unsupported syntax... _b='func_basename_result=${1##*/}' _d='case $1 in */*) func_dirname_result=${1%/*}$2 ;; * ) func_dirname_result=$3 ;; esac' else # ...otherwise fall back to using sed. _b='func_basename_result=`$ECHO "$1" |$SED "$sed_basename"`' _d='func_dirname_result=`$ECHO "$1" |$SED "$sed_dirname"` if test "X$func_dirname_result" = "X$1"; then func_dirname_result=$3 else func_append func_dirname_result "$2" fi' fi eval 'func_basename () { $debug_cmd '"$_b"' }' # func_dirname FILE APPEND NONDIR_REPLACEMENT # ------------------------------------------- # Compute the dirname of FILE. If nonempty, add APPEND to the result, # otherwise set result to NONDIR_REPLACEMENT. eval 'func_dirname () { $debug_cmd '"$_d"' }' # func_dirname_and_basename FILE APPEND NONDIR_REPLACEMENT # -------------------------------------------------------- # Perform func_basename and func_dirname in a single function # call: # dirname: Compute the dirname of FILE. If nonempty, # add APPEND to the result, otherwise set result # to NONDIR_REPLACEMENT. # value returned in "$func_dirname_result" # basename: Compute filename of FILE. # value retuned in "$func_basename_result" # For efficiency, we do not delegate to the functions above but instead # duplicate the functionality here. eval 'func_dirname_and_basename () { $debug_cmd '"$_b"' '"$_d"' }' # func_echo ARG... # ---------------- # Echo program name prefixed message. func_echo () { $debug_cmd _G_message=$* func_echo_IFS=$IFS IFS=$nl for _G_line in $_G_message; do IFS=$func_echo_IFS $ECHO "$progname: $_G_line" done IFS=$func_echo_IFS } # func_echo_all ARG... # -------------------- # Invoke $ECHO with all args, space-separated. func_echo_all () { $ECHO "$*" } # func_echo_infix_1 INFIX ARG... # ------------------------------ # Echo program name, followed by INFIX on the first line, with any # additional lines not showing INFIX. func_echo_infix_1 () { $debug_cmd $require_term_colors _G_infix=$1; shift _G_indent=$_G_infix _G_prefix="$progname: $_G_infix: " _G_message=$* # Strip color escape sequences before counting printable length for _G_tc in "$tc_reset" "$tc_bold" "$tc_standout" "$tc_red" "$tc_green" "$tc_blue" "$tc_cyan" do test -n "$_G_tc" && { _G_esc_tc=`$ECHO "$_G_tc" | $SED "$sed_make_literal_regex"` _G_indent=`$ECHO "$_G_indent" | $SED "s|$_G_esc_tc||g"` } done _G_indent="$progname: "`echo "$_G_indent" | $SED 's|.| |g'`" " ## exclude from sc_prohibit_nested_quotes func_echo_infix_1_IFS=$IFS IFS=$nl for _G_line in $_G_message; do IFS=$func_echo_infix_1_IFS $ECHO "$_G_prefix$tc_bold$_G_line$tc_reset" >&2 _G_prefix=$_G_indent done IFS=$func_echo_infix_1_IFS } # func_error ARG... # ----------------- # Echo program name prefixed message to standard error. func_error () { $debug_cmd $require_term_colors func_echo_infix_1 " $tc_standout${tc_red}error$tc_reset" "$*" >&2 } # func_fatal_error ARG... # ----------------------- # Echo program name prefixed message to standard error, and exit. func_fatal_error () { $debug_cmd func_error "$*" exit $EXIT_FAILURE } # func_grep EXPRESSION FILENAME # ----------------------------- # Check whether EXPRESSION matches any line of FILENAME, without output. func_grep () { $debug_cmd $GREP "$1" "$2" >/dev/null 2>&1 } # func_len STRING # --------------- # Set func_len_result to the length of STRING. STRING may not # start with a hyphen. test -z "$_G_HAVE_XSI_OPS" \ && (eval 'x=a/b/c; test 5aa/bb/cc = "${#x}${x%%/*}${x%/*}${x#*/}${x##*/}"') 2>/dev/null \ && _G_HAVE_XSI_OPS=yes if test yes = "$_G_HAVE_XSI_OPS"; then eval 'func_len () { $debug_cmd func_len_result=${#1} }' else func_len () { $debug_cmd func_len_result=`expr "$1" : ".*" 2>/dev/null || echo $max_cmd_len` } fi # func_mkdir_p DIRECTORY-PATH # --------------------------- # Make sure the entire path to DIRECTORY-PATH is available. func_mkdir_p () { $debug_cmd _G_directory_path=$1 _G_dir_list= if test -n "$_G_directory_path" && test : != "$opt_dry_run"; then # Protect directory names starting with '-' case $_G_directory_path in -*) _G_directory_path=./$_G_directory_path ;; esac # While some portion of DIR does not yet exist... while test ! -d "$_G_directory_path"; do # ...make a list in topmost first order. Use a colon delimited # list incase some portion of path contains whitespace. _G_dir_list=$_G_directory_path:$_G_dir_list # If the last portion added has no slash in it, the list is done case $_G_directory_path in */*) ;; *) break ;; esac # ...otherwise throw away the child directory and loop _G_directory_path=`$ECHO "$_G_directory_path" | $SED -e "$sed_dirname"` done _G_dir_list=`$ECHO "$_G_dir_list" | $SED 's|:*$||'` func_mkdir_p_IFS=$IFS; IFS=: for _G_dir in $_G_dir_list; do IFS=$func_mkdir_p_IFS # mkdir can fail with a 'File exist' error if two processes # try to create one of the directories concurrently. Don't # stop in that case! $MKDIR "$_G_dir" 2>/dev/null || : done IFS=$func_mkdir_p_IFS # Bail out if we (or some other process) failed to create a directory. test -d "$_G_directory_path" || \ func_fatal_error "Failed to create '$1'" fi } # func_mktempdir [BASENAME] # ------------------------- # Make a temporary directory that won't clash with other running # libtool processes, and avoids race conditions if possible. If # given, BASENAME is the basename for that directory. func_mktempdir () { $debug_cmd _G_template=${TMPDIR-/tmp}/${1-$progname} if test : = "$opt_dry_run"; then # Return a directory name, but don't create it in dry-run mode _G_tmpdir=$_G_template-$$ else # If mktemp works, use that first and foremost _G_tmpdir=`mktemp -d "$_G_template-XXXXXXXX" 2>/dev/null` if test ! -d "$_G_tmpdir"; then # Failing that, at least try and use $RANDOM to avoid a race _G_tmpdir=$_G_template-${RANDOM-0}$$ func_mktempdir_umask=`umask` umask 0077 $MKDIR "$_G_tmpdir" umask $func_mktempdir_umask fi # If we're not in dry-run mode, bomb out on failure test -d "$_G_tmpdir" || \ func_fatal_error "cannot create temporary directory '$_G_tmpdir'" fi $ECHO "$_G_tmpdir" } # func_normal_abspath PATH # ------------------------ # Remove doubled-up and trailing slashes, "." path components, # and cancel out any ".." path components in PATH after making # it an absolute path. func_normal_abspath () { $debug_cmd # These SED scripts presuppose an absolute path with a trailing slash. _G_pathcar='s|^/\([^/]*\).*$|\1|' _G_pathcdr='s|^/[^/]*||' _G_removedotparts=':dotsl s|/\./|/|g t dotsl s|/\.$|/|' _G_collapseslashes='s|/\{1,\}|/|g' _G_finalslash='s|/*$|/|' # Start from root dir and reassemble the path. func_normal_abspath_result= func_normal_abspath_tpath=$1 func_normal_abspath_altnamespace= case $func_normal_abspath_tpath in "") # Empty path, that just means $cwd. func_stripname '' '/' "`pwd`" func_normal_abspath_result=$func_stripname_result return ;; # The next three entries are used to spot a run of precisely # two leading slashes without using negated character classes; # we take advantage of case's first-match behaviour. ///*) # Unusual form of absolute path, do nothing. ;; //*) # Not necessarily an ordinary path; POSIX reserves leading '//' # and for example Cygwin uses it to access remote file shares # over CIFS/SMB, so we conserve a leading double slash if found. func_normal_abspath_altnamespace=/ ;; /*) # Absolute path, do nothing. ;; *) # Relative path, prepend $cwd. func_normal_abspath_tpath=`pwd`/$func_normal_abspath_tpath ;; esac # Cancel out all the simple stuff to save iterations. We also want # the path to end with a slash for ease of parsing, so make sure # there is one (and only one) here. func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \ -e "$_G_removedotparts" -e "$_G_collapseslashes" -e "$_G_finalslash"` while :; do # Processed it all yet? if test / = "$func_normal_abspath_tpath"; then # If we ascended to the root using ".." the result may be empty now. if test -z "$func_normal_abspath_result"; then func_normal_abspath_result=/ fi break fi func_normal_abspath_tcomponent=`$ECHO "$func_normal_abspath_tpath" | $SED \ -e "$_G_pathcar"` func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \ -e "$_G_pathcdr"` # Figure out what to do with it case $func_normal_abspath_tcomponent in "") # Trailing empty path component, ignore it. ;; ..) # Parent dir; strip last assembled component from result. func_dirname "$func_normal_abspath_result" func_normal_abspath_result=$func_dirname_result ;; *) # Actual path component, append it. func_append func_normal_abspath_result "/$func_normal_abspath_tcomponent" ;; esac done # Restore leading double-slash if one was found on entry. func_normal_abspath_result=$func_normal_abspath_altnamespace$func_normal_abspath_result } # func_notquiet ARG... # -------------------- # Echo program name prefixed message only when not in quiet mode. func_notquiet () { $debug_cmd $opt_quiet || func_echo ${1+"$@"} # A bug in bash halts the script if the last line of a function # fails when set -e is in force, so we need another command to # work around that: : } # func_relative_path SRCDIR DSTDIR # -------------------------------- # Set func_relative_path_result to the relative path from SRCDIR to DSTDIR. func_relative_path () { $debug_cmd func_relative_path_result= func_normal_abspath "$1" func_relative_path_tlibdir=$func_normal_abspath_result func_normal_abspath "$2" func_relative_path_tbindir=$func_normal_abspath_result # Ascend the tree starting from libdir while :; do # check if we have found a prefix of bindir case $func_relative_path_tbindir in $func_relative_path_tlibdir) # found an exact match func_relative_path_tcancelled= break ;; $func_relative_path_tlibdir*) # found a matching prefix func_stripname "$func_relative_path_tlibdir" '' "$func_relative_path_tbindir" func_relative_path_tcancelled=$func_stripname_result if test -z "$func_relative_path_result"; then func_relative_path_result=. fi break ;; *) func_dirname $func_relative_path_tlibdir func_relative_path_tlibdir=$func_dirname_result if test -z "$func_relative_path_tlibdir"; then # Have to descend all the way to the root! func_relative_path_result=../$func_relative_path_result func_relative_path_tcancelled=$func_relative_path_tbindir break fi func_relative_path_result=../$func_relative_path_result ;; esac done # Now calculate path; take care to avoid doubling-up slashes. func_stripname '' '/' "$func_relative_path_result" func_relative_path_result=$func_stripname_result func_stripname '/' '/' "$func_relative_path_tcancelled" if test -n "$func_stripname_result"; then func_append func_relative_path_result "/$func_stripname_result" fi # Normalisation. If bindir is libdir, return '.' else relative path. if test -n "$func_relative_path_result"; then func_stripname './' '' "$func_relative_path_result" func_relative_path_result=$func_stripname_result fi test -n "$func_relative_path_result" || func_relative_path_result=. : } # func_quote_for_eval ARG... # -------------------------- # Aesthetically quote ARGs to be evaled later. # This function returns two values: # i) func_quote_for_eval_result # double-quoted, suitable for a subsequent eval # ii) func_quote_for_eval_unquoted_result # has all characters that are still active within double # quotes backslashified. func_quote_for_eval () { $debug_cmd func_quote_for_eval_unquoted_result= func_quote_for_eval_result= while test 0 -lt $#; do case $1 in *[\\\`\"\$]*) _G_unquoted_arg=`printf '%s\n' "$1" |$SED "$sed_quote_subst"` ;; *) _G_unquoted_arg=$1 ;; esac if test -n "$func_quote_for_eval_unquoted_result"; then func_append func_quote_for_eval_unquoted_result " $_G_unquoted_arg" else func_append func_quote_for_eval_unquoted_result "$_G_unquoted_arg" fi case $_G_unquoted_arg in # Double-quote args containing shell metacharacters to delay # word splitting, command substitution and variable expansion # for a subsequent eval. # Many Bourne shells cannot handle close brackets correctly # in scan sets, so we specify it separately. *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*|"") _G_quoted_arg=\"$_G_unquoted_arg\" ;; *) _G_quoted_arg=$_G_unquoted_arg ;; esac if test -n "$func_quote_for_eval_result"; then func_append func_quote_for_eval_result " $_G_quoted_arg" else func_append func_quote_for_eval_result "$_G_quoted_arg" fi shift done } # func_quote_for_expand ARG # ------------------------- # Aesthetically quote ARG to be evaled later; same as above, # but do not quote variable references. func_quote_for_expand () { $debug_cmd case $1 in *[\\\`\"]*) _G_arg=`$ECHO "$1" | $SED \ -e "$sed_double_quote_subst" -e "$sed_double_backslash"` ;; *) _G_arg=$1 ;; esac case $_G_arg in # Double-quote args containing shell metacharacters to delay # word splitting and command substitution for a subsequent eval. # Many Bourne shells cannot handle close brackets correctly # in scan sets, so we specify it separately. *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*|"") _G_arg=\"$_G_arg\" ;; esac func_quote_for_expand_result=$_G_arg } # func_stripname PREFIX SUFFIX NAME # --------------------------------- # strip PREFIX and SUFFIX from NAME, and store in func_stripname_result. # PREFIX and SUFFIX must not contain globbing or regex special # characters, hashes, percent signs, but SUFFIX may contain a leading # dot (in which case that matches only a dot). if test yes = "$_G_HAVE_XSI_OPS"; then eval 'func_stripname () { $debug_cmd # pdksh 5.2.14 does not do ${X%$Y} correctly if both X and Y are # positional parameters, so assign one to ordinary variable first. func_stripname_result=$3 func_stripname_result=${func_stripname_result#"$1"} func_stripname_result=${func_stripname_result%"$2"} }' else func_stripname () { $debug_cmd case $2 in .*) func_stripname_result=`$ECHO "$3" | $SED -e "s%^$1%%" -e "s%\\\\$2\$%%"`;; *) func_stripname_result=`$ECHO "$3" | $SED -e "s%^$1%%" -e "s%$2\$%%"`;; esac } fi # func_show_eval CMD [FAIL_EXP] # ----------------------------- # Unless opt_quiet is true, then output CMD. Then, if opt_dryrun is # not true, evaluate CMD. If the evaluation of CMD fails, and FAIL_EXP # is given, then evaluate it. func_show_eval () { $debug_cmd _G_cmd=$1 _G_fail_exp=${2-':'} func_quote_for_expand "$_G_cmd" eval "func_notquiet $func_quote_for_expand_result" $opt_dry_run || { eval "$_G_cmd" _G_status=$? if test 0 -ne "$_G_status"; then eval "(exit $_G_status); $_G_fail_exp" fi } } # func_show_eval_locale CMD [FAIL_EXP] # ------------------------------------ # Unless opt_quiet is true, then output CMD. Then, if opt_dryrun is # not true, evaluate CMD. If the evaluation of CMD fails, and FAIL_EXP # is given, then evaluate it. Use the saved locale for evaluation. func_show_eval_locale () { $debug_cmd _G_cmd=$1 _G_fail_exp=${2-':'} $opt_quiet || { func_quote_for_expand "$_G_cmd" eval "func_echo $func_quote_for_expand_result" } $opt_dry_run || { eval "$_G_user_locale $_G_cmd" _G_status=$? eval "$_G_safe_locale" if test 0 -ne "$_G_status"; then eval "(exit $_G_status); $_G_fail_exp" fi } } # func_tr_sh # ---------- # Turn $1 into a string suitable for a shell variable name. # Result is stored in $func_tr_sh_result. All characters # not in the set a-zA-Z0-9_ are replaced with '_'. Further, # if $1 begins with a digit, a '_' is prepended as well. func_tr_sh () { $debug_cmd case $1 in [0-9]* | *[!a-zA-Z0-9_]*) func_tr_sh_result=`$ECHO "$1" | $SED -e 's/^\([0-9]\)/_\1/' -e 's/[^a-zA-Z0-9_]/_/g'` ;; * ) func_tr_sh_result=$1 ;; esac } # func_verbose ARG... # ------------------- # Echo program name prefixed message in verbose mode only. func_verbose () { $debug_cmd $opt_verbose && func_echo "$*" : } # func_warn_and_continue ARG... # ----------------------------- # Echo program name prefixed warning message to standard error. func_warn_and_continue () { $debug_cmd $require_term_colors func_echo_infix_1 "${tc_red}warning$tc_reset" "$*" >&2 } # func_warning CATEGORY ARG... # ---------------------------- # Echo program name prefixed warning message to standard error. Warning # messages can be filtered according to CATEGORY, where this function # elides messages where CATEGORY is not listed in the global variable # 'opt_warning_types'. func_warning () { $debug_cmd # CATEGORY must be in the warning_categories list! case " $warning_categories " in *" $1 "*) ;; *) func_internal_error "invalid warning category '$1'" ;; esac _G_category=$1 shift case " $opt_warning_types " in *" $_G_category "*) $warning_func ${1+"$@"} ;; esac } # func_sort_ver VER1 VER2 # ----------------------- # 'sort -V' is not generally available. # Note this deviates from the version comparison in automake # in that it treats 1.5 < 1.5.0, and treats 1.4.4a < 1.4-p3a # but this should suffice as we won't be specifying old # version formats or redundant trailing .0 in bootstrap.conf. # If we did want full compatibility then we should probably # use m4_version_compare from autoconf. func_sort_ver () { $debug_cmd printf '%s\n%s\n' "$1" "$2" \ | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n -k 6,6n -k 7,7n -k 8,8n -k 9,9n } # func_lt_ver PREV CURR # --------------------- # Return true if PREV and CURR are in the correct order according to # func_sort_ver, otherwise false. Use it like this: # # func_lt_ver "$prev_ver" "$proposed_ver" || func_fatal_error "..." func_lt_ver () { $debug_cmd test "x$1" = x`func_sort_ver "$1" "$2" | $SED 1q` } # Local variables: # mode: shell-script # sh-indentation: 2 # eval: (add-hook 'before-save-hook 'time-stamp) # time-stamp-pattern: "10/scriptversion=%:y-%02m-%02d.%02H; # UTC" # time-stamp-time-zone: "UTC" # End: #! /bin/sh # Set a version string for this script. scriptversion=2015-10-07.11; # UTC # A portable, pluggable option parser for Bourne shell. # Written by Gary V. Vaughan, 2010 # Copyright (C) 2010-2015 Free Software Foundation, Inc. # This is free software; see the source for copying conditions. There is NO # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # Please report bugs or propose patches to gary@gnu.org. ## ------ ## ## Usage. ## ## ------ ## # This file is a library for parsing options in your shell scripts along # with assorted other useful supporting features that you can make use # of too. # # For the simplest scripts you might need only: # # #!/bin/sh # . relative/path/to/funclib.sh # . relative/path/to/options-parser # scriptversion=1.0 # func_options ${1+"$@"} # eval set dummy "$func_options_result"; shift # ...rest of your script... # # In order for the '--version' option to work, you will need to have a # suitably formatted comment like the one at the top of this file # starting with '# Written by ' and ending with '# warranty; '. # # For '-h' and '--help' to work, you will also need a one line # description of your script's purpose in a comment directly above the # '# Written by ' line, like the one at the top of this file. # # The default options also support '--debug', which will turn on shell # execution tracing (see the comment above debug_cmd below for another # use), and '--verbose' and the func_verbose function to allow your script # to display verbose messages only when your user has specified # '--verbose'. # # After sourcing this file, you can plug processing for additional # options by amending the variables from the 'Configuration' section # below, and following the instructions in the 'Option parsing' # section further down. ## -------------- ## ## Configuration. ## ## -------------- ## # You should override these variables in your script after sourcing this # file so that they reflect the customisations you have added to the # option parser. # The usage line for option parsing errors and the start of '-h' and # '--help' output messages. You can embed shell variables for delayed # expansion at the time the message is displayed, but you will need to # quote other shell meta-characters carefully to prevent them being # expanded when the contents are evaled. usage='$progpath [OPTION]...' # Short help message in response to '-h' and '--help'. Add to this or # override it after sourcing this library to reflect the full set of # options your script accepts. usage_message="\ --debug enable verbose shell tracing -W, --warnings=CATEGORY report the warnings falling in CATEGORY [all] -v, --verbose verbosely report processing --version print version information and exit -h, --help print short or long help message and exit " # Additional text appended to 'usage_message' in response to '--help'. long_help_message=" Warning categories include: 'all' show all warnings 'none' turn off all the warnings 'error' warnings are treated as fatal errors" # Help message printed before fatal option parsing errors. fatal_help="Try '\$progname --help' for more information." ## ------------------------- ## ## Hook function management. ## ## ------------------------- ## # This section contains functions for adding, removing, and running hooks # to the main code. A hook is just a named list of of function, that can # be run in order later on. # func_hookable FUNC_NAME # ----------------------- # Declare that FUNC_NAME will run hooks added with # 'func_add_hook FUNC_NAME ...'. func_hookable () { $debug_cmd func_append hookable_fns " $1" } # func_add_hook FUNC_NAME HOOK_FUNC # --------------------------------- # Request that FUNC_NAME call HOOK_FUNC before it returns. FUNC_NAME must # first have been declared "hookable" by a call to 'func_hookable'. func_add_hook () { $debug_cmd case " $hookable_fns " in *" $1 "*) ;; *) func_fatal_error "'$1' does not accept hook functions." ;; esac eval func_append ${1}_hooks '" $2"' } # func_remove_hook FUNC_NAME HOOK_FUNC # ------------------------------------ # Remove HOOK_FUNC from the list of functions called by FUNC_NAME. func_remove_hook () { $debug_cmd eval ${1}_hooks='`$ECHO "\$'$1'_hooks" |$SED "s| '$2'||"`' } # func_run_hooks FUNC_NAME [ARG]... # --------------------------------- # Run all hook functions registered to FUNC_NAME. # It is assumed that the list of hook functions contains nothing more # than a whitespace-delimited list of legal shell function names, and # no effort is wasted trying to catch shell meta-characters or preserve # whitespace. func_run_hooks () { $debug_cmd _G_rc_run_hooks=false case " $hookable_fns " in *" $1 "*) ;; *) func_fatal_error "'$1' does not support hook funcions.n" ;; esac eval _G_hook_fns=\$$1_hooks; shift for _G_hook in $_G_hook_fns; do if eval $_G_hook '"$@"'; then # store returned options list back into positional # parameters for next 'cmd' execution. eval _G_hook_result=\$${_G_hook}_result eval set dummy "$_G_hook_result"; shift _G_rc_run_hooks=: fi done $_G_rc_run_hooks && func_run_hooks_result=$_G_hook_result } ## --------------- ## ## Option parsing. ## ## --------------- ## # In order to add your own option parsing hooks, you must accept the # full positional parameter list in your hook function, you may remove/edit # any options that you action, and then pass back the remaining unprocessed # options in '_result', escaped suitably for # 'eval'. In this case you also must return $EXIT_SUCCESS to let the # hook's caller know that it should pay attention to # '_result'. Returning $EXIT_FAILURE signalizes that # arguments are left untouched by the hook and therefore caller will ignore the # result variable. # # Like this: # # my_options_prep () # { # $debug_cmd # # # Extend the existing usage message. # usage_message=$usage_message' # -s, --silent don'\''t print informational messages # ' # # No change in '$@' (ignored completely by this hook). There is # # no need to do the equivalent (but slower) action: # # func_quote_for_eval ${1+"$@"} # # my_options_prep_result=$func_quote_for_eval_result # false # } # func_add_hook func_options_prep my_options_prep # # # my_silent_option () # { # $debug_cmd # # args_changed=false # # # Note that for efficiency, we parse as many options as we can # # recognise in a loop before passing the remainder back to the # # caller on the first unrecognised argument we encounter. # while test $# -gt 0; do # opt=$1; shift # case $opt in # --silent|-s) opt_silent=: # args_changed=: # ;; # # Separate non-argument short options: # -s*) func_split_short_opt "$_G_opt" # set dummy "$func_split_short_opt_name" \ # "-$func_split_short_opt_arg" ${1+"$@"} # shift # args_changed=: # ;; # *) # Make sure the first unrecognised option "$_G_opt" # # is added back to "$@", we could need that later # # if $args_changed is true. # set dummy "$_G_opt" ${1+"$@"}; shift; break ;; # esac # done # # if $args_changed; then # func_quote_for_eval ${1+"$@"} # my_silent_option_result=$func_quote_for_eval_result # fi # # $args_changed # } # func_add_hook func_parse_options my_silent_option # # # my_option_validation () # { # $debug_cmd # # $opt_silent && $opt_verbose && func_fatal_help "\ # '--silent' and '--verbose' options are mutually exclusive." # # false # } # func_add_hook func_validate_options my_option_validation # # You'll also need to manually amend $usage_message to reflect the extra # options you parse. It's preferable to append if you can, so that # multiple option parsing hooks can be added safely. # func_options_finish [ARG]... # ---------------------------- # Finishing the option parse loop (call 'func_options' hooks ATM). func_options_finish () { $debug_cmd _G_func_options_finish_exit=false if func_run_hooks func_options ${1+"$@"}; then func_options_finish_result=$func_run_hooks_result _G_func_options_finish_exit=: fi $_G_func_options_finish_exit } # func_options [ARG]... # --------------------- # All the functions called inside func_options are hookable. See the # individual implementations for details. func_hookable func_options func_options () { $debug_cmd _G_rc_options=false for my_func in options_prep parse_options validate_options options_finish do if eval func_$my_func '${1+"$@"}'; then eval _G_res_var='$'"func_${my_func}_result" eval set dummy "$_G_res_var" ; shift _G_rc_options=: fi done # Save modified positional parameters for caller. As a top-level # options-parser function we always need to set the 'func_options_result' # variable (regardless the $_G_rc_options value). if $_G_rc_options; then func_options_result=$_G_res_var else func_quote_for_eval ${1+"$@"} func_options_result=$func_quote_for_eval_result fi $_G_rc_options } # func_options_prep [ARG]... # -------------------------- # All initialisations required before starting the option parse loop. # Note that when calling hook functions, we pass through the list of # positional parameters. If a hook function modifies that list, and # needs to propagate that back to rest of this script, then the complete # modified list must be put in 'func_run_hooks_result' before # returning $EXIT_SUCCESS (otherwise $EXIT_FAILURE is returned). func_hookable func_options_prep func_options_prep () { $debug_cmd # Option defaults: opt_verbose=false opt_warning_types= _G_rc_options_prep=false if func_run_hooks func_options_prep ${1+"$@"}; then _G_rc_options_prep=: # save modified positional parameters for caller func_options_prep_result=$func_run_hooks_result fi $_G_rc_options_prep } # func_parse_options [ARG]... # --------------------------- # The main option parsing loop. func_hookable func_parse_options func_parse_options () { $debug_cmd func_parse_options_result= _G_rc_parse_options=false # this just eases exit handling while test $# -gt 0; do # Defer to hook functions for initial option parsing, so they # get priority in the event of reusing an option name. if func_run_hooks func_parse_options ${1+"$@"}; then eval set dummy "$func_run_hooks_result"; shift _G_rc_parse_options=: fi # Break out of the loop if we already parsed every option. test $# -gt 0 || break _G_match_parse_options=: _G_opt=$1 shift case $_G_opt in --debug|-x) debug_cmd='set -x' func_echo "enabling shell trace mode" $debug_cmd ;; --no-warnings|--no-warning|--no-warn) set dummy --warnings none ${1+"$@"} shift ;; --warnings|--warning|-W) if test $# = 0 && func_missing_arg $_G_opt; then _G_rc_parse_options=: break fi case " $warning_categories $1" in *" $1 "*) # trailing space prevents matching last $1 above func_append_uniq opt_warning_types " $1" ;; *all) opt_warning_types=$warning_categories ;; *none) opt_warning_types=none warning_func=: ;; *error) opt_warning_types=$warning_categories warning_func=func_fatal_error ;; *) func_fatal_error \ "unsupported warning category: '$1'" ;; esac shift ;; --verbose|-v) opt_verbose=: ;; --version) func_version ;; -\?|-h) func_usage ;; --help) func_help ;; # Separate optargs to long options (plugins may need this): --*=*) func_split_equals "$_G_opt" set dummy "$func_split_equals_lhs" \ "$func_split_equals_rhs" ${1+"$@"} shift ;; # Separate optargs to short options: -W*) func_split_short_opt "$_G_opt" set dummy "$func_split_short_opt_name" \ "$func_split_short_opt_arg" ${1+"$@"} shift ;; # Separate non-argument short options: -\?*|-h*|-v*|-x*) func_split_short_opt "$_G_opt" set dummy "$func_split_short_opt_name" \ "-$func_split_short_opt_arg" ${1+"$@"} shift ;; --) _G_rc_parse_options=: ; break ;; -*) func_fatal_help "unrecognised option: '$_G_opt'" ;; *) set dummy "$_G_opt" ${1+"$@"}; shift _G_match_parse_options=false break ;; esac $_G_match_parse_options && _G_rc_parse_options=: done if $_G_rc_parse_options; then # save modified positional parameters for caller func_quote_for_eval ${1+"$@"} func_parse_options_result=$func_quote_for_eval_result fi $_G_rc_parse_options } # func_validate_options [ARG]... # ------------------------------ # Perform any sanity checks on option settings and/or unconsumed # arguments. func_hookable func_validate_options func_validate_options () { $debug_cmd _G_rc_validate_options=false # Display all warnings if -W was not given. test -n "$opt_warning_types" || opt_warning_types=" $warning_categories" if func_run_hooks func_validate_options ${1+"$@"}; then # save modified positional parameters for caller func_validate_options_result=$func_run_hooks_result _G_rc_validate_options=: fi # Bail if the options were screwed! $exit_cmd $EXIT_FAILURE $_G_rc_validate_options } ## ----------------- ## ## Helper functions. ## ## ----------------- ## # This section contains the helper functions used by the rest of the # hookable option parser framework in ascii-betical order. # func_fatal_help ARG... # ---------------------- # Echo program name prefixed message to standard error, followed by # a help hint, and exit. func_fatal_help () { $debug_cmd eval \$ECHO \""Usage: $usage"\" eval \$ECHO \""$fatal_help"\" func_error ${1+"$@"} exit $EXIT_FAILURE } # func_help # --------- # Echo long help message to standard output and exit. func_help () { $debug_cmd func_usage_message $ECHO "$long_help_message" exit 0 } # func_missing_arg ARGNAME # ------------------------ # Echo program name prefixed message to standard error and set global # exit_cmd. func_missing_arg () { $debug_cmd func_error "Missing argument for '$1'." exit_cmd=exit } # func_split_equals STRING # ------------------------ # Set func_split_equals_lhs and func_split_equals_rhs shell variables after # splitting STRING at the '=' sign. test -z "$_G_HAVE_XSI_OPS" \ && (eval 'x=a/b/c; test 5aa/bb/cc = "${#x}${x%%/*}${x%/*}${x#*/}${x##*/}"') 2>/dev/null \ && _G_HAVE_XSI_OPS=yes if test yes = "$_G_HAVE_XSI_OPS" then # This is an XSI compatible shell, allowing a faster implementation... eval 'func_split_equals () { $debug_cmd func_split_equals_lhs=${1%%=*} func_split_equals_rhs=${1#*=} test "x$func_split_equals_lhs" = "x$1" \ && func_split_equals_rhs= }' else # ...otherwise fall back to using expr, which is often a shell builtin. func_split_equals () { $debug_cmd func_split_equals_lhs=`expr "x$1" : 'x\([^=]*\)'` func_split_equals_rhs= test "x$func_split_equals_lhs" = "x$1" \ || func_split_equals_rhs=`expr "x$1" : 'x[^=]*=\(.*\)$'` } fi #func_split_equals # func_split_short_opt SHORTOPT # ----------------------------- # Set func_split_short_opt_name and func_split_short_opt_arg shell # variables after splitting SHORTOPT after the 2nd character. if test yes = "$_G_HAVE_XSI_OPS" then # This is an XSI compatible shell, allowing a faster implementation... eval 'func_split_short_opt () { $debug_cmd func_split_short_opt_arg=${1#??} func_split_short_opt_name=${1%"$func_split_short_opt_arg"} }' else # ...otherwise fall back to using expr, which is often a shell builtin. func_split_short_opt () { $debug_cmd func_split_short_opt_name=`expr "x$1" : 'x-\(.\)'` func_split_short_opt_arg=`expr "x$1" : 'x-.\(.*\)$'` } fi #func_split_short_opt # func_usage # ---------- # Echo short help message to standard output and exit. func_usage () { $debug_cmd func_usage_message $ECHO "Run '$progname --help |${PAGER-more}' for full usage" exit 0 } # func_usage_message # ------------------ # Echo short help message to standard output. func_usage_message () { $debug_cmd eval \$ECHO \""Usage: $usage"\" echo $SED -n 's|^# || /^Written by/{ x;p;x } h /^Written by/q' < "$progpath" echo eval \$ECHO \""$usage_message"\" } # func_version # ------------ # Echo version message to standard output and exit. func_version () { $debug_cmd printf '%s\n' "$progname $scriptversion" $SED -n ' /(C)/!b go :more /\./!{ N s|\n# | | b more } :go /^# Written by /,/# warranty; / { s|^# || s|^# *$|| s|\((C)\)[ 0-9,-]*[ ,-]\([1-9][0-9]* \)|\1 \2| p } /^# Written by / { s|^# || p } /^warranty; /q' < "$progpath" exit $? } # Local variables: # mode: shell-script # sh-indentation: 2 # eval: (add-hook 'before-save-hook 'time-stamp) # time-stamp-pattern: "10/scriptversion=%:y-%02m-%02d.%02H; # UTC" # time-stamp-time-zone: "UTC" # End: # Set a version string. scriptversion='(GNU libtool) 2.4.6' # func_echo ARG... # ---------------- # Libtool also displays the current mode in messages, so override # funclib.sh func_echo with this custom definition. func_echo () { $debug_cmd _G_message=$* func_echo_IFS=$IFS IFS=$nl for _G_line in $_G_message; do IFS=$func_echo_IFS $ECHO "$progname${opt_mode+: $opt_mode}: $_G_line" done IFS=$func_echo_IFS } # func_warning ARG... # ------------------- # Libtool warnings are not categorized, so override funclib.sh # func_warning with this simpler definition. func_warning () { $debug_cmd $warning_func ${1+"$@"} } ## ---------------- ## ## Options parsing. ## ## ---------------- ## # Hook in the functions to make sure our own options are parsed during # the option parsing loop. usage='$progpath [OPTION]... [MODE-ARG]...' # Short help message in response to '-h'. usage_message="Options: --config show all configuration variables --debug enable verbose shell tracing -n, --dry-run display commands without modifying any files --features display basic configuration information and exit --mode=MODE use operation mode MODE --no-warnings equivalent to '-Wnone' --preserve-dup-deps don't remove duplicate dependency libraries --quiet, --silent don't print informational messages --tag=TAG use configuration variables from tag TAG -v, --verbose print more informational messages than default --version print version information -W, --warnings=CATEGORY report the warnings falling in CATEGORY [all] -h, --help, --help-all print short, long, or detailed help message " # Additional text appended to 'usage_message' in response to '--help'. func_help () { $debug_cmd func_usage_message $ECHO "$long_help_message MODE must be one of the following: clean remove files from the build directory compile compile a source file into a libtool object execute automatically set library path, then run a program finish complete the installation of libtool libraries install install libraries or executables link create a library or an executable uninstall remove libraries from an installed directory MODE-ARGS vary depending on the MODE. When passed as first option, '--mode=MODE' may be abbreviated as 'MODE' or a unique abbreviation of that. Try '$progname --help --mode=MODE' for a more detailed description of MODE. When reporting a bug, please describe a test case to reproduce it and include the following information: host-triplet: $host shell: $SHELL compiler: $LTCC compiler flags: $LTCFLAGS linker: $LD (gnu? $with_gnu_ld) version: $progname $scriptversion Debian-2.4.6-10 automake: `($AUTOMAKE --version) 2>/dev/null |$SED 1q` autoconf: `($AUTOCONF --version) 2>/dev/null |$SED 1q` Report bugs to . GNU libtool home page: . General help using GNU software: ." exit 0 } # func_lo2o OBJECT-NAME # --------------------- # Transform OBJECT-NAME from a '.lo' suffix to the platform specific # object suffix. lo2o=s/\\.lo\$/.$objext/ o2lo=s/\\.$objext\$/.lo/ if test yes = "$_G_HAVE_XSI_OPS"; then eval 'func_lo2o () { case $1 in *.lo) func_lo2o_result=${1%.lo}.$objext ;; * ) func_lo2o_result=$1 ;; esac }' # func_xform LIBOBJ-OR-SOURCE # --------------------------- # Transform LIBOBJ-OR-SOURCE from a '.o' or '.c' (or otherwise) # suffix to a '.lo' libtool-object suffix. eval 'func_xform () { func_xform_result=${1%.*}.lo }' else # ...otherwise fall back to using sed. func_lo2o () { func_lo2o_result=`$ECHO "$1" | $SED "$lo2o"` } func_xform () { func_xform_result=`$ECHO "$1" | $SED 's|\.[^.]*$|.lo|'` } fi # func_fatal_configuration ARG... # ------------------------------- # Echo program name prefixed message to standard error, followed by # a configuration failure hint, and exit. func_fatal_configuration () { func__fatal_error ${1+"$@"} \ "See the $PACKAGE documentation for more information." \ "Fatal configuration error." } # func_config # ----------- # Display the configuration for all the tags in this script. func_config () { re_begincf='^# ### BEGIN LIBTOOL' re_endcf='^# ### END LIBTOOL' # Default configuration. $SED "1,/$re_begincf CONFIG/d;/$re_endcf CONFIG/,\$d" < "$progpath" # Now print the configurations for the tags. for tagname in $taglist; do $SED -n "/$re_begincf TAG CONFIG: $tagname\$/,/$re_endcf TAG CONFIG: $tagname\$/p" < "$progpath" done exit $? } # func_features # ------------- # Display the features supported by this script. func_features () { echo "host: $host" if test yes = "$build_libtool_libs"; then echo "enable shared libraries" else echo "disable shared libraries" fi if test yes = "$build_old_libs"; then echo "enable static libraries" else echo "disable static libraries" fi exit $? } # func_enable_tag TAGNAME # ----------------------- # Verify that TAGNAME is valid, and either flag an error and exit, or # enable the TAGNAME tag. We also add TAGNAME to the global $taglist # variable here. func_enable_tag () { # Global variable: tagname=$1 re_begincf="^# ### BEGIN LIBTOOL TAG CONFIG: $tagname\$" re_endcf="^# ### END LIBTOOL TAG CONFIG: $tagname\$" sed_extractcf=/$re_begincf/,/$re_endcf/p # Validate tagname. case $tagname in *[!-_A-Za-z0-9,/]*) func_fatal_error "invalid tag name: $tagname" ;; esac # Don't test for the "default" C tag, as we know it's # there but not specially marked. case $tagname in CC) ;; *) if $GREP "$re_begincf" "$progpath" >/dev/null 2>&1; then taglist="$taglist $tagname" # Evaluate the configuration. Be careful to quote the path # and the sed script, to avoid splitting on whitespace, but # also don't use non-portable quotes within backquotes within # quotes we have to do it in 2 steps: extractedcf=`$SED -n -e "$sed_extractcf" < "$progpath"` eval "$extractedcf" else func_error "ignoring unknown tag $tagname" fi ;; esac } # func_check_version_match # ------------------------ # Ensure that we are using m4 macros, and libtool script from the same # release of libtool. func_check_version_match () { if test "$package_revision" != "$macro_revision"; then if test "$VERSION" != "$macro_version"; then if test -z "$macro_version"; then cat >&2 <<_LT_EOF $progname: Version mismatch error. This is $PACKAGE $VERSION, but the $progname: definition of this LT_INIT comes from an older release. $progname: You should recreate aclocal.m4 with macros from $PACKAGE $VERSION $progname: and run autoconf again. _LT_EOF else cat >&2 <<_LT_EOF $progname: Version mismatch error. This is $PACKAGE $VERSION, but the $progname: definition of this LT_INIT comes from $PACKAGE $macro_version. $progname: You should recreate aclocal.m4 with macros from $PACKAGE $VERSION $progname: and run autoconf again. _LT_EOF fi else cat >&2 <<_LT_EOF $progname: Version mismatch error. This is $PACKAGE $VERSION, revision $package_revision, $progname: but the definition of this LT_INIT comes from revision $macro_revision. $progname: You should recreate aclocal.m4 with macros from revision $package_revision $progname: of $PACKAGE $VERSION and run autoconf again. _LT_EOF fi exit $EXIT_MISMATCH fi } # libtool_options_prep [ARG]... # ----------------------------- # Preparation for options parsed by libtool. libtool_options_prep () { $debug_mode # Option defaults: opt_config=false opt_dlopen= opt_dry_run=false opt_help=false opt_mode= opt_preserve_dup_deps=false opt_quiet=false nonopt= preserve_args= _G_rc_lt_options_prep=: # Shorthand for --mode=foo, only valid as the first argument case $1 in clean|clea|cle|cl) shift; set dummy --mode clean ${1+"$@"}; shift ;; compile|compil|compi|comp|com|co|c) shift; set dummy --mode compile ${1+"$@"}; shift ;; execute|execut|execu|exec|exe|ex|e) shift; set dummy --mode execute ${1+"$@"}; shift ;; finish|finis|fini|fin|fi|f) shift; set dummy --mode finish ${1+"$@"}; shift ;; install|instal|insta|inst|ins|in|i) shift; set dummy --mode install ${1+"$@"}; shift ;; link|lin|li|l) shift; set dummy --mode link ${1+"$@"}; shift ;; uninstall|uninstal|uninsta|uninst|unins|unin|uni|un|u) shift; set dummy --mode uninstall ${1+"$@"}; shift ;; *) _G_rc_lt_options_prep=false ;; esac if $_G_rc_lt_options_prep; then # Pass back the list of options. func_quote_for_eval ${1+"$@"} libtool_options_prep_result=$func_quote_for_eval_result fi $_G_rc_lt_options_prep } func_add_hook func_options_prep libtool_options_prep # libtool_parse_options [ARG]... # --------------------------------- # Provide handling for libtool specific options. libtool_parse_options () { $debug_cmd _G_rc_lt_parse_options=false # Perform our own loop to consume as many options as possible in # each iteration. while test $# -gt 0; do _G_match_lt_parse_options=: _G_opt=$1 shift case $_G_opt in --dry-run|--dryrun|-n) opt_dry_run=: ;; --config) func_config ;; --dlopen|-dlopen) opt_dlopen="${opt_dlopen+$opt_dlopen }$1" shift ;; --preserve-dup-deps) opt_preserve_dup_deps=: ;; --features) func_features ;; --finish) set dummy --mode finish ${1+"$@"}; shift ;; --help) opt_help=: ;; --help-all) opt_help=': help-all' ;; --mode) test $# = 0 && func_missing_arg $_G_opt && break opt_mode=$1 case $1 in # Valid mode arguments: clean|compile|execute|finish|install|link|relink|uninstall) ;; # Catch anything else as an error *) func_error "invalid argument for $_G_opt" exit_cmd=exit break ;; esac shift ;; --no-silent|--no-quiet) opt_quiet=false func_append preserve_args " $_G_opt" ;; --no-warnings|--no-warning|--no-warn) opt_warning=false func_append preserve_args " $_G_opt" ;; --no-verbose) opt_verbose=false func_append preserve_args " $_G_opt" ;; --silent|--quiet) opt_quiet=: opt_verbose=false func_append preserve_args " $_G_opt" ;; --tag) test $# = 0 && func_missing_arg $_G_opt && break opt_tag=$1 func_append preserve_args " $_G_opt $1" func_enable_tag "$1" shift ;; --verbose|-v) opt_quiet=false opt_verbose=: func_append preserve_args " $_G_opt" ;; # An option not handled by this hook function: *) set dummy "$_G_opt" ${1+"$@"} ; shift _G_match_lt_parse_options=false break ;; esac $_G_match_lt_parse_options && _G_rc_lt_parse_options=: done if $_G_rc_lt_parse_options; then # save modified positional parameters for caller func_quote_for_eval ${1+"$@"} libtool_parse_options_result=$func_quote_for_eval_result fi $_G_rc_lt_parse_options } func_add_hook func_parse_options libtool_parse_options # libtool_validate_options [ARG]... # --------------------------------- # Perform any sanity checks on option settings and/or unconsumed # arguments. libtool_validate_options () { # save first non-option argument if test 0 -lt $#; then nonopt=$1 shift fi # preserve --debug test : = "$debug_cmd" || func_append preserve_args " --debug" case $host in # Solaris2 added to fix http://debbugs.gnu.org/cgi/bugreport.cgi?bug=16452 # see also: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59788 *cygwin* | *mingw* | *pw32* | *cegcc* | *solaris2* | *os2*) # don't eliminate duplications in $postdeps and $predeps opt_duplicate_compiler_generated_deps=: ;; *) opt_duplicate_compiler_generated_deps=$opt_preserve_dup_deps ;; esac $opt_help || { # Sanity checks first: func_check_version_match test yes != "$build_libtool_libs" \ && test yes != "$build_old_libs" \ && func_fatal_configuration "not configured to build any kind of library" # Darwin sucks eval std_shrext=\"$shrext_cmds\" # Only execute mode is allowed to have -dlopen flags. if test -n "$opt_dlopen" && test execute != "$opt_mode"; then func_error "unrecognized option '-dlopen'" $ECHO "$help" 1>&2 exit $EXIT_FAILURE fi # Change the help message to a mode-specific one. generic_help=$help help="Try '$progname --help --mode=$opt_mode' for more information." } # Pass back the unparsed argument list func_quote_for_eval ${1+"$@"} libtool_validate_options_result=$func_quote_for_eval_result } func_add_hook func_validate_options libtool_validate_options # Process options as early as possible so that --help and --version # can return quickly. func_options ${1+"$@"} eval set dummy "$func_options_result"; shift ## ----------- ## ## Main. ## ## ----------- ## magic='%%%MAGIC variable%%%' magic_exe='%%%MAGIC EXE variable%%%' # Global variables. extracted_archives= extracted_serial=0 # If this variable is set in any of the actions, the command in it # will be execed at the end. This prevents here-documents from being # left over by shells. exec_cmd= # A function that is used when there is no print builtin or printf. func_fallback_echo () { eval 'cat <<_LTECHO_EOF $1 _LTECHO_EOF' } # func_generated_by_libtool # True iff stdin has been generated by Libtool. This function is only # a basic sanity check; it will hardly flush out determined imposters. func_generated_by_libtool_p () { $GREP "^# Generated by .*$PACKAGE" > /dev/null 2>&1 } # func_lalib_p file # True iff FILE is a libtool '.la' library or '.lo' object file. # This function is only a basic sanity check; it will hardly flush out # determined imposters. func_lalib_p () { test -f "$1" && $SED -e 4q "$1" 2>/dev/null | func_generated_by_libtool_p } # func_lalib_unsafe_p file # True iff FILE is a libtool '.la' library or '.lo' object file. # This function implements the same check as func_lalib_p without # resorting to external programs. To this end, it redirects stdin and # closes it afterwards, without saving the original file descriptor. # As a safety measure, use it only where a negative result would be # fatal anyway. Works if 'file' does not exist. func_lalib_unsafe_p () { lalib_p=no if test -f "$1" && test -r "$1" && exec 5<&0 <"$1"; then for lalib_p_l in 1 2 3 4 do read lalib_p_line case $lalib_p_line in \#\ Generated\ by\ *$PACKAGE* ) lalib_p=yes; break;; esac done exec 0<&5 5<&- fi test yes = "$lalib_p" } # func_ltwrapper_script_p file # True iff FILE is a libtool wrapper script # This function is only a basic sanity check; it will hardly flush out # determined imposters. func_ltwrapper_script_p () { test -f "$1" && $lt_truncate_bin < "$1" 2>/dev/null | func_generated_by_libtool_p } # func_ltwrapper_executable_p file # True iff FILE is a libtool wrapper executable # This function is only a basic sanity check; it will hardly flush out # determined imposters. func_ltwrapper_executable_p () { func_ltwrapper_exec_suffix= case $1 in *.exe) ;; *) func_ltwrapper_exec_suffix=.exe ;; esac $GREP "$magic_exe" "$1$func_ltwrapper_exec_suffix" >/dev/null 2>&1 } # func_ltwrapper_scriptname file # Assumes file is an ltwrapper_executable # uses $file to determine the appropriate filename for a # temporary ltwrapper_script. func_ltwrapper_scriptname () { func_dirname_and_basename "$1" "" "." func_stripname '' '.exe' "$func_basename_result" func_ltwrapper_scriptname_result=$func_dirname_result/$objdir/${func_stripname_result}_ltshwrapper } # func_ltwrapper_p file # True iff FILE is a libtool wrapper script or wrapper executable # This function is only a basic sanity check; it will hardly flush out # determined imposters. func_ltwrapper_p () { func_ltwrapper_script_p "$1" || func_ltwrapper_executable_p "$1" } # func_execute_cmds commands fail_cmd # Execute tilde-delimited COMMANDS. # If FAIL_CMD is given, eval that upon failure. # FAIL_CMD may read-access the current command in variable CMD! func_execute_cmds () { $debug_cmd save_ifs=$IFS; IFS='~' for cmd in $1; do IFS=$sp$nl eval cmd=\"$cmd\" IFS=$save_ifs func_show_eval "$cmd" "${2-:}" done IFS=$save_ifs } # func_source file # Source FILE, adding directory component if necessary. # Note that it is not necessary on cygwin/mingw to append a dot to # FILE even if both FILE and FILE.exe exist: automatic-append-.exe # behavior happens only for exec(3), not for open(2)! Also, sourcing # 'FILE.' does not work on cygwin managed mounts. func_source () { $debug_cmd case $1 in */* | *\\*) . "$1" ;; *) . "./$1" ;; esac } # func_resolve_sysroot PATH # Replace a leading = in PATH with a sysroot. Store the result into # func_resolve_sysroot_result func_resolve_sysroot () { func_resolve_sysroot_result=$1 case $func_resolve_sysroot_result in =*) func_stripname '=' '' "$func_resolve_sysroot_result" func_resolve_sysroot_result=$lt_sysroot$func_stripname_result ;; esac } # func_replace_sysroot PATH # If PATH begins with the sysroot, replace it with = and # store the result into func_replace_sysroot_result. func_replace_sysroot () { case $lt_sysroot:$1 in ?*:"$lt_sysroot"*) func_stripname "$lt_sysroot" '' "$1" func_replace_sysroot_result='='$func_stripname_result ;; *) # Including no sysroot. func_replace_sysroot_result=$1 ;; esac } # func_infer_tag arg # Infer tagged configuration to use if any are available and # if one wasn't chosen via the "--tag" command line option. # Only attempt this if the compiler in the base compile # command doesn't match the default compiler. # arg is usually of the form 'gcc ...' func_infer_tag () { $debug_cmd if test -n "$available_tags" && test -z "$tagname"; then CC_quoted= for arg in $CC; do func_append_quoted CC_quoted "$arg" done CC_expanded=`func_echo_all $CC` CC_quoted_expanded=`func_echo_all $CC_quoted` case $@ in # Blanks in the command may have been stripped by the calling shell, # but not from the CC environment variable when configure was run. " $CC "* | "$CC "* | " $CC_expanded "* | "$CC_expanded "* | \ " $CC_quoted"* | "$CC_quoted "* | " $CC_quoted_expanded "* | "$CC_quoted_expanded "*) ;; # Blanks at the start of $base_compile will cause this to fail # if we don't check for them as well. *) for z in $available_tags; do if $GREP "^# ### BEGIN LIBTOOL TAG CONFIG: $z$" < "$progpath" > /dev/null; then # Evaluate the configuration. eval "`$SED -n -e '/^# ### BEGIN LIBTOOL TAG CONFIG: '$z'$/,/^# ### END LIBTOOL TAG CONFIG: '$z'$/p' < $progpath`" CC_quoted= for arg in $CC; do # Double-quote args containing other shell metacharacters. func_append_quoted CC_quoted "$arg" done CC_expanded=`func_echo_all $CC` CC_quoted_expanded=`func_echo_all $CC_quoted` case "$@ " in " $CC "* | "$CC "* | " $CC_expanded "* | "$CC_expanded "* | \ " $CC_quoted"* | "$CC_quoted "* | " $CC_quoted_expanded "* | "$CC_quoted_expanded "*) # The compiler in the base compile command matches # the one in the tagged configuration. # Assume this is the tagged configuration we want. tagname=$z break ;; esac fi done # If $tagname still isn't set, then no tagged configuration # was found and let the user know that the "--tag" command # line option must be used. if test -z "$tagname"; then func_echo "unable to infer tagged configuration" func_fatal_error "specify a tag with '--tag'" # else # func_verbose "using $tagname tagged configuration" fi ;; esac fi } # func_write_libtool_object output_name pic_name nonpic_name # Create a libtool object file (analogous to a ".la" file), # but don't create it if we're doing a dry run. func_write_libtool_object () { write_libobj=$1 if test yes = "$build_libtool_libs"; then write_lobj=\'$2\' else write_lobj=none fi if test yes = "$build_old_libs"; then write_oldobj=\'$3\' else write_oldobj=none fi $opt_dry_run || { cat >${write_libobj}T </dev/null` if test "$?" -eq 0 && test -n "$func_convert_core_file_wine_to_w32_tmp"; then func_convert_core_file_wine_to_w32_result=`$ECHO "$func_convert_core_file_wine_to_w32_tmp" | $SED -e "$sed_naive_backslashify"` else func_convert_core_file_wine_to_w32_result= fi fi } # end: func_convert_core_file_wine_to_w32 # func_convert_core_path_wine_to_w32 ARG # Helper function used by path conversion functions when $build is *nix, and # $host is mingw, cygwin, or some other w32 environment. Relies on a correctly # configured wine environment available, with the winepath program in $build's # $PATH. Assumes ARG has no leading or trailing path separator characters. # # ARG is path to be converted from $build format to win32. # Result is available in $func_convert_core_path_wine_to_w32_result. # Unconvertible file (directory) names in ARG are skipped; if no directory names # are convertible, then the result may be empty. func_convert_core_path_wine_to_w32 () { $debug_cmd # unfortunately, winepath doesn't convert paths, only file names func_convert_core_path_wine_to_w32_result= if test -n "$1"; then oldIFS=$IFS IFS=: for func_convert_core_path_wine_to_w32_f in $1; do IFS=$oldIFS func_convert_core_file_wine_to_w32 "$func_convert_core_path_wine_to_w32_f" if test -n "$func_convert_core_file_wine_to_w32_result"; then if test -z "$func_convert_core_path_wine_to_w32_result"; then func_convert_core_path_wine_to_w32_result=$func_convert_core_file_wine_to_w32_result else func_append func_convert_core_path_wine_to_w32_result ";$func_convert_core_file_wine_to_w32_result" fi fi done IFS=$oldIFS fi } # end: func_convert_core_path_wine_to_w32 # func_cygpath ARGS... # Wrapper around calling the cygpath program via LT_CYGPATH. This is used when # when (1) $build is *nix and Cygwin is hosted via a wine environment; or (2) # $build is MSYS and $host is Cygwin, or (3) $build is Cygwin. In case (1) or # (2), returns the Cygwin file name or path in func_cygpath_result (input # file name or path is assumed to be in w32 format, as previously converted # from $build's *nix or MSYS format). In case (3), returns the w32 file name # or path in func_cygpath_result (input file name or path is assumed to be in # Cygwin format). Returns an empty string on error. # # ARGS are passed to cygpath, with the last one being the file name or path to # be converted. # # Specify the absolute *nix (or w32) name to cygpath in the LT_CYGPATH # environment variable; do not put it in $PATH. func_cygpath () { $debug_cmd if test -n "$LT_CYGPATH" && test -f "$LT_CYGPATH"; then func_cygpath_result=`$LT_CYGPATH "$@" 2>/dev/null` if test "$?" -ne 0; then # on failure, ensure result is empty func_cygpath_result= fi else func_cygpath_result= func_error "LT_CYGPATH is empty or specifies non-existent file: '$LT_CYGPATH'" fi } #end: func_cygpath # func_convert_core_msys_to_w32 ARG # Convert file name or path ARG from MSYS format to w32 format. Return # result in func_convert_core_msys_to_w32_result. func_convert_core_msys_to_w32 () { $debug_cmd # awkward: cmd appends spaces to result func_convert_core_msys_to_w32_result=`( cmd //c echo "$1" ) 2>/dev/null | $SED -e 's/[ ]*$//' -e "$sed_naive_backslashify"` } #end: func_convert_core_msys_to_w32 # func_convert_file_check ARG1 ARG2 # Verify that ARG1 (a file name in $build format) was converted to $host # format in ARG2. Otherwise, emit an error message, but continue (resetting # func_to_host_file_result to ARG1). func_convert_file_check () { $debug_cmd if test -z "$2" && test -n "$1"; then func_error "Could not determine host file name corresponding to" func_error " '$1'" func_error "Continuing, but uninstalled executables may not work." # Fallback: func_to_host_file_result=$1 fi } # end func_convert_file_check # func_convert_path_check FROM_PATHSEP TO_PATHSEP FROM_PATH TO_PATH # Verify that FROM_PATH (a path in $build format) was converted to $host # format in TO_PATH. Otherwise, emit an error message, but continue, resetting # func_to_host_file_result to a simplistic fallback value (see below). func_convert_path_check () { $debug_cmd if test -z "$4" && test -n "$3"; then func_error "Could not determine the host path corresponding to" func_error " '$3'" func_error "Continuing, but uninstalled executables may not work." # Fallback. This is a deliberately simplistic "conversion" and # should not be "improved". See libtool.info. if test "x$1" != "x$2"; then lt_replace_pathsep_chars="s|$1|$2|g" func_to_host_path_result=`echo "$3" | $SED -e "$lt_replace_pathsep_chars"` else func_to_host_path_result=$3 fi fi } # end func_convert_path_check # func_convert_path_front_back_pathsep FRONTPAT BACKPAT REPL ORIG # Modifies func_to_host_path_result by prepending REPL if ORIG matches FRONTPAT # and appending REPL if ORIG matches BACKPAT. func_convert_path_front_back_pathsep () { $debug_cmd case $4 in $1 ) func_to_host_path_result=$3$func_to_host_path_result ;; esac case $4 in $2 ) func_append func_to_host_path_result "$3" ;; esac } # end func_convert_path_front_back_pathsep ################################################## # $build to $host FILE NAME CONVERSION FUNCTIONS # ################################################## # invoked via '$to_host_file_cmd ARG' # # In each case, ARG is the path to be converted from $build to $host format. # Result will be available in $func_to_host_file_result. # func_to_host_file ARG # Converts the file name ARG from $build format to $host format. Return result # in func_to_host_file_result. func_to_host_file () { $debug_cmd $to_host_file_cmd "$1" } # end func_to_host_file # func_to_tool_file ARG LAZY # converts the file name ARG from $build format to toolchain format. Return # result in func_to_tool_file_result. If the conversion in use is listed # in (the comma separated) LAZY, no conversion takes place. func_to_tool_file () { $debug_cmd case ,$2, in *,"$to_tool_file_cmd",*) func_to_tool_file_result=$1 ;; *) $to_tool_file_cmd "$1" func_to_tool_file_result=$func_to_host_file_result ;; esac } # end func_to_tool_file # func_convert_file_noop ARG # Copy ARG to func_to_host_file_result. func_convert_file_noop () { func_to_host_file_result=$1 } # end func_convert_file_noop # func_convert_file_msys_to_w32 ARG # Convert file name ARG from (mingw) MSYS to (mingw) w32 format; automatic # conversion to w32 is not available inside the cwrapper. Returns result in # func_to_host_file_result. func_convert_file_msys_to_w32 () { $debug_cmd func_to_host_file_result=$1 if test -n "$1"; then func_convert_core_msys_to_w32 "$1" func_to_host_file_result=$func_convert_core_msys_to_w32_result fi func_convert_file_check "$1" "$func_to_host_file_result" } # end func_convert_file_msys_to_w32 # func_convert_file_cygwin_to_w32 ARG # Convert file name ARG from Cygwin to w32 format. Returns result in # func_to_host_file_result. func_convert_file_cygwin_to_w32 () { $debug_cmd func_to_host_file_result=$1 if test -n "$1"; then # because $build is cygwin, we call "the" cygpath in $PATH; no need to use # LT_CYGPATH in this case. func_to_host_file_result=`cygpath -m "$1"` fi func_convert_file_check "$1" "$func_to_host_file_result" } # end func_convert_file_cygwin_to_w32 # func_convert_file_nix_to_w32 ARG # Convert file name ARG from *nix to w32 format. Requires a wine environment # and a working winepath. Returns result in func_to_host_file_result. func_convert_file_nix_to_w32 () { $debug_cmd func_to_host_file_result=$1 if test -n "$1"; then func_convert_core_file_wine_to_w32 "$1" func_to_host_file_result=$func_convert_core_file_wine_to_w32_result fi func_convert_file_check "$1" "$func_to_host_file_result" } # end func_convert_file_nix_to_w32 # func_convert_file_msys_to_cygwin ARG # Convert file name ARG from MSYS to Cygwin format. Requires LT_CYGPATH set. # Returns result in func_to_host_file_result. func_convert_file_msys_to_cygwin () { $debug_cmd func_to_host_file_result=$1 if test -n "$1"; then func_convert_core_msys_to_w32 "$1" func_cygpath -u "$func_convert_core_msys_to_w32_result" func_to_host_file_result=$func_cygpath_result fi func_convert_file_check "$1" "$func_to_host_file_result" } # end func_convert_file_msys_to_cygwin # func_convert_file_nix_to_cygwin ARG # Convert file name ARG from *nix to Cygwin format. Requires Cygwin installed # in a wine environment, working winepath, and LT_CYGPATH set. Returns result # in func_to_host_file_result. func_convert_file_nix_to_cygwin () { $debug_cmd func_to_host_file_result=$1 if test -n "$1"; then # convert from *nix to w32, then use cygpath to convert from w32 to cygwin. func_convert_core_file_wine_to_w32 "$1" func_cygpath -u "$func_convert_core_file_wine_to_w32_result" func_to_host_file_result=$func_cygpath_result fi func_convert_file_check "$1" "$func_to_host_file_result" } # end func_convert_file_nix_to_cygwin ############################################# # $build to $host PATH CONVERSION FUNCTIONS # ############################################# # invoked via '$to_host_path_cmd ARG' # # In each case, ARG is the path to be converted from $build to $host format. # The result will be available in $func_to_host_path_result. # # Path separators are also converted from $build format to $host format. If # ARG begins or ends with a path separator character, it is preserved (but # converted to $host format) on output. # # All path conversion functions are named using the following convention: # file name conversion function : func_convert_file_X_to_Y () # path conversion function : func_convert_path_X_to_Y () # where, for any given $build/$host combination the 'X_to_Y' value is the # same. If conversion functions are added for new $build/$host combinations, # the two new functions must follow this pattern, or func_init_to_host_path_cmd # will break. # func_init_to_host_path_cmd # Ensures that function "pointer" variable $to_host_path_cmd is set to the # appropriate value, based on the value of $to_host_file_cmd. to_host_path_cmd= func_init_to_host_path_cmd () { $debug_cmd if test -z "$to_host_path_cmd"; then func_stripname 'func_convert_file_' '' "$to_host_file_cmd" to_host_path_cmd=func_convert_path_$func_stripname_result fi } # func_to_host_path ARG # Converts the path ARG from $build format to $host format. Return result # in func_to_host_path_result. func_to_host_path () { $debug_cmd func_init_to_host_path_cmd $to_host_path_cmd "$1" } # end func_to_host_path # func_convert_path_noop ARG # Copy ARG to func_to_host_path_result. func_convert_path_noop () { func_to_host_path_result=$1 } # end func_convert_path_noop # func_convert_path_msys_to_w32 ARG # Convert path ARG from (mingw) MSYS to (mingw) w32 format; automatic # conversion to w32 is not available inside the cwrapper. Returns result in # func_to_host_path_result. func_convert_path_msys_to_w32 () { $debug_cmd func_to_host_path_result=$1 if test -n "$1"; then # Remove leading and trailing path separator characters from ARG. MSYS # behavior is inconsistent here; cygpath turns them into '.;' and ';.'; # and winepath ignores them completely. func_stripname : : "$1" func_to_host_path_tmp1=$func_stripname_result func_convert_core_msys_to_w32 "$func_to_host_path_tmp1" func_to_host_path_result=$func_convert_core_msys_to_w32_result func_convert_path_check : ";" \ "$func_to_host_path_tmp1" "$func_to_host_path_result" func_convert_path_front_back_pathsep ":*" "*:" ";" "$1" fi } # end func_convert_path_msys_to_w32 # func_convert_path_cygwin_to_w32 ARG # Convert path ARG from Cygwin to w32 format. Returns result in # func_to_host_file_result. func_convert_path_cygwin_to_w32 () { $debug_cmd func_to_host_path_result=$1 if test -n "$1"; then # See func_convert_path_msys_to_w32: func_stripname : : "$1" func_to_host_path_tmp1=$func_stripname_result func_to_host_path_result=`cygpath -m -p "$func_to_host_path_tmp1"` func_convert_path_check : ";" \ "$func_to_host_path_tmp1" "$func_to_host_path_result" func_convert_path_front_back_pathsep ":*" "*:" ";" "$1" fi } # end func_convert_path_cygwin_to_w32 # func_convert_path_nix_to_w32 ARG # Convert path ARG from *nix to w32 format. Requires a wine environment and # a working winepath. Returns result in func_to_host_file_result. func_convert_path_nix_to_w32 () { $debug_cmd func_to_host_path_result=$1 if test -n "$1"; then # See func_convert_path_msys_to_w32: func_stripname : : "$1" func_to_host_path_tmp1=$func_stripname_result func_convert_core_path_wine_to_w32 "$func_to_host_path_tmp1" func_to_host_path_result=$func_convert_core_path_wine_to_w32_result func_convert_path_check : ";" \ "$func_to_host_path_tmp1" "$func_to_host_path_result" func_convert_path_front_back_pathsep ":*" "*:" ";" "$1" fi } # end func_convert_path_nix_to_w32 # func_convert_path_msys_to_cygwin ARG # Convert path ARG from MSYS to Cygwin format. Requires LT_CYGPATH set. # Returns result in func_to_host_file_result. func_convert_path_msys_to_cygwin () { $debug_cmd func_to_host_path_result=$1 if test -n "$1"; then # See func_convert_path_msys_to_w32: func_stripname : : "$1" func_to_host_path_tmp1=$func_stripname_result func_convert_core_msys_to_w32 "$func_to_host_path_tmp1" func_cygpath -u -p "$func_convert_core_msys_to_w32_result" func_to_host_path_result=$func_cygpath_result func_convert_path_check : : \ "$func_to_host_path_tmp1" "$func_to_host_path_result" func_convert_path_front_back_pathsep ":*" "*:" : "$1" fi } # end func_convert_path_msys_to_cygwin # func_convert_path_nix_to_cygwin ARG # Convert path ARG from *nix to Cygwin format. Requires Cygwin installed in a # a wine environment, working winepath, and LT_CYGPATH set. Returns result in # func_to_host_file_result. func_convert_path_nix_to_cygwin () { $debug_cmd func_to_host_path_result=$1 if test -n "$1"; then # Remove leading and trailing path separator characters from # ARG. msys behavior is inconsistent here, cygpath turns them # into '.;' and ';.', and winepath ignores them completely. func_stripname : : "$1" func_to_host_path_tmp1=$func_stripname_result func_convert_core_path_wine_to_w32 "$func_to_host_path_tmp1" func_cygpath -u -p "$func_convert_core_path_wine_to_w32_result" func_to_host_path_result=$func_cygpath_result func_convert_path_check : : \ "$func_to_host_path_tmp1" "$func_to_host_path_result" func_convert_path_front_back_pathsep ":*" "*:" : "$1" fi } # end func_convert_path_nix_to_cygwin # func_dll_def_p FILE # True iff FILE is a Windows DLL '.def' file. # Keep in sync with _LT_DLL_DEF_P in libtool.m4 func_dll_def_p () { $debug_cmd func_dll_def_p_tmp=`$SED -n \ -e 's/^[ ]*//' \ -e '/^\(;.*\)*$/d' \ -e 's/^\(EXPORTS\|LIBRARY\)\([ ].*\)*$/DEF/p' \ -e q \ "$1"` test DEF = "$func_dll_def_p_tmp" } # func_mode_compile arg... func_mode_compile () { $debug_cmd # Get the compilation command and the source file. base_compile= srcfile=$nonopt # always keep a non-empty value in "srcfile" suppress_opt=yes suppress_output= arg_mode=normal libobj= later= pie_flag= for arg do case $arg_mode in arg ) # do not "continue". Instead, add this to base_compile lastarg=$arg arg_mode=normal ;; target ) libobj=$arg arg_mode=normal continue ;; normal ) # Accept any command-line options. case $arg in -o) test -n "$libobj" && \ func_fatal_error "you cannot specify '-o' more than once" arg_mode=target continue ;; -pie | -fpie | -fPIE) func_append pie_flag " $arg" continue ;; -shared | -static | -prefer-pic | -prefer-non-pic) func_append later " $arg" continue ;; -no-suppress) suppress_opt=no continue ;; -Xcompiler) arg_mode=arg # the next one goes into the "base_compile" arg list continue # The current "srcfile" will either be retained or ;; # replaced later. I would guess that would be a bug. -Wc,*) func_stripname '-Wc,' '' "$arg" args=$func_stripname_result lastarg= save_ifs=$IFS; IFS=, for arg in $args; do IFS=$save_ifs func_append_quoted lastarg "$arg" done IFS=$save_ifs func_stripname ' ' '' "$lastarg" lastarg=$func_stripname_result # Add the arguments to base_compile. func_append base_compile " $lastarg" continue ;; *) # Accept the current argument as the source file. # The previous "srcfile" becomes the current argument. # lastarg=$srcfile srcfile=$arg ;; esac # case $arg ;; esac # case $arg_mode # Aesthetically quote the previous argument. func_append_quoted base_compile "$lastarg" done # for arg case $arg_mode in arg) func_fatal_error "you must specify an argument for -Xcompile" ;; target) func_fatal_error "you must specify a target with '-o'" ;; *) # Get the name of the library object. test -z "$libobj" && { func_basename "$srcfile" libobj=$func_basename_result } ;; esac # Recognize several different file suffixes. # If the user specifies -o file.o, it is replaced with file.lo case $libobj in *.[cCFSifmso] | \ *.ada | *.adb | *.ads | *.asm | \ *.c++ | *.cc | *.ii | *.class | *.cpp | *.cxx | \ *.[fF][09]? | *.for | *.java | *.go | *.obj | *.sx | *.cu | *.cup) func_xform "$libobj" libobj=$func_xform_result ;; esac case $libobj in *.lo) func_lo2o "$libobj"; obj=$func_lo2o_result ;; *) func_fatal_error "cannot determine name of library object from '$libobj'" ;; esac func_infer_tag $base_compile for arg in $later; do case $arg in -shared) test yes = "$build_libtool_libs" \ || func_fatal_configuration "cannot build a shared library" build_old_libs=no continue ;; -static) build_libtool_libs=no build_old_libs=yes continue ;; -prefer-pic) pic_mode=yes continue ;; -prefer-non-pic) pic_mode=no continue ;; esac done func_quote_for_eval "$libobj" test "X$libobj" != "X$func_quote_for_eval_result" \ && $ECHO "X$libobj" | $GREP '[]~#^*{};<>?"'"'"' &()|`$[]' \ && func_warning "libobj name '$libobj' may not contain shell special characters." func_dirname_and_basename "$obj" "/" "" objname=$func_basename_result xdir=$func_dirname_result lobj=$xdir$objdir/$objname test -z "$base_compile" && \ func_fatal_help "you must specify a compilation command" # Delete any leftover library objects. if test yes = "$build_old_libs"; then removelist="$obj $lobj $libobj ${libobj}T" else removelist="$lobj $libobj ${libobj}T" fi # On Cygwin there's no "real" PIC flag so we must build both object types case $host_os in cygwin* | mingw* | pw32* | os2* | cegcc*) pic_mode=default ;; esac if test no = "$pic_mode" && test pass_all != "$deplibs_check_method"; then # non-PIC code in shared libraries is not supported pic_mode=default fi # Calculate the filename of the output object if compiler does # not support -o with -c if test no = "$compiler_c_o"; then output_obj=`$ECHO "$srcfile" | $SED 's%^.*/%%; s%\.[^.]*$%%'`.$objext lockfile=$output_obj.lock else output_obj= need_locks=no lockfile= fi # Lock this critical section if it is needed # We use this script file to make the link, it avoids creating a new file if test yes = "$need_locks"; then until $opt_dry_run || ln "$progpath" "$lockfile" 2>/dev/null; do func_echo "Waiting for $lockfile to be removed" sleep 2 done elif test warn = "$need_locks"; then if test -f "$lockfile"; then $ECHO "\ *** ERROR, $lockfile exists and contains: `cat $lockfile 2>/dev/null` This indicates that another process is trying to use the same temporary object file, and libtool could not work around it because your compiler does not support '-c' and '-o' together. If you repeat this compilation, it may succeed, by chance, but you had better avoid parallel builds (make -j) in this platform, or get a better compiler." $opt_dry_run || $RM $removelist exit $EXIT_FAILURE fi func_append removelist " $output_obj" $ECHO "$srcfile" > "$lockfile" fi $opt_dry_run || $RM $removelist func_append removelist " $lockfile" trap '$opt_dry_run || $RM $removelist; exit $EXIT_FAILURE' 1 2 15 func_to_tool_file "$srcfile" func_convert_file_msys_to_w32 srcfile=$func_to_tool_file_result func_quote_for_eval "$srcfile" qsrcfile=$func_quote_for_eval_result # Only build a PIC object if we are building libtool libraries. if test yes = "$build_libtool_libs"; then # Without this assignment, base_compile gets emptied. fbsd_hideous_sh_bug=$base_compile if test no != "$pic_mode"; then command="$base_compile $qsrcfile $pic_flag" else # Don't build PIC code command="$base_compile $qsrcfile" fi func_mkdir_p "$xdir$objdir" if test -z "$output_obj"; then # Place PIC objects in $objdir func_append command " -o $lobj" fi func_show_eval_locale "$command" \ 'test -n "$output_obj" && $RM $removelist; exit $EXIT_FAILURE' if test warn = "$need_locks" && test "X`cat $lockfile 2>/dev/null`" != "X$srcfile"; then $ECHO "\ *** ERROR, $lockfile contains: `cat $lockfile 2>/dev/null` but it should contain: $srcfile This indicates that another process is trying to use the same temporary object file, and libtool could not work around it because your compiler does not support '-c' and '-o' together. If you repeat this compilation, it may succeed, by chance, but you had better avoid parallel builds (make -j) in this platform, or get a better compiler." $opt_dry_run || $RM $removelist exit $EXIT_FAILURE fi # Just move the object if needed, then go on to compile the next one if test -n "$output_obj" && test "X$output_obj" != "X$lobj"; then func_show_eval '$MV "$output_obj" "$lobj"' \ 'error=$?; $opt_dry_run || $RM $removelist; exit $error' fi # Allow error messages only from the first compilation. if test yes = "$suppress_opt"; then suppress_output=' >/dev/null 2>&1' fi fi # Only build a position-dependent object if we build old libraries. if test yes = "$build_old_libs"; then if test yes != "$pic_mode"; then # Don't build PIC code command="$base_compile $qsrcfile$pie_flag" else command="$base_compile $qsrcfile $pic_flag" fi if test yes = "$compiler_c_o"; then func_append command " -o $obj" fi # Suppress compiler output if we already did a PIC compilation. func_append command "$suppress_output" func_show_eval_locale "$command" \ '$opt_dry_run || $RM $removelist; exit $EXIT_FAILURE' if test warn = "$need_locks" && test "X`cat $lockfile 2>/dev/null`" != "X$srcfile"; then $ECHO "\ *** ERROR, $lockfile contains: `cat $lockfile 2>/dev/null` but it should contain: $srcfile This indicates that another process is trying to use the same temporary object file, and libtool could not work around it because your compiler does not support '-c' and '-o' together. If you repeat this compilation, it may succeed, by chance, but you had better avoid parallel builds (make -j) in this platform, or get a better compiler." $opt_dry_run || $RM $removelist exit $EXIT_FAILURE fi # Just move the object if needed if test -n "$output_obj" && test "X$output_obj" != "X$obj"; then func_show_eval '$MV "$output_obj" "$obj"' \ 'error=$?; $opt_dry_run || $RM $removelist; exit $error' fi fi $opt_dry_run || { func_write_libtool_object "$libobj" "$objdir/$objname" "$objname" # Unlock the critical section if it was locked if test no != "$need_locks"; then removelist=$lockfile $RM "$lockfile" fi } exit $EXIT_SUCCESS } $opt_help || { test compile = "$opt_mode" && func_mode_compile ${1+"$@"} } func_mode_help () { # We need to display help for each of the modes. case $opt_mode in "") # Generic help is extracted from the usage comments # at the start of this file. func_help ;; clean) $ECHO \ "Usage: $progname [OPTION]... --mode=clean RM [RM-OPTION]... FILE... Remove files from the build directory. RM is the name of the program to use to delete files associated with each FILE (typically '/bin/rm'). RM-OPTIONS are options (such as '-f') to be passed to RM. If FILE is a libtool library, object or program, all the files associated with it are deleted. Otherwise, only FILE itself is deleted using RM." ;; compile) $ECHO \ "Usage: $progname [OPTION]... --mode=compile COMPILE-COMMAND... SOURCEFILE Compile a source file into a libtool library object. This mode accepts the following additional options: -o OUTPUT-FILE set the output file name to OUTPUT-FILE -no-suppress do not suppress compiler output for multiple passes -prefer-pic try to build PIC objects only -prefer-non-pic try to build non-PIC objects only -shared do not build a '.o' file suitable for static linking -static only build a '.o' file suitable for static linking -Wc,FLAG pass FLAG directly to the compiler COMPILE-COMMAND is a command to be used in creating a 'standard' object file from the given SOURCEFILE. The output file name is determined by removing the directory component from SOURCEFILE, then substituting the C source code suffix '.c' with the library object suffix, '.lo'." ;; execute) $ECHO \ "Usage: $progname [OPTION]... --mode=execute COMMAND [ARGS]... Automatically set library path, then run a program. This mode accepts the following additional options: -dlopen FILE add the directory containing FILE to the library path This mode sets the library path environment variable according to '-dlopen' flags. If any of the ARGS are libtool executable wrappers, then they are translated into their corresponding uninstalled binary, and any of their required library directories are added to the library path. Then, COMMAND is executed, with ARGS as arguments." ;; finish) $ECHO \ "Usage: $progname [OPTION]... --mode=finish [LIBDIR]... Complete the installation of libtool libraries. Each LIBDIR is a directory that contains libtool libraries. The commands that this mode executes may require superuser privileges. Use the '--dry-run' option if you just want to see what would be executed." ;; install) $ECHO \ "Usage: $progname [OPTION]... --mode=install INSTALL-COMMAND... Install executables or libraries. INSTALL-COMMAND is the installation command. The first component should be either the 'install' or 'cp' program. The following components of INSTALL-COMMAND are treated specially: -inst-prefix-dir PREFIX-DIR Use PREFIX-DIR as a staging area for installation The rest of the components are interpreted as arguments to that command (only BSD-compatible install options are recognized)." ;; link) $ECHO \ "Usage: $progname [OPTION]... --mode=link LINK-COMMAND... Link object files or libraries together to form another library, or to create an executable program. LINK-COMMAND is a command using the C compiler that you would use to create a program from several object files. The following components of LINK-COMMAND are treated specially: -all-static do not do any dynamic linking at all -avoid-version do not add a version suffix if possible -bindir BINDIR specify path to binaries directory (for systems where libraries must be found in the PATH setting at runtime) -dlopen FILE '-dlpreopen' FILE if it cannot be dlopened at runtime -dlpreopen FILE link in FILE and add its symbols to lt_preloaded_symbols -export-dynamic allow symbols from OUTPUT-FILE to be resolved with dlsym(3) -export-symbols SYMFILE try to export only the symbols listed in SYMFILE -export-symbols-regex REGEX try to export only the symbols matching REGEX -LLIBDIR search LIBDIR for required installed libraries -lNAME OUTPUT-FILE requires the installed library libNAME -module build a library that can dlopened -no-fast-install disable the fast-install mode -no-install link a not-installable executable -no-undefined declare that a library does not refer to external symbols -o OUTPUT-FILE create OUTPUT-FILE from the specified objects -objectlist FILE use a list of object files found in FILE to specify objects -os2dllname NAME force a short DLL name on OS/2 (no effect on other OSes) -precious-files-regex REGEX don't remove output files matching REGEX -release RELEASE specify package release information -rpath LIBDIR the created library will eventually be installed in LIBDIR -R[ ]LIBDIR add LIBDIR to the runtime path of programs and libraries -shared only do dynamic linking of libtool libraries -shrext SUFFIX override the standard shared library file extension -static do not do any dynamic linking of uninstalled libtool libraries -static-libtool-libs do not do any dynamic linking of libtool libraries -version-info CURRENT[:REVISION[:AGE]] specify library version info [each variable defaults to 0] -weak LIBNAME declare that the target provides the LIBNAME interface -Wc,FLAG -Xcompiler FLAG pass linker-specific FLAG directly to the compiler -Wl,FLAG -Xlinker FLAG pass linker-specific FLAG directly to the linker -XCClinker FLAG pass link-specific FLAG to the compiler driver (CC) All other options (arguments beginning with '-') are ignored. Every other argument is treated as a filename. Files ending in '.la' are treated as uninstalled libtool libraries, other files are standard or library object files. If the OUTPUT-FILE ends in '.la', then a libtool library is created, only library objects ('.lo' files) may be specified, and '-rpath' is required, except when creating a convenience library. If OUTPUT-FILE ends in '.a' or '.lib', then a standard library is created using 'ar' and 'ranlib', or on Windows using 'lib'. If OUTPUT-FILE ends in '.lo' or '.$objext', then a reloadable object file is created, otherwise an executable program is created." ;; uninstall) $ECHO \ "Usage: $progname [OPTION]... --mode=uninstall RM [RM-OPTION]... FILE... Remove libraries from an installation directory. RM is the name of the program to use to delete files associated with each FILE (typically '/bin/rm'). RM-OPTIONS are options (such as '-f') to be passed to RM. If FILE is a libtool library, all the files associated with it are deleted. Otherwise, only FILE itself is deleted using RM." ;; *) func_fatal_help "invalid operation mode '$opt_mode'" ;; esac echo $ECHO "Try '$progname --help' for more information about other modes." } # Now that we've collected a possible --mode arg, show help if necessary if $opt_help; then if test : = "$opt_help"; then func_mode_help else { func_help noexit for opt_mode in compile link execute install finish uninstall clean; do func_mode_help done } | $SED -n '1p; 2,$s/^Usage:/ or: /p' { func_help noexit for opt_mode in compile link execute install finish uninstall clean; do echo func_mode_help done } | $SED '1d /^When reporting/,/^Report/{ H d } $x /information about other modes/d /more detailed .*MODE/d s/^Usage:.*--mode=\([^ ]*\) .*/Description of \1 mode:/' fi exit $? fi # func_mode_execute arg... func_mode_execute () { $debug_cmd # The first argument is the command name. cmd=$nonopt test -z "$cmd" && \ func_fatal_help "you must specify a COMMAND" # Handle -dlopen flags immediately. for file in $opt_dlopen; do test -f "$file" \ || func_fatal_help "'$file' is not a file" dir= case $file in *.la) func_resolve_sysroot "$file" file=$func_resolve_sysroot_result # Check to see that this really is a libtool archive. func_lalib_unsafe_p "$file" \ || func_fatal_help "'$lib' is not a valid libtool archive" # Read the libtool library. dlname= library_names= func_source "$file" # Skip this library if it cannot be dlopened. if test -z "$dlname"; then # Warn if it was a shared library. test -n "$library_names" && \ func_warning "'$file' was not linked with '-export-dynamic'" continue fi func_dirname "$file" "" "." dir=$func_dirname_result if test -f "$dir/$objdir/$dlname"; then func_append dir "/$objdir" else if test ! -f "$dir/$dlname"; then func_fatal_error "cannot find '$dlname' in '$dir' or '$dir/$objdir'" fi fi ;; *.lo) # Just add the directory containing the .lo file. func_dirname "$file" "" "." dir=$func_dirname_result ;; *) func_warning "'-dlopen' is ignored for non-libtool libraries and objects" continue ;; esac # Get the absolute pathname. absdir=`cd "$dir" && pwd` test -n "$absdir" && dir=$absdir # Now add the directory to shlibpath_var. if eval "test -z \"\$$shlibpath_var\""; then eval "$shlibpath_var=\"\$dir\"" else eval "$shlibpath_var=\"\$dir:\$$shlibpath_var\"" fi done # This variable tells wrapper scripts just to set shlibpath_var # rather than running their programs. libtool_execute_magic=$magic # Check if any of the arguments is a wrapper script. args= for file do case $file in -* | *.la | *.lo ) ;; *) # Do a test to see if this is really a libtool program. if func_ltwrapper_script_p "$file"; then func_source "$file" # Transform arg to wrapped name. file=$progdir/$program elif func_ltwrapper_executable_p "$file"; then func_ltwrapper_scriptname "$file" func_source "$func_ltwrapper_scriptname_result" # Transform arg to wrapped name. file=$progdir/$program fi ;; esac # Quote arguments (to preserve shell metacharacters). func_append_quoted args "$file" done if $opt_dry_run; then # Display what would be done. if test -n "$shlibpath_var"; then eval "\$ECHO \"\$shlibpath_var=\$$shlibpath_var\"" echo "export $shlibpath_var" fi $ECHO "$cmd$args" exit $EXIT_SUCCESS else if test -n "$shlibpath_var"; then # Export the shlibpath_var. eval "export $shlibpath_var" fi # Restore saved environment variables for lt_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES do eval "if test \"\${save_$lt_var+set}\" = set; then $lt_var=\$save_$lt_var; export $lt_var else $lt_unset $lt_var fi" done # Now prepare to actually exec the command. exec_cmd=\$cmd$args fi } test execute = "$opt_mode" && func_mode_execute ${1+"$@"} # func_mode_finish arg... func_mode_finish () { $debug_cmd libs= libdirs= admincmds= for opt in "$nonopt" ${1+"$@"} do if test -d "$opt"; then func_append libdirs " $opt" elif test -f "$opt"; then if func_lalib_unsafe_p "$opt"; then func_append libs " $opt" else func_warning "'$opt' is not a valid libtool archive" fi else func_fatal_error "invalid argument '$opt'" fi done if test -n "$libs"; then if test -n "$lt_sysroot"; then sysroot_regex=`$ECHO "$lt_sysroot" | $SED "$sed_make_literal_regex"` sysroot_cmd="s/\([ ']\)$sysroot_regex/\1/g;" else sysroot_cmd= fi # Remove sysroot references if $opt_dry_run; then for lib in $libs; do echo "removing references to $lt_sysroot and '=' prefixes from $lib" done else tmpdir=`func_mktempdir` for lib in $libs; do $SED -e "$sysroot_cmd s/\([ ']-[LR]\)=/\1/g; s/\([ ']\)=/\1/g" $lib \ > $tmpdir/tmp-la mv -f $tmpdir/tmp-la $lib done ${RM}r "$tmpdir" fi fi if test -n "$finish_cmds$finish_eval" && test -n "$libdirs"; then for libdir in $libdirs; do if test -n "$finish_cmds"; then # Do each command in the finish commands. func_execute_cmds "$finish_cmds" 'admincmds="$admincmds '"$cmd"'"' fi if test -n "$finish_eval"; then # Do the single finish_eval. eval cmds=\"$finish_eval\" $opt_dry_run || eval "$cmds" || func_append admincmds " $cmds" fi done fi # Exit here if they wanted silent mode. $opt_quiet && exit $EXIT_SUCCESS if test -n "$finish_cmds$finish_eval" && test -n "$libdirs"; then echo "----------------------------------------------------------------------" echo "Libraries have been installed in:" for libdir in $libdirs; do $ECHO " $libdir" done echo echo "If you ever happen to want to link against installed libraries" echo "in a given directory, LIBDIR, you must either use libtool, and" echo "specify the full pathname of the library, or use the '-LLIBDIR'" echo "flag during linking and do at least one of the following:" if test -n "$shlibpath_var"; then echo " - add LIBDIR to the '$shlibpath_var' environment variable" echo " during execution" fi if test -n "$runpath_var"; then echo " - add LIBDIR to the '$runpath_var' environment variable" echo " during linking" fi if test -n "$hardcode_libdir_flag_spec"; then libdir=LIBDIR eval flag=\"$hardcode_libdir_flag_spec\" $ECHO " - use the '$flag' linker flag" fi if test -n "$admincmds"; then $ECHO " - have your system administrator run these commands:$admincmds" fi if test -f /etc/ld.so.conf; then echo " - have your system administrator add LIBDIR to '/etc/ld.so.conf'" fi echo echo "See any operating system documentation about shared libraries for" case $host in solaris2.[6789]|solaris2.1[0-9]) echo "more information, such as the ld(1), crle(1) and ld.so(8) manual" echo "pages." ;; *) echo "more information, such as the ld(1) and ld.so(8) manual pages." ;; esac echo "----------------------------------------------------------------------" fi exit $EXIT_SUCCESS } test finish = "$opt_mode" && func_mode_finish ${1+"$@"} # func_mode_install arg... func_mode_install () { $debug_cmd # There may be an optional sh(1) argument at the beginning of # install_prog (especially on Windows NT). if test "$SHELL" = "$nonopt" || test /bin/sh = "$nonopt" || # Allow the use of GNU shtool's install command. case $nonopt in *shtool*) :;; *) false;; esac then # Aesthetically quote it. func_quote_for_eval "$nonopt" install_prog="$func_quote_for_eval_result " arg=$1 shift else install_prog= arg=$nonopt fi # The real first argument should be the name of the installation program. # Aesthetically quote it. func_quote_for_eval "$arg" func_append install_prog "$func_quote_for_eval_result" install_shared_prog=$install_prog case " $install_prog " in *[\\\ /]cp\ *) install_cp=: ;; *) install_cp=false ;; esac # We need to accept at least all the BSD install flags. dest= files= opts= prev= install_type= isdir=false stripme= no_mode=: for arg do arg2= if test -n "$dest"; then func_append files " $dest" dest=$arg continue fi case $arg in -d) isdir=: ;; -f) if $install_cp; then :; else prev=$arg fi ;; -g | -m | -o) prev=$arg ;; -s) stripme=" -s" continue ;; -*) ;; *) # If the previous option needed an argument, then skip it. if test -n "$prev"; then if test X-m = "X$prev" && test -n "$install_override_mode"; then arg2=$install_override_mode no_mode=false fi prev= else dest=$arg continue fi ;; esac # Aesthetically quote the argument. func_quote_for_eval "$arg" func_append install_prog " $func_quote_for_eval_result" if test -n "$arg2"; then func_quote_for_eval "$arg2" fi func_append install_shared_prog " $func_quote_for_eval_result" done test -z "$install_prog" && \ func_fatal_help "you must specify an install program" test -n "$prev" && \ func_fatal_help "the '$prev' option requires an argument" if test -n "$install_override_mode" && $no_mode; then if $install_cp; then :; else func_quote_for_eval "$install_override_mode" func_append install_shared_prog " -m $func_quote_for_eval_result" fi fi if test -z "$files"; then if test -z "$dest"; then func_fatal_help "no file or destination specified" else func_fatal_help "you must specify a destination" fi fi # Strip any trailing slash from the destination. func_stripname '' '/' "$dest" dest=$func_stripname_result # Check to see that the destination is a directory. test -d "$dest" && isdir=: if $isdir; then destdir=$dest destname= else func_dirname_and_basename "$dest" "" "." destdir=$func_dirname_result destname=$func_basename_result # Not a directory, so check to see that there is only one file specified. set dummy $files; shift test "$#" -gt 1 && \ func_fatal_help "'$dest' is not a directory" fi case $destdir in [\\/]* | [A-Za-z]:[\\/]*) ;; *) for file in $files; do case $file in *.lo) ;; *) func_fatal_help "'$destdir' must be an absolute directory name" ;; esac done ;; esac # This variable tells wrapper scripts just to set variables rather # than running their programs. libtool_install_magic=$magic staticlibs= future_libdirs= current_libdirs= for file in $files; do # Do each installation. case $file in *.$libext) # Do the static libraries later. func_append staticlibs " $file" ;; *.la) func_resolve_sysroot "$file" file=$func_resolve_sysroot_result # Check to see that this really is a libtool archive. func_lalib_unsafe_p "$file" \ || func_fatal_help "'$file' is not a valid libtool archive" library_names= old_library= relink_command= func_source "$file" # Add the libdir to current_libdirs if it is the destination. if test "X$destdir" = "X$libdir"; then case "$current_libdirs " in *" $libdir "*) ;; *) func_append current_libdirs " $libdir" ;; esac else # Note the libdir as a future libdir. case "$future_libdirs " in *" $libdir "*) ;; *) func_append future_libdirs " $libdir" ;; esac fi func_dirname "$file" "/" "" dir=$func_dirname_result func_append dir "$objdir" if test -n "$relink_command"; then # Determine the prefix the user has applied to our future dir. inst_prefix_dir=`$ECHO "$destdir" | $SED -e "s%$libdir\$%%"` # Don't allow the user to place us outside of our expected # location b/c this prevents finding dependent libraries that # are installed to the same prefix. # At present, this check doesn't affect windows .dll's that # are installed into $libdir/../bin (currently, that works fine) # but it's something to keep an eye on. test "$inst_prefix_dir" = "$destdir" && \ func_fatal_error "error: cannot install '$file' to a directory not ending in $libdir" if test -n "$inst_prefix_dir"; then # Stick the inst_prefix_dir data into the link command. relink_command=`$ECHO "$relink_command" | $SED "s%@inst_prefix_dir@%-inst-prefix-dir $inst_prefix_dir%"` else relink_command=`$ECHO "$relink_command" | $SED "s%@inst_prefix_dir@%%"` fi func_warning "relinking '$file'" func_show_eval "$relink_command" \ 'func_fatal_error "error: relink '\''$file'\'' with the above command before installing it"' fi # See the names of the shared library. set dummy $library_names; shift if test -n "$1"; then realname=$1 shift srcname=$realname test -n "$relink_command" && srcname=${realname}T # Install the shared library and build the symlinks. func_show_eval "$install_shared_prog $dir/$srcname $destdir/$realname" \ 'exit $?' tstripme=$stripme case $host_os in cygwin* | mingw* | pw32* | cegcc*) case $realname in *.dll.a) tstripme= ;; esac ;; os2*) case $realname in *_dll.a) tstripme= ;; esac ;; esac if test -n "$tstripme" && test -n "$striplib"; then func_show_eval "$striplib $destdir/$realname" 'exit $?' fi if test "$#" -gt 0; then # Delete the old symlinks, and create new ones. # Try 'ln -sf' first, because the 'ln' binary might depend on # the symlink we replace! Solaris /bin/ln does not understand -f, # so we also need to try rm && ln -s. for linkname do test "$linkname" != "$realname" \ && func_show_eval "(cd $destdir && { $LN_S -f $realname $linkname || { $RM $linkname && $LN_S $realname $linkname; }; })" done fi # Do each command in the postinstall commands. lib=$destdir/$realname func_execute_cmds "$postinstall_cmds" 'exit $?' fi # Install the pseudo-library for information purposes. func_basename "$file" name=$func_basename_result instname=$dir/${name}i func_show_eval "$install_prog $instname $destdir/$name" 'exit $?' # Maybe install the static library, too. test -n "$old_library" && func_append staticlibs " $dir/$old_library" ;; *.lo) # Install (i.e. copy) a libtool object. # Figure out destination file name, if it wasn't already specified. if test -n "$destname"; then destfile=$destdir/$destname else func_basename "$file" destfile=$func_basename_result destfile=$destdir/$destfile fi # Deduce the name of the destination old-style object file. case $destfile in *.lo) func_lo2o "$destfile" staticdest=$func_lo2o_result ;; *.$objext) staticdest=$destfile destfile= ;; *) func_fatal_help "cannot copy a libtool object to '$destfile'" ;; esac # Install the libtool object if requested. test -n "$destfile" && \ func_show_eval "$install_prog $file $destfile" 'exit $?' # Install the old object if enabled. if test yes = "$build_old_libs"; then # Deduce the name of the old-style object file. func_lo2o "$file" staticobj=$func_lo2o_result func_show_eval "$install_prog \$staticobj \$staticdest" 'exit $?' fi exit $EXIT_SUCCESS ;; *) # Figure out destination file name, if it wasn't already specified. if test -n "$destname"; then destfile=$destdir/$destname else func_basename "$file" destfile=$func_basename_result destfile=$destdir/$destfile fi # If the file is missing, and there is a .exe on the end, strip it # because it is most likely a libtool script we actually want to # install stripped_ext= case $file in *.exe) if test ! -f "$file"; then func_stripname '' '.exe' "$file" file=$func_stripname_result stripped_ext=.exe fi ;; esac # Do a test to see if this is really a libtool program. case $host in *cygwin* | *mingw*) if func_ltwrapper_executable_p "$file"; then func_ltwrapper_scriptname "$file" wrapper=$func_ltwrapper_scriptname_result else func_stripname '' '.exe' "$file" wrapper=$func_stripname_result fi ;; *) wrapper=$file ;; esac if func_ltwrapper_script_p "$wrapper"; then notinst_deplibs= relink_command= func_source "$wrapper" # Check the variables that should have been set. test -z "$generated_by_libtool_version" && \ func_fatal_error "invalid libtool wrapper script '$wrapper'" finalize=: for lib in $notinst_deplibs; do # Check to see that each library is installed. libdir= if test -f "$lib"; then func_source "$lib" fi libfile=$libdir/`$ECHO "$lib" | $SED 's%^.*/%%g'` if test -n "$libdir" && test ! -f "$libfile"; then func_warning "'$lib' has not been installed in '$libdir'" finalize=false fi done relink_command= func_source "$wrapper" outputname= if test no = "$fast_install" && test -n "$relink_command"; then $opt_dry_run || { if $finalize; then tmpdir=`func_mktempdir` func_basename "$file$stripped_ext" file=$func_basename_result outputname=$tmpdir/$file # Replace the output file specification. relink_command=`$ECHO "$relink_command" | $SED 's%@OUTPUT@%'"$outputname"'%g'` $opt_quiet || { func_quote_for_expand "$relink_command" eval "func_echo $func_quote_for_expand_result" } if eval "$relink_command"; then : else func_error "error: relink '$file' with the above command before installing it" $opt_dry_run || ${RM}r "$tmpdir" continue fi file=$outputname else func_warning "cannot relink '$file'" fi } else # Install the binary that we compiled earlier. file=`$ECHO "$file$stripped_ext" | $SED "s%\([^/]*\)$%$objdir/\1%"` fi fi # remove .exe since cygwin /usr/bin/install will append another # one anyway case $install_prog,$host in */usr/bin/install*,*cygwin*) case $file:$destfile in *.exe:*.exe) # this is ok ;; *.exe:*) destfile=$destfile.exe ;; *:*.exe) func_stripname '' '.exe' "$destfile" destfile=$func_stripname_result ;; esac ;; esac func_show_eval "$install_prog\$stripme \$file \$destfile" 'exit $?' $opt_dry_run || if test -n "$outputname"; then ${RM}r "$tmpdir" fi ;; esac done for file in $staticlibs; do func_basename "$file" name=$func_basename_result # Set up the ranlib parameters. oldlib=$destdir/$name func_to_tool_file "$oldlib" func_convert_file_msys_to_w32 tool_oldlib=$func_to_tool_file_result func_show_eval "$install_prog \$file \$oldlib" 'exit $?' if test -n "$stripme" && test -n "$old_striplib"; then func_show_eval "$old_striplib $tool_oldlib" 'exit $?' fi # Do each command in the postinstall commands. func_execute_cmds "$old_postinstall_cmds" 'exit $?' done test -n "$future_libdirs" && \ func_warning "remember to run '$progname --finish$future_libdirs'" if test -n "$current_libdirs"; then # Maybe just do a dry run. $opt_dry_run && current_libdirs=" -n$current_libdirs" exec_cmd='$SHELL "$progpath" $preserve_args --finish$current_libdirs' else exit $EXIT_SUCCESS fi } test install = "$opt_mode" && func_mode_install ${1+"$@"} # func_generate_dlsyms outputname originator pic_p # Extract symbols from dlprefiles and create ${outputname}S.o with # a dlpreopen symbol table. func_generate_dlsyms () { $debug_cmd my_outputname=$1 my_originator=$2 my_pic_p=${3-false} my_prefix=`$ECHO "$my_originator" | $SED 's%[^a-zA-Z0-9]%_%g'` my_dlsyms= if test -n "$dlfiles$dlprefiles" || test no != "$dlself"; then if test -n "$NM" && test -n "$global_symbol_pipe"; then my_dlsyms=${my_outputname}S.c else func_error "not configured to extract global symbols from dlpreopened files" fi fi if test -n "$my_dlsyms"; then case $my_dlsyms in "") ;; *.c) # Discover the nlist of each of the dlfiles. nlist=$output_objdir/$my_outputname.nm func_show_eval "$RM $nlist ${nlist}S ${nlist}T" # Parse the name list into a source file. func_verbose "creating $output_objdir/$my_dlsyms" $opt_dry_run || $ECHO > "$output_objdir/$my_dlsyms" "\ /* $my_dlsyms - symbol resolution table for '$my_outputname' dlsym emulation. */ /* Generated by $PROGRAM (GNU $PACKAGE) $VERSION */ #ifdef __cplusplus extern \"C\" { #endif #if defined __GNUC__ && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)) || (__GNUC__ > 4)) #pragma GCC diagnostic ignored \"-Wstrict-prototypes\" #endif /* Keep this code in sync between libtool.m4, ltmain, lt_system.h, and tests. */ #if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE /* DATA imports from DLLs on WIN32 can't be const, because runtime relocations are performed -- see ld's documentation on pseudo-relocs. */ # define LT_DLSYM_CONST #elif defined __osf__ /* This system does not cope well with relocations in const data. */ # define LT_DLSYM_CONST #else # define LT_DLSYM_CONST const #endif #define STREQ(s1, s2) (strcmp ((s1), (s2)) == 0) /* External symbol declarations for the compiler. */\ " if test yes = "$dlself"; then func_verbose "generating symbol list for '$output'" $opt_dry_run || echo ': @PROGRAM@ ' > "$nlist" # Add our own program objects to the symbol list. progfiles=`$ECHO "$objs$old_deplibs" | $SP2NL | $SED "$lo2o" | $NL2SP` for progfile in $progfiles; do func_to_tool_file "$progfile" func_convert_file_msys_to_w32 func_verbose "extracting global C symbols from '$func_to_tool_file_result'" $opt_dry_run || eval "$NM $func_to_tool_file_result | $global_symbol_pipe >> '$nlist'" done if test -n "$exclude_expsyms"; then $opt_dry_run || { eval '$EGREP -v " ($exclude_expsyms)$" "$nlist" > "$nlist"T' eval '$MV "$nlist"T "$nlist"' } fi if test -n "$export_symbols_regex"; then $opt_dry_run || { eval '$EGREP -e "$export_symbols_regex" "$nlist" > "$nlist"T' eval '$MV "$nlist"T "$nlist"' } fi # Prepare the list of exported symbols if test -z "$export_symbols"; then export_symbols=$output_objdir/$outputname.exp $opt_dry_run || { $RM $export_symbols eval "$SED -n -e '/^: @PROGRAM@ $/d' -e 's/^.* \(.*\)$/\1/p' "'< "$nlist" > "$export_symbols"' case $host in *cygwin* | *mingw* | *cegcc* ) eval "echo EXPORTS "'> "$output_objdir/$outputname.def"' eval 'cat "$export_symbols" >> "$output_objdir/$outputname.def"' ;; esac } else $opt_dry_run || { eval "$SED -e 's/\([].[*^$]\)/\\\\\1/g' -e 's/^/ /' -e 's/$/$/'"' < "$export_symbols" > "$output_objdir/$outputname.exp"' eval '$GREP -f "$output_objdir/$outputname.exp" < "$nlist" > "$nlist"T' eval '$MV "$nlist"T "$nlist"' case $host in *cygwin* | *mingw* | *cegcc* ) eval "echo EXPORTS "'> "$output_objdir/$outputname.def"' eval 'cat "$nlist" >> "$output_objdir/$outputname.def"' ;; esac } fi fi for dlprefile in $dlprefiles; do func_verbose "extracting global C symbols from '$dlprefile'" func_basename "$dlprefile" name=$func_basename_result case $host in *cygwin* | *mingw* | *cegcc* ) # if an import library, we need to obtain dlname if func_win32_import_lib_p "$dlprefile"; then func_tr_sh "$dlprefile" eval "curr_lafile=\$libfile_$func_tr_sh_result" dlprefile_dlbasename= if test -n "$curr_lafile" && func_lalib_p "$curr_lafile"; then # Use subshell, to avoid clobbering current variable values dlprefile_dlname=`source "$curr_lafile" && echo "$dlname"` if test -n "$dlprefile_dlname"; then func_basename "$dlprefile_dlname" dlprefile_dlbasename=$func_basename_result else # no lafile. user explicitly requested -dlpreopen . $sharedlib_from_linklib_cmd "$dlprefile" dlprefile_dlbasename=$sharedlib_from_linklib_result fi fi $opt_dry_run || { if test -n "$dlprefile_dlbasename"; then eval '$ECHO ": $dlprefile_dlbasename" >> "$nlist"' else func_warning "Could not compute DLL name from $name" eval '$ECHO ": $name " >> "$nlist"' fi func_to_tool_file "$dlprefile" func_convert_file_msys_to_w32 eval "$NM \"$func_to_tool_file_result\" 2>/dev/null | $global_symbol_pipe | $SED -e '/I __imp/d' -e 's/I __nm_/D /;s/_nm__//' >> '$nlist'" } else # not an import lib $opt_dry_run || { eval '$ECHO ": $name " >> "$nlist"' func_to_tool_file "$dlprefile" func_convert_file_msys_to_w32 eval "$NM \"$func_to_tool_file_result\" 2>/dev/null | $global_symbol_pipe >> '$nlist'" } fi ;; *) $opt_dry_run || { eval '$ECHO ": $name " >> "$nlist"' func_to_tool_file "$dlprefile" func_convert_file_msys_to_w32 eval "$NM \"$func_to_tool_file_result\" 2>/dev/null | $global_symbol_pipe >> '$nlist'" } ;; esac done $opt_dry_run || { # Make sure we have at least an empty file. test -f "$nlist" || : > "$nlist" if test -n "$exclude_expsyms"; then $EGREP -v " ($exclude_expsyms)$" "$nlist" > "$nlist"T $MV "$nlist"T "$nlist" fi # Try sorting and uniquifying the output. if $GREP -v "^: " < "$nlist" | if sort -k 3 /dev/null 2>&1; then sort -k 3 else sort +2 fi | uniq > "$nlist"S; then : else $GREP -v "^: " < "$nlist" > "$nlist"S fi if test -f "$nlist"S; then eval "$global_symbol_to_cdecl"' < "$nlist"S >> "$output_objdir/$my_dlsyms"' else echo '/* NONE */' >> "$output_objdir/$my_dlsyms" fi func_show_eval '$RM "${nlist}I"' if test -n "$global_symbol_to_import"; then eval "$global_symbol_to_import"' < "$nlist"S > "$nlist"I' fi echo >> "$output_objdir/$my_dlsyms" "\ /* The mapping between symbol names and symbols. */ typedef struct { const char *name; void *address; } lt_dlsymlist; extern LT_DLSYM_CONST lt_dlsymlist lt_${my_prefix}_LTX_preloaded_symbols[];\ " if test -s "$nlist"I; then echo >> "$output_objdir/$my_dlsyms" "\ static void lt_syminit(void) { LT_DLSYM_CONST lt_dlsymlist *symbol = lt_${my_prefix}_LTX_preloaded_symbols; for (; symbol->name; ++symbol) {" $SED 's/.*/ if (STREQ (symbol->name, \"&\")) symbol->address = (void *) \&&;/' < "$nlist"I >> "$output_objdir/$my_dlsyms" echo >> "$output_objdir/$my_dlsyms" "\ } }" fi echo >> "$output_objdir/$my_dlsyms" "\ LT_DLSYM_CONST lt_dlsymlist lt_${my_prefix}_LTX_preloaded_symbols[] = { {\"$my_originator\", (void *) 0}," if test -s "$nlist"I; then echo >> "$output_objdir/$my_dlsyms" "\ {\"@INIT@\", (void *) <_syminit}," fi case $need_lib_prefix in no) eval "$global_symbol_to_c_name_address" < "$nlist" >> "$output_objdir/$my_dlsyms" ;; *) eval "$global_symbol_to_c_name_address_lib_prefix" < "$nlist" >> "$output_objdir/$my_dlsyms" ;; esac echo >> "$output_objdir/$my_dlsyms" "\ {0, (void *) 0} }; /* This works around a problem in FreeBSD linker */ #ifdef FREEBSD_WORKAROUND static const void *lt_preloaded_setup() { return lt_${my_prefix}_LTX_preloaded_symbols; } #endif #ifdef __cplusplus } #endif\ " } # !$opt_dry_run pic_flag_for_symtable= case "$compile_command " in *" -static "*) ;; *) case $host in # compiling the symbol table file with pic_flag works around # a FreeBSD bug that causes programs to crash when -lm is # linked before any other PIC object. But we must not use # pic_flag when linking with -static. The problem exists in # FreeBSD 2.2.6 and is fixed in FreeBSD 3.1. *-*-freebsd2.*|*-*-freebsd3.0*|*-*-freebsdelf3.0*) pic_flag_for_symtable=" $pic_flag -DFREEBSD_WORKAROUND" ;; *-*-hpux*) pic_flag_for_symtable=" $pic_flag" ;; *) $my_pic_p && pic_flag_for_symtable=" $pic_flag" ;; esac ;; esac symtab_cflags= for arg in $LTCFLAGS; do case $arg in -pie | -fpie | -fPIE) ;; *) func_append symtab_cflags " $arg" ;; esac done # Now compile the dynamic symbol file. func_show_eval '(cd $output_objdir && $LTCC$symtab_cflags -c$no_builtin_flag$pic_flag_for_symtable "$my_dlsyms")' 'exit $?' # Clean up the generated files. func_show_eval '$RM "$output_objdir/$my_dlsyms" "$nlist" "${nlist}S" "${nlist}T" "${nlist}I"' # Transform the symbol file into the correct name. symfileobj=$output_objdir/${my_outputname}S.$objext case $host in *cygwin* | *mingw* | *cegcc* ) if test -f "$output_objdir/$my_outputname.def"; then compile_command=`$ECHO "$compile_command" | $SED "s%@SYMFILE@%$output_objdir/$my_outputname.def $symfileobj%"` finalize_command=`$ECHO "$finalize_command" | $SED "s%@SYMFILE@%$output_objdir/$my_outputname.def $symfileobj%"` else compile_command=`$ECHO "$compile_command" | $SED "s%@SYMFILE@%$symfileobj%"` finalize_command=`$ECHO "$finalize_command" | $SED "s%@SYMFILE@%$symfileobj%"` fi ;; *) compile_command=`$ECHO "$compile_command" | $SED "s%@SYMFILE@%$symfileobj%"` finalize_command=`$ECHO "$finalize_command" | $SED "s%@SYMFILE@%$symfileobj%"` ;; esac ;; *) func_fatal_error "unknown suffix for '$my_dlsyms'" ;; esac else # We keep going just in case the user didn't refer to # lt_preloaded_symbols. The linker will fail if global_symbol_pipe # really was required. # Nullify the symbol file. compile_command=`$ECHO "$compile_command" | $SED "s% @SYMFILE@%%"` finalize_command=`$ECHO "$finalize_command" | $SED "s% @SYMFILE@%%"` fi } # func_cygming_gnu_implib_p ARG # This predicate returns with zero status (TRUE) if # ARG is a GNU/binutils-style import library. Returns # with nonzero status (FALSE) otherwise. func_cygming_gnu_implib_p () { $debug_cmd func_to_tool_file "$1" func_convert_file_msys_to_w32 func_cygming_gnu_implib_tmp=`$NM "$func_to_tool_file_result" | eval "$global_symbol_pipe" | $EGREP ' (_head_[A-Za-z0-9_]+_[ad]l*|[A-Za-z0-9_]+_[ad]l*_iname)$'` test -n "$func_cygming_gnu_implib_tmp" } # func_cygming_ms_implib_p ARG # This predicate returns with zero status (TRUE) if # ARG is an MS-style import library. Returns # with nonzero status (FALSE) otherwise. func_cygming_ms_implib_p () { $debug_cmd func_to_tool_file "$1" func_convert_file_msys_to_w32 func_cygming_ms_implib_tmp=`$NM "$func_to_tool_file_result" | eval "$global_symbol_pipe" | $GREP '_NULL_IMPORT_DESCRIPTOR'` test -n "$func_cygming_ms_implib_tmp" } # func_win32_libid arg # return the library type of file 'arg' # # Need a lot of goo to handle *both* DLLs and import libs # Has to be a shell function in order to 'eat' the argument # that is supplied when $file_magic_command is called. # Despite the name, also deal with 64 bit binaries. func_win32_libid () { $debug_cmd win32_libid_type=unknown win32_fileres=`file -L $1 2>/dev/null` case $win32_fileres in *ar\ archive\ import\ library*) # definitely import win32_libid_type="x86 archive import" ;; *ar\ archive*) # could be an import, or static # Keep the egrep pattern in sync with the one in _LT_CHECK_MAGIC_METHOD. if eval $OBJDUMP -f $1 | $SED -e '10q' 2>/dev/null | $EGREP 'file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' >/dev/null; then case $nm_interface in "MS dumpbin") if func_cygming_ms_implib_p "$1" || func_cygming_gnu_implib_p "$1" then win32_nmres=import else win32_nmres= fi ;; *) func_to_tool_file "$1" func_convert_file_msys_to_w32 win32_nmres=`eval $NM -f posix -A \"$func_to_tool_file_result\" | $SED -n -e ' 1,100{ / I /{ s|.*|import| p q } }'` ;; esac case $win32_nmres in import*) win32_libid_type="x86 archive import";; *) win32_libid_type="x86 archive static";; esac fi ;; *DLL*) win32_libid_type="x86 DLL" ;; *executable*) # but shell scripts are "executable" too... case $win32_fileres in *MS\ Windows\ PE\ Intel*) win32_libid_type="x86 DLL" ;; esac ;; esac $ECHO "$win32_libid_type" } # func_cygming_dll_for_implib ARG # # Platform-specific function to extract the # name of the DLL associated with the specified # import library ARG. # Invoked by eval'ing the libtool variable # $sharedlib_from_linklib_cmd # Result is available in the variable # $sharedlib_from_linklib_result func_cygming_dll_for_implib () { $debug_cmd sharedlib_from_linklib_result=`$DLLTOOL --identify-strict --identify "$1"` } # func_cygming_dll_for_implib_fallback_core SECTION_NAME LIBNAMEs # # The is the core of a fallback implementation of a # platform-specific function to extract the name of the # DLL associated with the specified import library LIBNAME. # # SECTION_NAME is either .idata$6 or .idata$7, depending # on the platform and compiler that created the implib. # # Echos the name of the DLL associated with the # specified import library. func_cygming_dll_for_implib_fallback_core () { $debug_cmd match_literal=`$ECHO "$1" | $SED "$sed_make_literal_regex"` $OBJDUMP -s --section "$1" "$2" 2>/dev/null | $SED '/^Contents of section '"$match_literal"':/{ # Place marker at beginning of archive member dllname section s/.*/====MARK====/ p d } # These lines can sometimes be longer than 43 characters, but # are always uninteresting /:[ ]*file format pe[i]\{,1\}-/d /^In archive [^:]*:/d # Ensure marker is printed /^====MARK====/p # Remove all lines with less than 43 characters /^.\{43\}/!d # From remaining lines, remove first 43 characters s/^.\{43\}//' | $SED -n ' # Join marker and all lines until next marker into a single line /^====MARK====/ b para H $ b para b :para x s/\n//g # Remove the marker s/^====MARK====// # Remove trailing dots and whitespace s/[\. \t]*$// # Print /./p' | # we now have a list, one entry per line, of the stringified # contents of the appropriate section of all members of the # archive that possess that section. Heuristic: eliminate # all those that have a first or second character that is # a '.' (that is, objdump's representation of an unprintable # character.) This should work for all archives with less than # 0x302f exports -- but will fail for DLLs whose name actually # begins with a literal '.' or a single character followed by # a '.'. # # Of those that remain, print the first one. $SED -e '/^\./d;/^.\./d;q' } # func_cygming_dll_for_implib_fallback ARG # Platform-specific function to extract the # name of the DLL associated with the specified # import library ARG. # # This fallback implementation is for use when $DLLTOOL # does not support the --identify-strict option. # Invoked by eval'ing the libtool variable # $sharedlib_from_linklib_cmd # Result is available in the variable # $sharedlib_from_linklib_result func_cygming_dll_for_implib_fallback () { $debug_cmd if func_cygming_gnu_implib_p "$1"; then # binutils import library sharedlib_from_linklib_result=`func_cygming_dll_for_implib_fallback_core '.idata$7' "$1"` elif func_cygming_ms_implib_p "$1"; then # ms-generated import library sharedlib_from_linklib_result=`func_cygming_dll_for_implib_fallback_core '.idata$6' "$1"` else # unknown sharedlib_from_linklib_result= fi } # func_extract_an_archive dir oldlib func_extract_an_archive () { $debug_cmd f_ex_an_ar_dir=$1; shift f_ex_an_ar_oldlib=$1 if test yes = "$lock_old_archive_extraction"; then lockfile=$f_ex_an_ar_oldlib.lock until $opt_dry_run || ln "$progpath" "$lockfile" 2>/dev/null; do func_echo "Waiting for $lockfile to be removed" sleep 2 done fi func_show_eval "(cd \$f_ex_an_ar_dir && $AR x \"\$f_ex_an_ar_oldlib\")" \ 'stat=$?; rm -f "$lockfile"; exit $stat' if test yes = "$lock_old_archive_extraction"; then $opt_dry_run || rm -f "$lockfile" fi if ($AR t "$f_ex_an_ar_oldlib" | sort | sort -uc >/dev/null 2>&1); then : else func_fatal_error "object name conflicts in archive: $f_ex_an_ar_dir/$f_ex_an_ar_oldlib" fi } # func_extract_archives gentop oldlib ... func_extract_archives () { $debug_cmd my_gentop=$1; shift my_oldlibs=${1+"$@"} my_oldobjs= my_xlib= my_xabs= my_xdir= for my_xlib in $my_oldlibs; do # Extract the objects. case $my_xlib in [\\/]* | [A-Za-z]:[\\/]*) my_xabs=$my_xlib ;; *) my_xabs=`pwd`"/$my_xlib" ;; esac func_basename "$my_xlib" my_xlib=$func_basename_result my_xlib_u=$my_xlib while :; do case " $extracted_archives " in *" $my_xlib_u "*) func_arith $extracted_serial + 1 extracted_serial=$func_arith_result my_xlib_u=lt$extracted_serial-$my_xlib ;; *) break ;; esac done extracted_archives="$extracted_archives $my_xlib_u" my_xdir=$my_gentop/$my_xlib_u func_mkdir_p "$my_xdir" case $host in *-darwin*) func_verbose "Extracting $my_xabs" # Do not bother doing anything if just a dry run $opt_dry_run || { darwin_orig_dir=`pwd` cd $my_xdir || exit $? darwin_archive=$my_xabs darwin_curdir=`pwd` func_basename "$darwin_archive" darwin_base_archive=$func_basename_result darwin_arches=`$LIPO -info "$darwin_archive" 2>/dev/null | $GREP Architectures 2>/dev/null || true` if test -n "$darwin_arches"; then darwin_arches=`$ECHO "$darwin_arches" | $SED -e 's/.*are://'` darwin_arch= func_verbose "$darwin_base_archive has multiple architectures $darwin_arches" for darwin_arch in $darwin_arches; do func_mkdir_p "unfat-$$/$darwin_base_archive-$darwin_arch" $LIPO -thin $darwin_arch -output "unfat-$$/$darwin_base_archive-$darwin_arch/$darwin_base_archive" "$darwin_archive" cd "unfat-$$/$darwin_base_archive-$darwin_arch" func_extract_an_archive "`pwd`" "$darwin_base_archive" cd "$darwin_curdir" $RM "unfat-$$/$darwin_base_archive-$darwin_arch/$darwin_base_archive" done # $darwin_arches ## Okay now we've a bunch of thin objects, gotta fatten them up :) darwin_filelist=`find unfat-$$ -type f -name \*.o -print -o -name \*.lo -print | $SED -e "$sed_basename" | sort -u` darwin_file= darwin_files= for darwin_file in $darwin_filelist; do darwin_files=`find unfat-$$ -name $darwin_file -print | sort | $NL2SP` $LIPO -create -output "$darwin_file" $darwin_files done # $darwin_filelist $RM -rf unfat-$$ cd "$darwin_orig_dir" else cd $darwin_orig_dir func_extract_an_archive "$my_xdir" "$my_xabs" fi # $darwin_arches } # !$opt_dry_run ;; *) func_extract_an_archive "$my_xdir" "$my_xabs" ;; esac my_oldobjs="$my_oldobjs "`find $my_xdir -name \*.$objext -print -o -name \*.lo -print | sort | $NL2SP` done func_extract_archives_result=$my_oldobjs } # func_emit_wrapper [arg=no] # # Emit a libtool wrapper script on stdout. # Don't directly open a file because we may want to # incorporate the script contents within a cygwin/mingw # wrapper executable. Must ONLY be called from within # func_mode_link because it depends on a number of variables # set therein. # # ARG is the value that the WRAPPER_SCRIPT_BELONGS_IN_OBJDIR # variable will take. If 'yes', then the emitted script # will assume that the directory where it is stored is # the $objdir directory. This is a cygwin/mingw-specific # behavior. func_emit_wrapper () { func_emit_wrapper_arg1=${1-no} $ECHO "\ #! $SHELL # $output - temporary wrapper script for $objdir/$outputname # Generated by $PROGRAM (GNU $PACKAGE) $VERSION # # The $output program cannot be directly executed until all the libtool # libraries that it depends on are installed. # # This wrapper script should never be moved out of the build directory. # If it is, it will not operate correctly. # Sed substitution that helps us do robust quoting. It backslashifies # metacharacters that are still active within double-quoted strings. sed_quote_subst='$sed_quote_subst' # Be Bourne compatible if test -n \"\${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then emulate sh NULLCMD=: # Zsh 3.x and 4.x performs word splitting on \${1+\"\$@\"}, which # is contrary to our usage. Disable this feature. alias -g '\${1+\"\$@\"}'='\"\$@\"' setopt NO_GLOB_SUBST else case \`(set -o) 2>/dev/null\` in *posix*) set -o posix;; esac fi BIN_SH=xpg4; export BIN_SH # for Tru64 DUALCASE=1; export DUALCASE # for MKS sh # The HP-UX ksh and POSIX shell print the target directory to stdout # if CDPATH is set. (unset CDPATH) >/dev/null 2>&1 && unset CDPATH relink_command=\"$relink_command\" # This environment variable determines our operation mode. if test \"\$libtool_install_magic\" = \"$magic\"; then # install mode needs the following variables: generated_by_libtool_version='$macro_version' notinst_deplibs='$notinst_deplibs' else # When we are sourced in execute mode, \$file and \$ECHO are already set. if test \"\$libtool_execute_magic\" != \"$magic\"; then file=\"\$0\"" qECHO=`$ECHO "$ECHO" | $SED "$sed_quote_subst"` $ECHO "\ # A function that is used when there is no print builtin or printf. func_fallback_echo () { eval 'cat <<_LTECHO_EOF \$1 _LTECHO_EOF' } ECHO=\"$qECHO\" fi # Very basic option parsing. These options are (a) specific to # the libtool wrapper, (b) are identical between the wrapper # /script/ and the wrapper /executable/ that is used only on # windows platforms, and (c) all begin with the string "--lt-" # (application programs are unlikely to have options that match # this pattern). # # There are only two supported options: --lt-debug and # --lt-dump-script. There is, deliberately, no --lt-help. # # The first argument to this parsing function should be the # script's $0 value, followed by "$@". lt_option_debug= func_parse_lt_options () { lt_script_arg0=\$0 shift for lt_opt do case \"\$lt_opt\" in --lt-debug) lt_option_debug=1 ;; --lt-dump-script) lt_dump_D=\`\$ECHO \"X\$lt_script_arg0\" | $SED -e 's/^X//' -e 's%/[^/]*$%%'\` test \"X\$lt_dump_D\" = \"X\$lt_script_arg0\" && lt_dump_D=. lt_dump_F=\`\$ECHO \"X\$lt_script_arg0\" | $SED -e 's/^X//' -e 's%^.*/%%'\` cat \"\$lt_dump_D/\$lt_dump_F\" exit 0 ;; --lt-*) \$ECHO \"Unrecognized --lt- option: '\$lt_opt'\" 1>&2 exit 1 ;; esac done # Print the debug banner immediately: if test -n \"\$lt_option_debug\"; then echo \"$outputname:$output:\$LINENO: libtool wrapper (GNU $PACKAGE) $VERSION\" 1>&2 fi } # Used when --lt-debug. Prints its arguments to stdout # (redirection is the responsibility of the caller) func_lt_dump_args () { lt_dump_args_N=1; for lt_arg do \$ECHO \"$outputname:$output:\$LINENO: newargv[\$lt_dump_args_N]: \$lt_arg\" lt_dump_args_N=\`expr \$lt_dump_args_N + 1\` done } # Core function for launching the target application func_exec_program_core () { " case $host in # Backslashes separate directories on plain windows *-*-mingw | *-*-os2* | *-cegcc*) $ECHO "\ if test -n \"\$lt_option_debug\"; then \$ECHO \"$outputname:$output:\$LINENO: newargv[0]: \$progdir\\\\\$program\" 1>&2 func_lt_dump_args \${1+\"\$@\"} 1>&2 fi exec \"\$progdir\\\\\$program\" \${1+\"\$@\"} " ;; *) $ECHO "\ if test -n \"\$lt_option_debug\"; then \$ECHO \"$outputname:$output:\$LINENO: newargv[0]: \$progdir/\$program\" 1>&2 func_lt_dump_args \${1+\"\$@\"} 1>&2 fi exec \"\$progdir/\$program\" \${1+\"\$@\"} " ;; esac $ECHO "\ \$ECHO \"\$0: cannot exec \$program \$*\" 1>&2 exit 1 } # A function to encapsulate launching the target application # Strips options in the --lt-* namespace from \$@ and # launches target application with the remaining arguments. func_exec_program () { case \" \$* \" in *\\ --lt-*) for lt_wr_arg do case \$lt_wr_arg in --lt-*) ;; *) set x \"\$@\" \"\$lt_wr_arg\"; shift;; esac shift done ;; esac func_exec_program_core \${1+\"\$@\"} } # Parse options func_parse_lt_options \"\$0\" \${1+\"\$@\"} # Find the directory that this script lives in. thisdir=\`\$ECHO \"\$file\" | $SED 's%/[^/]*$%%'\` test \"x\$thisdir\" = \"x\$file\" && thisdir=. # Follow symbolic links until we get to the real thisdir. file=\`ls -ld \"\$file\" | $SED -n 's/.*-> //p'\` while test -n \"\$file\"; do destdir=\`\$ECHO \"\$file\" | $SED 's%/[^/]*\$%%'\` # If there was a directory component, then change thisdir. if test \"x\$destdir\" != \"x\$file\"; then case \"\$destdir\" in [\\\\/]* | [A-Za-z]:[\\\\/]*) thisdir=\"\$destdir\" ;; *) thisdir=\"\$thisdir/\$destdir\" ;; esac fi file=\`\$ECHO \"\$file\" | $SED 's%^.*/%%'\` file=\`ls -ld \"\$thisdir/\$file\" | $SED -n 's/.*-> //p'\` done # Usually 'no', except on cygwin/mingw when embedded into # the cwrapper. WRAPPER_SCRIPT_BELONGS_IN_OBJDIR=$func_emit_wrapper_arg1 if test \"\$WRAPPER_SCRIPT_BELONGS_IN_OBJDIR\" = \"yes\"; then # special case for '.' if test \"\$thisdir\" = \".\"; then thisdir=\`pwd\` fi # remove .libs from thisdir case \"\$thisdir\" in *[\\\\/]$objdir ) thisdir=\`\$ECHO \"\$thisdir\" | $SED 's%[\\\\/][^\\\\/]*$%%'\` ;; $objdir ) thisdir=. ;; esac fi # Try to get the absolute directory name. absdir=\`cd \"\$thisdir\" && pwd\` test -n \"\$absdir\" && thisdir=\"\$absdir\" " if test yes = "$fast_install"; then $ECHO "\ program=lt-'$outputname'$exeext progdir=\"\$thisdir/$objdir\" if test ! -f \"\$progdir/\$program\" || { file=\`ls -1dt \"\$progdir/\$program\" \"\$progdir/../\$program\" 2>/dev/null | $SED 1q\`; \\ test \"X\$file\" != \"X\$progdir/\$program\"; }; then file=\"\$\$-\$program\" if test ! -d \"\$progdir\"; then $MKDIR \"\$progdir\" else $RM \"\$progdir/\$file\" fi" $ECHO "\ # relink executable if necessary if test -n \"\$relink_command\"; then if relink_command_output=\`eval \$relink_command 2>&1\`; then : else \$ECHO \"\$relink_command_output\" >&2 $RM \"\$progdir/\$file\" exit 1 fi fi $MV \"\$progdir/\$file\" \"\$progdir/\$program\" 2>/dev/null || { $RM \"\$progdir/\$program\"; $MV \"\$progdir/\$file\" \"\$progdir/\$program\"; } $RM \"\$progdir/\$file\" fi" else $ECHO "\ program='$outputname' progdir=\"\$thisdir/$objdir\" " fi $ECHO "\ if test -f \"\$progdir/\$program\"; then" # fixup the dll searchpath if we need to. # # Fix the DLL searchpath if we need to. Do this before prepending # to shlibpath, because on Windows, both are PATH and uninstalled # libraries must come first. if test -n "$dllsearchpath"; then $ECHO "\ # Add the dll search path components to the executable PATH PATH=$dllsearchpath:\$PATH " fi # Export our shlibpath_var if we have one. if test yes = "$shlibpath_overrides_runpath" && test -n "$shlibpath_var" && test -n "$temp_rpath"; then $ECHO "\ # Add our own library path to $shlibpath_var $shlibpath_var=\"$temp_rpath\$$shlibpath_var\" # Some systems cannot cope with colon-terminated $shlibpath_var # The second colon is a workaround for a bug in BeOS R4 sed $shlibpath_var=\`\$ECHO \"\$$shlibpath_var\" | $SED 's/::*\$//'\` export $shlibpath_var " fi $ECHO "\ if test \"\$libtool_execute_magic\" != \"$magic\"; then # Run the actual program with our arguments. func_exec_program \${1+\"\$@\"} fi else # The program doesn't exist. \$ECHO \"\$0: error: '\$progdir/\$program' does not exist\" 1>&2 \$ECHO \"This script is just a wrapper for \$program.\" 1>&2 \$ECHO \"See the $PACKAGE documentation for more information.\" 1>&2 exit 1 fi fi\ " } # func_emit_cwrapperexe_src # emit the source code for a wrapper executable on stdout # Must ONLY be called from within func_mode_link because # it depends on a number of variable set therein. func_emit_cwrapperexe_src () { cat < #include #ifdef _MSC_VER # include # include # include #else # include # include # ifdef __CYGWIN__ # include # endif #endif #include #include #include #include #include #include #include #include #define STREQ(s1, s2) (strcmp ((s1), (s2)) == 0) /* declarations of non-ANSI functions */ #if defined __MINGW32__ # ifdef __STRICT_ANSI__ int _putenv (const char *); # endif #elif defined __CYGWIN__ # ifdef __STRICT_ANSI__ char *realpath (const char *, char *); int putenv (char *); int setenv (const char *, const char *, int); # endif /* #elif defined other_platform || defined ... */ #endif /* portability defines, excluding path handling macros */ #if defined _MSC_VER # define setmode _setmode # define stat _stat # define chmod _chmod # define getcwd _getcwd # define putenv _putenv # define S_IXUSR _S_IEXEC #elif defined __MINGW32__ # define setmode _setmode # define stat _stat # define chmod _chmod # define getcwd _getcwd # define putenv _putenv #elif defined __CYGWIN__ # define HAVE_SETENV # define FOPEN_WB "wb" /* #elif defined other platforms ... */ #endif #if defined PATH_MAX # define LT_PATHMAX PATH_MAX #elif defined MAXPATHLEN # define LT_PATHMAX MAXPATHLEN #else # define LT_PATHMAX 1024 #endif #ifndef S_IXOTH # define S_IXOTH 0 #endif #ifndef S_IXGRP # define S_IXGRP 0 #endif /* path handling portability macros */ #ifndef DIR_SEPARATOR # define DIR_SEPARATOR '/' # define PATH_SEPARATOR ':' #endif #if defined _WIN32 || defined __MSDOS__ || defined __DJGPP__ || \ defined __OS2__ # define HAVE_DOS_BASED_FILE_SYSTEM # define FOPEN_WB "wb" # ifndef DIR_SEPARATOR_2 # define DIR_SEPARATOR_2 '\\' # endif # ifndef PATH_SEPARATOR_2 # define PATH_SEPARATOR_2 ';' # endif #endif #ifndef DIR_SEPARATOR_2 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) #else /* DIR_SEPARATOR_2 */ # define IS_DIR_SEPARATOR(ch) \ (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) #endif /* DIR_SEPARATOR_2 */ #ifndef PATH_SEPARATOR_2 # define IS_PATH_SEPARATOR(ch) ((ch) == PATH_SEPARATOR) #else /* PATH_SEPARATOR_2 */ # define IS_PATH_SEPARATOR(ch) ((ch) == PATH_SEPARATOR_2) #endif /* PATH_SEPARATOR_2 */ #ifndef FOPEN_WB # define FOPEN_WB "w" #endif #ifndef _O_BINARY # define _O_BINARY 0 #endif #define XMALLOC(type, num) ((type *) xmalloc ((num) * sizeof(type))) #define XFREE(stale) do { \ if (stale) { free (stale); stale = 0; } \ } while (0) #if defined LT_DEBUGWRAPPER static int lt_debug = 1; #else static int lt_debug = 0; #endif const char *program_name = "libtool-wrapper"; /* in case xstrdup fails */ void *xmalloc (size_t num); char *xstrdup (const char *string); const char *base_name (const char *name); char *find_executable (const char *wrapper); char *chase_symlinks (const char *pathspec); int make_executable (const char *path); int check_executable (const char *path); char *strendzap (char *str, const char *pat); void lt_debugprintf (const char *file, int line, const char *fmt, ...); void lt_fatal (const char *file, int line, const char *message, ...); static const char *nonnull (const char *s); static const char *nonempty (const char *s); void lt_setenv (const char *name, const char *value); char *lt_extend_str (const char *orig_value, const char *add, int to_end); void lt_update_exe_path (const char *name, const char *value); void lt_update_lib_path (const char *name, const char *value); char **prepare_spawn (char **argv); void lt_dump_script (FILE *f); EOF cat <= 0) && (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return 1; else return 0; } int make_executable (const char *path) { int rval = 0; struct stat st; lt_debugprintf (__FILE__, __LINE__, "(make_executable): %s\n", nonempty (path)); if ((!path) || (!*path)) return 0; if (stat (path, &st) >= 0) { rval = chmod (path, st.st_mode | S_IXOTH | S_IXGRP | S_IXUSR); } return rval; } /* Searches for the full path of the wrapper. Returns newly allocated full path name if found, NULL otherwise Does not chase symlinks, even on platforms that support them. */ char * find_executable (const char *wrapper) { int has_slash = 0; const char *p; const char *p_next; /* static buffer for getcwd */ char tmp[LT_PATHMAX + 1]; size_t tmp_len; char *concat_name; lt_debugprintf (__FILE__, __LINE__, "(find_executable): %s\n", nonempty (wrapper)); if ((wrapper == NULL) || (*wrapper == '\0')) return NULL; /* Absolute path? */ #if defined HAVE_DOS_BASED_FILE_SYSTEM if (isalpha ((unsigned char) wrapper[0]) && wrapper[1] == ':') { concat_name = xstrdup (wrapper); if (check_executable (concat_name)) return concat_name; XFREE (concat_name); } else { #endif if (IS_DIR_SEPARATOR (wrapper[0])) { concat_name = xstrdup (wrapper); if (check_executable (concat_name)) return concat_name; XFREE (concat_name); } #if defined HAVE_DOS_BASED_FILE_SYSTEM } #endif for (p = wrapper; *p; p++) if (*p == '/') { has_slash = 1; break; } if (!has_slash) { /* no slashes; search PATH */ const char *path = getenv ("PATH"); if (path != NULL) { for (p = path; *p; p = p_next) { const char *q; size_t p_len; for (q = p; *q; q++) if (IS_PATH_SEPARATOR (*q)) break; p_len = (size_t) (q - p); p_next = (*q == '\0' ? q : q + 1); if (p_len == 0) { /* empty path: current directory */ if (getcwd (tmp, LT_PATHMAX) == NULL) lt_fatal (__FILE__, __LINE__, "getcwd failed: %s", nonnull (strerror (errno))); tmp_len = strlen (tmp); concat_name = XMALLOC (char, tmp_len + 1 + strlen (wrapper) + 1); memcpy (concat_name, tmp, tmp_len); concat_name[tmp_len] = '/'; strcpy (concat_name + tmp_len + 1, wrapper); } else { concat_name = XMALLOC (char, p_len + 1 + strlen (wrapper) + 1); memcpy (concat_name, p, p_len); concat_name[p_len] = '/'; strcpy (concat_name + p_len + 1, wrapper); } if (check_executable (concat_name)) return concat_name; XFREE (concat_name); } } /* not found in PATH; assume curdir */ } /* Relative path | not found in path: prepend cwd */ if (getcwd (tmp, LT_PATHMAX) == NULL) lt_fatal (__FILE__, __LINE__, "getcwd failed: %s", nonnull (strerror (errno))); tmp_len = strlen (tmp); concat_name = XMALLOC (char, tmp_len + 1 + strlen (wrapper) + 1); memcpy (concat_name, tmp, tmp_len); concat_name[tmp_len] = '/'; strcpy (concat_name + tmp_len + 1, wrapper); if (check_executable (concat_name)) return concat_name; XFREE (concat_name); return NULL; } char * chase_symlinks (const char *pathspec) { #ifndef S_ISLNK return xstrdup (pathspec); #else char buf[LT_PATHMAX]; struct stat s; char *tmp_pathspec = xstrdup (pathspec); char *p; int has_symlinks = 0; while (strlen (tmp_pathspec) && !has_symlinks) { lt_debugprintf (__FILE__, __LINE__, "checking path component for symlinks: %s\n", tmp_pathspec); if (lstat (tmp_pathspec, &s) == 0) { if (S_ISLNK (s.st_mode) != 0) { has_symlinks = 1; break; } /* search backwards for last DIR_SEPARATOR */ p = tmp_pathspec + strlen (tmp_pathspec) - 1; while ((p > tmp_pathspec) && (!IS_DIR_SEPARATOR (*p))) p--; if ((p == tmp_pathspec) && (!IS_DIR_SEPARATOR (*p))) { /* no more DIR_SEPARATORS left */ break; } *p = '\0'; } else { lt_fatal (__FILE__, __LINE__, "error accessing file \"%s\": %s", tmp_pathspec, nonnull (strerror (errno))); } } XFREE (tmp_pathspec); if (!has_symlinks) { return xstrdup (pathspec); } tmp_pathspec = realpath (pathspec, buf); if (tmp_pathspec == 0) { lt_fatal (__FILE__, __LINE__, "could not follow symlinks for %s", pathspec); } return xstrdup (tmp_pathspec); #endif } char * strendzap (char *str, const char *pat) { size_t len, patlen; assert (str != NULL); assert (pat != NULL); len = strlen (str); patlen = strlen (pat); if (patlen <= len) { str += len - patlen; if (STREQ (str, pat)) *str = '\0'; } return str; } void lt_debugprintf (const char *file, int line, const char *fmt, ...) { va_list args; if (lt_debug) { (void) fprintf (stderr, "%s:%s:%d: ", program_name, file, line); va_start (args, fmt); (void) vfprintf (stderr, fmt, args); va_end (args); } } static void lt_error_core (int exit_status, const char *file, int line, const char *mode, const char *message, va_list ap) { fprintf (stderr, "%s:%s:%d: %s: ", program_name, file, line, mode); vfprintf (stderr, message, ap); fprintf (stderr, ".\n"); if (exit_status >= 0) exit (exit_status); } void lt_fatal (const char *file, int line, const char *message, ...) { va_list ap; va_start (ap, message); lt_error_core (EXIT_FAILURE, file, line, "FATAL", message, ap); va_end (ap); } static const char * nonnull (const char *s) { return s ? s : "(null)"; } static const char * nonempty (const char *s) { return (s && !*s) ? "(empty)" : nonnull (s); } void lt_setenv (const char *name, const char *value) { lt_debugprintf (__FILE__, __LINE__, "(lt_setenv) setting '%s' to '%s'\n", nonnull (name), nonnull (value)); { #ifdef HAVE_SETENV /* always make a copy, for consistency with !HAVE_SETENV */ char *str = xstrdup (value); setenv (name, str, 1); #else size_t len = strlen (name) + 1 + strlen (value) + 1; char *str = XMALLOC (char, len); sprintf (str, "%s=%s", name, value); if (putenv (str) != EXIT_SUCCESS) { XFREE (str); } #endif } } char * lt_extend_str (const char *orig_value, const char *add, int to_end) { char *new_value; if (orig_value && *orig_value) { size_t orig_value_len = strlen (orig_value); size_t add_len = strlen (add); new_value = XMALLOC (char, add_len + orig_value_len + 1); if (to_end) { strcpy (new_value, orig_value); strcpy (new_value + orig_value_len, add); } else { strcpy (new_value, add); strcpy (new_value + add_len, orig_value); } } else { new_value = xstrdup (add); } return new_value; } void lt_update_exe_path (const char *name, const char *value) { lt_debugprintf (__FILE__, __LINE__, "(lt_update_exe_path) modifying '%s' by prepending '%s'\n", nonnull (name), nonnull (value)); if (name && *name && value && *value) { char *new_value = lt_extend_str (getenv (name), value, 0); /* some systems can't cope with a ':'-terminated path #' */ size_t len = strlen (new_value); while ((len > 0) && IS_PATH_SEPARATOR (new_value[len-1])) { new_value[--len] = '\0'; } lt_setenv (name, new_value); XFREE (new_value); } } void lt_update_lib_path (const char *name, const char *value) { lt_debugprintf (__FILE__, __LINE__, "(lt_update_lib_path) modifying '%s' by prepending '%s'\n", nonnull (name), nonnull (value)); if (name && *name && value && *value) { char *new_value = lt_extend_str (getenv (name), value, 0); lt_setenv (name, new_value); XFREE (new_value); } } EOF case $host_os in mingw*) cat <<"EOF" /* Prepares an argument vector before calling spawn(). Note that spawn() does not by itself call the command interpreter (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") : ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&v); v.dwPlatformId == VER_PLATFORM_WIN32_NT; }) ? "cmd.exe" : "command.com"). Instead it simply concatenates the arguments, separated by ' ', and calls CreateProcess(). We must quote the arguments since Win32 CreateProcess() interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a special way: - Space and tab are interpreted as delimiters. They are not treated as delimiters if they are surrounded by double quotes: "...". - Unescaped double quotes are removed from the input. Their only effect is that within double quotes, space and tab are treated like normal characters. - Backslashes not followed by double quotes are not special. - But 2*n+1 backslashes followed by a double quote become n backslashes followed by a double quote (n >= 0): \" -> " \\\" -> \" \\\\\" -> \\" */ #define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037" #define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037" char ** prepare_spawn (char **argv) { size_t argc; char **new_argv; size_t i; /* Count number of arguments. */ for (argc = 0; argv[argc] != NULL; argc++) ; /* Allocate new argument vector. */ new_argv = XMALLOC (char *, argc + 1); /* Put quoted arguments into the new argument vector. */ for (i = 0; i < argc; i++) { const char *string = argv[i]; if (string[0] == '\0') new_argv[i] = xstrdup ("\"\""); else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL) { int quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL); size_t length; unsigned int backslashes; const char *s; char *quoted_string; char *p; length = 0; backslashes = 0; if (quote_around) length++; for (s = string; *s != '\0'; s++) { char c = *s; if (c == '"') length += backslashes + 1; length++; if (c == '\\') backslashes++; else backslashes = 0; } if (quote_around) length += backslashes + 1; quoted_string = XMALLOC (char, length + 1); p = quoted_string; backslashes = 0; if (quote_around) *p++ = '"'; for (s = string; *s != '\0'; s++) { char c = *s; if (c == '"') { unsigned int j; for (j = backslashes + 1; j > 0; j--) *p++ = '\\'; } *p++ = c; if (c == '\\') backslashes++; else backslashes = 0; } if (quote_around) { unsigned int j; for (j = backslashes; j > 0; j--) *p++ = '\\'; *p++ = '"'; } *p = '\0'; new_argv[i] = quoted_string; } else new_argv[i] = (char *) string; } new_argv[argc] = NULL; return new_argv; } EOF ;; esac cat <<"EOF" void lt_dump_script (FILE* f) { EOF func_emit_wrapper yes | $SED -n -e ' s/^\(.\{79\}\)\(..*\)/\1\ \2/ h s/\([\\"]\)/\\\1/g s/$/\\n/ s/\([^\n]*\).*/ fputs ("\1", f);/p g D' cat <<"EOF" } EOF } # end: func_emit_cwrapperexe_src # func_win32_import_lib_p ARG # True if ARG is an import lib, as indicated by $file_magic_cmd func_win32_import_lib_p () { $debug_cmd case `eval $file_magic_cmd \"\$1\" 2>/dev/null | $SED -e 10q` in *import*) : ;; *) false ;; esac } # func_suncc_cstd_abi # !!ONLY CALL THIS FOR SUN CC AFTER $compile_command IS FULLY EXPANDED!! # Several compiler flags select an ABI that is incompatible with the # Cstd library. Avoid specifying it if any are in CXXFLAGS. func_suncc_cstd_abi () { $debug_cmd case " $compile_command " in *" -compat=g "*|*\ -std=c++[0-9][0-9]\ *|*" -library=stdcxx4 "*|*" -library=stlport4 "*) suncc_use_cstd_abi=no ;; *) suncc_use_cstd_abi=yes ;; esac } # func_mode_link arg... func_mode_link () { $debug_cmd case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-cegcc*) # It is impossible to link a dll without this setting, and # we shouldn't force the makefile maintainer to figure out # what system we are compiling for in order to pass an extra # flag for every libtool invocation. # allow_undefined=no # FIXME: Unfortunately, there are problems with the above when trying # to make a dll that has undefined symbols, in which case not # even a static library is built. For now, we need to specify # -no-undefined on the libtool link line when we can be certain # that all symbols are satisfied, otherwise we get a static library. allow_undefined=yes ;; *) allow_undefined=yes ;; esac libtool_args=$nonopt base_compile="$nonopt $@" compile_command=$nonopt finalize_command=$nonopt compile_rpath= finalize_rpath= compile_shlibpath= finalize_shlibpath= convenience= old_convenience= deplibs= old_deplibs= compiler_flags= linker_flags= dllsearchpath= lib_search_path=`pwd` inst_prefix_dir= new_inherited_linker_flags= avoid_version=no bindir= dlfiles= dlprefiles= dlself=no export_dynamic=no export_symbols= export_symbols_regex= generated= libobjs= ltlibs= module=no no_install=no objs= os2dllname= non_pic_objects= precious_files_regex= prefer_static_libs=no preload=false prev= prevarg= release= rpath= xrpath= perm_rpath= temp_rpath= thread_safe=no vinfo= vinfo_number=no weak_libs= single_module=$wl-single_module func_infer_tag $base_compile # We need to know -static, to get the right output filenames. for arg do case $arg in -shared) test yes != "$build_libtool_libs" \ && func_fatal_configuration "cannot build a shared library" build_old_libs=no break ;; -all-static | -static | -static-libtool-libs) case $arg in -all-static) if test yes = "$build_libtool_libs" && test -z "$link_static_flag"; then func_warning "complete static linking is impossible in this configuration" fi if test -n "$link_static_flag"; then dlopen_self=$dlopen_self_static fi prefer_static_libs=yes ;; -static) if test -z "$pic_flag" && test -n "$link_static_flag"; then dlopen_self=$dlopen_self_static fi prefer_static_libs=built ;; -static-libtool-libs) if test -z "$pic_flag" && test -n "$link_static_flag"; then dlopen_self=$dlopen_self_static fi prefer_static_libs=yes ;; esac build_libtool_libs=no build_old_libs=yes break ;; esac done # See if our shared archives depend on static archives. test -n "$old_archive_from_new_cmds" && build_old_libs=yes # Go through the arguments, transforming them on the way. while test "$#" -gt 0; do arg=$1 shift func_quote_for_eval "$arg" qarg=$func_quote_for_eval_unquoted_result func_append libtool_args " $func_quote_for_eval_result" # If the previous option needs an argument, assign it. if test -n "$prev"; then case $prev in output) func_append compile_command " @OUTPUT@" func_append finalize_command " @OUTPUT@" ;; esac case $prev in bindir) bindir=$arg prev= continue ;; dlfiles|dlprefiles) $preload || { # Add the symbol object into the linking commands. func_append compile_command " @SYMFILE@" func_append finalize_command " @SYMFILE@" preload=: } case $arg in *.la | *.lo) ;; # We handle these cases below. force) if test no = "$dlself"; then dlself=needless export_dynamic=yes fi prev= continue ;; self) if test dlprefiles = "$prev"; then dlself=yes elif test dlfiles = "$prev" && test yes != "$dlopen_self"; then dlself=yes else dlself=needless export_dynamic=yes fi prev= continue ;; *) if test dlfiles = "$prev"; then func_append dlfiles " $arg" else func_append dlprefiles " $arg" fi prev= continue ;; esac ;; expsyms) export_symbols=$arg test -f "$arg" \ || func_fatal_error "symbol file '$arg' does not exist" prev= continue ;; expsyms_regex) export_symbols_regex=$arg prev= continue ;; framework) case $host in *-*-darwin*) case "$deplibs " in *" $qarg.ltframework "*) ;; *) func_append deplibs " $qarg.ltframework" # this is fixed later ;; esac ;; esac prev= continue ;; inst_prefix) inst_prefix_dir=$arg prev= continue ;; mllvm) # Clang does not use LLVM to link, so we can simply discard any # '-mllvm $arg' options when doing the link step. prev= continue ;; objectlist) if test -f "$arg"; then save_arg=$arg moreargs= for fil in `cat "$save_arg"` do # func_append moreargs " $fil" arg=$fil # A libtool-controlled object. # Check to see that this really is a libtool object. if func_lalib_unsafe_p "$arg"; then pic_object= non_pic_object= # Read the .lo file func_source "$arg" if test -z "$pic_object" || test -z "$non_pic_object" || test none = "$pic_object" && test none = "$non_pic_object"; then func_fatal_error "cannot find name of object for '$arg'" fi # Extract subdirectory from the argument. func_dirname "$arg" "/" "" xdir=$func_dirname_result if test none != "$pic_object"; then # Prepend the subdirectory the object is found in. pic_object=$xdir$pic_object if test dlfiles = "$prev"; then if test yes = "$build_libtool_libs" && test yes = "$dlopen_support"; then func_append dlfiles " $pic_object" prev= continue else # If libtool objects are unsupported, then we need to preload. prev=dlprefiles fi fi # CHECK ME: I think I busted this. -Ossama if test dlprefiles = "$prev"; then # Preload the old-style object. func_append dlprefiles " $pic_object" prev= fi # A PIC object. func_append libobjs " $pic_object" arg=$pic_object fi # Non-PIC object. if test none != "$non_pic_object"; then # Prepend the subdirectory the object is found in. non_pic_object=$xdir$non_pic_object # A standard non-PIC object func_append non_pic_objects " $non_pic_object" if test -z "$pic_object" || test none = "$pic_object"; then arg=$non_pic_object fi else # If the PIC object exists, use it instead. # $xdir was prepended to $pic_object above. non_pic_object=$pic_object func_append non_pic_objects " $non_pic_object" fi else # Only an error if not doing a dry-run. if $opt_dry_run; then # Extract subdirectory from the argument. func_dirname "$arg" "/" "" xdir=$func_dirname_result func_lo2o "$arg" pic_object=$xdir$objdir/$func_lo2o_result non_pic_object=$xdir$func_lo2o_result func_append libobjs " $pic_object" func_append non_pic_objects " $non_pic_object" else func_fatal_error "'$arg' is not a valid libtool object" fi fi done else func_fatal_error "link input file '$arg' does not exist" fi arg=$save_arg prev= continue ;; os2dllname) os2dllname=$arg prev= continue ;; precious_regex) precious_files_regex=$arg prev= continue ;; release) release=-$arg prev= continue ;; rpath | xrpath) # We need an absolute path. case $arg in [\\/]* | [A-Za-z]:[\\/]*) ;; *) func_fatal_error "only absolute run-paths are allowed" ;; esac if test rpath = "$prev"; then case "$rpath " in *" $arg "*) ;; *) func_append rpath " $arg" ;; esac else case "$xrpath " in *" $arg "*) ;; *) func_append xrpath " $arg" ;; esac fi prev= continue ;; shrext) shrext_cmds=$arg prev= continue ;; weak) func_append weak_libs " $arg" prev= continue ;; xcclinker) func_append linker_flags " $qarg" func_append compiler_flags " $qarg" prev= func_append compile_command " $qarg" func_append finalize_command " $qarg" continue ;; xcompiler) func_append compiler_flags " $qarg" prev= func_append compile_command " $qarg" func_append finalize_command " $qarg" continue ;; xlinker) func_append linker_flags " $qarg" func_append compiler_flags " $wl$qarg" prev= func_append compile_command " $wl$qarg" func_append finalize_command " $wl$qarg" continue ;; *) eval "$prev=\"\$arg\"" prev= continue ;; esac fi # test -n "$prev" prevarg=$arg case $arg in -all-static) if test -n "$link_static_flag"; then # See comment for -static flag below, for more details. func_append compile_command " $link_static_flag" func_append finalize_command " $link_static_flag" fi continue ;; -allow-undefined) # FIXME: remove this flag sometime in the future. func_fatal_error "'-allow-undefined' must not be used because it is the default" ;; -avoid-version) avoid_version=yes continue ;; -bindir) prev=bindir continue ;; -dlopen) prev=dlfiles continue ;; -dlpreopen) prev=dlprefiles continue ;; -export-dynamic) export_dynamic=yes continue ;; -export-symbols | -export-symbols-regex) if test -n "$export_symbols" || test -n "$export_symbols_regex"; then func_fatal_error "more than one -exported-symbols argument is not allowed" fi if test X-export-symbols = "X$arg"; then prev=expsyms else prev=expsyms_regex fi continue ;; -framework) prev=framework continue ;; -inst-prefix-dir) prev=inst_prefix continue ;; # The native IRIX linker understands -LANG:*, -LIST:* and -LNO:* # so, if we see these flags be careful not to treat them like -L -L[A-Z][A-Z]*:*) case $with_gcc/$host in no/*-*-irix* | /*-*-irix*) func_append compile_command " $arg" func_append finalize_command " $arg" ;; esac continue ;; -L*) func_stripname "-L" '' "$arg" if test -z "$func_stripname_result"; then if test "$#" -gt 0; then func_fatal_error "require no space between '-L' and '$1'" else func_fatal_error "need path for '-L' option" fi fi func_resolve_sysroot "$func_stripname_result" dir=$func_resolve_sysroot_result # We need an absolute path. case $dir in [\\/]* | [A-Za-z]:[\\/]*) ;; *) absdir=`cd "$dir" && pwd` test -z "$absdir" && \ func_fatal_error "cannot determine absolute directory name of '$dir'" dir=$absdir ;; esac case "$deplibs " in *" -L$dir "* | *" $arg "*) # Will only happen for absolute or sysroot arguments ;; *) # Preserve sysroot, but never include relative directories case $dir in [\\/]* | [A-Za-z]:[\\/]* | =*) func_append deplibs " $arg" ;; *) func_append deplibs " -L$dir" ;; esac func_append lib_search_path " $dir" ;; esac case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-cegcc*) testbindir=`$ECHO "$dir" | $SED 's*/lib$*/bin*'` case :$dllsearchpath: in *":$dir:"*) ;; ::) dllsearchpath=$dir;; *) func_append dllsearchpath ":$dir";; esac case :$dllsearchpath: in *":$testbindir:"*) ;; ::) dllsearchpath=$testbindir;; *) func_append dllsearchpath ":$testbindir";; esac ;; esac continue ;; -l*) if test X-lc = "X$arg" || test X-lm = "X$arg"; then case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-beos* | *-cegcc* | *-*-haiku*) # These systems don't actually have a C or math library (as such) continue ;; *-*-os2*) # These systems don't actually have a C library (as such) test X-lc = "X$arg" && continue ;; *-*-openbsd* | *-*-freebsd* | *-*-dragonfly* | *-*-bitrig*) # Do not include libc due to us having libc/libc_r. test X-lc = "X$arg" && continue ;; *-*-rhapsody* | *-*-darwin1.[012]) # Rhapsody C and math libraries are in the System framework func_append deplibs " System.ltframework" continue ;; *-*-sco3.2v5* | *-*-sco5v6*) # Causes problems with __ctype test X-lc = "X$arg" && continue ;; *-*-sysv4.2uw2* | *-*-sysv5* | *-*-unixware* | *-*-OpenUNIX*) # Compiler inserts libc in the correct place for threads to work test X-lc = "X$arg" && continue ;; esac elif test X-lc_r = "X$arg"; then case $host in *-*-openbsd* | *-*-freebsd* | *-*-dragonfly* | *-*-bitrig*) # Do not include libc_r directly, use -pthread flag. continue ;; esac fi func_append deplibs " $arg" continue ;; -mllvm) prev=mllvm continue ;; -module) module=yes continue ;; # Tru64 UNIX uses -model [arg] to determine the layout of C++ # classes, name mangling, and exception handling. # Darwin uses the -arch flag to determine output architecture. -model|-arch|-isysroot|--sysroot) func_append compiler_flags " $arg" func_append compile_command " $arg" func_append finalize_command " $arg" prev=xcompiler continue ;; -mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe \ |-threads|-fopenmp|-openmp|-mp|-xopenmp|-omp|-qsmp=*) func_append compiler_flags " $arg" func_append compile_command " $arg" func_append finalize_command " $arg" case "$new_inherited_linker_flags " in *" $arg "*) ;; * ) func_append new_inherited_linker_flags " $arg" ;; esac continue ;; -multi_module) single_module=$wl-multi_module continue ;; -no-fast-install) fast_install=no continue ;; -no-install) case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-*-darwin* | *-cegcc*) # The PATH hackery in wrapper scripts is required on Windows # and Darwin in order for the loader to find any dlls it needs. func_warning "'-no-install' is ignored for $host" func_warning "assuming '-no-fast-install' instead" fast_install=no ;; *) no_install=yes ;; esac continue ;; -no-undefined) allow_undefined=no continue ;; -objectlist) prev=objectlist continue ;; -os2dllname) prev=os2dllname continue ;; -o) prev=output ;; -precious-files-regex) prev=precious_regex continue ;; -release) prev=release continue ;; -rpath) prev=rpath continue ;; -R) prev=xrpath continue ;; -R*) func_stripname '-R' '' "$arg" dir=$func_stripname_result # We need an absolute path. case $dir in [\\/]* | [A-Za-z]:[\\/]*) ;; =*) func_stripname '=' '' "$dir" dir=$lt_sysroot$func_stripname_result ;; *) func_fatal_error "only absolute run-paths are allowed" ;; esac case "$xrpath " in *" $dir "*) ;; *) func_append xrpath " $dir" ;; esac continue ;; -shared) # The effects of -shared are defined in a previous loop. continue ;; -shrext) prev=shrext continue ;; -static | -static-libtool-libs) # The effects of -static are defined in a previous loop. # We used to do the same as -all-static on platforms that # didn't have a PIC flag, but the assumption that the effects # would be equivalent was wrong. It would break on at least # Digital Unix and AIX. continue ;; -thread-safe) thread_safe=yes continue ;; -version-info) prev=vinfo continue ;; -version-number) prev=vinfo vinfo_number=yes continue ;; -weak) prev=weak continue ;; -Wc,*) func_stripname '-Wc,' '' "$arg" args=$func_stripname_result arg= save_ifs=$IFS; IFS=, for flag in $args; do IFS=$save_ifs func_quote_for_eval "$flag" func_append arg " $func_quote_for_eval_result" func_append compiler_flags " $func_quote_for_eval_result" done IFS=$save_ifs func_stripname ' ' '' "$arg" arg=$func_stripname_result ;; -Wl,*) func_stripname '-Wl,' '' "$arg" args=$func_stripname_result arg= save_ifs=$IFS; IFS=, for flag in $args; do IFS=$save_ifs func_quote_for_eval "$flag" func_append arg " $wl$func_quote_for_eval_result" func_append compiler_flags " $wl$func_quote_for_eval_result" func_append linker_flags " $func_quote_for_eval_result" done IFS=$save_ifs func_stripname ' ' '' "$arg" arg=$func_stripname_result ;; -Xcompiler) prev=xcompiler continue ;; -Xlinker) prev=xlinker continue ;; -XCClinker) prev=xcclinker continue ;; # -msg_* for osf cc -msg_*) func_quote_for_eval "$arg" arg=$func_quote_for_eval_result ;; # Flags to be passed through unchanged, with rationale: # -64, -mips[0-9] enable 64-bit mode for the SGI compiler # -r[0-9][0-9]* specify processor for the SGI compiler # -xarch=*, -xtarget=* enable 64-bit mode for the Sun compiler # +DA*, +DD* enable 64-bit mode for the HP compiler # -q* compiler args for the IBM compiler # -m*, -t[45]*, -txscale* architecture-specific flags for GCC # -F/path path to uninstalled frameworks, gcc on darwin # -p, -pg, --coverage, -fprofile-* profiling flags for GCC # -fstack-protector* stack protector flags for GCC # @file GCC response files # -tp=* Portland pgcc target processor selection # --sysroot=* for sysroot support # -O*, -g*, -flto*, -fwhopr*, -fuse-linker-plugin GCC link-time optimization # -specs=* GCC specs files # -stdlib=* select c++ std lib with clang # -fsanitize=* Clang/GCC memory and address sanitizer # -fuse-ld=* Linker select flags for GCC -64|-mips[0-9]|-r[0-9][0-9]*|-xarch=*|-xtarget=*|+DA*|+DD*|-q*|-m*| \ -t[45]*|-txscale*|-p|-pg|--coverage|-fprofile-*|-F*|@*|-tp=*|--sysroot=*| \ -O*|-g*|-flto*|-fwhopr*|-fuse-linker-plugin|-fstack-protector*|-stdlib=*| \ -specs=*|-fsanitize=*|-fuse-ld=*) func_quote_for_eval "$arg" arg=$func_quote_for_eval_result func_append compile_command " $arg" func_append finalize_command " $arg" func_append compiler_flags " $arg" continue ;; -Z*) if test os2 = "`expr $host : '.*\(os2\)'`"; then # OS/2 uses -Zxxx to specify OS/2-specific options compiler_flags="$compiler_flags $arg" func_append compile_command " $arg" func_append finalize_command " $arg" case $arg in -Zlinker | -Zstack) prev=xcompiler ;; esac continue else # Otherwise treat like 'Some other compiler flag' below func_quote_for_eval "$arg" arg=$func_quote_for_eval_result fi ;; # Some other compiler flag. -* | +*) func_quote_for_eval "$arg" arg=$func_quote_for_eval_result ;; *.$objext) # A standard object. func_append objs " $arg" ;; *.lo) # A libtool-controlled object. # Check to see that this really is a libtool object. if func_lalib_unsafe_p "$arg"; then pic_object= non_pic_object= # Read the .lo file func_source "$arg" if test -z "$pic_object" || test -z "$non_pic_object" || test none = "$pic_object" && test none = "$non_pic_object"; then func_fatal_error "cannot find name of object for '$arg'" fi # Extract subdirectory from the argument. func_dirname "$arg" "/" "" xdir=$func_dirname_result test none = "$pic_object" || { # Prepend the subdirectory the object is found in. pic_object=$xdir$pic_object if test dlfiles = "$prev"; then if test yes = "$build_libtool_libs" && test yes = "$dlopen_support"; then func_append dlfiles " $pic_object" prev= continue else # If libtool objects are unsupported, then we need to preload. prev=dlprefiles fi fi # CHECK ME: I think I busted this. -Ossama if test dlprefiles = "$prev"; then # Preload the old-style object. func_append dlprefiles " $pic_object" prev= fi # A PIC object. func_append libobjs " $pic_object" arg=$pic_object } # Non-PIC object. if test none != "$non_pic_object"; then # Prepend the subdirectory the object is found in. non_pic_object=$xdir$non_pic_object # A standard non-PIC object func_append non_pic_objects " $non_pic_object" if test -z "$pic_object" || test none = "$pic_object"; then arg=$non_pic_object fi else # If the PIC object exists, use it instead. # $xdir was prepended to $pic_object above. non_pic_object=$pic_object func_append non_pic_objects " $non_pic_object" fi else # Only an error if not doing a dry-run. if $opt_dry_run; then # Extract subdirectory from the argument. func_dirname "$arg" "/" "" xdir=$func_dirname_result func_lo2o "$arg" pic_object=$xdir$objdir/$func_lo2o_result non_pic_object=$xdir$func_lo2o_result func_append libobjs " $pic_object" func_append non_pic_objects " $non_pic_object" else func_fatal_error "'$arg' is not a valid libtool object" fi fi ;; *.$libext) # An archive. func_append deplibs " $arg" func_append old_deplibs " $arg" continue ;; *.la) # A libtool-controlled library. func_resolve_sysroot "$arg" if test dlfiles = "$prev"; then # This library was specified with -dlopen. func_append dlfiles " $func_resolve_sysroot_result" prev= elif test dlprefiles = "$prev"; then # The library was specified with -dlpreopen. func_append dlprefiles " $func_resolve_sysroot_result" prev= else func_append deplibs " $func_resolve_sysroot_result" fi continue ;; # Some other compiler argument. *) # Unknown arguments in both finalize_command and compile_command need # to be aesthetically quoted because they are evaled later. func_quote_for_eval "$arg" arg=$func_quote_for_eval_result ;; esac # arg # Now actually substitute the argument into the commands. if test -n "$arg"; then func_append compile_command " $arg" func_append finalize_command " $arg" fi done # argument parsing loop test -n "$prev" && \ func_fatal_help "the '$prevarg' option requires an argument" if test yes = "$export_dynamic" && test -n "$export_dynamic_flag_spec"; then eval arg=\"$export_dynamic_flag_spec\" func_append compile_command " $arg" func_append finalize_command " $arg" fi oldlibs= # calculate the name of the file, without its directory func_basename "$output" outputname=$func_basename_result libobjs_save=$libobjs if test -n "$shlibpath_var"; then # get the directories listed in $shlibpath_var eval shlib_search_path=\`\$ECHO \"\$$shlibpath_var\" \| \$SED \'s/:/ /g\'\` else shlib_search_path= fi eval sys_lib_search_path=\"$sys_lib_search_path_spec\" eval sys_lib_dlsearch_path=\"$sys_lib_dlsearch_path_spec\" # Definition is injected by LT_CONFIG during libtool generation. func_munge_path_list sys_lib_dlsearch_path "$LT_SYS_LIBRARY_PATH" func_dirname "$output" "/" "" output_objdir=$func_dirname_result$objdir func_to_tool_file "$output_objdir/" tool_output_objdir=$func_to_tool_file_result # Create the object directory. func_mkdir_p "$output_objdir" # Determine the type of output case $output in "") func_fatal_help "you must specify an output file" ;; *.$libext) linkmode=oldlib ;; *.lo | *.$objext) linkmode=obj ;; *.la) linkmode=lib ;; *) linkmode=prog ;; # Anything else should be a program. esac specialdeplibs= libs= # Find all interdependent deplibs by searching for libraries # that are linked more than once (e.g. -la -lb -la) for deplib in $deplibs; do if $opt_preserve_dup_deps; then case "$libs " in *" $deplib "*) func_append specialdeplibs " $deplib" ;; esac fi func_append libs " $deplib" done if test lib = "$linkmode"; then libs="$predeps $libs $compiler_lib_search_path $postdeps" # Compute libraries that are listed more than once in $predeps # $postdeps and mark them as special (i.e., whose duplicates are # not to be eliminated). pre_post_deps= if $opt_duplicate_compiler_generated_deps; then for pre_post_dep in $predeps $postdeps; do case "$pre_post_deps " in *" $pre_post_dep "*) func_append specialdeplibs " $pre_post_deps" ;; esac func_append pre_post_deps " $pre_post_dep" done fi pre_post_deps= fi deplibs= newdependency_libs= newlib_search_path= need_relink=no # whether we're linking any uninstalled libtool libraries notinst_deplibs= # not-installed libtool libraries notinst_path= # paths that contain not-installed libtool libraries case $linkmode in lib) passes="conv dlpreopen link" for file in $dlfiles $dlprefiles; do case $file in *.la) ;; *) func_fatal_help "libraries can '-dlopen' only libtool libraries: $file" ;; esac done ;; prog) compile_deplibs= finalize_deplibs= alldeplibs=false newdlfiles= newdlprefiles= passes="conv scan dlopen dlpreopen link" ;; *) passes="conv" ;; esac for pass in $passes; do # The preopen pass in lib mode reverses $deplibs; put it back here # so that -L comes before libs that need it for instance... if test lib,link = "$linkmode,$pass"; then ## FIXME: Find the place where the list is rebuilt in the wrong ## order, and fix it there properly tmp_deplibs= for deplib in $deplibs; do tmp_deplibs="$deplib $tmp_deplibs" done deplibs=$tmp_deplibs fi if test lib,link = "$linkmode,$pass" || test prog,scan = "$linkmode,$pass"; then libs=$deplibs deplibs= fi if test prog = "$linkmode"; then case $pass in dlopen) libs=$dlfiles ;; dlpreopen) libs=$dlprefiles ;; link) libs="$deplibs %DEPLIBS%" test "X$link_all_deplibs" != Xno && libs="$libs $dependency_libs" ;; esac fi if test lib,dlpreopen = "$linkmode,$pass"; then # Collect and forward deplibs of preopened libtool libs for lib in $dlprefiles; do # Ignore non-libtool-libs dependency_libs= func_resolve_sysroot "$lib" case $lib in *.la) func_source "$func_resolve_sysroot_result" ;; esac # Collect preopened libtool deplibs, except any this library # has declared as weak libs for deplib in $dependency_libs; do func_basename "$deplib" deplib_base=$func_basename_result case " $weak_libs " in *" $deplib_base "*) ;; *) func_append deplibs " $deplib" ;; esac done done libs=$dlprefiles fi if test dlopen = "$pass"; then # Collect dlpreopened libraries save_deplibs=$deplibs deplibs= fi for deplib in $libs; do lib= found=false case $deplib in -mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe \ |-threads|-fopenmp|-openmp|-mp|-xopenmp|-omp|-qsmp=*) if test prog,link = "$linkmode,$pass"; then compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" else func_append compiler_flags " $deplib" if test lib = "$linkmode"; then case "$new_inherited_linker_flags " in *" $deplib "*) ;; * ) func_append new_inherited_linker_flags " $deplib" ;; esac fi fi continue ;; -l*) if test lib != "$linkmode" && test prog != "$linkmode"; then func_warning "'-l' is ignored for archives/objects" continue fi func_stripname '-l' '' "$deplib" name=$func_stripname_result if test lib = "$linkmode"; then searchdirs="$newlib_search_path $lib_search_path $compiler_lib_search_dirs $sys_lib_search_path $shlib_search_path" else searchdirs="$newlib_search_path $lib_search_path $sys_lib_search_path $shlib_search_path" fi for searchdir in $searchdirs; do for search_ext in .la $std_shrext .so .a; do # Search the libtool library lib=$searchdir/lib$name$search_ext if test -f "$lib"; then if test .la = "$search_ext"; then found=: else found=false fi break 2 fi done done if $found; then # deplib is a libtool library # If $allow_libtool_libs_with_static_runtimes && $deplib is a stdlib, # We need to do some special things here, and not later. if test yes = "$allow_libtool_libs_with_static_runtimes"; then case " $predeps $postdeps " in *" $deplib "*) if func_lalib_p "$lib"; then library_names= old_library= func_source "$lib" for l in $old_library $library_names; do ll=$l done if test "X$ll" = "X$old_library"; then # only static version available found=false func_dirname "$lib" "" "." ladir=$func_dirname_result lib=$ladir/$old_library if test prog,link = "$linkmode,$pass"; then compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" else deplibs="$deplib $deplibs" test lib = "$linkmode" && newdependency_libs="$deplib $newdependency_libs" fi continue fi fi ;; *) ;; esac fi else # deplib doesn't seem to be a libtool library if test prog,link = "$linkmode,$pass"; then compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" else deplibs="$deplib $deplibs" test lib = "$linkmode" && newdependency_libs="$deplib $newdependency_libs" fi continue fi ;; # -l *.ltframework) if test prog,link = "$linkmode,$pass"; then compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" else deplibs="$deplib $deplibs" if test lib = "$linkmode"; then case "$new_inherited_linker_flags " in *" $deplib "*) ;; * ) func_append new_inherited_linker_flags " $deplib" ;; esac fi fi continue ;; -L*) case $linkmode in lib) deplibs="$deplib $deplibs" test conv = "$pass" && continue newdependency_libs="$deplib $newdependency_libs" func_stripname '-L' '' "$deplib" func_resolve_sysroot "$func_stripname_result" func_append newlib_search_path " $func_resolve_sysroot_result" ;; prog) if test conv = "$pass"; then deplibs="$deplib $deplibs" continue fi if test scan = "$pass"; then deplibs="$deplib $deplibs" else compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" fi func_stripname '-L' '' "$deplib" func_resolve_sysroot "$func_stripname_result" func_append newlib_search_path " $func_resolve_sysroot_result" ;; *) func_warning "'-L' is ignored for archives/objects" ;; esac # linkmode continue ;; # -L -R*) if test link = "$pass"; then func_stripname '-R' '' "$deplib" func_resolve_sysroot "$func_stripname_result" dir=$func_resolve_sysroot_result # Make sure the xrpath contains only unique directories. case "$xrpath " in *" $dir "*) ;; *) func_append xrpath " $dir" ;; esac fi deplibs="$deplib $deplibs" continue ;; *.la) func_resolve_sysroot "$deplib" lib=$func_resolve_sysroot_result ;; *.$libext) if test conv = "$pass"; then deplibs="$deplib $deplibs" continue fi case $linkmode in lib) # Linking convenience modules into shared libraries is allowed, # but linking other static libraries is non-portable. case " $dlpreconveniencelibs " in *" $deplib "*) ;; *) valid_a_lib=false case $deplibs_check_method in match_pattern*) set dummy $deplibs_check_method; shift match_pattern_regex=`expr "$deplibs_check_method" : "$1 \(.*\)"` if eval "\$ECHO \"$deplib\"" 2>/dev/null | $SED 10q \ | $EGREP "$match_pattern_regex" > /dev/null; then valid_a_lib=: fi ;; pass_all) valid_a_lib=: ;; esac if $valid_a_lib; then echo $ECHO "*** Warning: Linking the shared library $output against the" $ECHO "*** static library $deplib is not portable!" deplibs="$deplib $deplibs" else echo $ECHO "*** Warning: Trying to link with static lib archive $deplib." echo "*** I have the capability to make that library automatically link in when" echo "*** you link to this library. But I can only do this if you have a" echo "*** shared version of the library, which you do not appear to have" echo "*** because the file extensions .$libext of this argument makes me believe" echo "*** that it is just a static archive that I should not use here." fi ;; esac continue ;; prog) if test link != "$pass"; then deplibs="$deplib $deplibs" else compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" fi continue ;; esac # linkmode ;; # *.$libext *.lo | *.$objext) if test conv = "$pass"; then deplibs="$deplib $deplibs" elif test prog = "$linkmode"; then if test dlpreopen = "$pass" || test yes != "$dlopen_support" || test no = "$build_libtool_libs"; then # If there is no dlopen support or we're linking statically, # we need to preload. func_append newdlprefiles " $deplib" compile_deplibs="$deplib $compile_deplibs" finalize_deplibs="$deplib $finalize_deplibs" else func_append newdlfiles " $deplib" fi fi continue ;; %DEPLIBS%) alldeplibs=: continue ;; esac # case $deplib $found || test -f "$lib" \ || func_fatal_error "cannot find the library '$lib' or unhandled argument '$deplib'" # Check to see that this really is a libtool archive. func_lalib_unsafe_p "$lib" \ || func_fatal_error "'$lib' is not a valid libtool archive" func_dirname "$lib" "" "." ladir=$func_dirname_result dlname= dlopen= dlpreopen= libdir= library_names= old_library= inherited_linker_flags= # If the library was installed with an old release of libtool, # it will not redefine variables installed, or shouldnotlink installed=yes shouldnotlink=no avoidtemprpath= # Read the .la file func_source "$lib" # Convert "-framework foo" to "foo.ltframework" if test -n "$inherited_linker_flags"; then tmp_inherited_linker_flags=`$ECHO "$inherited_linker_flags" | $SED 's/-framework \([^ $]*\)/\1.ltframework/g'` for tmp_inherited_linker_flag in $tmp_inherited_linker_flags; do case " $new_inherited_linker_flags " in *" $tmp_inherited_linker_flag "*) ;; *) func_append new_inherited_linker_flags " $tmp_inherited_linker_flag";; esac done fi dependency_libs=`$ECHO " $dependency_libs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` if test lib,link = "$linkmode,$pass" || test prog,scan = "$linkmode,$pass" || { test prog != "$linkmode" && test lib != "$linkmode"; }; then test -n "$dlopen" && func_append dlfiles " $dlopen" test -n "$dlpreopen" && func_append dlprefiles " $dlpreopen" fi if test conv = "$pass"; then # Only check for convenience libraries deplibs="$lib $deplibs" if test -z "$libdir"; then if test -z "$old_library"; then func_fatal_error "cannot find name of link library for '$lib'" fi # It is a libtool convenience library, so add in its objects. func_append convenience " $ladir/$objdir/$old_library" func_append old_convenience " $ladir/$objdir/$old_library" tmp_libs= for deplib in $dependency_libs; do deplibs="$deplib $deplibs" if $opt_preserve_dup_deps; then case "$tmp_libs " in *" $deplib "*) func_append specialdeplibs " $deplib" ;; esac fi func_append tmp_libs " $deplib" done elif test prog != "$linkmode" && test lib != "$linkmode"; then func_fatal_error "'$lib' is not a convenience library" fi continue fi # $pass = conv # Get the name of the library we link against. linklib= if test -n "$old_library" && { test yes = "$prefer_static_libs" || test built,no = "$prefer_static_libs,$installed"; }; then linklib=$old_library else for l in $old_library $library_names; do linklib=$l done fi if test -z "$linklib"; then func_fatal_error "cannot find name of link library for '$lib'" fi # This library was specified with -dlopen. if test dlopen = "$pass"; then test -z "$libdir" \ && func_fatal_error "cannot -dlopen a convenience library: '$lib'" if test -z "$dlname" || test yes != "$dlopen_support" || test no = "$build_libtool_libs" then # If there is no dlname, no dlopen support or we're linking # statically, we need to preload. We also need to preload any # dependent libraries so libltdl's deplib preloader doesn't # bomb out in the load deplibs phase. func_append dlprefiles " $lib $dependency_libs" else func_append newdlfiles " $lib" fi continue fi # $pass = dlopen # We need an absolute path. case $ladir in [\\/]* | [A-Za-z]:[\\/]*) abs_ladir=$ladir ;; *) abs_ladir=`cd "$ladir" && pwd` if test -z "$abs_ladir"; then func_warning "cannot determine absolute directory name of '$ladir'" func_warning "passing it literally to the linker, although it might fail" abs_ladir=$ladir fi ;; esac func_basename "$lib" laname=$func_basename_result # Find the relevant object directory and library name. if test yes = "$installed"; then if test ! -f "$lt_sysroot$libdir/$linklib" && test -f "$abs_ladir/$linklib"; then func_warning "library '$lib' was moved." dir=$ladir absdir=$abs_ladir libdir=$abs_ladir else dir=$lt_sysroot$libdir absdir=$lt_sysroot$libdir fi test yes = "$hardcode_automatic" && avoidtemprpath=yes else if test ! -f "$ladir/$objdir/$linklib" && test -f "$abs_ladir/$linklib"; then dir=$ladir absdir=$abs_ladir # Remove this search path later func_append notinst_path " $abs_ladir" else dir=$ladir/$objdir absdir=$abs_ladir/$objdir # Remove this search path later func_append notinst_path " $abs_ladir" fi fi # $installed = yes func_stripname 'lib' '.la' "$laname" name=$func_stripname_result # This library was specified with -dlpreopen. if test dlpreopen = "$pass"; then if test -z "$libdir" && test prog = "$linkmode"; then func_fatal_error "only libraries may -dlpreopen a convenience library: '$lib'" fi case $host in # special handling for platforms with PE-DLLs. *cygwin* | *mingw* | *cegcc* ) # Linker will automatically link against shared library if both # static and shared are present. Therefore, ensure we extract # symbols from the import library if a shared library is present # (otherwise, the dlopen module name will be incorrect). We do # this by putting the import library name into $newdlprefiles. # We recover the dlopen module name by 'saving' the la file # name in a special purpose variable, and (later) extracting the # dlname from the la file. if test -n "$dlname"; then func_tr_sh "$dir/$linklib" eval "libfile_$func_tr_sh_result=\$abs_ladir/\$laname" func_append newdlprefiles " $dir/$linklib" else func_append newdlprefiles " $dir/$old_library" # Keep a list of preopened convenience libraries to check # that they are being used correctly in the link pass. test -z "$libdir" && \ func_append dlpreconveniencelibs " $dir/$old_library" fi ;; * ) # Prefer using a static library (so that no silly _DYNAMIC symbols # are required to link). if test -n "$old_library"; then func_append newdlprefiles " $dir/$old_library" # Keep a list of preopened convenience libraries to check # that they are being used correctly in the link pass. test -z "$libdir" && \ func_append dlpreconveniencelibs " $dir/$old_library" # Otherwise, use the dlname, so that lt_dlopen finds it. elif test -n "$dlname"; then func_append newdlprefiles " $dir/$dlname" else func_append newdlprefiles " $dir/$linklib" fi ;; esac fi # $pass = dlpreopen if test -z "$libdir"; then # Link the convenience library if test lib = "$linkmode"; then deplibs="$dir/$old_library $deplibs" elif test prog,link = "$linkmode,$pass"; then compile_deplibs="$dir/$old_library $compile_deplibs" finalize_deplibs="$dir/$old_library $finalize_deplibs" else deplibs="$lib $deplibs" # used for prog,scan pass fi continue fi if test prog = "$linkmode" && test link != "$pass"; then func_append newlib_search_path " $ladir" deplibs="$lib $deplibs" linkalldeplibs=false if test no != "$link_all_deplibs" || test -z "$library_names" || test no = "$build_libtool_libs"; then linkalldeplibs=: fi tmp_libs= for deplib in $dependency_libs; do case $deplib in -L*) func_stripname '-L' '' "$deplib" func_resolve_sysroot "$func_stripname_result" func_append newlib_search_path " $func_resolve_sysroot_result" ;; esac # Need to link against all dependency_libs? if $linkalldeplibs; then deplibs="$deplib $deplibs" else # Need to hardcode shared library paths # or/and link against static libraries newdependency_libs="$deplib $newdependency_libs" fi if $opt_preserve_dup_deps; then case "$tmp_libs " in *" $deplib "*) func_append specialdeplibs " $deplib" ;; esac fi func_append tmp_libs " $deplib" done # for deplib continue fi # $linkmode = prog... if test prog,link = "$linkmode,$pass"; then if test -n "$library_names" && { { test no = "$prefer_static_libs" || test built,yes = "$prefer_static_libs,$installed"; } || test -z "$old_library"; }; then # We need to hardcode the library path if test -n "$shlibpath_var" && test -z "$avoidtemprpath"; then # Make sure the rpath contains only unique directories. case $temp_rpath: in *"$absdir:"*) ;; *) func_append temp_rpath "$absdir:" ;; esac fi # Hardcode the library path. # Skip directories that are in the system default run-time # search path. case " $sys_lib_dlsearch_path " in *" $absdir "*) ;; *) case "$compile_rpath " in *" $absdir "*) ;; *) func_append compile_rpath " $absdir" ;; esac ;; esac case " $sys_lib_dlsearch_path " in *" $libdir "*) ;; *) case "$finalize_rpath " in *" $libdir "*) ;; *) func_append finalize_rpath " $libdir" ;; esac ;; esac fi # $linkmode,$pass = prog,link... if $alldeplibs && { test pass_all = "$deplibs_check_method" || { test yes = "$build_libtool_libs" && test -n "$library_names"; }; }; then # We only need to search for static libraries continue fi fi link_static=no # Whether the deplib will be linked statically use_static_libs=$prefer_static_libs if test built = "$use_static_libs" && test yes = "$installed"; then use_static_libs=no fi if test -n "$library_names" && { test no = "$use_static_libs" || test -z "$old_library"; }; then case $host in *cygwin* | *mingw* | *cegcc* | *os2*) # No point in relinking DLLs because paths are not encoded func_append notinst_deplibs " $lib" need_relink=no ;; *) if test no = "$installed"; then func_append notinst_deplibs " $lib" need_relink=yes fi ;; esac # This is a shared library # Warn about portability, can't link against -module's on some # systems (darwin). Don't bleat about dlopened modules though! dlopenmodule= for dlpremoduletest in $dlprefiles; do if test "X$dlpremoduletest" = "X$lib"; then dlopenmodule=$dlpremoduletest break fi done if test -z "$dlopenmodule" && test yes = "$shouldnotlink" && test link = "$pass"; then echo if test prog = "$linkmode"; then $ECHO "*** Warning: Linking the executable $output against the loadable module" else $ECHO "*** Warning: Linking the shared library $output against the loadable module" fi $ECHO "*** $linklib is not portable!" fi if test lib = "$linkmode" && test yes = "$hardcode_into_libs"; then # Hardcode the library path. # Skip directories that are in the system default run-time # search path. case " $sys_lib_dlsearch_path " in *" $absdir "*) ;; *) case "$compile_rpath " in *" $absdir "*) ;; *) func_append compile_rpath " $absdir" ;; esac ;; esac case " $sys_lib_dlsearch_path " in *" $libdir "*) ;; *) case "$finalize_rpath " in *" $libdir "*) ;; *) func_append finalize_rpath " $libdir" ;; esac ;; esac fi if test -n "$old_archive_from_expsyms_cmds"; then # figure out the soname set dummy $library_names shift realname=$1 shift libname=`eval "\\$ECHO \"$libname_spec\""` # use dlname if we got it. it's perfectly good, no? if test -n "$dlname"; then soname=$dlname elif test -n "$soname_spec"; then # bleh windows case $host in *cygwin* | mingw* | *cegcc* | *os2*) func_arith $current - $age major=$func_arith_result versuffix=-$major ;; esac eval soname=\"$soname_spec\" else soname=$realname fi # Make a new name for the extract_expsyms_cmds to use soroot=$soname func_basename "$soroot" soname=$func_basename_result func_stripname 'lib' '.dll' "$soname" newlib=libimp-$func_stripname_result.a # If the library has no export list, then create one now if test -f "$output_objdir/$soname-def"; then : else func_verbose "extracting exported symbol list from '$soname'" func_execute_cmds "$extract_expsyms_cmds" 'exit $?' fi # Create $newlib if test -f "$output_objdir/$newlib"; then :; else func_verbose "generating import library for '$soname'" func_execute_cmds "$old_archive_from_expsyms_cmds" 'exit $?' fi # make sure the library variables are pointing to the new library dir=$output_objdir linklib=$newlib fi # test -n "$old_archive_from_expsyms_cmds" if test prog = "$linkmode" || test relink != "$opt_mode"; then add_shlibpath= add_dir= add= lib_linked=yes case $hardcode_action in immediate | unsupported) if test no = "$hardcode_direct"; then add=$dir/$linklib case $host in *-*-sco3.2v5.0.[024]*) add_dir=-L$dir ;; *-*-sysv4*uw2*) add_dir=-L$dir ;; *-*-sysv5OpenUNIX* | *-*-sysv5UnixWare7.[01].[10]* | \ *-*-unixware7*) add_dir=-L$dir ;; *-*-darwin* ) # if the lib is a (non-dlopened) module then we cannot # link against it, someone is ignoring the earlier warnings if /usr/bin/file -L $add 2> /dev/null | $GREP ": [^:]* bundle" >/dev/null; then if test "X$dlopenmodule" != "X$lib"; then $ECHO "*** Warning: lib $linklib is a module, not a shared library" if test -z "$old_library"; then echo echo "*** And there doesn't seem to be a static archive available" echo "*** The link will probably fail, sorry" else add=$dir/$old_library fi elif test -n "$old_library"; then add=$dir/$old_library fi fi esac elif test no = "$hardcode_minus_L"; then case $host in *-*-sunos*) add_shlibpath=$dir ;; esac add_dir=-L$dir add=-l$name elif test no = "$hardcode_shlibpath_var"; then add_shlibpath=$dir add=-l$name else lib_linked=no fi ;; relink) if test yes = "$hardcode_direct" && test no = "$hardcode_direct_absolute"; then add=$dir/$linklib elif test yes = "$hardcode_minus_L"; then add_dir=-L$absdir # Try looking first in the location we're being installed to. if test -n "$inst_prefix_dir"; then case $libdir in [\\/]*) func_append add_dir " -L$inst_prefix_dir$libdir" ;; esac fi add=-l$name elif test yes = "$hardcode_shlibpath_var"; then add_shlibpath=$dir add=-l$name else lib_linked=no fi ;; *) lib_linked=no ;; esac if test yes != "$lib_linked"; then func_fatal_configuration "unsupported hardcode properties" fi if test -n "$add_shlibpath"; then case :$compile_shlibpath: in *":$add_shlibpath:"*) ;; *) func_append compile_shlibpath "$add_shlibpath:" ;; esac fi if test prog = "$linkmode"; then test -n "$add_dir" && compile_deplibs="$add_dir $compile_deplibs" test -n "$add" && compile_deplibs="$add $compile_deplibs" else test -n "$add_dir" && deplibs="$add_dir $deplibs" test -n "$add" && deplibs="$add $deplibs" if test yes != "$hardcode_direct" && test yes != "$hardcode_minus_L" && test yes = "$hardcode_shlibpath_var"; then case :$finalize_shlibpath: in *":$libdir:"*) ;; *) func_append finalize_shlibpath "$libdir:" ;; esac fi fi fi if test prog = "$linkmode" || test relink = "$opt_mode"; then add_shlibpath= add_dir= add= # Finalize command for both is simple: just hardcode it. if test yes = "$hardcode_direct" && test no = "$hardcode_direct_absolute"; then add=$libdir/$linklib elif test yes = "$hardcode_minus_L"; then add_dir=-L$libdir add=-l$name elif test yes = "$hardcode_shlibpath_var"; then case :$finalize_shlibpath: in *":$libdir:"*) ;; *) func_append finalize_shlibpath "$libdir:" ;; esac add=-l$name elif test yes = "$hardcode_automatic"; then if test -n "$inst_prefix_dir" && test -f "$inst_prefix_dir$libdir/$linklib"; then add=$inst_prefix_dir$libdir/$linklib else add=$libdir/$linklib fi else # We cannot seem to hardcode it, guess we'll fake it. add_dir=-L$libdir # Try looking first in the location we're being installed to. if test -n "$inst_prefix_dir"; then case $libdir in [\\/]*) func_append add_dir " -L$inst_prefix_dir$libdir" ;; esac fi add=-l$name fi if test prog = "$linkmode"; then test -n "$add_dir" && finalize_deplibs="$add_dir $finalize_deplibs" test -n "$add" && finalize_deplibs="$add $finalize_deplibs" else test -n "$add_dir" && deplibs="$add_dir $deplibs" test -n "$add" && deplibs="$add $deplibs" fi fi elif test prog = "$linkmode"; then # Here we assume that one of hardcode_direct or hardcode_minus_L # is not unsupported. This is valid on all known static and # shared platforms. if test unsupported != "$hardcode_direct"; then test -n "$old_library" && linklib=$old_library compile_deplibs="$dir/$linklib $compile_deplibs" finalize_deplibs="$dir/$linklib $finalize_deplibs" else compile_deplibs="-l$name -L$dir $compile_deplibs" finalize_deplibs="-l$name -L$dir $finalize_deplibs" fi elif test yes = "$build_libtool_libs"; then # Not a shared library if test pass_all != "$deplibs_check_method"; then # We're trying link a shared library against a static one # but the system doesn't support it. # Just print a warning and add the library to dependency_libs so # that the program can be linked against the static library. echo $ECHO "*** Warning: This system cannot link to static lib archive $lib." echo "*** I have the capability to make that library automatically link in when" echo "*** you link to this library. But I can only do this if you have a" echo "*** shared version of the library, which you do not appear to have." if test yes = "$module"; then echo "*** But as you try to build a module library, libtool will still create " echo "*** a static module, that should work as long as the dlopening application" echo "*** is linked with the -dlopen flag to resolve symbols at runtime." if test -z "$global_symbol_pipe"; then echo echo "*** However, this would only work if libtool was able to extract symbol" echo "*** lists from a program, using 'nm' or equivalent, but libtool could" echo "*** not find such a program. So, this module is probably useless." echo "*** 'nm' from GNU binutils and a full rebuild may help." fi if test no = "$build_old_libs"; then build_libtool_libs=module build_old_libs=yes else build_libtool_libs=no fi fi else deplibs="$dir/$old_library $deplibs" link_static=yes fi fi # link shared/static library? if test lib = "$linkmode"; then if test -n "$dependency_libs" && { test yes != "$hardcode_into_libs" || test yes = "$build_old_libs" || test yes = "$link_static"; }; then # Extract -R from dependency_libs temp_deplibs= for libdir in $dependency_libs; do case $libdir in -R*) func_stripname '-R' '' "$libdir" temp_xrpath=$func_stripname_result case " $xrpath " in *" $temp_xrpath "*) ;; *) func_append xrpath " $temp_xrpath";; esac;; *) func_append temp_deplibs " $libdir";; esac done dependency_libs=$temp_deplibs fi func_append newlib_search_path " $absdir" # Link against this library test no = "$link_static" && newdependency_libs="$abs_ladir/$laname $newdependency_libs" # ... and its dependency_libs tmp_libs= for deplib in $dependency_libs; do newdependency_libs="$deplib $newdependency_libs" case $deplib in -L*) func_stripname '-L' '' "$deplib" func_resolve_sysroot "$func_stripname_result";; *) func_resolve_sysroot "$deplib" ;; esac if $opt_preserve_dup_deps; then case "$tmp_libs " in *" $func_resolve_sysroot_result "*) func_append specialdeplibs " $func_resolve_sysroot_result" ;; esac fi func_append tmp_libs " $func_resolve_sysroot_result" done if test no != "$link_all_deplibs"; then # Add the search paths of all dependency libraries for deplib in $dependency_libs; do path= case $deplib in -L*) path=$deplib ;; *.la) func_resolve_sysroot "$deplib" deplib=$func_resolve_sysroot_result func_dirname "$deplib" "" "." dir=$func_dirname_result # We need an absolute path. case $dir in [\\/]* | [A-Za-z]:[\\/]*) absdir=$dir ;; *) absdir=`cd "$dir" && pwd` if test -z "$absdir"; then func_warning "cannot determine absolute directory name of '$dir'" absdir=$dir fi ;; esac if $GREP "^installed=no" $deplib > /dev/null; then case $host in *-*-darwin*) depdepl= eval deplibrary_names=`$SED -n -e 's/^library_names=\(.*\)$/\1/p' $deplib` if test -n "$deplibrary_names"; then for tmp in $deplibrary_names; do depdepl=$tmp done if test -f "$absdir/$objdir/$depdepl"; then depdepl=$absdir/$objdir/$depdepl darwin_install_name=`$OTOOL -L $depdepl | awk '{if (NR == 2) {print $1;exit}}'` if test -z "$darwin_install_name"; then darwin_install_name=`$OTOOL64 -L $depdepl | awk '{if (NR == 2) {print $1;exit}}'` fi func_append compiler_flags " $wl-dylib_file $wl$darwin_install_name:$depdepl" func_append linker_flags " -dylib_file $darwin_install_name:$depdepl" path= fi fi ;; *) path=-L$absdir/$objdir ;; esac else eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $deplib` test -z "$libdir" && \ func_fatal_error "'$deplib' is not a valid libtool archive" test "$absdir" != "$libdir" && \ func_warning "'$deplib' seems to be moved" path=-L$absdir fi ;; esac case " $deplibs " in *" $path "*) ;; *) deplibs="$path $deplibs" ;; esac done fi # link_all_deplibs != no fi # linkmode = lib done # for deplib in $libs if test link = "$pass"; then if test prog = "$linkmode"; then compile_deplibs="$new_inherited_linker_flags $compile_deplibs" finalize_deplibs="$new_inherited_linker_flags $finalize_deplibs" else compiler_flags="$compiler_flags "`$ECHO " $new_inherited_linker_flags" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` fi fi dependency_libs=$newdependency_libs if test dlpreopen = "$pass"; then # Link the dlpreopened libraries before other libraries for deplib in $save_deplibs; do deplibs="$deplib $deplibs" done fi if test dlopen != "$pass"; then test conv = "$pass" || { # Make sure lib_search_path contains only unique directories. lib_search_path= for dir in $newlib_search_path; do case "$lib_search_path " in *" $dir "*) ;; *) func_append lib_search_path " $dir" ;; esac done newlib_search_path= } if test prog,link = "$linkmode,$pass"; then vars="compile_deplibs finalize_deplibs" else vars=deplibs fi for var in $vars dependency_libs; do # Add libraries to $var in reverse order eval tmp_libs=\"\$$var\" new_libs= for deplib in $tmp_libs; do # FIXME: Pedantically, this is the right thing to do, so # that some nasty dependency loop isn't accidentally # broken: #new_libs="$deplib $new_libs" # Pragmatically, this seems to cause very few problems in # practice: case $deplib in -L*) new_libs="$deplib $new_libs" ;; -R*) ;; *) # And here is the reason: when a library appears more # than once as an explicit dependence of a library, or # is implicitly linked in more than once by the # compiler, it is considered special, and multiple # occurrences thereof are not removed. Compare this # with having the same library being listed as a # dependency of multiple other libraries: in this case, # we know (pedantically, we assume) the library does not # need to be listed more than once, so we keep only the # last copy. This is not always right, but it is rare # enough that we require users that really mean to play # such unportable linking tricks to link the library # using -Wl,-lname, so that libtool does not consider it # for duplicate removal. case " $specialdeplibs " in *" $deplib "*) new_libs="$deplib $new_libs" ;; *) case " $new_libs " in *" $deplib "*) ;; *) new_libs="$deplib $new_libs" ;; esac ;; esac ;; esac done tmp_libs= for deplib in $new_libs; do case $deplib in -L*) case " $tmp_libs " in *" $deplib "*) ;; *) func_append tmp_libs " $deplib" ;; esac ;; *) func_append tmp_libs " $deplib" ;; esac done eval $var=\"$tmp_libs\" done # for var fi # Add Sun CC postdeps if required: test CXX = "$tagname" && { case $host_os in linux*) case `$CC -V 2>&1 | sed 5q` in *Sun\ C*) # Sun C++ 5.9 func_suncc_cstd_abi if test no != "$suncc_use_cstd_abi"; then func_append postdeps ' -library=Cstd -library=Crun' fi ;; esac ;; solaris*) func_cc_basename "$CC" case $func_cc_basename_result in CC* | sunCC*) func_suncc_cstd_abi if test no != "$suncc_use_cstd_abi"; then func_append postdeps ' -library=Cstd -library=Crun' fi ;; esac ;; esac } # Last step: remove runtime libs from dependency_libs # (they stay in deplibs) tmp_libs= for i in $dependency_libs; do case " $predeps $postdeps $compiler_lib_search_path " in *" $i "*) i= ;; esac if test -n "$i"; then func_append tmp_libs " $i" fi done dependency_libs=$tmp_libs done # for pass if test prog = "$linkmode"; then dlfiles=$newdlfiles fi if test prog = "$linkmode" || test lib = "$linkmode"; then dlprefiles=$newdlprefiles fi case $linkmode in oldlib) if test -n "$dlfiles$dlprefiles" || test no != "$dlself"; then func_warning "'-dlopen' is ignored for archives" fi case " $deplibs" in *\ -l* | *\ -L*) func_warning "'-l' and '-L' are ignored for archives" ;; esac test -n "$rpath" && \ func_warning "'-rpath' is ignored for archives" test -n "$xrpath" && \ func_warning "'-R' is ignored for archives" test -n "$vinfo" && \ func_warning "'-version-info/-version-number' is ignored for archives" test -n "$release" && \ func_warning "'-release' is ignored for archives" test -n "$export_symbols$export_symbols_regex" && \ func_warning "'-export-symbols' is ignored for archives" # Now set the variables for building old libraries. build_libtool_libs=no oldlibs=$output func_append objs "$old_deplibs" ;; lib) # Make sure we only generate libraries of the form 'libNAME.la'. case $outputname in lib*) func_stripname 'lib' '.la' "$outputname" name=$func_stripname_result eval shared_ext=\"$shrext_cmds\" eval libname=\"$libname_spec\" ;; *) test no = "$module" \ && func_fatal_help "libtool library '$output' must begin with 'lib'" if test no != "$need_lib_prefix"; then # Add the "lib" prefix for modules if required func_stripname '' '.la' "$outputname" name=$func_stripname_result eval shared_ext=\"$shrext_cmds\" eval libname=\"$libname_spec\" else func_stripname '' '.la' "$outputname" libname=$func_stripname_result fi ;; esac if test -n "$objs"; then if test pass_all != "$deplibs_check_method"; then func_fatal_error "cannot build libtool library '$output' from non-libtool objects on this host:$objs" else echo $ECHO "*** Warning: Linking the shared library $output against the non-libtool" $ECHO "*** objects $objs is not portable!" func_append libobjs " $objs" fi fi test no = "$dlself" \ || func_warning "'-dlopen self' is ignored for libtool libraries" set dummy $rpath shift test 1 -lt "$#" \ && func_warning "ignoring multiple '-rpath's for a libtool library" install_libdir=$1 oldlibs= if test -z "$rpath"; then if test yes = "$build_libtool_libs"; then # Building a libtool convenience library. # Some compilers have problems with a '.al' extension so # convenience libraries should have the same extension an # archive normally would. oldlibs="$output_objdir/$libname.$libext $oldlibs" build_libtool_libs=convenience build_old_libs=yes fi test -n "$vinfo" && \ func_warning "'-version-info/-version-number' is ignored for convenience libraries" test -n "$release" && \ func_warning "'-release' is ignored for convenience libraries" else # Parse the version information argument. save_ifs=$IFS; IFS=: set dummy $vinfo 0 0 0 shift IFS=$save_ifs test -n "$7" && \ func_fatal_help "too many parameters to '-version-info'" # convert absolute version numbers to libtool ages # this retains compatibility with .la files and attempts # to make the code below a bit more comprehensible case $vinfo_number in yes) number_major=$1 number_minor=$2 number_revision=$3 # # There are really only two kinds -- those that # use the current revision as the major version # and those that subtract age and use age as # a minor version. But, then there is irix # that has an extra 1 added just for fun # case $version_type in # correct linux to gnu/linux during the next big refactor darwin|freebsd-elf|linux|osf|windows|none) func_arith $number_major + $number_minor current=$func_arith_result age=$number_minor revision=$number_revision ;; freebsd-aout|qnx|sunos) current=$number_major revision=$number_minor age=0 ;; irix|nonstopux) func_arith $number_major + $number_minor current=$func_arith_result age=$number_minor revision=$number_minor lt_irix_increment=no ;; *) func_fatal_configuration "$modename: unknown library version type '$version_type'" ;; esac ;; no) current=$1 revision=$2 age=$3 ;; esac # Check that each of the things are valid numbers. case $current in 0|[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]) ;; *) func_error "CURRENT '$current' must be a nonnegative integer" func_fatal_error "'$vinfo' is not valid version information" ;; esac case $revision in 0|[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]) ;; *) func_error "REVISION '$revision' must be a nonnegative integer" func_fatal_error "'$vinfo' is not valid version information" ;; esac case $age in 0|[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]) ;; *) func_error "AGE '$age' must be a nonnegative integer" func_fatal_error "'$vinfo' is not valid version information" ;; esac if test "$age" -gt "$current"; then func_error "AGE '$age' is greater than the current interface number '$current'" func_fatal_error "'$vinfo' is not valid version information" fi # Calculate the version variables. major= versuffix= verstring= case $version_type in none) ;; darwin) # Like Linux, but with the current version available in # verstring for coding it into the library header func_arith $current - $age major=.$func_arith_result versuffix=$major.$age.$revision # Darwin ld doesn't like 0 for these options... func_arith $current + 1 minor_current=$func_arith_result xlcverstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision" verstring="-compatibility_version $minor_current -current_version $minor_current.$revision" # On Darwin other compilers case $CC in nagfor*) verstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision" ;; *) verstring="-compatibility_version $minor_current -current_version $minor_current.$revision" ;; esac ;; freebsd-aout) major=.$current versuffix=.$current.$revision ;; freebsd-elf) func_arith $current - $age major=.$func_arith_result versuffix=$major.$age.$revision ;; irix | nonstopux) if test no = "$lt_irix_increment"; then func_arith $current - $age else func_arith $current - $age + 1 fi major=$func_arith_result case $version_type in nonstopux) verstring_prefix=nonstopux ;; *) verstring_prefix=sgi ;; esac verstring=$verstring_prefix$major.$revision # Add in all the interfaces that we are compatible with. loop=$revision while test 0 -ne "$loop"; do func_arith $revision - $loop iface=$func_arith_result func_arith $loop - 1 loop=$func_arith_result verstring=$verstring_prefix$major.$iface:$verstring done # Before this point, $major must not contain '.'. major=.$major versuffix=$major.$revision ;; linux) # correct to gnu/linux during the next big refactor func_arith $current - $age major=.$func_arith_result versuffix=$major.$age.$revision ;; osf) func_arith $current - $age major=.$func_arith_result versuffix=.$current.$age.$revision verstring=$current.$age.$revision # Add in all the interfaces that we are compatible with. loop=$age while test 0 -ne "$loop"; do func_arith $current - $loop iface=$func_arith_result func_arith $loop - 1 loop=$func_arith_result verstring=$verstring:$iface.0 done # Make executables depend on our current version. func_append verstring ":$current.0" ;; qnx) major=.$current versuffix=.$current ;; sco) major=.$current versuffix=.$current ;; sunos) major=.$current versuffix=.$current.$revision ;; windows) # Use '-' rather than '.', since we only want one # extension on DOS 8.3 file systems. func_arith $current - $age major=$func_arith_result versuffix=-$major ;; *) func_fatal_configuration "unknown library version type '$version_type'" ;; esac # Clear the version info if we defaulted, and they specified a release. if test -z "$vinfo" && test -n "$release"; then major= case $version_type in darwin) # we can't check for "0.0" in archive_cmds due to quoting # problems, so we reset it completely verstring= ;; *) verstring=0.0 ;; esac if test no = "$need_version"; then versuffix= else versuffix=.0.0 fi fi # Remove version info from name if versioning should be avoided if test yes,no = "$avoid_version,$need_version"; then major= versuffix= verstring= fi # Check to see if the archive will have undefined symbols. if test yes = "$allow_undefined"; then if test unsupported = "$allow_undefined_flag"; then if test yes = "$build_old_libs"; then func_warning "undefined symbols not allowed in $host shared libraries; building static only" build_libtool_libs=no else func_fatal_error "can't build $host shared library unless -no-undefined is specified" fi fi else # Don't allow undefined symbols. allow_undefined_flag=$no_undefined_flag fi fi func_generate_dlsyms "$libname" "$libname" : func_append libobjs " $symfileobj" test " " = "$libobjs" && libobjs= if test relink != "$opt_mode"; then # Remove our outputs, but don't remove object files since they # may have been created when compiling PIC objects. removelist= tempremovelist=`$ECHO "$output_objdir/*"` for p in $tempremovelist; do case $p in *.$objext | *.gcno) ;; $output_objdir/$outputname | $output_objdir/$libname.* | $output_objdir/$libname$release.*) if test -n "$precious_files_regex"; then if $ECHO "$p" | $EGREP -e "$precious_files_regex" >/dev/null 2>&1 then continue fi fi func_append removelist " $p" ;; *) ;; esac done test -n "$removelist" && \ func_show_eval "${RM}r \$removelist" fi # Now set the variables for building old libraries. if test yes = "$build_old_libs" && test convenience != "$build_libtool_libs"; then func_append oldlibs " $output_objdir/$libname.$libext" # Transform .lo files to .o files. oldobjs="$objs "`$ECHO "$libobjs" | $SP2NL | $SED "/\.$libext$/d; $lo2o" | $NL2SP` fi # Eliminate all temporary directories. #for path in $notinst_path; do # lib_search_path=`$ECHO "$lib_search_path " | $SED "s% $path % %g"` # deplibs=`$ECHO "$deplibs " | $SED "s% -L$path % %g"` # dependency_libs=`$ECHO "$dependency_libs " | $SED "s% -L$path % %g"` #done if test -n "$xrpath"; then # If the user specified any rpath flags, then add them. temp_xrpath= for libdir in $xrpath; do func_replace_sysroot "$libdir" func_append temp_xrpath " -R$func_replace_sysroot_result" case "$finalize_rpath " in *" $libdir "*) ;; *) func_append finalize_rpath " $libdir" ;; esac done if test yes != "$hardcode_into_libs" || test yes = "$build_old_libs"; then dependency_libs="$temp_xrpath $dependency_libs" fi fi # Make sure dlfiles contains only unique files that won't be dlpreopened old_dlfiles=$dlfiles dlfiles= for lib in $old_dlfiles; do case " $dlprefiles $dlfiles " in *" $lib "*) ;; *) func_append dlfiles " $lib" ;; esac done # Make sure dlprefiles contains only unique files old_dlprefiles=$dlprefiles dlprefiles= for lib in $old_dlprefiles; do case "$dlprefiles " in *" $lib "*) ;; *) func_append dlprefiles " $lib" ;; esac done if test yes = "$build_libtool_libs"; then if test -n "$rpath"; then case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-*-beos* | *-cegcc* | *-*-haiku*) # these systems don't actually have a c library (as such)! ;; *-*-rhapsody* | *-*-darwin1.[012]) # Rhapsody C library is in the System framework func_append deplibs " System.ltframework" ;; *-*-netbsd*) # Don't link with libc until the a.out ld.so is fixed. ;; *-*-openbsd* | *-*-freebsd* | *-*-dragonfly*) # Do not include libc due to us having libc/libc_r. ;; *-*-sco3.2v5* | *-*-sco5v6*) # Causes problems with __ctype ;; *-*-sysv4.2uw2* | *-*-sysv5* | *-*-unixware* | *-*-OpenUNIX*) # Compiler inserts libc in the correct place for threads to work ;; *) # Add libc to deplibs on all other systems if necessary. if test yes = "$build_libtool_need_lc"; then func_append deplibs " -lc" fi ;; esac fi # Transform deplibs into only deplibs that can be linked in shared. name_save=$name libname_save=$libname release_save=$release versuffix_save=$versuffix major_save=$major # I'm not sure if I'm treating the release correctly. I think # release should show up in the -l (ie -lgmp5) so we don't want to # add it in twice. Is that correct? release= versuffix= major= newdeplibs= droppeddeps=no case $deplibs_check_method in pass_all) # Don't check for shared/static. Everything works. # This might be a little naive. We might want to check # whether the library exists or not. But this is on # osf3 & osf4 and I'm not really sure... Just # implementing what was already the behavior. newdeplibs=$deplibs ;; test_compile) # This code stresses the "libraries are programs" paradigm to its # limits. Maybe even breaks it. We compile a program, linking it # against the deplibs as a proxy for the library. Then we can check # whether they linked in statically or dynamically with ldd. $opt_dry_run || $RM conftest.c cat > conftest.c </dev/null` $nocaseglob else potential_libs=`ls $i/$libnameglob[.-]* 2>/dev/null` fi for potent_lib in $potential_libs; do # Follow soft links. if ls -lLd "$potent_lib" 2>/dev/null | $GREP " -> " >/dev/null; then continue fi # The statement above tries to avoid entering an # endless loop below, in case of cyclic links. # We might still enter an endless loop, since a link # loop can be closed while we follow links, # but so what? potlib=$potent_lib while test -h "$potlib" 2>/dev/null; do potliblink=`ls -ld $potlib | $SED 's/.* -> //'` case $potliblink in [\\/]* | [A-Za-z]:[\\/]*) potlib=$potliblink;; *) potlib=`$ECHO "$potlib" | $SED 's|[^/]*$||'`"$potliblink";; esac done if eval $file_magic_cmd \"\$potlib\" 2>/dev/null | $SED -e 10q | $EGREP "$file_magic_regex" > /dev/null; then func_append newdeplibs " $a_deplib" a_deplib= break 2 fi done done fi if test -n "$a_deplib"; then droppeddeps=yes echo $ECHO "*** Warning: linker path does not have real file for library $a_deplib." echo "*** I have the capability to make that library automatically link in when" echo "*** you link to this library. But I can only do this if you have a" echo "*** shared version of the library, which you do not appear to have" echo "*** because I did check the linker path looking for a file starting" if test -z "$potlib"; then $ECHO "*** with $libname but no candidates were found. (...for file magic test)" else $ECHO "*** with $libname and none of the candidates passed a file format test" $ECHO "*** using a file magic. Last file checked: $potlib" fi fi ;; *) # Add a -L argument. func_append newdeplibs " $a_deplib" ;; esac done # Gone through all deplibs. ;; match_pattern*) set dummy $deplibs_check_method; shift match_pattern_regex=`expr "$deplibs_check_method" : "$1 \(.*\)"` for a_deplib in $deplibs; do case $a_deplib in -l*) func_stripname -l '' "$a_deplib" name=$func_stripname_result if test yes = "$allow_libtool_libs_with_static_runtimes"; then case " $predeps $postdeps " in *" $a_deplib "*) func_append newdeplibs " $a_deplib" a_deplib= ;; esac fi if test -n "$a_deplib"; then libname=`eval "\\$ECHO \"$libname_spec\""` for i in $lib_search_path $sys_lib_search_path $shlib_search_path; do potential_libs=`ls $i/$libname[.-]* 2>/dev/null` for potent_lib in $potential_libs; do potlib=$potent_lib # see symlink-check above in file_magic test if eval "\$ECHO \"$potent_lib\"" 2>/dev/null | $SED 10q | \ $EGREP "$match_pattern_regex" > /dev/null; then func_append newdeplibs " $a_deplib" a_deplib= break 2 fi done done fi if test -n "$a_deplib"; then droppeddeps=yes echo $ECHO "*** Warning: linker path does not have real file for library $a_deplib." echo "*** I have the capability to make that library automatically link in when" echo "*** you link to this library. But I can only do this if you have a" echo "*** shared version of the library, which you do not appear to have" echo "*** because I did check the linker path looking for a file starting" if test -z "$potlib"; then $ECHO "*** with $libname but no candidates were found. (...for regex pattern test)" else $ECHO "*** with $libname and none of the candidates passed a file format test" $ECHO "*** using a regex pattern. Last file checked: $potlib" fi fi ;; *) # Add a -L argument. func_append newdeplibs " $a_deplib" ;; esac done # Gone through all deplibs. ;; none | unknown | *) newdeplibs= tmp_deplibs=`$ECHO " $deplibs" | $SED 's/ -lc$//; s/ -[LR][^ ]*//g'` if test yes = "$allow_libtool_libs_with_static_runtimes"; then for i in $predeps $postdeps; do # can't use Xsed below, because $i might contain '/' tmp_deplibs=`$ECHO " $tmp_deplibs" | $SED "s|$i||"` done fi case $tmp_deplibs in *[!\ \ ]*) echo if test none = "$deplibs_check_method"; then echo "*** Warning: inter-library dependencies are not supported in this platform." else echo "*** Warning: inter-library dependencies are not known to be supported." fi echo "*** All declared inter-library dependencies are being dropped." droppeddeps=yes ;; esac ;; esac versuffix=$versuffix_save major=$major_save release=$release_save libname=$libname_save name=$name_save case $host in *-*-rhapsody* | *-*-darwin1.[012]) # On Rhapsody replace the C library with the System framework newdeplibs=`$ECHO " $newdeplibs" | $SED 's/ -lc / System.ltframework /'` ;; esac if test yes = "$droppeddeps"; then if test yes = "$module"; then echo echo "*** Warning: libtool could not satisfy all declared inter-library" $ECHO "*** dependencies of module $libname. Therefore, libtool will create" echo "*** a static module, that should work as long as the dlopening" echo "*** application is linked with the -dlopen flag." if test -z "$global_symbol_pipe"; then echo echo "*** However, this would only work if libtool was able to extract symbol" echo "*** lists from a program, using 'nm' or equivalent, but libtool could" echo "*** not find such a program. So, this module is probably useless." echo "*** 'nm' from GNU binutils and a full rebuild may help." fi if test no = "$build_old_libs"; then oldlibs=$output_objdir/$libname.$libext build_libtool_libs=module build_old_libs=yes else build_libtool_libs=no fi else echo "*** The inter-library dependencies that have been dropped here will be" echo "*** automatically added whenever a program is linked with this library" echo "*** or is declared to -dlopen it." if test no = "$allow_undefined"; then echo echo "*** Since this library must not contain undefined symbols," echo "*** because either the platform does not support them or" echo "*** it was explicitly requested with -no-undefined," echo "*** libtool will only create a static version of it." if test no = "$build_old_libs"; then oldlibs=$output_objdir/$libname.$libext build_libtool_libs=module build_old_libs=yes else build_libtool_libs=no fi fi fi fi # Done checking deplibs! deplibs=$newdeplibs fi # Time to change all our "foo.ltframework" stuff back to "-framework foo" case $host in *-*-darwin*) newdeplibs=`$ECHO " $newdeplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` new_inherited_linker_flags=`$ECHO " $new_inherited_linker_flags" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` deplibs=`$ECHO " $deplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` ;; esac # move library search paths that coincide with paths to not yet # installed libraries to the beginning of the library search list new_libs= for path in $notinst_path; do case " $new_libs " in *" -L$path/$objdir "*) ;; *) case " $deplibs " in *" -L$path/$objdir "*) func_append new_libs " -L$path/$objdir" ;; esac ;; esac done for deplib in $deplibs; do case $deplib in -L*) case " $new_libs " in *" $deplib "*) ;; *) func_append new_libs " $deplib" ;; esac ;; *) func_append new_libs " $deplib" ;; esac done deplibs=$new_libs # All the library-specific variables (install_libdir is set above). library_names= old_library= dlname= # Test again, we may have decided not to build it any more if test yes = "$build_libtool_libs"; then # Remove $wl instances when linking with ld. # FIXME: should test the right _cmds variable. case $archive_cmds in *\$LD\ *) wl= ;; esac if test yes = "$hardcode_into_libs"; then # Hardcode the library paths hardcode_libdirs= dep_rpath= rpath=$finalize_rpath test relink = "$opt_mode" || rpath=$compile_rpath$rpath for libdir in $rpath; do if test -n "$hardcode_libdir_flag_spec"; then if test -n "$hardcode_libdir_separator"; then func_replace_sysroot "$libdir" libdir=$func_replace_sysroot_result if test -z "$hardcode_libdirs"; then hardcode_libdirs=$libdir else # Just accumulate the unique libdirs. case $hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator in *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*) ;; *) func_append hardcode_libdirs "$hardcode_libdir_separator$libdir" ;; esac fi else eval flag=\"$hardcode_libdir_flag_spec\" func_append dep_rpath " $flag" fi elif test -n "$runpath_var"; then case "$perm_rpath " in *" $libdir "*) ;; *) func_append perm_rpath " $libdir" ;; esac fi done # Substitute the hardcoded libdirs into the rpath. if test -n "$hardcode_libdir_separator" && test -n "$hardcode_libdirs"; then libdir=$hardcode_libdirs eval "dep_rpath=\"$hardcode_libdir_flag_spec\"" fi if test -n "$runpath_var" && test -n "$perm_rpath"; then # We should set the runpath_var. rpath= for dir in $perm_rpath; do func_append rpath "$dir:" done eval "$runpath_var='$rpath\$$runpath_var'; export $runpath_var" fi test -n "$dep_rpath" && deplibs="$dep_rpath $deplibs" fi shlibpath=$finalize_shlibpath test relink = "$opt_mode" || shlibpath=$compile_shlibpath$shlibpath if test -n "$shlibpath"; then eval "$shlibpath_var='$shlibpath\$$shlibpath_var'; export $shlibpath_var" fi # Get the real and link names of the library. eval shared_ext=\"$shrext_cmds\" eval library_names=\"$library_names_spec\" set dummy $library_names shift realname=$1 shift if test -n "$soname_spec"; then eval soname=\"$soname_spec\" else soname=$realname fi if test -z "$dlname"; then dlname=$soname fi lib=$output_objdir/$realname linknames= for link do func_append linknames " $link" done # Use standard objects if they are pic test -z "$pic_flag" && libobjs=`$ECHO "$libobjs" | $SP2NL | $SED "$lo2o" | $NL2SP` test "X$libobjs" = "X " && libobjs= delfiles= if test -n "$export_symbols" && test -n "$include_expsyms"; then $opt_dry_run || cp "$export_symbols" "$output_objdir/$libname.uexp" export_symbols=$output_objdir/$libname.uexp func_append delfiles " $export_symbols" fi orig_export_symbols= case $host_os in cygwin* | mingw* | cegcc*) if test -n "$export_symbols" && test -z "$export_symbols_regex"; then # exporting using user supplied symfile func_dll_def_p "$export_symbols" || { # and it's NOT already a .def file. Must figure out # which of the given symbols are data symbols and tag # them as such. So, trigger use of export_symbols_cmds. # export_symbols gets reassigned inside the "prepare # the list of exported symbols" if statement, so the # include_expsyms logic still works. orig_export_symbols=$export_symbols export_symbols= always_export_symbols=yes } fi ;; esac # Prepare the list of exported symbols if test -z "$export_symbols"; then if test yes = "$always_export_symbols" || test -n "$export_symbols_regex"; then func_verbose "generating symbol list for '$libname.la'" export_symbols=$output_objdir/$libname.exp $opt_dry_run || $RM $export_symbols cmds=$export_symbols_cmds save_ifs=$IFS; IFS='~' for cmd1 in $cmds; do IFS=$save_ifs # Take the normal branch if the nm_file_list_spec branch # doesn't work or if tool conversion is not needed. case $nm_file_list_spec~$to_tool_file_cmd in *~func_convert_file_noop | *~func_convert_file_msys_to_w32 | ~*) try_normal_branch=yes eval cmd=\"$cmd1\" func_len " $cmd" len=$func_len_result ;; *) try_normal_branch=no ;; esac if test yes = "$try_normal_branch" \ && { test "$len" -lt "$max_cmd_len" \ || test "$max_cmd_len" -le -1; } then func_show_eval "$cmd" 'exit $?' skipped_export=false elif test -n "$nm_file_list_spec"; then func_basename "$output" output_la=$func_basename_result save_libobjs=$libobjs save_output=$output output=$output_objdir/$output_la.nm func_to_tool_file "$output" libobjs=$nm_file_list_spec$func_to_tool_file_result func_append delfiles " $output" func_verbose "creating $NM input file list: $output" for obj in $save_libobjs; do func_to_tool_file "$obj" $ECHO "$func_to_tool_file_result" done > "$output" eval cmd=\"$cmd1\" func_show_eval "$cmd" 'exit $?' output=$save_output libobjs=$save_libobjs skipped_export=false else # The command line is too long to execute in one step. func_verbose "using reloadable object file for export list..." skipped_export=: # Break out early, otherwise skipped_export may be # set to false by a later but shorter cmd. break fi done IFS=$save_ifs if test -n "$export_symbols_regex" && test : != "$skipped_export"; then func_show_eval '$EGREP -e "$export_symbols_regex" "$export_symbols" > "${export_symbols}T"' func_show_eval '$MV "${export_symbols}T" "$export_symbols"' fi fi fi if test -n "$export_symbols" && test -n "$include_expsyms"; then tmp_export_symbols=$export_symbols test -n "$orig_export_symbols" && tmp_export_symbols=$orig_export_symbols $opt_dry_run || eval '$ECHO "$include_expsyms" | $SP2NL >> "$tmp_export_symbols"' fi if test : != "$skipped_export" && test -n "$orig_export_symbols"; then # The given exports_symbols file has to be filtered, so filter it. func_verbose "filter symbol list for '$libname.la' to tag DATA exports" # FIXME: $output_objdir/$libname.filter potentially contains lots of # 's' commands, which not all seds can handle. GNU sed should be fine # though. Also, the filter scales superlinearly with the number of # global variables. join(1) would be nice here, but unfortunately # isn't a blessed tool. $opt_dry_run || $SED -e '/[ ,]DATA/!d;s,\(.*\)\([ \,].*\),s|^\1$|\1\2|,' < $export_symbols > $output_objdir/$libname.filter func_append delfiles " $export_symbols $output_objdir/$libname.filter" export_symbols=$output_objdir/$libname.def $opt_dry_run || $SED -f $output_objdir/$libname.filter < $orig_export_symbols > $export_symbols fi tmp_deplibs= for test_deplib in $deplibs; do case " $convenience " in *" $test_deplib "*) ;; *) func_append tmp_deplibs " $test_deplib" ;; esac done deplibs=$tmp_deplibs if test -n "$convenience"; then if test -n "$whole_archive_flag_spec" && test yes = "$compiler_needs_object" && test -z "$libobjs"; then # extract the archives, so we have objects to list. # TODO: could optimize this to just extract one archive. whole_archive_flag_spec= fi if test -n "$whole_archive_flag_spec"; then save_libobjs=$libobjs eval libobjs=\"\$libobjs $whole_archive_flag_spec\" test "X$libobjs" = "X " && libobjs= else gentop=$output_objdir/${outputname}x func_append generated " $gentop" func_extract_archives $gentop $convenience func_append libobjs " $func_extract_archives_result" test "X$libobjs" = "X " && libobjs= fi fi if test yes = "$thread_safe" && test -n "$thread_safe_flag_spec"; then eval flag=\"$thread_safe_flag_spec\" func_append linker_flags " $flag" fi # Make a backup of the uninstalled library when relinking if test relink = "$opt_mode"; then $opt_dry_run || eval '(cd $output_objdir && $RM ${realname}U && $MV $realname ${realname}U)' || exit $? fi # Do each of the archive commands. if test yes = "$module" && test -n "$module_cmds"; then if test -n "$export_symbols" && test -n "$module_expsym_cmds"; then eval test_cmds=\"$module_expsym_cmds\" cmds=$module_expsym_cmds else eval test_cmds=\"$module_cmds\" cmds=$module_cmds fi else if test -n "$export_symbols" && test -n "$archive_expsym_cmds"; then eval test_cmds=\"$archive_expsym_cmds\" cmds=$archive_expsym_cmds else eval test_cmds=\"$archive_cmds\" cmds=$archive_cmds fi fi if test : != "$skipped_export" && func_len " $test_cmds" && len=$func_len_result && test "$len" -lt "$max_cmd_len" || test "$max_cmd_len" -le -1; then : else # The command line is too long to link in one step, link piecewise # or, if using GNU ld and skipped_export is not :, use a linker # script. # Save the value of $output and $libobjs because we want to # use them later. If we have whole_archive_flag_spec, we # want to use save_libobjs as it was before # whole_archive_flag_spec was expanded, because we can't # assume the linker understands whole_archive_flag_spec. # This may have to be revisited, in case too many # convenience libraries get linked in and end up exceeding # the spec. if test -z "$convenience" || test -z "$whole_archive_flag_spec"; then save_libobjs=$libobjs fi save_output=$output func_basename "$output" output_la=$func_basename_result # Clear the reloadable object creation command queue and # initialize k to one. test_cmds= concat_cmds= objlist= last_robj= k=1 if test -n "$save_libobjs" && test : != "$skipped_export" && test yes = "$with_gnu_ld"; then output=$output_objdir/$output_la.lnkscript func_verbose "creating GNU ld script: $output" echo 'INPUT (' > $output for obj in $save_libobjs do func_to_tool_file "$obj" $ECHO "$func_to_tool_file_result" >> $output done echo ')' >> $output func_append delfiles " $output" func_to_tool_file "$output" output=$func_to_tool_file_result elif test -n "$save_libobjs" && test : != "$skipped_export" && test -n "$file_list_spec"; then output=$output_objdir/$output_la.lnk func_verbose "creating linker input file list: $output" : > $output set x $save_libobjs shift firstobj= if test yes = "$compiler_needs_object"; then firstobj="$1 " shift fi for obj do func_to_tool_file "$obj" $ECHO "$func_to_tool_file_result" >> $output done func_append delfiles " $output" func_to_tool_file "$output" output=$firstobj\"$file_list_spec$func_to_tool_file_result\" else if test -n "$save_libobjs"; then func_verbose "creating reloadable object files..." output=$output_objdir/$output_la-$k.$objext eval test_cmds=\"$reload_cmds\" func_len " $test_cmds" len0=$func_len_result len=$len0 # Loop over the list of objects to be linked. for obj in $save_libobjs do func_len " $obj" func_arith $len + $func_len_result len=$func_arith_result if test -z "$objlist" || test "$len" -lt "$max_cmd_len"; then func_append objlist " $obj" else # The command $test_cmds is almost too long, add a # command to the queue. if test 1 -eq "$k"; then # The first file doesn't have a previous command to add. reload_objs=$objlist eval concat_cmds=\"$reload_cmds\" else # All subsequent reloadable object files will link in # the last one created. reload_objs="$objlist $last_robj" eval concat_cmds=\"\$concat_cmds~$reload_cmds~\$RM $last_robj\" fi last_robj=$output_objdir/$output_la-$k.$objext func_arith $k + 1 k=$func_arith_result output=$output_objdir/$output_la-$k.$objext objlist=" $obj" func_len " $last_robj" func_arith $len0 + $func_len_result len=$func_arith_result fi done # Handle the remaining objects by creating one last # reloadable object file. All subsequent reloadable object # files will link in the last one created. test -z "$concat_cmds" || concat_cmds=$concat_cmds~ reload_objs="$objlist $last_robj" eval concat_cmds=\"\$concat_cmds$reload_cmds\" if test -n "$last_robj"; then eval concat_cmds=\"\$concat_cmds~\$RM $last_robj\" fi func_append delfiles " $output" else output= fi ${skipped_export-false} && { func_verbose "generating symbol list for '$libname.la'" export_symbols=$output_objdir/$libname.exp $opt_dry_run || $RM $export_symbols libobjs=$output # Append the command to create the export file. test -z "$concat_cmds" || concat_cmds=$concat_cmds~ eval concat_cmds=\"\$concat_cmds$export_symbols_cmds\" if test -n "$last_robj"; then eval concat_cmds=\"\$concat_cmds~\$RM $last_robj\" fi } test -n "$save_libobjs" && func_verbose "creating a temporary reloadable object file: $output" # Loop through the commands generated above and execute them. save_ifs=$IFS; IFS='~' for cmd in $concat_cmds; do IFS=$save_ifs $opt_quiet || { func_quote_for_expand "$cmd" eval "func_echo $func_quote_for_expand_result" } $opt_dry_run || eval "$cmd" || { lt_exit=$? # Restore the uninstalled library and exit if test relink = "$opt_mode"; then ( cd "$output_objdir" && \ $RM "${realname}T" && \ $MV "${realname}U" "$realname" ) fi exit $lt_exit } done IFS=$save_ifs if test -n "$export_symbols_regex" && ${skipped_export-false}; then func_show_eval '$EGREP -e "$export_symbols_regex" "$export_symbols" > "${export_symbols}T"' func_show_eval '$MV "${export_symbols}T" "$export_symbols"' fi fi ${skipped_export-false} && { if test -n "$export_symbols" && test -n "$include_expsyms"; then tmp_export_symbols=$export_symbols test -n "$orig_export_symbols" && tmp_export_symbols=$orig_export_symbols $opt_dry_run || eval '$ECHO "$include_expsyms" | $SP2NL >> "$tmp_export_symbols"' fi if test -n "$orig_export_symbols"; then # The given exports_symbols file has to be filtered, so filter it. func_verbose "filter symbol list for '$libname.la' to tag DATA exports" # FIXME: $output_objdir/$libname.filter potentially contains lots of # 's' commands, which not all seds can handle. GNU sed should be fine # though. Also, the filter scales superlinearly with the number of # global variables. join(1) would be nice here, but unfortunately # isn't a blessed tool. $opt_dry_run || $SED -e '/[ ,]DATA/!d;s,\(.*\)\([ \,].*\),s|^\1$|\1\2|,' < $export_symbols > $output_objdir/$libname.filter func_append delfiles " $export_symbols $output_objdir/$libname.filter" export_symbols=$output_objdir/$libname.def $opt_dry_run || $SED -f $output_objdir/$libname.filter < $orig_export_symbols > $export_symbols fi } libobjs=$output # Restore the value of output. output=$save_output if test -n "$convenience" && test -n "$whole_archive_flag_spec"; then eval libobjs=\"\$libobjs $whole_archive_flag_spec\" test "X$libobjs" = "X " && libobjs= fi # Expand the library linking commands again to reset the # value of $libobjs for piecewise linking. # Do each of the archive commands. if test yes = "$module" && test -n "$module_cmds"; then if test -n "$export_symbols" && test -n "$module_expsym_cmds"; then cmds=$module_expsym_cmds else cmds=$module_cmds fi else if test -n "$export_symbols" && test -n "$archive_expsym_cmds"; then cmds=$archive_expsym_cmds else cmds=$archive_cmds fi fi fi if test -n "$delfiles"; then # Append the command to remove temporary files to $cmds. eval cmds=\"\$cmds~\$RM $delfiles\" fi # Add any objects from preloaded convenience libraries if test -n "$dlprefiles"; then gentop=$output_objdir/${outputname}x func_append generated " $gentop" func_extract_archives $gentop $dlprefiles func_append libobjs " $func_extract_archives_result" test "X$libobjs" = "X " && libobjs= fi save_ifs=$IFS; IFS='~' for cmd in $cmds; do IFS=$sp$nl eval cmd=\"$cmd\" IFS=$save_ifs $opt_quiet || { func_quote_for_expand "$cmd" eval "func_echo $func_quote_for_expand_result" } $opt_dry_run || eval "$cmd" || { lt_exit=$? # Restore the uninstalled library and exit if test relink = "$opt_mode"; then ( cd "$output_objdir" && \ $RM "${realname}T" && \ $MV "${realname}U" "$realname" ) fi exit $lt_exit } done IFS=$save_ifs # Restore the uninstalled library and exit if test relink = "$opt_mode"; then $opt_dry_run || eval '(cd $output_objdir && $RM ${realname}T && $MV $realname ${realname}T && $MV ${realname}U $realname)' || exit $? if test -n "$convenience"; then if test -z "$whole_archive_flag_spec"; then func_show_eval '${RM}r "$gentop"' fi fi exit $EXIT_SUCCESS fi # Create links to the real library. for linkname in $linknames; do if test "$realname" != "$linkname"; then func_show_eval '(cd "$output_objdir" && $RM "$linkname" && $LN_S "$realname" "$linkname")' 'exit $?' fi done # If -module or -export-dynamic was specified, set the dlname. if test yes = "$module" || test yes = "$export_dynamic"; then # On all known operating systems, these are identical. dlname=$soname fi fi ;; obj) if test -n "$dlfiles$dlprefiles" || test no != "$dlself"; then func_warning "'-dlopen' is ignored for objects" fi case " $deplibs" in *\ -l* | *\ -L*) func_warning "'-l' and '-L' are ignored for objects" ;; esac test -n "$rpath" && \ func_warning "'-rpath' is ignored for objects" test -n "$xrpath" && \ func_warning "'-R' is ignored for objects" test -n "$vinfo" && \ func_warning "'-version-info' is ignored for objects" test -n "$release" && \ func_warning "'-release' is ignored for objects" case $output in *.lo) test -n "$objs$old_deplibs" && \ func_fatal_error "cannot build library object '$output' from non-libtool objects" libobj=$output func_lo2o "$libobj" obj=$func_lo2o_result ;; *) libobj= obj=$output ;; esac # Delete the old objects. $opt_dry_run || $RM $obj $libobj # Objects from convenience libraries. This assumes # single-version convenience libraries. Whenever we create # different ones for PIC/non-PIC, this we'll have to duplicate # the extraction. reload_conv_objs= gentop= # if reload_cmds runs $LD directly, get rid of -Wl from # whole_archive_flag_spec and hope we can get by with turning comma # into space. case $reload_cmds in *\$LD[\ \$]*) wl= ;; esac if test -n "$convenience"; then if test -n "$whole_archive_flag_spec"; then eval tmp_whole_archive_flags=\"$whole_archive_flag_spec\" test -n "$wl" || tmp_whole_archive_flags=`$ECHO "$tmp_whole_archive_flags" | $SED 's|,| |g'` reload_conv_objs=$reload_objs\ $tmp_whole_archive_flags else gentop=$output_objdir/${obj}x func_append generated " $gentop" func_extract_archives $gentop $convenience reload_conv_objs="$reload_objs $func_extract_archives_result" fi fi # If we're not building shared, we need to use non_pic_objs test yes = "$build_libtool_libs" || libobjs=$non_pic_objects # Create the old-style object. reload_objs=$objs$old_deplibs' '`$ECHO "$libobjs" | $SP2NL | $SED "/\.$libext$/d; /\.lib$/d; $lo2o" | $NL2SP`' '$reload_conv_objs output=$obj func_execute_cmds "$reload_cmds" 'exit $?' # Exit if we aren't doing a library object file. if test -z "$libobj"; then if test -n "$gentop"; then func_show_eval '${RM}r "$gentop"' fi exit $EXIT_SUCCESS fi test yes = "$build_libtool_libs" || { if test -n "$gentop"; then func_show_eval '${RM}r "$gentop"' fi # Create an invalid libtool object if no PIC, so that we don't # accidentally link it into a program. # $show "echo timestamp > $libobj" # $opt_dry_run || eval "echo timestamp > $libobj" || exit $? exit $EXIT_SUCCESS } if test -n "$pic_flag" || test default != "$pic_mode"; then # Only do commands if we really have different PIC objects. reload_objs="$libobjs $reload_conv_objs" output=$libobj func_execute_cmds "$reload_cmds" 'exit $?' fi if test -n "$gentop"; then func_show_eval '${RM}r "$gentop"' fi exit $EXIT_SUCCESS ;; prog) case $host in *cygwin*) func_stripname '' '.exe' "$output" output=$func_stripname_result.exe;; esac test -n "$vinfo" && \ func_warning "'-version-info' is ignored for programs" test -n "$release" && \ func_warning "'-release' is ignored for programs" $preload \ && test unknown,unknown,unknown = "$dlopen_support,$dlopen_self,$dlopen_self_static" \ && func_warning "'LT_INIT([dlopen])' not used. Assuming no dlopen support." case $host in *-*-rhapsody* | *-*-darwin1.[012]) # On Rhapsody replace the C library is the System framework compile_deplibs=`$ECHO " $compile_deplibs" | $SED 's/ -lc / System.ltframework /'` finalize_deplibs=`$ECHO " $finalize_deplibs" | $SED 's/ -lc / System.ltframework /'` ;; esac case $host in *-*-darwin*) # Don't allow lazy linking, it breaks C++ global constructors # But is supposedly fixed on 10.4 or later (yay!). if test CXX = "$tagname"; then case ${MACOSX_DEPLOYMENT_TARGET-10.0} in 10.[0123]) func_append compile_command " $wl-bind_at_load" func_append finalize_command " $wl-bind_at_load" ;; esac fi # Time to change all our "foo.ltframework" stuff back to "-framework foo" compile_deplibs=`$ECHO " $compile_deplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` finalize_deplibs=`$ECHO " $finalize_deplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` ;; esac # move library search paths that coincide with paths to not yet # installed libraries to the beginning of the library search list new_libs= for path in $notinst_path; do case " $new_libs " in *" -L$path/$objdir "*) ;; *) case " $compile_deplibs " in *" -L$path/$objdir "*) func_append new_libs " -L$path/$objdir" ;; esac ;; esac done for deplib in $compile_deplibs; do case $deplib in -L*) case " $new_libs " in *" $deplib "*) ;; *) func_append new_libs " $deplib" ;; esac ;; *) func_append new_libs " $deplib" ;; esac done compile_deplibs=$new_libs func_append compile_command " $compile_deplibs" func_append finalize_command " $finalize_deplibs" if test -n "$rpath$xrpath"; then # If the user specified any rpath flags, then add them. for libdir in $rpath $xrpath; do # This is the magic to use -rpath. case "$finalize_rpath " in *" $libdir "*) ;; *) func_append finalize_rpath " $libdir" ;; esac done fi # Now hardcode the library paths rpath= hardcode_libdirs= for libdir in $compile_rpath $finalize_rpath; do if test -n "$hardcode_libdir_flag_spec"; then if test -n "$hardcode_libdir_separator"; then if test -z "$hardcode_libdirs"; then hardcode_libdirs=$libdir else # Just accumulate the unique libdirs. case $hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator in *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*) ;; *) func_append hardcode_libdirs "$hardcode_libdir_separator$libdir" ;; esac fi else eval flag=\"$hardcode_libdir_flag_spec\" func_append rpath " $flag" fi elif test -n "$runpath_var"; then case "$perm_rpath " in *" $libdir "*) ;; *) func_append perm_rpath " $libdir" ;; esac fi case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-cegcc*) testbindir=`$ECHO "$libdir" | $SED -e 's*/lib$*/bin*'` case :$dllsearchpath: in *":$libdir:"*) ;; ::) dllsearchpath=$libdir;; *) func_append dllsearchpath ":$libdir";; esac case :$dllsearchpath: in *":$testbindir:"*) ;; ::) dllsearchpath=$testbindir;; *) func_append dllsearchpath ":$testbindir";; esac ;; esac done # Substitute the hardcoded libdirs into the rpath. if test -n "$hardcode_libdir_separator" && test -n "$hardcode_libdirs"; then libdir=$hardcode_libdirs eval rpath=\" $hardcode_libdir_flag_spec\" fi compile_rpath=$rpath rpath= hardcode_libdirs= for libdir in $finalize_rpath; do if test -n "$hardcode_libdir_flag_spec"; then if test -n "$hardcode_libdir_separator"; then if test -z "$hardcode_libdirs"; then hardcode_libdirs=$libdir else # Just accumulate the unique libdirs. case $hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator in *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*) ;; *) func_append hardcode_libdirs "$hardcode_libdir_separator$libdir" ;; esac fi else eval flag=\"$hardcode_libdir_flag_spec\" func_append rpath " $flag" fi elif test -n "$runpath_var"; then case "$finalize_perm_rpath " in *" $libdir "*) ;; *) func_append finalize_perm_rpath " $libdir" ;; esac fi done # Substitute the hardcoded libdirs into the rpath. if test -n "$hardcode_libdir_separator" && test -n "$hardcode_libdirs"; then libdir=$hardcode_libdirs eval rpath=\" $hardcode_libdir_flag_spec\" fi finalize_rpath=$rpath if test -n "$libobjs" && test yes = "$build_old_libs"; then # Transform all the library objects into standard objects. compile_command=`$ECHO "$compile_command" | $SP2NL | $SED "$lo2o" | $NL2SP` finalize_command=`$ECHO "$finalize_command" | $SP2NL | $SED "$lo2o" | $NL2SP` fi func_generate_dlsyms "$outputname" "@PROGRAM@" false # template prelinking step if test -n "$prelink_cmds"; then func_execute_cmds "$prelink_cmds" 'exit $?' fi wrappers_required=: case $host in *cegcc* | *mingw32ce*) # Disable wrappers for cegcc and mingw32ce hosts, we are cross compiling anyway. wrappers_required=false ;; *cygwin* | *mingw* ) test yes = "$build_libtool_libs" || wrappers_required=false ;; *) if test no = "$need_relink" || test yes != "$build_libtool_libs"; then wrappers_required=false fi ;; esac $wrappers_required || { # Replace the output file specification. compile_command=`$ECHO "$compile_command" | $SED 's%@OUTPUT@%'"$output"'%g'` link_command=$compile_command$compile_rpath # We have no uninstalled library dependencies, so finalize right now. exit_status=0 func_show_eval "$link_command" 'exit_status=$?' if test -n "$postlink_cmds"; then func_to_tool_file "$output" postlink_cmds=`func_echo_all "$postlink_cmds" | $SED -e 's%@OUTPUT@%'"$output"'%g' -e 's%@TOOL_OUTPUT@%'"$func_to_tool_file_result"'%g'` func_execute_cmds "$postlink_cmds" 'exit $?' fi # Delete the generated files. if test -f "$output_objdir/${outputname}S.$objext"; then func_show_eval '$RM "$output_objdir/${outputname}S.$objext"' fi exit $exit_status } if test -n "$compile_shlibpath$finalize_shlibpath"; then compile_command="$shlibpath_var=\"$compile_shlibpath$finalize_shlibpath\$$shlibpath_var\" $compile_command" fi if test -n "$finalize_shlibpath"; then finalize_command="$shlibpath_var=\"$finalize_shlibpath\$$shlibpath_var\" $finalize_command" fi compile_var= finalize_var= if test -n "$runpath_var"; then if test -n "$perm_rpath"; then # We should set the runpath_var. rpath= for dir in $perm_rpath; do func_append rpath "$dir:" done compile_var="$runpath_var=\"$rpath\$$runpath_var\" " fi if test -n "$finalize_perm_rpath"; then # We should set the runpath_var. rpath= for dir in $finalize_perm_rpath; do func_append rpath "$dir:" done finalize_var="$runpath_var=\"$rpath\$$runpath_var\" " fi fi if test yes = "$no_install"; then # We don't need to create a wrapper script. link_command=$compile_var$compile_command$compile_rpath # Replace the output file specification. link_command=`$ECHO "$link_command" | $SED 's%@OUTPUT@%'"$output"'%g'` # Delete the old output file. $opt_dry_run || $RM $output # Link the executable and exit func_show_eval "$link_command" 'exit $?' if test -n "$postlink_cmds"; then func_to_tool_file "$output" postlink_cmds=`func_echo_all "$postlink_cmds" | $SED -e 's%@OUTPUT@%'"$output"'%g' -e 's%@TOOL_OUTPUT@%'"$func_to_tool_file_result"'%g'` func_execute_cmds "$postlink_cmds" 'exit $?' fi exit $EXIT_SUCCESS fi case $hardcode_action,$fast_install in relink,*) # Fast installation is not supported link_command=$compile_var$compile_command$compile_rpath relink_command=$finalize_var$finalize_command$finalize_rpath func_warning "this platform does not like uninstalled shared libraries" func_warning "'$output' will be relinked during installation" ;; *,yes) link_command=$finalize_var$compile_command$finalize_rpath relink_command=`$ECHO "$compile_var$compile_command$compile_rpath" | $SED 's%@OUTPUT@%\$progdir/\$file%g'` ;; *,no) link_command=$compile_var$compile_command$compile_rpath relink_command=$finalize_var$finalize_command$finalize_rpath ;; *,needless) link_command=$finalize_var$compile_command$finalize_rpath relink_command= ;; esac # Replace the output file specification. link_command=`$ECHO "$link_command" | $SED 's%@OUTPUT@%'"$output_objdir/$outputname"'%g'` # Delete the old output files. $opt_dry_run || $RM $output $output_objdir/$outputname $output_objdir/lt-$outputname func_show_eval "$link_command" 'exit $?' if test -n "$postlink_cmds"; then func_to_tool_file "$output_objdir/$outputname" postlink_cmds=`func_echo_all "$postlink_cmds" | $SED -e 's%@OUTPUT@%'"$output_objdir/$outputname"'%g' -e 's%@TOOL_OUTPUT@%'"$func_to_tool_file_result"'%g'` func_execute_cmds "$postlink_cmds" 'exit $?' fi # Now create the wrapper script. func_verbose "creating $output" # Quote the relink command for shipping. if test -n "$relink_command"; then # Preserve any variables that may affect compiler behavior for var in $variables_saved_for_relink; do if eval test -z \"\${$var+set}\"; then relink_command="{ test -z \"\${$var+set}\" || $lt_unset $var || { $var=; export $var; }; }; $relink_command" elif eval var_value=\$$var; test -z "$var_value"; then relink_command="$var=; export $var; $relink_command" else func_quote_for_eval "$var_value" relink_command="$var=$func_quote_for_eval_result; export $var; $relink_command" fi done relink_command="(cd `pwd`; $relink_command)" relink_command=`$ECHO "$relink_command" | $SED "$sed_quote_subst"` fi # Only actually do things if not in dry run mode. $opt_dry_run || { # win32 will think the script is a binary if it has # a .exe suffix, so we strip it off here. case $output in *.exe) func_stripname '' '.exe' "$output" output=$func_stripname_result ;; esac # test for cygwin because mv fails w/o .exe extensions case $host in *cygwin*) exeext=.exe func_stripname '' '.exe' "$outputname" outputname=$func_stripname_result ;; *) exeext= ;; esac case $host in *cygwin* | *mingw* ) func_dirname_and_basename "$output" "" "." output_name=$func_basename_result output_path=$func_dirname_result cwrappersource=$output_path/$objdir/lt-$output_name.c cwrapper=$output_path/$output_name.exe $RM $cwrappersource $cwrapper trap "$RM $cwrappersource $cwrapper; exit $EXIT_FAILURE" 1 2 15 func_emit_cwrapperexe_src > $cwrappersource # The wrapper executable is built using the $host compiler, # because it contains $host paths and files. If cross- # compiling, it, like the target executable, must be # executed on the $host or under an emulation environment. $opt_dry_run || { $LTCC $LTCFLAGS -o $cwrapper $cwrappersource $STRIP $cwrapper } # Now, create the wrapper script for func_source use: func_ltwrapper_scriptname $cwrapper $RM $func_ltwrapper_scriptname_result trap "$RM $func_ltwrapper_scriptname_result; exit $EXIT_FAILURE" 1 2 15 $opt_dry_run || { # note: this script will not be executed, so do not chmod. if test "x$build" = "x$host"; then $cwrapper --lt-dump-script > $func_ltwrapper_scriptname_result else func_emit_wrapper no > $func_ltwrapper_scriptname_result fi } ;; * ) $RM $output trap "$RM $output; exit $EXIT_FAILURE" 1 2 15 func_emit_wrapper no > $output chmod +x $output ;; esac } exit $EXIT_SUCCESS ;; esac # See if we need to build an old-fashioned archive. for oldlib in $oldlibs; do case $build_libtool_libs in convenience) oldobjs="$libobjs_save $symfileobj" addlibs=$convenience build_libtool_libs=no ;; module) oldobjs=$libobjs_save addlibs=$old_convenience build_libtool_libs=no ;; *) oldobjs="$old_deplibs $non_pic_objects" $preload && test -f "$symfileobj" \ && func_append oldobjs " $symfileobj" addlibs=$old_convenience ;; esac if test -n "$addlibs"; then gentop=$output_objdir/${outputname}x func_append generated " $gentop" func_extract_archives $gentop $addlibs func_append oldobjs " $func_extract_archives_result" fi # Do each command in the archive commands. if test -n "$old_archive_from_new_cmds" && test yes = "$build_libtool_libs"; then cmds=$old_archive_from_new_cmds else # Add any objects from preloaded convenience libraries if test -n "$dlprefiles"; then gentop=$output_objdir/${outputname}x func_append generated " $gentop" func_extract_archives $gentop $dlprefiles func_append oldobjs " $func_extract_archives_result" fi # POSIX demands no paths to be encoded in archives. We have # to avoid creating archives with duplicate basenames if we # might have to extract them afterwards, e.g., when creating a # static archive out of a convenience library, or when linking # the entirety of a libtool archive into another (currently # not supported by libtool). if (for obj in $oldobjs do func_basename "$obj" $ECHO "$func_basename_result" done | sort | sort -uc >/dev/null 2>&1); then : else echo "copying selected object files to avoid basename conflicts..." gentop=$output_objdir/${outputname}x func_append generated " $gentop" func_mkdir_p "$gentop" save_oldobjs=$oldobjs oldobjs= counter=1 for obj in $save_oldobjs do func_basename "$obj" objbase=$func_basename_result case " $oldobjs " in " ") oldobjs=$obj ;; *[\ /]"$objbase "*) while :; do # Make sure we don't pick an alternate name that also # overlaps. newobj=lt$counter-$objbase func_arith $counter + 1 counter=$func_arith_result case " $oldobjs " in *[\ /]"$newobj "*) ;; *) if test ! -f "$gentop/$newobj"; then break; fi ;; esac done func_show_eval "ln $obj $gentop/$newobj || cp $obj $gentop/$newobj" func_append oldobjs " $gentop/$newobj" ;; *) func_append oldobjs " $obj" ;; esac done fi func_to_tool_file "$oldlib" func_convert_file_msys_to_w32 tool_oldlib=$func_to_tool_file_result eval cmds=\"$old_archive_cmds\" func_len " $cmds" len=$func_len_result if test "$len" -lt "$max_cmd_len" || test "$max_cmd_len" -le -1; then cmds=$old_archive_cmds elif test -n "$archiver_list_spec"; then func_verbose "using command file archive linking..." for obj in $oldobjs do func_to_tool_file "$obj" $ECHO "$func_to_tool_file_result" done > $output_objdir/$libname.libcmd func_to_tool_file "$output_objdir/$libname.libcmd" oldobjs=" $archiver_list_spec$func_to_tool_file_result" cmds=$old_archive_cmds else # the command line is too long to link in one step, link in parts func_verbose "using piecewise archive linking..." save_RANLIB=$RANLIB RANLIB=: objlist= concat_cmds= save_oldobjs=$oldobjs oldobjs= # Is there a better way of finding the last object in the list? for obj in $save_oldobjs do last_oldobj=$obj done eval test_cmds=\"$old_archive_cmds\" func_len " $test_cmds" len0=$func_len_result len=$len0 for obj in $save_oldobjs do func_len " $obj" func_arith $len + $func_len_result len=$func_arith_result func_append objlist " $obj" if test "$len" -lt "$max_cmd_len"; then : else # the above command should be used before it gets too long oldobjs=$objlist if test "$obj" = "$last_oldobj"; then RANLIB=$save_RANLIB fi test -z "$concat_cmds" || concat_cmds=$concat_cmds~ eval concat_cmds=\"\$concat_cmds$old_archive_cmds\" objlist= len=$len0 fi done RANLIB=$save_RANLIB oldobjs=$objlist if test -z "$oldobjs"; then eval cmds=\"\$concat_cmds\" else eval cmds=\"\$concat_cmds~\$old_archive_cmds\" fi fi fi func_execute_cmds "$cmds" 'exit $?' done test -n "$generated" && \ func_show_eval "${RM}r$generated" # Now create the libtool archive. case $output in *.la) old_library= test yes = "$build_old_libs" && old_library=$libname.$libext func_verbose "creating $output" # Preserve any variables that may affect compiler behavior for var in $variables_saved_for_relink; do if eval test -z \"\${$var+set}\"; then relink_command="{ test -z \"\${$var+set}\" || $lt_unset $var || { $var=; export $var; }; }; $relink_command" elif eval var_value=\$$var; test -z "$var_value"; then relink_command="$var=; export $var; $relink_command" else func_quote_for_eval "$var_value" relink_command="$var=$func_quote_for_eval_result; export $var; $relink_command" fi done # Quote the link command for shipping. relink_command="(cd `pwd`; $SHELL \"$progpath\" $preserve_args --mode=relink $libtool_args @inst_prefix_dir@)" relink_command=`$ECHO "$relink_command" | $SED "$sed_quote_subst"` if test yes = "$hardcode_automatic"; then relink_command= fi # Only create the output if not a dry run. $opt_dry_run || { for installed in no yes; do if test yes = "$installed"; then if test -z "$install_libdir"; then break fi output=$output_objdir/${outputname}i # Replace all uninstalled libtool libraries with the installed ones newdependency_libs= for deplib in $dependency_libs; do case $deplib in *.la) func_basename "$deplib" name=$func_basename_result func_resolve_sysroot "$deplib" eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $func_resolve_sysroot_result` test -z "$libdir" && \ func_fatal_error "'$deplib' is not a valid libtool archive" func_append newdependency_libs " ${lt_sysroot:+=}$libdir/$name" ;; -L*) func_stripname -L '' "$deplib" func_replace_sysroot "$func_stripname_result" func_append newdependency_libs " -L$func_replace_sysroot_result" ;; -R*) func_stripname -R '' "$deplib" func_replace_sysroot "$func_stripname_result" func_append newdependency_libs " -R$func_replace_sysroot_result" ;; *) func_append newdependency_libs " $deplib" ;; esac done dependency_libs=$newdependency_libs newdlfiles= for lib in $dlfiles; do case $lib in *.la) func_basename "$lib" name=$func_basename_result eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $lib` test -z "$libdir" && \ func_fatal_error "'$lib' is not a valid libtool archive" func_append newdlfiles " ${lt_sysroot:+=}$libdir/$name" ;; *) func_append newdlfiles " $lib" ;; esac done dlfiles=$newdlfiles newdlprefiles= for lib in $dlprefiles; do case $lib in *.la) # Only pass preopened files to the pseudo-archive (for # eventual linking with the app. that links it) if we # didn't already link the preopened objects directly into # the library: func_basename "$lib" name=$func_basename_result eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $lib` test -z "$libdir" && \ func_fatal_error "'$lib' is not a valid libtool archive" func_append newdlprefiles " ${lt_sysroot:+=}$libdir/$name" ;; esac done dlprefiles=$newdlprefiles else newdlfiles= for lib in $dlfiles; do case $lib in [\\/]* | [A-Za-z]:[\\/]*) abs=$lib ;; *) abs=`pwd`"/$lib" ;; esac func_append newdlfiles " $abs" done dlfiles=$newdlfiles newdlprefiles= for lib in $dlprefiles; do case $lib in [\\/]* | [A-Za-z]:[\\/]*) abs=$lib ;; *) abs=`pwd`"/$lib" ;; esac func_append newdlprefiles " $abs" done dlprefiles=$newdlprefiles fi $RM $output # place dlname in correct position for cygwin # In fact, it would be nice if we could use this code for all target # systems that can't hard-code library paths into their executables # and that have no shared library path variable independent of PATH, # but it turns out we can't easily determine that from inspecting # libtool variables, so we have to hard-code the OSs to which it # applies here; at the moment, that means platforms that use the PE # object format with DLL files. See the long comment at the top of # tests/bindir.at for full details. tdlname=$dlname case $host,$output,$installed,$module,$dlname in *cygwin*,*lai,yes,no,*.dll | *mingw*,*lai,yes,no,*.dll | *cegcc*,*lai,yes,no,*.dll) # If a -bindir argument was supplied, place the dll there. if test -n "$bindir"; then func_relative_path "$install_libdir" "$bindir" tdlname=$func_relative_path_result/$dlname else # Otherwise fall back on heuristic. tdlname=../bin/$dlname fi ;; esac $ECHO > $output "\ # $outputname - a libtool library file # Generated by $PROGRAM (GNU $PACKAGE) $VERSION # # Please DO NOT delete this file! # It is necessary for linking the library. # The name that we can dlopen(3). dlname='$tdlname' # Names of this library. library_names='$library_names' # The name of the static archive. old_library='$old_library' # Linker flags that cannot go in dependency_libs. inherited_linker_flags='$new_inherited_linker_flags' # Libraries that this one depends upon. dependency_libs='$dependency_libs' # Names of additional weak libraries provided by this library weak_library_names='$weak_libs' # Version information for $libname. current=$current age=$age revision=$revision # Is this an already installed library? installed=$installed # Should we warn about portability when linking against -modules? shouldnotlink=$module # Files to dlopen/dlpreopen dlopen='$dlfiles' dlpreopen='$dlprefiles' # Directory that this library needs to be installed in: libdir='$install_libdir'" if test no,yes = "$installed,$need_relink"; then $ECHO >> $output "\ relink_command=\"$relink_command\"" fi done } # Do a symbolic link so that the libtool archive can be found in # LD_LIBRARY_PATH before the program is installed. func_show_eval '( cd "$output_objdir" && $RM "$outputname" && $LN_S "../$outputname" "$outputname" )' 'exit $?' ;; esac exit $EXIT_SUCCESS } if test link = "$opt_mode" || test relink = "$opt_mode"; then func_mode_link ${1+"$@"} fi # func_mode_uninstall arg... func_mode_uninstall () { $debug_cmd RM=$nonopt files= rmforce=false exit_status=0 # This variable tells wrapper scripts just to set variables rather # than running their programs. libtool_install_magic=$magic for arg do case $arg in -f) func_append RM " $arg"; rmforce=: ;; -*) func_append RM " $arg" ;; *) func_append files " $arg" ;; esac done test -z "$RM" && \ func_fatal_help "you must specify an RM program" rmdirs= for file in $files; do func_dirname "$file" "" "." dir=$func_dirname_result if test . = "$dir"; then odir=$objdir else odir=$dir/$objdir fi func_basename "$file" name=$func_basename_result test uninstall = "$opt_mode" && odir=$dir # Remember odir for removal later, being careful to avoid duplicates if test clean = "$opt_mode"; then case " $rmdirs " in *" $odir "*) ;; *) func_append rmdirs " $odir" ;; esac fi # Don't error if the file doesn't exist and rm -f was used. if { test -L "$file"; } >/dev/null 2>&1 || { test -h "$file"; } >/dev/null 2>&1 || test -f "$file"; then : elif test -d "$file"; then exit_status=1 continue elif $rmforce; then continue fi rmfiles=$file case $name in *.la) # Possibly a libtool archive, so verify it. if func_lalib_p "$file"; then func_source $dir/$name # Delete the libtool libraries and symlinks. for n in $library_names; do func_append rmfiles " $odir/$n" done test -n "$old_library" && func_append rmfiles " $odir/$old_library" case $opt_mode in clean) case " $library_names " in *" $dlname "*) ;; *) test -n "$dlname" && func_append rmfiles " $odir/$dlname" ;; esac test -n "$libdir" && func_append rmfiles " $odir/$name $odir/${name}i" ;; uninstall) if test -n "$library_names"; then # Do each command in the postuninstall commands. func_execute_cmds "$postuninstall_cmds" '$rmforce || exit_status=1' fi if test -n "$old_library"; then # Do each command in the old_postuninstall commands. func_execute_cmds "$old_postuninstall_cmds" '$rmforce || exit_status=1' fi # FIXME: should reinstall the best remaining shared library. ;; esac fi ;; *.lo) # Possibly a libtool object, so verify it. if func_lalib_p "$file"; then # Read the .lo file func_source $dir/$name # Add PIC object to the list of files to remove. if test -n "$pic_object" && test none != "$pic_object"; then func_append rmfiles " $dir/$pic_object" fi # Add non-PIC object to the list of files to remove. if test -n "$non_pic_object" && test none != "$non_pic_object"; then func_append rmfiles " $dir/$non_pic_object" fi fi ;; *) if test clean = "$opt_mode"; then noexename=$name case $file in *.exe) func_stripname '' '.exe' "$file" file=$func_stripname_result func_stripname '' '.exe' "$name" noexename=$func_stripname_result # $file with .exe has already been added to rmfiles, # add $file without .exe func_append rmfiles " $file" ;; esac # Do a test to see if this is a libtool program. if func_ltwrapper_p "$file"; then if func_ltwrapper_executable_p "$file"; then func_ltwrapper_scriptname "$file" relink_command= func_source $func_ltwrapper_scriptname_result func_append rmfiles " $func_ltwrapper_scriptname_result" else relink_command= func_source $dir/$noexename fi # note $name still contains .exe if it was in $file originally # as does the version of $file that was added into $rmfiles func_append rmfiles " $odir/$name $odir/${name}S.$objext" if test yes = "$fast_install" && test -n "$relink_command"; then func_append rmfiles " $odir/lt-$name" fi if test "X$noexename" != "X$name"; then func_append rmfiles " $odir/lt-$noexename.c" fi fi fi ;; esac func_show_eval "$RM $rmfiles" 'exit_status=1' done # Try to remove the $objdir's in the directories where we deleted files for dir in $rmdirs; do if test -d "$dir"; then func_show_eval "rmdir $dir >/dev/null 2>&1" fi done exit $exit_status } if test uninstall = "$opt_mode" || test clean = "$opt_mode"; then func_mode_uninstall ${1+"$@"} fi test -z "$opt_mode" && { help=$generic_help func_fatal_help "you must specify a MODE" } test -z "$exec_cmd" && \ func_fatal_help "invalid operation mode '$opt_mode'" if test -n "$exec_cmd"; then eval exec "$exec_cmd" exit $EXIT_FAILURE fi exit $exit_status # The TAGs below are defined such that we never get into a situation # where we disable both kinds of libraries. Given conflicting # choices, we go for a static library, that is the most portable, # since we can't tell whether shared libraries were disabled because # the user asked for that or because the platform doesn't support # them. This is particularly important on AIX, because we don't # support having both static and shared libraries enabled at the same # time on that platform, so we default to a shared-only configuration. # If a disable-shared tag is given, we'll fallback to a static-only # configuration. But we'll never go from static-only to shared-only. # ### BEGIN LIBTOOL TAG CONFIG: disable-shared build_libtool_libs=no build_old_libs=yes # ### END LIBTOOL TAG CONFIG: disable-shared # ### BEGIN LIBTOOL TAG CONFIG: disable-static build_old_libs=`case $build_libtool_libs in yes) echo no;; *) echo yes;; esac` # ### END LIBTOOL TAG CONFIG: disable-static # Local Variables: # mode:shell-script # sh-indentation:2 # End: capnproto-c++-0.8.0/build-aux/missing0000755000175000017500000001533613650317755020265 0ustar00kentonkenton00000000000000#! /bin/sh # Common wrapper for a few potentially missing GNU programs. scriptversion=2018-03-07.03; # UTC # Copyright (C) 1996-2018 Free Software Foundation, Inc. # Originally written by Fran,cois Pinard , 1996. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # As a special exception to the GNU General Public License, if you # distribute this file as part of a program that contains a # configuration script generated by Autoconf, you may include it under # the same distribution terms that you use for the rest of that program. if test $# -eq 0; then echo 1>&2 "Try '$0 --help' for more information" exit 1 fi case $1 in --is-lightweight) # Used by our autoconf macros to check whether the available missing # script is modern enough. exit 0 ;; --run) # Back-compat with the calling convention used by older automake. shift ;; -h|--h|--he|--hel|--help) echo "\ $0 [OPTION]... PROGRAM [ARGUMENT]... Run 'PROGRAM [ARGUMENT]...', returning a proper advice when this fails due to PROGRAM being missing or too old. Options: -h, --help display this help and exit -v, --version output version information and exit Supported PROGRAM values: aclocal autoconf autoheader autom4te automake makeinfo bison yacc flex lex help2man Version suffixes to PROGRAM as well as the prefixes 'gnu-', 'gnu', and 'g' are ignored when checking the name. Send bug reports to ." exit $? ;; -v|--v|--ve|--ver|--vers|--versi|--versio|--version) echo "missing $scriptversion (GNU Automake)" exit $? ;; -*) echo 1>&2 "$0: unknown '$1' option" echo 1>&2 "Try '$0 --help' for more information" exit 1 ;; esac # Run the given program, remember its exit status. "$@"; st=$? # If it succeeded, we are done. test $st -eq 0 && exit 0 # Also exit now if we it failed (or wasn't found), and '--version' was # passed; such an option is passed most likely to detect whether the # program is present and works. case $2 in --version|--help) exit $st;; esac # Exit code 63 means version mismatch. This often happens when the user # tries to use an ancient version of a tool on a file that requires a # minimum version. if test $st -eq 63; then msg="probably too old" elif test $st -eq 127; then # Program was missing. msg="missing on your system" else # Program was found and executed, but failed. Give up. exit $st fi perl_URL=https://www.perl.org/ flex_URL=https://github.com/westes/flex gnu_software_URL=https://www.gnu.org/software program_details () { case $1 in aclocal|automake) echo "The '$1' program is part of the GNU Automake package:" echo "<$gnu_software_URL/automake>" echo "It also requires GNU Autoconf, GNU m4 and Perl in order to run:" echo "<$gnu_software_URL/autoconf>" echo "<$gnu_software_URL/m4/>" echo "<$perl_URL>" ;; autoconf|autom4te|autoheader) echo "The '$1' program is part of the GNU Autoconf package:" echo "<$gnu_software_URL/autoconf/>" echo "It also requires GNU m4 and Perl in order to run:" echo "<$gnu_software_URL/m4/>" echo "<$perl_URL>" ;; esac } give_advice () { # Normalize program name to check for. normalized_program=`echo "$1" | sed ' s/^gnu-//; t s/^gnu//; t s/^g//; t'` printf '%s\n' "'$1' is $msg." configure_deps="'configure.ac' or m4 files included by 'configure.ac'" case $normalized_program in autoconf*) echo "You should only need it if you modified 'configure.ac'," echo "or m4 files included by it." program_details 'autoconf' ;; autoheader*) echo "You should only need it if you modified 'acconfig.h' or" echo "$configure_deps." program_details 'autoheader' ;; automake*) echo "You should only need it if you modified 'Makefile.am' or" echo "$configure_deps." program_details 'automake' ;; aclocal*) echo "You should only need it if you modified 'acinclude.m4' or" echo "$configure_deps." program_details 'aclocal' ;; autom4te*) echo "You might have modified some maintainer files that require" echo "the 'autom4te' program to be rebuilt." program_details 'autom4te' ;; bison*|yacc*) echo "You should only need it if you modified a '.y' file." echo "You may want to install the GNU Bison package:" echo "<$gnu_software_URL/bison/>" ;; lex*|flex*) echo "You should only need it if you modified a '.l' file." echo "You may want to install the Fast Lexical Analyzer package:" echo "<$flex_URL>" ;; help2man*) echo "You should only need it if you modified a dependency" \ "of a man page." echo "You may want to install the GNU Help2man package:" echo "<$gnu_software_URL/help2man/>" ;; makeinfo*) echo "You should only need it if you modified a '.texi' file, or" echo "any other file indirectly affecting the aspect of the manual." echo "You might want to install the Texinfo package:" echo "<$gnu_software_URL/texinfo/>" echo "The spurious makeinfo call might also be the consequence of" echo "using a buggy 'make' (AIX, DU, IRIX), in which case you might" echo "want to install GNU make:" echo "<$gnu_software_URL/make/>" ;; *) echo "You might have modified some files without having the proper" echo "tools for further handling them. Check the 'README' file, it" echo "often tells you about the needed prerequisites for installing" echo "this package. You may also peek at any GNU archive site, in" echo "case some other package contains this missing '$1' program." ;; esac } give_advice "$1" | sed -e '1s/^/WARNING: /' \ -e '2,$s/^/ /' >&2 # Propagate the correct exit status (expected to be 127 for a program # not found, 63 for a program that failed due to version mismatch). exit $st # Local variables: # eval: (add-hook 'before-save-hook 'time-stamp) # time-stamp-start: "scriptversion=" # time-stamp-format: "%:y-%02m-%02d.%02H" # time-stamp-time-zone: "UTC0" # time-stamp-end: "; # UTC" # End: capnproto-c++-0.8.0/cmake/0000755000175000017500000000000013650320027016026 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/cmake/CapnProtoConfig.cmake.in0000644000175000017500000000573013650101756022503 0ustar00kentonkenton00000000000000# Cap'n Proto CMake Package Configuration # # When configured and installed, this file enables client projects to find Cap'n Proto using # CMake's find_package() command. It adds imported targets in the CapnProto:: namespace, such as # CapnProto::kj, CapnProto::capnp, etc. (one target for each file in pkgconfig/*.pc.in), defines # the capnp_generate_cpp() function, and exposes some variables for compatibility with the original # FindCapnProto.cmake module. # # Example usage: # find_package(CapnProto) # capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp) # add_executable(foo main.cpp ${CAPNP_SRCS}) # target_link_libraries(foo PRIVATE CapnProto::capnp) # target_include_directories(foo PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # # If you are using RPC features, use 'CapnProto::capnp-rpc' in the target_link_libraries() call. # # Paths to `capnp` and `capnpc-c++` are exposed in the following variables: # CAPNP_EXECUTABLE # Path to the `capnp` tool (can be set to override). # CAPNPC_CXX_EXECUTABLE # Path to the `capnpc-c++` tool (can be set to override). # # For FindCapnProto.cmake compatibility, the following variables are also provided. Please prefer # using the imported targets in new CMake code. # CAPNP_INCLUDE_DIRS # Include directories for the library's headers. # CANP_LIBRARIES # The Cap'n Proto library paths. # CAPNP_LIBRARIES_LITE # Paths to only the 'lite' libraries. # CAPNP_DEFINITIONS # Compiler definitions required for building with the library. # CAPNP_FOUND # Set if the libraries have been located (prefer using CapnProto_FOUND in new code). # @PACKAGE_INIT@ set(CapnProto_VERSION @VERSION@) set(CAPNP_EXECUTABLE $ CACHE FILEPATH "Location of capnp executable") set(CAPNPC_CXX_EXECUTABLE $ CACHE FILEPATH "Location of capnpc-c++ executable") set(CAPNP_INCLUDE_DIRECTORY "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@") # work around http://public.kitware.com/Bug/view.php?id=15258 if(NOT _IMPORT_PREFIX) set(_IMPORT_PREFIX ${PACKAGE_PREFIX_DIR}) endif() include("${CMAKE_CURRENT_LIST_DIR}/CapnProtoTargets.cmake") include("${CMAKE_CURRENT_LIST_DIR}/CapnProtoMacros.cmake") # FindCapnProto.cmake provides dependency information via several CAPNP_-prefixed variables. New # code should not rely on these variables, but prefer linking directly to the imported targets we # now provide. However, we should still set these variables to ease the transition for projects # which currently depend on the find-module. set(CAPNP_INCLUDE_DIRS ${CAPNP_INCLUDE_DIRECTORY}) # No need to list all libraries, just the leaves of the dependency tree. set(CAPNP_LIBRARIES_LITE CapnProto::capnp) set(CAPNP_LIBRARIES CapnProto::capnp-rpc CapnProto::capnp-json CapnProto::kj-http) set(CAPNP_DEFINITIONS) if(TARGET CapnProto::capnp AND NOT TARGET CapnProto::capnp-rpc) set(CAPNP_DEFINITIONS -DCAPNP_LITE) endif() set(CAPNP_FOUND ${CapnProto_FOUND}) capnproto-c++-0.8.0/cmake/CapnProtoConfigVersion.cmake.in0000644000175000017500000000313013340402540024030 0ustar00kentonkenton00000000000000# This is a copy of /usr/share/cmake-3.5/Modules/BasicConfigVersion-AnyNewerVersion.cmake.in, with # the following change: # - @CVF_VERSION renamed to @PACKAGE_VERSION@. Autoconf defines a PACKAGE_VERSION # output variable for us, so might as well take advantage of that. # This is a basic version file for the Config-mode of find_package(). # It is used by write_basic_package_version_file() as input file for configure_file() # to create a version-file which can be installed along a config.cmake file. # # The created file sets PACKAGE_VERSION_EXACT if the current version string and # the requested version string are exactly the same and it sets # PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version. # The variable PACKAGE_VERSION must be set before calling configure_file(). set(PACKAGE_VERSION "@PACKAGE_VERSION@") if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) set(PACKAGE_VERSION_COMPATIBLE FALSE) else() set(PACKAGE_VERSION_COMPATIBLE TRUE) if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) set(PACKAGE_VERSION_EXACT TRUE) endif() endif() # if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it: if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "@CMAKE_SIZEOF_VOID_P@" STREQUAL "") return() endif() # check that the installed version has the same 32/64bit-ness as the one which is currently searching: if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "@CMAKE_SIZEOF_VOID_P@") math(EXPR installedBits "@CMAKE_SIZEOF_VOID_P@ * 8") set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)") set(PACKAGE_VERSION_UNSUITABLE TRUE) endif() capnproto-c++-0.8.0/cmake/CapnProtoMacros.cmake0000644000175000017500000001200113650101756022102 0ustar00kentonkenton00000000000000# CAPNP_GENERATE_CPP =========================================================== # # Example usage: # find_package(CapnProto) # capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp) # add_executable(foo main.cpp ${CAPNP_SRCS}) # target_link_libraries(foo PRIVATE CapnProto::capnp-rpc) # target_include_directories(foo PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # # If you are not using the RPC features you can use 'CapnProto::capnp' in the # target_link_libraries call # # Configuration variables (optional): # CAPNPC_OUTPUT_DIR # Directory to place compiled schema sources (default: CMAKE_CURRENT_BINARY_DIR). # CAPNPC_IMPORT_DIRS # List of additional include directories for the schema compiler. # (CAPNPC_SRC_PREFIX and CAPNP_INCLUDE_DIRECTORY are always included.) # CAPNPC_SRC_PREFIX # Schema file source prefix (default: CMAKE_CURRENT_SOURCE_DIR). # CAPNPC_FLAGS # Additional flags to pass to the schema compiler. # # TODO: convert to cmake_parse_arguments function(CAPNP_GENERATE_CPP SOURCES HEADERS) if(NOT ARGN) message(SEND_ERROR "CAPNP_GENERATE_CPP() called without any source files.") endif() set(tool_depends ${EMPTY_STRING}) #Use cmake targets available if(TARGET capnp_tool) if(NOT CAPNP_EXECUTABLE) set(CAPNP_EXECUTABLE $) endif() if(NOT CAPNPC_CXX_EXECUTABLE) get_target_property(CAPNPC_CXX_EXECUTABLE capnpc_cpp CAPNPC_CXX_EXECUTABLE) endif() if(NOT CAPNP_INCLUDE_DIRECTORY) get_target_property(CAPNP_INCLUDE_DIRECTORY capnp_tool CAPNP_INCLUDE_DIRECTORY) endif() list(APPEND tool_depends capnp_tool capnpc_cpp) endif() if(NOT CAPNP_EXECUTABLE) message(SEND_ERROR "Could not locate capnp executable (CAPNP_EXECUTABLE).") endif() if(NOT CAPNPC_CXX_EXECUTABLE) message(SEND_ERROR "Could not locate capnpc-c++ executable (CAPNPC_CXX_EXECUTABLE).") endif() if(NOT CAPNP_INCLUDE_DIRECTORY) message(SEND_ERROR "Could not locate capnp header files (CAPNP_INCLUDE_DIRECTORY).") endif() if(DEFINED CAPNPC_OUTPUT_DIR) # Prepend a ':' to get the format for the '-o' flag right set(output_dir ":${CAPNPC_OUTPUT_DIR}") else() set(output_dir ":.") endif() if(NOT DEFINED CAPNPC_SRC_PREFIX) set(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}") endif() get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE) # Default compiler includes. Note that in capnp's own test usage of capnp_generate_cpp(), these # two variables will end up evaluating to the same directory. However, it's difficult to # deduplicate them because if CAPNP_INCLUDE_DIRECTORY came from the capnp_tool target property, # then it must be a generator expression in order to handle usages in both the build tree and the # install tree. This vastly overcomplicates duplication detection, so the duplication doesn't seem # worth fixing. set(include_path -I "${CAPNPC_SRC_PREFIX}" -I "${CAPNP_INCLUDE_DIRECTORY}") if(DEFINED CAPNPC_IMPORT_DIRS) # Append each directory as a series of '-I' flags in ${include_path} foreach(directory ${CAPNPC_IMPORT_DIRS}) get_filename_component(absolute_path "${directory}" ABSOLUTE) list(APPEND include_path -I "${absolute_path}") endforeach() endif() set(${SOURCES}) set(${HEADERS}) foreach(schema_file ${ARGN}) get_filename_component(file_path "${schema_file}" ABSOLUTE) get_filename_component(file_dir "${file_path}" PATH) if(NOT EXISTS "${file_path}") message(FATAL_ERROR "Cap'n Proto schema file '${file_path}' does not exist!") endif() # Figure out where the output files will go if (NOT DEFINED CAPNPC_OUTPUT_DIR) set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/") endif() # Output files are placed in CAPNPC_OUTPUT_DIR, at a location as if they were # relative to CAPNPC_SRC_PREFIX. string(LENGTH "${CAPNPC_SRC_PREFIX}" prefix_len) string(SUBSTRING "${file_path}" 0 ${prefix_len} output_prefix) if(NOT "${CAPNPC_SRC_PREFIX}" STREQUAL "${output_prefix}") message(SEND_ERROR "Could not determine output path for '${schema_file}' ('${file_path}') with source prefix '${CAPNPC_SRC_PREFIX}' into '${CAPNPC_OUTPUT_DIR}'.") endif() string(SUBSTRING "${file_path}" ${prefix_len} -1 output_path) set(output_base "${CAPNPC_OUTPUT_DIR}${output_path}") add_custom_command( OUTPUT "${output_base}.c++" "${output_base}.h" COMMAND "${CAPNP_EXECUTABLE}" ARGS compile -o ${CAPNPC_CXX_EXECUTABLE}${output_dir} --src-prefix ${CAPNPC_SRC_PREFIX} ${include_path} ${CAPNPC_FLAGS} ${file_path} DEPENDS "${schema_file}" ${tool_depends} COMMENT "Compiling Cap'n Proto schema ${schema_file}" VERBATIM ) list(APPEND ${SOURCES} "${output_base}.c++") list(APPEND ${HEADERS} "${output_base}.h") endforeach() set_source_files_properties(${${SOURCES}} ${${HEADERS}} PROPERTIES GENERATED TRUE) set(${SOURCES} ${${SOURCES}} PARENT_SCOPE) set(${HEADERS} ${${HEADERS}} PARENT_SCOPE) endfunction() capnproto-c++-0.8.0/cmake/CapnProtoTargets.cmake0000644000175000017500000002216013340402540022265 0ustar00kentonkenton00000000000000# This CMake script adds imported targets for each shared library and executable distributed by # Cap'n Proto's autotools build. # # This file IS NOT USED by the CMake build! The CMake build generates its own version of this script # from its set of exported targets. I used such a generated script as a reference when writing this # one. # # The set of library targets provided by this script is automatically generated from the list of .pc # files maintained in configure.ac. The set of executable targets is hard-coded in this file. # # You can request that this script print debugging information by invoking cmake with: # # -DCapnProto_DEBUG=ON # # TODO(someday): Distinguish between debug and release builds. I.e., set IMPORTED_LOCATION_RELEASE # rather than IMPORTED_LOCATION, etc., if this installation was configured as a release build. But # how do we tell? grep for -g in CXXFLAGS? if(CMAKE_VERSION VERSION_LESS 3.1) message(FATAL_ERROR "CMake >= 3.1 required") endif() set(forwarded_config_flags) if(CapnProto_FIND_QUIETLY) list(APPEND forwarded_config_flags QUIET) endif() if(CapnProto_FIND_REQUIRED) list(APPEND forwarded_config_flags REQUIRED) endif() # If the consuming project called find_package(CapnProto) with the QUIET or REQUIRED flags, forward # them to calls to find_package(PkgConfig) and pkg_check_modules(). Note that find_dependency() # would do this for us in the former case, but there is no such forwarding wrapper for # pkg_check_modules(). find_package(PkgConfig ${forwarded_config_flags}) if(NOT ${PkgConfig_FOUND}) # If we're here, the REQUIRED flag must not have been passed, else we would have had a fatal # error. Nevertheless, a diagnostic for this case is probably nice. if(NOT CapnProto_FIND_QUIETLY) message(WARNING "pkg-config cannot be found") endif() set(CapnProto_FOUND OFF) return() endif() function(_capnp_import_pkg_config_target target) # Add an imported library target named CapnProto::${target}, using the output of various # invocations of `pkg-config ${target}`. The generated imported library target tries to mimic the # behavior of a real CMake-generated imported target as closely as possible. # # Usage: _capnp_import_pkg_config_target(target ) set(all_targets ${ARGN}) pkg_check_modules(${target} ${forwarded_config_flags} ${target}) if(NOT ${${target}_FOUND}) if(NOT CapnProto_FIND_QUIETLY) message(WARNING "CapnProtoConfig.cmake was configured to search for ${target}.pc, but pkg-config cannot find it. Ignoring this target.") endif() return() endif() if(CapnProto_DEBUG) # Dump the information pkg-config discovered. foreach(var VERSION LIBRARY_DIRS LIBRARIES LDFLAGS_OTHER INCLUDE_DIRS CFLAGS_OTHER) message(STATUS "${target}_${var} = ${${target}_${var}}") endforeach() endif() if(NOT ${${target}_VERSION} VERSION_EQUAL ${CapnProto_VERSION}) if(NOT CapnProto_FIND_QUIETLY) message(WARNING "CapnProtoConfig.cmake was configured to search for version ${CapnProto_VERSION}, but ${target} version ${${target}_VERSION} was found. Ignoring this target.") endif() return() endif() # Make an educated guess as to what the target's .so and .a filenames must be. set(target_name_shared ${CMAKE_SHARED_LIBRARY_PREFIX}${target}-${CapnProto_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}) set(target_name_static ${CMAKE_STATIC_LIBRARY_PREFIX}${target}${CMAKE_STATIC_LIBRARY_SUFFIX}) # Find the actual target's file. find_library() sets a cache variable, so I made the variable name # unique-ish. find_library(CapnProto_${target}_IMPORTED_LOCATION NAMES ${target_name_shared} ${target_name_static} # prefer libfoo-version.so over libfoo.a PATHS ${${target}_LIBRARY_DIRS} NO_DEFAULT_PATH ) # If the installed version of Cap'n Proto is in a system location, pkg-config will not have filled # in ${target}_LIBRARY_DIRS. To account for this, fall back to a regular search. find_library(CapnProto_${target}_IMPORTED_LOCATION NAMES ${target_name_shared} ${target_name_static} # prefer libfoo-version.so over libfoo.a ) if(NOT CapnProto_${target}_IMPORTED_LOCATION) # Not an error if the library doesn't exist -- we may have found a lite mode installation. if(CapnProto_DEBUG) message(STATUS "${target} library does not exist") endif() return() endif() # Record some information about this target -- shared versus static, location and soname -- which # we'll use to build our imported target later. set(target_location ${CapnProto_${target}_IMPORTED_LOCATION}) get_filename_component(target_name "${target_location}" NAME) set(target_type STATIC) set(imported_soname_property) if(target_name STREQUAL ${target_name_shared}) set(target_type SHARED) set(imported_soname_property IMPORTED_SONAME ${target_name}) endif() # Each library dependency of the target is either the target itself, a sibling Cap'n Proto # library, or a system library. We ignore the first case by removing this target from the # dependencies. The remaining dependencies are either passed through or, if they are a sibling # Cap'n Proto library, prefixed with `CapnProto::`. set(dependencies ${${target}_LIBRARIES}) list(REMOVE_ITEM dependencies ${target}) set(target_interface_libs) foreach(dependency ${dependencies}) list(FIND all_targets ${dependency} target_index) # TODO(cleanup): CMake >= 3.3 lets us write: `if(NOT ${dependency} IN_LIST all_targets)` if(target_index EQUAL -1) list(APPEND target_interface_libs ${dependency}) else() list(APPEND target_interface_libs CapnProto::${dependency}) endif() endforeach() add_library(CapnProto::${target} ${target_type} IMPORTED) set_target_properties(CapnProto::${target} PROPERTIES ${imported_soname_property} IMPORTED_LOCATION "${target_location}" # TODO(cleanup): Use cxx_std_14 once it's safe to require cmake 3.8. INTERFACE_COMPILE_FEATURES "cxx_generic_lambdas" INTERFACE_COMPILE_OPTIONS "${${target}_CFLAGS_OTHER}" INTERFACE_INCLUDE_DIRECTORIES "${${target}_INCLUDE_DIRS}" # I'm dumping LDFLAGS_OTHER in with the libraries because there exists no # INTERFACE_LINK_OPTIONS. See https://gitlab.kitware.com/cmake/cmake/issues/16543. INTERFACE_LINK_LIBRARIES "${target_interface_libs};${${target}_LDFLAGS_OTHER}" ) if(CapnProto_DEBUG) # Dump all the properties we generated for the imported target. foreach(prop IMPORTED_LOCATION IMPORTED_SONAME INTERFACE_COMPILE_FEATURES INTERFACE_COMPILE_OPTIONS INTERFACE_INCLUDE_DIRECTORIES INTERFACE_LINK_LIBRARIES) get_target_property(value CapnProto::${target} ${prop}) message(STATUS "CapnProto::${target} ${prop} = ${value}") endforeach() endif() endfunction() # ======================================================================================== # Imported library targets # Build a list of targets to search for from the list of .pc files. # I.e. [somewhere/foo.pc, somewhere/bar.pc] -> [foo, bar] set(library_targets) foreach(filename ${CAPNP_PKG_CONFIG_FILES}) get_filename_component(target ${filename} NAME_WE) list(APPEND library_targets ${target}) endforeach() # Try to add an imported library target CapnProto::foo for each foo.pc distributed with Cap'n Proto. foreach(target ${library_targets}) _capnp_import_pkg_config_target(${target} ${library_targets}) endforeach() # Handle lite-mode and no libraries found cases. It is tempting to set a CapnProto_LITE variable # here, but the real CMake-generated implementation does no such thing -- we'd need to set it in # CapnProtoConfig.cmake.in itself. if(TARGET CapnProto::capnp AND TARGET CapnProto::kj) if(NOT TARGET CapnProto::capnp-rpc) if(NOT CapnProto_FIND_QUIETLY) message(STATUS "Found an installation of Cap'n Proto lite. Executable and library targets beyond libkj and libcapnp will be unavailable.") endif() # Lite mode doesn't include the executables, so return here. return() endif() else() # If we didn't even find capnp or kj, then we didn't find anything usable. set(CapnProto_FOUND OFF) return() endif() # ======================================================================================== # Imported executable targets get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) # Add executable targets for the capnp compiler and plugins. This list must be kept manually in sync # with the rest of the project. add_executable(CapnProto::capnp_tool IMPORTED) set_target_properties(CapnProto::capnp_tool PROPERTIES IMPORTED_LOCATION "${_IMPORT_PREFIX}/bin/capnp${CMAKE_EXECUTABLE_SUFFIX}" ) add_executable(CapnProto::capnpc_cpp IMPORTED) set_target_properties(CapnProto::capnpc_cpp PROPERTIES IMPORTED_LOCATION "${_IMPORT_PREFIX}/bin/capnpc-c++${CMAKE_EXECUTABLE_SUFFIX}" ) add_executable(CapnProto::capnpc_capnp IMPORTED) set_target_properties(CapnProto::capnpc_capnp PROPERTIES IMPORTED_LOCATION "${_IMPORT_PREFIX}/bin/capnpc-capnp${CMAKE_EXECUTABLE_SUFFIX}" ) capnproto-c++-0.8.0/m4/0000755000175000017500000000000013650320027015266 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/m4/acx_pthread.m40000644000175000017500000003523013340402540020012 0ustar00kentonkenton00000000000000# This file was copied to Cap'n Proto from the Protocol Buffers distribution, # version 2.3.0. # This was retrieved from # http://svn.0pointer.de/viewvc/trunk/common/acx_pthread.m4?revision=1277&root=avahi # See also (perhaps for new versions?) # http://svn.0pointer.de/viewvc/trunk/common/acx_pthread.m4?root=avahi # # We've rewritten the inconsistency check code (from avahi), to work # more broadly. In particular, it no longer assumes ld accepts -zdefs. # This caused a restructing of the code, but the functionality has only # changed a little. dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) dnl dnl @summary figure out how to build C programs using POSIX threads dnl dnl This macro figures out how to build C programs using POSIX threads. dnl It sets the PTHREAD_LIBS output variable to the threads library and dnl linker flags, and the PTHREAD_CFLAGS output variable to any special dnl C compiler flags that are needed. (The user can also force certain dnl compiler flags/libs to be tested by setting these environment dnl variables.) dnl dnl Also sets PTHREAD_CC to any special C compiler that is needed for dnl multi-threaded programs (defaults to the value of CC otherwise). dnl (This is necessary on AIX to use the special cc_r compiler alias.) dnl dnl NOTE: You are assumed to not only compile your program with these dnl flags, but also link it with them as well. e.g. you should link dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS dnl $LIBS dnl dnl If you are only building threads programs, you may wish to use dnl these variables in your default LIBS, CFLAGS, and CC: dnl dnl LIBS="$PTHREAD_LIBS $LIBS" dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS" dnl CC="$PTHREAD_CC" dnl dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). dnl dnl ACTION-IF-FOUND is a list of shell commands to run if a threads dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the dnl default action will define HAVE_PTHREAD. dnl dnl Please let the authors know if this macro fails on any platform, or dnl if you have any other suggestions or comments. This macro was based dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros dnl posted by Alejandro Forero Cuervo to the autoconf macro repository. dnl We are also grateful for the helpful feedback of numerous users. dnl dnl @category InstalledPackages dnl @author Steven G. Johnson dnl @version 2006-05-29 dnl @license GPLWithACException dnl dnl Checks for GCC shared/pthread inconsistency based on work by dnl Marcin Owsiany AC_DEFUN([ACX_PTHREAD], [ AC_REQUIRE([AC_CANONICAL_HOST]) AC_LANG_SAVE AC_LANG_C acx_pthread_ok=no # We used to check for pthread.h first, but this fails if pthread.h # requires special compiler flags (e.g. on True64 or Sequent). # It gets checked for in the link test anyway. # First of all, check if the user has set any of the PTHREAD_LIBS, # etcetera environment variables, and if threads linking works using # them: if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then save_CFLAGS="$CFLAGS" CFLAGS="$CFLAGS $PTHREAD_CFLAGS" save_LIBS="$LIBS" LIBS="$PTHREAD_LIBS $LIBS" AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes) AC_MSG_RESULT($acx_pthread_ok) if test x"$acx_pthread_ok" = xno; then PTHREAD_LIBS="" PTHREAD_CFLAGS="" fi LIBS="$save_LIBS" CFLAGS="$save_CFLAGS" fi # We must check for the threads library under a number of different # names; the ordering is very important because some systems # (e.g. DEC) have both -lpthread and -lpthreads, where one of the # libraries is broken (non-POSIX). # Create a list of thread flags to try. Items starting with a "-" are # C compiler flags, and other items are library names, except for "none" # which indicates that we try without any flags at all, and "pthread-config" # which is a program returning the flags for the Pth emulation library. acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" # The ordering *is* (sometimes) important. Some notes on the # individual items follow: # pthreads: AIX (must check this before -lpthread) # none: in case threads are in libc; should be tried before -Kthread and # other compiler flags to prevent continual compiler warnings # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) # -pthreads: Solaris/gcc # -mthreads: Mingw32/gcc, Lynx/gcc # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it # doesn't hurt to check since this sometimes defines pthreads too; # also defines -D_REENTRANT) # ... -mt is also the pthreads flag for HP/aCC # pthread: Linux, etcetera # --thread-safe: KAI C++ # pthread-config: use pthread-config program (for GNU Pth library) case "${host_cpu}-${host_os}" in *solaris*) # On Solaris (at least, for some versions), libc contains stubbed # (non-functional) versions of the pthreads routines, so link-based # tests will erroneously succeed. (We need to link with -pthreads/-mt/ # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather # a function called by this macro, so we could check for that, but # who knows whether they'll stub that too in a future libc.) So, # we'll just look for -pthreads and -lpthread first: acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags" ;; esac if test x"$acx_pthread_ok" = xno; then for flag in $acx_pthread_flags; do case $flag in none) AC_MSG_CHECKING([whether pthreads work without any flags]) ;; -*) AC_MSG_CHECKING([whether pthreads work with $flag]) PTHREAD_CFLAGS="$flag" ;; pthread-config) AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no) if test x"$acx_pthread_config" = xno; then continue; fi PTHREAD_CFLAGS="`pthread-config --cflags`" PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" ;; *) AC_MSG_CHECKING([for the pthreads library -l$flag]) PTHREAD_LIBS="-l$flag" ;; esac save_LIBS="$LIBS" save_CFLAGS="$CFLAGS" LIBS="$PTHREAD_LIBS $LIBS" CFLAGS="$CFLAGS $PTHREAD_CFLAGS" # Check for various functions. We must include pthread.h, # since some functions may be macros. (On the Sequent, we # need a special flag -Kthread to make this header compile.) # We check for pthread_join because it is in -lpthread on IRIX # while pthread_create is in libc. We check for pthread_attr_init # due to DEC craziness with -lpthreads. We check for # pthread_cleanup_push because it is one of the few pthread # functions on Solaris that doesn't have a non-functional libc stub. # We try pthread_create on general principles. AC_TRY_LINK([#include ], [pthread_t th; pthread_join(th, 0); pthread_attr_init(0); pthread_cleanup_push(0, 0); pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], [acx_pthread_ok=yes]) LIBS="$save_LIBS" CFLAGS="$save_CFLAGS" AC_MSG_RESULT($acx_pthread_ok) if test "x$acx_pthread_ok" = xyes; then break; fi PTHREAD_LIBS="" PTHREAD_CFLAGS="" done fi # Various other checks: if test "x$acx_pthread_ok" = xyes; then save_LIBS="$LIBS" LIBS="$PTHREAD_LIBS $LIBS" save_CFLAGS="$CFLAGS" CFLAGS="$CFLAGS $PTHREAD_CFLAGS" # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. AC_MSG_CHECKING([for joinable pthread attribute]) attr_name=unknown for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do AC_TRY_LINK([#include ], [int attr=$attr; return attr;], [attr_name=$attr; break]) done AC_MSG_RESULT($attr_name) if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name, [Define to necessary symbol if this constant uses a non-standard name on your system.]) fi AC_MSG_CHECKING([if more special flags are required for pthreads]) flag=no case "${host_cpu}-${host_os}" in *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";; *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; esac AC_MSG_RESULT(${flag}) if test "x$flag" != xno; then PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" fi LIBS="$save_LIBS" CFLAGS="$save_CFLAGS" # More AIX lossage: must compile with xlc_r or cc_r if test x"$GCC" != xyes; then AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC}) else PTHREAD_CC=$CC fi # The next part tries to detect GCC inconsistency with -shared on some # architectures and systems. The problem is that in certain # configurations, when -shared is specified, GCC "forgets" to # internally use various flags which are still necessary. # # Prepare the flags # save_CFLAGS="$CFLAGS" save_LIBS="$LIBS" save_CC="$CC" # Try with the flags determined by the earlier checks. # # -Wl,-z,defs forces link-time symbol resolution, so that the # linking checks with -shared actually have any value # # FIXME: -fPIC is required for -shared on many architectures, # so we specify it here, but the right way would probably be to # properly detect whether it is actually required. CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS" LIBS="$PTHREAD_LIBS $LIBS" CC="$PTHREAD_CC" # In order not to create several levels of indentation, we test # the value of "$done" until we find the cure or run out of ideas. done="no" # First, make sure the CFLAGS we added are actually accepted by our # compiler. If not (and OS X's ld, for instance, does not accept -z), # then we can't do this test. if test x"$done" = xno; then AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies]) AC_TRY_LINK(,, , [done=yes]) if test "x$done" = xyes ; then AC_MSG_RESULT([no]) else AC_MSG_RESULT([yes]) fi fi if test x"$done" = xno; then AC_MSG_CHECKING([whether -pthread is sufficient with -shared]) AC_TRY_LINK([#include ], [pthread_t th; pthread_join(th, 0); pthread_attr_init(0); pthread_cleanup_push(0, 0); pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], [done=yes]) if test "x$done" = xyes; then AC_MSG_RESULT([yes]) else AC_MSG_RESULT([no]) fi fi # # Linux gcc on some architectures such as mips/mipsel forgets # about -lpthread # if test x"$done" = xno; then AC_MSG_CHECKING([whether -lpthread fixes that]) LIBS="-lpthread $PTHREAD_LIBS $save_LIBS" AC_TRY_LINK([#include ], [pthread_t th; pthread_join(th, 0); pthread_attr_init(0); pthread_cleanup_push(0, 0); pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], [done=yes]) if test "x$done" = xyes; then AC_MSG_RESULT([yes]) PTHREAD_LIBS="-lpthread $PTHREAD_LIBS" else AC_MSG_RESULT([no]) fi fi # # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc # if test x"$done" = xno; then AC_MSG_CHECKING([whether -lc_r fixes that]) LIBS="-lc_r $PTHREAD_LIBS $save_LIBS" AC_TRY_LINK([#include ], [pthread_t th; pthread_join(th, 0); pthread_attr_init(0); pthread_cleanup_push(0, 0); pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], [done=yes]) if test "x$done" = xyes; then AC_MSG_RESULT([yes]) PTHREAD_LIBS="-lc_r $PTHREAD_LIBS" else AC_MSG_RESULT([no]) fi fi if test x"$done" = xno; then # OK, we have run out of ideas AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries]) # so it's not safe to assume that we may use pthreads acx_pthread_ok=no fi CFLAGS="$save_CFLAGS" LIBS="$save_LIBS" CC="$save_CC" else PTHREAD_CC="$CC" fi if test "x$acx_pthread_ok" = xyes; then # One more check: If we chose to use a compiler flag like -pthread but it is combined with # -nostdlib then the compiler won't implicitly link against libpthread. This can happen # in particular when using some versions of libtool on some distros. See: # https://bugzilla.redhat.com/show_bug.cgi?id=661333 save_CFLAGS="$CFLAGS" save_LIBS="$LIBS" save_CC="$CC" CFLAGS="$CFLAGS $PTHREAD_CFLAGS" LIBS="-nostdlib $PTHREAD_LIBS $LIBS -lc" CC="$PTHREAD_CC" AC_MSG_CHECKING([whether pthread flag is sufficient with -nostdlib]) AC_TRY_LINK([#include ], [pthread_t th; pthread_join(th, 0); pthread_attr_init(0); pthread_cleanup_push(0, 0); pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], [AC_MSG_RESULT([yes])], [ AC_MSG_RESULT([no]) AC_MSG_CHECKING([whether adding -lpthread fixes that]) LIBS="-nostdlib $PTHREAD_LIBS -lpthread $save_LIBS -lc" AC_TRY_LINK([#include ], [pthread_t th; pthread_join(th, 0); pthread_attr_init(0); pthread_cleanup_push(0, 0); pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], [ AC_MSG_RESULT([yes]) PTHREAD_LIBS="$PTHREAD_LIBS -lpthread" ], [AC_MSG_RESULT([no])]) ]) CFLAGS="$save_CFLAGS" LIBS="$save_LIBS" CC="$save_CC" fi AC_SUBST(PTHREAD_LIBS) AC_SUBST(PTHREAD_CFLAGS) AC_SUBST(PTHREAD_CC) # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: if test x"$acx_pthread_ok" = xyes; then ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) : else acx_pthread_ok=no $2 fi AC_LANG_RESTORE ])dnl ACX_PTHREAD capnproto-c++-0.8.0/m4/ax_cxx_compile_stdcxx_14.m40000644000175000017500000001561513340402540022440 0ustar00kentonkenton00000000000000# ============================================================================ # http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html # Additionally modified to detect -stdlib by Kenton Varda. # Further modified for C++14 by Kenton Varda. # ============================================================================ # # SYNOPSIS # # AX_CXX_COMPILE_STDCXX_14([ext|noext]) # # DESCRIPTION # # Check for baseline language coverage in the compiler for the C++14 # standard; if necessary, add switches to CXXFLAGS to enable support. # Errors out if no mode that supports C++14 baseline syntax can be found. # The argument, if specified, indicates whether you insist on an extended # mode (e.g. -std=gnu++14) or a strict conformance mode (e.g. -std=c++14). # If neither is specified, you get whatever works, with preference for an # extended mode. # # Additionally, check if the standard library supports C++11. If not, # try adding -stdlib=libc++ to see if that fixes it. This is needed e.g. # on Mac OSX 10.8, which ships with a very old libstdc++ but a relatively # new libc++. # # Both flags are actually added to CXX rather than CXXFLAGS to work around # a bug in libtool: -stdlib is stripped from CXXFLAGS when linking dynamic # libraries because it is not recognized. A patch was committed to mainline # libtool in February 2012 but as of June 2013 there has not yet been a # release containing this patch. # http://git.savannah.gnu.org/gitweb/?p=libtool.git;a=commit;h=c0c49f289f22ae670066657c60905986da3b555f # # LICENSE # # Copyright (c) 2008 Benjamin Kosnik # Copyright (c) 2012 Zack Weinberg # Copyright (c) 2013 Kenton Varda # # Copying and distribution of this file, with or without modification, are # permitted in any medium without royalty provided the copyright notice # and this notice are preserved. This file is offered as-is, without any # warranty. #serial 1 m4_define([_AX_CXX_COMPILE_STDCXX_14_testbody], [[ template struct check { static_assert(sizeof(int) <= sizeof(T), "not big enough"); }; typedef check> right_angle_brackets; int a; decltype(a) b; typedef check check_type; check_type c; check_type&& cr = static_cast(c); // GCC 4.7 introduced __float128 and makes reference to it in type_traits. // Clang doesn't implement it, so produces an error. Using -std=c++11 // instead of -std=gnu++11 works around the problem. But on some // platforms we need -std=gnu++11. So we want to make sure the test of // -std=gnu++11 fails only where this problem is present, and we hope that // -std=c++11 is always an acceptable fallback in these cases. Complicating // matters, though, is that we don't want to fail here if the platform is // completely missing a C++11 standard library, because we want to probe that // in a later test. It happens, though, that Clang allows us to check // whether a header exists at all before we include it. // // So, if we detect that __has_include is available (which it is on Clang), // and we use it to detect that (a C++11 header) exists, then // we go ahead and #include it to see if it breaks. In all other cases, we // don't #include it at all. #ifdef __has_include #if __has_include() #include #endif #endif // C++14 stuff auto deduceReturnType(int i) { return i; } auto genericLambda = [](auto x, auto y) { return x + y; }; auto captureExpressions = [x = 123]() { return x; }; // Avoid unused variable warnings. int foo() { return genericLambda(1, 2) + captureExpressions(); } ]]) m4_define([_AX_CXX_COMPILE_STDCXX_11_testbody_lib], [ #include #include #include #include ]) AC_DEFUN([AX_CXX_COMPILE_STDCXX_14], [dnl m4_if([$1], [], [], [$1], [ext], [], [$1], [noext], [], [m4_fatal([invalid argument `$1' to AX_CXX_COMPILE_STDCXX_14])])dnl AC_LANG_ASSERT([C++])dnl ac_success=no AC_CACHE_CHECK(whether $CXX supports C++14 features by default, ax_cv_cxx_compile_cxx14, [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_14_testbody])], [ax_cv_cxx_compile_cxx14=yes], [ax_cv_cxx_compile_cxx14=no])]) if test x$ax_cv_cxx_compile_cxx14 = xyes; then ac_success=yes fi m4_if([$1], [noext], [], [dnl if test x$ac_success = xno; then for switch in -std=gnu++14 -std=gnu++1y; do cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx14_$switch]) AC_CACHE_CHECK(whether $CXX supports C++14 features with $switch, $cachevar, [ac_save_CXX="$CXX" CXX="$CXX $switch" AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_14_testbody])], [eval $cachevar=yes], [eval $cachevar=no]) CXX="$ac_save_CXX"]) if eval test x\$$cachevar = xyes; then CXX="$CXX $switch" ac_success=yes break fi done fi]) m4_if([$1], [ext], [], [dnl if test x$ac_success = xno; then for switch in -std=c++14 -std=c++1y; do cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx14_$switch]) AC_CACHE_CHECK(whether $CXX supports C++14 features with $switch, $cachevar, [ac_save_CXX="$CXX" CXX="$CXX $switch" AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_14_testbody])], [eval $cachevar=yes], [eval $cachevar=no]) CXX="$ac_save_CXX"]) if eval test x\$$cachevar = xyes; then CXX="$CXX $switch" ac_success=yes break fi done fi]) if test x$ac_success = xno; then AC_MSG_ERROR([*** A compiler with support for C++14 language features is required.]) else ac_success=no AC_CACHE_CHECK(whether $CXX supports C++11 library features by default, ax_cv_cxx_compile_cxx11_lib, [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody_lib])], [ax_cv_cxx_compile_cxx11_lib=yes], [ax_cv_cxx_compile_cxx11_lib=no]) ]) if test x$ax_cv_cxx_compile_cxx11_lib = xyes; then ac_success=yes else # Try with -stdlib=libc++ AC_CACHE_CHECK(whether $CXX supports C++11 library features with -stdlib=libc++, ax_cv_cxx_compile_cxx11_lib_libcxx, [ac_save_CXX="$CXX" CXX="$CXX -stdlib=libc++" AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_11_testbody_lib])], [eval ax_cv_cxx_compile_cxx11_lib_libcxx=yes], [eval ax_cv_cxx_compile_cxx11_lib_libcxx=no]) CXX="$ac_save_CXX"]) if eval test x$ax_cv_cxx_compile_cxx11_lib_libcxx = xyes; then CXX="$CXX -stdlib=libc++" ac_success=yes break fi fi if test x$ac_success = xno; then AC_MSG_ERROR([*** A C++ library with support for C++11 features is required.]) fi fi ]) capnproto-c++-0.8.0/m4/libtool.m40000644000175000017500000112676613650317751017231 0ustar00kentonkenton00000000000000# libtool.m4 - Configure libtool for the host system. -*-Autoconf-*- # # Copyright (C) 1996-2001, 2003-2015 Free Software Foundation, Inc. # Written by Gordon Matzigkeit, 1996 # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. m4_define([_LT_COPYING], [dnl # Copyright (C) 2014 Free Software Foundation, Inc. # This is free software; see the source for copying conditions. There is NO # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # GNU Libtool is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of of the License, or # (at your option) any later version. # # As a special exception to the GNU General Public License, if you # distribute this file as part of a program or library that is built # using GNU Libtool, you may include this file under the same # distribution terms that you use for the rest of that program. # # GNU Libtool is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ]) # serial 58 LT_INIT # LT_PREREQ(VERSION) # ------------------ # Complain and exit if this libtool version is less that VERSION. m4_defun([LT_PREREQ], [m4_if(m4_version_compare(m4_defn([LT_PACKAGE_VERSION]), [$1]), -1, [m4_default([$3], [m4_fatal([Libtool version $1 or higher is required], 63)])], [$2])]) # _LT_CHECK_BUILDDIR # ------------------ # Complain if the absolute build directory name contains unusual characters m4_defun([_LT_CHECK_BUILDDIR], [case `pwd` in *\ * | *\ *) AC_MSG_WARN([Libtool does not cope well with whitespace in `pwd`]) ;; esac ]) # LT_INIT([OPTIONS]) # ------------------ AC_DEFUN([LT_INIT], [AC_PREREQ([2.62])dnl We use AC_PATH_PROGS_FEATURE_CHECK AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT])dnl AC_BEFORE([$0], [LT_LANG])dnl AC_BEFORE([$0], [LT_OUTPUT])dnl AC_BEFORE([$0], [LTDL_INIT])dnl m4_require([_LT_CHECK_BUILDDIR])dnl dnl Autoconf doesn't catch unexpanded LT_ macros by default: m4_pattern_forbid([^_?LT_[A-Z_]+$])dnl m4_pattern_allow([^(_LT_EOF|LT_DLGLOBAL|LT_DLLAZY_OR_NOW|LT_MULTI_MODULE)$])dnl dnl aclocal doesn't pull ltoptions.m4, ltsugar.m4, or ltversion.m4 dnl unless we require an AC_DEFUNed macro: AC_REQUIRE([LTOPTIONS_VERSION])dnl AC_REQUIRE([LTSUGAR_VERSION])dnl AC_REQUIRE([LTVERSION_VERSION])dnl AC_REQUIRE([LTOBSOLETE_VERSION])dnl m4_require([_LT_PROG_LTMAIN])dnl _LT_SHELL_INIT([SHELL=${CONFIG_SHELL-/bin/sh}]) dnl Parse OPTIONS _LT_SET_OPTIONS([$0], [$1]) # This can be used to rebuild libtool when needed LIBTOOL_DEPS=$ltmain # Always use our own libtool. LIBTOOL='$(SHELL) $(top_builddir)/libtool' AC_SUBST(LIBTOOL)dnl _LT_SETUP # Only expand once: m4_define([LT_INIT]) ])# LT_INIT # Old names: AU_ALIAS([AC_PROG_LIBTOOL], [LT_INIT]) AU_ALIAS([AM_PROG_LIBTOOL], [LT_INIT]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_PROG_LIBTOOL], []) dnl AC_DEFUN([AM_PROG_LIBTOOL], []) # _LT_PREPARE_CC_BASENAME # ----------------------- m4_defun([_LT_PREPARE_CC_BASENAME], [ # Calculate cc_basename. Skip known compiler wrappers and cross-prefix. func_cc_basename () { for cc_temp in @S|@*""; do case $cc_temp in compile | *[[\\/]]compile | ccache | *[[\\/]]ccache ) ;; distcc | *[[\\/]]distcc | purify | *[[\\/]]purify ) ;; \-*) ;; *) break;; esac done func_cc_basename_result=`$ECHO "$cc_temp" | $SED "s%.*/%%; s%^$host_alias-%%"` } ])# _LT_PREPARE_CC_BASENAME # _LT_CC_BASENAME(CC) # ------------------- # It would be clearer to call AC_REQUIREs from _LT_PREPARE_CC_BASENAME, # but that macro is also expanded into generated libtool script, which # arranges for $SED and $ECHO to be set by different means. m4_defun([_LT_CC_BASENAME], [m4_require([_LT_PREPARE_CC_BASENAME])dnl AC_REQUIRE([_LT_DECL_SED])dnl AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])dnl func_cc_basename $1 cc_basename=$func_cc_basename_result ]) # _LT_FILEUTILS_DEFAULTS # ---------------------- # It is okay to use these file commands and assume they have been set # sensibly after 'm4_require([_LT_FILEUTILS_DEFAULTS])'. m4_defun([_LT_FILEUTILS_DEFAULTS], [: ${CP="cp -f"} : ${MV="mv -f"} : ${RM="rm -f"} ])# _LT_FILEUTILS_DEFAULTS # _LT_SETUP # --------- m4_defun([_LT_SETUP], [AC_REQUIRE([AC_CANONICAL_HOST])dnl AC_REQUIRE([AC_CANONICAL_BUILD])dnl AC_REQUIRE([_LT_PREPARE_SED_QUOTE_VARS])dnl AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])dnl _LT_DECL([], [PATH_SEPARATOR], [1], [The PATH separator for the build system])dnl dnl _LT_DECL([], [host_alias], [0], [The host system])dnl _LT_DECL([], [host], [0])dnl _LT_DECL([], [host_os], [0])dnl dnl _LT_DECL([], [build_alias], [0], [The build system])dnl _LT_DECL([], [build], [0])dnl _LT_DECL([], [build_os], [0])dnl dnl AC_REQUIRE([AC_PROG_CC])dnl AC_REQUIRE([LT_PATH_LD])dnl AC_REQUIRE([LT_PATH_NM])dnl dnl AC_REQUIRE([AC_PROG_LN_S])dnl test -z "$LN_S" && LN_S="ln -s" _LT_DECL([], [LN_S], [1], [Whether we need soft or hard links])dnl dnl AC_REQUIRE([LT_CMD_MAX_LEN])dnl _LT_DECL([objext], [ac_objext], [0], [Object file suffix (normally "o")])dnl _LT_DECL([], [exeext], [0], [Executable file suffix (normally "")])dnl dnl m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_CHECK_SHELL_FEATURES])dnl m4_require([_LT_PATH_CONVERSION_FUNCTIONS])dnl m4_require([_LT_CMD_RELOAD])dnl m4_require([_LT_CHECK_MAGIC_METHOD])dnl m4_require([_LT_CHECK_SHAREDLIB_FROM_LINKLIB])dnl m4_require([_LT_CMD_OLD_ARCHIVE])dnl m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl m4_require([_LT_WITH_SYSROOT])dnl m4_require([_LT_CMD_TRUNCATE])dnl _LT_CONFIG_LIBTOOL_INIT([ # See if we are running on zsh, and set the options that allow our # commands through without removal of \ escapes INIT. if test -n "\${ZSH_VERSION+set}"; then setopt NO_GLOB_SUBST fi ]) if test -n "${ZSH_VERSION+set}"; then setopt NO_GLOB_SUBST fi _LT_CHECK_OBJDIR m4_require([_LT_TAG_COMPILER])dnl case $host_os in aix3*) # AIX sometimes has problems with the GCC collect2 program. For some # reason, if we set the COLLECT_NAMES environment variable, the problems # vanish in a puff of smoke. if test set != "${COLLECT_NAMES+set}"; then COLLECT_NAMES= export COLLECT_NAMES fi ;; esac # Global variables: ofile=libtool can_build_shared=yes # All known linkers require a '.a' archive for static linking (except MSVC, # which needs '.lib'). libext=a with_gnu_ld=$lt_cv_prog_gnu_ld old_CC=$CC old_CFLAGS=$CFLAGS # Set sane defaults for various variables test -z "$CC" && CC=cc test -z "$LTCC" && LTCC=$CC test -z "$LTCFLAGS" && LTCFLAGS=$CFLAGS test -z "$LD" && LD=ld test -z "$ac_objext" && ac_objext=o _LT_CC_BASENAME([$compiler]) # Only perform the check for file, if the check method requires it test -z "$MAGIC_CMD" && MAGIC_CMD=file case $deplibs_check_method in file_magic*) if test "$file_magic_cmd" = '$MAGIC_CMD'; then _LT_PATH_MAGIC fi ;; esac # Use C for the default configuration in the libtool script LT_SUPPORTED_TAG([CC]) _LT_LANG_C_CONFIG _LT_LANG_DEFAULT_CONFIG _LT_CONFIG_COMMANDS ])# _LT_SETUP # _LT_PREPARE_SED_QUOTE_VARS # -------------------------- # Define a few sed substitution that help us do robust quoting. m4_defun([_LT_PREPARE_SED_QUOTE_VARS], [# Backslashify metacharacters that are still active within # double-quoted strings. sed_quote_subst='s/\([["`$\\]]\)/\\\1/g' # Same as above, but do not quote variable references. double_quote_subst='s/\([["`\\]]\)/\\\1/g' # Sed substitution to delay expansion of an escaped shell variable in a # double_quote_subst'ed string. delay_variable_subst='s/\\\\\\\\\\\$/\\\\\\$/g' # Sed substitution to delay expansion of an escaped single quote. delay_single_quote_subst='s/'\''/'\'\\\\\\\'\''/g' # Sed substitution to avoid accidental globbing in evaled expressions no_glob_subst='s/\*/\\\*/g' ]) # _LT_PROG_LTMAIN # --------------- # Note that this code is called both from 'configure', and 'config.status' # now that we use AC_CONFIG_COMMANDS to generate libtool. Notably, # 'config.status' has no value for ac_aux_dir unless we are using Automake, # so we pass a copy along to make sure it has a sensible value anyway. m4_defun([_LT_PROG_LTMAIN], [m4_ifdef([AC_REQUIRE_AUX_FILE], [AC_REQUIRE_AUX_FILE([ltmain.sh])])dnl _LT_CONFIG_LIBTOOL_INIT([ac_aux_dir='$ac_aux_dir']) ltmain=$ac_aux_dir/ltmain.sh ])# _LT_PROG_LTMAIN ## ------------------------------------- ## ## Accumulate code for creating libtool. ## ## ------------------------------------- ## # So that we can recreate a full libtool script including additional # tags, we accumulate the chunks of code to send to AC_CONFIG_COMMANDS # in macros and then make a single call at the end using the 'libtool' # label. # _LT_CONFIG_LIBTOOL_INIT([INIT-COMMANDS]) # ---------------------------------------- # Register INIT-COMMANDS to be passed to AC_CONFIG_COMMANDS later. m4_define([_LT_CONFIG_LIBTOOL_INIT], [m4_ifval([$1], [m4_append([_LT_OUTPUT_LIBTOOL_INIT], [$1 ])])]) # Initialize. m4_define([_LT_OUTPUT_LIBTOOL_INIT]) # _LT_CONFIG_LIBTOOL([COMMANDS]) # ------------------------------ # Register COMMANDS to be passed to AC_CONFIG_COMMANDS later. m4_define([_LT_CONFIG_LIBTOOL], [m4_ifval([$1], [m4_append([_LT_OUTPUT_LIBTOOL_COMMANDS], [$1 ])])]) # Initialize. m4_define([_LT_OUTPUT_LIBTOOL_COMMANDS]) # _LT_CONFIG_SAVE_COMMANDS([COMMANDS], [INIT_COMMANDS]) # ----------------------------------------------------- m4_defun([_LT_CONFIG_SAVE_COMMANDS], [_LT_CONFIG_LIBTOOL([$1]) _LT_CONFIG_LIBTOOL_INIT([$2]) ]) # _LT_FORMAT_COMMENT([COMMENT]) # ----------------------------- # Add leading comment marks to the start of each line, and a trailing # full-stop to the whole comment if one is not present already. m4_define([_LT_FORMAT_COMMENT], [m4_ifval([$1], [ m4_bpatsubst([m4_bpatsubst([$1], [^ *], [# ])], [['`$\]], [\\\&])]m4_bmatch([$1], [[!?.]$], [], [.]) )]) ## ------------------------ ## ## FIXME: Eliminate VARNAME ## ## ------------------------ ## # _LT_DECL([CONFIGNAME], VARNAME, VALUE, [DESCRIPTION], [IS-TAGGED?]) # ------------------------------------------------------------------- # CONFIGNAME is the name given to the value in the libtool script. # VARNAME is the (base) name used in the configure script. # VALUE may be 0, 1 or 2 for a computed quote escaped value based on # VARNAME. Any other value will be used directly. m4_define([_LT_DECL], [lt_if_append_uniq([lt_decl_varnames], [$2], [, ], [lt_dict_add_subkey([lt_decl_dict], [$2], [libtool_name], [m4_ifval([$1], [$1], [$2])]) lt_dict_add_subkey([lt_decl_dict], [$2], [value], [$3]) m4_ifval([$4], [lt_dict_add_subkey([lt_decl_dict], [$2], [description], [$4])]) lt_dict_add_subkey([lt_decl_dict], [$2], [tagged?], [m4_ifval([$5], [yes], [no])])]) ]) # _LT_TAGDECL([CONFIGNAME], VARNAME, VALUE, [DESCRIPTION]) # -------------------------------------------------------- m4_define([_LT_TAGDECL], [_LT_DECL([$1], [$2], [$3], [$4], [yes])]) # lt_decl_tag_varnames([SEPARATOR], [VARNAME1...]) # ------------------------------------------------ m4_define([lt_decl_tag_varnames], [_lt_decl_filter([tagged?], [yes], $@)]) # _lt_decl_filter(SUBKEY, VALUE, [SEPARATOR], [VARNAME1..]) # --------------------------------------------------------- m4_define([_lt_decl_filter], [m4_case([$#], [0], [m4_fatal([$0: too few arguments: $#])], [1], [m4_fatal([$0: too few arguments: $#: $1])], [2], [lt_dict_filter([lt_decl_dict], [$1], [$2], [], lt_decl_varnames)], [3], [lt_dict_filter([lt_decl_dict], [$1], [$2], [$3], lt_decl_varnames)], [lt_dict_filter([lt_decl_dict], $@)])[]dnl ]) # lt_decl_quote_varnames([SEPARATOR], [VARNAME1...]) # -------------------------------------------------- m4_define([lt_decl_quote_varnames], [_lt_decl_filter([value], [1], $@)]) # lt_decl_dquote_varnames([SEPARATOR], [VARNAME1...]) # --------------------------------------------------- m4_define([lt_decl_dquote_varnames], [_lt_decl_filter([value], [2], $@)]) # lt_decl_varnames_tagged([SEPARATOR], [VARNAME1...]) # --------------------------------------------------- m4_define([lt_decl_varnames_tagged], [m4_assert([$# <= 2])dnl _$0(m4_quote(m4_default([$1], [[, ]])), m4_ifval([$2], [[$2]], [m4_dquote(lt_decl_tag_varnames)]), m4_split(m4_normalize(m4_quote(_LT_TAGS)), [ ]))]) m4_define([_lt_decl_varnames_tagged], [m4_ifval([$3], [lt_combine([$1], [$2], [_], $3)])]) # lt_decl_all_varnames([SEPARATOR], [VARNAME1...]) # ------------------------------------------------ m4_define([lt_decl_all_varnames], [_$0(m4_quote(m4_default([$1], [[, ]])), m4_if([$2], [], m4_quote(lt_decl_varnames), m4_quote(m4_shift($@))))[]dnl ]) m4_define([_lt_decl_all_varnames], [lt_join($@, lt_decl_varnames_tagged([$1], lt_decl_tag_varnames([[, ]], m4_shift($@))))dnl ]) # _LT_CONFIG_STATUS_DECLARE([VARNAME]) # ------------------------------------ # Quote a variable value, and forward it to 'config.status' so that its # declaration there will have the same value as in 'configure'. VARNAME # must have a single quote delimited value for this to work. m4_define([_LT_CONFIG_STATUS_DECLARE], [$1='`$ECHO "$][$1" | $SED "$delay_single_quote_subst"`']) # _LT_CONFIG_STATUS_DECLARATIONS # ------------------------------ # We delimit libtool config variables with single quotes, so when # we write them to config.status, we have to be sure to quote all # embedded single quotes properly. In configure, this macro expands # each variable declared with _LT_DECL (and _LT_TAGDECL) into: # # ='`$ECHO "$" | $SED "$delay_single_quote_subst"`' m4_defun([_LT_CONFIG_STATUS_DECLARATIONS], [m4_foreach([_lt_var], m4_quote(lt_decl_all_varnames), [m4_n([_LT_CONFIG_STATUS_DECLARE(_lt_var)])])]) # _LT_LIBTOOL_TAGS # ---------------- # Output comment and list of tags supported by the script m4_defun([_LT_LIBTOOL_TAGS], [_LT_FORMAT_COMMENT([The names of the tagged configurations supported by this script])dnl available_tags='_LT_TAGS'dnl ]) # _LT_LIBTOOL_DECLARE(VARNAME, [TAG]) # ----------------------------------- # Extract the dictionary values for VARNAME (optionally with TAG) and # expand to a commented shell variable setting: # # # Some comment about what VAR is for. # visible_name=$lt_internal_name m4_define([_LT_LIBTOOL_DECLARE], [_LT_FORMAT_COMMENT(m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [description])))[]dnl m4_pushdef([_libtool_name], m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [libtool_name])))[]dnl m4_case(m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [value])), [0], [_libtool_name=[$]$1], [1], [_libtool_name=$lt_[]$1], [2], [_libtool_name=$lt_[]$1], [_libtool_name=lt_dict_fetch([lt_decl_dict], [$1], [value])])[]dnl m4_ifval([$2], [_$2])[]m4_popdef([_libtool_name])[]dnl ]) # _LT_LIBTOOL_CONFIG_VARS # ----------------------- # Produce commented declarations of non-tagged libtool config variables # suitable for insertion in the LIBTOOL CONFIG section of the 'libtool' # script. Tagged libtool config variables (even for the LIBTOOL CONFIG # section) are produced by _LT_LIBTOOL_TAG_VARS. m4_defun([_LT_LIBTOOL_CONFIG_VARS], [m4_foreach([_lt_var], m4_quote(_lt_decl_filter([tagged?], [no], [], lt_decl_varnames)), [m4_n([_LT_LIBTOOL_DECLARE(_lt_var)])])]) # _LT_LIBTOOL_TAG_VARS(TAG) # ------------------------- m4_define([_LT_LIBTOOL_TAG_VARS], [m4_foreach([_lt_var], m4_quote(lt_decl_tag_varnames), [m4_n([_LT_LIBTOOL_DECLARE(_lt_var, [$1])])])]) # _LT_TAGVAR(VARNAME, [TAGNAME]) # ------------------------------ m4_define([_LT_TAGVAR], [m4_ifval([$2], [$1_$2], [$1])]) # _LT_CONFIG_COMMANDS # ------------------- # Send accumulated output to $CONFIG_STATUS. Thanks to the lists of # variables for single and double quote escaping we saved from calls # to _LT_DECL, we can put quote escaped variables declarations # into 'config.status', and then the shell code to quote escape them in # for loops in 'config.status'. Finally, any additional code accumulated # from calls to _LT_CONFIG_LIBTOOL_INIT is expanded. m4_defun([_LT_CONFIG_COMMANDS], [AC_PROVIDE_IFELSE([LT_OUTPUT], dnl If the libtool generation code has been placed in $CONFIG_LT, dnl instead of duplicating it all over again into config.status, dnl then we will have config.status run $CONFIG_LT later, so it dnl needs to know what name is stored there: [AC_CONFIG_COMMANDS([libtool], [$SHELL $CONFIG_LT || AS_EXIT(1)], [CONFIG_LT='$CONFIG_LT'])], dnl If the libtool generation code is destined for config.status, dnl expand the accumulated commands and init code now: [AC_CONFIG_COMMANDS([libtool], [_LT_OUTPUT_LIBTOOL_COMMANDS], [_LT_OUTPUT_LIBTOOL_COMMANDS_INIT])]) ])#_LT_CONFIG_COMMANDS # Initialize. m4_define([_LT_OUTPUT_LIBTOOL_COMMANDS_INIT], [ # The HP-UX ksh and POSIX shell print the target directory to stdout # if CDPATH is set. (unset CDPATH) >/dev/null 2>&1 && unset CDPATH sed_quote_subst='$sed_quote_subst' double_quote_subst='$double_quote_subst' delay_variable_subst='$delay_variable_subst' _LT_CONFIG_STATUS_DECLARATIONS LTCC='$LTCC' LTCFLAGS='$LTCFLAGS' compiler='$compiler_DEFAULT' # A function that is used when there is no print builtin or printf. func_fallback_echo () { eval 'cat <<_LTECHO_EOF \$[]1 _LTECHO_EOF' } # Quote evaled strings. for var in lt_decl_all_varnames([[ \ ]], lt_decl_quote_varnames); do case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in *[[\\\\\\\`\\"\\\$]]*) eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED \\"\\\$sed_quote_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes ;; *) eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\"" ;; esac done # Double-quote double-evaled strings. for var in lt_decl_all_varnames([[ \ ]], lt_decl_dquote_varnames); do case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in *[[\\\\\\\`\\"\\\$]]*) eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED -e \\"\\\$double_quote_subst\\" -e \\"\\\$sed_quote_subst\\" -e \\"\\\$delay_variable_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes ;; *) eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\"" ;; esac done _LT_OUTPUT_LIBTOOL_INIT ]) # _LT_GENERATED_FILE_INIT(FILE, [COMMENT]) # ------------------------------------ # Generate a child script FILE with all initialization necessary to # reuse the environment learned by the parent script, and make the # file executable. If COMMENT is supplied, it is inserted after the # '#!' sequence but before initialization text begins. After this # macro, additional text can be appended to FILE to form the body of # the child script. The macro ends with non-zero status if the # file could not be fully written (such as if the disk is full). m4_ifdef([AS_INIT_GENERATED], [m4_defun([_LT_GENERATED_FILE_INIT],[AS_INIT_GENERATED($@)])], [m4_defun([_LT_GENERATED_FILE_INIT], [m4_require([AS_PREPARE])]dnl [m4_pushdef([AS_MESSAGE_LOG_FD])]dnl [lt_write_fail=0 cat >$1 <<_ASEOF || lt_write_fail=1 #! $SHELL # Generated by $as_me. $2 SHELL=\${CONFIG_SHELL-$SHELL} export SHELL _ASEOF cat >>$1 <<\_ASEOF || lt_write_fail=1 AS_SHELL_SANITIZE _AS_PREPARE exec AS_MESSAGE_FD>&1 _ASEOF test 0 = "$lt_write_fail" && chmod +x $1[]dnl m4_popdef([AS_MESSAGE_LOG_FD])])])# _LT_GENERATED_FILE_INIT # LT_OUTPUT # --------- # This macro allows early generation of the libtool script (before # AC_OUTPUT is called), incase it is used in configure for compilation # tests. AC_DEFUN([LT_OUTPUT], [: ${CONFIG_LT=./config.lt} AC_MSG_NOTICE([creating $CONFIG_LT]) _LT_GENERATED_FILE_INIT(["$CONFIG_LT"], [# Run this file to recreate a libtool stub with the current configuration.]) cat >>"$CONFIG_LT" <<\_LTEOF lt_cl_silent=false exec AS_MESSAGE_LOG_FD>>config.log { echo AS_BOX([Running $as_me.]) } >&AS_MESSAGE_LOG_FD lt_cl_help="\ '$as_me' creates a local libtool stub from the current configuration, for use in further configure time tests before the real libtool is generated. Usage: $[0] [[OPTIONS]] -h, --help print this help, then exit -V, --version print version number, then exit -q, --quiet do not print progress messages -d, --debug don't remove temporary files Report bugs to ." lt_cl_version="\ m4_ifset([AC_PACKAGE_NAME], [AC_PACKAGE_NAME ])config.lt[]dnl m4_ifset([AC_PACKAGE_VERSION], [ AC_PACKAGE_VERSION]) configured by $[0], generated by m4_PACKAGE_STRING. Copyright (C) 2011 Free Software Foundation, Inc. This config.lt script is free software; the Free Software Foundation gives unlimited permision to copy, distribute and modify it." while test 0 != $[#] do case $[1] in --version | --v* | -V ) echo "$lt_cl_version"; exit 0 ;; --help | --h* | -h ) echo "$lt_cl_help"; exit 0 ;; --debug | --d* | -d ) debug=: ;; --quiet | --q* | --silent | --s* | -q ) lt_cl_silent=: ;; -*) AC_MSG_ERROR([unrecognized option: $[1] Try '$[0] --help' for more information.]) ;; *) AC_MSG_ERROR([unrecognized argument: $[1] Try '$[0] --help' for more information.]) ;; esac shift done if $lt_cl_silent; then exec AS_MESSAGE_FD>/dev/null fi _LTEOF cat >>"$CONFIG_LT" <<_LTEOF _LT_OUTPUT_LIBTOOL_COMMANDS_INIT _LTEOF cat >>"$CONFIG_LT" <<\_LTEOF AC_MSG_NOTICE([creating $ofile]) _LT_OUTPUT_LIBTOOL_COMMANDS AS_EXIT(0) _LTEOF chmod +x "$CONFIG_LT" # configure is writing to config.log, but config.lt does its own redirection, # appending to config.log, which fails on DOS, as config.log is still kept # open by configure. Here we exec the FD to /dev/null, effectively closing # config.log, so it can be properly (re)opened and appended to by config.lt. lt_cl_success=: test yes = "$silent" && lt_config_lt_args="$lt_config_lt_args --quiet" exec AS_MESSAGE_LOG_FD>/dev/null $SHELL "$CONFIG_LT" $lt_config_lt_args || lt_cl_success=false exec AS_MESSAGE_LOG_FD>>config.log $lt_cl_success || AS_EXIT(1) ])# LT_OUTPUT # _LT_CONFIG(TAG) # --------------- # If TAG is the built-in tag, create an initial libtool script with a # default configuration from the untagged config vars. Otherwise add code # to config.status for appending the configuration named by TAG from the # matching tagged config vars. m4_defun([_LT_CONFIG], [m4_require([_LT_FILEUTILS_DEFAULTS])dnl _LT_CONFIG_SAVE_COMMANDS([ m4_define([_LT_TAG], m4_if([$1], [], [C], [$1]))dnl m4_if(_LT_TAG, [C], [ # See if we are running on zsh, and set the options that allow our # commands through without removal of \ escapes. if test -n "${ZSH_VERSION+set}"; then setopt NO_GLOB_SUBST fi cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15 $RM "$cfgfile" cat <<_LT_EOF >> "$cfgfile" #! $SHELL # Generated automatically by $as_me ($PACKAGE) $VERSION # NOTE: Changes made to this file will be lost: look at ltmain.sh. # Provide generalized library-building support services. # Written by Gordon Matzigkeit, 1996 _LT_COPYING _LT_LIBTOOL_TAGS # Configured defaults for sys_lib_dlsearch_path munging. : \${LT_SYS_LIBRARY_PATH="$configure_time_lt_sys_library_path"} # ### BEGIN LIBTOOL CONFIG _LT_LIBTOOL_CONFIG_VARS _LT_LIBTOOL_TAG_VARS # ### END LIBTOOL CONFIG _LT_EOF cat <<'_LT_EOF' >> "$cfgfile" # ### BEGIN FUNCTIONS SHARED WITH CONFIGURE _LT_PREPARE_MUNGE_PATH_LIST _LT_PREPARE_CC_BASENAME # ### END FUNCTIONS SHARED WITH CONFIGURE _LT_EOF case $host_os in aix3*) cat <<\_LT_EOF >> "$cfgfile" # AIX sometimes has problems with the GCC collect2 program. For some # reason, if we set the COLLECT_NAMES environment variable, the problems # vanish in a puff of smoke. if test set != "${COLLECT_NAMES+set}"; then COLLECT_NAMES= export COLLECT_NAMES fi _LT_EOF ;; esac _LT_PROG_LTMAIN # We use sed instead of cat because bash on DJGPP gets confused if # if finds mixed CR/LF and LF-only lines. Since sed operates in # text mode, it properly converts lines to CR/LF. This bash problem # is reportedly fixed, but why not run on old versions too? sed '$q' "$ltmain" >> "$cfgfile" \ || (rm -f "$cfgfile"; exit 1) mv -f "$cfgfile" "$ofile" || (rm -f "$ofile" && cp "$cfgfile" "$ofile" && rm -f "$cfgfile") chmod +x "$ofile" ], [cat <<_LT_EOF >> "$ofile" dnl Unfortunately we have to use $1 here, since _LT_TAG is not expanded dnl in a comment (ie after a #). # ### BEGIN LIBTOOL TAG CONFIG: $1 _LT_LIBTOOL_TAG_VARS(_LT_TAG) # ### END LIBTOOL TAG CONFIG: $1 _LT_EOF ])dnl /m4_if ], [m4_if([$1], [], [ PACKAGE='$PACKAGE' VERSION='$VERSION' RM='$RM' ofile='$ofile'], []) ])dnl /_LT_CONFIG_SAVE_COMMANDS ])# _LT_CONFIG # LT_SUPPORTED_TAG(TAG) # --------------------- # Trace this macro to discover what tags are supported by the libtool # --tag option, using: # autoconf --trace 'LT_SUPPORTED_TAG:$1' AC_DEFUN([LT_SUPPORTED_TAG], []) # C support is built-in for now m4_define([_LT_LANG_C_enabled], []) m4_define([_LT_TAGS], []) # LT_LANG(LANG) # ------------- # Enable libtool support for the given language if not already enabled. AC_DEFUN([LT_LANG], [AC_BEFORE([$0], [LT_OUTPUT])dnl m4_case([$1], [C], [_LT_LANG(C)], [C++], [_LT_LANG(CXX)], [Go], [_LT_LANG(GO)], [Java], [_LT_LANG(GCJ)], [Fortran 77], [_LT_LANG(F77)], [Fortran], [_LT_LANG(FC)], [Windows Resource], [_LT_LANG(RC)], [m4_ifdef([_LT_LANG_]$1[_CONFIG], [_LT_LANG($1)], [m4_fatal([$0: unsupported language: "$1"])])])dnl ])# LT_LANG # _LT_LANG(LANGNAME) # ------------------ m4_defun([_LT_LANG], [m4_ifdef([_LT_LANG_]$1[_enabled], [], [LT_SUPPORTED_TAG([$1])dnl m4_append([_LT_TAGS], [$1 ])dnl m4_define([_LT_LANG_]$1[_enabled], [])dnl _LT_LANG_$1_CONFIG($1)])dnl ])# _LT_LANG m4_ifndef([AC_PROG_GO], [ ############################################################ # NOTE: This macro has been submitted for inclusion into # # GNU Autoconf as AC_PROG_GO. When it is available in # # a released version of Autoconf we should remove this # # macro and use it instead. # ############################################################ m4_defun([AC_PROG_GO], [AC_LANG_PUSH(Go)dnl AC_ARG_VAR([GOC], [Go compiler command])dnl AC_ARG_VAR([GOFLAGS], [Go compiler flags])dnl _AC_ARG_VAR_LDFLAGS()dnl AC_CHECK_TOOL(GOC, gccgo) if test -z "$GOC"; then if test -n "$ac_tool_prefix"; then AC_CHECK_PROG(GOC, [${ac_tool_prefix}gccgo], [${ac_tool_prefix}gccgo]) fi fi if test -z "$GOC"; then AC_CHECK_PROG(GOC, gccgo, gccgo, false) fi ])#m4_defun ])#m4_ifndef # _LT_LANG_DEFAULT_CONFIG # ----------------------- m4_defun([_LT_LANG_DEFAULT_CONFIG], [AC_PROVIDE_IFELSE([AC_PROG_CXX], [LT_LANG(CXX)], [m4_define([AC_PROG_CXX], defn([AC_PROG_CXX])[LT_LANG(CXX)])]) AC_PROVIDE_IFELSE([AC_PROG_F77], [LT_LANG(F77)], [m4_define([AC_PROG_F77], defn([AC_PROG_F77])[LT_LANG(F77)])]) AC_PROVIDE_IFELSE([AC_PROG_FC], [LT_LANG(FC)], [m4_define([AC_PROG_FC], defn([AC_PROG_FC])[LT_LANG(FC)])]) dnl The call to [A][M_PROG_GCJ] is quoted like that to stop aclocal dnl pulling things in needlessly. AC_PROVIDE_IFELSE([AC_PROG_GCJ], [LT_LANG(GCJ)], [AC_PROVIDE_IFELSE([A][M_PROG_GCJ], [LT_LANG(GCJ)], [AC_PROVIDE_IFELSE([LT_PROG_GCJ], [LT_LANG(GCJ)], [m4_ifdef([AC_PROG_GCJ], [m4_define([AC_PROG_GCJ], defn([AC_PROG_GCJ])[LT_LANG(GCJ)])]) m4_ifdef([A][M_PROG_GCJ], [m4_define([A][M_PROG_GCJ], defn([A][M_PROG_GCJ])[LT_LANG(GCJ)])]) m4_ifdef([LT_PROG_GCJ], [m4_define([LT_PROG_GCJ], defn([LT_PROG_GCJ])[LT_LANG(GCJ)])])])])]) AC_PROVIDE_IFELSE([AC_PROG_GO], [LT_LANG(GO)], [m4_define([AC_PROG_GO], defn([AC_PROG_GO])[LT_LANG(GO)])]) AC_PROVIDE_IFELSE([LT_PROG_RC], [LT_LANG(RC)], [m4_define([LT_PROG_RC], defn([LT_PROG_RC])[LT_LANG(RC)])]) ])# _LT_LANG_DEFAULT_CONFIG # Obsolete macros: AU_DEFUN([AC_LIBTOOL_CXX], [LT_LANG(C++)]) AU_DEFUN([AC_LIBTOOL_F77], [LT_LANG(Fortran 77)]) AU_DEFUN([AC_LIBTOOL_FC], [LT_LANG(Fortran)]) AU_DEFUN([AC_LIBTOOL_GCJ], [LT_LANG(Java)]) AU_DEFUN([AC_LIBTOOL_RC], [LT_LANG(Windows Resource)]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_CXX], []) dnl AC_DEFUN([AC_LIBTOOL_F77], []) dnl AC_DEFUN([AC_LIBTOOL_FC], []) dnl AC_DEFUN([AC_LIBTOOL_GCJ], []) dnl AC_DEFUN([AC_LIBTOOL_RC], []) # _LT_TAG_COMPILER # ---------------- m4_defun([_LT_TAG_COMPILER], [AC_REQUIRE([AC_PROG_CC])dnl _LT_DECL([LTCC], [CC], [1], [A C compiler])dnl _LT_DECL([LTCFLAGS], [CFLAGS], [1], [LTCC compiler flags])dnl _LT_TAGDECL([CC], [compiler], [1], [A language specific compiler])dnl _LT_TAGDECL([with_gcc], [GCC], [0], [Is the compiler the GNU compiler?])dnl # If no C compiler was specified, use CC. LTCC=${LTCC-"$CC"} # If no C compiler flags were specified, use CFLAGS. LTCFLAGS=${LTCFLAGS-"$CFLAGS"} # Allow CC to be a program name with arguments. compiler=$CC ])# _LT_TAG_COMPILER # _LT_COMPILER_BOILERPLATE # ------------------------ # Check for compiler boilerplate output or warnings with # the simple compiler test code. m4_defun([_LT_COMPILER_BOILERPLATE], [m4_require([_LT_DECL_SED])dnl ac_outfile=conftest.$ac_objext echo "$lt_simple_compile_test_code" >conftest.$ac_ext eval "$ac_compile" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err _lt_compiler_boilerplate=`cat conftest.err` $RM conftest* ])# _LT_COMPILER_BOILERPLATE # _LT_LINKER_BOILERPLATE # ---------------------- # Check for linker boilerplate output or warnings with # the simple link test code. m4_defun([_LT_LINKER_BOILERPLATE], [m4_require([_LT_DECL_SED])dnl ac_outfile=conftest.$ac_objext echo "$lt_simple_link_test_code" >conftest.$ac_ext eval "$ac_link" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err _lt_linker_boilerplate=`cat conftest.err` $RM -r conftest* ])# _LT_LINKER_BOILERPLATE # _LT_REQUIRED_DARWIN_CHECKS # ------------------------- m4_defun_once([_LT_REQUIRED_DARWIN_CHECKS],[ case $host_os in rhapsody* | darwin*) AC_CHECK_TOOL([DSYMUTIL], [dsymutil], [:]) AC_CHECK_TOOL([NMEDIT], [nmedit], [:]) AC_CHECK_TOOL([LIPO], [lipo], [:]) AC_CHECK_TOOL([OTOOL], [otool], [:]) AC_CHECK_TOOL([OTOOL64], [otool64], [:]) _LT_DECL([], [DSYMUTIL], [1], [Tool to manipulate archived DWARF debug symbol files on Mac OS X]) _LT_DECL([], [NMEDIT], [1], [Tool to change global to local symbols on Mac OS X]) _LT_DECL([], [LIPO], [1], [Tool to manipulate fat objects and archives on Mac OS X]) _LT_DECL([], [OTOOL], [1], [ldd/readelf like tool for Mach-O binaries on Mac OS X]) _LT_DECL([], [OTOOL64], [1], [ldd/readelf like tool for 64 bit Mach-O binaries on Mac OS X 10.4]) AC_CACHE_CHECK([for -single_module linker flag],[lt_cv_apple_cc_single_mod], [lt_cv_apple_cc_single_mod=no if test -z "$LT_MULTI_MODULE"; then # By default we will add the -single_module flag. You can override # by either setting the environment variable LT_MULTI_MODULE # non-empty at configure time, or by adding -multi_module to the # link flags. rm -rf libconftest.dylib* echo "int foo(void){return 1;}" > conftest.c echo "$LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \ -dynamiclib -Wl,-single_module conftest.c" >&AS_MESSAGE_LOG_FD $LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \ -dynamiclib -Wl,-single_module conftest.c 2>conftest.err _lt_result=$? # If there is a non-empty error log, and "single_module" # appears in it, assume the flag caused a linker warning if test -s conftest.err && $GREP single_module conftest.err; then cat conftest.err >&AS_MESSAGE_LOG_FD # Otherwise, if the output was created with a 0 exit code from # the compiler, it worked. elif test -f libconftest.dylib && test 0 = "$_lt_result"; then lt_cv_apple_cc_single_mod=yes else cat conftest.err >&AS_MESSAGE_LOG_FD fi rm -rf libconftest.dylib* rm -f conftest.* fi]) AC_CACHE_CHECK([for -exported_symbols_list linker flag], [lt_cv_ld_exported_symbols_list], [lt_cv_ld_exported_symbols_list=no save_LDFLAGS=$LDFLAGS echo "_main" > conftest.sym LDFLAGS="$LDFLAGS -Wl,-exported_symbols_list,conftest.sym" AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])], [lt_cv_ld_exported_symbols_list=yes], [lt_cv_ld_exported_symbols_list=no]) LDFLAGS=$save_LDFLAGS ]) AC_CACHE_CHECK([for -force_load linker flag],[lt_cv_ld_force_load], [lt_cv_ld_force_load=no cat > conftest.c << _LT_EOF int forced_loaded() { return 2;} _LT_EOF echo "$LTCC $LTCFLAGS -c -o conftest.o conftest.c" >&AS_MESSAGE_LOG_FD $LTCC $LTCFLAGS -c -o conftest.o conftest.c 2>&AS_MESSAGE_LOG_FD echo "$AR cru libconftest.a conftest.o" >&AS_MESSAGE_LOG_FD $AR cru libconftest.a conftest.o 2>&AS_MESSAGE_LOG_FD echo "$RANLIB libconftest.a" >&AS_MESSAGE_LOG_FD $RANLIB libconftest.a 2>&AS_MESSAGE_LOG_FD cat > conftest.c << _LT_EOF int main() { return 0;} _LT_EOF echo "$LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a" >&AS_MESSAGE_LOG_FD $LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a 2>conftest.err _lt_result=$? if test -s conftest.err && $GREP force_load conftest.err; then cat conftest.err >&AS_MESSAGE_LOG_FD elif test -f conftest && test 0 = "$_lt_result" && $GREP forced_load conftest >/dev/null 2>&1; then lt_cv_ld_force_load=yes else cat conftest.err >&AS_MESSAGE_LOG_FD fi rm -f conftest.err libconftest.a conftest conftest.c rm -rf conftest.dSYM ]) case $host_os in rhapsody* | darwin1.[[012]]) _lt_dar_allow_undefined='$wl-undefined ${wl}suppress' ;; darwin1.*) _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;; darwin*) # darwin 5.x on # if running on 10.5 or later, the deployment target defaults # to the OS version, if on x86, and 10.4, the deployment # target defaults to 10.4. Don't you love it? case ${MACOSX_DEPLOYMENT_TARGET-10.0},$host in 10.0,*86*-darwin8*|10.0,*-darwin[[91]]*) _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;; 10.[[012]][[,.]]*) _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;; 10.*) _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;; esac ;; esac if test yes = "$lt_cv_apple_cc_single_mod"; then _lt_dar_single_mod='$single_module' fi if test yes = "$lt_cv_ld_exported_symbols_list"; then _lt_dar_export_syms=' $wl-exported_symbols_list,$output_objdir/$libname-symbols.expsym' else _lt_dar_export_syms='~$NMEDIT -s $output_objdir/$libname-symbols.expsym $lib' fi if test : != "$DSYMUTIL" && test no = "$lt_cv_ld_force_load"; then _lt_dsymutil='~$DSYMUTIL $lib || :' else _lt_dsymutil= fi ;; esac ]) # _LT_DARWIN_LINKER_FEATURES([TAG]) # --------------------------------- # Checks for linker and compiler features on darwin m4_defun([_LT_DARWIN_LINKER_FEATURES], [ m4_require([_LT_REQUIRED_DARWIN_CHECKS]) _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_automatic, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported if test yes = "$lt_cv_ld_force_load"; then _LT_TAGVAR(whole_archive_flag_spec, $1)='`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience $wl-force_load,$conv\"; done; func_echo_all \"$new_convenience\"`' m4_case([$1], [F77], [_LT_TAGVAR(compiler_needs_object, $1)=yes], [FC], [_LT_TAGVAR(compiler_needs_object, $1)=yes]) else _LT_TAGVAR(whole_archive_flag_spec, $1)='' fi _LT_TAGVAR(link_all_deplibs, $1)=yes _LT_TAGVAR(allow_undefined_flag, $1)=$_lt_dar_allow_undefined case $cc_basename in ifort*|nagfor*) _lt_dar_can_shared=yes ;; *) _lt_dar_can_shared=$GCC ;; esac if test yes = "$_lt_dar_can_shared"; then output_verbose_link_cmd=func_echo_all _LT_TAGVAR(archive_cmds, $1)="\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dsymutil" _LT_TAGVAR(module_cmds, $1)="\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dsymutil" _LT_TAGVAR(archive_expsym_cmds, $1)="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dar_export_syms$_lt_dsymutil" _LT_TAGVAR(module_expsym_cmds, $1)="sed -e 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dar_export_syms$_lt_dsymutil" m4_if([$1], [CXX], [ if test yes != "$lt_cv_apple_cc_single_mod"; then _LT_TAGVAR(archive_cmds, $1)="\$CC -r -keep_private_externs -nostdlib -o \$lib-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$lib-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring$_lt_dsymutil" _LT_TAGVAR(archive_expsym_cmds, $1)="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -r -keep_private_externs -nostdlib -o \$lib-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$lib-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring$_lt_dar_export_syms$_lt_dsymutil" fi ],[]) else _LT_TAGVAR(ld_shlibs, $1)=no fi ]) # _LT_SYS_MODULE_PATH_AIX([TAGNAME]) # ---------------------------------- # Links a minimal program and checks the executable # for the system default hardcoded library path. In most cases, # this is /usr/lib:/lib, but when the MPI compilers are used # the location of the communication and MPI libs are included too. # If we don't find anything, use the default library path according # to the aix ld manual. # Store the results from the different compilers for each TAGNAME. # Allow to override them for all tags through lt_cv_aix_libpath. m4_defun([_LT_SYS_MODULE_PATH_AIX], [m4_require([_LT_DECL_SED])dnl if test set = "${lt_cv_aix_libpath+set}"; then aix_libpath=$lt_cv_aix_libpath else AC_CACHE_VAL([_LT_TAGVAR([lt_cv_aix_libpath_], [$1])], [AC_LINK_IFELSE([AC_LANG_PROGRAM],[ lt_aix_libpath_sed='[ /Import File Strings/,/^$/ { /^0/ { s/^0 *\([^ ]*\) *$/\1/ p } }]' _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=`dump -H conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` # Check for a 64-bit object if we didn't find anything. if test -z "$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])"; then _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=`dump -HX64 conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` fi],[]) if test -z "$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])"; then _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=/usr/lib:/lib fi ]) aix_libpath=$_LT_TAGVAR([lt_cv_aix_libpath_], [$1]) fi ])# _LT_SYS_MODULE_PATH_AIX # _LT_SHELL_INIT(ARG) # ------------------- m4_define([_LT_SHELL_INIT], [m4_divert_text([M4SH-INIT], [$1 ])])# _LT_SHELL_INIT # _LT_PROG_ECHO_BACKSLASH # ----------------------- # Find how we can fake an echo command that does not interpret backslash. # In particular, with Autoconf 2.60 or later we add some code to the start # of the generated configure script that will find a shell with a builtin # printf (that we can use as an echo command). m4_defun([_LT_PROG_ECHO_BACKSLASH], [ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO AC_MSG_CHECKING([how to print strings]) # Test print first, because it will be a builtin if present. if test "X`( print -r -- -n ) 2>/dev/null`" = X-n && \ test "X`print -r -- $ECHO 2>/dev/null`" = "X$ECHO"; then ECHO='print -r --' elif test "X`printf %s $ECHO 2>/dev/null`" = "X$ECHO"; then ECHO='printf %s\n' else # Use this function as a fallback that always works. func_fallback_echo () { eval 'cat <<_LTECHO_EOF $[]1 _LTECHO_EOF' } ECHO='func_fallback_echo' fi # func_echo_all arg... # Invoke $ECHO with all args, space-separated. func_echo_all () { $ECHO "$*" } case $ECHO in printf*) AC_MSG_RESULT([printf]) ;; print*) AC_MSG_RESULT([print -r]) ;; *) AC_MSG_RESULT([cat]) ;; esac m4_ifdef([_AS_DETECT_SUGGESTED], [_AS_DETECT_SUGGESTED([ test -n "${ZSH_VERSION+set}${BASH_VERSION+set}" || ( ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO PATH=/empty FPATH=/empty; export PATH FPATH test "X`printf %s $ECHO`" = "X$ECHO" \ || test "X`print -r -- $ECHO`" = "X$ECHO" )])]) _LT_DECL([], [SHELL], [1], [Shell to use when invoking shell scripts]) _LT_DECL([], [ECHO], [1], [An echo program that protects backslashes]) ])# _LT_PROG_ECHO_BACKSLASH # _LT_WITH_SYSROOT # ---------------- AC_DEFUN([_LT_WITH_SYSROOT], [AC_MSG_CHECKING([for sysroot]) AC_ARG_WITH([sysroot], [AS_HELP_STRING([--with-sysroot@<:@=DIR@:>@], [Search for dependent libraries within DIR (or the compiler's sysroot if not specified).])], [], [with_sysroot=no]) dnl lt_sysroot will always be passed unquoted. We quote it here dnl in case the user passed a directory name. lt_sysroot= case $with_sysroot in #( yes) if test yes = "$GCC"; then lt_sysroot=`$CC --print-sysroot 2>/dev/null` fi ;; #( /*) lt_sysroot=`echo "$with_sysroot" | sed -e "$sed_quote_subst"` ;; #( no|'') ;; #( *) AC_MSG_RESULT([$with_sysroot]) AC_MSG_ERROR([The sysroot must be an absolute path.]) ;; esac AC_MSG_RESULT([${lt_sysroot:-no}]) _LT_DECL([], [lt_sysroot], [0], [The root where to search for ]dnl [dependent libraries, and where our libraries should be installed.])]) # _LT_ENABLE_LOCK # --------------- m4_defun([_LT_ENABLE_LOCK], [AC_ARG_ENABLE([libtool-lock], [AS_HELP_STRING([--disable-libtool-lock], [avoid locking (might break parallel builds)])]) test no = "$enable_libtool_lock" || enable_libtool_lock=yes # Some flags need to be propagated to the compiler or linker for good # libtool support. case $host in ia64-*-hpux*) # Find out what ABI is being produced by ac_compile, and set mode # options accordingly. echo 'int i;' > conftest.$ac_ext if AC_TRY_EVAL(ac_compile); then case `/usr/bin/file conftest.$ac_objext` in *ELF-32*) HPUX_IA64_MODE=32 ;; *ELF-64*) HPUX_IA64_MODE=64 ;; esac fi rm -rf conftest* ;; *-*-irix6*) # Find out what ABI is being produced by ac_compile, and set linker # options accordingly. echo '[#]line '$LINENO' "configure"' > conftest.$ac_ext if AC_TRY_EVAL(ac_compile); then if test yes = "$lt_cv_prog_gnu_ld"; then case `/usr/bin/file conftest.$ac_objext` in *32-bit*) LD="${LD-ld} -melf32bsmip" ;; *N32*) LD="${LD-ld} -melf32bmipn32" ;; *64-bit*) LD="${LD-ld} -melf64bmip" ;; esac else case `/usr/bin/file conftest.$ac_objext` in *32-bit*) LD="${LD-ld} -32" ;; *N32*) LD="${LD-ld} -n32" ;; *64-bit*) LD="${LD-ld} -64" ;; esac fi fi rm -rf conftest* ;; mips64*-*linux*) # Find out what ABI is being produced by ac_compile, and set linker # options accordingly. echo '[#]line '$LINENO' "configure"' > conftest.$ac_ext if AC_TRY_EVAL(ac_compile); then emul=elf case `/usr/bin/file conftest.$ac_objext` in *32-bit*) emul="${emul}32" ;; *64-bit*) emul="${emul}64" ;; esac case `/usr/bin/file conftest.$ac_objext` in *MSB*) emul="${emul}btsmip" ;; *LSB*) emul="${emul}ltsmip" ;; esac case `/usr/bin/file conftest.$ac_objext` in *N32*) emul="${emul}n32" ;; esac LD="${LD-ld} -m $emul" fi rm -rf conftest* ;; x86_64-*kfreebsd*-gnu|x86_64-*linux*|powerpc*-*linux*| \ s390*-*linux*|s390*-*tpf*|sparc*-*linux*) # Find out what ABI is being produced by ac_compile, and set linker # options accordingly. Note that the listed cases only cover the # situations where additional linker options are needed (such as when # doing 32-bit compilation for a host where ld defaults to 64-bit, or # vice versa); the common cases where no linker options are needed do # not appear in the list. echo 'int i;' > conftest.$ac_ext if AC_TRY_EVAL(ac_compile); then case `/usr/bin/file conftest.o` in *32-bit*) case $host in x86_64-*kfreebsd*-gnu) LD="${LD-ld} -m elf_i386_fbsd" ;; x86_64-*linux*) case `/usr/bin/file conftest.o` in *x86-64*) LD="${LD-ld} -m elf32_x86_64" ;; *) LD="${LD-ld} -m elf_i386" ;; esac ;; powerpc64le-*linux*) LD="${LD-ld} -m elf32lppclinux" ;; powerpc64-*linux*) LD="${LD-ld} -m elf32ppclinux" ;; s390x-*linux*) LD="${LD-ld} -m elf_s390" ;; sparc64-*linux*) LD="${LD-ld} -m elf32_sparc" ;; esac ;; *64-bit*) case $host in x86_64-*kfreebsd*-gnu) LD="${LD-ld} -m elf_x86_64_fbsd" ;; x86_64-*linux*) LD="${LD-ld} -m elf_x86_64" ;; powerpcle-*linux*) LD="${LD-ld} -m elf64lppc" ;; powerpc-*linux*) LD="${LD-ld} -m elf64ppc" ;; s390*-*linux*|s390*-*tpf*) LD="${LD-ld} -m elf64_s390" ;; sparc*-*linux*) LD="${LD-ld} -m elf64_sparc" ;; esac ;; esac fi rm -rf conftest* ;; *-*-sco3.2v5*) # On SCO OpenServer 5, we need -belf to get full-featured binaries. SAVE_CFLAGS=$CFLAGS CFLAGS="$CFLAGS -belf" AC_CACHE_CHECK([whether the C compiler needs -belf], lt_cv_cc_needs_belf, [AC_LANG_PUSH(C) AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],[[]])],[lt_cv_cc_needs_belf=yes],[lt_cv_cc_needs_belf=no]) AC_LANG_POP]) if test yes != "$lt_cv_cc_needs_belf"; then # this is probably gcc 2.8.0, egcs 1.0 or newer; no need for -belf CFLAGS=$SAVE_CFLAGS fi ;; *-*solaris*) # Find out what ABI is being produced by ac_compile, and set linker # options accordingly. echo 'int i;' > conftest.$ac_ext if AC_TRY_EVAL(ac_compile); then case `/usr/bin/file conftest.o` in *64-bit*) case $lt_cv_prog_gnu_ld in yes*) case $host in i?86-*-solaris*|x86_64-*-solaris*) LD="${LD-ld} -m elf_x86_64" ;; sparc*-*-solaris*) LD="${LD-ld} -m elf64_sparc" ;; esac # GNU ld 2.21 introduced _sol2 emulations. Use them if available. if ${LD-ld} -V | grep _sol2 >/dev/null 2>&1; then LD=${LD-ld}_sol2 fi ;; *) if ${LD-ld} -64 -r -o conftest2.o conftest.o >/dev/null 2>&1; then LD="${LD-ld} -64" fi ;; esac ;; esac fi rm -rf conftest* ;; esac need_locks=$enable_libtool_lock ])# _LT_ENABLE_LOCK # _LT_PROG_AR # ----------- m4_defun([_LT_PROG_AR], [AC_CHECK_TOOLS(AR, [ar], false) : ${AR=ar} : ${AR_FLAGS=cru} _LT_DECL([], [AR], [1], [The archiver]) _LT_DECL([], [AR_FLAGS], [1], [Flags to create an archive]) AC_CACHE_CHECK([for archiver @FILE support], [lt_cv_ar_at_file], [lt_cv_ar_at_file=no AC_COMPILE_IFELSE([AC_LANG_PROGRAM], [echo conftest.$ac_objext > conftest.lst lt_ar_try='$AR $AR_FLAGS libconftest.a @conftest.lst >&AS_MESSAGE_LOG_FD' AC_TRY_EVAL([lt_ar_try]) if test 0 -eq "$ac_status"; then # Ensure the archiver fails upon bogus file names. rm -f conftest.$ac_objext libconftest.a AC_TRY_EVAL([lt_ar_try]) if test 0 -ne "$ac_status"; then lt_cv_ar_at_file=@ fi fi rm -f conftest.* libconftest.a ]) ]) if test no = "$lt_cv_ar_at_file"; then archiver_list_spec= else archiver_list_spec=$lt_cv_ar_at_file fi _LT_DECL([], [archiver_list_spec], [1], [How to feed a file listing to the archiver]) ])# _LT_PROG_AR # _LT_CMD_OLD_ARCHIVE # ------------------- m4_defun([_LT_CMD_OLD_ARCHIVE], [_LT_PROG_AR AC_CHECK_TOOL(STRIP, strip, :) test -z "$STRIP" && STRIP=: _LT_DECL([], [STRIP], [1], [A symbol stripping program]) AC_CHECK_TOOL(RANLIB, ranlib, :) test -z "$RANLIB" && RANLIB=: _LT_DECL([], [RANLIB], [1], [Commands used to install an old-style archive]) # Determine commands to create old-style static archives. old_archive_cmds='$AR $AR_FLAGS $oldlib$oldobjs' old_postinstall_cmds='chmod 644 $oldlib' old_postuninstall_cmds= if test -n "$RANLIB"; then case $host_os in bitrig* | openbsd*) old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB -t \$tool_oldlib" ;; *) old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB \$tool_oldlib" ;; esac old_archive_cmds="$old_archive_cmds~\$RANLIB \$tool_oldlib" fi case $host_os in darwin*) lock_old_archive_extraction=yes ;; *) lock_old_archive_extraction=no ;; esac _LT_DECL([], [old_postinstall_cmds], [2]) _LT_DECL([], [old_postuninstall_cmds], [2]) _LT_TAGDECL([], [old_archive_cmds], [2], [Commands used to build an old-style archive]) _LT_DECL([], [lock_old_archive_extraction], [0], [Whether to use a lock for old archive extraction]) ])# _LT_CMD_OLD_ARCHIVE # _LT_COMPILER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS, # [OUTPUT-FILE], [ACTION-SUCCESS], [ACTION-FAILURE]) # ---------------------------------------------------------------- # Check whether the given compiler option works AC_DEFUN([_LT_COMPILER_OPTION], [m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_DECL_SED])dnl AC_CACHE_CHECK([$1], [$2], [$2=no m4_if([$4], , [ac_outfile=conftest.$ac_objext], [ac_outfile=$4]) echo "$lt_simple_compile_test_code" > conftest.$ac_ext lt_compiler_flag="$3" ## exclude from sc_useless_quotes_in_assignment # Insert the option either (1) after the last *FLAGS variable, or # (2) before a word containing "conftest.", or (3) at the end. # Note that $ac_compile itself does not contain backslashes and begins # with a dollar sign (not a hyphen), so the echo should work correctly. # The option is referenced via a variable to avoid confusing sed. lt_compile=`echo "$ac_compile" | $SED \ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&AS_MESSAGE_LOG_FD) (eval "$lt_compile" 2>conftest.err) ac_status=$? cat conftest.err >&AS_MESSAGE_LOG_FD echo "$as_me:$LINENO: \$? = $ac_status" >&AS_MESSAGE_LOG_FD if (exit $ac_status) && test -s "$ac_outfile"; then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings other than the usual output. $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' >conftest.exp $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then $2=yes fi fi $RM conftest* ]) if test yes = "[$]$2"; then m4_if([$5], , :, [$5]) else m4_if([$6], , :, [$6]) fi ])# _LT_COMPILER_OPTION # Old name: AU_ALIAS([AC_LIBTOOL_COMPILER_OPTION], [_LT_COMPILER_OPTION]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_COMPILER_OPTION], []) # _LT_LINKER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS, # [ACTION-SUCCESS], [ACTION-FAILURE]) # ---------------------------------------------------- # Check whether the given linker option works AC_DEFUN([_LT_LINKER_OPTION], [m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_DECL_SED])dnl AC_CACHE_CHECK([$1], [$2], [$2=no save_LDFLAGS=$LDFLAGS LDFLAGS="$LDFLAGS $3" echo "$lt_simple_link_test_code" > conftest.$ac_ext if (eval $ac_link 2>conftest.err) && test -s conftest$ac_exeext; then # The linker can only warn and ignore the option if not recognized # So say no if there are warnings if test -s conftest.err; then # Append any errors to the config.log. cat conftest.err 1>&AS_MESSAGE_LOG_FD $ECHO "$_lt_linker_boilerplate" | $SED '/^$/d' > conftest.exp $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 if diff conftest.exp conftest.er2 >/dev/null; then $2=yes fi else $2=yes fi fi $RM -r conftest* LDFLAGS=$save_LDFLAGS ]) if test yes = "[$]$2"; then m4_if([$4], , :, [$4]) else m4_if([$5], , :, [$5]) fi ])# _LT_LINKER_OPTION # Old name: AU_ALIAS([AC_LIBTOOL_LINKER_OPTION], [_LT_LINKER_OPTION]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_LINKER_OPTION], []) # LT_CMD_MAX_LEN #--------------- AC_DEFUN([LT_CMD_MAX_LEN], [AC_REQUIRE([AC_CANONICAL_HOST])dnl # find the maximum length of command line arguments AC_MSG_CHECKING([the maximum length of command line arguments]) AC_CACHE_VAL([lt_cv_sys_max_cmd_len], [dnl i=0 teststring=ABCD case $build_os in msdosdjgpp*) # On DJGPP, this test can blow up pretty badly due to problems in libc # (any single argument exceeding 2000 bytes causes a buffer overrun # during glob expansion). Even if it were fixed, the result of this # check would be larger than it should be. lt_cv_sys_max_cmd_len=12288; # 12K is about right ;; gnu*) # Under GNU Hurd, this test is not required because there is # no limit to the length of command line arguments. # Libtool will interpret -1 as no limit whatsoever lt_cv_sys_max_cmd_len=-1; ;; cygwin* | mingw* | cegcc*) # On Win9x/ME, this test blows up -- it succeeds, but takes # about 5 minutes as the teststring grows exponentially. # Worse, since 9x/ME are not pre-emptively multitasking, # you end up with a "frozen" computer, even though with patience # the test eventually succeeds (with a max line length of 256k). # Instead, let's just punt: use the minimum linelength reported by # all of the supported platforms: 8192 (on NT/2K/XP). lt_cv_sys_max_cmd_len=8192; ;; mint*) # On MiNT this can take a long time and run out of memory. lt_cv_sys_max_cmd_len=8192; ;; amigaos*) # On AmigaOS with pdksh, this test takes hours, literally. # So we just punt and use a minimum line length of 8192. lt_cv_sys_max_cmd_len=8192; ;; bitrig* | darwin* | dragonfly* | freebsd* | netbsd* | openbsd*) # This has been around since 386BSD, at least. Likely further. if test -x /sbin/sysctl; then lt_cv_sys_max_cmd_len=`/sbin/sysctl -n kern.argmax` elif test -x /usr/sbin/sysctl; then lt_cv_sys_max_cmd_len=`/usr/sbin/sysctl -n kern.argmax` else lt_cv_sys_max_cmd_len=65536 # usable default for all BSDs fi # And add a safety zone lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4` lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3` ;; interix*) # We know the value 262144 and hardcode it with a safety zone (like BSD) lt_cv_sys_max_cmd_len=196608 ;; os2*) # The test takes a long time on OS/2. lt_cv_sys_max_cmd_len=8192 ;; osf*) # Dr. Hans Ekkehard Plesser reports seeing a kernel panic running configure # due to this test when exec_disable_arg_limit is 1 on Tru64. It is not # nice to cause kernel panics so lets avoid the loop below. # First set a reasonable default. lt_cv_sys_max_cmd_len=16384 # if test -x /sbin/sysconfig; then case `/sbin/sysconfig -q proc exec_disable_arg_limit` in *1*) lt_cv_sys_max_cmd_len=-1 ;; esac fi ;; sco3.2v5*) lt_cv_sys_max_cmd_len=102400 ;; sysv5* | sco5v6* | sysv4.2uw2*) kargmax=`grep ARG_MAX /etc/conf/cf.d/stune 2>/dev/null` if test -n "$kargmax"; then lt_cv_sys_max_cmd_len=`echo $kargmax | sed 's/.*[[ ]]//'` else lt_cv_sys_max_cmd_len=32768 fi ;; *) lt_cv_sys_max_cmd_len=`(getconf ARG_MAX) 2> /dev/null` if test -n "$lt_cv_sys_max_cmd_len" && \ test undefined != "$lt_cv_sys_max_cmd_len"; then lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4` lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3` else # Make teststring a little bigger before we do anything with it. # a 1K string should be a reasonable start. for i in 1 2 3 4 5 6 7 8; do teststring=$teststring$teststring done SHELL=${SHELL-${CONFIG_SHELL-/bin/sh}} # If test is not a shell built-in, we'll probably end up computing a # maximum length that is only half of the actual maximum length, but # we can't tell. while { test X`env echo "$teststring$teststring" 2>/dev/null` \ = "X$teststring$teststring"; } >/dev/null 2>&1 && test 17 != "$i" # 1/2 MB should be enough do i=`expr $i + 1` teststring=$teststring$teststring done # Only check the string length outside the loop. lt_cv_sys_max_cmd_len=`expr "X$teststring" : ".*" 2>&1` teststring= # Add a significant safety factor because C++ compilers can tack on # massive amounts of additional arguments before passing them to the # linker. It appears as though 1/2 is a usable value. lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 2` fi ;; esac ]) if test -n "$lt_cv_sys_max_cmd_len"; then AC_MSG_RESULT($lt_cv_sys_max_cmd_len) else AC_MSG_RESULT(none) fi max_cmd_len=$lt_cv_sys_max_cmd_len _LT_DECL([], [max_cmd_len], [0], [What is the maximum length of a command?]) ])# LT_CMD_MAX_LEN # Old name: AU_ALIAS([AC_LIBTOOL_SYS_MAX_CMD_LEN], [LT_CMD_MAX_LEN]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_SYS_MAX_CMD_LEN], []) # _LT_HEADER_DLFCN # ---------------- m4_defun([_LT_HEADER_DLFCN], [AC_CHECK_HEADERS([dlfcn.h], [], [], [AC_INCLUDES_DEFAULT])dnl ])# _LT_HEADER_DLFCN # _LT_TRY_DLOPEN_SELF (ACTION-IF-TRUE, ACTION-IF-TRUE-W-USCORE, # ACTION-IF-FALSE, ACTION-IF-CROSS-COMPILING) # ---------------------------------------------------------------- m4_defun([_LT_TRY_DLOPEN_SELF], [m4_require([_LT_HEADER_DLFCN])dnl if test yes = "$cross_compiling"; then : [$4] else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF [#line $LINENO "configure" #include "confdefs.h" #if HAVE_DLFCN_H #include #endif #include #ifdef RTLD_GLOBAL # define LT_DLGLOBAL RTLD_GLOBAL #else # ifdef DL_GLOBAL # define LT_DLGLOBAL DL_GLOBAL # else # define LT_DLGLOBAL 0 # endif #endif /* We may have to define LT_DLLAZY_OR_NOW in the command line if we find out it does not work in some platform. */ #ifndef LT_DLLAZY_OR_NOW # ifdef RTLD_LAZY # define LT_DLLAZY_OR_NOW RTLD_LAZY # else # ifdef DL_LAZY # define LT_DLLAZY_OR_NOW DL_LAZY # else # ifdef RTLD_NOW # define LT_DLLAZY_OR_NOW RTLD_NOW # else # ifdef DL_NOW # define LT_DLLAZY_OR_NOW DL_NOW # else # define LT_DLLAZY_OR_NOW 0 # endif # endif # endif # endif #endif /* When -fvisibility=hidden is used, assume the code has been annotated correspondingly for the symbols needed. */ #if defined __GNUC__ && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)) int fnord () __attribute__((visibility("default"))); #endif int fnord () { return 42; } int main () { void *self = dlopen (0, LT_DLGLOBAL|LT_DLLAZY_OR_NOW); int status = $lt_dlunknown; if (self) { if (dlsym (self,"fnord")) status = $lt_dlno_uscore; else { if (dlsym( self,"_fnord")) status = $lt_dlneed_uscore; else puts (dlerror ()); } /* dlclose (self); */ } else puts (dlerror ()); return status; }] _LT_EOF if AC_TRY_EVAL(ac_link) && test -s "conftest$ac_exeext" 2>/dev/null; then (./conftest; exit; ) >&AS_MESSAGE_LOG_FD 2>/dev/null lt_status=$? case x$lt_status in x$lt_dlno_uscore) $1 ;; x$lt_dlneed_uscore) $2 ;; x$lt_dlunknown|x*) $3 ;; esac else : # compilation failed $3 fi fi rm -fr conftest* ])# _LT_TRY_DLOPEN_SELF # LT_SYS_DLOPEN_SELF # ------------------ AC_DEFUN([LT_SYS_DLOPEN_SELF], [m4_require([_LT_HEADER_DLFCN])dnl if test yes != "$enable_dlopen"; then enable_dlopen=unknown enable_dlopen_self=unknown enable_dlopen_self_static=unknown else lt_cv_dlopen=no lt_cv_dlopen_libs= case $host_os in beos*) lt_cv_dlopen=load_add_on lt_cv_dlopen_libs= lt_cv_dlopen_self=yes ;; mingw* | pw32* | cegcc*) lt_cv_dlopen=LoadLibrary lt_cv_dlopen_libs= ;; cygwin*) lt_cv_dlopen=dlopen lt_cv_dlopen_libs= ;; darwin*) # if libdl is installed we need to link against it AC_CHECK_LIB([dl], [dlopen], [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl],[ lt_cv_dlopen=dyld lt_cv_dlopen_libs= lt_cv_dlopen_self=yes ]) ;; tpf*) # Don't try to run any link tests for TPF. We know it's impossible # because TPF is a cross-compiler, and we know how we open DSOs. lt_cv_dlopen=dlopen lt_cv_dlopen_libs= lt_cv_dlopen_self=no ;; *) AC_CHECK_FUNC([shl_load], [lt_cv_dlopen=shl_load], [AC_CHECK_LIB([dld], [shl_load], [lt_cv_dlopen=shl_load lt_cv_dlopen_libs=-ldld], [AC_CHECK_FUNC([dlopen], [lt_cv_dlopen=dlopen], [AC_CHECK_LIB([dl], [dlopen], [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl], [AC_CHECK_LIB([svld], [dlopen], [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-lsvld], [AC_CHECK_LIB([dld], [dld_link], [lt_cv_dlopen=dld_link lt_cv_dlopen_libs=-ldld]) ]) ]) ]) ]) ]) ;; esac if test no = "$lt_cv_dlopen"; then enable_dlopen=no else enable_dlopen=yes fi case $lt_cv_dlopen in dlopen) save_CPPFLAGS=$CPPFLAGS test yes = "$ac_cv_header_dlfcn_h" && CPPFLAGS="$CPPFLAGS -DHAVE_DLFCN_H" save_LDFLAGS=$LDFLAGS wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $export_dynamic_flag_spec\" save_LIBS=$LIBS LIBS="$lt_cv_dlopen_libs $LIBS" AC_CACHE_CHECK([whether a program can dlopen itself], lt_cv_dlopen_self, [dnl _LT_TRY_DLOPEN_SELF( lt_cv_dlopen_self=yes, lt_cv_dlopen_self=yes, lt_cv_dlopen_self=no, lt_cv_dlopen_self=cross) ]) if test yes = "$lt_cv_dlopen_self"; then wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $lt_prog_compiler_static\" AC_CACHE_CHECK([whether a statically linked program can dlopen itself], lt_cv_dlopen_self_static, [dnl _LT_TRY_DLOPEN_SELF( lt_cv_dlopen_self_static=yes, lt_cv_dlopen_self_static=yes, lt_cv_dlopen_self_static=no, lt_cv_dlopen_self_static=cross) ]) fi CPPFLAGS=$save_CPPFLAGS LDFLAGS=$save_LDFLAGS LIBS=$save_LIBS ;; esac case $lt_cv_dlopen_self in yes|no) enable_dlopen_self=$lt_cv_dlopen_self ;; *) enable_dlopen_self=unknown ;; esac case $lt_cv_dlopen_self_static in yes|no) enable_dlopen_self_static=$lt_cv_dlopen_self_static ;; *) enable_dlopen_self_static=unknown ;; esac fi _LT_DECL([dlopen_support], [enable_dlopen], [0], [Whether dlopen is supported]) _LT_DECL([dlopen_self], [enable_dlopen_self], [0], [Whether dlopen of programs is supported]) _LT_DECL([dlopen_self_static], [enable_dlopen_self_static], [0], [Whether dlopen of statically linked programs is supported]) ])# LT_SYS_DLOPEN_SELF # Old name: AU_ALIAS([AC_LIBTOOL_DLOPEN_SELF], [LT_SYS_DLOPEN_SELF]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_DLOPEN_SELF], []) # _LT_COMPILER_C_O([TAGNAME]) # --------------------------- # Check to see if options -c and -o are simultaneously supported by compiler. # This macro does not hard code the compiler like AC_PROG_CC_C_O. m4_defun([_LT_COMPILER_C_O], [m4_require([_LT_DECL_SED])dnl m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_TAG_COMPILER])dnl AC_CACHE_CHECK([if $compiler supports -c -o file.$ac_objext], [_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)], [_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=no $RM -r conftest 2>/dev/null mkdir conftest cd conftest mkdir out echo "$lt_simple_compile_test_code" > conftest.$ac_ext lt_compiler_flag="-o out/conftest2.$ac_objext" # Insert the option either (1) after the last *FLAGS variable, or # (2) before a word containing "conftest.", or (3) at the end. # Note that $ac_compile itself does not contain backslashes and begins # with a dollar sign (not a hyphen), so the echo should work correctly. lt_compile=`echo "$ac_compile" | $SED \ -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \ -e 's:$: $lt_compiler_flag:'` (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&AS_MESSAGE_LOG_FD) (eval "$lt_compile" 2>out/conftest.err) ac_status=$? cat out/conftest.err >&AS_MESSAGE_LOG_FD echo "$as_me:$LINENO: \$? = $ac_status" >&AS_MESSAGE_LOG_FD if (exit $ac_status) && test -s out/conftest2.$ac_objext then # The compiler can only warn and ignore the option if not recognized # So say no if there are warnings $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' > out/conftest.exp $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2 if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then _LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes fi fi chmod u+w . 2>&AS_MESSAGE_LOG_FD $RM conftest* # SGI C++ compiler will create directory out/ii_files/ for # template instantiation test -d out/ii_files && $RM out/ii_files/* && rmdir out/ii_files $RM out/* && rmdir out cd .. $RM -r conftest $RM conftest* ]) _LT_TAGDECL([compiler_c_o], [lt_cv_prog_compiler_c_o], [1], [Does compiler simultaneously support -c and -o options?]) ])# _LT_COMPILER_C_O # _LT_COMPILER_FILE_LOCKS([TAGNAME]) # ---------------------------------- # Check to see if we can do hard links to lock some files if needed m4_defun([_LT_COMPILER_FILE_LOCKS], [m4_require([_LT_ENABLE_LOCK])dnl m4_require([_LT_FILEUTILS_DEFAULTS])dnl _LT_COMPILER_C_O([$1]) hard_links=nottested if test no = "$_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)" && test no != "$need_locks"; then # do not overwrite the value of need_locks provided by the user AC_MSG_CHECKING([if we can lock with hard links]) hard_links=yes $RM conftest* ln conftest.a conftest.b 2>/dev/null && hard_links=no touch conftest.a ln conftest.a conftest.b 2>&5 || hard_links=no ln conftest.a conftest.b 2>/dev/null && hard_links=no AC_MSG_RESULT([$hard_links]) if test no = "$hard_links"; then AC_MSG_WARN(['$CC' does not support '-c -o', so 'make -j' may be unsafe]) need_locks=warn fi else need_locks=no fi _LT_DECL([], [need_locks], [1], [Must we lock files when doing compilation?]) ])# _LT_COMPILER_FILE_LOCKS # _LT_CHECK_OBJDIR # ---------------- m4_defun([_LT_CHECK_OBJDIR], [AC_CACHE_CHECK([for objdir], [lt_cv_objdir], [rm -f .libs 2>/dev/null mkdir .libs 2>/dev/null if test -d .libs; then lt_cv_objdir=.libs else # MS-DOS does not allow filenames that begin with a dot. lt_cv_objdir=_libs fi rmdir .libs 2>/dev/null]) objdir=$lt_cv_objdir _LT_DECL([], [objdir], [0], [The name of the directory that contains temporary libtool files])dnl m4_pattern_allow([LT_OBJDIR])dnl AC_DEFINE_UNQUOTED([LT_OBJDIR], "$lt_cv_objdir/", [Define to the sub-directory where libtool stores uninstalled libraries.]) ])# _LT_CHECK_OBJDIR # _LT_LINKER_HARDCODE_LIBPATH([TAGNAME]) # -------------------------------------- # Check hardcoding attributes. m4_defun([_LT_LINKER_HARDCODE_LIBPATH], [AC_MSG_CHECKING([how to hardcode library paths into programs]) _LT_TAGVAR(hardcode_action, $1)= if test -n "$_LT_TAGVAR(hardcode_libdir_flag_spec, $1)" || test -n "$_LT_TAGVAR(runpath_var, $1)" || test yes = "$_LT_TAGVAR(hardcode_automatic, $1)"; then # We can hardcode non-existent directories. if test no != "$_LT_TAGVAR(hardcode_direct, $1)" && # If the only mechanism to avoid hardcoding is shlibpath_var, we # have to relink, otherwise we might link with an installed library # when we should be linking with a yet-to-be-installed one ## test no != "$_LT_TAGVAR(hardcode_shlibpath_var, $1)" && test no != "$_LT_TAGVAR(hardcode_minus_L, $1)"; then # Linking always hardcodes the temporary library directory. _LT_TAGVAR(hardcode_action, $1)=relink else # We can link without hardcoding, and we can hardcode nonexisting dirs. _LT_TAGVAR(hardcode_action, $1)=immediate fi else # We cannot hardcode anything, or else we can only hardcode existing # directories. _LT_TAGVAR(hardcode_action, $1)=unsupported fi AC_MSG_RESULT([$_LT_TAGVAR(hardcode_action, $1)]) if test relink = "$_LT_TAGVAR(hardcode_action, $1)" || test yes = "$_LT_TAGVAR(inherit_rpath, $1)"; then # Fast installation is not supported enable_fast_install=no elif test yes = "$shlibpath_overrides_runpath" || test no = "$enable_shared"; then # Fast installation is not necessary enable_fast_install=needless fi _LT_TAGDECL([], [hardcode_action], [0], [How to hardcode a shared library path into an executable]) ])# _LT_LINKER_HARDCODE_LIBPATH # _LT_CMD_STRIPLIB # ---------------- m4_defun([_LT_CMD_STRIPLIB], [m4_require([_LT_DECL_EGREP]) striplib= old_striplib= AC_MSG_CHECKING([whether stripping libraries is possible]) if test -n "$STRIP" && $STRIP -V 2>&1 | $GREP "GNU strip" >/dev/null; then test -z "$old_striplib" && old_striplib="$STRIP --strip-debug" test -z "$striplib" && striplib="$STRIP --strip-unneeded" AC_MSG_RESULT([yes]) else # FIXME - insert some real tests, host_os isn't really good enough case $host_os in darwin*) if test -n "$STRIP"; then striplib="$STRIP -x" old_striplib="$STRIP -S" AC_MSG_RESULT([yes]) else AC_MSG_RESULT([no]) fi ;; *) AC_MSG_RESULT([no]) ;; esac fi _LT_DECL([], [old_striplib], [1], [Commands to strip libraries]) _LT_DECL([], [striplib], [1]) ])# _LT_CMD_STRIPLIB # _LT_PREPARE_MUNGE_PATH_LIST # --------------------------- # Make sure func_munge_path_list() is defined correctly. m4_defun([_LT_PREPARE_MUNGE_PATH_LIST], [[# func_munge_path_list VARIABLE PATH # ----------------------------------- # VARIABLE is name of variable containing _space_ separated list of # directories to be munged by the contents of PATH, which is string # having a format: # "DIR[:DIR]:" # string "DIR[ DIR]" will be prepended to VARIABLE # ":DIR[:DIR]" # string "DIR[ DIR]" will be appended to VARIABLE # "DIRP[:DIRP]::[DIRA:]DIRA" # string "DIRP[ DIRP]" will be prepended to VARIABLE and string # "DIRA[ DIRA]" will be appended to VARIABLE # "DIR[:DIR]" # VARIABLE will be replaced by "DIR[ DIR]" func_munge_path_list () { case x@S|@2 in x) ;; *:) eval @S|@1=\"`$ECHO @S|@2 | $SED 's/:/ /g'` \@S|@@S|@1\" ;; x:*) eval @S|@1=\"\@S|@@S|@1 `$ECHO @S|@2 | $SED 's/:/ /g'`\" ;; *::*) eval @S|@1=\"\@S|@@S|@1\ `$ECHO @S|@2 | $SED -e 's/.*:://' -e 's/:/ /g'`\" eval @S|@1=\"`$ECHO @S|@2 | $SED -e 's/::.*//' -e 's/:/ /g'`\ \@S|@@S|@1\" ;; *) eval @S|@1=\"`$ECHO @S|@2 | $SED 's/:/ /g'`\" ;; esac } ]])# _LT_PREPARE_PATH_LIST # _LT_SYS_DYNAMIC_LINKER([TAG]) # ----------------------------- # PORTME Fill in your ld.so characteristics m4_defun([_LT_SYS_DYNAMIC_LINKER], [AC_REQUIRE([AC_CANONICAL_HOST])dnl m4_require([_LT_DECL_EGREP])dnl m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_DECL_OBJDUMP])dnl m4_require([_LT_DECL_SED])dnl m4_require([_LT_CHECK_SHELL_FEATURES])dnl m4_require([_LT_PREPARE_MUNGE_PATH_LIST])dnl AC_MSG_CHECKING([dynamic linker characteristics]) m4_if([$1], [], [ if test yes = "$GCC"; then case $host_os in darwin*) lt_awk_arg='/^libraries:/,/LR/' ;; *) lt_awk_arg='/^libraries:/' ;; esac case $host_os in mingw* | cegcc*) lt_sed_strip_eq='s|=\([[A-Za-z]]:\)|\1|g' ;; *) lt_sed_strip_eq='s|=/|/|g' ;; esac lt_search_path_spec=`$CC -print-search-dirs | awk $lt_awk_arg | $SED -e "s/^libraries://" -e $lt_sed_strip_eq` case $lt_search_path_spec in *\;*) # if the path contains ";" then we assume it to be the separator # otherwise default to the standard path separator (i.e. ":") - it is # assumed that no part of a normal pathname contains ";" but that should # okay in the real world where ";" in dirpaths is itself problematic. lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED 's/;/ /g'` ;; *) lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED "s/$PATH_SEPARATOR/ /g"` ;; esac # Ok, now we have the path, separated by spaces, we can step through it # and add multilib dir if necessary... lt_tmp_lt_search_path_spec= lt_multi_os_dir=/`$CC $CPPFLAGS $CFLAGS $LDFLAGS -print-multi-os-directory 2>/dev/null` # ...but if some path component already ends with the multilib dir we assume # that all is fine and trust -print-search-dirs as is (GCC 4.2? or newer). case "$lt_multi_os_dir; $lt_search_path_spec " in "/; "* | "/.; "* | "/./; "* | *"$lt_multi_os_dir "* | *"$lt_multi_os_dir/ "*) lt_multi_os_dir= ;; esac for lt_sys_path in $lt_search_path_spec; do if test -d "$lt_sys_path$lt_multi_os_dir"; then lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path$lt_multi_os_dir" elif test -n "$lt_multi_os_dir"; then test -d "$lt_sys_path" && \ lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path" fi done lt_search_path_spec=`$ECHO "$lt_tmp_lt_search_path_spec" | awk ' BEGIN {RS = " "; FS = "/|\n";} { lt_foo = ""; lt_count = 0; for (lt_i = NF; lt_i > 0; lt_i--) { if ($lt_i != "" && $lt_i != ".") { if ($lt_i == "..") { lt_count++; } else { if (lt_count == 0) { lt_foo = "/" $lt_i lt_foo; } else { lt_count--; } } } } if (lt_foo != "") { lt_freq[[lt_foo]]++; } if (lt_freq[[lt_foo]] == 1) { print lt_foo; } }'` # AWK program above erroneously prepends '/' to C:/dos/paths # for these hosts. case $host_os in mingw* | cegcc*) lt_search_path_spec=`$ECHO "$lt_search_path_spec" |\ $SED 's|/\([[A-Za-z]]:\)|\1|g'` ;; esac sys_lib_search_path_spec=`$ECHO "$lt_search_path_spec" | $lt_NL2SP` else sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib" fi]) library_names_spec= libname_spec='lib$name' soname_spec= shrext_cmds=.so postinstall_cmds= postuninstall_cmds= finish_cmds= finish_eval= shlibpath_var= shlibpath_overrides_runpath=unknown version_type=none dynamic_linker="$host_os ld.so" sys_lib_dlsearch_path_spec="/lib /usr/lib" need_lib_prefix=unknown hardcode_into_libs=no # when you set need_version to no, make sure it does not cause -set_version # flags to be left without arguments need_version=unknown AC_ARG_VAR([LT_SYS_LIBRARY_PATH], [User-defined run-time library search path.]) case $host_os in aix3*) version_type=linux # correct to gnu/linux during the next big refactor library_names_spec='$libname$release$shared_ext$versuffix $libname.a' shlibpath_var=LIBPATH # AIX 3 has no versioning support, so we append a major version to the name. soname_spec='$libname$release$shared_ext$major' ;; aix[[4-9]]*) version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no hardcode_into_libs=yes if test ia64 = "$host_cpu"; then # AIX 5 supports IA64 library_names_spec='$libname$release$shared_ext$major $libname$release$shared_ext$versuffix $libname$shared_ext' shlibpath_var=LD_LIBRARY_PATH else # With GCC up to 2.95.x, collect2 would create an import file # for dependence libraries. The import file would start with # the line '#! .'. This would cause the generated library to # depend on '.', always an invalid library. This was fixed in # development snapshots of GCC prior to 3.0. case $host_os in aix4 | aix4.[[01]] | aix4.[[01]].*) if { echo '#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97)' echo ' yes ' echo '#endif'; } | $CC -E - | $GREP yes > /dev/null; then : else can_build_shared=no fi ;; esac # Using Import Files as archive members, it is possible to support # filename-based versioning of shared library archives on AIX. While # this would work for both with and without runtime linking, it will # prevent static linking of such archives. So we do filename-based # shared library versioning with .so extension only, which is used # when both runtime linking and shared linking is enabled. # Unfortunately, runtime linking may impact performance, so we do # not want this to be the default eventually. Also, we use the # versioned .so libs for executables only if there is the -brtl # linker flag in LDFLAGS as well, or --with-aix-soname=svr4 only. # To allow for filename-based versioning support, we need to create # libNAME.so.V as an archive file, containing: # *) an Import File, referring to the versioned filename of the # archive as well as the shared archive member, telling the # bitwidth (32 or 64) of that shared object, and providing the # list of exported symbols of that shared object, eventually # decorated with the 'weak' keyword # *) the shared object with the F_LOADONLY flag set, to really avoid # it being seen by the linker. # At run time we better use the real file rather than another symlink, # but for link time we create the symlink libNAME.so -> libNAME.so.V case $with_aix_soname,$aix_use_runtimelinking in # AIX (on Power*) has no versioning support, so currently we cannot hardcode correct # soname into executable. Probably we can add versioning support to # collect2, so additional links can be useful in future. aix,yes) # traditional libtool dynamic_linker='AIX unversionable lib.so' # If using run time linking (on AIX 4.2 or later) use lib.so # instead of lib.a to let people know that these are not # typical AIX shared libraries. library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' ;; aix,no) # traditional AIX only dynamic_linker='AIX lib.a[(]lib.so.V[)]' # We preserve .a as extension for shared libraries through AIX4.2 # and later when we are not doing run time linking. library_names_spec='$libname$release.a $libname.a' soname_spec='$libname$release$shared_ext$major' ;; svr4,*) # full svr4 only dynamic_linker="AIX lib.so.V[(]$shared_archive_member_spec.o[)]" library_names_spec='$libname$release$shared_ext$major $libname$shared_ext' # We do not specify a path in Import Files, so LIBPATH fires. shlibpath_overrides_runpath=yes ;; *,yes) # both, prefer svr4 dynamic_linker="AIX lib.so.V[(]$shared_archive_member_spec.o[)], lib.a[(]lib.so.V[)]" library_names_spec='$libname$release$shared_ext$major $libname$shared_ext' # unpreferred sharedlib libNAME.a needs extra handling postinstall_cmds='test -n "$linkname" || linkname="$realname"~func_stripname "" ".so" "$linkname"~$install_shared_prog "$dir/$func_stripname_result.$libext" "$destdir/$func_stripname_result.$libext"~test -z "$tstripme" || test -z "$striplib" || $striplib "$destdir/$func_stripname_result.$libext"' postuninstall_cmds='for n in $library_names $old_library; do :; done~func_stripname "" ".so" "$n"~test "$func_stripname_result" = "$n" || func_append rmfiles " $odir/$func_stripname_result.$libext"' # We do not specify a path in Import Files, so LIBPATH fires. shlibpath_overrides_runpath=yes ;; *,no) # both, prefer aix dynamic_linker="AIX lib.a[(]lib.so.V[)], lib.so.V[(]$shared_archive_member_spec.o[)]" library_names_spec='$libname$release.a $libname.a' soname_spec='$libname$release$shared_ext$major' # unpreferred sharedlib libNAME.so.V and symlink libNAME.so need extra handling postinstall_cmds='test -z "$dlname" || $install_shared_prog $dir/$dlname $destdir/$dlname~test -z "$tstripme" || test -z "$striplib" || $striplib $destdir/$dlname~test -n "$linkname" || linkname=$realname~func_stripname "" ".a" "$linkname"~(cd "$destdir" && $LN_S -f $dlname $func_stripname_result.so)' postuninstall_cmds='test -z "$dlname" || func_append rmfiles " $odir/$dlname"~for n in $old_library $library_names; do :; done~func_stripname "" ".a" "$n"~func_append rmfiles " $odir/$func_stripname_result.so"' ;; esac shlibpath_var=LIBPATH fi ;; amigaos*) case $host_cpu in powerpc) # Since July 2007 AmigaOS4 officially supports .so libraries. # When compiling the executable, add -use-dynld -Lsobjs: to the compileline. library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' ;; m68k) library_names_spec='$libname.ixlibrary $libname.a' # Create ${libname}_ixlibrary.a entries in /sys/libs. finish_eval='for lib in `ls $libdir/*.ixlibrary 2>/dev/null`; do libname=`func_echo_all "$lib" | $SED '\''s%^.*/\([[^/]]*\)\.ixlibrary$%\1%'\''`; $RM /sys/libs/${libname}_ixlibrary.a; $show "cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a"; cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a || exit 1; done' ;; esac ;; beos*) library_names_spec='$libname$shared_ext' dynamic_linker="$host_os ld.so" shlibpath_var=LIBRARY_PATH ;; bsdi[[45]]*) version_type=linux # correct to gnu/linux during the next big refactor need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' finish_cmds='PATH="\$PATH:/sbin" ldconfig $libdir' shlibpath_var=LD_LIBRARY_PATH sys_lib_search_path_spec="/shlib /usr/lib /usr/X11/lib /usr/contrib/lib /lib /usr/local/lib" sys_lib_dlsearch_path_spec="/shlib /usr/lib /usr/local/lib" # the default ld.so.conf also contains /usr/contrib/lib and # /usr/X11R6/lib (/usr/X11 is a link to /usr/X11R6), but let us allow # libtool to hard-code these into programs ;; cygwin* | mingw* | pw32* | cegcc*) version_type=windows shrext_cmds=.dll need_version=no need_lib_prefix=no case $GCC,$cc_basename in yes,*) # gcc library_names_spec='$libname.dll.a' # DLL is installed to $(libdir)/../bin by postinstall_cmds postinstall_cmds='base_file=`basename \$file`~ dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~ dldir=$destdir/`dirname \$dlpath`~ test -d \$dldir || mkdir -p \$dldir~ $install_prog $dir/$dlname \$dldir/$dlname~ chmod a+x \$dldir/$dlname~ if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then eval '\''$striplib \$dldir/$dlname'\'' || exit \$?; fi' postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~ dlpath=$dir/\$dldll~ $RM \$dlpath' shlibpath_overrides_runpath=yes case $host_os in cygwin*) # Cygwin DLLs use 'cyg' prefix rather than 'lib' soname_spec='`echo $libname | sed -e 's/^lib/cyg/'``echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' m4_if([$1], [],[ sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/lib/w32api"]) ;; mingw* | cegcc*) # MinGW DLLs use traditional 'lib' prefix soname_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' ;; pw32*) # pw32 DLLs use 'pw' prefix rather than 'lib' library_names_spec='`echo $libname | sed -e 's/^lib/pw/'``echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' ;; esac dynamic_linker='Win32 ld.exe' ;; *,cl*) # Native MSVC libname_spec='$name' soname_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' library_names_spec='$libname.dll.lib' case $build_os in mingw*) sys_lib_search_path_spec= lt_save_ifs=$IFS IFS=';' for lt_path in $LIB do IFS=$lt_save_ifs # Let DOS variable expansion print the short 8.3 style file name. lt_path=`cd "$lt_path" 2>/dev/null && cmd //C "for %i in (".") do @echo %~si"` sys_lib_search_path_spec="$sys_lib_search_path_spec $lt_path" done IFS=$lt_save_ifs # Convert to MSYS style. sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | sed -e 's|\\\\|/|g' -e 's| \\([[a-zA-Z]]\\):| /\\1|g' -e 's|^ ||'` ;; cygwin*) # Convert to unix form, then to dos form, then back to unix form # but this time dos style (no spaces!) so that the unix form looks # like /cygdrive/c/PROGRA~1:/cygdr... sys_lib_search_path_spec=`cygpath --path --unix "$LIB"` sys_lib_search_path_spec=`cygpath --path --dos "$sys_lib_search_path_spec" 2>/dev/null` sys_lib_search_path_spec=`cygpath --path --unix "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"` ;; *) sys_lib_search_path_spec=$LIB if $ECHO "$sys_lib_search_path_spec" | [$GREP ';[c-zC-Z]:/' >/dev/null]; then # It is most probably a Windows format PATH. sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e 's/;/ /g'` else sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"` fi # FIXME: find the short name or the path components, as spaces are # common. (e.g. "Program Files" -> "PROGRA~1") ;; esac # DLL is installed to $(libdir)/../bin by postinstall_cmds postinstall_cmds='base_file=`basename \$file`~ dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~ dldir=$destdir/`dirname \$dlpath`~ test -d \$dldir || mkdir -p \$dldir~ $install_prog $dir/$dlname \$dldir/$dlname' postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~ dlpath=$dir/\$dldll~ $RM \$dlpath' shlibpath_overrides_runpath=yes dynamic_linker='Win32 link.exe' ;; *) # Assume MSVC wrapper library_names_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext $libname.lib' dynamic_linker='Win32 ld.exe' ;; esac # FIXME: first we should search . and the directory the executable is in shlibpath_var=PATH ;; darwin* | rhapsody*) dynamic_linker="$host_os dyld" version_type=darwin need_lib_prefix=no need_version=no library_names_spec='$libname$release$major$shared_ext $libname$shared_ext' soname_spec='$libname$release$major$shared_ext' shlibpath_overrides_runpath=yes shlibpath_var=DYLD_LIBRARY_PATH shrext_cmds='`test .$module = .yes && echo .so || echo .dylib`' m4_if([$1], [],[ sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/local/lib"]) sys_lib_dlsearch_path_spec='/usr/local/lib /lib /usr/lib' ;; dgux*) version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LD_LIBRARY_PATH ;; freebsd* | dragonfly*) # DragonFly does not have aout. When/if they implement a new # versioning mechanism, adjust this. if test -x /usr/bin/objformat; then objformat=`/usr/bin/objformat` else case $host_os in freebsd[[23]].*) objformat=aout ;; *) objformat=elf ;; esac fi version_type=freebsd-$objformat case $version_type in freebsd-elf*) library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' need_version=no need_lib_prefix=no ;; freebsd-*) library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' need_version=yes ;; esac shlibpath_var=LD_LIBRARY_PATH case $host_os in freebsd2.*) shlibpath_overrides_runpath=yes ;; freebsd3.[[01]]* | freebsdelf3.[[01]]*) shlibpath_overrides_runpath=yes hardcode_into_libs=yes ;; freebsd3.[[2-9]]* | freebsdelf3.[[2-9]]* | \ freebsd4.[[0-5]] | freebsdelf4.[[0-5]] | freebsd4.1.1 | freebsdelf4.1.1) shlibpath_overrides_runpath=no hardcode_into_libs=yes ;; *) # from 4.6 on, and DragonFly shlibpath_overrides_runpath=yes hardcode_into_libs=yes ;; esac ;; haiku*) version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no dynamic_linker="$host_os runtime_loader" library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LIBRARY_PATH shlibpath_overrides_runpath=no sys_lib_dlsearch_path_spec='/boot/home/config/lib /boot/common/lib /boot/system/lib' hardcode_into_libs=yes ;; hpux9* | hpux10* | hpux11*) # Give a soname corresponding to the major version so that dld.sl refuses to # link against other versions. version_type=sunos need_lib_prefix=no need_version=no case $host_cpu in ia64*) shrext_cmds='.so' hardcode_into_libs=yes dynamic_linker="$host_os dld.so" shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes # Unless +noenvvar is specified. library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' if test 32 = "$HPUX_IA64_MODE"; then sys_lib_search_path_spec="/usr/lib/hpux32 /usr/local/lib/hpux32 /usr/local/lib" sys_lib_dlsearch_path_spec=/usr/lib/hpux32 else sys_lib_search_path_spec="/usr/lib/hpux64 /usr/local/lib/hpux64" sys_lib_dlsearch_path_spec=/usr/lib/hpux64 fi ;; hppa*64*) shrext_cmds='.sl' hardcode_into_libs=yes dynamic_linker="$host_os dld.sl" shlibpath_var=LD_LIBRARY_PATH # How should we handle SHLIB_PATH shlibpath_overrides_runpath=yes # Unless +noenvvar is specified. library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' sys_lib_search_path_spec="/usr/lib/pa20_64 /usr/ccs/lib/pa20_64" sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec ;; *) shrext_cmds='.sl' dynamic_linker="$host_os dld.sl" shlibpath_var=SHLIB_PATH shlibpath_overrides_runpath=no # +s is required to enable SHLIB_PATH library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' ;; esac # HP-UX runs *really* slowly unless shared libraries are mode 555, ... postinstall_cmds='chmod 555 $lib' # or fails outright, so override atomically: install_override_mode=555 ;; interix[[3-9]]*) version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' dynamic_linker='Interix 3.x ld.so.1 (PE, like ELF)' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=no hardcode_into_libs=yes ;; irix5* | irix6* | nonstopux*) case $host_os in nonstopux*) version_type=nonstopux ;; *) if test yes = "$lt_cv_prog_gnu_ld"; then version_type=linux # correct to gnu/linux during the next big refactor else version_type=irix fi ;; esac need_lib_prefix=no need_version=no soname_spec='$libname$release$shared_ext$major' library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$release$shared_ext $libname$shared_ext' case $host_os in irix5* | nonstopux*) libsuff= shlibsuff= ;; *) case $LD in # libtool.m4 will add one of these switches to LD *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ") libsuff= shlibsuff= libmagic=32-bit;; *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ") libsuff=32 shlibsuff=N32 libmagic=N32;; *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ") libsuff=64 shlibsuff=64 libmagic=64-bit;; *) libsuff= shlibsuff= libmagic=never-match;; esac ;; esac shlibpath_var=LD_LIBRARY${shlibsuff}_PATH shlibpath_overrides_runpath=no sys_lib_search_path_spec="/usr/lib$libsuff /lib$libsuff /usr/local/lib$libsuff" sys_lib_dlsearch_path_spec="/usr/lib$libsuff /lib$libsuff" hardcode_into_libs=yes ;; # No shared lib support for Linux oldld, aout, or coff. linux*oldld* | linux*aout* | linux*coff*) dynamic_linker=no ;; linux*android*) version_type=none # Android doesn't support versioned libraries. need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext' soname_spec='$libname$release$shared_ext' finish_cmds= shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes # This implies no fast_install, which is unacceptable. # Some rework will be needed to allow for fast_install # before this can be enabled. hardcode_into_libs=yes dynamic_linker='Android linker' # Don't embed -rpath directories since the linker doesn't support them. _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' ;; # This must be glibc/ELF. linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' finish_cmds='PATH="\$PATH:/sbin" ldconfig -n $libdir' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=no # Some binutils ld are patched to set DT_RUNPATH AC_CACHE_VAL([lt_cv_shlibpath_overrides_runpath], [lt_cv_shlibpath_overrides_runpath=no save_LDFLAGS=$LDFLAGS save_libdir=$libdir eval "libdir=/foo; wl=\"$_LT_TAGVAR(lt_prog_compiler_wl, $1)\"; \ LDFLAGS=\"\$LDFLAGS $_LT_TAGVAR(hardcode_libdir_flag_spec, $1)\"" AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])], [AS_IF([ ($OBJDUMP -p conftest$ac_exeext) 2>/dev/null | grep "RUNPATH.*$libdir" >/dev/null], [lt_cv_shlibpath_overrides_runpath=yes])]) LDFLAGS=$save_LDFLAGS libdir=$save_libdir ]) shlibpath_overrides_runpath=$lt_cv_shlibpath_overrides_runpath # This implies no fast_install, which is unacceptable. # Some rework will be needed to allow for fast_install # before this can be enabled. hardcode_into_libs=yes # Ideally, we could use ldconfig to report *all* directores which are # searched for libraries, however this is still not possible. Aside from not # being certain /sbin/ldconfig is available, command # 'ldconfig -N -X -v | grep ^/' on 64bit Fedora does not report /usr/lib64, # even though it is searched at run-time. Try to do the best guess by # appending ld.so.conf contents (and includes) to the search path. if test -f /etc/ld.so.conf; then lt_ld_extra=`awk '/^include / { system(sprintf("cd /etc; cat %s 2>/dev/null", \[$]2)); skip = 1; } { if (!skip) print \[$]0; skip = 0; }' < /etc/ld.so.conf | $SED -e 's/#.*//;/^[ ]*hwcap[ ]/d;s/[:, ]/ /g;s/=[^=]*$//;s/=[^= ]* / /g;s/"//g;/^$/d' | tr '\n' ' '` sys_lib_dlsearch_path_spec="/lib /usr/lib $lt_ld_extra" fi # We used to test for /lib/ld.so.1 and disable shared libraries on # powerpc, because MkLinux only supported shared libraries with the # GNU dynamic linker. Since this was broken with cross compilers, # most powerpc-linux boxes support dynamic linking these days and # people can always --disable-shared, the test was removed, and we # assume the GNU/Linux dynamic linker is in use. dynamic_linker='GNU/Linux ld.so' ;; netbsdelf*-gnu) version_type=linux need_lib_prefix=no need_version=no library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major ${libname}${shared_ext}' soname_spec='${libname}${release}${shared_ext}$major' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=no hardcode_into_libs=yes dynamic_linker='NetBSD ld.elf_so' ;; netbsd*) version_type=sunos need_lib_prefix=no need_version=no if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir' dynamic_linker='NetBSD (a.out) ld.so' else library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' dynamic_linker='NetBSD ld.elf_so' fi shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes hardcode_into_libs=yes ;; newsos6) version_type=linux # correct to gnu/linux during the next big refactor library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes ;; *nto* | *qnx*) version_type=qnx need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=no hardcode_into_libs=yes dynamic_linker='ldqnx.so' ;; openbsd* | bitrig*) version_type=sunos sys_lib_dlsearch_path_spec=/usr/lib need_lib_prefix=no if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then need_version=no else need_version=yes fi library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes ;; os2*) libname_spec='$name' version_type=windows shrext_cmds=.dll need_version=no need_lib_prefix=no # OS/2 can only load a DLL with a base name of 8 characters or less. soname_spec='`test -n "$os2dllname" && libname="$os2dllname"; v=$($ECHO $release$versuffix | tr -d .-); n=$($ECHO $libname | cut -b -$((8 - ${#v})) | tr . _); $ECHO $n$v`$shared_ext' library_names_spec='${libname}_dll.$libext' dynamic_linker='OS/2 ld.exe' shlibpath_var=BEGINLIBPATH sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib" sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec postinstall_cmds='base_file=`basename \$file`~ dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; $ECHO \$dlname'\''`~ dldir=$destdir/`dirname \$dlpath`~ test -d \$dldir || mkdir -p \$dldir~ $install_prog $dir/$dlname \$dldir/$dlname~ chmod a+x \$dldir/$dlname~ if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then eval '\''$striplib \$dldir/$dlname'\'' || exit \$?; fi' postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; $ECHO \$dlname'\''`~ dlpath=$dir/\$dldll~ $RM \$dlpath' ;; osf3* | osf4* | osf5*) version_type=osf need_lib_prefix=no need_version=no soname_spec='$libname$release$shared_ext$major' library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' shlibpath_var=LD_LIBRARY_PATH sys_lib_search_path_spec="/usr/shlib /usr/ccs/lib /usr/lib/cmplrs/cc /usr/lib /usr/local/lib /var/shlib" sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec ;; rdos*) dynamic_linker=no ;; solaris*) version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes hardcode_into_libs=yes # ldd complains unless libraries are executable postinstall_cmds='chmod +x $lib' ;; sunos4*) version_type=sunos library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' finish_cmds='PATH="\$PATH:/usr/etc" ldconfig $libdir' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes if test yes = "$with_gnu_ld"; then need_lib_prefix=no fi need_version=yes ;; sysv4 | sysv4.3*) version_type=linux # correct to gnu/linux during the next big refactor library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LD_LIBRARY_PATH case $host_vendor in sni) shlibpath_overrides_runpath=no need_lib_prefix=no runpath_var=LD_RUN_PATH ;; siemens) need_lib_prefix=no ;; motorola) need_lib_prefix=no need_version=no shlibpath_overrides_runpath=no sys_lib_search_path_spec='/lib /usr/lib /usr/ccs/lib' ;; esac ;; sysv4*MP*) if test -d /usr/nec; then version_type=linux # correct to gnu/linux during the next big refactor library_names_spec='$libname$shared_ext.$versuffix $libname$shared_ext.$major $libname$shared_ext' soname_spec='$libname$shared_ext.$major' shlibpath_var=LD_LIBRARY_PATH fi ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) version_type=sco need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=yes hardcode_into_libs=yes if test yes = "$with_gnu_ld"; then sys_lib_search_path_spec='/usr/local/lib /usr/gnu/lib /usr/ccs/lib /usr/lib /lib' else sys_lib_search_path_spec='/usr/ccs/lib /usr/lib' case $host_os in sco3.2v5*) sys_lib_search_path_spec="$sys_lib_search_path_spec /lib" ;; esac fi sys_lib_dlsearch_path_spec='/usr/lib' ;; tpf*) # TPF is a cross-target only. Preferred cross-host = GNU/Linux. version_type=linux # correct to gnu/linux during the next big refactor need_lib_prefix=no need_version=no library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' shlibpath_var=LD_LIBRARY_PATH shlibpath_overrides_runpath=no hardcode_into_libs=yes ;; uts4*) version_type=linux # correct to gnu/linux during the next big refactor library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' soname_spec='$libname$release$shared_ext$major' shlibpath_var=LD_LIBRARY_PATH ;; *) dynamic_linker=no ;; esac AC_MSG_RESULT([$dynamic_linker]) test no = "$dynamic_linker" && can_build_shared=no variables_saved_for_relink="PATH $shlibpath_var $runpath_var" if test yes = "$GCC"; then variables_saved_for_relink="$variables_saved_for_relink GCC_EXEC_PREFIX COMPILER_PATH LIBRARY_PATH" fi if test set = "${lt_cv_sys_lib_search_path_spec+set}"; then sys_lib_search_path_spec=$lt_cv_sys_lib_search_path_spec fi if test set = "${lt_cv_sys_lib_dlsearch_path_spec+set}"; then sys_lib_dlsearch_path_spec=$lt_cv_sys_lib_dlsearch_path_spec fi # remember unaugmented sys_lib_dlsearch_path content for libtool script decls... configure_time_dlsearch_path=$sys_lib_dlsearch_path_spec # ... but it needs LT_SYS_LIBRARY_PATH munging for other configure-time code func_munge_path_list sys_lib_dlsearch_path_spec "$LT_SYS_LIBRARY_PATH" # to be used as default LT_SYS_LIBRARY_PATH value in generated libtool configure_time_lt_sys_library_path=$LT_SYS_LIBRARY_PATH _LT_DECL([], [variables_saved_for_relink], [1], [Variables whose values should be saved in libtool wrapper scripts and restored at link time]) _LT_DECL([], [need_lib_prefix], [0], [Do we need the "lib" prefix for modules?]) _LT_DECL([], [need_version], [0], [Do we need a version for libraries?]) _LT_DECL([], [version_type], [0], [Library versioning type]) _LT_DECL([], [runpath_var], [0], [Shared library runtime path variable]) _LT_DECL([], [shlibpath_var], [0],[Shared library path variable]) _LT_DECL([], [shlibpath_overrides_runpath], [0], [Is shlibpath searched before the hard-coded library search path?]) _LT_DECL([], [libname_spec], [1], [Format of library name prefix]) _LT_DECL([], [library_names_spec], [1], [[List of archive names. First name is the real one, the rest are links. The last name is the one that the linker finds with -lNAME]]) _LT_DECL([], [soname_spec], [1], [[The coded name of the library, if different from the real name]]) _LT_DECL([], [install_override_mode], [1], [Permission mode override for installation of shared libraries]) _LT_DECL([], [postinstall_cmds], [2], [Command to use after installation of a shared archive]) _LT_DECL([], [postuninstall_cmds], [2], [Command to use after uninstallation of a shared archive]) _LT_DECL([], [finish_cmds], [2], [Commands used to finish a libtool library installation in a directory]) _LT_DECL([], [finish_eval], [1], [[As "finish_cmds", except a single script fragment to be evaled but not shown]]) _LT_DECL([], [hardcode_into_libs], [0], [Whether we should hardcode library paths into libraries]) _LT_DECL([], [sys_lib_search_path_spec], [2], [Compile-time system search path for libraries]) _LT_DECL([sys_lib_dlsearch_path_spec], [configure_time_dlsearch_path], [2], [Detected run-time system search path for libraries]) _LT_DECL([], [configure_time_lt_sys_library_path], [2], [Explicit LT_SYS_LIBRARY_PATH set during ./configure time]) ])# _LT_SYS_DYNAMIC_LINKER # _LT_PATH_TOOL_PREFIX(TOOL) # -------------------------- # find a file program that can recognize shared library AC_DEFUN([_LT_PATH_TOOL_PREFIX], [m4_require([_LT_DECL_EGREP])dnl AC_MSG_CHECKING([for $1]) AC_CACHE_VAL(lt_cv_path_MAGIC_CMD, [case $MAGIC_CMD in [[\\/*] | ?:[\\/]*]) lt_cv_path_MAGIC_CMD=$MAGIC_CMD # Let the user override the test with a path. ;; *) lt_save_MAGIC_CMD=$MAGIC_CMD lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR dnl $ac_dummy forces splitting on constant user-supplied paths. dnl POSIX.2 word splitting is done only on the output of word expansions, dnl not every word. This closes a longstanding sh security hole. ac_dummy="m4_if([$2], , $PATH, [$2])" for ac_dir in $ac_dummy; do IFS=$lt_save_ifs test -z "$ac_dir" && ac_dir=. if test -f "$ac_dir/$1"; then lt_cv_path_MAGIC_CMD=$ac_dir/"$1" if test -n "$file_magic_test_file"; then case $deplibs_check_method in "file_magic "*) file_magic_regex=`expr "$deplibs_check_method" : "file_magic \(.*\)"` MAGIC_CMD=$lt_cv_path_MAGIC_CMD if eval $file_magic_cmd \$file_magic_test_file 2> /dev/null | $EGREP "$file_magic_regex" > /dev/null; then : else cat <<_LT_EOF 1>&2 *** Warning: the command libtool uses to detect shared libraries, *** $file_magic_cmd, produces output that libtool cannot recognize. *** The result is that libtool may fail to recognize shared libraries *** as such. This will affect the creation of libtool libraries that *** depend on shared libraries, but programs linked with such libtool *** libraries will work regardless of this problem. Nevertheless, you *** may want to report the problem to your system manager and/or to *** bug-libtool@gnu.org _LT_EOF fi ;; esac fi break fi done IFS=$lt_save_ifs MAGIC_CMD=$lt_save_MAGIC_CMD ;; esac]) MAGIC_CMD=$lt_cv_path_MAGIC_CMD if test -n "$MAGIC_CMD"; then AC_MSG_RESULT($MAGIC_CMD) else AC_MSG_RESULT(no) fi _LT_DECL([], [MAGIC_CMD], [0], [Used to examine libraries when file_magic_cmd begins with "file"])dnl ])# _LT_PATH_TOOL_PREFIX # Old name: AU_ALIAS([AC_PATH_TOOL_PREFIX], [_LT_PATH_TOOL_PREFIX]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_PATH_TOOL_PREFIX], []) # _LT_PATH_MAGIC # -------------- # find a file program that can recognize a shared library m4_defun([_LT_PATH_MAGIC], [_LT_PATH_TOOL_PREFIX(${ac_tool_prefix}file, /usr/bin$PATH_SEPARATOR$PATH) if test -z "$lt_cv_path_MAGIC_CMD"; then if test -n "$ac_tool_prefix"; then _LT_PATH_TOOL_PREFIX(file, /usr/bin$PATH_SEPARATOR$PATH) else MAGIC_CMD=: fi fi ])# _LT_PATH_MAGIC # LT_PATH_LD # ---------- # find the pathname to the GNU or non-GNU linker AC_DEFUN([LT_PATH_LD], [AC_REQUIRE([AC_PROG_CC])dnl AC_REQUIRE([AC_CANONICAL_HOST])dnl AC_REQUIRE([AC_CANONICAL_BUILD])dnl m4_require([_LT_DECL_SED])dnl m4_require([_LT_DECL_EGREP])dnl m4_require([_LT_PROG_ECHO_BACKSLASH])dnl AC_ARG_WITH([gnu-ld], [AS_HELP_STRING([--with-gnu-ld], [assume the C compiler uses GNU ld @<:@default=no@:>@])], [test no = "$withval" || with_gnu_ld=yes], [with_gnu_ld=no])dnl ac_prog=ld if test yes = "$GCC"; then # Check if gcc -print-prog-name=ld gives a path. AC_MSG_CHECKING([for ld used by $CC]) case $host in *-*-mingw*) # gcc leaves a trailing carriage return, which upsets mingw ac_prog=`($CC -print-prog-name=ld) 2>&5 | tr -d '\015'` ;; *) ac_prog=`($CC -print-prog-name=ld) 2>&5` ;; esac case $ac_prog in # Accept absolute paths. [[\\/]]* | ?:[[\\/]]*) re_direlt='/[[^/]][[^/]]*/\.\./' # Canonicalize the pathname of ld ac_prog=`$ECHO "$ac_prog"| $SED 's%\\\\%/%g'` while $ECHO "$ac_prog" | $GREP "$re_direlt" > /dev/null 2>&1; do ac_prog=`$ECHO $ac_prog| $SED "s%$re_direlt%/%"` done test -z "$LD" && LD=$ac_prog ;; "") # If it fails, then pretend we aren't using GCC. ac_prog=ld ;; *) # If it is relative, then search for the first ld in PATH. with_gnu_ld=unknown ;; esac elif test yes = "$with_gnu_ld"; then AC_MSG_CHECKING([for GNU ld]) else AC_MSG_CHECKING([for non-GNU ld]) fi AC_CACHE_VAL(lt_cv_path_LD, [if test -z "$LD"; then lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR for ac_dir in $PATH; do IFS=$lt_save_ifs test -z "$ac_dir" && ac_dir=. if test -f "$ac_dir/$ac_prog" || test -f "$ac_dir/$ac_prog$ac_exeext"; then lt_cv_path_LD=$ac_dir/$ac_prog # Check to see if the program is GNU ld. I'd rather use --version, # but apparently some variants of GNU ld only accept -v. # Break only if it was the GNU/non-GNU ld that we prefer. case `"$lt_cv_path_LD" -v 2>&1 &1 conftest.i cat conftest.i conftest.i >conftest2.i : ${lt_DD:=$DD} AC_PATH_PROGS_FEATURE_CHECK([lt_DD], [dd], [if "$ac_path_lt_DD" bs=32 count=1 conftest.out 2>/dev/null; then cmp -s conftest.i conftest.out \ && ac_cv_path_lt_DD="$ac_path_lt_DD" ac_path_lt_DD_found=: fi]) rm -f conftest.i conftest2.i conftest.out]) ])# _LT_PATH_DD # _LT_CMD_TRUNCATE # ---------------- # find command to truncate a binary pipe m4_defun([_LT_CMD_TRUNCATE], [m4_require([_LT_PATH_DD]) AC_CACHE_CHECK([how to truncate binary pipes], [lt_cv_truncate_bin], [printf 0123456789abcdef0123456789abcdef >conftest.i cat conftest.i conftest.i >conftest2.i lt_cv_truncate_bin= if "$ac_cv_path_lt_DD" bs=32 count=1 conftest.out 2>/dev/null; then cmp -s conftest.i conftest.out \ && lt_cv_truncate_bin="$ac_cv_path_lt_DD bs=4096 count=1" fi rm -f conftest.i conftest2.i conftest.out test -z "$lt_cv_truncate_bin" && lt_cv_truncate_bin="$SED -e 4q"]) _LT_DECL([lt_truncate_bin], [lt_cv_truncate_bin], [1], [Command to truncate a binary pipe]) ])# _LT_CMD_TRUNCATE # _LT_CHECK_MAGIC_METHOD # ---------------------- # how to check for library dependencies # -- PORTME fill in with the dynamic library characteristics m4_defun([_LT_CHECK_MAGIC_METHOD], [m4_require([_LT_DECL_EGREP]) m4_require([_LT_DECL_OBJDUMP]) AC_CACHE_CHECK([how to recognize dependent libraries], lt_cv_deplibs_check_method, [lt_cv_file_magic_cmd='$MAGIC_CMD' lt_cv_file_magic_test_file= lt_cv_deplibs_check_method='unknown' # Need to set the preceding variable on all platforms that support # interlibrary dependencies. # 'none' -- dependencies not supported. # 'unknown' -- same as none, but documents that we really don't know. # 'pass_all' -- all dependencies passed with no checks. # 'test_compile' -- check by making test program. # 'file_magic [[regex]]' -- check by looking for files in library path # that responds to the $file_magic_cmd with a given extended regex. # If you have 'file' or equivalent on your system and you're not sure # whether 'pass_all' will *always* work, you probably want this one. case $host_os in aix[[4-9]]*) lt_cv_deplibs_check_method=pass_all ;; beos*) lt_cv_deplibs_check_method=pass_all ;; bsdi[[45]]*) lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib)' lt_cv_file_magic_cmd='/usr/bin/file -L' lt_cv_file_magic_test_file=/shlib/libc.so ;; cygwin*) # func_win32_libid is a shell function defined in ltmain.sh lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL' lt_cv_file_magic_cmd='func_win32_libid' ;; mingw* | pw32*) # Base MSYS/MinGW do not provide the 'file' command needed by # func_win32_libid shell function, so use a weaker test based on 'objdump', # unless we find 'file', for example because we are cross-compiling. if ( file / ) >/dev/null 2>&1; then lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL' lt_cv_file_magic_cmd='func_win32_libid' else # Keep this pattern in sync with the one in func_win32_libid. lt_cv_deplibs_check_method='file_magic file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' lt_cv_file_magic_cmd='$OBJDUMP -f' fi ;; cegcc*) # use the weaker test based on 'objdump'. See mingw*. lt_cv_deplibs_check_method='file_magic file format pe-arm-.*little(.*architecture: arm)?' lt_cv_file_magic_cmd='$OBJDUMP -f' ;; darwin* | rhapsody*) lt_cv_deplibs_check_method=pass_all ;; freebsd* | dragonfly*) if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then case $host_cpu in i*86 ) # Not sure whether the presence of OpenBSD here was a mistake. # Let's accept both of them until this is cleared up. lt_cv_deplibs_check_method='file_magic (FreeBSD|OpenBSD|DragonFly)/i[[3-9]]86 (compact )?demand paged shared library' lt_cv_file_magic_cmd=/usr/bin/file lt_cv_file_magic_test_file=`echo /usr/lib/libc.so.*` ;; esac else lt_cv_deplibs_check_method=pass_all fi ;; haiku*) lt_cv_deplibs_check_method=pass_all ;; hpux10.20* | hpux11*) lt_cv_file_magic_cmd=/usr/bin/file case $host_cpu in ia64*) lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|ELF-[[0-9]][[0-9]]) shared object file - IA64' lt_cv_file_magic_test_file=/usr/lib/hpux32/libc.so ;; hppa*64*) [lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|ELF[ -][0-9][0-9])(-bit)?( [LM]SB)? shared object( file)?[, -]* PA-RISC [0-9]\.[0-9]'] lt_cv_file_magic_test_file=/usr/lib/pa20_64/libc.sl ;; *) lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|PA-RISC[[0-9]]\.[[0-9]]) shared library' lt_cv_file_magic_test_file=/usr/lib/libc.sl ;; esac ;; interix[[3-9]]*) # PIC code is broken on Interix 3.x, that's why |\.a not |_pic\.a here lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|\.a)$' ;; irix5* | irix6* | nonstopux*) case $LD in *-32|*"-32 ") libmagic=32-bit;; *-n32|*"-n32 ") libmagic=N32;; *-64|*"-64 ") libmagic=64-bit;; *) libmagic=never-match;; esac lt_cv_deplibs_check_method=pass_all ;; # This must be glibc/ELF. linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) lt_cv_deplibs_check_method=pass_all ;; netbsd* | netbsdelf*-gnu) if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$' else lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|_pic\.a)$' fi ;; newos6*) lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (executable|dynamic lib)' lt_cv_file_magic_cmd=/usr/bin/file lt_cv_file_magic_test_file=/usr/lib/libnls.so ;; *nto* | *qnx*) lt_cv_deplibs_check_method=pass_all ;; openbsd* | bitrig*) if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|\.so|_pic\.a)$' else lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$' fi ;; osf3* | osf4* | osf5*) lt_cv_deplibs_check_method=pass_all ;; rdos*) lt_cv_deplibs_check_method=pass_all ;; solaris*) lt_cv_deplibs_check_method=pass_all ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) lt_cv_deplibs_check_method=pass_all ;; sysv4 | sysv4.3*) case $host_vendor in motorola) lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib) M[[0-9]][[0-9]]* Version [[0-9]]' lt_cv_file_magic_test_file=`echo /usr/lib/libc.so*` ;; ncr) lt_cv_deplibs_check_method=pass_all ;; sequent) lt_cv_file_magic_cmd='/bin/file' lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB (shared object|dynamic lib )' ;; sni) lt_cv_file_magic_cmd='/bin/file' lt_cv_deplibs_check_method="file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB dynamic lib" lt_cv_file_magic_test_file=/lib/libc.so ;; siemens) lt_cv_deplibs_check_method=pass_all ;; pc) lt_cv_deplibs_check_method=pass_all ;; esac ;; tpf*) lt_cv_deplibs_check_method=pass_all ;; os2*) lt_cv_deplibs_check_method=pass_all ;; esac ]) file_magic_glob= want_nocaseglob=no if test "$build" = "$host"; then case $host_os in mingw* | pw32*) if ( shopt | grep nocaseglob ) >/dev/null 2>&1; then want_nocaseglob=yes else file_magic_glob=`echo aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ | $SED -e "s/\(..\)/s\/[[\1]]\/[[\1]]\/g;/g"` fi ;; esac fi file_magic_cmd=$lt_cv_file_magic_cmd deplibs_check_method=$lt_cv_deplibs_check_method test -z "$deplibs_check_method" && deplibs_check_method=unknown _LT_DECL([], [deplibs_check_method], [1], [Method to check whether dependent libraries are shared objects]) _LT_DECL([], [file_magic_cmd], [1], [Command to use when deplibs_check_method = "file_magic"]) _LT_DECL([], [file_magic_glob], [1], [How to find potential files when deplibs_check_method = "file_magic"]) _LT_DECL([], [want_nocaseglob], [1], [Find potential files using nocaseglob when deplibs_check_method = "file_magic"]) ])# _LT_CHECK_MAGIC_METHOD # LT_PATH_NM # ---------- # find the pathname to a BSD- or MS-compatible name lister AC_DEFUN([LT_PATH_NM], [AC_REQUIRE([AC_PROG_CC])dnl AC_CACHE_CHECK([for BSD- or MS-compatible name lister (nm)], lt_cv_path_NM, [if test -n "$NM"; then # Let the user override the test. lt_cv_path_NM=$NM else lt_nm_to_check=${ac_tool_prefix}nm if test -n "$ac_tool_prefix" && test "$build" = "$host"; then lt_nm_to_check="$lt_nm_to_check nm" fi for lt_tmp_nm in $lt_nm_to_check; do lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR for ac_dir in $PATH /usr/ccs/bin/elf /usr/ccs/bin /usr/ucb /bin; do IFS=$lt_save_ifs test -z "$ac_dir" && ac_dir=. tmp_nm=$ac_dir/$lt_tmp_nm if test -f "$tmp_nm" || test -f "$tmp_nm$ac_exeext"; then # Check to see if the nm accepts a BSD-compat flag. # Adding the 'sed 1q' prevents false positives on HP-UX, which says: # nm: unknown option "B" ignored # Tru64's nm complains that /dev/null is an invalid object file # MSYS converts /dev/null to NUL, MinGW nm treats NUL as empty case $build_os in mingw*) lt_bad_file=conftest.nm/nofile ;; *) lt_bad_file=/dev/null ;; esac case `"$tmp_nm" -B $lt_bad_file 2>&1 | sed '1q'` in *$lt_bad_file* | *'Invalid file or object type'*) lt_cv_path_NM="$tmp_nm -B" break 2 ;; *) case `"$tmp_nm" -p /dev/null 2>&1 | sed '1q'` in */dev/null*) lt_cv_path_NM="$tmp_nm -p" break 2 ;; *) lt_cv_path_NM=${lt_cv_path_NM="$tmp_nm"} # keep the first match, but continue # so that we can try to find one that supports BSD flags ;; esac ;; esac fi done IFS=$lt_save_ifs done : ${lt_cv_path_NM=no} fi]) if test no != "$lt_cv_path_NM"; then NM=$lt_cv_path_NM else # Didn't find any BSD compatible name lister, look for dumpbin. if test -n "$DUMPBIN"; then : # Let the user override the test. else AC_CHECK_TOOLS(DUMPBIN, [dumpbin "link -dump"], :) case `$DUMPBIN -symbols -headers /dev/null 2>&1 | sed '1q'` in *COFF*) DUMPBIN="$DUMPBIN -symbols -headers" ;; *) DUMPBIN=: ;; esac fi AC_SUBST([DUMPBIN]) if test : != "$DUMPBIN"; then NM=$DUMPBIN fi fi test -z "$NM" && NM=nm AC_SUBST([NM]) _LT_DECL([], [NM], [1], [A BSD- or MS-compatible name lister])dnl AC_CACHE_CHECK([the name lister ($NM) interface], [lt_cv_nm_interface], [lt_cv_nm_interface="BSD nm" echo "int some_variable = 0;" > conftest.$ac_ext (eval echo "\"\$as_me:$LINENO: $ac_compile\"" >&AS_MESSAGE_LOG_FD) (eval "$ac_compile" 2>conftest.err) cat conftest.err >&AS_MESSAGE_LOG_FD (eval echo "\"\$as_me:$LINENO: $NM \\\"conftest.$ac_objext\\\"\"" >&AS_MESSAGE_LOG_FD) (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out) cat conftest.err >&AS_MESSAGE_LOG_FD (eval echo "\"\$as_me:$LINENO: output\"" >&AS_MESSAGE_LOG_FD) cat conftest.out >&AS_MESSAGE_LOG_FD if $GREP 'External.*some_variable' conftest.out > /dev/null; then lt_cv_nm_interface="MS dumpbin" fi rm -f conftest*]) ])# LT_PATH_NM # Old names: AU_ALIAS([AM_PROG_NM], [LT_PATH_NM]) AU_ALIAS([AC_PROG_NM], [LT_PATH_NM]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AM_PROG_NM], []) dnl AC_DEFUN([AC_PROG_NM], []) # _LT_CHECK_SHAREDLIB_FROM_LINKLIB # -------------------------------- # how to determine the name of the shared library # associated with a specific link library. # -- PORTME fill in with the dynamic library characteristics m4_defun([_LT_CHECK_SHAREDLIB_FROM_LINKLIB], [m4_require([_LT_DECL_EGREP]) m4_require([_LT_DECL_OBJDUMP]) m4_require([_LT_DECL_DLLTOOL]) AC_CACHE_CHECK([how to associate runtime and link libraries], lt_cv_sharedlib_from_linklib_cmd, [lt_cv_sharedlib_from_linklib_cmd='unknown' case $host_os in cygwin* | mingw* | pw32* | cegcc*) # two different shell functions defined in ltmain.sh; # decide which one to use based on capabilities of $DLLTOOL case `$DLLTOOL --help 2>&1` in *--identify-strict*) lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib ;; *) lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib_fallback ;; esac ;; *) # fallback: assume linklib IS sharedlib lt_cv_sharedlib_from_linklib_cmd=$ECHO ;; esac ]) sharedlib_from_linklib_cmd=$lt_cv_sharedlib_from_linklib_cmd test -z "$sharedlib_from_linklib_cmd" && sharedlib_from_linklib_cmd=$ECHO _LT_DECL([], [sharedlib_from_linklib_cmd], [1], [Command to associate shared and link libraries]) ])# _LT_CHECK_SHAREDLIB_FROM_LINKLIB # _LT_PATH_MANIFEST_TOOL # ---------------------- # locate the manifest tool m4_defun([_LT_PATH_MANIFEST_TOOL], [AC_CHECK_TOOL(MANIFEST_TOOL, mt, :) test -z "$MANIFEST_TOOL" && MANIFEST_TOOL=mt AC_CACHE_CHECK([if $MANIFEST_TOOL is a manifest tool], [lt_cv_path_mainfest_tool], [lt_cv_path_mainfest_tool=no echo "$as_me:$LINENO: $MANIFEST_TOOL '-?'" >&AS_MESSAGE_LOG_FD $MANIFEST_TOOL '-?' 2>conftest.err > conftest.out cat conftest.err >&AS_MESSAGE_LOG_FD if $GREP 'Manifest Tool' conftest.out > /dev/null; then lt_cv_path_mainfest_tool=yes fi rm -f conftest*]) if test yes != "$lt_cv_path_mainfest_tool"; then MANIFEST_TOOL=: fi _LT_DECL([], [MANIFEST_TOOL], [1], [Manifest tool])dnl ])# _LT_PATH_MANIFEST_TOOL # _LT_DLL_DEF_P([FILE]) # --------------------- # True iff FILE is a Windows DLL '.def' file. # Keep in sync with func_dll_def_p in the libtool script AC_DEFUN([_LT_DLL_DEF_P], [dnl test DEF = "`$SED -n dnl -e '\''s/^[[ ]]*//'\'' dnl Strip leading whitespace -e '\''/^\(;.*\)*$/d'\'' dnl Delete empty lines and comments -e '\''s/^\(EXPORTS\|LIBRARY\)\([[ ]].*\)*$/DEF/p'\'' dnl -e q dnl Only consider the first "real" line $1`" dnl ])# _LT_DLL_DEF_P # LT_LIB_M # -------- # check for math library AC_DEFUN([LT_LIB_M], [AC_REQUIRE([AC_CANONICAL_HOST])dnl LIBM= case $host in *-*-beos* | *-*-cegcc* | *-*-cygwin* | *-*-haiku* | *-*-pw32* | *-*-darwin*) # These system don't have libm, or don't need it ;; *-ncr-sysv4.3*) AC_CHECK_LIB(mw, _mwvalidcheckl, LIBM=-lmw) AC_CHECK_LIB(m, cos, LIBM="$LIBM -lm") ;; *) AC_CHECK_LIB(m, cos, LIBM=-lm) ;; esac AC_SUBST([LIBM]) ])# LT_LIB_M # Old name: AU_ALIAS([AC_CHECK_LIBM], [LT_LIB_M]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_CHECK_LIBM], []) # _LT_COMPILER_NO_RTTI([TAGNAME]) # ------------------------------- m4_defun([_LT_COMPILER_NO_RTTI], [m4_require([_LT_TAG_COMPILER])dnl _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)= if test yes = "$GCC"; then case $cc_basename in nvcc*) _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -Xcompiler -fno-builtin' ;; *) _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin' ;; esac _LT_COMPILER_OPTION([if $compiler supports -fno-rtti -fno-exceptions], lt_cv_prog_compiler_rtti_exceptions, [-fno-rtti -fno-exceptions], [], [_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)="$_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1) -fno-rtti -fno-exceptions"]) fi _LT_TAGDECL([no_builtin_flag], [lt_prog_compiler_no_builtin_flag], [1], [Compiler flag to turn off builtin functions]) ])# _LT_COMPILER_NO_RTTI # _LT_CMD_GLOBAL_SYMBOLS # ---------------------- m4_defun([_LT_CMD_GLOBAL_SYMBOLS], [AC_REQUIRE([AC_CANONICAL_HOST])dnl AC_REQUIRE([AC_PROG_CC])dnl AC_REQUIRE([AC_PROG_AWK])dnl AC_REQUIRE([LT_PATH_NM])dnl AC_REQUIRE([LT_PATH_LD])dnl m4_require([_LT_DECL_SED])dnl m4_require([_LT_DECL_EGREP])dnl m4_require([_LT_TAG_COMPILER])dnl # Check for command to grab the raw symbol name followed by C symbol from nm. AC_MSG_CHECKING([command to parse $NM output from $compiler object]) AC_CACHE_VAL([lt_cv_sys_global_symbol_pipe], [ # These are sane defaults that work on at least a few old systems. # [They come from Ultrix. What could be older than Ultrix?!! ;)] # Character class describing NM global symbol codes. symcode='[[BCDEGRST]]' # Regexp to match symbols that can be accessed directly from C. sympat='\([[_A-Za-z]][[_A-Za-z0-9]]*\)' # Define system-specific variables. case $host_os in aix*) symcode='[[BCDT]]' ;; cygwin* | mingw* | pw32* | cegcc*) symcode='[[ABCDGISTW]]' ;; hpux*) if test ia64 = "$host_cpu"; then symcode='[[ABCDEGRST]]' fi ;; irix* | nonstopux*) symcode='[[BCDEGRST]]' ;; osf*) symcode='[[BCDEGQRST]]' ;; solaris*) symcode='[[BDRT]]' ;; sco3.2v5*) symcode='[[DT]]' ;; sysv4.2uw2*) symcode='[[DT]]' ;; sysv5* | sco5v6* | unixware* | OpenUNIX*) symcode='[[ABDT]]' ;; sysv4) symcode='[[DFNSTU]]' ;; esac # If we're using GNU nm, then use its standard symbol codes. case `$NM -V 2>&1` in *GNU* | *'with BFD'*) symcode='[[ABCDGIRSTW]]' ;; esac if test "$lt_cv_nm_interface" = "MS dumpbin"; then # Gets list of data symbols to import. lt_cv_sys_global_symbol_to_import="sed -n -e 's/^I .* \(.*\)$/\1/p'" # Adjust the below global symbol transforms to fixup imported variables. lt_cdecl_hook=" -e 's/^I .* \(.*\)$/extern __declspec(dllimport) char \1;/p'" lt_c_name_hook=" -e 's/^I .* \(.*\)$/ {\"\1\", (void *) 0},/p'" lt_c_name_lib_hook="\ -e 's/^I .* \(lib.*\)$/ {\"\1\", (void *) 0},/p'\ -e 's/^I .* \(.*\)$/ {\"lib\1\", (void *) 0},/p'" else # Disable hooks by default. lt_cv_sys_global_symbol_to_import= lt_cdecl_hook= lt_c_name_hook= lt_c_name_lib_hook= fi # Transform an extracted symbol line into a proper C declaration. # Some systems (esp. on ia64) link data and code symbols differently, # so use this general approach. lt_cv_sys_global_symbol_to_cdecl="sed -n"\ $lt_cdecl_hook\ " -e 's/^T .* \(.*\)$/extern int \1();/p'"\ " -e 's/^$symcode$symcode* .* \(.*\)$/extern char \1;/p'" # Transform an extracted symbol line into symbol name and symbol address lt_cv_sys_global_symbol_to_c_name_address="sed -n"\ $lt_c_name_hook\ " -e 's/^: \(.*\) .*$/ {\"\1\", (void *) 0},/p'"\ " -e 's/^$symcode$symcode* .* \(.*\)$/ {\"\1\", (void *) \&\1},/p'" # Transform an extracted symbol line into symbol name with lib prefix and # symbol address. lt_cv_sys_global_symbol_to_c_name_address_lib_prefix="sed -n"\ $lt_c_name_lib_hook\ " -e 's/^: \(.*\) .*$/ {\"\1\", (void *) 0},/p'"\ " -e 's/^$symcode$symcode* .* \(lib.*\)$/ {\"\1\", (void *) \&\1},/p'"\ " -e 's/^$symcode$symcode* .* \(.*\)$/ {\"lib\1\", (void *) \&\1},/p'" # Handle CRLF in mingw tool chain opt_cr= case $build_os in mingw*) opt_cr=`$ECHO 'x\{0,1\}' | tr x '\015'` # option cr in regexp ;; esac # Try without a prefix underscore, then with it. for ac_symprfx in "" "_"; do # Transform symcode, sympat, and symprfx into a raw symbol and a C symbol. symxfrm="\\1 $ac_symprfx\\2 \\2" # Write the raw and C identifiers. if test "$lt_cv_nm_interface" = "MS dumpbin"; then # Fake it for dumpbin and say T for any non-static function, # D for any global variable and I for any imported variable. # Also find C++ and __fastcall symbols from MSVC++, # which start with @ or ?. lt_cv_sys_global_symbol_pipe="$AWK ['"\ " {last_section=section; section=\$ 3};"\ " /^COFF SYMBOL TABLE/{for(i in hide) delete hide[i]};"\ " /Section length .*#relocs.*(pick any)/{hide[last_section]=1};"\ " /^ *Symbol name *: /{split(\$ 0,sn,\":\"); si=substr(sn[2],2)};"\ " /^ *Type *: code/{print \"T\",si,substr(si,length(prfx))};"\ " /^ *Type *: data/{print \"I\",si,substr(si,length(prfx))};"\ " \$ 0!~/External *\|/{next};"\ " / 0+ UNDEF /{next}; / UNDEF \([^|]\)*()/{next};"\ " {if(hide[section]) next};"\ " {f=\"D\"}; \$ 0~/\(\).*\|/{f=\"T\"};"\ " {split(\$ 0,a,/\||\r/); split(a[2],s)};"\ " s[1]~/^[@?]/{print f,s[1],s[1]; next};"\ " s[1]~prfx {split(s[1],t,\"@\"); print f,t[1],substr(t[1],length(prfx))}"\ " ' prfx=^$ac_symprfx]" else lt_cv_sys_global_symbol_pipe="sed -n -e 's/^.*[[ ]]\($symcode$symcode*\)[[ ]][[ ]]*$ac_symprfx$sympat$opt_cr$/$symxfrm/p'" fi lt_cv_sys_global_symbol_pipe="$lt_cv_sys_global_symbol_pipe | sed '/ __gnu_lto/d'" # Check to see that the pipe works correctly. pipe_works=no rm -f conftest* cat > conftest.$ac_ext <<_LT_EOF #ifdef __cplusplus extern "C" { #endif char nm_test_var; void nm_test_func(void); void nm_test_func(void){} #ifdef __cplusplus } #endif int main(){nm_test_var='a';nm_test_func();return(0);} _LT_EOF if AC_TRY_EVAL(ac_compile); then # Now try to grab the symbols. nlist=conftest.nm $ECHO "$as_me:$LINENO: $NM conftest.$ac_objext | $lt_cv_sys_global_symbol_pipe > $nlist" >&AS_MESSAGE_LOG_FD if eval "$NM" conftest.$ac_objext \| "$lt_cv_sys_global_symbol_pipe" \> $nlist 2>&AS_MESSAGE_LOG_FD && test -s "$nlist"; then # Try sorting and uniquifying the output. if sort "$nlist" | uniq > "$nlist"T; then mv -f "$nlist"T "$nlist" else rm -f "$nlist"T fi # Make sure that we snagged all the symbols we need. if $GREP ' nm_test_var$' "$nlist" >/dev/null; then if $GREP ' nm_test_func$' "$nlist" >/dev/null; then cat <<_LT_EOF > conftest.$ac_ext /* Keep this code in sync between libtool.m4, ltmain, lt_system.h, and tests. */ #if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE /* DATA imports from DLLs on WIN32 can't be const, because runtime relocations are performed -- see ld's documentation on pseudo-relocs. */ # define LT@&t@_DLSYM_CONST #elif defined __osf__ /* This system does not cope well with relocations in const data. */ # define LT@&t@_DLSYM_CONST #else # define LT@&t@_DLSYM_CONST const #endif #ifdef __cplusplus extern "C" { #endif _LT_EOF # Now generate the symbol file. eval "$lt_cv_sys_global_symbol_to_cdecl"' < "$nlist" | $GREP -v main >> conftest.$ac_ext' cat <<_LT_EOF >> conftest.$ac_ext /* The mapping between symbol names and symbols. */ LT@&t@_DLSYM_CONST struct { const char *name; void *address; } lt__PROGRAM__LTX_preloaded_symbols[[]] = { { "@PROGRAM@", (void *) 0 }, _LT_EOF $SED "s/^$symcode$symcode* .* \(.*\)$/ {\"\1\", (void *) \&\1},/" < "$nlist" | $GREP -v main >> conftest.$ac_ext cat <<\_LT_EOF >> conftest.$ac_ext {0, (void *) 0} }; /* This works around a problem in FreeBSD linker */ #ifdef FREEBSD_WORKAROUND static const void *lt_preloaded_setup() { return lt__PROGRAM__LTX_preloaded_symbols; } #endif #ifdef __cplusplus } #endif _LT_EOF # Now try linking the two files. mv conftest.$ac_objext conftstm.$ac_objext lt_globsym_save_LIBS=$LIBS lt_globsym_save_CFLAGS=$CFLAGS LIBS=conftstm.$ac_objext CFLAGS="$CFLAGS$_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)" if AC_TRY_EVAL(ac_link) && test -s conftest$ac_exeext; then pipe_works=yes fi LIBS=$lt_globsym_save_LIBS CFLAGS=$lt_globsym_save_CFLAGS else echo "cannot find nm_test_func in $nlist" >&AS_MESSAGE_LOG_FD fi else echo "cannot find nm_test_var in $nlist" >&AS_MESSAGE_LOG_FD fi else echo "cannot run $lt_cv_sys_global_symbol_pipe" >&AS_MESSAGE_LOG_FD fi else echo "$progname: failed program was:" >&AS_MESSAGE_LOG_FD cat conftest.$ac_ext >&5 fi rm -rf conftest* conftst* # Do not use the global_symbol_pipe unless it works. if test yes = "$pipe_works"; then break else lt_cv_sys_global_symbol_pipe= fi done ]) if test -z "$lt_cv_sys_global_symbol_pipe"; then lt_cv_sys_global_symbol_to_cdecl= fi if test -z "$lt_cv_sys_global_symbol_pipe$lt_cv_sys_global_symbol_to_cdecl"; then AC_MSG_RESULT(failed) else AC_MSG_RESULT(ok) fi # Response file support. if test "$lt_cv_nm_interface" = "MS dumpbin"; then nm_file_list_spec='@' elif $NM --help 2>/dev/null | grep '[[@]]FILE' >/dev/null; then nm_file_list_spec='@' fi _LT_DECL([global_symbol_pipe], [lt_cv_sys_global_symbol_pipe], [1], [Take the output of nm and produce a listing of raw symbols and C names]) _LT_DECL([global_symbol_to_cdecl], [lt_cv_sys_global_symbol_to_cdecl], [1], [Transform the output of nm in a proper C declaration]) _LT_DECL([global_symbol_to_import], [lt_cv_sys_global_symbol_to_import], [1], [Transform the output of nm into a list of symbols to manually relocate]) _LT_DECL([global_symbol_to_c_name_address], [lt_cv_sys_global_symbol_to_c_name_address], [1], [Transform the output of nm in a C name address pair]) _LT_DECL([global_symbol_to_c_name_address_lib_prefix], [lt_cv_sys_global_symbol_to_c_name_address_lib_prefix], [1], [Transform the output of nm in a C name address pair when lib prefix is needed]) _LT_DECL([nm_interface], [lt_cv_nm_interface], [1], [The name lister interface]) _LT_DECL([], [nm_file_list_spec], [1], [Specify filename containing input files for $NM]) ]) # _LT_CMD_GLOBAL_SYMBOLS # _LT_COMPILER_PIC([TAGNAME]) # --------------------------- m4_defun([_LT_COMPILER_PIC], [m4_require([_LT_TAG_COMPILER])dnl _LT_TAGVAR(lt_prog_compiler_wl, $1)= _LT_TAGVAR(lt_prog_compiler_pic, $1)= _LT_TAGVAR(lt_prog_compiler_static, $1)= m4_if([$1], [CXX], [ # C++ specific cases for pic, static, wl, etc. if test yes = "$GXX"; then _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' case $host_os in aix*) # All AIX code is PIC. if test ia64 = "$host_cpu"; then # AIX 5 now supports IA64 processor _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' fi _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; amigaos*) case $host_cpu in powerpc) # see comment about AmigaOS4 .so support _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; m68k) # FIXME: we need at least 68020 code to build shared libraries, but # adding the '-m68020' flag to GCC prevents building anything better, # like '-m68040'. _LT_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4' ;; esac ;; beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*) # PIC is the default for these OSes. ;; mingw* | cygwin* | os2* | pw32* | cegcc*) # This hack is so that the source file can tell whether it is being # built for inclusion in a dll (and should export symbols for example). # Although the cygwin gcc ignores -fPIC, still need this for old-style # (--disable-auto-import) libraries m4_if([$1], [GCJ], [], [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) case $host_os in os2*) _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static' ;; esac ;; darwin* | rhapsody*) # PIC is the default on this platform # Common symbols not allowed in MH_DYLIB files _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common' ;; *djgpp*) # DJGPP does not support shared libraries at all _LT_TAGVAR(lt_prog_compiler_pic, $1)= ;; haiku*) # PIC is the default for Haiku. # The "-static" flag exists, but is broken. _LT_TAGVAR(lt_prog_compiler_static, $1)= ;; interix[[3-9]]*) # Interix 3.x gcc -fpic/-fPIC options generate broken code. # Instead, we relocate shared libraries at runtime. ;; sysv4*MP*) if test -d /usr/nec; then _LT_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic fi ;; hpux*) # PIC is the default for 64-bit PA HP-UX, but not for 32-bit # PA HP-UX. On IA64 HP-UX, PIC is the default but the pic flag # sets the default TLS model and affects inlining. case $host_cpu in hppa*64*) ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; esac ;; *qnx* | *nto*) # QNX uses GNU C++, but need to define -shared option too, otherwise # it will coredump. _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; esac else case $host_os in aix[[4-9]]*) # All AIX code is PIC. if test ia64 = "$host_cpu"; then # AIX 5 now supports IA64 processor _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' else _LT_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp' fi ;; chorus*) case $cc_basename in cxch68*) # Green Hills C++ Compiler # _LT_TAGVAR(lt_prog_compiler_static, $1)="--no_auto_instantiation -u __main -u __premain -u _abort -r $COOL_DIR/lib/libOrb.a $MVME_DIR/lib/CC/libC.a $MVME_DIR/lib/classix/libcx.s.a" ;; esac ;; mingw* | cygwin* | os2* | pw32* | cegcc*) # This hack is so that the source file can tell whether it is being # built for inclusion in a dll (and should export symbols for example). m4_if([$1], [GCJ], [], [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) ;; dgux*) case $cc_basename in ec++*) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' ;; ghcx*) # Green Hills C++ Compiler _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' ;; *) ;; esac ;; freebsd* | dragonfly*) # FreeBSD uses GNU C++ ;; hpux9* | hpux10* | hpux11*) case $cc_basename in CC*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive' if test ia64 != "$host_cpu"; then _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z' fi ;; aCC*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive' case $host_cpu in hppa*64*|ia64*) # +Z the default ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z' ;; esac ;; *) ;; esac ;; interix*) # This is c89, which is MS Visual C++ (no shared libs) # Anyone wants to do a port? ;; irix5* | irix6* | nonstopux*) case $cc_basename in CC*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' # CC pic flag -KPIC is the default. ;; *) ;; esac ;; linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) case $cc_basename in KCC*) # KAI C++ Compiler _LT_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; ecpc* ) # old Intel C++ for x86_64, which still supported -KPIC. _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; icpc* ) # Intel C++, used to be incompatible with GCC. # ICC 10 doesn't accept -KPIC any more. _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; pgCC* | pgcpp*) # Portland Group C++ compiler _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; cxx*) # Compaq C++ # Make sure the PIC flag is empty. It appears that all Alpha # Linux and Compaq Tru64 Unix objects are PIC. _LT_TAGVAR(lt_prog_compiler_pic, $1)= _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' ;; xlc* | xlC* | bgxl[[cC]]* | mpixl[[cC]]*) # IBM XL 8.0, 9.0 on PPC and BlueGene _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-qpic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-qstaticlink' ;; *) case `$CC -V 2>&1 | sed 5q` in *Sun\ C*) # Sun C++ 5.9 _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' ;; esac ;; esac ;; lynxos*) ;; m88k*) ;; mvs*) case $cc_basename in cxx*) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-W c,exportall' ;; *) ;; esac ;; netbsd* | netbsdelf*-gnu) ;; *qnx* | *nto*) # QNX uses GNU C++, but need to define -shared option too, otherwise # it will coredump. _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' ;; osf3* | osf4* | osf5*) case $cc_basename in KCC*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,' ;; RCC*) # Rational C++ 2.4.1 _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' ;; cxx*) # Digital/Compaq C++ _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' # Make sure the PIC flag is empty. It appears that all Alpha # Linux and Compaq Tru64 Unix objects are PIC. _LT_TAGVAR(lt_prog_compiler_pic, $1)= _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' ;; *) ;; esac ;; psos*) ;; solaris*) case $cc_basename in CC* | sunCC*) # Sun C++ 4.2, 5.x and Centerline C++ _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' ;; gcx*) # Green Hills C++ Compiler _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' ;; *) ;; esac ;; sunos4*) case $cc_basename in CC*) # Sun C++ 4.x _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; lcc*) # Lucid _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' ;; *) ;; esac ;; sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) case $cc_basename in CC*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; esac ;; tandem*) case $cc_basename in NCC*) # NonStop-UX NCC 3.20 _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' ;; *) ;; esac ;; vxworks*) ;; *) _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no ;; esac fi ], [ if test yes = "$GCC"; then _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' case $host_os in aix*) # All AIX code is PIC. if test ia64 = "$host_cpu"; then # AIX 5 now supports IA64 processor _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' fi _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; amigaos*) case $host_cpu in powerpc) # see comment about AmigaOS4 .so support _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; m68k) # FIXME: we need at least 68020 code to build shared libraries, but # adding the '-m68020' flag to GCC prevents building anything better, # like '-m68040'. _LT_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4' ;; esac ;; beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*) # PIC is the default for these OSes. ;; mingw* | cygwin* | pw32* | os2* | cegcc*) # This hack is so that the source file can tell whether it is being # built for inclusion in a dll (and should export symbols for example). # Although the cygwin gcc ignores -fPIC, still need this for old-style # (--disable-auto-import) libraries m4_if([$1], [GCJ], [], [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) case $host_os in os2*) _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static' ;; esac ;; darwin* | rhapsody*) # PIC is the default on this platform # Common symbols not allowed in MH_DYLIB files _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common' ;; haiku*) # PIC is the default for Haiku. # The "-static" flag exists, but is broken. _LT_TAGVAR(lt_prog_compiler_static, $1)= ;; hpux*) # PIC is the default for 64-bit PA HP-UX, but not for 32-bit # PA HP-UX. On IA64 HP-UX, PIC is the default but the pic flag # sets the default TLS model and affects inlining. case $host_cpu in hppa*64*) # +Z the default ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; esac ;; interix[[3-9]]*) # Interix 3.x gcc -fpic/-fPIC options generate broken code. # Instead, we relocate shared libraries at runtime. ;; msdosdjgpp*) # Just because we use GCC doesn't mean we suddenly get shared libraries # on systems that don't support them. _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no enable_shared=no ;; *nto* | *qnx*) # QNX uses GNU C++, but need to define -shared option too, otherwise # it will coredump. _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' ;; sysv4*MP*) if test -d /usr/nec; then _LT_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic fi ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' ;; esac case $cc_basename in nvcc*) # Cuda Compiler Driver 2.2 _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Xlinker ' if test -n "$_LT_TAGVAR(lt_prog_compiler_pic, $1)"; then _LT_TAGVAR(lt_prog_compiler_pic, $1)="-Xcompiler $_LT_TAGVAR(lt_prog_compiler_pic, $1)" fi ;; esac else # PORTME Check for flag to pass linker flags through the system compiler. case $host_os in aix*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' if test ia64 = "$host_cpu"; then # AIX 5 now supports IA64 processor _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' else _LT_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp' fi ;; darwin* | rhapsody*) # PIC is the default on this platform # Common symbols not allowed in MH_DYLIB files _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common' case $cc_basename in nagfor*) # NAG Fortran compiler _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,-Wl,,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; esac ;; mingw* | cygwin* | pw32* | os2* | cegcc*) # This hack is so that the source file can tell whether it is being # built for inclusion in a dll (and should export symbols for example). m4_if([$1], [GCJ], [], [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) case $host_os in os2*) _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static' ;; esac ;; hpux9* | hpux10* | hpux11*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but # not for PA HP-UX. case $host_cpu in hppa*64*|ia64*) # +Z the default ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z' ;; esac # Is there a better lt_prog_compiler_static that works with the bundled CC? _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive' ;; irix5* | irix6* | nonstopux*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' # PIC (with -KPIC) is the default. _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' ;; linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) case $cc_basename in # old Intel for x86_64, which still supported -KPIC. ecc*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; # flang / f18. f95 an alias for gfortran or flang on Debian flang* | f18* | f95*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; # icc used to be incompatible with GCC. # ICC 10 doesn't accept -KPIC any more. icc* | ifort*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; # Lahey Fortran 8.1. lf95*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='--shared' _LT_TAGVAR(lt_prog_compiler_static, $1)='--static' ;; nagfor*) # NAG Fortran compiler _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,-Wl,,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; tcc*) # Fabrice Bellard et al's Tiny C Compiler _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; pgcc* | pgf77* | pgf90* | pgf95* | pgfortran*) # Portland Group compilers (*not* the Pentium gcc compiler, # which looks to be a dead project) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; ccc*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' # All Alpha code is PIC. _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' ;; xl* | bgxl* | bgf* | mpixl*) # IBM XL C 8.0/Fortran 10.1, 11.1 on PPC and BlueGene _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-qpic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-qstaticlink' ;; *) case `$CC -V 2>&1 | sed 5q` in *Sun\ Ceres\ Fortran* | *Sun*Fortran*\ [[1-7]].* | *Sun*Fortran*\ 8.[[0-3]]*) # Sun Fortran 8.3 passes all unrecognized flags to the linker _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' _LT_TAGVAR(lt_prog_compiler_wl, $1)='' ;; *Sun\ F* | *Sun*Fortran*) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' ;; *Sun\ C*) # Sun C 5.9 _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' ;; *Intel*\ [[CF]]*Compiler*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' ;; *Portland\ Group*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; esac ;; esac ;; newsos6) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; *nto* | *qnx*) # QNX uses GNU C++, but need to define -shared option too, otherwise # it will coredump. _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' ;; osf3* | osf4* | osf5*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' # All OSF/1 code is PIC. _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' ;; rdos*) _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' ;; solaris*) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' case $cc_basename in f77* | f90* | f95* | sunf77* | sunf90* | sunf95*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ';; *) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,';; esac ;; sunos4*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; sysv4 | sysv4.2uw2* | sysv4.3*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; sysv4*MP*) if test -d /usr/nec; then _LT_TAGVAR(lt_prog_compiler_pic, $1)='-Kconform_pic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' fi ;; sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; unicos*) _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no ;; uts4*) _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' ;; *) _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no ;; esac fi ]) case $host_os in # For platforms that do not support PIC, -DPIC is meaningless: *djgpp*) _LT_TAGVAR(lt_prog_compiler_pic, $1)= ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)="$_LT_TAGVAR(lt_prog_compiler_pic, $1)@&t@m4_if([$1],[],[ -DPIC],[m4_if([$1],[CXX],[ -DPIC],[])])" ;; esac AC_CACHE_CHECK([for $compiler option to produce PIC], [_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)], [_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)=$_LT_TAGVAR(lt_prog_compiler_pic, $1)]) _LT_TAGVAR(lt_prog_compiler_pic, $1)=$_LT_TAGVAR(lt_cv_prog_compiler_pic, $1) # # Check to make sure the PIC flag actually works. # if test -n "$_LT_TAGVAR(lt_prog_compiler_pic, $1)"; then _LT_COMPILER_OPTION([if $compiler PIC flag $_LT_TAGVAR(lt_prog_compiler_pic, $1) works], [_LT_TAGVAR(lt_cv_prog_compiler_pic_works, $1)], [$_LT_TAGVAR(lt_prog_compiler_pic, $1)@&t@m4_if([$1],[],[ -DPIC],[m4_if([$1],[CXX],[ -DPIC],[])])], [], [case $_LT_TAGVAR(lt_prog_compiler_pic, $1) in "" | " "*) ;; *) _LT_TAGVAR(lt_prog_compiler_pic, $1)=" $_LT_TAGVAR(lt_prog_compiler_pic, $1)" ;; esac], [_LT_TAGVAR(lt_prog_compiler_pic, $1)= _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no]) fi _LT_TAGDECL([pic_flag], [lt_prog_compiler_pic], [1], [Additional compiler flags for building library objects]) _LT_TAGDECL([wl], [lt_prog_compiler_wl], [1], [How to pass a linker flag through the compiler]) # # Check to make sure the static flag actually works. # wl=$_LT_TAGVAR(lt_prog_compiler_wl, $1) eval lt_tmp_static_flag=\"$_LT_TAGVAR(lt_prog_compiler_static, $1)\" _LT_LINKER_OPTION([if $compiler static flag $lt_tmp_static_flag works], _LT_TAGVAR(lt_cv_prog_compiler_static_works, $1), $lt_tmp_static_flag, [], [_LT_TAGVAR(lt_prog_compiler_static, $1)=]) _LT_TAGDECL([link_static_flag], [lt_prog_compiler_static], [1], [Compiler flag to prevent dynamic linking]) ])# _LT_COMPILER_PIC # _LT_LINKER_SHLIBS([TAGNAME]) # ---------------------------- # See if the linker supports building shared libraries. m4_defun([_LT_LINKER_SHLIBS], [AC_REQUIRE([LT_PATH_LD])dnl AC_REQUIRE([LT_PATH_NM])dnl m4_require([_LT_PATH_MANIFEST_TOOL])dnl m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_DECL_EGREP])dnl m4_require([_LT_DECL_SED])dnl m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl m4_require([_LT_TAG_COMPILER])dnl AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries]) m4_if([$1], [CXX], [ _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' _LT_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*'] case $host_os in aix[[4-9]]*) # If we're using GNU nm, then we don't want the "-C" option. # -C means demangle to GNU nm, but means don't demangle to AIX nm. # Without the "-l" option, or with the "-B" option, AIX nm treats # weak defined symbols like other global defined symbols, whereas # GNU nm marks them as "W". # While the 'weak' keyword is ignored in the Export File, we need # it in the Import File for the 'aix-soname' feature, so we have # to replace the "-B" option with "-P" for AIX nm. if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then _LT_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && ([substr](\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols' else _LT_TAGVAR(export_symbols_cmds, $1)='`func_echo_all $NM | $SED -e '\''s/B\([[^B]]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && ([substr](\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols' fi ;; pw32*) _LT_TAGVAR(export_symbols_cmds, $1)=$ltdll_cmds ;; cygwin* | mingw* | cegcc*) case $cc_basename in cl*) _LT_TAGVAR(exclude_expsyms, $1)='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*' ;; *) _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.* //'\'' | sort | uniq > $export_symbols' _LT_TAGVAR(exclude_expsyms, $1)=['[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname'] ;; esac ;; linux* | k*bsd*-gnu | gnu*) _LT_TAGVAR(link_all_deplibs, $1)=no ;; *) _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' ;; esac ], [ runpath_var= _LT_TAGVAR(allow_undefined_flag, $1)= _LT_TAGVAR(always_export_symbols, $1)=no _LT_TAGVAR(archive_cmds, $1)= _LT_TAGVAR(archive_expsym_cmds, $1)= _LT_TAGVAR(compiler_needs_object, $1)=no _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no _LT_TAGVAR(export_dynamic_flag_spec, $1)= _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' _LT_TAGVAR(hardcode_automatic, $1)=no _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= _LT_TAGVAR(hardcode_libdir_separator, $1)= _LT_TAGVAR(hardcode_minus_L, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported _LT_TAGVAR(inherit_rpath, $1)=no _LT_TAGVAR(link_all_deplibs, $1)=unknown _LT_TAGVAR(module_cmds, $1)= _LT_TAGVAR(module_expsym_cmds, $1)= _LT_TAGVAR(old_archive_from_new_cmds, $1)= _LT_TAGVAR(old_archive_from_expsyms_cmds, $1)= _LT_TAGVAR(thread_safe_flag_spec, $1)= _LT_TAGVAR(whole_archive_flag_spec, $1)= # include_expsyms should be a list of space-separated symbols to be *always* # included in the symbol list _LT_TAGVAR(include_expsyms, $1)= # exclude_expsyms can be an extended regexp of symbols to exclude # it will be wrapped by ' (' and ')$', so one must not match beginning or # end of line. Example: 'a|bc|.*d.*' will exclude the symbols 'a' and 'bc', # as well as any symbol that contains 'd'. _LT_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*'] # Although _GLOBAL_OFFSET_TABLE_ is a valid symbol C name, most a.out # platforms (ab)use it in PIC code, but their linkers get confused if # the symbol is explicitly referenced. Since portable code cannot # rely on this symbol name, it's probably fine to never include it in # preloaded symbol tables. # Exclude shared library initialization/finalization symbols. dnl Note also adjust exclude_expsyms for C++ above. extract_expsyms_cmds= case $host_os in cygwin* | mingw* | pw32* | cegcc*) # FIXME: the MSVC++ port hasn't been tested in a loooong time # When not using gcc, we currently assume that we are using # Microsoft Visual C++. if test yes != "$GCC"; then with_gnu_ld=no fi ;; interix*) # we just hope/assume this is gcc and not c89 (= MSVC++) with_gnu_ld=yes ;; openbsd* | bitrig*) with_gnu_ld=no ;; linux* | k*bsd*-gnu | gnu*) _LT_TAGVAR(link_all_deplibs, $1)=no ;; esac _LT_TAGVAR(ld_shlibs, $1)=yes # On some targets, GNU ld is compatible enough with the native linker # that we're better off using the native interface for both. lt_use_gnu_ld_interface=no if test yes = "$with_gnu_ld"; then case $host_os in aix*) # The AIX port of GNU ld has always aspired to compatibility # with the native linker. However, as the warning in the GNU ld # block says, versions before 2.19.5* couldn't really create working # shared libraries, regardless of the interface used. case `$LD -v 2>&1` in *\ \(GNU\ Binutils\)\ 2.19.5*) ;; *\ \(GNU\ Binutils\)\ 2.[[2-9]]*) ;; *\ \(GNU\ Binutils\)\ [[3-9]]*) ;; *) lt_use_gnu_ld_interface=yes ;; esac ;; *) lt_use_gnu_ld_interface=yes ;; esac fi if test yes = "$lt_use_gnu_ld_interface"; then # If archive_cmds runs LD, not CC, wlarc should be empty wlarc='$wl' # Set some defaults for GNU ld with shared library support. These # are reset later if shared libraries are not supported. Putting them # here allows them to be overridden if necessary. runpath_var=LD_RUN_PATH _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' # ancient GNU ld didn't support --whole-archive et. al. if $LD --help 2>&1 | $GREP 'no-whole-archive' > /dev/null; then _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' else _LT_TAGVAR(whole_archive_flag_spec, $1)= fi supports_anon_versioning=no case `$LD -v | $SED -e 's/([^)]\+)\s\+//' 2>&1` in *GNU\ gold*) supports_anon_versioning=yes ;; *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.10.*) ;; # catch versions < 2.11 *\ 2.11.93.0.2\ *) supports_anon_versioning=yes ;; # RH7.3 ... *\ 2.11.92.0.12\ *) supports_anon_versioning=yes ;; # Mandrake 8.2 ... *\ 2.11.*) ;; # other 2.11 versions *) supports_anon_versioning=yes ;; esac # See if GNU ld supports shared libraries. case $host_os in aix[[3-9]]*) # On AIX/PPC, the GNU linker is very broken if test ia64 != "$host_cpu"; then _LT_TAGVAR(ld_shlibs, $1)=no cat <<_LT_EOF 1>&2 *** Warning: the GNU linker, at least up to release 2.19, is reported *** to be unable to reliably create shared libraries on AIX. *** Therefore, libtool is disabling shared libraries support. If you *** really care for shared libraries, you may want to install binutils *** 2.20 or above, or modify your PATH so that a non-GNU linker is found. *** You will then need to restart the configuration process. _LT_EOF fi ;; amigaos*) case $host_cpu in powerpc) # see comment about AmigaOS4 .so support _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='' ;; m68k) _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_minus_L, $1)=yes ;; esac ;; beos*) if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then _LT_TAGVAR(allow_undefined_flag, $1)=unsupported # Joseph Beckenbach says some releases of gcc # support --undefined. This deserves some investigation. FIXME _LT_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; cygwin* | mingw* | pw32* | cegcc*) # _LT_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless, # as there is no search path for DLLs. _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-all-symbols' _LT_TAGVAR(allow_undefined_flag, $1)=unsupported _LT_TAGVAR(always_export_symbols, $1)=no _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.* //'\'' | sort | uniq > $export_symbols' _LT_TAGVAR(exclude_expsyms, $1)=['[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname'] if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' # If the export-symbols file already is a .def file, use it as # is; otherwise, prepend EXPORTS... _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then cp $export_symbols $output_objdir/$soname.def; else echo EXPORTS > $output_objdir/$soname.def; cat $export_symbols >> $output_objdir/$soname.def; fi~ $CC -shared $output_objdir/$soname.def $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; haiku*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(link_all_deplibs, $1)=yes ;; os2*) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(allow_undefined_flag, $1)=unsupported shrext_cmds=.dll _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ $ECHO EXPORTS >> $output_objdir/$libname.def~ emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ emximp -o $lib $output_objdir/$libname.def' _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ $ECHO EXPORTS >> $output_objdir/$libname.def~ prefix_cmds="$SED"~ if test EXPORTS = "`$SED 1q $export_symbols`"; then prefix_cmds="$prefix_cmds -e 1d"; fi~ prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ emximp -o $lib $output_objdir/$libname.def' _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes ;; interix[[3-9]]*) _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc. # Instead, shared libraries are loaded at an image base (0x10000000 by # default) and relocated if they conflict, which is a slow very memory # consuming and fragmenting process. To avoid this, we pick a random, # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link # time. Moving up from 0x10000000 also allows more sbrk(2) space. _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' ;; gnu* | linux* | tpf* | k*bsd*-gnu | kopensolaris*-gnu) tmp_diet=no if test linux-dietlibc = "$host_os"; then case $cc_basename in diet\ *) tmp_diet=yes;; # linux-dietlibc with static linking (!diet-dyn) esac fi if $LD --help 2>&1 | $EGREP ': supported targets:.* elf' > /dev/null \ && test no = "$tmp_diet" then tmp_addflag=' $pic_flag' tmp_sharedflag='-shared' case $cc_basename,$host_cpu in pgcc*) # Portland Group C compiler _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' tmp_addflag=' $pic_flag' ;; pgf77* | pgf90* | pgf95* | pgfortran*) # Portland Group f77 and f90 compilers _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' tmp_addflag=' $pic_flag -Mnomain' ;; ecc*,ia64* | icc*,ia64*) # Intel C compiler on ia64 tmp_addflag=' -i_dynamic' ;; efc*,ia64* | ifort*,ia64*) # Intel Fortran compiler on ia64 tmp_addflag=' -i_dynamic -nofor_main' ;; ifc* | ifort*) # Intel Fortran compiler tmp_addflag=' -nofor_main' ;; lf95*) # Lahey Fortran 8.1 _LT_TAGVAR(whole_archive_flag_spec, $1)= tmp_sharedflag='--shared' ;; nagfor*) # NAGFOR 5.3 tmp_sharedflag='-Wl,-shared' ;; xl[[cC]]* | bgxl[[cC]]* | mpixl[[cC]]*) # IBM XL C 8.0 on PPC (deal with xlf below) tmp_sharedflag='-qmkshrobj' tmp_addflag= ;; nvcc*) # Cuda Compiler Driver 2.2 _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' _LT_TAGVAR(compiler_needs_object, $1)=yes ;; esac case `$CC -V 2>&1 | sed 5q` in *Sun\ C*) # Sun C 5.9 _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' _LT_TAGVAR(compiler_needs_object, $1)=yes tmp_sharedflag='-G' ;; *Sun\ F*) # Sun Fortran 8.3 tmp_sharedflag='-G' ;; esac _LT_TAGVAR(archive_cmds, $1)='$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' if test yes = "$supports_anon_versioning"; then _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~ cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ echo "local: *; };" >> $output_objdir/$libname.ver~ $CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib' fi case $cc_basename in tcc*) _LT_TAGVAR(export_dynamic_flag_spec, $1)='-rdynamic' ;; xlf* | bgf* | bgxlf* | mpixlf*) # IBM XL Fortran 10.1 on PPC cannot create shared libs itself _LT_TAGVAR(whole_archive_flag_spec, $1)='--whole-archive$convenience --no-whole-archive' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(archive_cmds, $1)='$LD -shared $libobjs $deplibs $linker_flags -soname $soname -o $lib' if test yes = "$supports_anon_versioning"; then _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~ cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ echo "local: *; };" >> $output_objdir/$libname.ver~ $LD -shared $libobjs $deplibs $linker_flags -soname $soname -version-script $output_objdir/$libname.ver -o $lib' fi ;; esac else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; netbsd* | netbsdelf*-gnu) if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable $libobjs $deplibs $linker_flags -o $lib' wlarc= else _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' fi ;; solaris*) if $LD -v 2>&1 | $GREP 'BFD 2\.8' > /dev/null; then _LT_TAGVAR(ld_shlibs, $1)=no cat <<_LT_EOF 1>&2 *** Warning: The releases 2.8.* of the GNU linker cannot reliably *** create shared libraries on Solaris systems. Therefore, libtool *** is disabling shared libraries support. We urge you to upgrade GNU *** binutils to release 2.9.1 or newer. Another option is to modify *** your PATH or compiler configuration so that the native linker is *** used, and then restart. _LT_EOF elif $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*) case `$LD -v 2>&1` in *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.1[[0-5]].*) _LT_TAGVAR(ld_shlibs, $1)=no cat <<_LT_EOF 1>&2 *** Warning: Releases of the GNU linker prior to 2.16.91.0.3 cannot *** reliably create shared libraries on SCO systems. Therefore, libtool *** is disabling shared libraries support. We urge you to upgrade GNU *** binutils to release 2.16.91.0.3 or newer. Another option is to modify *** your PATH or compiler configuration so that the native linker is *** used, and then restart. _LT_EOF ;; *) # For security reasons, it is highly recommended that you always # use absolute paths for naming shared libraries, and exclude the # DT_RUNPATH tag from executables and libraries. But doing so # requires that you compile everything twice, which is a pain. if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; esac ;; sunos4*) _LT_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bshareable -o $lib $libobjs $deplibs $linker_flags' wlarc= _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; *) if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; esac if test no = "$_LT_TAGVAR(ld_shlibs, $1)"; then runpath_var= _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= _LT_TAGVAR(export_dynamic_flag_spec, $1)= _LT_TAGVAR(whole_archive_flag_spec, $1)= fi else # PORTME fill in a description of your system's linker (not GNU ld) case $host_os in aix3*) _LT_TAGVAR(allow_undefined_flag, $1)=unsupported _LT_TAGVAR(always_export_symbols, $1)=yes _LT_TAGVAR(archive_expsym_cmds, $1)='$LD -o $output_objdir/$soname $libobjs $deplibs $linker_flags -bE:$export_symbols -T512 -H512 -bM:SRE~$AR $AR_FLAGS $lib $output_objdir/$soname' # Note: this linker hardcodes the directories in LIBPATH if there # are no directories specified by -L. _LT_TAGVAR(hardcode_minus_L, $1)=yes if test yes = "$GCC" && test -z "$lt_prog_compiler_static"; then # Neither direct hardcoding nor static linking is supported with a # broken collect2. _LT_TAGVAR(hardcode_direct, $1)=unsupported fi ;; aix[[4-9]]*) if test ia64 = "$host_cpu"; then # On IA64, the linker does run time linking by default, so we don't # have to do anything special. aix_use_runtimelinking=no exp_sym_flag='-Bexport' no_entry_flag= else # If we're using GNU nm, then we don't want the "-C" option. # -C means demangle to GNU nm, but means don't demangle to AIX nm. # Without the "-l" option, or with the "-B" option, AIX nm treats # weak defined symbols like other global defined symbols, whereas # GNU nm marks them as "W". # While the 'weak' keyword is ignored in the Export File, we need # it in the Import File for the 'aix-soname' feature, so we have # to replace the "-B" option with "-P" for AIX nm. if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then _LT_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && ([substr](\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols' else _LT_TAGVAR(export_symbols_cmds, $1)='`func_echo_all $NM | $SED -e '\''s/B\([[^B]]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && ([substr](\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols' fi aix_use_runtimelinking=no # Test if we are trying to use run time linking or normal # AIX style linking. If -brtl is somewhere in LDFLAGS, we # have runtime linking enabled, and use it for executables. # For shared libraries, we enable/disable runtime linking # depending on the kind of the shared library created - # when "with_aix_soname,aix_use_runtimelinking" is: # "aix,no" lib.a(lib.so.V) shared, rtl:no, for executables # "aix,yes" lib.so shared, rtl:yes, for executables # lib.a static archive # "both,no" lib.so.V(shr.o) shared, rtl:yes # lib.a(lib.so.V) shared, rtl:no, for executables # "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables # lib.a(lib.so.V) shared, rtl:no # "svr4,*" lib.so.V(shr.o) shared, rtl:yes, for executables # lib.a static archive case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*) for ld_flag in $LDFLAGS; do if (test x-brtl = "x$ld_flag" || test x-Wl,-brtl = "x$ld_flag"); then aix_use_runtimelinking=yes break fi done if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then # With aix-soname=svr4, we create the lib.so.V shared archives only, # so we don't have lib.a shared libs to link our executables. # We have to force runtime linking in this case. aix_use_runtimelinking=yes LDFLAGS="$LDFLAGS -Wl,-brtl" fi ;; esac exp_sym_flag='-bexport' no_entry_flag='-bnoentry' fi # When large executables or shared objects are built, AIX ld can # have problems creating the table of contents. If linking a library # or program results in "error TOC overflow" add -mminimal-toc to # CXXFLAGS/CFLAGS for g++/gcc. In the cases where that is not # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS. _LT_TAGVAR(archive_cmds, $1)='' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_direct_absolute, $1)=yes _LT_TAGVAR(hardcode_libdir_separator, $1)=':' _LT_TAGVAR(link_all_deplibs, $1)=yes _LT_TAGVAR(file_list_spec, $1)='$wl-f,' case $with_aix_soname,$aix_use_runtimelinking in aix,*) ;; # traditional, no import file svr4,* | *,yes) # use import file # The Import File defines what to hardcode. _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=no ;; esac if test yes = "$GCC"; then case $host_os in aix4.[[012]]|aix4.[[012]].*) # We only want to do this on AIX 4.2 and lower, the check # below for broken collect2 doesn't work under 4.3+ collect2name=`$CC -print-prog-name=collect2` if test -f "$collect2name" && strings "$collect2name" | $GREP resolve_lib_name >/dev/null then # We have reworked collect2 : else # We have old collect2 _LT_TAGVAR(hardcode_direct, $1)=unsupported # It fails to find uninstalled libraries when the uninstalled # path is not listed in the libpath. Setting hardcode_minus_L # to unsupported forces relinking _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)= fi ;; esac shared_flag='-shared' if test yes = "$aix_use_runtimelinking"; then shared_flag="$shared_flag "'$wl-G' fi # Need to ensure runtime linking is disabled for the traditional # shared library, or the linker may eventually find shared libraries # /with/ Import File - we do not want to mix them. shared_flag_aix='-shared' shared_flag_svr4='-shared $wl-G' else # not using gcc if test ia64 = "$host_cpu"; then # VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release # chokes on -Wl,-G. The following line is correct: shared_flag='-G' else if test yes = "$aix_use_runtimelinking"; then shared_flag='$wl-G' else shared_flag='$wl-bM:SRE' fi shared_flag_aix='$wl-bM:SRE' shared_flag_svr4='$wl-G' fi fi _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-bexpall' # It seems that -bexpall does not export symbols beginning with # underscore (_), so it is better to generate a list of symbols to export. _LT_TAGVAR(always_export_symbols, $1)=yes if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then # Warning - without using the other runtime loading flags (-brtl), # -berok will link without error, but may produce a broken library. _LT_TAGVAR(allow_undefined_flag, $1)='-berok' # Determine the default libpath from the value encoded in an # empty executable. _LT_SYS_MODULE_PATH_AIX([$1]) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag else if test ia64 = "$host_cpu"; then _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $libdir:/usr/lib:/lib' _LT_TAGVAR(allow_undefined_flag, $1)="-z nodefs" _LT_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols" else # Determine the default libpath from the value encoded in an # empty executable. _LT_SYS_MODULE_PATH_AIX([$1]) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" # Warning - without using the other run time loading flags, # -berok will link without error, but may produce a broken library. _LT_TAGVAR(no_undefined_flag, $1)=' $wl-bernotok' _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-berok' if test yes = "$with_gnu_ld"; then # We only use this code for GNU lds that support --whole-archive. _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive' else # Exported symbols can be pulled into shared objects from archives _LT_TAGVAR(whole_archive_flag_spec, $1)='$convenience' fi _LT_TAGVAR(archive_cmds_need_lc, $1)=yes _LT_TAGVAR(archive_expsym_cmds, $1)='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d' # -brtl affects multiple linker settings, -berok does not and is overridden later compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([[, ]]\\)%-berok\\1%g"`' if test svr4 != "$with_aix_soname"; then # This is similar to how AIX traditionally builds its shared libraries. _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname' fi if test aix != "$with_aix_soname"; then _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp' else # used by -dlpreopen to get the symbols _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$MV $output_objdir/$realname.d/$soname $output_objdir' fi _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$RM -r $output_objdir/$realname.d' fi fi ;; amigaos*) case $host_cpu in powerpc) # see comment about AmigaOS4 .so support _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='' ;; m68k) _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_minus_L, $1)=yes ;; esac ;; bsdi[[45]]*) _LT_TAGVAR(export_dynamic_flag_spec, $1)=-rdynamic ;; cygwin* | mingw* | pw32* | cegcc*) # When not using gcc, we currently assume that we are using # Microsoft Visual C++. # hardcode_libdir_flag_spec is actually meaningless, as there is # no search path for DLLs. case $cc_basename in cl*) # Native MSVC _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' ' _LT_TAGVAR(allow_undefined_flag, $1)=unsupported _LT_TAGVAR(always_export_symbols, $1)=yes _LT_TAGVAR(file_list_spec, $1)='@' # Tell ltmain to make .lib files, not .a files. libext=lib # Tell ltmain to make .dll files, not .so files. shrext_cmds=.dll # FIXME: Setting linknames here is a bad hack. _LT_TAGVAR(archive_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames=' _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then cp "$export_symbols" "$output_objdir/$soname.def"; echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp"; else $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp; fi~ $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~ linknames=' # The linker will not automatically build a static lib if we build a DLL. # _LT_TAGVAR(old_archive_from_new_cmds, $1)='true' _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes _LT_TAGVAR(exclude_expsyms, $1)='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*' _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1,DATA/'\'' | $SED -e '\''/^[[AITW]][[ ]]/s/.*[[ ]]//'\'' | sort | uniq > $export_symbols' # Don't use ranlib _LT_TAGVAR(old_postinstall_cmds, $1)='chmod 644 $oldlib' _LT_TAGVAR(postlink_cmds, $1)='lt_outputfile="@OUTPUT@"~ lt_tool_outputfile="@TOOL_OUTPUT@"~ case $lt_outputfile in *.exe|*.EXE) ;; *) lt_outputfile=$lt_outputfile.exe lt_tool_outputfile=$lt_tool_outputfile.exe ;; esac~ if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1; $RM "$lt_outputfile.manifest"; fi' ;; *) # Assume MSVC wrapper _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' ' _LT_TAGVAR(allow_undefined_flag, $1)=unsupported # Tell ltmain to make .lib files, not .a files. libext=lib # Tell ltmain to make .dll files, not .so files. shrext_cmds=.dll # FIXME: Setting linknames here is a bad hack. _LT_TAGVAR(archive_cmds, $1)='$CC -o $lib $libobjs $compiler_flags `func_echo_all "$deplibs" | $SED '\''s/ -lc$//'\''` -link -dll~linknames=' # The linker will automatically build a .lib file if we build a DLL. _LT_TAGVAR(old_archive_from_new_cmds, $1)='true' # FIXME: Should let the user specify the lib program. _LT_TAGVAR(old_archive_cmds, $1)='lib -OUT:$oldlib$oldobjs$old_deplibs' _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes ;; esac ;; darwin* | rhapsody*) _LT_DARWIN_LINKER_FEATURES($1) ;; dgux*) _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor # support. Future versions do this automatically, but an explicit c++rt0.o # does not break anything, and helps significantly (at the cost of a little # extra space). freebsd2.2*) _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags /usr/lib/c++rt0.o' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; # Unfortunately, older versions of FreeBSD 2 do not have this feature. freebsd2.*) _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; # FreeBSD 3 and greater uses gcc -shared to do shared libraries. freebsd* | dragonfly*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; hpux9*) if test yes = "$GCC"; then _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -shared $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $libobjs $deplibs $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' else _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$LD -b +b $install_libdir -o $output_objdir/$soname $libobjs $deplibs $linker_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' fi _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: _LT_TAGVAR(hardcode_direct, $1)=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' ;; hpux10*) if test yes,no = "$GCC,$with_gnu_ld"; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' else _LT_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags' fi if test no = "$with_gnu_ld"; then _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_direct_absolute, $1)=yes _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. _LT_TAGVAR(hardcode_minus_L, $1)=yes fi ;; hpux11*) if test yes,no = "$GCC,$with_gnu_ld"; then case $host_cpu in hppa*64*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' ;; ia64*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags' ;; *) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' ;; esac else case $host_cpu in hppa*64*) _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' ;; ia64*) _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags' ;; *) m4_if($1, [], [ # Older versions of the 11.00 compiler do not understand -b yet # (HP92453-01 A.11.01.20 doesn't, HP92453-01 B.11.X.35175-35176.GP does) _LT_LINKER_OPTION([if $CC understands -b], _LT_TAGVAR(lt_cv_prog_compiler__b, $1), [-b], [_LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags'], [_LT_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags'])], [_LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags']) ;; esac fi if test no = "$with_gnu_ld"; then _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: case $host_cpu in hppa*64*|ia64*) _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; *) _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_direct_absolute, $1)=yes _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. _LT_TAGVAR(hardcode_minus_L, $1)=yes ;; esac fi ;; irix5* | irix6* | nonstopux*) if test yes = "$GCC"; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' # Try to use the -exported_symbol ld option, if it does not # work, assume that -exports_file does not work either and # implicitly export all symbols. # This should be the same for all languages, so no per-tag cache variable. AC_CACHE_CHECK([whether the $host_os linker accepts -exported_symbol], [lt_cv_irix_exported_symbol], [save_LDFLAGS=$LDFLAGS LDFLAGS="$LDFLAGS -shared $wl-exported_symbol ${wl}foo $wl-update_registry $wl/dev/null" AC_LINK_IFELSE( [AC_LANG_SOURCE( [AC_LANG_CASE([C], [[int foo (void) { return 0; }]], [C++], [[int foo (void) { return 0; }]], [Fortran 77], [[ subroutine foo end]], [Fortran], [[ subroutine foo end]])])], [lt_cv_irix_exported_symbol=yes], [lt_cv_irix_exported_symbol=no]) LDFLAGS=$save_LDFLAGS]) if test yes = "$lt_cv_irix_exported_symbol"; then _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations $wl-exports_file $wl$export_symbols -o $lib' fi _LT_TAGVAR(link_all_deplibs, $1)=no else _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -exports_file $export_symbols -o $lib' fi _LT_TAGVAR(archive_cmds_need_lc, $1)='no' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: _LT_TAGVAR(inherit_rpath, $1)=yes _LT_TAGVAR(link_all_deplibs, $1)=yes ;; linux*) case $cc_basename in tcc*) # Fabrice Bellard et al's Tiny C Compiler _LT_TAGVAR(ld_shlibs, $1)=yes _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' ;; esac ;; netbsd* | netbsdelf*-gnu) if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags' # a.out else _LT_TAGVAR(archive_cmds, $1)='$LD -shared -o $lib $libobjs $deplibs $linker_flags' # ELF fi _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; newsos6) _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; *nto* | *qnx*) ;; openbsd* | bitrig*) if test -f /usr/libexec/ld.so; then _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=yes if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags $wl-retain-symbols-file,$export_symbols' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' else _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' fi else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; os2*) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(allow_undefined_flag, $1)=unsupported shrext_cmds=.dll _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ $ECHO EXPORTS >> $output_objdir/$libname.def~ emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ emximp -o $lib $output_objdir/$libname.def' _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ $ECHO EXPORTS >> $output_objdir/$libname.def~ prefix_cmds="$SED"~ if test EXPORTS = "`$SED 1q $export_symbols`"; then prefix_cmds="$prefix_cmds -e 1d"; fi~ prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ emximp -o $lib $output_objdir/$libname.def' _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes ;; osf3*) if test yes = "$GCC"; then _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' else _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*' _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' fi _LT_TAGVAR(archive_cmds_need_lc, $1)='no' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: ;; osf4* | osf5*) # as osf3* with the addition of -msym flag if test yes = "$GCC"; then _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $pic_flag $libobjs $deplibs $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' else _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*' _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done; printf "%s\\n" "-hidden">> $lib.exp~ $CC -shared$allow_undefined_flag $wl-input $wl$lib.exp $compiler_flags $libobjs $deplibs -soname $soname `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~$RM $lib.exp' # Both c and cxx compiler support -rpath directly _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir' fi _LT_TAGVAR(archive_cmds_need_lc, $1)='no' _LT_TAGVAR(hardcode_libdir_separator, $1)=: ;; solaris*) _LT_TAGVAR(no_undefined_flag, $1)=' -z defs' if test yes = "$GCC"; then wlarc='$wl' _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl-z ${wl}text $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ $CC -shared $pic_flag $wl-z ${wl}text $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp' else case `$CC -V 2>&1` in *"Compilers 5.0"*) wlarc='' _LT_TAGVAR(archive_cmds, $1)='$LD -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ $LD -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $linker_flags~$RM $lib.exp' ;; *) wlarc='$wl' _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ $CC -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp' ;; esac fi _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no case $host_os in solaris2.[[0-5]] | solaris2.[[0-5]].*) ;; *) # The compiler driver will combine and reorder linker options, # but understands '-z linker_flag'. GCC discards it without '$wl', # but is careful enough not to reorder. # Supported since Solaris 2.6 (maybe 2.5.1?) if test yes = "$GCC"; then _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract' else _LT_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract' fi ;; esac _LT_TAGVAR(link_all_deplibs, $1)=yes ;; sunos4*) if test sequent = "$host_vendor"; then # Use $CC to link under sequent, because it throws in some extra .o # files that make .init and .fini sections work. _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h $soname -o $lib $libobjs $deplibs $compiler_flags' else _LT_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bstatic -o $lib $libobjs $deplibs $linker_flags' fi _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; sysv4) case $host_vendor in sni) _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_direct, $1)=yes # is this really true??? ;; siemens) ## LD is ld it makes a PLAMLIB ## CC just makes a GrossModule. _LT_TAGVAR(archive_cmds, $1)='$LD -G -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(reload_cmds, $1)='$CC -r -o $output$reload_objs' _LT_TAGVAR(hardcode_direct, $1)=no ;; motorola) _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_direct, $1)=no #Motorola manual says yes, but my tests say they lie ;; esac runpath_var='LD_RUN_PATH' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; sysv4.3*) _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(export_dynamic_flag_spec, $1)='-Bexport' ;; sysv4*MP*) if test -d /usr/nec; then _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no runpath_var=LD_RUN_PATH hardcode_runpath_var=yes _LT_TAGVAR(ld_shlibs, $1)=yes fi ;; sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*) _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no runpath_var='LD_RUN_PATH' if test yes = "$GCC"; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' else _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' fi ;; sysv5* | sco3.2v5* | sco5v6*) # Note: We CANNOT use -z defs as we might desire, because we do not # link with -lc, and that would cause any symbols used from libc to # always be unresolved, which means just about no library would # ever link correctly. If we're not using GNU ld we use -z text # though, which does catch some bad symbols but isn't as heavy-handed # as -z defs. _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' _LT_TAGVAR(allow_undefined_flag, $1)='$wl-z,nodefs' _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R,$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=':' _LT_TAGVAR(link_all_deplibs, $1)=yes _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Bexport' runpath_var='LD_RUN_PATH' if test yes = "$GCC"; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' else _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' fi ;; uts4*) _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; *) _LT_TAGVAR(ld_shlibs, $1)=no ;; esac if test sni = "$host_vendor"; then case $host in sysv4 | sysv4.2uw2* | sysv4.3* | sysv5*) _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Blargedynsym' ;; esac fi fi ]) AC_MSG_RESULT([$_LT_TAGVAR(ld_shlibs, $1)]) test no = "$_LT_TAGVAR(ld_shlibs, $1)" && can_build_shared=no _LT_TAGVAR(with_gnu_ld, $1)=$with_gnu_ld _LT_DECL([], [libext], [0], [Old archive suffix (normally "a")])dnl _LT_DECL([], [shrext_cmds], [1], [Shared library suffix (normally ".so")])dnl _LT_DECL([], [extract_expsyms_cmds], [2], [The commands to extract the exported symbol list from a shared archive]) # # Do we need to explicitly link libc? # case "x$_LT_TAGVAR(archive_cmds_need_lc, $1)" in x|xyes) # Assume -lc should be added _LT_TAGVAR(archive_cmds_need_lc, $1)=yes if test yes,yes = "$GCC,$enable_shared"; then case $_LT_TAGVAR(archive_cmds, $1) in *'~'*) # FIXME: we may have to deal with multi-command sequences. ;; '$CC '*) # Test whether the compiler implicitly links with -lc since on some # systems, -lgcc has to come before -lc. If gcc already passes -lc # to ld, don't add -lc before -lgcc. AC_CACHE_CHECK([whether -lc should be explicitly linked in], [lt_cv_]_LT_TAGVAR(archive_cmds_need_lc, $1), [$RM conftest* echo "$lt_simple_compile_test_code" > conftest.$ac_ext if AC_TRY_EVAL(ac_compile) 2>conftest.err; then soname=conftest lib=conftest libobjs=conftest.$ac_objext deplibs= wl=$_LT_TAGVAR(lt_prog_compiler_wl, $1) pic_flag=$_LT_TAGVAR(lt_prog_compiler_pic, $1) compiler_flags=-v linker_flags=-v verstring= output_objdir=. libname=conftest lt_save_allow_undefined_flag=$_LT_TAGVAR(allow_undefined_flag, $1) _LT_TAGVAR(allow_undefined_flag, $1)= if AC_TRY_EVAL(_LT_TAGVAR(archive_cmds, $1) 2\>\&1 \| $GREP \" -lc \" \>/dev/null 2\>\&1) then lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)=no else lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)=yes fi _LT_TAGVAR(allow_undefined_flag, $1)=$lt_save_allow_undefined_flag else cat conftest.err 1>&5 fi $RM conftest* ]) _LT_TAGVAR(archive_cmds_need_lc, $1)=$lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1) ;; esac fi ;; esac _LT_TAGDECL([build_libtool_need_lc], [archive_cmds_need_lc], [0], [Whether or not to add -lc for building shared libraries]) _LT_TAGDECL([allow_libtool_libs_with_static_runtimes], [enable_shared_with_static_runtimes], [0], [Whether or not to disallow shared libs when runtime libs are static]) _LT_TAGDECL([], [export_dynamic_flag_spec], [1], [Compiler flag to allow reflexive dlopens]) _LT_TAGDECL([], [whole_archive_flag_spec], [1], [Compiler flag to generate shared objects directly from archives]) _LT_TAGDECL([], [compiler_needs_object], [1], [Whether the compiler copes with passing no objects directly]) _LT_TAGDECL([], [old_archive_from_new_cmds], [2], [Create an old-style archive from a shared archive]) _LT_TAGDECL([], [old_archive_from_expsyms_cmds], [2], [Create a temporary old-style archive to link instead of a shared archive]) _LT_TAGDECL([], [archive_cmds], [2], [Commands used to build a shared archive]) _LT_TAGDECL([], [archive_expsym_cmds], [2]) _LT_TAGDECL([], [module_cmds], [2], [Commands used to build a loadable module if different from building a shared archive.]) _LT_TAGDECL([], [module_expsym_cmds], [2]) _LT_TAGDECL([], [with_gnu_ld], [1], [Whether we are building with GNU ld or not]) _LT_TAGDECL([], [allow_undefined_flag], [1], [Flag that allows shared libraries with undefined symbols to be built]) _LT_TAGDECL([], [no_undefined_flag], [1], [Flag that enforces no undefined symbols]) _LT_TAGDECL([], [hardcode_libdir_flag_spec], [1], [Flag to hardcode $libdir into a binary during linking. This must work even if $libdir does not exist]) _LT_TAGDECL([], [hardcode_libdir_separator], [1], [Whether we need a single "-rpath" flag with a separated argument]) _LT_TAGDECL([], [hardcode_direct], [0], [Set to "yes" if using DIR/libNAME$shared_ext during linking hardcodes DIR into the resulting binary]) _LT_TAGDECL([], [hardcode_direct_absolute], [0], [Set to "yes" if using DIR/libNAME$shared_ext during linking hardcodes DIR into the resulting binary and the resulting library dependency is "absolute", i.e impossible to change by setting $shlibpath_var if the library is relocated]) _LT_TAGDECL([], [hardcode_minus_L], [0], [Set to "yes" if using the -LDIR flag during linking hardcodes DIR into the resulting binary]) _LT_TAGDECL([], [hardcode_shlibpath_var], [0], [Set to "yes" if using SHLIBPATH_VAR=DIR during linking hardcodes DIR into the resulting binary]) _LT_TAGDECL([], [hardcode_automatic], [0], [Set to "yes" if building a shared library automatically hardcodes DIR into the library and all subsequent libraries and executables linked against it]) _LT_TAGDECL([], [inherit_rpath], [0], [Set to yes if linker adds runtime paths of dependent libraries to runtime path list]) _LT_TAGDECL([], [link_all_deplibs], [0], [Whether libtool must link a program against all its dependency libraries]) _LT_TAGDECL([], [always_export_symbols], [0], [Set to "yes" if exported symbols are required]) _LT_TAGDECL([], [export_symbols_cmds], [2], [The commands to list exported symbols]) _LT_TAGDECL([], [exclude_expsyms], [1], [Symbols that should not be listed in the preloaded symbols]) _LT_TAGDECL([], [include_expsyms], [1], [Symbols that must always be exported]) _LT_TAGDECL([], [prelink_cmds], [2], [Commands necessary for linking programs (against libraries) with templates]) _LT_TAGDECL([], [postlink_cmds], [2], [Commands necessary for finishing linking programs]) _LT_TAGDECL([], [file_list_spec], [1], [Specify filename containing input files]) dnl FIXME: Not yet implemented dnl _LT_TAGDECL([], [thread_safe_flag_spec], [1], dnl [Compiler flag to generate thread safe objects]) ])# _LT_LINKER_SHLIBS # _LT_LANG_C_CONFIG([TAG]) # ------------------------ # Ensure that the configuration variables for a C compiler are suitably # defined. These variables are subsequently used by _LT_CONFIG to write # the compiler configuration to 'libtool'. m4_defun([_LT_LANG_C_CONFIG], [m4_require([_LT_DECL_EGREP])dnl lt_save_CC=$CC AC_LANG_PUSH(C) # Source file extension for C test sources. ac_ext=c # Object file extension for compiled C test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # Code to be used in simple compile tests lt_simple_compile_test_code="int some_variable = 0;" # Code to be used in simple link tests lt_simple_link_test_code='int main(){return(0);}' _LT_TAG_COMPILER # Save the default compiler, since it gets overwritten when the other # tags are being tested, and _LT_TAGVAR(compiler, []) is a NOP. compiler_DEFAULT=$CC # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE ## CAVEAT EMPTOR: ## There is no encapsulation within the following macros, do not change ## the running order or otherwise move them around unless you know exactly ## what you are doing... if test -n "$compiler"; then _LT_COMPILER_NO_RTTI($1) _LT_COMPILER_PIC($1) _LT_COMPILER_C_O($1) _LT_COMPILER_FILE_LOCKS($1) _LT_LINKER_SHLIBS($1) _LT_SYS_DYNAMIC_LINKER($1) _LT_LINKER_HARDCODE_LIBPATH($1) LT_SYS_DLOPEN_SELF _LT_CMD_STRIPLIB # Report what library types will actually be built AC_MSG_CHECKING([if libtool supports shared libraries]) AC_MSG_RESULT([$can_build_shared]) AC_MSG_CHECKING([whether to build shared libraries]) test no = "$can_build_shared" && enable_shared=no # On AIX, shared libraries and static libraries use the same namespace, and # are all built from PIC. case $host_os in aix3*) test yes = "$enable_shared" && enable_static=no if test -n "$RANLIB"; then archive_cmds="$archive_cmds~\$RANLIB \$lib" postinstall_cmds='$RANLIB $lib' fi ;; aix[[4-9]]*) if test ia64 != "$host_cpu"; then case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in yes,aix,yes) ;; # shared object as lib.so file only yes,svr4,*) ;; # shared object as lib.so archive member only yes,*) enable_static=no ;; # shared object in lib.a archive as well esac fi ;; esac AC_MSG_RESULT([$enable_shared]) AC_MSG_CHECKING([whether to build static libraries]) # Make sure either enable_shared or enable_static is yes. test yes = "$enable_shared" || enable_static=yes AC_MSG_RESULT([$enable_static]) _LT_CONFIG($1) fi AC_LANG_POP CC=$lt_save_CC ])# _LT_LANG_C_CONFIG # _LT_LANG_CXX_CONFIG([TAG]) # -------------------------- # Ensure that the configuration variables for a C++ compiler are suitably # defined. These variables are subsequently used by _LT_CONFIG to write # the compiler configuration to 'libtool'. m4_defun([_LT_LANG_CXX_CONFIG], [m4_require([_LT_FILEUTILS_DEFAULTS])dnl m4_require([_LT_DECL_EGREP])dnl m4_require([_LT_PATH_MANIFEST_TOOL])dnl if test -n "$CXX" && ( test no != "$CXX" && ( (test g++ = "$CXX" && `g++ -v >/dev/null 2>&1` ) || (test g++ != "$CXX"))); then AC_PROG_CXXCPP else _lt_caught_CXX_error=yes fi AC_LANG_PUSH(C++) _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(allow_undefined_flag, $1)= _LT_TAGVAR(always_export_symbols, $1)=no _LT_TAGVAR(archive_expsym_cmds, $1)= _LT_TAGVAR(compiler_needs_object, $1)=no _LT_TAGVAR(export_dynamic_flag_spec, $1)= _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= _LT_TAGVAR(hardcode_libdir_separator, $1)= _LT_TAGVAR(hardcode_minus_L, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported _LT_TAGVAR(hardcode_automatic, $1)=no _LT_TAGVAR(inherit_rpath, $1)=no _LT_TAGVAR(module_cmds, $1)= _LT_TAGVAR(module_expsym_cmds, $1)= _LT_TAGVAR(link_all_deplibs, $1)=unknown _LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds _LT_TAGVAR(reload_flag, $1)=$reload_flag _LT_TAGVAR(reload_cmds, $1)=$reload_cmds _LT_TAGVAR(no_undefined_flag, $1)= _LT_TAGVAR(whole_archive_flag_spec, $1)= _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no # Source file extension for C++ test sources. ac_ext=cpp # Object file extension for compiled C++ test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # No sense in running all these tests if we already determined that # the CXX compiler isn't working. Some variables (like enable_shared) # are currently assumed to apply to all compilers on this platform, # and will be corrupted by setting them based on a non-working compiler. if test yes != "$_lt_caught_CXX_error"; then # Code to be used in simple compile tests lt_simple_compile_test_code="int some_variable = 0;" # Code to be used in simple link tests lt_simple_link_test_code='int main(int, char *[[]]) { return(0); }' # ltmain only uses $CC for tagged configurations so make sure $CC is set. _LT_TAG_COMPILER # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE # Allow CC to be a program name with arguments. lt_save_CC=$CC lt_save_CFLAGS=$CFLAGS lt_save_LD=$LD lt_save_GCC=$GCC GCC=$GXX lt_save_with_gnu_ld=$with_gnu_ld lt_save_path_LD=$lt_cv_path_LD if test -n "${lt_cv_prog_gnu_ldcxx+set}"; then lt_cv_prog_gnu_ld=$lt_cv_prog_gnu_ldcxx else $as_unset lt_cv_prog_gnu_ld fi if test -n "${lt_cv_path_LDCXX+set}"; then lt_cv_path_LD=$lt_cv_path_LDCXX else $as_unset lt_cv_path_LD fi test -z "${LDCXX+set}" || LD=$LDCXX CC=${CXX-"c++"} CFLAGS=$CXXFLAGS compiler=$CC _LT_TAGVAR(compiler, $1)=$CC _LT_CC_BASENAME([$compiler]) if test -n "$compiler"; then # We don't want -fno-exception when compiling C++ code, so set the # no_builtin_flag separately if test yes = "$GXX"; then _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin' else _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)= fi if test yes = "$GXX"; then # Set up default GNU C++ configuration LT_PATH_LD # Check if GNU C++ uses GNU ld as the underlying linker, since the # archiving commands below assume that GNU ld is being used. if test yes = "$with_gnu_ld"; then _LT_TAGVAR(archive_cmds, $1)='$CC $pic_flag -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC $pic_flag -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' # If archive_cmds runs LD, not CC, wlarc should be empty # XXX I think wlarc can be eliminated in ltcf-cxx, but I need to # investigate it a little bit more. (MM) wlarc='$wl' # ancient GNU ld didn't support --whole-archive et. al. if eval "`$CC -print-prog-name=ld` --help 2>&1" | $GREP 'no-whole-archive' > /dev/null; then _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' else _LT_TAGVAR(whole_archive_flag_spec, $1)= fi else with_gnu_ld=no wlarc= # A generic and very simple default shared library creation # command for GNU C++ for the case where it uses the native # linker, instead of GNU ld. If possible, this setting should # overridden to take advantage of the native linker features on # the platform it is being used on. _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib' fi # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP " \-L"' else GXX=no with_gnu_ld=no wlarc= fi # PORTME: fill in a description of your system's C++ link characteristics AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries]) _LT_TAGVAR(ld_shlibs, $1)=yes case $host_os in aix3*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; aix[[4-9]]*) if test ia64 = "$host_cpu"; then # On IA64, the linker does run time linking by default, so we don't # have to do anything special. aix_use_runtimelinking=no exp_sym_flag='-Bexport' no_entry_flag= else aix_use_runtimelinking=no # Test if we are trying to use run time linking or normal # AIX style linking. If -brtl is somewhere in LDFLAGS, we # have runtime linking enabled, and use it for executables. # For shared libraries, we enable/disable runtime linking # depending on the kind of the shared library created - # when "with_aix_soname,aix_use_runtimelinking" is: # "aix,no" lib.a(lib.so.V) shared, rtl:no, for executables # "aix,yes" lib.so shared, rtl:yes, for executables # lib.a static archive # "both,no" lib.so.V(shr.o) shared, rtl:yes # lib.a(lib.so.V) shared, rtl:no, for executables # "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables # lib.a(lib.so.V) shared, rtl:no # "svr4,*" lib.so.V(shr.o) shared, rtl:yes, for executables # lib.a static archive case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*) for ld_flag in $LDFLAGS; do case $ld_flag in *-brtl*) aix_use_runtimelinking=yes break ;; esac done if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then # With aix-soname=svr4, we create the lib.so.V shared archives only, # so we don't have lib.a shared libs to link our executables. # We have to force runtime linking in this case. aix_use_runtimelinking=yes LDFLAGS="$LDFLAGS -Wl,-brtl" fi ;; esac exp_sym_flag='-bexport' no_entry_flag='-bnoentry' fi # When large executables or shared objects are built, AIX ld can # have problems creating the table of contents. If linking a library # or program results in "error TOC overflow" add -mminimal-toc to # CXXFLAGS/CFLAGS for g++/gcc. In the cases where that is not # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS. _LT_TAGVAR(archive_cmds, $1)='' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_direct_absolute, $1)=yes _LT_TAGVAR(hardcode_libdir_separator, $1)=':' _LT_TAGVAR(link_all_deplibs, $1)=yes _LT_TAGVAR(file_list_spec, $1)='$wl-f,' case $with_aix_soname,$aix_use_runtimelinking in aix,*) ;; # no import file svr4,* | *,yes) # use import file # The Import File defines what to hardcode. _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=no ;; esac if test yes = "$GXX"; then case $host_os in aix4.[[012]]|aix4.[[012]].*) # We only want to do this on AIX 4.2 and lower, the check # below for broken collect2 doesn't work under 4.3+ collect2name=`$CC -print-prog-name=collect2` if test -f "$collect2name" && strings "$collect2name" | $GREP resolve_lib_name >/dev/null then # We have reworked collect2 : else # We have old collect2 _LT_TAGVAR(hardcode_direct, $1)=unsupported # It fails to find uninstalled libraries when the uninstalled # path is not listed in the libpath. Setting hardcode_minus_L # to unsupported forces relinking _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)= fi esac shared_flag='-shared' if test yes = "$aix_use_runtimelinking"; then shared_flag=$shared_flag' $wl-G' fi # Need to ensure runtime linking is disabled for the traditional # shared library, or the linker may eventually find shared libraries # /with/ Import File - we do not want to mix them. shared_flag_aix='-shared' shared_flag_svr4='-shared $wl-G' else # not using gcc if test ia64 = "$host_cpu"; then # VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release # chokes on -Wl,-G. The following line is correct: shared_flag='-G' else if test yes = "$aix_use_runtimelinking"; then shared_flag='$wl-G' else shared_flag='$wl-bM:SRE' fi shared_flag_aix='$wl-bM:SRE' shared_flag_svr4='$wl-G' fi fi _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-bexpall' # It seems that -bexpall does not export symbols beginning with # underscore (_), so it is better to generate a list of symbols to # export. _LT_TAGVAR(always_export_symbols, $1)=yes if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then # Warning - without using the other runtime loading flags (-brtl), # -berok will link without error, but may produce a broken library. # The "-G" linker flag allows undefined symbols. _LT_TAGVAR(no_undefined_flag, $1)='-bernotok' # Determine the default libpath from the value encoded in an empty # executable. _LT_SYS_MODULE_PATH_AIX([$1]) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag else if test ia64 = "$host_cpu"; then _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $libdir:/usr/lib:/lib' _LT_TAGVAR(allow_undefined_flag, $1)="-z nodefs" _LT_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols" else # Determine the default libpath from the value encoded in an # empty executable. _LT_SYS_MODULE_PATH_AIX([$1]) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" # Warning - without using the other run time loading flags, # -berok will link without error, but may produce a broken library. _LT_TAGVAR(no_undefined_flag, $1)=' $wl-bernotok' _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-berok' if test yes = "$with_gnu_ld"; then # We only use this code for GNU lds that support --whole-archive. _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive' else # Exported symbols can be pulled into shared objects from archives _LT_TAGVAR(whole_archive_flag_spec, $1)='$convenience' fi _LT_TAGVAR(archive_cmds_need_lc, $1)=yes _LT_TAGVAR(archive_expsym_cmds, $1)='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d' # -brtl affects multiple linker settings, -berok does not and is overridden later compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([[, ]]\\)%-berok\\1%g"`' if test svr4 != "$with_aix_soname"; then # This is similar to how AIX traditionally builds its shared # libraries. Need -bnortl late, we may have -brtl in LDFLAGS. _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname' fi if test aix != "$with_aix_soname"; then _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp' else # used by -dlpreopen to get the symbols _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$MV $output_objdir/$realname.d/$soname $output_objdir' fi _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$RM -r $output_objdir/$realname.d' fi fi ;; beos*) if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then _LT_TAGVAR(allow_undefined_flag, $1)=unsupported # Joseph Beckenbach says some releases of gcc # support --undefined. This deserves some investigation. FIXME _LT_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; chorus*) case $cc_basename in *) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; esac ;; cygwin* | mingw* | pw32* | cegcc*) case $GXX,$cc_basename in ,cl* | no,cl*) # Native MSVC # hardcode_libdir_flag_spec is actually meaningless, as there is # no search path for DLLs. _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' ' _LT_TAGVAR(allow_undefined_flag, $1)=unsupported _LT_TAGVAR(always_export_symbols, $1)=yes _LT_TAGVAR(file_list_spec, $1)='@' # Tell ltmain to make .lib files, not .a files. libext=lib # Tell ltmain to make .dll files, not .so files. shrext_cmds=.dll # FIXME: Setting linknames here is a bad hack. _LT_TAGVAR(archive_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames=' _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then cp "$export_symbols" "$output_objdir/$soname.def"; echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp"; else $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp; fi~ $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~ linknames=' # The linker will not automatically build a static lib if we build a DLL. # _LT_TAGVAR(old_archive_from_new_cmds, $1)='true' _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes # Don't use ranlib _LT_TAGVAR(old_postinstall_cmds, $1)='chmod 644 $oldlib' _LT_TAGVAR(postlink_cmds, $1)='lt_outputfile="@OUTPUT@"~ lt_tool_outputfile="@TOOL_OUTPUT@"~ case $lt_outputfile in *.exe|*.EXE) ;; *) lt_outputfile=$lt_outputfile.exe lt_tool_outputfile=$lt_tool_outputfile.exe ;; esac~ func_to_tool_file "$lt_outputfile"~ if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1; $RM "$lt_outputfile.manifest"; fi' ;; *) # g++ # _LT_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless, # as there is no search path for DLLs. _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-all-symbols' _LT_TAGVAR(allow_undefined_flag, $1)=unsupported _LT_TAGVAR(always_export_symbols, $1)=no _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' # If the export-symbols file already is a .def file, use it as # is; otherwise, prepend EXPORTS... _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then cp $export_symbols $output_objdir/$soname.def; else echo EXPORTS > $output_objdir/$soname.def; cat $export_symbols >> $output_objdir/$soname.def; fi~ $CC -shared -nostdlib $output_objdir/$soname.def $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; esac ;; darwin* | rhapsody*) _LT_DARWIN_LINKER_FEATURES($1) ;; os2*) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' _LT_TAGVAR(hardcode_minus_L, $1)=yes _LT_TAGVAR(allow_undefined_flag, $1)=unsupported shrext_cmds=.dll _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ $ECHO EXPORTS >> $output_objdir/$libname.def~ emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ emximp -o $lib $output_objdir/$libname.def' _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ $ECHO EXPORTS >> $output_objdir/$libname.def~ prefix_cmds="$SED"~ if test EXPORTS = "`$SED 1q $export_symbols`"; then prefix_cmds="$prefix_cmds -e 1d"; fi~ prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ emximp -o $lib $output_objdir/$libname.def' _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes ;; dgux*) case $cc_basename in ec++*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; ghcx*) # Green Hills C++ Compiler # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; *) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; esac ;; freebsd2.*) # C++ shared libraries reported to be fairly broken before # switch to ELF _LT_TAGVAR(ld_shlibs, $1)=no ;; freebsd-elf*) _LT_TAGVAR(archive_cmds_need_lc, $1)=no ;; freebsd* | dragonfly*) # FreeBSD 3 and later use GNU C++ and GNU ld with standard ELF # conventions _LT_TAGVAR(ld_shlibs, $1)=yes ;; haiku*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(link_all_deplibs, $1)=yes ;; hpux9*) _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH, # but as the default # location of the library. case $cc_basename in CC*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; aCC*) _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -b $wl+b $wl$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. # # There doesn't appear to be a way to prevent this compiler from # explicitly linking system object files so we need to strip them # from the output so that they don't get included in the library # dependencies. output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | $EGREP " \-L"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' ;; *) if test yes = "$GXX"; then _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -shared -nostdlib $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' else # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no fi ;; esac ;; hpux10*|hpux11*) if test no = "$with_gnu_ld"; then _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: case $host_cpu in hppa*64*|ia64*) ;; *) _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' ;; esac fi case $host_cpu in hppa*64*|ia64*) _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no ;; *) _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_direct_absolute, $1)=yes _LT_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH, # but as the default # location of the library. ;; esac case $cc_basename in CC*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; aCC*) case $host_cpu in hppa*64*) _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' ;; ia64*) _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' ;; *) _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' ;; esac # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. # # There doesn't appear to be a way to prevent this compiler from # explicitly linking system object files so we need to strip them # from the output so that they don't get included in the library # dependencies. output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | $GREP " \-L"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' ;; *) if test yes = "$GXX"; then if test no = "$with_gnu_ld"; then case $host_cpu in hppa*64*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib -fPIC $wl+h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' ;; ia64*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' ;; *) _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' ;; esac fi else # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no fi ;; esac ;; interix[[3-9]]*) _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc. # Instead, shared libraries are loaded at an image base (0x10000000 by # default) and relocated if they conflict, which is a slow very memory # consuming and fragmenting process. To avoid this, we pick a random, # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link # time. Moving up from 0x10000000 also allows more sbrk(2) space. _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' ;; irix5* | irix6*) case $cc_basename in CC*) # SGI C++ _LT_TAGVAR(archive_cmds, $1)='$CC -shared -all -multigot $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' # Archives containing C++ object files must be created using # "CC -ar", where "CC" is the IRIX C++ compiler. This is # necessary to make sure instantiated templates are included # in the archive. _LT_TAGVAR(old_archive_cmds, $1)='$CC -ar -WR,-u -o $oldlib $oldobjs' ;; *) if test yes = "$GXX"; then if test no = "$with_gnu_ld"; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' else _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` -o $lib' fi fi _LT_TAGVAR(link_all_deplibs, $1)=yes ;; esac _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: _LT_TAGVAR(inherit_rpath, $1)=yes ;; linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) case $cc_basename in KCC*) # Kuck and Associates, Inc. (KAI) C++ Compiler # KCC will only create a shared library if the output file # ends with ".so" (or ".sl" for HP-UX), so rename the library # to its proper name (with version) after linking. _LT_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib $wl-retain-symbols-file,$export_symbols; mv \$templib $lib' # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. # # There doesn't appear to be a way to prevent this compiler from # explicitly linking system object files so we need to strip them # from the output so that they don't get included in the library # dependencies. output_verbose_link_cmd='templist=`$CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1 | $GREP "ld"`; rm -f libconftest$shared_ext; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' # Archives containing C++ object files must be created using # "CC -Bstatic", where "CC" is the KAI C++ compiler. _LT_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs' ;; icpc* | ecpc* ) # Intel C++ with_gnu_ld=yes # version 8.0 and above of icpc choke on multiply defined symbols # if we add $predep_objects and $postdep_objects, however 7.1 and # earlier do not add the objects themselves. case `$CC -V 2>&1` in *"Version 7."*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' ;; *) # Version 8.0 or newer tmp_idyn= case $host_cpu in ia64*) tmp_idyn=' -i_dynamic';; esac _LT_TAGVAR(archive_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' ;; esac _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive' ;; pgCC* | pgcpp*) # Portland Group C++ compiler case `$CC -V` in *pgCC\ [[1-5]].* | *pgcpp\ [[1-5]].*) _LT_TAGVAR(prelink_cmds, $1)='tpldir=Template.dir~ rm -rf $tpldir~ $CC --prelink_objects --instantiation_dir $tpldir $objs $libobjs $compile_deplibs~ compile_command="$compile_command `find $tpldir -name \*.o | sort | $NL2SP`"' _LT_TAGVAR(old_archive_cmds, $1)='tpldir=Template.dir~ rm -rf $tpldir~ $CC --prelink_objects --instantiation_dir $tpldir $oldobjs$old_deplibs~ $AR $AR_FLAGS $oldlib$oldobjs$old_deplibs `find $tpldir -name \*.o | sort | $NL2SP`~ $RANLIB $oldlib' _LT_TAGVAR(archive_cmds, $1)='tpldir=Template.dir~ rm -rf $tpldir~ $CC --prelink_objects --instantiation_dir $tpldir $predep_objects $libobjs $deplibs $convenience $postdep_objects~ $CC -shared $pic_flag $predep_objects $libobjs $deplibs `find $tpldir -name \*.o | sort | $NL2SP` $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='tpldir=Template.dir~ rm -rf $tpldir~ $CC --prelink_objects --instantiation_dir $tpldir $predep_objects $libobjs $deplibs $convenience $postdep_objects~ $CC -shared $pic_flag $predep_objects $libobjs $deplibs `find $tpldir -name \*.o | sort | $NL2SP` $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' ;; *) # Version 6 and above use weak symbols _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' ;; esac _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl--rpath $wl$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' ;; cxx*) # Compaq C++ _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib $wl-retain-symbols-file $wl$export_symbols' runpath_var=LD_RUN_PATH _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. # # There doesn't appear to be a way to prevent this compiler from # explicitly linking system object files so we need to strip them # from the output so that they don't get included in the library # dependencies. output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP "ld"`; templist=`func_echo_all "$templist" | $SED "s/\(^.*ld.*\)\( .*ld .*$\)/\1/"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "X$list" | $Xsed' ;; xl* | mpixl* | bgxl*) # IBM XL 8.0 on PPC, with GNU ld _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' _LT_TAGVAR(archive_cmds, $1)='$CC -qmkshrobj $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' if test yes = "$supports_anon_versioning"; then _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~ cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ echo "local: *; };" >> $output_objdir/$libname.ver~ $CC -qmkshrobj $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib' fi ;; *) case `$CC -V 2>&1 | sed 5q` in *Sun\ C*) # Sun C++ 5.9 _LT_TAGVAR(no_undefined_flag, $1)=' -zdefs' _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-retain-symbols-file $wl$export_symbols' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' _LT_TAGVAR(compiler_needs_object, $1)=yes # Not sure whether something based on # $CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1 # would be better. output_verbose_link_cmd='func_echo_all' # Archives containing C++ object files must be created using # "CC -xar", where "CC" is the Sun C++ compiler. This is # necessary to make sure instantiated templates are included # in the archive. _LT_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs' ;; esac ;; esac ;; lynxos*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; m88k*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; mvs*) case $cc_basename in cxx*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; *) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; esac ;; netbsd*) if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $predep_objects $libobjs $deplibs $postdep_objects $linker_flags' wlarc= _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no fi # Workaround some broken pre-1.5 toolchains output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP conftest.$objext | $SED -e "s:-lgcc -lc -lgcc::"' ;; *nto* | *qnx*) _LT_TAGVAR(ld_shlibs, $1)=yes ;; openbsd* | bitrig*) if test -f /usr/libexec/ld.so; then _LT_TAGVAR(hardcode_direct, $1)=yes _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=yes _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`"; then _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-retain-symbols-file,$export_symbols -o $lib' _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' fi output_verbose_link_cmd=func_echo_all else _LT_TAGVAR(ld_shlibs, $1)=no fi ;; osf3* | osf4* | osf5*) case $cc_basename in KCC*) # Kuck and Associates, Inc. (KAI) C++ Compiler # KCC will only create a shared library if the output file # ends with ".so" (or ".sl" for HP-UX), so rename the library # to its proper name (with version) after linking. _LT_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo "$lib" | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: # Archives containing C++ object files must be created using # the KAI C++ compiler. case $host in osf3*) _LT_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs' ;; *) _LT_TAGVAR(old_archive_cmds, $1)='$CC -o $oldlib $oldobjs' ;; esac ;; RCC*) # Rational C++ 2.4.1 # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; cxx*) case $host in osf3*) _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $soname `test -n "$verstring" && func_echo_all "$wl-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' ;; *) _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*' _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done~ echo "-hidden">> $lib.exp~ $CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname $wl-input $wl$lib.exp `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~ $RM $lib.exp' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir' ;; esac _LT_TAGVAR(hardcode_libdir_separator, $1)=: # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. # # There doesn't appear to be a way to prevent this compiler from # explicitly linking system object files so we need to strip them # from the output so that they don't get included in the library # dependencies. output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP "ld" | $GREP -v "ld:"`; templist=`func_echo_all "$templist" | $SED "s/\(^.*ld.*\)\( .*ld.*$\)/\1/"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' ;; *) if test yes,no = "$GXX,$with_gnu_ld"; then _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' case $host in osf3*) _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' ;; *) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' ;; esac _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=: # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP " \-L"' else # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no fi ;; esac ;; psos*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; sunos4*) case $cc_basename in CC*) # Sun C++ 4.x # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; lcc*) # Lucid # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; *) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; esac ;; solaris*) case $cc_basename in CC* | sunCC*) # Sun C++ 4.2, 5.x and Centerline C++ _LT_TAGVAR(archive_cmds_need_lc,$1)=yes _LT_TAGVAR(no_undefined_flag, $1)=' -zdefs' _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ $CC -G$allow_undefined_flag $wl-M $wl$lib.exp -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp' _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' _LT_TAGVAR(hardcode_shlibpath_var, $1)=no case $host_os in solaris2.[[0-5]] | solaris2.[[0-5]].*) ;; *) # The compiler driver will combine and reorder linker options, # but understands '-z linker_flag'. # Supported since Solaris 2.6 (maybe 2.5.1?) _LT_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract' ;; esac _LT_TAGVAR(link_all_deplibs, $1)=yes output_verbose_link_cmd='func_echo_all' # Archives containing C++ object files must be created using # "CC -xar", where "CC" is the Sun C++ compiler. This is # necessary to make sure instantiated templates are included # in the archive. _LT_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs' ;; gcx*) # Green Hills C++ Compiler _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib' # The C++ compiler must be used to create the archive. _LT_TAGVAR(old_archive_cmds, $1)='$CC $LDFLAGS -archive -o $oldlib $oldobjs' ;; *) # GNU C++ compiler with Solaris linker if test yes,no = "$GXX,$with_gnu_ld"; then _LT_TAGVAR(no_undefined_flag, $1)=' $wl-z ${wl}defs' if $CC --version | $GREP -v '^2\.7' > /dev/null; then _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ $CC -shared $pic_flag -nostdlib $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp' # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP " \-L"' else # g++ 2.7 appears to require '-G' NOT '-shared' on this # platform. _LT_TAGVAR(archive_cmds, $1)='$CC -G -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib' _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ $CC -G -nostdlib $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp' # Commands to make compiler produce verbose output that lists # what "hidden" libraries, object files and flags are used when # linking a shared library. output_verbose_link_cmd='$CC -G $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP " \-L"' fi _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $wl$libdir' case $host_os in solaris2.[[0-5]] | solaris2.[[0-5]].*) ;; *) _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract' ;; esac fi ;; esac ;; sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*) _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no runpath_var='LD_RUN_PATH' case $cc_basename in CC*) _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' ;; *) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' ;; esac ;; sysv5* | sco3.2v5* | sco5v6*) # Note: We CANNOT use -z defs as we might desire, because we do not # link with -lc, and that would cause any symbols used from libc to # always be unresolved, which means just about no library would # ever link correctly. If we're not using GNU ld we use -z text # though, which does catch some bad symbols but isn't as heavy-handed # as -z defs. _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' _LT_TAGVAR(allow_undefined_flag, $1)='$wl-z,nodefs' _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(hardcode_shlibpath_var, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R,$libdir' _LT_TAGVAR(hardcode_libdir_separator, $1)=':' _LT_TAGVAR(link_all_deplibs, $1)=yes _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Bexport' runpath_var='LD_RUN_PATH' case $cc_basename in CC*) _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(old_archive_cmds, $1)='$CC -Tprelink_objects $oldobjs~ '"$_LT_TAGVAR(old_archive_cmds, $1)" _LT_TAGVAR(reload_cmds, $1)='$CC -Tprelink_objects $reload_objs~ '"$_LT_TAGVAR(reload_cmds, $1)" ;; *) _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' ;; esac ;; tandem*) case $cc_basename in NCC*) # NonStop-UX NCC 3.20 # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; *) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; esac ;; vxworks*) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; *) # FIXME: insert proper C++ library support _LT_TAGVAR(ld_shlibs, $1)=no ;; esac AC_MSG_RESULT([$_LT_TAGVAR(ld_shlibs, $1)]) test no = "$_LT_TAGVAR(ld_shlibs, $1)" && can_build_shared=no _LT_TAGVAR(GCC, $1)=$GXX _LT_TAGVAR(LD, $1)=$LD ## CAVEAT EMPTOR: ## There is no encapsulation within the following macros, do not change ## the running order or otherwise move them around unless you know exactly ## what you are doing... _LT_SYS_HIDDEN_LIBDEPS($1) _LT_COMPILER_PIC($1) _LT_COMPILER_C_O($1) _LT_COMPILER_FILE_LOCKS($1) _LT_LINKER_SHLIBS($1) _LT_SYS_DYNAMIC_LINKER($1) _LT_LINKER_HARDCODE_LIBPATH($1) _LT_CONFIG($1) fi # test -n "$compiler" CC=$lt_save_CC CFLAGS=$lt_save_CFLAGS LDCXX=$LD LD=$lt_save_LD GCC=$lt_save_GCC with_gnu_ld=$lt_save_with_gnu_ld lt_cv_path_LDCXX=$lt_cv_path_LD lt_cv_path_LD=$lt_save_path_LD lt_cv_prog_gnu_ldcxx=$lt_cv_prog_gnu_ld lt_cv_prog_gnu_ld=$lt_save_with_gnu_ld fi # test yes != "$_lt_caught_CXX_error" AC_LANG_POP ])# _LT_LANG_CXX_CONFIG # _LT_FUNC_STRIPNAME_CNF # ---------------------- # func_stripname_cnf prefix suffix name # strip PREFIX and SUFFIX off of NAME. # PREFIX and SUFFIX must not contain globbing or regex special # characters, hashes, percent signs, but SUFFIX may contain a leading # dot (in which case that matches only a dot). # # This function is identical to the (non-XSI) version of func_stripname, # except this one can be used by m4 code that may be executed by configure, # rather than the libtool script. m4_defun([_LT_FUNC_STRIPNAME_CNF],[dnl AC_REQUIRE([_LT_DECL_SED]) AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH]) func_stripname_cnf () { case @S|@2 in .*) func_stripname_result=`$ECHO "@S|@3" | $SED "s%^@S|@1%%; s%\\\\@S|@2\$%%"`;; *) func_stripname_result=`$ECHO "@S|@3" | $SED "s%^@S|@1%%; s%@S|@2\$%%"`;; esac } # func_stripname_cnf ])# _LT_FUNC_STRIPNAME_CNF # _LT_SYS_HIDDEN_LIBDEPS([TAGNAME]) # --------------------------------- # Figure out "hidden" library dependencies from verbose # compiler output when linking a shared library. # Parse the compiler output and extract the necessary # objects, libraries and library flags. m4_defun([_LT_SYS_HIDDEN_LIBDEPS], [m4_require([_LT_FILEUTILS_DEFAULTS])dnl AC_REQUIRE([_LT_FUNC_STRIPNAME_CNF])dnl # Dependencies to place before and after the object being linked: _LT_TAGVAR(predep_objects, $1)= _LT_TAGVAR(postdep_objects, $1)= _LT_TAGVAR(predeps, $1)= _LT_TAGVAR(postdeps, $1)= _LT_TAGVAR(compiler_lib_search_path, $1)= dnl we can't use the lt_simple_compile_test_code here, dnl because it contains code intended for an executable, dnl not a library. It's possible we should let each dnl tag define a new lt_????_link_test_code variable, dnl but it's only used here... m4_if([$1], [], [cat > conftest.$ac_ext <<_LT_EOF int a; void foo (void) { a = 0; } _LT_EOF ], [$1], [CXX], [cat > conftest.$ac_ext <<_LT_EOF class Foo { public: Foo (void) { a = 0; } private: int a; }; _LT_EOF ], [$1], [F77], [cat > conftest.$ac_ext <<_LT_EOF subroutine foo implicit none integer*4 a a=0 return end _LT_EOF ], [$1], [FC], [cat > conftest.$ac_ext <<_LT_EOF subroutine foo implicit none integer a a=0 return end _LT_EOF ], [$1], [GCJ], [cat > conftest.$ac_ext <<_LT_EOF public class foo { private int a; public void bar (void) { a = 0; } }; _LT_EOF ], [$1], [GO], [cat > conftest.$ac_ext <<_LT_EOF package foo func foo() { } _LT_EOF ]) _lt_libdeps_save_CFLAGS=$CFLAGS case "$CC $CFLAGS " in #( *\ -flto*\ *) CFLAGS="$CFLAGS -fno-lto" ;; *\ -fwhopr*\ *) CFLAGS="$CFLAGS -fno-whopr" ;; *\ -fuse-linker-plugin*\ *) CFLAGS="$CFLAGS -fno-use-linker-plugin" ;; esac dnl Parse the compiler output and extract the necessary dnl objects, libraries and library flags. if AC_TRY_EVAL(ac_compile); then # Parse the compiler output and extract the necessary # objects, libraries and library flags. # Sentinel used to keep track of whether or not we are before # the conftest object file. pre_test_object_deps_done=no for p in `eval "$output_verbose_link_cmd"`; do case $prev$p in -L* | -R* | -l*) # Some compilers place space between "-{L,R}" and the path. # Remove the space. if test x-L = "$p" || test x-R = "$p"; then prev=$p continue fi # Expand the sysroot to ease extracting the directories later. if test -z "$prev"; then case $p in -L*) func_stripname_cnf '-L' '' "$p"; prev=-L; p=$func_stripname_result ;; -R*) func_stripname_cnf '-R' '' "$p"; prev=-R; p=$func_stripname_result ;; -l*) func_stripname_cnf '-l' '' "$p"; prev=-l; p=$func_stripname_result ;; esac fi case $p in =*) func_stripname_cnf '=' '' "$p"; p=$lt_sysroot$func_stripname_result ;; esac if test no = "$pre_test_object_deps_done"; then case $prev in -L | -R) # Internal compiler library paths should come after those # provided the user. The postdeps already come after the # user supplied libs so there is no need to process them. if test -z "$_LT_TAGVAR(compiler_lib_search_path, $1)"; then _LT_TAGVAR(compiler_lib_search_path, $1)=$prev$p else _LT_TAGVAR(compiler_lib_search_path, $1)="${_LT_TAGVAR(compiler_lib_search_path, $1)} $prev$p" fi ;; # The "-l" case would never come before the object being # linked, so don't bother handling this case. esac else if test -z "$_LT_TAGVAR(postdeps, $1)"; then _LT_TAGVAR(postdeps, $1)=$prev$p else _LT_TAGVAR(postdeps, $1)="${_LT_TAGVAR(postdeps, $1)} $prev$p" fi fi prev= ;; *.lto.$objext) ;; # Ignore GCC LTO objects *.$objext) # This assumes that the test object file only shows up # once in the compiler output. if test "$p" = "conftest.$objext"; then pre_test_object_deps_done=yes continue fi if test no = "$pre_test_object_deps_done"; then if test -z "$_LT_TAGVAR(predep_objects, $1)"; then _LT_TAGVAR(predep_objects, $1)=$p else _LT_TAGVAR(predep_objects, $1)="$_LT_TAGVAR(predep_objects, $1) $p" fi else if test -z "$_LT_TAGVAR(postdep_objects, $1)"; then _LT_TAGVAR(postdep_objects, $1)=$p else _LT_TAGVAR(postdep_objects, $1)="$_LT_TAGVAR(postdep_objects, $1) $p" fi fi ;; *) ;; # Ignore the rest. esac done # Clean up. rm -f a.out a.exe else echo "libtool.m4: error: problem compiling $1 test program" fi $RM -f confest.$objext CFLAGS=$_lt_libdeps_save_CFLAGS # PORTME: override above test on systems where it is broken m4_if([$1], [CXX], [case $host_os in interix[[3-9]]*) # Interix 3.5 installs completely hosed .la files for C++, so rather than # hack all around it, let's just trust "g++" to DTRT. _LT_TAGVAR(predep_objects,$1)= _LT_TAGVAR(postdep_objects,$1)= _LT_TAGVAR(postdeps,$1)= ;; esac ]) case " $_LT_TAGVAR(postdeps, $1) " in *" -lc "*) _LT_TAGVAR(archive_cmds_need_lc, $1)=no ;; esac _LT_TAGVAR(compiler_lib_search_dirs, $1)= if test -n "${_LT_TAGVAR(compiler_lib_search_path, $1)}"; then _LT_TAGVAR(compiler_lib_search_dirs, $1)=`echo " ${_LT_TAGVAR(compiler_lib_search_path, $1)}" | $SED -e 's! -L! !g' -e 's!^ !!'` fi _LT_TAGDECL([], [compiler_lib_search_dirs], [1], [The directories searched by this compiler when creating a shared library]) _LT_TAGDECL([], [predep_objects], [1], [Dependencies to place before and after the objects being linked to create a shared library]) _LT_TAGDECL([], [postdep_objects], [1]) _LT_TAGDECL([], [predeps], [1]) _LT_TAGDECL([], [postdeps], [1]) _LT_TAGDECL([], [compiler_lib_search_path], [1], [The library search path used internally by the compiler when linking a shared library]) ])# _LT_SYS_HIDDEN_LIBDEPS # _LT_LANG_F77_CONFIG([TAG]) # -------------------------- # Ensure that the configuration variables for a Fortran 77 compiler are # suitably defined. These variables are subsequently used by _LT_CONFIG # to write the compiler configuration to 'libtool'. m4_defun([_LT_LANG_F77_CONFIG], [AC_LANG_PUSH(Fortran 77) if test -z "$F77" || test no = "$F77"; then _lt_disable_F77=yes fi _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(allow_undefined_flag, $1)= _LT_TAGVAR(always_export_symbols, $1)=no _LT_TAGVAR(archive_expsym_cmds, $1)= _LT_TAGVAR(export_dynamic_flag_spec, $1)= _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= _LT_TAGVAR(hardcode_libdir_separator, $1)= _LT_TAGVAR(hardcode_minus_L, $1)=no _LT_TAGVAR(hardcode_automatic, $1)=no _LT_TAGVAR(inherit_rpath, $1)=no _LT_TAGVAR(module_cmds, $1)= _LT_TAGVAR(module_expsym_cmds, $1)= _LT_TAGVAR(link_all_deplibs, $1)=unknown _LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds _LT_TAGVAR(reload_flag, $1)=$reload_flag _LT_TAGVAR(reload_cmds, $1)=$reload_cmds _LT_TAGVAR(no_undefined_flag, $1)= _LT_TAGVAR(whole_archive_flag_spec, $1)= _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no # Source file extension for f77 test sources. ac_ext=f # Object file extension for compiled f77 test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # No sense in running all these tests if we already determined that # the F77 compiler isn't working. Some variables (like enable_shared) # are currently assumed to apply to all compilers on this platform, # and will be corrupted by setting them based on a non-working compiler. if test yes != "$_lt_disable_F77"; then # Code to be used in simple compile tests lt_simple_compile_test_code="\ subroutine t return end " # Code to be used in simple link tests lt_simple_link_test_code="\ program t end " # ltmain only uses $CC for tagged configurations so make sure $CC is set. _LT_TAG_COMPILER # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE # Allow CC to be a program name with arguments. lt_save_CC=$CC lt_save_GCC=$GCC lt_save_CFLAGS=$CFLAGS CC=${F77-"f77"} CFLAGS=$FFLAGS compiler=$CC _LT_TAGVAR(compiler, $1)=$CC _LT_CC_BASENAME([$compiler]) GCC=$G77 if test -n "$compiler"; then AC_MSG_CHECKING([if libtool supports shared libraries]) AC_MSG_RESULT([$can_build_shared]) AC_MSG_CHECKING([whether to build shared libraries]) test no = "$can_build_shared" && enable_shared=no # On AIX, shared libraries and static libraries use the same namespace, and # are all built from PIC. case $host_os in aix3*) test yes = "$enable_shared" && enable_static=no if test -n "$RANLIB"; then archive_cmds="$archive_cmds~\$RANLIB \$lib" postinstall_cmds='$RANLIB $lib' fi ;; aix[[4-9]]*) if test ia64 != "$host_cpu"; then case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in yes,aix,yes) ;; # shared object as lib.so file only yes,svr4,*) ;; # shared object as lib.so archive member only yes,*) enable_static=no ;; # shared object in lib.a archive as well esac fi ;; esac AC_MSG_RESULT([$enable_shared]) AC_MSG_CHECKING([whether to build static libraries]) # Make sure either enable_shared or enable_static is yes. test yes = "$enable_shared" || enable_static=yes AC_MSG_RESULT([$enable_static]) _LT_TAGVAR(GCC, $1)=$G77 _LT_TAGVAR(LD, $1)=$LD ## CAVEAT EMPTOR: ## There is no encapsulation within the following macros, do not change ## the running order or otherwise move them around unless you know exactly ## what you are doing... _LT_COMPILER_PIC($1) _LT_COMPILER_C_O($1) _LT_COMPILER_FILE_LOCKS($1) _LT_LINKER_SHLIBS($1) _LT_SYS_DYNAMIC_LINKER($1) _LT_LINKER_HARDCODE_LIBPATH($1) _LT_CONFIG($1) fi # test -n "$compiler" GCC=$lt_save_GCC CC=$lt_save_CC CFLAGS=$lt_save_CFLAGS fi # test yes != "$_lt_disable_F77" AC_LANG_POP ])# _LT_LANG_F77_CONFIG # _LT_LANG_FC_CONFIG([TAG]) # ------------------------- # Ensure that the configuration variables for a Fortran compiler are # suitably defined. These variables are subsequently used by _LT_CONFIG # to write the compiler configuration to 'libtool'. m4_defun([_LT_LANG_FC_CONFIG], [AC_LANG_PUSH(Fortran) if test -z "$FC" || test no = "$FC"; then _lt_disable_FC=yes fi _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(allow_undefined_flag, $1)= _LT_TAGVAR(always_export_symbols, $1)=no _LT_TAGVAR(archive_expsym_cmds, $1)= _LT_TAGVAR(export_dynamic_flag_spec, $1)= _LT_TAGVAR(hardcode_direct, $1)=no _LT_TAGVAR(hardcode_direct_absolute, $1)=no _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= _LT_TAGVAR(hardcode_libdir_separator, $1)= _LT_TAGVAR(hardcode_minus_L, $1)=no _LT_TAGVAR(hardcode_automatic, $1)=no _LT_TAGVAR(inherit_rpath, $1)=no _LT_TAGVAR(module_cmds, $1)= _LT_TAGVAR(module_expsym_cmds, $1)= _LT_TAGVAR(link_all_deplibs, $1)=unknown _LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds _LT_TAGVAR(reload_flag, $1)=$reload_flag _LT_TAGVAR(reload_cmds, $1)=$reload_cmds _LT_TAGVAR(no_undefined_flag, $1)= _LT_TAGVAR(whole_archive_flag_spec, $1)= _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no # Source file extension for fc test sources. ac_ext=${ac_fc_srcext-f} # Object file extension for compiled fc test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # No sense in running all these tests if we already determined that # the FC compiler isn't working. Some variables (like enable_shared) # are currently assumed to apply to all compilers on this platform, # and will be corrupted by setting them based on a non-working compiler. if test yes != "$_lt_disable_FC"; then # Code to be used in simple compile tests lt_simple_compile_test_code="\ subroutine t return end " # Code to be used in simple link tests lt_simple_link_test_code="\ program t end " # ltmain only uses $CC for tagged configurations so make sure $CC is set. _LT_TAG_COMPILER # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE # Allow CC to be a program name with arguments. lt_save_CC=$CC lt_save_GCC=$GCC lt_save_CFLAGS=$CFLAGS CC=${FC-"f95"} CFLAGS=$FCFLAGS compiler=$CC GCC=$ac_cv_fc_compiler_gnu _LT_TAGVAR(compiler, $1)=$CC _LT_CC_BASENAME([$compiler]) if test -n "$compiler"; then AC_MSG_CHECKING([if libtool supports shared libraries]) AC_MSG_RESULT([$can_build_shared]) AC_MSG_CHECKING([whether to build shared libraries]) test no = "$can_build_shared" && enable_shared=no # On AIX, shared libraries and static libraries use the same namespace, and # are all built from PIC. case $host_os in aix3*) test yes = "$enable_shared" && enable_static=no if test -n "$RANLIB"; then archive_cmds="$archive_cmds~\$RANLIB \$lib" postinstall_cmds='$RANLIB $lib' fi ;; aix[[4-9]]*) if test ia64 != "$host_cpu"; then case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in yes,aix,yes) ;; # shared object as lib.so file only yes,svr4,*) ;; # shared object as lib.so archive member only yes,*) enable_static=no ;; # shared object in lib.a archive as well esac fi ;; esac AC_MSG_RESULT([$enable_shared]) AC_MSG_CHECKING([whether to build static libraries]) # Make sure either enable_shared or enable_static is yes. test yes = "$enable_shared" || enable_static=yes AC_MSG_RESULT([$enable_static]) _LT_TAGVAR(GCC, $1)=$ac_cv_fc_compiler_gnu _LT_TAGVAR(LD, $1)=$LD ## CAVEAT EMPTOR: ## There is no encapsulation within the following macros, do not change ## the running order or otherwise move them around unless you know exactly ## what you are doing... _LT_SYS_HIDDEN_LIBDEPS($1) _LT_COMPILER_PIC($1) _LT_COMPILER_C_O($1) _LT_COMPILER_FILE_LOCKS($1) _LT_LINKER_SHLIBS($1) _LT_SYS_DYNAMIC_LINKER($1) _LT_LINKER_HARDCODE_LIBPATH($1) _LT_CONFIG($1) fi # test -n "$compiler" GCC=$lt_save_GCC CC=$lt_save_CC CFLAGS=$lt_save_CFLAGS fi # test yes != "$_lt_disable_FC" AC_LANG_POP ])# _LT_LANG_FC_CONFIG # _LT_LANG_GCJ_CONFIG([TAG]) # -------------------------- # Ensure that the configuration variables for the GNU Java Compiler compiler # are suitably defined. These variables are subsequently used by _LT_CONFIG # to write the compiler configuration to 'libtool'. m4_defun([_LT_LANG_GCJ_CONFIG], [AC_REQUIRE([LT_PROG_GCJ])dnl AC_LANG_SAVE # Source file extension for Java test sources. ac_ext=java # Object file extension for compiled Java test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # Code to be used in simple compile tests lt_simple_compile_test_code="class foo {}" # Code to be used in simple link tests lt_simple_link_test_code='public class conftest { public static void main(String[[]] argv) {}; }' # ltmain only uses $CC for tagged configurations so make sure $CC is set. _LT_TAG_COMPILER # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE # Allow CC to be a program name with arguments. lt_save_CC=$CC lt_save_CFLAGS=$CFLAGS lt_save_GCC=$GCC GCC=yes CC=${GCJ-"gcj"} CFLAGS=$GCJFLAGS compiler=$CC _LT_TAGVAR(compiler, $1)=$CC _LT_TAGVAR(LD, $1)=$LD _LT_CC_BASENAME([$compiler]) # GCJ did not exist at the time GCC didn't implicitly link libc in. _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds _LT_TAGVAR(reload_flag, $1)=$reload_flag _LT_TAGVAR(reload_cmds, $1)=$reload_cmds ## CAVEAT EMPTOR: ## There is no encapsulation within the following macros, do not change ## the running order or otherwise move them around unless you know exactly ## what you are doing... if test -n "$compiler"; then _LT_COMPILER_NO_RTTI($1) _LT_COMPILER_PIC($1) _LT_COMPILER_C_O($1) _LT_COMPILER_FILE_LOCKS($1) _LT_LINKER_SHLIBS($1) _LT_LINKER_HARDCODE_LIBPATH($1) _LT_CONFIG($1) fi AC_LANG_RESTORE GCC=$lt_save_GCC CC=$lt_save_CC CFLAGS=$lt_save_CFLAGS ])# _LT_LANG_GCJ_CONFIG # _LT_LANG_GO_CONFIG([TAG]) # -------------------------- # Ensure that the configuration variables for the GNU Go compiler # are suitably defined. These variables are subsequently used by _LT_CONFIG # to write the compiler configuration to 'libtool'. m4_defun([_LT_LANG_GO_CONFIG], [AC_REQUIRE([LT_PROG_GO])dnl AC_LANG_SAVE # Source file extension for Go test sources. ac_ext=go # Object file extension for compiled Go test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # Code to be used in simple compile tests lt_simple_compile_test_code="package main; func main() { }" # Code to be used in simple link tests lt_simple_link_test_code='package main; func main() { }' # ltmain only uses $CC for tagged configurations so make sure $CC is set. _LT_TAG_COMPILER # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE # Allow CC to be a program name with arguments. lt_save_CC=$CC lt_save_CFLAGS=$CFLAGS lt_save_GCC=$GCC GCC=yes CC=${GOC-"gccgo"} CFLAGS=$GOFLAGS compiler=$CC _LT_TAGVAR(compiler, $1)=$CC _LT_TAGVAR(LD, $1)=$LD _LT_CC_BASENAME([$compiler]) # Go did not exist at the time GCC didn't implicitly link libc in. _LT_TAGVAR(archive_cmds_need_lc, $1)=no _LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds _LT_TAGVAR(reload_flag, $1)=$reload_flag _LT_TAGVAR(reload_cmds, $1)=$reload_cmds ## CAVEAT EMPTOR: ## There is no encapsulation within the following macros, do not change ## the running order or otherwise move them around unless you know exactly ## what you are doing... if test -n "$compiler"; then _LT_COMPILER_NO_RTTI($1) _LT_COMPILER_PIC($1) _LT_COMPILER_C_O($1) _LT_COMPILER_FILE_LOCKS($1) _LT_LINKER_SHLIBS($1) _LT_LINKER_HARDCODE_LIBPATH($1) _LT_CONFIG($1) fi AC_LANG_RESTORE GCC=$lt_save_GCC CC=$lt_save_CC CFLAGS=$lt_save_CFLAGS ])# _LT_LANG_GO_CONFIG # _LT_LANG_RC_CONFIG([TAG]) # ------------------------- # Ensure that the configuration variables for the Windows resource compiler # are suitably defined. These variables are subsequently used by _LT_CONFIG # to write the compiler configuration to 'libtool'. m4_defun([_LT_LANG_RC_CONFIG], [AC_REQUIRE([LT_PROG_RC])dnl AC_LANG_SAVE # Source file extension for RC test sources. ac_ext=rc # Object file extension for compiled RC test sources. objext=o _LT_TAGVAR(objext, $1)=$objext # Code to be used in simple compile tests lt_simple_compile_test_code='sample MENU { MENUITEM "&Soup", 100, CHECKED }' # Code to be used in simple link tests lt_simple_link_test_code=$lt_simple_compile_test_code # ltmain only uses $CC for tagged configurations so make sure $CC is set. _LT_TAG_COMPILER # save warnings/boilerplate of simple test code _LT_COMPILER_BOILERPLATE _LT_LINKER_BOILERPLATE # Allow CC to be a program name with arguments. lt_save_CC=$CC lt_save_CFLAGS=$CFLAGS lt_save_GCC=$GCC GCC= CC=${RC-"windres"} CFLAGS= compiler=$CC _LT_TAGVAR(compiler, $1)=$CC _LT_CC_BASENAME([$compiler]) _LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes if test -n "$compiler"; then : _LT_CONFIG($1) fi GCC=$lt_save_GCC AC_LANG_RESTORE CC=$lt_save_CC CFLAGS=$lt_save_CFLAGS ])# _LT_LANG_RC_CONFIG # LT_PROG_GCJ # ----------- AC_DEFUN([LT_PROG_GCJ], [m4_ifdef([AC_PROG_GCJ], [AC_PROG_GCJ], [m4_ifdef([A][M_PROG_GCJ], [A][M_PROG_GCJ], [AC_CHECK_TOOL(GCJ, gcj,) test set = "${GCJFLAGS+set}" || GCJFLAGS="-g -O2" AC_SUBST(GCJFLAGS)])])[]dnl ]) # Old name: AU_ALIAS([LT_AC_PROG_GCJ], [LT_PROG_GCJ]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([LT_AC_PROG_GCJ], []) # LT_PROG_GO # ---------- AC_DEFUN([LT_PROG_GO], [AC_CHECK_TOOL(GOC, gccgo,) ]) # LT_PROG_RC # ---------- AC_DEFUN([LT_PROG_RC], [AC_CHECK_TOOL(RC, windres,) ]) # Old name: AU_ALIAS([LT_AC_PROG_RC], [LT_PROG_RC]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([LT_AC_PROG_RC], []) # _LT_DECL_EGREP # -------------- # If we don't have a new enough Autoconf to choose the best grep # available, choose the one first in the user's PATH. m4_defun([_LT_DECL_EGREP], [AC_REQUIRE([AC_PROG_EGREP])dnl AC_REQUIRE([AC_PROG_FGREP])dnl test -z "$GREP" && GREP=grep _LT_DECL([], [GREP], [1], [A grep program that handles long lines]) _LT_DECL([], [EGREP], [1], [An ERE matcher]) _LT_DECL([], [FGREP], [1], [A literal string matcher]) dnl Non-bleeding-edge autoconf doesn't subst GREP, so do it here too AC_SUBST([GREP]) ]) # _LT_DECL_OBJDUMP # -------------- # If we don't have a new enough Autoconf to choose the best objdump # available, choose the one first in the user's PATH. m4_defun([_LT_DECL_OBJDUMP], [AC_CHECK_TOOL(OBJDUMP, objdump, false) test -z "$OBJDUMP" && OBJDUMP=objdump _LT_DECL([], [OBJDUMP], [1], [An object symbol dumper]) AC_SUBST([OBJDUMP]) ]) # _LT_DECL_DLLTOOL # ---------------- # Ensure DLLTOOL variable is set. m4_defun([_LT_DECL_DLLTOOL], [AC_CHECK_TOOL(DLLTOOL, dlltool, false) test -z "$DLLTOOL" && DLLTOOL=dlltool _LT_DECL([], [DLLTOOL], [1], [DLL creation program]) AC_SUBST([DLLTOOL]) ]) # _LT_DECL_SED # ------------ # Check for a fully-functional sed program, that truncates # as few characters as possible. Prefer GNU sed if found. m4_defun([_LT_DECL_SED], [AC_PROG_SED test -z "$SED" && SED=sed Xsed="$SED -e 1s/^X//" _LT_DECL([], [SED], [1], [A sed program that does not truncate output]) _LT_DECL([], [Xsed], ["\$SED -e 1s/^X//"], [Sed that helps us avoid accidentally triggering echo(1) options like -n]) ])# _LT_DECL_SED m4_ifndef([AC_PROG_SED], [ ############################################################ # NOTE: This macro has been submitted for inclusion into # # GNU Autoconf as AC_PROG_SED. When it is available in # # a released version of Autoconf we should remove this # # macro and use it instead. # ############################################################ m4_defun([AC_PROG_SED], [AC_MSG_CHECKING([for a sed that does not truncate output]) AC_CACHE_VAL(lt_cv_path_SED, [# Loop through the user's path and test for sed and gsed. # Then use that list of sed's as ones to test for truncation. as_save_IFS=$IFS; IFS=$PATH_SEPARATOR for as_dir in $PATH do IFS=$as_save_IFS test -z "$as_dir" && as_dir=. for lt_ac_prog in sed gsed; do for ac_exec_ext in '' $ac_executable_extensions; do if $as_executable_p "$as_dir/$lt_ac_prog$ac_exec_ext"; then lt_ac_sed_list="$lt_ac_sed_list $as_dir/$lt_ac_prog$ac_exec_ext" fi done done done IFS=$as_save_IFS lt_ac_max=0 lt_ac_count=0 # Add /usr/xpg4/bin/sed as it is typically found on Solaris # along with /bin/sed that truncates output. for lt_ac_sed in $lt_ac_sed_list /usr/xpg4/bin/sed; do test ! -f "$lt_ac_sed" && continue cat /dev/null > conftest.in lt_ac_count=0 echo $ECHO_N "0123456789$ECHO_C" >conftest.in # Check for GNU sed and select it if it is found. if "$lt_ac_sed" --version 2>&1 < /dev/null | grep 'GNU' > /dev/null; then lt_cv_path_SED=$lt_ac_sed break fi while true; do cat conftest.in conftest.in >conftest.tmp mv conftest.tmp conftest.in cp conftest.in conftest.nl echo >>conftest.nl $lt_ac_sed -e 's/a$//' < conftest.nl >conftest.out || break cmp -s conftest.out conftest.nl || break # 10000 chars as input seems more than enough test 10 -lt "$lt_ac_count" && break lt_ac_count=`expr $lt_ac_count + 1` if test "$lt_ac_count" -gt "$lt_ac_max"; then lt_ac_max=$lt_ac_count lt_cv_path_SED=$lt_ac_sed fi done done ]) SED=$lt_cv_path_SED AC_SUBST([SED]) AC_MSG_RESULT([$SED]) ])#AC_PROG_SED ])#m4_ifndef # Old name: AU_ALIAS([LT_AC_PROG_SED], [AC_PROG_SED]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([LT_AC_PROG_SED], []) # _LT_CHECK_SHELL_FEATURES # ------------------------ # Find out whether the shell is Bourne or XSI compatible, # or has some other useful features. m4_defun([_LT_CHECK_SHELL_FEATURES], [if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then lt_unset=unset else lt_unset=false fi _LT_DECL([], [lt_unset], [0], [whether the shell understands "unset"])dnl # test EBCDIC or ASCII case `echo X|tr X '\101'` in A) # ASCII based system # \n is not interpreted correctly by Solaris 8 /usr/ucb/tr lt_SP2NL='tr \040 \012' lt_NL2SP='tr \015\012 \040\040' ;; *) # EBCDIC based system lt_SP2NL='tr \100 \n' lt_NL2SP='tr \r\n \100\100' ;; esac _LT_DECL([SP2NL], [lt_SP2NL], [1], [turn spaces into newlines])dnl _LT_DECL([NL2SP], [lt_NL2SP], [1], [turn newlines into spaces])dnl ])# _LT_CHECK_SHELL_FEATURES # _LT_PATH_CONVERSION_FUNCTIONS # ----------------------------- # Determine what file name conversion functions should be used by # func_to_host_file (and, implicitly, by func_to_host_path). These are needed # for certain cross-compile configurations and native mingw. m4_defun([_LT_PATH_CONVERSION_FUNCTIONS], [AC_REQUIRE([AC_CANONICAL_HOST])dnl AC_REQUIRE([AC_CANONICAL_BUILD])dnl AC_MSG_CHECKING([how to convert $build file names to $host format]) AC_CACHE_VAL(lt_cv_to_host_file_cmd, [case $host in *-*-mingw* ) case $build in *-*-mingw* ) # actually msys lt_cv_to_host_file_cmd=func_convert_file_msys_to_w32 ;; *-*-cygwin* ) lt_cv_to_host_file_cmd=func_convert_file_cygwin_to_w32 ;; * ) # otherwise, assume *nix lt_cv_to_host_file_cmd=func_convert_file_nix_to_w32 ;; esac ;; *-*-cygwin* ) case $build in *-*-mingw* ) # actually msys lt_cv_to_host_file_cmd=func_convert_file_msys_to_cygwin ;; *-*-cygwin* ) lt_cv_to_host_file_cmd=func_convert_file_noop ;; * ) # otherwise, assume *nix lt_cv_to_host_file_cmd=func_convert_file_nix_to_cygwin ;; esac ;; * ) # unhandled hosts (and "normal" native builds) lt_cv_to_host_file_cmd=func_convert_file_noop ;; esac ]) to_host_file_cmd=$lt_cv_to_host_file_cmd AC_MSG_RESULT([$lt_cv_to_host_file_cmd]) _LT_DECL([to_host_file_cmd], [lt_cv_to_host_file_cmd], [0], [convert $build file names to $host format])dnl AC_MSG_CHECKING([how to convert $build file names to toolchain format]) AC_CACHE_VAL(lt_cv_to_tool_file_cmd, [#assume ordinary cross tools, or native build. lt_cv_to_tool_file_cmd=func_convert_file_noop case $host in *-*-mingw* ) case $build in *-*-mingw* ) # actually msys lt_cv_to_tool_file_cmd=func_convert_file_msys_to_w32 ;; esac ;; esac ]) to_tool_file_cmd=$lt_cv_to_tool_file_cmd AC_MSG_RESULT([$lt_cv_to_tool_file_cmd]) _LT_DECL([to_tool_file_cmd], [lt_cv_to_tool_file_cmd], [0], [convert $build files to toolchain format])dnl ])# _LT_PATH_CONVERSION_FUNCTIONS capnproto-c++-0.8.0/m4/ltoptions.m40000644000175000017500000003426213650317751017604 0ustar00kentonkenton00000000000000# Helper functions for option handling. -*- Autoconf -*- # # Copyright (C) 2004-2005, 2007-2009, 2011-2015 Free Software # Foundation, Inc. # Written by Gary V. Vaughan, 2004 # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # serial 8 ltoptions.m4 # This is to help aclocal find these macros, as it can't see m4_define. AC_DEFUN([LTOPTIONS_VERSION], [m4_if([1])]) # _LT_MANGLE_OPTION(MACRO-NAME, OPTION-NAME) # ------------------------------------------ m4_define([_LT_MANGLE_OPTION], [[_LT_OPTION_]m4_bpatsubst($1__$2, [[^a-zA-Z0-9_]], [_])]) # _LT_SET_OPTION(MACRO-NAME, OPTION-NAME) # --------------------------------------- # Set option OPTION-NAME for macro MACRO-NAME, and if there is a # matching handler defined, dispatch to it. Other OPTION-NAMEs are # saved as a flag. m4_define([_LT_SET_OPTION], [m4_define(_LT_MANGLE_OPTION([$1], [$2]))dnl m4_ifdef(_LT_MANGLE_DEFUN([$1], [$2]), _LT_MANGLE_DEFUN([$1], [$2]), [m4_warning([Unknown $1 option '$2'])])[]dnl ]) # _LT_IF_OPTION(MACRO-NAME, OPTION-NAME, IF-SET, [IF-NOT-SET]) # ------------------------------------------------------------ # Execute IF-SET if OPTION is set, IF-NOT-SET otherwise. m4_define([_LT_IF_OPTION], [m4_ifdef(_LT_MANGLE_OPTION([$1], [$2]), [$3], [$4])]) # _LT_UNLESS_OPTIONS(MACRO-NAME, OPTION-LIST, IF-NOT-SET) # ------------------------------------------------------- # Execute IF-NOT-SET unless all options in OPTION-LIST for MACRO-NAME # are set. m4_define([_LT_UNLESS_OPTIONS], [m4_foreach([_LT_Option], m4_split(m4_normalize([$2])), [m4_ifdef(_LT_MANGLE_OPTION([$1], _LT_Option), [m4_define([$0_found])])])[]dnl m4_ifdef([$0_found], [m4_undefine([$0_found])], [$3 ])[]dnl ]) # _LT_SET_OPTIONS(MACRO-NAME, OPTION-LIST) # ---------------------------------------- # OPTION-LIST is a space-separated list of Libtool options associated # with MACRO-NAME. If any OPTION has a matching handler declared with # LT_OPTION_DEFINE, dispatch to that macro; otherwise complain about # the unknown option and exit. m4_defun([_LT_SET_OPTIONS], [# Set options m4_foreach([_LT_Option], m4_split(m4_normalize([$2])), [_LT_SET_OPTION([$1], _LT_Option)]) m4_if([$1],[LT_INIT],[ dnl dnl Simply set some default values (i.e off) if boolean options were not dnl specified: _LT_UNLESS_OPTIONS([LT_INIT], [dlopen], [enable_dlopen=no ]) _LT_UNLESS_OPTIONS([LT_INIT], [win32-dll], [enable_win32_dll=no ]) dnl dnl If no reference was made to various pairs of opposing options, then dnl we run the default mode handler for the pair. For example, if neither dnl 'shared' nor 'disable-shared' was passed, we enable building of shared dnl archives by default: _LT_UNLESS_OPTIONS([LT_INIT], [shared disable-shared], [_LT_ENABLE_SHARED]) _LT_UNLESS_OPTIONS([LT_INIT], [static disable-static], [_LT_ENABLE_STATIC]) _LT_UNLESS_OPTIONS([LT_INIT], [pic-only no-pic], [_LT_WITH_PIC]) _LT_UNLESS_OPTIONS([LT_INIT], [fast-install disable-fast-install], [_LT_ENABLE_FAST_INSTALL]) _LT_UNLESS_OPTIONS([LT_INIT], [aix-soname=aix aix-soname=both aix-soname=svr4], [_LT_WITH_AIX_SONAME([aix])]) ]) ])# _LT_SET_OPTIONS ## --------------------------------- ## ## Macros to handle LT_INIT options. ## ## --------------------------------- ## # _LT_MANGLE_DEFUN(MACRO-NAME, OPTION-NAME) # ----------------------------------------- m4_define([_LT_MANGLE_DEFUN], [[_LT_OPTION_DEFUN_]m4_bpatsubst(m4_toupper([$1__$2]), [[^A-Z0-9_]], [_])]) # LT_OPTION_DEFINE(MACRO-NAME, OPTION-NAME, CODE) # ----------------------------------------------- m4_define([LT_OPTION_DEFINE], [m4_define(_LT_MANGLE_DEFUN([$1], [$2]), [$3])[]dnl ])# LT_OPTION_DEFINE # dlopen # ------ LT_OPTION_DEFINE([LT_INIT], [dlopen], [enable_dlopen=yes ]) AU_DEFUN([AC_LIBTOOL_DLOPEN], [_LT_SET_OPTION([LT_INIT], [dlopen]) AC_DIAGNOSE([obsolete], [$0: Remove this warning and the call to _LT_SET_OPTION when you put the 'dlopen' option into LT_INIT's first parameter.]) ]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_DLOPEN], []) # win32-dll # --------- # Declare package support for building win32 dll's. LT_OPTION_DEFINE([LT_INIT], [win32-dll], [enable_win32_dll=yes case $host in *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-cegcc*) AC_CHECK_TOOL(AS, as, false) AC_CHECK_TOOL(DLLTOOL, dlltool, false) AC_CHECK_TOOL(OBJDUMP, objdump, false) ;; esac test -z "$AS" && AS=as _LT_DECL([], [AS], [1], [Assembler program])dnl test -z "$DLLTOOL" && DLLTOOL=dlltool _LT_DECL([], [DLLTOOL], [1], [DLL creation program])dnl test -z "$OBJDUMP" && OBJDUMP=objdump _LT_DECL([], [OBJDUMP], [1], [Object dumper program])dnl ])# win32-dll AU_DEFUN([AC_LIBTOOL_WIN32_DLL], [AC_REQUIRE([AC_CANONICAL_HOST])dnl _LT_SET_OPTION([LT_INIT], [win32-dll]) AC_DIAGNOSE([obsolete], [$0: Remove this warning and the call to _LT_SET_OPTION when you put the 'win32-dll' option into LT_INIT's first parameter.]) ]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_WIN32_DLL], []) # _LT_ENABLE_SHARED([DEFAULT]) # ---------------------------- # implement the --enable-shared flag, and supports the 'shared' and # 'disable-shared' LT_INIT options. # DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. m4_define([_LT_ENABLE_SHARED], [m4_define([_LT_ENABLE_SHARED_DEFAULT], [m4_if($1, no, no, yes)])dnl AC_ARG_ENABLE([shared], [AS_HELP_STRING([--enable-shared@<:@=PKGS@:>@], [build shared libraries @<:@default=]_LT_ENABLE_SHARED_DEFAULT[@:>@])], [p=${PACKAGE-default} case $enableval in yes) enable_shared=yes ;; no) enable_shared=no ;; *) enable_shared=no # Look at the argument we got. We use all the common list separators. lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, for pkg in $enableval; do IFS=$lt_save_ifs if test "X$pkg" = "X$p"; then enable_shared=yes fi done IFS=$lt_save_ifs ;; esac], [enable_shared=]_LT_ENABLE_SHARED_DEFAULT) _LT_DECL([build_libtool_libs], [enable_shared], [0], [Whether or not to build shared libraries]) ])# _LT_ENABLE_SHARED LT_OPTION_DEFINE([LT_INIT], [shared], [_LT_ENABLE_SHARED([yes])]) LT_OPTION_DEFINE([LT_INIT], [disable-shared], [_LT_ENABLE_SHARED([no])]) # Old names: AC_DEFUN([AC_ENABLE_SHARED], [_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[shared]) ]) AC_DEFUN([AC_DISABLE_SHARED], [_LT_SET_OPTION([LT_INIT], [disable-shared]) ]) AU_DEFUN([AM_ENABLE_SHARED], [AC_ENABLE_SHARED($@)]) AU_DEFUN([AM_DISABLE_SHARED], [AC_DISABLE_SHARED($@)]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AM_ENABLE_SHARED], []) dnl AC_DEFUN([AM_DISABLE_SHARED], []) # _LT_ENABLE_STATIC([DEFAULT]) # ---------------------------- # implement the --enable-static flag, and support the 'static' and # 'disable-static' LT_INIT options. # DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. m4_define([_LT_ENABLE_STATIC], [m4_define([_LT_ENABLE_STATIC_DEFAULT], [m4_if($1, no, no, yes)])dnl AC_ARG_ENABLE([static], [AS_HELP_STRING([--enable-static@<:@=PKGS@:>@], [build static libraries @<:@default=]_LT_ENABLE_STATIC_DEFAULT[@:>@])], [p=${PACKAGE-default} case $enableval in yes) enable_static=yes ;; no) enable_static=no ;; *) enable_static=no # Look at the argument we got. We use all the common list separators. lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, for pkg in $enableval; do IFS=$lt_save_ifs if test "X$pkg" = "X$p"; then enable_static=yes fi done IFS=$lt_save_ifs ;; esac], [enable_static=]_LT_ENABLE_STATIC_DEFAULT) _LT_DECL([build_old_libs], [enable_static], [0], [Whether or not to build static libraries]) ])# _LT_ENABLE_STATIC LT_OPTION_DEFINE([LT_INIT], [static], [_LT_ENABLE_STATIC([yes])]) LT_OPTION_DEFINE([LT_INIT], [disable-static], [_LT_ENABLE_STATIC([no])]) # Old names: AC_DEFUN([AC_ENABLE_STATIC], [_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[static]) ]) AC_DEFUN([AC_DISABLE_STATIC], [_LT_SET_OPTION([LT_INIT], [disable-static]) ]) AU_DEFUN([AM_ENABLE_STATIC], [AC_ENABLE_STATIC($@)]) AU_DEFUN([AM_DISABLE_STATIC], [AC_DISABLE_STATIC($@)]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AM_ENABLE_STATIC], []) dnl AC_DEFUN([AM_DISABLE_STATIC], []) # _LT_ENABLE_FAST_INSTALL([DEFAULT]) # ---------------------------------- # implement the --enable-fast-install flag, and support the 'fast-install' # and 'disable-fast-install' LT_INIT options. # DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. m4_define([_LT_ENABLE_FAST_INSTALL], [m4_define([_LT_ENABLE_FAST_INSTALL_DEFAULT], [m4_if($1, no, no, yes)])dnl AC_ARG_ENABLE([fast-install], [AS_HELP_STRING([--enable-fast-install@<:@=PKGS@:>@], [optimize for fast installation @<:@default=]_LT_ENABLE_FAST_INSTALL_DEFAULT[@:>@])], [p=${PACKAGE-default} case $enableval in yes) enable_fast_install=yes ;; no) enable_fast_install=no ;; *) enable_fast_install=no # Look at the argument we got. We use all the common list separators. lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, for pkg in $enableval; do IFS=$lt_save_ifs if test "X$pkg" = "X$p"; then enable_fast_install=yes fi done IFS=$lt_save_ifs ;; esac], [enable_fast_install=]_LT_ENABLE_FAST_INSTALL_DEFAULT) _LT_DECL([fast_install], [enable_fast_install], [0], [Whether or not to optimize for fast installation])dnl ])# _LT_ENABLE_FAST_INSTALL LT_OPTION_DEFINE([LT_INIT], [fast-install], [_LT_ENABLE_FAST_INSTALL([yes])]) LT_OPTION_DEFINE([LT_INIT], [disable-fast-install], [_LT_ENABLE_FAST_INSTALL([no])]) # Old names: AU_DEFUN([AC_ENABLE_FAST_INSTALL], [_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[fast-install]) AC_DIAGNOSE([obsolete], [$0: Remove this warning and the call to _LT_SET_OPTION when you put the 'fast-install' option into LT_INIT's first parameter.]) ]) AU_DEFUN([AC_DISABLE_FAST_INSTALL], [_LT_SET_OPTION([LT_INIT], [disable-fast-install]) AC_DIAGNOSE([obsolete], [$0: Remove this warning and the call to _LT_SET_OPTION when you put the 'disable-fast-install' option into LT_INIT's first parameter.]) ]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_ENABLE_FAST_INSTALL], []) dnl AC_DEFUN([AM_DISABLE_FAST_INSTALL], []) # _LT_WITH_AIX_SONAME([DEFAULT]) # ---------------------------------- # implement the --with-aix-soname flag, and support the `aix-soname=aix' # and `aix-soname=both' and `aix-soname=svr4' LT_INIT options. DEFAULT # is either `aix', `both' or `svr4'. If omitted, it defaults to `aix'. m4_define([_LT_WITH_AIX_SONAME], [m4_define([_LT_WITH_AIX_SONAME_DEFAULT], [m4_if($1, svr4, svr4, m4_if($1, both, both, aix))])dnl shared_archive_member_spec= case $host,$enable_shared in power*-*-aix[[5-9]]*,yes) AC_MSG_CHECKING([which variant of shared library versioning to provide]) AC_ARG_WITH([aix-soname], [AS_HELP_STRING([--with-aix-soname=aix|svr4|both], [shared library versioning (aka "SONAME") variant to provide on AIX, @<:@default=]_LT_WITH_AIX_SONAME_DEFAULT[@:>@.])], [case $withval in aix|svr4|both) ;; *) AC_MSG_ERROR([Unknown argument to --with-aix-soname]) ;; esac lt_cv_with_aix_soname=$with_aix_soname], [AC_CACHE_VAL([lt_cv_with_aix_soname], [lt_cv_with_aix_soname=]_LT_WITH_AIX_SONAME_DEFAULT) with_aix_soname=$lt_cv_with_aix_soname]) AC_MSG_RESULT([$with_aix_soname]) if test aix != "$with_aix_soname"; then # For the AIX way of multilib, we name the shared archive member # based on the bitwidth used, traditionally 'shr.o' or 'shr_64.o', # and 'shr.imp' or 'shr_64.imp', respectively, for the Import File. # Even when GNU compilers ignore OBJECT_MODE but need '-maix64' flag, # the AIX toolchain works better with OBJECT_MODE set (default 32). if test 64 = "${OBJECT_MODE-32}"; then shared_archive_member_spec=shr_64 else shared_archive_member_spec=shr fi fi ;; *) with_aix_soname=aix ;; esac _LT_DECL([], [shared_archive_member_spec], [0], [Shared archive member basename, for filename based shared library versioning on AIX])dnl ])# _LT_WITH_AIX_SONAME LT_OPTION_DEFINE([LT_INIT], [aix-soname=aix], [_LT_WITH_AIX_SONAME([aix])]) LT_OPTION_DEFINE([LT_INIT], [aix-soname=both], [_LT_WITH_AIX_SONAME([both])]) LT_OPTION_DEFINE([LT_INIT], [aix-soname=svr4], [_LT_WITH_AIX_SONAME([svr4])]) # _LT_WITH_PIC([MODE]) # -------------------- # implement the --with-pic flag, and support the 'pic-only' and 'no-pic' # LT_INIT options. # MODE is either 'yes' or 'no'. If omitted, it defaults to 'both'. m4_define([_LT_WITH_PIC], [AC_ARG_WITH([pic], [AS_HELP_STRING([--with-pic@<:@=PKGS@:>@], [try to use only PIC/non-PIC objects @<:@default=use both@:>@])], [lt_p=${PACKAGE-default} case $withval in yes|no) pic_mode=$withval ;; *) pic_mode=default # Look at the argument we got. We use all the common list separators. lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, for lt_pkg in $withval; do IFS=$lt_save_ifs if test "X$lt_pkg" = "X$lt_p"; then pic_mode=yes fi done IFS=$lt_save_ifs ;; esac], [pic_mode=m4_default([$1], [default])]) _LT_DECL([], [pic_mode], [0], [What type of objects to build])dnl ])# _LT_WITH_PIC LT_OPTION_DEFINE([LT_INIT], [pic-only], [_LT_WITH_PIC([yes])]) LT_OPTION_DEFINE([LT_INIT], [no-pic], [_LT_WITH_PIC([no])]) # Old name: AU_DEFUN([AC_LIBTOOL_PICMODE], [_LT_SET_OPTION([LT_INIT], [pic-only]) AC_DIAGNOSE([obsolete], [$0: Remove this warning and the call to _LT_SET_OPTION when you put the 'pic-only' option into LT_INIT's first parameter.]) ]) dnl aclocal-1.4 backwards compatibility: dnl AC_DEFUN([AC_LIBTOOL_PICMODE], []) ## ----------------- ## ## LTDL_INIT Options ## ## ----------------- ## m4_define([_LTDL_MODE], []) LT_OPTION_DEFINE([LTDL_INIT], [nonrecursive], [m4_define([_LTDL_MODE], [nonrecursive])]) LT_OPTION_DEFINE([LTDL_INIT], [recursive], [m4_define([_LTDL_MODE], [recursive])]) LT_OPTION_DEFINE([LTDL_INIT], [subproject], [m4_define([_LTDL_MODE], [subproject])]) m4_define([_LTDL_TYPE], []) LT_OPTION_DEFINE([LTDL_INIT], [installable], [m4_define([_LTDL_TYPE], [installable])]) LT_OPTION_DEFINE([LTDL_INIT], [convenience], [m4_define([_LTDL_TYPE], [convenience])]) capnproto-c++-0.8.0/m4/ltsugar.m40000644000175000017500000001044013650317751017222 0ustar00kentonkenton00000000000000# ltsugar.m4 -- libtool m4 base layer. -*-Autoconf-*- # # Copyright (C) 2004-2005, 2007-2008, 2011-2015 Free Software # Foundation, Inc. # Written by Gary V. Vaughan, 2004 # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # serial 6 ltsugar.m4 # This is to help aclocal find these macros, as it can't see m4_define. AC_DEFUN([LTSUGAR_VERSION], [m4_if([0.1])]) # lt_join(SEP, ARG1, [ARG2...]) # ----------------------------- # Produce ARG1SEPARG2...SEPARGn, omitting [] arguments and their # associated separator. # Needed until we can rely on m4_join from Autoconf 2.62, since all earlier # versions in m4sugar had bugs. m4_define([lt_join], [m4_if([$#], [1], [], [$#], [2], [[$2]], [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift(m4_shift($@)))])]) m4_define([_lt_join], [m4_if([$#$2], [2], [], [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift(m4_shift($@)))])]) # lt_car(LIST) # lt_cdr(LIST) # ------------ # Manipulate m4 lists. # These macros are necessary as long as will still need to support # Autoconf-2.59, which quotes differently. m4_define([lt_car], [[$1]]) m4_define([lt_cdr], [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])], [$#], 1, [], [m4_dquote(m4_shift($@))])]) m4_define([lt_unquote], $1) # lt_append(MACRO-NAME, STRING, [SEPARATOR]) # ------------------------------------------ # Redefine MACRO-NAME to hold its former content plus 'SEPARATOR''STRING'. # Note that neither SEPARATOR nor STRING are expanded; they are appended # to MACRO-NAME as is (leaving the expansion for when MACRO-NAME is invoked). # No SEPARATOR is output if MACRO-NAME was previously undefined (different # than defined and empty). # # This macro is needed until we can rely on Autoconf 2.62, since earlier # versions of m4sugar mistakenly expanded SEPARATOR but not STRING. m4_define([lt_append], [m4_define([$1], m4_ifdef([$1], [m4_defn([$1])[$3]])[$2])]) # lt_combine(SEP, PREFIX-LIST, INFIX, SUFFIX1, [SUFFIX2...]) # ---------------------------------------------------------- # Produce a SEP delimited list of all paired combinations of elements of # PREFIX-LIST with SUFFIX1 through SUFFIXn. Each element of the list # has the form PREFIXmINFIXSUFFIXn. # Needed until we can rely on m4_combine added in Autoconf 2.62. m4_define([lt_combine], [m4_if(m4_eval([$# > 3]), [1], [m4_pushdef([_Lt_sep], [m4_define([_Lt_sep], m4_defn([lt_car]))])]]dnl [[m4_foreach([_Lt_prefix], [$2], [m4_foreach([_Lt_suffix], ]m4_dquote(m4_dquote(m4_shift(m4_shift(m4_shift($@)))))[, [_Lt_sep([$1])[]m4_defn([_Lt_prefix])[$3]m4_defn([_Lt_suffix])])])])]) # lt_if_append_uniq(MACRO-NAME, VARNAME, [SEPARATOR], [UNIQ], [NOT-UNIQ]) # ----------------------------------------------------------------------- # Iff MACRO-NAME does not yet contain VARNAME, then append it (delimited # by SEPARATOR if supplied) and expand UNIQ, else NOT-UNIQ. m4_define([lt_if_append_uniq], [m4_ifdef([$1], [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1], [lt_append([$1], [$2], [$3])$4], [$5])], [lt_append([$1], [$2], [$3])$4])]) # lt_dict_add(DICT, KEY, VALUE) # ----------------------------- m4_define([lt_dict_add], [m4_define([$1($2)], [$3])]) # lt_dict_add_subkey(DICT, KEY, SUBKEY, VALUE) # -------------------------------------------- m4_define([lt_dict_add_subkey], [m4_define([$1($2:$3)], [$4])]) # lt_dict_fetch(DICT, KEY, [SUBKEY]) # ---------------------------------- m4_define([lt_dict_fetch], [m4_ifval([$3], m4_ifdef([$1($2:$3)], [m4_defn([$1($2:$3)])]), m4_ifdef([$1($2)], [m4_defn([$1($2)])]))]) # lt_if_dict_fetch(DICT, KEY, [SUBKEY], VALUE, IF-TRUE, [IF-FALSE]) # ----------------------------------------------------------------- m4_define([lt_if_dict_fetch], [m4_if(lt_dict_fetch([$1], [$2], [$3]), [$4], [$5], [$6])]) # lt_dict_filter(DICT, [SUBKEY], VALUE, [SEPARATOR], KEY, [...]) # -------------------------------------------------------------- m4_define([lt_dict_filter], [m4_if([$5], [], [], [lt_join(m4_quote(m4_default([$4], [[, ]])), lt_unquote(m4_split(m4_normalize(m4_foreach(_Lt_key, lt_car([m4_shiftn(4, $@)]), [lt_if_dict_fetch([$1], _Lt_key, [$2], [$3], [_Lt_key ])])))))])[]dnl ]) capnproto-c++-0.8.0/m4/ltversion.m40000644000175000017500000000127313650317751017572 0ustar00kentonkenton00000000000000# ltversion.m4 -- version numbers -*- Autoconf -*- # # Copyright (C) 2004, 2011-2015 Free Software Foundation, Inc. # Written by Scott James Remnant, 2004 # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # @configure_input@ # serial 4179 ltversion.m4 # This file is part of GNU Libtool m4_define([LT_PACKAGE_VERSION], [2.4.6]) m4_define([LT_PACKAGE_REVISION], [2.4.6]) AC_DEFUN([LTVERSION_VERSION], [macro_version='2.4.6' macro_revision='2.4.6' _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) _LT_DECL(, macro_revision, 0) ]) capnproto-c++-0.8.0/m4/lt~obsolete.m40000644000175000017500000001377413650317751020130 0ustar00kentonkenton00000000000000# lt~obsolete.m4 -- aclocal satisfying obsolete definitions. -*-Autoconf-*- # # Copyright (C) 2004-2005, 2007, 2009, 2011-2015 Free Software # Foundation, Inc. # Written by Scott James Remnant, 2004. # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # serial 5 lt~obsolete.m4 # These exist entirely to fool aclocal when bootstrapping libtool. # # In the past libtool.m4 has provided macros via AC_DEFUN (or AU_DEFUN), # which have later been changed to m4_define as they aren't part of the # exported API, or moved to Autoconf or Automake where they belong. # # The trouble is, aclocal is a bit thick. It'll see the old AC_DEFUN # in /usr/share/aclocal/libtool.m4 and remember it, then when it sees us # using a macro with the same name in our local m4/libtool.m4 it'll # pull the old libtool.m4 in (it doesn't see our shiny new m4_define # and doesn't know about Autoconf macros at all.) # # So we provide this file, which has a silly filename so it's always # included after everything else. This provides aclocal with the # AC_DEFUNs it wants, but when m4 processes it, it doesn't do anything # because those macros already exist, or will be overwritten later. # We use AC_DEFUN over AU_DEFUN for compatibility with aclocal-1.6. # # Anytime we withdraw an AC_DEFUN or AU_DEFUN, remember to add it here. # Yes, that means every name once taken will need to remain here until # we give up compatibility with versions before 1.7, at which point # we need to keep only those names which we still refer to. # This is to help aclocal find these macros, as it can't see m4_define. AC_DEFUN([LTOBSOLETE_VERSION], [m4_if([1])]) m4_ifndef([AC_LIBTOOL_LINKER_OPTION], [AC_DEFUN([AC_LIBTOOL_LINKER_OPTION])]) m4_ifndef([AC_PROG_EGREP], [AC_DEFUN([AC_PROG_EGREP])]) m4_ifndef([_LT_AC_PROG_ECHO_BACKSLASH], [AC_DEFUN([_LT_AC_PROG_ECHO_BACKSLASH])]) m4_ifndef([_LT_AC_SHELL_INIT], [AC_DEFUN([_LT_AC_SHELL_INIT])]) m4_ifndef([_LT_AC_SYS_LIBPATH_AIX], [AC_DEFUN([_LT_AC_SYS_LIBPATH_AIX])]) m4_ifndef([_LT_PROG_LTMAIN], [AC_DEFUN([_LT_PROG_LTMAIN])]) m4_ifndef([_LT_AC_TAGVAR], [AC_DEFUN([_LT_AC_TAGVAR])]) m4_ifndef([AC_LTDL_ENABLE_INSTALL], [AC_DEFUN([AC_LTDL_ENABLE_INSTALL])]) m4_ifndef([AC_LTDL_PREOPEN], [AC_DEFUN([AC_LTDL_PREOPEN])]) m4_ifndef([_LT_AC_SYS_COMPILER], [AC_DEFUN([_LT_AC_SYS_COMPILER])]) m4_ifndef([_LT_AC_LOCK], [AC_DEFUN([_LT_AC_LOCK])]) m4_ifndef([AC_LIBTOOL_SYS_OLD_ARCHIVE], [AC_DEFUN([AC_LIBTOOL_SYS_OLD_ARCHIVE])]) m4_ifndef([_LT_AC_TRY_DLOPEN_SELF], [AC_DEFUN([_LT_AC_TRY_DLOPEN_SELF])]) m4_ifndef([AC_LIBTOOL_PROG_CC_C_O], [AC_DEFUN([AC_LIBTOOL_PROG_CC_C_O])]) m4_ifndef([AC_LIBTOOL_SYS_HARD_LINK_LOCKS], [AC_DEFUN([AC_LIBTOOL_SYS_HARD_LINK_LOCKS])]) m4_ifndef([AC_LIBTOOL_OBJDIR], [AC_DEFUN([AC_LIBTOOL_OBJDIR])]) m4_ifndef([AC_LTDL_OBJDIR], [AC_DEFUN([AC_LTDL_OBJDIR])]) m4_ifndef([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH], [AC_DEFUN([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH])]) m4_ifndef([AC_LIBTOOL_SYS_LIB_STRIP], [AC_DEFUN([AC_LIBTOOL_SYS_LIB_STRIP])]) m4_ifndef([AC_PATH_MAGIC], [AC_DEFUN([AC_PATH_MAGIC])]) m4_ifndef([AC_PROG_LD_GNU], [AC_DEFUN([AC_PROG_LD_GNU])]) m4_ifndef([AC_PROG_LD_RELOAD_FLAG], [AC_DEFUN([AC_PROG_LD_RELOAD_FLAG])]) m4_ifndef([AC_DEPLIBS_CHECK_METHOD], [AC_DEFUN([AC_DEPLIBS_CHECK_METHOD])]) m4_ifndef([AC_LIBTOOL_PROG_COMPILER_NO_RTTI], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_NO_RTTI])]) m4_ifndef([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE], [AC_DEFUN([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE])]) m4_ifndef([AC_LIBTOOL_PROG_COMPILER_PIC], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_PIC])]) m4_ifndef([AC_LIBTOOL_PROG_LD_SHLIBS], [AC_DEFUN([AC_LIBTOOL_PROG_LD_SHLIBS])]) m4_ifndef([AC_LIBTOOL_POSTDEP_PREDEP], [AC_DEFUN([AC_LIBTOOL_POSTDEP_PREDEP])]) m4_ifndef([LT_AC_PROG_EGREP], [AC_DEFUN([LT_AC_PROG_EGREP])]) m4_ifndef([LT_AC_PROG_SED], [AC_DEFUN([LT_AC_PROG_SED])]) m4_ifndef([_LT_CC_BASENAME], [AC_DEFUN([_LT_CC_BASENAME])]) m4_ifndef([_LT_COMPILER_BOILERPLATE], [AC_DEFUN([_LT_COMPILER_BOILERPLATE])]) m4_ifndef([_LT_LINKER_BOILERPLATE], [AC_DEFUN([_LT_LINKER_BOILERPLATE])]) m4_ifndef([_AC_PROG_LIBTOOL], [AC_DEFUN([_AC_PROG_LIBTOOL])]) m4_ifndef([AC_LIBTOOL_SETUP], [AC_DEFUN([AC_LIBTOOL_SETUP])]) m4_ifndef([_LT_AC_CHECK_DLFCN], [AC_DEFUN([_LT_AC_CHECK_DLFCN])]) m4_ifndef([AC_LIBTOOL_SYS_DYNAMIC_LINKER], [AC_DEFUN([AC_LIBTOOL_SYS_DYNAMIC_LINKER])]) m4_ifndef([_LT_AC_TAGCONFIG], [AC_DEFUN([_LT_AC_TAGCONFIG])]) m4_ifndef([AC_DISABLE_FAST_INSTALL], [AC_DEFUN([AC_DISABLE_FAST_INSTALL])]) m4_ifndef([_LT_AC_LANG_CXX], [AC_DEFUN([_LT_AC_LANG_CXX])]) m4_ifndef([_LT_AC_LANG_F77], [AC_DEFUN([_LT_AC_LANG_F77])]) m4_ifndef([_LT_AC_LANG_GCJ], [AC_DEFUN([_LT_AC_LANG_GCJ])]) m4_ifndef([AC_LIBTOOL_LANG_C_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_C_CONFIG])]) m4_ifndef([_LT_AC_LANG_C_CONFIG], [AC_DEFUN([_LT_AC_LANG_C_CONFIG])]) m4_ifndef([AC_LIBTOOL_LANG_CXX_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_CXX_CONFIG])]) m4_ifndef([_LT_AC_LANG_CXX_CONFIG], [AC_DEFUN([_LT_AC_LANG_CXX_CONFIG])]) m4_ifndef([AC_LIBTOOL_LANG_F77_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_F77_CONFIG])]) m4_ifndef([_LT_AC_LANG_F77_CONFIG], [AC_DEFUN([_LT_AC_LANG_F77_CONFIG])]) m4_ifndef([AC_LIBTOOL_LANG_GCJ_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_GCJ_CONFIG])]) m4_ifndef([_LT_AC_LANG_GCJ_CONFIG], [AC_DEFUN([_LT_AC_LANG_GCJ_CONFIG])]) m4_ifndef([AC_LIBTOOL_LANG_RC_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_RC_CONFIG])]) m4_ifndef([_LT_AC_LANG_RC_CONFIG], [AC_DEFUN([_LT_AC_LANG_RC_CONFIG])]) m4_ifndef([AC_LIBTOOL_CONFIG], [AC_DEFUN([AC_LIBTOOL_CONFIG])]) m4_ifndef([_LT_AC_FILE_LTDLL_C], [AC_DEFUN([_LT_AC_FILE_LTDLL_C])]) m4_ifndef([_LT_REQUIRED_DARWIN_CHECKS], [AC_DEFUN([_LT_REQUIRED_DARWIN_CHECKS])]) m4_ifndef([_LT_AC_PROG_CXXCPP], [AC_DEFUN([_LT_AC_PROG_CXXCPP])]) m4_ifndef([_LT_PREPARE_SED_QUOTE_VARS], [AC_DEFUN([_LT_PREPARE_SED_QUOTE_VARS])]) m4_ifndef([_LT_PROG_ECHO_BACKSLASH], [AC_DEFUN([_LT_PROG_ECHO_BACKSLASH])]) m4_ifndef([_LT_PROG_F77], [AC_DEFUN([_LT_PROG_F77])]) m4_ifndef([_LT_PROG_FC], [AC_DEFUN([_LT_PROG_FC])]) m4_ifndef([_LT_PROG_CXX], [AC_DEFUN([_LT_PROG_CXX])]) capnproto-c++-0.8.0/pkgconfig/0000755000175000017500000000000013650320027016715 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/pkgconfig/capnp-json.pc.in0000644000175000017500000000043513340402540021715 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: Cap'n Proto JSON Description: JSON encoder and decoder for Cap'n Proto objects Version: @VERSION@ Libs: -L${libdir} -lcapnp-json Requires: capnp = @VERSION@ kj = @VERSION@ Cflags: -I${includedir} capnproto-c++-0.8.0/pkgconfig/capnp-rpc.pc.in0000644000175000017500000000042013340402540021522 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: Cap'n Proto RPC Description: Fast object-oriented RPC system Version: @VERSION@ Libs: -L${libdir} -lcapnp-rpc Requires: capnp = @VERSION@ kj-async = @VERSION@ Cflags: -I${includedir} capnproto-c++-0.8.0/pkgconfig/capnp.pc.in0000644000175000017500000000054713340402540020752 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: Cap'n Proto Description: Insanely fast serialization system Version: @VERSION@ Libs: -L${libdir} -lcapnp @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Libs.private: @LIBS@ Requires: kj = @VERSION@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/pkgconfig/kj-async.pc.in0000644000175000017500000000055613340402540021370 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: KJ Async Framework Library Description: Basic utility library called KJ (async part) Version: @VERSION@ Libs: -L${libdir} -lkj-async @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Requires: kj = @VERSION@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/pkgconfig/kj-gzip.pc.in0000644000175000017500000000055013650101756021227 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: KJ Gzip Adapters Description: Basic utility library called KJ (gzip part) Version: @VERSION@ Libs: -L${libdir} -lkj-gzip @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Requires: kj-async = @VERSION@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/pkgconfig/kj-http.pc.in0000644000175000017500000000054713340402540021232 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: KJ HTTP Library Description: Basic utility library called KJ (HTTP part) Version: @VERSION@ Libs: -L${libdir} -lkj-http @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Requires: kj-async = @VERSION@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/pkgconfig/kj-test.pc.in0000644000175000017500000000054313340402540021226 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: KJ Test Framework Description: Basic utility library called KJ (test part) Version: @VERSION@ Libs: -L${libdir} -lkj-test @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Requires: kj = @VERSION@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/pkgconfig/kj-tls.pc.in0000644000175000017500000000054513650101756021064 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: KJ TLS Adapters Description: Basic utility library called KJ (TLS part) Version: @VERSION@ Libs: -L${libdir} -lkj-tls @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Requires: kj-async = @VERSION@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/pkgconfig/kj.pc.in0000644000175000017500000000047413340402540020254 0ustar00kentonkenton00000000000000prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: KJ Framework Library Description: Basic utility library called KJ Version: @VERSION@ Libs: -L${libdir} -lkj @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ @STDLIB_FLAG@ Cflags: -I${includedir} @PTHREAD_CFLAGS@ @STDLIB_FLAG@ @CAPNP_LITE_FLAG@ capnproto-c++-0.8.0/src/0000755000175000017500000000000013650320027015535 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/capnp/0000755000175000017500000000000013650320027016636 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/capnp/compat/0000755000175000017500000000000013650320027020121 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/capnp/compat/json.capnp0000644000175000017500000001131113650101756022120 0ustar00kentonkenton00000000000000# Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0x8ef99297a43a5e34; $import "/capnp/c++.capnp".namespace("capnp::json"); struct Value { union { null @0 :Void; boolean @1 :Bool; number @2 :Float64; string @3 :Text; array @4 :List(Value); object @5 :List(Field); # Standard JSON values. call @6 :Call; # Non-standard: A "function call", applying a named function (named by a single identifier) # to a parameter list. Examples: # # BinData(0, "Zm9vCg==") # ISODate("2015-04-15T08:44:50.218Z") # # Mongo DB users will recognize the above as exactly the syntax Mongo uses to represent BSON # "binary" and "date" types in text, since JSON has no analog of these. This is basically the # reason this extension exists. We do NOT recommend using `call` unless you specifically need # to be compatible with some silly format that uses this syntax. } struct Field { name @0 :Text; value @1 :Value; } struct Call { function @0 :Text; params @1 :List(Value); } } # ======================================================================================== # Annotations to control parsing. Typical usage: # # using Json = import "/capnp/compat/json.capnp"; # # And then later on: # # myField @0 :Text $Json.name("my_field"); annotation name @0xfa5b1fd61c2e7c3d (field, enumerant, method, group, union) :Text; # Define an alternative name to use when encoding the given item in JSON. This can be used, for # example, to use snake_case names where needed, even though Cap'n Proto uses strictly camelCase. # # (However, because JSON is derived from JavaScript, you *should* use camelCase names when # defining JSON-based APIs. But, when supporting a pre-existing API you may not have a choice.) annotation flatten @0x82d3e852af0336bf (field, group, union) :FlattenOptions; # Specifies that an aggregate field should be flattened into its parent. # # In order to flatten a member of a union, the union (or, for an anonymous union, the parent # struct type) must have the $jsonDiscriminator annotation. # # TODO(someday): Maybe support "flattening" a List(Value.Field) as a way to support unknown JSON # fields? struct FlattenOptions { prefix @0 :Text = ""; # Optional: Adds the given prefix to flattened field names. } annotation discriminator @0xcfa794e8d19a0162 (struct, union) :DiscriminatorOptions; # Specifies that a union's variant will be decided not by which fields are present, but instead # by a special discriminator field. The value of the discriminator field is a string naming which # variant is active. This allows the members of the union to have the $jsonFlatten annotation, or # to all have the same name. struct DiscriminatorOptions { name @0 :Text; # The name of the discriminator field. Defaults to matching the name of the union. valueName @1 :Text; # If non-null, specifies that the union's value shall have the given field name, rather than the # value's name. In this case the union's variant can only be determined by looking at the # discriminant field, not by inspecting which value field is present. # # It is an error to use `valueName` while also declaring some variants as $flatten. } annotation base64 @0xd7d879450a253e4b (field) :Void; # Place on a field of type `Data` to indicate that its JSON representation is a Base64 string. annotation hex @0xf061e22f0ae5c7b5 (field) :Void; # Place on a field of type `Data` to indicate that its JSON representation is a hex string. annotation notification @0xa0a054dea32fd98c (method) :Void; # Indicates that this method is a JSON-RPC "notification", meaning it expects no response. capnproto-c++-0.8.0/src/capnp/compat/json.h0000644000175000017500000005666613650101756021274 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include namespace capnp { typedef json::Value JsonValue; // For backwards-compatibility. // // TODO(cleanup): Consider replacing all uses of JsonValue with json::Value? class JsonCodec { // Flexible class for encoding Cap'n Proto types as JSON, and decoding JSON back to Cap'n Proto. // // Typical usage: // // JsonCodec json; // // // encode // kj::String encoded = json.encode(someStructReader); // // // decode // json.decode(encoded, someStructBuilder); // // Advanced users can do fancy things like override the way certain types or fields are // represented in JSON by registering handlers. See the unit test for an example. // // Notes: // - When encoding, all primitive fields are always encoded, even if default-valued. Pointer // fields are only encoded if they are non-null. // - 64-bit integers are encoded as strings, since JSON "numbers" are double-precision floating // points which cannot store a 64-bit integer without losing data. // - NaNs and infinite floating point numbers are not allowed by the JSON spec, and so are encoded // as strings. // - Data is encoded as an array of numbers in the range [0,255]. You probably want to register // a handler that does something better, like maybe base64 encoding, but there are a zillion // different ways people do this. // - Encoding/decoding capabilities and AnyPointers requires registering a Handler, since there's // no obvious default behavior. // - When decoding, unrecognized field names are ignored. Note: This means that JSON is NOT a // good format for receiving input from a human. Consider `capnp eval` or the SchemaParser // library for human input. public: JsonCodec(); ~JsonCodec() noexcept(false); // --------------------------------------------------------------------------- // standard API void setPrettyPrint(bool enabled); // Enable to insert newlines, indentation, and other extra spacing into the output. The default // is to use minimal whitespace. void setMaxNestingDepth(size_t maxNestingDepth); // Set maximum nesting depth when decoding JSON to prevent highly nested input from overflowing // the call stack. The default is 64. void setHasMode(HasMode mode); // Normally, primitive field values are always included even if they are equal to the default // value (HasMode::NON_NULL -- only null pointers are omitted). You can use // setHasMode(HasMode::NON_DEFAULT) to specify that default-valued primitive fields should be // omitted as well. template kj::String encode(T&& value) const; // Encode any Cap'n Proto value to JSON, including primitives and // Dynamic{Enum,Struct,List,Capability}, but not DynamicValue (see below). kj::String encode(DynamicValue::Reader value, Type type) const; // Encode a DynamicValue to JSON. `type` is needed because `DynamicValue` itself does // not distinguish between e.g. int32 and int64, which in JSON are handled differently. Most // of the time, though, you can use the single-argument templated version of `encode()` instead. void decode(kj::ArrayPtr input, DynamicStruct::Builder output) const; // Decode JSON text directly into a struct builder. This only works for structs since lists // need to be allocated with the correct size in advance. // // (Remember that any Cap'n Proto struct reader type can be implicitly cast to // DynamicStruct::Reader.) template Orphan decode(kj::ArrayPtr input, Orphanage orphanage) const; // Decode JSON text to any Cap'n Proto object (pointer value), allocated using the given // orphanage. T must be specified explicitly and cannot be dynamic, e.g.: // // Orphan orphan = json.decode(text, orphanage); template ReaderFor decode(kj::ArrayPtr input) const; // Decode JSON text into a primitive or capability value. T must be specified explicitly and // cannot be dynamic, e.g.: // // uint32_t n = json.decode(text); Orphan decode(kj::ArrayPtr input, Type type, Orphanage orphanage) const; Orphan decode( kj::ArrayPtr input, ListSchema type, Orphanage orphanage) const; Orphan decode( kj::ArrayPtr input, StructSchema type, Orphanage orphanage) const; DynamicCapability::Client decode(kj::ArrayPtr input, InterfaceSchema type) const; DynamicEnum decode(kj::ArrayPtr input, EnumSchema type) const; // Decode to a dynamic value, specifying the type schema. // --------------------------------------------------------------------------- // layered API // // You can separate text <-> JsonValue from JsonValue <-> T. These are particularly useful // for calling from Handler implementations. kj::String encodeRaw(JsonValue::Reader value) const; void decodeRaw(kj::ArrayPtr input, JsonValue::Builder output) const; // Translate JsonValue <-> text. template void encode(T&& value, JsonValue::Builder output) const; void encode(DynamicValue::Reader input, Type type, JsonValue::Builder output) const; void decode(JsonValue::Reader input, DynamicStruct::Builder output) const; template Orphan decode(JsonValue::Reader input, Orphanage orphanage) const; template ReaderFor decode(JsonValue::Reader input) const; Orphan decode(JsonValue::Reader input, Type type, Orphanage orphanage) const; Orphan decode(JsonValue::Reader input, ListSchema type, Orphanage orphanage) const; Orphan decode( JsonValue::Reader input, StructSchema type, Orphanage orphanage) const; DynamicCapability::Client decode(JsonValue::Reader input, InterfaceSchema type) const; DynamicEnum decode(JsonValue::Reader input, EnumSchema type) const; // --------------------------------------------------------------------------- // specializing particular types template ()> class Handler; // Implement this interface to specify a special encoding for a particular type or field. // // The templates are a bit ugly, but subclasses of this type essentially implement two methods, // one to encode values of this type and one to decode values of this type. `encode()` is simple: // // void encode(const JsonCodec& codec, ReaderFor input, JsonValue::Builder output) const; // // `decode()` is a bit trickier. When T is a struct (including DynamicStruct), it is: // // void decode(const JsonCodec& codec, JsonValue::Reader input, BuilderFor output) const; // // However, when T is a primitive, decode() is: // // T decode(const JsonCodec& codec, JsonValue::Reader input) const; // // Or when T is any non-struct object (list, blob), decode() is: // // Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const; // // Or when T is an interface: // // T::Client decode(const JsonCodec& codec, JsonValue::Reader input) const; // // Additionally, when T is a struct you can *optionally* also implement the orphan-returning form // of decode(), but it will only be called when the struct would be allocated as an individual // object, not as part of a list. This allows you to return "nullptr" in these cases to say that // the pointer value should be null. This does not apply to list elements because struct list // elements cannot ever be null (since Cap'n Proto encodes struct lists as a flat list rather // than list-of-pointers). template void addTypeHandler(Handler& handler); void addTypeHandler(Type type, Handler& handler); void addTypeHandler(EnumSchema type, Handler& handler); void addTypeHandler(StructSchema type, Handler& handler); void addTypeHandler(ListSchema type, Handler& handler); void addTypeHandler(InterfaceSchema type, Handler& handler); // Arrange that whenever the type T appears in the message, your handler will be used to // encode/decode it. // // Note that if you register a handler for a capability type, it will also apply to subtypes. // Thus Handler handles all capabilities. template void addFieldHandler(StructSchema::Field field, Handler& handler); // Matches only the specific field. T can be a dynamic type. T must match the field's type. void handleByAnnotation(Schema schema); template void handleByAnnotation(); // Inspects the given type (as specified by type parameter or dynamic schema) and all its // dependencies looking for JSON annotations (see json.capnp), building and registering Handlers // based on these annotations. // // If you'd like to use annotations to control JSON, you must call these functions before you // start using the codec. They are not loaded "on demand" because that would require mutex // locking. // --------------------------------------------------------------------------- // Hack to support string literal parameters template auto decode(const char (&input)[size], Params&&... params) const -> decltype(decode(kj::arrayPtr(input, size), kj::fwd(params)...)) { return decode(kj::arrayPtr(input, size - 1), kj::fwd(params)...); } template auto decodeRaw(const char (&input)[size], Params&&... params) const -> decltype(decodeRaw(kj::arrayPtr(input, size), kj::fwd(params)...)) { return decodeRaw(kj::arrayPtr(input, size - 1), kj::fwd(params)...); } private: class HandlerBase; class AnnotatedHandler; class AnnotatedEnumHandler; class Base64Handler; class HexHandler; class JsonValueHandler; struct Impl; kj::Own impl; void encodeField(StructSchema::Field field, DynamicValue::Reader input, JsonValue::Builder output) const; Orphan decodeArray(List::Reader input, ListSchema type, Orphanage orphanage) const; void decodeObject(JsonValue::Reader input, StructSchema type, Orphanage orphanage, DynamicStruct::Builder output) const; void decodeField(StructSchema::Field fieldSchema, JsonValue::Reader fieldValue, Orphanage orphanage, DynamicStruct::Builder output) const; void addTypeHandlerImpl(Type type, HandlerBase& handler); void addFieldHandlerImpl(StructSchema::Field field, Type type, HandlerBase& handler); AnnotatedHandler& loadAnnotatedHandler( StructSchema schema, kj::Maybe discriminator, kj::Maybe unionDeclName, kj::Vector& dependencies); }; // ======================================================================================= // inline implementation details template struct EncodeImpl; template kj::String JsonCodec::encode(T&& value) const { Type type = Type::from(value); typedef FromAny> Base; return encode(DynamicValue::Reader(ReaderFor(kj::fwd(value))), type); } template inline Orphan JsonCodec::decode(kj::ArrayPtr input, Orphanage orphanage) const { return decode(input, Type::from(), orphanage).template releaseAs(); } template inline ReaderFor JsonCodec::decode(kj::ArrayPtr input) const { static_assert(style() == Style::PRIMITIVE || style() == Style::CAPABILITY, "must specify an orphanage to decode an object type"); return decode(input, Type::from(), Orphanage()).getReader().template as(); } inline Orphan JsonCodec::decode( kj::ArrayPtr input, ListSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline Orphan JsonCodec::decode( kj::ArrayPtr input, StructSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline DynamicCapability::Client JsonCodec::decode( kj::ArrayPtr input, InterfaceSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } inline DynamicEnum JsonCodec::decode(kj::ArrayPtr input, EnumSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } // ----------------------------------------------------------------------------- template void JsonCodec::encode(T&& value, JsonValue::Builder output) const { typedef FromAny> Base; encode(DynamicValue::Reader(ReaderFor(kj::fwd(value))), Type::from(), output); } template <> inline void JsonCodec::encode( DynamicStruct::Reader&& value, JsonValue::Builder output) const { encode(DynamicValue::Reader(value), value.getSchema(), output); } template inline Orphan JsonCodec::decode(JsonValue::Reader input, Orphanage orphanage) const { return decode(input, Type::from(), orphanage).template releaseAs(); } template inline ReaderFor JsonCodec::decode(JsonValue::Reader input) const { static_assert(style() == Style::PRIMITIVE || style() == Style::CAPABILITY, "must specify an orphanage to decode an object type"); return decode(input, Type::from(), Orphanage()).getReader().template as(); } inline Orphan JsonCodec::decode( JsonValue::Reader input, ListSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline Orphan JsonCodec::decode( JsonValue::Reader input, StructSchema type, Orphanage orphanage) const { return decode(input, Type(type), orphanage).releaseAs(); } inline DynamicCapability::Client JsonCodec::decode( JsonValue::Reader input, InterfaceSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } inline DynamicEnum JsonCodec::decode(JsonValue::Reader input, EnumSchema type) const { return decode(input, Type(type), Orphanage()).getReader().as(); } // ----------------------------------------------------------------------------- class JsonCodec::HandlerBase { // Internal helper; ignore. public: virtual void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const = 0; virtual Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const; virtual void decodeStructBase(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, ReaderFor input, JsonValue::Builder output) const = 0; virtual Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const = 0; private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input, orphanage); } friend class JsonCodec; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, ReaderFor input, JsonValue::Builder output) const = 0; virtual void decode(const JsonCodec& codec, JsonValue::Reader input, BuilderFor output) const = 0; virtual Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const { // If subclass does not override, fall back to regular version. auto result = orphanage.newOrphan(); decode(codec, input, result.get()); return result; } private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input, orphanage); } void decodeStructBase(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override final { decode(codec, input, output.as()); } friend class JsonCodec; }; template <> class JsonCodec::Handler: private JsonCodec::HandlerBase { // Almost identical to Style::STRUCT except that we pass the struct type to decode(). public: virtual void encode(const JsonCodec& codec, DynamicStruct::Reader input, JsonValue::Builder output) const = 0; virtual void decode(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const = 0; virtual Orphan decode(const JsonCodec& codec, JsonValue::Reader input, StructSchema type, Orphanage orphanage) const { // If subclass does not override, fall back to regular version. auto result = orphanage.newOrphan(type); decode(codec, input, result.get()); return result; } private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input, type.asStruct(), orphanage); } void decodeStructBase(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override final { decode(codec, input, output.as()); } friend class JsonCodec; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, T input, JsonValue::Builder output) const = 0; virtual T decode(const JsonCodec& codec, JsonValue::Reader input) const = 0; private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return decode(codec, input); } friend class JsonCodec; }; template class JsonCodec::Handler: private JsonCodec::HandlerBase { public: virtual void encode(const JsonCodec& codec, typename T::Client input, JsonValue::Builder output) const = 0; virtual typename T::Client decode(const JsonCodec& codec, JsonValue::Reader input) const = 0; private: void encodeBase(const JsonCodec& codec, DynamicValue::Reader input, JsonValue::Builder output) const override final { encode(codec, input.as(), output); } Orphan decodeBase(const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const override final { return orphanage.newOrphanCopy(decode(codec, input)); } friend class JsonCodec; }; template inline void JsonCodec::addTypeHandler(Handler& handler) { addTypeHandlerImpl(Type::from(), handler); } inline void JsonCodec::addTypeHandler(Type type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(EnumSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(StructSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(ListSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } inline void JsonCodec::addTypeHandler(InterfaceSchema type, Handler& handler) { addTypeHandlerImpl(type, handler); } template inline void JsonCodec::addFieldHandler(StructSchema::Field field, Handler& handler) { addFieldHandlerImpl(field, Type::from(), handler); } template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); template <> void JsonCodec::addTypeHandler(Handler& handler) KJ_UNAVAILABLE("JSON handlers for type sets (e.g. all structs, all lists) not implemented; " "try specifying a specific type schema as the first parameter"); // TODO(someday): Implement support for registering handlers that cover thinsg like "all structs" // or "all lists". Currently you can only target a specific struct or list type. template void JsonCodec::handleByAnnotation() { return handleByAnnotation(Schema::from()); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/compat/json.capnp.h0000644000175000017500000013226013650317744022363 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: json.capnp #pragma once #include #include #if !CAPNP_LITE #include #endif // !CAPNP_LITE #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(a3fa7845f919dd83); CAPNP_DECLARE_SCHEMA(e31026e735d69ddf); CAPNP_DECLARE_SCHEMA(a0d9f6eca1c93d48); CAPNP_DECLARE_SCHEMA(fa5b1fd61c2e7c3d); CAPNP_DECLARE_SCHEMA(82d3e852af0336bf); CAPNP_DECLARE_SCHEMA(c4df13257bc2ea61); CAPNP_DECLARE_SCHEMA(cfa794e8d19a0162); CAPNP_DECLARE_SCHEMA(c2f8c20c293e5319); CAPNP_DECLARE_SCHEMA(d7d879450a253e4b); CAPNP_DECLARE_SCHEMA(f061e22f0ae5c7b5); CAPNP_DECLARE_SCHEMA(a0a054dea32fd98c); } // namespace schemas } // namespace capnp namespace capnp { namespace json { struct Value { Value() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NULL_, BOOLEAN, NUMBER, STRING, ARRAY, OBJECT, CALL, }; struct Field; struct Call; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(a3fa7845f919dd83, 2, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Value::Field { Field() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e31026e735d69ddf, 0, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Value::Call { Call() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(a0d9f6eca1c93d48, 0, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct FlattenOptions { FlattenOptions() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c4df13257bc2ea61, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct DiscriminatorOptions { DiscriminatorOptions() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c2f8c20c293e5319, 0, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class Value::Reader { public: typedef Value Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNull() const; inline ::capnp::Void getNull() const; inline bool isBoolean() const; inline bool getBoolean() const; inline bool isNumber() const; inline double getNumber() const; inline bool isString() const; inline bool hasString() const; inline ::capnp::Text::Reader getString() const; inline bool isArray() const; inline bool hasArray() const; inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader getArray() const; inline bool isObject() const; inline bool hasObject() const; inline ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Reader getObject() const; inline bool isCall() const; inline bool hasCall() const; inline ::capnp::json::Value::Call::Reader getCall() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Value::Builder { public: typedef Value Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNull(); inline ::capnp::Void getNull(); inline void setNull( ::capnp::Void value = ::capnp::VOID); inline bool isBoolean(); inline bool getBoolean(); inline void setBoolean(bool value); inline bool isNumber(); inline double getNumber(); inline void setNumber(double value); inline bool isString(); inline bool hasString(); inline ::capnp::Text::Builder getString(); inline void setString( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initString(unsigned int size); inline void adoptString(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownString(); inline bool isArray(); inline bool hasArray(); inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder getArray(); inline void setArray( ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder initArray(unsigned int size); inline void adoptArray(::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>> disownArray(); inline bool isObject(); inline bool hasObject(); inline ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Builder getObject(); inline void setObject( ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Builder initObject(unsigned int size); inline void adoptObject(::capnp::Orphan< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>> disownObject(); inline bool isCall(); inline bool hasCall(); inline ::capnp::json::Value::Call::Builder getCall(); inline void setCall( ::capnp::json::Value::Call::Reader value); inline ::capnp::json::Value::Call::Builder initCall(); inline void adoptCall(::capnp::Orphan< ::capnp::json::Value::Call>&& value); inline ::capnp::Orphan< ::capnp::json::Value::Call> disownCall(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Value::Pipeline { public: typedef Value Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Value::Field::Reader { public: typedef Field Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline bool hasValue() const; inline ::capnp::json::Value::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Value::Field::Builder { public: typedef Field Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline bool hasValue(); inline ::capnp::json::Value::Builder getValue(); inline void setValue( ::capnp::json::Value::Reader value); inline ::capnp::json::Value::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::json::Value>&& value); inline ::capnp::Orphan< ::capnp::json::Value> disownValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Value::Field::Pipeline { public: typedef Field Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::json::Value::Pipeline getValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Value::Call::Reader { public: typedef Call Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasFunction() const; inline ::capnp::Text::Reader getFunction() const; inline bool hasParams() const; inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader getParams() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Value::Call::Builder { public: typedef Call Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasFunction(); inline ::capnp::Text::Builder getFunction(); inline void setFunction( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initFunction(unsigned int size); inline void adoptFunction(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownFunction(); inline bool hasParams(); inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder getParams(); inline void setParams( ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder initParams(unsigned int size); inline void adoptParams(::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>> disownParams(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Value::Call::Pipeline { public: typedef Call Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class FlattenOptions::Reader { public: typedef FlattenOptions Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasPrefix() const; inline ::capnp::Text::Reader getPrefix() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class FlattenOptions::Builder { public: typedef FlattenOptions Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasPrefix(); inline ::capnp::Text::Builder getPrefix(); inline void setPrefix( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initPrefix(unsigned int size); inline void adoptPrefix(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownPrefix(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class FlattenOptions::Pipeline { public: typedef FlattenOptions Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class DiscriminatorOptions::Reader { public: typedef DiscriminatorOptions Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline bool hasValueName() const; inline ::capnp::Text::Reader getValueName() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class DiscriminatorOptions::Builder { public: typedef DiscriminatorOptions Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline bool hasValueName(); inline ::capnp::Text::Builder getValueName(); inline void setValueName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initValueName(unsigned int size); inline void adoptValueName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownValueName(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class DiscriminatorOptions::Pipeline { public: typedef DiscriminatorOptions Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= inline ::capnp::json::Value::Which Value::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::json::Value::Which Value::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Value::Reader::isNull() const { return which() == Value::NULL_; } inline bool Value::Builder::isNull() { return which() == Value::NULL_; } inline ::capnp::Void Value::Reader::getNull() const { KJ_IREQUIRE((which() == Value::NULL_), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Value::Builder::getNull() { KJ_IREQUIRE((which() == Value::NULL_), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Value::Builder::setNull( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::NULL_); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isBoolean() const { return which() == Value::BOOLEAN; } inline bool Value::Builder::isBoolean() { return which() == Value::BOOLEAN; } inline bool Value::Reader::getBoolean() const { KJ_IREQUIRE((which() == Value::BOOLEAN), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<16>() * ::capnp::ELEMENTS); } inline bool Value::Builder::getBoolean() { KJ_IREQUIRE((which() == Value::BOOLEAN), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<16>() * ::capnp::ELEMENTS); } inline void Value::Builder::setBoolean(bool value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::BOOLEAN); _builder.setDataField( ::capnp::bounded<16>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isNumber() const { return which() == Value::NUMBER; } inline bool Value::Builder::isNumber() { return which() == Value::NUMBER; } inline double Value::Reader::getNumber() const { KJ_IREQUIRE((which() == Value::NUMBER), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline double Value::Builder::getNumber() { KJ_IREQUIRE((which() == Value::NUMBER), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setNumber(double value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::NUMBER); _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isString() const { return which() == Value::STRING; } inline bool Value::Builder::isString() { return which() == Value::STRING; } inline bool Value::Reader::hasString() const { if (which() != Value::STRING) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasString() { if (which() != Value::STRING) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Value::Reader::getString() const { KJ_IREQUIRE((which() == Value::STRING), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Value::Builder::getString() { KJ_IREQUIRE((which() == Value::STRING), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::setString( ::capnp::Text::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::STRING); ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Value::Builder::initString(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::STRING); return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Builder::adoptString( ::capnp::Orphan< ::capnp::Text>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::STRING); ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Value::Builder::disownString() { KJ_IREQUIRE((which() == Value::STRING), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Reader::isArray() const { return which() == Value::ARRAY; } inline bool Value::Builder::isArray() { return which() == Value::ARRAY; } inline bool Value::Reader::hasArray() const { if (which() != Value::ARRAY) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasArray() { if (which() != Value::ARRAY) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader Value::Reader::getArray() const { KJ_IREQUIRE((which() == Value::ARRAY), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder Value::Builder::getArray() { KJ_IREQUIRE((which() == Value::ARRAY), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::setArray( ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::ARRAY); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder Value::Builder::initArray(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::ARRAY); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Builder::adoptArray( ::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::ARRAY); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>> Value::Builder::disownArray() { KJ_IREQUIRE((which() == Value::ARRAY), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Reader::isObject() const { return which() == Value::OBJECT; } inline bool Value::Builder::isObject() { return which() == Value::OBJECT; } inline bool Value::Reader::hasObject() const { if (which() != Value::OBJECT) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasObject() { if (which() != Value::OBJECT) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Reader Value::Reader::getObject() const { KJ_IREQUIRE((which() == Value::OBJECT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Builder Value::Builder::getObject() { KJ_IREQUIRE((which() == Value::OBJECT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::setObject( ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::OBJECT); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>::Builder Value::Builder::initObject(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::OBJECT); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Builder::adoptObject( ::capnp::Orphan< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::OBJECT); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>> Value::Builder::disownObject() { KJ_IREQUIRE((which() == Value::OBJECT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value::Field, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Reader::isCall() const { return which() == Value::CALL; } inline bool Value::Builder::isCall() { return which() == Value::CALL; } inline bool Value::Reader::hasCall() const { if (which() != Value::CALL) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasCall() { if (which() != Value::CALL) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::json::Value::Call::Reader Value::Reader::getCall() const { KJ_IREQUIRE((which() == Value::CALL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::json::Value::Call>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::json::Value::Call::Builder Value::Builder::getCall() { KJ_IREQUIRE((which() == Value::CALL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::json::Value::Call>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::setCall( ::capnp::json::Value::Call::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::CALL); ::capnp::_::PointerHelpers< ::capnp::json::Value::Call>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::json::Value::Call::Builder Value::Builder::initCall() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::CALL); return ::capnp::_::PointerHelpers< ::capnp::json::Value::Call>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::adoptCall( ::capnp::Orphan< ::capnp::json::Value::Call>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::CALL); ::capnp::_::PointerHelpers< ::capnp::json::Value::Call>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::json::Value::Call> Value::Builder::disownCall() { KJ_IREQUIRE((which() == Value::CALL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::json::Value::Call>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Field::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Field::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Value::Field::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Value::Field::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Field::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Value::Field::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Field::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Value::Field::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Field::Reader::hasValue() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Value::Field::Builder::hasValue() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::json::Value::Reader Value::Field::Reader::getValue() const { return ::capnp::_::PointerHelpers< ::capnp::json::Value>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::json::Value::Builder Value::Field::Builder::getValue() { return ::capnp::_::PointerHelpers< ::capnp::json::Value>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::json::Value::Pipeline Value::Field::Pipeline::getValue() { return ::capnp::json::Value::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE inline void Value::Field::Builder::setValue( ::capnp::json::Value::Reader value) { ::capnp::_::PointerHelpers< ::capnp::json::Value>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::json::Value::Builder Value::Field::Builder::initValue() { return ::capnp::_::PointerHelpers< ::capnp::json::Value>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Value::Field::Builder::adoptValue( ::capnp::Orphan< ::capnp::json::Value>&& value) { ::capnp::_::PointerHelpers< ::capnp::json::Value>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::json::Value> Value::Field::Builder::disownValue() { return ::capnp::_::PointerHelpers< ::capnp::json::Value>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Value::Call::Reader::hasFunction() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Call::Builder::hasFunction() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Value::Call::Reader::getFunction() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Value::Call::Builder::getFunction() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Call::Builder::setFunction( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Value::Call::Builder::initFunction(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Call::Builder::adoptFunction( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Value::Call::Builder::disownFunction() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Call::Reader::hasParams() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Value::Call::Builder::hasParams() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader Value::Call::Reader::getParams() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder Value::Call::Builder::getParams() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Value::Call::Builder::setParams( ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>::Builder Value::Call::Builder::initParams(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Value::Call::Builder::adoptParams( ::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>> Value::Call::Builder::disownParams() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::json::Value, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool FlattenOptions::Reader::hasPrefix() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool FlattenOptions::Builder::hasPrefix() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader FlattenOptions::Reader::getPrefix() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), ::capnp::schemas::bp_c4df13257bc2ea61 + 34); } inline ::capnp::Text::Builder FlattenOptions::Builder::getPrefix() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), ::capnp::schemas::bp_c4df13257bc2ea61 + 34); } inline void FlattenOptions::Builder::setPrefix( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder FlattenOptions::Builder::initPrefix(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void FlattenOptions::Builder::adoptPrefix( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> FlattenOptions::Builder::disownPrefix() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool DiscriminatorOptions::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool DiscriminatorOptions::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader DiscriminatorOptions::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder DiscriminatorOptions::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void DiscriminatorOptions::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder DiscriminatorOptions::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void DiscriminatorOptions::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> DiscriminatorOptions::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool DiscriminatorOptions::Reader::hasValueName() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool DiscriminatorOptions::Builder::hasValueName() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader DiscriminatorOptions::Reader::getValueName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder DiscriminatorOptions::Builder::getValueName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void DiscriminatorOptions::Builder::setValueName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder DiscriminatorOptions::Builder::initValueName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void DiscriminatorOptions::Builder::adoptValueName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> DiscriminatorOptions::Builder::disownValueName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/compat/std-iterator.h0000644000175000017500000000335513650101756022727 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // This exposes IndexingIterator as something compatible with std::iterator so that things like // std::copy work with List::begin/List::end. // Make sure that if this header is before list.h by the user it includes it to make // IndexingIterator visible to avoid brittle header problems. #include "../list.h" #include namespace std { template struct iterator_traits> : public std::iterator {}; } // namespace std capnproto-c++-0.8.0/src/capnp/compat/json.c++0000644000175000017500000014573213650317313021404 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "json.h" #include // for HUGEVAL to check for overflow in strtod #include // strtod #include // for strtod errors #include #include #include #include #include #include #include namespace capnp { struct JsonCodec::Impl { bool prettyPrint = false; HasMode hasMode = HasMode::NON_NULL; size_t maxNestingDepth = 64; kj::HashMap typeHandlers; kj::HashMap fieldHandlers; kj::HashMap>> annotatedHandlers; kj::HashMap> annotatedEnumHandlers; kj::StringTree encodeRaw(JsonValue::Reader value, uint indent, bool& multiline, bool hasPrefix) const { switch (value.which()) { case JsonValue::NULL_: return kj::strTree("null"); case JsonValue::BOOLEAN: return kj::strTree(value.getBoolean()); case JsonValue::NUMBER: return kj::strTree(value.getNumber()); case JsonValue::STRING: return kj::strTree(encodeString(value.getString())); case JsonValue::ARRAY: { auto array = value.getArray(); uint subIndent = indent + (array.size() > 1); bool childMultiline = false; auto encodedElements = KJ_MAP(element, array) { return encodeRaw(element, subIndent, childMultiline, false); }; return kj::strTree('[', encodeList( kj::mv(encodedElements), childMultiline, indent, multiline, hasPrefix), ']'); } case JsonValue::OBJECT: { auto object = value.getObject(); uint subIndent = indent + (object.size() > 1); bool childMultiline = false; kj::StringPtr colon = prettyPrint ? ": " : ":"; auto encodedElements = KJ_MAP(field, object) { return kj::strTree( encodeString(field.getName()), colon, encodeRaw(field.getValue(), subIndent, childMultiline, true)); }; return kj::strTree('{', encodeList( kj::mv(encodedElements), childMultiline, indent, multiline, hasPrefix), '}'); } case JsonValue::CALL: { auto call = value.getCall(); auto params = call.getParams(); uint subIndent = indent + (params.size() > 1); bool childMultiline = false; auto encodedElements = KJ_MAP(element, params) { return encodeRaw(element, subIndent, childMultiline, false); }; return kj::strTree(call.getFunction(), '(', encodeList( kj::mv(encodedElements), childMultiline, indent, multiline, true), ')'); } } KJ_FAIL_ASSERT("unknown JsonValue type", static_cast(value.which())); } kj::String encodeString(kj::StringPtr chars) const { static const char HEXDIGITS[] = "0123456789abcdef"; kj::Vector escaped(chars.size() + 3); escaped.add('"'); for (char c: chars) { switch (c) { case '\"': escaped.addAll(kj::StringPtr("\\\"")); break; case '\\': escaped.addAll(kj::StringPtr("\\\\")); break; case '\b': escaped.addAll(kj::StringPtr("\\b")); break; case '\f': escaped.addAll(kj::StringPtr("\\f")); break; case '\n': escaped.addAll(kj::StringPtr("\\n")); break; case '\r': escaped.addAll(kj::StringPtr("\\r")); break; case '\t': escaped.addAll(kj::StringPtr("\\t")); break; default: if (static_cast(c) < 0x20) { escaped.addAll(kj::StringPtr("\\u00")); uint8_t c2 = c; escaped.add(HEXDIGITS[c2 / 16]); escaped.add(HEXDIGITS[c2 % 16]); } else { escaped.add(c); } break; } } escaped.add('"'); escaped.add('\0'); return kj::String(escaped.releaseAsArray()); } kj::StringTree encodeList(kj::Array elements, bool hasMultilineElement, uint indent, bool& multiline, bool hasPrefix) const { size_t maxChildSize = 0; for (auto& e: elements) maxChildSize = kj::max(maxChildSize, e.size()); kj::StringPtr prefix; kj::StringPtr delim; kj::StringPtr suffix; kj::String ownPrefix; kj::String ownDelim; if (!prettyPrint) { // No whitespace. delim = ","; prefix = ""; suffix = ""; } else if ((elements.size() > 1) && (hasMultilineElement || maxChildSize > 50)) { // If the array contained any multi-line elements, OR it contained sufficiently long // elements, then put each element on its own line. auto indentSpace = kj::repeat(' ', (indent + 1) * 2); delim = ownDelim = kj::str(",\n", indentSpace); multiline = true; if (hasPrefix) { // We're producing a multi-line list, and the first line has some garbage in front of it. // Therefore, move the first element to the next line. prefix = ownPrefix = kj::str("\n", indentSpace); } else { prefix = " "; } suffix = " "; } else { // Put everything on one line, but add spacing between elements for legibility. delim = ", "; prefix = ""; suffix = ""; } return kj::strTree(prefix, kj::StringTree(kj::mv(elements), delim), suffix); } }; JsonCodec::JsonCodec() : impl(kj::heap()) {} JsonCodec::~JsonCodec() noexcept(false) {} void JsonCodec::setPrettyPrint(bool enabled) { impl->prettyPrint = enabled; } void JsonCodec::setMaxNestingDepth(size_t maxNestingDepth) { impl->maxNestingDepth = maxNestingDepth; } void JsonCodec::setHasMode(HasMode mode) { impl->hasMode = mode; } kj::String JsonCodec::encode(DynamicValue::Reader value, Type type) const { MallocMessageBuilder message; auto json = message.getRoot(); encode(value, type, json); return encodeRaw(json); } void JsonCodec::decode(kj::ArrayPtr input, DynamicStruct::Builder output) const { MallocMessageBuilder message; auto json = message.getRoot(); decodeRaw(input, json); decode(json, output); } Orphan JsonCodec::decode( kj::ArrayPtr input, Type type, Orphanage orphanage) const { MallocMessageBuilder message; auto json = message.getRoot(); decodeRaw(input, json); return decode(json, type, orphanage); } kj::String JsonCodec::encodeRaw(JsonValue::Reader value) const { bool multiline = false; return impl->encodeRaw(value, 0, multiline, false).flatten(); } void JsonCodec::encode(DynamicValue::Reader input, Type type, JsonValue::Builder output) const { // TODO(someday): For interfaces, check for handlers on superclasses, per documentation... // TODO(someday): For branded types, should we check for handlers on the generic? // TODO(someday): Allow registering handlers for "all structs", "all lists", etc? KJ_IF_MAYBE(handler, impl->typeHandlers.find(type)) { (*handler)->encodeBase(*this, input, output); return; } switch (type.which()) { case schema::Type::VOID: output.setNull(); break; case schema::Type::BOOL: output.setBoolean(input.as()); break; case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: output.setNumber(input.as()); break; case schema::Type::FLOAT32: case schema::Type::FLOAT64: { double value = input.as(); // Inf, -inf and NaN are not allowed in the JSON spec. Storing into string. if (kj::inf() == value) { output.setString("Infinity"); } else if (-kj::inf() == value) { output.setString("-Infinity"); } else if (kj::isNaN(value)) { output.setString("NaN"); } else { output.setNumber(value); } } break; case schema::Type::INT64: output.setString(kj::str(input.as())); break; case schema::Type::UINT64: output.setString(kj::str(input.as())); break; case schema::Type::TEXT: output.setString(kj::str(input.as())); break; case schema::Type::DATA: { // Turn into array of byte values. Yep, this is pretty ugly. People really need to override // this with a handler. auto bytes = input.as(); auto array = output.initArray(bytes.size()); for (auto i: kj::indices(bytes)) { array[i].setNumber(bytes[i]); } break; } case schema::Type::LIST: { auto list = input.as(); auto elementType = type.asList().getElementType(); auto array = output.initArray(list.size()); for (auto i: kj::indices(list)) { encode(list[i], elementType, array[i]); } break; } case schema::Type::ENUM: { auto e = input.as(); KJ_IF_MAYBE(symbol, e.getEnumerant()) { output.setString(symbol->getProto().getName()); } else { output.setNumber(e.getRaw()); } break; } case schema::Type::STRUCT: { auto structValue = input.as(); auto nonUnionFields = structValue.getSchema().getNonUnionFields(); KJ_STACK_ARRAY(bool, hasField, nonUnionFields.size(), 32, 128); uint fieldCount = 0; for (auto i: kj::indices(nonUnionFields)) { fieldCount += (hasField[i] = structValue.has(nonUnionFields[i], impl->hasMode)); } // We try to write the union field, if any, in proper order with the rest. auto which = structValue.which(); bool unionFieldIsNull = false; KJ_IF_MAYBE(field, which) { // Even if the union field is null, if it is not the default field of the union then we // have to print it anyway. unionFieldIsNull = !structValue.has(*field, impl->hasMode); if (field->getProto().getDiscriminantValue() != 0 || !unionFieldIsNull) { ++fieldCount; } else { which = nullptr; } } auto object = output.initObject(fieldCount); size_t pos = 0; for (auto i: kj::indices(nonUnionFields)) { auto field = nonUnionFields[i]; KJ_IF_MAYBE(unionField, which) { if (unionField->getIndex() < field.getIndex()) { auto outField = object[pos++]; outField.setName(unionField->getProto().getName()); if (unionFieldIsNull) { outField.initValue().setNull(); } else { encodeField(*unionField, structValue.get(*unionField), outField.initValue()); } which = nullptr; } } if (hasField[i]) { auto outField = object[pos++]; outField.setName(field.getProto().getName()); encodeField(field, structValue.get(field), outField.initValue()); } } if (which != nullptr) { // Union field not printed yet; must be last. auto unionField = KJ_ASSERT_NONNULL(which); auto outField = object[pos++]; outField.setName(unionField.getProto().getName()); if (unionFieldIsNull) { outField.initValue().setNull(); } else { encodeField(unionField, structValue.get(unionField), outField.initValue()); } } KJ_ASSERT(pos == fieldCount); break; } case schema::Type::INTERFACE: KJ_FAIL_REQUIRE("don't know how to JSON-encode capabilities; " "please register a JsonCodec::Handler for this"); case schema::Type::ANY_POINTER: KJ_FAIL_REQUIRE("don't know how to JSON-encode AnyPointer; " "please register a JsonCodec::Handler for this"); } } void JsonCodec::encodeField(StructSchema::Field field, DynamicValue::Reader input, JsonValue::Builder output) const { KJ_IF_MAYBE(handler, impl->fieldHandlers.find(field)) { (*handler)->encodeBase(*this, input, output); return; } encode(input, field.getType(), output); } Orphan JsonCodec::decodeArray(List::Reader input, ListSchema type, Orphanage orphanage) const { auto orphan = orphanage.newOrphan(type, input.size()); auto output = orphan.get(); for (auto i: kj::indices(input)) { output.adopt(i, decode(input[i], type.getElementType(), orphanage)); } return orphan; } void JsonCodec::decodeObject(JsonValue::Reader input, StructSchema type, Orphanage orphanage, DynamicStruct::Builder output) const { KJ_REQUIRE(input.isObject(), "Expected object value") { return; } for (auto field: input.getObject()) { KJ_IF_MAYBE(fieldSchema, type.findFieldByName(field.getName())) { decodeField(*fieldSchema, field.getValue(), orphanage, output); } else { // Unknown json fields are ignored to allow schema evolution } } } void JsonCodec::decodeField(StructSchema::Field fieldSchema, JsonValue::Reader fieldValue, Orphanage orphanage, DynamicStruct::Builder output) const { auto fieldType = fieldSchema.getType(); KJ_IF_MAYBE(handler, impl->fieldHandlers.find(fieldSchema)) { output.adopt(fieldSchema, (*handler)->decodeBase(*this, fieldValue, fieldType, orphanage)); } else { output.adopt(fieldSchema, decode(fieldValue, fieldType, orphanage)); } } void JsonCodec::decode(JsonValue::Reader input, DynamicStruct::Builder output) const { auto type = output.getSchema(); KJ_IF_MAYBE(handler, impl->typeHandlers.find(type)) { return (*handler)->decodeStructBase(*this, input, output); } decodeObject(input, type, Orphanage::getForMessageContaining(output), output); } Orphan JsonCodec::decode( JsonValue::Reader input, Type type, Orphanage orphanage) const { KJ_IF_MAYBE(handler, impl->typeHandlers.find(type)) { return (*handler)->decodeBase(*this, input, type, orphanage); } switch(type.which()) { case schema::Type::VOID: return capnp::VOID; case schema::Type::BOOL: switch (input.which()) { case JsonValue::BOOLEAN: return input.getBoolean(); default: KJ_FAIL_REQUIRE("Expected boolean value"); } case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: // Relies on range check in DynamicValue::Reader::as switch (input.which()) { case JsonValue::NUMBER: return input.getNumber(); case JsonValue::STRING: return input.getString().parseAs(); default: KJ_FAIL_REQUIRE("Expected integer value"); } case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: // Relies on range check in DynamicValue::Reader::as switch (input.which()) { case JsonValue::NUMBER: return input.getNumber(); case JsonValue::STRING: return input.getString().parseAs(); default: KJ_FAIL_REQUIRE("Expected integer value"); } case schema::Type::FLOAT32: case schema::Type::FLOAT64: switch (input.which()) { case JsonValue::NULL_: return kj::nan(); case JsonValue::NUMBER: return input.getNumber(); case JsonValue::STRING: return input.getString().parseAs(); default: KJ_FAIL_REQUIRE("Expected float value"); } case schema::Type::TEXT: switch (input.which()) { case JsonValue::STRING: return orphanage.newOrphanCopy(input.getString()); default: KJ_FAIL_REQUIRE("Expected text value"); } case schema::Type::DATA: switch (input.which()) { case JsonValue::ARRAY: { auto array = input.getArray(); auto orphan = orphanage.newOrphan(array.size()); auto data = orphan.get(); for (auto i: kj::indices(array)) { auto x = array[i].getNumber(); KJ_REQUIRE(byte(x) == x, "Number in byte array is not an integer in [0, 255]"); data[i] = x; } return kj::mv(orphan); } default: KJ_FAIL_REQUIRE("Expected data value"); } case schema::Type::LIST: switch (input.which()) { case JsonValue::ARRAY: return decodeArray(input.getArray(), type.asList(), orphanage); default: KJ_FAIL_REQUIRE("Expected list value") { break; } return orphanage.newOrphan(type.asList(), 0); } case schema::Type::ENUM: switch (input.which()) { case JsonValue::STRING: return DynamicEnum(type.asEnum().getEnumerantByName(input.getString())); default: KJ_FAIL_REQUIRE("Expected enum value") { break; } return DynamicEnum(type.asEnum(), 0); } case schema::Type::STRUCT: { auto structType = type.asStruct(); auto orphan = orphanage.newOrphan(structType); decodeObject(input, structType, orphanage, orphan.get()); return kj::mv(orphan); } case schema::Type::INTERFACE: KJ_FAIL_REQUIRE("don't know how to JSON-decode capabilities; " "please register a JsonCodec::Handler for this"); case schema::Type::ANY_POINTER: KJ_FAIL_REQUIRE("don't know how to JSON-decode AnyPointer; " "please register a JsonCodec::Handler for this"); } KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT; } // ----------------------------------------------------------------------------- namespace { class Input { public: Input(kj::ArrayPtr input) : wrapped(input) {} bool exhausted() { return wrapped.size() == 0 || wrapped.front() == '\0'; } char nextChar() { KJ_REQUIRE(!exhausted(), "JSON message ends prematurely."); return wrapped.front(); } void advance(size_t numBytes = 1) { KJ_REQUIRE(numBytes <= wrapped.size(), "JSON message ends prematurely."); wrapped = kj::arrayPtr(wrapped.begin() + numBytes, wrapped.end()); } void advanceTo(const char *newPos) { KJ_REQUIRE(wrapped.begin() <= newPos && newPos < wrapped.end(), "JSON message ends prematurely."); wrapped = kj::arrayPtr(newPos, wrapped.end()); } kj::ArrayPtr consume(size_t numBytes = 1) { auto originalPos = wrapped.begin(); advance(numBytes); return kj::arrayPtr(originalPos, wrapped.begin()); } void consume(char expected) { char current = nextChar(); KJ_REQUIRE(current == expected, "Unexpected input in JSON message."); advance(); } void consume(kj::ArrayPtr expected) { KJ_REQUIRE(wrapped.size() >= expected.size()); auto prefix = wrapped.slice(0, expected.size()); KJ_REQUIRE(prefix == expected, "Unexpected input in JSON message."); advance(expected.size()); } bool tryConsume(char expected) { bool found = !exhausted() && nextChar() == expected; if (found) { advance(); } return found; } template void consumeOne(Predicate&& predicate) { char current = nextChar(); KJ_REQUIRE(predicate(current), "Unexpected input in JSON message."); advance(); } template kj::ArrayPtr consumeWhile(Predicate&& predicate) { auto originalPos = wrapped.begin(); while (!exhausted() && predicate(nextChar())) { advance(); } return kj::arrayPtr(originalPos, wrapped.begin()); } template // Function kj::ArrayPtr consumeCustom(F&& f) { // Allows consuming in a custom manner without exposing the wrapped ArrayPtr. auto originalPos = wrapped.begin(); f(*this); return kj::arrayPtr(originalPos, wrapped.begin()); } void consumeWhitespace() { consumeWhile([](char chr) { return ( chr == ' ' || chr == '\n' || chr == '\r' || chr == '\t' ); }); } private: kj::ArrayPtr wrapped; }; // class Input class Parser { public: Parser(size_t maxNestingDepth, kj::ArrayPtr input) : maxNestingDepth(maxNestingDepth), input(input), nestingDepth(0) {} void parseValue(JsonValue::Builder& output) { input.consumeWhitespace(); KJ_DEFER(input.consumeWhitespace()); KJ_REQUIRE(!input.exhausted(), "JSON message ends prematurely."); switch (input.nextChar()) { case 'n': input.consume(kj::StringPtr("null")); output.setNull(); break; case 'f': input.consume(kj::StringPtr("false")); output.setBoolean(false); break; case 't': input.consume(kj::StringPtr("true")); output.setBoolean(true); break; case '"': parseString(output); break; case '[': parseArray(output); break; case '{': parseObject(output); break; case '-': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': parseNumber(output); break; default: KJ_FAIL_REQUIRE("Unexpected input in JSON message."); } } void parseNumber(JsonValue::Builder& output) { output.setNumber(consumeNumber().parseAs()); } void parseString(JsonValue::Builder& output) { output.setString(consumeQuotedString()); } void parseArray(JsonValue::Builder& output) { // TODO(perf): Using orphans leaves holes in the message. It's expected // that a JsonValue is used for interop, and won't be sent or written as a // Cap'n Proto message. This also applies to parseObject below. kj::Vector> values; auto orphanage = Orphanage::getForMessageContaining(output); bool expectComma = false; input.consume('['); KJ_REQUIRE(++nestingDepth <= maxNestingDepth, "JSON message nested too deeply."); KJ_DEFER(--nestingDepth); while (input.consumeWhitespace(), input.nextChar() != ']') { auto orphan = orphanage.newOrphan(); auto builder = orphan.get(); if (expectComma) { input.consumeWhitespace(); input.consume(','); input.consumeWhitespace(); } parseValue(builder); values.add(kj::mv(orphan)); expectComma = true; } output.initArray(values.size()); auto array = output.getArray(); for (auto i : kj::indices(values)) { array.adoptWithCaveats(i, kj::mv(values[i])); } input.consume(']'); } void parseObject(JsonValue::Builder& output) { kj::Vector> fields; auto orphanage = Orphanage::getForMessageContaining(output); bool expectComma = false; input.consume('{'); KJ_REQUIRE(++nestingDepth <= maxNestingDepth, "JSON message nested too deeply."); KJ_DEFER(--nestingDepth); while (input.consumeWhitespace(), input.nextChar() != '}') { auto orphan = orphanage.newOrphan(); auto builder = orphan.get(); if (expectComma) { input.consumeWhitespace(); input.consume(','); input.consumeWhitespace(); } builder.setName(consumeQuotedString()); input.consumeWhitespace(); input.consume(':'); input.consumeWhitespace(); auto valueBuilder = builder.getValue(); parseValue(valueBuilder); fields.add(kj::mv(orphan)); expectComma = true; } output.initObject(fields.size()); auto object = output.getObject(); for (auto i : kj::indices(fields)) { object.adoptWithCaveats(i, kj::mv(fields[i])); } input.consume('}'); } bool inputExhausted() { return input.exhausted(); } private: kj::String consumeQuotedString() { input.consume('"'); // TODO(perf): Avoid copy / alloc if no escapes encoutered. // TODO(perf): Get statistics on string size and preallocate? kj::Vector decoded; do { auto stringValue = input.consumeWhile([](const char chr) { return chr != '"' && chr != '\\'; }); decoded.addAll(stringValue); if (input.nextChar() == '\\') { // handle escapes. input.advance(); switch(input.nextChar()) { case '"' : decoded.add('"' ); input.advance(); break; case '\\': decoded.add('\\'); input.advance(); break; case '/' : decoded.add('/' ); input.advance(); break; case 'b' : decoded.add('\b'); input.advance(); break; case 'f' : decoded.add('\f'); input.advance(); break; case 'n' : decoded.add('\n'); input.advance(); break; case 'r' : decoded.add('\r'); input.advance(); break; case 't' : decoded.add('\t'); input.advance(); break; case 'u' : input.consume('u'); unescapeAndAppend(input.consume(size_t(4)), decoded); break; default: KJ_FAIL_REQUIRE("Invalid escape in JSON string."); break; } } } while(input.nextChar() != '"'); input.consume('"'); decoded.add('\0'); // TODO(perf): This copy can be eliminated, but I can't find the kj::wayToDoIt(); return kj::String(decoded.releaseAsArray()); } kj::String consumeNumber() { auto numArrayPtr = input.consumeCustom([](Input& input) { input.tryConsume('-'); if (!input.tryConsume('0')) { input.consumeOne([](char c) { return '1' <= c && c <= '9'; }); input.consumeWhile([](char c) { return '0' <= c && c <= '9'; }); } if (input.tryConsume('.')) { input.consumeWhile([](char c) { return '0' <= c && c <= '9'; }); } if (input.tryConsume('e') || input.tryConsume('E')) { input.tryConsume('+') || input.tryConsume('-'); input.consumeWhile([](char c) { return '0' <= c && c <= '9'; }); } }); KJ_REQUIRE(numArrayPtr.size() > 0, "Expected number in JSON input."); kj::Vector number; number.addAll(numArrayPtr); number.add('\0'); return kj::String(number.releaseAsArray()); } // TODO(someday): This "interface" is ugly, and won't work if/when surrogates are handled. void unescapeAndAppend(kj::ArrayPtr hex, kj::Vector& target) { KJ_REQUIRE(hex.size() == 4); int codePoint = 0; for (int i = 0; i < 4; ++i) { char c = hex[i]; codePoint <<= 4; if ('0' <= c && c <= '9') { codePoint |= c - '0'; } else if ('a' <= c && c <= 'f') { codePoint |= c - 'a'; } else if ('A' <= c && c <= 'F') { codePoint |= c - 'A'; } else { KJ_FAIL_REQUIRE("Invalid hex digit in unicode escape.", c); } } if (codePoint < 128) { target.add(0x7f & static_cast(codePoint)); } else { // TODO(perf): This is sorta malloc-heavy... char16_t u = codePoint; target.addAll(kj::decodeUtf16(kj::arrayPtr(&u, 1))); } } const size_t maxNestingDepth; Input input; size_t nestingDepth; }; // class Parser } // namespace void JsonCodec::decodeRaw(kj::ArrayPtr input, JsonValue::Builder output) const { Parser parser(impl->maxNestingDepth, input); parser.parseValue(output); KJ_REQUIRE(parser.inputExhausted(), "Input remains after parsing JSON."); } // ----------------------------------------------------------------------------- Orphan JsonCodec::HandlerBase::decodeBase( const JsonCodec& codec, JsonValue::Reader input, Type type, Orphanage orphanage) const { KJ_FAIL_ASSERT("JSON decoder handler type / value type mismatch"); } void JsonCodec::HandlerBase::decodeStructBase( const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const { KJ_FAIL_ASSERT("JSON decoder handler type / value type mismatch"); } void JsonCodec::addTypeHandlerImpl(Type type, HandlerBase& handler) { impl->typeHandlers.upsert(type, &handler, [](HandlerBase*& existing, HandlerBase* replacement) { KJ_REQUIRE(existing == replacement, "type already has a different registered handler"); }); } void JsonCodec::addFieldHandlerImpl(StructSchema::Field field, Type type, HandlerBase& handler) { KJ_REQUIRE(type == field.getType(), "handler type did not match field type for addFieldHandler()"); impl->fieldHandlers.upsert(field, &handler, [](HandlerBase*& existing, HandlerBase* replacement) { KJ_REQUIRE(existing == replacement, "field already has a different registered handler"); }); } // ======================================================================================= static constexpr uint64_t JSON_NAME_ANNOTATION_ID = 0xfa5b1fd61c2e7c3dull; static constexpr uint64_t JSON_FLATTEN_ANNOTATION_ID = 0x82d3e852af0336bfull; static constexpr uint64_t JSON_DISCRIMINATOR_ANNOTATION_ID = 0xcfa794e8d19a0162ull; static constexpr uint64_t JSON_BASE64_ANNOTATION_ID = 0xd7d879450a253e4bull; static constexpr uint64_t JSON_HEX_ANNOTATION_ID = 0xf061e22f0ae5c7b5ull; class JsonCodec::Base64Handler final: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, capnp::Data::Reader input, JsonValue::Builder output) const { output.setString(kj::encodeBase64(input)); } Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const { return orphanage.newOrphanCopy(capnp::Data::Reader(kj::decodeBase64(input.getString()))); } }; class JsonCodec::HexHandler final: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, capnp::Data::Reader input, JsonValue::Builder output) const { output.setString(kj::encodeHex(input)); } Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const { return orphanage.newOrphanCopy(capnp::Data::Reader(kj::decodeHex(input.getString()))); } }; class JsonCodec::AnnotatedHandler final: public JsonCodec::Handler { public: AnnotatedHandler(JsonCodec& codec, StructSchema schema, kj::Maybe discriminator, kj::Maybe unionDeclName, kj::Vector& dependencies) : schema(schema) { auto schemaProto = schema.getProto(); auto typeName = schemaProto.getDisplayName(); if (discriminator == nullptr) { // There are two cases of unions: // * Named unions, which are special cases of named groups. In this case, the union may be // annotated by annotating the field. In this case, we receive a non-null `discriminator` // as a constructor parameter, and schemaProto.getAnnotations() must be empty because // it's not possible to annotate a group's type (because the type is anonymous). // * Unnamed unions, of which there can only be one in any particular scope. In this case, // the parent struct type itself is annotated. // So if we received `null` as the constructor parameter, check for annotations on the struct // type. for (auto anno: schemaProto.getAnnotations()) { switch (anno.getId()) { case JSON_DISCRIMINATOR_ANNOTATION_ID: discriminator = anno.getValue().getStruct().getAs(); break; } } } KJ_IF_MAYBE(d, discriminator) { if (d->hasName()) { unionTagName = d->getName(); } else { unionTagName = unionDeclName; } KJ_IF_MAYBE(u, unionTagName) { fieldsByName.insert(*u, FieldNameInfo { FieldNameInfo::UNION_TAG, 0, 0, nullptr }); } if (d->hasValueName()) { fieldsByName.insert(d->getValueName(), FieldNameInfo { FieldNameInfo::UNION_VALUE, 0, 0, nullptr }); } } discriminantOffset = schemaProto.getStruct().getDiscriminantOffset(); fields = KJ_MAP(field, schema.getFields()) { auto fieldProto = field.getProto(); auto type = field.getType(); auto fieldName = fieldProto.getName(); FieldNameInfo nameInfo; nameInfo.index = field.getIndex(); nameInfo.type = FieldNameInfo::NORMAL; nameInfo.prefixLength = 0; FieldInfo info; info.name = fieldName; kj::Maybe subDiscriminator; bool flattened = false; for (auto anno: field.getProto().getAnnotations()) { switch (anno.getId()) { case JSON_NAME_ANNOTATION_ID: info.name = anno.getValue().getText(); break; case JSON_FLATTEN_ANNOTATION_ID: KJ_REQUIRE(type.isStruct(), "only struct types can be flattened", fieldName, typeName); flattened = true; info.prefix = anno.getValue().getStruct().getAs().getPrefix(); break; case JSON_DISCRIMINATOR_ANNOTATION_ID: KJ_REQUIRE(fieldProto.isGroup(), "only unions can have discriminator"); subDiscriminator = anno.getValue().getStruct().getAs(); break; case JSON_BASE64_ANNOTATION_ID: { KJ_REQUIRE(field.getType().isData(), "only Data can be marked for base64 encoding"); static Base64Handler handler; codec.addFieldHandler(field, handler); break; } case JSON_HEX_ANNOTATION_ID: { KJ_REQUIRE(field.getType().isData(), "only Data can be marked for hex encoding"); static HexHandler handler; codec.addFieldHandler(field, handler); break; } } } if (fieldProto.isGroup()) { // Load group type handler now, even if not flattened, so that we can pass its // `subDiscriminator`. kj::Maybe subFieldName; if (flattened) { // If the group was flattened, then we allow its field name to be used as the // discriminator name, so that the discriminator doesn't have to explicitly specify a // name. subFieldName = fieldName; } auto& subHandler = codec.loadAnnotatedHandler( type.asStruct(), subDiscriminator, subFieldName, dependencies); if (flattened) { info.flattenHandler = subHandler; } } else if (type.isStruct()) { if (flattened) { info.flattenHandler = codec.loadAnnotatedHandler( type.asStruct(), nullptr, nullptr, dependencies); } } bool isUnionMember = fieldProto.getDiscriminantValue() != schema::Field::NO_DISCRIMINANT; KJ_IF_MAYBE(fh, info.flattenHandler) { // Set up fieldsByName for each of the child's fields. for (auto& entry: fh->fieldsByName) { kj::StringPtr flattenedName; kj::String ownName; if (info.prefix.size() > 0) { ownName = kj::str(info.prefix, entry.key); flattenedName = ownName; } else { flattenedName = entry.key; } fieldsByName.upsert(flattenedName, FieldNameInfo { isUnionMember ? FieldNameInfo::FLATTENED_FROM_UNION : FieldNameInfo::FLATTENED, field.getIndex(), (uint)info.prefix.size(), kj::mv(ownName) }, [&](FieldNameInfo& existing, FieldNameInfo&& replacement) { KJ_REQUIRE(existing.type == FieldNameInfo::FLATTENED_FROM_UNION && replacement.type == FieldNameInfo::FLATTENED_FROM_UNION, "flattened members have the same name and are not mutually exclusive"); }); } } info.nameForDiscriminant = info.name; if (!flattened) { bool isUnionWithValueName = false; if (isUnionMember) { KJ_IF_MAYBE(d, discriminator) { if (d->hasValueName()) { info.name = d->getValueName(); isUnionWithValueName = true; } } } if (!isUnionWithValueName) { fieldsByName.insert(info.name, kj::mv(nameInfo)); } } if (isUnionMember) { unionTagValues.insert(info.nameForDiscriminant, field); } // Look for dependencies that we need to add. while (type.isList()) type = type.asList().getElementType(); if (codec.impl->typeHandlers.find(type) == nullptr) { switch (type.which()) { case schema::Type::STRUCT: dependencies.add(type.asStruct()); break; case schema::Type::ENUM: dependencies.add(type.asEnum()); break; case schema::Type::INTERFACE: dependencies.add(type.asInterface()); break; default: break; } } return info; }; } const StructSchema schema; void encode(const JsonCodec& codec, DynamicStruct::Reader input, JsonValue::Builder output) const override { kj::Vector flattenedFields; gatherForEncode(codec, input, nullptr, nullptr, flattenedFields); auto outs = output.initObject(flattenedFields.size()); for (auto i: kj::indices(flattenedFields)) { auto& in = flattenedFields[i]; auto out = outs[i]; out.setName(in.name); KJ_SWITCH_ONEOF(in.type) { KJ_CASE_ONEOF(type, Type) { codec.encode(in.value, type, out.initValue()); } KJ_CASE_ONEOF(field, StructSchema::Field) { codec.encodeField(field, in.value, out.initValue()); } } } } void decode(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override { KJ_REQUIRE(input.isObject()); kj::HashSet unionsSeen; kj::Vector retries; for (auto field: input.getObject()) { if (!decodeField(codec, field.getName(), field.getValue(), output, unionsSeen)) { retries.add(field); } } while (!retries.empty()) { auto retriesCopy = kj::mv(retries); KJ_ASSERT(retries.empty()); for (auto field: retriesCopy) { if (!decodeField(codec, field.getName(), field.getValue(), output, unionsSeen)) { retries.add(field); } } if (retries.size() == retriesCopy.size()) { // We made no progress in this iteration. Give up on the remaining fields. break; } } } private: struct FieldInfo { kj::StringPtr name; kj::StringPtr nameForDiscriminant; kj::Maybe flattenHandler; kj::StringPtr prefix; }; kj::Array fields; // Maps field index -> info about the field struct FieldNameInfo { enum { NORMAL, // This is a normal field with the given `index`. FLATTENED, // This is a field of a flattened inner struct or group (that is not in a union). `index` // is the field index of the particular struct/group field. UNION_TAG, // The parent struct is a flattened union, and this field is the discriminant tag. It is a // string field whose name determines the union type. `index` is not used. FLATTENED_FROM_UNION, // The parent struct is a flattened union, and some of the union's members are flattened // structs or groups, and this field is possibly a member of one or more of them. `index` // is not used, because it's possible that the same field name appears in multiple variants. // Instead, the parser must find the union tag, and then can descend and attempt to parse // the field in the context of whichever variant is selected. UNION_VALUE // This field is the value of a discriminated union that has `valueName` set. } type; uint index; // For `NORMAL` and `FLATTENED`, the index of the field in schema.getFields(). uint prefixLength; kj::String ownName; }; kj::HashMap fieldsByName; // Maps JSON names to info needed to parse them. kj::HashMap unionTagValues; // If the parent struct is a flattened union, it has a tag field which is a string with one of // these values. The map maps to the union member to set. kj::Maybe unionTagName; // If the parent struct is a flattened union, the name of the "tag" field. uint discriminantOffset; // Shortcut for schema.getProto().getStruct().getDiscriminantOffset(), used in a hack to identify // which unions have been seen. struct FlattenedField { kj::String ownName; kj::StringPtr name; kj::OneOf type; DynamicValue::Reader value; FlattenedField(kj::StringPtr prefix, kj::StringPtr name, kj::OneOf type, DynamicValue::Reader value) : ownName(prefix.size() > 0 ? kj::str(prefix, name) : nullptr), name(prefix.size() > 0 ? ownName : name), type(type), value(value) {} }; void gatherForEncode(const JsonCodec& codec, DynamicValue::Reader input, kj::StringPtr prefix, kj::StringPtr morePrefix, kj::Vector& flattenedFields) const { kj::String ownPrefix; if (morePrefix.size() > 0) { if (prefix.size() > 0) { ownPrefix = kj::str(prefix, morePrefix); prefix = ownPrefix; } else { prefix = morePrefix; } } auto reader = input.as(); auto schema = reader.getSchema(); for (auto field: schema.getNonUnionFields()) { auto& info = fields[field.getIndex()]; if (!reader.has(field, codec.impl->hasMode)) { // skip } else KJ_IF_MAYBE(handler, info.flattenHandler) { handler->gatherForEncode(codec, reader.get(field), prefix, info.prefix, flattenedFields); } else { flattenedFields.add(FlattenedField { prefix, info.name, field, reader.get(field) }); } } KJ_IF_MAYBE(which, reader.which()) { auto& info = fields[which->getIndex()]; KJ_IF_MAYBE(tag, unionTagName) { flattenedFields.add(FlattenedField { prefix, *tag, Type(schema::Type::TEXT), Text::Reader(info.nameForDiscriminant) }); } KJ_IF_MAYBE(handler, info.flattenHandler) { handler->gatherForEncode(codec, reader.get(*which), prefix, info.prefix, flattenedFields); } else { auto type = which->getType(); if (type.which() == schema::Type::VOID && unionTagName != nullptr) { // When we have an explicit union discriminant, we don't need to encode void fields. } else { flattenedFields.add(FlattenedField { prefix, info.name, *which, reader.get(*which) }); } } } } bool decodeField(const JsonCodec& codec, kj::StringPtr name, JsonValue::Reader value, DynamicStruct::Builder output, kj::HashSet& unionsSeen) const { KJ_ASSERT(output.getSchema() == schema); KJ_IF_MAYBE(info, fieldsByName.find(name)) { switch (info->type) { case FieldNameInfo::NORMAL: { auto field = output.getSchema().getFields()[info->index]; codec.decodeField(field, value, Orphanage::getForMessageContaining(output), output); return true; } case FieldNameInfo::FLATTENED: return KJ_ASSERT_NONNULL(fields[info->index].flattenHandler) .decodeField(codec, name.slice(info->prefixLength), value, output.get(output.getSchema().getFields()[info->index]).as(), unionsSeen); case FieldNameInfo::UNION_TAG: { KJ_REQUIRE(value.isString(), "Expected string value."); // Mark that we've seen a union tag for this struct. const void* ptr = getUnionInstanceIdentifier(output); KJ_IF_MAYBE(field, unionTagValues.find(value.getString())) { // clear() has the side-effect of activating this member of the union, without // allocating any objects. output.clear(*field); unionsSeen.insert(ptr); } return true; } case FieldNameInfo::FLATTENED_FROM_UNION: { const void* ptr = getUnionInstanceIdentifier(output); if (unionsSeen.contains(ptr)) { auto variant = KJ_ASSERT_NONNULL(output.which()); return KJ_ASSERT_NONNULL(fields[variant.getIndex()].flattenHandler) .decodeField(codec, name.slice(info->prefixLength), value, output.get(variant).as(), unionsSeen); } else { // We haven't seen the union tag yet, so we can't parse this field yet. Try again later. return false; } } case FieldNameInfo::UNION_VALUE: { const void* ptr = getUnionInstanceIdentifier(output); if (unionsSeen.contains(ptr)) { auto variant = KJ_ASSERT_NONNULL(output.which()); codec.decodeField(variant, value, Orphanage::getForMessageContaining(output), output); return true; } else { // We haven't seen the union tag yet, so we can't parse this field yet. Try again later. return false; } } } KJ_UNREACHABLE; } else { // Ignore undefined field. return true; } } const void* getUnionInstanceIdentifier(DynamicStruct::Builder obj) const { // Gets a value uniquely identifying an instance of a union. // HACK: We return a poniter to the union's discriminant within the underlying buffer. return reinterpret_cast( AnyStruct::Reader(obj.asReader()).getDataSection().begin()) + discriminantOffset; } }; class JsonCodec::AnnotatedEnumHandler final: public JsonCodec::Handler { public: AnnotatedEnumHandler(EnumSchema schema): schema(schema) { auto enumerants = schema.getEnumerants(); auto builder = kj::heapArrayBuilder(enumerants.size()); for (auto e: enumerants) { auto proto = e.getProto(); kj::StringPtr name = proto.getName(); for (auto anno: proto.getAnnotations()) { switch (anno.getId()) { case JSON_NAME_ANNOTATION_ID: name = anno.getValue().getText(); break; } } builder.add(name); nameToValue.insert(name, e.getIndex()); } valueToName = builder.finish(); } void encode(const JsonCodec& codec, DynamicEnum input, JsonValue::Builder output) const override { KJ_IF_MAYBE(e, input.getEnumerant()) { KJ_ASSERT(e->getIndex() < valueToName.size()); output.setString(valueToName[e->getIndex()]); } else { output.setNumber(input.getRaw()); } } DynamicEnum decode(const JsonCodec& codec, JsonValue::Reader input) const override { if (input.isNumber()) { return DynamicEnum(schema, static_cast(input.getNumber())); } else { uint16_t val = KJ_REQUIRE_NONNULL(nameToValue.find(input.getString()), "invalid enum value", input.getString()); return DynamicEnum(schema.getEnumerants()[val]); } } private: EnumSchema schema; kj::Array valueToName; kj::HashMap nameToValue; }; class JsonCodec::JsonValueHandler final: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, DynamicStruct::Reader input, JsonValue::Builder output) const override { #if _MSC_VER // TODO(msvc): Hack to work around missing AnyStruct::Builder constructor on MSVC. rawCopy(input, toDynamic(output)); #else rawCopy(input, kj::mv(output)); #endif } void decode(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override { rawCopy(input, kj::mv(output)); } private: void rawCopy(AnyStruct::Reader input, AnyStruct::Builder output) const { // HACK: Manually copy using AnyStruct, so that if JsonValue's definition changes, this code // doesn't need to be updated. However, note that if JsonValue ever adds new fields that // change its size, and the input struct is a newer version than the output, we may lose // the new fields. Technically the "correct" thing to do would be to allocate the output // struct to be exactly the same size as the input, but JsonCodec's Handler interface is // not designed to allow that -- it passes in an already-allocated builder. Oops. auto dataIn = input.getDataSection(); auto dataOut = output.getDataSection(); memcpy(dataOut.begin(), dataIn.begin(), kj::min(dataOut.size(), dataIn.size())); auto ptrIn = input.getPointerSection(); auto ptrOut = output.getPointerSection(); for (auto i: kj::zeroTo(kj::min(ptrIn.size(), ptrOut.size()))) { ptrOut[i].set(ptrIn[i]); } } }; JsonCodec::AnnotatedHandler& JsonCodec::loadAnnotatedHandler( StructSchema schema, kj::Maybe discriminator, kj::Maybe unionDeclName, kj::Vector& dependencies) { auto& entry = impl->annotatedHandlers.upsert(schema, nullptr, [&](kj::Maybe>& existing, auto dummy) { KJ_ASSERT(existing != nullptr, "cyclic JSON flattening detected", schema.getProto().getDisplayName()); }); KJ_IF_MAYBE(v, entry.value) { // Already exists. return **v; } else { // Not seen before. auto newHandler = kj::heap( *this, schema, discriminator, unionDeclName, dependencies); auto& result = *newHandler; // Map may have changed, so we have to look up again. KJ_ASSERT_NONNULL(impl->annotatedHandlers.find(schema)) = kj::mv(newHandler); addTypeHandler(schema, result); return result; }; } void JsonCodec::handleByAnnotation(Schema schema) { switch (schema.getProto().which()) { case schema::Node::STRUCT: { if (schema.getProto().getId() == capnp::typeId()) { // Special handler for JsonValue. static JsonValueHandler GLOBAL_HANDLER; addTypeHandler(schema.asStruct(), GLOBAL_HANDLER); } else { kj::Vector dependencies; loadAnnotatedHandler(schema.asStruct(), nullptr, nullptr, dependencies); for (auto dep: dependencies) { handleByAnnotation(dep); } } break; } case schema::Node::ENUM: { auto enumSchema = schema.asEnum(); impl->annotatedEnumHandlers.findOrCreate(enumSchema, [&]() { auto handler = kj::heap(enumSchema); addTypeHandler(enumSchema, *handler); return kj::HashMap>::Entry { enumSchema, kj::mv(handler) }; }); break; } default: break; } } } // namespace capnp capnproto-c++-0.8.0/src/capnp/compat/json.capnp.c++0000644000175000017500000006526713650101756022512 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: json.capnp #include "json.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<137> b_a3fa7845f919dd83 = { { 0, 0, 0, 0, 5, 0, 6, 0, 131, 221, 25, 249, 69, 120, 250, 163, 24, 0, 0, 0, 1, 0, 2, 0, 52, 94, 58, 164, 151, 146, 249, 142, 1, 0, 7, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 33, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 143, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 86, 97, 108, 117, 101, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 223, 157, 214, 53, 231, 38, 16, 227, 9, 0, 0, 0, 50, 0, 0, 0, 72, 61, 201, 161, 236, 246, 217, 160, 5, 0, 0, 0, 42, 0, 0, 0, 70, 105, 101, 108, 100, 0, 0, 0, 67, 97, 108, 108, 0, 0, 0, 0, 28, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 0, 3, 0, 1, 0, 188, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 16, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 0, 0, 0, 3, 0, 1, 0, 192, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 3, 0, 1, 0, 196, 0, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 188, 0, 0, 0, 3, 0, 1, 0, 200, 0, 0, 0, 2, 0, 1, 0, 4, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 3, 0, 1, 0, 220, 0, 0, 0, 2, 0, 1, 0, 5, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 217, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 212, 0, 0, 0, 3, 0, 1, 0, 240, 0, 0, 0, 2, 0, 1, 0, 6, 0, 249, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 0, 0, 0, 3, 0, 1, 0, 244, 0, 0, 0, 2, 0, 1, 0, 110, 117, 108, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 111, 111, 108, 101, 97, 110, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 117, 109, 98, 101, 114, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 105, 110, 103, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 114, 114, 97, 121, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 131, 221, 25, 249, 69, 120, 250, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 98, 106, 101, 99, 116, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 223, 157, 214, 53, 231, 38, 16, 227, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 108, 108, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 72, 61, 201, 161, 236, 246, 217, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_a3fa7845f919dd83 = b_a3fa7845f919dd83.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_a3fa7845f919dd83[] = { &s_a0d9f6eca1c93d48, &s_a3fa7845f919dd83, &s_e31026e735d69ddf, }; static const uint16_t m_a3fa7845f919dd83[] = {4, 1, 6, 0, 2, 5, 3}; static const uint16_t i_a3fa7845f919dd83[] = {0, 1, 2, 3, 4, 5, 6}; const ::capnp::_::RawSchema s_a3fa7845f919dd83 = { 0xa3fa7845f919dd83, b_a3fa7845f919dd83.words, 137, d_a3fa7845f919dd83, m_a3fa7845f919dd83, 3, 7, i_a3fa7845f919dd83, nullptr, nullptr, { &s_a3fa7845f919dd83, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<49> b_e31026e735d69ddf = { { 0, 0, 0, 0, 5, 0, 6, 0, 223, 157, 214, 53, 231, 38, 16, 227, 30, 0, 0, 0, 1, 0, 0, 0, 131, 221, 25, 249, 69, 120, 250, 163, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 34, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 86, 97, 108, 117, 101, 46, 70, 105, 101, 108, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 131, 221, 25, 249, 69, 120, 250, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e31026e735d69ddf = b_e31026e735d69ddf.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_e31026e735d69ddf[] = { &s_a3fa7845f919dd83, }; static const uint16_t m_e31026e735d69ddf[] = {0, 1}; static const uint16_t i_e31026e735d69ddf[] = {0, 1}; const ::capnp::_::RawSchema s_e31026e735d69ddf = { 0xe31026e735d69ddf, b_e31026e735d69ddf.words, 49, d_e31026e735d69ddf, m_e31026e735d69ddf, 1, 2, i_e31026e735d69ddf, nullptr, nullptr, { &s_e31026e735d69ddf, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<54> b_a0d9f6eca1c93d48 = { { 0, 0, 0, 0, 5, 0, 6, 0, 72, 61, 201, 161, 236, 246, 217, 160, 30, 0, 0, 0, 1, 0, 0, 0, 131, 221, 25, 249, 69, 120, 250, 163, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 86, 97, 108, 117, 101, 46, 67, 97, 108, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 72, 0, 0, 0, 2, 0, 1, 0, 102, 117, 110, 99, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 131, 221, 25, 249, 69, 120, 250, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_a0d9f6eca1c93d48 = b_a0d9f6eca1c93d48.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_a0d9f6eca1c93d48[] = { &s_a3fa7845f919dd83, }; static const uint16_t m_a0d9f6eca1c93d48[] = {0, 1}; static const uint16_t i_a0d9f6eca1c93d48[] = {0, 1}; const ::capnp::_::RawSchema s_a0d9f6eca1c93d48 = { 0xa0d9f6eca1c93d48, b_a0d9f6eca1c93d48.words, 54, d_a0d9f6eca1c93d48, m_a0d9f6eca1c93d48, 1, 2, i_a0d9f6eca1c93d48, nullptr, nullptr, { &s_a0d9f6eca1c93d48, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<21> b_fa5b1fd61c2e7c3d = { { 0, 0, 0, 0, 5, 0, 6, 0, 61, 124, 46, 28, 214, 31, 91, 250, 24, 0, 0, 0, 5, 0, 232, 2, 52, 94, 58, 164, 151, 146, 249, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 234, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 110, 97, 109, 101, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_fa5b1fd61c2e7c3d = b_fa5b1fd61c2e7c3d.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_fa5b1fd61c2e7c3d = { 0xfa5b1fd61c2e7c3d, b_fa5b1fd61c2e7c3d.words, 21, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_fa5b1fd61c2e7c3d, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<21> b_82d3e852af0336bf = { { 0, 0, 0, 0, 5, 0, 6, 0, 191, 54, 3, 175, 82, 232, 211, 130, 24, 0, 0, 0, 5, 0, 224, 0, 52, 94, 58, 164, 151, 146, 249, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 2, 1, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 102, 108, 97, 116, 116, 101, 110, 0, 0, 0, 0, 0, 1, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 97, 234, 194, 123, 37, 19, 223, 196, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_82d3e852af0336bf = b_82d3e852af0336bf.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_82d3e852af0336bf = { 0x82d3e852af0336bf, b_82d3e852af0336bf.words, 21, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_82d3e852af0336bf, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<35> b_c4df13257bc2ea61 = { { 0, 0, 0, 0, 5, 0, 6, 0, 97, 234, 194, 123, 37, 19, 223, 196, 24, 0, 0, 0, 1, 0, 0, 0, 52, 94, 58, 164, 151, 146, 249, 142, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 58, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 70, 108, 97, 116, 116, 101, 110, 79, 112, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 112, 114, 101, 102, 105, 120, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c4df13257bc2ea61 = b_c4df13257bc2ea61.words; #if !CAPNP_LITE static const uint16_t m_c4df13257bc2ea61[] = {0}; static const uint16_t i_c4df13257bc2ea61[] = {0}; const ::capnp::_::RawSchema s_c4df13257bc2ea61 = { 0xc4df13257bc2ea61, b_c4df13257bc2ea61.words, 35, nullptr, m_c4df13257bc2ea61, 0, 1, i_c4df13257bc2ea61, nullptr, nullptr, { &s_c4df13257bc2ea61, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<22> b_cfa794e8d19a0162 = { { 0, 0, 0, 0, 5, 0, 6, 0, 98, 1, 154, 209, 232, 148, 167, 207, 24, 0, 0, 0, 5, 0, 80, 0, 52, 94, 58, 164, 151, 146, 249, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 50, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 100, 105, 115, 99, 114, 105, 109, 105, 110, 97, 116, 111, 114, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 25, 83, 62, 41, 12, 194, 248, 194, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_cfa794e8d19a0162 = b_cfa794e8d19a0162.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_cfa794e8d19a0162 = { 0xcfa794e8d19a0162, b_cfa794e8d19a0162.words, 22, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_cfa794e8d19a0162, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<51> b_c2f8c20c293e5319 = { { 0, 0, 0, 0, 5, 0, 6, 0, 25, 83, 62, 41, 12, 194, 248, 194, 24, 0, 0, 0, 1, 0, 0, 0, 52, 94, 58, 164, 151, 146, 249, 142, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 106, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 68, 105, 115, 99, 114, 105, 109, 105, 110, 97, 116, 111, 114, 79, 112, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 56, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 78, 97, 109, 101, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c2f8c20c293e5319 = b_c2f8c20c293e5319.words; #if !CAPNP_LITE static const uint16_t m_c2f8c20c293e5319[] = {0, 1}; static const uint16_t i_c2f8c20c293e5319[] = {0, 1}; const ::capnp::_::RawSchema s_c2f8c20c293e5319 = { 0xc2f8c20c293e5319, b_c2f8c20c293e5319.words, 51, nullptr, m_c2f8c20c293e5319, 0, 2, i_c2f8c20c293e5319, nullptr, nullptr, { &s_c2f8c20c293e5319, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<21> b_d7d879450a253e4b = { { 0, 0, 0, 0, 5, 0, 6, 0, 75, 62, 37, 10, 69, 121, 216, 215, 24, 0, 0, 0, 5, 0, 32, 0, 52, 94, 58, 164, 151, 146, 249, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 98, 97, 115, 101, 54, 52, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d7d879450a253e4b = b_d7d879450a253e4b.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_d7d879450a253e4b = { 0xd7d879450a253e4b, b_d7d879450a253e4b.words, 21, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_d7d879450a253e4b, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<21> b_f061e22f0ae5c7b5 = { { 0, 0, 0, 0, 5, 0, 6, 0, 181, 199, 229, 10, 47, 226, 97, 240, 24, 0, 0, 0, 5, 0, 32, 0, 52, 94, 58, 164, 151, 146, 249, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 226, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 104, 101, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f061e22f0ae5c7b5 = b_f061e22f0ae5c7b5.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_f061e22f0ae5c7b5 = { 0xf061e22f0ae5c7b5, b_f061e22f0ae5c7b5.words, 21, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_f061e22f0ae5c7b5, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<22> b_a0a054dea32fd98c = { { 0, 0, 0, 0, 5, 0, 6, 0, 140, 217, 47, 163, 222, 84, 160, 160, 24, 0, 0, 0, 5, 0, 0, 2, 52, 94, 58, 164, 151, 146, 249, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 42, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 97, 116, 47, 106, 115, 111, 110, 46, 99, 97, 112, 110, 112, 58, 110, 111, 116, 105, 102, 105, 99, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_a0a054dea32fd98c = b_a0a054dea32fd98c.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_a0a054dea32fd98c = { 0xa0a054dea32fd98c, b_a0a054dea32fd98c.words, 22, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_a0a054dea32fd98c, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { namespace json { // Value constexpr uint16_t Value::_capnpPrivate::dataWordSize; constexpr uint16_t Value::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Value::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Value::_capnpPrivate::schema; #endif // !CAPNP_LITE // Value::Field constexpr uint16_t Value::Field::_capnpPrivate::dataWordSize; constexpr uint16_t Value::Field::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Value::Field::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Value::Field::_capnpPrivate::schema; #endif // !CAPNP_LITE // Value::Call constexpr uint16_t Value::Call::_capnpPrivate::dataWordSize; constexpr uint16_t Value::Call::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Value::Call::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Value::Call::_capnpPrivate::schema; #endif // !CAPNP_LITE // FlattenOptions constexpr uint16_t FlattenOptions::_capnpPrivate::dataWordSize; constexpr uint16_t FlattenOptions::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind FlattenOptions::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* FlattenOptions::_capnpPrivate::schema; #endif // !CAPNP_LITE // DiscriminatorOptions constexpr uint16_t DiscriminatorOptions::_capnpPrivate::dataWordSize; constexpr uint16_t DiscriminatorOptions::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind DiscriminatorOptions::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* DiscriminatorOptions::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/compat/json-test.c++0000644000175000017500000011672313650101756022361 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "json.h" #include #include #include #include #include #include namespace capnp { namespace _ { // private namespace { KJ_TEST("basic json encoding") { JsonCodec json; KJ_EXPECT(json.encode(VOID) == "null"); KJ_EXPECT(json.encode(true) == "true"); KJ_EXPECT(json.encode(false) == "false"); KJ_EXPECT(json.encode(123) == "123"); KJ_EXPECT(json.encode(-5.5) == "-5.5"); KJ_EXPECT(json.encode(Text::Reader("foo")) == "\"foo\""); KJ_EXPECT(json.encode(Text::Reader("ab\"cd\\ef\x03")) == "\"ab\\\"cd\\\\ef\\u0003\""); KJ_EXPECT(json.encode(test::TestEnum::CORGE) == "\"corge\""); byte bytes[] = {12, 34, 56}; KJ_EXPECT(json.encode(Data::Reader(bytes, 3)) == "[12,34,56]"); json.setPrettyPrint(true); KJ_EXPECT(json.encode(Data::Reader(bytes, 3)) == "[12, 34, 56]"); } const char ALL_TYPES_JSON[] = "{ \"voidField\": null,\n" " \"boolField\": true,\n" " \"int8Field\": -123,\n" " \"int16Field\": -12345,\n" " \"int32Field\": -12345678,\n" " \"int64Field\": \"-123456789012345\",\n" " \"uInt8Field\": 234,\n" " \"uInt16Field\": 45678,\n" " \"uInt32Field\": 3456789012,\n" " \"uInt64Field\": \"12345678901234567890\",\n" " \"float32Field\": 1234.5,\n" " \"float64Field\": -1.23e47,\n" " \"textField\": \"foo\",\n" " \"dataField\": [98, 97, 114],\n" " \"structField\": {\n" " \"voidField\": null,\n" " \"boolField\": true,\n" " \"int8Field\": -12,\n" " \"int16Field\": 3456,\n" " \"int32Field\": -78901234,\n" " \"int64Field\": \"56789012345678\",\n" " \"uInt8Field\": 90,\n" " \"uInt16Field\": 1234,\n" " \"uInt32Field\": 56789012,\n" " \"uInt64Field\": \"345678901234567890\",\n" " \"float32Field\": -1.2499999646475857e-10,\n" " \"float64Field\": 345,\n" " \"textField\": \"baz\",\n" " \"dataField\": [113, 117, 120],\n" " \"structField\": {\n" " \"voidField\": null,\n" " \"boolField\": false,\n" " \"int8Field\": 0,\n" " \"int16Field\": 0,\n" " \"int32Field\": 0,\n" " \"int64Field\": \"0\",\n" " \"uInt8Field\": 0,\n" " \"uInt16Field\": 0,\n" " \"uInt32Field\": 0,\n" " \"uInt64Field\": \"0\",\n" " \"float32Field\": 0,\n" " \"float64Field\": 0,\n" " \"textField\": \"nested\",\n" " \"structField\": {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"really nested\", \"enumField\": \"foo\", \"interfaceField\": null},\n" " \"enumField\": \"foo\",\n" " \"interfaceField\": null },\n" " \"enumField\": \"baz\",\n" " \"interfaceField\": null,\n" " \"voidList\": [null, null, null],\n" " \"boolList\": [false, true, false, true, true],\n" " \"int8List\": [12, -34, -128, 127],\n" " \"int16List\": [1234, -5678, -32768, 32767],\n" " \"int32List\": [12345678, -90123456, -2147483648, 2147483647],\n" " \"int64List\": [\"123456789012345\", \"-678901234567890\", \"-9223372036854775808\", \"9223372036854775807\"],\n" " \"uInt8List\": [12, 34, 0, 255],\n" " \"uInt16List\": [1234, 5678, 0, 65535],\n" " \"uInt32List\": [12345678, 90123456, 0, 4294967295],\n" " \"uInt64List\": [\"123456789012345\", \"678901234567890\", \"0\", \"18446744073709551615\"],\n" " \"float32List\": [0, 1234567, 9.9999999338158125e36, -9.9999999338158125e36, 9.99999991097579e-38, -9.99999991097579e-38],\n" " \"float64List\": [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306],\n" " \"textList\": [\"quux\", \"corge\", \"grault\"],\n" " \"dataList\": [[103, 97, 114, 112, 108, 121], [119, 97, 108, 100, 111], [102, 114, 101, 100]],\n" " \"structList\": [\n" " {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"x structlist 1\", \"enumField\": \"foo\", \"interfaceField\": null},\n" " {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"x structlist 2\", \"enumField\": \"foo\", \"interfaceField\": null},\n" " {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"x structlist 3\", \"enumField\": \"foo\", \"interfaceField\": null} ],\n" " \"enumList\": [\"qux\", \"bar\", \"grault\"] },\n" " \"enumField\": \"corge\",\n" " \"interfaceField\": null,\n" " \"voidList\": [null, null, null, null, null, null],\n" " \"boolList\": [true, false, false, true],\n" " \"int8List\": [111, -111],\n" " \"int16List\": [11111, -11111],\n" " \"int32List\": [111111111, -111111111],\n" " \"int64List\": [\"1111111111111111111\", \"-1111111111111111111\"],\n" " \"uInt8List\": [111, 222],\n" " \"uInt16List\": [33333, 44444],\n" " \"uInt32List\": [3333333333],\n" " \"uInt64List\": [\"11111111111111111111\"],\n" " \"float32List\": [5555.5, \"Infinity\", \"-Infinity\", \"NaN\"],\n" " \"float64List\": [7777.75, \"Infinity\", \"-Infinity\", \"NaN\"],\n" " \"textList\": [\"plugh\", \"xyzzy\", \"thud\"],\n" " \"dataList\": [[111, 111, 112, 115], [101, 120, 104, 97, 117, 115, 116, 101, 100], [114, 102, 99, 51, 48, 57, 50]],\n" " \"structList\": [\n" " {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"structlist 1\", \"enumField\": \"foo\", \"interfaceField\": null},\n" " {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"structlist 2\", \"enumField\": \"foo\", \"interfaceField\": null},\n" " {\"voidField\": null, \"boolField\": false, \"int8Field\": 0, \"int16Field\": 0, \"int32Field\": 0, \"int64Field\": \"0\", \"uInt8Field\": 0, \"uInt16Field\": 0, \"uInt32Field\": 0, \"uInt64Field\": \"0\", \"float32Field\": 0, \"float64Field\": 0, \"textField\": \"structlist 3\", \"enumField\": \"foo\", \"interfaceField\": null} ],\n" " \"enumList\": [\"foo\", \"garply\"] }"; KJ_TEST("encode all types") { MallocMessageBuilder message; auto root = message.getRoot(); initTestMessage(root); JsonCodec json; json.setPrettyPrint(true); KJ_EXPECT(json.encode(root) == ALL_TYPES_JSON); // Verify that if we strip out the non-string spaces, we get the non-pretty-print version. kj::Vector chars; bool inQuotes = false; for (char c: ALL_TYPES_JSON) { if (c == '\"') inQuotes = !inQuotes; if ((c == '\n' || c == ' ') && !inQuotes) { // skip space } else { chars.add(c); } } kj::String nospaces(chars.releaseAsArray()); json.setPrettyPrint(false); KJ_EXPECT(json.encode(root) == nospaces); } KJ_TEST("encode union") { MallocMessageBuilder message; auto root = message.getRoot(); root.setBefore("a"); root.setMiddle(44); root.setAfter("c"); JsonCodec json; root.setFoo(123); KJ_EXPECT(json.encode(root) == "{\"before\":\"a\",\"foo\":123,\"middle\":44,\"after\":\"c\"}"); root.setBar(321); KJ_EXPECT(json.encode(root) == "{\"before\":\"a\",\"middle\":44,\"bar\":321,\"after\":\"c\"}"); } KJ_TEST("decode all types") { JsonCodec json; json.setHasMode(HasMode::NON_DEFAULT); #define CASE_MAYBE_ROUNDTRIP(s, f, roundtrip) \ { \ MallocMessageBuilder message; \ auto root = message.initRoot(); \ kj::StringPtr input = s; \ json.decode(input, root); \ KJ_EXPECT((f), input, root); \ auto reencoded = json.encode(root); \ KJ_EXPECT(roundtrip == (input == reencoded), roundtrip, input, reencoded); \ } #define CASE_NO_ROUNDTRIP(s, f) CASE_MAYBE_ROUNDTRIP(s, f, false) #define CASE(s, f) CASE_MAYBE_ROUNDTRIP(s, f, true) #define CASE_THROW(s, errorMessage) \ { \ MallocMessageBuilder message; \ auto root = message.initRoot(); \ KJ_EXPECT_THROW_MESSAGE(errorMessage, json.decode(s, root)); \ } #define CASE_THROW_RECOVERABLE(s, errorMessage) \ { \ MallocMessageBuilder message; \ auto root = message.initRoot(); \ KJ_EXPECT_THROW_RECOVERABLE_MESSAGE(errorMessage, json.decode(s, root)); \ } CASE(R"({})", root.getBoolField() == false); CASE_NO_ROUNDTRIP(R"({"unknownField":7})", root.getBoolField() == false); CASE(R"({"boolField":true})", root.getBoolField() == true); CASE(R"({"int8Field":-128})", root.getInt8Field() == -128); CASE_NO_ROUNDTRIP(R"({"int8Field":"127"})", root.getInt8Field() == 127); CASE_THROW_RECOVERABLE(R"({"int8Field":"-129"})", "Value out-of-range"); CASE_THROW_RECOVERABLE(R"({"int8Field":128})", "Value out-of-range"); CASE(R"({"int16Field":-32768})", root.getInt16Field() == -32768); CASE_NO_ROUNDTRIP(R"({"int16Field":"32767"})", root.getInt16Field() == 32767); CASE_THROW_RECOVERABLE(R"({"int16Field":"-32769"})", "Value out-of-range"); CASE_THROW_RECOVERABLE(R"({"int16Field":32768})", "Value out-of-range"); CASE(R"({"int32Field":-2147483648})", root.getInt32Field() == -2147483648); CASE_NO_ROUNDTRIP(R"({"int32Field":"2147483647"})", root.getInt32Field() == 2147483647); CASE_NO_ROUNDTRIP(R"({"int64Field":-9007199254740992})", root.getInt64Field() == -9007199254740992LL); CASE_NO_ROUNDTRIP(R"({"int64Field":9007199254740991})", root.getInt64Field() == 9007199254740991LL); CASE(R"({"int64Field":"-9223372036854775808"})", root.getInt64Field() == -9223372036854775808ULL); CASE(R"({"int64Field":"9223372036854775807"})", root.getInt64Field() == 9223372036854775807LL); CASE_THROW_RECOVERABLE(R"({"int64Field":"-9223372036854775809"})", "Value out-of-range"); CASE_THROW_RECOVERABLE(R"({"int64Field":"9223372036854775808"})", "Value out-of-range"); CASE(R"({"uInt8Field":255})", root.getUInt8Field() == 255); CASE_NO_ROUNDTRIP(R"({"uInt8Field":"0"})", root.getUInt8Field() == 0); CASE_THROW_RECOVERABLE(R"({"uInt8Field":"256"})", "Value out-of-range"); CASE_THROW_RECOVERABLE(R"({"uInt8Field":-1})", "Value out-of-range"); CASE(R"({"uInt16Field":65535})", root.getUInt16Field() == 65535); CASE_NO_ROUNDTRIP(R"({"uInt16Field":"0"})", root.getUInt16Field() == 0); CASE_THROW_RECOVERABLE(R"({"uInt16Field":"655356"})", "Value out-of-range"); CASE_THROW_RECOVERABLE(R"({"uInt16Field":-1})", "Value out-of-range"); CASE(R"({"uInt32Field":4294967295})", root.getUInt32Field() == 4294967295); CASE_NO_ROUNDTRIP(R"({"uInt32Field":"0"})", root.getUInt32Field() == 0); CASE_THROW_RECOVERABLE(R"({"uInt32Field":"42949672956"})", "Value out-of-range"); CASE_THROW_RECOVERABLE(R"({"uInt32Field":-1})", "Value out-of-range"); CASE_NO_ROUNDTRIP(R"({"uInt64Field":9007199254740991})", root.getUInt64Field() == 9007199254740991ULL); CASE(R"({"uInt64Field":"18446744073709551615"})", root.getUInt64Field() == 18446744073709551615ULL); CASE_NO_ROUNDTRIP(R"({"uInt64Field":"0"})", root.getUInt64Field() == 0); CASE_THROW_RECOVERABLE(R"({"uInt64Field":"18446744073709551616"})", "Value out-of-range"); CASE_NO_ROUNDTRIP(R"({"float32Field":0})", root.getFloat32Field() == 0); CASE(R"({"float32Field":4.5})", root.getFloat32Field() == 4.5); CASE_NO_ROUNDTRIP(R"({"float32Field":null})", kj::isNaN(root.getFloat32Field())); CASE(R"({"float32Field":"NaN"})", kj::isNaN(root.getFloat32Field())); CASE_NO_ROUNDTRIP(R"({"float32Field":"nan"})", kj::isNaN(root.getFloat32Field())); CASE(R"({"float32Field":"Infinity"})", root.getFloat32Field() == kj::inf()); CASE(R"({"float32Field":"-Infinity"})", root.getFloat32Field() == -kj::inf()); CASE_NO_ROUNDTRIP(R"({"float32Field":"infinity"})", root.getFloat32Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float32Field":"-infinity"})", root.getFloat32Field() == -kj::inf()); CASE_NO_ROUNDTRIP(R"({"float32Field":"INF"})", root.getFloat32Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float32Field":"-INF"})", root.getFloat32Field() == -kj::inf()); CASE_NO_ROUNDTRIP(R"({"float32Field":1e39})", root.getFloat32Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float32Field":-1e39})", root.getFloat32Field() == -kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":0})", root.getFloat64Field() == 0); CASE(R"({"float64Field":4.5})", root.getFloat64Field() == 4.5); CASE_NO_ROUNDTRIP(R"({"float64Field":null})", kj::isNaN(root.getFloat64Field())); CASE(R"({"float64Field":"NaN"})", kj::isNaN(root.getFloat64Field())); CASE_NO_ROUNDTRIP(R"({"float64Field":"nan"})", kj::isNaN(root.getFloat64Field())); CASE(R"({"float64Field":"Infinity"})", root.getFloat64Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":"infinity"})", root.getFloat64Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":"-infinity"})", root.getFloat64Field() == -kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":"INF"})", root.getFloat64Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":"-INF"})", root.getFloat64Field() == -kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":1e309})", root.getFloat64Field() == kj::inf()); CASE_NO_ROUNDTRIP(R"({"float64Field":-1e309})", root.getFloat64Field() == -kj::inf()); CASE(R"({"textField":"hello"})", kj::str("hello") == root.getTextField()); CASE(R"({"dataField":[7,0,122]})", kj::heapArray({7,0,122}).asPtr() == root.getDataField()); CASE(R"({"structField":{}})", root.hasStructField() == true); CASE(R"({"structField":{}})", root.getStructField().getBoolField() == false); CASE_NO_ROUNDTRIP(R"({"structField":{"boolField":false}})", root.getStructField().getBoolField() == false); CASE(R"({"structField":{"boolField":true}})", root.getStructField().getBoolField() == true); CASE(R"({"enumField":"bar"})", root.getEnumField() == TestEnum::BAR); CASE_NO_ROUNDTRIP(R"({"textField":"foo\u1234bar"})", kj::str(u8"foo\u1234bar") == root.getTextField()); CASE_THROW_RECOVERABLE(R"({"structField":null})", "Expected object value"); CASE_THROW_RECOVERABLE(R"({"structList":null})", "Expected list value"); CASE_THROW_RECOVERABLE(R"({"boolList":null})", "Expected list value"); CASE_THROW_RECOVERABLE(R"({"structList":[null]})", "Expected object value"); CASE_THROW_RECOVERABLE(R"({"int64Field":"177a"})", "String does not contain valid"); CASE_THROW_RECOVERABLE(R"({"uInt64Field":"177a"})", "String does not contain valid"); CASE_THROW_RECOVERABLE(R"({"float64Field":"177a"})", "String does not contain valid"); CASE(R"({})", root.hasBoolList() == false); CASE(R"({"boolList":[]})", root.hasBoolList() == true); CASE(R"({"boolList":[]})", root.getBoolList().size() == 0); CASE(R"({"boolList":[false]})", root.getBoolList().size() == 1); CASE(R"({"boolList":[false]})", root.getBoolList()[0] == false); CASE(R"({"boolList":[true]})", root.getBoolList()[0] == true); CASE(R"({"int8List":[7]})", root.getInt8List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"int8List":["7"]})", root.getInt8List()[0] == 7); CASE(R"({"int16List":[7]})", root.getInt16List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"int16List":["7"]})", root.getInt16List()[0] == 7); CASE(R"({"int32List":[7]})", root.getInt32List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"int32List":["7"]})", root.getInt32List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"int64List":[7]})", root.getInt64List()[0] == 7); CASE(R"({"int64List":["7"]})", root.getInt64List()[0] == 7); CASE(R"({"uInt8List":[7]})", root.getUInt8List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"uInt8List":["7"]})", root.getUInt8List()[0] == 7); CASE(R"({"uInt16List":[7]})", root.getUInt16List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"uInt16List":["7"]})", root.getUInt16List()[0] == 7); CASE(R"({"uInt32List":[7]})", root.getUInt32List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"uInt32List":["7"]})", root.getUInt32List()[0] == 7); CASE_NO_ROUNDTRIP(R"({"uInt64List":[7]})", root.getUInt64List()[0] == 7); CASE(R"({"uInt64List":["7"]})", root.getUInt64List()[0] == 7); CASE(R"({"float32List":[4.5]})", root.getFloat32List()[0] == 4.5); CASE_NO_ROUNDTRIP(R"({"float32List":["4.5"]})", root.getFloat32List()[0] == 4.5); CASE_NO_ROUNDTRIP(R"({"float32List":[null]})", kj::isNaN(root.getFloat32List()[0])); CASE(R"({"float32List":["NaN"]})", kj::isNaN(root.getFloat32List()[0])); CASE(R"({"float32List":["Infinity"]})", root.getFloat32List()[0] == kj::inf()); CASE(R"({"float32List":["-Infinity"]})", root.getFloat32List()[0] == -kj::inf()); CASE(R"({"float64List":[4.5]})", root.getFloat64List()[0] == 4.5); CASE_NO_ROUNDTRIP(R"({"float64List":["4.5"]})", root.getFloat64List()[0] == 4.5); CASE_NO_ROUNDTRIP(R"({"float64List":[null]})", kj::isNaN(root.getFloat64List()[0])); CASE(R"({"float64List":["NaN"]})", kj::isNaN(root.getFloat64List()[0])); CASE(R"({"float64List":["Infinity"]})", root.getFloat64List()[0] == kj::inf()); CASE(R"({"float64List":["-Infinity"]})", root.getFloat64List()[0] == -kj::inf()); CASE(R"({"textList":["hello"]})", kj::str("hello") == root.getTextList()[0]); CASE(R"({"dataList":[[7,0,122]]})", kj::heapArray({7,0,122}).asPtr() == root.getDataList()[0]); CASE(R"({"structList":[{}]})", root.hasStructList() == true); CASE(R"({"structList":[{}]})", root.getStructList()[0].getBoolField() == false); CASE_NO_ROUNDTRIP(R"({"structList":[{"boolField":false}]})", root.getStructList()[0].getBoolField() == false); CASE(R"({"structList":[{"boolField":true}]})", root.getStructList()[0].getBoolField() == true); CASE(R"({"enumList":["bar"]})", root.getEnumList()[0] == TestEnum::BAR); #undef CASE_MAYBE_ROUNDTRIP #undef CASE_NO_ROUNDTRIP #undef CASE #undef CASE_THROW #undef CASE_THROW_RECOVERABLE } KJ_TEST("decode test message") { MallocMessageBuilder message; auto root = message.getRoot(); initTestMessage(root); JsonCodec json; auto encoded = json.encode(root); MallocMessageBuilder decodedMessage; auto decodedRoot = decodedMessage.initRoot(); json.decode(encoded, decodedRoot); KJ_EXPECT(root.toString().flatten() == decodedRoot.toString().flatten()); } KJ_TEST("basic json decoding") { // TODO(cleanup): this test is a mess! JsonCodec json; { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("null", root); KJ_EXPECT(root.which() == JsonValue::NULL_); KJ_EXPECT(root.getNull() == VOID); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("false", root); KJ_EXPECT(root.which() == JsonValue::BOOLEAN); KJ_EXPECT(root.getBoolean() == false); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("true", root); KJ_EXPECT(root.which() == JsonValue::BOOLEAN); KJ_EXPECT(root.getBoolean() == true); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("\"foo\"", root); KJ_EXPECT(root.which() == JsonValue::STRING); KJ_EXPECT(kj::str("foo") == root.getString()); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(R"("\"")", root); KJ_EXPECT(root.which() == JsonValue::STRING); KJ_EXPECT(kj::str("\"") == root.getString()); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(R"("\\abc\"d\\e")", root); KJ_EXPECT(root.which() == JsonValue::STRING); KJ_EXPECT(kj::str("\\abc\"d\\e") == root.getString()); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(R"("\"\\\/\b\f\n\r\t\u0003abc\u0064\u0065f")", root); KJ_EXPECT(root.which() == JsonValue::STRING); KJ_EXPECT(kj::str("\"\\/\b\f\n\r\t\x03""abcdef") == root.getString(), root.getString()); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("[]", root); KJ_EXPECT(root.which() == JsonValue::ARRAY, (uint)root.which()); KJ_EXPECT(root.getArray().size() == 0); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("[true]", root); KJ_EXPECT(root.which() == JsonValue::ARRAY); auto array = root.getArray(); KJ_EXPECT(array.size() == 1, array.size()); KJ_EXPECT(root.getArray()[0].which() == JsonValue::BOOLEAN); KJ_EXPECT(root.getArray()[0].getBoolean() == true); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(" [ true , false\t\n , null]", root); KJ_EXPECT(root.which() == JsonValue::ARRAY); auto array = root.getArray(); KJ_EXPECT(array.size() == 3); KJ_EXPECT(array[0].which() == JsonValue::BOOLEAN); KJ_EXPECT(array[0].getBoolean() == true); KJ_EXPECT(array[1].which() == JsonValue::BOOLEAN); KJ_EXPECT(array[1].getBoolean() == false); KJ_EXPECT(array[2].which() == JsonValue::NULL_); KJ_EXPECT(array[2].getNull() == VOID); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("{}", root); KJ_EXPECT(root.which() == JsonValue::OBJECT, (uint)root.which()); KJ_EXPECT(root.getObject().size() == 0); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("\r\n\t {\r\n\t }\r\n\t ", root); KJ_EXPECT(root.which() == JsonValue::OBJECT, (uint)root.which()); KJ_EXPECT(root.getObject().size() == 0); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(R"({"some": null})", root); KJ_EXPECT(root.which() == JsonValue::OBJECT, (uint)root.which()); auto object = root.getObject(); KJ_EXPECT(object.size() == 1); KJ_EXPECT(kj::str("some") == object[0].getName()); KJ_EXPECT(object[0].getValue().which() == JsonValue::NULL_); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(R"({"foo\n\tbaz": "a val", "bar": ["a", -5.5e11, { "z": {}}]})", root); KJ_EXPECT(root.which() == JsonValue::OBJECT, (uint)root.which()); auto object = root.getObject(); KJ_EXPECT(object.size() == 2); KJ_EXPECT(kj::str("foo\n\tbaz") == object[0].getName()); KJ_EXPECT(object[0].getValue().which() == JsonValue::STRING); KJ_EXPECT(kj::str("a val") == object[0].getValue().getString()); KJ_EXPECT(kj::str("bar") == object[1].getName()); KJ_EXPECT(object[1].getValue().which() == JsonValue::ARRAY); auto array = object[1].getValue().getArray(); KJ_EXPECT(array.size() == 3, array.size()); KJ_EXPECT(array[0].which() == JsonValue::STRING); KJ_EXPECT(kj::str("a") == array[0].getString()); KJ_EXPECT(array[1].which() == JsonValue::NUMBER); KJ_EXPECT(array[1].getNumber() == -5.5e11); KJ_EXPECT(array[2].which() == JsonValue::OBJECT); KJ_EXPECT(array[2].getObject().size() == 1); KJ_EXPECT(array[2].getObject()[0].getValue().which() == JsonValue::OBJECT); KJ_EXPECT(array[2].getObject()[0].getValue().getObject().size() == 0); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("123", root); KJ_EXPECT(root.which() == JsonValue::NUMBER); KJ_EXPECT(root.getNumber() == 123); } { MallocMessageBuilder message; auto root = message.initRoot(); KJ_EXPECT_THROW_MESSAGE("input", json.decodeRaw("z", root)); } { MallocMessageBuilder message; auto root = message.initRoot(); // Leading + not allowed in numbers. KJ_EXPECT_THROW_MESSAGE("Unexpected", json.decodeRaw("+123", root)); } { MallocMessageBuilder message; auto root = message.initRoot(); KJ_EXPECT_THROW_MESSAGE("Unexpected", json.decodeRaw("[00]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected", json.decodeRaw("[01]", root)); } { MallocMessageBuilder message; auto root = message.initRoot(); KJ_EXPECT_THROW_MESSAGE("ends prematurely", json.decodeRaw("-", root)); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("-5", root); KJ_EXPECT(root.which() == JsonValue::NUMBER); KJ_EXPECT(root.getNumber() == -5); } { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw("-5.5", root); KJ_EXPECT(root.which() == JsonValue::NUMBER); KJ_EXPECT(root.getNumber() == -5.5); } { MallocMessageBuilder message; auto root = message.initRoot(); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("a", root)); KJ_EXPECT_THROW_MESSAGE("ends prematurely", json.decodeRaw("[", root)); KJ_EXPECT_THROW_MESSAGE("ends prematurely", json.decodeRaw("{", root)); KJ_EXPECT_THROW_MESSAGE("ends prematurely", json.decodeRaw("\"\\u\"", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[}", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("{]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[}]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[1, , ]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[,]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[true,]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[, 1]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[1\"\"]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("[1,, \"\"]", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("{\"a\"1: 0}", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw(R"({"some": null,})", root)); KJ_EXPECT_THROW_MESSAGE("Input remains", json.decodeRaw("11a", root)); KJ_EXPECT_THROW_MESSAGE("Invalid escape", json.decodeRaw(R"("\z")", root)); KJ_EXPECT_THROW_MESSAGE("Invalid escape", json.decodeRaw(R"("\z")", root)); { // TODO(msvc): This raw string literal currently confuses MSVC's preprocessor, so I hoisted // it outside the macro. auto f = [&] { json.decodeRaw(R"(["\n\", 3])", root); }; KJ_EXPECT_THROW_MESSAGE("ends prematurely", f()); } KJ_EXPECT_THROW_MESSAGE("Invalid hex", json.decodeRaw(R"("\u12zz")", root)); KJ_EXPECT_THROW_MESSAGE("ends prematurely", json.decodeRaw("-", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("--", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("\f{}", root)); KJ_EXPECT_THROW_MESSAGE("Unexpected input", json.decodeRaw("{\v}", root)); } } KJ_TEST("maximum nesting depth") { JsonCodec json; auto input = kj::str(R"({"foo": "a", "bar": ["b", { "baz": [-5.5e11] }, [ [ 1 ], { "z": 2 }]]})"); // `input` has a maximum nesting depth of 4, reached 3 times. { MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(input, root); } { json.setMaxNestingDepth(0); MallocMessageBuilder message; auto root = message.initRoot(); KJ_EXPECT_THROW_MESSAGE("nest", json.decodeRaw(input, root)); } { json.setMaxNestingDepth(3); MallocMessageBuilder message; auto root = message.initRoot(); KJ_EXPECT_THROW_MESSAGE("nest", json.decodeRaw(input, root)); } { json.setMaxNestingDepth(4); MallocMessageBuilder message; auto root = message.initRoot(); json.decodeRaw(input, root); } } class TestCallHandler: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, Text::Reader input, JsonValue::Builder output) const override { auto call = output.initCall(); call.setFunction("Frob"); auto params = call.initParams(2); params[0].setNumber(123); params[1].setString(input); } Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const override { KJ_UNIMPLEMENTED("TestHandler::decode"); } }; class TestDynamicStructHandler: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, DynamicStruct::Reader input, JsonValue::Builder output) const override { auto fields = input.getSchema().getFields(); auto items = output.initArray(fields.size()); for (auto field: fields) { KJ_REQUIRE(field.getIndex() < items.size()); auto item = items[field.getIndex()]; if (input.has(field)) { codec.encode(input.get(field), field.getType(), item); } else { item.setNull(); } } } void decode(const JsonCodec& codec, JsonValue::Reader input, DynamicStruct::Builder output) const override { auto orphanage = Orphanage::getForMessageContaining(output); auto fields = output.getSchema().getFields(); auto items = input.getArray(); for (auto field: fields) { KJ_REQUIRE(field.getIndex() < items.size()); auto item = items[field.getIndex()]; if (!item.isNull()) { output.adopt(field, codec.decode(item, field.getType(), orphanage)); } } } }; class TestStructHandler: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, test::TestOldVersion::Reader input, JsonValue::Builder output) const override { dynamicHandler.encode(codec, input, output); } void decode(const JsonCodec& codec, JsonValue::Reader input, test::TestOldVersion::Builder output) const override { dynamicHandler.decode(codec, input, output); } private: TestDynamicStructHandler dynamicHandler; }; KJ_TEST("register custom encoding handlers") { JsonCodec json; TestStructHandler structHandler; json.addTypeHandler(structHandler); // JSON decoder can't parse calls back, so test only encoder here TestCallHandler callHandler; json.addTypeHandler(callHandler); MallocMessageBuilder message; auto root = message.getRoot(); root.setOld1(123); root.setOld2("foo"); KJ_EXPECT(json.encode(root) == "[\"123\",Frob(123,\"foo\"),null]"); } KJ_TEST("register custom roundtrip handler") { for (auto i = 1; i <= 2; i++) { JsonCodec json; TestStructHandler staticHandler; TestDynamicStructHandler dynamicHandler; kj::String encoded; if (i == 1) { // first iteration: test with explicit struct handler json.addTypeHandler(staticHandler); } else { // second iteration: same checks, but with DynamicStruct handler json.addTypeHandler(StructSchema::from(), dynamicHandler); } { MallocMessageBuilder message; auto root = message.getRoot(); root.setOld1(123); root.initOld3().setOld2("foo"); encoded = json.encode(root); KJ_EXPECT(encoded == "[\"123\",null,[\"0\",\"foo\",null]]"); } { MallocMessageBuilder message; auto root = message.getRoot(); json.decode(encoded, root); KJ_EXPECT(root.getOld1() == 123); KJ_EXPECT(!root.hasOld2()); auto nested = root.getOld3(); KJ_EXPECT(nested.getOld1() == 0); KJ_EXPECT("foo" == nested.getOld2()); KJ_EXPECT(!nested.hasOld3()); } } } KJ_TEST("register field handler") { TestStructHandler handler; JsonCodec json; json.addFieldHandler(StructSchema::from().getFieldByName("old3"), handler); kj::String encoded; { MallocMessageBuilder message; auto root = message.getRoot(); root.setOld1(123); root.setOld2("foo"); auto nested = root.initOld3(); nested.setOld2("bar"); encoded = json.encode(root); KJ_EXPECT(encoded == "{\"old1\":\"123\",\"old2\":\"foo\",\"old3\":[\"0\",\"bar\",null]}") } { MallocMessageBuilder message; auto root = message.getRoot(); json.decode(encoded, root); KJ_EXPECT(root.getOld1() == 123); KJ_EXPECT("foo" == root.getOld2()); auto nested = root.getOld3(); KJ_EXPECT(nested.getOld1() == 0); KJ_EXPECT("bar" == nested.getOld2()); KJ_EXPECT(!nested.hasOld3()); } } class TestCapabilityHandler: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, test::TestInterface::Client input, JsonValue::Builder output) const override { KJ_UNIMPLEMENTED("TestCapabilityHandler::encode"); } test::TestInterface::Client decode( const JsonCodec& codec, JsonValue::Reader input) const override { return nullptr; } }; KJ_TEST("register capability handler") { // This test currently only checks that this compiles, which at one point wasn't the caes. // TODO(test): Actually run some code here. TestCapabilityHandler handler; JsonCodec json; json.addTypeHandler(handler); } static constexpr kj::StringPtr GOLDEN_ANNOTATED = R"({ "names-can_contain!anything Really": "foo", "flatFoo": 123, "flatBar": "abc", "renamed-flatBaz": {"hello": true}, "flatQux": "cba", "pfx.foo": "this is a long string in order to force multi-line pretty printing", "pfx.renamed-bar": 321, "pfx.baz": {"hello": true}, "pfx.xfp.qux": "fed", "union-type": "renamed-bar", "barMember": 789, "multiMember": "ghi", "dependency": {"renamed-foo": "corge"}, "simpleGroup": {"renamed-grault": "garply"}, "enums": ["qux", "renamed-bar", "foo", "renamed-baz"], "innerJson": [123, "hello", {"object": true}], "customFieldHandler": "add-prefix-waldo", "testBase64": "ZnJlZA==", "testHex": "706c756768", "bUnion": "renamed-bar", "bValue": 678, "externalUnion": {"type": "bar", "value": "cba"}, "unionWithVoid": {"type": "voidValue"} })"_kj; static constexpr kj::StringPtr GOLDEN_ANNOTATED_REVERSE = R"({ "unionWithVoid": {"type": "voidValue"}, "externalUnion": {"type": "bar", "value": "cba"}, "bValue": 678, "bUnion": "renamed-bar", "testHex": "706c756768", "testBase64": "ZnJlZA==", "customFieldHandler": "add-prefix-waldo", "innerJson": [123, "hello", {"object": true}], "enums": ["qux", "renamed-bar", "foo", "renamed-baz"], "simpleGroup": { "renamed-grault": "garply" }, "dependency": { "renamed-foo": "corge" }, "multiMember": "ghi", "barMember": 789, "union-type": "renamed-bar", "pfx.xfp.qux": "fed", "pfx.baz": {"hello": true}, "pfx.renamed-bar": 321, "pfx.foo": "this is a long string in order to force multi-line pretty printing", "flatQux": "cba", "renamed-flatBaz": {"hello": true}, "flatBar": "abc", "flatFoo": 123, "names-can_contain!anything Really": "foo" })"_kj; class PrefixAdder: public JsonCodec::Handler { public: void encode(const JsonCodec& codec, capnp::Text::Reader input, JsonValue::Builder output) const { output.setString(kj::str("add-prefix-", input)); } Orphan decode(const JsonCodec& codec, JsonValue::Reader input, Orphanage orphanage) const { return orphanage.newOrphanCopy(capnp::Text::Reader(input.getString().slice(11))); } }; KJ_TEST("rename fields") { JsonCodec json; json.handleByAnnotation(); json.setPrettyPrint(true); PrefixAdder customHandler; json.addFieldHandler(Schema::from().getFieldByName("customFieldHandler"), customHandler); kj::String goldenText; { MallocMessageBuilder message; auto root = message.getRoot(); root.setSomeField("foo"); auto aGroup = root.getAGroup(); aGroup.setFlatFoo(123); aGroup.setFlatBar("abc"); aGroup.getFlatBaz().setHello(true); aGroup.getDoubleFlat().setFlatQux("cba"); auto prefixedGroup = root.getPrefixedGroup(); prefixedGroup.setFoo("this is a long string in order to force multi-line pretty printing"); prefixedGroup.setBar(321); prefixedGroup.getBaz().setHello(true); prefixedGroup.getMorePrefix().setQux("fed"); auto unionBar = root.getAUnion().initBar(); unionBar.setBarMember(789); unionBar.setMultiMember("ghi"); root.initDependency().setFoo("corge"); root.initSimpleGroup().setGrault("garply"); root.setEnums({ TestJsonAnnotatedEnum::QUX, TestJsonAnnotatedEnum::BAR, TestJsonAnnotatedEnum::FOO, TestJsonAnnotatedEnum::BAZ }); auto val = root.initInnerJson(); auto arr = val.initArray(3); arr[0].setNumber(123); arr[1].setString("hello"); auto field = arr[2].initObject(1)[0]; field.setName("object"); field.initValue().setBoolean(true); root.setCustomFieldHandler("waldo"); root.setTestBase64("fred"_kj.asBytes()); root.setTestHex("plugh"_kj.asBytes()); root.getBUnion().setBar(678); root.initExternalUnion().initBar().setValue("cba"); root.initUnionWithVoid().setVoidValue(); auto encoded = json.encode(root.asReader()); KJ_EXPECT(encoded == GOLDEN_ANNOTATED, encoded); goldenText = kj::str(root); } { MallocMessageBuilder message; auto root = message.getRoot(); json.decode(GOLDEN_ANNOTATED, root); KJ_EXPECT(kj::str(root) == goldenText, root, goldenText); } { // Try parsing in reverse, mostly to test that union tags can come after content. MallocMessageBuilder message; auto root = message.getRoot(); json.decode(GOLDEN_ANNOTATED_REVERSE, root); KJ_EXPECT(kj::str(root) == goldenText, root, goldenText); } } KJ_TEST("base64 union encoded correctly") { // At one point field handlers were not correctly applied when the field was a member of a union // in a type that was handled by annotation. JsonCodec json; json.handleByAnnotation(); json.setPrettyPrint(true); MallocMessageBuilder message; auto root = message.getRoot(); root.initFoo(5); KJ_EXPECT(json.encode(root) == "{\"foo\": \"AAAAAAA=\"}", json.encode(root)); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/compat/json-test.capnp0000644000175000017500000000654713650101756023114 0ustar00kentonkenton00000000000000# Copyright (c) 2018 Cloudflare, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xc9d405cf4333e4c9; using Json = import "/capnp/compat/json.capnp"; $import "/capnp/c++.capnp".namespace("capnp"); struct TestJsonAnnotations { someField @0 :Text $Json.name("names-can_contain!anything Really"); aGroup :group $Json.flatten() { flatFoo @1 :UInt32; flatBar @2 :Text; flatBaz :group $Json.name("renamed-flatBaz") { hello @3 :Bool; } doubleFlat :group $Json.flatten() { flatQux @4 :Text; } } prefixedGroup :group $Json.flatten(prefix = "pfx.") { foo @5 :Text; bar @6 :UInt32 $Json.name("renamed-bar"); baz :group { hello @7 :Bool; } morePrefix :group $Json.flatten(prefix = "xfp.") { qux @8 :Text; } } aUnion :union $Json.flatten() $Json.discriminator(name = "union-type") { foo :group $Json.flatten() { fooMember @9 :Text; multiMember @10 :UInt32; } bar :group $Json.flatten() $Json.name("renamed-bar") { barMember @11 :UInt32; multiMember @12 :Text; } } dependency @13 :TestJsonAnnotations2; # To test that dependencies are loaded even if not flattened. simpleGroup :group { # To test that group types are loaded even if not flattened. grault @14 :Text $Json.name("renamed-grault"); } enums @15 :List(TestJsonAnnotatedEnum); innerJson @16 :Json.Value; customFieldHandler @17 :Text; testBase64 @18 :Data $Json.base64; testHex @19 :Data $Json.hex; bUnion :union $Json.flatten() $Json.discriminator(valueName = "bValue") { foo @20 :Text; bar @21 :UInt32 $Json.name("renamed-bar"); } externalUnion @22 :TestJsonAnnotations3; unionWithVoid :union $Json.discriminator(name = "type") { intValue @23 :UInt32; voidValue @24 :Void; textValue @25 :Text; } } struct TestJsonAnnotations2 { foo @0 :Text $Json.name("renamed-foo"); cycle @1 :TestJsonAnnotations; } struct TestJsonAnnotations3 $Json.discriminator(name = "type") { union { foo @0 :UInt32; bar @1 :TestFlattenedStruct $Json.flatten(); } } struct TestFlattenedStruct { value @0 :Text; } enum TestJsonAnnotatedEnum { foo @0; bar @1 $Json.name("renamed-bar"); baz @2 $Json.name("renamed-baz"); qux @3; } struct TestBase64Union { union { foo @0 :Data $Json.base64; bar @1 :Text; } } capnproto-c++-0.8.0/src/capnp/compiler/0000755000175000017500000000000013650320027020450 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/capnp/compiler/type-id.h0000644000175000017500000000357213340402540022200 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include namespace capnp { namespace compiler { uint64_t generateChildId(uint64_t parentId, kj::StringPtr childName); uint64_t generateGroupId(uint64_t parentId, uint16_t groupIndex); uint64_t generateMethodParamsId(uint64_t parentId, uint16_t methodOrdinal, bool isResults); // Generate a default type ID for various symbols. These are used only if the developer did not // specify an ID explicitly. // // The returned ID always has the most-significant bit set. The remaining bits are generated // pseudo-randomly from the input using an algorithm that should produce a uniform distribution of // IDs. } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/type-id.c++0000644000175000017500000003252113340402540022315 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "type-id.h" #include #include namespace capnp { namespace compiler { class TypeIdGenerator { // A non-cryptographic deterministic random number generator used to generate type IDs when the // developer did not specify one themselves. // // The underlying algorithm is MD5. MD5 is safe to use here because this is not intended to be a // cryptographic random number generator. In retrospect it would have been nice to use something // else just to avoid people freaking out about it, but changing the algorithm now would break // backwards-compatibility. public: TypeIdGenerator(); void update(kj::ArrayPtr data); inline void update(kj::ArrayPtr data) { return update(data.asBytes()); } inline void update(kj::StringPtr data) { return update(data.asArray()); } kj::ArrayPtr finish(); private: bool finished = false; struct { uint lo, hi; uint a, b, c, d; kj::byte buffer[64]; uint block[16]; } ctx; const kj::byte* body(const kj::byte* ptr, size_t size); }; uint64_t generateChildId(uint64_t parentId, kj::StringPtr childName) { // Compute ID by hashing the concatenation of the parent ID and the declaration name, and // then taking the first 8 bytes. kj::byte parentIdBytes[sizeof(uint64_t)]; for (uint i = 0; i < sizeof(uint64_t); i++) { parentIdBytes[i] = (parentId >> (i * 8)) & 0xff; } TypeIdGenerator generator; generator.update(kj::arrayPtr(parentIdBytes, kj::size(parentIdBytes))); generator.update(childName); kj::ArrayPtr resultBytes = generator.finish(); uint64_t result = 0; for (uint i = 0; i < sizeof(uint64_t); i++) { result = (result << 8) | resultBytes[i]; } return result | (1ull << 63); } uint64_t generateGroupId(uint64_t parentId, uint16_t groupIndex) { // Compute ID by hashing the concatenation of the parent ID and the group index, and // then taking the first 8 bytes. kj::byte bytes[sizeof(uint64_t) + sizeof(uint16_t)]; for (uint i = 0; i < sizeof(uint64_t); i++) { bytes[i] = (parentId >> (i * 8)) & 0xff; } for (uint i = 0; i < sizeof(uint16_t); i++) { bytes[sizeof(uint64_t) + i] = (groupIndex >> (i * 8)) & 0xff; } TypeIdGenerator generator; generator.update(bytes); kj::ArrayPtr resultBytes = generator.finish(); uint64_t result = 0; for (uint i = 0; i < sizeof(uint64_t); i++) { result = (result << 8) | resultBytes[i]; } return result | (1ull << 63); } uint64_t generateMethodParamsId(uint64_t parentId, uint16_t methodOrdinal, bool isResults) { // Compute ID by hashing the concatenation of the parent ID, the method ordinal, and a // boolean indicating whether this is the params or the results, and then taking the first 8 // bytes. kj::byte bytes[sizeof(uint64_t) + sizeof(uint16_t) + 1]; for (uint i = 0; i < sizeof(uint64_t); i++) { bytes[i] = (parentId >> (i * 8)) & 0xff; } for (uint i = 0; i < sizeof(uint16_t); i++) { bytes[sizeof(uint64_t) + i] = (methodOrdinal >> (i * 8)) & 0xff; } bytes[sizeof(bytes) - 1] = isResults; TypeIdGenerator generator; generator.update(bytes); kj::ArrayPtr resultBytes = generator.finish(); uint64_t result = 0; for (uint i = 0; i < sizeof(uint64_t); i++) { result = (result << 8) | resultBytes[i]; } return result | (1ull << 63); } // The remainder of this file was derived from code placed in the public domain. // The original code bore the following notice: /* * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. * MD5 Message-Digest Algorithm (RFC 1321). * * Homepage: * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 * * Author: * Alexander Peslyak, better known as Solar Designer * * This software was written by Alexander Peslyak in 2001. No copyright is * claimed, and the software is hereby placed in the public domain. * In case this attempt to disclaim copyright and place the software in the * public domain is deemed null and void, then the software is * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the * general public under the following terms: * * Redistribution and use in source and binary forms, with or without * modification, are permitted. * * There's ABSOLUTELY NO WARRANTY, express or implied. * * (This is a heavily cut-down "BSD license".) * * This differs from Colin Plumb's older public domain implementation in that * no exactly 32-bit integer data type is required (any 32-bit or wider * unsigned integer data type will do), there's no compile-time endianness * configuration, and the function prototypes match OpenSSL's. No code from * Colin Plumb's implementation has been reused; this comment merely compares * the properties of the two independent implementations. * * The primary goals of this implementation are portability and ease of use. * It is meant to be fast, but not as fast as possible. Some known * optimizations are not included to reduce source code size and avoid * compile-time configuration. */ /* * The basic MD5 functions. * * F and G are optimized compared to their RFC 1321 definitions for * architectures that lack an AND-NOT instruction, just like in Colin Plumb's * implementation. */ #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | ~(z))) /* * The MD5 transformation for all four rounds. */ #define STEP(f, a, b, c, d, x, t, s) \ (a) += f((b), (c), (d)) + (x) + (t); \ (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ (a) += (b); /* * SET reads 4 input bytes in little-endian byte order and stores them * in a properly aligned word in host byte order. * * The check for little-endian architectures that tolerate unaligned * memory accesses is just an optimization. Nothing will break if it * doesn't work. */ #if defined(__i386__) || defined(__x86_64__) || defined(__vax__) #define SET(n) \ (*(uint *)&ptr[(n) * 4]) #define GET(n) \ SET(n) #else #define SET(n) \ (ctx.block[(n)] = \ (uint)ptr[(n) * 4] | \ ((uint)ptr[(n) * 4 + 1] << 8) | \ ((uint)ptr[(n) * 4 + 2] << 16) | \ ((uint)ptr[(n) * 4 + 3] << 24)) #define GET(n) \ (ctx.block[(n)]) #endif /* * This processes one or more 64-byte data blocks, but does NOT update * the bit counters. There are no alignment requirements. */ const kj::byte* TypeIdGenerator::body(const kj::byte* ptr, size_t size) { uint a, b, c, d; uint saved_a, saved_b, saved_c, saved_d; a = ctx.a; b = ctx.b; c = ctx.c; d = ctx.d; do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; /* Round 1 */ STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) STEP(F, c, d, a, b, SET(2), 0x242070db, 17) STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) /* Round 2 */ STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) STEP(G, d, a, b, c, GET(10), 0x02441453, 9) STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) /* Round 3 */ STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) STEP(H, d, a, b, c, GET(8), 0x8771f681, 11) STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23) STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11) STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23) STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11) STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) STEP(H, b, c, d, a, GET(6), 0x04881d05, 23) STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23) /* Round 4 */ STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) a += saved_a; b += saved_b; c += saved_c; d += saved_d; ptr += 64; } while (size -= 64); ctx.a = a; ctx.b = b; ctx.c = c; ctx.d = d; return ptr; } TypeIdGenerator::TypeIdGenerator() { ctx.a = 0x67452301; ctx.b = 0xefcdab89; ctx.c = 0x98badcfe; ctx.d = 0x10325476; ctx.lo = 0; ctx.hi = 0; } void TypeIdGenerator::update(kj::ArrayPtr dataArray) { KJ_REQUIRE(!finished, "already called TypeIdGenerator::finish()"); const kj::byte* data = dataArray.begin(); unsigned long size = dataArray.size(); uint saved_lo; unsigned long used, free; saved_lo = ctx.lo; if ((ctx.lo = (saved_lo + size) & 0x1fffffff) < saved_lo) ctx.hi++; ctx.hi += size >> 29; used = saved_lo & 0x3f; if (used) { free = 64 - used; if (size < free) { memcpy(&ctx.buffer[used], data, size); return; } memcpy(&ctx.buffer[used], data, free); data = data + free; size -= free; body(ctx.buffer, 64); } if (size >= 64) { data = body(data, size & ~(unsigned long)0x3f); size &= 0x3f; } memcpy(ctx.buffer, data, size); } kj::ArrayPtr TypeIdGenerator::finish() { if (!finished) { unsigned long used, free; used = ctx.lo & 0x3f; ctx.buffer[used++] = 0x80; free = 64 - used; if (free < 8) { memset(&ctx.buffer[used], 0, free); body(ctx.buffer, 64); used = 0; free = 64; } memset(&ctx.buffer[used], 0, free - 8); ctx.lo <<= 3; ctx.buffer[56] = ctx.lo; ctx.buffer[57] = ctx.lo >> 8; ctx.buffer[58] = ctx.lo >> 16; ctx.buffer[59] = ctx.lo >> 24; ctx.buffer[60] = ctx.hi; ctx.buffer[61] = ctx.hi >> 8; ctx.buffer[62] = ctx.hi >> 16; ctx.buffer[63] = ctx.hi >> 24; body(ctx.buffer, 64); // Store final result into ctx.buffer. ctx.buffer[0] = ctx.a; ctx.buffer[1] = ctx.a >> 8; ctx.buffer[2] = ctx.a >> 16; ctx.buffer[3] = ctx.a >> 24; ctx.buffer[4] = ctx.b; ctx.buffer[5] = ctx.b >> 8; ctx.buffer[6] = ctx.b >> 16; ctx.buffer[7] = ctx.b >> 24; ctx.buffer[8] = ctx.c; ctx.buffer[9] = ctx.c >> 8; ctx.buffer[10] = ctx.c >> 16; ctx.buffer[11] = ctx.c >> 24; ctx.buffer[12] = ctx.d; ctx.buffer[13] = ctx.d >> 8; ctx.buffer[14] = ctx.d >> 16; ctx.buffer[15] = ctx.d >> 24; finished = true; } return kj::arrayPtr(ctx.buffer, 16); } } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/error-reporter.h0000644000175000017500000000712413650101756023624 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "../common.h" #include #include #include #include CAPNP_BEGIN_HEADER namespace capnp { namespace compiler { class ErrorReporter { // Callback for reporting errors within a particular file. public: virtual void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) = 0; // Report an error at the given location in the input text. `startByte` and `endByte` indicate // the span of text that is erroneous. They may be equal, in which case the parser was only // able to identify where the error begins, not where it ends. template inline void addErrorOn(T&& decl, kj::StringPtr message) { // Works for any `T` that defines `getStartByte()` and `getEndByte()` methods, which many // of the Cap'n Proto types defined in `grammar.capnp` do. addError(decl.getStartByte(), decl.getEndByte(), message); } virtual bool hadErrors() = 0; // Return true if any errors have been reported, globally. The main use case for this callback // is to inhibit the reporting of errors which may have been caused by previous errors, or to // allow the compiler to bail out entirely if it gets confused and thinks this could be because // of previous errors. }; class GlobalErrorReporter { // Callback for reporting errors in any file. public: struct SourcePos { uint byte; uint line; uint column; }; virtual void addError(const kj::ReadableDirectory& directory, kj::PathPtr path, SourcePos start, SourcePos end, kj::StringPtr message) = 0; // Report an error at the given location in the given file. virtual bool hadErrors() = 0; // Return true if any errors have been reported, globally. The main use case for this callback // is to inhibit the reporting of errors which may have been caused by previous errors, or to // allow the compiler to bail out entirely if it gets confused and thinks this could be because // of previous errors. }; class LineBreakTable { public: LineBreakTable(kj::ArrayPtr content); GlobalErrorReporter::SourcePos toSourcePos(uint32_t byteOffset) const; private: kj::Vector lineBreaks; // Byte offsets of the first byte in each source line. The first element is always zero. // Initialized the first time the module is loaded. }; } // namespace compiler } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/compiler/error-reporter.c++0000644000175000017500000000435313340402540023735 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "error-reporter.h" #include namespace capnp { namespace compiler { namespace { template static size_t findLargestElementBefore(const kj::Vector& vec, const T& key) { KJ_REQUIRE(vec.size() > 0 && vec[0] <= key); size_t lower = 0; size_t upper = vec.size(); while (upper - lower > 1) { size_t mid = (lower + upper) / 2; if (vec[mid] > key) { upper = mid; } else { lower = mid; } } return lower; } } // namespace LineBreakTable::LineBreakTable(kj::ArrayPtr content) : lineBreaks(content.size() / 40) { lineBreaks.add(0); for (const char* pos = content.begin(); pos < content.end(); ++pos) { if (*pos == '\n') { lineBreaks.add(pos + 1 - content.begin()); } } } GlobalErrorReporter::SourcePos LineBreakTable::toSourcePos(uint32_t byteOffset) const { uint line = findLargestElementBefore(lineBreaks, byteOffset); uint col = byteOffset - lineBreaks[line]; return GlobalErrorReporter::SourcePos { byteOffset, line, col }; } } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/lexer.capnp.h0000644000175000017500000015444113650317744023065 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: lexer.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(91cc55cd57de5419); CAPNP_DECLARE_SCHEMA(c6725e678d60fa37); CAPNP_DECLARE_SCHEMA(9e69a92512b19d18); CAPNP_DECLARE_SCHEMA(a11f97b9d6c73dd4); } // namespace schemas } // namespace capnp namespace capnp { namespace compiler { struct Token { Token() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { IDENTIFIER, STRING_LITERAL, INTEGER_LITERAL, FLOAT_LITERAL, OPERATOR, PARENTHESIZED_LIST, BRACKETED_LIST, BINARY_LITERAL, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(91cc55cd57de5419, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Statement { Statement() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { LINE, BLOCK, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c6725e678d60fa37, 2, 3) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct LexedTokens { LexedTokens() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9e69a92512b19d18, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct LexedStatements { LexedStatements() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(a11f97b9d6c73dd4, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class Token::Reader { public: typedef Token Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isIdentifier() const; inline bool hasIdentifier() const; inline ::capnp::Text::Reader getIdentifier() const; inline bool isStringLiteral() const; inline bool hasStringLiteral() const; inline ::capnp::Text::Reader getStringLiteral() const; inline bool isIntegerLiteral() const; inline ::uint64_t getIntegerLiteral() const; inline bool isFloatLiteral() const; inline double getFloatLiteral() const; inline bool isOperator() const; inline bool hasOperator() const; inline ::capnp::Text::Reader getOperator() const; inline bool isParenthesizedList() const; inline bool hasParenthesizedList() const; inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader getParenthesizedList() const; inline bool isBracketedList() const; inline bool hasBracketedList() const; inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader getBracketedList() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; inline bool isBinaryLiteral() const; inline bool hasBinaryLiteral() const; inline ::capnp::Data::Reader getBinaryLiteral() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Token::Builder { public: typedef Token Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isIdentifier(); inline bool hasIdentifier(); inline ::capnp::Text::Builder getIdentifier(); inline void setIdentifier( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initIdentifier(unsigned int size); inline void adoptIdentifier(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownIdentifier(); inline bool isStringLiteral(); inline bool hasStringLiteral(); inline ::capnp::Text::Builder getStringLiteral(); inline void setStringLiteral( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initStringLiteral(unsigned int size); inline void adoptStringLiteral(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownStringLiteral(); inline bool isIntegerLiteral(); inline ::uint64_t getIntegerLiteral(); inline void setIntegerLiteral( ::uint64_t value); inline bool isFloatLiteral(); inline double getFloatLiteral(); inline void setFloatLiteral(double value); inline bool isOperator(); inline bool hasOperator(); inline ::capnp::Text::Builder getOperator(); inline void setOperator( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initOperator(unsigned int size); inline void adoptOperator(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownOperator(); inline bool isParenthesizedList(); inline bool hasParenthesizedList(); inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder getParenthesizedList(); inline void setParenthesizedList( ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader value); inline void setParenthesizedList(::kj::ArrayPtr::Reader> value); inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder initParenthesizedList(unsigned int size); inline void adoptParenthesizedList(::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>> disownParenthesizedList(); inline bool isBracketedList(); inline bool hasBracketedList(); inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder getBracketedList(); inline void setBracketedList( ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader value); inline void setBracketedList(::kj::ArrayPtr::Reader> value); inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder initBracketedList(unsigned int size); inline void adoptBracketedList(::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>> disownBracketedList(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); inline bool isBinaryLiteral(); inline bool hasBinaryLiteral(); inline ::capnp::Data::Builder getBinaryLiteral(); inline void setBinaryLiteral( ::capnp::Data::Reader value); inline ::capnp::Data::Builder initBinaryLiteral(unsigned int size); inline void adoptBinaryLiteral(::capnp::Orphan< ::capnp::Data>&& value); inline ::capnp::Orphan< ::capnp::Data> disownBinaryLiteral(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Token::Pipeline { public: typedef Token Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Statement::Reader { public: typedef Statement Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool hasTokens() const; inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader getTokens() const; inline bool isLine() const; inline ::capnp::Void getLine() const; inline bool isBlock() const; inline bool hasBlock() const; inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader getBlock() const; inline bool hasDocComment() const; inline ::capnp::Text::Reader getDocComment() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Statement::Builder { public: typedef Statement Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool hasTokens(); inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder getTokens(); inline void setTokens( ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder initTokens(unsigned int size); inline void adoptTokens(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>> disownTokens(); inline bool isLine(); inline ::capnp::Void getLine(); inline void setLine( ::capnp::Void value = ::capnp::VOID); inline bool isBlock(); inline bool hasBlock(); inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder getBlock(); inline void setBlock( ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder initBlock(unsigned int size); inline void adoptBlock(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>> disownBlock(); inline bool hasDocComment(); inline ::capnp::Text::Builder getDocComment(); inline void setDocComment( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initDocComment(unsigned int size); inline void adoptDocComment(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownDocComment(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Statement::Pipeline { public: typedef Statement Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class LexedTokens::Reader { public: typedef LexedTokens Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasTokens() const; inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader getTokens() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class LexedTokens::Builder { public: typedef LexedTokens Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasTokens(); inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder getTokens(); inline void setTokens( ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder initTokens(unsigned int size); inline void adoptTokens(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>> disownTokens(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class LexedTokens::Pipeline { public: typedef LexedTokens Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class LexedStatements::Reader { public: typedef LexedStatements Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasStatements() const; inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader getStatements() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class LexedStatements::Builder { public: typedef LexedStatements Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasStatements(); inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder getStatements(); inline void setStatements( ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder initStatements(unsigned int size); inline void adoptStatements(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>> disownStatements(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class LexedStatements::Pipeline { public: typedef LexedStatements Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= inline ::capnp::compiler::Token::Which Token::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Token::Which Token::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Token::Reader::isIdentifier() const { return which() == Token::IDENTIFIER; } inline bool Token::Builder::isIdentifier() { return which() == Token::IDENTIFIER; } inline bool Token::Reader::hasIdentifier() const { if (which() != Token::IDENTIFIER) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Token::Builder::hasIdentifier() { if (which() != Token::IDENTIFIER) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Token::Reader::getIdentifier() const { KJ_IREQUIRE((which() == Token::IDENTIFIER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Token::Builder::getIdentifier() { KJ_IREQUIRE((which() == Token::IDENTIFIER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Token::Builder::setIdentifier( ::capnp::Text::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::IDENTIFIER); ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Token::Builder::initIdentifier(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::IDENTIFIER); return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Token::Builder::adoptIdentifier( ::capnp::Orphan< ::capnp::Text>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::IDENTIFIER); ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Token::Builder::disownIdentifier() { KJ_IREQUIRE((which() == Token::IDENTIFIER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Token::Reader::isStringLiteral() const { return which() == Token::STRING_LITERAL; } inline bool Token::Builder::isStringLiteral() { return which() == Token::STRING_LITERAL; } inline bool Token::Reader::hasStringLiteral() const { if (which() != Token::STRING_LITERAL) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Token::Builder::hasStringLiteral() { if (which() != Token::STRING_LITERAL) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Token::Reader::getStringLiteral() const { KJ_IREQUIRE((which() == Token::STRING_LITERAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Token::Builder::getStringLiteral() { KJ_IREQUIRE((which() == Token::STRING_LITERAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Token::Builder::setStringLiteral( ::capnp::Text::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::STRING_LITERAL); ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Token::Builder::initStringLiteral(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::STRING_LITERAL); return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Token::Builder::adoptStringLiteral( ::capnp::Orphan< ::capnp::Text>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::STRING_LITERAL); ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Token::Builder::disownStringLiteral() { KJ_IREQUIRE((which() == Token::STRING_LITERAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Token::Reader::isIntegerLiteral() const { return which() == Token::INTEGER_LITERAL; } inline bool Token::Builder::isIntegerLiteral() { return which() == Token::INTEGER_LITERAL; } inline ::uint64_t Token::Reader::getIntegerLiteral() const { KJ_IREQUIRE((which() == Token::INTEGER_LITERAL), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Token::Builder::getIntegerLiteral() { KJ_IREQUIRE((which() == Token::INTEGER_LITERAL), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Token::Builder::setIntegerLiteral( ::uint64_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::INTEGER_LITERAL); _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Token::Reader::isFloatLiteral() const { return which() == Token::FLOAT_LITERAL; } inline bool Token::Builder::isFloatLiteral() { return which() == Token::FLOAT_LITERAL; } inline double Token::Reader::getFloatLiteral() const { KJ_IREQUIRE((which() == Token::FLOAT_LITERAL), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline double Token::Builder::getFloatLiteral() { KJ_IREQUIRE((which() == Token::FLOAT_LITERAL), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Token::Builder::setFloatLiteral(double value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::FLOAT_LITERAL); _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Token::Reader::isOperator() const { return which() == Token::OPERATOR; } inline bool Token::Builder::isOperator() { return which() == Token::OPERATOR; } inline bool Token::Reader::hasOperator() const { if (which() != Token::OPERATOR) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Token::Builder::hasOperator() { if (which() != Token::OPERATOR) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Token::Reader::getOperator() const { KJ_IREQUIRE((which() == Token::OPERATOR), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Token::Builder::getOperator() { KJ_IREQUIRE((which() == Token::OPERATOR), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Token::Builder::setOperator( ::capnp::Text::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::OPERATOR); ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Token::Builder::initOperator(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::OPERATOR); return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Token::Builder::adoptOperator( ::capnp::Orphan< ::capnp::Text>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::OPERATOR); ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Token::Builder::disownOperator() { KJ_IREQUIRE((which() == Token::OPERATOR), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Token::Reader::isParenthesizedList() const { return which() == Token::PARENTHESIZED_LIST; } inline bool Token::Builder::isParenthesizedList() { return which() == Token::PARENTHESIZED_LIST; } inline bool Token::Reader::hasParenthesizedList() const { if (which() != Token::PARENTHESIZED_LIST) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Token::Builder::hasParenthesizedList() { if (which() != Token::PARENTHESIZED_LIST) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader Token::Reader::getParenthesizedList() const { KJ_IREQUIRE((which() == Token::PARENTHESIZED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder Token::Builder::getParenthesizedList() { KJ_IREQUIRE((which() == Token::PARENTHESIZED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Token::Builder::setParenthesizedList( ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::PARENTHESIZED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline void Token::Builder::setParenthesizedList(::kj::ArrayPtr::Reader> value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::PARENTHESIZED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder Token::Builder::initParenthesizedList(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::PARENTHESIZED_LIST); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Token::Builder::adoptParenthesizedList( ::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::PARENTHESIZED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>> Token::Builder::disownParenthesizedList() { KJ_IREQUIRE((which() == Token::PARENTHESIZED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Token::Reader::isBracketedList() const { return which() == Token::BRACKETED_LIST; } inline bool Token::Builder::isBracketedList() { return which() == Token::BRACKETED_LIST; } inline bool Token::Reader::hasBracketedList() const { if (which() != Token::BRACKETED_LIST) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Token::Builder::hasBracketedList() { if (which() != Token::BRACKETED_LIST) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader Token::Reader::getBracketedList() const { KJ_IREQUIRE((which() == Token::BRACKETED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder Token::Builder::getBracketedList() { KJ_IREQUIRE((which() == Token::BRACKETED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Token::Builder::setBracketedList( ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BRACKETED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline void Token::Builder::setBracketedList(::kj::ArrayPtr::Reader> value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BRACKETED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>::Builder Token::Builder::initBracketedList(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BRACKETED_LIST); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Token::Builder::adoptBracketedList( ::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BRACKETED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>> Token::Builder::disownBracketedList() { KJ_IREQUIRE((which() == Token::BRACKETED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>, ::capnp::Kind::LIST>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Token::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Token::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Token::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Token::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline ::uint32_t Token::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline void Token::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<4>() * ::capnp::ELEMENTS, value); } inline bool Token::Reader::isBinaryLiteral() const { return which() == Token::BINARY_LITERAL; } inline bool Token::Builder::isBinaryLiteral() { return which() == Token::BINARY_LITERAL; } inline bool Token::Reader::hasBinaryLiteral() const { if (which() != Token::BINARY_LITERAL) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Token::Builder::hasBinaryLiteral() { if (which() != Token::BINARY_LITERAL) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Data::Reader Token::Reader::getBinaryLiteral() const { KJ_IREQUIRE((which() == Token::BINARY_LITERAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Data::Builder Token::Builder::getBinaryLiteral() { KJ_IREQUIRE((which() == Token::BINARY_LITERAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Token::Builder::setBinaryLiteral( ::capnp::Data::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BINARY_LITERAL); ::capnp::_::PointerHelpers< ::capnp::Data>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Data::Builder Token::Builder::initBinaryLiteral(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BINARY_LITERAL); return ::capnp::_::PointerHelpers< ::capnp::Data>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Token::Builder::adoptBinaryLiteral( ::capnp::Orphan< ::capnp::Data>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Token::BINARY_LITERAL); ::capnp::_::PointerHelpers< ::capnp::Data>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Data> Token::Builder::disownBinaryLiteral() { KJ_IREQUIRE((which() == Token::BINARY_LITERAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Statement::Which Statement::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Statement::Which Statement::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Statement::Reader::hasTokens() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Statement::Builder::hasTokens() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader Statement::Reader::getTokens() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder Statement::Builder::getTokens() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Statement::Builder::setTokens( ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder Statement::Builder::initTokens(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Statement::Builder::adoptTokens( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>> Statement::Builder::disownTokens() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Statement::Reader::isLine() const { return which() == Statement::LINE; } inline bool Statement::Builder::isLine() { return which() == Statement::LINE; } inline ::capnp::Void Statement::Reader::getLine() const { KJ_IREQUIRE((which() == Statement::LINE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Statement::Builder::getLine() { KJ_IREQUIRE((which() == Statement::LINE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Statement::Builder::setLine( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Statement::LINE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Statement::Reader::isBlock() const { return which() == Statement::BLOCK; } inline bool Statement::Builder::isBlock() { return which() == Statement::BLOCK; } inline bool Statement::Reader::hasBlock() const { if (which() != Statement::BLOCK) return false; return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Statement::Builder::hasBlock() { if (which() != Statement::BLOCK) return false; return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader Statement::Reader::getBlock() const { KJ_IREQUIRE((which() == Statement::BLOCK), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder Statement::Builder::getBlock() { KJ_IREQUIRE((which() == Statement::BLOCK), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Statement::Builder::setBlock( ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Statement::BLOCK); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder Statement::Builder::initBlock(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Statement::BLOCK); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Statement::Builder::adoptBlock( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Statement::BLOCK); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>> Statement::Builder::disownBlock() { KJ_IREQUIRE((which() == Statement::BLOCK), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Statement::Reader::hasDocComment() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Statement::Builder::hasDocComment() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Statement::Reader::getDocComment() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Statement::Builder::getDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void Statement::Builder::setDocComment( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Statement::Builder::initDocComment(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), size); } inline void Statement::Builder::adoptDocComment( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Statement::Builder::disownDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::uint32_t Statement::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Statement::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Statement::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Statement::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Statement::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Statement::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool LexedTokens::Reader::hasTokens() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool LexedTokens::Builder::hasTokens() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader LexedTokens::Reader::getTokens() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder LexedTokens::Builder::getTokens() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void LexedTokens::Builder::setTokens( ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>::Builder LexedTokens::Builder::initTokens(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void LexedTokens::Builder::adoptTokens( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>> LexedTokens::Builder::disownTokens() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Token, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool LexedStatements::Reader::hasStatements() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool LexedStatements::Builder::hasStatements() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader LexedStatements::Reader::getStatements() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder LexedStatements::Builder::getStatements() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void LexedStatements::Builder::setStatements( ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>::Builder LexedStatements::Builder::initStatements(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void LexedStatements::Builder::adoptStatements( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>> LexedStatements::Builder::disownStatements() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Statement, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/compiler/lexer.capnp.c++0000644000175000017500000005166713340402540023175 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: lexer.capnp #include "lexer.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<195> b_91cc55cd57de5419 = { { 0, 0, 0, 0, 5, 0, 6, 0, 25, 84, 222, 87, 205, 85, 204, 145, 27, 0, 0, 0, 1, 0, 3, 0, 238, 195, 31, 98, 210, 86, 57, 167, 1, 0, 7, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 10, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 55, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 108, 101, 120, 101, 114, 46, 99, 97, 112, 110, 112, 58, 84, 111, 107, 101, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 40, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1, 0, 0, 3, 0, 1, 0, 20, 1, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 1, 0, 0, 3, 0, 1, 0, 28, 1, 0, 0, 2, 0, 1, 0, 3, 0, 253, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 1, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 1, 0, 0, 3, 0, 1, 0, 36, 1, 0, 0, 2, 0, 1, 0, 4, 0, 252, 255, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 1, 0, 0, 3, 0, 1, 0, 44, 1, 0, 0, 2, 0, 1, 0, 5, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 1, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 1, 0, 0, 3, 0, 1, 0, 52, 1, 0, 0, 2, 0, 1, 0, 6, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 1, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 1, 0, 0, 3, 0, 1, 0, 96, 1, 0, 0, 2, 0, 1, 0, 7, 0, 249, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92, 1, 0, 0, 3, 0, 1, 0, 136, 1, 0, 0, 2, 0, 1, 0, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 1, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 1, 0, 0, 3, 0, 1, 0, 144, 1, 0, 0, 2, 0, 1, 0, 9, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 1, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 1, 0, 0, 3, 0, 1, 0, 148, 1, 0, 0, 2, 0, 1, 0, 2, 0, 248, 255, 0, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 1, 0, 0, 3, 0, 1, 0, 156, 1, 0, 0, 2, 0, 1, 0, 105, 100, 101, 110, 116, 105, 102, 105, 101, 114, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 105, 110, 103, 76, 105, 116, 101, 114, 97, 108, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 101, 103, 101, 114, 76, 105, 116, 101, 114, 97, 108, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 108, 111, 97, 116, 76, 105, 116, 101, 114, 97, 108, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 112, 101, 114, 97, 116, 111, 114, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 101, 110, 116, 104, 101, 115, 105, 122, 101, 100, 76, 105, 115, 116, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 25, 84, 222, 87, 205, 85, 204, 145, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 114, 97, 99, 107, 101, 116, 101, 100, 76, 105, 115, 116, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 25, 84, 222, 87, 205, 85, 204, 145, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 105, 110, 97, 114, 121, 76, 105, 116, 101, 114, 97, 108, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_91cc55cd57de5419 = b_91cc55cd57de5419.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_91cc55cd57de5419[] = { &s_91cc55cd57de5419, }; static const uint16_t m_91cc55cd57de5419[] = {9, 6, 8, 3, 0, 2, 4, 5, 7, 1}; static const uint16_t i_91cc55cd57de5419[] = {0, 1, 2, 3, 4, 5, 6, 9, 7, 8}; const ::capnp::_::RawSchema s_91cc55cd57de5419 = { 0x91cc55cd57de5419, b_91cc55cd57de5419.words, 195, d_91cc55cd57de5419, m_91cc55cd57de5419, 1, 10, i_91cc55cd57de5419, nullptr, nullptr, { &s_91cc55cd57de5419, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<119> b_c6725e678d60fa37 = { { 0, 0, 0, 0, 5, 0, 6, 0, 55, 250, 96, 141, 103, 94, 114, 198, 27, 0, 0, 0, 1, 0, 2, 0, 238, 195, 31, 98, 210, 86, 57, 167, 3, 0, 7, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 42, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 87, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 108, 101, 120, 101, 114, 46, 99, 97, 112, 110, 112, 58, 83, 116, 97, 116, 101, 109, 101, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 24, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 0, 0, 0, 3, 0, 1, 0, 176, 0, 0, 0, 2, 0, 1, 0, 1, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 3, 0, 1, 0, 180, 0, 0, 0, 2, 0, 1, 0, 2, 0, 254, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 0, 0, 0, 3, 0, 1, 0, 200, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 0, 0, 0, 3, 0, 1, 0, 208, 0, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 205, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 0, 0, 0, 3, 0, 1, 0, 216, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 0, 0, 0, 3, 0, 1, 0, 220, 0, 0, 0, 2, 0, 1, 0, 116, 111, 107, 101, 110, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 25, 84, 222, 87, 205, 85, 204, 145, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 105, 110, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 108, 111, 99, 107, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 55, 250, 96, 141, 103, 94, 114, 198, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 111, 99, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c6725e678d60fa37 = b_c6725e678d60fa37.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c6725e678d60fa37[] = { &s_91cc55cd57de5419, &s_c6725e678d60fa37, }; static const uint16_t m_c6725e678d60fa37[] = {2, 3, 5, 1, 4, 0}; static const uint16_t i_c6725e678d60fa37[] = {1, 2, 0, 3, 4, 5}; const ::capnp::_::RawSchema s_c6725e678d60fa37 = { 0xc6725e678d60fa37, b_c6725e678d60fa37.words, 119, d_c6725e678d60fa37, m_c6725e678d60fa37, 2, 6, i_c6725e678d60fa37, nullptr, nullptr, { &s_c6725e678d60fa37, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<38> b_9e69a92512b19d18 = { { 0, 0, 0, 0, 5, 0, 6, 0, 24, 157, 177, 18, 37, 169, 105, 158, 27, 0, 0, 0, 1, 0, 0, 0, 238, 195, 31, 98, 210, 86, 57, 167, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 58, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 108, 101, 120, 101, 114, 46, 99, 97, 112, 110, 112, 58, 76, 101, 120, 101, 100, 84, 111, 107, 101, 110, 115, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 36, 0, 0, 0, 2, 0, 1, 0, 116, 111, 107, 101, 110, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 25, 84, 222, 87, 205, 85, 204, 145, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9e69a92512b19d18 = b_9e69a92512b19d18.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9e69a92512b19d18[] = { &s_91cc55cd57de5419, }; static const uint16_t m_9e69a92512b19d18[] = {0}; static const uint16_t i_9e69a92512b19d18[] = {0}; const ::capnp::_::RawSchema s_9e69a92512b19d18 = { 0x9e69a92512b19d18, b_9e69a92512b19d18.words, 38, d_9e69a92512b19d18, m_9e69a92512b19d18, 1, 1, i_9e69a92512b19d18, nullptr, nullptr, { &s_9e69a92512b19d18, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<40> b_a11f97b9d6c73dd4 = { { 0, 0, 0, 0, 5, 0, 6, 0, 212, 61, 199, 214, 185, 151, 31, 161, 27, 0, 0, 0, 1, 0, 0, 0, 238, 195, 31, 98, 210, 86, 57, 167, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 90, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 108, 101, 120, 101, 114, 46, 99, 97, 112, 110, 112, 58, 76, 101, 120, 101, 100, 83, 116, 97, 116, 101, 109, 101, 110, 116, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 40, 0, 0, 0, 2, 0, 1, 0, 115, 116, 97, 116, 101, 109, 101, 110, 116, 115, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 55, 250, 96, 141, 103, 94, 114, 198, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_a11f97b9d6c73dd4 = b_a11f97b9d6c73dd4.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_a11f97b9d6c73dd4[] = { &s_c6725e678d60fa37, }; static const uint16_t m_a11f97b9d6c73dd4[] = {0}; static const uint16_t i_a11f97b9d6c73dd4[] = {0}; const ::capnp::_::RawSchema s_a11f97b9d6c73dd4 = { 0xa11f97b9d6c73dd4, b_a11f97b9d6c73dd4.words, 40, d_a11f97b9d6c73dd4, m_a11f97b9d6c73dd4, 1, 1, i_a11f97b9d6c73dd4, nullptr, nullptr, { &s_a11f97b9d6c73dd4, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { namespace compiler { // Token constexpr uint16_t Token::_capnpPrivate::dataWordSize; constexpr uint16_t Token::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Token::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Token::_capnpPrivate::schema; #endif // !CAPNP_LITE // Statement constexpr uint16_t Statement::_capnpPrivate::dataWordSize; constexpr uint16_t Statement::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Statement::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Statement::_capnpPrivate::schema; #endif // !CAPNP_LITE // LexedTokens constexpr uint16_t LexedTokens::_capnpPrivate::dataWordSize; constexpr uint16_t LexedTokens::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind LexedTokens::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* LexedTokens::_capnpPrivate::schema; #endif // !CAPNP_LITE // LexedStatements constexpr uint16_t LexedStatements::_capnpPrivate::dataWordSize; constexpr uint16_t LexedStatements::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind LexedStatements::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* LexedStatements::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/compiler/lexer.h0000644000175000017500000000733613650101756021757 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include #include "error-reporter.h" CAPNP_BEGIN_HEADER namespace capnp { namespace compiler { bool lex(kj::ArrayPtr input, LexedStatements::Builder result, ErrorReporter& errorReporter); bool lex(kj::ArrayPtr input, LexedTokens::Builder result, ErrorReporter& errorReporter); // Lex the given source code, placing the results in `result`. Returns true if there // were no errors, false if there were. Even when errors are present, the file may have partial // content which can be fed into later stages of parsing in order to find more errors. // // There are two versions, one that parses a list of statements, and one which just parses tokens // that might form a part of one statement. In other words, in the later case, the input should // not contain semicolons or curly braces, unless they are in string literals of course. class Lexer { // Advanced lexer interface. This interface exposes the inner parsers so that you can embed them // into your own parsers. public: Lexer(Orphanage orphanage, ErrorReporter& errorReporter); // `orphanage` is used to allocate Cap'n Proto message objects in the result. `inputStart` is // a pointer to the beginning of the input, used to compute byte offsets. ~Lexer() noexcept(false); class ParserInput: public kj::parse::IteratorInput { // Like IteratorInput except that positions are measured as byte offsets // rather than pointers. public: ParserInput(const char* begin, const char* end) : IteratorInput(begin, end), begin(begin) {} explicit ParserInput(ParserInput& parent) : IteratorInput(parent), begin(parent.begin) {} inline uint32_t getBest() { return IteratorInput::getBest() - begin; } inline uint32_t getPosition() { return IteratorInput::getPosition() - begin; } private: const char* begin; }; template using Parser = kj::parse::ParserRef; struct Parsers { Parser> emptySpace; Parser> token; Parser>> tokenSequence; Parser> statement; Parser>> statementSequence; }; const Parsers& getParsers() { return parsers; } private: Orphanage orphanage; kj::Arena arena; Parsers parsers; }; } // namespace compiler } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/compiler/lexer.c++0000644000175000017500000002746613340402540022075 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "lexer.h" #include #include namespace capnp { namespace compiler { namespace p = kj::parse; bool lex(kj::ArrayPtr input, LexedStatements::Builder result, ErrorReporter& errorReporter) { Lexer lexer(Orphanage::getForMessageContaining(result), errorReporter); auto parser = p::sequence(lexer.getParsers().statementSequence, p::endOfInput); Lexer::ParserInput parserInput(input.begin(), input.end()); kj::Maybe>> parseOutput = parser(parserInput); KJ_IF_MAYBE(output, parseOutput) { auto l = result.initStatements(output->size()); for (uint i = 0; i < output->size(); i++) { l.adoptWithCaveats(i, kj::mv((*output)[i])); } return true; } else { uint32_t best = parserInput.getBest(); errorReporter.addError(best, best, kj::str("Parse error.")); return false; } } bool lex(kj::ArrayPtr input, LexedTokens::Builder result, ErrorReporter& errorReporter) { Lexer lexer(Orphanage::getForMessageContaining(result), errorReporter); auto parser = p::sequence(lexer.getParsers().tokenSequence, p::endOfInput); Lexer::ParserInput parserInput(input.begin(), input.end()); kj::Maybe>> parseOutput = parser(parserInput); KJ_IF_MAYBE(output, parseOutput) { auto l = result.initTokens(output->size()); for (uint i = 0; i < output->size(); i++) { l.adoptWithCaveats(i, kj::mv((*output)[i])); } return true; } else { uint32_t best = parserInput.getBest(); errorReporter.addError(best, best, kj::str("Parse error.")); return false; } } namespace { typedef p::Span Location; Token::Builder initTok(Orphan& t, const Location& loc) { auto builder = t.get(); builder.setStartByte(loc.begin()); builder.setEndByte(loc.end()); return builder; } void buildTokenSequenceList(List>::Builder builder, kj::Array>>&& items) { for (uint i = 0; i < items.size(); i++) { auto& item = items[i]; auto itemBuilder = builder.init(i, item.size()); for (uint j = 0; j < item.size(); j++) { itemBuilder.adoptWithCaveats(j, kj::mv(item[j])); } } } void attachDocComment(Statement::Builder statement, kj::Array&& comment) { size_t size = 0; for (auto& line: comment) { size += line.size() + 1; // include newline } Text::Builder builder = statement.initDocComment(size); char* pos = builder.begin(); for (auto& line: comment) { memcpy(pos, line.begin(), line.size()); pos += line.size(); *pos++ = '\n'; } KJ_ASSERT(pos == builder.end()); } constexpr auto discardComment = sequence(p::exactChar<'#'>(), p::discard(p::many(p::discard(p::anyOfChars("\n").invert()))), p::oneOf(p::exactChar<'\n'>(), p::endOfInput)); constexpr auto saveComment = sequence(p::exactChar<'#'>(), p::discard(p::optional(p::exactChar<' '>())), p::charsToString(p::many(p::anyOfChars("\n").invert())), p::oneOf(p::exactChar<'\n'>(), p::endOfInput)); constexpr auto utf8Bom = sequence(p::exactChar<'\xef'>(), p::exactChar<'\xbb'>(), p::exactChar<'\xbf'>()); constexpr auto bomsAndWhitespace = sequence(p::discardWhitespace, p::discard(p::many(sequence(utf8Bom, p::discardWhitespace)))); constexpr auto commentsAndWhitespace = sequence(bomsAndWhitespace, p::discard(p::many(sequence(discardComment, bomsAndWhitespace)))); constexpr auto discardLineWhitespace = p::discard(p::many(p::discard(p::whitespaceChar.invert().orAny("\r\n").invert()))); constexpr auto newline = p::oneOf( p::exactChar<'\n'>(), sequence(p::exactChar<'\r'>(), p::discard(p::optional(p::exactChar<'\n'>())))); constexpr auto docComment = p::optional(p::sequence( discardLineWhitespace, p::discard(p::optional(newline)), p::oneOrMore(p::sequence(discardLineWhitespace, saveComment)))); // Parses a set of comment lines preceded by at most one newline and with no intervening blank // lines. } // namespace Lexer::Lexer(Orphanage orphanageParam, ErrorReporter& errorReporter) : orphanage(orphanageParam) { // Note that because passing an lvalue to a parser constructor uses it by-referencee, it's safe // for us to use parsers.tokenSequence even though we haven't yet constructed it. auto& tokenSequence = parsers.tokenSequence; auto& commaDelimitedList = arena.copy(p::transform( p::sequence(tokenSequence, p::many(p::sequence(p::exactChar<','>(), tokenSequence))), [](kj::Array>&& first, kj::Array>>&& rest) -> kj::Array>> { if (first == nullptr && rest == nullptr) { // Completely empty list. return nullptr; } else { uint restSize = rest.size(); if (restSize > 0 && rest[restSize - 1] == nullptr) { // Allow for trailing commas by shortening the list by one item if the final token is // nullptr restSize--; } auto result = kj::heapArrayBuilder>>(1 + restSize); // first+rest result.add(kj::mv(first)); for (uint i = 0; i < restSize ; i++) { result.add(kj::mv(rest[i])); } return result.finish(); } })); auto& token = arena.copy(p::oneOf( p::transformWithLocation(p::identifier, [this](Location loc, kj::String name) -> Orphan { auto t = orphanage.newOrphan(); initTok(t, loc).setIdentifier(name); return t; }), p::transformWithLocation(p::doubleQuotedString, [this](Location loc, kj::String text) -> Orphan { auto t = orphanage.newOrphan(); initTok(t, loc).setStringLiteral(text); return t; }), p::transformWithLocation(p::doubleQuotedHexBinary, [this](Location loc, kj::Array data) -> Orphan { auto t = orphanage.newOrphan(); initTok(t, loc).setBinaryLiteral(data); return t; }), p::transformWithLocation(p::integer, [this](Location loc, uint64_t i) -> Orphan { auto t = orphanage.newOrphan(); initTok(t, loc).setIntegerLiteral(i); return t; }), p::transformWithLocation(p::number, [this](Location loc, double x) -> Orphan { auto t = orphanage.newOrphan(); initTok(t, loc).setFloatLiteral(x); return t; }), p::transformWithLocation( p::charsToString(p::oneOrMore(p::anyOfChars("!$%&*+-./:<=>?@^|~"))), [this](Location loc, kj::String text) -> Orphan { auto t = orphanage.newOrphan(); initTok(t, loc).setOperator(text); return t; }), p::transformWithLocation( sequence(p::exactChar<'('>(), commaDelimitedList, p::exactChar<')'>()), [this](Location loc, kj::Array>>&& items) -> Orphan { auto t = orphanage.newOrphan(); buildTokenSequenceList( initTok(t, loc).initParenthesizedList(items.size()), kj::mv(items)); return t; }), p::transformWithLocation( sequence(p::exactChar<'['>(), commaDelimitedList, p::exactChar<']'>()), [this](Location loc, kj::Array>>&& items) -> Orphan { auto t = orphanage.newOrphan(); buildTokenSequenceList( initTok(t, loc).initBracketedList(items.size()), kj::mv(items)); return t; }), p::transformOrReject(p::transformWithLocation( p::oneOf(sequence(p::exactChar<'\xff'>(), p::exactChar<'\xfe'>()), sequence(p::exactChar<'\xfe'>(), p::exactChar<'\xff'>()), sequence(p::exactChar<'\x00'>())), [&errorReporter](Location loc) -> kj::Maybe> { errorReporter.addError(loc.begin(), loc.end(), "Non-UTF-8 input detected. Cap'n Proto schema files must be UTF-8 text."); return nullptr; }), [](kj::Maybe> param) { return param; }))); parsers.tokenSequence = arena.copy(p::sequence( commentsAndWhitespace, p::many(p::sequence(token, commentsAndWhitespace)))); auto& statementSequence = parsers.statementSequence; auto& statementEnd = arena.copy(p::oneOf( transform(p::sequence(p::exactChar<';'>(), docComment), [this](kj::Maybe>&& comment) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); KJ_IF_MAYBE(c, comment) { attachDocComment(builder, kj::mv(*c)); } builder.setLine(); return result; }), transform( p::sequence(p::exactChar<'{'>(), docComment, statementSequence, p::exactChar<'}'>(), docComment), [this](kj::Maybe>&& comment, kj::Array>&& statements, kj::Maybe>&& lateComment) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); KJ_IF_MAYBE(c, comment) { attachDocComment(builder, kj::mv(*c)); } else KJ_IF_MAYBE(c, lateComment) { attachDocComment(builder, kj::mv(*c)); } auto list = builder.initBlock(statements.size()); for (uint i = 0; i < statements.size(); i++) { list.adoptWithCaveats(i, kj::mv(statements[i])); } return result; }) )); auto& statement = arena.copy(p::transformWithLocation(p::sequence(tokenSequence, statementEnd), [](Location loc, kj::Array>&& tokens, Orphan&& statement) { auto builder = statement.get(); auto tokensBuilder = builder.initTokens(tokens.size()); for (uint i = 0; i < tokens.size(); i++) { tokensBuilder.adoptWithCaveats(i, kj::mv(tokens[i])); } builder.setStartByte(loc.begin()); builder.setEndByte(loc.end()); return kj::mv(statement); })); parsers.statementSequence = arena.copy(sequence( commentsAndWhitespace, many(sequence(statement, commentsAndWhitespace)))); parsers.token = token; parsers.statement = statement; parsers.emptySpace = commentsAndWhitespace; } Lexer::~Lexer() noexcept(false) {} } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/grammar.capnp.h0000644000175000017500000105461513650317744023377 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: grammar.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(e75816b56529d464); CAPNP_DECLARE_SCHEMA(991c7a3693d62cf2); CAPNP_DECLARE_SCHEMA(90f2a60678fd2367); CAPNP_DECLARE_SCHEMA(8e207d4dfe54d0de); CAPNP_DECLARE_SCHEMA(c90246b71adedbaa); CAPNP_DECLARE_SCHEMA(aee8397040b0df7a); CAPNP_DECLARE_SCHEMA(aa28e1400d793359); CAPNP_DECLARE_SCHEMA(96efe787c17e83bb); CAPNP_DECLARE_SCHEMA(d5e71144af1ce175); CAPNP_DECLARE_SCHEMA(d00489d473826290); CAPNP_DECLARE_SCHEMA(fb5aeed95cdf6af9); CAPNP_DECLARE_SCHEMA(94099c3f9eb32d6b); CAPNP_DECLARE_SCHEMA(b3f66e7a79d81bcd); CAPNP_DECLARE_SCHEMA(fffe08a9a697d2a5); CAPNP_DECLARE_SCHEMA(e5104515fd88ea47); CAPNP_DECLARE_SCHEMA(89f0c973c103ae96); CAPNP_DECLARE_SCHEMA(e93164a80bfe2ccf); CAPNP_DECLARE_SCHEMA(b348322a8dcf0d0c); CAPNP_DECLARE_SCHEMA(8f2622208fb358c8); CAPNP_DECLARE_SCHEMA(d0d1a21de617951f); CAPNP_DECLARE_SCHEMA(992a90eaf30235d3); CAPNP_DECLARE_SCHEMA(eb971847d617c0b9); CAPNP_DECLARE_SCHEMA(c6238c7d62d65173); CAPNP_DECLARE_SCHEMA(9cb9e86e3198037f); CAPNP_DECLARE_SCHEMA(84e4f3f5a807605c); } // namespace schemas } // namespace capnp namespace capnp { namespace compiler { struct LocatedText { LocatedText() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e75816b56529d464, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct LocatedInteger { LocatedInteger() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(991c7a3693d62cf2, 2, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct LocatedFloat { LocatedFloat() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(90f2a60678fd2367, 2, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Expression { Expression() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { UNKNOWN, POSITIVE_INT, NEGATIVE_INT, FLOAT, STRING, RELATIVE_NAME, LIST, TUPLE, BINARY, APPLICATION, MEMBER, ABSOLUTE_NAME, IMPORT, EMBED, }; struct Param; struct Application; struct Member; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(8e207d4dfe54d0de, 3, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Expression::Param { Param() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { UNNAMED, NAMED, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c90246b71adedbaa, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Expression::Application { Application() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(aee8397040b0df7a, 3, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Expression::Member { Member() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(aa28e1400d793359, 3, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration { Declaration() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { FILE, USING, CONST, ENUM, ENUMERANT, STRUCT, FIELD, UNION, GROUP, INTERFACE, METHOD, ANNOTATION, NAKED_ID, NAKED_ANNOTATION, BUILTIN_VOID, BUILTIN_BOOL, BUILTIN_INT8, BUILTIN_INT16, BUILTIN_INT32, BUILTIN_INT64, BUILTIN_U_INT8, BUILTIN_U_INT16, BUILTIN_U_INT32, BUILTIN_U_INT64, BUILTIN_FLOAT32, BUILTIN_FLOAT64, BUILTIN_TEXT, BUILTIN_DATA, BUILTIN_LIST, BUILTIN_OBJECT, BUILTIN_ANY_POINTER, BUILTIN_ANY_STRUCT, BUILTIN_ANY_LIST, BUILTIN_CAPABILITY, }; struct BrandParameter; struct AnnotationApplication; struct ParamList; struct Param; struct Id; struct Using; struct Const; struct Field; struct Interface; struct Method; struct Annotation; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(96efe787c17e83bb, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::BrandParameter { BrandParameter() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d5e71144af1ce175, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::AnnotationApplication { AnnotationApplication() = delete; class Reader; class Builder; class Pipeline; struct Value; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d00489d473826290, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::AnnotationApplication::Value { Value() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NONE, EXPRESSION, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(fb5aeed95cdf6af9, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::ParamList { ParamList() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NAMED_LIST, TYPE, STREAM, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b3f66e7a79d81bcd, 2, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Param { Param() = delete; class Reader; class Builder; class Pipeline; struct DefaultValue; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(fffe08a9a697d2a5, 2, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Param::DefaultValue { DefaultValue() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NONE, VALUE, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e5104515fd88ea47, 2, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Id { Id() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { UNSPECIFIED, UID, ORDINAL, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(89f0c973c103ae96, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Using { Using() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e93164a80bfe2ccf, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Const { Const() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b348322a8dcf0d0c, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Field { Field() = delete; class Reader; class Builder; class Pipeline; struct DefaultValue; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(8f2622208fb358c8, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Field::DefaultValue { DefaultValue() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NONE, VALUE, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d0d1a21de617951f, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Interface { Interface() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(992a90eaf30235d3, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Method { Method() = delete; class Reader; class Builder; class Pipeline; struct Results; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(eb971847d617c0b9, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Method::Results { Results() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NONE, EXPLICIT, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c6238c7d62d65173, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Declaration::Annotation { Annotation() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9cb9e86e3198037f, 2, 8) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct ParsedFile { ParsedFile() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(84e4f3f5a807605c, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class LocatedText::Reader { public: typedef LocatedText Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasValue() const; inline ::capnp::Text::Reader getValue() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class LocatedText::Builder { public: typedef LocatedText Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasValue(); inline ::capnp::Text::Builder getValue(); inline void setValue( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initValue(unsigned int size); inline void adoptValue(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownValue(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class LocatedText::Pipeline { public: typedef LocatedText Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class LocatedInteger::Reader { public: typedef LocatedInteger Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getValue() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class LocatedInteger::Builder { public: typedef LocatedInteger Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getValue(); inline void setValue( ::uint64_t value); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class LocatedInteger::Pipeline { public: typedef LocatedInteger Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class LocatedFloat::Reader { public: typedef LocatedFloat Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline double getValue() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class LocatedFloat::Builder { public: typedef LocatedFloat Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline double getValue(); inline void setValue(double value); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class LocatedFloat::Pipeline { public: typedef LocatedFloat Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Expression::Reader { public: typedef Expression Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isUnknown() const; inline ::capnp::Void getUnknown() const; inline bool isPositiveInt() const; inline ::uint64_t getPositiveInt() const; inline bool isNegativeInt() const; inline ::uint64_t getNegativeInt() const; inline bool isFloat() const; inline double getFloat() const; inline bool isString() const; inline bool hasString() const; inline ::capnp::Text::Reader getString() const; inline bool isRelativeName() const; inline bool hasRelativeName() const; inline ::capnp::compiler::LocatedText::Reader getRelativeName() const; inline bool isList() const; inline bool hasList() const; inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader getList() const; inline bool isTuple() const; inline bool hasTuple() const; inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader getTuple() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; inline bool isBinary() const; inline bool hasBinary() const; inline ::capnp::Data::Reader getBinary() const; inline bool isApplication() const; inline typename Application::Reader getApplication() const; inline bool isMember() const; inline typename Member::Reader getMember() const; inline bool isAbsoluteName() const; inline bool hasAbsoluteName() const; inline ::capnp::compiler::LocatedText::Reader getAbsoluteName() const; inline bool isImport() const; inline bool hasImport() const; inline ::capnp::compiler::LocatedText::Reader getImport() const; inline bool isEmbed() const; inline bool hasEmbed() const; inline ::capnp::compiler::LocatedText::Reader getEmbed() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Expression::Builder { public: typedef Expression Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isUnknown(); inline ::capnp::Void getUnknown(); inline void setUnknown( ::capnp::Void value = ::capnp::VOID); inline bool isPositiveInt(); inline ::uint64_t getPositiveInt(); inline void setPositiveInt( ::uint64_t value); inline bool isNegativeInt(); inline ::uint64_t getNegativeInt(); inline void setNegativeInt( ::uint64_t value); inline bool isFloat(); inline double getFloat(); inline void setFloat(double value); inline bool isString(); inline bool hasString(); inline ::capnp::Text::Builder getString(); inline void setString( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initString(unsigned int size); inline void adoptString(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownString(); inline bool isRelativeName(); inline bool hasRelativeName(); inline ::capnp::compiler::LocatedText::Builder getRelativeName(); inline void setRelativeName( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initRelativeName(); inline void adoptRelativeName(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownRelativeName(); inline bool isList(); inline bool hasList(); inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder getList(); inline void setList( ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder initList(unsigned int size); inline void adoptList(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>> disownList(); inline bool isTuple(); inline bool hasTuple(); inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder getTuple(); inline void setTuple( ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder initTuple(unsigned int size); inline void adoptTuple(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>> disownTuple(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); inline bool isBinary(); inline bool hasBinary(); inline ::capnp::Data::Builder getBinary(); inline void setBinary( ::capnp::Data::Reader value); inline ::capnp::Data::Builder initBinary(unsigned int size); inline void adoptBinary(::capnp::Orphan< ::capnp::Data>&& value); inline ::capnp::Orphan< ::capnp::Data> disownBinary(); inline bool isApplication(); inline typename Application::Builder getApplication(); inline typename Application::Builder initApplication(); inline bool isMember(); inline typename Member::Builder getMember(); inline typename Member::Builder initMember(); inline bool isAbsoluteName(); inline bool hasAbsoluteName(); inline ::capnp::compiler::LocatedText::Builder getAbsoluteName(); inline void setAbsoluteName( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initAbsoluteName(); inline void adoptAbsoluteName(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownAbsoluteName(); inline bool isImport(); inline bool hasImport(); inline ::capnp::compiler::LocatedText::Builder getImport(); inline void setImport( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initImport(); inline void adoptImport(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownImport(); inline bool isEmbed(); inline bool hasEmbed(); inline ::capnp::compiler::LocatedText::Builder getEmbed(); inline void setEmbed( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initEmbed(); inline void adoptEmbed(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownEmbed(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Expression::Pipeline { public: typedef Expression Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Expression::Param::Reader { public: typedef Param Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isUnnamed() const; inline ::capnp::Void getUnnamed() const; inline bool isNamed() const; inline bool hasNamed() const; inline ::capnp::compiler::LocatedText::Reader getNamed() const; inline bool hasValue() const; inline ::capnp::compiler::Expression::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Expression::Param::Builder { public: typedef Param Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isUnnamed(); inline ::capnp::Void getUnnamed(); inline void setUnnamed( ::capnp::Void value = ::capnp::VOID); inline bool isNamed(); inline bool hasNamed(); inline ::capnp::compiler::LocatedText::Builder getNamed(); inline void setNamed( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initNamed(); inline void adoptNamed(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownNamed(); inline bool hasValue(); inline ::capnp::compiler::Expression::Builder getValue(); inline void setValue( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Expression::Param::Pipeline { public: typedef Param Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Expression::Application::Reader { public: typedef Application Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasFunction() const; inline ::capnp::compiler::Expression::Reader getFunction() const; inline bool hasParams() const; inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader getParams() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Expression::Application::Builder { public: typedef Application Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasFunction(); inline ::capnp::compiler::Expression::Builder getFunction(); inline void setFunction( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initFunction(); inline void adoptFunction(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownFunction(); inline bool hasParams(); inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder getParams(); inline void setParams( ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder initParams(unsigned int size); inline void adoptParams(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>> disownParams(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Expression::Application::Pipeline { public: typedef Application Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getFunction(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Expression::Member::Reader { public: typedef Member Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasParent() const; inline ::capnp::compiler::Expression::Reader getParent() const; inline bool hasName() const; inline ::capnp::compiler::LocatedText::Reader getName() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Expression::Member::Builder { public: typedef Member Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasParent(); inline ::capnp::compiler::Expression::Builder getParent(); inline void setParent( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initParent(); inline void adoptParent(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownParent(); inline bool hasName(); inline ::capnp::compiler::LocatedText::Builder getName(); inline void setName( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initName(); inline void adoptName(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownName(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Expression::Member::Pipeline { public: typedef Member Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getParent(); inline ::capnp::compiler::LocatedText::Pipeline getName(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Reader { public: typedef Declaration Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool hasName() const; inline ::capnp::compiler::LocatedText::Reader getName() const; inline typename Id::Reader getId() const; inline bool hasNestedDecls() const; inline ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Reader getNestedDecls() const; inline bool hasAnnotations() const; inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader getAnnotations() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; inline bool hasDocComment() const; inline ::capnp::Text::Reader getDocComment() const; inline bool isFile() const; inline ::capnp::Void getFile() const; inline bool isUsing() const; inline typename Using::Reader getUsing() const; inline bool isConst() const; inline typename Const::Reader getConst() const; inline bool isEnum() const; inline ::capnp::Void getEnum() const; inline bool isEnumerant() const; inline ::capnp::Void getEnumerant() const; inline bool isStruct() const; inline ::capnp::Void getStruct() const; inline bool isField() const; inline typename Field::Reader getField() const; inline bool isUnion() const; inline ::capnp::Void getUnion() const; inline bool isGroup() const; inline ::capnp::Void getGroup() const; inline bool isInterface() const; inline typename Interface::Reader getInterface() const; inline bool isMethod() const; inline typename Method::Reader getMethod() const; inline bool isAnnotation() const; inline typename Annotation::Reader getAnnotation() const; inline bool isNakedId() const; inline bool hasNakedId() const; inline ::capnp::compiler::LocatedInteger::Reader getNakedId() const; inline bool isNakedAnnotation() const; inline bool hasNakedAnnotation() const; inline ::capnp::compiler::Declaration::AnnotationApplication::Reader getNakedAnnotation() const; inline bool isBuiltinVoid() const; inline ::capnp::Void getBuiltinVoid() const; inline bool isBuiltinBool() const; inline ::capnp::Void getBuiltinBool() const; inline bool isBuiltinInt8() const; inline ::capnp::Void getBuiltinInt8() const; inline bool isBuiltinInt16() const; inline ::capnp::Void getBuiltinInt16() const; inline bool isBuiltinInt32() const; inline ::capnp::Void getBuiltinInt32() const; inline bool isBuiltinInt64() const; inline ::capnp::Void getBuiltinInt64() const; inline bool isBuiltinUInt8() const; inline ::capnp::Void getBuiltinUInt8() const; inline bool isBuiltinUInt16() const; inline ::capnp::Void getBuiltinUInt16() const; inline bool isBuiltinUInt32() const; inline ::capnp::Void getBuiltinUInt32() const; inline bool isBuiltinUInt64() const; inline ::capnp::Void getBuiltinUInt64() const; inline bool isBuiltinFloat32() const; inline ::capnp::Void getBuiltinFloat32() const; inline bool isBuiltinFloat64() const; inline ::capnp::Void getBuiltinFloat64() const; inline bool isBuiltinText() const; inline ::capnp::Void getBuiltinText() const; inline bool isBuiltinData() const; inline ::capnp::Void getBuiltinData() const; inline bool isBuiltinList() const; inline ::capnp::Void getBuiltinList() const; inline bool isBuiltinObject() const; inline ::capnp::Void getBuiltinObject() const; inline bool isBuiltinAnyPointer() const; inline ::capnp::Void getBuiltinAnyPointer() const; inline bool hasParameters() const; inline ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Reader getParameters() const; inline bool isBuiltinAnyStruct() const; inline ::capnp::Void getBuiltinAnyStruct() const; inline bool isBuiltinAnyList() const; inline ::capnp::Void getBuiltinAnyList() const; inline bool isBuiltinCapability() const; inline ::capnp::Void getBuiltinCapability() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Builder { public: typedef Declaration Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool hasName(); inline ::capnp::compiler::LocatedText::Builder getName(); inline void setName( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initName(); inline void adoptName(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownName(); inline typename Id::Builder getId(); inline typename Id::Builder initId(); inline bool hasNestedDecls(); inline ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Builder getNestedDecls(); inline void setNestedDecls( ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Builder initNestedDecls(unsigned int size); inline void adoptNestedDecls(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>> disownNestedDecls(); inline bool hasAnnotations(); inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder getAnnotations(); inline void setAnnotations( ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder initAnnotations(unsigned int size); inline void adoptAnnotations(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>> disownAnnotations(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); inline bool hasDocComment(); inline ::capnp::Text::Builder getDocComment(); inline void setDocComment( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initDocComment(unsigned int size); inline void adoptDocComment(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownDocComment(); inline bool isFile(); inline ::capnp::Void getFile(); inline void setFile( ::capnp::Void value = ::capnp::VOID); inline bool isUsing(); inline typename Using::Builder getUsing(); inline typename Using::Builder initUsing(); inline bool isConst(); inline typename Const::Builder getConst(); inline typename Const::Builder initConst(); inline bool isEnum(); inline ::capnp::Void getEnum(); inline void setEnum( ::capnp::Void value = ::capnp::VOID); inline bool isEnumerant(); inline ::capnp::Void getEnumerant(); inline void setEnumerant( ::capnp::Void value = ::capnp::VOID); inline bool isStruct(); inline ::capnp::Void getStruct(); inline void setStruct( ::capnp::Void value = ::capnp::VOID); inline bool isField(); inline typename Field::Builder getField(); inline typename Field::Builder initField(); inline bool isUnion(); inline ::capnp::Void getUnion(); inline void setUnion( ::capnp::Void value = ::capnp::VOID); inline bool isGroup(); inline ::capnp::Void getGroup(); inline void setGroup( ::capnp::Void value = ::capnp::VOID); inline bool isInterface(); inline typename Interface::Builder getInterface(); inline typename Interface::Builder initInterface(); inline bool isMethod(); inline typename Method::Builder getMethod(); inline typename Method::Builder initMethod(); inline bool isAnnotation(); inline typename Annotation::Builder getAnnotation(); inline typename Annotation::Builder initAnnotation(); inline bool isNakedId(); inline bool hasNakedId(); inline ::capnp::compiler::LocatedInteger::Builder getNakedId(); inline void setNakedId( ::capnp::compiler::LocatedInteger::Reader value); inline ::capnp::compiler::LocatedInteger::Builder initNakedId(); inline void adoptNakedId(::capnp::Orphan< ::capnp::compiler::LocatedInteger>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedInteger> disownNakedId(); inline bool isNakedAnnotation(); inline bool hasNakedAnnotation(); inline ::capnp::compiler::Declaration::AnnotationApplication::Builder getNakedAnnotation(); inline void setNakedAnnotation( ::capnp::compiler::Declaration::AnnotationApplication::Reader value); inline ::capnp::compiler::Declaration::AnnotationApplication::Builder initNakedAnnotation(); inline void adoptNakedAnnotation(::capnp::Orphan< ::capnp::compiler::Declaration::AnnotationApplication>&& value); inline ::capnp::Orphan< ::capnp::compiler::Declaration::AnnotationApplication> disownNakedAnnotation(); inline bool isBuiltinVoid(); inline ::capnp::Void getBuiltinVoid(); inline void setBuiltinVoid( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinBool(); inline ::capnp::Void getBuiltinBool(); inline void setBuiltinBool( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinInt8(); inline ::capnp::Void getBuiltinInt8(); inline void setBuiltinInt8( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinInt16(); inline ::capnp::Void getBuiltinInt16(); inline void setBuiltinInt16( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinInt32(); inline ::capnp::Void getBuiltinInt32(); inline void setBuiltinInt32( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinInt64(); inline ::capnp::Void getBuiltinInt64(); inline void setBuiltinInt64( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinUInt8(); inline ::capnp::Void getBuiltinUInt8(); inline void setBuiltinUInt8( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinUInt16(); inline ::capnp::Void getBuiltinUInt16(); inline void setBuiltinUInt16( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinUInt32(); inline ::capnp::Void getBuiltinUInt32(); inline void setBuiltinUInt32( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinUInt64(); inline ::capnp::Void getBuiltinUInt64(); inline void setBuiltinUInt64( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinFloat32(); inline ::capnp::Void getBuiltinFloat32(); inline void setBuiltinFloat32( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinFloat64(); inline ::capnp::Void getBuiltinFloat64(); inline void setBuiltinFloat64( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinText(); inline ::capnp::Void getBuiltinText(); inline void setBuiltinText( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinData(); inline ::capnp::Void getBuiltinData(); inline void setBuiltinData( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinList(); inline ::capnp::Void getBuiltinList(); inline void setBuiltinList( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinObject(); inline ::capnp::Void getBuiltinObject(); inline void setBuiltinObject( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinAnyPointer(); inline ::capnp::Void getBuiltinAnyPointer(); inline void setBuiltinAnyPointer( ::capnp::Void value = ::capnp::VOID); inline bool hasParameters(); inline ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Builder getParameters(); inline void setParameters( ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Builder initParameters(unsigned int size); inline void adoptParameters(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>> disownParameters(); inline bool isBuiltinAnyStruct(); inline ::capnp::Void getBuiltinAnyStruct(); inline void setBuiltinAnyStruct( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinAnyList(); inline ::capnp::Void getBuiltinAnyList(); inline void setBuiltinAnyList( ::capnp::Void value = ::capnp::VOID); inline bool isBuiltinCapability(); inline ::capnp::Void getBuiltinCapability(); inline void setBuiltinCapability( ::capnp::Void value = ::capnp::VOID); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Pipeline { public: typedef Declaration Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::LocatedText::Pipeline getName(); inline typename Id::Pipeline getId(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::BrandParameter::Reader { public: typedef BrandParameter Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::BrandParameter::Builder { public: typedef BrandParameter Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::BrandParameter::Pipeline { public: typedef BrandParameter Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::AnnotationApplication::Reader { public: typedef AnnotationApplication Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::compiler::Expression::Reader getName() const; inline typename Value::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::AnnotationApplication::Builder { public: typedef AnnotationApplication Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::compiler::Expression::Builder getName(); inline void setName( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initName(); inline void adoptName(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownName(); inline typename Value::Builder getValue(); inline typename Value::Builder initValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::AnnotationApplication::Pipeline { public: typedef AnnotationApplication Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getName(); inline typename Value::Pipeline getValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::AnnotationApplication::Value::Reader { public: typedef Value Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNone() const; inline ::capnp::Void getNone() const; inline bool isExpression() const; inline bool hasExpression() const; inline ::capnp::compiler::Expression::Reader getExpression() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::AnnotationApplication::Value::Builder { public: typedef Value Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNone(); inline ::capnp::Void getNone(); inline void setNone( ::capnp::Void value = ::capnp::VOID); inline bool isExpression(); inline bool hasExpression(); inline ::capnp::compiler::Expression::Builder getExpression(); inline void setExpression( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initExpression(); inline void adoptExpression(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownExpression(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::AnnotationApplication::Value::Pipeline { public: typedef Value Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::ParamList::Reader { public: typedef ParamList Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNamedList() const; inline bool hasNamedList() const; inline ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Reader getNamedList() const; inline bool isType() const; inline bool hasType() const; inline ::capnp::compiler::Expression::Reader getType() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; inline bool isStream() const; inline ::capnp::Void getStream() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::ParamList::Builder { public: typedef ParamList Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNamedList(); inline bool hasNamedList(); inline ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Builder getNamedList(); inline void setNamedList( ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Builder initNamedList(unsigned int size); inline void adoptNamedList(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>> disownNamedList(); inline bool isType(); inline bool hasType(); inline ::capnp::compiler::Expression::Builder getType(); inline void setType( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownType(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); inline bool isStream(); inline ::capnp::Void getStream(); inline void setStream( ::capnp::Void value = ::capnp::VOID); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::ParamList::Pipeline { public: typedef ParamList Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Param::Reader { public: typedef Param Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::compiler::LocatedText::Reader getName() const; inline bool hasType() const; inline ::capnp::compiler::Expression::Reader getType() const; inline bool hasAnnotations() const; inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader getAnnotations() const; inline typename DefaultValue::Reader getDefaultValue() const; inline ::uint32_t getStartByte() const; inline ::uint32_t getEndByte() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Param::Builder { public: typedef Param Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::compiler::LocatedText::Builder getName(); inline void setName( ::capnp::compiler::LocatedText::Reader value); inline ::capnp::compiler::LocatedText::Builder initName(); inline void adoptName(::capnp::Orphan< ::capnp::compiler::LocatedText>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedText> disownName(); inline bool hasType(); inline ::capnp::compiler::Expression::Builder getType(); inline void setType( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownType(); inline bool hasAnnotations(); inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder getAnnotations(); inline void setAnnotations( ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder initAnnotations(unsigned int size); inline void adoptAnnotations(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>> disownAnnotations(); inline typename DefaultValue::Builder getDefaultValue(); inline typename DefaultValue::Builder initDefaultValue(); inline ::uint32_t getStartByte(); inline void setStartByte( ::uint32_t value); inline ::uint32_t getEndByte(); inline void setEndByte( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Param::Pipeline { public: typedef Param Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::LocatedText::Pipeline getName(); inline ::capnp::compiler::Expression::Pipeline getType(); inline typename DefaultValue::Pipeline getDefaultValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Param::DefaultValue::Reader { public: typedef DefaultValue Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNone() const; inline ::capnp::Void getNone() const; inline bool isValue() const; inline bool hasValue() const; inline ::capnp::compiler::Expression::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Param::DefaultValue::Builder { public: typedef DefaultValue Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNone(); inline ::capnp::Void getNone(); inline void setNone( ::capnp::Void value = ::capnp::VOID); inline bool isValue(); inline bool hasValue(); inline ::capnp::compiler::Expression::Builder getValue(); inline void setValue( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Param::DefaultValue::Pipeline { public: typedef DefaultValue Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Id::Reader { public: typedef Id Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isUnspecified() const; inline ::capnp::Void getUnspecified() const; inline bool isUid() const; inline bool hasUid() const; inline ::capnp::compiler::LocatedInteger::Reader getUid() const; inline bool isOrdinal() const; inline bool hasOrdinal() const; inline ::capnp::compiler::LocatedInteger::Reader getOrdinal() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Id::Builder { public: typedef Id Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isUnspecified(); inline ::capnp::Void getUnspecified(); inline void setUnspecified( ::capnp::Void value = ::capnp::VOID); inline bool isUid(); inline bool hasUid(); inline ::capnp::compiler::LocatedInteger::Builder getUid(); inline void setUid( ::capnp::compiler::LocatedInteger::Reader value); inline ::capnp::compiler::LocatedInteger::Builder initUid(); inline void adoptUid(::capnp::Orphan< ::capnp::compiler::LocatedInteger>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedInteger> disownUid(); inline bool isOrdinal(); inline bool hasOrdinal(); inline ::capnp::compiler::LocatedInteger::Builder getOrdinal(); inline void setOrdinal( ::capnp::compiler::LocatedInteger::Reader value); inline ::capnp::compiler::LocatedInteger::Builder initOrdinal(); inline void adoptOrdinal(::capnp::Orphan< ::capnp::compiler::LocatedInteger>&& value); inline ::capnp::Orphan< ::capnp::compiler::LocatedInteger> disownOrdinal(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Id::Pipeline { public: typedef Id Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Using::Reader { public: typedef Using Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasTarget() const; inline ::capnp::compiler::Expression::Reader getTarget() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Using::Builder { public: typedef Using Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasTarget(); inline ::capnp::compiler::Expression::Builder getTarget(); inline void setTarget( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initTarget(); inline void adoptTarget(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownTarget(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Using::Pipeline { public: typedef Using Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getTarget(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Const::Reader { public: typedef Const Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasType() const; inline ::capnp::compiler::Expression::Reader getType() const; inline bool hasValue() const; inline ::capnp::compiler::Expression::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Const::Builder { public: typedef Const Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasType(); inline ::capnp::compiler::Expression::Builder getType(); inline void setType( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownType(); inline bool hasValue(); inline ::capnp::compiler::Expression::Builder getValue(); inline void setValue( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Const::Pipeline { public: typedef Const Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getType(); inline ::capnp::compiler::Expression::Pipeline getValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Field::Reader { public: typedef Field Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasType() const; inline ::capnp::compiler::Expression::Reader getType() const; inline typename DefaultValue::Reader getDefaultValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Field::Builder { public: typedef Field Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasType(); inline ::capnp::compiler::Expression::Builder getType(); inline void setType( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownType(); inline typename DefaultValue::Builder getDefaultValue(); inline typename DefaultValue::Builder initDefaultValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Field::Pipeline { public: typedef Field Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getType(); inline typename DefaultValue::Pipeline getDefaultValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Field::DefaultValue::Reader { public: typedef DefaultValue Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNone() const; inline ::capnp::Void getNone() const; inline bool isValue() const; inline bool hasValue() const; inline ::capnp::compiler::Expression::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Field::DefaultValue::Builder { public: typedef DefaultValue Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNone(); inline ::capnp::Void getNone(); inline void setNone( ::capnp::Void value = ::capnp::VOID); inline bool isValue(); inline bool hasValue(); inline ::capnp::compiler::Expression::Builder getValue(); inline void setValue( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Field::DefaultValue::Pipeline { public: typedef DefaultValue Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Interface::Reader { public: typedef Interface Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasSuperclasses() const; inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader getSuperclasses() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Interface::Builder { public: typedef Interface Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasSuperclasses(); inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder getSuperclasses(); inline void setSuperclasses( ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder initSuperclasses(unsigned int size); inline void adoptSuperclasses(::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>> disownSuperclasses(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Interface::Pipeline { public: typedef Interface Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Method::Reader { public: typedef Method Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasParams() const; inline ::capnp::compiler::Declaration::ParamList::Reader getParams() const; inline typename Results::Reader getResults() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Method::Builder { public: typedef Method Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasParams(); inline ::capnp::compiler::Declaration::ParamList::Builder getParams(); inline void setParams( ::capnp::compiler::Declaration::ParamList::Reader value); inline ::capnp::compiler::Declaration::ParamList::Builder initParams(); inline void adoptParams(::capnp::Orphan< ::capnp::compiler::Declaration::ParamList>&& value); inline ::capnp::Orphan< ::capnp::compiler::Declaration::ParamList> disownParams(); inline typename Results::Builder getResults(); inline typename Results::Builder initResults(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Method::Pipeline { public: typedef Method Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Declaration::ParamList::Pipeline getParams(); inline typename Results::Pipeline getResults(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Method::Results::Reader { public: typedef Results Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNone() const; inline ::capnp::Void getNone() const; inline bool isExplicit() const; inline bool hasExplicit() const; inline ::capnp::compiler::Declaration::ParamList::Reader getExplicit() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Method::Results::Builder { public: typedef Results Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNone(); inline ::capnp::Void getNone(); inline void setNone( ::capnp::Void value = ::capnp::VOID); inline bool isExplicit(); inline bool hasExplicit(); inline ::capnp::compiler::Declaration::ParamList::Builder getExplicit(); inline void setExplicit( ::capnp::compiler::Declaration::ParamList::Reader value); inline ::capnp::compiler::Declaration::ParamList::Builder initExplicit(); inline void adoptExplicit(::capnp::Orphan< ::capnp::compiler::Declaration::ParamList>&& value); inline ::capnp::Orphan< ::capnp::compiler::Declaration::ParamList> disownExplicit(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Method::Results::Pipeline { public: typedef Results Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Declaration::Annotation::Reader { public: typedef Annotation Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasType() const; inline ::capnp::compiler::Expression::Reader getType() const; inline bool getTargetsFile() const; inline bool getTargetsConst() const; inline bool getTargetsEnum() const; inline bool getTargetsEnumerant() const; inline bool getTargetsStruct() const; inline bool getTargetsField() const; inline bool getTargetsUnion() const; inline bool getTargetsGroup() const; inline bool getTargetsInterface() const; inline bool getTargetsMethod() const; inline bool getTargetsParam() const; inline bool getTargetsAnnotation() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Declaration::Annotation::Builder { public: typedef Annotation Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasType(); inline ::capnp::compiler::Expression::Builder getType(); inline void setType( ::capnp::compiler::Expression::Reader value); inline ::capnp::compiler::Expression::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::compiler::Expression>&& value); inline ::capnp::Orphan< ::capnp::compiler::Expression> disownType(); inline bool getTargetsFile(); inline void setTargetsFile(bool value); inline bool getTargetsConst(); inline void setTargetsConst(bool value); inline bool getTargetsEnum(); inline void setTargetsEnum(bool value); inline bool getTargetsEnumerant(); inline void setTargetsEnumerant(bool value); inline bool getTargetsStruct(); inline void setTargetsStruct(bool value); inline bool getTargetsField(); inline void setTargetsField(bool value); inline bool getTargetsUnion(); inline void setTargetsUnion(bool value); inline bool getTargetsGroup(); inline void setTargetsGroup(bool value); inline bool getTargetsInterface(); inline void setTargetsInterface(bool value); inline bool getTargetsMethod(); inline void setTargetsMethod(bool value); inline bool getTargetsParam(); inline void setTargetsParam(bool value); inline bool getTargetsAnnotation(); inline void setTargetsAnnotation(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Declaration::Annotation::Pipeline { public: typedef Annotation Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Expression::Pipeline getType(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class ParsedFile::Reader { public: typedef ParsedFile Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasRoot() const; inline ::capnp::compiler::Declaration::Reader getRoot() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class ParsedFile::Builder { public: typedef ParsedFile Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasRoot(); inline ::capnp::compiler::Declaration::Builder getRoot(); inline void setRoot( ::capnp::compiler::Declaration::Reader value); inline ::capnp::compiler::Declaration::Builder initRoot(); inline void adoptRoot(::capnp::Orphan< ::capnp::compiler::Declaration>&& value); inline ::capnp::Orphan< ::capnp::compiler::Declaration> disownRoot(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class ParsedFile::Pipeline { public: typedef ParsedFile Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::compiler::Declaration::Pipeline getRoot(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= inline bool LocatedText::Reader::hasValue() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool LocatedText::Builder::hasValue() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader LocatedText::Reader::getValue() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder LocatedText::Builder::getValue() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void LocatedText::Builder::setValue( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder LocatedText::Builder::initValue(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void LocatedText::Builder::adoptValue( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> LocatedText::Builder::disownValue() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t LocatedText::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t LocatedText::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void LocatedText::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t LocatedText::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t LocatedText::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void LocatedText::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint64_t LocatedInteger::Reader::getValue() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t LocatedInteger::Builder::getValue() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void LocatedInteger::Builder::setValue( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t LocatedInteger::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t LocatedInteger::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void LocatedInteger::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::uint32_t LocatedInteger::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::uint32_t LocatedInteger::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline void LocatedInteger::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS, value); } inline double LocatedFloat::Reader::getValue() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline double LocatedFloat::Builder::getValue() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void LocatedFloat::Builder::setValue(double value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t LocatedFloat::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t LocatedFloat::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void LocatedFloat::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::uint32_t LocatedFloat::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::uint32_t LocatedFloat::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline void LocatedFloat::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS, value); } inline ::capnp::compiler::Expression::Which Expression::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Expression::Which Expression::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Expression::Reader::isUnknown() const { return which() == Expression::UNKNOWN; } inline bool Expression::Builder::isUnknown() { return which() == Expression::UNKNOWN; } inline ::capnp::Void Expression::Reader::getUnknown() const { KJ_IREQUIRE((which() == Expression::UNKNOWN), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Expression::Builder::getUnknown() { KJ_IREQUIRE((which() == Expression::UNKNOWN), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Expression::Builder::setUnknown( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::UNKNOWN); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Expression::Reader::isPositiveInt() const { return which() == Expression::POSITIVE_INT; } inline bool Expression::Builder::isPositiveInt() { return which() == Expression::POSITIVE_INT; } inline ::uint64_t Expression::Reader::getPositiveInt() const { KJ_IREQUIRE((which() == Expression::POSITIVE_INT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Expression::Builder::getPositiveInt() { KJ_IREQUIRE((which() == Expression::POSITIVE_INT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Expression::Builder::setPositiveInt( ::uint64_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::POSITIVE_INT); _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Expression::Reader::isNegativeInt() const { return which() == Expression::NEGATIVE_INT; } inline bool Expression::Builder::isNegativeInt() { return which() == Expression::NEGATIVE_INT; } inline ::uint64_t Expression::Reader::getNegativeInt() const { KJ_IREQUIRE((which() == Expression::NEGATIVE_INT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Expression::Builder::getNegativeInt() { KJ_IREQUIRE((which() == Expression::NEGATIVE_INT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Expression::Builder::setNegativeInt( ::uint64_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::NEGATIVE_INT); _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Expression::Reader::isFloat() const { return which() == Expression::FLOAT; } inline bool Expression::Builder::isFloat() { return which() == Expression::FLOAT; } inline double Expression::Reader::getFloat() const { KJ_IREQUIRE((which() == Expression::FLOAT), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline double Expression::Builder::getFloat() { KJ_IREQUIRE((which() == Expression::FLOAT), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Expression::Builder::setFloat(double value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::FLOAT); _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Expression::Reader::isString() const { return which() == Expression::STRING; } inline bool Expression::Builder::isString() { return which() == Expression::STRING; } inline bool Expression::Reader::hasString() const { if (which() != Expression::STRING) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasString() { if (which() != Expression::STRING) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Expression::Reader::getString() const { KJ_IREQUIRE((which() == Expression::STRING), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Expression::Builder::getString() { KJ_IREQUIRE((which() == Expression::STRING), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setString( ::capnp::Text::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::STRING); ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Expression::Builder::initString(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::STRING); return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Expression::Builder::adoptString( ::capnp::Orphan< ::capnp::Text>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::STRING); ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Expression::Builder::disownString() { KJ_IREQUIRE((which() == Expression::STRING), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Reader::isRelativeName() const { return which() == Expression::RELATIVE_NAME; } inline bool Expression::Builder::isRelativeName() { return which() == Expression::RELATIVE_NAME; } inline bool Expression::Reader::hasRelativeName() const { if (which() != Expression::RELATIVE_NAME) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasRelativeName() { if (which() != Expression::RELATIVE_NAME) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Expression::Reader::getRelativeName() const { KJ_IREQUIRE((which() == Expression::RELATIVE_NAME), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::getRelativeName() { KJ_IREQUIRE((which() == Expression::RELATIVE_NAME), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setRelativeName( ::capnp::compiler::LocatedText::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::RELATIVE_NAME); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::initRelativeName() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::RELATIVE_NAME); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::adoptRelativeName( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::RELATIVE_NAME); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Expression::Builder::disownRelativeName() { KJ_IREQUIRE((which() == Expression::RELATIVE_NAME), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Reader::isList() const { return which() == Expression::LIST; } inline bool Expression::Builder::isList() { return which() == Expression::LIST; } inline bool Expression::Reader::hasList() const { if (which() != Expression::LIST) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasList() { if (which() != Expression::LIST) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader Expression::Reader::getList() const { KJ_IREQUIRE((which() == Expression::LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder Expression::Builder::getList() { KJ_IREQUIRE((which() == Expression::LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setList( ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder Expression::Builder::initList(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::LIST); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Expression::Builder::adoptList( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>> Expression::Builder::disownList() { KJ_IREQUIRE((which() == Expression::LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Reader::isTuple() const { return which() == Expression::TUPLE; } inline bool Expression::Builder::isTuple() { return which() == Expression::TUPLE; } inline bool Expression::Reader::hasTuple() const { if (which() != Expression::TUPLE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasTuple() { if (which() != Expression::TUPLE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader Expression::Reader::getTuple() const { KJ_IREQUIRE((which() == Expression::TUPLE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder Expression::Builder::getTuple() { KJ_IREQUIRE((which() == Expression::TUPLE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setTuple( ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::TUPLE); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder Expression::Builder::initTuple(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::TUPLE); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Expression::Builder::adoptTuple( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::TUPLE); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>> Expression::Builder::disownTuple() { KJ_IREQUIRE((which() == Expression::TUPLE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Expression::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Expression::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Expression::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Expression::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline ::uint32_t Expression::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline void Expression::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<4>() * ::capnp::ELEMENTS, value); } inline bool Expression::Reader::isBinary() const { return which() == Expression::BINARY; } inline bool Expression::Builder::isBinary() { return which() == Expression::BINARY; } inline bool Expression::Reader::hasBinary() const { if (which() != Expression::BINARY) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasBinary() { if (which() != Expression::BINARY) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Data::Reader Expression::Reader::getBinary() const { KJ_IREQUIRE((which() == Expression::BINARY), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Data::Builder Expression::Builder::getBinary() { KJ_IREQUIRE((which() == Expression::BINARY), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setBinary( ::capnp::Data::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::BINARY); ::capnp::_::PointerHelpers< ::capnp::Data>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Data::Builder Expression::Builder::initBinary(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::BINARY); return ::capnp::_::PointerHelpers< ::capnp::Data>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Expression::Builder::adoptBinary( ::capnp::Orphan< ::capnp::Data>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::BINARY); ::capnp::_::PointerHelpers< ::capnp::Data>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Data> Expression::Builder::disownBinary() { KJ_IREQUIRE((which() == Expression::BINARY), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Reader::isApplication() const { return which() == Expression::APPLICATION; } inline bool Expression::Builder::isApplication() { return which() == Expression::APPLICATION; } inline typename Expression::Application::Reader Expression::Reader::getApplication() const { KJ_IREQUIRE((which() == Expression::APPLICATION), "Must check which() before get()ing a union member."); return typename Expression::Application::Reader(_reader); } inline typename Expression::Application::Builder Expression::Builder::getApplication() { KJ_IREQUIRE((which() == Expression::APPLICATION), "Must check which() before get()ing a union member."); return typename Expression::Application::Builder(_builder); } inline typename Expression::Application::Builder Expression::Builder::initApplication() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::APPLICATION); _builder.getPointerField(::capnp::bounded<0>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<1>() * ::capnp::POINTERS).clear(); return typename Expression::Application::Builder(_builder); } inline bool Expression::Reader::isMember() const { return which() == Expression::MEMBER; } inline bool Expression::Builder::isMember() { return which() == Expression::MEMBER; } inline typename Expression::Member::Reader Expression::Reader::getMember() const { KJ_IREQUIRE((which() == Expression::MEMBER), "Must check which() before get()ing a union member."); return typename Expression::Member::Reader(_reader); } inline typename Expression::Member::Builder Expression::Builder::getMember() { KJ_IREQUIRE((which() == Expression::MEMBER), "Must check which() before get()ing a union member."); return typename Expression::Member::Builder(_builder); } inline typename Expression::Member::Builder Expression::Builder::initMember() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::MEMBER); _builder.getPointerField(::capnp::bounded<0>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<1>() * ::capnp::POINTERS).clear(); return typename Expression::Member::Builder(_builder); } inline bool Expression::Reader::isAbsoluteName() const { return which() == Expression::ABSOLUTE_NAME; } inline bool Expression::Builder::isAbsoluteName() { return which() == Expression::ABSOLUTE_NAME; } inline bool Expression::Reader::hasAbsoluteName() const { if (which() != Expression::ABSOLUTE_NAME) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasAbsoluteName() { if (which() != Expression::ABSOLUTE_NAME) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Expression::Reader::getAbsoluteName() const { KJ_IREQUIRE((which() == Expression::ABSOLUTE_NAME), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::getAbsoluteName() { KJ_IREQUIRE((which() == Expression::ABSOLUTE_NAME), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setAbsoluteName( ::capnp::compiler::LocatedText::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::ABSOLUTE_NAME); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::initAbsoluteName() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::ABSOLUTE_NAME); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::adoptAbsoluteName( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::ABSOLUTE_NAME); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Expression::Builder::disownAbsoluteName() { KJ_IREQUIRE((which() == Expression::ABSOLUTE_NAME), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Reader::isImport() const { return which() == Expression::IMPORT; } inline bool Expression::Builder::isImport() { return which() == Expression::IMPORT; } inline bool Expression::Reader::hasImport() const { if (which() != Expression::IMPORT) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasImport() { if (which() != Expression::IMPORT) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Expression::Reader::getImport() const { KJ_IREQUIRE((which() == Expression::IMPORT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::getImport() { KJ_IREQUIRE((which() == Expression::IMPORT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setImport( ::capnp::compiler::LocatedText::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::IMPORT); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::initImport() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::IMPORT); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::adoptImport( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::IMPORT); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Expression::Builder::disownImport() { KJ_IREQUIRE((which() == Expression::IMPORT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Reader::isEmbed() const { return which() == Expression::EMBED; } inline bool Expression::Builder::isEmbed() { return which() == Expression::EMBED; } inline bool Expression::Reader::hasEmbed() const { if (which() != Expression::EMBED) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Builder::hasEmbed() { if (which() != Expression::EMBED) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Expression::Reader::getEmbed() const { KJ_IREQUIRE((which() == Expression::EMBED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::getEmbed() { KJ_IREQUIRE((which() == Expression::EMBED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::setEmbed( ::capnp::compiler::LocatedText::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::EMBED); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Expression::Builder::initEmbed() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::EMBED); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Builder::adoptEmbed( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::EMBED); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Expression::Builder::disownEmbed() { KJ_IREQUIRE((which() == Expression::EMBED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Param::Which Expression::Param::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Expression::Param::Which Expression::Param::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Expression::Param::Reader::isUnnamed() const { return which() == Expression::Param::UNNAMED; } inline bool Expression::Param::Builder::isUnnamed() { return which() == Expression::Param::UNNAMED; } inline ::capnp::Void Expression::Param::Reader::getUnnamed() const { KJ_IREQUIRE((which() == Expression::Param::UNNAMED), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Expression::Param::Builder::getUnnamed() { KJ_IREQUIRE((which() == Expression::Param::UNNAMED), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Expression::Param::Builder::setUnnamed( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::Param::UNNAMED); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Expression::Param::Reader::isNamed() const { return which() == Expression::Param::NAMED; } inline bool Expression::Param::Builder::isNamed() { return which() == Expression::Param::NAMED; } inline bool Expression::Param::Reader::hasNamed() const { if (which() != Expression::Param::NAMED) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Param::Builder::hasNamed() { if (which() != Expression::Param::NAMED) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Expression::Param::Reader::getNamed() const { KJ_IREQUIRE((which() == Expression::Param::NAMED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Expression::Param::Builder::getNamed() { KJ_IREQUIRE((which() == Expression::Param::NAMED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Param::Builder::setNamed( ::capnp::compiler::LocatedText::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::Param::NAMED); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Expression::Param::Builder::initNamed() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::Param::NAMED); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Param::Builder::adoptNamed( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Expression::Param::NAMED); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Expression::Param::Builder::disownNamed() { KJ_IREQUIRE((which() == Expression::Param::NAMED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Param::Reader::hasValue() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Param::Builder::hasValue() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Expression::Param::Reader::getValue() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Expression::Param::Builder::getValue() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Expression::Param::Pipeline::getValue() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE inline void Expression::Param::Builder::setValue( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Expression::Param::Builder::initValue() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Expression::Param::Builder::adoptValue( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Expression::Param::Builder::disownValue() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Expression::Application::Reader::hasFunction() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Application::Builder::hasFunction() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Expression::Application::Reader::getFunction() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Expression::Application::Builder::getFunction() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Expression::Application::Pipeline::getFunction() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Expression::Application::Builder::setFunction( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Expression::Application::Builder::initFunction() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Application::Builder::adoptFunction( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Expression::Application::Builder::disownFunction() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Application::Reader::hasParams() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Application::Builder::hasParams() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader Expression::Application::Reader::getParams() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder Expression::Application::Builder::getParams() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Expression::Application::Builder::setParams( ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>::Builder Expression::Application::Builder::initParams(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Expression::Application::Builder::adoptParams( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>> Expression::Application::Builder::disownParams() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression::Param, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Expression::Member::Reader::hasParent() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Member::Builder::hasParent() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Expression::Member::Reader::getParent() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Expression::Member::Builder::getParent() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Expression::Member::Pipeline::getParent() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Expression::Member::Builder::setParent( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Expression::Member::Builder::initParent() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Expression::Member::Builder::adoptParent( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Expression::Member::Builder::disownParent() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Expression::Member::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Expression::Member::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Expression::Member::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Expression::Member::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::LocatedText::Pipeline Expression::Member::Pipeline::getName() { return ::capnp::compiler::LocatedText::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE inline void Expression::Member::Builder::setName( ::capnp::compiler::LocatedText::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Expression::Member::Builder::initName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Expression::Member::Builder::adoptName( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Expression::Member::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::Which Declaration::Reader::which() const { return _reader.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::Which Declaration::Builder::which() { return _builder.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline bool Declaration::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Declaration::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Declaration::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::LocatedText::Pipeline Declaration::Pipeline::getName() { return ::capnp::compiler::LocatedText::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Declaration::Builder::setName( ::capnp::compiler::LocatedText::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Declaration::Builder::initName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::Builder::adoptName( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Declaration::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline typename Declaration::Id::Reader Declaration::Reader::getId() const { return typename Declaration::Id::Reader(_reader); } inline typename Declaration::Id::Builder Declaration::Builder::getId() { return typename Declaration::Id::Builder(_builder); } #if !CAPNP_LITE inline typename Declaration::Id::Pipeline Declaration::Pipeline::getId() { return typename Declaration::Id::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Declaration::Id::Builder Declaration::Builder::initId() { _builder.setDataField< ::uint16_t>(::capnp::bounded<0>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<1>() * ::capnp::POINTERS).clear(); return typename Declaration::Id::Builder(_builder); } inline bool Declaration::Reader::hasNestedDecls() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasNestedDecls() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Reader Declaration::Reader::getNestedDecls() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Builder Declaration::Builder::getNestedDecls() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void Declaration::Builder::setNestedDecls( ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>::Builder Declaration::Builder::initNestedDecls(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), size); } inline void Declaration::Builder::adoptNestedDecls( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>> Declaration::Builder::disownNestedDecls() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline bool Declaration::Reader::hasAnnotations() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasAnnotations() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader Declaration::Reader::getAnnotations() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder Declaration::Builder::getAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Declaration::Builder::setAnnotations( ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder Declaration::Builder::initAnnotations(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), size); } inline void Declaration::Builder::adoptAnnotations( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>> Declaration::Builder::disownAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::uint32_t Declaration::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Declaration::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::hasDocComment() const { return !_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasDocComment() { return !_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Declaration::Reader::getDocComment() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Declaration::Builder::getDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline void Declaration::Builder::setDocComment( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Declaration::Builder::initDocComment(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), size); } inline void Declaration::Builder::adoptDocComment( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Declaration::Builder::disownDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline bool Declaration::Reader::isFile() const { return which() == Declaration::FILE; } inline bool Declaration::Builder::isFile() { return which() == Declaration::FILE; } inline ::capnp::Void Declaration::Reader::getFile() const { KJ_IREQUIRE((which() == Declaration::FILE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getFile() { KJ_IREQUIRE((which() == Declaration::FILE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setFile( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::FILE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isUsing() const { return which() == Declaration::USING; } inline bool Declaration::Builder::isUsing() { return which() == Declaration::USING; } inline typename Declaration::Using::Reader Declaration::Reader::getUsing() const { KJ_IREQUIRE((which() == Declaration::USING), "Must check which() before get()ing a union member."); return typename Declaration::Using::Reader(_reader); } inline typename Declaration::Using::Builder Declaration::Builder::getUsing() { KJ_IREQUIRE((which() == Declaration::USING), "Must check which() before get()ing a union member."); return typename Declaration::Using::Builder(_builder); } inline typename Declaration::Using::Builder Declaration::Builder::initUsing() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::USING); _builder.getPointerField(::capnp::bounded<5>() * ::capnp::POINTERS).clear(); return typename Declaration::Using::Builder(_builder); } inline bool Declaration::Reader::isConst() const { return which() == Declaration::CONST; } inline bool Declaration::Builder::isConst() { return which() == Declaration::CONST; } inline typename Declaration::Const::Reader Declaration::Reader::getConst() const { KJ_IREQUIRE((which() == Declaration::CONST), "Must check which() before get()ing a union member."); return typename Declaration::Const::Reader(_reader); } inline typename Declaration::Const::Builder Declaration::Builder::getConst() { KJ_IREQUIRE((which() == Declaration::CONST), "Must check which() before get()ing a union member."); return typename Declaration::Const::Builder(_builder); } inline typename Declaration::Const::Builder Declaration::Builder::initConst() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::CONST); _builder.getPointerField(::capnp::bounded<5>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<6>() * ::capnp::POINTERS).clear(); return typename Declaration::Const::Builder(_builder); } inline bool Declaration::Reader::isEnum() const { return which() == Declaration::ENUM; } inline bool Declaration::Builder::isEnum() { return which() == Declaration::ENUM; } inline ::capnp::Void Declaration::Reader::getEnum() const { KJ_IREQUIRE((which() == Declaration::ENUM), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getEnum() { KJ_IREQUIRE((which() == Declaration::ENUM), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setEnum( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::ENUM); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isEnumerant() const { return which() == Declaration::ENUMERANT; } inline bool Declaration::Builder::isEnumerant() { return which() == Declaration::ENUMERANT; } inline ::capnp::Void Declaration::Reader::getEnumerant() const { KJ_IREQUIRE((which() == Declaration::ENUMERANT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getEnumerant() { KJ_IREQUIRE((which() == Declaration::ENUMERANT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setEnumerant( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::ENUMERANT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isStruct() const { return which() == Declaration::STRUCT; } inline bool Declaration::Builder::isStruct() { return which() == Declaration::STRUCT; } inline ::capnp::Void Declaration::Reader::getStruct() const { KJ_IREQUIRE((which() == Declaration::STRUCT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getStruct() { KJ_IREQUIRE((which() == Declaration::STRUCT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setStruct( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::STRUCT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isField() const { return which() == Declaration::FIELD; } inline bool Declaration::Builder::isField() { return which() == Declaration::FIELD; } inline typename Declaration::Field::Reader Declaration::Reader::getField() const { KJ_IREQUIRE((which() == Declaration::FIELD), "Must check which() before get()ing a union member."); return typename Declaration::Field::Reader(_reader); } inline typename Declaration::Field::Builder Declaration::Builder::getField() { KJ_IREQUIRE((which() == Declaration::FIELD), "Must check which() before get()ing a union member."); return typename Declaration::Field::Builder(_builder); } inline typename Declaration::Field::Builder Declaration::Builder::initField() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::FIELD); _builder.setDataField< ::uint16_t>(::capnp::bounded<6>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<5>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<6>() * ::capnp::POINTERS).clear(); return typename Declaration::Field::Builder(_builder); } inline bool Declaration::Reader::isUnion() const { return which() == Declaration::UNION; } inline bool Declaration::Builder::isUnion() { return which() == Declaration::UNION; } inline ::capnp::Void Declaration::Reader::getUnion() const { KJ_IREQUIRE((which() == Declaration::UNION), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getUnion() { KJ_IREQUIRE((which() == Declaration::UNION), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setUnion( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::UNION); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isGroup() const { return which() == Declaration::GROUP; } inline bool Declaration::Builder::isGroup() { return which() == Declaration::GROUP; } inline ::capnp::Void Declaration::Reader::getGroup() const { KJ_IREQUIRE((which() == Declaration::GROUP), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getGroup() { KJ_IREQUIRE((which() == Declaration::GROUP), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setGroup( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::GROUP); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isInterface() const { return which() == Declaration::INTERFACE; } inline bool Declaration::Builder::isInterface() { return which() == Declaration::INTERFACE; } inline typename Declaration::Interface::Reader Declaration::Reader::getInterface() const { KJ_IREQUIRE((which() == Declaration::INTERFACE), "Must check which() before get()ing a union member."); return typename Declaration::Interface::Reader(_reader); } inline typename Declaration::Interface::Builder Declaration::Builder::getInterface() { KJ_IREQUIRE((which() == Declaration::INTERFACE), "Must check which() before get()ing a union member."); return typename Declaration::Interface::Builder(_builder); } inline typename Declaration::Interface::Builder Declaration::Builder::initInterface() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::INTERFACE); _builder.getPointerField(::capnp::bounded<5>() * ::capnp::POINTERS).clear(); return typename Declaration::Interface::Builder(_builder); } inline bool Declaration::Reader::isMethod() const { return which() == Declaration::METHOD; } inline bool Declaration::Builder::isMethod() { return which() == Declaration::METHOD; } inline typename Declaration::Method::Reader Declaration::Reader::getMethod() const { KJ_IREQUIRE((which() == Declaration::METHOD), "Must check which() before get()ing a union member."); return typename Declaration::Method::Reader(_reader); } inline typename Declaration::Method::Builder Declaration::Builder::getMethod() { KJ_IREQUIRE((which() == Declaration::METHOD), "Must check which() before get()ing a union member."); return typename Declaration::Method::Builder(_builder); } inline typename Declaration::Method::Builder Declaration::Builder::initMethod() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::METHOD); _builder.setDataField< ::uint16_t>(::capnp::bounded<6>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<5>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<6>() * ::capnp::POINTERS).clear(); return typename Declaration::Method::Builder(_builder); } inline bool Declaration::Reader::isAnnotation() const { return which() == Declaration::ANNOTATION; } inline bool Declaration::Builder::isAnnotation() { return which() == Declaration::ANNOTATION; } inline typename Declaration::Annotation::Reader Declaration::Reader::getAnnotation() const { KJ_IREQUIRE((which() == Declaration::ANNOTATION), "Must check which() before get()ing a union member."); return typename Declaration::Annotation::Reader(_reader); } inline typename Declaration::Annotation::Builder Declaration::Builder::getAnnotation() { KJ_IREQUIRE((which() == Declaration::ANNOTATION), "Must check which() before get()ing a union member."); return typename Declaration::Annotation::Builder(_builder); } inline typename Declaration::Annotation::Builder Declaration::Builder::initAnnotation() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::ANNOTATION); _builder.setDataField(::capnp::bounded<96>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<97>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<98>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<99>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<100>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<101>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<102>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<103>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<104>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<105>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<106>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<107>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<5>() * ::capnp::POINTERS).clear(); return typename Declaration::Annotation::Builder(_builder); } inline bool Declaration::Reader::isNakedId() const { return which() == Declaration::NAKED_ID; } inline bool Declaration::Builder::isNakedId() { return which() == Declaration::NAKED_ID; } inline bool Declaration::Reader::hasNakedId() const { if (which() != Declaration::NAKED_ID) return false; return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasNakedId() { if (which() != Declaration::NAKED_ID) return false; return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedInteger::Reader Declaration::Reader::getNakedId() const { KJ_IREQUIRE((which() == Declaration::NAKED_ID), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedInteger::Builder Declaration::Builder::getNakedId() { KJ_IREQUIRE((which() == Declaration::NAKED_ID), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Builder::setNakedId( ::capnp::compiler::LocatedInteger::Reader value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::NAKED_ID); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedInteger::Builder Declaration::Builder::initNakedId() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::NAKED_ID); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Builder::adoptNakedId( ::capnp::Orphan< ::capnp::compiler::LocatedInteger>&& value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::NAKED_ID); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedInteger> Declaration::Builder::disownNakedId() { KJ_IREQUIRE((which() == Declaration::NAKED_ID), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Declaration::Reader::isNakedAnnotation() const { return which() == Declaration::NAKED_ANNOTATION; } inline bool Declaration::Builder::isNakedAnnotation() { return which() == Declaration::NAKED_ANNOTATION; } inline bool Declaration::Reader::hasNakedAnnotation() const { if (which() != Declaration::NAKED_ANNOTATION) return false; return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasNakedAnnotation() { if (which() != Declaration::NAKED_ANNOTATION) return false; return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Declaration::AnnotationApplication::Reader Declaration::Reader::getNakedAnnotation() const { KJ_IREQUIRE((which() == Declaration::NAKED_ANNOTATION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::AnnotationApplication>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::AnnotationApplication::Builder Declaration::Builder::getNakedAnnotation() { KJ_IREQUIRE((which() == Declaration::NAKED_ANNOTATION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::AnnotationApplication>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Builder::setNakedAnnotation( ::capnp::compiler::Declaration::AnnotationApplication::Reader value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::NAKED_ANNOTATION); ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::AnnotationApplication>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Declaration::AnnotationApplication::Builder Declaration::Builder::initNakedAnnotation() { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::NAKED_ANNOTATION); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::AnnotationApplication>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Builder::adoptNakedAnnotation( ::capnp::Orphan< ::capnp::compiler::Declaration::AnnotationApplication>&& value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::NAKED_ANNOTATION); ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::AnnotationApplication>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Declaration::AnnotationApplication> Declaration::Builder::disownNakedAnnotation() { KJ_IREQUIRE((which() == Declaration::NAKED_ANNOTATION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::AnnotationApplication>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Declaration::Reader::isBuiltinVoid() const { return which() == Declaration::BUILTIN_VOID; } inline bool Declaration::Builder::isBuiltinVoid() { return which() == Declaration::BUILTIN_VOID; } inline ::capnp::Void Declaration::Reader::getBuiltinVoid() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_VOID), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinVoid() { KJ_IREQUIRE((which() == Declaration::BUILTIN_VOID), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinVoid( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_VOID); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinBool() const { return which() == Declaration::BUILTIN_BOOL; } inline bool Declaration::Builder::isBuiltinBool() { return which() == Declaration::BUILTIN_BOOL; } inline ::capnp::Void Declaration::Reader::getBuiltinBool() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_BOOL), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinBool() { KJ_IREQUIRE((which() == Declaration::BUILTIN_BOOL), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinBool( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_BOOL); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinInt8() const { return which() == Declaration::BUILTIN_INT8; } inline bool Declaration::Builder::isBuiltinInt8() { return which() == Declaration::BUILTIN_INT8; } inline ::capnp::Void Declaration::Reader::getBuiltinInt8() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT8), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinInt8() { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT8), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinInt8( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_INT8); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinInt16() const { return which() == Declaration::BUILTIN_INT16; } inline bool Declaration::Builder::isBuiltinInt16() { return which() == Declaration::BUILTIN_INT16; } inline ::capnp::Void Declaration::Reader::getBuiltinInt16() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT16), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinInt16() { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT16), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinInt16( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_INT16); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinInt32() const { return which() == Declaration::BUILTIN_INT32; } inline bool Declaration::Builder::isBuiltinInt32() { return which() == Declaration::BUILTIN_INT32; } inline ::capnp::Void Declaration::Reader::getBuiltinInt32() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinInt32() { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinInt32( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_INT32); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinInt64() const { return which() == Declaration::BUILTIN_INT64; } inline bool Declaration::Builder::isBuiltinInt64() { return which() == Declaration::BUILTIN_INT64; } inline ::capnp::Void Declaration::Reader::getBuiltinInt64() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinInt64() { KJ_IREQUIRE((which() == Declaration::BUILTIN_INT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinInt64( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_INT64); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinUInt8() const { return which() == Declaration::BUILTIN_U_INT8; } inline bool Declaration::Builder::isBuiltinUInt8() { return which() == Declaration::BUILTIN_U_INT8; } inline ::capnp::Void Declaration::Reader::getBuiltinUInt8() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT8), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinUInt8() { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT8), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinUInt8( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_U_INT8); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinUInt16() const { return which() == Declaration::BUILTIN_U_INT16; } inline bool Declaration::Builder::isBuiltinUInt16() { return which() == Declaration::BUILTIN_U_INT16; } inline ::capnp::Void Declaration::Reader::getBuiltinUInt16() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT16), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinUInt16() { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT16), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinUInt16( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_U_INT16); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinUInt32() const { return which() == Declaration::BUILTIN_U_INT32; } inline bool Declaration::Builder::isBuiltinUInt32() { return which() == Declaration::BUILTIN_U_INT32; } inline ::capnp::Void Declaration::Reader::getBuiltinUInt32() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinUInt32() { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinUInt32( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_U_INT32); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinUInt64() const { return which() == Declaration::BUILTIN_U_INT64; } inline bool Declaration::Builder::isBuiltinUInt64() { return which() == Declaration::BUILTIN_U_INT64; } inline ::capnp::Void Declaration::Reader::getBuiltinUInt64() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinUInt64() { KJ_IREQUIRE((which() == Declaration::BUILTIN_U_INT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinUInt64( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_U_INT64); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinFloat32() const { return which() == Declaration::BUILTIN_FLOAT32; } inline bool Declaration::Builder::isBuiltinFloat32() { return which() == Declaration::BUILTIN_FLOAT32; } inline ::capnp::Void Declaration::Reader::getBuiltinFloat32() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_FLOAT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinFloat32() { KJ_IREQUIRE((which() == Declaration::BUILTIN_FLOAT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinFloat32( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_FLOAT32); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinFloat64() const { return which() == Declaration::BUILTIN_FLOAT64; } inline bool Declaration::Builder::isBuiltinFloat64() { return which() == Declaration::BUILTIN_FLOAT64; } inline ::capnp::Void Declaration::Reader::getBuiltinFloat64() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_FLOAT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinFloat64() { KJ_IREQUIRE((which() == Declaration::BUILTIN_FLOAT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinFloat64( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_FLOAT64); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinText() const { return which() == Declaration::BUILTIN_TEXT; } inline bool Declaration::Builder::isBuiltinText() { return which() == Declaration::BUILTIN_TEXT; } inline ::capnp::Void Declaration::Reader::getBuiltinText() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_TEXT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinText() { KJ_IREQUIRE((which() == Declaration::BUILTIN_TEXT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinText( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_TEXT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinData() const { return which() == Declaration::BUILTIN_DATA; } inline bool Declaration::Builder::isBuiltinData() { return which() == Declaration::BUILTIN_DATA; } inline ::capnp::Void Declaration::Reader::getBuiltinData() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_DATA), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinData() { KJ_IREQUIRE((which() == Declaration::BUILTIN_DATA), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinData( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_DATA); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinList() const { return which() == Declaration::BUILTIN_LIST; } inline bool Declaration::Builder::isBuiltinList() { return which() == Declaration::BUILTIN_LIST; } inline ::capnp::Void Declaration::Reader::getBuiltinList() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_LIST), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinList() { KJ_IREQUIRE((which() == Declaration::BUILTIN_LIST), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinList( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_LIST); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinObject() const { return which() == Declaration::BUILTIN_OBJECT; } inline bool Declaration::Builder::isBuiltinObject() { return which() == Declaration::BUILTIN_OBJECT; } inline ::capnp::Void Declaration::Reader::getBuiltinObject() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_OBJECT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinObject() { KJ_IREQUIRE((which() == Declaration::BUILTIN_OBJECT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinObject( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_OBJECT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinAnyPointer() const { return which() == Declaration::BUILTIN_ANY_POINTER; } inline bool Declaration::Builder::isBuiltinAnyPointer() { return which() == Declaration::BUILTIN_ANY_POINTER; } inline ::capnp::Void Declaration::Reader::getBuiltinAnyPointer() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_ANY_POINTER), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinAnyPointer() { KJ_IREQUIRE((which() == Declaration::BUILTIN_ANY_POINTER), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinAnyPointer( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_ANY_POINTER); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::hasParameters() const { return !_reader.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Builder::hasParameters() { return !_builder.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Reader Declaration::Reader::getParameters() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Builder Declaration::Builder::getParameters() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS)); } inline void Declaration::Builder::setParameters( ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>::Builder Declaration::Builder::initParameters(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS), size); } inline void Declaration::Builder::adoptParameters( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>> Declaration::Builder::disownParameters() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::BrandParameter, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<7>() * ::capnp::POINTERS)); } inline bool Declaration::Reader::isBuiltinAnyStruct() const { return which() == Declaration::BUILTIN_ANY_STRUCT; } inline bool Declaration::Builder::isBuiltinAnyStruct() { return which() == Declaration::BUILTIN_ANY_STRUCT; } inline ::capnp::Void Declaration::Reader::getBuiltinAnyStruct() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_ANY_STRUCT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinAnyStruct() { KJ_IREQUIRE((which() == Declaration::BUILTIN_ANY_STRUCT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinAnyStruct( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_ANY_STRUCT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinAnyList() const { return which() == Declaration::BUILTIN_ANY_LIST; } inline bool Declaration::Builder::isBuiltinAnyList() { return which() == Declaration::BUILTIN_ANY_LIST; } inline ::capnp::Void Declaration::Reader::getBuiltinAnyList() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_ANY_LIST), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinAnyList() { KJ_IREQUIRE((which() == Declaration::BUILTIN_ANY_LIST), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinAnyList( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_ANY_LIST); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Reader::isBuiltinCapability() const { return which() == Declaration::BUILTIN_CAPABILITY; } inline bool Declaration::Builder::isBuiltinCapability() { return which() == Declaration::BUILTIN_CAPABILITY; } inline ::capnp::Void Declaration::Reader::getBuiltinCapability() const { KJ_IREQUIRE((which() == Declaration::BUILTIN_CAPABILITY), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Builder::getBuiltinCapability() { KJ_IREQUIRE((which() == Declaration::BUILTIN_CAPABILITY), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Builder::setBuiltinCapability( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, Declaration::BUILTIN_CAPABILITY); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::BrandParameter::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::BrandParameter::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Declaration::BrandParameter::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Declaration::BrandParameter::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::BrandParameter::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Declaration::BrandParameter::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Declaration::BrandParameter::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Declaration::BrandParameter::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Declaration::BrandParameter::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::BrandParameter::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::BrandParameter::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Declaration::BrandParameter::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::BrandParameter::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Declaration::BrandParameter::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Declaration::AnnotationApplication::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::AnnotationApplication::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::AnnotationApplication::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::AnnotationApplication::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::AnnotationApplication::Pipeline::getName() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Declaration::AnnotationApplication::Builder::setName( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::AnnotationApplication::Builder::initName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::AnnotationApplication::Builder::adoptName( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::AnnotationApplication::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline typename Declaration::AnnotationApplication::Value::Reader Declaration::AnnotationApplication::Reader::getValue() const { return typename Declaration::AnnotationApplication::Value::Reader(_reader); } inline typename Declaration::AnnotationApplication::Value::Builder Declaration::AnnotationApplication::Builder::getValue() { return typename Declaration::AnnotationApplication::Value::Builder(_builder); } #if !CAPNP_LITE inline typename Declaration::AnnotationApplication::Value::Pipeline Declaration::AnnotationApplication::Pipeline::getValue() { return typename Declaration::AnnotationApplication::Value::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Declaration::AnnotationApplication::Value::Builder Declaration::AnnotationApplication::Builder::initValue() { _builder.setDataField< ::uint16_t>(::capnp::bounded<0>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<1>() * ::capnp::POINTERS).clear(); return typename Declaration::AnnotationApplication::Value::Builder(_builder); } inline ::capnp::compiler::Declaration::AnnotationApplication::Value::Which Declaration::AnnotationApplication::Value::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::AnnotationApplication::Value::Which Declaration::AnnotationApplication::Value::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Declaration::AnnotationApplication::Value::Reader::isNone() const { return which() == Declaration::AnnotationApplication::Value::NONE; } inline bool Declaration::AnnotationApplication::Value::Builder::isNone() { return which() == Declaration::AnnotationApplication::Value::NONE; } inline ::capnp::Void Declaration::AnnotationApplication::Value::Reader::getNone() const { KJ_IREQUIRE((which() == Declaration::AnnotationApplication::Value::NONE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::AnnotationApplication::Value::Builder::getNone() { KJ_IREQUIRE((which() == Declaration::AnnotationApplication::Value::NONE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::AnnotationApplication::Value::Builder::setNone( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::AnnotationApplication::Value::NONE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::AnnotationApplication::Value::Reader::isExpression() const { return which() == Declaration::AnnotationApplication::Value::EXPRESSION; } inline bool Declaration::AnnotationApplication::Value::Builder::isExpression() { return which() == Declaration::AnnotationApplication::Value::EXPRESSION; } inline bool Declaration::AnnotationApplication::Value::Reader::hasExpression() const { if (which() != Declaration::AnnotationApplication::Value::EXPRESSION) return false; return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::AnnotationApplication::Value::Builder::hasExpression() { if (which() != Declaration::AnnotationApplication::Value::EXPRESSION) return false; return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::AnnotationApplication::Value::Reader::getExpression() const { KJ_IREQUIRE((which() == Declaration::AnnotationApplication::Value::EXPRESSION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::AnnotationApplication::Value::Builder::getExpression() { KJ_IREQUIRE((which() == Declaration::AnnotationApplication::Value::EXPRESSION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::AnnotationApplication::Value::Builder::setExpression( ::capnp::compiler::Expression::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::AnnotationApplication::Value::EXPRESSION); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::AnnotationApplication::Value::Builder::initExpression() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::AnnotationApplication::Value::EXPRESSION); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::AnnotationApplication::Value::Builder::adoptExpression( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::AnnotationApplication::Value::EXPRESSION); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::AnnotationApplication::Value::Builder::disownExpression() { KJ_IREQUIRE((which() == Declaration::AnnotationApplication::Value::EXPRESSION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::ParamList::Which Declaration::ParamList::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::ParamList::Which Declaration::ParamList::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Declaration::ParamList::Reader::isNamedList() const { return which() == Declaration::ParamList::NAMED_LIST; } inline bool Declaration::ParamList::Builder::isNamedList() { return which() == Declaration::ParamList::NAMED_LIST; } inline bool Declaration::ParamList::Reader::hasNamedList() const { if (which() != Declaration::ParamList::NAMED_LIST) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::ParamList::Builder::hasNamedList() { if (which() != Declaration::ParamList::NAMED_LIST) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Reader Declaration::ParamList::Reader::getNamedList() const { KJ_IREQUIRE((which() == Declaration::ParamList::NAMED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Builder Declaration::ParamList::Builder::getNamedList() { KJ_IREQUIRE((which() == Declaration::ParamList::NAMED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::ParamList::Builder::setNamedList( ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::NAMED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>::Builder Declaration::ParamList::Builder::initNamedList(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::NAMED_LIST); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Declaration::ParamList::Builder::adoptNamedList( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::NAMED_LIST); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>> Declaration::ParamList::Builder::disownNamedList() { KJ_IREQUIRE((which() == Declaration::ParamList::NAMED_LIST), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::Param, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Declaration::ParamList::Reader::isType() const { return which() == Declaration::ParamList::TYPE; } inline bool Declaration::ParamList::Builder::isType() { return which() == Declaration::ParamList::TYPE; } inline bool Declaration::ParamList::Reader::hasType() const { if (which() != Declaration::ParamList::TYPE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::ParamList::Builder::hasType() { if (which() != Declaration::ParamList::TYPE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::ParamList::Reader::getType() const { KJ_IREQUIRE((which() == Declaration::ParamList::TYPE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::ParamList::Builder::getType() { KJ_IREQUIRE((which() == Declaration::ParamList::TYPE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::ParamList::Builder::setType( ::capnp::compiler::Expression::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::TYPE); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::ParamList::Builder::initType() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::TYPE); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::ParamList::Builder::adoptType( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::TYPE); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::ParamList::Builder::disownType() { KJ_IREQUIRE((which() == Declaration::ParamList::TYPE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Declaration::ParamList::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::ParamList::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Declaration::ParamList::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Declaration::ParamList::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::ParamList::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Declaration::ParamList::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Declaration::ParamList::Reader::isStream() const { return which() == Declaration::ParamList::STREAM; } inline bool Declaration::ParamList::Builder::isStream() { return which() == Declaration::ParamList::STREAM; } inline ::capnp::Void Declaration::ParamList::Reader::getStream() const { KJ_IREQUIRE((which() == Declaration::ParamList::STREAM), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::ParamList::Builder::getStream() { KJ_IREQUIRE((which() == Declaration::ParamList::STREAM), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::ParamList::Builder::setStream( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::ParamList::STREAM); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Param::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Param::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedText::Reader Declaration::Param::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedText::Builder Declaration::Param::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::LocatedText::Pipeline Declaration::Param::Pipeline::getName() { return ::capnp::compiler::LocatedText::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Declaration::Param::Builder::setName( ::capnp::compiler::LocatedText::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedText::Builder Declaration::Param::Builder::initName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Declaration::Param::Builder::adoptName( ::capnp::Orphan< ::capnp::compiler::LocatedText>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedText> Declaration::Param::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedText>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Declaration::Param::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Param::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Param::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Param::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::Param::Pipeline::getType() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE inline void Declaration::Param::Builder::setType( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Param::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::Param::Builder::adoptType( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Param::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Declaration::Param::Reader::hasAnnotations() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Param::Builder::hasAnnotations() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader Declaration::Param::Reader::getAnnotations() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder Declaration::Param::Builder::getAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void Declaration::Param::Builder::setAnnotations( ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>::Builder Declaration::Param::Builder::initAnnotations(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), size); } inline void Declaration::Param::Builder::adoptAnnotations( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>> Declaration::Param::Builder::disownAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Declaration::AnnotationApplication, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline typename Declaration::Param::DefaultValue::Reader Declaration::Param::Reader::getDefaultValue() const { return typename Declaration::Param::DefaultValue::Reader(_reader); } inline typename Declaration::Param::DefaultValue::Builder Declaration::Param::Builder::getDefaultValue() { return typename Declaration::Param::DefaultValue::Builder(_builder); } #if !CAPNP_LITE inline typename Declaration::Param::DefaultValue::Pipeline Declaration::Param::Pipeline::getDefaultValue() { return typename Declaration::Param::DefaultValue::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Declaration::Param::DefaultValue::Builder Declaration::Param::Builder::initDefaultValue() { _builder.setDataField< ::uint16_t>(::capnp::bounded<0>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); return typename Declaration::Param::DefaultValue::Builder(_builder); } inline ::uint32_t Declaration::Param::Reader::getStartByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::Param::Builder::getStartByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Declaration::Param::Builder::setStartByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Declaration::Param::Reader::getEndByte() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Declaration::Param::Builder::getEndByte() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Declaration::Param::Builder::setEndByte( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::capnp::compiler::Declaration::Param::DefaultValue::Which Declaration::Param::DefaultValue::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::Param::DefaultValue::Which Declaration::Param::DefaultValue::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Declaration::Param::DefaultValue::Reader::isNone() const { return which() == Declaration::Param::DefaultValue::NONE; } inline bool Declaration::Param::DefaultValue::Builder::isNone() { return which() == Declaration::Param::DefaultValue::NONE; } inline ::capnp::Void Declaration::Param::DefaultValue::Reader::getNone() const { KJ_IREQUIRE((which() == Declaration::Param::DefaultValue::NONE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Param::DefaultValue::Builder::getNone() { KJ_IREQUIRE((which() == Declaration::Param::DefaultValue::NONE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Param::DefaultValue::Builder::setNone( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Param::DefaultValue::NONE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Param::DefaultValue::Reader::isValue() const { return which() == Declaration::Param::DefaultValue::VALUE; } inline bool Declaration::Param::DefaultValue::Builder::isValue() { return which() == Declaration::Param::DefaultValue::VALUE; } inline bool Declaration::Param::DefaultValue::Reader::hasValue() const { if (which() != Declaration::Param::DefaultValue::VALUE) return false; return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Param::DefaultValue::Builder::hasValue() { if (which() != Declaration::Param::DefaultValue::VALUE) return false; return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Param::DefaultValue::Reader::getValue() const { KJ_IREQUIRE((which() == Declaration::Param::DefaultValue::VALUE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Param::DefaultValue::Builder::getValue() { KJ_IREQUIRE((which() == Declaration::Param::DefaultValue::VALUE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Declaration::Param::DefaultValue::Builder::setValue( ::capnp::compiler::Expression::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Param::DefaultValue::VALUE); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Param::DefaultValue::Builder::initValue() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Param::DefaultValue::VALUE); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Declaration::Param::DefaultValue::Builder::adoptValue( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Param::DefaultValue::VALUE); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Param::DefaultValue::Builder::disownValue() { KJ_IREQUIRE((which() == Declaration::Param::DefaultValue::VALUE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::Id::Which Declaration::Id::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::Id::Which Declaration::Id::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Declaration::Id::Reader::isUnspecified() const { return which() == Declaration::Id::UNSPECIFIED; } inline bool Declaration::Id::Builder::isUnspecified() { return which() == Declaration::Id::UNSPECIFIED; } inline ::capnp::Void Declaration::Id::Reader::getUnspecified() const { KJ_IREQUIRE((which() == Declaration::Id::UNSPECIFIED), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Id::Builder::getUnspecified() { KJ_IREQUIRE((which() == Declaration::Id::UNSPECIFIED), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Id::Builder::setUnspecified( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::UNSPECIFIED); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Id::Reader::isUid() const { return which() == Declaration::Id::UID; } inline bool Declaration::Id::Builder::isUid() { return which() == Declaration::Id::UID; } inline bool Declaration::Id::Reader::hasUid() const { if (which() != Declaration::Id::UID) return false; return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Id::Builder::hasUid() { if (which() != Declaration::Id::UID) return false; return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedInteger::Reader Declaration::Id::Reader::getUid() const { KJ_IREQUIRE((which() == Declaration::Id::UID), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedInteger::Builder Declaration::Id::Builder::getUid() { KJ_IREQUIRE((which() == Declaration::Id::UID), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::Id::Builder::setUid( ::capnp::compiler::LocatedInteger::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::UID); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedInteger::Builder Declaration::Id::Builder::initUid() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::UID); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::Id::Builder::adoptUid( ::capnp::Orphan< ::capnp::compiler::LocatedInteger>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::UID); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedInteger> Declaration::Id::Builder::disownUid() { KJ_IREQUIRE((which() == Declaration::Id::UID), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Declaration::Id::Reader::isOrdinal() const { return which() == Declaration::Id::ORDINAL; } inline bool Declaration::Id::Builder::isOrdinal() { return which() == Declaration::Id::ORDINAL; } inline bool Declaration::Id::Reader::hasOrdinal() const { if (which() != Declaration::Id::ORDINAL) return false; return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Id::Builder::hasOrdinal() { if (which() != Declaration::Id::ORDINAL) return false; return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::LocatedInteger::Reader Declaration::Id::Reader::getOrdinal() const { KJ_IREQUIRE((which() == Declaration::Id::ORDINAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::compiler::LocatedInteger::Builder Declaration::Id::Builder::getOrdinal() { KJ_IREQUIRE((which() == Declaration::Id::ORDINAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::Id::Builder::setOrdinal( ::capnp::compiler::LocatedInteger::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::ORDINAL); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::LocatedInteger::Builder Declaration::Id::Builder::initOrdinal() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::ORDINAL); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Declaration::Id::Builder::adoptOrdinal( ::capnp::Orphan< ::capnp::compiler::LocatedInteger>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Declaration::Id::ORDINAL); ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::LocatedInteger> Declaration::Id::Builder::disownOrdinal() { KJ_IREQUIRE((which() == Declaration::Id::ORDINAL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::LocatedInteger>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Declaration::Using::Reader::hasTarget() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Using::Builder::hasTarget() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Using::Reader::getTarget() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Using::Builder::getTarget() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::Using::Pipeline::getTarget() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(5)); } #endif // !CAPNP_LITE inline void Declaration::Using::Builder::setTarget( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Using::Builder::initTarget() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Using::Builder::adoptTarget( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Using::Builder::disownTarget() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Declaration::Const::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Const::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Const::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Const::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::Const::Pipeline::getType() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(5)); } #endif // !CAPNP_LITE inline void Declaration::Const::Builder::setType( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Const::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Const::Builder::adoptType( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Const::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Declaration::Const::Reader::hasValue() const { return !_reader.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Const::Builder::hasValue() { return !_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Const::Reader::getValue() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Const::Builder::getValue() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::Const::Pipeline::getValue() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(6)); } #endif // !CAPNP_LITE inline void Declaration::Const::Builder::setValue( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Const::Builder::initValue() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline void Declaration::Const::Builder::adoptValue( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Const::Builder::disownValue() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline bool Declaration::Field::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Field::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Field::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Field::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::Field::Pipeline::getType() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(5)); } #endif // !CAPNP_LITE inline void Declaration::Field::Builder::setType( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Field::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Field::Builder::adoptType( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Field::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline typename Declaration::Field::DefaultValue::Reader Declaration::Field::Reader::getDefaultValue() const { return typename Declaration::Field::DefaultValue::Reader(_reader); } inline typename Declaration::Field::DefaultValue::Builder Declaration::Field::Builder::getDefaultValue() { return typename Declaration::Field::DefaultValue::Builder(_builder); } #if !CAPNP_LITE inline typename Declaration::Field::DefaultValue::Pipeline Declaration::Field::Pipeline::getDefaultValue() { return typename Declaration::Field::DefaultValue::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Declaration::Field::DefaultValue::Builder Declaration::Field::Builder::initDefaultValue() { _builder.setDataField< ::uint16_t>(::capnp::bounded<6>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<6>() * ::capnp::POINTERS).clear(); return typename Declaration::Field::DefaultValue::Builder(_builder); } inline ::capnp::compiler::Declaration::Field::DefaultValue::Which Declaration::Field::DefaultValue::Reader::which() const { return _reader.getDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::Field::DefaultValue::Which Declaration::Field::DefaultValue::Builder::which() { return _builder.getDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline bool Declaration::Field::DefaultValue::Reader::isNone() const { return which() == Declaration::Field::DefaultValue::NONE; } inline bool Declaration::Field::DefaultValue::Builder::isNone() { return which() == Declaration::Field::DefaultValue::NONE; } inline ::capnp::Void Declaration::Field::DefaultValue::Reader::getNone() const { KJ_IREQUIRE((which() == Declaration::Field::DefaultValue::NONE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Field::DefaultValue::Builder::getNone() { KJ_IREQUIRE((which() == Declaration::Field::DefaultValue::NONE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Field::DefaultValue::Builder::setNone( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Field::DefaultValue::NONE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Field::DefaultValue::Reader::isValue() const { return which() == Declaration::Field::DefaultValue::VALUE; } inline bool Declaration::Field::DefaultValue::Builder::isValue() { return which() == Declaration::Field::DefaultValue::VALUE; } inline bool Declaration::Field::DefaultValue::Reader::hasValue() const { if (which() != Declaration::Field::DefaultValue::VALUE) return false; return !_reader.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Field::DefaultValue::Builder::hasValue() { if (which() != Declaration::Field::DefaultValue::VALUE) return false; return !_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Field::DefaultValue::Reader::getValue() const { KJ_IREQUIRE((which() == Declaration::Field::DefaultValue::VALUE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Field::DefaultValue::Builder::getValue() { KJ_IREQUIRE((which() == Declaration::Field::DefaultValue::VALUE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline void Declaration::Field::DefaultValue::Builder::setValue( ::capnp::compiler::Expression::Reader value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Field::DefaultValue::VALUE); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Field::DefaultValue::Builder::initValue() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Field::DefaultValue::VALUE); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline void Declaration::Field::DefaultValue::Builder::adoptValue( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Field::DefaultValue::VALUE); ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Field::DefaultValue::Builder::disownValue() { KJ_IREQUIRE((which() == Declaration::Field::DefaultValue::VALUE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline bool Declaration::Interface::Reader::hasSuperclasses() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Interface::Builder::hasSuperclasses() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader Declaration::Interface::Reader::getSuperclasses() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder Declaration::Interface::Builder::getSuperclasses() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Interface::Builder::setSuperclasses( ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>::Builder Declaration::Interface::Builder::initSuperclasses(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), size); } inline void Declaration::Interface::Builder::adoptSuperclasses( ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>> Declaration::Interface::Builder::disownSuperclasses() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::compiler::Expression, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Declaration::Method::Reader::hasParams() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Method::Builder::hasParams() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Declaration::ParamList::Reader Declaration::Method::Reader::getParams() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::ParamList::Builder Declaration::Method::Builder::getParams() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Declaration::ParamList::Pipeline Declaration::Method::Pipeline::getParams() { return ::capnp::compiler::Declaration::ParamList::Pipeline(_typeless.getPointerField(5)); } #endif // !CAPNP_LITE inline void Declaration::Method::Builder::setParams( ::capnp::compiler::Declaration::ParamList::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Declaration::ParamList::Builder Declaration::Method::Builder::initParams() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Method::Builder::adoptParams( ::capnp::Orphan< ::capnp::compiler::Declaration::ParamList>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Declaration::ParamList> Declaration::Method::Builder::disownParams() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline typename Declaration::Method::Results::Reader Declaration::Method::Reader::getResults() const { return typename Declaration::Method::Results::Reader(_reader); } inline typename Declaration::Method::Results::Builder Declaration::Method::Builder::getResults() { return typename Declaration::Method::Results::Builder(_builder); } #if !CAPNP_LITE inline typename Declaration::Method::Results::Pipeline Declaration::Method::Pipeline::getResults() { return typename Declaration::Method::Results::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Declaration::Method::Results::Builder Declaration::Method::Builder::initResults() { _builder.setDataField< ::uint16_t>(::capnp::bounded<6>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<6>() * ::capnp::POINTERS).clear(); return typename Declaration::Method::Results::Builder(_builder); } inline ::capnp::compiler::Declaration::Method::Results::Which Declaration::Method::Results::Reader::which() const { return _reader.getDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline ::capnp::compiler::Declaration::Method::Results::Which Declaration::Method::Results::Builder::which() { return _builder.getDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline bool Declaration::Method::Results::Reader::isNone() const { return which() == Declaration::Method::Results::NONE; } inline bool Declaration::Method::Results::Builder::isNone() { return which() == Declaration::Method::Results::NONE; } inline ::capnp::Void Declaration::Method::Results::Reader::getNone() const { KJ_IREQUIRE((which() == Declaration::Method::Results::NONE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Declaration::Method::Results::Builder::getNone() { KJ_IREQUIRE((which() == Declaration::Method::Results::NONE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Declaration::Method::Results::Builder::setNone( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Method::Results::NONE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Method::Results::Reader::isExplicit() const { return which() == Declaration::Method::Results::EXPLICIT; } inline bool Declaration::Method::Results::Builder::isExplicit() { return which() == Declaration::Method::Results::EXPLICIT; } inline bool Declaration::Method::Results::Reader::hasExplicit() const { if (which() != Declaration::Method::Results::EXPLICIT) return false; return !_reader.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Method::Results::Builder::hasExplicit() { if (which() != Declaration::Method::Results::EXPLICIT) return false; return !_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Declaration::ParamList::Reader Declaration::Method::Results::Reader::getExplicit() const { KJ_IREQUIRE((which() == Declaration::Method::Results::EXPLICIT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::get(_reader.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::ParamList::Builder Declaration::Method::Results::Builder::getExplicit() { KJ_IREQUIRE((which() == Declaration::Method::Results::EXPLICIT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::get(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline void Declaration::Method::Results::Builder::setExplicit( ::capnp::compiler::Declaration::ParamList::Reader value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Method::Results::EXPLICIT); ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::set(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Declaration::ParamList::Builder Declaration::Method::Results::Builder::initExplicit() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Method::Results::EXPLICIT); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::init(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline void Declaration::Method::Results::Builder::adoptExplicit( ::capnp::Orphan< ::capnp::compiler::Declaration::ParamList>&& value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Declaration::Method::Results::EXPLICIT); ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::adopt(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Declaration::ParamList> Declaration::Method::Results::Builder::disownExplicit() { KJ_IREQUIRE((which() == Declaration::Method::Results::EXPLICIT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration::ParamList>::disown(_builder.getPointerField( ::capnp::bounded<6>() * ::capnp::POINTERS)); } inline bool Declaration::Annotation::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Declaration::Annotation::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Expression::Reader Declaration::Annotation::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Expression::Builder Declaration::Annotation::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Expression::Pipeline Declaration::Annotation::Pipeline::getType() { return ::capnp::compiler::Expression::Pipeline(_typeless.getPointerField(5)); } #endif // !CAPNP_LITE inline void Declaration::Annotation::Builder::setType( ::capnp::compiler::Expression::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Expression::Builder Declaration::Annotation::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Declaration::Annotation::Builder::adoptType( ::capnp::Orphan< ::capnp::compiler::Expression>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Expression> Declaration::Annotation::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Expression>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Declaration::Annotation::Reader::getTargetsFile() const { return _reader.getDataField( ::capnp::bounded<96>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsFile() { return _builder.getDataField( ::capnp::bounded<96>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsFile(bool value) { _builder.setDataField( ::capnp::bounded<96>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsConst() const { return _reader.getDataField( ::capnp::bounded<97>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsConst() { return _builder.getDataField( ::capnp::bounded<97>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsConst(bool value) { _builder.setDataField( ::capnp::bounded<97>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsEnum() const { return _reader.getDataField( ::capnp::bounded<98>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsEnum() { return _builder.getDataField( ::capnp::bounded<98>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsEnum(bool value) { _builder.setDataField( ::capnp::bounded<98>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsEnumerant() const { return _reader.getDataField( ::capnp::bounded<99>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsEnumerant() { return _builder.getDataField( ::capnp::bounded<99>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsEnumerant(bool value) { _builder.setDataField( ::capnp::bounded<99>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsStruct() const { return _reader.getDataField( ::capnp::bounded<100>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsStruct() { return _builder.getDataField( ::capnp::bounded<100>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsStruct(bool value) { _builder.setDataField( ::capnp::bounded<100>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsField() const { return _reader.getDataField( ::capnp::bounded<101>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsField() { return _builder.getDataField( ::capnp::bounded<101>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsField(bool value) { _builder.setDataField( ::capnp::bounded<101>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsUnion() const { return _reader.getDataField( ::capnp::bounded<102>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsUnion() { return _builder.getDataField( ::capnp::bounded<102>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsUnion(bool value) { _builder.setDataField( ::capnp::bounded<102>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsGroup() const { return _reader.getDataField( ::capnp::bounded<103>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsGroup() { return _builder.getDataField( ::capnp::bounded<103>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsGroup(bool value) { _builder.setDataField( ::capnp::bounded<103>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsInterface() const { return _reader.getDataField( ::capnp::bounded<104>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsInterface() { return _builder.getDataField( ::capnp::bounded<104>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsInterface(bool value) { _builder.setDataField( ::capnp::bounded<104>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsMethod() const { return _reader.getDataField( ::capnp::bounded<105>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsMethod() { return _builder.getDataField( ::capnp::bounded<105>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsMethod(bool value) { _builder.setDataField( ::capnp::bounded<105>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsParam() const { return _reader.getDataField( ::capnp::bounded<106>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsParam() { return _builder.getDataField( ::capnp::bounded<106>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsParam(bool value) { _builder.setDataField( ::capnp::bounded<106>() * ::capnp::ELEMENTS, value); } inline bool Declaration::Annotation::Reader::getTargetsAnnotation() const { return _reader.getDataField( ::capnp::bounded<107>() * ::capnp::ELEMENTS); } inline bool Declaration::Annotation::Builder::getTargetsAnnotation() { return _builder.getDataField( ::capnp::bounded<107>() * ::capnp::ELEMENTS); } inline void Declaration::Annotation::Builder::setTargetsAnnotation(bool value) { _builder.setDataField( ::capnp::bounded<107>() * ::capnp::ELEMENTS, value); } inline bool ParsedFile::Reader::hasRoot() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool ParsedFile::Builder::hasRoot() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::compiler::Declaration::Reader ParsedFile::Reader::getRoot() const { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::compiler::Declaration::Builder ParsedFile::Builder::getRoot() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::compiler::Declaration::Pipeline ParsedFile::Pipeline::getRoot() { return ::capnp::compiler::Declaration::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void ParsedFile::Builder::setRoot( ::capnp::compiler::Declaration::Reader value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::compiler::Declaration::Builder ParsedFile::Builder::initRoot() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void ParsedFile::Builder::adoptRoot( ::capnp::Orphan< ::capnp::compiler::Declaration>&& value) { ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::compiler::Declaration> ParsedFile::Builder::disownRoot() { return ::capnp::_::PointerHelpers< ::capnp::compiler::Declaration>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/compiler/grammar.capnp.c++0000644000175000017500000040001513650101756023476 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: grammar.capnp #include "grammar.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<66> b_e75816b56529d464 = { { 0, 0, 0, 0, 5, 0, 6, 0, 100, 212, 41, 101, 181, 22, 88, 231, 29, 0, 0, 0, 1, 0, 1, 0, 198, 195, 187, 220, 104, 225, 107, 197, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 74, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 76, 111, 99, 97, 116, 101, 100, 84, 101, 120, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 118, 97, 108, 117, 101, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e75816b56529d464 = b_e75816b56529d464.words; #if !CAPNP_LITE static const uint16_t m_e75816b56529d464[] = {2, 1, 0}; static const uint16_t i_e75816b56529d464[] = {0, 1, 2}; const ::capnp::_::RawSchema s_e75816b56529d464 = { 0xe75816b56529d464, b_e75816b56529d464.words, 66, nullptr, m_e75816b56529d464, 0, 3, i_e75816b56529d464, nullptr, nullptr, { &s_e75816b56529d464, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<66> b_991c7a3693d62cf2 = { { 0, 0, 0, 0, 5, 0, 6, 0, 242, 44, 214, 147, 54, 122, 28, 153, 29, 0, 0, 0, 1, 0, 2, 0, 198, 195, 187, 220, 104, 225, 107, 197, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 98, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 76, 111, 99, 97, 116, 101, 100, 73, 110, 116, 101, 103, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 118, 97, 108, 117, 101, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_991c7a3693d62cf2 = b_991c7a3693d62cf2.words; #if !CAPNP_LITE static const uint16_t m_991c7a3693d62cf2[] = {2, 1, 0}; static const uint16_t i_991c7a3693d62cf2[] = {0, 1, 2}; const ::capnp::_::RawSchema s_991c7a3693d62cf2 = { 0x991c7a3693d62cf2, b_991c7a3693d62cf2.words, 66, nullptr, m_991c7a3693d62cf2, 0, 3, i_991c7a3693d62cf2, nullptr, nullptr, { &s_991c7a3693d62cf2, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<66> b_90f2a60678fd2367 = { { 0, 0, 0, 0, 5, 0, 6, 0, 103, 35, 253, 120, 6, 166, 242, 144, 29, 0, 0, 0, 1, 0, 2, 0, 198, 195, 187, 220, 104, 225, 107, 197, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 82, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 76, 111, 99, 97, 116, 101, 100, 70, 108, 111, 97, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 118, 97, 108, 117, 101, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_90f2a60678fd2367 = b_90f2a60678fd2367.words; #if !CAPNP_LITE static const uint16_t m_90f2a60678fd2367[] = {2, 1, 0}; static const uint16_t i_90f2a60678fd2367[] = {0, 1, 2}; const ::capnp::_::RawSchema s_90f2a60678fd2367 = { 0x90f2a60678fd2367, b_90f2a60678fd2367.words, 66, nullptr, m_90f2a60678fd2367, 0, 3, i_90f2a60678fd2367, nullptr, nullptr, { &s_90f2a60678fd2367, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<262> b_8e207d4dfe54d0de = { { 0, 0, 0, 0, 5, 0, 6, 0, 222, 208, 84, 254, 77, 125, 32, 142, 29, 0, 0, 0, 1, 0, 3, 0, 198, 195, 187, 220, 104, 225, 107, 197, 2, 0, 7, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 66, 1, 0, 0, 37, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 135, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 69, 120, 112, 114, 101, 115, 115, 105, 111, 110, 0, 4, 0, 0, 0, 1, 0, 1, 0, 170, 219, 222, 26, 183, 70, 2, 201, 1, 0, 0, 0, 50, 0, 0, 0, 80, 97, 114, 97, 109, 0, 0, 0, 64, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 1, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 1, 0, 0, 3, 0, 1, 0, 184, 1, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 1, 0, 0, 3, 0, 1, 0, 192, 1, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 188, 1, 0, 0, 3, 0, 1, 0, 200, 1, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 1, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 1, 0, 0, 3, 0, 1, 0, 204, 1, 0, 0, 2, 0, 1, 0, 4, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 201, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1, 0, 0, 3, 0, 1, 0, 208, 1, 0, 0, 2, 0, 1, 0, 6, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 205, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 1, 0, 0, 3, 0, 1, 0, 216, 1, 0, 0, 2, 0, 1, 0, 10, 0, 249, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 1, 0, 0, 3, 0, 1, 0, 236, 1, 0, 0, 2, 0, 1, 0, 11, 0, 248, 255, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 233, 1, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 228, 1, 0, 0, 3, 0, 1, 0, 0, 2, 0, 0, 2, 0, 1, 0, 14, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 253, 1, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252, 1, 0, 0, 3, 0, 1, 0, 8, 2, 0, 0, 2, 0, 1, 0, 15, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 1, 0, 12, 2, 0, 0, 2, 0, 1, 0, 5, 0, 247, 255, 0, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 0, 0, 3, 0, 1, 0, 16, 2, 0, 0, 2, 0, 1, 0, 12, 0, 246, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 122, 223, 176, 64, 112, 57, 232, 174, 13, 2, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 245, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 89, 51, 121, 13, 64, 225, 40, 170, 249, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 244, 255, 0, 0, 0, 0, 0, 0, 1, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 225, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 224, 1, 0, 0, 3, 0, 1, 0, 236, 1, 0, 0, 2, 0, 1, 0, 8, 0, 243, 255, 0, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 233, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 228, 1, 0, 0, 3, 0, 1, 0, 240, 1, 0, 0, 2, 0, 1, 0, 9, 0, 242, 255, 0, 0, 0, 0, 0, 0, 1, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 237, 1, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 1, 0, 0, 3, 0, 1, 0, 244, 1, 0, 0, 2, 0, 1, 0, 117, 110, 107, 110, 111, 119, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 111, 115, 105, 116, 105, 118, 101, 73, 110, 116, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 101, 103, 97, 116, 105, 118, 101, 73, 110, 116, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 108, 111, 97, 116, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 105, 110, 103, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 108, 97, 116, 105, 118, 101, 78, 97, 109, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 105, 115, 116, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 117, 112, 108, 101, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 170, 219, 222, 26, 183, 70, 2, 201, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 105, 110, 97, 114, 121, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 109, 101, 109, 98, 101, 114, 0, 0, 97, 98, 115, 111, 108, 117, 116, 101, 78, 97, 109, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 109, 112, 111, 114, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 109, 98, 101, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_8e207d4dfe54d0de = b_8e207d4dfe54d0de.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_8e207d4dfe54d0de[] = { &s_8e207d4dfe54d0de, &s_aa28e1400d793359, &s_aee8397040b0df7a, &s_c90246b71adedbaa, &s_e75816b56529d464, }; static const uint16_t m_8e207d4dfe54d0de[] = {13, 11, 10, 15, 9, 3, 14, 6, 12, 2, 1, 5, 8, 4, 7, 0}; static const uint16_t i_8e207d4dfe54d0de[] = {0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 8, 9}; const ::capnp::_::RawSchema s_8e207d4dfe54d0de = { 0x8e207d4dfe54d0de, b_8e207d4dfe54d0de.words, 262, d_8e207d4dfe54d0de, m_8e207d4dfe54d0de, 5, 16, i_8e207d4dfe54d0de, nullptr, nullptr, { &s_8e207d4dfe54d0de, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<65> b_c90246b71adedbaa = { { 0, 0, 0, 0, 5, 0, 6, 0, 170, 219, 222, 26, 183, 70, 2, 201, 40, 0, 0, 0, 1, 0, 1, 0, 222, 208, 84, 254, 77, 125, 32, 142, 2, 0, 7, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 114, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 69, 120, 112, 114, 101, 115, 115, 105, 111, 110, 46, 80, 97, 114, 97, 109, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 117, 110, 110, 97, 109, 101, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 97, 109, 101, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c90246b71adedbaa = b_c90246b71adedbaa.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c90246b71adedbaa[] = { &s_8e207d4dfe54d0de, &s_e75816b56529d464, }; static const uint16_t m_c90246b71adedbaa[] = {1, 0, 2}; static const uint16_t i_c90246b71adedbaa[] = {0, 1, 2}; const ::capnp::_::RawSchema s_c90246b71adedbaa = { 0xc90246b71adedbaa, b_c90246b71adedbaa.words, 65, d_c90246b71adedbaa, m_c90246b71adedbaa, 2, 3, i_c90246b71adedbaa, nullptr, nullptr, { &s_c90246b71adedbaa, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<55> b_aee8397040b0df7a = { { 0, 0, 0, 0, 5, 0, 6, 0, 122, 223, 176, 64, 112, 57, 232, 174, 40, 0, 0, 0, 1, 0, 3, 0, 222, 208, 84, 254, 77, 125, 32, 142, 2, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 162, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 69, 120, 112, 114, 101, 115, 115, 105, 111, 110, 46, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 72, 0, 0, 0, 2, 0, 1, 0, 102, 117, 110, 99, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 170, 219, 222, 26, 183, 70, 2, 201, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_aee8397040b0df7a = b_aee8397040b0df7a.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_aee8397040b0df7a[] = { &s_8e207d4dfe54d0de, &s_c90246b71adedbaa, }; static const uint16_t m_aee8397040b0df7a[] = {0, 1}; static const uint16_t i_aee8397040b0df7a[] = {0, 1}; const ::capnp::_::RawSchema s_aee8397040b0df7a = { 0xaee8397040b0df7a, b_aee8397040b0df7a.words, 55, d_aee8397040b0df7a, m_aee8397040b0df7a, 2, 2, i_aee8397040b0df7a, nullptr, nullptr, { &s_aee8397040b0df7a, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<49> b_aa28e1400d793359 = { { 0, 0, 0, 0, 5, 0, 6, 0, 89, 51, 121, 13, 64, 225, 40, 170, 40, 0, 0, 0, 1, 0, 3, 0, 222, 208, 84, 254, 77, 125, 32, 142, 2, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 122, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 69, 120, 112, 114, 101, 115, 115, 105, 111, 110, 46, 109, 101, 109, 98, 101, 114, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 112, 97, 114, 101, 110, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 97, 109, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_aa28e1400d793359 = b_aa28e1400d793359.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_aa28e1400d793359[] = { &s_8e207d4dfe54d0de, &s_e75816b56529d464, }; static const uint16_t m_aa28e1400d793359[] = {1, 0}; static const uint16_t i_aa28e1400d793359[] = {0, 1}; const ::capnp::_::RawSchema s_aa28e1400d793359 = { 0xaa28e1400d793359, b_aa28e1400d793359.words, 49, d_aa28e1400d793359, m_aa28e1400d793359, 2, 2, i_aa28e1400d793359, nullptr, nullptr, { &s_aa28e1400d793359, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<677> b_96efe787c17e83bb = { { 0, 0, 0, 0, 5, 0, 6, 0, 187, 131, 126, 193, 135, 231, 239, 150, 29, 0, 0, 0, 1, 0, 2, 0, 198, 195, 187, 220, 104, 225, 107, 197, 8, 0, 7, 0, 0, 0, 34, 0, 1, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 74, 1, 0, 0, 41, 0, 0, 0, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 0, 0, 0, 55, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 1, 0, 1, 0, 117, 225, 28, 175, 68, 17, 231, 213, 33, 0, 0, 0, 122, 0, 0, 0, 144, 98, 130, 115, 212, 137, 4, 208, 33, 0, 0, 0, 178, 0, 0, 0, 107, 45, 179, 158, 63, 156, 9, 148, 37, 0, 0, 0, 114, 0, 0, 0, 205, 27, 216, 121, 122, 110, 246, 179, 37, 0, 0, 0, 82, 0, 0, 0, 165, 210, 151, 166, 169, 8, 254, 255, 37, 0, 0, 0, 50, 0, 0, 0, 66, 114, 97, 110, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 65, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 80, 97, 114, 97, 109, 115, 0, 0, 0, 80, 97, 114, 97, 109, 76, 105, 115, 116, 0, 0, 0, 0, 0, 0, 0, 80, 97, 114, 97, 109, 0, 0, 0, 168, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 4, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 4, 0, 0, 3, 0, 1, 0, 144, 4, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 150, 174, 3, 193, 115, 201, 240, 137, 141, 4, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 4, 0, 0, 3, 0, 1, 0, 144, 4, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 4, 0, 0, 3, 0, 1, 0, 168, 4, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 4, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 164, 4, 0, 0, 3, 0, 1, 0, 176, 4, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 4, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 4, 0, 0, 3, 0, 1, 0, 180, 4, 0, 0, 2, 0, 1, 0, 7, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 4, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 4, 0, 0, 3, 0, 1, 0, 188, 4, 0, 0, 2, 0, 1, 0, 8, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 4, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 4, 0, 0, 3, 0, 1, 0, 192, 4, 0, 0, 2, 0, 1, 0, 9, 0, 254, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 207, 44, 254, 11, 168, 100, 49, 233, 189, 4, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 253, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 12, 13, 207, 141, 42, 50, 72, 179, 165, 4, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 4, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 4, 0, 0, 3, 0, 1, 0, 148, 4, 0, 0, 2, 0, 1, 0, 12, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 4, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 4, 0, 0, 3, 0, 1, 0, 156, 4, 0, 0, 2, 0, 1, 0, 13, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 4, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 4, 0, 0, 3, 0, 1, 0, 160, 4, 0, 0, 2, 0, 1, 0, 14, 0, 249, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 200, 88, 179, 143, 32, 34, 38, 143, 157, 4, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 248, 255, 0, 0, 0, 0, 0, 0, 1, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 4, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 4, 0, 0, 3, 0, 1, 0, 140, 4, 0, 0, 2, 0, 1, 0, 16, 0, 247, 255, 0, 0, 0, 0, 0, 0, 1, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 4, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 4, 0, 0, 3, 0, 1, 0, 144, 4, 0, 0, 2, 0, 1, 0, 17, 0, 246, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 211, 53, 2, 243, 234, 144, 42, 153, 141, 4, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 245, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 185, 192, 23, 214, 71, 24, 151, 235, 121, 4, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 244, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 127, 3, 152, 49, 110, 232, 185, 156, 97, 4, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 243, 255, 5, 0, 0, 0, 0, 0, 1, 0, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 4, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 4, 0, 0, 3, 0, 1, 0, 84, 4, 0, 0, 2, 0, 1, 0, 21, 0, 242, 255, 5, 0, 0, 0, 0, 0, 1, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 4, 0, 0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 4, 0, 0, 3, 0, 1, 0, 92, 4, 0, 0, 2, 0, 1, 0, 22, 0, 241, 255, 0, 0, 0, 0, 0, 0, 1, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 4, 0, 0, 3, 0, 1, 0, 100, 4, 0, 0, 2, 0, 1, 0, 23, 0, 240, 255, 0, 0, 0, 0, 0, 0, 1, 0, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 4, 0, 0, 3, 0, 1, 0, 108, 4, 0, 0, 2, 0, 1, 0, 24, 0, 239, 255, 0, 0, 0, 0, 0, 0, 1, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 4, 0, 0, 3, 0, 1, 0, 116, 4, 0, 0, 2, 0, 1, 0, 25, 0, 238, 255, 0, 0, 0, 0, 0, 0, 1, 0, 43, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 4, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 4, 0, 0, 3, 0, 1, 0, 124, 4, 0, 0, 2, 0, 1, 0, 26, 0, 237, 255, 0, 0, 0, 0, 0, 0, 1, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 4, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 4, 0, 0, 3, 0, 1, 0, 132, 4, 0, 0, 2, 0, 1, 0, 27, 0, 236, 255, 0, 0, 0, 0, 0, 0, 1, 0, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 129, 4, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 4, 0, 0, 3, 0, 1, 0, 140, 4, 0, 0, 2, 0, 1, 0, 28, 0, 235, 255, 0, 0, 0, 0, 0, 0, 1, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 4, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 4, 0, 0, 3, 0, 1, 0, 148, 4, 0, 0, 2, 0, 1, 0, 29, 0, 234, 255, 0, 0, 0, 0, 0, 0, 1, 0, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 4, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 4, 0, 0, 3, 0, 1, 0, 156, 4, 0, 0, 2, 0, 1, 0, 30, 0, 233, 255, 0, 0, 0, 0, 0, 0, 1, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 4, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 4, 0, 0, 3, 0, 1, 0, 164, 4, 0, 0, 2, 0, 1, 0, 31, 0, 232, 255, 0, 0, 0, 0, 0, 0, 1, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161, 4, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 4, 0, 0, 3, 0, 1, 0, 172, 4, 0, 0, 2, 0, 1, 0, 32, 0, 231, 255, 0, 0, 0, 0, 0, 0, 1, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 169, 4, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 4, 0, 0, 3, 0, 1, 0, 180, 4, 0, 0, 2, 0, 1, 0, 33, 0, 230, 255, 0, 0, 0, 0, 0, 0, 1, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 4, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 4, 0, 0, 3, 0, 1, 0, 188, 4, 0, 0, 2, 0, 1, 0, 34, 0, 229, 255, 0, 0, 0, 0, 0, 0, 1, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 4, 0, 0, 3, 0, 1, 0, 196, 4, 0, 0, 2, 0, 1, 0, 35, 0, 228, 255, 0, 0, 0, 0, 0, 0, 1, 0, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 4, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 4, 0, 0, 3, 0, 1, 0, 204, 4, 0, 0, 2, 0, 1, 0, 36, 0, 227, 255, 0, 0, 0, 0, 0, 0, 1, 0, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 201, 4, 0, 0, 98, 0, 0, 0, 205, 4, 0, 0, 31, 0, 0, 0, 248, 4, 0, 0, 3, 0, 1, 0, 4, 5, 0, 0, 2, 0, 1, 0, 37, 0, 226, 255, 0, 0, 0, 0, 0, 0, 1, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 5, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 3, 0, 1, 0, 12, 5, 0, 0, 2, 0, 1, 0, 38, 0, 225, 255, 0, 0, 0, 0, 0, 0, 1, 0, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 5, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 5, 0, 0, 3, 0, 1, 0, 24, 5, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 7, 0, 0, 0, 0, 0, 1, 0, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 5, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 5, 0, 0, 3, 0, 1, 0, 48, 5, 0, 0, 2, 0, 1, 0, 39, 0, 224, 255, 0, 0, 0, 0, 0, 0, 1, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 5, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 5, 0, 0, 3, 0, 1, 0, 60, 5, 0, 0, 2, 0, 1, 0, 40, 0, 223, 255, 0, 0, 0, 0, 0, 0, 1, 0, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 5, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 5, 0, 0, 3, 0, 1, 0, 68, 5, 0, 0, 2, 0, 1, 0, 41, 0, 222, 255, 0, 0, 0, 0, 0, 0, 1, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 5, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 5, 0, 0, 3, 0, 1, 0, 80, 5, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 100, 0, 0, 0, 0, 0, 0, 110, 101, 115, 116, 101, 100, 68, 101, 99, 108, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 187, 131, 126, 193, 135, 231, 239, 150, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 144, 98, 130, 115, 212, 137, 4, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 111, 99, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 105, 108, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 115, 105, 110, 103, 0, 0, 0, 99, 111, 110, 115, 116, 0, 0, 0, 101, 110, 117, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 117, 109, 101, 114, 97, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 117, 99, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 105, 101, 108, 100, 0, 0, 0, 117, 110, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 114, 111, 117, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 109, 101, 116, 104, 111, 100, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 110, 97, 107, 101, 100, 73, 100, 0, 16, 0, 0, 0, 0, 0, 0, 0, 242, 44, 214, 147, 54, 122, 28, 153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 97, 107, 101, 100, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 16, 0, 0, 0, 0, 0, 0, 0, 144, 98, 130, 115, 212, 137, 4, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 86, 111, 105, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 66, 111, 111, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 73, 110, 116, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 73, 110, 116, 49, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 73, 110, 116, 51, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 73, 110, 116, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 85, 73, 110, 116, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 85, 73, 110, 116, 49, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 85, 73, 110, 116, 51, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 85, 73, 110, 116, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 70, 108, 111, 97, 116, 51, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 70, 108, 111, 97, 116, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 84, 101, 120, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 68, 97, 116, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 76, 105, 115, 116, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 2, 0, 107, 45, 179, 158, 63, 156, 9, 148, 4, 0, 0, 0, 2, 0, 1, 0, 28, 0, 0, 0, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 23, 0, 0, 0, 4, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 66, 0, 0, 0, 69, 108, 101, 109, 101, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 79, 98, 106, 101, 99, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 65, 110, 121, 80, 111, 105, 110, 116, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 101, 116, 101, 114, 115, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 117, 225, 28, 175, 68, 17, 231, 213, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 65, 110, 121, 83, 116, 114, 117, 99, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 65, 110, 121, 76, 105, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 117, 105, 108, 116, 105, 110, 67, 97, 112, 97, 98, 105, 108, 105, 116, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_96efe787c17e83bb = b_96efe787c17e83bb.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_96efe787c17e83bb[] = { &s_89f0c973c103ae96, &s_8f2622208fb358c8, &s_96efe787c17e83bb, &s_991c7a3693d62cf2, &s_992a90eaf30235d3, &s_9cb9e86e3198037f, &s_b348322a8dcf0d0c, &s_d00489d473826290, &s_d5e71144af1ce175, &s_e75816b56529d464, &s_e93164a80bfe2ccf, &s_eb971847d617c0b9, }; static const uint16_t m_96efe787c17e83bb[] = {18, 3, 40, 37, 39, 22, 41, 34, 31, 32, 24, 25, 26, 23, 35, 36, 33, 28, 29, 30, 27, 21, 9, 6, 5, 10, 11, 13, 7, 15, 1, 16, 17, 20, 19, 0, 2, 38, 4, 12, 14, 8}; static const uint16_t i_96efe787c17e83bb[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 0, 1, 2, 3, 4, 5, 6, 38}; const ::capnp::_::RawSchema s_96efe787c17e83bb = { 0x96efe787c17e83bb, b_96efe787c17e83bb.words, 677, d_96efe787c17e83bb, m_96efe787c17e83bb, 12, 42, i_96efe787c17e83bb, nullptr, nullptr, { &s_96efe787c17e83bb, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<67> b_d5e71144af1ce175 = { { 0, 0, 0, 0, 5, 0, 6, 0, 117, 225, 28, 175, 68, 17, 231, 213, 41, 0, 0, 0, 1, 0, 1, 0, 187, 131, 126, 193, 135, 231, 239, 150, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 1, 0, 0, 45, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 66, 114, 97, 110, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d5e71144af1ce175 = b_d5e71144af1ce175.words; #if !CAPNP_LITE static const uint16_t m_d5e71144af1ce175[] = {2, 0, 1}; static const uint16_t i_d5e71144af1ce175[] = {0, 1, 2}; const ::capnp::_::RawSchema s_d5e71144af1ce175 = { 0xd5e71144af1ce175, b_d5e71144af1ce175.words, 67, nullptr, m_d5e71144af1ce175, 0, 3, i_d5e71144af1ce175, nullptr, nullptr, { &s_d5e71144af1ce175, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<45> b_d00489d473826290 = { { 0, 0, 0, 0, 5, 0, 6, 0, 144, 98, 130, 115, 212, 137, 4, 208, 41, 0, 0, 0, 1, 0, 1, 0, 187, 131, 126, 193, 135, 231, 239, 150, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 1, 0, 0, 49, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 65, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 249, 106, 223, 92, 217, 238, 90, 251, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 97, 109, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, } }; ::capnp::word const* const bp_d00489d473826290 = b_d00489d473826290.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d00489d473826290[] = { &s_8e207d4dfe54d0de, &s_fb5aeed95cdf6af9, }; static const uint16_t m_d00489d473826290[] = {0, 1}; static const uint16_t i_d00489d473826290[] = {0, 1}; const ::capnp::_::RawSchema s_d00489d473826290 = { 0xd00489d473826290, b_d00489d473826290.words, 45, d_d00489d473826290, m_d00489d473826290, 2, 2, i_d00489d473826290, nullptr, nullptr, { &s_d00489d473826290, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<53> b_fb5aeed95cdf6af9 = { { 0, 0, 0, 0, 5, 0, 6, 0, 249, 106, 223, 92, 217, 238, 90, 251, 63, 0, 0, 0, 1, 0, 1, 0, 144, 98, 130, 115, 212, 137, 4, 208, 2, 0, 7, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 42, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 65, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 46, 118, 97, 108, 117, 101, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 56, 0, 0, 0, 2, 0, 1, 0, 110, 111, 110, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 120, 112, 114, 101, 115, 115, 105, 111, 110, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_fb5aeed95cdf6af9 = b_fb5aeed95cdf6af9.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_fb5aeed95cdf6af9[] = { &s_8e207d4dfe54d0de, &s_d00489d473826290, }; static const uint16_t m_fb5aeed95cdf6af9[] = {1, 0}; static const uint16_t i_fb5aeed95cdf6af9[] = {0, 1}; const ::capnp::_::RawSchema s_fb5aeed95cdf6af9 = { 0xfb5aeed95cdf6af9, b_fb5aeed95cdf6af9.words, 53, d_fb5aeed95cdf6af9, m_fb5aeed95cdf6af9, 2, 2, i_fb5aeed95cdf6af9, nullptr, nullptr, { &s_fb5aeed95cdf6af9, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<28> b_94099c3f9eb32d6b = { { 0, 0, 0, 0, 5, 0, 6, 0, 107, 45, 179, 158, 63, 156, 9, 148, 41, 0, 0, 0, 5, 0, 32, 0, 187, 131, 126, 193, 135, 231, 239, 150, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 186, 1, 0, 0, 45, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 98, 117, 105, 108, 116, 105, 110, 80, 97, 114, 97, 109, 115, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 117, 225, 28, 175, 68, 17, 231, 213, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_94099c3f9eb32d6b = b_94099c3f9eb32d6b.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_94099c3f9eb32d6b = { 0x94099c3f9eb32d6b, b_94099c3f9eb32d6b.words, 28, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_94099c3f9eb32d6b, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<102> b_b3f66e7a79d81bcd = { { 0, 0, 0, 0, 5, 0, 6, 0, 205, 27, 216, 121, 122, 110, 246, 179, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 1, 0, 7, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 154, 1, 0, 0, 45, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 31, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 80, 97, 114, 97, 109, 76, 105, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 20, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 125, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 0, 0, 0, 3, 0, 1, 0, 152, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 0, 0, 0, 3, 0, 1, 0, 156, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 0, 0, 0, 3, 0, 1, 0, 164, 0, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 156, 0, 0, 0, 3, 0, 1, 0, 168, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 0, 0, 0, 3, 0, 1, 0, 172, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 100, 76, 105, 115, 116, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 165, 210, 151, 166, 169, 8, 254, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 101, 97, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b3f66e7a79d81bcd = b_b3f66e7a79d81bcd.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_b3f66e7a79d81bcd[] = { &s_8e207d4dfe54d0de, &s_fffe08a9a697d2a5, }; static const uint16_t m_b3f66e7a79d81bcd[] = {3, 0, 2, 4, 1}; static const uint16_t i_b3f66e7a79d81bcd[] = {0, 1, 4, 2, 3}; const ::capnp::_::RawSchema s_b3f66e7a79d81bcd = { 0xb3f66e7a79d81bcd, b_b3f66e7a79d81bcd.words, 102, d_b3f66e7a79d81bcd, m_b3f66e7a79d81bcd, 2, 5, i_b3f66e7a79d81bcd, nullptr, nullptr, { &s_b3f66e7a79d81bcd, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<110> b_fffe08a9a697d2a5 = { { 0, 0, 0, 0, 5, 0, 6, 0, 165, 210, 151, 166, 169, 8, 254, 255, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 4, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 122, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 87, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 80, 97, 114, 97, 109, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 24, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 0, 0, 0, 3, 0, 1, 0, 160, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 157, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 0, 0, 0, 3, 0, 1, 0, 164, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 0, 0, 0, 3, 0, 1, 0, 188, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 71, 234, 136, 253, 21, 69, 16, 229, 185, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 164, 0, 0, 0, 3, 0, 1, 0, 176, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 3, 0, 1, 0, 180, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 100, 212, 41, 101, 181, 22, 88, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 144, 98, 130, 115, 212, 137, 4, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 115, 116, 97, 114, 116, 66, 121, 116, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 100, 66, 121, 116, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_fffe08a9a697d2a5 = b_fffe08a9a697d2a5.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_fffe08a9a697d2a5[] = { &s_8e207d4dfe54d0de, &s_d00489d473826290, &s_e5104515fd88ea47, &s_e75816b56529d464, }; static const uint16_t m_fffe08a9a697d2a5[] = {2, 3, 5, 0, 4, 1}; static const uint16_t i_fffe08a9a697d2a5[] = {0, 1, 2, 3, 4, 5}; const ::capnp::_::RawSchema s_fffe08a9a697d2a5 = { 0xfffe08a9a697d2a5, b_fffe08a9a697d2a5.words, 110, d_fffe08a9a697d2a5, m_fffe08a9a697d2a5, 4, 6, i_fffe08a9a697d2a5, nullptr, nullptr, { &s_fffe08a9a697d2a5, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<51> b_e5104515fd88ea47 = { { 0, 0, 0, 0, 5, 0, 6, 0, 71, 234, 136, 253, 21, 69, 16, 229, 47, 0, 0, 0, 1, 0, 2, 0, 165, 210, 151, 166, 169, 8, 254, 255, 4, 0, 7, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 226, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 80, 97, 114, 97, 109, 46, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 3, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 110, 111, 110, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e5104515fd88ea47 = b_e5104515fd88ea47.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_e5104515fd88ea47[] = { &s_8e207d4dfe54d0de, &s_fffe08a9a697d2a5, }; static const uint16_t m_e5104515fd88ea47[] = {0, 1}; static const uint16_t i_e5104515fd88ea47[] = {0, 1}; const ::capnp::_::RawSchema s_e5104515fd88ea47 = { 0xe5104515fd88ea47, b_e5104515fd88ea47.words, 51, d_e5104515fd88ea47, m_e5104515fd88ea47, 2, 2, i_e5104515fd88ea47, nullptr, nullptr, { &s_e5104515fd88ea47, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<65> b_89f0c973c103ae96 = { { 0, 0, 0, 0, 5, 0, 6, 0, 150, 174, 3, 193, 115, 201, 240, 137, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 98, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 105, 100, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 117, 110, 115, 112, 101, 99, 105, 102, 105, 101, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 100, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 242, 44, 214, 147, 54, 122, 28, 153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 114, 100, 105, 110, 97, 108, 0, 16, 0, 0, 0, 0, 0, 0, 0, 242, 44, 214, 147, 54, 122, 28, 153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_89f0c973c103ae96 = b_89f0c973c103ae96.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_89f0c973c103ae96[] = { &s_96efe787c17e83bb, &s_991c7a3693d62cf2, }; static const uint16_t m_89f0c973c103ae96[] = {2, 1, 0}; static const uint16_t i_89f0c973c103ae96[] = {0, 1, 2}; const ::capnp::_::RawSchema s_89f0c973c103ae96 = { 0x89f0c973c103ae96, b_89f0c973c103ae96.words, 65, d_89f0c973c103ae96, m_89f0c973c103ae96, 2, 3, i_89f0c973c103ae96, nullptr, nullptr, { &s_89f0c973c103ae96, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<34> b_e93164a80bfe2ccf = { { 0, 0, 0, 0, 5, 0, 6, 0, 207, 44, 254, 11, 168, 100, 49, 233, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 122, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 117, 115, 105, 110, 103, 0, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 116, 97, 114, 103, 101, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e93164a80bfe2ccf = b_e93164a80bfe2ccf.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_e93164a80bfe2ccf[] = { &s_8e207d4dfe54d0de, &s_96efe787c17e83bb, }; static const uint16_t m_e93164a80bfe2ccf[] = {0}; static const uint16_t i_e93164a80bfe2ccf[] = {0}; const ::capnp::_::RawSchema s_e93164a80bfe2ccf = { 0xe93164a80bfe2ccf, b_e93164a80bfe2ccf.words, 34, d_e93164a80bfe2ccf, m_e93164a80bfe2ccf, 2, 1, i_e93164a80bfe2ccf, nullptr, nullptr, { &s_e93164a80bfe2ccf, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<49> b_b348322a8dcf0d0c = { { 0, 0, 0, 0, 5, 0, 6, 0, 12, 13, 207, 141, 42, 50, 72, 179, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 122, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 99, 111, 110, 115, 116, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b348322a8dcf0d0c = b_b348322a8dcf0d0c.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_b348322a8dcf0d0c[] = { &s_8e207d4dfe54d0de, &s_96efe787c17e83bb, }; static const uint16_t m_b348322a8dcf0d0c[] = {0, 1}; static const uint16_t i_b348322a8dcf0d0c[] = {0, 1}; const ::capnp::_::RawSchema s_b348322a8dcf0d0c = { 0xb348322a8dcf0d0c, b_b348322a8dcf0d0c.words, 49, d_b348322a8dcf0d0c, m_b348322a8dcf0d0c, 2, 2, i_b348322a8dcf0d0c, nullptr, nullptr, { &s_b348322a8dcf0d0c, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<43> b_8f2622208fb358c8 = { { 0, 0, 0, 0, 5, 0, 6, 0, 200, 88, 179, 143, 32, 34, 38, 143, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 122, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 102, 105, 101, 108, 100, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 31, 149, 23, 230, 29, 162, 209, 208, 45, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, } }; ::capnp::word const* const bp_8f2622208fb358c8 = b_8f2622208fb358c8.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_8f2622208fb358c8[] = { &s_8e207d4dfe54d0de, &s_96efe787c17e83bb, &s_d0d1a21de617951f, }; static const uint16_t m_8f2622208fb358c8[] = {1, 0}; static const uint16_t i_8f2622208fb358c8[] = {0, 1}; const ::capnp::_::RawSchema s_8f2622208fb358c8 = { 0x8f2622208fb358c8, b_8f2622208fb358c8.words, 43, d_8f2622208fb358c8, m_8f2622208fb358c8, 3, 2, i_8f2622208fb358c8, nullptr, nullptr, { &s_8f2622208fb358c8, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<51> b_d0d1a21de617951f = { { 0, 0, 0, 0, 5, 0, 6, 0, 31, 149, 23, 230, 29, 162, 209, 208, 47, 0, 0, 0, 1, 0, 2, 0, 200, 88, 179, 143, 32, 34, 38, 143, 8, 0, 7, 0, 1, 0, 2, 0, 6, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 226, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 102, 105, 101, 108, 100, 46, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 6, 0, 0, 0, 0, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 110, 111, 110, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d0d1a21de617951f = b_d0d1a21de617951f.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d0d1a21de617951f[] = { &s_8e207d4dfe54d0de, &s_8f2622208fb358c8, }; static const uint16_t m_d0d1a21de617951f[] = {0, 1}; static const uint16_t i_d0d1a21de617951f[] = {0, 1}; const ::capnp::_::RawSchema s_d0d1a21de617951f = { 0xd0d1a21de617951f, b_d0d1a21de617951f.words, 51, d_d0d1a21de617951f, m_d0d1a21de617951f, 2, 2, i_d0d1a21de617951f, nullptr, nullptr, { &s_d0d1a21de617951f, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<40> b_992a90eaf30235d3 = { { 0, 0, 0, 0, 5, 0, 6, 0, 211, 53, 2, 243, 234, 144, 42, 153, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 154, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 40, 0, 0, 0, 2, 0, 1, 0, 115, 117, 112, 101, 114, 99, 108, 97, 115, 115, 101, 115, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_992a90eaf30235d3 = b_992a90eaf30235d3.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_992a90eaf30235d3[] = { &s_8e207d4dfe54d0de, &s_96efe787c17e83bb, }; static const uint16_t m_992a90eaf30235d3[] = {0}; static const uint16_t i_992a90eaf30235d3[] = {0}; const ::capnp::_::RawSchema s_992a90eaf30235d3 = { 0x992a90eaf30235d3, b_992a90eaf30235d3.words, 40, d_992a90eaf30235d3, m_992a90eaf30235d3, 2, 1, i_992a90eaf30235d3, nullptr, nullptr, { &s_992a90eaf30235d3, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<42> b_eb971847d617c0b9 = { { 0, 0, 0, 0, 5, 0, 6, 0, 185, 192, 23, 214, 71, 24, 151, 235, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 130, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 109, 101, 116, 104, 111, 100, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 115, 81, 214, 98, 125, 140, 35, 198, 45, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 115, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 205, 27, 216, 121, 122, 110, 246, 179, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 115, 117, 108, 116, 115, 0, } }; ::capnp::word const* const bp_eb971847d617c0b9 = b_eb971847d617c0b9.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_eb971847d617c0b9[] = { &s_96efe787c17e83bb, &s_b3f66e7a79d81bcd, &s_c6238c7d62d65173, }; static const uint16_t m_eb971847d617c0b9[] = {0, 1}; static const uint16_t i_eb971847d617c0b9[] = {0, 1}; const ::capnp::_::RawSchema s_eb971847d617c0b9 = { 0xeb971847d617c0b9, b_eb971847d617c0b9.words, 42, d_eb971847d617c0b9, m_eb971847d617c0b9, 3, 2, i_eb971847d617c0b9, nullptr, nullptr, { &s_eb971847d617c0b9, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<51> b_c6238c7d62d65173 = { { 0, 0, 0, 0, 5, 0, 6, 0, 115, 81, 214, 98, 125, 140, 35, 198, 48, 0, 0, 0, 1, 0, 2, 0, 185, 192, 23, 214, 71, 24, 151, 235, 8, 0, 7, 0, 1, 0, 2, 0, 6, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 109, 101, 116, 104, 111, 100, 46, 114, 101, 115, 117, 108, 116, 115, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 6, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 56, 0, 0, 0, 2, 0, 1, 0, 110, 111, 110, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 120, 112, 108, 105, 99, 105, 116, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 205, 27, 216, 121, 122, 110, 246, 179, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c6238c7d62d65173 = b_c6238c7d62d65173.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c6238c7d62d65173[] = { &s_b3f66e7a79d81bcd, &s_eb971847d617c0b9, }; static const uint16_t m_c6238c7d62d65173[] = {1, 0}; static const uint16_t i_c6238c7d62d65173[] = {0, 1}; const ::capnp::_::RawSchema s_c6238c7d62d65173 = { 0xc6238c7d62d65173, b_c6238c7d62d65173.words, 51, d_c6238c7d62d65173, m_c6238c7d62d65173, 2, 2, i_c6238c7d62d65173, nullptr, nullptr, { &s_c6238c7d62d65173, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<230> b_9cb9e86e3198037f = { { 0, 0, 0, 0, 5, 0, 6, 0, 127, 3, 152, 49, 110, 232, 185, 156, 41, 0, 0, 0, 1, 0, 2, 0, 187, 131, 126, 193, 135, 231, 239, 150, 8, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 162, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 223, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 68, 101, 99, 108, 97, 114, 97, 116, 105, 111, 110, 46, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 52, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 1, 0, 0, 3, 0, 1, 0, 100, 1, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 96, 0, 0, 0, 0, 0, 1, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 1, 0, 0, 3, 0, 1, 0, 108, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 97, 0, 0, 0, 0, 0, 1, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 1, 0, 0, 3, 0, 1, 0, 116, 1, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 98, 0, 0, 0, 0, 0, 1, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 1, 0, 0, 3, 0, 1, 0, 124, 1, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 99, 0, 0, 0, 0, 0, 1, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 1, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 1, 0, 0, 3, 0, 1, 0, 136, 1, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 100, 0, 0, 0, 0, 0, 1, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 1, 0, 0, 3, 0, 1, 0, 144, 1, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 101, 0, 0, 0, 0, 0, 1, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 1, 0, 0, 3, 0, 1, 0, 152, 1, 0, 0, 2, 0, 1, 0, 7, 0, 0, 0, 102, 0, 0, 0, 0, 0, 1, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 1, 0, 0, 3, 0, 1, 0, 160, 1, 0, 0, 2, 0, 1, 0, 8, 0, 0, 0, 103, 0, 0, 0, 0, 0, 1, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 157, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 156, 1, 0, 0, 3, 0, 1, 0, 168, 1, 0, 0, 2, 0, 1, 0, 9, 0, 0, 0, 104, 0, 0, 0, 0, 0, 1, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 1, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 1, 0, 0, 3, 0, 1, 0, 180, 1, 0, 0, 2, 0, 1, 0, 10, 0, 0, 0, 105, 0, 0, 0, 0, 0, 1, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 1, 0, 0, 3, 0, 1, 0, 188, 1, 0, 0, 2, 0, 1, 0, 11, 0, 0, 0, 106, 0, 0, 0, 0, 0, 1, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 1, 0, 0, 3, 0, 1, 0, 196, 1, 0, 0, 2, 0, 1, 0, 12, 0, 0, 0, 107, 0, 0, 0, 0, 0, 1, 0, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 1, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1, 0, 0, 3, 0, 1, 0, 208, 1, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 222, 208, 84, 254, 77, 125, 32, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 70, 105, 108, 101, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 67, 111, 110, 115, 116, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 69, 110, 117, 109, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 69, 110, 117, 109, 101, 114, 97, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 83, 116, 114, 117, 99, 116, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 70, 105, 101, 108, 100, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 85, 110, 105, 111, 110, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 71, 114, 111, 117, 112, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 73, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 77, 101, 116, 104, 111, 100, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 80, 97, 114, 97, 109, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9cb9e86e3198037f = b_9cb9e86e3198037f.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9cb9e86e3198037f[] = { &s_8e207d4dfe54d0de, &s_96efe787c17e83bb, }; static const uint16_t m_9cb9e86e3198037f[] = {12, 2, 3, 4, 6, 1, 8, 9, 10, 11, 5, 7, 0}; static const uint16_t i_9cb9e86e3198037f[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; const ::capnp::_::RawSchema s_9cb9e86e3198037f = { 0x9cb9e86e3198037f, b_9cb9e86e3198037f.words, 230, d_9cb9e86e3198037f, m_9cb9e86e3198037f, 2, 13, i_9cb9e86e3198037f, nullptr, nullptr, { &s_9cb9e86e3198037f, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<34> b_84e4f3f5a807605c = { { 0, 0, 0, 0, 5, 0, 6, 0, 92, 96, 7, 168, 245, 243, 228, 132, 29, 0, 0, 0, 1, 0, 0, 0, 198, 195, 187, 220, 104, 225, 107, 197, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 66, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 111, 109, 112, 105, 108, 101, 114, 47, 103, 114, 97, 109, 109, 97, 114, 46, 99, 97, 112, 110, 112, 58, 80, 97, 114, 115, 101, 100, 70, 105, 108, 101, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 114, 111, 111, 116, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 187, 131, 126, 193, 135, 231, 239, 150, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_84e4f3f5a807605c = b_84e4f3f5a807605c.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_84e4f3f5a807605c[] = { &s_96efe787c17e83bb, }; static const uint16_t m_84e4f3f5a807605c[] = {0}; static const uint16_t i_84e4f3f5a807605c[] = {0}; const ::capnp::_::RawSchema s_84e4f3f5a807605c = { 0x84e4f3f5a807605c, b_84e4f3f5a807605c.words, 34, d_84e4f3f5a807605c, m_84e4f3f5a807605c, 1, 1, i_84e4f3f5a807605c, nullptr, nullptr, { &s_84e4f3f5a807605c, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { namespace compiler { // LocatedText constexpr uint16_t LocatedText::_capnpPrivate::dataWordSize; constexpr uint16_t LocatedText::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind LocatedText::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* LocatedText::_capnpPrivate::schema; #endif // !CAPNP_LITE // LocatedInteger constexpr uint16_t LocatedInteger::_capnpPrivate::dataWordSize; constexpr uint16_t LocatedInteger::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind LocatedInteger::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* LocatedInteger::_capnpPrivate::schema; #endif // !CAPNP_LITE // LocatedFloat constexpr uint16_t LocatedFloat::_capnpPrivate::dataWordSize; constexpr uint16_t LocatedFloat::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind LocatedFloat::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* LocatedFloat::_capnpPrivate::schema; #endif // !CAPNP_LITE // Expression constexpr uint16_t Expression::_capnpPrivate::dataWordSize; constexpr uint16_t Expression::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Expression::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Expression::_capnpPrivate::schema; #endif // !CAPNP_LITE // Expression::Param constexpr uint16_t Expression::Param::_capnpPrivate::dataWordSize; constexpr uint16_t Expression::Param::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Expression::Param::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Expression::Param::_capnpPrivate::schema; #endif // !CAPNP_LITE // Expression::Application constexpr uint16_t Expression::Application::_capnpPrivate::dataWordSize; constexpr uint16_t Expression::Application::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Expression::Application::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Expression::Application::_capnpPrivate::schema; #endif // !CAPNP_LITE // Expression::Member constexpr uint16_t Expression::Member::_capnpPrivate::dataWordSize; constexpr uint16_t Expression::Member::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Expression::Member::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Expression::Member::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration constexpr uint16_t Declaration::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::BrandParameter constexpr uint16_t Declaration::BrandParameter::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::BrandParameter::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::BrandParameter::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::BrandParameter::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::AnnotationApplication constexpr uint16_t Declaration::AnnotationApplication::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::AnnotationApplication::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::AnnotationApplication::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::AnnotationApplication::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::AnnotationApplication::Value constexpr uint16_t Declaration::AnnotationApplication::Value::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::AnnotationApplication::Value::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::AnnotationApplication::Value::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::AnnotationApplication::Value::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::ParamList constexpr uint16_t Declaration::ParamList::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::ParamList::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::ParamList::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::ParamList::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Param constexpr uint16_t Declaration::Param::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Param::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Param::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Param::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Param::DefaultValue constexpr uint16_t Declaration::Param::DefaultValue::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Param::DefaultValue::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Param::DefaultValue::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Param::DefaultValue::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Id constexpr uint16_t Declaration::Id::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Id::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Id::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Id::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Using constexpr uint16_t Declaration::Using::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Using::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Using::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Using::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Const constexpr uint16_t Declaration::Const::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Const::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Const::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Const::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Field constexpr uint16_t Declaration::Field::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Field::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Field::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Field::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Field::DefaultValue constexpr uint16_t Declaration::Field::DefaultValue::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Field::DefaultValue::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Field::DefaultValue::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Field::DefaultValue::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Interface constexpr uint16_t Declaration::Interface::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Interface::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Interface::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Interface::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Method constexpr uint16_t Declaration::Method::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Method::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Method::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Method::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Method::Results constexpr uint16_t Declaration::Method::Results::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Method::Results::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Method::Results::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Method::Results::_capnpPrivate::schema; #endif // !CAPNP_LITE // Declaration::Annotation constexpr uint16_t Declaration::Annotation::_capnpPrivate::dataWordSize; constexpr uint16_t Declaration::Annotation::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Declaration::Annotation::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Declaration::Annotation::_capnpPrivate::schema; #endif // !CAPNP_LITE // ParsedFile constexpr uint16_t ParsedFile::_capnpPrivate::dataWordSize; constexpr uint16_t ParsedFile::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind ParsedFile::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* ParsedFile::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/compiler/parser.h0000644000175000017500000001332313650101756022125 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include #include #include "error-reporter.h" CAPNP_BEGIN_HEADER namespace capnp { namespace compiler { void parseFile(List::Reader statements, ParsedFile::Builder result, ErrorReporter& errorReporter); // Parse a list of statements to build a ParsedFile. // // If any errors are reported, then the output is not usable. However, it may be passed on through // later stages of compilation in order to detect additional errors. uint64_t generateRandomId(); // Generate a new random unique ID. This lives here mostly for lack of a better location. uint64_t generateChildId(uint64_t parentId, kj::StringPtr childName); // Generate the ID for a child node given its parent ID and name. uint64_t generateGroupId(uint64_t parentId, uint16_t groupIndex); // Generate the ID for a group within a struct. uint64_t generateMethodParamsId(uint64_t parentId, uint16_t methodOrdinal, bool isResults); // Generate the ID for a struct representing method params / results. // // TODO(cleanup): Move generate*Id() somewhere more sensible. class CapnpParser { // Advanced parser interface. This interface exposes the inner parsers so that you can embed // them into your own parsers. public: CapnpParser(Orphanage orphanage, ErrorReporter& errorReporter); // `orphanage` is used to allocate Cap'n Proto message objects in the result. `inputStart` is // a pointer to the beginning of the input, used to compute byte offsets. ~CapnpParser() noexcept(false); KJ_DISALLOW_COPY(CapnpParser); using ParserInput = kj::parse::IteratorInput::Reader::Iterator>; struct DeclParserResult; template using Parser = kj::parse::ParserRef; using DeclParser = Parser; kj::Maybe> parseStatement( Statement::Reader statement, const DeclParser& parser); // Parse a statement using the given parser. In addition to parsing the token sequence itself, // this takes care of parsing the block (if any) and copying over the doc comment (if any). struct DeclParserResult { // DeclParser parses a sequence of tokens representing just the "line" part of the statement -- // i.e. everything up to the semicolon or opening curly brace. // // Use `parseStatement()` to avoid having to deal with this struct. Orphan decl; // The decl parsed so far. The decl's `docComment` and `nestedDecls` are both empty at this // point. kj::Maybe memberParser; // If null, the statement should not have a block. If non-null, the statement should have a // block containing statements parseable by this parser. DeclParserResult(Orphan&& decl, const DeclParser& memberParser) : decl(kj::mv(decl)), memberParser(memberParser) {} explicit DeclParserResult(Orphan&& decl) : decl(kj::mv(decl)), memberParser(nullptr) {} }; struct Parsers { DeclParser genericDecl; // Parser that matches any declaration type except those that have ordinals (since they are // context-dependent). DeclParser fileLevelDecl; DeclParser enumLevelDecl; DeclParser structLevelDecl; DeclParser interfaceLevelDecl; // Parsers that match genericDecl *and* the ordinal-based declaration types valid in the given // contexts. Note that these may match declarations that are not actually allowed in the given // contexts, as long as the grammar is unambiguous. E.g. nested types are not allowed in // enums, but they'll be accepted by enumLevelDecl. A later stage of compilation should report // these as errors. Parser> expression; Parser> annotation; Parser> uid; Parser> ordinal; Parser> param; DeclParser usingDecl; DeclParser constDecl; DeclParser enumDecl; DeclParser enumerantDecl; DeclParser structDecl; DeclParser fieldDecl; DeclParser unionDecl; DeclParser groupDecl; DeclParser interfaceDecl; DeclParser methodDecl; DeclParser paramDecl; DeclParser annotationDecl; // Parsers for individual declaration types. }; const Parsers& getParsers() { return parsers; } private: Orphanage orphanage; ErrorReporter& errorReporter; kj::Arena arena; Parsers parsers; }; } // namespace compiler } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/compiler/parser.c++0000644000175000017500000013361113650101756022251 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "parser.h" #include "type-id.h" #include #include #if !_MSC_VER #include #endif #include #include #include #if _WIN32 #define WIN32_LEAN_AND_MEAN #include #include #undef VOID #endif namespace capnp { namespace compiler { uint64_t generateRandomId() { uint64_t result; #if _WIN32 HCRYPTPROV handle; KJ_ASSERT(CryptAcquireContextW(&handle, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)); KJ_DEFER(KJ_ASSERT(CryptReleaseContext(handle, 0)) {break;}); KJ_ASSERT(CryptGenRandom(handle, sizeof(result), reinterpret_cast(&result))); #else int fd; KJ_SYSCALL(fd = open("/dev/urandom", O_RDONLY)); ssize_t n; KJ_SYSCALL(n = read(fd, &result, sizeof(result)), "/dev/urandom"); KJ_ASSERT(n == sizeof(result), "Incomplete read from /dev/urandom.", n); #endif return result | (1ull << 63); } void parseFile(List::Reader statements, ParsedFile::Builder result, ErrorReporter& errorReporter) { CapnpParser parser(Orphanage::getForMessageContaining(result), errorReporter); kj::Vector> decls(statements.size()); kj::Vector> annotations; auto fileDecl = result.getRoot(); fileDecl.setFile(VOID); for (auto statement: statements) { KJ_IF_MAYBE(decl, parser.parseStatement(statement, parser.getParsers().fileLevelDecl)) { Declaration::Builder builder = decl->get(); switch (builder.which()) { case Declaration::NAKED_ID: if (fileDecl.getId().isUid()) { errorReporter.addError(builder.getStartByte(), builder.getEndByte(), "File can only have one ID."); } else { fileDecl.getId().adoptUid(builder.disownNakedId()); if (builder.hasDocComment()) { fileDecl.adoptDocComment(builder.disownDocComment()); } } break; case Declaration::NAKED_ANNOTATION: annotations.add(builder.disownNakedAnnotation()); break; default: decls.add(kj::mv(*decl)); break; } } } if (fileDecl.getId().which() != Declaration::Id::UID) { // We didn't see an ID. Generate one randomly for now. uint64_t id = generateRandomId(); fileDecl.getId().initUid().setValue(id); // Don't report missing ID if there was a parse error, because quite often the parse error // prevents us from parsing the ID even though it is actually there. if (!errorReporter.hadErrors()) { errorReporter.addError(0, 0, kj::str("File does not declare an ID. I've generated one for you. Add this line to " "your file: @0x", kj::hex(id), ";")); } } auto declsBuilder = fileDecl.initNestedDecls(decls.size()); for (size_t i = 0; i < decls.size(); i++) { declsBuilder.adoptWithCaveats(i, kj::mv(decls[i])); } auto annotationsBuilder = fileDecl.initAnnotations(annotations.size()); for (size_t i = 0; i < annotations.size(); i++) { annotationsBuilder.adoptWithCaveats(i, kj::mv(annotations[i])); } } namespace p = kj::parse; namespace { // ======================================================================================= template struct Located { T value; uint32_t startByte; uint32_t endByte; template void copyLocationTo(Builder builder) { builder.setStartByte(startByte); builder.setEndByte(endByte); } template void copyTo(Builder builder) { builder.setValue(value); copyLocationTo(builder); } template Orphan asProto(Orphanage orphanage) { auto result = orphanage.newOrphan(); copyTo(result.get()); return result; } template Located> rewrap(Other&& other) { return Located(kj::fwd(other), startByte, endByte); } Located(const T& value, uint32_t startByte, uint32_t endByte) : value(value), startByte(startByte), endByte(endByte) {} Located(T&& value, uint32_t startByte, uint32_t endByte) : value(kj::mv(value)), startByte(startByte), endByte(endByte) {} }; // ======================================================================================= template struct MatchTokenType { kj::Maybe> operator()(Token::Reader token) const { if (token.which() == type) { return Located((token.*get)(), token.getStartByte(), token.getEndByte()); } else { return nullptr; } } }; #define TOKEN_TYPE_PARSER(type, discrim, getter) \ p::transformOrReject(p::any, \ MatchTokenType()) constexpr auto identifier = TOKEN_TYPE_PARSER(Text::Reader, IDENTIFIER, getIdentifier); constexpr auto stringLiteral = TOKEN_TYPE_PARSER(Text::Reader, STRING_LITERAL, getStringLiteral); constexpr auto binaryLiteral = TOKEN_TYPE_PARSER(Data::Reader, BINARY_LITERAL, getBinaryLiteral); constexpr auto integerLiteral = TOKEN_TYPE_PARSER(uint64_t, INTEGER_LITERAL, getIntegerLiteral); constexpr auto floatLiteral = TOKEN_TYPE_PARSER(double, FLOAT_LITERAL, getFloatLiteral); constexpr auto operatorToken = TOKEN_TYPE_PARSER(Text::Reader, OPERATOR, getOperator); constexpr auto rawParenthesizedList = TOKEN_TYPE_PARSER(List>::Reader, PARENTHESIZED_LIST, getParenthesizedList); constexpr auto rawBracketedList = TOKEN_TYPE_PARSER(List>::Reader, BRACKETED_LIST, getBracketedList); // ======================================================================================= class ExactString { public: constexpr ExactString(const char* expected): expected(expected) {} kj::Maybe> operator()(Located&& text) const { if (text.value == expected) { return kj::Tuple<>(); } else { return nullptr; } } private: const char* expected; }; constexpr auto keyword(const char* expected) -> decltype(p::transformOrReject(identifier, ExactString(expected))) { return p::transformOrReject(identifier, ExactString(expected)); } constexpr auto op(const char* expected) -> decltype(p::transformOrReject(operatorToken, ExactString(expected))) { return p::transformOrReject(operatorToken, ExactString(expected)); } class LocatedExactString { public: constexpr LocatedExactString(const char* expected): expected(expected) {} kj::Maybe> operator()(Located&& text) const { if (text.value == expected) { return kj::mv(text); } else { return nullptr; } } private: const char* expected; }; constexpr auto locatedKeyword(const char* expected) -> decltype(p::transformOrReject(identifier, LocatedExactString(expected))) { return p::transformOrReject(identifier, LocatedExactString(expected)); } // ======================================================================================= template class ParseListItems { // Transformer that parses all items in the input token sequence list using the given parser. public: constexpr ParseListItems(ItemParser&& itemParser, ErrorReporter& errorReporter) : itemParser(p::sequence(kj::fwd(itemParser), p::endOfInput)), errorReporter(errorReporter) {} Located>>> operator()( Located>::Reader>&& items) const { auto result = kj::heapArray>>( items.value.size()); for (uint i = 0; i < items.value.size(); i++) { auto item = items.value[i]; CapnpParser::ParserInput input(item.begin(), item.end()); result[i] = itemParser(input); if (result[i] == nullptr) { // Parsing failed. Report an error. auto best = input.getBest(); if (best < item.end()) { // Report error from the point where parsing failed to the end of the item. errorReporter.addError( best->getStartByte(), (item.end() - 1)->getEndByte(), "Parse error."); } else if (item.size() > 0) { // The item is non-empty and the parser consumed all of it before failing. Report an // error for the whole thing. errorReporter.addError( item.begin()->getStartByte(), (item.end() - 1)->getEndByte(), "Parse error."); } else { // The item has no content. // TODO(cleanup): We don't actually know the item's location, so we can only report // an error across the whole list. Fix this. errorReporter.addError(items.startByte, items.endByte, "Parse error: Empty list item."); } } } return Located>>>( kj::mv(result), items.startByte, items.endByte); } private: decltype(p::sequence(kj::instance(), p::endOfInput)) itemParser; ErrorReporter& errorReporter; }; template constexpr auto parenthesizedList(ItemParser&& itemParser, ErrorReporter& errorReporter) -> decltype( transform(rawParenthesizedList, ParseListItems( kj::fwd(itemParser), errorReporter))) { return transform(rawParenthesizedList, ParseListItems( kj::fwd(itemParser), errorReporter)); } template constexpr auto bracketedList(ItemParser&& itemParser, ErrorReporter& errorReporter) -> decltype( transform(rawBracketedList, ParseListItems( kj::fwd(itemParser), errorReporter))) { return transform(rawBracketedList, ParseListItems( kj::fwd(itemParser), errorReporter)); } // ======================================================================================= template Orphan> arrayToList(Orphanage& orphanage, kj::Array>&& elements) { auto result = orphanage.newOrphan>(elements.size()); auto builder = result.get(); for (size_t i = 0; i < elements.size(); i++) { builder.adoptWithCaveats(i, kj::mv(elements[i])); } return kj::mv(result); } static void initGenericParams(Declaration::Builder builder, kj::Maybe>>>>&& genericParameters) { KJ_IF_MAYBE(p, genericParameters) { auto params = builder.initParameters(p->value.size()); for (uint i: kj::indices(p->value)) { KJ_IF_MAYBE(name, p->value[i]) { auto param = params[i]; param.setName(name->value); name->copyLocationTo(param); } } } } static Declaration::Builder initDecl( Declaration::Builder builder, Located&& name, kj::Maybe>&& id, kj::Maybe>>>>&& genericParameters, kj::Array>&& annotations) { name.copyTo(builder.initName()); KJ_IF_MAYBE(i, id) { builder.getId().adoptUid(kj::mv(*i)); } initGenericParams(builder, kj::mv(genericParameters)); auto list = builder.initAnnotations(annotations.size()); for (uint i = 0; i < annotations.size(); i++) { list.adoptWithCaveats(i, kj::mv(annotations[i])); } return builder; } static Declaration::Builder initMemberDecl( Declaration::Builder builder, Located&& name, Orphan&& ordinal, kj::Array>&& annotations) { name.copyTo(builder.initName()); builder.getId().adoptOrdinal(kj::mv(ordinal)); auto list = builder.initAnnotations(annotations.size()); for (uint i = 0; i < annotations.size(); i++) { list.adoptWithCaveats(i, kj::mv(annotations[i])); } return builder; } template void initLocation(kj::parse::Span::Reader::Iterator> location, BuilderType builder) { if (location.begin() < location.end()) { builder.setStartByte(location.begin()->getStartByte()); builder.setEndByte((location.end() - 1)->getEndByte()); } } } // namespace // ======================================================================================= CapnpParser::CapnpParser(Orphanage orphanageParam, ErrorReporter& errorReporterParam) : orphanage(orphanageParam), errorReporter(errorReporterParam) { auto& tupleElement = arena.copy(p::transform( p::sequence(p::optional(p::sequence(identifier, op("="))), parsers.expression), [this](kj::Maybe>&& fieldName, Orphan&& fieldValue) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); KJ_IF_MAYBE(fn, fieldName) { fn->copyTo(builder.initNamed()); } else { builder.setUnnamed(); } builder.adoptValue(kj::mv(fieldValue)); return kj::mv(result); })); auto& tuple = arena.copy>>>>( arena.copy(p::transform( parenthesizedList(tupleElement, errorReporter), [this](Located>>>&& elements) -> Located>> { auto result = orphanage.newOrphan>(elements.value.size()); auto builder = result.get(); for (uint i: kj::indices(elements.value)) { KJ_IF_MAYBE(e, elements.value[i]) { builder.adoptWithCaveats(i, kj::mv(*e)); } else { builder[i].initValue().setUnknown(); } } return elements.rewrap(kj::mv(result)); }))); parsers.expression = arena.copy(p::transform( p::sequence( // Base expression. p::oneOf( p::transform(integerLiteral, [this](Located&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setPositiveInt(value.value); value.copyLocationTo(builder); return result; }), p::transform(p::sequence(op("-"), integerLiteral), [this](Located&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setNegativeInt(value.value); value.copyLocationTo(builder); return result; }), p::transform(floatLiteral, [this](Located&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setFloat(value.value); value.copyLocationTo(builder); return result; }), p::transform(p::sequence(op("-"), floatLiteral), [this](Located&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setFloat(-value.value); value.copyLocationTo(builder); return result; }), p::transformWithLocation(p::sequence(op("-"), keyword("inf")), [this](kj::parse::Span::Reader::Iterator> location) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setFloat(-kj::inf()); initLocation(location, builder); return result; }), p::transform(p::oneOrMore(stringLiteral), [this](kj::Array>&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setString(kj::strArray( KJ_MAP(part, value) { return part.value; }, "")); builder.setStartByte(value.front().startByte); builder.setEndByte(value.back().endByte); return result; }), p::transform(binaryLiteral, [this](Located&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.setBinary(value.value); value.copyLocationTo(builder); return result; }), p::transform(bracketedList(parsers.expression, errorReporter), [this](Located>>>&& value) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); auto listBuilder = builder.initList(value.value.size()); for (uint i = 0; i < value.value.size(); i++) { KJ_IF_MAYBE(element, value.value[i]) { listBuilder.adoptWithCaveats(i, kj::mv(*element)); } } value.copyLocationTo(builder); return result; }), p::transform(tuple, [this](Located>>&& value) -> Orphan { auto elements = value.value.get(); if (elements.size() == 1 && elements[0].isUnnamed()) { // Single-value tuple is just a value. return elements[0].disownValue(); } else { auto result = orphanage.newOrphan(); auto builder = result.get(); builder.adoptTuple(kj::mv(value.value)); value.copyLocationTo(builder); return result; } }), p::transformWithLocation(p::sequence(keyword("import"), stringLiteral), [this](kj::parse::Span::Reader::Iterator> location, Located&& filename) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); initLocation(location, builder); filename.copyTo(builder.initImport()); return result; }), p::transformWithLocation(p::sequence(keyword("embed"), stringLiteral), [this](kj::parse::Span::Reader::Iterator> location, Located&& filename) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); initLocation(location, builder); filename.copyTo(builder.initEmbed()); return result; }), p::transformWithLocation(p::sequence(op("."), identifier), [this](kj::parse::Span::Reader::Iterator> location, Located&& name) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); initLocation(location, builder); name.copyTo(builder.initAbsoluteName()); return result; }), p::transform(identifier, [this](Located&& name) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); name.copyTo(builder.initRelativeName()); name.copyLocationTo(builder); return result; })), // Suffixes, e.g. ".member" or "(param1, param2)". p::many(p::oneOf( p::transformWithLocation(p::sequence(op("."), identifier), [this](kj::parse::Span::Reader::Iterator> location, Located&& name) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); initLocation(location, builder); name.copyTo(builder.initMember().initName()); return result; }), p::transform(tuple, [this](Located>>&& params) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); params.copyLocationTo(builder); builder.initApplication().adoptParams(kj::mv(params.value)); return result; })))), [](Orphan&& base, kj::Array>&& suffixes) -> Orphan { // Apply all the suffixes to the base expression. uint startByte = base.getReader().getStartByte(); for (auto& suffix: suffixes) { auto builder = suffix.get(); if (builder.isApplication()) { builder.getApplication().adoptFunction(kj::mv(base)); } else if (builder.isMember()) { builder.getMember().adoptParent(kj::mv(base)); } else { KJ_FAIL_ASSERT("Unknown suffix?", (uint)builder.which()); } builder.setStartByte(startByte); base = kj::mv(suffix); } return kj::mv(base); })); parsers.annotation = arena.copy(p::transform( p::sequence(op("$"), parsers.expression), [this](Orphan&& expression) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); auto exp = expression.get(); if (exp.isApplication()) { // Oops, this annotation specifies the value, but we parsed it as an application on // the preceding expression. Pull it back apart. auto app = exp.getApplication(); builder.adoptName(app.disownFunction()); auto params = app.getParams(); if (params.size() == 1 && params[0].isUnnamed()) { // Params has a single unnamed element, so reduce it to a simple value rather than // a tuple. builder.getValue().adoptExpression(params[0].disownValue()); } else { // Params is not a single unnamed element, so it's a tuple. builder.getValue().initExpression().adoptTuple(app.disownParams()); } } else { // The annotation has no value. builder.adoptName(kj::mv(expression)); builder.getValue().setNone(); } return result; })); parsers.uid = arena.copy(p::transform( p::sequence(op("@"), integerLiteral), [this](Located&& value) { if (value.value < (1ull << 63)) { errorReporter.addError(value.startByte, value.endByte, "Invalid ID. Please generate a new one with 'capnpc -i'."); } return value.asProto(orphanage); })); parsers.ordinal = arena.copy(p::transform( p::sequence(op("@"), integerLiteral), [this](Located&& value) { if (value.value >= 65536) { errorReporter.addError(value.startByte, value.endByte, "Ordinals cannot be greater than 65535."); } return value.asProto(orphanage); })); // ----------------------------------------------------------------- parsers.usingDecl = arena.copy(p::transform( p::sequence(keyword("using"), p::optional(p::sequence(identifier, op("="))), parsers.expression), [this](kj::Maybe>&& name, Orphan&& target) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto builder = decl.get(); KJ_IF_MAYBE(n, name) { n->copyTo(builder.initName()); } else { auto targetReader = target.getReader(); if (targetReader.isMember()) { builder.setName(targetReader.getMember().getName()); } else { errorReporter.addErrorOn(targetReader, "'using' declaration without '=' must specify a named declaration from a " "different scope."); } } // no id, no annotations for using decl builder.initUsing().adoptTarget(kj::mv(target)); return DeclParserResult(kj::mv(decl)); })); parsers.constDecl = arena.copy(p::transform( p::sequence(keyword("const"), identifier, p::optional(parsers.uid), op(":"), parsers.expression, op("="), parsers.expression, p::many(parsers.annotation)), [this](Located&& name, kj::Maybe>&& id, Orphan&& type, Orphan&& value, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto builder = initDecl(decl.get(), kj::mv(name), kj::mv(id), nullptr, kj::mv(annotations)).initConst(); builder.adoptType(kj::mv(type)); builder.adoptValue(kj::mv(value)); return DeclParserResult(kj::mv(decl)); })); parsers.enumDecl = arena.copy(p::transform( p::sequence(keyword("enum"), identifier, p::optional(parsers.uid), p::many(parsers.annotation)), [this](Located&& name, kj::Maybe>&& id, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); initDecl(decl.get(), kj::mv(name), kj::mv(id), nullptr, kj::mv(annotations)).setEnum(); return DeclParserResult(kj::mv(decl), parsers.enumLevelDecl); })); parsers.enumerantDecl = arena.copy(p::transform( p::sequence(identifier, parsers.ordinal, p::many(parsers.annotation)), [this](Located&& name, Orphan&& ordinal, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); initMemberDecl(decl.get(), kj::mv(name), kj::mv(ordinal), kj::mv(annotations)) .setEnumerant(); return DeclParserResult(kj::mv(decl)); })); parsers.structDecl = arena.copy(p::transform( p::sequence(keyword("struct"), identifier, p::optional(parsers.uid), p::optional(parenthesizedList(identifier, errorReporter)), p::many(parsers.annotation)), [this](Located&& name, kj::Maybe>&& id, kj::Maybe>>>>&& genericParameters, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); initDecl(decl.get(), kj::mv(name), kj::mv(id), kj::mv(genericParameters), kj::mv(annotations)).setStruct(); return DeclParserResult(kj::mv(decl), parsers.structLevelDecl); })); parsers.fieldDecl = arena.copy(p::transform( p::sequence(identifier, parsers.ordinal, op(":"), parsers.expression, p::optional(p::sequence(op("="), parsers.expression)), p::many(parsers.annotation)), [this](Located&& name, Orphan&& ordinal, Orphan&& type, kj::Maybe>&& defaultValue, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto builder = initMemberDecl(decl.get(), kj::mv(name), kj::mv(ordinal), kj::mv(annotations)) .initField(); builder.adoptType(kj::mv(type)); KJ_IF_MAYBE(val, defaultValue) { builder.getDefaultValue().adoptValue(kj::mv(*val)); } else { builder.getDefaultValue().setNone(); } return DeclParserResult(kj::mv(decl)); })); // Parse an ordinal followed by an optional colon, or no ordinal but require a colon. auto& ordinalOrColon = arena.copy(p::oneOf( p::transform(p::sequence(parsers.ordinal, p::optional(op("!")), p::optional(op(":"))), [](Orphan&& ordinal, kj::Maybe> exclamation, kj::Maybe> colon) -> kj::Tuple>, bool, bool> { return kj::tuple(kj::mv(ordinal), exclamation == nullptr, colon == nullptr); }), p::transform(op(":"), []() -> kj::Tuple>, bool, bool> { return kj::tuple(nullptr, false, false); }))); parsers.unionDecl = arena.copy(p::transform( // The first branch of this oneOf() matches named unions. The second branch matches unnamed // unions and generates dummy values for the parse results. p::oneOf( p::sequence( identifier, ordinalOrColon, keyword("union"), p::many(parsers.annotation)), p::transformWithLocation(p::sequence(keyword("union"), p::endOfInput), [](kj::parse::Span::Reader::Iterator> location) { return kj::tuple( Located("", location.begin()->getStartByte(), location.begin()->getEndByte()), kj::Maybe>(nullptr), false, false, kj::Array>(nullptr)); })), [this](Located&& name, kj::Maybe>&& ordinal, bool missingExclamation, bool missingColon, kj::Array>&& annotations) -> DeclParserResult { if (missingExclamation) { errorReporter.addErrorOn(KJ_ASSERT_NONNULL(ordinal).getReader(), "As of Cap'n Proto v0.3, it is no longer necessary to assign numbers to " "unions. However, removing the number will break binary compatibility. " "If this is an old protocol and you need to retain compatibility, please " "add an exclamation point after the number to indicate that it is really " "needed, e.g. `foo @1! :union {`. If this is a new protocol or compatibility " "doesn't matter, just remove the @n entirely. Sorry for the inconvenience, " "and thanks for being an early adopter! :)"); } if (missingColon) { errorReporter.addErrorOn(KJ_ASSERT_NONNULL(ordinal).getReader(), "As of Cap'n Proto v0.3, the 'union' keyword should be prefixed with a colon " "for named unions, e.g. `foo :union {`."); } auto decl = orphanage.newOrphan(); auto builder = decl.get(); name.copyTo(builder.initName()); KJ_IF_MAYBE(ord, ordinal) { builder.getId().adoptOrdinal(kj::mv(*ord)); } else { builder.getId().setUnspecified(); } auto list = builder.initAnnotations(annotations.size()); for (uint i = 0; i < annotations.size(); i++) { list.adoptWithCaveats(i, kj::mv(annotations[i])); } builder.setUnion(); return DeclParserResult(kj::mv(decl), parsers.structLevelDecl); })); parsers.groupDecl = arena.copy(p::transform( p::sequence(identifier, op(":"), keyword("group"), p::many(parsers.annotation)), [this](Located&& name, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto builder = decl.get(); name.copyTo(builder.getName()); builder.getId().setUnspecified(); auto list = builder.initAnnotations(annotations.size()); for (uint i = 0; i < annotations.size(); i++) { list.adoptWithCaveats(i, kj::mv(annotations[i])); } builder.setGroup(); return DeclParserResult(kj::mv(decl), parsers.structLevelDecl); })); parsers.interfaceDecl = arena.copy(p::transform( p::sequence(keyword("interface"), identifier, p::optional(parsers.uid), p::optional(parenthesizedList(identifier, errorReporter)), p::optional(p::sequence( keyword("extends"), parenthesizedList(parsers.expression, errorReporter))), p::many(parsers.annotation)), [this](Located&& name, kj::Maybe>&& id, kj::Maybe>>>>&& genericParameters, kj::Maybe>>>>&& superclasses, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto builder = initDecl( decl.get(), kj::mv(name), kj::mv(id), kj::mv(genericParameters), kj::mv(annotations)).initInterface(); KJ_IF_MAYBE(s, superclasses) { auto superclassesBuilder = builder.initSuperclasses(s->value.size()); for (uint i: kj::indices(s->value)) { KJ_IF_MAYBE(superclass, s->value[i]) { superclassesBuilder.adoptWithCaveats(i, kj::mv(*superclass)); } } } return DeclParserResult(kj::mv(decl), parsers.interfaceLevelDecl); })); parsers.param = arena.copy(p::transformWithLocation( p::sequence(identifier, op(":"), parsers.expression, p::optional(p::sequence(op("="), parsers.expression)), p::many(parsers.annotation)), [this](kj::parse::Span::Reader::Iterator> location, Located&& name, Orphan&& type, kj::Maybe>&& defaultValue, kj::Array>&& annotations) -> Orphan { auto result = orphanage.newOrphan(); auto builder = result.get(); initLocation(location, builder); name.copyTo(builder.initName()); builder.adoptType(kj::mv(type)); builder.adoptAnnotations(arrayToList(orphanage, kj::mv(annotations))); KJ_IF_MAYBE(val, defaultValue) { builder.getDefaultValue().adoptValue(kj::mv(*val)); } else { builder.getDefaultValue().setNone(); } return kj::mv(result); })); auto& paramList = arena.copy(p::oneOf( p::transform(parenthesizedList(parsers.param, errorReporter), [this](Located>>>&& params) -> Orphan { auto decl = orphanage.newOrphan(); auto builder = decl.get(); params.copyLocationTo(builder); auto listBuilder = builder.initNamedList(params.value.size()); for (uint i: kj::indices(params.value)) { KJ_IF_MAYBE(param, params.value[i]) { listBuilder.adoptWithCaveats(i, kj::mv(*param)); } } return decl; }), p::transform(locatedKeyword("stream"), [this](Located&& kw) -> Orphan { auto decl = orphanage.newOrphan(); auto builder = decl.get(); kw.copyLocationTo(builder); builder.setStream(); return decl; }), p::transform(parsers.expression, [this](Orphan&& name) -> Orphan { auto decl = orphanage.newOrphan(); auto builder = decl.get(); auto nameReader = name.getReader(); builder.setStartByte(nameReader.getStartByte()); builder.setEndByte(nameReader.getEndByte()); builder.adoptType(kj::mv(name)); return decl; }))); parsers.methodDecl = arena.copy(p::transform( p::sequence(identifier, parsers.ordinal, p::optional(bracketedList(identifier, errorReporter)), paramList, p::optional(p::sequence(op("->"), paramList)), p::many(parsers.annotation)), [this](Located&& name, Orphan&& ordinal, kj::Maybe>>>>&& genericParams, Orphan&& params, kj::Maybe>&& results, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto nodeBuilder = initMemberDecl( decl.get(), kj::mv(name), kj::mv(ordinal), kj::mv(annotations)); initGenericParams(nodeBuilder, kj::mv(genericParams)); auto builder = nodeBuilder.initMethod(); builder.adoptParams(kj::mv(params)); KJ_IF_MAYBE(r, results) { builder.getResults().adoptExplicit(kj::mv(*r)); } else { builder.getResults().setNone(); } return DeclParserResult(kj::mv(decl)); })); auto& annotationTarget = arena.copy(p::oneOf( identifier, p::transformWithLocation(op("*"), [](kj::parse::Span::Reader::Iterator> location) { // Hacky... return Located("*", location.begin()->getStartByte(), location.begin()->getEndByte()); }))); parsers.annotationDecl = arena.copy(p::transform( p::sequence(keyword("annotation"), identifier, p::optional(parsers.uid), parenthesizedList(annotationTarget, errorReporter), op(":"), parsers.expression, p::many(parsers.annotation)), [this](Located&& name, kj::Maybe>&& id, Located>>>&& targets, Orphan&& type, kj::Array>&& annotations) -> DeclParserResult { auto decl = orphanage.newOrphan(); auto builder = initDecl(decl.get(), kj::mv(name), kj::mv(id), nullptr, kj::mv(annotations)).initAnnotation(); builder.adoptType(kj::mv(type)); DynamicStruct::Builder dynamicBuilder = builder; for (auto& maybeTarget: targets.value) { KJ_IF_MAYBE(target, maybeTarget) { if (target->value == "*") { // Set all. if (targets.value.size() > 1) { errorReporter.addError(target->startByte, target->endByte, "Wildcard should not be specified together with other targets."); } for (auto field: dynamicBuilder.getSchema().getFields()) { if (field.getProto().getName().startsWith("targets")) { dynamicBuilder.set(field, true); } } } else { if (target->value.size() == 0 || target->value.size() >= 32 || target->value[0] < 'a' || target->value[0] > 'z') { errorReporter.addError(target->startByte, target->endByte, "Not a valid annotation target."); } else { char buffer[64]; strcpy(buffer, "targets"); strcat(buffer, target->value.cStr()); buffer[strlen("targets")] += 'A' - 'a'; KJ_IF_MAYBE(field, dynamicBuilder.getSchema().findFieldByName(buffer)) { if (dynamicBuilder.get(*field).as()) { errorReporter.addError(target->startByte, target->endByte, "Duplicate target specification."); } dynamicBuilder.set(*field, true); } else { errorReporter.addError(target->startByte, target->endByte, "Not a valid annotation target."); } } } } } return DeclParserResult(kj::mv(decl)); })); // ----------------------------------------------------------------- auto& nakedId = arena.copy(p::transform(parsers.uid, [this](Orphan&& value) -> DeclParserResult { auto decl = orphanage.newOrphan(); decl.get().adoptNakedId(kj::mv(value)); return DeclParserResult(kj::mv(decl)); })); auto& nakedAnnotation = arena.copy(p::transform(parsers.annotation, [this](Orphan&& value) -> DeclParserResult { auto decl = orphanage.newOrphan(); decl.get().adoptNakedAnnotation(kj::mv(value)); return DeclParserResult(kj::mv(decl)); })); // ----------------------------------------------------------------- parsers.genericDecl = arena.copy(p::oneOf( parsers.usingDecl, parsers.constDecl, parsers.annotationDecl, parsers.enumDecl, parsers.structDecl, parsers.interfaceDecl)); parsers.fileLevelDecl = arena.copy(p::oneOf( parsers.genericDecl, nakedId, nakedAnnotation)); parsers.enumLevelDecl = arena.copy(p::oneOf(parsers.enumerantDecl)); parsers.structLevelDecl = arena.copy(p::oneOf( parsers.unionDecl, parsers.fieldDecl, parsers.groupDecl, parsers.genericDecl)); parsers.interfaceLevelDecl = arena.copy(p::oneOf( parsers.methodDecl, parsers.genericDecl)); } CapnpParser::~CapnpParser() noexcept(false) {} kj::Maybe> CapnpParser::parseStatement( Statement::Reader statement, const DeclParser& parser) { auto fullParser = p::sequence(parser, p::endOfInput); auto tokens = statement.getTokens(); ParserInput parserInput(tokens.begin(), tokens.end()); KJ_IF_MAYBE(output, fullParser(parserInput)) { auto builder = output->decl.get(); if (statement.hasDocComment()) { builder.setDocComment(statement.getDocComment()); } builder.setStartByte(statement.getStartByte()); builder.setEndByte(statement.getEndByte()); switch (statement.which()) { case Statement::LINE: if (output->memberParser != nullptr) { errorReporter.addError(statement.getStartByte(), statement.getEndByte(), "This statement should end with a block, not a semicolon."); } break; case Statement::BLOCK: KJ_IF_MAYBE(memberParser, output->memberParser) { auto memberStatements = statement.getBlock(); kj::Vector> members(memberStatements.size()); for (auto memberStatement: memberStatements) { KJ_IF_MAYBE(member, parseStatement(memberStatement, *memberParser)) { members.add(kj::mv(*member)); } } builder.adoptNestedDecls(arrayToList(orphanage, members.releaseAsArray())); } else { errorReporter.addError(statement.getStartByte(), statement.getEndByte(), "This statement should end with a semicolon, not a block."); } break; } return kj::mv(output->decl); } else { // Parse error. Figure out where to report it. auto best = parserInput.getBest(); uint32_t bestByte; if (best != tokens.end()) { bestByte = best->getStartByte(); } else if (tokens.end() != tokens.begin()) { bestByte = (tokens.end() - 1)->getEndByte(); } else { bestByte = statement.getStartByte(); } errorReporter.addError(bestByte, bestByte, "Parse error."); return nullptr; } } } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/node-translator.h0000644000175000017500000003344713650101756023756 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include #include #include #include #include "error-reporter.h" #include CAPNP_BEGIN_HEADER namespace capnp { namespace compiler { class NodeTranslator { // Translates one node in the schema from AST form to final schema form. A "node" is anything // that has a unique ID, such as structs, enums, constants, and annotations, but not fields, // unions, enumerants, or methods (the latter set have 16-bit ordinals but not 64-bit global IDs). public: class Resolver { // Callback class used to find other nodes relative to this one. // // TODO(cleanup): This has evolved into being a full interface for traversing the node tree. // Maybe we should rename it as such, and move it out of NodeTranslator. See also // TODO(cleanup) on NodeTranslator::BrandedDecl. public: struct ResolvedDecl { uint64_t id; uint genericParamCount; uint64_t scopeId; Declaration::Which kind; Resolver* resolver; kj::Maybe brand; // If present, then it is necessary to replace the brand scope with the given brand before // using the target type. This happens when the decl resolved to an alias; all other fields // of `ResolvedDecl` refer to the target of the alias, except for `scopeId` which is the // scope that contained the alias. }; struct ResolvedParameter { uint64_t id; // ID of the node declaring the parameter. uint index; // Index of the parameter. }; typedef kj::OneOf ResolveResult; virtual kj::Maybe resolve(kj::StringPtr name) = 0; // Look up the given name, relative to this node, and return basic information about the // target. virtual kj::Maybe resolveMember(kj::StringPtr name) = 0; // Look up a member of this node. virtual ResolvedDecl resolveBuiltin(Declaration::Which which) = 0; virtual ResolvedDecl resolveId(uint64_t id) = 0; virtual kj::Maybe getParent() = 0; // Returns the parent of this scope, or null if this is the top scope. virtual ResolvedDecl getTopScope() = 0; // Get the top-level scope containing this node. virtual kj::Maybe resolveBootstrapSchema(uint64_t id, schema::Brand::Reader brand) = 0; // Get the schema for the given ID. If a schema is returned, it must be safe to traverse its // dependencies via the Schema API. A schema that is only at the bootstrap stage is // acceptable. // // Throws an exception if the id is not one that was found by calling resolve() or by // traversing other schemas. Returns null if the ID is recognized, but the corresponding // schema node failed to be built for reasons that were already reported. virtual kj::Maybe resolveFinalSchema(uint64_t id) = 0; // Get the final schema for the given ID. A bootstrap schema is not acceptable. A raw // node reader is returned rather than a Schema object because using a Schema object built // by the final schema loader could trigger lazy initialization of dependencies which could // lead to a cycle and deadlock. // // Throws an exception if the id is not one that was found by calling resolve() or by // traversing other schemas. Returns null if the ID is recognized, but the corresponding // schema node failed to be built for reasons that were already reported. virtual kj::Maybe resolveImport(kj::StringPtr name) = 0; // Get the ID of an imported file given the import path. virtual kj::Maybe> readEmbed(kj::StringPtr name) = 0; // Read and return the contents of a file for an `embed` expression. virtual kj::Maybe resolveBootstrapType(schema::Type::Reader type, Schema scope) = 0; // Compile a schema::Type into a Type whose dependencies may safely be traversed via the schema // API. These dependencies may have only bootstrap schemas. Returns null if the type could not // be constructed due to already-reported errors. }; NodeTranslator(Resolver& resolver, ErrorReporter& errorReporter, const Declaration::Reader& decl, Orphan wipNode, bool compileAnnotations); // Construct a NodeTranslator to translate the given declaration. The wipNode starts out with // `displayName`, `id`, `scopeId`, and `nestedNodes` already initialized. The `NodeTranslator` // fills in the rest. ~NodeTranslator() noexcept(false); struct NodeSet { schema::Node::Reader node; // The main node. kj::Array auxNodes; // Auxiliary nodes that were produced when translating this node and should be loaded along // with it. In particular, structs that contain groups (or named unions) spawn extra nodes // representing those, and interfaces spawn struct nodes representing method params/results. kj::Array sourceInfo; // The SourceInfo for the node and all aux nodes. }; NodeSet getBootstrapNode(); // Get an incomplete version of the node in which pointer-typed value expressions have not yet // been translated. Instead, for all `schema.Value` objects representing pointer-type values, // the value is set to an appropriate "empty" value. This version of the schema can be used to // bootstrap the dynamic API which can then in turn be used to encode the missing complex values. // // If the final node has already been built, this will actually return the final node (in fact, // it's the same node object). NodeSet finish(Schema selfUnboundBootstrap); // Finish translating the node (including filling in all the pieces that are missing from the // bootstrap node) and return it. // // `selfUnboundBootstrap` is a Schema build using the Node returned by getBootstrapNode(), and // with generic parameters "unbound", i.e. it was returned by SchemaLoader::getUnbound(). static kj::Maybe compileDecl( uint64_t scopeId, uint scopeParameterCount, Resolver& resolver, ErrorReporter& errorReporter, Expression::Reader expression, schema::Brand::Builder brandBuilder); // Compile a one-off declaration expression without building a NodeTranslator. Used for // evaluating aliases. // // `brandBuilder` may be used to construct a message which will fill in ResolvedDecl::brand in // the result. private: class DuplicateNameDetector; class DuplicateOrdinalDetector; class StructLayout; class StructTranslator; class BrandedDecl; class BrandScope; Resolver& resolver; ErrorReporter& errorReporter; Orphanage orphanage; bool compileAnnotations; kj::Own localBrand; Orphan wipNode; // The work-in-progress schema node. Orphan sourceInfo; // Doc comments and other source info for this node. struct AuxNode { Orphan node; Orphan sourceInfo; }; kj::Vector groups; // If this is a struct node and it contains groups, these are the nodes for those groups, which // must be loaded together with the top-level node. kj::Vector paramStructs; // If this is an interface, these are the auto-generated structs representing params and results. struct UnfinishedValue { Expression::Reader source; schema::Type::Reader type; kj::Maybe typeScope; schema::Value::Builder target; }; kj::Vector unfinishedValues; // List of values in `wipNode` which have not yet been interpreted, because they are structs // or lists and as such interpreting them require using the types' schemas (to take advantage // of the dynamic API). Once bootstrap schemas have been built, they can be used to interpret // these values. void compileNode(Declaration::Reader decl, schema::Node::Builder builder); void compileConst(Declaration::Const::Reader decl, schema::Node::Const::Builder builder); void compileAnnotation(Declaration::Annotation::Reader decl, schema::Node::Annotation::Builder builder); void compileEnum(Void decl, List::Reader members, schema::Node::Builder builder); void compileStruct(Void decl, List::Reader members, schema::Node::Builder builder); void compileInterface(Declaration::Interface::Reader decl, List::Reader members, schema::Node::Builder builder); // The `members` arrays contain only members with ordinal numbers, in code order. Other members // are handled elsewhere. struct ImplicitParams { // Represents a set of implicit parameters visible in the current context. uint64_t scopeId; // If zero, then any reference to an implciit param in this context should be compiled to a // `implicitMethodParam` AnyPointer. If non-zero, it should be compiled to a `parameter` // AnyPointer. List::Reader params; }; static inline ImplicitParams noImplicitParams() { return { 0, List::Reader() }; } template uint64_t compileParamList(kj::StringPtr methodName, uint16_t ordinal, bool isResults, Declaration::ParamList::Reader paramList, typename List::Reader implicitParams, InitBrandFunc&& initBrand); // Compile a param (or result) list and return the type ID of the struct type. kj::Maybe compileDeclExpression( Expression::Reader source, ImplicitParams implicitMethodParams); // Compile an expression which is expected to resolve to a declaration or type expression. bool compileType(Expression::Reader source, schema::Type::Builder target, ImplicitParams implicitMethodParams); // Returns false if there was a problem, in which case value expressions of this type should // not be parsed. void compileDefaultDefaultValue(schema::Type::Reader type, schema::Value::Builder target); // Initializes `target` to contain the "default default" value for `type`. void compileBootstrapValue( Expression::Reader source, schema::Type::Reader type, schema::Value::Builder target, kj::Maybe typeScope = nullptr); // Calls compileValue() if this value should be interpreted at bootstrap time. Otheriwse, // adds the value to `unfinishedValues` for later evaluation. // // If `type` comes from some other node, `typeScope` is the schema for that node. Otherwise the // scope of the type expression is assumed to be this node (meaning, in particular, that no // generic type parameters are bound). void compileValue(Expression::Reader source, schema::Type::Reader type, Schema typeScope, schema::Value::Builder target, bool isBootstrap); // Interprets the value expression and initializes `target` with the result. kj::Maybe readConstant(Expression::Reader name, bool isBootstrap); // Get the value of the given constant. May return null if some error occurs, which will already // have been reported. kj::Maybe> readEmbed(LocatedText::Reader filename); // Read a raw file for embedding. Orphan> compileAnnotationApplications( List::Reader annotations, kj::StringPtr targetsFlagName); }; class ValueTranslator { public: class Resolver { public: virtual kj::Maybe resolveConstant(Expression::Reader name) = 0; virtual kj::Maybe> readEmbed(LocatedText::Reader filename) = 0; }; ValueTranslator(Resolver& resolver, ErrorReporter& errorReporter, Orphanage orphanage) : resolver(resolver), errorReporter(errorReporter), orphanage(orphanage) {} kj::Maybe> compileValue(Expression::Reader src, Type type); void fillStructValue(DynamicStruct::Builder builder, List::Reader assignments); // Interprets the given assignments and uses them to fill in the given struct builder. private: Resolver& resolver; ErrorReporter& errorReporter; Orphanage orphanage; Orphan compileValueInner(Expression::Reader src, Type type); // Helper for compileValue(). kj::String makeNodeName(Schema node); kj::String makeTypeName(Type type); kj::Maybe makeListSchemaOf(schema::Type::Reader elementType); }; } // namespace compiler } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/compiler/node-translator.c++0000644000175000017500000036261713650101756024103 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "node-translator.h" #include "parser.h" // only for generateGroupId() #include #include #include #include #include #include #include #include namespace capnp { namespace compiler { bool shouldDetectIssue344() { return getenv("CAPNP_IGNORE_ISSUE_344") == nullptr; } class NodeTranslator::StructLayout { // Massive, disgusting class which implements the layout algorithm, which decides the offset // for each field. public: template struct HoleSet { inline HoleSet(): holes{0, 0, 0, 0, 0, 0} {} // 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. UIntType holes[6]; // 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 tryAllocate(UIntType lgSize) { // Try to find space for a field of size 2^lgSize within the set of holes. If found, // 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). if (lgSize >= kj::size(holes)) { return nullptr; } else if (holes[lgSize] != 0) { 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, UIntType limitLgSize = sizeof(HoleSet::holes) / sizeof(HoleSet::holes[0])) { // 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. KJ_DREQUIRE(limitLgSize <= kj::size(holes)); 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; } } kj::Maybe smallestAtLeast(uint size) { // Return the size of the smallest hole that is equal to or larger than the given size. for (uint i = size; i < kj::size(holes); i++) { if (holes[i] != 0) { return i; } } return nullptr; } 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. for (uint i = kj::size(holes); i > 0; i--) { if (holes[i - 1] != 1) { return i; } } return 0; } }; struct StructOrGroup { // Abstract interface for scopes in which fields can be added. virtual void addVoid() = 0; 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 holes; void addVoid() override {} 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; kj::Maybe discriminantOffset; kj::Vector dataLocations; kj::Vector 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()); } void newGroupAddingFirstMember() { if (++groupCount == 2) { addDiscriminant(); } } bool addDiscriminant() { if (discriminantOffset == nullptr) { discriminantOffset = parent.addData(4); // 2^4 = 16 bits return true; } else { return false; } } }; struct Group final: public StructOrGroup { public: class DataLocationUsage { public: DataLocationUsage(): isUsed(false) {} explicit DataLocationUsage(uint lgSize): isUsed(true), lgSizeUsed(lgSize) {} kj::Maybe 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, // to reduce fragmentation. Returns the size of the hole, if found. if (!isUsed) { // The location is effectively one big hole. if (lgSize <= location.lgSize) { return location.lgSize; } else { return nullptr; } } 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 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; return location.offset << (location.lgSize - lgSize); } else { return nullptr; } } else { uint newSize = kj::max(lgSizeUsed, lgSize) + 1; if (tryExpandUsage(group, location, newSize, true)) { uint result = KJ_ASSERT_NONNULL(holes.tryAllocate(lgSize)); uint locationOffset = location.offset << (location.lgSize - lgSize); return locationOffset + result; } 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. return tryExpandUsage(group, location, oldLgSize + expansionFactor, false); } 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 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. bool tryExpandUsage(Group& group, Union::DataLocation& location, uint desiredUsage, bool newHoles) { 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. if (newHoles) { holes.addHolesAtEnd(lgSizeUsed, 1, desiredUsage); } else if (shouldDetectIssue344()) { // 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-compatibility 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"); } lgSizeUsed = desiredUsage; return true; } }; Union& parent; kj::Vector 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. bool hasMembers = false; inline Group(Union& parent): parent(parent) {} KJ_DISALLOW_COPY(Group); void addMember() { if (!hasMembers) { hasMembers = true; parent.newGroupAddingFirstMember(); } } 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(); } uint addData(uint lgSize) override { addMember(); uint bestSize = kj::maxValue; kj::Maybe 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 { addMember(); if (parentPointerLocationUsage < parent.pointerLocations.size()) { return parent.pointerLocations[parentPointerLocationUsage++]; } else { parentPointerLocationUsage++; return parent.addNewPointerLocation(); } } bool tryExpandData(uint oldLgSize, uint oldOffset, uint expansionFactor) override { bool mustFail = false; 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. // 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. if (shouldDetectIssue344()) { mustFail = true; } else { return false; } } 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. 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; } } KJ_FAIL_ASSERT("Tried to expand field that was never allocated."); return false; } }; Top& getTop() { return top; } private: Top top; }; // ======================================================================================= class NodeTranslator::BrandedDecl { // Represents a declaration possibly with generic parameter bindings. // // TODO(cleaup): This is too complicated to live here. We should refactor this class and // BrandScope out into their own file, independent of NodeTranslator. public: inline BrandedDecl(Resolver::ResolvedDecl decl, kj::Own&& brand, Expression::Reader source) : brand(kj::mv(brand)), source(source) { body.init(kj::mv(decl)); } inline BrandedDecl(Resolver::ResolvedParameter variable, Expression::Reader source) : source(source) { body.init(kj::mv(variable)); } inline BrandedDecl(decltype(nullptr)) {} 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()); } BrandedDecl(BrandedDecl& other); BrandedDecl(BrandedDecl&& other) = default; BrandedDecl& operator=(BrandedDecl& other); BrandedDecl& operator=(BrandedDecl&& other) = default; // 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. kj::Maybe applyParams(kj::Array params, Expression::Reader subSource); // Treat the declaration as a generic and apply it to the given parameter list. kj::Maybe getMember(kj::StringPtr memberName, Expression::Reader subSource); // Get a member of this declaration. kj::Maybe getKind(); // Returns the kind of declaration, or null if this is an unbound generic variable. template uint64_t getIdAndFillBrand(InitBrandFunc&& initBrand); // 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. // // It is an error to call this when `getKind()` returns null. kj::Maybe getListParam(); // 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. bool compileAsType(ErrorReporter& errorReporter, schema::Type::Builder target); // Compile this decl to a schema::Type. inline void addError(ErrorReporter& errorReporter, kj::StringPtr message) { errorReporter.addErrorOn(source, message); } Resolver::ResolveResult asResolveResult(uint64_t scopeId, schema::Brand::Builder brandBuilder); // Reverse this into a ResolveResult. If necessary, use `brandBuilder` to fill in // ResolvedDecl.brand. kj::String toString(); kj::String toDebugString(); private: Resolver::ResolveResult body; kj::Own brand; // null if parameter Expression::Reader source; }; class NodeTranslator::BrandScope: public kj::Refcounted { // Tracks the brand parameter bindings affecting the current scope. For example, if we are // interpreting the type expression "Foo(Text).Bar", we would start with the current scopes // 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)". // // TODO(cleanup): This is too complicated to live here. We should refactor this class and // BrandedDecl out into their own file, independent of NodeTranslator. public: BrandScope(ErrorReporter& errorReporter, uint64_t startingScopeId, uint startingScopeParamCount, Resolver& startingScope) : errorReporter(errorReporter), parent(nullptr), leafId(startingScopeId), leafParamCount(startingScopeParamCount), inherited(true) { // Create all lexical parent scopes, all with no brand bindings. KJ_IF_MAYBE(p, startingScope.getParent()) { parent = kj::refcounted( errorReporter, p->id, p->genericParamCount, *p->resolver); } } bool isGeneric() { if (leafParamCount > 0) return true; KJ_IF_MAYBE(p, parent) { return p->get()->isGeneric(); } else { return false; } } kj::Own push(uint64_t typeId, uint paramCount) { return kj::refcounted(kj::addRef(*this), typeId, paramCount); } kj::Maybe> setParams( kj::Array params, Declaration::Which genericType, Expression::Reader source) { if (this->params.size() != 0) { errorReporter.addErrorOn(source, "Double-application of generic parameters."); return nullptr; } else if (params.size() > leafParamCount) { if (leafParamCount == 0) { errorReporter.addErrorOn(source, "Declaration does not accept generic parameters."); } else { errorReporter.addErrorOn(source, "Too many generic parameters."); } return nullptr; } else if (params.size() < leafParamCount) { errorReporter.addErrorOn(source, "Not enough generic parameters."); return nullptr; } else { 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; } } } } return kj::refcounted(*this, kj::mv(params)); } } kj::Own pop(uint64_t newLeafId) { if (leafId == newLeafId) { return kj::addRef(*this); } KJ_IF_MAYBE(p, parent) { return (*p)->pop(newLeafId); } else { // Looks like we're moving into a whole top-level scope. return kj::refcounted(errorReporter, newLeafId); } } kj::Maybe lookupParameter(Resolver& resolver, uint64_t scopeId, uint index) { // Returns null if the param should be inherited from the client scope. if (scopeId == leafId) { if (index < params.size()) { return params[index]; } else if (inherited) { return nullptr; } else { // Unbound and not inherited, so return AnyPointer. auto decl = resolver.resolveBuiltin(Declaration::BUILTIN_ANY_POINTER); return BrandedDecl(decl, evaluateBrand(resolver, decl, List::Reader()), Expression::Reader()); } } else KJ_IF_MAYBE(p, parent) { return p->get()->lookupParameter(resolver, scopeId, index); } else { KJ_FAIL_REQUIRE("scope is not a parent"); } } kj::Maybe> getParams(uint64_t scopeId) { // Returns null if params at the requested scope should be inherited from the client scope. if (scopeId == leafId) { if (inherited) { return nullptr; } else { return params.asPtr(); } } else KJ_IF_MAYBE(p, parent) { return p->get()->getParams(scopeId); } else { KJ_FAIL_REQUIRE("scope is not a parent"); } } template void compile(InitBrandFunc&& initBrand) { kj::Vector levels; BrandScope* ptr = this; for (;;) { if (ptr->params.size() > 0 || (ptr->inherited && ptr->leafParamCount > 0)) { levels.add(ptr); } KJ_IF_MAYBE(p, ptr->parent) { ptr = *p; } else { break; } } if (levels.size() > 0) { auto scopes = initBrand().initScopes(levels.size()); for (uint i: kj::indices(levels)) { auto scope = scopes[i]; scope.setScopeId(levels[i]->leafId); if (levels[i]->inherited) { scope.setInherit(); } else { auto bindings = scope.initBind(levels[i]->params.size()); for (uint j: kj::indices(bindings)) { levels[i]->params[j].compileAsType(errorReporter, bindings[j].initType()); } } } } } kj::Maybe compileDeclExpression( Expression::Reader source, Resolver& resolver, ImplicitParams implicitMethodParams); NodeTranslator::BrandedDecl interpretResolve( Resolver& resolver, Resolver::ResolveResult& result, Expression::Reader source); kj::Own evaluateBrand( Resolver& resolver, Resolver::ResolvedDecl decl, List::Reader brand, uint index = 0); BrandedDecl decompileType(Resolver& resolver, schema::Type::Reader type); inline uint64_t getScopeId() { return leafId; } private: ErrorReporter& errorReporter; kj::Maybe> parent; uint64_t leafId; // zero = this is the root uint leafParamCount; // number of generic parameters on this leaf bool inherited; kj::Array params; BrandScope(kj::Own parent, uint64_t leafId, uint leafParamCount) : errorReporter(parent->errorReporter), parent(kj::mv(parent)), leafId(leafId), leafParamCount(leafParamCount), inherited(false) {} BrandScope(BrandScope& base, kj::Array params) : errorReporter(base.errorReporter), leafId(base.leafId), leafParamCount(base.leafParamCount), inherited(false), params(kj::mv(params)) { KJ_IF_MAYBE(p, base.parent) { parent = kj::addRef(**p); } } BrandScope(ErrorReporter& errorReporter, uint64_t scopeId) : errorReporter(errorReporter), leafId(scopeId), leafParamCount(0), inherited(false) {} template friend kj::Own kj::refcounted(Params&&... params); }; NodeTranslator::BrandedDecl::BrandedDecl(BrandedDecl& other) : body(other.body), source(other.source) { if (body.is()) { brand = kj::addRef(*other.brand); } } NodeTranslator::BrandedDecl& NodeTranslator::BrandedDecl::operator=(BrandedDecl& other) { body = other.body; source = other.source; if (body.is()) { brand = kj::addRef(*other.brand); } return *this; } kj::Maybe NodeTranslator::BrandedDecl::applyParams( kj::Array params, Expression::Reader subSource) { if (body.is()) { return nullptr; } else { return brand->setParams(kj::mv(params), body.get().kind, subSource) .map([&](kj::Own&& scope) { BrandedDecl result = *this; result.brand = kj::mv(scope); result.source = subSource; return result; }); } } kj::Maybe NodeTranslator::BrandedDecl::getMember( kj::StringPtr memberName, Expression::Reader subSource) { if (body.is()) { return nullptr; } else KJ_IF_MAYBE(r, body.get().resolver->resolveMember(memberName)) { return brand->interpretResolve(*body.get().resolver, *r, subSource); } else { return nullptr; } } kj::Maybe NodeTranslator::BrandedDecl::getKind() { if (body.is()) { return nullptr; } else { return body.get().kind; } } template uint64_t NodeTranslator::BrandedDecl::getIdAndFillBrand(InitBrandFunc&& initBrand) { KJ_REQUIRE(body.is()); brand->compile(kj::fwd(initBrand)); return body.get().id; } kj::Maybe NodeTranslator::BrandedDecl::getListParam() { KJ_REQUIRE(body.is()); auto& decl = body.get(); KJ_REQUIRE(decl.kind == Declaration::BUILTIN_LIST); auto params = KJ_ASSERT_NONNULL(brand->getParams(decl.id)); if (params.size() != 1) { return nullptr; } else { return params[0]; } } NodeTranslator::Resolver::ResolvedParameter NodeTranslator::BrandedDecl::asVariable() { KJ_REQUIRE(body.is()); return body.get(); } 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(); KJ_IF_MAYBE(param, getListParam()) { if (!param->compileAsType(errorReporter, elementType)) { return false; } } else { addError(errorReporter, "'List' requires exactly one parameter."); 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. :)"); // fallthrough case Declaration::BUILTIN_ANY_POINTER: 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(); return true; 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(); 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; } } } NodeTranslator::Resolver::ResolveResult NodeTranslator::BrandedDecl::asResolveResult( uint64_t scopeId, schema::Brand::Builder brandBuilder) { auto result = body; if (result.is()) { // May need to compile our context as the "brand". result.get().scopeId = scopeId; getIdAndFillBrand([&]() { result.get().brand = brandBuilder.asReader(); return brandBuilder; }); } return result; } static kj::String expressionString(Expression::Reader name); // defined later kj::String NodeTranslator::BrandedDecl::toString() { return expressionString(source); } kj::String NodeTranslator::BrandedDecl::toDebugString() { if (body.is()) { auto variable = body.get(); return kj::str("variable(", variable.id, ", ", variable.index, ")"); } else { auto decl = body.get(); 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()) { auto& decl = result.get(); 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(); KJ_IF_MAYBE(p, lookupParameter(resolver, param.id, param.index)) { return *p; } else { return BrandedDecl(param, source); } } } kj::Own NodeTranslator::BrandScope::evaluateBrand( Resolver& resolver, Resolver::ResolvedDecl decl, List::Reader brand, uint index) { auto result = kj::refcounted(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(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(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::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()); } } case schema::Type::AnyPointer::IMPLICIT_METHOD_PARAMETER: KJ_FAIL_ASSERT("Alias pointed to implicit method type parameter?"); } KJ_UNREACHABLE; } } KJ_UNREACHABLE; } kj::Maybe NodeTranslator::BrandScope::compileDeclExpression( Expression::Reader source, Resolver& resolver, ImplicitParams implicitMethodParams) { 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: case Expression::EMBED: errorReporter.addErrorOn(source, "Expected name."); return nullptr; case Expression::RELATIVE_NAME: { auto name = source.getRelativeName(); 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(i) }, Expression::Reader()); } } } KJ_IF_MAYBE(r, resolver.resolve(nameValue)) { return interpretResolve(resolver, *r, source); } else { errorReporter.addErrorOn(name, kj::str("Not defined: ", nameValue)); 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())) { // Import is always a root scope, so create a fresh BrandScope. return BrandedDecl(*decl, kj::refcounted( 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(); KJ_IF_MAYBE(decl, compileDeclExpression(app.getFunction(), resolver, implicitMethodParams)) { // Compile all params. auto params = app.getParams(); auto compiledParams = kj::heapArrayBuilder(params.size()); bool paramFailed = false; for (auto param: params) { if (param.isNamed()) { errorReporter.addErrorOn(param.getNamed(), "Named parameter not allowed here."); } KJ_IF_MAYBE(d, compileDeclExpression(param.getValue(), resolver, implicitMethodParams)) { 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(); KJ_IF_MAYBE(decl, compileDeclExpression(member.getParent(), resolver, implicitMethodParams)) { auto name = member.getName(); KJ_IF_MAYBE(memberDecl, decl->getMember(name.getValue(), source)) { return kj::mv(*memberDecl); } else { errorReporter.addErrorOn(name, kj::str( "'", expressionString(member.getParent()), "' has no member named '", name.getValue(), "'")); return nullptr; } } else { // error already reported return nullptr; } } } KJ_UNREACHABLE; } // ======================================================================================= NodeTranslator::NodeTranslator( Resolver& resolver, ErrorReporter& errorReporter, const Declaration::Reader& decl, Orphan wipNodeParam, bool compileAnnotations) : resolver(resolver), errorReporter(errorReporter), orphanage(Orphanage::getForMessageContaining(wipNodeParam.get())), compileAnnotations(compileAnnotations), localBrand(kj::refcounted( errorReporter, wipNodeParam.getReader().getId(), decl.getParameters().size(), resolver)), wipNode(kj::mv(wipNodeParam)), sourceInfo(orphanage.newOrphan()) { compileNode(decl, wipNode.get()); } NodeTranslator::~NodeTranslator() noexcept(false) {} NodeTranslator::NodeSet NodeTranslator::getBootstrapNode() { auto sourceInfos = kj::heapArrayBuilder( 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()); } auto nodeReader = wipNode.getReader(); if (nodeReader.isInterface()) { return NodeSet { nodeReader, KJ_MAP(g, paramStructs) { return g.node.getReader(); }, sourceInfos.finish() }; } else { return NodeSet { nodeReader, KJ_MAP(g, groups) { return g.node.getReader(); }, sourceInfos.finish() }; } } NodeTranslator::NodeSet NodeTranslator::finish(Schema selfBootstrapSchema) { // 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]; compileValue(value.source, value.type, value.typeScope.orDefault(selfBootstrapSchema), value.target, false); } return getBootstrapNode(); } class NodeTranslator::DuplicateNameDetector { public: inline explicit DuplicateNameDetector(ErrorReporter& errorReporter) : errorReporter(errorReporter) {} void check(List::Reader nestedDecls, Declaration::Which parentKind); private: ErrorReporter& errorReporter; std::map names; }; void NodeTranslator::compileNode(Declaration::Reader decl, schema::Node::Builder builder) { DuplicateNameDetector(errorReporter) .check(decl.getNestedDecls(), decl.which()); 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()); } } builder.setIsGeneric(localBrand->isGeneric()); kj::StringPtr targetsFlagName; switch (decl.which()) { case Declaration::FILE: targetsFlagName = "targetsFile"; break; case Declaration::CONST: compileConst(decl.getConst(), builder.initConst()); targetsFlagName = "targetsConst"; break; case Declaration::ANNOTATION: compileAnnotation(decl.getAnnotation(), builder.initAnnotation()); targetsFlagName = "targetsAnnotation"; break; case Declaration::ENUM: compileEnum(decl.getEnum(), decl.getNestedDecls(), builder); targetsFlagName = "targetsEnum"; break; case Declaration::STRUCT: compileStruct(decl.getStruct(), decl.getNestedDecls(), builder); targetsFlagName = "targetsStruct"; break; case Declaration::INTERFACE: compileInterface(decl.getInterface(), decl.getNestedDecls(), builder); targetsFlagName = "targetsInterface"; break; default: KJ_FAIL_REQUIRE("This Declaration is not a node."); break; } builder.adoptAnnotations(compileAnnotationApplications(decl.getAnnotations(), targetsFlagName)); auto di = sourceInfo.get(); di.setId(wipNode.getReader().getId()); if (decl.hasDocComment()) { di.setDocComment(decl.getDocComment()); } } 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; } } void NodeTranslator::DuplicateNameDetector::check( List::Reader nestedDecls, Declaration::Which parentKind) { for (auto decl: nestedDecls) { { auto name = decl.getName(); auto nameText = name.getValue(); auto insertResult = names.insert(std::make_pair(nameText, name)); if (!insertResult.second) { if (nameText.size() == 0 && decl.isUnion()) { 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.")); } } switch (decl.which()) { 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; } 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.)"); } } switch (decl.which()) { case Declaration::USING: case Declaration::CONST: case Declaration::ENUM: case Declaration::STRUCT: case Declaration::INTERFACE: case Declaration::ANNOTATION: switch (parentKind) { case Declaration::FILE: case Declaration::STRUCT: case Declaration::INTERFACE: // OK. break; default: errorReporter.addErrorOn(decl, "This kind of declaration doesn't belong here."); break; } break; case Declaration::ENUMERANT: if (parentKind != Declaration::ENUM) { errorReporter.addErrorOn(decl, "Enumerants can only appear in enums."); } break; case Declaration::METHOD: if (parentKind != Declaration::INTERFACE) { errorReporter.addErrorOn(decl, "Methods can only appear in interfaces."); } break; case Declaration::FIELD: case Declaration::UNION: case Declaration::GROUP: switch (parentKind) { case Declaration::STRUCT: case Declaration::UNION: case Declaration::GROUP: // OK. break; default: errorReporter.addErrorOn(decl, "This declaration can only appear in structs."); break; } // 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. check(decl.getNestedDecls(), decl.which()); } else { // Children are in their own scope. DuplicateNameDetector(errorReporter) .check(decl.getNestedDecls(), decl.which()); } break; default: errorReporter.addErrorOn(decl, "This kind of declaration doesn't belong here."); break; } } } void NodeTranslator::compileConst(Declaration::Const::Reader decl, schema::Node::Const::Builder builder) { auto typeBuilder = builder.initType(); if (compileType(decl.getType(), typeBuilder, noImplicitParams())) { compileBootstrapValue(decl.getValue(), typeBuilder.asReader(), builder.initValue()); } } void NodeTranslator::compileAnnotation(Declaration::Annotation::Reader decl, schema::Node::Annotation::Builder builder) { compileType(decl.getType(), builder.initType(), noImplicitParams()); // Dynamically copy over the values of all of the "targets" members. DynamicStruct::Reader src = decl; DynamicStruct::Builder dst = builder; 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)); } } } class NodeTranslator::DuplicateOrdinalDetector { public: DuplicateOrdinalDetector(ErrorReporter& errorReporter): errorReporter(errorReporter) {} 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.")); expectedOrdinal = ordinal.getValue() + 1; } else { ++expectedOrdinal; lastOrdinalLocation = ordinal; } } private: ErrorReporter& errorReporter; uint expectedOrdinal = 0; kj::Maybe lastOrdinalLocation; }; void NodeTranslator::compileEnum(Void decl, List::Reader members, schema::Node::Builder builder) { // maps ordinal -> (code order, declaration) std::multimap> enumerants; uint codeOrder = 0; for (auto member: members) { if (member.isEnumerant()) { enumerants.insert( std::make_pair(member.getId().getOrdinal().getValue(), std::make_pair(codeOrder++, member))); } } auto list = builder.initEnum().initEnumerants(enumerants.size()); auto sourceInfoList = sourceInfo.get().initMembers(enumerants.size()); 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()); if (enumerantDecl.hasDocComment()) { sourceInfoList[i].setDocComment(enumerantDecl.getDocComment()); } auto enumerantBuilder = list[i++]; enumerantBuilder.setName(enumerantDecl.getName().getValue()); enumerantBuilder.setCodeOrder(codeOrder); enumerantBuilder.adoptAnnotations(compileAnnotationApplications( enumerantDecl.getAnnotations(), "targetsEnumerant")); } } // ------------------------------------------------------------------- class NodeTranslator::StructTranslator { public: explicit StructTranslator(NodeTranslator& translator, ImplicitParams implicitMethodParams) : translator(translator), errorReporter(translator.errorReporter), implicitMethodParams(implicitMethodParams) {} KJ_DISALLOW_COPY(StructTranslator); void translate(Void decl, List::Reader members, schema::Node::Builder builder, schema::Node::SourceInfo::Builder sourceInfo) { // Build the member-info-by-ordinal map. MemberInfo root(builder, sourceInfo); traverseTopOrGroup(members, root, layout.getTop()); translateInternal(root, builder); } void translate(List::Reader params, schema::Node::Builder builder, schema::Node::SourceInfo::Builder sourceInfo) { // Build a struct from a method param / result list. MemberInfo root(builder, sourceInfo); traverseParams(params, root, layout.getTop()); translateInternal(root, builder); } private: NodeTranslator& translator; ErrorReporter& errorReporter; ImplicitParams implicitMethodParams; StructLayout layout; kj::Arena arena; struct NodeSourceInfoBuilderPair { schema::Node::Builder node; schema::Node::SourceInfo::Builder sourceInfo; }; struct FieldSourceInfoBuilderPair { schema::Field::Builder field; schema::Node::SourceInfo::Member::Builder sourceInfo; }; struct MemberInfo { MemberInfo* parent; // The MemberInfo for the parent scope. uint codeOrder; // Code order within the parent. uint index = 0; // Index within the parent. uint childCount = 0; // Number of children this member has. uint childInitializedCount = 0; // Number of children whose `schema` member has been initialized. This initialization happens // while walking the fields in ordinal order. 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. kj::StringPtr name; Declaration::Id::Reader declId; Declaration::Which declKind; bool isParam = false; bool hasDefaultValue = false; // if declKind == FIELD Expression::Reader fieldType; // if declKind == FIELD Expression::Reader fieldDefaultValue; // if declKind == FIELD && hasDefaultValue List::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. kj::Maybe docComment = nullptr; kj::Maybe schema; // Schema for the field. Initialized when getSchema() is first called. schema::Node::Builder node; schema::Node::SourceInfo::Builder sourceInfo; // If it's a group, or the top-level struct. 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; // 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. }; inline explicit MemberInfo(schema::Node::Builder node, schema::Node::SourceInfo::Builder sourceInfo) : parent(nullptr), codeOrder(0), isInUnion(false), node(node), sourceInfo(sourceInfo), unionScope(nullptr) {} inline MemberInfo(MemberInfo& parent, uint codeOrder, const Declaration::Reader& decl, StructLayout::StructOrGroup& fieldScope, bool isInUnion) : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion), name(decl.getName().getValue()), declId(decl.getId()), declKind(Declaration::FIELD), declAnnotations(decl.getAnnotations()), startByte(decl.getStartByte()), endByte(decl.getEndByte()), node(nullptr), sourceInfo(nullptr), fieldScope(&fieldScope) { KJ_REQUIRE(decl.which() == Declaration::FIELD); auto fieldDecl = decl.getField(); fieldType = fieldDecl.getType(); if (fieldDecl.getDefaultValue().isValue()) { hasDefaultValue = true; fieldDefaultValue = fieldDecl.getDefaultValue().getValue(); } if (decl.hasDocComment()) { docComment = decl.getDocComment(); } } inline MemberInfo(MemberInfo& parent, uint codeOrder, const Declaration::Param::Reader& decl, StructLayout::StructOrGroup& fieldScope, bool isInUnion) : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion), name(decl.getName().getValue()), declKind(Declaration::FIELD), isParam(true), declAnnotations(decl.getAnnotations()), startByte(decl.getStartByte()), endByte(decl.getEndByte()), node(nullptr), sourceInfo(nullptr), fieldScope(&fieldScope) { fieldType = decl.getType(); if (decl.getDefaultValue().isValue()) { hasDefaultValue = true; fieldDefaultValue = decl.getDefaultValue().getValue(); } } inline MemberInfo(MemberInfo& parent, uint codeOrder, const Declaration::Reader& decl, NodeSourceInfoBuilderPair builderPair, bool isInUnion) : parent(&parent), codeOrder(codeOrder), isInUnion(isInUnion), name(decl.getName().getValue()), declId(decl.getId()), declKind(decl.which()), declAnnotations(decl.getAnnotations()), startByte(decl.getStartByte()), endByte(decl.getEndByte()), node(builderPair.node), sourceInfo(builderPair.sourceInfo), unionScope(nullptr) { KJ_REQUIRE(decl.which() != Declaration::FIELD); if (decl.hasDocComment()) { docComment = decl.getDocComment(); } } schema::Field::Builder getSchema() { KJ_IF_MAYBE(result, schema) { return *result; } else { index = parent->childInitializedCount; auto builderPair = parent->addMemberSchema(); auto builder = builderPair.field; if (isInUnion) { builder.setDiscriminantValue(parent->unionDiscriminantCount++); } builder.setName(name); builder.setCodeOrder(codeOrder); KJ_IF_MAYBE(dc, docComment) { builderPair.sourceInfo.setDocComment(*dc); } schema = builder; return builder; } } FieldSourceInfoBuilderPair addMemberSchema() { // Get the schema builder for the child member at the given index. This lazily/dynamically // builds the builder tree. KJ_REQUIRE(childInitializedCount < childCount); auto structNode = node.getStruct(); if (!structNode.hasFields()) { if (parent != nullptr) { getSchema(); // Make sure field exists in parent once the first child is added. } FieldSourceInfoBuilderPair result { structNode.initFields(childCount)[childInitializedCount], sourceInfo.initMembers(childCount)[childInitializedCount] }; ++childInitializedCount; return result; } else { FieldSourceInfoBuilderPair result { structNode.getFields()[childInitializedCount], sourceInfo.getMembers()[childInitializedCount] }; ++childInitializedCount; return result; } } void finishGroup() { 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)); } if (parent != nullptr) { uint64_t groupId = generateGroupId(parent->node.getId(), index); node.setId(groupId); node.setScopeId(parent->node.getId()); getSchema().initGroup().setTypeId(groupId); sourceInfo.setId(groupId); KJ_IF_MAYBE(dc, docComment) { sourceInfo.setDocComment(*dc); } } } }; std::multimap membersByOrdinal; // 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). kj::Vector allMembers; // All members, including ones that don't have ordinals. void traverseUnion(const Declaration::Reader& decl, List::Reader members, MemberInfo& parent, StructLayout::Union& layout, uint& codeOrder) { if (members.size() < 2) { errorReporter.addErrorOn(decl, "Union must have at least two members."); } for (auto member: members) { kj::Maybe ordinal; MemberInfo* memberInfo = nullptr; switch (member.which()) { case Declaration::FIELD: { parent.childCount++; // For layout purposes, pretend this field is enclosed in a one-member group. StructLayout::Group& singletonGroup = arena.allocate(layout); memberInfo = &arena.allocate(parent, codeOrder++, member, singletonGroup, true); allMembers.add(memberInfo); ordinal = member.getId().getOrdinal().getValue(); break; } case Declaration::UNION: 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(layout); StructLayout::Union& unionLayout = arena.allocate(singletonGroup); memberInfo = &arena.allocate( parent, codeOrder++, member, newGroupNode(parent.node, member.getName().getValue()), true); allMembers.add(memberInfo); memberInfo->unionScope = &unionLayout; uint subCodeOrder = 0; traverseUnion(member, member.getNestedDecls(), *memberInfo, unionLayout, subCodeOrder); if (member.getId().isOrdinal()) { ordinal = member.getId().getOrdinal().getValue(); } } break; case Declaration::GROUP: { parent.childCount++; StructLayout::Group& group = arena.allocate(layout); memberInfo = &arena.allocate( parent, codeOrder++, member, newGroupNode(parent.node, member.getName().getValue()), true); allMembers.add(memberInfo); traverseGroup(member.getNestedDecls(), *memberInfo, group); break; } default: // Ignore others. break; } KJ_IF_MAYBE(o, ordinal) { membersByOrdinal.insert(std::make_pair(*o, memberInfo)); } } } void traverseGroup(List::Reader members, MemberInfo& parent, StructLayout::StructOrGroup& layout) { if (members.size() < 1) { errorReporter.addError(parent.startByte, parent.endByte, "Group must have at least one member."); } traverseTopOrGroup(members, parent, layout); } void traverseTopOrGroup(List::Reader members, MemberInfo& parent, StructLayout::StructOrGroup& layout) { uint codeOrder = 0; for (auto member: members) { kj::Maybe ordinal; MemberInfo* memberInfo = nullptr; switch (member.which()) { case Declaration::FIELD: { parent.childCount++; memberInfo = &arena.allocate( parent, codeOrder++, member, layout, false); allMembers.add(memberInfo); ordinal = member.getId().getOrdinal().getValue(); break; } case Declaration::UNION: { StructLayout::Union& unionLayout = arena.allocate(layout); uint independentSubCodeOrder = 0; uint* subCodeOrder = &independentSubCodeOrder; if (member.getName().getValue() == "") { memberInfo = &parent; subCodeOrder = &codeOrder; } else { parent.childCount++; memberInfo = &arena.allocate( parent, codeOrder++, member, newGroupNode(parent.node, member.getName().getValue()), false); allMembers.add(memberInfo); } memberInfo->unionScope = &unionLayout; traverseUnion(member, member.getNestedDecls(), *memberInfo, unionLayout, *subCodeOrder); if (member.getId().isOrdinal()) { ordinal = member.getId().getOrdinal().getValue(); } break; } case Declaration::GROUP: parent.childCount++; memberInfo = &arena.allocate( 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. break; default: // Ignore others. break; } KJ_IF_MAYBE(o, ordinal) { membersByOrdinal.insert(std::make_pair(*o, memberInfo)); } } } void traverseParams(List::Reader params, MemberInfo& parent, StructLayout::StructOrGroup& layout) { for (uint i: kj::indices(params)) { auto param = params[i]; parent.childCount++; MemberInfo* memberInfo = &arena.allocate(parent, i, param, layout, false); allMembers.add(memberInfo); membersByOrdinal.insert(std::make_pair(i, memberInfo)); } } NodeSourceInfoBuilderPair newGroupNode(schema::Node::Reader parent, kj::StringPtr name) { AuxNode aux { translator.orphanage.newOrphan(), translator.orphanage.newOrphan() }; auto node = aux.node.get(); auto sourceInfo = aux.sourceInfo.get(); // We'll set the ID and scope ID later. node.setDisplayName(kj::str(parent.getDisplayName(), '.', name)); node.setDisplayNamePrefixLength(node.getDisplayName().size() - name.size()); node.setIsGeneric(parent.getIsGeneric()); node.initStruct().setIsGroup(true); // The remaining contents of node.struct will be filled in later. translator.groups.add(kj::mv(aux)); return { node, sourceInfo }; } 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; // Make sure the exceptions added relating to // https://github.com/sandstorm-io/capnproto/issues/344 identify the affected field. KJ_CONTEXT(member.name); 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(); if (translator.compileType(member.fieldType, typeBuilder, implicitMethodParams)) { if (member.hasDefaultValue) { 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()); } slot.setHadExplicitDefault(true); } 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; case schema::Type::ANY_POINTER: lgSize = -2; break; } 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; } } member->getSchema().adoptAnnotations(translator.compileAnnotationApplications( member->declAnnotations, targetsFlagName)); } // And fill in the sizes. structBuilder.setDataWordCount(layout.getTop().dataWordCount); structBuilder.setPointerCount(layout.getTop().pointerCount); structBuilder.setPreferredListEncoding(schema::ElementSize::INLINE_COMPOSITE); for (auto& group: translator.groups) { auto groupBuilder = group.node.get().getStruct(); groupBuilder.setDataWordCount(structBuilder.getDataWordCount()); groupBuilder.setPointerCount(structBuilder.getPointerCount()); groupBuilder.setPreferredListEncoding(structBuilder.getPreferredListEncoding()); } } }; void NodeTranslator::compileStruct(Void decl, List::Reader members, schema::Node::Builder builder) { StructTranslator(*this, noImplicitParams()).translate(decl, members, builder, sourceInfo.get()); } // ------------------------------------------------------------------- static kj::String expressionString(Expression::Reader name); void NodeTranslator::compileInterface(Declaration::Interface::Reader decl, List::Reader members, schema::Node::Builder builder) { auto interfaceBuilder = builder.initInterface(); auto superclassesDecl = decl.getSuperclasses(); auto superclassesBuilder = interfaceBuilder.initSuperclasses(superclassesDecl.size()); for (uint i: kj::indices(superclassesDecl)) { auto superclass = superclassesDecl[i]; KJ_IF_MAYBE(decl, compileDeclExpression(superclass, noImplicitParams())) { KJ_IF_MAYBE(kind, decl->getKind()) { if (*kind == Declaration::INTERFACE) { auto s = superclassesBuilder[i]; s.setId(decl->getIdAndFillBrand([&]() { return s.initBrand(); })); } else { decl->addError(errorReporter, kj::str( "'", decl->toString(), "' is not an interface.")); } } else { // A variable? decl->addError(errorReporter, kj::str( "'", decl->toString(), "' is an unbound generic parameter. Currently we don't support " "extending these.")); } } } // maps ordinal -> (code order, declaration) std::multimap> 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()); auto sourceInfoList = sourceInfo.get().initMembers(methods.size()); uint i = 0; DuplicateOrdinalDetector dupDetector(errorReporter); for (auto& entry: methods) { uint codeOrder = entry.second.first; Declaration::Reader methodDecl = entry.second.second; auto methodReader = methodDecl.getMethod(); auto ordinalDecl = methodDecl.getId().getOrdinal(); dupDetector.check(ordinalDecl); uint16_t ordinal = ordinalDecl.getValue(); if (methodDecl.hasDocComment()) { sourceInfoList[i].setDocComment(methodDecl.getDocComment()); } auto methodBuilder = list[i++]; methodBuilder.setName(methodDecl.getName().getValue()); methodBuilder.setCodeOrder(codeOrder); auto implicits = methodDecl.getParameters(); auto implicitsBuilder = methodBuilder.initImplicitParameters(implicits.size()); for (auto i: kj::indices(implicits)) { implicitsBuilder[i].setName(implicits[i].getName()); } auto params = methodReader.getParams(); if (params.isStream()) { errorReporter.addErrorOn(params, "'stream' can only appear after '->', not before."); } methodBuilder.setParamStructType(compileParamList( methodDecl.getName().getValue(), ordinal, false, params, implicits, [&]() { return methodBuilder.initParamBrand(); })); auto results = methodReader.getResults(); Declaration::ParamList::Reader resultList; if (results.isExplicit()) { resultList = results.getExplicit(); } else { // 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. } methodBuilder.setResultStructType(compileParamList( methodDecl.getName().getValue(), ordinal, true, resultList, implicits, [&]() { return methodBuilder.initResultBrand(); })); methodBuilder.adoptAnnotations(compileAnnotationApplications( methodDecl.getAnnotations(), "targetsMethod")); } } template uint64_t NodeTranslator::compileParamList( kj::StringPtr methodName, uint16_t ordinal, bool isResults, Declaration::ParamList::Reader paramList, typename List::Reader implicitParams, InitBrandFunc&& initBrand) { switch (paramList.which()) { case Declaration::ParamList::NAMED_LIST: { auto newStruct = orphanage.newOrphan(); auto newSourceInfo = orphanage.newOrphan(); 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()); builder.setIsGeneric(parent.getIsGeneric() || implicitParams.size() > 0); builder.setScopeId(0); // detached struct type builder.initStruct(); // Note that the struct we create here has a brand parameter list mirrioring the method's // implicit parameter list. Of course, fields inside the struct using the method's implicit // params as types actually need to refer to them as regular params, so we create an // ImplicitParams with a scopeId here. StructTranslator(*this, ImplicitParams { builder.getId(), implicitParams }) .translate(paramList.getNamedList(), builder, newSourceInfo.get()); uint64_t id = builder.getId(); paramStructs.add(AuxNode { kj::mv(newStruct), kj::mv(newSourceInfo) }); auto brand = localBrand->push(builder.getId(), implicitParams.size()); if (implicitParams.size() > 0) { auto implicitDecls = kj::heapArrayBuilder(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); return id; } case Declaration::ParamList::TYPE: KJ_IF_MAYBE(target, compileDeclExpression( paramList.getType(), ImplicitParams { 0, implicitParams })) { KJ_IF_MAYBE(kind, target->getKind()) { if (*kind == Declaration::STRUCT) { return target->getIdAndFillBrand(kj::fwd(initBrand)); } else { errorReporter.addErrorOn( paramList.getType(), kj::str("'", expressionString(paramList.getType()), "' is not a struct type.")); } } else { // 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; } } return 0; 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(); } KJ_UNREACHABLE; } // ------------------------------------------------------------------- static const char HEXDIGITS[] = "0123456789abcdef"; static kj::StringTree stringLiteral(kj::StringPtr chars) { return kj::strTree('"', kj::encodeCEscape(chars), '"'); } static kj::StringTree binaryLiteral(Data::Reader data) { kj::Vector escaped(data.size() * 3); for (byte b: data) { escaped.add(HEXDIGITS[b % 16]); escaped.add(HEXDIGITS[b / 16]); escaped.add(' '); } escaped.removeLast(); return kj::strTree("0x\"", escaped, '"'); } static kj::StringTree expressionStringTree(Expression::Reader exp); static kj::StringTree tupleLiteral(List::Reader params) { auto parts = kj::heapArrayBuilder(params.size()); for (auto param: params) { auto part = expressionStringTree(param.getValue()); if (param.isNamed()) { part = kj::strTree(param.getNamed().getValue(), " = ", kj::mv(part)); } parts.add(kj::mv(part)); } return kj::strTree("( ", kj::StringTree(parts.finish(), ", "), " )"); } static kj::StringTree expressionStringTree(Expression::Reader exp) { switch (exp.which()) { case Expression::UNKNOWN: return kj::strTree(""); 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())); case Expression::EMBED: return kj::strTree("embed ", stringLiteral(exp.getEmbed().getValue())); case Expression::LIST: { auto list = exp.getList(); auto parts = kj::heapArrayBuilder(list.size()); for (auto element: list) { parts.add(expressionStringTree(element)); } return kj::strTree("[ ", kj::StringTree(parts.finish(), ", "), " ]"); } case Expression::TUPLE: return tupleLiteral(exp.getTuple()); 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(); } // ------------------------------------------------------------------- kj::Maybe NodeTranslator::compileDeclExpression( Expression::Reader source, ImplicitParams implicitMethodParams) { return localBrand->compileDeclExpression(source, resolver, implicitMethodParams); } /* static */ kj::Maybe NodeTranslator::compileDecl( uint64_t scopeId, uint scopeParameterCount, Resolver& resolver, ErrorReporter& errorReporter, Expression::Reader expression, schema::Brand::Builder brandBuilder) { auto scope = kj::refcounted(errorReporter, scopeId, scopeParameterCount, resolver); KJ_IF_MAYBE(decl, scope->compileDeclExpression(expression, resolver, noImplicitParams())) { return decl->asResolveResult(scope->getScopeId(), brandBuilder); } else { return nullptr; } } bool NodeTranslator::compileType(Expression::Reader source, schema::Type::Builder target, ImplicitParams implicitMethodParams) { KJ_IF_MAYBE(decl, compileDeclExpression(source, implicitMethodParams)) { return decl->compileAsType(errorReporter, target); } else { return false; } } // ------------------------------------------------------------------- void NodeTranslator::compileDefaultDefaultValue( schema::Type::Reader type, schema::Value::Builder target) { switch (type.which()) { 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; // Bit of a hack: For Text/Data, we adopt a null orphan, which sets the field to null. // TODO(cleanup): Create a cleaner way to do this. case schema::Type::TEXT: target.adoptText(Orphan()); break; case schema::Type::DATA: target.adoptData(Orphan()); break; case schema::Type::STRUCT: target.initStruct(); break; case schema::Type::LIST: target.initList(); break; case schema::Type::ANY_POINTER: target.initAnyPointer(); break; } } void NodeTranslator::compileBootstrapValue( Expression::Reader source, schema::Type::Reader type, schema::Value::Builder target, kj::Maybe typeScope) { // 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); switch (type.which()) { case schema::Type::LIST: case schema::Type::STRUCT: case schema::Type::INTERFACE: case schema::Type::ANY_POINTER: unfinishedValues.add(UnfinishedValue { source, type, typeScope, target }); break; default: // Primitive value. (Note that the scope can't possibly matter since primitives are not // generic.) compileValue(source, type, typeScope.orDefault(Schema()), target, true); break; } } void NodeTranslator::compileValue(Expression::Reader source, schema::Type::Reader type, Schema typeScope, schema::Value::Builder target, bool isBootstrap) { class ResolverGlue: public ValueTranslator::Resolver { public: inline ResolverGlue(NodeTranslator& translator, bool isBootstrap) : translator(translator), isBootstrap(isBootstrap) {} kj::Maybe resolveConstant(Expression::Reader name) override { return translator.readConstant(name, isBootstrap); } kj::Maybe> readEmbed(LocatedText::Reader filename) override { return translator.readEmbed(filename); } private: NodeTranslator& translator; bool isBootstrap; }; ResolverGlue glue(*this, isBootstrap); ValueTranslator valueTranslator(glue, errorReporter, orphanage); KJ_IF_MAYBE(typeSchema, resolver.resolveBootstrapType(type, typeScope)) { kj::StringPtr fieldName = Schema::from() .getUnionFields()[static_cast(typeSchema->which())].getProto().getName(); KJ_IF_MAYBE(value, valueTranslator.compileValue(source, *typeSchema)) { if (typeSchema->isEnum()) { target.setEnum(value->getReader().as().getRaw()); } else { toDynamic(target).adopt(fieldName, kj::mv(*value)); } } } } kj::Maybe> ValueTranslator::compileValue(Expression::Reader src, Type type) { if (type.isAnyPointer()) { if (type.getBrandParameter() != nullptr || type.getImplicitParameter() != nullptr) { errorReporter.addErrorOn(src, "Cannot interpret value because the type is a generic type parameter which is not " "yet bound. We don't know what type to expect here."); return nullptr; } } Orphan result = compileValueInner(src, type); // compileValueInner() evaluated `src` and only used `type` as a hint in interpreting `src` if // `src`'s type wasn't already obvious. So, now we need to check that the resulting value // actually matches `type`. switch (result.getType()) { case DynamicValue::UNKNOWN: // Error already reported. return nullptr; case DynamicValue::VOID: if (type.isVoid()) { return kj::mv(result); } break; case DynamicValue::BOOL: if (type.isBool()) { return kj::mv(result); } break; case DynamicValue::INT: { int64_t value = result.getReader().as(); if (value < 0) { int64_t minValue = 1; switch (type.which()) { 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; case schema::Type::FLOAT32: case schema::Type::FLOAT64: // Any integer is acceptable. minValue = (int64_t)kj::minValue; break; default: break; } if (minValue == 1) break; if (value < minValue) { errorReporter.addErrorOn(src, "Integer value out of range."); result = minValue; } return kj::mv(result); } } // fallthrough -- value is positive, so we can just go on to the uint case below. case DynamicValue::UINT: { uint64_t maxValue = 0; switch (type.which()) { 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; case schema::Type::FLOAT32: case schema::Type::FLOAT64: // Any integer is acceptable. maxValue = (uint64_t)kj::maxValue; break; default: break; } if (maxValue == 0) break; if (result.getReader().as() > 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); } break; case DynamicValue::TEXT: if (type.isText()) { return kj::mv(result); } break; case DynamicValue::DATA: if (type.isData()) { return kj::mv(result); } break; case DynamicValue::LIST: if (type.isList()) { if (result.getReader().as().getSchema() == type.asList()) { return kj::mv(result); } } 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; } } break; case DynamicValue::ENUM: if (type.isEnum()) { if (result.getReader().as().getSchema() == type.asEnum()) { return kj::mv(result); } } break; case DynamicValue::STRUCT: if (type.isStruct()) { if (result.getReader().as().getSchema() == type.asStruct()) { return kj::mv(result); } } 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; } } break; case DynamicValue::CAPABILITY: KJ_FAIL_ASSERT("Interfaces can't have literal values."); case DynamicValue::ANY_POINTER: KJ_FAIL_ASSERT("AnyPointers can't have literal values."); } errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), ".")); return nullptr; } Orphan ValueTranslator::compileValueInner(Expression::Reader src, Type type) { switch (src.which()) { 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()) { KJ_IF_MAYBE(enumerant, type.asEnum().findEnumerantByName(id)) { return DynamicEnum(*enumerant); } } 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(); } } // Apparently not a literal. Try resolving it. KJ_IF_MAYBE(constValue, resolver.resolveConstant(src)) { return orphanage.newOrphanCopy(*constValue); } else { return nullptr; } } 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; } 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(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. 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 copy; kj::ArrayPtr words; if (reinterpret_cast(data->begin()) % sizeof(void*) == 0) { // Hooray, data is aligned. words = kj::ArrayPtr( reinterpret_cast(data->begin()), data->size() / sizeof(word)); } else { // Ugh, data not aligned. Make a copy. copy = kj::heapArray(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(type.asStruct())); } default: errorReporter.addErrorOn(src, "Embeds can only be used when Text, Data, or a struct is expected."); return nullptr; } } else { return nullptr; } case Expression::POSITIVE_INT: return src.getPositiveInt(); case Expression::NEGATIVE_INT: { uint64_t nValue = src.getNegativeInt(); if (nValue > ((uint64_t)kj::maxValue >> 1) + 1) { errorReporter.addErrorOn(src, "Integer is too big to be negative."); return nullptr; } else { return kj::implicitCast(-nValue); } } case Expression::FLOAT: return src.getFloat(); break; case Expression::STRING: if (type.isData()) { Text::Reader text = src.getString(); return orphanage.newOrphanCopy(Data::Reader(text.asBytes())); } else { return orphanage.newOrphanCopy(src.getString()); } break; case Expression::BINARY: if (!type.isData()) { errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), ".")); return nullptr; } return orphanage.newOrphanCopy(src.getBinary()); case Expression::LIST: { if (!type.isList()) { errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), ".")); return nullptr; } auto listSchema = type.asList(); Type elementType = listSchema.getElementType(); auto srcList = src.getList(); Orphan 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)); } } return kj::mv(result); } case Expression::TUPLE: { if (!type.isStruct()) { errorReporter.addErrorOn(src, kj::str("Type mismatch; expected ", makeTypeName(type), ".")); return nullptr; } auto structSchema = type.asStruct(); Orphan result = orphanage.newOrphan(structSchema); fillStructValue(result.get(), src.getTuple()); return kj::mv(result); } case Expression::UNKNOWN: // Ignore earlier error. return nullptr; } KJ_UNREACHABLE; } void ValueTranslator::fillStructValue(DynamicStruct::Builder builder, List::Reader assignments) { for (auto assignment: assignments) { 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: KJ_IF_MAYBE(compiledValue, compileValue(value, field->getType())) { builder.adopt(*field, kj::mv(*compiledValue)); } break; case schema::Field::GROUP: if (value.isTuple()) { fillStructValue(builder.init(*field).as(), value.getTuple()); } else { errorReporter.addErrorOn(value, "Type mismatch; expected group."); } break; } } else { errorReporter.addErrorOn(fieldName, kj::str( "Struct has no field named '", fieldName.getValue(), "'.")); } } else { errorReporter.addErrorOn(assignment.getValue(), kj::str("Missing field name.")); } } } kj::String ValueTranslator::makeNodeName(Schema schema) { schema::Node::Reader proto = schema.getProto(); return kj::str(proto.getDisplayName().slice(proto.getDisplayNamePrefixLength())); } kj::String ValueTranslator::makeTypeName(Type type) { 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"); case schema::Type::LIST: 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()); case schema::Type::ANY_POINTER: return kj::str("AnyPointer"); } KJ_UNREACHABLE; } kj::Maybe NodeTranslator::readConstant( Expression::Reader source, bool isBootstrap) { // Look up the constant decl. NodeTranslator::BrandedDecl constDecl = nullptr; KJ_IF_MAYBE(decl, compileDeclExpression(source, noImplicitParams())) { constDecl = *decl; } else { // Lookup will have reported an error. return nullptr; } // 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; } // Extract the ID and brand. MallocMessageBuilder builder(256); auto constBrand = builder.getRoot(); uint64_t id = constDecl.getIdAndFillBrand([&]() { return constBrand; }); // Look up the schema -- we'll need this to compile the constant's type. Schema constSchema; KJ_IF_MAYBE(s, resolver.resolveBootstrapSchema(id, constBrand)) { constSchema = *s; } else { // The constant's schema is broken for reasons already reported. return nullptr; } // 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; } else { // The constant's final schema is broken for reasons already reported. return nullptr; } } 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(); auto constType = constSchema.asConst().getType(); switch (constType.which()) { case schema::Type::STRUCT: constValue = objValue.getAs(constType.asStruct()); break; case schema::Type::LIST: constValue = objValue.getAs(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(), schema::Brand::Reader())) { auto scopeReader = scope->getProto(); kj::StringPtr parent; if (scopeReader.isFile()) { parent = ""; } else { parent = scopeReader.getDisplayName().slice(scopeReader.getDisplayNamePrefixLength()); } kj::StringPtr id = source.getRelativeName().getValue(); 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.")); } } return constValue; } kj::Maybe> 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; } } Orphan> NodeTranslator::compileAnnotationApplications( List::Reader annotations, kj::StringPtr targetsFlagName) { if (annotations.size() == 0 || !compileAnnotations) { // Return null. return Orphan>(); } auto result = orphanage.newOrphan>(annotations.size()); auto builder = result.get(); for (uint i = 0; i < annotations.size(); i++) { Declaration::AnnotationApplication::Reader annotation = annotations[i]; schema::Annotation::Builder annotationBuilder = builder[i]; // Set the annotation's value to void in case we fail to produce something better below. annotationBuilder.initValue().setVoid(); auto name = annotation.getName(); KJ_IF_MAYBE(decl, compileDeclExpression(name, noImplicitParams())) { KJ_IF_MAYBE(kind, decl->getKind()) { if (*kind != Declaration::ANNOTATION) { errorReporter.addErrorOn(name, kj::str( "'", expressionString(name), "' is not an annotation.")); } else { annotationBuilder.setId(decl->getIdAndFillBrand( [&]() { return annotationBuilder.initBrand(); })); KJ_IF_MAYBE(annotationSchema, resolver.resolveBootstrapSchema(annotationBuilder.getId(), annotationBuilder.getBrand())) { auto node = annotationSchema->getProto().getAnnotation(); if (!toDynamic(node).get(targetsFlagName).as()) { errorReporter.addErrorOn(name, kj::str( "'", expressionString(name), "' cannot be applied to this kind of declaration.")); } // 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(), annotationBuilder.getValue(), *annotationSchema); break; } } } } else if (*kind != Declaration::ANNOTATION) { errorReporter.addErrorOn(name, kj::str( "'", expressionString(name), "' is not an annotation.")); } } } return result; } } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/compiler.h0000644000175000017500000002343713650101756022452 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include #include "error-reporter.h" CAPNP_BEGIN_HEADER namespace capnp { namespace compiler { class Module: public ErrorReporter { public: virtual kj::StringPtr getSourceName() = 0; // The name of the module file relative to the source tree. Used to decide where to output // generated code and to form the `displayName` in the schema. virtual Orphan loadContent(Orphanage orphanage) = 0; // Loads the module content, using the given orphanage to allocate objects if necessary. virtual kj::Maybe importRelative(kj::StringPtr importPath) = 0; // Find another module, relative to this one. Importing the same logical module twice should // produce the exact same object, comparable by identity. These objects are owned by some // outside pool that outlives the Compiler instance. virtual kj::Maybe> embedRelative(kj::StringPtr embedPath) = 0; // Read and return the content of a file specified using `embed`. }; class Compiler final: private SchemaLoader::LazyLoadCallback { // Cross-links separate modules (schema files) and translates them into schema nodes. // // This class is thread-safe, hence all its methods are const. public: enum AnnotationFlag { COMPILE_ANNOTATIONS, // Compile annotations normally. DROP_ANNOTATIONS // Do not compile any annotations, eagerly or lazily. All "annotations" fields in the schema // will be left empty. This is useful to avoid parsing imports that are used only for // annotations which you don't intend to use anyway. // // Unfortunately annotations cannot simply be compiled lazily because filling in the // "annotations" field at the usage site requires knowing the annotation's type, which requires // compiling the annotation, and the schema API has no particular way to detect when you // try to access the "annotations" field in order to lazily compile the annotations at that // point. }; explicit Compiler(AnnotationFlag annotationFlag = COMPILE_ANNOTATIONS); ~Compiler() noexcept(false); KJ_DISALLOW_COPY(Compiler); uint64_t add(Module& module) const; // Add a module to the Compiler, returning the module's file ID. The ID can then be looked up in // the `SchemaLoader` returned by `getLoader()`. However, the SchemaLoader may behave as if the // schema node doesn't exist if any compilation errors occur (reported via the module's // ErrorReporter). The module is parsed at the time `add()` is called, but not fully compiled -- // individual schema nodes are compiled lazily. If you want to force eager compilation, // see `eagerlyCompile()`, below. kj::Maybe lookup(uint64_t parent, kj::StringPtr childName) const; // Given the type ID of a schema node, find the ID of a node nested within it. Throws an // exception if the parent ID is not recognized; returns null if the parent has no child of the // given name. Neither the parent nor the child schema node is actually compiled. kj::Maybe getSourceInfo(uint64_t id) const; // Get the SourceInfo for the given type ID, if available. Orphan> getFileImportTable(Module& module, Orphanage orphanage) const; // Build the import table for the CodeGeneratorRequest for the given module. Orphan> getAllSourceInfo(Orphanage orphanage) const; // Gets the SourceInfo structs for all nodes parsed by the compiler. enum Eagerness: uint32_t { // Flags specifying how eager to be about compilation. These are intended to be bitwise OR'd. // Used with the method `eagerlyCompile()`. // // Schema declarations can be compiled upfront, or they can be compiled lazily as they are // needed. Usually, the difference is not observable, but it is not a perfect abstraction. // The difference has the following effects: // * `getLoader().getAllLoaded()` only returns the schema nodes which have been compiled so // far. // * `getLoader().get()` (i.e. searching for a schema by ID) can only find schema nodes that // have either been compiled already, or which are referenced by schema nodes which have been // compiled already. This means that if the ID you pass in came from another schema node // compiled with the same compiler, there should be no observable difference, but if you // have an ID from elsewhere which you _a priori_ expect is defined in a particular schema // file, you will need to compile that file eagerly before you look up the node by ID. // * Errors are reported when they are encountered, so some errors will not be reported until // the node is actually compiled. // * If an imported file is not needed, it will never even be read from disk. // // The last point is the main reason why you might want to prefer lazy compilation: it allows // you to use a schema file with missing imports, so long as those missing imports are not // actually needed. // // For example, the flag combo: // EAGER_NODE | EAGER_CHILDREN | EAGER_DEPENDENCIES | EAGER_DEPENDENCY_PARENTS // will compile the entire given module, plus all direct dependencies of anything in that // module, plus all lexical ancestors of those dependencies. This is what the Cap'n Proto // compiler uses when building initial code generator requests. ALL_RELATED_NODES = ~0u, // Compile everything that is in any way related to the target node, including its entire // containing file and everything transitively imported by it. NODE = 1 << 0, // Eagerly compile the requested node, but not necessarily any of its parents, children, or // dependencies. PARENTS = 1 << 1, // Eagerly compile all lexical parents of the requested node. Only meaningful in conjuction // with NODE. CHILDREN = 1 << 2, // Eagerly compile all of the node's lexically nested nodes. Only meaningful in conjuction // with NODE. DEPENDENCIES = NODE << 15, // For all nodes compiled as a result of the above flags, also compile their direct // dependencies. E.g. if Foo is a struct which contains a field of type Bar, and Foo is // compiled, then also compile Bar. "Dependencies" are defined as field types, method // parameter and return types, and annotation types. Nested types and outer types are not // considered dependencies. DEPENDENCY_PARENTS = PARENTS * DEPENDENCIES, DEPENDENCY_CHILDREN = CHILDREN * DEPENDENCIES, DEPENDENCY_DEPENDENCIES = DEPENDENCIES * DEPENDENCIES, // Like PARENTS, CHILDREN, and DEPENDENCIES, but applies relative to dependency nodes rather // than the original requested node. Note that DEPENDENCY_DEPENDENCIES causes all transitive // dependencies of the requested node to be compiled. // // These flags are defined as multiples of the original flag and DEPENDENCIES so that we // can form the flags to use when traversing a dependency by shifting bits. }; void eagerlyCompile(uint64_t id, uint eagerness) const; // Force eager compilation of schema nodes related to the given ID. `eagerness` specifies which // related nodes should be compiled before returning. It is a bitwise OR of the possible values // of the `Eagerness` enum. // // If this returns and no errors have been reported, then it is guaranteed that the compiled // nodes can be found in the SchemaLoader returned by `getLoader()`. const SchemaLoader& getLoader() const { return loader; } SchemaLoader& getLoader() { return loader; } // Get a SchemaLoader backed by this compiler. Schema nodes will be lazily constructed as you // traverse them using this loader. void clearWorkspace() const; // The compiler builds a lot of temporary tables and data structures while it works. It's // useful to keep these around if more work is expected (especially if you are using lazy // compilation and plan to look up Schema nodes that haven't already been seen), but once // the SchemaLoader has everything you need, you can call clearWorkspace() to free up the // temporary space. Note that it's safe to call clearWorkspace() even if you do expect to // compile more nodes in the future; it may simply lead to redundant work if the discarded // structures are needed again. private: class Impl; kj::MutexGuarded> impl; SchemaLoader loader; class CompiledModule; class Node; class Alias; void load(const SchemaLoader& loader, uint64_t id) const override; }; } // namespace compiler } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/compiler/compiler.c++0000644000175000017500000014142613650101756022572 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "compiler.h" #include "parser.h" // only for generateChildId() #include #include #include #include #include #include #include #include #include "node-translator.h" namespace capnp { namespace compiler { typedef std::unordered_map> SourceInfoMap; class Compiler::Alias { public: Alias(CompiledModule& module, Node& parent, const Expression::Reader& targetName) : module(module), parent(parent), targetName(targetName) {} kj::Maybe compile(); private: CompiledModule& module; Node& parent; Expression::Reader targetName; kj::Maybe target; Orphan brandOrphan; bool initialized = false; }; class Compiler::Node final: public NodeTranslator::Resolver { // Passes through four states: // - Stub: On initial construction, the Node is just a placeholder object. Its ID has been // determined, and it is placed in its parent's member table as well as the compiler's // nodes-by-ID table. // - Expanded: Nodes have been constructed for all of this Node's nested children. This happens // the first time a lookup is performed for one of those children. // - Bootstrap: A NodeTranslator has been built and advanced to the bootstrap phase. // - Finished: A final Schema object has been constructed. public: explicit Node(CompiledModule& module); // Create a root node representing the given file. May Node(Node& parent, const Declaration::Reader& declaration); // Create a child node. Node(kj::StringPtr name, Declaration::Which kind, List::Reader genericParams); // Create a dummy node representing a built-in declaration, like "Int32" or "true". uint64_t getId() { return id; } uint getParameterCount() { return genericParamCount; } Declaration::Which getKind() { return kind; } kj::Maybe getBootstrapSchema(); kj::Maybe getFinalSchema(); void loadFinalSchema(const SchemaLoader& loader); void traverse(uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo); // Get the final schema for this node, and also possibly traverse the node's children and // dependencies to ensure that they are loaded, depending on the mode. void addError(kj::StringPtr error); // Report an error on this Node. // implements NodeTranslator::Resolver ----------------------------- kj::Maybe resolve(kj::StringPtr name) override; kj::Maybe resolveMember(kj::StringPtr name) override; ResolvedDecl resolveBuiltin(Declaration::Which which) override; ResolvedDecl resolveId(uint64_t id) override; kj::Maybe getParent() override; ResolvedDecl getTopScope() override; kj::Maybe resolveBootstrapSchema( uint64_t id, schema::Brand::Reader brand) override; kj::Maybe resolveFinalSchema(uint64_t id) override; kj::Maybe resolveImport(kj::StringPtr name) override; kj::Maybe> readEmbed(kj::StringPtr name) override; kj::Maybe resolveBootstrapType(schema::Type::Reader type, Schema scope) override; private: CompiledModule* module; // null iff isBuiltin is true kj::Maybe parent; Declaration::Reader declaration; // AST of the declaration parsed from the schema file. May become invalid once the content // state has reached FINISHED. uint64_t id; // The ID of this node, either taken from the AST or computed based on the parent. Or, a dummy // value, if duplicates were detected. kj::StringPtr displayName; // Fully-qualified display name for this node. For files, this is just the file name, otherwise // it is "filename:Path.To.Decl". Declaration::Which kind; // Kind of node. uint genericParamCount; // Number of generic parameters. bool isBuiltin; // Whether this is a bulit-in declaration, like "Int32" or "true". uint32_t startByte; uint32_t endByte; // Start and end byte for reporting general errors. struct Content { inline Content(): state(STUB) {} enum State { STUB, EXPANDED, BOOTSTRAP, FINISHED }; State state; // Indicates which fields below are valid. inline bool stateHasReached(State minimumState) { return state >= minimumState; } inline void advanceState(State newState) { state = newState; } // EXPANDED ------------------------------------ typedef std::multimap> NestedNodesMap; NestedNodesMap nestedNodes; kj::Vector orderedNestedNodes; // multimap in case of duplicate member names -- we still want to compile them, even if it's an // error. typedef std::multimap> AliasMap; AliasMap aliases; // The "using" declarations. These are just links to nodes elsewhere. // BOOTSTRAP ----------------------------------- NodeTranslator* translator; // Node translator, allocated in the bootstrap arena. kj::Maybe bootstrapSchema; // The schema built in the bootstrap loader. Null if the bootstrap loader threw an exception. // FINISHED ------------------------------------ kj::Maybe finalSchema; // The completed schema, ready to load into the real schema loader. kj::Array auxSchemas; // Schemas for all auxiliary nodes built by the NodeTranslator. kj::Array sourceInfo; // All source info structs as built by the NodeTranslator. }; Content guardedContent; // Read using getContent() only! bool inGetContent = false; // True while getContent() is running; detects cycles. kj::Maybe loadedFinalSchema; // Copy of `finalSchema` as loaded into the final schema loader. This doesn't go away if the // workspace is destroyed. // --------------------------------------------- static uint64_t generateId(uint64_t parentId, kj::StringPtr declName, Declaration::Id::Reader declId); // Extract the ID from the declaration, or if it has none, generate one based on the name and // parent ID. static kj::StringPtr joinDisplayName(kj::Arena& arena, Node& parent, kj::StringPtr declName); // Join the parent's display name with the child's unqualified name to construct the child's // display name. kj::Maybe getContent(Content::State minimumState); // Advances the content to at least the given state and returns it. Returns null if getContent() // is being called recursively and the given state has not yet been reached, as this indicates // that the declaration recursively depends on itself. void traverseNodeDependencies(const schema::Node::Reader& schemaNode, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo); void traverseType(const schema::Type::Reader& type, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo); void traverseBrand(const schema::Brand::Reader& brand, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo); void traverseAnnotations(const List::Reader& annotations, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo); void traverseDependency(uint64_t depId, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo, bool ignoreIfNotFound = false); // Helpers for traverse(). }; class Compiler::CompiledModule { public: CompiledModule(Compiler::Impl& compiler, Module& parserModule); Compiler::Impl& getCompiler() { return compiler; } ErrorReporter& getErrorReporter() { return parserModule; } ParsedFile::Reader getParsedFile() { return content.getReader(); } Node& getRootNode() { return rootNode; } kj::StringPtr getSourceName() { return parserModule.getSourceName(); } kj::Maybe importRelative(kj::StringPtr importPath); kj::Maybe> embedRelative(kj::StringPtr importPath); Orphan> getFileImportTable(Orphanage orphanage); private: Compiler::Impl& compiler; Module& parserModule; MallocMessageBuilder contentArena; Orphan content; Node rootNode; }; class Compiler::Impl: public SchemaLoader::LazyLoadCallback { public: explicit Impl(AnnotationFlag annotationFlag); virtual ~Impl() noexcept(false); uint64_t add(Module& module); kj::Maybe lookup(uint64_t parent, kj::StringPtr childName); kj::Maybe getSourceInfo(uint64_t id); Orphan> getFileImportTable(Module& module, Orphanage orphanage); Orphan> getAllSourceInfo(Orphanage orphanage); void eagerlyCompile(uint64_t id, uint eagerness, const SchemaLoader& loader); CompiledModule& addInternal(Module& parsedModule); struct Workspace { // Scratch space where stuff can be allocated while working. The Workspace is available // whenever nodes are actively being compiled, then is destroyed once control exits the // compiler. Note that since nodes are compiled lazily, a new Workspace may have to be // constructed in order to compile more nodes later. MallocMessageBuilder message; Orphanage orphanage; // Orphanage for allocating temporary Cap'n Proto objects. kj::Arena arena; // Arena for allocating temporary native objects. Note that objects in `arena` may contain // pointers into `message` that will be manipulated on destruction, so `arena` must be declared // after `message`. SchemaLoader bootstrapLoader; // Loader used to load bootstrap schemas. The bootstrap schema nodes are similar to the final // versions except that any value expressions which depend on knowledge of other types (e.g. // default values for struct fields) are left unevaluated (the values in the schema are empty). // These bootstrap schemas can then be plugged into the dynamic API and used to evaluate these // remaining values. inline explicit Workspace(const SchemaLoader::LazyLoadCallback& loaderCallback) : orphanage(message.getOrphanage()), bootstrapLoader(loaderCallback) {} }; kj::Arena& getNodeArena() { return nodeArena; } // Arena where nodes and other permanent objects should be allocated. Workspace& getWorkspace() { return workspace; } // Temporary workspace that can be used to construct bootstrap objects. inline bool shouldCompileAnnotations() { return annotationFlag == AnnotationFlag::COMPILE_ANNOTATIONS; } void clearWorkspace(); // Reset the temporary workspace. uint64_t addNode(uint64_t desiredId, Node& node); // Add the given node to the by-ID map under the given ID. If another node with the same ID // already exists, choose a new one arbitrarily and use that instead. Return the ID that was // finally used. kj::Maybe findNode(uint64_t id); kj::Maybe lookupBuiltin(kj::StringPtr name); Node& getBuiltin(Declaration::Which which); void load(const SchemaLoader& loader, uint64_t id) const override; // SchemaLoader callback for the bootstrap loader. void loadFinal(const SchemaLoader& loader, uint64_t id); // Called from the SchemaLoader callback for the final loader. private: AnnotationFlag annotationFlag; kj::Arena nodeArena; // Arena used to allocate nodes and other permanent objects. std::unordered_map> modules; // Map of parser modules to compiler modules. Workspace workspace; // The temporary workspace. This field must be declared after `modules` because objects // allocated in the workspace may hold references to the compiled modules in `modules`. std::unordered_map nodesById; // Map of nodes by ID. std::unordered_map sourceInfoById; // Map of SourceInfos by ID, including SourceInfos for groups and param sturcts (which are not // listed in nodesById). std::map> builtinDecls; std::map builtinDeclsByKind; // Map of built-in declarations, like "Int32" and "List", which make up the global scope. uint64_t nextBogusId = 1000; // Counter for assigning bogus IDs to nodes whose real ID is a duplicate. }; // ======================================================================================= kj::Maybe Compiler::Alias::compile() { if (!initialized) { initialized = true; auto& workspace = module.getCompiler().getWorkspace(); brandOrphan = workspace.orphanage.newOrphan(); // If the Workspace is destroyed, revert the alias to the uninitialized state, because the // orphan we created is no longer valid in this case. workspace.arena.copy(kj::defer([this]() { initialized = false; brandOrphan = Orphan(); })); target = NodeTranslator::compileDecl( parent.getId(), parent.getParameterCount(), parent, module.getErrorReporter(), targetName, brandOrphan.get()); } return target; } // ======================================================================================= Compiler::Node::Node(CompiledModule& module) : module(&module), parent(nullptr), declaration(module.getParsedFile().getRoot()), id(generateId(0, declaration.getName().getValue(), declaration.getId())), displayName(module.getSourceName()), kind(declaration.which()), genericParamCount(declaration.getParameters().size()), isBuiltin(false) { auto name = declaration.getName(); if (name.getValue().size() > 0) { startByte = name.getStartByte(); endByte = name.getEndByte(); } else { startByte = declaration.getStartByte(); endByte = declaration.getEndByte(); } id = module.getCompiler().addNode(id, *this); } Compiler::Node::Node(Node& parent, const Declaration::Reader& declaration) : module(parent.module), parent(parent), declaration(declaration), id(generateId(parent.id, declaration.getName().getValue(), declaration.getId())), displayName(joinDisplayName(parent.module->getCompiler().getNodeArena(), parent, declaration.getName().getValue())), kind(declaration.which()), genericParamCount(declaration.getParameters().size()), isBuiltin(false) { auto name = declaration.getName(); if (name.getValue().size() > 0) { startByte = name.getStartByte(); endByte = name.getEndByte(); } else { startByte = declaration.getStartByte(); endByte = declaration.getEndByte(); } id = module->getCompiler().addNode(id, *this); } Compiler::Node::Node(kj::StringPtr name, Declaration::Which kind, List::Reader genericParams) : module(nullptr), parent(nullptr), // It's helpful if these have unique IDs. Real type IDs can't be under 2^31 anyway. id(1000 + static_cast(kind)), displayName(name), kind(kind), genericParamCount(genericParams.size()), isBuiltin(true), startByte(0), endByte(0) {} uint64_t Compiler::Node::generateId(uint64_t parentId, kj::StringPtr declName, Declaration::Id::Reader declId) { if (declId.isUid()) { return declId.getUid().getValue(); } return generateChildId(parentId, declName); } kj::StringPtr Compiler::Node::joinDisplayName( kj::Arena& arena, Node& parent, kj::StringPtr declName) { kj::ArrayPtr result = arena.allocateArray( parent.displayName.size() + declName.size() + 2); size_t separatorPos = parent.displayName.size(); memcpy(result.begin(), parent.displayName.begin(), separatorPos); result[separatorPos] = parent.parent == nullptr ? ':' : '.'; memcpy(result.begin() + separatorPos + 1, declName.begin(), declName.size()); result[result.size() - 1] = '\0'; return kj::StringPtr(result.begin(), result.size() - 1); } kj::Maybe Compiler::Node::getContent(Content::State minimumState) { KJ_REQUIRE(!isBuiltin, "illegal method call for built-in declaration"); auto& content = guardedContent; if (content.stateHasReached(minimumState)) { return content; } if (inGetContent) { addError("Declaration recursively depends on itself."); return nullptr; } inGetContent = true; KJ_DEFER(inGetContent = false); switch (content.state) { case Content::STUB: { if (minimumState <= Content::STUB) break; // Expand the child nodes. auto& arena = module->getCompiler().getNodeArena(); for (auto nestedDecl: declaration.getNestedDecls()) { switch (nestedDecl.which()) { case Declaration::FILE: case Declaration::CONST: case Declaration::ANNOTATION: case Declaration::ENUM: case Declaration::STRUCT: case Declaration::INTERFACE: { kj::Own subNode = arena.allocateOwn(*this, nestedDecl); kj::StringPtr name = nestedDecl.getName().getValue(); content.orderedNestedNodes.add(subNode); content.nestedNodes.insert(std::make_pair(name, kj::mv(subNode))); break; } case Declaration::USING: { kj::Own alias = arena.allocateOwn( *module, *this, nestedDecl.getUsing().getTarget()); kj::StringPtr name = nestedDecl.getName().getValue(); content.aliases.insert(std::make_pair(name, kj::mv(alias))); break; } case Declaration::ENUMERANT: case Declaration::FIELD: case Declaration::UNION: case Declaration::GROUP: case Declaration::METHOD: case Declaration::NAKED_ID: case Declaration::NAKED_ANNOTATION: // Not a node. Skip. break; default: KJ_FAIL_ASSERT("unknown declaration type", nestedDecl); break; } } content.advanceState(Content::EXPANDED); } // fallthrough case Content::EXPANDED: { if (minimumState <= Content::EXPANDED) break; // Construct the NodeTranslator. auto& workspace = module->getCompiler().getWorkspace(); auto schemaNode = workspace.orphanage.newOrphan(); auto builder = schemaNode.get(); builder.setId(id); builder.setDisplayName(displayName); // TODO(cleanup): Would be better if we could remember the prefix length from before we // added this decl's name to the end. KJ_IF_MAYBE(lastDot, displayName.findLast('.')) { builder.setDisplayNamePrefixLength(*lastDot + 1); } KJ_IF_MAYBE(lastColon, displayName.findLast(':')) { if (*lastColon > builder.getDisplayNamePrefixLength()) { builder.setDisplayNamePrefixLength(*lastColon + 1); } } KJ_IF_MAYBE(p, parent) { builder.setScopeId(p->id); } auto nestedNodes = builder.initNestedNodes(content.orderedNestedNodes.size()); auto nestedIter = nestedNodes.begin(); for (auto node: content.orderedNestedNodes) { nestedIter->setName(node->declaration.getName().getValue()); nestedIter->setId(node->id); ++nestedIter; } content.translator = &workspace.arena.allocate( *this, module->getErrorReporter(), declaration, kj::mv(schemaNode), module->getCompiler().shouldCompileAnnotations()); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&](){ auto nodeSet = content.translator->getBootstrapNode(); for (auto& auxNode: nodeSet.auxNodes) { workspace.bootstrapLoader.loadOnce(auxNode); } content.bootstrapSchema = workspace.bootstrapLoader.loadOnce(nodeSet.node); })) { content.bootstrapSchema = nullptr; // Only bother to report validation failures if we think we haven't seen any errors. // Otherwise we assume that the errors caused the validation failure. if (!module->getErrorReporter().hadErrors()) { addError(kj::str("Internal compiler bug: Bootstrap schema failed validation:\n", *exception)); } } // If the Workspace is destroyed, revert the node to the EXPANDED state, because the // NodeTranslator is no longer valid in this case. workspace.arena.copy(kj::defer([&content]() { content.bootstrapSchema = nullptr; if (content.state > Content::EXPANDED) { content.state = Content::EXPANDED; } })); content.advanceState(Content::BOOTSTRAP); } // fallthrough case Content::BOOTSTRAP: { if (minimumState <= Content::BOOTSTRAP) break; // Create the final schema. NodeTranslator::NodeSet nodeSet; if (content.bootstrapSchema == nullptr) { // Must have failed in an earlier stage. KJ_ASSERT(module->getErrorReporter().hadErrors()); nodeSet = content.translator->getBootstrapNode(); } else { nodeSet = content.translator->finish( module->getCompiler().getWorkspace().bootstrapLoader.getUnbound(id)); } content.finalSchema = nodeSet.node; content.auxSchemas = kj::mv(nodeSet.auxNodes); content.sourceInfo = kj::mv(nodeSet.sourceInfo); content.advanceState(Content::FINISHED); } // fallthrough case Content::FINISHED: break; } return content; } kj::Maybe Compiler::Node::getBootstrapSchema() { KJ_IF_MAYBE(schema, loadedFinalSchema) { // We don't need to rebuild the bootstrap schema if we already have a final schema. return module->getCompiler().getWorkspace().bootstrapLoader.loadOnce(*schema); } else KJ_IF_MAYBE(content, getContent(Content::BOOTSTRAP)) { if (content->state == Content::FINISHED && content->bootstrapSchema == nullptr) { // The bootstrap schema was discarded. Copy it from the final schema. // (We can't just return the final schema because using it could trigger schema loader // callbacks that would deadlock.) KJ_IF_MAYBE(finalSchema, content->finalSchema) { return module->getCompiler().getWorkspace().bootstrapLoader.loadOnce(*finalSchema); } else { return nullptr; } } else { return content->bootstrapSchema; } } else { return nullptr; } } kj::Maybe Compiler::Node::getFinalSchema() { KJ_IF_MAYBE(schema, loadedFinalSchema) { return *schema; } else KJ_IF_MAYBE(content, getContent(Content::FINISHED)) { return content->finalSchema; } else { return nullptr; } } void Compiler::Node::loadFinalSchema(const SchemaLoader& loader) { KJ_IF_MAYBE(content, getContent(Content::FINISHED)) { KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&](){ KJ_IF_MAYBE(finalSchema, content->finalSchema) { KJ_MAP(auxSchema, content->auxSchemas) { return loader.loadOnce(auxSchema); }; loadedFinalSchema = loader.loadOnce(*finalSchema).getProto(); } })) { // Schema validation threw an exception. // Don't try loading this again. content->finalSchema = nullptr; // Only bother to report validation failures if we think we haven't seen any errors. // Otherwise we assume that the errors caused the validation failure. if (!module->getErrorReporter().hadErrors()) { addError(kj::str("Internal compiler bug: Schema failed validation:\n", *exception)); } } } } void Compiler::Node::traverse(uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo) { uint& slot = seen[this]; if ((slot & eagerness) == eagerness) { // We've already covered this node. return; } slot |= eagerness; KJ_IF_MAYBE(content, getContent(Content::FINISHED)) { loadFinalSchema(finalLoader); KJ_IF_MAYBE(schema, getFinalSchema()) { if (eagerness / DEPENDENCIES != 0) { // For traversing dependencies, discard the bits lower than DEPENDENCIES and replace // them with the bits above DEPENDENCIES shifted over. uint newEagerness = (eagerness & ~(DEPENDENCIES - 1)) | (eagerness / DEPENDENCIES); traverseNodeDependencies(*schema, newEagerness, seen, finalLoader, sourceInfo); for (auto& aux: content->auxSchemas) { traverseNodeDependencies(aux, newEagerness, seen, finalLoader, sourceInfo); } } } sourceInfo.addAll(content->sourceInfo); } if (eagerness & PARENTS) { KJ_IF_MAYBE(p, parent) { p->traverse(eagerness, seen, finalLoader, sourceInfo); } } if (eagerness & CHILDREN) { KJ_IF_MAYBE(content, getContent(Content::EXPANDED)) { for (auto& child: content->orderedNestedNodes) { child->traverse(eagerness, seen, finalLoader, sourceInfo); } // Also traverse `using` declarations. for (auto& child: content->aliases) { child.second->compile(); } } } } void Compiler::Node::traverseNodeDependencies( const schema::Node::Reader& schemaNode, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo) { switch (schemaNode.which()) { case schema::Node::STRUCT: for (auto field: schemaNode.getStruct().getFields()) { switch (field.which()) { case schema::Field::SLOT: traverseType(field.getSlot().getType(), eagerness, seen, finalLoader, sourceInfo); break; case schema::Field::GROUP: // Aux node will be scanned later. break; } traverseAnnotations(field.getAnnotations(), eagerness, seen, finalLoader, sourceInfo); } break; case schema::Node::ENUM: for (auto enumerant: schemaNode.getEnum().getEnumerants()) { traverseAnnotations(enumerant.getAnnotations(), eagerness, seen, finalLoader, sourceInfo); } break; case schema::Node::INTERFACE: { auto interface = schemaNode.getInterface(); for (auto superclass: interface.getSuperclasses()) { uint64_t superclassId = superclass.getId(); if (superclassId != 0) { // if zero, we reported an error earlier traverseDependency(superclassId, eagerness, seen, finalLoader, sourceInfo); } traverseBrand(superclass.getBrand(), eagerness, seen, finalLoader, sourceInfo); } for (auto method: interface.getMethods()) { traverseDependency( method.getParamStructType(), eagerness, seen, finalLoader, sourceInfo, true); traverseBrand(method.getParamBrand(), eagerness, seen, finalLoader, sourceInfo); traverseDependency( method.getResultStructType(), eagerness, seen, finalLoader, sourceInfo, true); traverseBrand(method.getResultBrand(), eagerness, seen, finalLoader, sourceInfo); traverseAnnotations(method.getAnnotations(), eagerness, seen, finalLoader, sourceInfo); } break; } case schema::Node::CONST: traverseType(schemaNode.getConst().getType(), eagerness, seen, finalLoader, sourceInfo); break; case schema::Node::ANNOTATION: traverseType(schemaNode.getAnnotation().getType(), eagerness, seen, finalLoader, sourceInfo); break; default: break; } traverseAnnotations(schemaNode.getAnnotations(), eagerness, seen, finalLoader, sourceInfo); } void Compiler::Node::traverseType(const schema::Type::Reader& type, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo) { uint64_t id = 0; schema::Brand::Reader brand; switch (type.which()) { case schema::Type::STRUCT: id = type.getStruct().getTypeId(); brand = type.getStruct().getBrand(); break; case schema::Type::ENUM: id = type.getEnum().getTypeId(); brand = type.getEnum().getBrand(); break; case schema::Type::INTERFACE: id = type.getInterface().getTypeId(); brand = type.getInterface().getBrand(); break; case schema::Type::LIST: traverseType(type.getList().getElementType(), eagerness, seen, finalLoader, sourceInfo); return; default: return; } traverseDependency(id, eagerness, seen, finalLoader, sourceInfo); traverseBrand(brand, eagerness, seen, finalLoader, sourceInfo); } void Compiler::Node::traverseBrand( const schema::Brand::Reader& brand, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo) { for (auto scope: brand.getScopes()) { switch (scope.which()) { case schema::Brand::Scope::BIND: for (auto binding: scope.getBind()) { switch (binding.which()) { case schema::Brand::Binding::UNBOUND: break; case schema::Brand::Binding::TYPE: traverseType(binding.getType(), eagerness, seen, finalLoader, sourceInfo); break; } } break; case schema::Brand::Scope::INHERIT: break; } } } void Compiler::Node::traverseDependency(uint64_t depId, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo, bool ignoreIfNotFound) { KJ_IF_MAYBE(node, module->getCompiler().findNode(depId)) { node->traverse(eagerness, seen, finalLoader, sourceInfo); } else if (!ignoreIfNotFound) { KJ_FAIL_ASSERT("Dependency ID not present in compiler?", depId); } } void Compiler::Node::traverseAnnotations(const List::Reader& annotations, uint eagerness, std::unordered_map& seen, const SchemaLoader& finalLoader, kj::Vector& sourceInfo) { for (auto annotation: annotations) { KJ_IF_MAYBE(node, module->getCompiler().findNode(annotation.getId())) { node->traverse(eagerness, seen, finalLoader, sourceInfo); } } } void Compiler::Node::addError(kj::StringPtr error) { module->getErrorReporter().addError(startByte, endByte, error); } kj::Maybe Compiler::Node::resolve(kj::StringPtr name) { // Check members. KJ_IF_MAYBE(member, resolveMember(name)) { return *member; } // Check parameters. // TODO(perf): Maintain a map? auto params = declaration.getParameters(); for (uint i: kj::indices(params)) { if (params[i].getName() == name) { ResolveResult result; result.init(ResolvedParameter {id, i}); return result; } } // Check parent scope. KJ_IF_MAYBE(p, parent) { return p->resolve(name); } else KJ_IF_MAYBE(b, module->getCompiler().lookupBuiltin(name)) { ResolveResult result; result.init(ResolvedDecl { b->id, b->genericParamCount, 0, b->kind, b, nullptr }); return result; } else { return nullptr; } } kj::Maybe Compiler::Node::resolveMember(kj::StringPtr name) { if (isBuiltin) return nullptr; KJ_IF_MAYBE(content, getContent(Content::EXPANDED)) { { auto iter = content->nestedNodes.find(name); if (iter != content->nestedNodes.end()) { Node* node = iter->second; ResolveResult result; result.init(ResolvedDecl { node->id, node->genericParamCount, id, node->kind, node, nullptr }); return result; } } { auto iter = content->aliases.find(name); if (iter != content->aliases.end()) { return iter->second->compile(); } } } return nullptr; } NodeTranslator::Resolver::ResolvedDecl Compiler::Node::resolveBuiltin(Declaration::Which which) { auto& b = module->getCompiler().getBuiltin(which); return { b.id, b.genericParamCount, 0, b.kind, &b, nullptr }; } NodeTranslator::Resolver::ResolvedDecl Compiler::Node::resolveId(uint64_t id) { auto& n = KJ_ASSERT_NONNULL(module->getCompiler().findNode(id)); uint64_t parentId = n.parent.map([](Node& n) { return n.id; }).orDefault(0); return { n.id, n.genericParamCount, parentId, n.kind, &n, nullptr }; } kj::Maybe Compiler::Node::getParent() { return parent.map([](Node& parent) { uint64_t scopeId = parent.parent.map([](Node& gp) { return gp.id; }).orDefault(0); return ResolvedDecl { parent.id, parent.genericParamCount, scopeId, parent.kind, &parent, nullptr }; }); } NodeTranslator::Resolver::ResolvedDecl Compiler::Node::getTopScope() { Node& node = module->getRootNode(); return ResolvedDecl { node.id, 0, 0, node.kind, &node, nullptr }; } kj::Maybe Compiler::Node::resolveBootstrapSchema( uint64_t id, schema::Brand::Reader brand) { KJ_IF_MAYBE(node, module->getCompiler().findNode(id)) { // Make sure the bootstrap schema is loaded into the SchemaLoader. if (node->getBootstrapSchema() == nullptr) { return nullptr; } // Now we actually invoke get() to evaluate the brand. return module->getCompiler().getWorkspace().bootstrapLoader.get(id, brand); } else { KJ_FAIL_REQUIRE("Tried to get schema for ID we haven't seen before."); } } kj::Maybe Compiler::Node::resolveFinalSchema(uint64_t id) { KJ_IF_MAYBE(node, module->getCompiler().findNode(id)) { return node->getFinalSchema(); } else { KJ_FAIL_REQUIRE("Tried to get schema for ID we haven't seen before."); } } kj::Maybe Compiler::Node::resolveImport(kj::StringPtr name) { KJ_IF_MAYBE(m, module->importRelative(name)) { Node& root = m->getRootNode(); return ResolvedDecl { root.id, 0, 0, root.kind, &root, nullptr }; } else { return nullptr; } } kj::Maybe> Compiler::Node::readEmbed(kj::StringPtr name) { return module->embedRelative(name); } kj::Maybe Compiler::Node::resolveBootstrapType(schema::Type::Reader type, Schema scope) { // TODO(someday): Arguably should return null if the type or its dependencies are placeholders. kj::Maybe result; KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { result = module->getCompiler().getWorkspace().bootstrapLoader.getType(type, scope); })) { result = nullptr; if (!module->getErrorReporter().hadErrors()) { addError(kj::str("Internal compiler bug: Bootstrap schema failed to load:\n", *exception)); } } return result; } // ======================================================================================= Compiler::CompiledModule::CompiledModule(Compiler::Impl& compiler, Module& parserModule) : compiler(compiler), parserModule(parserModule), content(parserModule.loadContent(contentArena.getOrphanage())), rootNode(*this) {} kj::Maybe Compiler::CompiledModule::importRelative( kj::StringPtr importPath) { return parserModule.importRelative(importPath).map( [this](Module& module) -> Compiler::CompiledModule& { return compiler.addInternal(module); }); } kj::Maybe> Compiler::CompiledModule::embedRelative(kj::StringPtr embedPath) { return parserModule.embedRelative(embedPath); } static void findImports(Expression::Reader exp, std::set& output) { switch (exp.which()) { case Expression::UNKNOWN: case Expression::POSITIVE_INT: case Expression::NEGATIVE_INT: case Expression::FLOAT: case Expression::STRING: case Expression::BINARY: case Expression::RELATIVE_NAME: case Expression::ABSOLUTE_NAME: case Expression::EMBED: break; case Expression::IMPORT: output.insert(exp.getImport().getValue()); break; case Expression::LIST: for (auto element: exp.getList()) { findImports(element, output); } break; case Expression::TUPLE: for (auto element: exp.getTuple()) { findImports(element.getValue(), output); } break; case Expression::APPLICATION: { auto app = exp.getApplication(); findImports(app.getFunction(), output); for (auto param: app.getParams()) { findImports(param.getValue(), output); } break; } case Expression::MEMBER: { findImports(exp.getMember().getParent(), output); break; } } } static void findImports(Declaration::ParamList::Reader paramList, std::set& output) { switch (paramList.which()) { case Declaration::ParamList::NAMED_LIST: for (auto param: paramList.getNamedList()) { findImports(param.getType(), output); for (auto ann: param.getAnnotations()) { findImports(ann.getName(), output); } } break; case Declaration::ParamList::TYPE: findImports(paramList.getType(), output); break; case Declaration::ParamList::STREAM: output.insert("/capnp/stream.capnp"); break; } } static void findImports(Declaration::Reader decl, std::set& output) { switch (decl.which()) { case Declaration::USING: findImports(decl.getUsing().getTarget(), output); break; case Declaration::CONST: findImports(decl.getConst().getType(), output); break; case Declaration::FIELD: findImports(decl.getField().getType(), output); break; case Declaration::INTERFACE: for (auto superclass: decl.getInterface().getSuperclasses()) { findImports(superclass, output); } break; case Declaration::METHOD: { auto method = decl.getMethod(); findImports(method.getParams(), output); if (method.getResults().isExplicit()) { findImports(method.getResults().getExplicit(), output); } break; } default: break; } for (auto ann: decl.getAnnotations()) { findImports(ann.getName(), output); } for (auto nested: decl.getNestedDecls()) { findImports(nested, output); } } Orphan> Compiler::CompiledModule::getFileImportTable(Orphanage orphanage) { // Build a table of imports for CodeGeneratorRequest.RequestedFile.imports. Note that we only // care about type imports, not constant value imports, since constant values (including default // values) are already embedded in full in the schema. In other words, we only need the imports // that would need to be #included in the generated code. std::set importNames; findImports(content.getReader().getRoot(), importNames); auto result = orphanage.newOrphan>( importNames.size()); auto builder = result.get(); uint i = 0; for (auto name: importNames) { // We presumably ran this import before, so it shouldn't throw now. auto entry = builder[i++]; entry.setId(KJ_ASSERT_NONNULL(importRelative(name)).rootNode.getId()); entry.setName(name); } return result; } // ======================================================================================= Compiler::Impl::Impl(AnnotationFlag annotationFlag) : annotationFlag(annotationFlag), workspace(*this) { // Reflectively interpret the members of Declaration.body. Any member prefixed by "builtin" // defines a builtin declaration visible in the global scope. StructSchema declSchema = Schema::from(); for (auto field: declSchema.getFields()) { auto fieldProto = field.getProto(); if (fieldProto.getDiscriminantValue() != schema::Field::NO_DISCRIMINANT) { auto name = fieldProto.getName(); if (name.startsWith("builtin")) { kj::StringPtr symbolName = name.slice(strlen("builtin")); List::Reader params; for (auto annotation: fieldProto.getAnnotations()) { if (annotation.getId() == 0x94099c3f9eb32d6bull) { params = annotation.getValue().getList().getAs>(); break; } } Declaration::Which which = static_cast(fieldProto.getDiscriminantValue()); kj::Own newNode = nodeArena.allocateOwn(symbolName, which, params); builtinDeclsByKind[which] = newNode; builtinDecls[symbolName] = kj::mv(newNode); } } } } Compiler::Impl::~Impl() noexcept(false) {} void Compiler::Impl::clearWorkspace() { // Make sure we reconstruct the workspace even if destroying it throws an exception. KJ_DEFER(kj::ctor(workspace, *this)); kj::dtor(workspace); } Compiler::CompiledModule& Compiler::Impl::addInternal(Module& parsedModule) { kj::Own& slot = modules[&parsedModule]; if (slot.get() == nullptr) { slot = kj::heap(*this, parsedModule); } return *slot; } uint64_t Compiler::Impl::addNode(uint64_t desiredId, Node& node) { for (;;) { auto insertResult = nodesById.insert(std::make_pair(desiredId, &node)); if (insertResult.second) { return desiredId; } // Only report an error if this ID is not bogus. Actual IDs specified in the original source // code are required to have the upper bit set. Anything else must have been manufactured // at some point to cover up an error. if (desiredId & (1ull << 63)) { node.addError(kj::str("Duplicate ID @0x", kj::hex(desiredId), ".")); insertResult.first->second->addError( kj::str("ID @0x", kj::hex(desiredId), " originally used here.")); } // Assign a new bogus ID. desiredId = nextBogusId++; } } kj::Maybe Compiler::Impl::findNode(uint64_t id) { auto iter = nodesById.find(id); if (iter == nodesById.end()) { return nullptr; } else { return *iter->second; } } kj::Maybe Compiler::Impl::lookupBuiltin(kj::StringPtr name) { auto iter = builtinDecls.find(name); if (iter == builtinDecls.end()) { return nullptr; } else { return *iter->second; } } Compiler::Node& Compiler::Impl::getBuiltin(Declaration::Which which) { auto iter = builtinDeclsByKind.find(which); KJ_REQUIRE(iter != builtinDeclsByKind.end(), "invalid builtin", (uint)which); return *iter->second; } uint64_t Compiler::Impl::add(Module& module) { return addInternal(module).getRootNode().getId(); } kj::Maybe Compiler::Impl::lookup(uint64_t parent, kj::StringPtr childName) { // Looking up members does not use the workspace, so we don't need to lock it. KJ_IF_MAYBE(parentNode, findNode(parent)) { KJ_IF_MAYBE(child, parentNode->resolveMember(childName)) { if (child->is()) { return child->get().id; } else { // An alias. We don't support looking up aliases with this method. return nullptr; } } else { return nullptr; } } else { KJ_FAIL_REQUIRE("lookup()s parameter 'parent' must be a known ID.", parent); } } kj::Maybe Compiler::Impl::getSourceInfo(uint64_t id) { auto iter = sourceInfoById.find(id); if (iter == sourceInfoById.end()) { return nullptr; } else { return iter->second; } } Orphan> Compiler::Impl::getFileImportTable(Module& module, Orphanage orphanage) { return addInternal(module).getFileImportTable(orphanage); } Orphan> Compiler::Impl::getAllSourceInfo(Orphanage orphanage) { auto result = orphanage.newOrphan>(sourceInfoById.size()); auto builder = result.get(); size_t i = 0; for (auto& entry: sourceInfoById) { builder.setWithCaveats(i++, entry.second); } return result; } void Compiler::Impl::eagerlyCompile(uint64_t id, uint eagerness, const SchemaLoader& finalLoader) { KJ_IF_MAYBE(node, findNode(id)) { std::unordered_map seen; kj::Vector sourceInfos; node->traverse(eagerness, seen, finalLoader, sourceInfos); // Copy the SourceInfo structures into permanent space so that they aren't invalidated when // clearWorkspace() is called. for (auto& sourceInfo: sourceInfos) { auto words = nodeArena.allocateArray(sourceInfo.totalSize().wordCount + 1); memset(words.begin(), 0, words.asBytes().size()); copyToUnchecked(sourceInfo, words); sourceInfoById.insert(std::make_pair(sourceInfo.getId(), readMessageUnchecked(words.begin()))); } } else { KJ_FAIL_REQUIRE("id did not come from this Compiler.", id); } } void Compiler::Impl::load(const SchemaLoader& loader, uint64_t id) const { // We know that this load() is only called from the bootstrap loader which is already protected // by our mutex, so we can drop thread-safety. auto& self = const_cast(*this); KJ_IF_MAYBE(node, self.findNode(id)) { node->getBootstrapSchema(); } } void Compiler::Impl::loadFinal(const SchemaLoader& loader, uint64_t id) { KJ_IF_MAYBE(node, findNode(id)) { node->loadFinalSchema(loader); } } // ======================================================================================= Compiler::Compiler(AnnotationFlag annotationFlag) : impl(kj::heap(annotationFlag)), loader(*this) {} Compiler::~Compiler() noexcept(false) {} uint64_t Compiler::add(Module& module) const { return impl.lockExclusive()->get()->add(module); } kj::Maybe Compiler::lookup(uint64_t parent, kj::StringPtr childName) const { return impl.lockExclusive()->get()->lookup(parent, childName); } kj::Maybe Compiler::getSourceInfo(uint64_t id) const { return impl.lockExclusive()->get()->getSourceInfo(id); } Orphan> Compiler::getFileImportTable(Module& module, Orphanage orphanage) const { return impl.lockExclusive()->get()->getFileImportTable(module, orphanage); } Orphan> Compiler::getAllSourceInfo(Orphanage orphanage) const { return impl.lockExclusive()->get()->getAllSourceInfo(orphanage); } void Compiler::eagerlyCompile(uint64_t id, uint eagerness) const { impl.lockExclusive()->get()->eagerlyCompile(id, eagerness, loader); } void Compiler::clearWorkspace() const { impl.lockExclusive()->get()->clearWorkspace(); } void Compiler::load(const SchemaLoader& loader, uint64_t id) const { impl.lockExclusive()->get()->loadFinal(loader, id); } } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/module-loader.h0000644000175000017500000000414113650101756023360 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "compiler.h" #include "error-reporter.h" #include #include #include #include CAPNP_BEGIN_HEADER namespace capnp { namespace compiler { class ModuleLoader { public: explicit ModuleLoader(GlobalErrorReporter& errorReporter); // Create a ModuleLoader that reports error messages to the given reporter. KJ_DISALLOW_COPY(ModuleLoader); ~ModuleLoader() noexcept(false); void addImportPath(const kj::ReadableDirectory& dir); // Add a directory to the list of paths that is searched for imports that start with a '/'. kj::Maybe loadModule(const kj::ReadableDirectory& dir, kj::PathPtr path); // Tries to load a module with the given path inside the given directory. Returns nullptr if the // file doesn't exist. private: class Impl; kj::Own impl; class ModuleImpl; }; } // namespace compiler } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/compiler/module-loader.c++0000644000175000017500000002451613340402540023500 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "module-loader.h" #include "lexer.h" #include "parser.h" #include #include #include #include #include #include namespace capnp { namespace compiler { namespace { struct FileKey { // Key type for the modules map. We need to implement some complicated heuristics to detect when // two files are actually the same underlying file on disk, in order to handle the case where // people have mapped the same file into multiple locations in the import tree, whether by // passing overlapping import paths, weird symlinks, or whatever. // // This is probably over-engineered. const kj::ReadableDirectory& baseDir; kj::PathPtr path; kj::Maybe file; uint64_t hashCode; uint64_t size; kj::Date lastModified; FileKey(const kj::ReadableDirectory& baseDir, kj::PathPtr path) : baseDir(baseDir), path(path), file(nullptr), hashCode(0), size(0), lastModified(kj::UNIX_EPOCH) {} FileKey(const kj::ReadableDirectory& baseDir, kj::PathPtr path, const kj::ReadableFile& file) : FileKey(baseDir, path, file, file.stat()) {} FileKey(const kj::ReadableDirectory& baseDir, kj::PathPtr path, const kj::ReadableFile& file, kj::FsNode::Metadata meta) : baseDir(baseDir), path(path), file(&file), hashCode(meta.hashCode), size(meta.size), lastModified(meta.lastModified) {} bool operator==(const FileKey& other) const { // Allow matching on baseDir and path without a file. if (&baseDir == &other.baseDir && path == other.path) return true; if (file == nullptr || other.file == nullptr) return false; // Try comparing various file metadata to rule out obvious differences. if (hashCode != other.hashCode) return false; if (size != other.size || lastModified != other.lastModified) return false; if (path.size() > 0 && other.path.size() > 0 && path[path.size() - 1] != other.path[other.path.size() - 1]) { // Names differ, so probably not the same file. return false; } // Same file hash, but different paths, but same size and modification date. This could be a // case of two different import paths overlapping and containing the same file. We'll need to // check the content. auto mapping1 = KJ_ASSERT_NONNULL(file).mmap(0, size); auto mapping2 = KJ_ASSERT_NONNULL(other.file).mmap(0, size); if (memcmp(mapping1.begin(), mapping2.begin(), size) != 0) return false; if (path == other.path) { // Exactly the same content was mapped at exactly the same path relative to two different // import directories. This can only really happen if this was one of the files passed on // the command line, but its --src-prefix is not also an import path, but some other // directory containing the same file was given as an import path. Whatever, we'll ignore // this. return true; } // Exactly the same content! static bool warned = false; if (!warned) { KJ_LOG(WARNING, "Found exactly the same source file mapped at two different paths. This suggests " "that your -I and --src-prefix flags are overlapping or inconsistent. Remember, these " "flags should only specify directories that are logical 'roots' of the source tree. " "It should never be the case that one of the import directories contains another one of " "them.", path, other.path); warned = true; } return true; } }; struct FileKeyHash { size_t operator()(const FileKey& key) const { if (sizeof(size_t) < sizeof(key.hashCode)) { // 32-bit system, do more mixing return (key.hashCode >> 32) * 31 + static_cast(key.hashCode) + key.size * 103 + (key.lastModified - kj::UNIX_EPOCH) / kj::MILLISECONDS * 73; } else { return key.hashCode + key.size * 103 + (key.lastModified - kj::UNIX_EPOCH) / kj::NANOSECONDS * 73; } } }; }; class ModuleLoader::Impl { public: Impl(GlobalErrorReporter& errorReporter) : errorReporter(errorReporter) {} void addImportPath(const kj::ReadableDirectory& dir) { searchPath.add(&dir); } kj::Maybe loadModule(const kj::ReadableDirectory& dir, kj::PathPtr path); kj::Maybe loadModuleFromSearchPath(kj::PathPtr path); kj::Maybe> readEmbed(const kj::ReadableDirectory& dir, kj::PathPtr path); kj::Maybe> readEmbedFromSearchPath(kj::PathPtr path); GlobalErrorReporter& getErrorReporter() { return errorReporter; } private: GlobalErrorReporter& errorReporter; kj::Vector searchPath; std::unordered_map, FileKeyHash> modules; }; class ModuleLoader::ModuleImpl final: public Module { public: ModuleImpl(ModuleLoader::Impl& loader, kj::Own file, const kj::ReadableDirectory& sourceDir, kj::Path pathParam) : loader(loader), file(kj::mv(file)), sourceDir(sourceDir), path(kj::mv(pathParam)), sourceNameStr(path.toString()) { KJ_REQUIRE(path.size() > 0); } kj::PathPtr getPath() { return path; } kj::StringPtr getSourceName() override { return sourceNameStr; } Orphan loadContent(Orphanage orphanage) override { kj::Array content = file->mmap(0, file->stat().size).releaseAsChars(); lineBreaks = nullptr; // In case loadContent() is called multiple times. lineBreaks = lineBreaksSpace.construct(content); MallocMessageBuilder lexedBuilder; auto statements = lexedBuilder.initRoot(); lex(content, statements, *this); auto parsed = orphanage.newOrphan(); parseFile(statements.getStatements(), parsed.get(), *this); return parsed; } kj::Maybe importRelative(kj::StringPtr importPath) override { if (importPath.size() > 0 && importPath[0] == '/') { return loader.loadModuleFromSearchPath(kj::Path::parse(importPath.slice(1))); } else { return loader.loadModule(sourceDir, path.parent().eval(importPath)); } } kj::Maybe> embedRelative(kj::StringPtr embedPath) override { if (embedPath.size() > 0 && embedPath[0] == '/') { return loader.readEmbedFromSearchPath(kj::Path::parse(embedPath.slice(1))); } else { return loader.readEmbed(sourceDir, path.parent().eval(embedPath)); } } void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) override { auto& lines = *KJ_REQUIRE_NONNULL(lineBreaks, "Can't report errors until loadContent() is called."); loader.getErrorReporter().addError(sourceDir, path, lines.toSourcePos(startByte), lines.toSourcePos(endByte), message); } bool hadErrors() override { return loader.getErrorReporter().hadErrors(); } private: ModuleLoader::Impl& loader; kj::Own file; const kj::ReadableDirectory& sourceDir; kj::Path path; kj::String sourceNameStr; kj::SpaceFor lineBreaksSpace; kj::Maybe> lineBreaks; }; // ======================================================================================= kj::Maybe ModuleLoader::Impl::loadModule( const kj::ReadableDirectory& dir, kj::PathPtr path) { auto iter = modules.find(FileKey(dir, path)); if (iter != modules.end()) { // Return existing file. return *iter->second; } KJ_IF_MAYBE(file, dir.tryOpenFile(path)) { auto pathCopy = path.clone(); auto key = FileKey(dir, pathCopy, **file); auto module = kj::heap(*this, kj::mv(*file), dir, kj::mv(pathCopy)); auto& result = *module; auto insertResult = modules.insert(std::make_pair(key, kj::mv(module))); if (insertResult.second) { return result; } else { // Now that we have the file open, we noticed a collision. Return the old file. return *insertResult.first->second; } } else { // No such file. return nullptr; } } kj::Maybe ModuleLoader::Impl::loadModuleFromSearchPath(kj::PathPtr path) { for (auto candidate: searchPath) { KJ_IF_MAYBE(module, loadModule(*candidate, path)) { return *module; } } return nullptr; } kj::Maybe> ModuleLoader::Impl::readEmbed( const kj::ReadableDirectory& dir, kj::PathPtr path) { KJ_IF_MAYBE(file, dir.tryOpenFile(path)) { return file->get()->mmap(0, file->get()->stat().size); } return nullptr; } kj::Maybe> ModuleLoader::Impl::readEmbedFromSearchPath(kj::PathPtr path) { for (auto candidate: searchPath) { KJ_IF_MAYBE(module, readEmbed(*candidate, path)) { return kj::mv(*module); } } return nullptr; } // ======================================================================================= ModuleLoader::ModuleLoader(GlobalErrorReporter& errorReporter) : impl(kj::heap(errorReporter)) {} ModuleLoader::~ModuleLoader() noexcept(false) {} void ModuleLoader::addImportPath(const kj::ReadableDirectory& dir) { impl->addImportPath(dir); } kj::Maybe ModuleLoader::loadModule(const kj::ReadableDirectory& dir, kj::PathPtr path) { return impl->loadModule(dir, path); } } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/capnp.c++0000644000175000017500000021037313650101756022057 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "lexer.h" #include "parser.h" #include "compiler.h" #include "module-loader.h" #include "node-translator.h" #include #include #include #include #include #include #include "../message.h" #include #include #include #include #include #include #include #include #include #include #include #include #if _WIN32 #include #define WIN32_LEAN_AND_MEAN // ::eyeroll:: #include #include #undef VOID #undef CONST #else #include #endif #if HAVE_CONFIG_H #include "config.h" #endif #ifndef VERSION #define VERSION "(unknown)" #endif namespace capnp { namespace compiler { static const char VERSION_STRING[] = "Cap'n Proto version " VERSION; class CompilerMain final: public GlobalErrorReporter { public: explicit CompilerMain(kj::ProcessContext& context) : context(context), disk(kj::newDiskFilesystem()), loader(*this) {} kj::MainFunc getMain() { if (context.getProgramName().endsWith("capnpc") || context.getProgramName().endsWith("capnpc.exe")) { kj::MainBuilder builder(context, VERSION_STRING, "Compiles Cap'n Proto schema files and generates corresponding source code in one or " "more languages."); addGlobalOptions(builder); addCompileOptions(builder); builder.addOption({'i', "generate-id"}, KJ_BIND_METHOD(*this, generateId), "Generate a new 64-bit unique ID for use in a Cap'n Proto schema."); return builder.build(); } else { kj::MainBuilder builder(context, VERSION_STRING, "Command-line tool for Cap'n Proto development and debugging."); builder.addSubCommand("compile", KJ_BIND_METHOD(*this, getCompileMain), "Generate source code from schema files.") .addSubCommand("id", KJ_BIND_METHOD(*this, getGenIdMain), "Generate a new unique ID.") .addSubCommand("convert", KJ_BIND_METHOD(*this, getConvertMain), "Convert messages between binary, text, JSON, etc.") .addSubCommand("decode", KJ_BIND_METHOD(*this, getDecodeMain), "DEPRECATED (use `convert`)") .addSubCommand("encode", KJ_BIND_METHOD(*this, getEncodeMain), "DEPRECATED (use `convert`)") .addSubCommand("eval", KJ_BIND_METHOD(*this, getEvalMain), "Evaluate a const from a schema file."); addGlobalOptions(builder); return builder.build(); } } kj::MainFunc getCompileMain() { kj::MainBuilder builder(context, VERSION_STRING, "Compiles Cap'n Proto schema files and generates corresponding source code in one or " "more languages."); addGlobalOptions(builder); addCompileOptions(builder); return builder.build(); } kj::MainFunc getGenIdMain() { return kj::MainBuilder(context, VERSION_STRING, "Generates a new 64-bit unique ID for use in a Cap'n Proto schema.") .callAfterParsing(KJ_BIND_METHOD(*this, generateId)) .build(); } kj::MainFunc getConvertMain() { // Only parse the schemas we actually need for decoding. compileEagerness = Compiler::NODE; // Drop annotations since we don't need them. This avoids importing files like c++.capnp. annotationFlag = Compiler::DROP_ANNOTATIONS; kj::MainBuilder builder(context, VERSION_STRING, "Converts messages between formats. Reads a stream of messages from stdin in format " " and writes them to stdout in format . Valid formats are:\n" " binary standard binary format\n" " packed packed binary format (deflates zeroes)\n" " flat binary single segment, no segment table (rare)\n" " flat-packed flat and packed\n" " canonical canonicalized binary single segment, no segment table\n" " text schema language struct literal format\n" " json JSON format\n" "When using \"text\" or \"json\" format, you must specify and " "(but they are ignored and can be omitted for binary-to-binary conversions). " " names names a struct type defined in , which is the root type " "of the message(s)."); addGlobalOptions(builder); builder.addOption({"short"}, KJ_BIND_METHOD(*this, printShort), "Write text or JSON output in short (non-pretty) format. Each message will " "be printed on one line, without using whitespace to improve readability.") .addOptionWithArg({"segment-size"}, KJ_BIND_METHOD(*this, setSegmentSize), "", "For binary output, sets the preferred segment size on the MallocMessageBuilder to " "words and turns off heuristic growth. This flag is mainly useful " "for testing. Without it, each message will be written as a single " "segment.") .addOption({"quiet"}, KJ_BIND_METHOD(*this, setQuiet), "Do not print warning messages about the input being in the wrong format. " "Use this if you find the warnings are wrong (but also let us know so " "we can improve them).") .expectArg(":", KJ_BIND_METHOD(*this, setConversion)) .expectOptionalArg("", KJ_BIND_METHOD(*this, addSource)) .expectOptionalArg("", KJ_BIND_METHOD(*this, setRootType)) .callAfterParsing(KJ_BIND_METHOD(*this, convert)); return builder.build(); } kj::MainFunc getDecodeMain() { // Only parse the schemas we actually need for decoding. compileEagerness = Compiler::NODE; // Drop annotations since we don't need them. This avoids importing files like c++.capnp. annotationFlag = Compiler::DROP_ANNOTATIONS; kj::MainBuilder builder(context, VERSION_STRING, "Decodes one or more encoded Cap'n Proto messages as text. The messages have root " "type defined in . Messages are read from standard input and " "by default are expected to be in standard Cap'n Proto serialization format."); addGlobalOptions(builder); builder.addOption({"flat"}, KJ_BIND_METHOD(*this, codeFlat), "Interpret the input as one large single-segment message rather than a " "stream in standard serialization format. (Rarely used.)") .addOption({'p', "packed"}, KJ_BIND_METHOD(*this, codePacked), "Expect the input to be packed using standard Cap'n Proto packing, which " "deflates zero-valued bytes. (This reads messages written with " "capnp::writePackedMessage*() from . Do not use " "this for messages written with capnp::writeMessage*() from " ".)") .addOption({"short"}, KJ_BIND_METHOD(*this, printShort), "Print in short (non-pretty) format. Each message will be printed on one " "line, without using whitespace to improve readability.") .addOption({"quiet"}, KJ_BIND_METHOD(*this, setQuiet), "Do not print warning messages about the input being in the wrong format. " "Use this if you find the warnings are wrong (but also let us know so " "we can improve them).") .expectArg("", KJ_BIND_METHOD(*this, addSource)) .expectArg("", KJ_BIND_METHOD(*this, setRootType)) .callAfterParsing(KJ_BIND_METHOD(*this, decode)); return builder.build(); } kj::MainFunc getEncodeMain() { // Only parse the schemas we actually need for decoding. compileEagerness = Compiler::NODE; // Drop annotations since we don't need them. This avoids importing files like c++.capnp. annotationFlag = Compiler::DROP_ANNOTATIONS; kj::MainBuilder builder(context, VERSION_STRING, "Encodes one or more textual Cap'n Proto messages to binary. The messages have root " "type defined in . Messages are read from standard input. Each " "message is a parenthesized struct literal, like the format used to specify constants " "and default values of struct type in the schema language. For example:\n" " (foo = 123, bar = \"hello\", baz = [true, false, true])\n" "The input may contain any number of such values; each will be encoded as a separate " "message.", "Note that the current implementation reads the entire input into memory before " "beginning to encode. A better implementation would read and encode one message at " "a time."); addGlobalOptions(builder); builder.addOption({"flat"}, KJ_BIND_METHOD(*this, codeFlat), "Expect only one input value, serializing it as a single-segment message " "with no framing.") .addOption({'p', "packed"}, KJ_BIND_METHOD(*this, codePacked), "Pack the output message with standard Cap'n Proto packing, which " "deflates zero-valued bytes. (This writes messages using " "capnp::writePackedMessage() from . Without " "this, capnp::writeMessage() from is used.)") .addOptionWithArg({"segment-size"}, KJ_BIND_METHOD(*this, setSegmentSize), "", "Sets the preferred segment size on the MallocMessageBuilder to " "words and turns off heuristic growth. This flag is mainly useful " "for testing. Without it, each message will be written as a single " "segment.") .expectArg("", KJ_BIND_METHOD(*this, addSource)) .expectArg("", KJ_BIND_METHOD(*this, setRootType)) .callAfterParsing(KJ_BIND_METHOD(*this, encode)); return builder.build(); } kj::MainFunc getEvalMain() { // Only parse the schemas we actually need for decoding. compileEagerness = Compiler::NODE; // Drop annotations since we don't need them. This avoids importing files like c++.capnp. annotationFlag = Compiler::DROP_ANNOTATIONS; // Default convert to text unless -o is given. convertTo = Format::TEXT; kj::MainBuilder builder(context, VERSION_STRING, "Prints (or encodes) the value of , which must be defined in . " " must refer to a const declaration, a field of a struct type (prints the default " "value), or a field or list element nested within some other value. Examples:\n" " capnp eval myschema.capnp MyType.someField\n" " capnp eval myschema.capnp someConstant\n" " capnp eval myschema.capnp someConstant.someField\n" " capnp eval myschema.capnp someConstant.someList[4]\n" " capnp eval myschema.capnp someConstant.someList[4].anotherField[1][2][3]\n" "Since consts can have complex struct types, and since you can define a const using " "import and variable substitution, this can be a convenient way to write text-format " "config files which are compiled to binary before deployment.", "By default the value is written in text format and can have any type. The -b, -p, " "and --flat flags specify binary output, in which case the const must be of struct " "type."); addGlobalOptions(builder); builder.addOptionWithArg({'o', "output"}, KJ_BIND_METHOD(*this, setEvalOutputFormat), "", "Encode the output in the given format. See `capnp help convert` " "for a list of formats. Defaults to \"text\".") .addOption({'b', "binary"}, KJ_BIND_METHOD(*this, codeBinary), "same as -obinary") .addOption({"flat"}, KJ_BIND_METHOD(*this, codeFlat), "same as -oflat") .addOption({'p', "packed"}, KJ_BIND_METHOD(*this, codePacked), "same as -opacked") .addOption({"short"}, KJ_BIND_METHOD(*this, printShort), "If output format is text or JSON, write in short (non-pretty) format. The " "message will be printed on one line, without using whitespace to improve " "readability.") .expectArg("", KJ_BIND_METHOD(*this, addSource)) .expectArg("", KJ_BIND_METHOD(*this, evalConst)); return builder.build(); } void addGlobalOptions(kj::MainBuilder& builder) { builder.addOptionWithArg({'I', "import-path"}, KJ_BIND_METHOD(*this, addImportPath), "", "Add to the list of directories searched for non-relative " "imports (ones that start with a '/').") .addOption({"no-standard-import"}, KJ_BIND_METHOD(*this, noStandardImport), "Do not add any default import paths; use only those specified by -I. " "Otherwise, typically /usr/include and /usr/local/include are added by " "default."); } void addCompileOptions(kj::MainBuilder& builder) { builder.addOptionWithArg({'o', "output"}, KJ_BIND_METHOD(*this, addOutput), "[:]", "Generate source code for language in directory " "(default: current directory). actually specifies a plugin " "to use. If is a simple word, the compiler searches for a plugin " "called 'capnpc-' in $PATH. If is a file path " "containing slashes, it is interpreted as the exact plugin " "executable file name, and $PATH is not searched. If is '-', " "the compiler dumps the request to standard output.") .addOptionWithArg({"src-prefix"}, KJ_BIND_METHOD(*this, addSourcePrefix), "", "If a file specified for compilation starts with , remove " "the prefix for the purpose of deciding the names of output files. " "For example, the following command:\n" " capnp compile --src-prefix=foo/bar -oc++:corge foo/bar/baz/qux.capnp\n" "would generate the files corge/baz/qux.capnp.{h,c++}.") .expectOneOrMoreArgs("", KJ_BIND_METHOD(*this, addSource)) .callAfterParsing(KJ_BIND_METHOD(*this, generateOutput)); } // ===================================================================================== // shared options kj::MainBuilder::Validity addImportPath(kj::StringPtr path) { KJ_IF_MAYBE(dir, getSourceDirectory(path, false)) { loader.addImportPath(*dir); return true; } else { return "no such directory"; } } kj::MainBuilder::Validity noStandardImport() { addStandardImportPaths = false; return true; } kj::MainBuilder::Validity addSource(kj::StringPtr file) { if (!compilerConstructed) { compiler = compilerSpace.construct(annotationFlag); compilerConstructed = true; } if (addStandardImportPaths) { static constexpr kj::StringPtr STANDARD_IMPORT_PATHS[] = { "/usr/local/include"_kj, "/usr/include"_kj, #ifdef CAPNP_INCLUDE_DIR KJ_CONCAT(CAPNP_INCLUDE_DIR, _kj), #endif }; for (auto path: STANDARD_IMPORT_PATHS) { KJ_IF_MAYBE(dir, getSourceDirectory(path, false)) { loader.addImportPath(*dir); } else { // ignore standard path that doesn't exist } } addStandardImportPaths = false; } auto dirPathPair = interpretSourceFile(file); KJ_IF_MAYBE(module, loader.loadModule(dirPathPair.dir, dirPathPair.path)) { uint64_t id = compiler->add(*module); compiler->eagerlyCompile(id, compileEagerness); sourceFiles.add(SourceFile { id, module->getSourceName(), &*module }); } else { return "no such file"; } return true; } public: // ===================================================================================== // "id" command kj::MainBuilder::Validity generateId() { context.exitInfo(kj::str("@0x", kj::hex(generateRandomId()))); KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT; } // ===================================================================================== // "compile" command kj::MainBuilder::Validity addOutput(kj::StringPtr spec) { KJ_IF_MAYBE(split, spec.findFirst(':')) { kj::StringPtr dir = spec.slice(*split + 1); auto plugin = spec.slice(0, *split); if (*split == 1 && (dir.startsWith("/") || dir.startsWith("\\"))) { // The colon is the second character and is immediately followed by a slash or backslash. // So, the user passed something like `-o c:/foo`. Is this a request to run the C plugin // and output to `/foo`? Or are we on Windows, and is this a request to run the plugin // `c:/foo`? KJ_IF_MAYBE(split2, dir.findFirst(':')) { // There are two colons. The first ':' was the second char, and was followed by '/' or // '\', e.g.: // capnp compile -o c:/foo.exe:bar // // In this case we can conclude that the second colon is actually meant to be the // plugin/location separator, and the first colon was simply signifying a drive letter. // // Proof by contradiction: // - Say that none of the colons were meant to be plugin/location separators; i.e. the // whole argument is meant to be a plugin indicator and the location defaults to ".". // -> In this case, the plugin path has two colons, which is not valid. // -> CONTRADICTION // - Say that the first colon was meant to be the plugin/location separator. // -> In this case, the second colon must be the drive letter separator for the // output location. // -> However, the output location begins with '/' or '\', which is not a drive letter. // -> CONTRADICTION // - Say that there are more colons beyond the first two, and one of these is meant to // be the plugin/location separator. // -> In this case, the plugin path has two or more colons, which is not valid. // -> CONTRADICTION // // We therefore conclude that the *second* colon is in fact the plugin/location separator. dir = dir.slice(*split2 + 1); plugin = spec.slice(0, *split2 + 2); #if _WIN32 } else { // The user wrote something like: // // capnp compile -o c:/foo/bar // // What does this mean? It depends on what system we're on. On a Unix system, the above // clearly is a request to run the `capnpc-c` plugin (perhaps to output C code) and write // to the directory /foo/bar. But on Windows, absolute paths do not start with '/', and // the above is actually a request to run the plugin `c:/foo/bar`, outputting to the // current directory. outputs.add(OutputDirective { spec.asArray(), nullptr }); return true; #endif } } struct stat stats; if (stat(dir.cStr(), &stats) < 0 || !S_ISDIR(stats.st_mode)) { return "output location is inaccessible or is not a directory"; } outputs.add(OutputDirective { plugin, disk->getCurrentPath().evalNative(dir) }); } else { outputs.add(OutputDirective { spec.asArray(), nullptr }); } return true; } kj::MainBuilder::Validity addSourcePrefix(kj::StringPtr prefix) { if (getSourceDirectory(prefix, true) == nullptr) { return "no such directory"; } else { return true; } } kj::MainBuilder::Validity generateOutput() { if (hadErrors()) { // Skip output if we had any errors. return true; } // We require one or more sources and if they failed to compile we quit above, so this should // pass. (This assertion also guarantees that `compiler` has been initialized.) KJ_ASSERT(sourceFiles.size() > 0, "Shouldn't have gotten here without sources."); if (outputs.size() == 0) { return "no outputs specified"; } MallocMessageBuilder message; auto request = message.initRoot(); auto version = request.getCapnpVersion(); version.setMajor(CAPNP_VERSION_MAJOR); version.setMinor(CAPNP_VERSION_MINOR); version.setMicro(CAPNP_VERSION_MICRO); auto schemas = compiler->getLoader().getAllLoaded(); auto nodes = request.initNodes(schemas.size()); for (size_t i = 0; i < schemas.size(); i++) { nodes.setWithCaveats(i, schemas[i].getProto()); } request.adoptSourceInfo(compiler->getAllSourceInfo(message.getOrphanage())); auto requestedFiles = request.initRequestedFiles(sourceFiles.size()); for (size_t i = 0; i < sourceFiles.size(); i++) { auto requestedFile = requestedFiles[i]; requestedFile.setId(sourceFiles[i].id); requestedFile.setFilename(sourceFiles[i].name); requestedFile.adoptImports(compiler->getFileImportTable( *sourceFiles[i].module, Orphanage::getForMessageContaining(requestedFile))); } for (auto& output: outputs) { if (kj::str(output.name) == "-") { writeMessageToFd(STDOUT_FILENO, message); continue; } int pipeFds[2]; KJ_SYSCALL(kj::miniposix::pipe(pipeFds)); kj::String exeName; bool shouldSearchPath = true; for (char c: output.name) { #if _WIN32 if (c == '/' || c == '\\') { #else if (c == '/') { #endif shouldSearchPath = false; break; } } if (shouldSearchPath) { exeName = kj::str("capnpc-", output.name); } else { exeName = kj::heapString(output.name); } kj::Array pwd = kj::heapArray(256); while (getcwd(pwd.begin(), pwd.size()) == nullptr) { KJ_REQUIRE(pwd.size() < 8192, "WTF your working directory path is more than 8k?"); pwd = kj::heapArray(pwd.size() * 2); } #if _WIN32 int oldStdin; KJ_SYSCALL(oldStdin = dup(STDIN_FILENO)); intptr_t child; #else // _WIN32 pid_t child; KJ_SYSCALL(child = fork()); if (child == 0) { // I am the child! KJ_SYSCALL(close(pipeFds[1])); #endif // _WIN32, else KJ_SYSCALL(dup2(pipeFds[0], STDIN_FILENO)); KJ_SYSCALL(close(pipeFds[0])); KJ_IF_MAYBE(d, output.dir) { #if _WIN32 KJ_SYSCALL(SetCurrentDirectoryW(d->forWin32Api(true).begin()), d->toWin32String(true)); #else auto wd = d->toString(true); KJ_SYSCALL(chdir(wd.cStr()), wd); KJ_SYSCALL(setenv("PWD", wd.cStr(), true)); #endif } #if _WIN32 // MSVCRT's spawn*() don't correctly escape arguments, which is necessary on Windows // since the underlying system call takes a single command line string rather than // an arg list. We do the escaping ourselves by wrapping the name in quotes. We know // that exeName itself can't contain quotes (since filenames aren't allowed to contain // quotes on Windows), so we don't have to account for those. KJ_ASSERT(exeName.findFirst('\"') == nullptr, "Windows filenames can't contain quotes", exeName); auto escapedExeName = kj::str("\"", exeName, "\""); #endif if (shouldSearchPath) { #if _WIN32 child = _spawnlp(_P_NOWAIT, exeName.cStr(), escapedExeName.cStr(), nullptr); #else execlp(exeName.cStr(), exeName.cStr(), nullptr); #endif } else { #if _WIN32 if (!exeName.startsWith("/") && !exeName.startsWith("\\") && !(exeName.size() >= 2 && exeName[1] == ':')) { #else if (!exeName.startsWith("/")) { #endif // The name is relative. Prefix it with our original working directory path. exeName = kj::str(pwd.begin(), "/", exeName); } #if _WIN32 child = _spawnl(_P_NOWAIT, exeName.cStr(), escapedExeName.cStr(), nullptr); #else execl(exeName.cStr(), exeName.cStr(), nullptr); #endif } #if _WIN32 if (child == -1) { #endif int error = errno; if (error == ENOENT) { context.exitError(kj::str(output.name, ": no such plugin (executable should be '", exeName, "')")); } else { #if _WIN32 KJ_FAIL_SYSCALL("spawn()", error); #else KJ_FAIL_SYSCALL("exec()", error); #endif } #if _WIN32 } // Restore stdin. KJ_SYSCALL(dup2(oldStdin, STDIN_FILENO)); KJ_SYSCALL(close(oldStdin)); // Restore current directory. KJ_SYSCALL(chdir(pwd.begin()), pwd.begin()); #else // _WIN32 } KJ_SYSCALL(close(pipeFds[0])); #endif // _WIN32, else writeMessageToFd(pipeFds[1], message); KJ_SYSCALL(close(pipeFds[1])); #if _WIN32 int status; if (_cwait(&status, child, 0) == -1) { KJ_FAIL_SYSCALL("_cwait()", errno); } if (status != 0) { context.error(kj::str(output.name, ": plugin failed: exit code ", status)); } #else // _WIN32 int status; KJ_SYSCALL(waitpid(child, &status, 0)); if (WIFSIGNALED(status)) { context.error(kj::str(output.name, ": plugin failed: ", strsignal(WTERMSIG(status)))); } else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { context.error(kj::str(output.name, ": plugin failed: exit code ", WEXITSTATUS(status))); } #endif // _WIN32, else } return true; } // ===================================================================================== // "convert" command private: enum class Format { BINARY, PACKED, FLAT, FLAT_PACKED, CANONICAL, TEXT, JSON }; kj::Maybe parseFormatName(kj::StringPtr name) { if (name == "binary" ) return Format::BINARY; if (name == "packed" ) return Format::PACKED; if (name == "flat" ) return Format::FLAT; if (name == "flat-packed") return Format::FLAT_PACKED; if (name == "canonical" ) return Format::CANONICAL; if (name == "text" ) return Format::TEXT; if (name == "json" ) return Format::JSON; return nullptr; } kj::StringPtr toString(Format format) { switch (format) { case Format::BINARY : return "binary"; case Format::PACKED : return "packed"; case Format::FLAT : return "flat"; case Format::FLAT_PACKED: return "flat-packed"; case Format::CANONICAL : return "canonical"; case Format::TEXT : return "text"; case Format::JSON : return "json"; } KJ_UNREACHABLE; } Format formatFromDeprecatedFlags(Format defaultFormat) { // For deprecated commands "decode" and "encode". if (flat) { if (packed) { return Format::FLAT_PACKED; } else { return Format::FLAT; } } if (packed) { return Format::PACKED; } else if (binary) { return Format::BINARY; } else { return defaultFormat; } } kj::MainBuilder::Validity verifyRequirements(Format format) { if ((format == Format::TEXT || format == Format::JSON) && rootType == StructSchema()) { return kj::str("format requires schema: ", toString(format)); } else { return true; } } public: kj::MainBuilder::Validity setConversion(kj::StringPtr conversion) { KJ_IF_MAYBE(colon, conversion.findFirst(':')) { auto from = kj::str(conversion.slice(0, *colon)); auto to = conversion.slice(*colon + 1); KJ_IF_MAYBE(f, parseFormatName(from)) { convertFrom = *f; } else { return kj::str("unknown format: ", from); } KJ_IF_MAYBE(t, parseFormatName(to)) { convertTo = *t; } else { return kj::str("unknown format: ", to); } if (convertFrom == Format::JSON || convertTo == Format::JSON) { // We need annotations to process JSON. // TODO(someday): Find a way that we can process annotations from json.capnp without // requiring other annotation-only imports like c++.capnp annotationFlag = Compiler::COMPILE_ANNOTATIONS; } return true; } else { return "invalid conversion, format is: :"; } } kj::MainBuilder::Validity convert() { { auto result = verifyRequirements(convertFrom); if (result.getError() != nullptr) return result; } { auto result = verifyRequirements(convertTo); if (result.getError() != nullptr) return result; } kj::FdInputStream rawInput(STDIN_FILENO); kj::BufferedInputStreamWrapper input(rawInput); kj::FdOutputStream output(STDOUT_FILENO); if (!quiet) { auto result = checkPlausibility(convertFrom, input.getReadBuffer()); if (result.getError() != nullptr) { return kj::mv(result); } } while (input.tryGetReadBuffer().size() > 0) { readOneAndConvert(input, output); } context.exit(); KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT; } private: kj::Vector readAll(kj::BufferedInputStreamWrapper& input) { kj::Vector allBytes; for (;;) { auto buffer = input.tryGetReadBuffer(); if (buffer.size() == 0) break; allBytes.addAll(buffer); input.skip(buffer.size()); } return allBytes; } kj::String readOneText(kj::BufferedInputStreamWrapper& input) { // Consume and return one parentheses-delimited message from the input. // // Accounts for nested parentheses, comments, and string literals. enum { NORMAL, COMMENT, QUOTE, QUOTE_ESCAPE, DQUOTE, DQUOTE_ESCAPE } state = NORMAL; uint depth = 0; bool sawClose = false; kj::Vector chars; for (;;) { auto buffer = input.tryGetReadBuffer(); if (buffer == nullptr) { // EOF chars.add('\0'); return kj::String(chars.releaseAsArray()); } for (auto i: kj::indices(buffer)) { char c = buffer[i]; switch (state) { case NORMAL: switch (c) { case '#': state = COMMENT; break; case '(': if (depth == 0 && sawClose) { // We already got one complete message. This is the start of the next message. // Stop here. chars.addAll(buffer.slice(0, i)); chars.add('\0'); input.skip(i); return kj::String(chars.releaseAsArray()); } ++depth; break; case ')': if (depth > 0) { if (--depth == 0) { sawClose = true; } } break; default: break; } break; case COMMENT: switch (c) { case '\n': state = NORMAL; break; default: break; } break; case QUOTE: switch (c) { case '\'': state = NORMAL; break; case '\\': state = QUOTE_ESCAPE; break; default: break; } break; case QUOTE_ESCAPE: break; case DQUOTE: switch (c) { case '\"': state = NORMAL; break; case '\\': state = DQUOTE_ESCAPE; break; default: break; } break; case DQUOTE_ESCAPE: break; } } chars.addAll(buffer); input.skip(buffer.size()); } } kj::String readOneJson(kj::BufferedInputStreamWrapper& input) { // Consume and return one brace-delimited message from the input. // // Accounts for nested braces, string literals, and comments starting with # or //. Technically // JSON does not permit comments but this code is lenient in case we change things later. enum { NORMAL, SLASH, COMMENT, QUOTE, QUOTE_ESCAPE, DQUOTE, DQUOTE_ESCAPE } state = NORMAL; uint depth = 0; bool sawClose = false; kj::Vector chars; for (;;) { auto buffer = input.tryGetReadBuffer(); if (buffer == nullptr) { // EOF chars.add('\0'); return kj::String(chars.releaseAsArray()); } for (auto i: kj::indices(buffer)) { char c = buffer[i]; switch (state) { case SLASH: if (c == '/') { state = COMMENT; break; } // fallthrough case NORMAL: switch (c) { case '#': state = COMMENT; break; case '/': state = SLASH; break; case '{': if (depth == 0 && sawClose) { // We already got one complete message. This is the start of the next message. // Stop here. chars.addAll(buffer.slice(0, i)); chars.add('\0'); input.skip(i); return kj::String(chars.releaseAsArray()); } ++depth; break; case '}': if (depth > 0) { if (--depth == 0) { sawClose = true; } } break; default: break; } break; case COMMENT: switch (c) { case '\n': state = NORMAL; break; default: break; } break; case QUOTE: switch (c) { case '\'': state = NORMAL; break; case '\\': state = QUOTE_ESCAPE; break; default: break; } break; case QUOTE_ESCAPE: break; case DQUOTE: switch (c) { case '\"': state = NORMAL; break; case '\\': state = DQUOTE_ESCAPE; break; default: break; } break; case DQUOTE_ESCAPE: break; } } chars.addAll(buffer); input.skip(buffer.size()); } } class ParseErrorCatcher: public kj::ExceptionCallback { public: ParseErrorCatcher(kj::ProcessContext& context): context(context) {} ~ParseErrorCatcher() noexcept(false) { if (!unwindDetector.isUnwinding()) { KJ_IF_MAYBE(e, exception) { context.error(kj::str( "*** ERROR CONVERTING PREVIOUS MESSAGE ***\n" "The following error occurred while converting the message above.\n" "This probably means the input data is invalid/corrupted.\n", "Exception description: ", e->getDescription(), "\n" "Code location: ", e->getFile(), ":", e->getLine(), "\n" "*** END ERROR ***")); } } } void onRecoverableException(kj::Exception&& e) { // Only capture the first exception, on the assumption that later exceptions are probably // just cascading problems. if (exception == nullptr) { exception = kj::mv(e); } } private: kj::ProcessContext& context; kj::Maybe exception; kj::UnwindDetector unwindDetector; }; void readOneAndConvert(kj::BufferedInputStreamWrapper& input, kj::OutputStream& output) { // Since this is a debug tool, lift the usual security limits. Worse case is the process // crashes or has to be killed. ReaderOptions options; options.nestingLimit = kj::maxValue; options.traversalLimitInWords = kj::maxValue; ParseErrorCatcher parseErrorCatcher(context); switch (convertFrom) { case Format::BINARY: { capnp::InputStreamMessageReader message(input, options); return writeConversion(message.getRoot(), output); } case Format::PACKED: { capnp::PackedMessageReader message(input, options); return writeConversion(message.getRoot(), output); } case Format::FLAT: case Format::CANONICAL: { auto allBytes = readAll(input); // Technically we don't know if the bytes are aligned so we'd better copy them to a new // array. Note that if we have a non-whole number of words we chop off the straggler // bytes. This is fine because if those bytes are actually part of the message we will // hit an error later and if they are not then who cares? auto words = kj::heapArray(allBytes.size() / sizeof(word)); memcpy(words.begin(), allBytes.begin(), words.size() * sizeof(word)); kj::ArrayPtr segments[1] = { words }; SegmentArrayMessageReader message(segments, options); if (convertFrom == Format::CANONICAL) { KJ_REQUIRE(message.isCanonical()); } return writeConversion(message.getRoot(), output); } case Format::FLAT_PACKED: { auto allBytes = readAll(input); auto words = kj::heapArray(computeUnpackedSizeInWords(allBytes)); kj::ArrayInputStream input(allBytes); capnp::_::PackedInputStream unpacker(input); unpacker.read(words.asBytes().begin(), words.asBytes().size()); word dummy; KJ_ASSERT(unpacker.tryRead(&dummy, sizeof(dummy), sizeof(dummy)) == 0); kj::ArrayPtr segments[1] = { words }; SegmentArrayMessageReader message(segments, options); return writeConversion(message.getRoot(), output); } case Format::TEXT: { auto text = readOneText(input); MallocMessageBuilder message; TextCodec codec; codec.setPrettyPrint(pretty); auto root = message.initRoot(rootType); codec.decode(text, root); return writeConversion(root.asReader(), output); } case Format::JSON: { auto text = readOneJson(input); MallocMessageBuilder message; JsonCodec codec; codec.setPrettyPrint(pretty); codec.handleByAnnotation(rootType); auto root = message.initRoot(rootType); codec.decode(text, root); return writeConversion(root.asReader(), output); } } KJ_UNREACHABLE; } void writeConversion(AnyStruct::Reader reader, kj::OutputStream& output) { switch (convertTo) { case Format::BINARY: { MallocMessageBuilder message( segmentSize == 0 ? SUGGESTED_FIRST_SEGMENT_WORDS : segmentSize, segmentSize == 0 ? SUGGESTED_ALLOCATION_STRATEGY : AllocationStrategy::FIXED_SIZE); message.setRoot(reader); capnp::writeMessage(output, message); return; } case Format::PACKED: { MallocMessageBuilder message( segmentSize == 0 ? SUGGESTED_FIRST_SEGMENT_WORDS : segmentSize, segmentSize == 0 ? SUGGESTED_ALLOCATION_STRATEGY : AllocationStrategy::FIXED_SIZE); message.setRoot(reader); capnp::writePackedMessage(output, message); return; } case Format::FLAT: { auto words = kj::heapArray(reader.totalSize().wordCount + 1); memset(words.begin(), 0, words.asBytes().size()); copyToUnchecked(reader, words); output.write(words.begin(), words.asBytes().size()); return; } case Format::FLAT_PACKED: { auto words = kj::heapArray(reader.totalSize().wordCount + 1); memset(words.begin(), 0, words.asBytes().size()); copyToUnchecked(reader, words); kj::BufferedOutputStreamWrapper buffered(output); capnp::_::PackedOutputStream packed(buffered); packed.write(words.begin(), words.asBytes().size()); return; } case Format::CANONICAL: { auto words = reader.canonicalize(); output.write(words.begin(), words.asBytes().size()); return; } case Format::TEXT: { TextCodec codec; codec.setPrettyPrint(pretty); auto text = codec.encode(reader.as(rootType)); output.write({text.asBytes(), kj::StringPtr("\n").asBytes()}); return; } case Format::JSON: { JsonCodec codec; codec.setPrettyPrint(pretty); codec.handleByAnnotation(rootType); auto text = codec.encode(reader.as(rootType)); output.write({text.asBytes(), kj::StringPtr("\n").asBytes()}); return; } } KJ_UNREACHABLE; } public: // ===================================================================================== // "decode" command kj::MainBuilder::Validity codeBinary() { if (packed) return "cannot be used with --packed"; if (flat) return "cannot be used with --flat"; binary = true; return true; } kj::MainBuilder::Validity codeFlat() { if (binary) return "cannot be used with --binary"; flat = true; return true; } kj::MainBuilder::Validity codePacked() { if (binary) return "cannot be used with --binary"; packed = true; return true; } kj::MainBuilder::Validity printShort() { pretty = false; return true; } kj::MainBuilder::Validity setQuiet() { quiet = true; return true; } kj::MainBuilder::Validity setSegmentSize(kj::StringPtr size) { if (flat) return "cannot be used with --flat"; char* end; segmentSize = strtol(size.cStr(), &end, 0); if (size.size() == 0 || *end != '\0') { return "not an integer"; } return true; } kj::MainBuilder::Validity setRootType(kj::StringPtr type) { KJ_ASSERT(sourceFiles.size() == 1); KJ_IF_MAYBE(schema, resolveName(sourceFiles[0].id, type)) { if (schema->getProto().which() != schema::Node::STRUCT) { return "not a struct type"; } rootType = schema->asStruct(); return true; } else { return "no such type"; } } private: kj::Maybe resolveName(uint64_t scopeId, kj::StringPtr name) { while (name.size() > 0) { kj::String temp; kj::StringPtr part; KJ_IF_MAYBE(dotpos, name.findFirst('.')) { temp = kj::heapString(name.slice(0, *dotpos)); part = temp; name = name.slice(*dotpos + 1); } else { part = name; name = nullptr; } KJ_IF_MAYBE(childId, compiler->lookup(scopeId, part)) { scopeId = *childId; } else { return nullptr; } } return compiler->getLoader().get(scopeId); } public: kj::MainBuilder::Validity decode() { convertTo = Format::TEXT; convertFrom = formatFromDeprecatedFlags(Format::BINARY); return convert(); } private: enum Plausibility { IMPOSSIBLE, IMPLAUSIBLE, WRONG_TYPE, PLAUSIBLE }; bool plausibleOrWrongType(Plausibility p) { return p == PLAUSIBLE || p == WRONG_TYPE; } Plausibility isPlausiblyFlat(kj::ArrayPtr prefix, uint segmentCount = 1) { if (prefix.size() < 8) { // Not enough prefix to say. return PLAUSIBLE; } if ((prefix[0] & 3) == 2) { // Far pointer. Verify the segment ID. uint32_t segmentId = prefix[4] | (prefix[5] << 8) | (prefix[6] << 16) | (prefix[7] << 24); if (segmentId == 0 || segmentId >= segmentCount) { return IMPOSSIBLE; } else { return PLAUSIBLE; } } if ((prefix[0] & 3) != 0) { // Not a struct pointer. return IMPOSSIBLE; } if ((prefix[3] & 0x80) != 0) { // Offset is negative (invalid). return IMPOSSIBLE; } if ((prefix[3] & 0xe0) != 0) { // Offset is over a gigabyte (implausible). return IMPLAUSIBLE; } uint data = prefix[4] | (prefix[5] << 8); uint pointers = prefix[6] | (prefix[7] << 8); if (data + pointers > 2048) { // Root struct is huge (over 16 KiB). return IMPLAUSIBLE; } auto structSchema = rootType.getProto().getStruct(); if ((data < structSchema.getDataWordCount() && pointers > structSchema.getPointerCount()) || (data > structSchema.getDataWordCount() && pointers < structSchema.getPointerCount())) { // Struct is neither older nor newer than the schema. return WRONG_TYPE; } if (data > structSchema.getDataWordCount() && data - structSchema.getDataWordCount() > 128) { // Data section appears to have grown by 1k (128 words). This seems implausible. return WRONG_TYPE; } if (pointers > structSchema.getPointerCount() && pointers - structSchema.getPointerCount() > 128) { // Pointer section appears to have grown by 1k (128 words). This seems implausible. return WRONG_TYPE; } return PLAUSIBLE; } Plausibility isPlausiblyBinary(kj::ArrayPtr prefix) { if (prefix.size() < 8) { // Not enough prefix to say. return PLAUSIBLE; } uint32_t segmentCount = prefix[0] | (prefix[1] << 8) | (prefix[2] << 16) | (prefix[3] << 24); // Actually, the bytes store segmentCount - 1. ++segmentCount; if (segmentCount > 65536) { // While technically possible, this is so implausible that we should mark it impossible. // This helps to make sure we fail fast on packed input. return IMPOSSIBLE; } else if (segmentCount > 256) { // Implausible segment count. return IMPLAUSIBLE; } uint32_t segment0Size = prefix[4] | (prefix[5] << 8) | (prefix[6] << 16) | (prefix[7] << 24); if (segment0Size > (1 << 27)) { // Segment larger than 1G seems implausible. return IMPLAUSIBLE; } uint32_t segment0Offset = 4 + segmentCount * 4; if (segment0Offset % 8 != 0) { segment0Offset += 4; } KJ_ASSERT(segment0Offset % 8 == 0); if (prefix.size() < segment0Offset + 8) { // Segment 0 is past our prefix, so we can't check it. return PLAUSIBLE; } return isPlausiblyFlat(prefix.slice(segment0Offset, prefix.size()), segmentCount); } Plausibility isPlausiblyPacked(kj::ArrayPtr prefix, kj::Function)> checkUnpacked) { kj::Vector unpacked; // Try to unpack a prefix so that we can check it. const byte* pos = prefix.begin(); const byte* end = prefix.end(); if (end - pos > 1024) { // Don't bother unpacking more than 1k. end = pos + 1024; } while (pos < end) { byte tag = *pos++; for (uint i = 0; i < 8 && pos < end; i++) { if (tag & (1 << i)) { byte b = *pos++; if (b == 0) { // A zero byte should have been deflated away. return IMPOSSIBLE; } unpacked.add(b); } else { unpacked.add(0); } } if (pos == end) { break; } if (tag == 0) { uint count = *pos++ * 8; unpacked.addAll(kj::repeat(byte(0), count)); } else if (tag == 0xff) { uint count = *pos++ * 8; size_t available = end - pos; uint n = kj::min(count, available); unpacked.addAll(pos, pos + n); pos += n; } } return checkUnpacked(unpacked); } Plausibility isPlausiblyPacked(kj::ArrayPtr prefix) { return isPlausiblyPacked(prefix, KJ_BIND_METHOD(*this, isPlausiblyBinary)); } Plausibility isPlausiblyPackedFlat(kj::ArrayPtr prefix) { return isPlausiblyPacked(prefix, [this](kj::ArrayPtr prefix) { return isPlausiblyFlat(prefix); }); } Plausibility isPlausiblyText(kj::ArrayPtr prefix) { enum { PREAMBLE, COMMENT, BODY } state = PREAMBLE; for (char c: prefix.asChars()) { switch (state) { case PREAMBLE: // Before opening parenthesis. switch (c) { case '(': state = BODY; continue; case '#': state = COMMENT; continue; case ' ': case '\n': case '\r': case '\t': case '\v': // whitespace break; default: // Not whitespace, not comment, not open parenthesis. Impossible! return IMPOSSIBLE; } break; case COMMENT: switch (c) { case '\n': state = PREAMBLE; continue; default: break; } break; case BODY: switch (c) { case '\"': case '\'': // String literal. Let's stop here before things get complicated. return PLAUSIBLE; default: break; } break; } if ((static_cast(c) < 0x20 && c != '\n' && c != '\r' && c != '\t' && c != '\v') || c == 0x7f) { // Unprintable character. return IMPOSSIBLE; } } return PLAUSIBLE; } Plausibility isPlausiblyJson(kj::ArrayPtr prefix) { enum { PREAMBLE, COMMENT, BODY } state = PREAMBLE; for (char c: prefix.asChars()) { switch (state) { case PREAMBLE: // Before opening parenthesis. switch (c) { case '{': state = BODY; continue; case '#': state = COMMENT; continue; case '/': state = COMMENT; continue; case ' ': case '\n': case '\r': case '\t': case '\v': // whitespace break; default: // Not whitespace, not comment, not open brace. Impossible! return IMPOSSIBLE; } break; case COMMENT: switch (c) { case '\n': state = PREAMBLE; continue; default: break; } break; case BODY: switch (c) { case '\"': // String literal. Let's stop here before things get complicated. return PLAUSIBLE; default: break; } break; } if ((c > 0 && c < ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\v') || c == 0x7f) { // Unprintable character. return IMPOSSIBLE; } } return PLAUSIBLE; } Plausibility isPlausibly(Format format, kj::ArrayPtr prefix) { switch (format) { case Format::BINARY : return isPlausiblyBinary (prefix); case Format::PACKED : return isPlausiblyPacked (prefix); case Format::FLAT : return isPlausiblyFlat (prefix); case Format::FLAT_PACKED: return isPlausiblyPackedFlat(prefix); case Format::CANONICAL : return isPlausiblyFlat (prefix); case Format::TEXT : return isPlausiblyText (prefix); case Format::JSON : return isPlausiblyJson (prefix); } KJ_UNREACHABLE; } kj::Maybe guessFormat(kj::ArrayPtr prefix) { Format candidates[] = { Format::BINARY, Format::TEXT, Format::PACKED, Format::JSON, Format::FLAT, Format::FLAT_PACKED }; for (Format candidate: candidates) { if (plausibleOrWrongType(isPlausibly(candidate, prefix))) { return candidate; } } return nullptr; } kj::MainBuilder::Validity checkPlausibility(Format format, kj::ArrayPtr prefix) { switch (isPlausibly(format, prefix)) { case PLAUSIBLE: return true; case IMPOSSIBLE: KJ_IF_MAYBE(guess, guessFormat(prefix)) { return kj::str( "The input is not in \"", toString(format), "\" format. It looks like it is in \"", toString(*guess), "\" format. Try that instead."); } else { return kj::str( "The input is not in \"", toString(format), "\" format."); } case IMPLAUSIBLE: KJ_IF_MAYBE(guess, guessFormat(prefix)) { context.warning(kj::str( "*** WARNING ***\n" "The input data does not appear to be in \"", toString(format), "\" format. It\n" "looks like it may be in \"", toString(*guess), "\" format. I'll try to parse\n" "it in \"", toString(format), "\" format as you requested, but if it doesn't work,\n" "try \"", toString(format), "\" instead. Use --quiet to suppress this warning.\n" "*** END WARNING ***\n")); } else { context.warning(kj::str( "*** WARNING ***\n" "The input data does not appear to be in \"", toString(format), "\" format, nor\n" "in any other known format. I'll try to parse it in \"", toString(format), "\"\n" "format anyway, as you requested. Use --quiet to suppress this warning.\n" "*** END WARNING ***\n")); } return true; case WRONG_TYPE: if (format == Format::FLAT && plausibleOrWrongType(isPlausiblyBinary(prefix))) { context.warning( "*** WARNING ***\n" "The input data does not appear to match the schema that you specified. I'll try\n" "to parse it anyway, but if it doesn't look right, please verify that you\n" "have the right schema. This could also be because the input is not in \"flat\"\n" "format; indeed, it looks like this input may be in regular binary format,\n" "so you might want to try \"binary\" instead. Use --quiet to suppress this\n" "warning.\n" "*** END WARNING ***\n"); } else if (format == Format::FLAT_PACKED && plausibleOrWrongType(isPlausiblyPacked(prefix))) { context.warning( "*** WARNING ***\n" "The input data does not appear to match the schema that you specified. I'll try\n" "to parse it anyway, but if it doesn't look right, please verify that you\n" "have the right schema. This could also be because the input is not in \"flat-packed\"\n" "format; indeed, it looks like this input may be in regular packed format,\n" "so you might want to try \"packed\" instead. Use --quiet to suppress this\n" "warning.\n" "*** END WARNING ***\n"); } else { context.warning( "*** WARNING ***\n" "The input data does not appear to be the type that you specified. I'll try\n" "to parse it anyway, but if it doesn't look right, please verify that you\n" "have the right type. Use --quiet to suppress this warning.\n" "*** END WARNING ***\n"); } return true; } KJ_UNREACHABLE; } public: // ----------------------------------------------------------------- kj::MainBuilder::Validity encode() { convertFrom = Format::TEXT; convertTo = formatFromDeprecatedFlags(Format::BINARY); return convert(); } kj::MainBuilder::Validity setEvalOutputFormat(kj::StringPtr format) { KJ_IF_MAYBE(f, parseFormatName(format)) { convertTo = *f; return true; } else { return kj::str("unknown format: ", format); } } kj::MainBuilder::Validity evalConst(kj::StringPtr name) { convertTo = formatFromDeprecatedFlags(convertTo); KJ_ASSERT(sourceFiles.size() == 1); auto parser = kj::parse::sequence( kj::parse::many( kj::parse::sequence( kj::parse::identifier, kj::parse::many( kj::parse::sequence( kj::parse::exactChar<'['>(), kj::parse::integer, kj::parse::exactChar<']'>())), kj::parse::oneOf( kj::parse::endOfInput, kj::parse::sequence( kj::parse::exactChar<'.'>(), kj::parse::notLookingAt(kj::parse::endOfInput))))), kj::parse::endOfInput); kj::parse::IteratorInput input(name.begin(), name.end()); kj::Array>> nameParts; KJ_IF_MAYBE(p, parser(input)) { nameParts = kj::mv(*p); } else { return "invalid syntax"; } auto pos = nameParts.begin(); // Traverse the path to find a schema. uint64_t scopeId = sourceFiles[0].id; bool stoppedAtSubscript = false; for (; pos != nameParts.end(); ++pos) { kj::StringPtr part = kj::get<0>(*pos); KJ_IF_MAYBE(childId, compiler->lookup(scopeId, part)) { scopeId = *childId; if (kj::get<1>(*pos).size() > 0) { stoppedAtSubscript = true; break; } } else { break; } } Schema schema = compiler->getLoader().get(scopeId); // Evaluate this schema to a DynamicValue. DynamicValue::Reader value; word zeroWord[1]; memset(&zeroWord, 0, sizeof(zeroWord)); kj::ArrayPtr segments[1] = { kj::arrayPtr(zeroWord, 1) }; SegmentArrayMessageReader emptyMessage(segments); switch (schema.getProto().which()) { case schema::Node::CONST: value = schema.asConst(); break; case schema::Node::STRUCT: if (pos == nameParts.end()) { return kj::str("'", schema.getShortDisplayName(), "' cannot be evaluated."); } // Use the struct's default value. value = emptyMessage.getRoot(schema.asStruct()); break; default: if (stoppedAtSubscript) { return kj::str("'", schema.getShortDisplayName(), "' is not a list."); } else if (pos != nameParts.end()) { return kj::str("'", kj::get<0>(*pos), "' is not defined."); } else { return kj::str("'", schema.getShortDisplayName(), "' cannot be evaluated."); } } // Traverse the rest of the path as struct fields. for (; pos != nameParts.end(); ++pos) { kj::StringPtr partName = kj::get<0>(*pos); if (!stoppedAtSubscript) { if (value.getType() == DynamicValue::STRUCT) { auto structValue = value.as(); KJ_IF_MAYBE(field, structValue.getSchema().findFieldByName(partName)) { value = structValue.get(*field); } else { return kj::str("'", kj::get<0>(pos[-1]), "' has no member '", partName, "'."); } } else { return kj::str("'", kj::get<0>(pos[-1]), "' is not a struct."); } } auto& subscripts = kj::get<1>(*pos); for (uint i = 0; i < subscripts.size(); i++) { uint64_t subscript = subscripts[i]; if (value.getType() == DynamicValue::LIST) { auto listValue = value.as(); if (subscript < listValue.size()) { value = listValue[subscript]; } else { return kj::str("'", partName, "[", kj::strArray(subscripts.slice(0, i + 1), "]["), "]' is out-of-bounds."); } } else { if (i > 0) { return kj::str("'", partName, "[", kj::strArray(subscripts.slice(0, i), "]["), "]' is not a list."); } else { return kj::str("'", partName, "' is not a list."); } } } stoppedAtSubscript = false; } // OK, we have a value. Print it. if (convertTo != Format::TEXT) { if (value.getType() != DynamicValue::STRUCT) { return "not a struct; binary output is only available on structs"; } auto structValue = value.as(); rootType = structValue.getSchema(); kj::FdOutputStream output(STDOUT_FILENO); writeConversion(structValue, output); context.exit(); } else { if (pretty && value.getType() == DynamicValue::STRUCT) { context.exitInfo(prettyPrint(value.as()).flatten()); } else if (pretty && value.getType() == DynamicValue::LIST) { context.exitInfo(prettyPrint(value.as()).flatten()); } else { context.exitInfo(kj::str(value)); } } KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT; } public: // ===================================================================================== void addError(const kj::ReadableDirectory& directory, kj::PathPtr path, SourcePos start, SourcePos end, kj::StringPtr message) override { auto file = getDisplayName(directory, path); kj::String wholeMessage; if (end.line == start.line) { if (end.column == start.column) { wholeMessage = kj::str(file, ":", start.line + 1, ":", start.column + 1, ": error: ", message, "\n"); } else { wholeMessage = kj::str(file, ":", start.line + 1, ":", start.column + 1, "-", end.column + 1, ": error: ", message, "\n"); } } else { // The error spans multiple lines, so just report it on the first such line. wholeMessage = kj::str(file, ":", start.line + 1, ": error: ", message, "\n"); } context.error(wholeMessage); hadErrors_ = true; } bool hadErrors() override { return hadErrors_; } private: kj::ProcessContext& context; kj::Own disk; ModuleLoader loader; kj::SpaceFor compilerSpace; bool compilerConstructed = false; kj::Own compiler; Compiler::AnnotationFlag annotationFlag = Compiler::COMPILE_ANNOTATIONS; uint compileEagerness = Compiler::NODE | Compiler::CHILDREN | Compiler::DEPENDENCIES | Compiler::DEPENDENCY_PARENTS; // By default we compile each explicitly listed schema in full, plus first-level dependencies // of those schemas, plus the parent nodes of any dependencies. This is what most code generators // require to function. struct SourceDirectory { kj::Own dir; bool isSourcePrefix; }; kj::HashMap sourceDirectories; // For each import path and source prefix, tracks the directory object we opened for it. // // Use via getSourceDirectory(). kj::HashMap dirPrefixes; // For each open directory object, maps to a path prefix to add when displaying this path in // error messages. This keeps track of the original directory name as given by the user, before // canonicalization. // // Use via getDisplayName(). bool addStandardImportPaths = true; Format convertFrom = Format::BINARY; Format convertTo = Format::BINARY; // For the "convert" command. bool binary = false; bool flat = false; bool packed = false; bool pretty = true; bool quiet = false; uint segmentSize = 0; StructSchema rootType; // For the "decode" and "encode" commands. struct SourceFile { uint64_t id; kj::StringPtr name; Module* module; }; kj::Vector sourceFiles; struct OutputDirective { kj::ArrayPtr name; kj::Maybe dir; KJ_DISALLOW_COPY(OutputDirective); OutputDirective(OutputDirective&&) = default; OutputDirective(kj::ArrayPtr name, kj::Maybe dir) : name(name), dir(kj::mv(dir)) {} }; kj::Vector outputs; bool hadErrors_ = false; kj::Maybe getSourceDirectory( kj::StringPtr pathStr, bool isSourcePrefix) { auto cwd = disk->getCurrentPath(); auto path = cwd.evalNative(pathStr); if (path.size() == 0) return disk->getRoot(); KJ_IF_MAYBE(sdir, sourceDirectories.find(path)) { sdir->isSourcePrefix = sdir->isSourcePrefix || isSourcePrefix; return *sdir->dir; } if (path == cwd) { // Slight hack if the working directory is explicitly specified: // - We want to avoid opening a new copy of the working directory, as tryOpenSubdir() would // do. // - If isSourcePrefix is true, we need to add it to sourceDirectories to track that. // Otherwise we don't need to add it at all. // - We do not need to add it to dirPrefixes since the cwd is already handled in // getDisplayName(). auto& result = disk->getCurrent(); if (isSourcePrefix) { kj::Own fakeOwn(&result, kj::NullDisposer::instance); sourceDirectories.insert(kj::mv(path), { kj::mv(fakeOwn), isSourcePrefix }); } return result; } KJ_IF_MAYBE(dir, disk->getRoot().tryOpenSubdir(path)) { auto& result = *dir->get(); sourceDirectories.insert(kj::mv(path), { kj::mv(*dir), isSourcePrefix }); #if _WIN32 kj::String prefix = pathStr.endsWith("/") || pathStr.endsWith("\\") ? kj::str(pathStr) : kj::str(pathStr, '\\'); #else kj::String prefix = pathStr.endsWith("/") ? kj::str(pathStr) : kj::str(pathStr, '/'); #endif dirPrefixes.insert(&result, kj::mv(prefix)); return result; } else { return nullptr; } } struct DirPathPair { const kj::ReadableDirectory& dir; kj::Path path; }; DirPathPair interpretSourceFile(kj::StringPtr pathStr) { auto cwd = disk->getCurrentPath(); auto path = cwd.evalNative(pathStr); KJ_REQUIRE(path.size() > 0); for (size_t i = path.size() - 1; i > 0; i--) { auto prefix = path.slice(0, i); auto remainder = path.slice(i, path.size()); KJ_IF_MAYBE(sdir, sourceDirectories.find(prefix)) { if (sdir->isSourcePrefix) { return { *sdir->dir, remainder.clone() }; } } } // No source prefix matched. Fall back to heuristic: try stripping the current directory, // otherwise don't strip anything. if (path.startsWith(cwd)) { return { disk->getCurrent(), path.slice(cwd.size(), path.size()).clone() }; } else { // Hmm, no src-prefix matched and the file isn't even in the current directory. This might // be OK if we aren't generating any output anyway, but otherwise the results will almost // certainly not be what the user wanted. Let's print a warning, unless the output directives // are ones which we know do not produce output files. This is a hack. for (auto& output: outputs) { auto name = kj::str(output.name); if (name != "-" && name != "capnp") { context.warning(kj::str(pathStr, ": File is not in the current directory and does not match any prefix defined with " "--src-prefix. Please pass an appropriate --src-prefix so I can figure out where to " "write the output for this file.")); break; } } return { disk->getRoot(), kj::mv(path) }; } } kj::String getDisplayName(const kj::ReadableDirectory& dir, kj::PathPtr path) { KJ_IF_MAYBE(prefix, dirPrefixes.find(&dir)) { return kj::str(*prefix, path.toNativeString()); } else if (&dir == &disk->getRoot()) { return path.toNativeString(true); } else if (&dir == &disk->getCurrent()) { return path.toNativeString(false); } else { KJ_FAIL_ASSERT("unrecognized directory"); } } }; } // namespace compiler } // namespace capnp KJ_MAIN(capnp::compiler::CompilerMain); capnproto-c++-0.8.0/src/capnp/compiler/evolution-test.c++0000644000175000017500000007454313340402540023755 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This is a fuzz test which randomly generates a schema for a struct one change at a time. // Each modification is known a priori to be compatible or incompatible. The type is compiled // before and after the change and both versions are loaded into a SchemaLoader with the // expectation that this will succeed if they are compatible and fail if they are not. If // the types are expected to be compatible, the test also constructs an instance of the old // type and reads it as the new type, and vice versa. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include "compiler.h" #include #include #include #include #include #include #include namespace capnp { namespace compiler { namespace { static const kj::StringPtr RFC3092[] = {"foo", "bar", "baz", "qux"}; template T& chooseFrom(T (&arr)[size]) { return arr[rand() % size]; } template auto chooseFrom(T arr) -> decltype(arr[0]) { return arr[rand() % arr.size()]; } static Declaration::Builder addNested(Declaration::Builder parent) { auto oldNestedOrphan = parent.disownNestedDecls(); auto oldNested = oldNestedOrphan.get(); auto newNested = parent.initNestedDecls(oldNested.size() + 1); uint index = rand() % (oldNested.size() + 1); for (uint i = 0; i < index; i++) { newNested.setWithCaveats(i, oldNested[i]); } for (uint i = index + 1; i < newNested.size(); i++) { newNested.setWithCaveats(i, oldNested[i - 1]); } return newNested[index]; } struct TypeOption { kj::StringPtr name; kj::ConstFunction makeValue; }; static const TypeOption TYPE_OPTIONS[] = { { "Int32", [](Expression::Builder builder) { builder.setPositiveInt(rand() % (1 << 24)); }}, { "Float64", [](Expression::Builder builder) { builder.setPositiveInt(rand()); }}, { "Int8", [](Expression::Builder builder) { builder.setPositiveInt(rand() % 128); }}, { "UInt16", [](Expression::Builder builder) { builder.setPositiveInt(rand() % (1 << 16)); }}, { "Bool", [](Expression::Builder builder) { builder.initRelativeName().setValue("true"); }}, { "Text", [](Expression::Builder builder) { builder.setString(chooseFrom(RFC3092)); }}, { "StructType", [](Expression::Builder builder) { auto assignment = builder.initTuple(1)[0]; assignment.initNamed().setValue("i"); assignment.initValue().setPositiveInt(rand() % (1 << 24)); }}, { "EnumType", [](Expression::Builder builder) { builder.initRelativeName().setValue(chooseFrom(RFC3092)); }}, }; void setDeclName(Expression::Builder decl, kj::StringPtr name) { decl.initRelativeName().setValue(name); } static kj::ConstFunction randomizeType(Expression::Builder type) { auto option = &chooseFrom(TYPE_OPTIONS); if (rand() % 4 == 0) { auto app = type.initApplication(); setDeclName(app.initFunction(), "List"); setDeclName(app.initParams(1)[0].initValue(), option->name); return [option](Expression::Builder builder) { for (auto element: builder.initList(rand() % 4 + 1)) { option->makeValue(element); } }; } else { setDeclName(type, option->name); return option->makeValue.reference(); } } enum ChangeKind { NO_CHANGE, COMPATIBLE, INCOMPATIBLE, SUBTLY_COMPATIBLE // The change is technically compatible on the wire, but SchemaLoader will complain. }; struct ChangeInfo { ChangeKind kind; kj::String description; ChangeInfo(): kind(NO_CHANGE) {} ChangeInfo(ChangeKind kind, kj::StringPtr description) : kind(kind), description(kj::str(description)) {} ChangeInfo(ChangeKind kind, kj::String&& description) : kind(kind), description(kj::mv(description)) {} }; extern kj::ArrayPtr> STRUCT_MODS; extern kj::ArrayPtr> FIELD_MODS; // ================================================================================ static ChangeInfo declChangeName(Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { auto name = decl.getName(); if (name.getValue().size() == 0) { // Naming an unnamed union. name.setValue(kj::str("unUnnamed", nextOrdinal)); return { SUBTLY_COMPATIBLE, "Assign name to unnamed union." }; } else { name.setValue(kj::str(name.getValue(), "Xx")); return { COMPATIBLE, "Rename declaration." }; } } static ChangeInfo structAddField(Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { auto fieldDecl = addNested(decl); uint ordinal = nextOrdinal++; fieldDecl.initName().setValue(kj::str("f", ordinal)); fieldDecl.getId().initOrdinal().setValue(ordinal); auto field = fieldDecl.initField(); auto makeValue = randomizeType(field.initType()); if (rand() % 4 == 0) { makeValue(field.getDefaultValue().initValue()); } else { field.getDefaultValue().setNone(); } return { COMPATIBLE, "Add field." }; } static ChangeInfo structModifyField(Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { auto nested = decl.getNestedDecls(); if (nested.size() == 0) { return { NO_CHANGE, "Modify field, but there were none to modify." }; } auto field = chooseFrom(nested); bool hasUnion = false; if (decl.isUnion()) { hasUnion = true; } else { for (auto n: nested) { if (n.isUnion() && n.getName().getValue().size() == 0) { hasUnion = true; break; } } } if (field.isGroup() || field.isUnion()) { return chooseFrom(STRUCT_MODS)(field, nextOrdinal, hasUnion); } else { return chooseFrom(FIELD_MODS)(field, nextOrdinal, hasUnion); } } static ChangeInfo structGroupifyFields( Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { // Place a random subset of the fields into a group. if (decl.isUnion()) { return { NO_CHANGE, "Randomly make a group out of some fields, but I can't do this to a union." }; } kj::Vector> groupified; kj::Vector> notGroupified; auto orphanage = Orphanage::getForMessageContaining(decl); for (auto nested: decl.getNestedDecls()) { if (rand() % 2) { groupified.add(orphanage.newOrphanCopy(nested.asReader())); } else { notGroupified.add(orphanage.newOrphanCopy(nested.asReader())); } } if (groupified.size() == 0) { return { NO_CHANGE, "Randomly make a group out of some fields, but I ended up choosing none of them." }; } auto newNested = decl.initNestedDecls(notGroupified.size() + 1); uint index = rand() % (notGroupified.size() + 1); for (uint i = 0; i < index; i++) { newNested.adoptWithCaveats(i, kj::mv(notGroupified[i])); } for (uint i = index; i < notGroupified.size(); i++) { newNested.adoptWithCaveats(i + 1, kj::mv(notGroupified[i])); } auto newGroup = newNested[index]; auto groupNested = newGroup.initNestedDecls(groupified.size()); for (uint i = 0; i < groupified.size(); i++) { groupNested.adoptWithCaveats(i, kj::mv(groupified[i])); } newGroup.initName().setValue(kj::str("g", nextOrdinal, "x", groupNested[0].getName().getValue())); newGroup.getId().setUnspecified(); newGroup.setGroup(); return { SUBTLY_COMPATIBLE, "Randomly group some set of existing fields." }; } static ChangeInfo structPermuteFields( Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { if (decl.getNestedDecls().size() == 0) { return { NO_CHANGE, "Permute field code order, but there were none." }; } auto oldOrphan = decl.disownNestedDecls(); auto old = oldOrphan.get(); KJ_STACK_ARRAY(uint, mapping, old.size(), 16, 64); for (uint i = 0; i < mapping.size(); i++) { mapping[i] = i; } for (uint i = mapping.size() - 1; i > 0; i--) { uint j = rand() % i; uint temp = mapping[j]; mapping[j] = mapping[i]; mapping[i] = temp; } auto newNested = decl.initNestedDecls(old.size()); for (uint i = 0; i < old.size(); i++) { newNested.setWithCaveats(i, old[mapping[i]]); } return { COMPATIBLE, "Permute field code order." }; } kj::ConstFunction STRUCT_MODS_[] = { structAddField, structAddField, structAddField, structModifyField, structModifyField, structModifyField, structPermuteFields, declChangeName, structGroupifyFields // do more rarely because it creates slowness }; kj::ArrayPtr> STRUCT_MODS = STRUCT_MODS_; // ================================================================================ static ChangeInfo fieldUpgradeList(Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { // Upgrades a non-struct list to a struct list. auto field = decl.getField(); if (field.getDefaultValue().isValue()) { return { NO_CHANGE, "Upgrade primitive list to struct list, but it had a default value." }; } auto type = field.getType(); if (!type.isApplication()) { return { NO_CHANGE, "Upgrade primitive list to struct list, but it wasn't a list." }; } auto typeParams = type.getApplication().getParams(); auto elementType = typeParams[0].getValue(); auto relativeName = elementType.getRelativeName(); auto nameText = relativeName.asReader().getValue(); if (nameText == "StructType" || nameText.endsWith("Struct")) { return { NO_CHANGE, "Upgrade primitive list to struct list, but it was already a struct list."}; } if (nameText == "Bool") { return { NO_CHANGE, "Upgrade primitive list to struct list, but bool lists can't be upgraded."}; } relativeName.setValue(kj::str(nameText, "Struct")); return { COMPATIBLE, "Upgrade primitive list to struct list" }; } static ChangeInfo fieldExpandGroup(Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { Declaration::Builder newDecl = decl.initNestedDecls(1)[0]; newDecl.adoptName(decl.disownName()); newDecl.getId().adoptOrdinal(decl.getId().disownOrdinal()); auto field = decl.getField(); auto newField = newDecl.initField(); newField.adoptType(field.disownType()); if (field.getDefaultValue().isValue()) { newField.getDefaultValue().adoptValue(field.getDefaultValue().disownValue()); } else { newField.getDefaultValue().setNone(); } decl.initName().setValue(kj::str("g", newDecl.getName().getValue())); decl.getId().setUnspecified(); if (rand() % 2 == 0) { decl.setGroup(); } else { decl.setUnion(); if (!scopeHasUnion && rand() % 2 == 0) { // Make it an unnamed union. decl.getName().setValue(""); } structAddField(decl, nextOrdinal, scopeHasUnion); // union must have two members } return { COMPATIBLE, "Wrap a field in a singleton group." }; } static ChangeInfo fieldChangeType(Declaration::Builder decl, uint& nextOrdinal, bool scopeHasUnion) { auto field = decl.getField(); if (field.getDefaultValue().isNone()) { // Change the type. auto type = field.getType(); while (type.isApplication()) { // Either change the list parameter, or revert to a non-list. if (rand() % 2) { type = type.getApplication().getParams()[0].getValue(); } else { type.initRelativeName(); } } auto typeName = type.getRelativeName(); if (typeName.asReader().getValue().startsWith("Text")) { typeName.setValue("Int32"); } else { typeName.setValue("Text"); } return { INCOMPATIBLE, "Change the type of a field." }; } else { // Change the default value. auto dval = field.getDefaultValue().getValue(); switch (dval.which()) { case Expression::UNKNOWN: KJ_FAIL_ASSERT("unknown value expression?"); case Expression::POSITIVE_INT: dval.setPositiveInt(dval.getPositiveInt() ^ 1); break; case Expression::NEGATIVE_INT: dval.setNegativeInt(dval.getNegativeInt() ^ 1); break; case Expression::FLOAT: dval.setFloat(-dval.getFloat()); break; case Expression::RELATIVE_NAME: { auto name = dval.getRelativeName(); auto nameText = name.asReader().getValue(); if (nameText == "true") { name.setValue("false"); } else if (nameText == "false") { name.setValue("true"); } else if (nameText == "foo") { name.setValue("bar"); } else { name.setValue("foo"); } break; } case Expression::STRING: case Expression::BINARY: case Expression::LIST: case Expression::TUPLE: return { NO_CHANGE, "Change the default value of a field, but it's a pointer field." }; case Expression::ABSOLUTE_NAME: case Expression::IMPORT: case Expression::EMBED: case Expression::APPLICATION: case Expression::MEMBER: KJ_FAIL_ASSERT("Unexpected expression type."); } return { INCOMPATIBLE, "Change the default value of a pritimive field." }; } } kj::ConstFunction FIELD_MODS_[] = { fieldUpgradeList, fieldExpandGroup, fieldChangeType, declChangeName }; kj::ArrayPtr> FIELD_MODS = FIELD_MODS_; // ================================================================================ uint getOrdinal(StructSchema::Field field) { auto proto = field.getProto(); if (proto.getOrdinal().isExplicit()) { return proto.getOrdinal().getExplicit(); } KJ_ASSERT(proto.isGroup()); auto group = field.getType().asStruct(); return getOrdinal(group.getFields()[0]); } Orphan makeExampleStruct( Orphanage orphanage, StructSchema schema, uint sharedOrdinalCount); void checkExampleStruct(DynamicStruct::Reader reader, uint sharedOrdinalCount); Orphan makeExampleValue( Orphanage orphanage, uint ordinal, Type type, uint sharedOrdinalCount) { switch (type.which()) { case schema::Type::INT32: return ordinal * 47327; case schema::Type::FLOAT64: return ordinal * 313.25; case schema::Type::INT8: return int(ordinal % 256) - 128; case schema::Type::UINT16: return ordinal * 13; case schema::Type::BOOL: return ordinal % 2 == 0; case schema::Type::TEXT: return orphanage.newOrphanCopy(Text::Reader(kj::str(ordinal))); case schema::Type::STRUCT: { auto structType = type.asStruct(); auto result = orphanage.newOrphan(structType); auto builder = result.get(); KJ_IF_MAYBE(fieldI, structType.findFieldByName("i")) { // Type is "StructType" builder.set(*fieldI, ordinal); } else { // Type is "Int32Struct" or the like. auto field = structType.getFieldByName("f0"); builder.adopt(field, makeExampleValue( orphanage, ordinal, field.getType(), sharedOrdinalCount)); } return kj::mv(result); } case schema::Type::ENUM: { auto enumerants = type.asEnum().getEnumerants(); return DynamicEnum(enumerants[ordinal %enumerants.size()]); } case schema::Type::LIST: { auto listType = type.asList(); auto elementType = listType.getElementType(); auto result = orphanage.newOrphan(listType, 1); result.get().adopt(0, makeExampleValue( orphanage, ordinal, elementType, sharedOrdinalCount)); return kj::mv(result); } default: KJ_FAIL_ASSERT("You added a new possible field type!"); } } void checkExampleValue(DynamicValue::Reader value, uint ordinal, schema::Type::Reader type, uint sharedOrdinalCount) { switch (type.which()) { case schema::Type::INT32: KJ_ASSERT(value.as() == ordinal * 47327); break; case schema::Type::FLOAT64: KJ_ASSERT(value.as() == ordinal * 313.25); break; case schema::Type::INT8: KJ_ASSERT(value.as() == int(ordinal % 256) - 128); break; case schema::Type::UINT16: KJ_ASSERT(value.as() == ordinal * 13); break; case schema::Type::BOOL: KJ_ASSERT(value.as() == (ordinal % 2 == 0)); break; case schema::Type::TEXT: KJ_ASSERT(value.as() == kj::str(ordinal)); break; case schema::Type::STRUCT: { auto structValue = value.as(); auto structType = structValue.getSchema(); KJ_IF_MAYBE(fieldI, structType.findFieldByName("i")) { // Type is "StructType" KJ_ASSERT(structValue.get(*fieldI).as() == ordinal); } else { // Type is "Int32Struct" or the like. auto field = structType.getFieldByName("f0"); checkExampleValue(structValue.get(field), ordinal, field.getProto().getSlot().getType(), sharedOrdinalCount); } break; } case schema::Type::ENUM: { auto enumerant = KJ_ASSERT_NONNULL(value.as().getEnumerant()); KJ_ASSERT(enumerant.getIndex() == ordinal % enumerant.getContainingEnum().getEnumerants().size()); break; } case schema::Type::LIST: checkExampleValue(value.as()[0], ordinal, type.getList().getElementType(), sharedOrdinalCount); break; default: KJ_FAIL_ASSERT("You added a new possible field type!"); } } void setExampleField(DynamicStruct::Builder builder, StructSchema::Field field, uint sharedOrdinalCount) { auto fieldProto = field.getProto(); switch (fieldProto.which()) { case schema::Field::SLOT: builder.adopt(field, makeExampleValue( Orphanage::getForMessageContaining(builder), getOrdinal(field), field.getType(), sharedOrdinalCount)); break; case schema::Field::GROUP: builder.adopt(field, makeExampleStruct( Orphanage::getForMessageContaining(builder), field.getType().asStruct(), sharedOrdinalCount)); break; } } void checkExampleField(DynamicStruct::Reader reader, StructSchema::Field field, uint sharedOrdinalCount) { auto fieldProto = field.getProto(); switch (fieldProto.which()) { case schema::Field::SLOT: { uint ordinal = getOrdinal(field); if (ordinal < sharedOrdinalCount) { checkExampleValue(reader.get(field), ordinal, fieldProto.getSlot().getType(), sharedOrdinalCount); } break; } case schema::Field::GROUP: checkExampleStruct(reader.get(field).as(), sharedOrdinalCount); break; } } Orphan makeExampleStruct( Orphanage orphanage, StructSchema schema, uint sharedOrdinalCount) { // Initialize all fields of the struct via reflection, such that they can be verified using // a different version of the struct. sharedOrdinalCount is the number of ordinals shared by // the two versions. This is used mainly to avoid setting union members that the other version // doesn't have. Orphan result = orphanage.newOrphan(schema); auto builder = result.get(); for (auto field: schema.getNonUnionFields()) { setExampleField(builder, field, sharedOrdinalCount); } auto unionFields = schema.getUnionFields(); // Pretend the union doesn't have any fields that aren't in the shared ordinal range. uint range = unionFields.size(); while (range > 0 && getOrdinal(unionFields[range - 1]) >= sharedOrdinalCount) { --range; } if (range > 0) { auto field = unionFields[getOrdinal(unionFields[0]) % range]; setExampleField(builder, field, sharedOrdinalCount); } return kj::mv(result); } void checkExampleStruct(DynamicStruct::Reader reader, uint sharedOrdinalCount) { auto schema = reader.getSchema(); for (auto field: schema.getNonUnionFields()) { checkExampleField(reader, field, sharedOrdinalCount); } auto unionFields = schema.getUnionFields(); // Pretend the union doesn't have any fields that aren't in the shared ordinal range. uint range = unionFields.size(); while (range > 0 && getOrdinal(unionFields[range - 1]) >= sharedOrdinalCount) { --range; } if (range > 0) { auto field = unionFields[getOrdinal(unionFields[0]) % range]; checkExampleField(reader, field, sharedOrdinalCount); } } // ================================================================================ class ModuleImpl final: public Module { public: explicit ModuleImpl(ParsedFile::Reader content): content(content) {} kj::StringPtr getSourceName() override { return "evolving-schema.capnp"; } Orphan loadContent(Orphanage orphanage) override { return orphanage.newOrphanCopy(content); } kj::Maybe importRelative(kj::StringPtr importPath) override { return nullptr; } kj::Maybe> embedRelative(kj::StringPtr embedPath) override { return nullptr; } void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) override { KJ_FAIL_ASSERT("Unexpected parse error.", startByte, endByte, message); } bool hadErrors() override { return false; } private: ParsedFile::Reader content; }; static void loadStructAndGroups(const SchemaLoader& src, SchemaLoader& dst, uint64_t id) { auto proto = src.get(id).getProto(); dst.load(proto); for (auto field: proto.getStruct().getFields()) { if (field.isGroup()) { loadStructAndGroups(src, dst, field.getGroup().getTypeId()); } } } static kj::Maybe loadFile( ParsedFile::Reader file, SchemaLoader& loader, bool allNodes, kj::Maybe>& messageBuilder, uint sharedOrdinalCount) { Compiler compiler; ModuleImpl module(file); KJ_ASSERT(compiler.add(module) == 0x8123456789abcdefllu); if (allNodes) { // Eagerly compile and load the whole thing. compiler.eagerlyCompile(0x8123456789abcdefllu, Compiler::ALL_RELATED_NODES); KJ_IF_MAYBE(m, messageBuilder) { // Build an example struct using the compiled schema. m->get()->adoptRoot(makeExampleStruct( m->get()->getOrphanage(), compiler.getLoader().get(0x823456789abcdef1llu).asStruct(), sharedOrdinalCount)); } for (auto schema: compiler.getLoader().getAllLoaded()) { loader.load(schema.getProto()); } return nullptr; } else { // Compile the file root so that the children are findable, then load the specific child // we want. compiler.eagerlyCompile(0x8123456789abcdefllu, Compiler::NODE); KJ_IF_MAYBE(m, messageBuilder) { // Check that the example struct matches the compiled schema. auto root = m->get()->getRoot( compiler.getLoader().get(0x823456789abcdef1llu).asStruct()).asReader(); KJ_CONTEXT(root); checkExampleStruct(root, sharedOrdinalCount); } return kj::runCatchingExceptions([&]() { loadStructAndGroups(compiler.getLoader(), loader, 0x823456789abcdef1llu); }); } } bool checkChange(ParsedFile::Reader file1, ParsedFile::Reader file2, ChangeKind changeKind, uint sharedOrdinalCount) { // Try loading file1 followed by file2 into the same SchemaLoader, expecting it to behave // according to changeKind. Returns true if the files are both expected to be compatible and // actually are -- the main loop uses this to decide which version to keep kj::Maybe> exampleBuilder; if (changeKind != INCOMPATIBLE) { // For COMPATIBLE and SUBTLY_COMPATIBLE changes, build an example message with one schema // and check it with the other. exampleBuilder = kj::heap(); } SchemaLoader loader; loadFile(file1, loader, true, exampleBuilder, sharedOrdinalCount); auto exception = loadFile(file2, loader, false, exampleBuilder, sharedOrdinalCount); if (changeKind == COMPATIBLE) { KJ_IF_MAYBE(e, exception) { kj::getExceptionCallback().onFatalException(kj::mv(*e)); return false; } else { return true; } } else if (changeKind == INCOMPATIBLE) { KJ_ASSERT(exception != nullptr, file1, file2); return false; } else { KJ_ASSERT(changeKind == SUBTLY_COMPATIBLE); // SchemaLoader is allowed to throw an exception in this case, but we ignore it. return true; } } void doTest() { auto builder = kj::heap(); { // Set up the basic file decl. auto parsedFile = builder->initRoot(); auto file = parsedFile.initRoot(); file.setFile(); file.initId().initUid().setValue(0x8123456789abcdefllu); auto decls = file.initNestedDecls(3 + kj::size(TYPE_OPTIONS)); { auto decl = decls[0]; decl.initName().setValue("EvolvingStruct"); decl.initId().initUid().setValue(0x823456789abcdef1llu); decl.setStruct(); } { auto decl = decls[1]; decl.initName().setValue("StructType"); decl.setStruct(); auto fieldDecl = decl.initNestedDecls(1)[0]; fieldDecl.initName().setValue("i"); fieldDecl.getId().initOrdinal().setValue(0); auto field = fieldDecl.initField(); setDeclName(field.initType(), "UInt32"); } { auto decl = decls[2]; decl.initName().setValue("EnumType"); decl.setEnum(); auto enumerants = decl.initNestedDecls(4); for (uint i = 0; i < kj::size(RFC3092); i++) { auto enumerantDecl = enumerants[i]; enumerantDecl.initName().setValue(RFC3092[i]); enumerantDecl.getId().initOrdinal().setValue(i); enumerantDecl.setEnumerant(); } } // For each of TYPE_OPTIONS, declare a struct type that contains that type as its @0 field. for (uint i = 0; i < kj::size(TYPE_OPTIONS); i++) { auto decl = decls[3 + i]; auto& option = TYPE_OPTIONS[i]; decl.initName().setValue(kj::str(option.name, "Struct")); decl.setStruct(); auto fieldDecl = decl.initNestedDecls(1)[0]; fieldDecl.initName().setValue("f0"); fieldDecl.getId().initOrdinal().setValue(0); auto field = fieldDecl.initField(); setDeclName(field.initType(), option.name); uint ordinal = 1; for (auto j: kj::range(0, rand() % 4)) { (void)j; structAddField(decl, ordinal, false); } } } uint nextOrdinal = 0; for (uint i = 0; i < 96; i++) { uint oldOrdinalCount = nextOrdinal; auto newBuilder = kj::heap(); newBuilder->setRoot(builder->getRoot().asReader()); auto parsedFile = newBuilder->getRoot(); Declaration::Builder decl = parsedFile.getRoot().getNestedDecls()[0]; // Apply a random modification. ChangeInfo changeInfo; while (changeInfo.kind == NO_CHANGE) { auto& mod = chooseFrom(STRUCT_MODS); changeInfo = mod(decl, nextOrdinal, false); } KJ_CONTEXT(changeInfo.description); if (checkChange(builder->getRoot(), parsedFile, changeInfo.kind, oldOrdinalCount) && checkChange(parsedFile, builder->getRoot(), changeInfo.kind, oldOrdinalCount)) { builder = kj::mv(newBuilder); } } } class EvolutionTestMain { public: explicit EvolutionTestMain(kj::ProcessContext& context) : context(context) {} kj::MainFunc getMain() { return kj::MainBuilder(context, "(unknown version)", "Integration test / fuzzer which randomly modifies schemas is backwards-compatible ways " "and verifies that they do actually remain compatible.") .addOptionWithArg({"seed"}, KJ_BIND_METHOD(*this, setSeed), "", "Set random number seed to . By default, time() is used.") .callAfterParsing(KJ_BIND_METHOD(*this, run)) .build(); } kj::MainBuilder::Validity setSeed(kj::StringPtr value) { char* end; seed = strtol(value.cStr(), &end, 0); if (value.size() == 0 || *end != '\0') { return "not an integer"; } else { return true; } } kj::MainBuilder::Validity run() { // https://github.com/sandstorm-io/capnproto/issues/344 describes an obscure bug in the layout // algorithm, the fix for which breaks backwards-compatibility for any schema triggering the // bug. In order to avoid silently breaking protocols, we are temporarily throwing an exception // in cases where this bug would have occurred, so that people can decide what to do. // However, the evolution test can occasionally trigger the bug (depending on the random path // it takes). Rather than try to avoid it, we disable the exception-throwing, because the bug // is actually fixed, and the exception is only there to raise awareness of the compatibility // concerns. // // On Linux, seed 1467142714 (for example) will trigger the exception (without this env var). #if defined(__MINGW32__) || defined(_MSC_VER) putenv("CAPNP_IGNORE_ISSUE_344=1"); #else setenv("CAPNP_IGNORE_ISSUE_344", "1", true); #endif srand(seed); { kj::String text = kj::str( "Randomly testing backwards-compatibility scenarios with seed: ", seed, "\n"); kj::FdOutputStream(STDOUT_FILENO).write(text.begin(), text.size()); } KJ_CONTEXT(seed, "PLEASE REPORT THIS FAILURE AND INCLUDE THE SEED"); doTest(); return true; } private: kj::ProcessContext& context; uint seed = time(nullptr); }; } // namespace } // namespace compiler } // namespace capnp KJ_MAIN(capnp::compiler::EvolutionTestMain); capnproto-c++-0.8.0/src/capnp/compiler/lexer-test.c++0000644000175000017500000003073413340402540023042 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "lexer.h" #include "../message.h" #include namespace capnp { namespace compiler { namespace { class TestFailingErrorReporter: public ErrorReporter { public: void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) override { KJ_FAIL_EXPECT("Parse failed.", startByte, endByte, message); } bool hadErrors() override { // Not used by lexer. return false; } }; template kj::String doLex(kj::StringPtr constText) { // Parse the given string into the given Cap'n Proto struct type using lex(), then stringify the // result and return that string. Additionally, single quotes in the input are converted to // double quotes, and double quotes in the output are converted to single quotes, to reduce the // amount of escaping needed in the test strings. // // Comparing stringifications against golden strings is ugly and brittle. If we had a // text-format parser we could use that. Except that said parser would probably be built on // the very lexer being tested here, so... maybe this is the best we can reasonably do. kj::String text = heapString(constText); for (char& c: text) { // Make it easier to write input strings below. if (c == '\'') c = '\"'; } MallocMessageBuilder message; auto file = message.initRoot(); TestFailingErrorReporter errorReporter; EXPECT_TRUE(lex(text, file, errorReporter)); kj::String result = kj::str(file); for (char& c: result) { // Make it easier to write golden strings below. if (c == '\"') c = '\''; } return result; } TEST(Lexer, Tokens) { EXPECT_STREQ( "(tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3), " "(identifier = 'bar', startByte = 4, endByte = 7)" "])", doLex("foo bar").cStr()); EXPECT_STREQ( "(tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3), " "(identifier = 'bar', startByte = 15, endByte = 18)" "])", doLex("foo # comment\n bar").cStr()); EXPECT_STREQ( "(tokens = [" "(stringLiteral = 'foo ', startByte = 2, endByte = 11), " "(integerLiteral = 123, startByte = 12, endByte = 15), " "(floatLiteral = 2.75, startByte = 16, endByte = 20), " "(floatLiteral = 60000, startByte = 21, endByte = 24), " "(operator = '+', startByte = 25, endByte = 26), " "(operator = '-=', startByte = 27, endByte = 29)" "])", doLex(" 'foo\\x20' 123 2.75 6e4 + -= ").cStr()); EXPECT_STREQ( "(tokens = [" "(parenthesizedList = [" "[" "(identifier = 'foo', startByte = 1, endByte = 4), " "(identifier = 'bar', startByte = 5, endByte = 8)" "], [" "(identifier = 'baz', startByte = 10, endByte = 13), " "(identifier = 'qux', startByte = 14, endByte = 17)" "], [" "(identifier = 'corge', startByte = 19, endByte = 24), " "(identifier = 'grault', startByte = 25, endByte = 31)" "]" "], startByte = 0, endByte = 32)" "])", doLex("(foo bar, baz qux, corge grault)").cStr()); EXPECT_STREQ( "(tokens = [" "(parenthesizedList = [" "[" "(identifier = 'foo', startByte = 1, endByte = 4), " "(identifier = 'bar', startByte = 5, endByte = 8)" "]" "], startByte = 0, endByte = 9)" "])", doLex("(foo bar)").cStr()); // Empty parentheses should result in an empty list-of-lists, *not* a list containing an empty // list. EXPECT_STREQ( "(tokens = [" "(parenthesizedList = [], startByte = 0, endByte = 4)" "])", doLex("( )").cStr()); EXPECT_STREQ( "(tokens = [" "(bracketedList = [" "[" "(identifier = 'foo', startByte = 1, endByte = 4), " "(identifier = 'bar', startByte = 5, endByte = 8)" "], [" "(identifier = 'baz', startByte = 10, endByte = 13), " "(identifier = 'qux', startByte = 14, endByte = 17)" "], [" "(identifier = 'corge', startByte = 19, endByte = 24), " "(identifier = 'grault', startByte = 25, endByte = 31)" "]" "], startByte = 0, endByte = 32)" "])", doLex("[foo bar, baz qux, corge grault]").cStr()); // Trailing commas should not create an empty final list item, but be stripped by the lexer. EXPECT_STREQ( "(tokens = [" "(bracketedList = [" "[" "(identifier = 'foo', startByte = 1, endByte = 4)" "], [" "(identifier = 'bar', startByte = 6, endByte = 9)" "]" "], startByte = 0, endByte = 11)" "])", doLex("[foo, bar,]").cStr()); EXPECT_STREQ( "(tokens = [" "(bracketedList = [" "[" "(identifier = 'foo', startByte = 1, endByte = 4)" "], [" "(parenthesizedList = [" "[" "(identifier = 'bar', startByte = 7, endByte = 10)" "], [" "(identifier = 'baz', startByte = 12, endByte = 15)" "]" "], startByte = 6, endByte = 16)" "]" "], startByte = 0, endByte = 17), " "(identifier = 'qux', startByte = 18, endByte = 21)" "])", doLex("[foo, (bar, baz)] qux").cStr()); EXPECT_STREQ( "(tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3), " "(identifier = 'bar', startByte = 7, endByte = 10)" "])", doLex("foo\n\r\t\vbar").cStr()); } TEST(Lexer, Statements) { EXPECT_STREQ( "(statements = [" "(tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3), " "(identifier = 'bar', startByte = 4, endByte = 7)" "], line = void, startByte = 0, endByte = 8)" "])", doLex("foo bar;").cStr()); EXPECT_STREQ( "(statements = [" "(tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], line = void, startByte = 0, endByte = 4), " "(tokens = [" "(identifier = 'bar', startByte = 5, endByte = 8)" "], line = void, startByte = 5, endByte = 9), " "(tokens = [" "(identifier = 'baz', startByte = 10, endByte = 13)" "], line = void, startByte = 10, endByte = 14)" "])", doLex("foo; bar; baz; ").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "block = [" "(tokens = [" "(identifier = 'bar', startByte = 5, endByte = 8)" "], line = void, startByte = 5, endByte = 9), " "(tokens = [" "(identifier = 'baz', startByte = 10, endByte = 13)" "], line = void, startByte = 10, endByte = 14)" "], " "startByte = 0, endByte = 15" "), " "(tokens = [" "(identifier = 'qux', startByte = 16, endByte = 19)" "], line = void, startByte = 16, endByte = 20)" "])", doLex("foo {bar; baz;} qux;").cStr()); } TEST(Lexer, DocComments) { EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "line = void, " "docComment = 'blah blah\\n', " "startByte = 0, endByte = 16" ")" "])", doLex("foo; # blah blah").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "line = void, " "docComment = 'blah blah\\n', " "startByte = 0, endByte = 15" ")" "])", doLex("foo; #blah blah").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "line = void, " "docComment = ' blah blah\\n', " "startByte = 0, endByte = 17" ")" "])", doLex("foo; # blah blah").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "line = void, " "docComment = 'blah blah\\n', " "startByte = 0, endByte = 16" ")" "])", doLex("foo;\n# blah blah").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "line = void, " "startByte = 0, endByte = 4" ")" "])", doLex("foo;\n\n# blah blah").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "line = void, " "docComment = 'bar baz\\nqux corge\\n', " "startByte = 0, endByte = 30" ")" "])", doLex("foo;\n # bar baz\n # qux corge\n\n# grault\n# garply").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "block = [" "(tokens = [" "(identifier = 'bar', startByte = 17, endByte = 20)" "], line = void, docComment = 'hi\\n', startByte = 17, endByte = 27), " "(tokens = [" "(identifier = 'baz', startByte = 28, endByte = 31)" "], line = void, startByte = 28, endByte = 32)" "], " "docComment = 'blah blah\\n', " "startByte = 0, endByte = 44" "), " "(tokens = [" "(identifier = 'qux', startByte = 44, endByte = 47)" "], line = void, startByte = 44, endByte = 48)" "])", doLex("foo {# blah blah\nbar; # hi\n baz;} # ignored\nqux;").cStr()); EXPECT_STREQ( "(statements = [" "(" "tokens = [" "(identifier = 'foo', startByte = 0, endByte = 3)" "], " "block = [" "(tokens = [" "(identifier = 'bar', startByte = 5, endByte = 8)" "], line = void, startByte = 5, endByte = 9), " "(tokens = [" "(identifier = 'baz', startByte = 10, endByte = 13)" "], line = void, startByte = 10, endByte = 14)" "], " "docComment = 'late comment\\n', " "startByte = 0, endByte = 31" "), " "(tokens = [" "(identifier = 'qux', startByte = 31, endByte = 34)" "], line = void, startByte = 31, endByte = 35)" "])", doLex("foo {bar; baz;}\n# late comment\nqux;").cStr()); } TEST(Lexer, Utf8Bom) { EXPECT_STREQ( "(tokens = [" "(identifier = 'foo', startByte = 3, endByte = 6), " "(identifier = 'bar', startByte = 7, endByte = 10), " "(identifier = 'baz', startByte = 13, endByte = 16)" "])", doLex("\xef\xbb\xbf""foo bar\xef\xbb\xbf""baz").cStr()); } } // namespace } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/type-id-test.c++0000644000175000017500000000437413340402540023277 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "type-id.h" #include #include namespace capnp { namespace compiler { namespace { KJ_TEST("type ID generation hasn't changed") { KJ_EXPECT(generateChildId(0xa93fc509624c72d9ull, "Node") == 0xe682ab4cf923a417ull); KJ_EXPECT(generateChildId(0xe682ab4cf923a417ull, "NestedNode") == 0xdebf55bbfa0fc242ull); KJ_EXPECT(generateGroupId(0xe682ab4cf923a417ull, 7) == 0x9ea0b19b37fb4435ull); KJ_EXPECT(typeId() == 0xe682ab4cf923a417ull); KJ_EXPECT(typeId() == 0xdebf55bbfa0fc242ull); KJ_EXPECT(typeId() == 0x9ea0b19b37fb4435ull); // Methods of TestInterface. KJ_EXPECT(generateMethodParamsId(0x88eb12a0e0af92b2ull, 0, false) == 0xb874edc0d559b391ull); KJ_EXPECT(generateMethodParamsId(0x88eb12a0e0af92b2ull, 0, true) == 0xb04fcaddab714ba4ull); KJ_EXPECT(generateMethodParamsId(0x88eb12a0e0af92b2ull, 1, false) == 0xd044893357b42568ull); KJ_EXPECT(generateMethodParamsId(0x88eb12a0e0af92b2ull, 1, true) == 0x9bf141df4247d52full); } } // namespace } // namespace compiler } // namespace capnp capnproto-c++-0.8.0/src/capnp/compiler/capnpc-c++.c++0000644000175000017500000036713513650101756022601 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This program is a code generator plugin for `capnp compile` which generates C++ code. #include #include "../serialize.h" #include #include #include #include #include #include #include "../schema-loader.h" #include "../dynamic.h" #include #include #include #include #include #include #include #if _WIN32 #define WIN32_LEAN_AND_MEAN // ::eyeroll:: #include #include #undef VOID #undef CONST #else #include #endif #if HAVE_CONFIG_H #include "config.h" #endif #ifndef VERSION #define VERSION "(unknown)" #endif namespace capnp { namespace { static constexpr uint64_t NAMESPACE_ANNOTATION_ID = 0xb9c6f99ebf805f2cull; static constexpr uint64_t NAME_ANNOTATION_ID = 0xf264a779fef191ceull; bool hasDiscriminantValue(const schema::Field::Reader& reader) { return reader.getDiscriminantValue() != schema::Field::NO_DISCRIMINANT; } void enumerateDeps(schema::Type::Reader type, std::set& deps) { switch (type.which()) { case schema::Type::STRUCT: deps.insert(type.getStruct().getTypeId()); break; case schema::Type::ENUM: deps.insert(type.getEnum().getTypeId()); break; case schema::Type::INTERFACE: deps.insert(type.getInterface().getTypeId()); break; case schema::Type::LIST: enumerateDeps(type.getList().getElementType(), deps); break; default: break; } } void enumerateDeps(schema::Node::Reader node, std::set& deps) { switch (node.which()) { case schema::Node::STRUCT: { auto structNode = node.getStruct(); for (auto field: structNode.getFields()) { switch (field.which()) { case schema::Field::SLOT: enumerateDeps(field.getSlot().getType(), deps); break; case schema::Field::GROUP: deps.insert(field.getGroup().getTypeId()); break; } } if (structNode.getIsGroup()) { deps.insert(node.getScopeId()); } break; } case schema::Node::INTERFACE: { auto interfaceNode = node.getInterface(); for (auto superclass: interfaceNode.getSuperclasses()) { deps.insert(superclass.getId()); } for (auto method: interfaceNode.getMethods()) { deps.insert(method.getParamStructType()); deps.insert(method.getResultStructType()); } break; } default: break; } } struct OrderByName { template inline bool operator()(const T& a, const T& b) const { return a.getProto().getName() < b.getProto().getName(); } }; template kj::Array makeMembersByName(MemberList&& members) { auto sorted = KJ_MAP(member, members) { return member; }; std::sort(sorted.begin(), sorted.end(), OrderByName()); return KJ_MAP(member, sorted) { return member.getIndex(); }; } kj::StringPtr baseName(kj::StringPtr path) { KJ_IF_MAYBE(slashPos, path.findLast('/')) { return path.slice(*slashPos + 1); } else { return path; } } kj::String safeIdentifier(kj::StringPtr identifier) { // Given a desired identifier name, munge it to make it safe for use in generated code. // // If the identifier is a keyword, this adds an underscore to the end. static const std::set keywords({ "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", "compl", "const", "constexpr", "const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" }); if (keywords.count(identifier) > 0) { return kj::str(identifier, '_'); } else { return kj::heapString(identifier); } } // ======================================================================================= class CppTypeName { // Used to build a C++ type name string. This is complicated in the presence of templates, // because we must add the "typename" and "template" disambiguator keywords as needed. public: inline CppTypeName(): isArgDependent(false), needsTypename(false), hasInterfaces_(false), hasDisambiguatedTemplate_(false) {} CppTypeName(CppTypeName&& other) = default; CppTypeName(const CppTypeName& other) : name(kj::strTree(other.name.flatten())), isArgDependent(other.isArgDependent), needsTypename(other.needsTypename), hasInterfaces_(other.hasInterfaces_), hasDisambiguatedTemplate_(other.hasDisambiguatedTemplate_) {} CppTypeName& operator=(CppTypeName&& other) = default; CppTypeName& operator=(const CppTypeName& other) { name = kj::strTree(other.name.flatten()); isArgDependent = other.isArgDependent; needsTypename = other.needsTypename; return *this; } static CppTypeName makeRoot() { return CppTypeName(kj::strTree(" "), false); } static CppTypeName makeNamespace(kj::StringPtr name) { return CppTypeName(kj::strTree(" ::", name), false); } static CppTypeName makeTemplateParam(kj::StringPtr name) { return CppTypeName(kj::strTree(name), true); } static CppTypeName makePrimitive(kj::StringPtr name) { return CppTypeName(kj::strTree(name), false); } void addMemberType(kj::StringPtr innerName) { // Append "::innerName" to refer to a member. name = kj::strTree(kj::mv(name), "::", innerName); needsTypename = isArgDependent; } void addMemberValue(kj::StringPtr innerName) { // Append "::innerName" to refer to a member. name = kj::strTree(kj::mv(name), "::", innerName); needsTypename = false; } bool hasDisambiguatedTemplate() { return hasDisambiguatedTemplate_; } void addMemberTemplate(kj::StringPtr innerName, kj::Array&& params) { // Append "::innerName". // // If necessary, add the "template" disambiguation keyword in front of `innerName`. bool parentIsArgDependent = isArgDependent; needsTypename = parentIsArgDependent; hasDisambiguatedTemplate_ = hasDisambiguatedTemplate_ || parentIsArgDependent; name = kj::strTree(kj::mv(name), parentIsArgDependent ? "::template " : "::", innerName, '<', kj::StringTree(KJ_MAP(p, params) { if (p.isArgDependent) isArgDependent = true; if (p.hasInterfaces_) hasInterfaces_ = true; if (p.hasDisambiguatedTemplate_) hasDisambiguatedTemplate_ = true; return kj::strTree(kj::mv(p)); }, ", "), '>'); } void setHasInterfaces() { hasInterfaces_ = true; } bool hasInterfaces() { return hasInterfaces_; } kj::StringTree strNoTypename() const & { return name.flatten(); } kj::StringTree strNoTypename() && { return kj::mv(name); } // Stringify but never prefix with `typename`. Use in contexts where `typename` is implicit. private: kj::StringTree name; bool isArgDependent; // Does the name contain any template-argument-dependent types? bool needsTypename; // Does the name require a prefix of "typename"? bool hasInterfaces_; // Does this type name refer to any interface types? If so it may need to be #ifdefed out in // lite mode. bool hasDisambiguatedTemplate_; // Whether the type name contains a template type that had to be disambiguated using the // "template" keyword, e.g. "Foo::template Bar". // // TODO(msvc): We only track this because MSVC seems to get confused by it in some weird cases. inline CppTypeName(kj::StringTree&& name, bool isArgDependent) : name(kj::mv(name)), isArgDependent(isArgDependent), needsTypename(false), hasInterfaces_(false), hasDisambiguatedTemplate_(false) {} friend kj::StringTree KJ_STRINGIFY(CppTypeName&& typeName); friend kj::String KJ_STRINGIFY(const CppTypeName& typeName); }; kj::StringTree KJ_STRINGIFY(CppTypeName&& typeName) { if (typeName.needsTypename) { return kj::strTree("typename ", kj::mv(typeName.name)); } else { return kj::mv(typeName.name); } } kj::String KJ_STRINGIFY(const CppTypeName& typeName) { if (typeName.needsTypename) { return kj::str("typename ", typeName.name); } else { return typeName.name.flatten(); } } CppTypeName whichKind(schema::Type::Which which) { // Make a CppTypeName representing the capnp::Kind value for the given schema type. This makes // CppTypeName conflate types and values, but this is all just a hack for MSVC's benefit. Its // primary use is as a non-type template parameter to `capnp::List` -- normally the Kind K // is deduced via SFINAE, but MSVC just can't do it in certain cases, such as when a nested type // of `capnp::List` is the return type of a function, and the element type T is a template // instantiation. switch (which) { case schema::Type::VOID: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::BOOL: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::INT8: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::INT16: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::INT32: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::INT64: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::UINT8: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::UINT16: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::UINT32: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::UINT64: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::FLOAT32: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::FLOAT64: return CppTypeName::makePrimitive(" ::capnp::Kind::PRIMITIVE"); case schema::Type::TEXT: return CppTypeName::makePrimitive(" ::capnp::Kind::BLOB"); case schema::Type::DATA: return CppTypeName::makePrimitive(" ::capnp::Kind::BLOB"); case schema::Type::ENUM: return CppTypeName::makePrimitive(" ::capnp::Kind::ENUM"); case schema::Type::STRUCT: return CppTypeName::makePrimitive(" ::capnp::Kind::STRUCT"); case schema::Type::INTERFACE: return CppTypeName::makePrimitive(" ::capnp::Kind::INTERFACE"); case schema::Type::LIST: return CppTypeName::makePrimitive(" ::capnp::Kind::LIST"); case schema::Type::ANY_POINTER: return CppTypeName::makePrimitive(" ::capnp::Kind::OTHER"); } KJ_UNREACHABLE; } // ======================================================================================= class CapnpcCppMain { public: CapnpcCppMain(kj::ProcessContext& context): context(context) {} kj::MainFunc getMain() { return kj::MainBuilder(context, "Cap'n Proto C++ plugin version " VERSION, "This is a Cap'n Proto compiler plugin which generates C++ code. " "It is meant to be run using the Cap'n Proto compiler, e.g.:\n" " capnp compile -oc++ foo.capnp") .callAfterParsing(KJ_BIND_METHOD(*this, run)) .build(); } private: kj::ProcessContext& context; SchemaLoader schemaLoader; std::unordered_set usedImports; bool hasInterfaces = false; CppTypeName cppFullName(Schema schema, kj::Maybe method) { return cppFullName(schema, schema, method); } CppTypeName cppFullName(Schema schema, Schema brand, kj::Maybe method) { auto node = schema.getProto(); if (node.getScopeId() == 0) { // This is the file-level scope. Search for the namespace annotation. KJ_REQUIRE(node.isFile(), "Non-file had scopeId zero; perhaps it's a method param / result struct?"); usedImports.insert(node.getId()); KJ_IF_MAYBE(ns, annotationValue(node, NAMESPACE_ANNOTATION_ID)) { return CppTypeName::makeNamespace(ns->getText()); } else { return CppTypeName::makeRoot(); } } else { // This is a named type. // Figure out what name to use. Schema parent = schemaLoader.get(node.getScopeId()); kj::StringPtr unqualifiedName; kj::String ownUnqualifiedName; KJ_IF_MAYBE(annotatedName, annotationValue(node, NAME_ANNOTATION_ID)) { // The node's name has been overridden for C++ by an annotation. unqualifiedName = annotatedName->getText(); } else { // Search among the parent's nested nodes to for this node, in order to determine its name. auto parentProto = parent.getProto(); for (auto nested: parentProto.getNestedNodes()) { if (nested.getId() == node.getId()) { unqualifiedName = nested.getName(); break; } } if (unqualifiedName == nullptr) { // Hmm, maybe it's a group node? if (parentProto.isStruct()) { for (auto field: parentProto.getStruct().getFields()) { if (field.isGroup() && field.getGroup().getTypeId() == node.getId()) { ownUnqualifiedName = toTitleCase(protoName(field)); unqualifiedName = ownUnqualifiedName; break; } } } } KJ_REQUIRE(unqualifiedName != nullptr, "A schema Node's supposed scope did not contain the node as a NestedNode."); } auto result = cppFullName(parent, brand, method); // Construct the generic arguments. auto params = node.getParameters(); if (params.size() > 0) { auto args = brand.getBrandArgumentsAtScope(node.getId()); #if 0 // Figure out exactly how many params are not bound to AnyPointer. // TODO(msvc): In a few obscure cases, MSVC does not like empty template pramater lists, // even if all parameters have defaults. So, we give in and explicitly list all // parameters in our generated code for now. Try again later. uint paramCount = 0; for (uint i: kj::indices(params)) { auto arg = args[i]; if (arg.which() != schema::Type::ANY_POINTER || arg.getBrandParameter() != nullptr || (arg.getImplicitParameter() != nullptr && method != nullptr)) { paramCount = i + 1; } } #else uint paramCount = params.size(); #endif result.addMemberTemplate(unqualifiedName, KJ_MAP(i, kj::range(0u, paramCount)) { return typeName(args[i], method); }); } else { result.addMemberType(unqualifiedName); } return result; } } kj::String toUpperCase(kj::StringPtr name) { kj::Vector result(name.size() + 4); for (char c: name) { if ('a' <= c && c <= 'z') { result.add(c - 'a' + 'A'); } else if (result.size() > 0 && 'A' <= c && c <= 'Z') { result.add('_'); result.add(c); } else { result.add(c); } } if (result.size() == 4 && memcmp(result.begin(), "NULL", 4) == 0) { // NULL probably collides with a macro. result.add('_'); } result.add('\0'); return kj::String(result.releaseAsArray()); } kj::String toTitleCase(kj::StringPtr name) { kj::String result = kj::heapString(name); if ('a' <= result[0] && result[0] <= 'z') { result[0] = result[0] - 'a' + 'A'; } return kj::mv(result); } CppTypeName typeName(Type type, kj::Maybe method) { switch (type.which()) { case schema::Type::VOID: return CppTypeName::makePrimitive(" ::capnp::Void"); case schema::Type::BOOL: return CppTypeName::makePrimitive("bool"); case schema::Type::INT8: return CppTypeName::makePrimitive(" ::int8_t"); case schema::Type::INT16: return CppTypeName::makePrimitive(" ::int16_t"); case schema::Type::INT32: return CppTypeName::makePrimitive(" ::int32_t"); case schema::Type::INT64: return CppTypeName::makePrimitive(" ::int64_t"); case schema::Type::UINT8: return CppTypeName::makePrimitive(" ::uint8_t"); case schema::Type::UINT16: return CppTypeName::makePrimitive(" ::uint16_t"); case schema::Type::UINT32: return CppTypeName::makePrimitive(" ::uint32_t"); case schema::Type::UINT64: return CppTypeName::makePrimitive(" ::uint64_t"); case schema::Type::FLOAT32: return CppTypeName::makePrimitive("float"); case schema::Type::FLOAT64: return CppTypeName::makePrimitive("double"); case schema::Type::TEXT: return CppTypeName::makePrimitive(" ::capnp::Text"); case schema::Type::DATA: return CppTypeName::makePrimitive(" ::capnp::Data"); case schema::Type::ENUM: return cppFullName(type.asEnum(), method); case schema::Type::STRUCT: return cppFullName(type.asStruct(), method); case schema::Type::INTERFACE: { auto result = cppFullName(type.asInterface(), method); result.setHasInterfaces(); return result; } case schema::Type::LIST: { CppTypeName result = CppTypeName::makeNamespace("capnp"); auto params = kj::heapArrayBuilder(2); auto list = type.asList(); params.add(typeName(list.getElementType(), method)); params.add(whichKind(list.whichElementType())); result.addMemberTemplate("List", params.finish()); return result; } case schema::Type::ANY_POINTER: KJ_IF_MAYBE(param, type.getBrandParameter()) { return CppTypeName::makeTemplateParam(schemaLoader.get(param->scopeId).getProto() .getParameters()[param->index].getName()); } else KJ_IF_MAYBE(param, type.getImplicitParameter()) { KJ_IF_MAYBE(m, method) { auto params = m->getProto().getImplicitParameters(); KJ_REQUIRE(param->index < params.size()); return CppTypeName::makeTemplateParam(params[param->index].getName()); } else { return CppTypeName::makePrimitive(" ::capnp::AnyPointer"); } } else { switch (type.whichAnyPointerKind()) { case schema::Type::AnyPointer::Unconstrained::ANY_KIND: return CppTypeName::makePrimitive(" ::capnp::AnyPointer"); case schema::Type::AnyPointer::Unconstrained::STRUCT: return CppTypeName::makePrimitive(" ::capnp::AnyStruct"); case schema::Type::AnyPointer::Unconstrained::LIST: return CppTypeName::makePrimitive(" ::capnp::AnyList"); case schema::Type::AnyPointer::Unconstrained::CAPABILITY: hasInterfaces = true; // Probably need to #include . return CppTypeName::makePrimitive(" ::capnp::Capability"); } KJ_UNREACHABLE; } } KJ_UNREACHABLE; } template kj::Maybe annotationValue(P proto, uint64_t annotationId) { for (auto annotation: proto.getAnnotations()) { if (annotation.getId() == annotationId) { return annotation.getValue(); } } return nullptr; } template kj::StringPtr protoName(P proto) { KJ_IF_MAYBE(name, annotationValue(proto, NAME_ANNOTATION_ID)) { return name->getText(); } else { return proto.getName(); } } kj::StringTree literalValue(Type type, schema::Value::Reader value) { switch (value.which()) { case schema::Value::VOID: return kj::strTree(" ::capnp::VOID"); case schema::Value::BOOL: return kj::strTree(value.getBool() ? "true" : "false"); case schema::Value::INT8: return kj::strTree(value.getInt8()); case schema::Value::INT16: return kj::strTree(value.getInt16()); case schema::Value::INT32: return kj::strTree(value.getInt32()); case schema::Value::INT64: return kj::strTree(value.getInt64(), "ll"); case schema::Value::UINT8: return kj::strTree(value.getUint8(), "u"); case schema::Value::UINT16: return kj::strTree(value.getUint16(), "u"); case schema::Value::UINT32: return kj::strTree(value.getUint32(), "u"); case schema::Value::UINT64: return kj::strTree(value.getUint64(), "llu"); case schema::Value::FLOAT32: { auto text = kj::str(value.getFloat32()); if (text.findFirst('.') == nullptr && text.findFirst('e') == nullptr && text.findFirst('E') == nullptr) { text = kj::str(text, ".0"); } return kj::strTree(kj::mv(text), "f"); } case schema::Value::FLOAT64: return kj::strTree(value.getFloat64()); case schema::Value::ENUM: { EnumSchema schema = type.asEnum(); if (value.getEnum() < schema.getEnumerants().size()) { return kj::strTree( cppFullName(schema, nullptr), "::", toUpperCase(protoName(schema.getEnumerants()[value.getEnum()].getProto()))); } else { return kj::strTree("static_cast<", cppFullName(schema, nullptr), ">(", value.getEnum(), ")"); } } case schema::Value::TEXT: case schema::Value::DATA: case schema::Value::STRUCT: case schema::Value::INTERFACE: case schema::Value::LIST: case schema::Value::ANY_POINTER: KJ_FAIL_REQUIRE("literalValue() can only be used on primitive types."); } KJ_UNREACHABLE; } // ----------------------------------------------------------------- class TemplateContext { public: TemplateContext(): parent(nullptr) {} explicit TemplateContext(schema::Node::Reader node) : parent(nullptr), node(node) {} TemplateContext(const TemplateContext& parent, kj::StringPtr name, schema::Node::Reader node) : parent(parent), name(name), node(node) {} bool hasParams() const { return node.getParameters().size() > 0; } bool isGeneric() const { return node.getIsGeneric(); } kj::Maybe getParent() const { return parent; } kj::StringPtr getName() const { return name; } kj::StringTree decl(bool withDefaults, kj::StringPtr suffix = nullptr) const { // "template " for this type. Includes default assignments // ("= ::capnp::AnyPointer") if `withDefaults` is true. Returns empty string if this type // is not parameterized. auto params = node.getParameters(); if (params.size() == 0) { return kj::strTree(); } else { return kj::strTree( "template <", kj::StringTree(KJ_MAP(p, params) { return kj::strTree("typename ", p.getName(), suffix, withDefaults ? " = ::capnp::AnyPointer" : ""); }, ", "), ">\n"); } } kj::StringTree allDecls() const { // Decl for each generic parent plus this type, one per line. return kj::strTree(parentDecls(), decl(false)); } kj::StringTree parentDecls() const { // Decls just for the parents. KJ_IF_MAYBE(p, parent) { return p->allDecls(); } else { return kj::strTree(); } } kj::StringTree args(kj::StringPtr suffix = nullptr) const { // "" for this type. auto params = node.getParameters(); if (params.size() == 0) { return kj::strTree(); } else { return kj::strTree( "<", kj::StringTree(KJ_MAP(p, params) { return kj::strTree(p.getName(), suffix); }, ", "), ">"); } } kj::StringTree allArgs() const { // "S, T, U, V" -- listing all args for all enclosing scopes starting from the outermost. auto params = node.getParameters(); kj::StringTree self(KJ_MAP(p, params) { return kj::strTree(p.getName()); }, ", "); kj::StringTree up; KJ_IF_MAYBE(p, parent) { up = p->allArgs(); } if (self.size() == 0) { return up; } else if (up.size() == 0) { return self; } else { return kj::strTree(kj::mv(up), ", ", kj::mv(self)); } } std::map::Reader> getScopeMap() const { std::map::Reader> result; const TemplateContext* current = this; for (;;) { auto params = current->node.getParameters(); if (params.size() > 0) { result[current->node.getId()] = params; } KJ_IF_MAYBE(p, current->parent) { current = p; } else { return result; } } } private: kj::Maybe parent; kj::StringPtr name; schema::Node::Reader node; }; struct BrandInitializerText { kj::StringTree scopes; kj::StringTree bindings; kj::StringTree dependencies; size_t dependencyCount; // TODO(msvc): `dependencyCount` is the number of individual dependency definitions in // `dependencies`. It's a hack to allow makeGenericDefinitions to hard-code the size of the // `_capnpPrivate::brandDependencies` array into the definition of // `_capnpPrivate::specificBrand::dependencyCount`. This is necessary because MSVC cannot // deduce the size of `brandDependencies` if it is nested under a class template. It's // probably this demoralizingly deferred bug: // https://connect.microsoft.com/VisualStudio/feedback/details/759407/can-not-get-size-of-static-array-defined-in-class-template }; BrandInitializerText makeBrandInitializers( const TemplateContext& templateContext, Schema schema) { auto scopeMap = templateContext.getScopeMap(); auto scopes = kj::heapArrayBuilder(scopeMap.size()); kj::Vector bindings(scopeMap.size() * 2); // (estimate two params per scope) for (auto& scope: scopeMap) { scopes.add(kj::strTree(" { ", "0x", kj::hex(scope.first), ", " "brandBindings + ", bindings.size(), ", ", scope.second.size(), ", " "false" "},\n")); for (auto param: scope.second) { bindings.add(kj::strTree(" ::capnp::_::brandBindingFor<", param.getName(), ">(),\n")); } } auto depMap = makeBrandDepMap(templateContext, schema); auto dependencyCount = depMap.size(); return { kj::strTree("{\n", scopes.finish(), "}"), kj::strTree("{\n", bindings.releaseAsArray(), "}"), makeBrandDepInitializers(kj::mv(depMap)), dependencyCount }; } std::map makeBrandDepMap(const TemplateContext& templateContext, Schema schema) { // Build deps. This is separate from makeBrandDepInitializers to give calling code the // opportunity to count the number of dependencies, to calculate array sizes. std::map depMap; #define ADD_DEP(kind, index, ...) \ { \ uint location = _::RawBrandedSchema::makeDepLocation( \ _::RawBrandedSchema::DepKind::kind, index); \ KJ_IF_MAYBE(dep, makeBrandDepInitializer(__VA_ARGS__)) { \ depMap[location] = kj::mv(*dep); \ } \ } switch (schema.getProto().which()) { case schema::Node::FILE: case schema::Node::ENUM: case schema::Node::ANNOTATION: break; case schema::Node::STRUCT: for (auto field: schema.asStruct().getFields()) { ADD_DEP(FIELD, field.getIndex(), field.getType()); } break; case schema::Node::INTERFACE: { auto interface = schema.asInterface(); auto superclasses = interface.getSuperclasses(); for (auto i: kj::indices(superclasses)) { ADD_DEP(SUPERCLASS, i, superclasses[i]); } auto methods = interface.getMethods(); for (auto i: kj::indices(methods)) { auto method = methods[i]; ADD_DEP(METHOD_PARAMS, i, method, method.getParamType(), "Params"); ADD_DEP(METHOD_RESULTS, i, method, method.getResultType(), "Results"); } break; } case schema::Node::CONST: ADD_DEP(CONST_TYPE, 0, schema.asConst().getType()); break; } #undef ADD_DEP return depMap; } kj::StringTree makeBrandDepInitializers(std::map&& depMap) { // Process depMap. Returns a braced initialiser list, or an empty string if there are no // dependencies. if (!depMap.size()) { return kj::strTree(); } auto deps = kj::heapArrayBuilder(depMap.size()); for (auto& entry: depMap) { deps.add(kj::strTree(" { ", entry.first, ", ", kj::mv(entry.second), " },\n")); } return kj::strTree("{\n", kj::StringTree(deps.finish(), ""), "}"); } kj::Maybe makeBrandDepInitializer(Schema type) { // Be careful not to invoke cppFullName() if it would just be thrown away, as doing so will // add the type's declaring file to `usedImports`. In particular, this causes `stream.capnp.h` // to be #included unnecessarily. if (type.isBranded()) { return makeBrandDepInitializer(type, cppFullName(type, nullptr)); } else { return nullptr; } } kj::Maybe makeBrandDepInitializer( InterfaceSchema::Method method, StructSchema type, kj::StringPtr suffix) { auto typeProto = type.getProto(); if (typeProto.getScopeId() == 0) { // This is an auto-generated params or results type. auto name = cppFullName(method.getContainingInterface(), nullptr); auto memberTypeName = kj::str(toTitleCase(protoName(method.getProto())), suffix); if (typeProto.getParameters().size() == 0) { name.addMemberType(memberTypeName); } else { // The method has implicit parameters (i.e. it's generic). For the purpose of the brand // dep initializer, we only want to supply the default AnyPointer variant, so just don't // pass any parameters here. name.addMemberTemplate(memberTypeName, nullptr); } return makeBrandDepInitializer(type, kj::mv(name)); } else { return makeBrandDepInitializer(type); } } kj::Maybe makeBrandDepInitializer(Schema type, CppTypeName name) { if (type.isBranded()) { name.addMemberType("_capnpPrivate"); name.addMemberValue("brand"); return kj::strTree(name, "()"); } else { return nullptr; } } kj::Maybe makeBrandDepInitializer(Type type) { switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::ENUM: case schema::Type::ANY_POINTER: return nullptr; case schema::Type::STRUCT: return makeBrandDepInitializer(type.asStruct()); case schema::Type::INTERFACE: return makeBrandDepInitializer(type.asInterface()); case schema::Type::LIST: return makeBrandDepInitializer(type.asList().getElementType()); } KJ_UNREACHABLE; } // ----------------------------------------------------------------- // Code to deal with "slots" -- determines what to zero out when we clear a group. static uint typeSizeBits(schema::Type::Which whichType) { switch (whichType) { case schema::Type::BOOL: return 1; case schema::Type::INT8: return 8; case schema::Type::INT16: return 16; case schema::Type::INT32: return 32; case schema::Type::INT64: return 64; case schema::Type::UINT8: return 8; case schema::Type::UINT16: return 16; case schema::Type::UINT32: return 32; case schema::Type::UINT64: return 64; case schema::Type::FLOAT32: return 32; case schema::Type::FLOAT64: return 64; case schema::Type::ENUM: return 16; case schema::Type::VOID: 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: KJ_FAIL_REQUIRE("Should only be called for data types."); } KJ_UNREACHABLE; } enum class Section { NONE, DATA, POINTERS }; static Section sectionFor(schema::Type::Which whichType) { switch (whichType) { case schema::Type::VOID: return Section::NONE; case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: return Section::DATA; 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: return Section::POINTERS; } KJ_UNREACHABLE; } static kj::StringPtr maskType(schema::Type::Which whichType) { switch (whichType) { case schema::Type::BOOL: return "bool"; case schema::Type::INT8: return " ::uint8_t"; case schema::Type::INT16: return " ::uint16_t"; case schema::Type::INT32: return " ::uint32_t"; case schema::Type::INT64: return " ::uint64_t"; case schema::Type::UINT8: return " ::uint8_t"; case schema::Type::UINT16: return " ::uint16_t"; case schema::Type::UINT32: return " ::uint32_t"; case schema::Type::UINT64: return " ::uint64_t"; case schema::Type::FLOAT32: return " ::uint32_t"; case schema::Type::FLOAT64: return " ::uint64_t"; case schema::Type::ENUM: return " ::uint16_t"; case schema::Type::VOID: 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: KJ_FAIL_REQUIRE("Should only be called for data types."); } KJ_UNREACHABLE; } struct Slot { schema::Type::Which whichType; uint offset; bool isSupersetOf(Slot other) const { auto section = sectionFor(whichType); if (section != sectionFor(other.whichType)) return false; switch (section) { case Section::NONE: return true; // all voids overlap case Section::DATA: { auto bits = typeSizeBits(whichType); auto start = offset * bits; auto otherBits = typeSizeBits(other.whichType); auto otherStart = other.offset * otherBits; return start <= otherStart && otherStart + otherBits <= start + bits; } case Section::POINTERS: return offset == other.offset; } KJ_UNREACHABLE; } bool operator<(Slot other) const { // Sort by section, then start position, and finally size. auto section = sectionFor(whichType); auto otherSection = sectionFor(other.whichType); if (section < otherSection) { return true; } else if (section > otherSection) { return false; } switch (section) { case Section::NONE: return false; case Section::DATA: { auto bits = typeSizeBits(whichType); auto start = offset * bits; auto otherBits = typeSizeBits(other.whichType); auto otherStart = other.offset * otherBits; if (start < otherStart) { return true; } else if (start > otherStart) { return false; } // Sort larger sizes before smaller. return bits > otherBits; } case Section::POINTERS: return offset < other.offset; } KJ_UNREACHABLE; } }; void getSlots(StructSchema schema, kj::Vector& slots) { auto structProto = schema.getProto().getStruct(); if (structProto.getDiscriminantCount() > 0) { slots.add(Slot { schema::Type::UINT16, structProto.getDiscriminantOffset() }); } for (auto field: schema.getFields()) { auto proto = field.getProto(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); slots.add(Slot { slot.getType().which(), slot.getOffset() }); break; } case schema::Field::GROUP: getSlots(field.getType().asStruct(), slots); break; } } } kj::Array getSortedSlots(StructSchema schema) { // Get a representation of all of the field locations owned by this schema, e.g. so that they // can be zero'd out. kj::Vector slots(schema.getFields().size()); getSlots(schema, slots); std::sort(slots.begin(), slots.end()); kj::Vector result(slots.size()); // All void slots are redundant, and they sort towards the front of the list. By starting out // with `prevSlot` = void, we will end up skipping them all, which is what we want. Slot prevSlot = { schema::Type::VOID, 0 }; for (auto slot: slots) { if (prevSlot.isSupersetOf(slot)) { // This slot is redundant as prevSlot is a superset of it. continue; } // Since all sizes are power-of-two, if two slots overlap at all, one must be a superset of // the other. Since we sort slots by starting position, we know that the only way `slot` // could be a superset of `prevSlot` is if they have the same starting position. However, // since we sort slots with the same starting position by descending size, this is not // possible. KJ_DASSERT(!slot.isSupersetOf(prevSlot)); result.add(slot); prevSlot = slot; } return result.releaseAsArray(); } // ----------------------------------------------------------------- struct DiscriminantChecks { kj::String has; kj::String check; kj::String set; kj::StringTree readerIsDecl; kj::StringTree builderIsDecl; kj::StringTree isDefs; }; DiscriminantChecks makeDiscriminantChecks(kj::StringPtr scope, kj::StringPtr memberName, StructSchema containingStruct, const TemplateContext& templateContext) { auto discrimOffset = containingStruct.getProto().getStruct().getDiscriminantOffset(); kj::String titleCase = toTitleCase(memberName); kj::String upperCase = toUpperCase(memberName); return DiscriminantChecks { kj::str( " if (which() != ", scope, upperCase, ") return false;\n"), kj::str( // Extra parens around the condition are needed for when we're compiling a multi-arg // generic type, which will have a comma, which would otherwise mess up the macro. // Ah, C++. " KJ_IREQUIRE((which() == ", scope, upperCase, "),\n" " \"Must check which() before get()ing a union member.\");\n"), kj::str( " _builder.setDataField<", scope, "Which>(\n" " ::capnp::bounded<", discrimOffset, ">() * ::capnp::ELEMENTS, ", scope, upperCase, ");\n"), kj::strTree(" inline bool is", titleCase, "() const;\n"), kj::strTree(" inline bool is", titleCase, "();\n"), kj::strTree( templateContext.allDecls(), "inline bool ", scope, "Reader::is", titleCase, "() const {\n" " return which() == ", scope, upperCase, ";\n" "}\n", templateContext.allDecls(), "inline bool ", scope, "Builder::is", titleCase, "() {\n" " return which() == ", scope, upperCase, ";\n" "}\n") }; } // ----------------------------------------------------------------- struct FieldText { kj::StringTree readerMethodDecls; kj::StringTree builderMethodDecls; kj::StringTree pipelineMethodDecls; kj::StringTree inlineMethodDefs; }; enum class FieldKind { PRIMITIVE, BLOB, STRUCT, LIST, INTERFACE, ANY_POINTER, BRAND_PARAMETER }; FieldText makeFieldText(kj::StringPtr scope, StructSchema::Field field, const TemplateContext& templateContext) { auto proto = field.getProto(); auto typeSchema = field.getType(); auto baseName = protoName(proto); kj::String titleCase = toTitleCase(baseName); DiscriminantChecks unionDiscrim; if (hasDiscriminantValue(proto)) { unionDiscrim = makeDiscriminantChecks(scope, baseName, field.getContainingStruct(), templateContext); } switch (proto.which()) { case schema::Field::SLOT: // Continue below. break; case schema::Field::GROUP: { auto slots = getSortedSlots(field.getType().asStruct()); return FieldText { kj::strTree( kj::mv(unionDiscrim.readerIsDecl), " inline typename ", titleCase, "::Reader get", titleCase, "() const;\n" "\n"), kj::strTree( kj::mv(unionDiscrim.builderIsDecl), " inline typename ", titleCase, "::Builder get", titleCase, "();\n" " inline typename ", titleCase, "::Builder init", titleCase, "();\n" "\n"), hasDiscriminantValue(proto) ? kj::strTree() : kj::strTree(" inline typename ", titleCase, "::Pipeline get", titleCase, "();\n"), kj::strTree( kj::mv(unionDiscrim.isDefs), templateContext.allDecls(), "inline typename ", scope, titleCase, "::Reader ", scope, "Reader::get", titleCase, "() const {\n", unionDiscrim.check, " return typename ", scope, titleCase, "::Reader(_reader);\n" "}\n", templateContext.allDecls(), "inline typename ", scope, titleCase, "::Builder ", scope, "Builder::get", titleCase, "() {\n", unionDiscrim.check, " return typename ", scope, titleCase, "::Builder(_builder);\n" "}\n", hasDiscriminantValue(proto) ? kj::strTree() : kj::strTree( "#if !CAPNP_LITE\n", templateContext.allDecls(), "inline typename ", scope, titleCase, "::Pipeline ", scope, "Pipeline::get", titleCase, "() {\n", " return typename ", scope, titleCase, "::Pipeline(_typeless.noop());\n" "}\n" "#endif // !CAPNP_LITE\n"), templateContext.allDecls(), "inline typename ", scope, titleCase, "::Builder ", scope, "Builder::init", titleCase, "() {\n", unionDiscrim.set, KJ_MAP(slot, slots) { switch (sectionFor(slot.whichType)) { case Section::NONE: return kj::strTree(); case Section::DATA: return kj::strTree( " _builder.setDataField<", maskType(slot.whichType), ">(::capnp::bounded<", slot.offset, ">() * ::capnp::ELEMENTS, 0);\n"); case Section::POINTERS: return kj::strTree( " _builder.getPointerField(::capnp::bounded<", slot.offset, ">() * ::capnp::POINTERS).clear();\n"); } KJ_UNREACHABLE; }, " return typename ", scope, titleCase, "::Builder(_builder);\n" "}\n") }; } } auto slot = proto.getSlot(); FieldKind kind = FieldKind::PRIMITIVE; kj::String ownedType; CppTypeName type = typeName(typeSchema, nullptr); kj::StringPtr setterDefault; // only for void kj::String defaultMask; // primitives only size_t defaultOffset = 0; // pointers only: offset of the default value within the schema. size_t defaultSize = 0; // blobs only: byte size of the default value. auto defaultBody = slot.getDefaultValue(); switch (typeSchema.which()) { case schema::Type::VOID: kind = FieldKind::PRIMITIVE; setterDefault = " = ::capnp::VOID"; break; #define HANDLE_PRIMITIVE(discrim, typeName, defaultName, suffix) \ case schema::Type::discrim: \ kind = FieldKind::PRIMITIVE; \ if (defaultBody.get##defaultName() != 0) { \ defaultMask = kj::str(defaultBody.get##defaultName(), #suffix); \ } \ break; HANDLE_PRIMITIVE(BOOL, bool, Bool, ); HANDLE_PRIMITIVE(INT8 , ::int8_t , Int8 , ); HANDLE_PRIMITIVE(INT16, ::int16_t, Int16, ); HANDLE_PRIMITIVE(INT32, ::int32_t, Int32, ); HANDLE_PRIMITIVE(INT64, ::int64_t, Int64, ll); HANDLE_PRIMITIVE(UINT8 , ::uint8_t , Uint8 , u); HANDLE_PRIMITIVE(UINT16, ::uint16_t, Uint16, u); HANDLE_PRIMITIVE(UINT32, ::uint32_t, Uint32, u); HANDLE_PRIMITIVE(UINT64, ::uint64_t, Uint64, ull); #undef HANDLE_PRIMITIVE case schema::Type::FLOAT32: kind = FieldKind::PRIMITIVE; if (defaultBody.getFloat32() != 0) { uint32_t mask; float value = defaultBody.getFloat32(); static_assert(sizeof(mask) == sizeof(value), "bug"); memcpy(&mask, &value, sizeof(mask)); defaultMask = kj::str(mask, "u"); } break; case schema::Type::FLOAT64: kind = FieldKind::PRIMITIVE; if (defaultBody.getFloat64() != 0) { uint64_t mask; double value = defaultBody.getFloat64(); static_assert(sizeof(mask) == sizeof(value), "bug"); memcpy(&mask, &value, sizeof(mask)); defaultMask = kj::str(mask, "ull"); } break; case schema::Type::TEXT: kind = FieldKind::BLOB; if (defaultBody.hasText()) { defaultOffset = field.getDefaultValueSchemaOffset(); defaultSize = defaultBody.getText().size(); } break; case schema::Type::DATA: kind = FieldKind::BLOB; if (defaultBody.hasData()) { defaultOffset = field.getDefaultValueSchemaOffset(); defaultSize = defaultBody.getData().size(); } break; case schema::Type::ENUM: kind = FieldKind::PRIMITIVE; if (defaultBody.getEnum() != 0) { defaultMask = kj::str(defaultBody.getEnum(), "u"); } break; case schema::Type::STRUCT: kind = FieldKind::STRUCT; if (defaultBody.hasStruct()) { defaultOffset = field.getDefaultValueSchemaOffset(); } break; case schema::Type::LIST: kind = FieldKind::LIST; if (defaultBody.hasList()) { defaultOffset = field.getDefaultValueSchemaOffset(); } break; case schema::Type::INTERFACE: kind = FieldKind::INTERFACE; break; case schema::Type::ANY_POINTER: if (defaultBody.hasAnyPointer()) { defaultOffset = field.getDefaultValueSchemaOffset(); } if (typeSchema.getBrandParameter() != nullptr) { kind = FieldKind::BRAND_PARAMETER; } else { kind = FieldKind::ANY_POINTER; switch (typeSchema.whichAnyPointerKind()) { case schema::Type::AnyPointer::Unconstrained::ANY_KIND: kind = FieldKind::ANY_POINTER; break; case schema::Type::AnyPointer::Unconstrained::STRUCT: kind = FieldKind::STRUCT; break; case schema::Type::AnyPointer::Unconstrained::LIST: kind = FieldKind::LIST; break; case schema::Type::AnyPointer::Unconstrained::CAPABILITY: kind = FieldKind::INTERFACE; break; } } break; } kj::String defaultMaskParam; if (defaultMask.size() > 0) { defaultMaskParam = kj::str(", ", defaultMask); } uint offset = slot.getOffset(); if (kind == FieldKind::PRIMITIVE) { return FieldText { kj::strTree( kj::mv(unionDiscrim.readerIsDecl), " inline ", type, " get", titleCase, "() const;\n" "\n"), kj::strTree( kj::mv(unionDiscrim.builderIsDecl), " inline ", type, " get", titleCase, "();\n" " inline void set", titleCase, "(", type, " value", setterDefault, ");\n" "\n"), kj::strTree(), kj::strTree( kj::mv(unionDiscrim.isDefs), templateContext.allDecls(), "inline ", type, " ", scope, "Reader::get", titleCase, "() const {\n", unionDiscrim.check, " return _reader.getDataField<", type, ">(\n" " ::capnp::bounded<", offset, ">() * ::capnp::ELEMENTS", defaultMaskParam, ");\n", "}\n" "\n", templateContext.allDecls(), "inline ", type, " ", scope, "Builder::get", titleCase, "() {\n", unionDiscrim.check, " return _builder.getDataField<", type, ">(\n" " ::capnp::bounded<", offset, ">() * ::capnp::ELEMENTS", defaultMaskParam, ");\n", "}\n", templateContext.allDecls(), "inline void ", scope, "Builder::set", titleCase, "(", type, " value) {\n", unionDiscrim.set, " _builder.setDataField<", type, ">(\n" " ::capnp::bounded<", offset, ">() * ::capnp::ELEMENTS, value", defaultMaskParam, ");\n", "}\n" "\n") }; } else if (kind == FieldKind::INTERFACE) { CppTypeName clientType = type; clientType.addMemberType("Client"); return FieldText { kj::strTree( kj::mv(unionDiscrim.readerIsDecl), " inline bool has", titleCase, "() const;\n" "#if !CAPNP_LITE\n" " inline ", clientType, " get", titleCase, "() const;\n" "#endif // !CAPNP_LITE\n" "\n"), kj::strTree( kj::mv(unionDiscrim.builderIsDecl), " inline bool has", titleCase, "();\n" "#if !CAPNP_LITE\n" " inline ", clientType, " get", titleCase, "();\n" " inline void set", titleCase, "(", clientType, "&& value);\n", " inline void set", titleCase, "(", clientType, "& value);\n", " inline void adopt", titleCase, "(::capnp::Orphan<", type, ">&& value);\n" " inline ::capnp::Orphan<", type, "> disown", titleCase, "();\n" "#endif // !CAPNP_LITE\n" "\n"), kj::strTree( hasDiscriminantValue(proto) ? kj::strTree() : kj::strTree( " inline ", clientType, " get", titleCase, "();\n")), kj::strTree( kj::mv(unionDiscrim.isDefs), templateContext.allDecls(), "inline bool ", scope, "Reader::has", titleCase, "() const {\n", unionDiscrim.has, " return !_reader.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS).isNull();\n" "}\n", templateContext.allDecls(), "inline bool ", scope, "Builder::has", titleCase, "() {\n", unionDiscrim.has, " return !_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS).isNull();\n" "}\n" "#if !CAPNP_LITE\n", templateContext.allDecls(), "inline ", clientType, " ", scope, "Reader::get", titleCase, "() const {\n", unionDiscrim.check, " return ::capnp::_::PointerHelpers<", type, ">::get(_reader.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n", templateContext.allDecls(), "inline ", clientType, " ", scope, "Builder::get", titleCase, "() {\n", unionDiscrim.check, " return ::capnp::_::PointerHelpers<", type, ">::get(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n", hasDiscriminantValue(proto) ? kj::strTree() : kj::strTree( templateContext.allDecls(), "inline ", clientType, " ", scope, "Pipeline::get", titleCase, "() {\n", " return ", clientType, "(_typeless.getPointerField(", offset, ").asCap());\n" "}\n"), templateContext.allDecls(), "inline void ", scope, "Builder::set", titleCase, "(", clientType, "&& cap) {\n", unionDiscrim.set, " ::capnp::_::PointerHelpers<", type, ">::set(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), kj::mv(cap));\n" "}\n", templateContext.allDecls(), "inline void ", scope, "Builder::set", titleCase, "(", clientType, "& cap) {\n", unionDiscrim.set, " ::capnp::_::PointerHelpers<", type, ">::set(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), cap);\n" "}\n", templateContext.allDecls(), "inline void ", scope, "Builder::adopt", titleCase, "(\n" " ::capnp::Orphan<", type, ">&& value) {\n", unionDiscrim.set, " ::capnp::_::PointerHelpers<", type, ">::adopt(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), kj::mv(value));\n" "}\n", templateContext.allDecls(), "inline ::capnp::Orphan<", type, "> ", scope, "Builder::disown", titleCase, "() {\n", unionDiscrim.check, " return ::capnp::_::PointerHelpers<", type, ">::disown(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n" "#endif // !CAPNP_LITE\n" "\n") }; } else if (kind == FieldKind::ANY_POINTER) { return FieldText { kj::strTree( kj::mv(unionDiscrim.readerIsDecl), " inline bool has", titleCase, "() const;\n" " inline ::capnp::AnyPointer::Reader get", titleCase, "() const;\n" "\n"), kj::strTree( kj::mv(unionDiscrim.builderIsDecl), " inline bool has", titleCase, "();\n" " inline ::capnp::AnyPointer::Builder get", titleCase, "();\n" " inline ::capnp::AnyPointer::Builder init", titleCase, "();\n" "\n"), kj::strTree(), kj::strTree( kj::mv(unionDiscrim.isDefs), templateContext.allDecls(), "inline bool ", scope, "Reader::has", titleCase, "() const {\n", unionDiscrim.has, " return !_reader.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS).isNull();\n" "}\n", templateContext.allDecls(), "inline bool ", scope, "Builder::has", titleCase, "() {\n", unionDiscrim.has, " return !_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS).isNull();\n" "}\n", templateContext.allDecls(), "inline ::capnp::AnyPointer::Reader ", scope, "Reader::get", titleCase, "() const {\n", unionDiscrim.check, " return ::capnp::AnyPointer::Reader(_reader.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n", templateContext.allDecls(), "inline ::capnp::AnyPointer::Builder ", scope, "Builder::get", titleCase, "() {\n", unionDiscrim.check, " return ::capnp::AnyPointer::Builder(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n", templateContext.allDecls(), "inline ::capnp::AnyPointer::Builder ", scope, "Builder::init", titleCase, "() {\n", unionDiscrim.set, " auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" " result.clear();\n" " return result;\n" "}\n" "\n") }; } else { // Blob, struct, list, or template param. These have only minor differences. uint64_t typeId = field.getContainingStruct().getProto().getId(); kj::String defaultParam = defaultOffset == 0 ? kj::str() : kj::str( ",\n ::capnp::schemas::bp_", kj::hex(typeId), " + ", defaultOffset, defaultSize == 0 ? kj::strTree() : kj::strTree(", ", defaultSize)); bool shouldIncludeStructInit = kind == FieldKind::STRUCT || kind == FieldKind::BRAND_PARAMETER; bool shouldIncludeSizedInit = kind != FieldKind::STRUCT || kind == FieldKind::BRAND_PARAMETER; bool shouldIncludePipelineGetter = !hasDiscriminantValue(proto) && (kind == FieldKind::STRUCT || kind == FieldKind::BRAND_PARAMETER); bool shouldIncludeArrayInitializer = false; bool shouldExcludeInLiteMode = type.hasInterfaces(); bool shouldTemplatizeInit = typeSchema.which() == schema::Type::ANY_POINTER && kind != FieldKind::BRAND_PARAMETER; CppTypeName elementReaderType; if (typeSchema.isList()) { bool primitiveElement = false; bool interface = false; auto elementType = typeSchema.asList().getElementType(); switch (elementType.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: primitiveElement = true; shouldIncludeArrayInitializer = true; break; case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::LIST: primitiveElement = false; shouldIncludeArrayInitializer = true; break; case schema::Type::ANY_POINTER: primitiveElement = false; shouldIncludeArrayInitializer = elementType.getBrandParameter() != nullptr; break; case schema::Type::INTERFACE: primitiveElement = false; interface = true; break; case schema::Type::STRUCT: primitiveElement = false; break; } elementReaderType = typeName(elementType, nullptr); if (!primitiveElement) { if (interface) { elementReaderType.addMemberType("Client"); } else { elementReaderType.addMemberType("Reader"); } } }; CppTypeName readerType; CppTypeName builderType; CppTypeName pipelineType; if (kind == FieldKind::BRAND_PARAMETER) { readerType = CppTypeName::makeNamespace("capnp"); readerType.addMemberTemplate("ReaderFor", kj::heapArray(&type, 1)); builderType = CppTypeName::makeNamespace("capnp"); builderType.addMemberTemplate("BuilderFor", kj::heapArray(&type, 1)); pipelineType = CppTypeName::makeNamespace("capnp"); pipelineType.addMemberTemplate("PipelineFor", kj::heapArray(&type, 1)); } else { readerType = type; readerType.addMemberType("Reader"); builderType = type; builderType.addMemberType("Builder"); pipelineType = type; pipelineType.addMemberType("Pipeline"); } #define COND(cond, ...) ((cond) ? kj::strTree(__VA_ARGS__) : kj::strTree()) return FieldText { kj::strTree( kj::mv(unionDiscrim.readerIsDecl), " inline bool has", titleCase, "() const;\n", COND(shouldExcludeInLiteMode, "#if !CAPNP_LITE\n"), " inline ", readerType, " get", titleCase, "() const;\n", COND(shouldExcludeInLiteMode, "#endif // !CAPNP_LITE\n"), "\n"), kj::strTree( kj::mv(unionDiscrim.builderIsDecl), " inline bool has", titleCase, "();\n", COND(shouldExcludeInLiteMode, "#if !CAPNP_LITE\n"), " inline ", builderType, " get", titleCase, "();\n" " inline void set", titleCase, "(", readerType, " value);\n", COND(shouldIncludeArrayInitializer, " inline void set", titleCase, "(::kj::ArrayPtr value);\n"), COND(shouldIncludeStructInit, COND(shouldTemplatizeInit, " template \n" " inline ::capnp::BuilderFor init", titleCase, "As();\n"), COND(!shouldTemplatizeInit, " inline ", builderType, " init", titleCase, "();\n")), COND(shouldIncludeSizedInit, COND(shouldTemplatizeInit, " template \n" " inline ::capnp::BuilderFor init", titleCase, "As(unsigned int size);\n"), COND(!shouldTemplatizeInit, " inline ", builderType, " init", titleCase, "(unsigned int size);\n")), " inline void adopt", titleCase, "(::capnp::Orphan<", type, ">&& value);\n" " inline ::capnp::Orphan<", type, "> disown", titleCase, "();\n", COND(shouldExcludeInLiteMode, "#endif // !CAPNP_LITE\n"), "\n"), kj::strTree( COND(shouldIncludePipelineGetter, " inline ", pipelineType, " get", titleCase, "();\n")), kj::strTree( kj::mv(unionDiscrim.isDefs), templateContext.allDecls(), "inline bool ", scope, "Reader::has", titleCase, "() const {\n", unionDiscrim.has, " return !_reader.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS).isNull();\n" "}\n", templateContext.allDecls(), "inline bool ", scope, "Builder::has", titleCase, "() {\n", unionDiscrim.has, " return !_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS).isNull();\n" "}\n", COND(shouldExcludeInLiteMode, "#if !CAPNP_LITE\n"), templateContext.allDecls(), "inline ", readerType, " ", scope, "Reader::get", titleCase, "() const {\n", unionDiscrim.check, " return ::capnp::_::PointerHelpers<", type, ">::get(_reader.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS)", defaultParam, ");\n" "}\n", templateContext.allDecls(), "inline ", builderType, " ", scope, "Builder::get", titleCase, "() {\n", unionDiscrim.check, " return ::capnp::_::PointerHelpers<", type, ">::get(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS)", defaultParam, ");\n" "}\n", COND(shouldIncludePipelineGetter, "#if !CAPNP_LITE\n", templateContext.allDecls(), "inline ", pipelineType, " ", scope, "Pipeline::get", titleCase, "() {\n", " return ", pipelineType, "(_typeless.getPointerField(", offset, "));\n" "}\n" "#endif // !CAPNP_LITE\n"), templateContext.allDecls(), "inline void ", scope, "Builder::set", titleCase, "(", readerType, " value) {\n", unionDiscrim.set, " ::capnp::_::PointerHelpers<", type, ">::set(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), value);\n" "}\n", COND(shouldIncludeArrayInitializer, templateContext.allDecls(), "inline void ", scope, "Builder::set", titleCase, "(::kj::ArrayPtr value) {\n", unionDiscrim.set, " ::capnp::_::PointerHelpers<", type, ">::set(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), value);\n" "}\n"), COND(shouldIncludeStructInit, COND(shouldTemplatizeInit, templateContext.allDecls(), "template \n" "inline ::capnp::BuilderFor ", scope, "Builder::init", titleCase, "As() {\n", " static_assert(::capnp::kind() == ::capnp::Kind::STRUCT,\n" " \"", proto.getName(), " must be a struct\");\n", unionDiscrim.set, " return ::capnp::_::PointerHelpers::init(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n"), COND(!shouldTemplatizeInit, templateContext.allDecls(), "inline ", builderType, " ", scope, "Builder::init", titleCase, "() {\n", unionDiscrim.set, " return ::capnp::_::PointerHelpers<", type, ">::init(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n")), COND(shouldIncludeSizedInit, COND(shouldTemplatizeInit, templateContext.allDecls(), "template \n" "inline ::capnp::BuilderFor ", scope, "Builder::init", titleCase, "As(unsigned int size) {\n", " static_assert(::capnp::kind() == ::capnp::Kind::LIST,\n" " \"", proto.getName(), " must be a list\");\n", unionDiscrim.set, " return ::capnp::_::PointerHelpers::init(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), size);\n" "}\n"), COND(!shouldTemplatizeInit, templateContext.allDecls(), "inline ", builderType, " ", scope, "Builder::init", titleCase, "(unsigned int size) {\n", unionDiscrim.set, " return ::capnp::_::PointerHelpers<", type, ">::init(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), size);\n" "}\n")), templateContext.allDecls(), "inline void ", scope, "Builder::adopt", titleCase, "(\n" " ::capnp::Orphan<", type, ">&& value) {\n", unionDiscrim.set, " ::capnp::_::PointerHelpers<", type, ">::adopt(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS), kj::mv(value));\n" "}\n", COND(type.hasDisambiguatedTemplate(), "#ifndef _MSC_VER\n" "// Excluded under MSVC because bugs may make it unable to compile this method.\n"), templateContext.allDecls(), "inline ::capnp::Orphan<", type, "> ", scope, "Builder::disown", titleCase, "() {\n", unionDiscrim.check, " return ::capnp::_::PointerHelpers<", type, ">::disown(_builder.getPointerField(\n" " ::capnp::bounded<", offset, ">() * ::capnp::POINTERS));\n" "}\n", COND(type.hasDisambiguatedTemplate(), "#endif // !_MSC_VER\n"), COND(shouldExcludeInLiteMode, "#endif // !CAPNP_LITE\n"), "\n") }; #undef COND } } // ----------------------------------------------------------------- enum class AsGenericRole { READER, BUILDER, CLIENT }; kj::StringTree makeAsGenericDef(AsGenericRole role, const TemplateContext& templateContext, kj::StringPtr type, kj::String innerType = kj::String()) { if (!templateContext.isGeneric()) { return kj::StringTree(); } kj::StringTree self, up; if (templateContext.hasParams()) { auto returnType = [&]() { return kj::strTree(type, templateContext.args("2"), innerType); }; kj::StringTree asGeneric = kj::strTree("as", innerType.size() ? type : "", "Generic()"); switch (role) { case AsGenericRole::READER: self = kj::strTree( " ", templateContext.decl(true, "2"), " typename ", returnType(), "::Reader ", kj::mv(asGeneric), " {\n" " return typename ", returnType(), "::Reader(_reader);\n" " }\n" "\n"); break; case AsGenericRole::BUILDER: self = kj::strTree( " ", templateContext.decl(true, "2"), " typename ", returnType(), "::Builder ", kj::mv(asGeneric), " {\n" " return typename ", returnType(), "::Builder(_builder);\n" " }\n" "\n"); break; case AsGenericRole::CLIENT: self = kj::strTree( " ", templateContext.decl(true, "2"), " typename ", returnType(), "::Client ", kj::mv(asGeneric), " {\n" " return castAs<", innerType.size() ? "typename " : "", returnType(), ">();\n" " }\n" "\n"); break; } } KJ_IF_MAYBE(p, templateContext.getParent()) { up = makeAsGenericDef(role, *p, p->getName(), kj::strTree( templateContext.hasParams() ? "::template " : "::", type, templateContext.args()).flatten()); } return kj::strTree(kj::mv(self), kj::mv(up)); } // ----------------------------------------------------------------- struct StructText { kj::StringTree outerTypeDecl; kj::StringTree outerTypeDef; kj::StringTree readerBuilderDefs; kj::StringTree inlineMethodDefs; kj::StringTree sourceDefs; }; kj::StringTree makeReaderDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType, const TemplateContext& templateContext, bool isUnion, kj::Array&& methodDecls) { return kj::strTree( templateContext.allDecls(), "class ", fullName, "::Reader {\n" "public:\n" " typedef ", unqualifiedParentType, " Reads;\n" "\n" " Reader() = default;\n" " inline explicit Reader(::capnp::_::StructReader base): _reader(base) {}\n" "\n" " inline ::capnp::MessageSize totalSize() const {\n" " return _reader.totalSize().asPublic();\n" " }\n" "\n" "#if !CAPNP_LITE\n" " inline ::kj::StringTree toString() const {\n" " return ::capnp::_::structString(_reader, *_capnpPrivate::brand());\n" " }\n" "#endif // !CAPNP_LITE\n" "\n", makeAsGenericDef(AsGenericRole::READER, templateContext, unqualifiedParentType), isUnion ? kj::strTree(" inline Which which() const;\n") : kj::strTree(), kj::mv(methodDecls), "private:\n" " ::capnp::_::StructReader _reader;\n" " template \n" " friend struct ::capnp::ToDynamic_;\n" " template \n" " friend struct ::capnp::_::PointerHelpers;\n" " template \n" " friend struct ::capnp::List;\n" " friend class ::capnp::MessageBuilder;\n" " friend class ::capnp::Orphanage;\n" "};\n" "\n"); } kj::StringTree makeBuilderDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType, const TemplateContext& templateContext, bool isUnion, kj::Array&& methodDecls) { return kj::strTree( templateContext.allDecls(), "class ", fullName, "::Builder {\n" "public:\n" " typedef ", unqualifiedParentType, " Builds;\n" "\n" " Builder() = delete; // Deleted to discourage incorrect usage.\n" " // You can explicitly initialize to nullptr instead.\n" " inline Builder(decltype(nullptr)) {}\n" " inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {}\n" " inline operator Reader() const { return Reader(_builder.asReader()); }\n" " inline Reader asReader() const { return *this; }\n" "\n" " inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); }\n" "#if !CAPNP_LITE\n" " inline ::kj::StringTree toString() const { return asReader().toString(); }\n" "#endif // !CAPNP_LITE\n" "\n", makeAsGenericDef(AsGenericRole::BUILDER, templateContext, unqualifiedParentType), isUnion ? kj::strTree(" inline Which which();\n") : kj::strTree(), kj::mv(methodDecls), "private:\n" " ::capnp::_::StructBuilder _builder;\n" " template \n" " friend struct ::capnp::ToDynamic_;\n" " friend class ::capnp::Orphanage;\n", " template \n" " friend struct ::capnp::_::PointerHelpers;\n" "};\n" "\n"); } kj::StringTree makePipelineDef(kj::StringPtr fullName, kj::StringPtr unqualifiedParentType, const TemplateContext& templateContext, bool isUnion, kj::Array&& methodDecls) { return kj::strTree( "#if !CAPNP_LITE\n", templateContext.allDecls(), "class ", fullName, "::Pipeline {\n" "public:\n" " typedef ", unqualifiedParentType, " Pipelines;\n" "\n" " inline Pipeline(decltype(nullptr)): _typeless(nullptr) {}\n" " inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless)\n" " : _typeless(kj::mv(typeless)) {}\n" "\n", kj::mv(methodDecls), "private:\n" " ::capnp::AnyPointer::Pipeline _typeless;\n" " friend class ::capnp::PipelineHook;\n" " template \n" " friend struct ::capnp::ToDynamic_;\n" "};\n" "#endif // !CAPNP_LITE\n" "\n"); } kj::StringTree makeGenericDeclarations(const TemplateContext& templateContext, bool hasBrandDependencies) { // Returns the declarations for the private members of a generic struct/interface; // paired with the definitions from makeGenericDefinitions(). return kj::strTree( " static const ::capnp::_::RawBrandedSchema::Scope brandScopes[];\n" " static const ::capnp::_::RawBrandedSchema::Binding brandBindings[];\n", (!hasBrandDependencies ? "" : " static const ::capnp::_::RawBrandedSchema::Dependency brandDependencies[];\n"), " static const ::capnp::_::RawBrandedSchema specificBrand;\n" " static constexpr ::capnp::_::RawBrandedSchema const* brand() { " "return ::capnp::_::ChooseBrand<_capnpPrivate, ", templateContext.allArgs(), ">::brand(); }\n"); } kj::StringTree makeGenericDefinitions( const TemplateContext& templateContext, kj::StringPtr fullName, kj::StringPtr hexId, BrandInitializerText brandInitializers) { // Returns the definitions for the members from makeGenericDeclarations(). bool hasBrandDependencies = (brandInitializers.dependencies.size() != 0); auto scopeCount = templateContext.getScopeMap().size(); auto dependencyCount = brandInitializers.dependencyCount; kj::String templates = kj::str(templateContext.allDecls()); return kj::strTree( templates, "const ::capnp::_::RawBrandedSchema::Scope ", fullName, "::_capnpPrivate::brandScopes[] = ", kj::mv(brandInitializers.scopes), ";\n", templates, "const ::capnp::_::RawBrandedSchema::Binding ", fullName, "::_capnpPrivate::brandBindings[] = ", kj::mv(brandInitializers.bindings), ";\n", (!hasBrandDependencies ? kj::strTree("") : kj::strTree( templates, "const ::capnp::_::RawBrandedSchema::Dependency ", fullName, "::_capnpPrivate::brandDependencies[] = ", kj::mv(brandInitializers.dependencies), ";\n")), templates, "const ::capnp::_::RawBrandedSchema ", fullName, "::_capnpPrivate::specificBrand = {\n", " &::capnp::schemas::s_", hexId, ", brandScopes, ", (!hasBrandDependencies ? "nullptr" : "brandDependencies"), ",\n", " ", scopeCount, ", ", dependencyCount, ", nullptr\n" "};\n"); } StructText makeStructText(kj::StringPtr scope, kj::StringPtr name, StructSchema schema, kj::Array nestedTypeDecls, const TemplateContext& templateContext) { auto proto = schema.getProto(); KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) { name = annotatedName->getText(); } auto fullName = kj::str(scope, name, templateContext.args()); auto subScope = kj::str(fullName, "::"); auto fieldTexts = KJ_MAP(f, schema.getFields()) { return makeFieldText(subScope, f, templateContext); }; auto structNode = proto.getStruct(); uint discrimOffset = structNode.getDiscriminantOffset(); auto hexId = kj::hex(proto.getId()); kj::String templates = kj::str(templateContext.allDecls()); // Ends with a newline // Private members struct kj::StringTree declareText = kj::strTree( " struct _capnpPrivate {\n" " CAPNP_DECLARE_STRUCT_HEADER(", hexId, ", ", structNode.getDataWordCount(), ", ", structNode.getPointerCount(), ")\n"); kj::StringTree defineText = kj::strTree( "// ", fullName, "\n", templates, "constexpr uint16_t ", fullName, "::_capnpPrivate::dataWordSize;\n", templates, "constexpr uint16_t ", fullName, "::_capnpPrivate::pointerCount;\n" "#if !CAPNP_LITE\n", templates, "constexpr ::capnp::Kind ", fullName, "::_capnpPrivate::kind;\n", templates, "constexpr ::capnp::_::RawSchema const* ", fullName, "::_capnpPrivate::schema;\n"); if (templateContext.isGeneric()) { auto brandInitializers = makeBrandInitializers(templateContext, schema); bool hasDeps = (brandInitializers.dependencies.size() != 0); declareText = kj::strTree(kj::mv(declareText), " #if !CAPNP_LITE\n", makeGenericDeclarations(templateContext, hasDeps), " #endif // !CAPNP_LITE\n"); defineText = kj::strTree(kj::mv(defineText), makeGenericDefinitions( templateContext, fullName, kj::str(hexId), kj::mv(brandInitializers))); } else { declareText = kj::strTree(kj::mv(declareText), " #if !CAPNP_LITE\n" " static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; }\n" " #endif // !CAPNP_LITE\n"); } declareText = kj::strTree(kj::mv(declareText), " };"); defineText = kj::strTree(kj::mv(defineText), "#endif // !CAPNP_LITE\n\n"); // Name of the ::Which type, when applicable. CppTypeName whichName; if (structNode.getDiscriminantCount() != 0) { whichName = cppFullName(schema, nullptr); whichName.addMemberType("Which"); } return StructText { kj::strTree( templateContext.hasParams() ? " " : "", templateContext.decl(true), " struct ", name, ";\n"), kj::strTree( templateContext.parentDecls(), templateContext.decl(scope == nullptr), "struct ", scope, name, " {\n", " ", name, "() = delete;\n" "\n" " class Reader;\n" " class Builder;\n" " class Pipeline;\n", structNode.getDiscriminantCount() == 0 ? kj::strTree() : kj::strTree( " enum Which: uint16_t {\n", KJ_MAP(f, structNode.getFields()) { if (hasDiscriminantValue(f)) { return kj::strTree(" ", toUpperCase(protoName(f)), ",\n"); } else { return kj::strTree(); } }, " };\n"), KJ_MAP(n, nestedTypeDecls) { return kj::mv(n); }, "\n", kj::mv(declareText), "\n", "};\n" "\n"), kj::strTree( makeReaderDef(fullName, name, templateContext, structNode.getDiscriminantCount() != 0, KJ_MAP(f, fieldTexts) { return kj::mv(f.readerMethodDecls); }), makeBuilderDef(fullName, name, templateContext, structNode.getDiscriminantCount() != 0, KJ_MAP(f, fieldTexts) { return kj::mv(f.builderMethodDecls); }), makePipelineDef(fullName, name, templateContext, structNode.getDiscriminantCount() != 0, KJ_MAP(f, fieldTexts) { return kj::mv(f.pipelineMethodDecls); })), kj::strTree( structNode.getDiscriminantCount() == 0 ? kj::strTree() : kj::strTree( templateContext.allDecls(), "inline ", whichName, " ", fullName, "::Reader::which() const {\n" " return _reader.getDataField(\n" " ::capnp::bounded<", discrimOffset, ">() * ::capnp::ELEMENTS);\n" "}\n", templateContext.allDecls(), "inline ", whichName, " ", fullName, "::Builder::which() {\n" " return _builder.getDataField(\n" " ::capnp::bounded<", discrimOffset, ">() * ::capnp::ELEMENTS);\n" "}\n" "\n"), KJ_MAP(f, fieldTexts) { return kj::mv(f.inlineMethodDefs); }), kj::mv(defineText) }; } // ----------------------------------------------------------------- struct MethodText { kj::StringTree clientDecls; kj::StringTree serverDecls; kj::StringTree inlineDefs; kj::StringTree sourceDefs; kj::StringTree dispatchCase; }; MethodText makeMethodText(kj::StringPtr interfaceName, InterfaceSchema::Method method, const TemplateContext& templateContext) { auto proto = method.getProto(); auto name = protoName(proto); auto titleCase = toTitleCase(name); auto paramSchema = method.getParamType(); auto resultSchema = method.getResultType(); auto identifierName = safeIdentifier(name); auto paramProto = paramSchema.getProto(); auto resultProto = resultSchema.getProto(); bool isStreaming = method.isStreaming(); auto implicitParamsReader = proto.getImplicitParameters(); auto implicitParamsBuilder = kj::heapArrayBuilder(implicitParamsReader.size()); for (auto param: implicitParamsReader) { implicitParamsBuilder.add(CppTypeName::makeTemplateParam(param.getName())); } auto implicitParams = implicitParamsBuilder.finish(); kj::String implicitParamsTemplateDecl; if (implicitParams.size() > 0) { implicitParamsTemplateDecl = kj::str( "template <", kj::StringTree(KJ_MAP(p, implicitParams) { return kj::strTree("typename ", p); }, ", "), ">\n"); } CppTypeName interfaceTypeName = cppFullName(method.getContainingInterface(), nullptr); CppTypeName paramType; CppTypeName genericParamType; if (paramProto.getScopeId() == 0) { paramType = interfaceTypeName; if (implicitParams.size() == 0) { paramType.addMemberType(kj::str(titleCase, "Params")); genericParamType = paramType; } else { genericParamType = paramType; genericParamType.addMemberTemplate(kj::str(titleCase, "Params"), nullptr); paramType.addMemberTemplate(kj::str(titleCase, "Params"), kj::heapArray(implicitParams.asPtr())); } } else { paramType = cppFullName(paramSchema, method); genericParamType = cppFullName(paramSchema, nullptr); } CppTypeName resultType; CppTypeName genericResultType; if (isStreaming) { // We don't use resultType or genericResultType in this case. We want to avoid computing them // at all so that we don't end up marking stream.capnp.h in usedImports. } else if (resultProto.getScopeId() == 0) { resultType = interfaceTypeName; if (implicitParams.size() == 0) { resultType.addMemberType(kj::str(titleCase, "Results")); genericResultType = resultType; } else { genericResultType = resultType; genericResultType.addMemberTemplate(kj::str(titleCase, "Results"), nullptr); resultType.addMemberTemplate(kj::str(titleCase, "Results"), kj::heapArray(implicitParams.asPtr())); } } else { resultType = cppFullName(resultSchema, method); genericResultType = cppFullName(resultSchema, nullptr); } kj::String shortParamType = paramProto.getScopeId() == 0 ? kj::str(titleCase, "Params") : kj::str(genericParamType); kj::String shortResultType = resultProto.getScopeId() == 0 || isStreaming ? kj::str(titleCase, "Results") : kj::str(genericResultType); auto interfaceProto = method.getContainingInterface().getProto(); uint64_t interfaceId = interfaceProto.getId(); auto interfaceIdHex = kj::hex(interfaceId); uint16_t methodId = method.getIndex(); // TODO(msvc): Notice that the return type of this method's request function is supposed to be // `::capnp::Request`. If the first template parameter to ::capnp::Request is a // template instantiation, MSVC will sometimes complain that it's unspecialized and can't be // used as a parameter in the return type (error C3203). It is not clear to me under what exact // conditions this bug occurs, but it commonly crops up in test.capnp.h. // // The easiest (and only) workaround I found is to use C++14's return type deduction here, thus // the `CAPNP_AUTO_IF_MSVC()` hackery in the return type declarations below. We're depending on // the fact that that this function has an inline implementation for the deduction to work. auto requestMethodImpl = kj::strTree( templateContext.allDecls(), implicitParamsTemplateDecl, templateContext.isGeneric() ? "CAPNP_AUTO_IF_MSVC(" : "", isStreaming ? kj::strTree("::capnp::StreamingRequest<", paramType, ">") : kj::strTree("::capnp::Request<", paramType, ", ", resultType, ">"), templateContext.isGeneric() ? ")\n" : "\n", interfaceName, "::Client::", name, "Request(::kj::Maybe< ::capnp::MessageSize> sizeHint) {\n", isStreaming ? kj::strTree(" return newStreamingCall<", paramType, ">(\n") : kj::strTree(" return newCall<", paramType, ", ", resultType, ">(\n"), " 0x", interfaceIdHex, "ull, ", methodId, ", sizeHint);\n" "}\n"); return MethodText { kj::strTree( implicitParamsTemplateDecl.size() == 0 ? "" : " ", implicitParamsTemplateDecl, templateContext.isGeneric() ? " CAPNP_AUTO_IF_MSVC(" : " ", isStreaming ? kj::strTree("::capnp::StreamingRequest<", paramType, ">") : kj::strTree("::capnp::Request<", paramType, ", ", resultType, ">"), templateContext.isGeneric() ? ")" : "", " ", name, "Request(\n" " ::kj::Maybe< ::capnp::MessageSize> sizeHint = nullptr);\n"), kj::strTree( paramProto.getScopeId() != 0 ? kj::strTree() : kj::strTree( " typedef ", genericParamType, " ", titleCase, "Params;\n"), resultProto.getScopeId() != 0 ? kj::strTree() : kj::strTree( " typedef ", genericResultType, " ", titleCase, "Results;\n"), isStreaming ? kj::strTree(" typedef ::capnp::StreamingCallContext<", shortParamType, "> ") : kj::strTree( " typedef ::capnp::CallContext<", shortParamType, ", ", shortResultType, "> "), titleCase, "Context;\n" " virtual ::kj::Promise ", identifierName, "(", titleCase, "Context context);\n"), implicitParams.size() == 0 ? kj::strTree() : kj::mv(requestMethodImpl), kj::strTree( implicitParams.size() == 0 ? kj::mv(requestMethodImpl) : kj::strTree(), templateContext.allDecls(), "::kj::Promise ", interfaceName, "::Server::", identifierName, "(", titleCase, "Context) {\n" " return ::capnp::Capability::Server::internalUnimplemented(\n" " \"", interfaceProto.getDisplayName(), "\", \"", name, "\",\n" " 0x", interfaceIdHex, "ull, ", methodId, ");\n" "}\n"), kj::strTree( " case ", methodId, ":\n", isStreaming ? kj::strTree( // For streaming calls, we need to add an evalNow() here so that exceptions thrown // directly from the call can propagate to later calls. If we don't capture the // exception properly then the caller will never find out that this is a streaming // call (indicated by the boolean in the return value) so won't know to propagate // the exception. " return {\n" " kj::evalNow([&]() {\n" " return ", identifierName, "(::capnp::Capability::Server::internalGetTypedStreamingContext<\n" " ", genericParamType, ">(context));\n" " }),\n" " true\n" " };\n") : kj::strTree( // For non-streaming calls we let exceptions just flow through for a little more // efficiency. " return {\n" " ", identifierName, "(::capnp::Capability::Server::internalGetTypedContext<\n" " ", genericParamType, ", ", genericResultType, ">(context)),\n" " false\n" " };\n")) }; } struct InterfaceText { kj::StringTree outerTypeDecl; kj::StringTree outerTypeDef; kj::StringTree clientServerDefs; kj::StringTree inlineMethodDefs; kj::StringTree sourceDefs; }; struct ExtendInfo { CppTypeName typeName; uint64_t id; }; void getTransitiveSuperclasses(InterfaceSchema schema, std::map& map) { if (map.insert(std::make_pair(schema.getProto().getId(), schema)).second) { for (auto sup: schema.getSuperclasses()) { getTransitiveSuperclasses(sup, map); } } } InterfaceText makeInterfaceText(kj::StringPtr scope, kj::StringPtr name, InterfaceSchema schema, kj::Array nestedTypeDecls, const TemplateContext& templateContext) { auto fullName = kj::str(scope, name, templateContext.args()); auto methods = KJ_MAP(m, schema.getMethods()) { return makeMethodText(fullName, m, templateContext); }; auto proto = schema.getProto(); auto hexId = kj::hex(proto.getId()); auto superclasses = KJ_MAP(superclass, schema.getSuperclasses()) { return ExtendInfo { cppFullName(superclass, nullptr), superclass.getProto().getId() }; }; kj::Array transitiveSuperclasses; { std::map map; getTransitiveSuperclasses(schema, map); map.erase(schema.getProto().getId()); transitiveSuperclasses = KJ_MAP(entry, map) { return ExtendInfo { cppFullName(entry.second, nullptr), entry.second.getProto().getId() }; }; } CppTypeName typeName = cppFullName(schema, nullptr); CppTypeName clientName = typeName; clientName.addMemberType("Client"); kj::String templates = kj::str(templateContext.allDecls()); // Ends with a newline // Private members struct kj::StringTree declareText = kj::strTree( " #if !CAPNP_LITE\n" " struct _capnpPrivate {\n" " CAPNP_DECLARE_INTERFACE_HEADER(", hexId, ")\n"); kj::StringTree defineText = kj::strTree( "// ", fullName, "\n", "#if !CAPNP_LITE\n", templates, "constexpr ::capnp::Kind ", fullName, "::_capnpPrivate::kind;\n", templates, "constexpr ::capnp::_::RawSchema const* ", fullName, "::_capnpPrivate::schema;\n"); if (templateContext.isGeneric()) { auto brandInitializers = makeBrandInitializers(templateContext, schema); bool hasDeps = (brandInitializers.dependencies.size() != 0); declareText = kj::strTree(kj::mv(declareText), makeGenericDeclarations(templateContext, hasDeps)); defineText = kj::strTree(kj::mv(defineText), makeGenericDefinitions( templateContext, fullName, kj::str(hexId), kj::mv(brandInitializers))); } else { declareText = kj::strTree(kj::mv(declareText), " static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; }\n"); } declareText = kj::strTree(kj::mv(declareText), " };\n #endif // !CAPNP_LITE"); defineText = kj::strTree(kj::mv(defineText), "#endif // !CAPNP_LITE\n\n"); return InterfaceText { kj::strTree( templateContext.hasParams() ? " " : "", templateContext.decl(true), " struct ", name, ";\n"), kj::strTree( templateContext.parentDecls(), templateContext.decl(scope == nullptr), "struct ", scope, name, " {\n", " ", name, "() = delete;\n" "\n" "#if !CAPNP_LITE\n" " class Client;\n" " class Server;\n" "#endif // !CAPNP_LITE\n" "\n", KJ_MAP(n, nestedTypeDecls) { return kj::mv(n); }, "\n", kj::mv(declareText), "\n" "};\n" "\n"), kj::strTree( "#if !CAPNP_LITE\n", templateContext.allDecls(), "class ", fullName, "::Client\n" " : public virtual ::capnp::Capability::Client", KJ_MAP(s, superclasses) { return kj::strTree(",\n public virtual ", s.typeName.strNoTypename(), "::Client"); }, " {\n" "public:\n" " typedef ", name, " Calls;\n" " typedef ", name, " Reads;\n" "\n" " Client(decltype(nullptr));\n" " explicit Client(::kj::Own< ::capnp::ClientHook>&& hook);\n" " template ()>>\n" " Client(::kj::Own<_t>&& server);\n" " template ()>>\n" " Client(::kj::Promise<_t>&& promise);\n" " Client(::kj::Exception&& exception);\n" " Client(Client&) = default;\n" " Client(Client&&) = default;\n" " Client& operator=(Client& other);\n" " Client& operator=(Client&& other);\n" "\n", makeAsGenericDef(AsGenericRole::CLIENT, templateContext, name), KJ_MAP(m, methods) { return kj::mv(m.clientDecls); }, "\n" "protected:\n" " Client() = default;\n" "};\n" "\n", templateContext.allDecls(), "class ", fullName, "::Server\n" " : public virtual ::capnp::Capability::Server", KJ_MAP(s, superclasses) { return kj::strTree(",\n public virtual ", s.typeName.strNoTypename(), "::Server"); }, " {\n" "public:\n", " typedef ", name, " Serves;\n" "\n" " ::capnp::Capability::Server::DispatchCallResult dispatchCall(\n" " uint64_t interfaceId, uint16_t methodId,\n" " ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context)\n" " override;\n" "\n" "protected:\n", KJ_MAP(m, methods) { return kj::mv(m.serverDecls); }, "\n" " inline ", clientName, " thisCap() {\n" " return ::capnp::Capability::Server::thisCap()\n" " .template castAs<", typeName, ">();\n" " }\n" "\n" " ::capnp::Capability::Server::DispatchCallResult dispatchCallInternal(\n" " uint16_t methodId,\n" " ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context);\n" "};\n" "#endif // !CAPNP_LITE\n" "\n"), kj::strTree( "#if !CAPNP_LITE\n", templateContext.allDecls(), "inline ", fullName, "::Client::Client(decltype(nullptr))\n" " : ::capnp::Capability::Client(nullptr) {}\n", templateContext.allDecls(), "inline ", fullName, "::Client::Client(\n" " ::kj::Own< ::capnp::ClientHook>&& hook)\n" " : ::capnp::Capability::Client(::kj::mv(hook)) {}\n", templateContext.allDecls(), "template \n" "inline ", fullName, "::Client::Client(::kj::Own<_t>&& server)\n" " : ::capnp::Capability::Client(::kj::mv(server)) {}\n", templateContext.allDecls(), "template \n" "inline ", fullName, "::Client::Client(::kj::Promise<_t>&& promise)\n" " : ::capnp::Capability::Client(::kj::mv(promise)) {}\n", templateContext.allDecls(), "inline ", fullName, "::Client::Client(::kj::Exception&& exception)\n" " : ::capnp::Capability::Client(::kj::mv(exception)) {}\n", templateContext.allDecls(), "inline ", clientName, "& ", fullName, "::Client::operator=(Client& other) {\n" " ::capnp::Capability::Client::operator=(other);\n" " return *this;\n" "}\n", templateContext.allDecls(), "inline ", clientName, "& ", fullName, "::Client::operator=(Client&& other) {\n" " ::capnp::Capability::Client::operator=(kj::mv(other));\n" " return *this;\n" "}\n" "\n", KJ_MAP(m, methods) { return kj::mv(m.inlineDefs); }, "#endif // !CAPNP_LITE\n"), kj::strTree( "#if !CAPNP_LITE\n", KJ_MAP(m, methods) { return kj::mv(m.sourceDefs); }, templateContext.allDecls(), "::capnp::Capability::Server::DispatchCallResult ", fullName, "::Server::dispatchCall(\n" " uint64_t interfaceId, uint16_t methodId,\n" " ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) {\n" " switch (interfaceId) {\n" " case 0x", kj::hex(proto.getId()), "ull:\n" " return dispatchCallInternal(methodId, context);\n", KJ_MAP(s, transitiveSuperclasses) { return kj::strTree( " case 0x", kj::hex(s.id), "ull:\n" " return ", s.typeName.strNoTypename(), "::Server::dispatchCallInternal(methodId, context);\n"); }, " default:\n" " return internalUnimplemented(\"", proto.getDisplayName(), "\", interfaceId);\n" " }\n" "}\n", templateContext.allDecls(), "::capnp::Capability::Server::DispatchCallResult ", fullName, "::Server::dispatchCallInternal(\n" " uint16_t methodId,\n" " ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) {\n" " switch (methodId) {\n", KJ_MAP(m, methods) { return kj::mv(m.dispatchCase); }, " default:\n" " (void)context;\n" " return ::capnp::Capability::Server::internalUnimplemented(\n" " \"", proto.getDisplayName(), "\",\n" " 0x", kj::hex(proto.getId()), "ull, methodId);\n" " }\n" "}\n" "#endif // !CAPNP_LITE\n" "\n", kj::mv(defineText)) }; } // ----------------------------------------------------------------- struct ConstText { bool needsSchema; kj::StringTree decl; kj::StringTree def; }; ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema, const TemplateContext& templateContext) { auto proto = schema.getProto(); auto constProto = proto.getConst(); auto type = schema.getType(); auto typeName_ = typeName(type, nullptr); auto upperCase = toUpperCase(name); // Linkage qualifier for non-primitive types. const char* linkage = scope.size() == 0 ? "extern " : "static "; switch (type.which()) { case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::ENUM: return ConstText { false, kj::strTree("static constexpr ", typeName_, ' ', upperCase, " = ", literalValue(schema.getType(), constProto.getValue()), ";\n"), scope.size() == 0 ? kj::strTree() : kj::strTree( // TODO(msvc): MSVC doesn't like definitions of constexprs, but other compilers and // the standard require them. "#ifndef _MSC_VER\n" "constexpr ", typeName_, ' ', scope, upperCase, ";\n" "#endif\n") }; case schema::Type::VOID: case schema::Type::FLOAT32: case schema::Type::FLOAT64: { // TODO(msvc): MSVC doesn't like float- or class-typed constexprs. As soon as this is fixed, // treat VOID, FLOAT32, and FLOAT64 the same as the other primitives. kj::String value = literalValue(schema.getType(), constProto.getValue()).flatten(); return ConstText { false, kj::strTree("static KJ_CONSTEXPR(const) ", typeName_, ' ', upperCase, " CAPNP_NON_INT_CONSTEXPR_DECL_INIT(", value, ");\n"), scope.size() == 0 ? kj::strTree() : kj::strTree( "KJ_CONSTEXPR(const) ", typeName_, ' ', scope, upperCase, " CAPNP_NON_INT_CONSTEXPR_DEF_INIT(", value, ");\n") }; } case schema::Type::TEXT: { kj::String constType = kj::strTree( "::capnp::_::ConstText<", schema.as().size(), ">").flatten(); return ConstText { true, kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"), kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") }; } case schema::Type::DATA: { kj::String constType = kj::strTree( "::capnp::_::ConstData<", schema.as().size(), ">").flatten(); return ConstText { true, kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"), kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") }; } case schema::Type::STRUCT: { kj::String constType = kj::strTree( "::capnp::_::ConstStruct<", typeName_, ">").flatten(); return ConstText { true, kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"), kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") }; } case schema::Type::LIST: { kj::String constType = kj::strTree( "::capnp::_::ConstList<", typeName(type.asList().getElementType(), nullptr), ">") .flatten(); return ConstText { true, kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"), kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") }; } case schema::Type::ANY_POINTER: case schema::Type::INTERFACE: return ConstText { false, kj::strTree(), kj::strTree() }; } KJ_UNREACHABLE; } // ----------------------------------------------------------------- struct NodeText { kj::StringTree outerTypeDecl; kj::StringTree outerTypeDef; kj::StringTree readerBuilderDefs; kj::StringTree inlineMethodDefs; kj::StringTree capnpSchemaDecls; kj::StringTree capnpSchemaDefs; kj::StringTree sourceFileDefs; }; NodeText makeNodeText(kj::StringPtr namespace_, kj::StringPtr scope, kj::StringPtr name, Schema schema, const TemplateContext& parentTemplateContext) { // `templateContext` is something like "template \ntemplate \n" // declaring template parameters for all parent scopes. auto proto = schema.getProto(); KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) { name = annotatedName->getText(); } auto hexId = kj::hex(proto.getId()); TemplateContext templateContext(parentTemplateContext, name, proto); auto subScope = kj::str(scope, name, templateContext.args(), "::"); // Compute nested nodes, including groups. kj::Vector nestedTexts(proto.getNestedNodes().size()); for (auto nested: proto.getNestedNodes()) { nestedTexts.add(makeNodeText( namespace_, subScope, nested.getName(), schemaLoader.getUnbound(nested.getId()),\ templateContext)); }; if (proto.isStruct()) { for (auto field: proto.getStruct().getFields()) { if (field.isGroup()) { nestedTexts.add(makeNodeText( namespace_, subScope, toTitleCase(protoName(field)), schemaLoader.getUnbound(field.getGroup().getTypeId()), templateContext)); } } } else if (proto.isInterface()) { for (auto method: proto.getInterface().getMethods()) { { Schema params = schemaLoader.getUnbound(method.getParamStructType()); auto paramsProto = schemaLoader.getUnbound(method.getParamStructType()).getProto(); if (paramsProto.getScopeId() == 0) { nestedTexts.add(makeNodeText(namespace_, subScope, toTitleCase(kj::str(protoName(method), "Params")), params, templateContext)); } } { Schema results = schemaLoader.getUnbound(method.getResultStructType()); auto resultsProto = schemaLoader.getUnbound(method.getResultStructType()).getProto(); if (resultsProto.getScopeId() == 0) { nestedTexts.add(makeNodeText(namespace_, subScope, toTitleCase(kj::str(protoName(method), "Results")), results, templateContext)); } } } } // Convert the encoded schema to a literal byte array. kj::ArrayPtr rawSchema = schema.asUncheckedMessage(); auto schemaLiteral = kj::StringTree(KJ_MAP(w, rawSchema) { const byte* bytes = reinterpret_cast(&w); return kj::strTree(KJ_MAP(i, kj::range(0, sizeof(word))) { auto text = kj::toCharSequence(kj::implicitCast(bytes[i])); return kj::strTree(kj::repeat(' ', 4 - text.size()), text, ","); }); }, "\n "); auto schemaDecl = kj::strTree( "CAPNP_DECLARE_SCHEMA(", hexId, ");\n"); std::set deps; enumerateDeps(proto, deps); kj::Array membersByName; kj::Array membersByDiscrim; switch (proto.which()) { case schema::Node::STRUCT: { auto structSchema = schema.asStruct(); membersByName = makeMembersByName(structSchema.getFields()); auto builder = kj::heapArrayBuilder(structSchema.getFields().size()); for (auto field: structSchema.getUnionFields()) { builder.add(field.getIndex()); } for (auto field: structSchema.getNonUnionFields()) { builder.add(field.getIndex()); } membersByDiscrim = builder.finish(); break; } case schema::Node::ENUM: membersByName = makeMembersByName(schema.asEnum().getEnumerants()); break; case schema::Node::INTERFACE: membersByName = makeMembersByName(schema.asInterface().getMethods()); break; default: break; } auto brandDeps = makeBrandDepInitializers( makeBrandDepMap(templateContext, schema.getGeneric())); auto schemaDef = kj::strTree( "static const ::capnp::_::AlignedData<", rawSchema.size(), "> b_", hexId, " = {\n" " {", kj::mv(schemaLiteral), " }\n" "};\n" "::capnp::word const* const bp_", hexId, " = b_", hexId, ".words;\n" "#if !CAPNP_LITE\n", deps.size() == 0 ? kj::strTree() : kj::strTree( "static const ::capnp::_::RawSchema* const d_", hexId, "[] = {\n", KJ_MAP(depId, deps) { return kj::strTree(" &s_", kj::hex(depId), ",\n"); }, "};\n"), membersByName.size() == 0 ? kj::strTree() : kj::strTree( "static const uint16_t m_", hexId, "[] = {", kj::StringTree(KJ_MAP(index, membersByName) { return kj::strTree(index); }, ", "), "};\n"), membersByDiscrim.size() == 0 ? kj::strTree() : kj::strTree( "static const uint16_t i_", hexId, "[] = {", kj::StringTree(KJ_MAP(index, membersByDiscrim) { return kj::strTree(index); }, ", "), "};\n"), brandDeps.size() == 0 ? kj::strTree() : kj::strTree( "KJ_CONSTEXPR(const) ::capnp::_::RawBrandedSchema::Dependency bd_", hexId, "[] = ", kj::mv(brandDeps), ";\n"), "const ::capnp::_::RawSchema s_", hexId, " = {\n" " 0x", hexId, ", b_", hexId, ".words, ", rawSchema.size(), ", ", deps.size() == 0 ? kj::strTree("nullptr") : kj::strTree("d_", hexId), ", ", membersByName.size() == 0 ? kj::strTree("nullptr") : kj::strTree("m_", hexId), ",\n", " ", deps.size(), ", ", membersByName.size(), ", ", membersByDiscrim.size() == 0 ? kj::strTree("nullptr") : kj::strTree("i_", hexId), ", nullptr, nullptr, { &s_", hexId, ", nullptr, ", brandDeps.size() == 0 ? kj::strTree("nullptr, 0, 0") : kj::strTree( "bd_", hexId, ", 0, " "sizeof(bd_", hexId, ") / sizeof(bd_", hexId, "[0])"), ", nullptr }\n" "};\n" "#endif // !CAPNP_LITE\n"); NodeText top = makeNodeTextWithoutNested( namespace_, scope, name, schema, KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDecl); }, templateContext); NodeText result = { kj::mv(top.outerTypeDecl), kj::strTree( kj::mv(top.outerTypeDef), KJ_MAP(n, nestedTexts) { return kj::mv(n.outerTypeDef); }), kj::strTree( kj::mv(top.readerBuilderDefs), KJ_MAP(n, nestedTexts) { return kj::mv(n.readerBuilderDefs); }), kj::strTree( kj::mv(top.inlineMethodDefs), KJ_MAP(n, nestedTexts) { return kj::mv(n.inlineMethodDefs); }), kj::strTree( kj::mv(schemaDecl), kj::mv(top.capnpSchemaDecls), KJ_MAP(n, nestedTexts) { return kj::mv(n.capnpSchemaDecls); }), kj::strTree( kj::mv(schemaDef), kj::mv(top.capnpSchemaDefs), KJ_MAP(n, nestedTexts) { return kj::mv(n.capnpSchemaDefs); }), kj::strTree( kj::mv(top.sourceFileDefs), KJ_MAP(n, nestedTexts) { return kj::mv(n.sourceFileDefs); }), }; if (templateContext.isGeneric()) { // This is a template, so move all source declarations into the header. result.inlineMethodDefs = kj::strTree( kj::mv(result.inlineMethodDefs), kj::mv(result.sourceFileDefs)); result.sourceFileDefs = kj::strTree(); } return result; } NodeText makeNodeTextWithoutNested(kj::StringPtr namespace_, kj::StringPtr scope, kj::StringPtr name, Schema schema, kj::Array nestedTypeDecls, const TemplateContext& templateContext) { auto proto = schema.getProto(); KJ_IF_MAYBE(annotatedName, annotationValue(proto, NAME_ANNOTATION_ID)) { name = annotatedName->getText(); } auto hexId = kj::hex(proto.getId()); switch (proto.which()) { case schema::Node::FILE: KJ_FAIL_REQUIRE("This method shouldn't be called on file nodes."); case schema::Node::STRUCT: { StructText structText = makeStructText(scope, name, schema.asStruct(), kj::mv(nestedTypeDecls), templateContext); return NodeText { kj::mv(structText.outerTypeDecl), kj::mv(structText.outerTypeDef), kj::mv(structText.readerBuilderDefs), kj::mv(structText.inlineMethodDefs), kj::strTree(), kj::strTree(), kj::mv(structText.sourceDefs), }; } case schema::Node::ENUM: { auto enumerants = schema.asEnum().getEnumerants(); return NodeText { scope.size() == 0 ? kj::strTree() : kj::strTree( " typedef ::capnp::schemas::", name, "_", hexId, " ", name, ";\n" "\n"), scope.size() > 0 ? kj::strTree() : kj::strTree( "typedef ::capnp::schemas::", name, "_", hexId, " ", name, ";\n" "\n"), kj::strTree(), kj::strTree(), kj::strTree( // We declare enums in the capnp::schemas namespace and then typedef them into // place because we don't want them to be parameterized for generics. "enum class ", name, "_", hexId, ": uint16_t {\n", KJ_MAP(e, enumerants) { return kj::strTree(" ", toUpperCase(protoName(e.getProto())), ",\n"); }, "};\n" "CAPNP_DECLARE_ENUM(", name, ", ", hexId, ");\n"), kj::strTree( "CAPNP_DEFINE_ENUM(", name, "_", hexId, ", ", hexId, ");\n"), kj::strTree(), }; } case schema::Node::INTERFACE: { hasInterfaces = true; InterfaceText interfaceText = makeInterfaceText(scope, name, schema.asInterface(), kj::mv(nestedTypeDecls), templateContext); return NodeText { kj::mv(interfaceText.outerTypeDecl), kj::mv(interfaceText.outerTypeDef), kj::mv(interfaceText.clientServerDefs), kj::mv(interfaceText.inlineMethodDefs), kj::strTree(), kj::strTree(), kj::mv(interfaceText.sourceDefs), }; } case schema::Node::CONST: { auto constText = makeConstText(scope, name, schema.asConst(), templateContext); return NodeText { scope.size() == 0 ? kj::strTree() : kj::strTree(" ", kj::mv(constText.decl)), scope.size() > 0 ? kj::strTree() : kj::mv(constText.decl), kj::strTree(), kj::strTree(), kj::strTree(), kj::strTree(), kj::mv(constText.def), }; } case schema::Node::ANNOTATION: { return NodeText { kj::strTree(), kj::strTree(), kj::strTree(), kj::strTree(), kj::strTree(), kj::strTree(), kj::strTree(), }; } } KJ_UNREACHABLE; } // ----------------------------------------------------------------- struct FileText { kj::StringTree header; kj::StringTree source; }; FileText makeFileText(Schema schema, schema::CodeGeneratorRequest::RequestedFile::Reader request) { usedImports.clear(); auto node = schema.getProto(); auto displayName = node.getDisplayName(); kj::Vector> namespaceParts; kj::String namespacePrefix; for (auto annotation: node.getAnnotations()) { if (annotation.getId() == NAMESPACE_ANNOTATION_ID) { kj::StringPtr ns = annotation.getValue().getText(); kj::StringPtr ns2 = ns; namespacePrefix = kj::str("::", ns); for (;;) { KJ_IF_MAYBE(colonPos, ns.findFirst(':')) { namespaceParts.add(ns.slice(0, *colonPos)); ns = ns.slice(*colonPos); if (!ns.startsWith("::")) { context.exitError(kj::str(displayName, ": invalid namespace spec: ", ns2)); } ns = ns.slice(2); } else { namespaceParts.add(ns); break; } } break; } } auto nodeTexts = KJ_MAP(nested, node.getNestedNodes()) { return makeNodeText(namespacePrefix, "", nested.getName(), schemaLoader.getUnbound(nested.getId()), TemplateContext()); }; kj::String separator = kj::str("// ", kj::repeat('=', 87), "\n"); kj::Vector includes; for (auto import: request.getImports()) { if (usedImports.count(import.getId()) > 0) { includes.add(import.getName()); } } kj::StringTree sourceDefs = kj::strTree( KJ_MAP(n, nodeTexts) { return kj::mv(n.sourceFileDefs); }); return FileText { kj::strTree( "// Generated by Cap'n Proto compiler, DO NOT EDIT\n" "// source: ", baseName(displayName), "\n" "\n" "#pragma once\n" "\n" "#include \n" "#include \n", // work-around macro conflict with VOID hasInterfaces ? kj::strTree( "#if !CAPNP_LITE\n" "#include \n" "#endif // !CAPNP_LITE\n" ) : kj::strTree(), "\n" "#if CAPNP_VERSION != ", CAPNP_VERSION, "\n" "#error \"Version mismatch between generated code and library headers. You must " "use the same version of the Cap'n Proto compiler and library.\"\n" "#endif\n" "\n", KJ_MAP(path, includes) { if (path.startsWith("/")) { return kj::strTree("#include <", path.slice(1), ".h>\n"); } else { return kj::strTree("#include \"", path, ".h\"\n"); } }, "\n" "namespace capnp {\n" "namespace schemas {\n" "\n", KJ_MAP(n, nodeTexts) { return kj::mv(n.capnpSchemaDecls); }, "\n" "} // namespace schemas\n" "} // namespace capnp\n" "\n", KJ_MAP(n, namespaceParts) { return kj::strTree("namespace ", n, " {\n"); }, "\n", KJ_MAP(n, nodeTexts) { return kj::mv(n.outerTypeDef); }, separator, "\n", KJ_MAP(n, nodeTexts) { return kj::mv(n.readerBuilderDefs); }, separator, "\n", KJ_MAP(n, nodeTexts) { return kj::mv(n.inlineMethodDefs); }, KJ_MAP(n, namespaceParts) { return kj::strTree("} // namespace\n"); }, "\n"), kj::strTree( "// Generated by Cap'n Proto compiler, DO NOT EDIT\n" "// source: ", baseName(displayName), "\n" "\n" "#include \"", baseName(displayName), ".h\"\n" "\n" "namespace capnp {\n" "namespace schemas {\n", KJ_MAP(n, nodeTexts) { return kj::mv(n.capnpSchemaDefs); }, "} // namespace schemas\n" "} // namespace capnp\n", sourceDefs.size() == 0 ? kj::strTree() : kj::strTree( "\n", separator, "\n", KJ_MAP(n, namespaceParts) { return kj::strTree("namespace ", n, " {\n"); }, "\n", kj::mv(sourceDefs), "\n", KJ_MAP(n, namespaceParts) { return kj::strTree("} // namespace\n"); }, "\n")) }; } // ----------------------------------------------------------------- kj::Own fs = kj::newDiskFilesystem(); void writeFile(kj::StringPtr filename, const kj::StringTree& text) { // We don't use replaceFile() here because atomic replacements are actually detrimental for // build tools: // - It's the responsibility of the build pipeline to ensure that no one else is concurrently // reading the file when we write it, so atomicity brings no benefit. // - Atomic replacements force disk syncs which could slow us down for no benefit at all. // - Opening the existing file and overwriting it may allow the filesystem to reuse // already-allocated blocks, or maybe even notice that no actual changes occurred. // - In a power outage scenario, the user would obviously restart the build from scratch // anyway. // // At one point, in a fit of over-engineering, we used writable mmap() here. That turned out // to be a bad idea: writable mmap() is not implemented on some filesystems, especially shared // folders in VirtualBox. Oh well. auto path = kj::Path::parse(filename); auto file = fs->getCurrent().openFile(path, kj::WriteMode::CREATE | kj::WriteMode::MODIFY | kj::WriteMode::CREATE_PARENT); file->writeAll(text.flatten()); } kj::MainBuilder::Validity run() { ReaderOptions options; options.traversalLimitInWords = 1 << 30; // Don't limit. StreamFdMessageReader reader(0, options); auto request = reader.getRoot(); auto capnpVersion = request.getCapnpVersion(); if (capnpVersion.getMajor() != CAPNP_VERSION_MAJOR || capnpVersion.getMinor() != CAPNP_VERSION_MINOR || capnpVersion.getMicro() != CAPNP_VERSION_MICRO) { auto compilerVersion = request.hasCapnpVersion() ? kj::str(capnpVersion.getMajor(), '.', capnpVersion.getMinor(), '.', capnpVersion.getMicro()) : kj::str("pre-0.6"); // pre-0.6 didn't send the version. auto generatorVersion = kj::str( CAPNP_VERSION_MAJOR, '.', CAPNP_VERSION_MINOR, '.', CAPNP_VERSION_MICRO); KJ_LOG(WARNING, "You appear to be using different versions of 'capnp' (the compiler) and " "'capnpc-c++' (the code generator). This can happen, for example, if you built " "a custom version of 'capnp' but then ran it with '-oc++', which invokes " "'capnpc-c++' from your PATH (i.e. the installed version). To specify an alternate " "'capnpc-c++' executable, try something like '-o/path/to/capnpc-c++' instead.", compilerVersion, generatorVersion); } for (auto node: request.getNodes()) { schemaLoader.load(node); } for (auto requestedFile: request.getRequestedFiles()) { auto schema = schemaLoader.get(requestedFile.getId()); auto fileText = makeFileText(schema, requestedFile); writeFile(kj::str(schema.getProto().getDisplayName(), ".h"), fileText.header); writeFile(kj::str(schema.getProto().getDisplayName(), ".c++"), fileText.source); } return true; } }; } // namespace } // namespace capnp KJ_MAIN(capnp::CapnpcCppMain); capnproto-c++-0.8.0/src/capnp/compiler/capnpc-capnp.c++0000644000175000017500000006654713650101756023335 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This program is a code generator plugin for `capnp compile` which writes the schema back to // stdout in roughly capnpc format. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include "../serialize.h" #include #include #include #include #include "../schema-loader.h" #include "../dynamic.h" #include #include #include #include #include #include #if HAVE_CONFIG_H #include "config.h" #endif #ifndef VERSION #define VERSION "(unknown)" #endif namespace capnp { namespace { bool hasDiscriminantValue(const schema::Field::Reader& reader) { return reader.getDiscriminantValue() != schema::Field::NO_DISCRIMINANT; } struct Indent { uint amount; Indent() = default; inline Indent(int amount): amount(amount) {} Indent next() { return Indent(amount + 2); } struct Iterator { uint i; Iterator() = default; inline Iterator(uint i): i(i) {} inline char operator*() const { return ' '; } inline Iterator& operator++() { ++i; return *this; } inline Iterator operator++(int) { Iterator result = *this; ++i; return result; } inline bool operator==(const Iterator& other) const { return i == other.i; } inline bool operator!=(const Iterator& other) const { return i != other.i; } }; inline size_t size() const { return amount; } inline Iterator begin() const { return Iterator(0); } inline Iterator end() const { return Iterator(amount); } }; inline Indent KJ_STRINGIFY(const Indent& indent) { return indent; } // ======================================================================================= class CapnpcCapnpMain { public: CapnpcCapnpMain(kj::ProcessContext& context): context(context) {} kj::MainFunc getMain() { return kj::MainBuilder(context, "Cap'n Proto loopback plugin version " VERSION, "This is a Cap'n Proto compiler plugin which \"de-compiles\" the schema back into " "Cap'n Proto schema language format, with comments showing the offsets chosen by the " "compiler. This is meant to be run using the Cap'n Proto compiler, e.g.:\n" " capnp compile -ocapnp foo.capnp") .callAfterParsing(KJ_BIND_METHOD(*this, run)) .build(); } private: kj::ProcessContext& context; SchemaLoader schemaLoader; Text::Reader getUnqualifiedName(Schema schema) { auto proto = schema.getProto(); KJ_CONTEXT(proto.getDisplayName()); auto parent = schemaLoader.get(proto.getScopeId()); for (auto nested: parent.getProto().getNestedNodes()) { if (nested.getId() == proto.getId()) { return nested.getName(); } } KJ_FAIL_REQUIRE("A schema Node's supposed scope did not contain the node as a NestedNode."); return "(?)"; } kj::StringTree nodeName(Schema target, Schema scope, schema::Brand::Reader brand, kj::Maybe method) { kj::Vector targetPath; kj::Vector scopeParts; targetPath.add(target); std::map::Reader> scopeBindings; for (auto scopeBrand: brand.getScopes()) { switch (scopeBrand.which()) { case schema::Brand::Scope::BIND: scopeBindings[scopeBrand.getScopeId()] = scopeBrand.getBind(); break; case schema::Brand::Scope::INHERIT: // TODO(someday): We need to pay attention to INHERIT and be sure to explicitly override // any bindings that are not inherited. This requires a way to determine which of our // parent scopes have a non-empty parameter list. break; } } { Schema parent = target; while (parent.getProto().getScopeId() != 0) { parent = schemaLoader.get(parent.getProto().getScopeId()); targetPath.add(parent); } } { Schema parent = scope; scopeParts.add(parent); while (parent.getProto().getScopeId() != 0) { parent = schemaLoader.get(parent.getProto().getScopeId()); scopeParts.add(parent); } } // Remove common scope (unless it has been reparameterized). // TODO(someday): This is broken in that we aren't checking for shadowing. while (!scopeParts.empty() && targetPath.size() > 1 && scopeParts.back() == targetPath.back() && scopeBindings.count(scopeParts.back().getProto().getId()) == 0) { scopeParts.removeLast(); targetPath.removeLast(); } auto parts = kj::heapArrayBuilder(targetPath.size()); while (!targetPath.empty()) { auto part = targetPath.back(); auto proto = part.getProto(); kj::StringTree partStr; if (proto.getScopeId() == 0) { partStr = kj::strTree("import \"/", proto.getDisplayName(), '\"'); } else { partStr = kj::strTree(getUnqualifiedName(part)); } auto iter = scopeBindings.find(proto.getId()); if (iter != scopeBindings.end()) { auto bindings = KJ_MAP(binding, iter->second) { switch (binding.which()) { case schema::Brand::Binding::UNBOUND: return kj::strTree("AnyPointer"); case schema::Brand::Binding::TYPE: return genType(binding.getType(), scope, method); } return kj::strTree(""); }; partStr = kj::strTree(kj::mv(partStr), "(", kj::StringTree(kj::mv(bindings), ", "), ")"); } parts.add(kj::mv(partStr)); targetPath.removeLast(); } return kj::StringTree(parts.finish(), "."); } kj::StringTree genType(schema::Type::Reader type, Schema scope, kj::Maybe method) { switch (type.which()) { case schema::Type::VOID: return kj::strTree("Void"); case schema::Type::BOOL: return kj::strTree("Bool"); case schema::Type::INT8: return kj::strTree("Int8"); case schema::Type::INT16: return kj::strTree("Int16"); case schema::Type::INT32: return kj::strTree("Int32"); case schema::Type::INT64: return kj::strTree("Int64"); case schema::Type::UINT8: return kj::strTree("UInt8"); case schema::Type::UINT16: return kj::strTree("UInt16"); case schema::Type::UINT32: return kj::strTree("UInt32"); case schema::Type::UINT64: return kj::strTree("UInt64"); case schema::Type::FLOAT32: return kj::strTree("Float32"); case schema::Type::FLOAT64: return kj::strTree("Float64"); case schema::Type::TEXT: return kj::strTree("Text"); case schema::Type::DATA: return kj::strTree("Data"); case schema::Type::LIST: return kj::strTree("List(", genType(type.getList().getElementType(), scope, method), ")"); case schema::Type::ENUM: return nodeName(schemaLoader.get(type.getEnum().getTypeId()), scope, type.getEnum().getBrand(), method); case schema::Type::STRUCT: return nodeName(schemaLoader.get(type.getStruct().getTypeId()), scope, type.getStruct().getBrand(), method); case schema::Type::INTERFACE: return nodeName(schemaLoader.get(type.getInterface().getTypeId()), scope, type.getInterface().getBrand(), method); case schema::Type::ANY_POINTER: { auto anyPointer = type.getAnyPointer(); switch (anyPointer.which()) { case schema::Type::AnyPointer::UNCONSTRAINED: switch (anyPointer.getUnconstrained().which()) { case schema::Type::AnyPointer::Unconstrained::ANY_KIND: return kj::strTree("AnyPointer"); case schema::Type::AnyPointer::Unconstrained::STRUCT: return kj::strTree("AnyStruct"); case schema::Type::AnyPointer::Unconstrained::LIST: return kj::strTree("AnyList"); case schema::Type::AnyPointer::Unconstrained::CAPABILITY: return kj::strTree("Capability"); } KJ_UNREACHABLE; case schema::Type::AnyPointer::PARAMETER: { auto param = anyPointer.getParameter(); auto scopeProto = scope.getProto(); auto targetScopeId = param.getScopeId(); while (scopeProto.getId() != targetScopeId) { scopeProto = schemaLoader.get(param.getScopeId()).getProto(); } auto params = scopeProto.getParameters(); KJ_REQUIRE(param.getParameterIndex() < params.size()); return kj::strTree(params[param.getParameterIndex()].getName()); } case schema::Type::AnyPointer::IMPLICIT_METHOD_PARAMETER: { auto params = KJ_REQUIRE_NONNULL(method).getProto().getImplicitParameters(); uint index = anyPointer.getImplicitMethodParameter().getParameterIndex(); KJ_REQUIRE(index < params.size()); return kj::strTree(params[index].getName()); } } KJ_UNREACHABLE; } } return kj::strTree(); } int typeSizeBits(schema::Type::Reader type) { switch (type.which()) { case schema::Type::VOID: return 0; case schema::Type::BOOL: return 1; case schema::Type::INT8: return 8; case schema::Type::INT16: return 16; case schema::Type::INT32: return 32; case schema::Type::INT64: return 64; case schema::Type::UINT8: return 8; case schema::Type::UINT16: return 16; case schema::Type::UINT32: return 32; case schema::Type::UINT64: return 64; case schema::Type::FLOAT32: return 32; case schema::Type::FLOAT64: return 64; case schema::Type::TEXT: return -1; case schema::Type::DATA: return -1; case schema::Type::LIST: return -1; case schema::Type::ENUM: return 16; case schema::Type::STRUCT: return -1; case schema::Type::INTERFACE: return -1; case schema::Type::ANY_POINTER: return -1; } return 0; } bool isEmptyValue(schema::Value::Reader value) { switch (value.which()) { case schema::Value::VOID: return true; case schema::Value::BOOL: return value.getBool() == false; case schema::Value::INT8: return value.getInt8() == 0; case schema::Value::INT16: return value.getInt16() == 0; case schema::Value::INT32: return value.getInt32() == 0; case schema::Value::INT64: return value.getInt64() == 0; case schema::Value::UINT8: return value.getUint8() == 0; case schema::Value::UINT16: return value.getUint16() == 0; case schema::Value::UINT32: return value.getUint32() == 0; case schema::Value::UINT64: return value.getUint64() == 0; case schema::Value::FLOAT32: return value.getFloat32() == 0; case schema::Value::FLOAT64: return value.getFloat64() == 0; case schema::Value::TEXT: return !value.hasText(); case schema::Value::DATA: return !value.hasData(); case schema::Value::LIST: return !value.hasList(); case schema::Value::ENUM: return value.getEnum() == 0; case schema::Value::STRUCT: return !value.hasStruct(); case schema::Value::INTERFACE: return true; case schema::Value::ANY_POINTER: return true; } return true; } kj::StringTree genValue(Type type, schema::Value::Reader value) { switch (value.which()) { case schema::Value::VOID: return kj::strTree("void"); case schema::Value::BOOL: return kj::strTree(value.getBool() ? "true" : "false"); case schema::Value::INT8: return kj::strTree((int)value.getInt8()); case schema::Value::INT16: return kj::strTree(value.getInt16()); case schema::Value::INT32: return kj::strTree(value.getInt32()); case schema::Value::INT64: return kj::strTree(value.getInt64()); case schema::Value::UINT8: return kj::strTree((uint)value.getUint8()); case schema::Value::UINT16: return kj::strTree(value.getUint16()); case schema::Value::UINT32: return kj::strTree(value.getUint32()); case schema::Value::UINT64: return kj::strTree(value.getUint64()); case schema::Value::FLOAT32: return kj::strTree(value.getFloat32()); case schema::Value::FLOAT64: return kj::strTree(value.getFloat64()); case schema::Value::TEXT: return kj::strTree(DynamicValue::Reader(value.getText())); case schema::Value::DATA: return kj::strTree(DynamicValue::Reader(value.getData())); case schema::Value::LIST: { auto listValue = value.getList().getAs(type.asList()); return kj::strTree(listValue); } case schema::Value::ENUM: { auto enumNode = type.asEnum().getProto(); auto enumerants = enumNode.getEnum().getEnumerants(); KJ_REQUIRE(value.getEnum() < enumerants.size(), "Enum value out-of-range.", value.getEnum(), enumNode.getDisplayName()); return kj::strTree(enumerants[value.getEnum()].getName()); } case schema::Value::STRUCT: { KJ_REQUIRE(type.which() == schema::Type::STRUCT, "type/value mismatch"); auto structValue = value.getStruct().getAs(type.asStruct()); return kj::strTree(structValue); } case schema::Value::INTERFACE: { return kj::strTree(""); } case schema::Value::ANY_POINTER: { return kj::strTree(""); } } return kj::strTree(""); } kj::StringTree genGenericParams(List::Reader params, Schema scope) { if (params.size() == 0) { return kj::strTree(); } return kj::strTree(" (", kj::StringTree( KJ_MAP(param, params) { return kj::strTree(param.getName()); }, ", "), ')'); } kj::StringTree genGenericParams(Schema schema) { auto proto = schema.getProto(); return genGenericParams(proto.getParameters(), schemaLoader.get(proto.getScopeId())); } kj::StringTree genAnnotation(schema::Annotation::Reader annotation, Schema scope, const char* prefix = " ", const char* suffix = "") { auto decl = schemaLoader.get(annotation.getId(), annotation.getBrand(), scope); auto proto = decl.getProto(); KJ_REQUIRE(proto.isAnnotation()); auto annDecl = proto.getAnnotation(); auto value = genValue(schemaLoader.getType(annDecl.getType(), decl), annotation.getValue()).flatten(); if (value.startsWith("(")) { return kj::strTree(prefix, "$", nodeName(decl, scope, annotation.getBrand(), nullptr), value, suffix); } else { return kj::strTree(prefix, "$", nodeName(decl, scope, annotation.getBrand(), nullptr), "(", value, ")", suffix); } } kj::StringTree genAnnotations(List::Reader list, Schema scope) { return kj::strTree(KJ_MAP(ann, list) { return genAnnotation(ann, scope); }); } kj::StringTree genAnnotations(Schema schema) { auto proto = schema.getProto(); return genAnnotations(proto.getAnnotations(), schemaLoader.get(proto.getScopeId())); } const char* elementSizeName(schema::ElementSize size) { switch (size) { case schema::ElementSize::EMPTY: return "void"; case schema::ElementSize::BIT: return "1-bit"; case schema::ElementSize::BYTE: return "8-bit"; case schema::ElementSize::TWO_BYTES: return "16-bit"; case schema::ElementSize::FOUR_BYTES: return "32-bit"; case schema::ElementSize::EIGHT_BYTES: return "64-bit"; case schema::ElementSize::POINTER: return "pointer"; case schema::ElementSize::INLINE_COMPOSITE: return "inline composite"; } return ""; } struct OrderByCodeOrder { template inline bool operator()(const T& a, const T& b) const { return a.getProto().getCodeOrder() < b.getProto().getCodeOrder(); } }; template kj::Array()[0])> sortByCodeOrder(MemberList&& list) { auto sorted = KJ_MAP(item, list) { return item; }; std::sort(sorted.begin(), sorted.end(), OrderByCodeOrder()); return kj::mv(sorted); } kj::Array genStructFields(StructSchema schema, Indent indent) { // Slightly hacky: We want to print in code order, but we also need to print the union in one // chunk. Its fields should be together in code order anyway, but it's easier to simply // output the whole union in place of the first union field, and then output nothing for the // subsequent fields. bool seenUnion = false; return KJ_MAP(field, sortByCodeOrder(schema.getFields())) { if (hasDiscriminantValue(field.getProto())) { if (seenUnion) { return kj::strTree(); } else { seenUnion = true; uint offset = schema.getProto().getStruct().getDiscriminantOffset(); // GCC 4.7.3 crashes if you inline unionFields. auto unionFields = sortByCodeOrder(schema.getUnionFields()); return kj::strTree( indent, "union { # tag bits [", offset * 16, ", ", offset * 16 + 16, ")\n", KJ_MAP(uField, unionFields) { return genStructField(uField, schema, indent.next()); }, indent, "}\n"); } } else { return genStructField(field, schema, indent); } }; } kj::StringTree genStructField(StructSchema::Field field, Schema scope, Indent indent) { auto proto = field.getProto(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); int size = typeSizeBits(slot.getType()); return kj::strTree( indent, proto.getName(), " @", proto.getOrdinal().getExplicit(), " :", genType(slot.getType(), scope, nullptr), isEmptyValue(slot.getDefaultValue()) ? kj::strTree("") : kj::strTree(" = ", genValue(field.getType(), slot.getDefaultValue())), genAnnotations(proto.getAnnotations(), scope), "; # ", size == -1 ? kj::strTree("ptr[", slot.getOffset(), "]") : kj::strTree("bits[", slot.getOffset() * size, ", ", (slot.getOffset() + 1) * size, ")"), hasDiscriminantValue(proto) ? kj::strTree(", union tag = ", proto.getDiscriminantValue()) : kj::strTree(), "\n"); } case schema::Field::GROUP: { auto group = field.getType().asStruct(); return kj::strTree( indent, proto.getName(), " :group", genAnnotations(proto.getAnnotations(), scope), " {", hasDiscriminantValue(proto) ? kj::strTree(" # union tag = ", proto.getDiscriminantValue()) : kj::strTree(), "\n", genStructFields(group, indent.next()), indent, "}\n"); } } return kj::strTree(); } kj::StringTree genParamList(InterfaceSchema interface, StructSchema schema, schema::Brand::Reader brand, InterfaceSchema::Method method) { if (schema.getProto().getId() == typeId()) { return kj::strTree("stream"); } else if (schema.getProto().getScopeId() == 0) { // A named parameter list. return kj::strTree("(", kj::StringTree( KJ_MAP(field, schema.getFields()) { auto proto = field.getProto(); auto slot = proto.getSlot(); return kj::strTree( proto.getName(), " :", genType(slot.getType(), interface, nullptr), isEmptyValue(slot.getDefaultValue()) ? kj::strTree("") : kj::strTree(" = ", genValue(field.getType(), slot.getDefaultValue())), genAnnotations(proto.getAnnotations(), interface)); }, ", "), ")"); } else { return nodeName(schema, interface, brand, method); } } kj::StringTree genSuperclasses(InterfaceSchema interface) { auto superclasses = interface.getProto().getInterface().getSuperclasses(); if (superclasses.size() == 0) { return kj::strTree(); } else { return kj::strTree(" superclasses(", kj::StringTree( KJ_MAP(superclass, superclasses) { return nodeName(schemaLoader.get(superclass.getId()), interface, superclass.getBrand(), nullptr); }, ", "), ")"); } } kj::StringTree genDecl(Schema schema, Text::Reader name, uint64_t scopeId, Indent indent) { auto proto = schema.getProto(); if (proto.getScopeId() != scopeId) { // This appears to be an alias for something declared elsewhere. KJ_FAIL_REQUIRE("Aliases not implemented."); } switch (proto.which()) { case schema::Node::FILE: KJ_FAIL_REQUIRE("Encountered nested file node."); break; case schema::Node::STRUCT: { auto structProto = proto.getStruct(); return kj::strTree( indent, "struct ", name, " @0x", kj::hex(proto.getId()), genGenericParams(schema), genAnnotations(schema), " { # ", structProto.getDataWordCount() * 8, " bytes, ", structProto.getPointerCount(), " ptrs", structProto.getPreferredListEncoding() == schema::ElementSize::INLINE_COMPOSITE ? kj::strTree() : kj::strTree(", packed as ", elementSizeName(structProto.getPreferredListEncoding())), "\n", genStructFields(schema.asStruct(), indent.next()), genNestedDecls(schema, indent.next()), indent, "}\n"); } case schema::Node::ENUM: { return kj::strTree( indent, "enum ", name, " @0x", kj::hex(proto.getId()), genAnnotations(schema), " {\n", KJ_MAP(enumerant, sortByCodeOrder(schema.asEnum().getEnumerants())) { return kj::strTree(indent.next(), enumerant.getProto().getName(), " @", enumerant.getIndex(), genAnnotations(enumerant.getProto().getAnnotations(), schema), ";\n"); }, genNestedDecls(schema, indent.next()), indent, "}\n"); } case schema::Node::INTERFACE: { auto interface = schema.asInterface(); return kj::strTree( indent, "interface ", name, " @0x", kj::hex(proto.getId()), genGenericParams(schema), genSuperclasses(interface), genAnnotations(schema), " {\n", KJ_MAP(method, sortByCodeOrder(interface.getMethods())) { auto methodProto = method.getProto(); auto implicits = methodProto.getImplicitParameters(); kj::StringTree implicitsStr; if (implicits.size() > 0) { implicitsStr = kj::strTree( "[", kj::StringTree(KJ_MAP(implicit, implicits) { return kj::strTree(implicit.getName()); }, ", "), "] "); } auto params = schemaLoader.get(methodProto.getParamStructType()).asStruct(); auto results = schemaLoader.get(methodProto.getResultStructType()).asStruct(); return kj::strTree( indent.next(), methodProto.getName(), " @", method.getIndex(), " ", kj::mv(implicitsStr), genParamList(interface, params, methodProto.getParamBrand(), method), " -> ", genParamList(interface, results, methodProto.getResultBrand(), method), genAnnotations(methodProto.getAnnotations(), interface), ";\n"); }, genNestedDecls(schema, indent.next()), indent, "}\n"); } case schema::Node::CONST: { auto constProto = proto.getConst(); return kj::strTree( indent, "const ", name, " @0x", kj::hex(proto.getId()), " :", genType(constProto.getType(), schema, nullptr), " = ", genValue(schema.asConst().getType(), constProto.getValue()), genAnnotations(schema), ";\n"); } case schema::Node::ANNOTATION: { auto annotationProto = proto.getAnnotation(); kj::Vector targets(8); bool targetsAll = true; auto dynamic = toDynamic(annotationProto); for (auto field: dynamic.getSchema().getFields()) { auto fieldName = field.getProto().getName(); if (fieldName.startsWith("targets")) { if (dynamic.get(field).as()) { auto target = kj::str(fieldName.slice(strlen("targets"))); target[0] = target[0] - 'A' + 'a'; targets.add(kj::mv(target)); } else { targetsAll = false; } } } if (targetsAll) { targets = kj::Vector(1); targets.add(kj::heapString("*")); } return kj::strTree( indent, "annotation ", name, " @0x", kj::hex(proto.getId()), " (", strArray(targets, ", "), ") :", genType(annotationProto.getType(), schema, nullptr), genAnnotations(schema), ";\n"); } } return kj::strTree(); } kj::StringTree genNestedDecls(Schema schema, Indent indent) { uint64_t id = schema.getProto().getId(); return kj::strTree(KJ_MAP(nested, schema.getProto().getNestedNodes()) { return genDecl(schemaLoader.get(nested.getId()), nested.getName(), id, indent); }); } kj::StringTree genFile(Schema file) { auto proto = file.getProto(); KJ_REQUIRE(proto.isFile(), "Expected a file node.", (uint)proto.which()); return kj::strTree( "# ", proto.getDisplayName(), "\n", "@0x", kj::hex(proto.getId()), ";\n", KJ_MAP(ann, proto.getAnnotations()) { return genAnnotation(ann, file, "", ";\n"); }, genNestedDecls(file, Indent(0))); } kj::MainBuilder::Validity run() { ReaderOptions options; options.traversalLimitInWords = 1 << 30; // Don't limit. StreamFdMessageReader reader(STDIN_FILENO, options); auto request = reader.getRoot(); for (auto node: request.getNodes()) { schemaLoader.load(node); } kj::FdOutputStream rawOut(STDOUT_FILENO); kj::BufferedOutputStreamWrapper out(rawOut); for (auto requestedFile: request.getRequestedFiles()) { genFile(schemaLoader.get(requestedFile.getId())).visit( [&](kj::ArrayPtr text) { out.write(text.begin(), text.size()); }); } return true; } }; } // namespace } // namespace capnp KJ_MAIN(capnp::CapnpcCapnpMain); capnproto-c++-0.8.0/src/capnp/compiler/capnp-test.sh0000755000175000017500000001544013650101756023077 0ustar00kentonkenton00000000000000#! /bin/sh # Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. # Tests the `capnp` tool's various commands, other than `compile`. set -eu fail() { echo "FAILED: $@" >&2 exit 1 } if test -f ./capnp; then CAPNP=${CAPNP:-./capnp} elif test -f ./capnp.exe; then CAPNP=${CAPNP:-./capnp.exe} else CAPNP=${CAPNP:-capnp} fi SCHEMA=`dirname "$0"`/../test.capnp JSON_SCHEMA=`dirname "$0"`/../compat/json-test.capnp TESTDATA=`dirname "$0"`/../testdata SRCDIR=`dirname "$0"`/../.. SUFFIX=${TESTDATA#*/src/} PREFIX=${TESTDATA%${SUFFIX}} if [ "$PREFIX" = "" ]; then PREFIX=. fi # ======================================================================================== # convert $CAPNP convert text:binary $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/binary - || fail encode $CAPNP convert text:flat $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/flat - || fail encode flat $CAPNP convert text:packed $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/packed - || fail encode packed $CAPNP convert text:flat-packed $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/packedflat - || fail encode packedflat $CAPNP convert text:binary $SCHEMA TestAllTypes < $TESTDATA/pretty.txt | cmp $TESTDATA/binary - || fail parse pretty $CAPNP convert binary:text $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/pretty.txt - || fail decode $CAPNP convert flat:text $SCHEMA TestAllTypes < $TESTDATA/flat | cmp $TESTDATA/pretty.txt - || fail decode flat $CAPNP convert packed:text $SCHEMA TestAllTypes < $TESTDATA/packed | cmp $TESTDATA/pretty.txt - || fail decode packed $CAPNP convert flat-packed:text $SCHEMA TestAllTypes < $TESTDATA/packedflat | cmp $TESTDATA/pretty.txt - || fail decode packedflat $CAPNP convert binary:text --short $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/short.txt - || fail decode short $CAPNP convert binary:text $SCHEMA TestAllTypes < $TESTDATA/segmented | cmp $TESTDATA/pretty.txt - || fail decode segmented $CAPNP convert packed:text $SCHEMA TestAllTypes < $TESTDATA/segmented-packed | cmp $TESTDATA/pretty.txt - || fail decode segmented-packed $CAPNP convert binary:packed < $TESTDATA/binary | cmp $TESTDATA/packed - || fail binary to packed $CAPNP convert packed:binary < $TESTDATA/packed | cmp $TESTDATA/binary - || fail packed to binary $CAPNP convert binary:json $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/pretty.json - || fail binary to json $CAPNP convert binary:json --short $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/short.json - || fail binary to short json $CAPNP convert json:binary $SCHEMA TestAllTypes < $TESTDATA/pretty.json | cmp $TESTDATA/binary - || fail json to binary $CAPNP convert json:binary $SCHEMA TestAllTypes < $TESTDATA/short.json | cmp $TESTDATA/binary - || fail short json to binary $CAPNP convert json:binary $JSON_SCHEMA TestJsonAnnotations -I"$SRCDIR" < $TESTDATA/annotated.json | cmp $TESTDATA/annotated-json.binary || fail annotated json to binary $CAPNP convert binary:json $JSON_SCHEMA TestJsonAnnotations -I"$SRCDIR" < $TESTDATA/annotated-json.binary | cmp $TESTDATA/annotated.json || fail annotated json to binary # ======================================================================================== # DEPRECATED encode/decode $CAPNP encode $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/binary - || fail encode $CAPNP encode --flat $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/flat - || fail encode flat $CAPNP encode --packed $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/packed - || fail encode packed $CAPNP encode --packed --flat $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/packedflat - || fail encode packedflat $CAPNP encode $SCHEMA TestAllTypes < $TESTDATA/pretty.txt | cmp $TESTDATA/binary - || fail parse pretty $CAPNP decode $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/pretty.txt - || fail decode $CAPNP decode --flat $SCHEMA TestAllTypes < $TESTDATA/flat | cmp $TESTDATA/pretty.txt - || fail decode flat $CAPNP decode --packed $SCHEMA TestAllTypes < $TESTDATA/packed | cmp $TESTDATA/pretty.txt - || fail decode packed $CAPNP decode --packed --flat $SCHEMA TestAllTypes < $TESTDATA/packedflat | cmp $TESTDATA/pretty.txt - || fail decode packedflat $CAPNP decode --short $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/short.txt - || fail decode short $CAPNP decode $SCHEMA TestAllTypes < $TESTDATA/segmented | cmp $TESTDATA/pretty.txt - || fail decode segmented $CAPNP decode --packed $SCHEMA TestAllTypes < $TESTDATA/segmented-packed | cmp $TESTDATA/pretty.txt - || fail decode segmented-packed # ======================================================================================== # eval test_eval() { test "x`$CAPNP eval $SCHEMA $1 | tr -d '\r'`" = "x$2" || fail eval "$1 == $2" } test_eval TestDefaults.uInt32Field 3456789012 test_eval TestDefaults.structField.textField '"baz"' test_eval TestDefaults.int8List "[111, -111]" test_eval 'TestDefaults.structList[1].textField' '"structlist 2"' test_eval globalPrintableStruct '(someText = "foo")' test_eval TestConstants.enumConst corge test_eval 'TestListDefaults.lists.int32ListList[2][0]' 12341234 test "x`$CAPNP eval $SCHEMA -ojson globalPrintableStruct | tr -d '\r'`" = "x{\"someText\": \"foo\"}" || fail eval json "globalPrintableStruct == {someText = \"foo\"}" $CAPNP compile --no-standard-import --src-prefix="$PREFIX" -ofoo $TESTDATA/errors.capnp.nobuild 2>&1 | sed -e "s,^.*errors[.]capnp[.]nobuild:,file:,g" | tr -d '\r' | diff -u $TESTDATA/errors.txt - || fail error output $CAPNP compile --no-standard-import --src-prefix="$PREFIX" -ofoo $TESTDATA/errors2.capnp.nobuild 2>&1 | sed -e "s,^.*errors2[.]capnp[.]nobuild:,file:,g" | tr -d '\r' | diff -u $TESTDATA/errors2.txt - || fail error2 output capnproto-c++-0.8.0/src/capnp/testdata/0000755000175000017500000000000013650320027020447 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/capnp/testdata/segmented-packed0000644000175000017500000000251013340402540023565 0ustar00kentonkenton00000000000000|OO|ÿ…ÇϲžCÿ‡ òy·ÿÿên²j ÎÒ 댩T«PšDþ»àÀ‚‹µÉGHJLNPRTVXZ\^fny{foo"barÿô€ Lû?Nsè8¦3ýZÒˆbÿÒ o!Ì_p ¯àu@ !#%')19DFbaz"qux  ?nested :ÿreally nestedrP P ) Þ€"ïÒÒé€ÿ#÷Na¼@Ó úø€ÿÿÿ$?yß †Hpÿ.uýŠ–ýÿ€€ÿÿÿÿÿÿÿÿ% "ÿ"ÏÒ.ÿÿ #÷Na¼À,_ðÿÿÿÿ"$?yß †HpÒŠìuiÿÿÿÿÿÿÿÿÿ$%ð8´–Iÿ½ð|½ðüêê‚&4ÿ@Þwƒ!ÜB)#ÊåÈv)#ÊåÈvÿ‘÷P7žxf‘÷P7žxf€(5,.0quux+*corge-2?grault/:*468?garply32waldo5*fred7"2Q =@Cÿx struct?list 1<zÿx struct?list 2?zÿx struct?list 3Bz:1wEP 0 I!o‘Kg+™ÔMÿÇkŸ9”`ùOÿÇqÄ+«uk9Ž;ÔTŠ”ðQoÞS5‚œ­UU¡®ÆW ÿÇq¬µ¯˜2šY Μ­E€Ì€ÿÀ[$ðÀa¾@ÀðÀðÿÀø]%aceplugh`2xyzzyb2thudd*_ikmoopsh"ÿexhaustedjJrfc3092l:gQ ruxÿstructlist 1qjÿstructlist 2tjÿstructlist 3wjo1wzPcapnproto-c++-0.8.0/src/capnp/testdata/errors.capnp.nobuild0000644000175000017500000001037213650101756024452 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. # This file is intended to test that various error cases are detected as errors. The error # output is matched against a golden file. The file name has the .nobuild extension to make # sure that a build system which automatically builds .capnp files does not try to build this one. # @0xccd0890aa4926a9b; # Can't really test the missing-ID error because the output is intentionally unpredictable. const notType :Int32 = 123; annotation notFieldAnnotation(struct) :Int32; annotation fieldAnnotation(field) :Int32; struct Foo { dupName @0 :Int32; dupName @1 :Int32; dupNumber1 @2 :Int32; dupNumber2 @2 :Int32; missingNumber @4 :Int32; next @5 :Int32; emptyUnion :union {} emptyGroup :group {} singletonUnion :union { field @6 :Int32; } union { dupName @7 :Int32; f8 @8 :Int32; } union { f9 @9 :Int32; f10 @10 :Int32; } struct wrongTypeStyle {} WrongFieldStyle @11 :Int32; under_score @12 :Int32; containsStruct :group { f13 @13 :Int32; struct CantNestHere {} } retroUnion @16! :union { f14 @14 :Int32; f15 @15 :Int32; } missingColonAndEclamation @18 union { f19 @19 :Int32; f20 @20 :Int32; } missingExclamation @21 :union { f22 @22 :Int32; f23 @23 :Int32; } missingColon @24! union { f19 @25 :Int32; f20 @26 :Int32; } unnamedInNamed :union { f27 @27 :Int32; f28 @28 :Int32; union { # content is ignored } } listWithoutParam @31 :List; listWithTooManyParams @32 :List(Int32, Int64); listAnyPointer @33 :List(AnyPointer); notAType @34 :notType; noParams @35 :Foo(Int32); defaultOutOfRange @36 :Int16 = 1234567; defaultOutOfRange2 @37 :UInt16 = -1; defaultWrongType @38 :Text = 123; defaultWrongType2 @39 :Text = [123]; defaultWrongType3 @40 :Text = (foo = 123, bar = 456); defaultTooBigToBeNegative @41 :Int64 = -0x8000000000000001; defaultNotConstant @42 :Int32 = .Foo; defaultConstantNotQualified @43 :Int32 = notType; notAnnotation @44 :Int32 $Foo(123); badAnnotation @45 :Int32 $notFieldAnnotation(123); notVoidAnnotation @46 :Int32 $fieldAnnotation; undefinedImport @17 :import "noshuchfile.capnp".Bar; undefinedAbsolute @47 : .NoSuch; undefinedRelative @29 :NoSuch; undefinedMember @30 :Foo.NoSuch; } struct Bar { x @3 :Text; someGroup :group { defaultMissingFieldName @2 :Bar = (x = "abcd", 456); defaultNoSuchField @0 :Bar = (nosuchfield = 123); defaultGroupMismatch @1 :Bar = (someGroup = 123); } } using Bar; enum DupEnumerants { dupName @0; dupName @1; dupNumber1 @2; dupNumber2 @2; } const recursive: UInt32 = .recursive; struct Generic(T, U) { } struct UseGeneric { tooFew @0 :Generic(Text); tooMany @1 :Generic(Text, Data, List(Int32)); doubleBind @2 :Generic(Text, Data)(Data, Text); primitiveBinding @3 :Generic(Text, Int32); } const embedBadType :UInt32 = embed "binary"; const embedNoSuchFile :Data = embed "no-such-file"; using Baz = import "nosuchfile-unused.capnp".Baz; # Check that an import in an unused `using` still reports error. interface TestInterface { foo @0 (a :UInt32 = null); bar @1 stream -> (); } capnproto-c++-0.8.0/src/capnp/testdata/errors2.capnp.nobuild0000644000175000017500000000306513650101756024535 0ustar00kentonkenton00000000000000# Copyright (c) 2020 Cloudflare, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xea7dcf0ca9acfa97; # This is much like errors.capnp.nobuild but expresses errors that occur in a later phase of # compilation, which is never reached when building errors.capnp.nobuild because the compiler # bails out after other errors. struct DummyType {} const dummyValue :DummyType = (); struct TestDefaultValueForGeneric(A) { single @0 :A = .dummyValue; nested @1 :Box(A) = (val = .dummyValue); } struct Box(B) { val @0 :B; } capnproto-c++-0.8.0/src/capnp/testdata/short.txt0000644000175000017500000001021313340402540022341 0ustar00kentonkenton00000000000000(voidField = void, boolField = true, int8Field = -123, int16Field = -12345, int32Field = -12345678, int64Field = -123456789012345, uInt8Field = 234, uInt16Field = 45678, uInt32Field = 3456789012, uInt64Field = 12345678901234567890, float32Field = 1234.5, float64Field = -1.23e47, textField = "foo", dataField = "bar", structField = (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 = (voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "nested", structField = (voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "really nested", enumField = foo, interfaceField = void), enumField = foo, interfaceField = void), enumField = baz, interfaceField = void, voidList = [void, void, void], boolList = [false, true, false, true, true], int8List = [12, -34, -128, 127], int16List = [1234, -5678, -32768, 32767], int32List = [12345678, -90123456, -2147483648, 2147483647], int64List = [123456789012345, -678901234567890, -9223372036854775808, 9223372036854775807], uInt8List = [12, 34, 0, 255], uInt16List = [1234, 5678, 0, 65535], uInt32List = [12345678, 90123456, 0, 4294967295], uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615], 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 = [(voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "x structlist 1", enumField = foo, interfaceField = void), (voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "x structlist 2", enumField = foo, interfaceField = void), (voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "x structlist 3", enumField = foo, interfaceField = void)], enumList = [qux, bar, grault]), enumField = corge, interfaceField = void, voidList = [void, void, void, void, void, void], boolList = [true, false, false, true], int8List = [111, -111], int16List = [11111, -11111], int32List = [111111111, -111111111], int64List = [1111111111111111111, -1111111111111111111], uInt8List = [111, 222], uInt16List = [33333, 44444], uInt32List = [3333333333], uInt64List = [11111111111111111111], float32List = [5555.5, inf, -inf, nan], float64List = [7777.75, inf, -inf, nan], textList = ["plugh", "xyzzy", "thud"], dataList = ["oops", "exhausted", "rfc3092"], structList = [(voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "structlist 1", enumField = foo, interfaceField = void), (voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "structlist 2", enumField = foo, interfaceField = void), (voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "structlist 3", enumField = foo, interfaceField = void)], enumList = [foo, garply]) capnproto-c++-0.8.0/src/capnp/testdata/flat0000644000175000017500000000537013340402540021322 0ustar00kentonkenton00000000000000…ÇϲžCÿ‡ òy·ÿÿên²j ÎÒ 댩T«PšD»àÀ‚‹µÉM"ML…0!……… … …$‰%•©Áwfoobarô€ LûNsè8¦3ZÒˆbÒ o!Ì_p ¯u@M"ML%!)!"!#!$%%1"1#1$5%A4I5]q…wÕbazquxM:HnestedMrreally nested Þ€ÒÒé€ÿNa¼@Ó ú€ÿÿÿyß †Hp.uýŠ–ýÿ€ÿÿÿÿÿÿÿ "ÿÒ.ÿÿNa¼À,_ÿÿÿÿyß †HpÒŠìuiÿÿÿÿÿÿÿÿ8´–I½ð|½ðüêê‚@Þwƒ!ÜB)#ÊåÈv)#ÊåÈvÿ‘÷P7žxf‘÷P7žxf€ * 2 :quuxcorgegrault 2 * "garplywaldofred z½z]zx structlist 1x structlist 2x structlist 3 o‘g+™ÔÇkŸ9”`ùÇqÄ+«uk9Ž;ÔTŠ”ðoÞ5‚œ­U¡®ÆÇq¬µ¯˜2šœ­E€€ÿÀÀa¾@ððÿø 2 2 *plughxyzzythud " J :oopsexhaustedrfc3092 j½j]jstructlist 1structlist 2structlist 3capnproto-c++-0.8.0/src/capnp/testdata/binary0000644000175000017500000000540013340402540021652 0ustar00kentonkenton00000000000000_…ÇϲžCÿ‡ òy·ÿÿên²j ÎÒ 댩T«PšD»àÀ‚‹µÉM"ML…0!……… … …$‰%•©Áwfoobarô€ LûNsè8¦3ZÒˆbÒ o!Ì_p ¯u@M"ML%!)!"!#!$%%1"1#1$5%A4I5]q…wÕbazquxM:HnestedMrreally nested Þ€ÒÒé€ÿNa¼@Ó ú€ÿÿÿyß †Hp.uýŠ–ýÿ€ÿÿÿÿÿÿÿ "ÿÒ.ÿÿNa¼À,_ÿÿÿÿyß †HpÒŠìuiÿÿÿÿÿÿÿÿ8´–I½ð|½ðüêê‚@Þwƒ!ÜB)#ÊåÈv)#ÊåÈvÿ‘÷P7žxf‘÷P7žxf€ * 2 :quuxcorgegrault 2 * "garplywaldofred z½z]zx structlist 1x structlist 2x structlist 3 o‘g+™ÔÇkŸ9”`ùÇqÄ+«uk9Ž;ÔTŠ”ðoÞ5‚œ­U¡®ÆÇq¬µ¯˜2šœ­E€€ÿÀÀa¾@ððÿø 2 2 *plughxyzzythud " J :oopsexhaustedrfc3092 j½j]jstructlist 1structlist 2structlist 3capnproto-c++-0.8.0/src/capnp/testdata/errors.txt0000644000175000017500000001225713650101756022541 0ustar00kentonkenton00000000000000file:74:30-32: error: As of Cap'n Proto v0.3, it is no longer necessary to assign numbers to unions. However, removing the number will break binary compatibility. If this is an old protocol and you need to retain compatibility, please add an exclamation point after the number to indicate that it is really needed, e.g. `foo @1! :union {`. If this is a new protocol or compatibility doesn't matter, just remove the @n entirely. Sorry for the inconvenience, and thanks for being an early adopter! :) file:74:30-32: error: As of Cap'n Proto v0.3, the 'union' keyword should be prefixed with a colon for named unions, e.g. `foo :union {`. file:79:23-25: error: As of Cap'n Proto v0.3, it is no longer necessary to assign numbers to unions. However, removing the number will break binary compatibility. If this is an old protocol and you need to retain compatibility, please add an exclamation point after the number to indicate that it is really needed, e.g. `foo @1! :union {`. If this is a new protocol or compatibility doesn't matter, just remove the @n entirely. Sorry for the inconvenience, and thanks for being an early adopter! :) file:84:17-19: error: As of Cap'n Proto v0.3, the 'union' keyword should be prefixed with a colon for named unions, e.g. `foo :union {`. file:132:7-10: error: 'using' declaration without '=' must specify a named declaration from a different scope. file:37:3-10: error: 'dupName' is already defined in this scope. file:36:3-10: error: 'dupName' previously defined here. file:52:5-12: error: 'dupName' is already defined in this scope. file:36:3-10: error: 'dupName' previously defined here. file:55:3-8: error: An unnamed union is already defined in this scope. file:51:3-8: error: Previously defined here. file:60:10-24: error: Type names must begin with a capital letter. file:61:3-18: error: Non-type names must begin with a lower-case letter. file:62:3-14: error: 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.) file:66:5-27: error: This kind of declaration doesn't belong here. file:44:3-23: error: Union must have at least two members. file:45:3-23: error: Group must have at least one member. file:47: error: Union must have at least two members. file:92: error: Unions cannot contain unnamed unions. file:39:15-16: error: Duplicate ordinal number. file:38:15-16: error: Ordinal @2 originally used here. file:41:18-19: error: Skipped ordinal @3. Ordinals must be sequential with no holes. file:69:15-17: error: 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). file:116:31-50: error: Import failed: noshuchfile.capnp file:118:26-32: error: Not defined: NoSuch file:119:28-34: error: 'Foo' has no member named 'NoSuch' file:97:25-29: error: 'List' requires exactly one parameter. file:98:30-48: error: Too many generic parameters. file:98:30-34: error: 'List' requires exactly one parameter. file:99:23-39: error: 'List(AnyPointer)' is not supported. file:100:17-24: error: 'notType' is not a type. file:101:17-27: error: Declaration does not accept generic parameters. file:103:34-41: error: Integer value out of range. file:104:37-38: error: Integer value out of range. file:105:32-35: error: Type mismatch; expected Text. file:106:33-38: error: Type mismatch; expected Text. file:107:33-55: error: Type mismatch; expected Text. file:108:43-61: error: Integer is too big to be negative. file:109:35-39: error: '.Foo' does not refer to a constant. file:110:44-51: error: Constant names must be qualified to avoid confusion. Please replace 'notType' with '.notType', if that's what you intended. file:117:28-34: error: Not defined: NoSuch file:112:29-32: error: 'Foo' is not an annotation. file:113:29-47: error: 'notFieldAnnotation' cannot be applied to this kind of declaration. file:114:33-48: error: 'fieldAnnotation' requires a value. file:126:35-46: error: Struct has no field named 'nosuchfield'. file:127:49-52: error: Type mismatch; expected group. file:125:52-55: error: Missing field name. file:136:3-10: error: 'dupName' is already defined in this scope. file:135:3-10: error: 'dupName' previously defined here. file:138:15-16: error: Duplicate ordinal number. file:137:15-16: error: Ordinal @2 originally used here. file:141:7-16: error: Declaration recursively depends on itself. file:147:14-27: error: Not enough generic parameters. file:148:15-47: error: Too many generic parameters. file:149:18-49: error: Double-application of generic parameters. file:150:38-43: error: Sorry, only pointer types can be used as generic parameters. file:153:30-44: error: Embeds can only be used when Text, Data, or a struct is expected. file:154:37-51: error: Couldn't read file for embed: no-such-file file:160:23-27: error: Only pointer parameters can declare their default as 'null'. file:161:10-16: error: 'stream' can only appear after '->', not before. file:161:10-16: error: 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. file:156:20-45: error: Import failed: nosuchfile-unused.capnp capnproto-c++-0.8.0/src/capnp/testdata/errors2.txt0000644000175000017500000000046413650101756022620 0ustar00kentonkenton00000000000000file:31:18-29: error: Cannot interpret value because the type is a generic type parameter which is not yet bound. We don't know what type to expect here. file:32:30-41: error: Cannot interpret value because the type is a generic type parameter which is not yet bound. We don't know what type to expect here. capnproto-c++-0.8.0/src/capnp/testdata/segmented0000644000175000017500000001262013340402540022343 0ustar00kentonkenton00000000000000|OO|…ÇϲžCÿ‡ òy·ÿÿên²j ÎÒ 댩T«PšD»àÀ‚‹µÉGHJLNPRTVXZ\^fny{foo"barô€ LûNsè8¦3ZÒˆbÒ o!Ì_p ¯u@ !#%')19DFbaz"qux  nested :really nestedr  ) Þ€"ÒÒé€ÿ#Na¼@Ó ú€ÿÿÿ$yß †Hp.uýŠ–ýÿ€ÿÿÿÿÿÿÿ% "ÿ"Ò.ÿÿ #Na¼À,_ÿÿÿÿ"$yß †HpÒŠìuiÿÿÿÿÿÿÿÿ$%8´–I½ð|½ðüêê‚&4@Þwƒ!ÜB)#ÊåÈv)#ÊåÈvÿ‘÷P7žxf‘÷P7žxf€(5,.0quux+*corge-2grault/:*468garply32waldo5*fred7"2 =@Cx structlist 1<zx structlist 2?zx structlist 3Bz:wE 0 I!o‘Kg+™ÔMÇkŸ9”`ùOÇqÄ+«uk9Ž;ÔTŠ”ðQoÞS5‚œ­UU¡®ÆW Çq¬µ¯˜2šY œ­E€€ÿÀ[$Àa¾@ððÿø]%aceplugh`2xyzzyb2thudd*_ikmoopsh"exhaustedjJrfc3092l:g ruxstructlist 1qjstructlist 2tjstructlist 3wjowzcapnproto-c++-0.8.0/src/capnp/testdata/packed0000644000175000017500000000147713340402540021627 0ustar00kentonkenton000000000000000_Pÿ…ÇϲžCÿ‡ òy·ÿÿên²j ÎÒ 댩T«PšDþ»àÀ‚‹µÉM"MQL…0!……… … …$‰%•©3Áwfoobarÿô€ Lû?Nsè8¦3ýZÒˆbÿÒ o!Ì_p ¯àu@M"MQL%!)!"!#!$%%1"1#1$5%A4I5]q3…wÕbazquxM:QH?nestedMrÿreally nested Þ€ïÒÒé€ÿ÷Na¼@Ó úø€ÿÿÿ?yß †Hpÿ.uýŠ–ýÿ€€ÿÿÿÿÿÿÿÿ "ÿÏÒ.ÿÿ÷Na¼À,_ðÿÿÿÿ?yß †HpÒŠìuiÿÿÿÿÿÿÿÿÿð8´–Iÿ½ð|½ðüêê‚ÿ@Þwƒ!ÜB)#ÊåÈv)#ÊåÈvÿ‘÷P7žxf‘÷P7žxf€ * 2 :quuxcorge?grault 2 * "?garplywaldofredQ z½z]zÿx struct?list 1ÿx struct?list 2ÿx struct?list 3 o‘g+™ÔÿÇkŸ9”`ùÇqÄ+«uk9Ž;ÔTŠ”ðoÞ5‚œ­U¡®ÆÿÇq¬µ¯˜2šÎœ­E€Ì€ÿÀðÀa¾@ÀðÀðÿÀø 2 2 *plughxyzzythud " J :oopsÿexhaustedrfc3092Q j½j]jÿstructlist 1ÿstructlist 2ÿstructlist 3capnproto-c++-0.8.0/src/capnp/testdata/pretty.txt0000644000175000017500000001225113340402540022535 0ustar00kentonkenton00000000000000( voidField = void, boolField = true, int8Field = -123, int16Field = -12345, int32Field = -12345678, int64Field = -123456789012345, uInt8Field = 234, uInt16Field = 45678, uInt32Field = 3456789012, uInt64Field = 12345678901234567890, float32Field = 1234.5, float64Field = -1.23e47, textField = "foo", dataField = "bar", structField = ( 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 = ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "nested", structField = ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "really nested", enumField = foo, interfaceField = void ), enumField = foo, interfaceField = void ), enumField = baz, interfaceField = void, voidList = [void, void, void], boolList = [false, true, false, true, true], int8List = [12, -34, -128, 127], int16List = [1234, -5678, -32768, 32767], int32List = [12345678, -90123456, -2147483648, 2147483647], int64List = [123456789012345, -678901234567890, -9223372036854775808, 9223372036854775807], uInt8List = [12, 34, 0, 255], uInt16List = [1234, 5678, 0, 65535], uInt32List = [12345678, 90123456, 0, 4294967295], uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615], 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 = [ ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "x structlist 1", enumField = foo, interfaceField = void ), ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "x structlist 2", enumField = foo, interfaceField = void ), ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "x structlist 3", enumField = foo, interfaceField = void ) ], enumList = [qux, bar, grault] ), enumField = corge, interfaceField = void, voidList = [void, void, void, void, void, void], boolList = [true, false, false, true], int8List = [111, -111], int16List = [11111, -11111], int32List = [111111111, -111111111], int64List = [1111111111111111111, -1111111111111111111], uInt8List = [111, 222], uInt16List = [33333, 44444], uInt32List = [3333333333], uInt64List = [11111111111111111111], float32List = [5555.5, inf, -inf, nan], float64List = [7777.75, inf, -inf, nan], textList = ["plugh", "xyzzy", "thud"], dataList = ["oops", "exhausted", "rfc3092"], structList = [ ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "structlist 1", enumField = foo, interfaceField = void ), ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "structlist 2", enumField = foo, interfaceField = void ), ( voidField = void, boolField = false, int8Field = 0, int16Field = 0, int32Field = 0, int64Field = 0, uInt8Field = 0, uInt16Field = 0, uInt32Field = 0, uInt64Field = 0, float32Field = 0, float64Field = 0, textField = "structlist 3", enumField = foo, interfaceField = void ) ], enumList = [foo, garply] ) capnproto-c++-0.8.0/src/capnp/testdata/lists.binary0000644000175000017500000000211013340402540023002 0ustar00kentonkenton00000000000000ˆ %%'5=EMUe}­{-90…Í[Ò8û ÀºŠ<Õb‡KªíaU""foobar   òO¼ ""foobar"baz"2quxcorge§Õ×{Ècapnproto-c++-0.8.0/src/capnp/testdata/packedflat0000644000175000017500000000147413340402540022473 0ustar00kentonkenton00000000000000Pÿ…ÇϲžCÿ‡ òy·ÿÿên²j ÎÒ 댩T«PšDþ»àÀ‚‹µÉM"MQL…0!……… … …$‰%•©3Áwfoobarÿô€ Lû?Nsè8¦3ýZÒˆbÿÒ o!Ì_p ¯àu@M"MQL%!)!"!#!$%%1"1#1$5%A4I5]q3…wÕbazquxM:QH?nestedMrÿreally nested Þ€ïÒÒé€ÿ÷Na¼@Ó úø€ÿÿÿ?yß †Hpÿ.uýŠ–ýÿ€€ÿÿÿÿÿÿÿÿ "ÿÏÒ.ÿÿ÷Na¼À,_ðÿÿÿÿ?yß †HpÒŠìuiÿÿÿÿÿÿÿÿÿð8´–Iÿ½ð|½ðüêê‚ÿ@Þwƒ!ÜB)#ÊåÈv)#ÊåÈvÿ‘÷P7žxf‘÷P7žxf€ * 2 :quuxcorge?grault 2 * "?garplywaldofredQ z½z]zÿx struct?list 1ÿx struct?list 2ÿx struct?list 3 o‘g+™ÔÿÇkŸ9”`ùÇqÄ+«uk9Ž;ÔTŠ”ðoÞ5‚œ­U¡®ÆÿÇq¬µ¯˜2šÎœ­E€Ì€ÿÀðÀa¾@ÀðÀðÿÀø 2 2 *plughxyzzythud " J :oopsÿexhaustedrfc3092Q j½j]jÿstructlist 1ÿstructlist 2ÿstructlist 3capnproto-c++-0.8.0/src/capnp/testdata/pretty.json0000644000175000017500000001174613340402540022677 0ustar00kentonkenton00000000000000{ "voidField": null, "boolField": true, "int8Field": -123, "int16Field": -12345, "int32Field": -12345678, "int64Field": "-123456789012345", "uInt8Field": 234, "uInt16Field": 45678, "uInt32Field": 3456789012, "uInt64Field": "12345678901234567890", "float32Field": 1234.5, "float64Field": -1.23e47, "textField": "foo", "dataField": [98, 97, 114], "structField": { "voidField": null, "boolField": true, "int8Field": -12, "int16Field": 3456, "int32Field": -78901234, "int64Field": "56789012345678", "uInt8Field": 90, "uInt16Field": 1234, "uInt32Field": 56789012, "uInt64Field": "345678901234567890", "float32Field": -1.2499999646475857e-10, "float64Field": 345, "textField": "baz", "dataField": [113, 117, 120], "structField": { "voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "nested", "structField": {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "really nested", "enumField": "foo", "interfaceField": null}, "enumField": "foo", "interfaceField": null }, "enumField": "baz", "interfaceField": null, "voidList": [null, null, null], "boolList": [false, true, false, true, true], "int8List": [12, -34, -128, 127], "int16List": [1234, -5678, -32768, 32767], "int32List": [12345678, -90123456, -2147483648, 2147483647], "int64List": ["123456789012345", "-678901234567890", "-9223372036854775808", "9223372036854775807"], "uInt8List": [12, 34, 0, 255], "uInt16List": [1234, 5678, 0, 65535], "uInt32List": [12345678, 90123456, 0, 4294967295], "uInt64List": ["123456789012345", "678901234567890", "0", "18446744073709551615"], "float32List": [0, 1234567, 9.9999999338158125e36, -9.9999999338158125e36, 9.99999991097579e-38, -9.99999991097579e-38], "float64List": [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306], "textList": ["quux", "corge", "grault"], "dataList": [[103, 97, 114, 112, 108, 121], [119, 97, 108, 100, 111], [102, 114, 101, 100]], "structList": [ {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "x structlist 1", "enumField": "foo", "interfaceField": null}, {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "x structlist 2", "enumField": "foo", "interfaceField": null}, {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "x structlist 3", "enumField": "foo", "interfaceField": null} ], "enumList": ["qux", "bar", "grault"] }, "enumField": "corge", "interfaceField": null, "voidList": [null, null, null, null, null, null], "boolList": [true, false, false, true], "int8List": [111, -111], "int16List": [11111, -11111], "int32List": [111111111, -111111111], "int64List": ["1111111111111111111", "-1111111111111111111"], "uInt8List": [111, 222], "uInt16List": [33333, 44444], "uInt32List": [3333333333], "uInt64List": ["11111111111111111111"], "float32List": [5555.5, "Infinity", "-Infinity", "NaN"], "float64List": [7777.75, "Infinity", "-Infinity", "NaN"], "textList": ["plugh", "xyzzy", "thud"], "dataList": [[111, 111, 112, 115], [101, 120, 104, 97, 117, 115, 116, 101, 100], [114, 102, 99, 51, 48, 57, 50]], "structList": [ {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "structlist 1", "enumField": "foo", "interfaceField": null}, {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "structlist 2", "enumField": "foo", "interfaceField": null}, {"voidField": null, "boolField": false, "int8Field": 0, "int16Field": 0, "int32Field": 0, "int64Field": "0", "uInt8Field": 0, "uInt16Field": 0, "uInt32Field": 0, "uInt64Field": "0", "float32Field": 0, "float64Field": 0, "textField": "structlist 3", "enumField": "foo", "interfaceField": null} ], "enumList": ["foo", "garply"] } capnproto-c++-0.8.0/src/capnp/testdata/short.json0000644000175000017500000001030213340402540022472 0ustar00kentonkenton00000000000000{"voidField":null,"boolField":true,"int8Field":-123,"int16Field":-12345,"int32Field":-12345678,"int64Field":"-123456789012345","uInt8Field":234,"uInt16Field":45678,"uInt32Field":3456789012,"uInt64Field":"12345678901234567890","float32Field":1234.5,"float64Field":-1.23e47,"textField":"foo","dataField":[98,97,114],"structField":{"voidField":null,"boolField":true,"int8Field":-12,"int16Field":3456,"int32Field":-78901234,"int64Field":"56789012345678","uInt8Field":90,"uInt16Field":1234,"uInt32Field":56789012,"uInt64Field":"345678901234567890","float32Field":-1.2499999646475857e-10,"float64Field":345,"textField":"baz","dataField":[113,117,120],"structField":{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"nested","structField":{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"really nested","enumField":"foo","interfaceField":null},"enumField":"foo","interfaceField":null},"enumField":"baz","interfaceField":null,"voidList":[null,null,null],"boolList":[false,true,false,true,true],"int8List":[12,-34,-128,127],"int16List":[1234,-5678,-32768,32767],"int32List":[12345678,-90123456,-2147483648,2147483647],"int64List":["123456789012345","-678901234567890","-9223372036854775808","9223372036854775807"],"uInt8List":[12,34,0,255],"uInt16List":[1234,5678,0,65535],"uInt32List":[12345678,90123456,0,4294967295],"uInt64List":["123456789012345","678901234567890","0","18446744073709551615"],"float32List":[0,1234567,9.9999999338158125e36,-9.9999999338158125e36,9.99999991097579e-38,-9.99999991097579e-38],"float64List":[0,123456789012345,1e306,-1e306,1e-306,-1e-306],"textList":["quux","corge","grault"],"dataList":[[103,97,114,112,108,121],[119,97,108,100,111],[102,114,101,100]],"structList":[{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"x structlist 1","enumField":"foo","interfaceField":null},{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"x structlist 2","enumField":"foo","interfaceField":null},{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"x structlist 3","enumField":"foo","interfaceField":null}],"enumList":["qux","bar","grault"]},"enumField":"corge","interfaceField":null,"voidList":[null,null,null,null,null,null],"boolList":[true,false,false,true],"int8List":[111,-111],"int16List":[11111,-11111],"int32List":[111111111,-111111111],"int64List":["1111111111111111111","-1111111111111111111"],"uInt8List":[111,222],"uInt16List":[33333,44444],"uInt32List":[3333333333],"uInt64List":["11111111111111111111"],"float32List":[5555.5,"Infinity","-Infinity","NaN"],"float64List":[7777.75,"Infinity","-Infinity","NaN"],"textList":["plugh","xyzzy","thud"],"dataList":[[111,111,112,115],[101,120,104,97,117,115,116,101,100],[114,102,99,51,48,57,50]],"structList":[{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"structlist 1","enumField":"foo","interfaceField":null},{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"structlist 2","enumField":"foo","interfaceField":null},{"voidField":null,"boolField":false,"int8Field":0,"int16Field":0,"int32Field":0,"int64Field":"0","uInt8Field":0,"uInt16Field":0,"uInt32Field":0,"uInt64Field":"0","float32Field":0,"float64Field":0,"textField":"structlist 3","enumField":"foo","interfaceField":null}],"enumList":["foo","garply"]} capnproto-c++-0.8.0/src/capnp/testdata/annotated-json.binary0000644000175000017500000000104013340402540024571 0ustar00kentonkenton00000000000000C{A¦="="="=]"]"\e:e#d±"±*¬fooabccbathis is a long string in order to force multi-line pretty printingfedghi2corgegarplyO À^@ 2hello:objectfredplugh"cbacapnproto-c++-0.8.0/src/capnp/testdata/annotated.json0000644000175000017500000000140113340402540023310 0ustar00kentonkenton00000000000000{ "names-can_contain!anything Really": "foo", "flatFoo": 123, "flatBar": "abc", "renamed-flatBaz": {"hello": true}, "flatQux": "cba", "pfx.foo": "this is a long string in order to force multi-line pretty printing", "pfx.renamed-bar": 321, "pfx.baz": {"hello": true}, "pfx.xfp.qux": "fed", "union-type": "renamed-bar", "barMember": 789, "multiMember": "ghi", "dependency": {"renamed-foo": "corge"}, "simpleGroup": {"renamed-grault": "garply"}, "enums": ["qux", "renamed-bar", "foo", "renamed-baz"], "innerJson": [123, "hello", {"object": true}], "testBase64": "ZnJlZA==", "testHex": "706c756768", "bUnion": "renamed-bar", "bValue": 678, "externalUnion": {"type": "bar", "value": "cba"}, "unionWithVoid": {"type": "voidValue"} } capnproto-c++-0.8.0/src/capnp/c++.capnp0000644000175000017500000000251213340402540020226 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xbdf87d7bb8304e81; $namespace("capnp::annotations"); annotation namespace(file): Text; annotation name(field, enumerant, struct, enum, interface, method, param, group, union): Text; capnproto-c++-0.8.0/src/capnp/schema.capnp0000644000175000017500000004342613340402540021127 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. using Cxx = import "/capnp/c++.capnp"; @0xa93fc509624c72d9; $Cxx.namespace("capnp::schema"); using Id = UInt64; # The globally-unique ID of a file, type, or annotation. struct Node { id @0 :Id; displayName @1 :Text; # Name to present to humans to identify this Node. You should not attempt to parse this. Its # format could change. It is not guaranteed to be unique. # # (On Zooko's triangle, this is the node's nickname.) displayNamePrefixLength @2 :UInt32; # If you want a shorter version of `displayName` (just naming this node, without its surrounding # scope), chop off this many characters from the beginning of `displayName`. scopeId @3 :Id; # ID of the lexical parent node. Typically, the scope node will have a NestedNode pointing back # at this node, but robust code should avoid relying on this (and, in fact, group nodes are not # listed in the outer struct's nestedNodes, since they are listed in the fields). `scopeId` is # zero if the node has no parent, which is normally only the case with files, but should be # allowed for any kind of node (in order to make runtime type generation easier). parameters @32 :List(Parameter); # If this node is parameterized (generic), the list of parameters. Empty for non-generic types. isGeneric @33 :Bool; # True if this node is generic, meaning that it or one of its parent scopes has a non-empty # `parameters`. struct Parameter { # Information about one of the node's parameters. name @0 :Text; } nestedNodes @4 :List(NestedNode); # List of nodes nested within this node, along with the names under which they were declared. struct NestedNode { name @0 :Text; # Unqualified symbol name. Unlike Node.displayName, this *can* be used programmatically. # # (On Zooko's triangle, this is the node's petname according to its parent scope.) id @1 :Id; # ID of the nested node. Typically, the target node's scopeId points back to this node, but # robust code should avoid relying on this. } annotations @5 :List(Annotation); # Annotations applied to this node. union { # Info specific to each kind of node. file @6 :Void; struct :group { dataWordCount @7 :UInt16; # Size of the data section, in words. pointerCount @8 :UInt16; # Size of the pointer section, in pointers (which are one word each). preferredListEncoding @9 :ElementSize; # The preferred element size to use when encoding a list of this struct. If this is anything # other than `inlineComposite` then the struct is one word or less in size and is a candidate # for list packing optimization. isGroup @10 :Bool; # If true, then this "struct" node is actually not an independent node, but merely represents # some named union or group within a particular parent struct. This node's scopeId refers # to the parent struct, which may itself be a union/group in yet another struct. # # All group nodes share the same dataWordCount and pointerCount as the top-level # struct, and their fields live in the same ordinal and offset spaces as all other fields in # the struct. # # Note that a named union is considered a special kind of group -- in fact, a named union # is exactly equivalent to a group that contains nothing but an unnamed union. discriminantCount @11 :UInt16; # Number of fields in this struct which are members of an anonymous union, and thus may # overlap. If this is non-zero, then a 16-bit discriminant is present indicating which # of the overlapping fields is active. This can never be 1 -- if it is non-zero, it must be # two or more. # # Note that the fields of an unnamed union are considered fields of the scope containing the # union -- an unnamed union is not its own group. So, a top-level struct may contain a # non-zero discriminant count. Named unions, on the other hand, are equivalent to groups # containing unnamed unions. So, a named union has its own independent schema node, with # `isGroup` = true. discriminantOffset @12 :UInt32; # If `discriminantCount` is non-zero, this is the offset of the union discriminant, in # multiples of 16 bits. fields @13 :List(Field); # Fields defined within this scope (either the struct's top-level fields, or the fields of # a particular group; see `isGroup`). # # The fields are sorted by ordinal number, but note that because groups share the same # ordinal space, the field's index in this list is not necessarily exactly its ordinal. # On the other hand, the field's position in this list does remain the same even as the # protocol evolves, since it is not possible to insert or remove an earlier ordinal. # Therefore, for most use cases, if you want to identify a field by number, it may make the # most sense to use the field's index in this list rather than its ordinal. } enum :group { enumerants@14 :List(Enumerant); # Enumerants ordered by numeric value (ordinal). } interface :group { methods @15 :List(Method); # Methods ordered by ordinal. superclasses @31 :List(Superclass); # Superclasses of this interface. } const :group { type @16 :Type; value @17 :Value; } annotation :group { type @18 :Type; targetsFile @19 :Bool; targetsConst @20 :Bool; targetsEnum @21 :Bool; targetsEnumerant @22 :Bool; targetsStruct @23 :Bool; targetsField @24 :Bool; targetsUnion @25 :Bool; targetsGroup @26 :Bool; targetsInterface @27 :Bool; targetsMethod @28 :Bool; targetsParam @29 :Bool; targetsAnnotation @30 :Bool; } } struct SourceInfo { # Additional information about a node which is not needed at runtime, but may be useful for # documentation or debugging purposes. This is kept in a separate struct to make sure it # doesn't accidentally get included in contexts where it is not needed. The # `CodeGeneratorRequest` includes this information in a separate array. id @0 :Id; # ID of the Node which this info describes. docComment @1 :Text; # The top-level doc comment for the Node. members @2 :List(Member); # Information about each member -- i.e. fields (for structs), enumerants (for enums), or # methods (for interfaces). # # This list is the same length and order as the corresponding list in the Node, i.e. # Node.struct.fields, Node.enum.enumerants, or Node.interface.methods. struct Member { docComment @0 :Text; # Doc comment on the member. } # TODO(someday): Record location of the declaration in the original source code. } } struct Field { # Schema for a field of a struct. name @0 :Text; codeOrder @1 :UInt16; # Indicates where this member appeared in the code, relative to other members. # Code ordering may have semantic relevance -- programmers tend to place related fields # together. So, using code ordering makes sense in human-readable formats where ordering is # otherwise irrelevant, like JSON. The values of codeOrder are tightly-packed, so the maximum # value is count(members) - 1. Fields that are members of a union are only ordered relative to # the other members of that union, so the maximum value there is count(union.members). annotations @2 :List(Annotation); const noDiscriminant :UInt16 = 0xffff; discriminantValue @3 :UInt16 = Field.noDiscriminant; # If the field is in a union, this is the value which the union's discriminant should take when # the field is active. If the field is not in a union, this is 0xffff. union { slot :group { # A regular, non-group, non-fixed-list field. offset @4 :UInt32; # Offset, in units of the field's size, from the beginning of the section in which the field # resides. E.g. for a UInt32 field, multiply this by 4 to get the byte offset from the # beginning of the data section. type @5 :Type; defaultValue @6 :Value; hadExplicitDefault @10 :Bool; # Whether the default value was specified explicitly. Non-explicit default values are always # zero or empty values. Usually, whether the default value was explicit shouldn't matter. # The main use case for this flag is for structs representing method parameters: # explicitly-defaulted parameters may be allowed to be omitted when calling the method. } group :group { # A group. typeId @7 :Id; # The ID of the group's node. } } ordinal :union { implicit @8 :Void; explicit @9 :UInt16; # The original ordinal number given to the field. You probably should NOT use this; if you need # a numeric identifier for a field, use its position within the field array for its scope. # The ordinal is given here mainly just so that the original schema text can be reproduced given # the compiled version -- i.e. so that `capnp compile -ocapnp` can do its job. } } struct Enumerant { # Schema for member of an enum. name @0 :Text; codeOrder @1 :UInt16; # Specifies order in which the enumerants were declared in the code. # Like Struct.Field.codeOrder. annotations @2 :List(Annotation); } struct Superclass { id @0 :Id; brand @1 :Brand; } struct Method { # Schema for method of an interface. name @0 :Text; codeOrder @1 :UInt16; # Specifies order in which the methods were declared in the code. # Like Struct.Field.codeOrder. implicitParameters @7 :List(Node.Parameter); # The parameters listed in [] (typically, type / generic parameters), whose bindings are intended # to be inferred rather than specified explicitly, although not all languages support this. paramStructType @2 :Id; # ID of the parameter struct type. If a named parameter list was specified in the method # declaration (rather than a single struct parameter type) then a corresponding struct type is # auto-generated. Such an auto-generated type will not be listed in the interface's # `nestedNodes` and its `scopeId` will be zero -- it is completely detached from the namespace. # (Awkwardly, it does of course inherit generic parameters from the method's scope, which makes # this a situation where you can't just climb the scope chain to find where a particular # generic parameter was introduced. Making the `scopeId` zero was a mistake.) paramBrand @5 :Brand; # Brand of param struct type. resultStructType @3 :Id; # ID of the return struct type; similar to `paramStructType`. resultBrand @6 :Brand; # Brand of result struct type. annotations @4 :List(Annotation); } struct Type { # Represents a type expression. union { # The ordinals intentionally match those of Value. void @0 :Void; bool @1 :Void; int8 @2 :Void; int16 @3 :Void; int32 @4 :Void; int64 @5 :Void; uint8 @6 :Void; uint16 @7 :Void; uint32 @8 :Void; uint64 @9 :Void; float32 @10 :Void; float64 @11 :Void; text @12 :Void; data @13 :Void; list :group { elementType @14 :Type; } enum :group { typeId @15 :Id; brand @21 :Brand; } struct :group { typeId @16 :Id; brand @22 :Brand; } interface :group { typeId @17 :Id; brand @23 :Brand; } anyPointer :union { unconstrained :union { # A regular AnyPointer. # # The name "unconstrained" means as opposed to constraining it to match a type parameter. # In retrospect this name is probably a poor choice given that it may still be constrained # to be a struct, list, or capability. anyKind @18 :Void; # truly AnyPointer struct @25 :Void; # AnyStruct list @26 :Void; # AnyList capability @27 :Void; # Capability } parameter :group { # This is actually a reference to a type parameter defined within this scope. scopeId @19 :Id; # ID of the generic type whose parameter we're referencing. This should be a parent of the # current scope. parameterIndex @20 :UInt16; # Index of the parameter within the generic type's parameter list. } implicitMethodParameter :group { # This is actually a reference to an implicit (generic) parameter of a method. The only # legal context for this type to appear is inside Method.paramBrand or Method.resultBrand. parameterIndex @24 :UInt16; } } } } struct Brand { # Specifies bindings for parameters of generics. Since these bindings turn a generic into a # non-generic, we call it the "brand". scopes @0 :List(Scope); # For each of the target type and each of its parent scopes, a parameterization may be included # in this list. If no parameterization is included for a particular relevant scope, then either # that scope has no parameters or all parameters should be considered to be `AnyPointer`. struct Scope { scopeId @0 :Id; # ID of the scope to which these params apply. union { bind @1 :List(Binding); # List of parameter bindings. inherit @2 :Void; # The place where this Brand appears is actually within this scope or a sub-scope, # and the bindings for this scope should be inherited from the reference point. } } struct Binding { union { unbound @0 :Void; type @1 :Type; # TODO(someday): Allow non-type parameters? Unsure if useful. } } } struct Value { # Represents a value, e.g. a field default value, constant value, or annotation value. union { # The ordinals intentionally match those of Type. void @0 :Void; bool @1 :Bool; int8 @2 :Int8; int16 @3 :Int16; int32 @4 :Int32; int64 @5 :Int64; uint8 @6 :UInt8; uint16 @7 :UInt16; uint32 @8 :UInt32; uint64 @9 :UInt64; float32 @10 :Float32; float64 @11 :Float64; text @12 :Text; data @13 :Data; list @14 :AnyPointer; enum @15 :UInt16; struct @16 :AnyPointer; interface @17 :Void; # The only interface value that can be represented statically is "null", whose methods always # throw exceptions. anyPointer @18 :AnyPointer; } } struct Annotation { # Describes an annotation applied to a declaration. Note AnnotationNode describes the # annotation's declaration, while this describes a use of the annotation. id @0 :Id; # ID of the annotation node. brand @2 :Brand; # Brand of the annotation. # # Note that the annotation itself is not allowed to be parameterized, but its scope might be. value @1 :Value; } enum ElementSize { # Possible element sizes for encoded lists. These correspond exactly to the possible values of # the 3-bit element size component of a list pointer. empty @0; # aka "void", but that's a keyword. bit @1; byte @2; twoBytes @3; fourBytes @4; eightBytes @5; pointer @6; inlineComposite @7; } struct CapnpVersion { major @0 :UInt16; minor @1 :UInt8; micro @2 :UInt8; } struct CodeGeneratorRequest { capnpVersion @2 :CapnpVersion; # Version of the `capnp` executable. Generally, code generators should ignore this, but the code # generators that ship with `capnp` itself will print a warning if this mismatches since that # probably indicates something is misconfigured. # # The first version of 'capnp' to set this was 0.6.0. So, if it's missing, the compiler version # is older than that. nodes @0 :List(Node); # All nodes parsed by the compiler, including for the files on the command line and their # imports. sourceInfo @3 :List(Node.SourceInfo); # Information about the original source code for each node, where available. This array may be # omitted or may be missing some nodes if no info is available for them. requestedFiles @1 :List(RequestedFile); # Files which were listed on the command line. struct RequestedFile { id @0 :Id; # ID of the file. filename @1 :Text; # Name of the file as it appeared on the command-line (minus the src-prefix). You may use # this to decide where to write the output. imports @2 :List(Import); # List of all imported paths seen in this file. struct Import { id @0 :Id; # ID of the imported file. name @1 :Text; # Name which *this* file used to refer to the foreign file. This may be a relative name. # This information is provided because it might be useful for code generation, e.g. to # generate #include directives in C++. We don't put this in Node.file because this # information is only meaningful at compile time anyway. # # (On Zooko's triangle, this is the import's petname according to the importing file.) } } } capnproto-c++-0.8.0/src/capnp/stream.capnp0000644000175000017500000000476113650101756021172 0ustar00kentonkenton00000000000000# Copyright (c) 2019 Cloudflare, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0x86c366a91393f3f8; # Defines placeholder types used to provide backwards-compatibility while introducing streaming # to the language. The goal is that old code generators that don't know about streaming can still # generate code that functions, leaving it up to the application to implement flow control # manually. $import "/capnp/c++.capnp".namespace("capnp"); struct StreamResult @0x995f9a3377c0b16e { # Empty struct that serves as the return type for "streaming" methods. # # Defining a method like: # # write @0 (bytes :Data) -> stream; # # Is equivalent to: # # write @0 (bytes :Data) -> import "/capnp/stream.capnp".StreamResult; # # However, implementations that recognize streaming will elide the reference to StreamResult # and instead give write() a different signature appropriate for streaming. # # Streaming methods do not return a result -- that is, they return Promise. This promise # resolves not to indicate that the call was actually delivered, but instead to provide # backpressure. When the previous call's promise resolves, it is time to make another call. On # the client side, the RPC system will resolve promises immediately until an appropriate number # of requests are in-flight, and then will delay promise resolution to apply back-pressure. # On the server side, the RPC system will deliver one call at a time. } capnproto-c++-0.8.0/src/capnp/rpc.capnp0000644000175000017500000024055513650101756020466 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xb312981b2552a250; # Recall that Cap'n Proto RPC allows messages to contain references to remote objects that # implement interfaces. These references are called "capabilities", because they both designate # the remote object to use and confer permission to use it. # # Recall also that Cap'n Proto RPC has the feature that when a method call itself returns a # capability, the caller can begin calling methods on that capability _before the first call has # returned_. The caller essentially sends a message saying "Hey server, as soon as you finish # that previous call, do this with the result!". Cap'n Proto's RPC protocol makes this possible. # # The protocol is significantly more complicated than most RPC protocols. However, this is # implementation complexity that underlies an easy-to-grasp higher-level model of object oriented # programming. That is, just like TCP is a surprisingly complicated protocol that implements a # conceptually-simple byte stream abstraction, Cap'n Proto is a surprisingly complicated protocol # that implements a conceptually-simple object abstraction. # # Cap'n Proto RPC is based heavily on CapTP, the object-capability protocol used by the E # programming language: # http://www.erights.org/elib/distrib/captp/index.html # # Cap'n Proto RPC takes place between "vats". A vat hosts some set of objects and talks to other # vats through direct bilateral connections. Typically, there is a 1:1 correspondence between vats # and processes (in the unix sense of the word), although this is not strictly always true (one # process could run multiple vats, or a distributed virtual vat might live across many processes). # # Cap'n Proto does not distinguish between "clients" and "servers" -- this is up to the application. # Either end of any connection can potentially hold capabilities pointing to the other end, and # can call methods on those capabilities. In the doc comments below, we use the words "sender" # and "receiver". These refer to the sender and receiver of an instance of the struct or field # being documented. Sometimes we refer to a "third-party" that is neither the sender nor the # receiver. Documentation is generally written from the point of view of the sender. # # It is generally up to the vat network implementation to securely verify that connections are made # to the intended vat as well as to encrypt transmitted data for privacy and integrity. See the # `VatNetwork` example interface near the end of this file. # # When a new connection is formed, the only interesting things that can be done are to send a # `Bootstrap` (level 0) or `Accept` (level 3) message. # # Unless otherwise specified, messages must be delivered to the receiving application in the same # order in which they were initiated by the sending application. The goal is to support "E-Order", # which states that two calls made on the same reference must be delivered in the order which they # were made: # http://erights.org/elib/concurrency/partial-order.html # # Since the full protocol is complicated, we define multiple levels of support that an # implementation may target. For many applications, level 1 support will be sufficient. # Comments in this file indicate which level requires the corresponding feature to be # implemented. # # * **Level 0:** The implementation does not support object references. Only the bootstrap interface # can be called. At this level, the implementation does not support object-oriented protocols and # is similar in complexity to JSON-RPC or Protobuf services. This level should be considered only # a temporary stepping-stone toward level 1 as the lack of object references drastically changes # how protocols are designed. Applications _should not_ attempt to design their protocols around # the limitations of level 0 implementations. # # * **Level 1:** The implementation supports simple bilateral interaction with object references # and promise pipelining, but interactions between three or more parties are supported only via # proxying of objects. E.g. if Alice (in Vat A) wants to send Bob (in Vat B) a capability # pointing to Carol (in Vat C), Alice must create a proxy of Carol within Vat A and send Bob a # reference to that; Bob cannot form a direct connection to Carol. Level 1 implementations do # not support checking if two capabilities received from different vats actually point to the # same object ("join"), although they should be able to do this check on capabilities received # from the same vat. # # * **Level 2:** The implementation supports saving persistent capabilities -- i.e. capabilities # that remain valid even after disconnect, and can be restored on a future connection. When a # capability is saved, the requester receives a `SturdyRef`, which is a token that can be used # to restore the capability later. # # * **Level 3:** The implementation supports three-way interactions. That is, if Alice (in Vat A) # sends Bob (in Vat B) a capability pointing to Carol (in Vat C), then Vat B will automatically # form a direct connection to Vat C rather than have requests be proxied through Vat A. # # * **Level 4:** The entire protocol is implemented, including joins (checking if two capabilities # are equivalent). # # Note that an implementation must also support specific networks (transports), as described in # the "Network-specific Parameters" section below. An implementation might have different levels # depending on the network used. # # New implementations of Cap'n Proto should start out targeting the simplistic two-party network # type as defined in `rpc-twoparty.capnp`. With this network type, level 3 is irrelevant and # levels 2 and 4 are much easier than usual to implement. When such an implementation is paired # with a container proxy, the contained app effectively gets to make full use of the proxy's # network at level 4. And since Cap'n Proto IPC is extremely fast, it may never make sense to # bother implementing any other vat network protocol -- just use the correct container type and get # it for free. using Cxx = import "/capnp/c++.capnp"; $Cxx.namespace("capnp::rpc"); # ======================================================================================== # The Four Tables # # Cap'n Proto RPC connections are stateful (although an application built on Cap'n Proto could # export a stateless interface). As in CapTP, for each open connection, a vat maintains four state # tables: questions, answers, imports, and exports. See the diagram at: # http://www.erights.org/elib/distrib/captp/4tables.html # # The question table corresponds to the other end's answer table, and the imports table corresponds # to the other end's exports table. # # The entries in each table are identified by ID numbers (defined below as 32-bit integers). These # numbers are always specific to the connection; a newly-established connection starts with no # valid IDs. Since low-numbered IDs will pack better, it is suggested that IDs be assigned like # Unix file descriptors -- prefer the lowest-number ID that is currently available. # # IDs in the questions/answers tables are chosen by the questioner and generally represent method # calls that are in progress. # # IDs in the imports/exports tables are chosen by the exporter and generally represent objects on # which methods may be called. Exports may be "settled", meaning the exported object is an actual # object living in the exporter's vat, or they may be "promises", meaning the exported object is # the as-yet-unknown result of an ongoing operation and will eventually be resolved to some other # object once that operation completes. Calls made to a promise will be forwarded to the eventual # target once it is known. The eventual replacement object does *not* get the same ID as the # promise, as it may turn out to be an object that is already exported (so already has an ID) or # may even live in a completely different vat (and so won't get an ID on the same export table # at all). # # IDs can be reused over time. To make this safe, we carefully define the lifetime of IDs. Since # messages using the ID could be traveling in both directions simultaneously, we must define the # end of life of each ID _in each direction_. The ID is only safe to reuse once it has been # released by both sides. # # When a Cap'n Proto connection is lost, everything on the four tables is lost. All questions are # canceled and throw exceptions. All imports become broken (all future calls to them throw # exceptions). All exports and answers are implicitly released. The only things not lost are # persistent capabilities (`SturdyRef`s). The application must plan for this and should respond by # establishing a new connection and restoring from these persistent capabilities. using QuestionId = UInt32; # **(level 0)** # # Identifies a question in the sender's question table (which corresponds to the receiver's answer # table). The questioner (caller) chooses an ID when making a call. The ID remains valid in # caller -> callee messages until a Finish message is sent, and remains valid in callee -> caller # messages until a Return message is sent. using AnswerId = QuestionId; # **(level 0)** # # Identifies an answer in the sender's answer table (which corresponds to the receiver's question # table). # # AnswerId is physically equivalent to QuestionId, since the question and answer tables correspond, # but we define a separate type for documentation purposes: we always use the type representing # the sender's point of view. using ExportId = UInt32; # **(level 1)** # # Identifies an exported capability or promise in the sender's export table (which corresponds # to the receiver's import table). The exporter chooses an ID before sending a capability over the # wire. If the capability is already in the table, the exporter should reuse the same ID. If the # ID is a promise (as opposed to a settled capability), this must be indicated at the time the ID # is introduced (e.g. by using `senderPromise` instead of `senderHosted` in `CapDescriptor`); in # this case, the importer shall expect a later `Resolve` message that replaces the promise. # # ExportId/ImportIds are subject to reference counting. Whenever an `ExportId` is sent over the # wire (from the exporter to the importer), the export's reference count is incremented (unless # otherwise specified). The reference count is later decremented by a `Release` message. Since # the `Release` message can specify an arbitrary number by which to reduce the reference count, the # importer should usually batch reference decrements and only send a `Release` when it believes the # reference count has hit zero. Of course, it is possible that a new reference to the export is # in-flight at the time that the `Release` message is sent, so it is necessary for the exporter to # keep track of the reference count on its end as well to avoid race conditions. # # When a connection is lost, all exports are implicitly released. It is not possible to restore # a connection state after disconnect (although a transport layer could implement a concept of # persistent connections if it is transparent to the RPC layer). using ImportId = ExportId; # **(level 1)** # # Identifies an imported capability or promise in the sender's import table (which corresponds to # the receiver's export table). # # ImportId is physically equivalent to ExportId, since the export and import tables correspond, # but we define a separate type for documentation purposes: we always use the type representing # the sender's point of view. # # An `ImportId` remains valid in importer -> exporter messages until the importer has sent # `Release` messages that (it believes) have reduced the reference count to zero. # ======================================================================================== # Messages struct Message { # An RPC connection is a bi-directional stream of Messages. union { unimplemented @0 :Message; # The sender previously received this message from the peer but didn't understand it or doesn't # yet implement the functionality that was requested. So, the sender is echoing the message # back. In some cases, the receiver may be able to recover from this by pretending the sender # had taken some appropriate "null" action. # # For example, say `resolve` is received by a level 0 implementation (because a previous call # or return happened to contain a promise). The level 0 implementation will echo it back as # `unimplemented`. The original sender can then simply release the cap to which the promise # had resolved, thus avoiding a leak. # # For any message type that introduces a question, if the message comes back unimplemented, # the original sender may simply treat it as if the question failed with an exception. # # In cases where there is no sensible way to react to an `unimplemented` message (without # resource leaks or other serious problems), the connection may need to be aborted. This is # a gray area; different implementations may take different approaches. abort @1 :Exception; # Sent when a connection is being aborted due to an unrecoverable error. This could be e.g. # because the sender received an invalid or nonsensical message or because the sender had an # internal error. The sender will shut down the outgoing half of the connection after `abort` # and will completely close the connection shortly thereafter (it's up to the sender how much # of a time buffer they want to offer for the client to receive the `abort` before the # connection is reset). # Level 0 features ----------------------------------------------- bootstrap @8 :Bootstrap; # Request the peer's bootstrap interface. call @2 :Call; # Begin a method call. return @3 :Return; # Complete a method call. finish @4 :Finish; # Release a returned answer / cancel a call. # Level 1 features ----------------------------------------------- resolve @5 :Resolve; # Resolve a previously-sent promise. release @6 :Release; # Release a capability so that the remote object can be deallocated. disembargo @13 :Disembargo; # Lift an embargo used to enforce E-order over promise resolution. # Level 2 features ----------------------------------------------- obsoleteSave @7 :AnyPointer; # Obsolete request to save a capability, resulting in a SturdyRef. This has been replaced # by the `Persistent` interface defined in `persistent.capnp`. This operation was never # implemented. obsoleteDelete @9 :AnyPointer; # Obsolete way to delete a SturdyRef. This operation was never implemented. # Level 3 features ----------------------------------------------- provide @10 :Provide; # Provide a capability to a third party. accept @11 :Accept; # Accept a capability provided by a third party. # Level 4 features ----------------------------------------------- join @12 :Join; # Directly connect to the common root of two or more proxied caps. } } # Level 0 message types ---------------------------------------------- struct Bootstrap { # **(level 0)** # # Get the "bootstrap" interface exported by the remote vat. # # For level 0, 1, and 2 implementations, the "bootstrap" interface is simply the main interface # exported by a vat. If the vat acts as a server fielding connections from clients, then the # bootstrap interface defines the basic functionality available to a client when it connects. # The exact interface definition obviously depends on the application. # # We call this a "bootstrap" because in an ideal Cap'n Proto world, bootstrap interfaces would # never be used. In such a world, any time you connect to a new vat, you do so because you # received an introduction from some other vat (see `ThirdPartyCapId`). Thus, the first message # you send is `Accept`, and further communications derive from there. `Bootstrap` is not used. # # In such an ideal world, DNS itself would support Cap'n Proto -- performing a DNS lookup would # actually return a new Cap'n Proto capability, thus introducing you to the target system via # level 3 RPC. Applications would receive the capability to talk to DNS in the first place as # an initial endowment or part of a Powerbox interaction. Therefore, an app can form arbitrary # connections without ever using `Bootstrap`. # # Of course, in the real world, DNS is not Cap'n-Proto-based, and we don't want Cap'n Proto to # require a whole new internet infrastructure to be useful. Therefore, we offer bootstrap # interfaces as a way to get up and running without a level 3 introduction. Thus, bootstrap # interfaces are used to "bootstrap" from other, non-Cap'n-Proto-based means of service discovery, # such as legacy DNS. # # Note that a vat need not provide a bootstrap interface, and in fact many vats (especially those # acting as clients) do not. In this case, the vat should either reply to `Bootstrap` with a # `Return` indicating an exception, or should return a dummy capability with no methods. questionId @0 :QuestionId; # A new question ID identifying this request, which will eventually receive a Return message # containing the restored capability. deprecatedObjectId @1 :AnyPointer; # ** DEPRECATED ** # # A Vat may export multiple bootstrap interfaces. In this case, `deprecatedObjectId` specifies # which one to return. If this pointer is null, then the default bootstrap interface is returned. # # As of verison 0.5, use of this field is deprecated. If a service wants to export multiple # bootstrap interfaces, it should instead define a single bootstrap interface that has methods # that return each of the other interfaces. # # **History** # # In the first version of Cap'n Proto RPC (0.4.x) the `Bootstrap` message was called `Restore`. # At the time, it was thought that this would eventually serve as the way to restore SturdyRefs # (level 2). Meanwhile, an application could offer its "main" interface on a well-known # (non-secret) SturdyRef. # # Since level 2 RPC was not implemented at the time, the `Restore` message was in practice only # used to obtain the main interface. Since most applications had only one main interface that # they wanted to restore, they tended to designate this with a null `objectId`. # # Unfortunately, the earliest version of the EZ RPC interfaces set a precedent of exporting # multiple main interfaces by allowing them to be exported under string names. In this case, # `objectId` was a Text value specifying the name. # # All of this proved problematic for several reasons: # # - The arrangement assumed that a client wishing to restore a SturdyRef would know exactly what # machine to connect to and would be able to immediately restore a SturdyRef on connection. # However, in practice, the ability to restore SturdyRefs is itself a capability that may # require going through an authentication process to obtain. Thus, it makes more sense to # define a "restorer service" as a full Cap'n Proto interface. If this restorer interface is # offered as the vat's bootstrap interface, then this is equivalent to the old arrangement. # # - Overloading "Restore" for the purpose of obtaining well-known capabilities encouraged the # practice of exporting singleton services with string names. If singleton services are desired, # it is better to have one main interface that has methods that can be used to obtain each # service, in order to get all the usual benefits of schemas and type checking. # # - Overloading "Restore" also had a security problem: Often, "main" or "well-known" # capabilities exported by a vat are in fact not public: they are intended to be accessed only # by clients who are capable of forming a connection to the vat. This can lead to trouble if # the client itself has other clients and wishes to foward some `Restore` requests from those # external clients -- it has to be very careful not to allow through `Restore` requests # addressing the default capability. # # For example, consider the case of a sandboxed Sandstorm application and its supervisor. The # application exports a default capability to its supervisor that provides access to # functionality that only the supervisor is supposed to access. Meanwhile, though, applications # may publish other capabilities that may be persistent, in which case the application needs # to field `Restore` requests that could come from anywhere. These requests of course have to # pass through the supervisor, as all communications with the outside world must. But, the # supervisor has to be careful not to honor an external request addressing the application's # default capability, since this capability is privileged. Unfortunately, the default # capability cannot be given an unguessable name, because then the supervisor itself would not # be able to address it! # # As of Cap'n Proto 0.5, `Restore` has been renamed to `Bootstrap` and is no longer planned for # use in restoring SturdyRefs. # # Note that 0.4 also defined a message type called `Delete` that, like `Restore`, addressed a # SturdyRef, but indicated that the client would not restore the ref again in the future. This # operation was never implemented, so it was removed entirely. If a "delete" operation is desired, # it should exist as a method on the same interface that handles restoring SturdyRefs. However, # the utility of such an operation is questionable. You wouldn't be able to rely on it for # garbage collection since a client could always disappear permanently without remembering to # delete all its SturdyRefs, thus leaving them dangling forever. Therefore, it is advisable to # design systems such that SturdyRefs never represent "owned" pointers. # # For example, say a SturdyRef points to an image file hosted on some server. That image file # should also live inside a collection (a gallery, perhaps) hosted on the same server, owned by # a user who can delete the image at any time. If the user deletes the image, the SturdyRef # stops working. On the other hand, if the SturdyRef is discarded, this has no effect on the # existence of the image in its collection. } struct Call { # **(level 0)** # # Message type initiating a method call on a capability. questionId @0 :QuestionId; # A number, chosen by the caller, that identifies this call in future messages. This number # must be different from all other calls originating from the same end of the connection (but # may overlap with question IDs originating from the opposite end). A fine strategy is to use # sequential question IDs, but the recipient should not assume this. # # A question ID can be reused once both: # - A matching Return has been received from the callee. # - A matching Finish has been sent from the caller. target @1 :MessageTarget; # The object that should receive this call. interfaceId @2 :UInt64; # The type ID of the interface being called. Each capability may implement multiple interfaces. methodId @3 :UInt16; # The ordinal number of the method to call within the requested interface. allowThirdPartyTailCall @8 :Bool = false; # Indicates whether or not the receiver is allowed to send a `Return` containing # `acceptFromThirdParty`. Level 3 implementations should set this true. Otherwise, the callee # will have to proxy the return in the case of a tail call to a third-party vat. params @4 :Payload; # The call parameters. `params.content` is a struct whose fields correspond to the parameters of # the method. sendResultsTo :union { # Where should the return message be sent? caller @5 :Void; # Send the return message back to the caller (the usual). yourself @6 :Void; # **(level 1)** # # Don't actually return the results to the sender. Instead, hold on to them and await # instructions from the sender regarding what to do with them. In particular, the sender # may subsequently send a `Return` for some other call (which the receiver had previously made # to the sender) with `takeFromOtherQuestion` set. The results from this call are then used # as the results of the other call. # # When `yourself` is used, the receiver must still send a `Return` for the call, but sets the # field `resultsSentElsewhere` in that `Return` rather than including the results. # # This feature can be used to implement tail calls in which a call from Vat A to Vat B ends up # returning the result of a call from Vat B back to Vat A. # # In particular, the most common use case for this feature is when Vat A makes a call to a # promise in Vat B, and then that promise ends up resolving to a capability back in Vat A. # Vat B must forward all the queued calls on that promise back to Vat A, but can set `yourself` # in the calls so that the results need not pass back through Vat B. # # For example: # - Alice, in Vat A, calls foo() on Bob in Vat B. # - Alice makes a pipelined call bar() on the promise returned by foo(). # - Later on, Bob resolves the promise from foo() to point at Carol, who lives in Vat A (next # to Alice). # - Vat B dutifully forwards the bar() call to Carol. Let us call this forwarded call bar'(). # Notice that bar() and bar'() are travelling in opposite directions on the same network # link. # - The `Call` for bar'() has `sendResultsTo` set to `yourself`. # - Vat B sends a `Return` for bar() with `takeFromOtherQuestion` set in place of the results, # with the value set to the question ID of bar'(). Vat B does not wait for bar'() to return, # as doing so would introduce unnecessary round trip latency. # - Vat A receives bar'() and delivers it to Carol. # - When bar'() returns, Vat A sends a `Return` for bar'() to Vat B, with `resultsSentElsewhere` # set in place of results. # - Vat A sends a `Finish` for the bar() call to Vat B. # - Vat B receives the `Finish` for bar() and sends a `Finish` for bar'(). thirdParty @7 :RecipientId; # **(level 3)** # # The call's result should be returned to a different vat. The receiver (the callee) expects # to receive an `Accept` message from the indicated vat, and should return the call's result # to it, rather than to the sender of the `Call`. # # This operates much like `yourself`, above, except that Carol is in a separate Vat C. `Call` # messages are sent from Vat A -> Vat B and Vat B -> Vat C. A `Return` message is sent from # Vat B -> Vat A that contains `acceptFromThirdParty` in place of results. When Vat A sends # an `Accept` to Vat C, it receives back a `Return` containing the call's actual result. Vat C # also sends a `Return` to Vat B with `resultsSentElsewhere`. } } struct Return { # **(level 0)** # # Message type sent from callee to caller indicating that the call has completed. answerId @0 :AnswerId; # Equal to the QuestionId of the corresponding `Call` message. releaseParamCaps @1 :Bool = true; # If true, all capabilities that were in the params should be considered released. The sender # must not send separate `Release` messages for them. Level 0 implementations in particular # should always set this true. This defaults true because if level 0 implementations forget to # set it they'll never notice (just silently leak caps), but if level >=1 implementations forget # to set it to false they'll quickly get errors. # # The receiver should act as if the sender had sent a release message with count=1 for each # CapDescriptor in the original Call message. union { results @2 :Payload; # The result. # # For regular method calls, `results.content` points to the result struct. # # For a `Return` in response to an `Accept` or `Bootstrap`, `results` contains a single # capability (rather than a struct), and `results.content` is just a capability pointer with # index 0. A `Finish` is still required in this case. exception @3 :Exception; # Indicates that the call failed and explains why. canceled @4 :Void; # Indicates that the call was canceled due to the caller sending a Finish message # before the call had completed. resultsSentElsewhere @5 :Void; # This is set when returning from a `Call` that had `sendResultsTo` set to something other # than `caller`. # # It doesn't matter too much when this is sent, as the receiver doesn't need to do anything # with it, but the C++ implementation appears to wait for the call to finish before sending # this. takeFromOtherQuestion @6 :QuestionId; # The sender has also sent (before this message) a `Call` with the given question ID and with # `sendResultsTo.yourself` set, and the results of that other call should be used as the # results here. `takeFromOtherQuestion` can only used once per question. acceptFromThirdParty @7 :ThirdPartyCapId; # **(level 3)** # # The caller should contact a third-party vat to pick up the results. An `Accept` message # sent to the vat will return the result. This pairs with `Call.sendResultsTo.thirdParty`. # It should only be used if the corresponding `Call` had `allowThirdPartyTailCall` set. } } struct Finish { # **(level 0)** # # Message type sent from the caller to the callee to indicate: # 1) The questionId will no longer be used in any messages sent by the callee (no further # pipelined requests). # 2) If the call has not returned yet, the caller no longer cares about the result. If nothing # else cares about the result either (e.g. there are no other outstanding calls pipelined on # the result of this one) then the callee may wish to immediately cancel the operation and # send back a Return message with "canceled" set. However, implementations are not required # to support premature cancellation -- instead, the implementation may wait until the call # actually completes and send a normal `Return` message. # # TODO(someday): Should we separate (1) and implicitly releasing result capabilities? It would be # possible and useful to notify the server that it doesn't need to keep around the response to # service pipeline requests even though the caller still wants to receive it / hasn't yet # finished processing it. It could also be useful to notify the server that it need not marshal # the results because the caller doesn't want them anyway, even if the caller is still sending # pipelined calls, although this seems less useful (just saving some bytes on the wire). questionId @0 :QuestionId; # ID of the call whose result is to be released. releaseResultCaps @1 :Bool = true; # If true, all capabilities that were in the results should be considered released. The sender # must not send separate `Release` messages for them. Level 0 implementations in particular # should always set this true. This defaults true because if level 0 implementations forget to # set it they'll never notice (just silently leak caps), but if level >=1 implementations forget # set it false they'll quickly get errors. } # Level 1 message types ---------------------------------------------- struct Resolve { # **(level 1)** # # Message type sent to indicate that a previously-sent promise has now been resolved to some other # object (possibly another promise) -- or broken, or canceled. # # Keep in mind that it's possible for a `Resolve` to be sent to a level 0 implementation that # doesn't implement it. For example, a method call or return might contain a capability in the # payload. Normally this is fine even if the receiver is level 0, because they will implicitly # release all such capabilities on return / finish. But if the cap happens to be a promise, then # a follow-up `Resolve` may be sent regardless of this release. The level 0 receiver will reply # with an `unimplemented` message, and the sender (of the `Resolve`) can respond to this as if the # receiver had immediately released any capability to which the promise resolved. # # When implementing promise resolution, it's important to understand how embargos work and the # tricky case of the Tribble 4-way race condition. See the comments for the Disembargo message, # below. promiseId @0 :ExportId; # The ID of the promise to be resolved. # # Unlike all other instances of `ExportId` sent from the exporter, the `Resolve` message does # _not_ increase the reference count of `promiseId`. In fact, it is expected that the receiver # will release the export soon after receiving `Resolve`, and the sender will not send this # `ExportId` again until it has been released and recycled. # # When an export ID sent over the wire (e.g. in a `CapDescriptor`) is indicated to be a promise, # this indicates that the sender will follow up at some point with a `Resolve` message. If the # same `promiseId` is sent again before `Resolve`, still only one `Resolve` is sent. If the # same ID is sent again later _after_ a `Resolve`, it can only be because the export's # reference count hit zero in the meantime and the ID was re-assigned to a new export, therefore # this later promise does _not_ correspond to the earlier `Resolve`. # # If a promise ID's reference count reaches zero before a `Resolve` is sent, the `Resolve` # message may or may not still be sent (the `Resolve` may have already been in-flight when # `Release` was sent, but if the `Release` is received before `Resolve` then there is no longer # any reason to send a `Resolve`). Thus a `Resolve` may be received for a promise of which # the receiver has no knowledge, because it already released it earlier. In this case, the # receiver should simply release the capability to which the promise resolved. union { cap @1 :CapDescriptor; # The object to which the promise resolved. # # The sender promises that from this point forth, until `promiseId` is released, it shall # simply forward all messages to the capability designated by `cap`. This is true even if # `cap` itself happens to designate another promise, and that other promise later resolves -- # messages sent to `promiseId` shall still go to that other promise, not to its resolution. # This is important in the case that the receiver of the `Resolve` ends up sending a # `Disembargo` message towards `promiseId` in order to control message ordering -- that # `Disembargo` really needs to reflect back to exactly the object designated by `cap` even # if that object is itself a promise. exception @2 :Exception; # Indicates that the promise was broken. } } struct Release { # **(level 1)** # # Message type sent to indicate that the sender is done with the given capability and the receiver # can free resources allocated to it. id @0 :ImportId; # What to release. referenceCount @1 :UInt32; # The amount by which to decrement the reference count. The export is only actually released # when the reference count reaches zero. } struct Disembargo { # **(level 1)** # # Message sent to indicate that an embargo on a recently-resolved promise may now be lifted. # # Embargos are used to enforce E-order in the presence of promise resolution. That is, if an # application makes two calls foo() and bar() on the same capability reference, in that order, # the calls should be delivered in the order in which they were made. But if foo() is called # on a promise, and that promise happens to resolve before bar() is called, then the two calls # may travel different paths over the network, and thus could arrive in the wrong order. In # this case, the call to `bar()` must be embargoed, and a `Disembargo` message must be sent along # the same path as `foo()` to ensure that the `Disembargo` arrives after `foo()`. Once the # `Disembargo` arrives, `bar()` can then be delivered. # # There are two particular cases where embargos are important. Consider object Alice, in Vat A, # who holds a promise P, pointing towards Vat B, that eventually resolves to Carol. The two # cases are: # - Carol lives in Vat A, i.e. next to Alice. In this case, Vat A needs to send a `Disembargo` # message that echos through Vat B and back, to ensure that all pipelined calls on the promise # have been delivered. # - Carol lives in a different Vat C. When the promise resolves, a three-party handoff occurs # (see `Provide` and `Accept`, which constitute level 3 of the protocol). In this case, we # piggyback on the state that has already been set up to handle the handoff: the `Accept` # message (from Vat A to Vat C) is embargoed, as are all pipelined messages sent to it, while # a `Disembargo` message is sent from Vat A through Vat B to Vat C. See `Accept.embargo` for # an example. # # Note that in the case where Carol actually lives in Vat B (i.e., the same vat that the promise # already pointed at), no embargo is needed, because the pipelined calls are delivered over the # same path as the later direct calls. # # Keep in mind that promise resolution happens both in the form of Resolve messages as well as # Return messages (which resolve PromisedAnswers). Embargos apply in both cases. # # An alternative strategy for enforcing E-order over promise resolution could be for Vat A to # implement the embargo internally. When Vat A is notified of promise resolution, it could # send a dummy no-op call to promise P and wait for it to complete. Until that call completes, # all calls to the capability are queued locally. This strategy works, but is pessimistic: # in the three-party case, it requires an A -> B -> C -> B -> A round trip before calls can start # being delivered directly to from Vat A to Vat C. The `Disembargo` message allows latency to be # reduced. (In the two-party loopback case, the `Disembargo` message is just a more explicit way # of accomplishing the same thing as a no-op call, but isn't any faster.) # # *The Tribble 4-way Race Condition* # # Any implementation of promise resolution and embargos must be aware of what we call the # "Tribble 4-way race condition", after Dean Tribble, who explained the problem in a lively # Friam meeting. # # Embargos are designed to work in the case where a two-hop path is being shortened to one hop. # But sometimes there are more hops. Imagine that Alice has a reference to a remote promise P1 # that eventually resolves to _another_ remote promise P2 (in a third vat), which _at the same # time_ happens to resolve to Bob (in a fourth vat). In this case, we're shortening from a 3-hop # path (with four parties) to a 1-hop path (Alice -> Bob). # # Extending the embargo/disembargo protocol to be able to shorted multiple hops at once seems # difficult. Instead, we make a rule that prevents this case from coming up: # # One a promise P has been resolved to a remote object reference R, then all further messages # received addressed to P will be forwarded strictly to R. Even if it turns out later that R is # itself a promise, and has resolved to some other object Q, messages sent to P will still be # forwarded to R, not directly to Q (R will of course further forward the messages to Q). # # This rule does not cause a significant performance burden because once P has resolved to R, it # is expected that people sending messages to P will shortly start sending them to R instead and # drop P. P is at end-of-life anyway, so it doesn't matter if it ignores chances to further # optimize its path. target @0 :MessageTarget; # What is to be disembargoed. using EmbargoId = UInt32; # Used in `senderLoopback` and `receiverLoopback`, below. context :union { senderLoopback @1 :EmbargoId; # The sender is requesting a disembargo on a promise that is known to resolve back to a # capability hosted by the sender. As soon as the receiver has echoed back all pipelined calls # on this promise, it will deliver the Disembargo back to the sender with `receiverLoopback` # set to the same value as `senderLoopback`. This value is chosen by the sender, and since # it is also consumed be the sender, the sender can use whatever strategy it wants to make sure # the value is unambiguous. # # The receiver must verify that the target capability actually resolves back to the sender's # vat. Otherwise, the sender has committed a protocol error and should be disconnected. receiverLoopback @2 :EmbargoId; # The receiver previously sent a `senderLoopback` Disembargo towards a promise resolving to # this capability, and that Disembargo is now being echoed back. accept @3 :Void; # **(level 3)** # # The sender is requesting a disembargo on a promise that is known to resolve to a third-party # capability that the sender is currently in the process of accepting (using `Accept`). # The receiver of this `Disembargo` has an outstanding `Provide` on said capability. The # receiver should now send a `Disembargo` with `provide` set to the question ID of that # `Provide` message. # # See `Accept.embargo` for an example. provide @4 :QuestionId; # **(level 3)** # # The sender is requesting a disembargo on a capability currently being provided to a third # party. The question ID identifies the `Provide` message previously sent by the sender to # this capability. On receipt, the receiver (the capability host) shall release the embargo # on the `Accept` message that it has received from the third party. See `Accept.embargo` for # an example. } } # Level 2 message types ---------------------------------------------- # See persistent.capnp. # Level 3 message types ---------------------------------------------- struct Provide { # **(level 3)** # # Message type sent to indicate that the sender wishes to make a particular capability implemented # by the receiver available to a third party for direct access (without the need for the third # party to proxy through the sender). # # (In CapTP, `Provide` and `Accept` are methods of the global `NonceLocator` object exported by # every vat. In Cap'n Proto, we bake this into the core protocol.) questionId @0 :QuestionId; # Question ID to be held open until the recipient has received the capability. A result will be # returned once the third party has successfully received the capability. The sender must at some # point send a `Finish` message as with any other call, and that message can be used to cancel the # whole operation. target @1 :MessageTarget; # What is to be provided to the third party. recipient @2 :RecipientId; # Identity of the third party that is expected to pick up the capability. } struct Accept { # **(level 3)** # # Message type sent to pick up a capability hosted by the receiving vat and provided by a third # party. The third party previously designated the capability using `Provide`. # # This message is also used to pick up a redirected return -- see `Return.acceptFromThirdParty`. questionId @0 :QuestionId; # A new question ID identifying this accept message, which will eventually receive a Return # message containing the provided capability (or the call result in the case of a redirected # return). provision @1 :ProvisionId; # Identifies the provided object to be picked up. embargo @2 :Bool; # If true, this accept shall be temporarily embargoed. The resulting `Return` will not be sent, # and any pipelined calls will not be delivered, until the embargo is released. The receiver # (the capability host) will expect the provider (the vat that sent the `Provide` message) to # eventually send a `Disembargo` message with the field `context.provide` set to the question ID # of the original `Provide` message. At that point, the embargo is released and the queued # messages are delivered. # # For example: # - Alice, in Vat A, holds a promise P, which currently points toward Vat B. # - Alice calls foo() on P. The `Call` message is sent to Vat B. # - The promise P in Vat B ends up resolving to Carol, in Vat C. # - Vat B sends a `Provide` message to Vat C, identifying Vat A as the recipient. # - Vat B sends a `Resolve` message to Vat A, indicating that the promise has resolved to a # `ThirdPartyCapId` identifying Carol in Vat C. # - Vat A sends an `Accept` message to Vat C to pick up the capability. Since Vat A knows that # it has an outstanding call to the promise, it sets `embargo` to `true` in the `Accept` # message. # - Vat A sends a `Disembargo` message to Vat B on promise P, with `context.accept` set. # - Alice makes a call bar() to promise P, which is now pointing towards Vat C. Alice doesn't # know anything about the mechanics of promise resolution happening under the hood, but she # expects that bar() will be delivered after foo() because that is the order in which she # initiated the calls. # - Vat A sends the bar() call to Vat C, as a pipelined call on the result of the `Accept` (which # hasn't returned yet, due to the embargo). Since calls to the newly-accepted capability # are embargoed, Vat C does not deliver the call yet. # - At some point, Vat B forwards the foo() call from the beginning of this example on to Vat C. # - Vat B forwards the `Disembargo` from Vat A on to vat C. It sets `context.provide` to the # question ID of the `Provide` message it had sent previously. # - Vat C receives foo() before `Disembargo`, thus allowing it to correctly deliver foo() # before delivering bar(). # - Vat C receives `Disembargo` from Vat B. It can now send a `Return` for the `Accept` from # Vat A, as well as deliver bar(). } # Level 4 message types ---------------------------------------------- struct Join { # **(level 4)** # # Message type sent to implement E.join(), which, given a number of capabilities that are # expected to be equivalent, finds the underlying object upon which they all agree and forms a # direct connection to it, skipping any proxies that may have been constructed by other vats # while transmitting the capability. See: # http://erights.org/elib/equality/index.html # # Note that this should only serve to bypass fully-transparent proxies -- proxies that were # created merely for convenience, without any intention of hiding the underlying object. # # For example, say Bob holds two capabilities hosted by Alice and Carol, but he expects that both # are simply proxies for a capability hosted elsewhere. He then issues a join request, which # operates as follows: # - Bob issues Join requests on both Alice and Carol. Each request contains a different piece # of the JoinKey. # - Alice is proxying a capability hosted by Dana, so forwards the request to Dana's cap. # - Dana receives the first request and sees that the JoinKeyPart is one of two. She notes that # she doesn't have the other part yet, so she records the request and responds with a # JoinResult. # - Alice relays the JoinAnswer back to Bob. # - Carol is also proxying a capability from Dana, and so forwards her Join request to Dana as # well. # - Dana receives Carol's request and notes that she now has both parts of a JoinKey. She # combines them in order to form information needed to form a secure connection to Bob. She # also responds with another JoinResult. # - Bob receives the responses from Alice and Carol. He uses the returned JoinResults to # determine how to connect to Dana and attempts to form the connection. Since Bob and Dana now # agree on a secret key that neither Alice nor Carol ever saw, this connection can be made # securely even if Alice or Carol is conspiring against the other. (If Alice and Carol are # conspiring _together_, they can obviously reproduce the key, but this doesn't matter because # the whole point of the join is to verify that Alice and Carol agree on what capability they # are proxying.) # # If the two capabilities aren't actually proxies of the same object, then the join requests # will come back with conflicting `hostId`s and the join will fail before attempting to form any # connection. questionId @0 :QuestionId; # Question ID used to respond to this Join. (Note that this ID only identifies one part of the # request for one hop; each part has a different ID and relayed copies of the request have # (probably) different IDs still.) # # The receiver will reply with a `Return` whose `results` is a JoinResult. This `JoinResult` # is relayed from the joined object's host, possibly with transformation applied as needed # by the network. # # Like any return, the result must be released using a `Finish`. However, this release # should not occur until the joiner has either successfully connected to the joined object. # Vats relaying a `Join` message similarly must not release the result they receive until the # return they relayed back towards the joiner has itself been released. This allows the # joined object's host to detect when the Join operation is canceled before completing -- if # it receives a `Finish` for one of the join results before the joiner successfully # connects. It can then free any resources it had allocated as part of the join. target @1 :MessageTarget; # The capability to join. keyPart @2 :JoinKeyPart; # A part of the join key. These combine to form the complete join key, which is used to establish # a direct connection. # TODO(before implementing): Change this so that multiple parts can be sent in a single Join # message, so that if multiple join parts are going to cross the same connection they can be sent # together, so that the receive can potentially optimize its handling of them. In the case where # all parts are bundled together, should the recipient be expected to simply return a cap, so # that the caller can immediately start pipelining to it? } # ======================================================================================== # Common structures used in messages struct MessageTarget { # The target of a `Call` or other messages that target a capability. union { importedCap @0 :ImportId; # This message is to a capability or promise previously imported by the caller (exported by # the receiver). promisedAnswer @1 :PromisedAnswer; # This message is to a capability that is expected to be returned by another call that has not # yet been completed. # # At level 0, this is supported only for addressing the result of a previous `Bootstrap`, so # that initial startup doesn't require a round trip. } } struct Payload { # Represents some data structure that might contain capabilities. content @0 :AnyPointer; # Some Cap'n Proto data structure. Capability pointers embedded in this structure index into # `capTable`. capTable @1 :List(CapDescriptor); # Descriptors corresponding to the cap pointers in `content`. } struct CapDescriptor { # **(level 1)** # # When an application-defined type contains an interface pointer, that pointer contains an index # into the message's capability table -- i.e. the `capTable` part of the `Payload`. Each # capability in the table is represented as a `CapDescriptor`. The runtime API should not reveal # the CapDescriptor directly to the application, but should instead wrap it in some kind of # callable object with methods corresponding to the interface that the capability implements. # # Keep in mind that `ExportIds` in a `CapDescriptor` are subject to reference counting. See the # description of `ExportId`. # # Note that it is currently not possible to include a broken capability in the CapDescriptor # table. Instead, create a new export (`senderPromise`) for each broken capability and then # immediately follow the payload-bearing Call or Return message with one Resolve message for each # broken capability, resolving it to an exception. union { none @0 :Void; # There is no capability here. This `CapDescriptor` should not appear in the payload content. # A `none` CapDescriptor can be generated when an application inserts a capability into a # message and then later changes its mind and removes it -- rewriting all of the other # capability pointers may be hard, so instead a tombstone is left, similar to the way a removed # struct or list instance is zeroed out of the message but the space is not reclaimed. # Hopefully this is unusual. senderHosted @1 :ExportId; # The ID of a capability in the sender's export table (receiver's import table). It may be a # newly allocated table entry, or an existing entry (increments the reference count). senderPromise @2 :ExportId; # A promise that the sender will resolve later. The sender will send exactly one Resolve # message at a future point in time to replace this promise. Note that even if the same # `senderPromise` is received multiple times, only one `Resolve` is sent to cover all of # them. If `senderPromise` is released before the `Resolve` is sent, the sender (of this # `CapDescriptor`) may choose not to send the `Resolve` at all. receiverHosted @3 :ImportId; # A capability (or promise) previously exported by the receiver (imported by the sender). receiverAnswer @4 :PromisedAnswer; # A capability expected to be returned in the results of a currently-outstanding call posed # by the sender. thirdPartyHosted @5 :ThirdPartyCapDescriptor; # **(level 3)** # # A capability that lives in neither the sender's nor the receiver's vat. The sender needs # to form a direct connection to a third party to pick up the capability. # # Level 1 and 2 implementations that receive a `thirdPartyHosted` may simply send calls to its # `vine` instead. } attachedFd @6 :UInt8 = 0xff; # If the RPC message in which this CapDescriptor was delivered also had file descriptors # attached, and `fd` is a valid index into the list of attached file descriptors, then # that file descriptor should be attached to this capability. If `attachedFd` is out-of-bounds # for said list, then no FD is attached. # # For example, if the RPC message arrived over a Unix socket, then file descriptors may be # attached by sending an SCM_RIGHTS ancillary message attached to the data bytes making up the # raw message. Receivers who wish to opt into FD passing should arrange to receive SCM_RIGHTS # whenever receiving an RPC message. Senders who wish to send FDs need not verify whether the # receiver knows how to receive them, because the operating system will automatically discard # ancillary messages like SCM_RIGHTS if the receiver doesn't ask to receive them, including # automatically closing any FDs. # # It is up to the application protocol to define what capabilities are expected to have file # descriptors attached, and what those FDs mean. But, for example, an application could use this # to open a file on disk and then transmit the open file descriptor to a sandboxed process that # does not otherwise have permission to access the filesystem directly. This is usually an # optimization: the sending process could instead provide an RPC interface supporting all the # operations needed (such as reading and writing a file), but by passing the file descriptor # directly, the recipient can often perform operations much more efficiently. Application # designers are encouraged to provide such RPC interfaces and automatically fall back to them # when FD passing is not available, so that the application can still work when the parties are # remote over a network. # # An attached FD is most often associated with a `senderHosted` descriptor. It could also make # sense in the case of `thirdPartyHosted`: in this case, the sender is forwarding the FD that # they received from the third party, so that the receiver can start using it without first # interacting with the third party. This is an optional optimization -- the middleman may choose # not to forward capabilities, in which case the receiver will need to complete the handshake # with the third party directly before receiving the FD. If an implementation receives a second # attached FD after having already received one previously (e.g. both in a `thirdPartyHosted` # CapDescriptor and then later again when receiving the final capability directly from the # third party), the implementation should discard the later FD and stick with the original. At # present, there is no known reason why other capability types (e.g. `receiverHosted`) would want # to carry an attached FD, but we reserve the right to define a meaning for this in the future. # # Each file descriptor attached to the message must be used in no more than one CapDescriptor, # so that the receiver does not need to use dup() or refcounting to handle the possibility of # multiple capabilities using the same descriptor. If multiple CapDescriptors do point to the # same FD index, then the receiver can arbitrarily choose which capability ends up having the # FD attached. # # To mitigate DoS attacks, RPC implementations should limit the number of FDs they are willing to # receive in a single message to a small value. If a message happens to contain more than that, # the list is truncated. Moreover, in some cases, FD passing needs to be blocked entirely for # security or implementation reasons, in which case the list may be truncated to zero. Hence, # `attachedFd` might point past the end of the list, which the implementation should treat as if # no FD was attached at all. # # The type of this field was chosen to be UInt8 because Linux supports sending only a maximum # of 253 file descriptors in an SCM_RIGHTS message anyway, and CapDescriptor had two bytes of # padding left -- so after adding this, there is still one byte for a future feature. # Conveniently, this also means we're able to use 0xff as the default value, which will always # be out-of-range (of course, the implementation should explicitly enforce that 255 descriptors # cannot be sent at once, rather than relying on Linux to do so). } struct PromisedAnswer { # **(mostly level 1)** # # Specifies how to derive a promise from an unanswered question, by specifying the path of fields # to follow from the root of the eventual result struct to get to the desired capability. Used # to address method calls to a not-yet-returned capability or to pass such a capability as an # input to some other method call. # # Level 0 implementations must support `PromisedAnswer` only for the case where the answer is # to a `Bootstrap` message. In this case, `path` is always empty since `Bootstrap` always returns # a raw capability. questionId @0 :QuestionId; # ID of the question (in the sender's question table / receiver's answer table) whose answer is # expected to contain the capability. transform @1 :List(Op); # Operations / transformations to apply to the result in order to get the capability actually # being addressed. E.g. if the result is a struct and you want to call a method on a capability # pointed to by a field of the struct, you need a `getPointerField` op. struct Op { union { noop @0 :Void; # Does nothing. This member is mostly defined so that we can make `Op` a union even # though (as of this writing) only one real operation is defined. getPointerField @1 :UInt16; # Get a pointer field within a struct. The number is an index into the pointer section, NOT # a field ordinal, so that the receiver does not need to understand the schema. # TODO(someday): We could add: # - For lists, the ability to address every member of the list, or a slice of the list, the # result of which would be another list. This is useful for implementing the equivalent of # a SQL table join (not to be confused with the `Join` message type). # - Maybe some ability to test a union. # - Probably not a good idea: the ability to specify an arbitrary script to run on the # result. We could define a little stack-based language where `Op` specifies one # "instruction" or transformation to apply. Although this is not a good idea # (over-engineered), any narrower additions to `Op` should be designed as if this # were the eventual goal. } } } struct ThirdPartyCapDescriptor { # **(level 3)** # # Identifies a capability in a third-party vat that the sender wants the receiver to pick up. id @0 :ThirdPartyCapId; # Identifies the third-party host and the specific capability to accept from it. vineId @1 :ExportId; # A proxy for the third-party object exported by the sender. In CapTP terminology this is called # a "vine", because it is an indirect reference to the third-party object that snakes through the # sender vat. This serves two purposes: # # * Level 1 and 2 implementations that don't understand how to connect to a third party may # simply send calls to the vine. Such calls will be forwarded to the third-party by the # sender. # # * Level 3 implementations must release the vine only once they have successfully picked up the # object from the third party. This ensures that the capability is not released by the sender # prematurely. # # The sender will close the `Provide` request that it has sent to the third party as soon as # it receives either a `Call` or a `Release` message directed at the vine. } struct Exception { # **(level 0)** # # Describes an arbitrary error that prevented an operation (e.g. a call) from completing. # # Cap'n Proto exceptions always indicate that something went wrong. In other words, in a fantasy # world where everything always works as expected, no exceptions would ever be thrown. Clients # should only ever catch exceptions as a means to implement fault-tolerance, where "fault" can # mean: # - Bugs. # - Invalid input. # - Configuration errors. # - Network problems. # - Insufficient resources. # - Version skew (unimplemented functionality). # - Other logistical problems. # # Exceptions should NOT be used to flag application-specific conditions that a client is expected # to handle in an application-specific way. Put another way, in the Cap'n Proto world, # "checked exceptions" (where an interface explicitly defines the exceptions it throws and # clients are forced by the type system to handle those exceptions) do NOT make sense. reason @0 :Text; # Human-readable failure description. type @3 :Type; # The type of the error. The purpose of this enum is not to describe the error itself, but # rather to describe how the client might want to respond to the error. enum Type { failed @0; # A generic problem occurred, and it is believed that if the operation were repeated without # any change in the state of the world, the problem would occur again. # # A client might respond to this error by logging it for investigation by the developer and/or # displaying it to the user. overloaded @1; # The request was rejected due to a temporary lack of resources. # # Examples include: # - There's not enough CPU time to keep up with incoming requests, so some are rejected. # - The server ran out of RAM or disk space during the request. # - The operation timed out (took significantly longer than it should have). # # A client might respond to this error by scheduling to retry the operation much later. The # client should NOT retry again immediately since this would likely exacerbate the problem. disconnected @2; # The method failed because a connection to some necessary capability was lost. # # Examples include: # - The client introduced the server to a third-party capability, the connection to that third # party was subsequently lost, and then the client requested that the server use the dead # capability for something. # - The client previously requested that the server obtain a capability from some third party. # The server returned a capability to an object wrapping the third-party capability. Later, # the server's connection to the third party was lost. # - The capability has been revoked. Revocation does not necessarily mean that the client is # no longer authorized to use the capability; it is often used simply as a way to force the # client to repeat the setup process, perhaps to efficiently move them to a new back-end or # get them to recognize some other change that has occurred. # # A client should normally respond to this error by releasing all capabilities it is currently # holding related to the one it called and then re-creating them by restoring SturdyRefs and/or # repeating the method calls used to create them originally. In other words, disconnect and # start over. This should in turn cause the server to obtain a new copy of the capability that # it lost, thus making everything work. # # If the client receives another `disconnected` error in the process of rebuilding the # capability and retrying the call, it should treat this as an `overloaded` error: the network # is currently unreliable, possibly due to load or other temporary issues. unimplemented @3; # The server doesn't implement the requested method. If there is some other method that the # client could call (perhaps an older and/or slower interface), it should try that instead. # Otherwise, this should be treated like `failed`. } obsoleteIsCallersFault @1 :Bool; # OBSOLETE. Ignore. obsoleteDurability @2 :UInt16; # OBSOLETE. See `type` instead. } # ======================================================================================== # Network-specific Parameters # # Some parts of the Cap'n Proto RPC protocol are not specified here because different vat networks # may wish to use different approaches to solving them. For example, on the public internet, you # may want to authenticate vats using public-key cryptography, but on a local intranet with trusted # infrastructure, you may be happy to authenticate based on network address only, or some other # lightweight mechanism. # # To accommodate this, we specify several "parameter" types. Each type is defined here as an # alias for `AnyPointer`, but a specific network will want to define a specific set of types to use. # All vats in a vat network must agree on these parameters in order to be able to communicate. # Inter-network communication can be accomplished through "gateways" that perform translation # between the primitives used on each network; these gateways may need to be deeply stateful, # depending on the translations they perform. # # For interaction over the global internet between parties with no other prior arrangement, a # particular set of bindings for these types is defined elsewhere. (TODO(someday): Specify where # these common definitions live.) # # Another common network type is the two-party network, in which one of the parties typically # interacts with the outside world entirely through the other party. In such a connection between # Alice and Bob, all objects that exist on Bob's other networks appear to Alice as if they were # hosted by Bob himself, and similarly all objects on Alice's network (if she even has one) appear # to Bob as if they were hosted by Alice. This network type is interesting because from the point # of view of a simple application that communicates with only one other party via the two-party # protocol, there are no three-party interactions at all, and joins are unusually simple to # implement, so implementing at level 4 is barely more complicated than implementing at level 1. # Moreover, if you pair an app implementing the two-party network with a container that implements # some other network, the app can then participate on the container's network just as if it # implemented that network directly. The types used by the two-party network are defined in # `rpc-twoparty.capnp`. # # The things that we need to parameterize are: # - How to store capabilities long-term without holding a connection open (mostly level 2). # - How to authenticate vats in three-party introductions (level 3). # - How to implement `Join` (level 4). # # Persistent references # --------------------- # # **(mostly level 2)** # # We want to allow some capabilities to be stored long-term, even if a connection is lost and later # recreated. ExportId is a short-term identifier that is specific to a connection, so it doesn't # help here. We need a way to specify long-term identifiers, as well as a strategy for # reconnecting to a referenced capability later. # # Three-party interactions # ------------------------ # # **(level 3)** # # In cases where more than two vats are interacting, we have situations where VatA holds a # capability hosted by VatB and wants to send that capability to VatC. This can be accomplished # by VatA proxying requests on the new capability, but doing so has two big problems: # - It's inefficient, requiring an extra network hop. # - If VatC receives another capability to the same object from VatD, it is difficult for VatC to # detect that the two capabilities are really the same and to implement the E "join" operation, # which is necessary for certain four-or-more-party interactions, such as the escrow pattern. # See: http://www.erights.org/elib/equality/grant-matcher/index.html # # Instead, we want a way for VatC to form a direct, authenticated connection to VatB. # # Join # ---- # # **(level 4)** # # The `Join` message type and corresponding operation arranges for a direct connection to be formed # between the joiner and the host of the joined object, and this connection must be authenticated. # Thus, the details are network-dependent. using SturdyRef = AnyPointer; # **(level 2)** # # Identifies a persisted capability that can be restored in the future. How exactly a SturdyRef # is restored to a live object is specified along with the SturdyRef definition (i.e. not by # rpc.capnp). # # Generally a SturdyRef needs to specify three things: # - How to reach the vat that can restore the ref (e.g. a hostname or IP address). # - How to authenticate the vat after connecting (e.g. a public key fingerprint). # - The identity of a specific object hosted by the vat. Generally, this is an opaque pointer whose # format is defined by the specific vat -- the client has no need to inspect the object ID. # It is important that the object ID be unguessable if the object is not public (and objects # should almost never be public). # # The above are only suggestions. Some networks might work differently. For example, a private # network might employ a special restorer service whose sole purpose is to restore SturdyRefs. # In this case, the entire contents of SturdyRef might be opaque, because they are intended only # to be forwarded to the restorer service. using ProvisionId = AnyPointer; # **(level 3)** # # The information that must be sent in an `Accept` message to identify the object being accepted. # # In a network where each vat has a public/private key pair, this could simply be the public key # fingerprint of the provider vat along with a nonce matching the one in the `RecipientId` used # in the `Provide` message sent from that provider. using RecipientId = AnyPointer; # **(level 3)** # # The information that must be sent in a `Provide` message to identify the recipient of the # capability. # # In a network where each vat has a public/private key pair, this could simply be the public key # fingerprint of the recipient along with a nonce matching the one in the `ProvisionId`. # # As another example, when communicating between processes on the same machine over Unix sockets, # RecipientId could simply refer to a file descriptor attached to the message via SCM_RIGHTS. # This file descriptor would be one end of a newly-created socketpair, with the other end having # been sent to the capability's recipient in ThirdPartyCapId. using ThirdPartyCapId = AnyPointer; # **(level 3)** # # The information needed to connect to a third party and accept a capability from it. # # In a network where each vat has a public/private key pair, this could be a combination of the # third party's public key fingerprint, hints on how to connect to the third party (e.g. an IP # address), and the nonce used in the corresponding `Provide` message's `RecipientId` as sent # to that third party (used to identify which capability to pick up). # # As another example, when communicating between processes on the same machine over Unix sockets, # ThirdPartyCapId could simply refer to a file descriptor attached to the message via SCM_RIGHTS. # This file descriptor would be one end of a newly-created socketpair, with the other end having # been sent to the process hosting the capability in RecipientId. using JoinKeyPart = AnyPointer; # **(level 4)** # # A piece of a secret key. One piece is sent along each path that is expected to lead to the same # place. Once the pieces are combined, a direct connection may be formed between the sender and # the receiver, bypassing any men-in-the-middle along the paths. See the `Join` message type. # # The motivation for Joins is discussed under "Supporting Equality" in the "Unibus" protocol # sketch: http://www.erights.org/elib/distrib/captp/unibus.html # # In a network where each vat has a public/private key pair and each vat forms no more than one # connection to each other vat, Joins will rarely -- perhaps never -- be needed, as objects never # need to be transparently proxied and references to the same object sent over the same connection # have the same export ID. Thus, a successful join requires only checking that the two objects # come from the same connection and have the same ID, and then completes immediately. # # However, in networks where two vats may form more than one connection between each other, or # where proxying of objects occurs, joins are necessary. # # Typically, each JoinKeyPart would include a fixed-length data value such that all value parts # XOR'd together forms a shared secret that can be used to form an encrypted connection between # the joiner and the joined object's host. Each JoinKeyPart should also include an indication of # how many parts to expect and a hash of the shared secret (used to match up parts). using JoinResult = AnyPointer; # **(level 4)** # # Information returned as the result to a `Join` message, needed by the joiner in order to form a # direct connection to a joined object. This might simply be the address of the joined object's # host vat, since the `JoinKey` has already been communicated so the two vats already have a shared # secret to use to authenticate each other. # # The `JoinResult` should also contain information that can be used to detect when the Join # requests ended up reaching different objects, so that this situation can be detected easily. # This could be a simple matter of including a sequence number -- if the joiner receives two # `JoinResult`s with sequence number 0, then they must have come from different objects and the # whole join is a failure. # ======================================================================================== # Network interface sketch # # The interfaces below are meant to be pseudo-code to illustrate how the details of a particular # vat network might be abstracted away. They are written like Cap'n Proto interfaces, but in # practice you'd probably define these interfaces manually in the target programming language. A # Cap'n Proto RPC implementation should be able to use these interfaces without knowing the # definitions of the various network-specific parameters defined above. # interface VatNetwork { # # Represents a vat network, with the ability to connect to particular vats and receive # # connections from vats. # # # # Note that methods returning a `Connection` may return a pre-existing `Connection`, and the # # caller is expected to find and share state with existing users of the connection. # # # Level 0 features ----------------------------------------------- # # connect(vatId :VatId) :Connection; # # Connect to the given vat. The transport should return a promise that does not # # resolve until authentication has completed, but allows messages to be pipelined in before # # that; the transport either queues these messages until authenticated, or sends them encrypted # # such that only the authentic vat would be able to decrypt them. The latter approach avoids a # # round trip for authentication. # # accept() :Connection; # # Wait for the next incoming connection and return it. Only connections formed by # # connect() are returned by this method. # # # Level 4 features ----------------------------------------------- # # newJoiner(count :UInt32) :NewJoinerResponse; # # Prepare a new Join operation, which will eventually lead to forming a new direct connection # # to the host of the joined capability. `count` is the number of capabilities to join. # # struct NewJoinerResponse { # joinKeyParts :List(JoinKeyPart); # # Key parts to send in Join messages to each capability. # # joiner :Joiner; # # Used to establish the final connection. # } # # interface Joiner { # addJoinResult(result :JoinResult) :Void; # # Add a JoinResult received in response to one of the `Join` messages. All `JoinResult`s # # returned from all paths must be added before trying to connect. # # connect() :ConnectionAndProvisionId; # # Try to form a connection to the joined capability's host, verifying that it has received # # all of the JoinKeyParts. Once the connection is formed, the caller should send an `Accept` # # message on it with the specified `ProvisionId` in order to receive the final capability. # } # # acceptConnectionFromJoiner(parts :List(JoinKeyPart), paths :List(VatPath)) # :ConnectionAndProvisionId; # # Called on a joined capability's host to receive the connection from the joiner, once all # # key parts have arrived. The caller should expect to receive an `Accept` message over the # # connection with the given ProvisionId. # } # # interface Connection { # # Level 0 features ----------------------------------------------- # # send(message :Message) :Void; # # Send the message. Returns successfully when the message (and all preceding messages) has # # been acknowledged by the recipient. # # receive() :Message; # # Receive the next message, and acknowledges receipt to the sender. Messages are received in # # the order in which they are sent. # # # Level 3 features ----------------------------------------------- # # introduceTo(recipient :Connection) :IntroductionInfo; # # Call before starting a three-way introduction, assuming a `Provide` message is to be sent on # # this connection and a `ThirdPartyCapId` is to be sent to `recipient`. # # struct IntroductionInfo { # sendToRecipient :ThirdPartyCapId; # sendToTarget :RecipientId; # } # # connectToIntroduced(capId :ThirdPartyCapId) :ConnectionAndProvisionId; # # Given a ThirdPartyCapId received over this connection, connect to the third party. The # # caller should then send an `Accept` message over the new connection. # # acceptIntroducedConnection(recipientId :RecipientId) :Connection; # # Given a RecipientId received in a `Provide` message on this `Connection`, wait for the # # recipient to connect, and return the connection formed. Usually, the first message received # # on the new connection will be an `Accept` message. # } # # struct ConnectionAndProvisionId { # # **(level 3)** # # connection :Connection; # # Connection on which to issue `Accept` message. # # provision :ProvisionId; # # `ProvisionId` to send in the `Accept` message. # } capnproto-c++-0.8.0/src/capnp/rpc-twoparty.capnp0000644000175000017500000002276413340402540022344 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xa184c7885cdaf2a1; # This file defines the "network-specific parameters" in rpc.capnp to support a network consisting # of two vats. Each of these vats may in fact be in communication with other vats, but any # capabilities they forward must be proxied. Thus, to each end of the connection, all capabilities # received from the other end appear to live in a single vat. # # Two notable use cases for this model include: # - Regular client-server communications, where a remote client machine (perhaps living on an end # user's personal device) connects to a server. The server may be part of a cluster, and may # call on other servers in the cluster to help service the user's request. It may even obtain # capabilities from these other servers which it passes on to the user. To simplify network # common traversal problems (e.g. if the user is behind a firewall), it is probably desirable to # multiplex all communications between the server cluster and the client over the original # connection rather than form new ones. This connection should use the two-party protocol, as # the client has no interest in knowing about additional servers. # - Applications running in a sandbox. A supervisor process may execute a confined application # such that all of the confined app's communications with the outside world must pass through # the supervisor. In this case, the connection between the confined app and the supervisor might # as well use the two-party protocol, because the confined app is intentionally prevented from # talking to any other vat anyway. Any external resources will be proxied through the supervisor, # and so to the contained app will appear as if they were hosted by the supervisor itself. # # Since there are only two vats in this network, there is never a need for three-way introductions, # so level 3 is free. Moreover, because it is never necessary to form new connections, the # two-party protocol can be used easily anywhere where a two-way byte stream exists, without regard # to where that byte stream goes or how it was initiated. This makes the two-party runtime library # highly reusable. # # Joins (level 4) _could_ be needed in cases where one or both vats are participating in other # networks that use joins. For instance, if Alice and Bob are speaking through the two-party # protocol, and Bob is also participating on another network, Bob may send Alice two or more # proxied capabilities which, unbeknownst to Bob at the time, are in fact pointing at the same # remote object. Alice may then request to join these capabilities, at which point Bob will have # to forward the join to the other network. Note, however, that if Alice is _not_ participating on # any other network, then Alice will never need to _receive_ a Join, because Alice would always # know when two locally-hosted capabilities are the same and would never export a redundant alias # to Bob. So, Alice can respond to all incoming joins with an error, and only needs to implement # outgoing joins if she herself desires to use this feature. Also, outgoing joins are relatively # easy to implement in this scenario. # # What all this means is that a level 4 implementation of the confined network is barely more # complicated than a level 2 implementation. However, such an implementation allows the "client" # or "confined" app to access the server's/supervisor's network with equal functionality to any # native participant. In other words, an application which implements only the two-party protocol # can be paired with a proxy app in order to participate in any network. # # So, when implementing Cap'n Proto in a new language, it makes sense to implement only the # two-party protocol initially, and then pair applications with an appropriate proxy written in # C++, rather than implement other parameterizations of the RPC protocol directly. using Cxx = import "/capnp/c++.capnp"; $Cxx.namespace("capnp::rpc::twoparty"); # Note: SturdyRef is not specified here. It is up to the application to define semantics of # SturdyRefs if desired. enum Side { server @0; # The object lives on the "server" or "supervisor" end of the connection. Only the # server/supervisor knows how to interpret the ref; to the client, it is opaque. # # Note that containers intending to implement strong confinement should rewrite SturdyRefs # received from the external network before passing them on to the confined app. The confined # app thus does not ever receive the raw bits of the SturdyRef (which it could perhaps # maliciously leak), but instead receives only a thing that it can pass back to the container # later to restore the ref. See: # http://www.erights.org/elib/capability/dist-confine.html client @1; # The object lives on the "client" or "confined app" end of the connection. Only the client # knows how to interpret the ref; to the server/supervisor, it is opaque. Most clients do not # actually know how to persist capabilities at all, so use of this is unusual. } struct VatId { side @0 :Side; } struct ProvisionId { # Only used for joins, since three-way introductions never happen on a two-party network. joinId @0 :UInt32; # The ID from `JoinKeyPart`. } struct RecipientId {} # Never used, because there are only two parties. struct ThirdPartyCapId {} # Never used, because there is no third party. struct JoinKeyPart { # Joins in the two-party case are simplified by a few observations. # # First, on a two-party network, a Join only ever makes sense if the receiving end is also # connected to other networks. A vat which is not connected to any other network can safely # reject all joins. # # Second, since a two-party connection bisects the network -- there can be no other connections # between the networks at either end of the connection -- if one part of a join crosses the # connection, then _all_ parts must cross it. Therefore, a vat which is receiving a Join request # off some other network which needs to be forwarded across the two-party connection can # collect all the parts on its end and only forward them across the two-party connection when all # have been received. # # For example, imagine that Alice and Bob are vats connected over a two-party connection, and # each is also connected to other networks. At some point, Alice receives one part of a Join # request off her network. The request is addressed to a capability that Alice received from # Bob and is proxying to her other network. Alice goes ahead and responds to the Join part as # if she hosted the capability locally (this is important so that if not all the Join parts end # up at Alice, the original sender can detect the failed Join without hanging). As other parts # trickle in, Alice verifies that each part is addressed to a capability from Bob and continues # to respond to each one. Once the complete set of join parts is received, Alice checks if they # were all for the exact same capability. If so, she doesn't need to send anything to Bob at # all. Otherwise, she collects the set of capabilities (from Bob) to which the join parts were # addressed and essentially initiates a _new_ Join request on those capabilities to Bob. Alice # does not forward the Join parts she received herself, but essentially forwards the Join as a # whole. # # On Bob's end, since he knows that Alice will always send all parts of a Join together, he # simply waits until he's received them all, then performs a join on the respective capabilities # as if it had been requested locally. joinId @0 :UInt32; # A number identifying this join, chosen by the sender. May be reused once `Finish` messages are # sent corresponding to all of the `Join` messages. partCount @1 :UInt16; # The number of capabilities to be joined. partNum @2 :UInt16; # Which part this request targets -- a number in the range [0, partCount). } struct JoinResult { joinId @0 :UInt32; # Matches `JoinKeyPart`. succeeded @1 :Bool; # All JoinResults in the set will have the same value for `succeeded`. The receiver actually # implements the join by waiting for all the `JoinKeyParts` and then performing its own join on # them, then going back and answering all the join requests afterwards. cap @2 :AnyPointer; # One of the JoinResults will have a non-null `cap` which is the joined capability. # # TODO(cleanup): Change `AnyPointer` to `Capability` when that is supported. } capnproto-c++-0.8.0/src/capnp/persistent.capnp0000644000175000017500000002012113340402540022052 0ustar00kentonkenton00000000000000# Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xb8630836983feed7; $import "/capnp/c++.capnp".namespace("capnp"); interface Persistent@0xc8cb212fcd9f5691(SturdyRef, Owner) { # Interface implemented by capabilities that outlive a single connection. A client may save() # the capability, producing a SturdyRef. The SturdyRef can be stored to disk, then later used to # obtain a new reference to the capability on a future connection. # # The exact format of SturdyRef depends on the "realm" in which the SturdyRef appears. A "realm" # is an abstract space in which all SturdyRefs have the same format and refer to the same set of # resources. Every vat is in exactly one realm. All capability clients within that vat must # produce SturdyRefs of the format appropriate for the realm. # # Similarly, every VatNetwork also resides in a particular realm. Usually, a vat's "realm" # corresponds to the realm of its main VatNetwork. However, a Vat can in fact communicate over # a VatNetwork in a different realm -- in this case, all SturdyRefs need to be transformed when # coming or going through said VatNetwork. The RPC system has hooks for registering # transformation callbacks for this purpose. # # Since the format of SturdyRef is realm-dependent, it is not defined here. An application should # choose an appropriate realm for itself as part of its design. Note that under Sandstorm, every # application exists in its own realm and is therefore free to define its own SturdyRef format; # the Sandstorm platform handles translating between realms. # # Note that whether a capability is persistent is often orthogonal to its type. In these cases, # the capability's interface should NOT inherit `Persistent`; instead, just perform a cast at # runtime. It's not type-safe, but trying to be type-safe in these cases will likely lead to # tears. In cases where a particular interface only makes sense on persistent capabilities, it # still should not explicitly inherit Persistent because the `SturdyRef` and `Owner` types will # vary between realms (they may even be different at the call site than they are on the # implementation). Instead, mark persistent interfaces with the $persistent annotation (defined # below). # # Sealing # ------- # # As an added security measure, SturdyRefs may be "sealed" to a particular owner, such that # if the SturdyRef itself leaks to a third party, that party cannot actually restore it because # they are not the owner. To restore a sealed capability, you must first prove to its host that # you are the rightful owner. The precise mechanism for this authentication is defined by the # realm. # # Sealing is a defense-in-depth mechanism meant to mitigate damage in the case of catastrophic # attacks. For example, say an attacker temporarily gains read access to a database full of # SturdyRefs: it would be unfortunate if it were then necessary to revoke every single reference # in the database to prevent the attacker from using them. # # In general, an "owner" is a course-grained identity. Because capability-based security is still # the primary mechanism of security, it is not necessary nor desirable to have a separate "owner" # identity for every single process or object; that is exactly what capabilities are supposed to # avoid! Instead, it makes sense for an "owner" to literally identify the owner of the machines # where the capability is stored. If untrusted third parties are able to run arbitrary code on # said machines, then the sandbox for that code should be designed using Distributed Confinement # such that the third-party code never sees the bits of the SturdyRefs and cannot directly # exercise the owner's power to restore refs. See: # # http://www.erights.org/elib/capability/dist-confine.html # # Resist the urge to represent an Owner as a simple public key. The whole point of sealing is to # defend against leaked-storage attacks. Such attacks can easily result in the owner's private # key being stolen as well. A better solution is for `Owner` to contain a simple globally unique # identifier for the owner, and for everyone to separately maintain a mapping of owner IDs to # public keys. If an owner's private key is compromised, then humans will need to communicate # and agree on a replacement public key, then update the mapping. # # As a concrete example, an `Owner` could simply contain a domain name, and restoring a SturdyRef # would require signing a request using the domain's private key. Authenticating this key could # be accomplished through certificate authorities or web-of-trust techniques. save @0 SaveParams -> SaveResults; # Save a capability persistently so that it can be restored by a future connection. Not all # capabilities can be saved -- application interfaces should define which capabilities support # this and which do not. struct SaveParams { sealFor @0 :Owner; # Seal the SturdyRef so that it can only be restored by the specified Owner. This is meant # to mitigate damage when a SturdyRef is leaked. See comments above. # # Leaving this value null may or may not be allowed; it is up to the realm to decide. If a # realm does allow a null owner, this should indicate that anyone is allowed to restore the # ref. } struct SaveResults { sturdyRef @0 :SturdyRef; } } interface RealmGateway(InternalRef, ExternalRef, InternalOwner, ExternalOwner) { # Interface invoked when a SturdyRef is about to cross realms. The RPC system supports providing # a RealmGateway as a callback hook when setting up RPC over some VatNetwork. import @0 (cap :Persistent(ExternalRef, ExternalOwner), params :Persistent(InternalRef, InternalOwner).SaveParams) -> Persistent(InternalRef, InternalOwner).SaveResults; # Given an external capability, save it and return an internal reference. Used when someone # inside the realm tries to save a capability from outside the realm. export @1 (cap :Persistent(InternalRef, InternalOwner), params :Persistent(ExternalRef, ExternalOwner).SaveParams) -> Persistent(ExternalRef, ExternalOwner).SaveResults; # Given an internal capability, save it and return an external reference. Used when someone # outside the realm tries to save a capability from inside the realm. } annotation persistent(interface, field) :Void; # Apply this annotation to interfaces for objects that will always be persistent, instead of # extending the Persistent capability, since the correct type parameters to Persistent depend on # the realm, which is orthogonal to the interface type and therefore should not be defined # along-side it. # # You may also apply this annotation to a capability-typed field which will always contain a # persistent capability, but where the capability's interface itself is not already marked # persistent. # # Note that absence of the $persistent annotation doesn't mean a capability of that type isn't # persistent; it just means not *all* such capabilities are persistent. capnproto-c++-0.8.0/src/capnp/c++.capnp.h0000644000175000017500000000137613650317744020502 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: c++.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(b9c6f99ebf805f2c); CAPNP_DECLARE_SCHEMA(f264a779fef191ce); } // namespace schemas } // namespace capnp namespace capnp { namespace annotations { // ======================================================================================= // ======================================================================================= } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/common.h0000644000175000017500000006624313650317744020326 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains types which are intended to help detect incorrect usage at compile // time, but should then be optimized down to basic primitives (usually, integers) by the // compiler. #pragma once #include #include #include #include // work-around macro conflict with `VOID` #if CAPNP_DEBUG_TYPES #include #endif #if !defined(CAPNP_HEADER_WARNINGS) || !CAPNP_HEADER_WARNINGS #define CAPNP_BEGIN_HEADER KJ_BEGIN_SYSTEM_HEADER #define CAPNP_END_HEADER KJ_END_SYSTEM_HEADER #else #define CAPNP_BEGIN_HEADER #define CAPNP_END_HEADER #endif CAPNP_BEGIN_HEADER namespace capnp { #define CAPNP_VERSION_MAJOR 0 #define CAPNP_VERSION_MINOR 8 #define CAPNP_VERSION_MICRO 0 #define CAPNP_VERSION \ (CAPNP_VERSION_MAJOR * 1000000 + CAPNP_VERSION_MINOR * 1000 + CAPNP_VERSION_MICRO) #ifndef CAPNP_LITE #define CAPNP_LITE 0 #endif #if CAPNP_TESTING_CAPNP // defined in Cap'n Proto's own unit tests; others should not define this #define CAPNP_DEPRECATED(reason) #else #define CAPNP_DEPRECATED KJ_DEPRECATED #endif typedef unsigned int uint; struct Void { // Type used for Void fields. Using C++'s "void" type creates a bunch of issues since it behaves // differently from other types. inline constexpr bool operator==(Void other) const { return true; } inline constexpr bool operator!=(Void other) const { return false; } }; static constexpr Void VOID = Void(); // Constant value for `Void`, which is an empty struct. inline kj::StringPtr KJ_STRINGIFY(Void) { return "void"; } struct Text; struct Data; enum class Kind: uint8_t { PRIMITIVE, BLOB, ENUM, STRUCT, UNION, INTERFACE, LIST, OTHER // Some other type which is often a type parameter to Cap'n Proto templates, but which needs // special handling. This includes types like AnyPointer, Dynamic*, etc. }; enum class Style: uint8_t { PRIMITIVE, POINTER, // other than struct STRUCT, CAPABILITY }; enum class ElementSize: uint8_t { // Size of a list element. VOID = 0, BIT = 1, BYTE = 2, TWO_BYTES = 3, FOUR_BYTES = 4, EIGHT_BYTES = 5, POINTER = 6, INLINE_COMPOSITE = 7 }; enum class PointerType { // Various wire types a pointer field can take NULL_, // Should be NULL, but that's #defined in stddef.h STRUCT, LIST, CAPABILITY }; namespace schemas { template struct EnumInfo; } // namespace schemas namespace _ { // private template struct Kind_; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::PRIMITIVE; }; template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; template <> struct Kind_ { static constexpr Kind kind = Kind::BLOB; }; template struct Kind_> { static constexpr Kind kind = Kind::STRUCT; }; template struct Kind_> { static constexpr Kind kind = Kind::INTERFACE; }; template struct Kind_::IsEnum>> { static constexpr Kind kind = Kind::ENUM; }; } // namespace _ (private) template ::kind> inline constexpr Kind kind() { // This overload of kind() matches types which have a Kind_ specialization. return k; } #if _MSC_VER #define CAPNP_KIND(T) ::capnp::_::Kind_::kind // Avoid constexpr methods in MSVC (it remains buggy in many situations). #else // _MSC_VER #define CAPNP_KIND(T) ::capnp::kind() // Use this macro rather than kind() in any code which must work in MSVC. #endif // _MSC_VER, else #if !CAPNP_LITE template ()> inline constexpr Style style() { return k == Kind::PRIMITIVE || k == Kind::ENUM ? Style::PRIMITIVE : k == Kind::STRUCT ? Style::STRUCT : k == Kind::INTERFACE ? Style::CAPABILITY : Style::POINTER; } #endif // !CAPNP_LITE template struct List; #if _MSC_VER template struct List {}; // For some reason, without this declaration, MSVC will error out on some uses of List // claiming that "T" -- as used in the default initializer for the second template param, "k" -- // is not defined. I do not understand this error, but adding this empty default declaration fixes // it. #endif template struct ListElementType_; template struct ListElementType_> { typedef T Type; }; template using ListElementType = typename ListElementType_::Type; namespace _ { // private template struct Kind_> { static constexpr Kind kind = Kind::LIST; }; } // namespace _ (private) template struct ReaderFor_ { typedef typename T::Reader Type; }; template struct ReaderFor_ { typedef T Type; }; template struct ReaderFor_ { typedef T Type; }; template struct ReaderFor_ { typedef typename T::Client Type; }; template using ReaderFor = typename ReaderFor_::Type; // The type returned by List::Reader::operator[]. template struct BuilderFor_ { typedef typename T::Builder Type; }; template struct BuilderFor_ { typedef T Type; }; template struct BuilderFor_ { typedef T Type; }; template struct BuilderFor_ { typedef typename T::Client Type; }; template using BuilderFor = typename BuilderFor_::Type; // The type returned by List::Builder::operator[]. template struct PipelineFor_ { typedef typename T::Pipeline Type;}; template struct PipelineFor_ { typedef typename T::Client Type; }; template using PipelineFor = typename PipelineFor_::Type; template struct TypeIfEnum_; template struct TypeIfEnum_ { typedef T Type; }; template using TypeIfEnum = typename TypeIfEnum_>::Type; template using FromReader = typename kj::Decay::Reads; // FromReader = MyType (for any Cap'n Proto type). template using FromBuilder = typename kj::Decay::Builds; // FromBuilder = MyType (for any Cap'n Proto type). template using FromPipeline = typename kj::Decay::Pipelines; // FromBuilder = MyType (for any Cap'n Proto type). template using FromClient = typename kj::Decay::Calls; // FromReader = MyType (for any Cap'n Proto interface type). template using FromServer = typename kj::Decay::Serves; // FromBuilder = MyType (for any Cap'n Proto interface type). template struct FromAny_; template struct FromAny_>> { using Type = FromReader; }; template struct FromAny_>> { using Type = FromBuilder; }; template struct FromAny_>> { using Type = FromPipeline; }; // Note that T::Client is covered by FromReader template struct FromAny_, kj::VoidSfinae>> { using Type = FromServer; }; template struct FromAny_::kind == Kind::PRIMITIVE || _::Kind_::kind == Kind::ENUM>> { // TODO(msvc): Ideally the EnableIf condition would be `style() == Style::PRIMITIVE`, but MSVC // cannot yet use style() in this constexpr context. using Type = kj::Decay; }; template using FromAny = typename FromAny_::Type; // Given any Cap'n Proto value type as an input, return the Cap'n Proto base type. That is: // // Foo::Reader -> Foo // Foo::Builder -> Foo // Foo::Pipeline -> Foo // Foo::Client -> Foo // Own -> Foo // uint32_t -> uint32_t namespace _ { // private template struct PointerHelpers; #if _MSC_VER template struct PointerHelpers {}; // For some reason, without this declaration, MSVC will error out on some uses of PointerHelpers // claiming that "T" -- as used in the default initializer for the second template param, "k" -- // is not defined. I do not understand this error, but adding this empty default declaration fixes // it. #endif } // namespace _ (private) struct MessageSize { // Size of a message. Every struct and list type has a method `.totalSize()` that returns this. uint64_t wordCount; uint capCount; inline constexpr MessageSize operator+(const MessageSize& other) const { return { wordCount + other.wordCount, capCount + other.capCount }; } }; // ======================================================================================= // Raw memory types and measures using kj::byte; class word { // word is an opaque type with size of 64 bits. This type is useful only to make pointer // arithmetic clearer. Since the contents are private, the only way to access them is to first // reinterpret_cast to some other pointer type. // // Copying is disallowed because you should always use memcpy(). Otherwise, you may run afoul of // aliasing rules. // // A pointer of type word* should always be word-aligned even if won't actually be dereferenced // as that type. public: word() = default; private: uint64_t content KJ_UNUSED_MEMBER; #if __GNUC__ < 8 || __clang__ // GCC 8's -Wclass-memaccess complains whenever we try to memcpy() a `word` if we've disallowed // the copy constructor. We don't want to disable the warning because it's a useful warning and // we'd have to disable it for all applications that include this header. Instead we allow `word` // to be copyable on GCC. KJ_DISALLOW_COPY(word); #endif }; static_assert(sizeof(byte) == 1, "uint8_t is not one byte?"); static_assert(sizeof(word) == 8, "uint64_t is not 8 bytes?"); #if CAPNP_DEBUG_TYPES // Set CAPNP_DEBUG_TYPES to 1 to use kj::Quantity for "count" types. Otherwise, plain integers are // used. All the code should still operate exactly the same, we just lose compile-time checking. // Note that this will also change symbol names, so it's important that the library and any clients // be compiled with the same setting here. // // We disable this by default to reduce symbol name size and avoid any possibility of the compiler // failing to fully-optimize the types, but anyone modifying Cap'n Proto itself should enable this // during development and testing. namespace _ { class BitLabel; class ElementLabel; struct WirePointer; } template using BitCountN = kj::Quantity(), T>, _::BitLabel>; template using ByteCountN = kj::Quantity(), T>, byte>; template using WordCountN = kj::Quantity(), T>, word>; template using ElementCountN = kj::Quantity(), T>, _::ElementLabel>; template using WirePointerCountN = kj::Quantity(), T>, _::WirePointer>; typedef BitCountN<8, uint8_t> BitCount8; typedef BitCountN<16, uint16_t> BitCount16; typedef BitCountN<32, uint32_t> BitCount32; typedef BitCountN<64, uint64_t> BitCount64; typedef BitCountN BitCount; typedef ByteCountN<8, uint8_t> ByteCount8; typedef ByteCountN<16, uint16_t> ByteCount16; typedef ByteCountN<32, uint32_t> ByteCount32; typedef ByteCountN<64, uint64_t> ByteCount64; typedef ByteCountN ByteCount; typedef WordCountN<8, uint8_t> WordCount8; typedef WordCountN<16, uint16_t> WordCount16; typedef WordCountN<32, uint32_t> WordCount32; typedef WordCountN<64, uint64_t> WordCount64; typedef WordCountN WordCount; typedef ElementCountN<8, uint8_t> ElementCount8; typedef ElementCountN<16, uint16_t> ElementCount16; typedef ElementCountN<32, uint32_t> ElementCount32; typedef ElementCountN<64, uint64_t> ElementCount64; typedef ElementCountN ElementCount; typedef WirePointerCountN<8, uint8_t> WirePointerCount8; typedef WirePointerCountN<16, uint16_t> WirePointerCount16; typedef WirePointerCountN<32, uint32_t> WirePointerCount32; typedef WirePointerCountN<64, uint64_t> WirePointerCount64; typedef WirePointerCountN WirePointerCount; template using BitsPerElementN = decltype(BitCountN() / ElementCountN()); template using BytesPerElementN = decltype(ByteCountN() / ElementCountN()); template using WordsPerElementN = decltype(WordCountN() / ElementCountN()); template using PointersPerElementN = decltype(WirePointerCountN() / ElementCountN()); using kj::bounded; using kj::unbound; using kj::unboundAs; using kj::unboundMax; using kj::unboundMaxBits; using kj::assertMax; using kj::assertMaxBits; using kj::upgradeBound; using kj::ThrowOverflow; using kj::assumeBits; using kj::assumeMax; using kj::subtractChecked; using kj::trySubtract; template inline constexpr U* operator+(U* ptr, kj::Quantity offset) { return ptr + unbound(offset / kj::unit>()); } template inline constexpr const U* operator+(const U* ptr, kj::Quantity offset) { return ptr + unbound(offset / kj::unit>()); } template inline constexpr U* operator+=(U*& ptr, kj::Quantity offset) { return ptr = ptr + unbound(offset / kj::unit>()); } template inline constexpr const U* operator+=(const U*& ptr, kj::Quantity offset) { return ptr = ptr + unbound(offset / kj::unit>()); } template inline constexpr U* operator-(U* ptr, kj::Quantity offset) { return ptr - unbound(offset / kj::unit>()); } template inline constexpr const U* operator-(const U* ptr, kj::Quantity offset) { return ptr - unbound(offset / kj::unit>()); } template inline constexpr U* operator-=(U*& ptr, kj::Quantity offset) { return ptr = ptr - unbound(offset / kj::unit>()); } template inline constexpr const U* operator-=(const U*& ptr, kj::Quantity offset) { return ptr = ptr - unbound(offset / kj::unit>()); } constexpr auto BITS = kj::unit>(); constexpr auto BYTES = kj::unit>(); constexpr auto WORDS = kj::unit>(); constexpr auto ELEMENTS = kj::unit>(); constexpr auto POINTERS = kj::unit>(); constexpr auto ZERO = kj::bounded<0>(); constexpr auto ONE = kj::bounded<1>(); // GCC 4.7 actually gives unused warnings on these constants in opt mode... constexpr auto BITS_PER_BYTE KJ_UNUSED = bounded<8>() * BITS / BYTES; constexpr auto BITS_PER_WORD KJ_UNUSED = bounded<64>() * BITS / WORDS; constexpr auto BYTES_PER_WORD KJ_UNUSED = bounded<8>() * BYTES / WORDS; constexpr auto BITS_PER_POINTER KJ_UNUSED = bounded<64>() * BITS / POINTERS; constexpr auto BYTES_PER_POINTER KJ_UNUSED = bounded<8>() * BYTES / POINTERS; constexpr auto WORDS_PER_POINTER KJ_UNUSED = ONE * WORDS / POINTERS; constexpr auto POINTER_SIZE_IN_WORDS = ONE * POINTERS * WORDS_PER_POINTER; constexpr uint SEGMENT_WORD_COUNT_BITS = 29; // Number of words in a segment. constexpr uint LIST_ELEMENT_COUNT_BITS = 29; // Number of elements in a list. constexpr uint STRUCT_DATA_WORD_COUNT_BITS = 16; // Number of words in a Struct data section. constexpr uint STRUCT_POINTER_COUNT_BITS = 16; // Number of pointers in a Struct pointer section. constexpr uint BLOB_SIZE_BITS = 29; // Number of bytes in a blob. typedef WordCountN SegmentWordCount; typedef ElementCountN ListElementCount; typedef WordCountN StructDataWordCount; typedef WirePointerCountN StructPointerCount; typedef ByteCountN BlobSize; constexpr auto MAX_SEGMENT_WORDS = bounded()>() * WORDS; constexpr auto MAX_LIST_ELEMENTS = bounded()>() * ELEMENTS; constexpr auto MAX_STUCT_DATA_WORDS = bounded()>() * WORDS; constexpr auto MAX_STRUCT_POINTER_COUNT = bounded()>() * POINTERS; using StructDataBitCount = decltype(WordCountN() * BITS_PER_WORD); // Number of bits in a Struct data segment (should come out to BitCountN<22>). using StructDataOffset = decltype(StructDataBitCount() * (ONE * ELEMENTS / BITS)); using StructPointerOffset = StructPointerCount; // Type of a field offset. inline StructDataOffset assumeDataOffset(uint32_t offset) { return assumeMax(MAX_STUCT_DATA_WORDS * BITS_PER_WORD * (ONE * ELEMENTS / BITS), bounded(offset) * ELEMENTS); } inline StructPointerOffset assumePointerOffset(uint32_t offset) { return assumeMax(MAX_STRUCT_POINTER_COUNT, bounded(offset) * POINTERS); } constexpr uint MAX_TEXT_SIZE = kj::maxValueForBits() - 1; typedef kj::Quantity, byte> TextSize; // Not including NUL terminator. template inline KJ_CONSTEXPR() decltype(bounded() * BYTES / ELEMENTS) bytesPerElement() { return bounded() * BYTES / ELEMENTS; } template inline KJ_CONSTEXPR() decltype(bounded() * BITS / ELEMENTS) bitsPerElement() { return bounded() * BITS / ELEMENTS; } template inline constexpr kj::Quantity, T> intervalLength(const T* a, const T* b, kj::Quantity, T>) { return kj::assumeMax(b - a) * kj::unit, T>>(); } template inline constexpr kj::ArrayPtr arrayPtr(const U* ptr, kj::Quantity size) { return kj::ArrayPtr(ptr, unbound(size / kj::unit>())); } template inline constexpr kj::ArrayPtr arrayPtr(U* ptr, kj::Quantity size) { return kj::ArrayPtr(ptr, unbound(size / kj::unit>())); } #else template using BitCountN = T; template using ByteCountN = T; template using WordCountN = T; template using ElementCountN = T; template using WirePointerCountN = T; // XXX typedef BitCountN<8, uint8_t> BitCount8; typedef BitCountN<16, uint16_t> BitCount16; typedef BitCountN<32, uint32_t> BitCount32; typedef BitCountN<64, uint64_t> BitCount64; typedef BitCountN BitCount; typedef ByteCountN<8, uint8_t> ByteCount8; typedef ByteCountN<16, uint16_t> ByteCount16; typedef ByteCountN<32, uint32_t> ByteCount32; typedef ByteCountN<64, uint64_t> ByteCount64; typedef ByteCountN ByteCount; typedef WordCountN<8, uint8_t> WordCount8; typedef WordCountN<16, uint16_t> WordCount16; typedef WordCountN<32, uint32_t> WordCount32; typedef WordCountN<64, uint64_t> WordCount64; typedef WordCountN WordCount; typedef ElementCountN<8, uint8_t> ElementCount8; typedef ElementCountN<16, uint16_t> ElementCount16; typedef ElementCountN<32, uint32_t> ElementCount32; typedef ElementCountN<64, uint64_t> ElementCount64; typedef ElementCountN ElementCount; typedef WirePointerCountN<8, uint8_t> WirePointerCount8; typedef WirePointerCountN<16, uint16_t> WirePointerCount16; typedef WirePointerCountN<32, uint32_t> WirePointerCount32; typedef WirePointerCountN<64, uint64_t> WirePointerCount64; typedef WirePointerCountN WirePointerCount; template using BitsPerElementN = decltype(BitCountN() / ElementCountN()); template using BytesPerElementN = decltype(ByteCountN() / ElementCountN()); template using WordsPerElementN = decltype(WordCountN() / ElementCountN()); template using PointersPerElementN = decltype(WirePointerCountN() / ElementCountN()); using kj::ThrowOverflow; // YYY template inline constexpr uint bounded() { return i; } template inline constexpr T bounded(T i) { return i; } template inline constexpr T unbound(T i) { return i; } template inline constexpr T unboundAs(U i) { return i; } template inline constexpr uint unboundMax(T i) { return i; } template inline constexpr uint unboundMaxBits(T i) { return i; } template inline T assertMax(T value, ErrorFunc&& func) { if (KJ_UNLIKELY(value > newMax)) func(); return value; } template inline T assertMax(uint newMax, T value, ErrorFunc&& func) { if (KJ_UNLIKELY(value > newMax)) func(); return value; } template inline T assertMaxBits(T value, ErrorFunc&& func = ErrorFunc()) { if (KJ_UNLIKELY(value > kj::maxValueForBits())) func(); return value; } template inline T assertMaxBits(uint bits, T value, ErrorFunc&& func = ErrorFunc()) { if (KJ_UNLIKELY(value > (1ull << bits) - 1)) func(); return value; } template inline constexpr T upgradeBound(U i) { return i; } template inline constexpr T assumeBits(T i) { return i; } template inline constexpr T assumeMax(T i) { return i; } template inline auto subtractChecked(T a, U b, ErrorFunc&& errorFunc = ErrorFunc()) -> decltype(a - b) { if (b > a) errorFunc(); return a - b; } template inline auto trySubtract(T a, U b) -> kj::Maybe { if (b > a) { return nullptr; } else { return a - b; } } constexpr uint BITS = 1; constexpr uint BYTES = 1; constexpr uint WORDS = 1; constexpr uint ELEMENTS = 1; constexpr uint POINTERS = 1; constexpr uint ZERO = 0; constexpr uint ONE = 1; // GCC 4.7 actually gives unused warnings on these constants in opt mode... constexpr uint BITS_PER_BYTE KJ_UNUSED = 8; constexpr uint BITS_PER_WORD KJ_UNUSED = 64; constexpr uint BYTES_PER_WORD KJ_UNUSED = 8; constexpr uint BITS_PER_POINTER KJ_UNUSED = 64; constexpr uint BYTES_PER_POINTER KJ_UNUSED = 8; constexpr uint WORDS_PER_POINTER KJ_UNUSED = 1; // XXX constexpr uint POINTER_SIZE_IN_WORDS = ONE * POINTERS * WORDS_PER_POINTER; constexpr uint SEGMENT_WORD_COUNT_BITS = 29; // Number of words in a segment. constexpr uint LIST_ELEMENT_COUNT_BITS = 29; // Number of elements in a list. constexpr uint STRUCT_DATA_WORD_COUNT_BITS = 16; // Number of words in a Struct data section. constexpr uint STRUCT_POINTER_COUNT_BITS = 16; // Number of pointers in a Struct pointer section. constexpr uint BLOB_SIZE_BITS = 29; // Number of bytes in a blob. typedef WordCountN SegmentWordCount; typedef ElementCountN ListElementCount; typedef WordCountN StructDataWordCount; typedef WirePointerCountN StructPointerCount; typedef ByteCountN BlobSize; // YYY constexpr auto MAX_SEGMENT_WORDS = kj::maxValueForBits(); constexpr auto MAX_LIST_ELEMENTS = kj::maxValueForBits(); constexpr auto MAX_STUCT_DATA_WORDS = kj::maxValueForBits(); constexpr auto MAX_STRUCT_POINTER_COUNT = kj::maxValueForBits(); typedef uint StructDataBitCount; typedef uint StructDataOffset; typedef uint StructPointerOffset; inline StructDataOffset assumeDataOffset(uint32_t offset) { return offset; } inline StructPointerOffset assumePointerOffset(uint32_t offset) { return offset; } constexpr uint MAX_TEXT_SIZE = kj::maxValueForBits() - 1; typedef uint TextSize; template inline KJ_CONSTEXPR() size_t bytesPerElement() { return sizeof(T); } template inline KJ_CONSTEXPR() size_t bitsPerElement() { return sizeof(T) * 8; } template inline constexpr ptrdiff_t intervalLength(const T* a, const T* b, uint) { return b - a; } template inline constexpr kj::ArrayPtr arrayPtr(const U* ptr, T size) { return kj::arrayPtr(ptr, size); } template inline constexpr kj::ArrayPtr arrayPtr(U* ptr, T size) { return kj::arrayPtr(ptr, size); } #endif } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/blob.h0000644000175000017500000002040613650101756017735 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include "common.h" #include CAPNP_BEGIN_HEADER namespace capnp { struct Data { Data() = delete; class Reader; class Builder; class Pipeline {}; }; struct Text { Text() = delete; class Reader; class Builder; class Pipeline {}; }; class Data::Reader: public kj::ArrayPtr { // Points to a blob of bytes. The usual Reader rules apply -- Data::Reader behaves like a simple // pointer which does not own its target, can be passed by value, etc. public: typedef Data Reads; Reader() = default; inline Reader(decltype(nullptr)): ArrayPtr(nullptr) {} inline Reader(const byte* value, size_t size): ArrayPtr(value, size) {} inline Reader(const kj::Array& value): ArrayPtr(value) {} inline Reader(const ArrayPtr& value): ArrayPtr(value) {} inline Reader(const kj::Array& value): ArrayPtr(value) {} inline Reader(const ArrayPtr& value): ArrayPtr(value) {} }; class Text::Reader: public kj::StringPtr { // Like Data::Reader, but points at NUL-terminated UTF-8 text. The NUL terminator is not counted // in the size but must be present immediately after the last byte. // // Text::Reader's interface contract is that its data MUST be NUL-terminated. The producer of // the Text::Reader must guarantee this, so that the consumer need not check. The data SHOULD // also be valid UTF-8, but this is NOT guaranteed -- the consumer must verify if it cares. public: typedef Text Reads; Reader() = default; inline Reader(decltype(nullptr)): StringPtr(nullptr) {} inline Reader(const char* value): StringPtr(value) {} inline Reader(const char* value, size_t size): StringPtr(value, size) {} inline Reader(const kj::String& value): StringPtr(value) {} inline Reader(const StringPtr& value): StringPtr(value) {} #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP template ().c_str())> inline Reader(const T& t): StringPtr(t) {} // Allow implicit conversion from any class that has a c_str() method (namely, std::string). // We use a template trick to detect std::string in order to avoid including the header for // those who don't want it. #endif }; class Data::Builder: public kj::ArrayPtr { // Like Data::Reader except the pointers aren't const. public: typedef Data Builds; Builder() = default; inline Builder(decltype(nullptr)): ArrayPtr(nullptr) {} inline Builder(byte* value, size_t size): ArrayPtr(value, size) {} inline Builder(kj::Array& value): ArrayPtr(value) {} inline Builder(ArrayPtr value): ArrayPtr(value) {} inline Data::Reader asReader() const { return Data::Reader(kj::implicitCast&>(*this)); } inline operator Reader() const { return asReader(); } }; class Text::Builder: public kj::DisallowConstCopy { // Basically identical to kj::StringPtr, except that the contents are non-const. public: inline Builder(): content(nulstr, 1) {} inline Builder(decltype(nullptr)): content(nulstr, 1) {} inline Builder(char* value): content(value, strlen(value) + 1) {} inline Builder(char* value, size_t size): content(value, size + 1) { KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated."); } inline Reader asReader() const { return Reader(content.begin(), content.size() - 1); } inline operator Reader() const { return asReader(); } inline operator kj::ArrayPtr(); inline kj::ArrayPtr asArray(); inline operator kj::ArrayPtr() const; inline kj::ArrayPtr asArray() const; inline kj::ArrayPtr asBytes() { return asArray().asBytes(); } inline kj::ArrayPtr asBytes() const { return asArray().asBytes(); } // Result does not include NUL terminator. inline operator kj::StringPtr() const; inline kj::StringPtr asString() const; inline const char* cStr() const { return content.begin(); } // Returns NUL-terminated string. inline size_t size() const { return content.size() - 1; } // Result does not include NUL terminator. inline char operator[](size_t index) const { return content[index]; } inline char& operator[](size_t index) { return content[index]; } inline char* begin() { return content.begin(); } inline char* end() { return content.end() - 1; } inline const char* begin() const { return content.begin(); } inline const char* end() const { return content.end() - 1; } inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } inline bool operator==(Builder other) const { return asString() == other.asString(); } inline bool operator!=(Builder other) const { return asString() != other.asString(); } inline bool operator< (Builder other) const { return asString() < other.asString(); } inline bool operator> (Builder other) const { return asString() > other.asString(); } inline bool operator<=(Builder other) const { return asString() <= other.asString(); } inline bool operator>=(Builder other) const { return asString() >= other.asString(); } inline kj::StringPtr slice(size_t start) const; inline kj::ArrayPtr slice(size_t start, size_t end) const; inline Builder slice(size_t start); inline kj::ArrayPtr slice(size_t start, size_t end); // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter // version that assumes end = size(). private: inline explicit Builder(kj::ArrayPtr content): content(content) {} kj::ArrayPtr content; static char nulstr[1]; }; inline kj::StringPtr KJ_STRINGIFY(Text::Builder builder) { return builder.asString(); } inline bool operator==(const char* a, const Text::Builder& b) { return a == b.asString(); } inline bool operator!=(const char* a, const Text::Builder& b) { return a != b.asString(); } inline Text::Builder::operator kj::StringPtr() const { return kj::StringPtr(content.begin(), content.size() - 1); } inline kj::StringPtr Text::Builder::asString() const { return kj::StringPtr(content.begin(), content.size() - 1); } inline Text::Builder::operator kj::ArrayPtr() { return content.slice(0, content.size() - 1); } inline kj::ArrayPtr Text::Builder::asArray() { return content.slice(0, content.size() - 1); } inline Text::Builder::operator kj::ArrayPtr() const { return content.slice(0, content.size() - 1); } inline kj::ArrayPtr Text::Builder::asArray() const { return content.slice(0, content.size() - 1); } inline kj::StringPtr Text::Builder::slice(size_t start) const { return asReader().slice(start); } inline kj::ArrayPtr Text::Builder::slice(size_t start, size_t end) const { return content.slice(start, end); } inline Text::Builder Text::Builder::slice(size_t start) { return Text::Builder(content.slice(start, content.size())); } inline kj::ArrayPtr Text::Builder::slice(size_t start, size_t end) { return content.slice(start, end); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/endian.h0000644000175000017500000002207613650101756020262 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "common.h" #include #include // memcpy CAPNP_BEGIN_HEADER namespace capnp { namespace _ { // private // WireValue // // Wraps a primitive value as it appears on the wire. Namely, values are little-endian on the // wire, because little-endian is the most common endianness in modern CPUs. // // Note: In general, code that depends cares about byte ordering is bad. See: // http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html // Cap'n Proto is special because it is essentially doing compiler-like things, fussing over // allocation and layout of memory, in order to squeeze out every last drop of performance. #if _MSC_VER // Assume Windows is little-endian. // // TODO(msvc): This is ugly. Maybe refactor later checks to be based on CAPNP_BYTE_ORDER or // CAPNP_SWAP_BYTES or something, and define that in turn based on _MSC_VER or the GCC // intrinsics. #ifndef __ORDER_BIG_ENDIAN__ #define __ORDER_BIG_ENDIAN__ 4321 #endif #ifndef __ORDER_LITTLE_ENDIAN__ #define __ORDER_LITTLE_ENDIAN__ 1234 #endif #ifndef __BYTE_ORDER__ #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ #endif #endif #if CAPNP_REVERSE_ENDIAN #define CAPNP_WIRE_BYTE_ORDER __ORDER_BIG_ENDIAN__ #define CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER __ORDER_LITTLE_ENDIAN__ #else #define CAPNP_WIRE_BYTE_ORDER __ORDER_LITTLE_ENDIAN__ #define CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER __ORDER_BIG_ENDIAN__ #endif #if defined(__BYTE_ORDER__) && \ __BYTE_ORDER__ == CAPNP_WIRE_BYTE_ORDER && \ !CAPNP_DISABLE_ENDIAN_DETECTION // CPU is little-endian. We can just read/write the memory directly. template class DirectWireValue { public: KJ_ALWAYS_INLINE(T get() const) { return value; } KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; } private: T value; }; template using WireValue = DirectWireValue; // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are // linked together, we define each implementation with a different name and define an alias to the // one we want to use. #elif defined(__BYTE_ORDER__) && \ __BYTE_ORDER__ == CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER && \ defined(__GNUC__) && !CAPNP_DISABLE_ENDIAN_DETECTION // Big-endian, but GCC's __builtin_bswap() is available. // TODO(perf): Use dedicated instructions to read little-endian data on big-endian CPUs that have // them. // TODO(perf): Verify that this code optimizes reasonably. In particular, ensure that the // compiler optimizes away the memcpy()s and keeps everything in registers. template class SwappingWireValue; template class SwappingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { return value; } KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; } private: T value; }; template class SwappingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { // Not all platforms have __builtin_bswap16() for some reason. In particular, it is missing // on gcc-4.7.3-cygwin32 (but present on gcc-4.8.1-cygwin64). uint16_t swapped = (value << 8) | (value >> 8); T result; memcpy(&result, &swapped, sizeof(T)); return result; } KJ_ALWAYS_INLINE(void set(T newValue)) { uint16_t raw; memcpy(&raw, &newValue, sizeof(T)); // Not all platforms have __builtin_bswap16() for some reason. In particular, it is missing // on gcc-4.7.3-cygwin32 (but present on gcc-4.8.1-cygwin64). value = (raw << 8) | (raw >> 8); } private: uint16_t value; }; template class SwappingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { uint32_t swapped = __builtin_bswap32(value); T result; memcpy(&result, &swapped, sizeof(T)); return result; } KJ_ALWAYS_INLINE(void set(T newValue)) { uint32_t raw; memcpy(&raw, &newValue, sizeof(T)); value = __builtin_bswap32(raw); } private: uint32_t value; }; template class SwappingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { uint64_t swapped = __builtin_bswap64(value); T result; memcpy(&result, &swapped, sizeof(T)); return result; } KJ_ALWAYS_INLINE(void set(T newValue)) { uint64_t raw; memcpy(&raw, &newValue, sizeof(T)); value = __builtin_bswap64(raw); } private: uint64_t value; }; template using WireValue = SwappingWireValue; // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are // linked together, we define each implementation with a different name and define an alias to the // one we want to use. #else // Unknown endianness. Fall back to bit shifts. #if !CAPNP_DISABLE_ENDIAN_DETECTION #if _MSC_VER #pragma message("Couldn't detect endianness of your platform. Using unoptimized fallback implementation.") #pragma message("Consider changing this code to detect your platform and send us a patch!") #else #warning "Couldn't detect endianness of your platform. Using unoptimized fallback implementation." #warning "Consider changing this code to detect your platform and send us a patch!" #endif #endif // !CAPNP_DISABLE_ENDIAN_DETECTION template class ShiftingWireValue; template class ShiftingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { return value; } KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; } private: T value; }; template class ShiftingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { uint16_t raw = (static_cast(bytes[0]) ) | (static_cast(bytes[1]) << 8); T result; memcpy(&result, &raw, sizeof(T)); return result; } KJ_ALWAYS_INLINE(void set(T newValue)) { uint16_t raw; memcpy(&raw, &newValue, sizeof(T)); bytes[0] = raw; bytes[1] = raw >> 8; } private: union { byte bytes[2]; uint16_t align; }; }; template class ShiftingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { uint32_t raw = (static_cast(bytes[0]) ) | (static_cast(bytes[1]) << 8) | (static_cast(bytes[2]) << 16) | (static_cast(bytes[3]) << 24); T result; memcpy(&result, &raw, sizeof(T)); return result; } KJ_ALWAYS_INLINE(void set(T newValue)) { uint32_t raw; memcpy(&raw, &newValue, sizeof(T)); bytes[0] = raw; bytes[1] = raw >> 8; bytes[2] = raw >> 16; bytes[3] = raw >> 24; } private: union { byte bytes[4]; uint32_t align; }; }; template class ShiftingWireValue { public: KJ_ALWAYS_INLINE(T get() const) { uint64_t raw = (static_cast(bytes[0]) ) | (static_cast(bytes[1]) << 8) | (static_cast(bytes[2]) << 16) | (static_cast(bytes[3]) << 24) | (static_cast(bytes[4]) << 32) | (static_cast(bytes[5]) << 40) | (static_cast(bytes[6]) << 48) | (static_cast(bytes[7]) << 56); T result; memcpy(&result, &raw, sizeof(T)); return result; } KJ_ALWAYS_INLINE(void set(T newValue)) { uint64_t raw; memcpy(&raw, &newValue, sizeof(T)); bytes[0] = raw; bytes[1] = raw >> 8; bytes[2] = raw >> 16; bytes[3] = raw >> 24; bytes[4] = raw >> 32; bytes[5] = raw >> 40; bytes[6] = raw >> 48; bytes[7] = raw >> 56; } private: union { byte bytes[8]; uint64_t align; }; }; template using WireValue = ShiftingWireValue; // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are // linked together, we define each implementation with a different name and define an alias to the // one we want to use. #endif } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/layout.h0000644000175000017500000014522113650101756020337 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file is NOT intended for use by clients, except in generated code. // // This file defines low-level, non-type-safe classes for traversing the Cap'n Proto memory layout // (which is also its wire format). Code generated by the Cap'n Proto compiler uses these classes, // as does other parts of the Cap'n proto library which provide a higher-level interface for // dynamic introspection. #pragma once #include #include #include "common.h" #include "blob.h" #include "endian.h" #include // work-around macro conflict with `VOID` CAPNP_BEGIN_HEADER #if (defined(__mips__) || defined(__hppa__)) && !defined(CAPNP_CANONICALIZE_NAN) #define CAPNP_CANONICALIZE_NAN 1 // Explicitly detect NaNs and canonicalize them to the quiet NaN value as would be returned by // __builtin_nan("") on systems implementing the IEEE-754 recommended (but not required) NaN // signalling/quiet differentiation (such as x86). Unfortunately, some architectures -- in // particular, MIPS -- represent quiet vs. signalling nans differently than the rest of the world. // Canonicalizing them makes output consistent (which is important!), but hurts performance // slightly. // // Note that trying to convert MIPS NaNs to standard NaNs without losing data doesn't work. // Signaling vs. quiet is indicated by a bit, with the meaning being the opposite on MIPS vs. // everyone else. It would be great if we could just flip that bit, but we can't, because if the // significand is all-zero, then the value is infinity rather than NaN. This means that on most // machines, where the bit indicates quietness, there is one more quiet NaN value than signalling // NaN value, whereas on MIPS there is one more sNaN than qNaN, and thus there is no isomorphic // mapping that properly preserves quietness. Instead of doing something hacky, we just give up // and blow away NaN payloads, because no one uses them anyway. #endif namespace capnp { class ClientHook; namespace _ { // private class PointerBuilder; class PointerReader; class StructBuilder; class StructReader; class ListBuilder; class ListReader; class OrphanBuilder; struct WirePointer; struct WireHelpers; class SegmentReader; class SegmentBuilder; class Arena; class BuilderArena; // ============================================================================= #if CAPNP_DEBUG_TYPES typedef kj::UnitRatio, BitLabel, ElementLabel> BitsPerElementTableType; #else typedef uint BitsPerElementTableType; #endif static constexpr BitsPerElementTableType BITS_PER_ELEMENT_TABLE[8] = { bounded< 0>() * BITS / ELEMENTS, bounded< 1>() * BITS / ELEMENTS, bounded< 8>() * BITS / ELEMENTS, bounded<16>() * BITS / ELEMENTS, bounded<32>() * BITS / ELEMENTS, bounded<64>() * BITS / ELEMENTS, bounded< 0>() * BITS / ELEMENTS, bounded< 0>() * BITS / ELEMENTS }; inline KJ_CONSTEXPR() BitsPerElementTableType dataBitsPerElement(ElementSize size) { return _::BITS_PER_ELEMENT_TABLE[static_cast(size)]; } inline constexpr PointersPerElementN<1> pointersPerElement(ElementSize size) { return size == ElementSize::POINTER ? PointersPerElementN<1>(ONE * POINTERS / ELEMENTS) : PointersPerElementN<1>(ZERO * POINTERS / ELEMENTS); } static constexpr BitsPerElementTableType BITS_PER_ELEMENT_INCLUDING_PONITERS_TABLE[8] = { bounded< 0>() * BITS / ELEMENTS, bounded< 1>() * BITS / ELEMENTS, bounded< 8>() * BITS / ELEMENTS, bounded<16>() * BITS / ELEMENTS, bounded<32>() * BITS / ELEMENTS, bounded<64>() * BITS / ELEMENTS, bounded<64>() * BITS / ELEMENTS, bounded< 0>() * BITS / ELEMENTS }; inline KJ_CONSTEXPR() BitsPerElementTableType bitsPerElementIncludingPointers(ElementSize size) { return _::BITS_PER_ELEMENT_INCLUDING_PONITERS_TABLE[static_cast(size)]; } template struct ElementSizeForByteSize; template <> struct ElementSizeForByteSize<1> { static constexpr ElementSize value = ElementSize::BYTE; }; template <> struct ElementSizeForByteSize<2> { static constexpr ElementSize value = ElementSize::TWO_BYTES; }; template <> struct ElementSizeForByteSize<4> { static constexpr ElementSize value = ElementSize::FOUR_BYTES; }; template <> struct ElementSizeForByteSize<8> { static constexpr ElementSize value = ElementSize::EIGHT_BYTES; }; template struct ElementSizeForType { static constexpr ElementSize value = // Primitive types that aren't special-cased below can be determined from sizeof(). CAPNP_KIND(T) == Kind::PRIMITIVE ? ElementSizeForByteSize::value : CAPNP_KIND(T) == Kind::ENUM ? ElementSize::TWO_BYTES : CAPNP_KIND(T) == Kind::STRUCT ? ElementSize::INLINE_COMPOSITE : // Everything else is a pointer. ElementSize::POINTER; }; // Void and bool are special. template <> struct ElementSizeForType { static constexpr ElementSize value = ElementSize::VOID; }; template <> struct ElementSizeForType { static constexpr ElementSize value = ElementSize::BIT; }; // Lists and blobs are pointers, not structs. template struct ElementSizeForType> { static constexpr ElementSize value = ElementSize::POINTER; }; template <> struct ElementSizeForType { static constexpr ElementSize value = ElementSize::POINTER; }; template <> struct ElementSizeForType { static constexpr ElementSize value = ElementSize::POINTER; }; template inline constexpr ElementSize elementSizeForType() { return ElementSizeForType::value; } struct MessageSizeCounts { WordCountN<61, uint64_t> wordCount; // 2^64 bytes uint capCount; MessageSizeCounts& operator+=(const MessageSizeCounts& other) { // OK to truncate unchecked because this class is used to count actual stuff in memory, and // we couldn't possibly have anywhere near 2^61 words. wordCount = assumeBits<61>(wordCount + other.wordCount); capCount += other.capCount; return *this; } void addWords(WordCountN<61, uint64_t> other) { wordCount = assumeBits<61>(wordCount + other); } MessageSize asPublic() { return MessageSize { unbound(wordCount / WORDS), capCount }; } }; // ============================================================================= template union AlignedData { // Useful for declaring static constant data blobs as an array of bytes, but forcing those // bytes to be word-aligned. uint8_t bytes[wordCount * sizeof(word)]; word words[wordCount]; }; struct StructSize { StructDataWordCount data; StructPointerCount pointers; inline constexpr WordCountN<17> total() const { return data + pointers * WORDS_PER_POINTER; } StructSize() = default; inline constexpr StructSize(StructDataWordCount data, StructPointerCount pointers) : data(data), pointers(pointers) {} }; template inline constexpr StructSize structSize() { return StructSize(bounded(CapnpPrivate::dataWordSize) * WORDS, bounded(CapnpPrivate::pointerCount) * POINTERS); } template > inline constexpr StructSize minStructSizeForElement() { // If T is a struct, return its struct size. Otherwise return the minimum struct size big enough // to hold a T. return StructSize(bounded(CapnpPrivate::dataWordSize) * WORDS, bounded(CapnpPrivate::pointerCount) * POINTERS); } template > inline constexpr StructSize minStructSizeForElement() { // If T is a struct, return its struct size. Otherwise return the minimum struct size big enough // to hold a T. return StructSize( dataBitsPerElement(elementSizeForType()) * ELEMENTS > ZERO * BITS ? StructDataWordCount(ONE * WORDS) : StructDataWordCount(ZERO * WORDS), pointersPerElement(elementSizeForType()) * ELEMENTS); } // ------------------------------------------------------------------- // Masking of default values template struct Mask_; template struct Mask_ { typedef T Type; }; template struct Mask_ { typedef uint16_t Type; }; template <> struct Mask_ { typedef uint32_t Type; }; template <> struct Mask_ { typedef uint64_t Type; }; template struct Mask_ { // Union discriminants end up here. static_assert(sizeof(T) == 2, "Don't know how to mask this type."); typedef uint16_t Type; }; template using Mask = typename Mask_::Type; template KJ_ALWAYS_INLINE(Mask mask(T value, Mask mask)); template KJ_ALWAYS_INLINE(T unmask(Mask value, Mask mask)); template inline Mask mask(T value, Mask mask) { return static_cast >(value) ^ mask; } template <> inline uint32_t mask(float value, uint32_t mask) { #if CAPNP_CANONICALIZE_NAN if (value != value) { return 0x7fc00000u ^ mask; } #endif uint32_t i; static_assert(sizeof(i) == sizeof(value), "float is not 32 bits?"); memcpy(&i, &value, sizeof(value)); return i ^ mask; } template <> inline uint64_t mask(double value, uint64_t mask) { #if CAPNP_CANONICALIZE_NAN if (value != value) { return 0x7ff8000000000000ull ^ mask; } #endif uint64_t i; static_assert(sizeof(i) == sizeof(value), "double is not 64 bits?"); memcpy(&i, &value, sizeof(value)); return i ^ mask; } template inline T unmask(Mask value, Mask mask) { return static_cast(value ^ mask); } template <> inline float unmask(uint32_t value, uint32_t mask) { value ^= mask; float result; static_assert(sizeof(result) == sizeof(value), "float is not 32 bits?"); memcpy(&result, &value, sizeof(value)); return result; } template <> inline double unmask(uint64_t value, uint64_t mask) { value ^= mask; double result; static_assert(sizeof(result) == sizeof(value), "double is not 64 bits?"); memcpy(&result, &value, sizeof(value)); return result; } // ------------------------------------------------------------------- class CapTableReader { public: virtual kj::Maybe> extractCap(uint index) = 0; // Extract the capability at the given index. If the index is invalid, returns null. }; class CapTableBuilder: public CapTableReader { public: virtual uint injectCap(kj::Own&& cap) = 0; // Add the capability to the message and return its index. If the same ClientHook is injected // twice, this may return the same index both times, but in this case dropCap() needs to be // called an equal number of times to actually remove the cap. virtual void dropCap(uint index) = 0; // Remove a capability injected earlier. Called when the pointer is overwritten or zero'd out. }; // ------------------------------------------------------------------- class PointerBuilder: public kj::DisallowConstCopy { // Represents a single pointer, usually embedded in a struct or a list. public: inline PointerBuilder(): segment(nullptr), capTable(nullptr), pointer(nullptr) {} static inline PointerBuilder getRoot( SegmentBuilder* segment, CapTableBuilder* capTable, word* location); // Get a PointerBuilder representing a message root located in the given segment at the given // location. inline bool isNull() { return getPointerType() == PointerType::NULL_; } PointerType getPointerType() const; StructBuilder getStruct(StructSize size, const word* defaultValue); ListBuilder getList(ElementSize elementSize, const word* defaultValue); ListBuilder getStructList(StructSize elementSize, const word* defaultValue); ListBuilder getListAnySize(const word* defaultValue); template typename T::Builder getBlob( const void* defaultValue, ByteCount defaultSize); #if !CAPNP_LITE kj::Own getCapability(); #endif // !CAPNP_LITE // Get methods: Get the value. If it is null, initialize it to a copy of the default value. // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a // simple byte array for blobs. StructBuilder initStruct(StructSize size); ListBuilder initList(ElementSize elementSize, ElementCount elementCount); ListBuilder initStructList(ElementCount elementCount, StructSize size); template typename T::Builder initBlob(ByteCount size); // Init methods: Initialize the pointer to a newly-allocated object, discarding the existing // object. void setStruct(const StructReader& value, bool canonical = false); void setList(const ListReader& value, bool canonical = false); template void setBlob(typename T::Reader value); #if !CAPNP_LITE void setCapability(kj::Own&& cap); #endif // !CAPNP_LITE // Set methods: Initialize the pointer to a newly-allocated copy of the given value, discarding // the existing object. void adopt(OrphanBuilder&& orphan); // Set the pointer to point at the given orphaned value. OrphanBuilder disown(); // Set the pointer to null and return its previous value as an orphan. void clear(); // Clear the pointer to null, discarding its previous value. void transferFrom(PointerBuilder other); // Equivalent to `adopt(other.disown())`. void copyFrom(PointerReader other, bool canonical = false); // Equivalent to `set(other.get())`. // If you set the canonical flag, it will attempt to lay the target out // canonically, provided enough space is available. PointerReader asReader() const; BuilderArena* getArena() const; // Get the arena containing this pointer. CapTableBuilder* getCapTable(); // Gets the capability context in which this object is operating. PointerBuilder imbue(CapTableBuilder* capTable); // Return a copy of this builder except using the given capability context. private: SegmentBuilder* segment; // Memory segment in which the pointer resides. CapTableBuilder* capTable; // Table of capability indexes. WirePointer* pointer; // Pointer to the pointer. inline PointerBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* pointer) : segment(segment), capTable(capTable), pointer(pointer) {} friend class StructBuilder; friend class ListBuilder; friend class OrphanBuilder; }; class PointerReader { public: inline PointerReader() : segment(nullptr), capTable(nullptr), pointer(nullptr), nestingLimit(0x7fffffff) {} static PointerReader getRoot(SegmentReader* segment, CapTableReader* capTable, const word* location, int nestingLimit); // Get a PointerReader representing a message root located in the given segment at the given // location. static inline PointerReader getRootUnchecked(const word* location); // Get a PointerReader for an unchecked message. MessageSizeCounts targetSize() const; // Return the total size of the target object and everything to which it points. Does not count // far pointer overhead. This is useful for deciding how much space is needed to copy the object // into a flat array. However, the caller is advised NOT to treat this value as secure. Instead, // use the result as a hint for allocating the first segment, do the copy, and then throw an // exception if it overruns. inline bool isNull() const { return getPointerType() == PointerType::NULL_; } PointerType getPointerType() const; StructReader getStruct(const word* defaultValue) const; ListReader getList(ElementSize expectedElementSize, const word* defaultValue) const; ListReader getListAnySize(const word* defaultValue) const; template typename T::Reader getBlob(const void* defaultValue, ByteCount defaultSize) const; #if !CAPNP_LITE kj::Own getCapability() const; #endif // !CAPNP_LITE // Get methods: Get the value. If it is null, return the default value instead. // The default value is encoded as an "unchecked message" for structs, lists, and objects, or a // simple byte array for blobs. const word* getUnchecked() const; // If this is an unchecked message, get a word* pointing at the location of the pointer. This // word* can actually be passed to readUnchecked() to read the designated sub-object later. If // this isn't an unchecked message, throws an exception. kj::Maybe getArena() const; // Get the arena containing this pointer. CapTableReader* getCapTable(); // Gets the capability context in which this object is operating. PointerReader imbue(CapTableReader* capTable) const; // Return a copy of this reader except using the given capability context. bool isCanonical(const word **readHead); // Validate this pointer's canonicity, subject to the conditions: // * All data to the left of readHead has been read thus far (for pointer // ordering) // * All pointers in preorder have already been checked // * This pointer is in the first and only segment of the message private: SegmentReader* segment; // Memory segment in which the pointer resides. CapTableReader* capTable; // Table of capability indexes. const WirePointer* pointer; // Pointer to the pointer. null = treat as null pointer. int nestingLimit; // Limits the depth of message structures to guard against stack-overflow-based DoS attacks. // Once this reaches zero, further pointers will be pruned. inline PointerReader(SegmentReader* segment, CapTableReader* capTable, const WirePointer* pointer, int nestingLimit) : segment(segment), capTable(capTable), pointer(pointer), nestingLimit(nestingLimit) {} friend class StructReader; friend class ListReader; friend class PointerBuilder; friend class OrphanBuilder; }; // ------------------------------------------------------------------- class StructBuilder: public kj::DisallowConstCopy { public: inline StructBuilder(): segment(nullptr), capTable(nullptr), data(nullptr), pointers(nullptr) {} inline word* getLocation() { return reinterpret_cast(data); } // Get the object's location. Only valid for independently-allocated objects (i.e. not list // elements). inline StructDataBitCount getDataSectionSize() const { return dataSize; } inline StructPointerCount getPointerSectionSize() const { return pointerCount; } inline kj::ArrayPtr getDataSectionAsBlob(); inline _::ListBuilder getPointerSectionAsList(); template KJ_ALWAYS_INLINE(bool hasDataField(StructDataOffset offset)); // Return true if the field is set to something other than its default value. template KJ_ALWAYS_INLINE(T getDataField(StructDataOffset offset)); // Gets the data field value of the given type at the given offset. The offset is measured in // multiples of the field size, determined by the type. template KJ_ALWAYS_INLINE(T getDataField(StructDataOffset offset, Mask mask)); // Like getDataField() but applies the given XOR mask to the data on load. Used for reading // fields with non-zero default values. template KJ_ALWAYS_INLINE(void setDataField(StructDataOffset offset, kj::NoInfer value)); // Sets the data field value at the given offset. template KJ_ALWAYS_INLINE(void setDataField(StructDataOffset offset, kj::NoInfer value, Mask mask)); // Like setDataField() but applies the given XOR mask before storing. Used for writing fields // with non-zero default values. KJ_ALWAYS_INLINE(PointerBuilder getPointerField(StructPointerOffset ptrIndex)); // Get a builder for a pointer field given the index within the pointer section. void clearAll(); // Clear all pointers and data. void transferContentFrom(StructBuilder other); // Adopt all pointers from `other`, and also copy all data. If `other`'s sections are larger // than this, the extra data is not transferred, meaning there is a risk of data loss when // transferring from messages built with future versions of the protocol. void copyContentFrom(StructReader other); // Copy content from `other`. If `other`'s sections are larger than this, the extra data is not // copied, meaning there is a risk of data loss when copying from messages built with future // versions of the protocol. StructReader asReader() const; // Gets a StructReader pointing at the same memory. BuilderArena* getArena(); // Gets the arena in which this object is allocated. CapTableBuilder* getCapTable(); // Gets the capability context in which this object is operating. StructBuilder imbue(CapTableBuilder* capTable); // Return a copy of this builder except using the given capability context. private: SegmentBuilder* segment; // Memory segment in which the struct resides. CapTableBuilder* capTable; // Table of capability indexes. void* data; // Pointer to the encoded data. WirePointer* pointers; // Pointer to the encoded pointers. StructDataBitCount dataSize; // Size of data section. We use a bit count rather than a word count to more easily handle the // case of struct lists encoded with less than a word per element. StructPointerCount pointerCount; // Size of the pointer section. inline StructBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, void* data, WirePointer* pointers, StructDataBitCount dataSize, StructPointerCount pointerCount) : segment(segment), capTable(capTable), data(data), pointers(pointers), dataSize(dataSize), pointerCount(pointerCount) {} friend class ListBuilder; friend struct WireHelpers; friend class OrphanBuilder; }; class StructReader { public: inline StructReader() : segment(nullptr), capTable(nullptr), data(nullptr), pointers(nullptr), dataSize(ZERO * BITS), pointerCount(ZERO * POINTERS), nestingLimit(0x7fffffff) {} inline StructReader(kj::ArrayPtr data) : segment(nullptr), capTable(nullptr), data(data.begin()), pointers(nullptr), dataSize(assumeBits(data.size()) * WORDS * BITS_PER_WORD), pointerCount(ZERO * POINTERS), nestingLimit(0x7fffffff) {} const void* getLocation() const { return data; } inline StructDataBitCount getDataSectionSize() const { return dataSize; } inline StructPointerCount getPointerSectionSize() const { return pointerCount; } inline kj::ArrayPtr getDataSectionAsBlob() const; inline _::ListReader getPointerSectionAsList() const; kj::Array canonicalize(); template KJ_ALWAYS_INLINE(bool hasDataField(StructDataOffset offset) const); // Return true if the field is set to something other than its default value. template KJ_ALWAYS_INLINE(T getDataField(StructDataOffset offset) const); // Get the data field value of the given type at the given offset. The offset is measured in // multiples of the field size, determined by the type. Returns zero if the offset is past the // end of the struct's data section. template KJ_ALWAYS_INLINE(T getDataField(StructDataOffset offset, Mask mask) const); // Like getDataField(offset), but applies the given XOR mask to the result. Used for reading // fields with non-zero default values. KJ_ALWAYS_INLINE(PointerReader getPointerField(StructPointerOffset ptrIndex) const); // Get a reader for a pointer field given the index within the pointer section. If the index // is out-of-bounds, returns a null pointer. MessageSizeCounts totalSize() const; // Return the total size of the struct and everything to which it points. Does not count far // pointer overhead. This is useful for deciding how much space is needed to copy the struct // into a flat array. CapTableReader* getCapTable(); // Gets the capability context in which this object is operating. StructReader imbue(CapTableReader* capTable) const; // Return a copy of this reader except using the given capability context. bool isCanonical(const word **readHead, const word **ptrHead, bool *dataTrunc, bool *ptrTrunc); // Validate this pointer's canonicity, subject to the conditions: // * All data to the left of readHead has been read thus far (for pointer // ordering) // * All pointers in preorder have already been checked // * This pointer is in the first and only segment of the message // // If this function returns false, the struct is non-canonical. If it // returns true, then: // * If it is a composite in a list, it is canonical if at least one struct // in the list outputs dataTrunc = 1, and at least one outputs ptrTrunc = 1 // * If it is derived from a struct pointer, it is canonical if // dataTrunc = 1 AND ptrTrunc = 1 private: SegmentReader* segment; // Memory segment in which the struct resides. CapTableReader* capTable; // Table of capability indexes. const void* data; const WirePointer* pointers; StructDataBitCount dataSize; // Size of data section. We use a bit count rather than a word count to more easily handle the // case of struct lists encoded with less than a word per element. StructPointerCount pointerCount; // Size of the pointer section. int nestingLimit; // Limits the depth of message structures to guard against stack-overflow-based DoS attacks. // Once this reaches zero, further pointers will be pruned. // TODO(perf): Limit to 16 bits for better packing? inline StructReader(SegmentReader* segment, CapTableReader* capTable, const void* data, const WirePointer* pointers, StructDataBitCount dataSize, StructPointerCount pointerCount, int nestingLimit) : segment(segment), capTable(capTable), data(data), pointers(pointers), dataSize(dataSize), pointerCount(pointerCount), nestingLimit(nestingLimit) {} friend class ListReader; friend class StructBuilder; friend struct WireHelpers; }; // ------------------------------------------------------------------- class ListBuilder: public kj::DisallowConstCopy { public: inline explicit ListBuilder(ElementSize elementSize) : segment(nullptr), capTable(nullptr), ptr(nullptr), elementCount(ZERO * ELEMENTS), step(ZERO * BITS / ELEMENTS), structDataSize(ZERO * BITS), structPointerCount(ZERO * POINTERS), elementSize(elementSize) {} inline word* getLocation() { // Get the object's location. if (elementSize == ElementSize::INLINE_COMPOSITE && ptr != nullptr) { return reinterpret_cast(ptr) - POINTER_SIZE_IN_WORDS; } else { return reinterpret_cast(ptr); } } inline ElementSize getElementSize() const { return elementSize; } inline ListElementCount size() const; // The number of elements in the list. Text::Builder asText(); Data::Builder asData(); // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized. template KJ_ALWAYS_INLINE(T getDataElement(ElementCount index)); // Get the element of the given type at the given index. template KJ_ALWAYS_INLINE(void setDataElement(ElementCount index, kj::NoInfer value)); // Set the element at the given index. KJ_ALWAYS_INLINE(PointerBuilder getPointerElement(ElementCount index)); StructBuilder getStructElement(ElementCount index); ListReader asReader() const; // Get a ListReader pointing at the same memory. BuilderArena* getArena(); // Gets the arena in which this object is allocated. CapTableBuilder* getCapTable(); // Gets the capability context in which this object is operating. ListBuilder imbue(CapTableBuilder* capTable); // Return a copy of this builder except using the given capability context. private: SegmentBuilder* segment; // Memory segment in which the list resides. CapTableBuilder* capTable; // Table of capability indexes. byte* ptr; // Pointer to list content. ListElementCount elementCount; // Number of elements in the list. BitsPerElementN<23> step; // The distance between elements. The maximum value occurs when a struct contains 2^16-1 data // words and 2^16-1 pointers, i.e. 2^17 - 2 words, or 2^23 - 128 bits. StructDataBitCount structDataSize; StructPointerCount structPointerCount; // The struct properties to use when interpreting the elements as structs. All lists can be // interpreted as struct lists, so these are always filled in. ElementSize elementSize; // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE // from other types when the overall size is exactly zero or one words. inline ListBuilder(SegmentBuilder* segment, CapTableBuilder* capTable, void* ptr, BitsPerElementN<23> step, ListElementCount size, StructDataBitCount structDataSize, StructPointerCount structPointerCount, ElementSize elementSize) : segment(segment), capTable(capTable), ptr(reinterpret_cast(ptr)), elementCount(size), step(step), structDataSize(structDataSize), structPointerCount(structPointerCount), elementSize(elementSize) {} friend class StructBuilder; friend struct WireHelpers; friend class OrphanBuilder; }; class ListReader { public: inline explicit ListReader(ElementSize elementSize) : segment(nullptr), capTable(nullptr), ptr(nullptr), elementCount(ZERO * ELEMENTS), step(ZERO * BITS / ELEMENTS), structDataSize(ZERO * BITS), structPointerCount(ZERO * POINTERS), elementSize(elementSize), nestingLimit(0x7fffffff) {} inline ListElementCount size() const; // The number of elements in the list. inline ElementSize getElementSize() const { return elementSize; } Text::Reader asText(); Data::Reader asData(); // Reinterpret the list as a blob. Throws an exception if the elements are not byte-sized. kj::ArrayPtr asRawBytes() const; template KJ_ALWAYS_INLINE(T getDataElement(ElementCount index) const); // Get the element of the given type at the given index. KJ_ALWAYS_INLINE(PointerReader getPointerElement(ElementCount index) const); StructReader getStructElement(ElementCount index) const; MessageSizeCounts totalSize() const; // Like StructReader::totalSize(). Note that for struct lists, the size includes the list tag. CapTableReader* getCapTable(); // Gets the capability context in which this object is operating. ListReader imbue(CapTableReader* capTable) const; // Return a copy of this reader except using the given capability context. bool isCanonical(const word **readHead, const WirePointer* ref); // Validate this pointer's canonicity, subject to the conditions: // * All data to the left of readHead has been read thus far (for pointer // ordering) // * All pointers in preorder have already been checked // * This pointer is in the first and only segment of the message private: SegmentReader* segment; // Memory segment in which the list resides. CapTableReader* capTable; // Table of capability indexes. const byte* ptr; // Pointer to list content. ListElementCount elementCount; // Number of elements in the list. BitsPerElementN<23> step; // The distance between elements. The maximum value occurs when a struct contains 2^16-1 data // words and 2^16-1 pointers, i.e. 2^17 - 2 words, or 2^23 - 2 bits. StructDataBitCount structDataSize; StructPointerCount structPointerCount; // The struct properties to use when interpreting the elements as structs. All lists can be // interpreted as struct lists, so these are always filled in. ElementSize elementSize; // The element size as a ElementSize. This is only really needed to disambiguate INLINE_COMPOSITE // from other types when the overall size is exactly zero or one words. int nestingLimit; // Limits the depth of message structures to guard against stack-overflow-based DoS attacks. // Once this reaches zero, further pointers will be pruned. inline ListReader(SegmentReader* segment, CapTableReader* capTable, const void* ptr, ListElementCount elementCount, BitsPerElementN<23> step, StructDataBitCount structDataSize, StructPointerCount structPointerCount, ElementSize elementSize, int nestingLimit) : segment(segment), capTable(capTable), ptr(reinterpret_cast(ptr)), elementCount(elementCount), step(step), structDataSize(structDataSize), structPointerCount(structPointerCount), elementSize(elementSize), nestingLimit(nestingLimit) {} friend class StructReader; friend class ListBuilder; friend struct WireHelpers; friend class OrphanBuilder; }; // ------------------------------------------------------------------- class OrphanBuilder { public: inline OrphanBuilder(): segment(nullptr), capTable(nullptr), location(nullptr) { memset(&tag, 0, sizeof(tag)); } OrphanBuilder(const OrphanBuilder& other) = delete; inline OrphanBuilder(OrphanBuilder&& other) noexcept; inline ~OrphanBuilder() noexcept(false); static OrphanBuilder initStruct(BuilderArena* arena, CapTableBuilder* capTable, StructSize size); static OrphanBuilder initList(BuilderArena* arena, CapTableBuilder* capTable, ElementCount elementCount, ElementSize elementSize); static OrphanBuilder initStructList(BuilderArena* arena, CapTableBuilder* capTable, ElementCount elementCount, StructSize elementSize); static OrphanBuilder initText(BuilderArena* arena, CapTableBuilder* capTable, ByteCount size); static OrphanBuilder initData(BuilderArena* arena, CapTableBuilder* capTable, ByteCount size); static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, StructReader copyFrom); static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, ListReader copyFrom); static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, PointerReader copyFrom); static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, Text::Reader copyFrom); static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, Data::Reader copyFrom); #if !CAPNP_LITE static OrphanBuilder copy(BuilderArena* arena, CapTableBuilder* capTable, kj::Own copyFrom); #endif // !CAPNP_LITE static OrphanBuilder concat(BuilderArena* arena, CapTableBuilder* capTable, ElementSize expectedElementSize, StructSize expectedStructSize, kj::ArrayPtr lists); static OrphanBuilder referenceExternalData(BuilderArena* arena, Data::Reader data); OrphanBuilder& operator=(const OrphanBuilder& other) = delete; inline OrphanBuilder& operator=(OrphanBuilder&& other); inline bool operator==(decltype(nullptr)) const { return location == nullptr; } inline bool operator!=(decltype(nullptr)) const { return location != nullptr; } StructBuilder asStruct(StructSize size); // Interpret as a struct, or throw an exception if not a struct. ListBuilder asList(ElementSize elementSize); // Interpret as a list, or throw an exception if not a list. elementSize cannot be // INLINE_COMPOSITE -- use asStructList() instead. ListBuilder asStructList(StructSize elementSize); // Interpret as a struct list, or throw an exception if not a list. ListBuilder asListAnySize(); // For AnyList. Text::Builder asText(); Data::Builder asData(); // Interpret as a blob, or throw an exception if not a blob. StructReader asStructReader(StructSize size) const; ListReader asListReader(ElementSize elementSize) const; ListReader asListReaderAnySize() const; #if !CAPNP_LITE kj::Own asCapability() const; #endif // !CAPNP_LITE Text::Reader asTextReader() const; Data::Reader asDataReader() const; bool truncate(ElementCount size, bool isText) KJ_WARN_UNUSED_RESULT; // Resize the orphan list to the given size. Returns false if the list is currently empty but // the requested size is non-zero, in which case the caller will need to allocate a new list. void truncate(ElementCount size, ElementSize elementSize); void truncate(ElementCount size, StructSize elementSize); void truncateText(ElementCount size); // Versions of truncate() that know how to allocate a new list if needed. private: static_assert(ONE * POINTERS * WORDS_PER_POINTER == ONE * WORDS, "This struct assumes a pointer is one word."); word tag; // Contains an encoded WirePointer representing this object. WirePointer is defined in // layout.c++, but fits in a word. // // This may be a FAR pointer. Even in that case, `location` points to the eventual destination // of that far pointer. The reason we keep the far pointer around rather than just making `tag` // represent the final destination is because if the eventual adopter of the pointer is not in // the target's segment then it may be useful to reuse the far pointer landing pad. // // If `tag` is not a far pointer, its offset is garbage; only `location` points to the actual // target. SegmentBuilder* segment; // Segment in which the object resides. CapTableBuilder* capTable; // Table of capability indexes. word* location; // Pointer to the object, or nullptr if the pointer is null. For capabilities, we make this // 0x1 just so that it is non-null for operator==, but it is never used. inline OrphanBuilder(const void* tagPtr, SegmentBuilder* segment, CapTableBuilder* capTable, word* location) : segment(segment), capTable(capTable), location(location) { memcpy(&tag, tagPtr, sizeof(tag)); } inline WirePointer* tagAsPtr() { return reinterpret_cast(&tag); } inline const WirePointer* tagAsPtr() const { return reinterpret_cast(&tag); } void euthanize(); // Erase the target object, zeroing it out and possibly reclaiming the memory. Called when // the OrphanBuilder is being destroyed or overwritten and it is non-null. friend struct WireHelpers; }; // ======================================================================================= // Internal implementation details... // These are defined in the source file. template <> typename Text::Builder PointerBuilder::initBlob(ByteCount size); template <> void PointerBuilder::setBlob(typename Text::Reader value); template <> typename Text::Builder PointerBuilder::getBlob( const void* defaultValue, ByteCount defaultSize); template <> typename Text::Reader PointerReader::getBlob( const void* defaultValue, ByteCount defaultSize) const; template <> typename Data::Builder PointerBuilder::initBlob(ByteCount size); template <> void PointerBuilder::setBlob(typename Data::Reader value); template <> typename Data::Builder PointerBuilder::getBlob( const void* defaultValue, ByteCount defaultSize); template <> typename Data::Reader PointerReader::getBlob( const void* defaultValue, ByteCount defaultSize) const; inline PointerBuilder PointerBuilder::getRoot( SegmentBuilder* segment, CapTableBuilder* capTable, word* location) { return PointerBuilder(segment, capTable, reinterpret_cast(location)); } inline PointerReader PointerReader::getRootUnchecked(const word* location) { return PointerReader(nullptr, nullptr, reinterpret_cast(location), 0x7fffffff); } // ------------------------------------------------------------------- inline kj::ArrayPtr StructBuilder::getDataSectionAsBlob() { return kj::ArrayPtr(reinterpret_cast(data), unbound(dataSize / BITS_PER_BYTE / BYTES)); } inline _::ListBuilder StructBuilder::getPointerSectionAsList() { return _::ListBuilder(segment, capTable, pointers, ONE * POINTERS * BITS_PER_POINTER / ELEMENTS, pointerCount * (ONE * ELEMENTS / POINTERS), ZERO * BITS, ONE * POINTERS, ElementSize::POINTER); } template inline bool StructBuilder::hasDataField(StructDataOffset offset) { return getDataField>(offset) != 0; } template <> inline bool StructBuilder::hasDataField(StructDataOffset offset) { return false; } template inline T StructBuilder::getDataField(StructDataOffset offset) { return reinterpret_cast*>(data)[unbound(offset / ELEMENTS)].get(); } template <> inline bool StructBuilder::getDataField(StructDataOffset offset) { BitCount32 boffset = offset * (ONE * BITS / ELEMENTS); byte* b = reinterpret_cast(data) + boffset / BITS_PER_BYTE; return (*reinterpret_cast(b) & unbound(ONE << (boffset % BITS_PER_BYTE / BITS))) != 0; } template <> inline Void StructBuilder::getDataField(StructDataOffset offset) { return VOID; } template inline T StructBuilder::getDataField(StructDataOffset offset, Mask mask) { return unmask(getDataField >(offset), mask); } template inline void StructBuilder::setDataField(StructDataOffset offset, kj::NoInfer value) { reinterpret_cast*>(data)[unbound(offset / ELEMENTS)].set(value); } #if CAPNP_CANONICALIZE_NAN // Use mask() on floats and doubles to make sure we canonicalize NaNs. template <> inline void StructBuilder::setDataField(StructDataOffset offset, float value) { setDataField(offset, mask(value, 0)); } template <> inline void StructBuilder::setDataField(StructDataOffset offset, double value) { setDataField(offset, mask(value, 0)); } #endif template <> inline void StructBuilder::setDataField(StructDataOffset offset, bool value) { auto boffset = offset * (ONE * BITS / ELEMENTS); byte* b = reinterpret_cast(data) + boffset / BITS_PER_BYTE; uint bitnum = unboundMaxBits<3>(boffset % BITS_PER_BYTE / BITS); *reinterpret_cast(b) = (*reinterpret_cast(b) & ~(1 << bitnum)) | (static_cast(value) << bitnum); } template <> inline void StructBuilder::setDataField(StructDataOffset offset, Void value) {} template inline void StructBuilder::setDataField(StructDataOffset offset, kj::NoInfer value, Mask m) { setDataField >(offset, mask(value, m)); } inline PointerBuilder StructBuilder::getPointerField(StructPointerOffset ptrIndex) { // Hacky because WirePointer is defined in the .c++ file (so is incomplete here). return PointerBuilder(segment, capTable, reinterpret_cast( reinterpret_cast(pointers) + ptrIndex * WORDS_PER_POINTER)); } // ------------------------------------------------------------------- inline kj::ArrayPtr StructReader::getDataSectionAsBlob() const { return kj::ArrayPtr(reinterpret_cast(data), unbound(dataSize / BITS_PER_BYTE / BYTES)); } inline _::ListReader StructReader::getPointerSectionAsList() const { return _::ListReader(segment, capTable, pointers, pointerCount * (ONE * ELEMENTS / POINTERS), ONE * POINTERS * BITS_PER_POINTER / ELEMENTS, ZERO * BITS, ONE * POINTERS, ElementSize::POINTER, nestingLimit); } template inline bool StructReader::hasDataField(StructDataOffset offset) const { return getDataField>(offset) != 0; } template <> inline bool StructReader::hasDataField(StructDataOffset offset) const { return false; } template inline T StructReader::getDataField(StructDataOffset offset) const { if ((offset + ONE * ELEMENTS) * capnp::bitsPerElement() <= dataSize) { return reinterpret_cast*>(data)[unbound(offset / ELEMENTS)].get(); } else { return static_cast(0); } } template <> inline bool StructReader::getDataField(StructDataOffset offset) const { auto boffset = offset * (ONE * BITS / ELEMENTS); if (boffset < dataSize) { const byte* b = reinterpret_cast(data) + boffset / BITS_PER_BYTE; return (*reinterpret_cast(b) & unbound(ONE << (boffset % BITS_PER_BYTE / BITS))) != 0; } else { return false; } } template <> inline Void StructReader::getDataField(StructDataOffset offset) const { return VOID; } template T StructReader::getDataField(StructDataOffset offset, Mask mask) const { return unmask(getDataField >(offset), mask); } inline PointerReader StructReader::getPointerField(StructPointerOffset ptrIndex) const { if (ptrIndex < pointerCount) { // Hacky because WirePointer is defined in the .c++ file (so is incomplete here). return PointerReader(segment, capTable, reinterpret_cast( reinterpret_cast(pointers) + ptrIndex * WORDS_PER_POINTER), nestingLimit); } else{ return PointerReader(); } } // ------------------------------------------------------------------- inline ListElementCount ListBuilder::size() const { return elementCount; } template inline T ListBuilder::getDataElement(ElementCount index) { return reinterpret_cast*>( ptr + upgradeBound(index) * step / BITS_PER_BYTE)->get(); // TODO(perf): Benchmark this alternate implementation, which I suspect may make better use of // the x86 SIB byte. Also use it for all the other getData/setData implementations below, and // the various non-inline methods that look up pointers. // Also if using this, consider changing ptr back to void* instead of byte*. // return reinterpret_cast*>(ptr)[ // index / ELEMENTS * (step / capnp::bitsPerElement())].get(); } template <> inline bool ListBuilder::getDataElement(ElementCount index) { // Ignore step for bit lists because bit lists cannot be upgraded to struct lists. auto bindex = index * (ONE * BITS / ELEMENTS); byte* b = ptr + bindex / BITS_PER_BYTE; return (*reinterpret_cast(b) & unbound(ONE << (bindex % BITS_PER_BYTE / BITS))) != 0; } template <> inline Void ListBuilder::getDataElement(ElementCount index) { return VOID; } template inline void ListBuilder::setDataElement(ElementCount index, kj::NoInfer value) { reinterpret_cast*>( ptr + upgradeBound(index) * step / BITS_PER_BYTE)->set(value); } #if CAPNP_CANONICALIZE_NAN // Use mask() on floats and doubles to make sure we canonicalize NaNs. template <> inline void ListBuilder::setDataElement(ElementCount index, float value) { setDataElement(index, mask(value, 0)); } template <> inline void ListBuilder::setDataElement(ElementCount index, double value) { setDataElement(index, mask(value, 0)); } #endif template <> inline void ListBuilder::setDataElement(ElementCount index, bool value) { // Ignore stepBytes for bit lists because bit lists cannot be upgraded to struct lists. auto bindex = index * (ONE * BITS / ELEMENTS); byte* b = ptr + bindex / BITS_PER_BYTE; auto bitnum = bindex % BITS_PER_BYTE / BITS; *reinterpret_cast(b) = (*reinterpret_cast(b) & ~(1 << unbound(bitnum))) | (static_cast(value) << unbound(bitnum)); } template <> inline void ListBuilder::setDataElement(ElementCount index, Void value) {} inline PointerBuilder ListBuilder::getPointerElement(ElementCount index) { return PointerBuilder(segment, capTable, reinterpret_cast(ptr + upgradeBound(index) * step / BITS_PER_BYTE)); } // ------------------------------------------------------------------- inline ListElementCount ListReader::size() const { return elementCount; } template inline T ListReader::getDataElement(ElementCount index) const { return reinterpret_cast*>( ptr + upgradeBound(index) * step / BITS_PER_BYTE)->get(); } template <> inline bool ListReader::getDataElement(ElementCount index) const { // Ignore step for bit lists because bit lists cannot be upgraded to struct lists. auto bindex = index * (ONE * BITS / ELEMENTS); const byte* b = ptr + bindex / BITS_PER_BYTE; return (*reinterpret_cast(b) & unbound(ONE << (bindex % BITS_PER_BYTE / BITS))) != 0; } template <> inline Void ListReader::getDataElement(ElementCount index) const { return VOID; } inline PointerReader ListReader::getPointerElement(ElementCount index) const { return PointerReader(segment, capTable, reinterpret_cast( ptr + upgradeBound(index) * step / BITS_PER_BYTE), nestingLimit); } // ------------------------------------------------------------------- inline OrphanBuilder::OrphanBuilder(OrphanBuilder&& other) noexcept : segment(other.segment), capTable(other.capTable), location(other.location) { memcpy(&tag, &other.tag, sizeof(tag)); // Needs memcpy to comply with aliasing rules. other.segment = nullptr; other.location = nullptr; } inline OrphanBuilder::~OrphanBuilder() noexcept(false) { if (segment != nullptr) euthanize(); } inline OrphanBuilder& OrphanBuilder::operator=(OrphanBuilder&& other) { // With normal smart pointers, it's important to handle the case where the incoming pointer // is actually transitively owned by this one. In this case, euthanize() would destroy `other` // before we copied it. This isn't possible in the case of `OrphanBuilder` because it only // owns message objects, and `other` is not itself a message object, therefore cannot possibly // be transitively owned by `this`. if (segment != nullptr) euthanize(); segment = other.segment; capTable = other.capTable; location = other.location; memcpy(&tag, &other.tag, sizeof(tag)); // Needs memcpy to comply with aliasing rules. other.segment = nullptr; other.location = nullptr; return *this; } } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/orphan.h0000644000175000017500000004253613650101756020316 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "layout.h" CAPNP_BEGIN_HEADER namespace capnp { class StructSchema; class ListSchema; struct DynamicStruct; struct DynamicList; namespace _ { struct OrphanageInternal; } template class Orphan { // Represents an object which is allocated within some message builder but has no pointers // pointing at it. An Orphan can later be "adopted" by some other object as one of that object's // fields, without having to copy the orphan. For a field `foo` of pointer type, the generated // code will define builder methods `void adoptFoo(Orphan)` and `Orphan disownFoo()`. // Orphans can also be created independently of any parent using an Orphanage. // // `Orphan` can be moved but not copied, like `Own`, so that it is impossible for one // orphan to be adopted multiple times. If an orphan is destroyed without being adopted, its // contents are zero'd out (and possibly reused, if we ever implement the ability to reuse space // in a message arena). public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; inline Orphan(_::OrphanBuilder&& builder): builder(kj::mv(builder)) {} inline BuilderFor get(); // Get the underlying builder. If the orphan is null, this will allocate and return a default // object rather than crash. This is done for security -- otherwise, you might enable a DoS // attack any time you disown a field and fail to check if it is null. In the case of structs, // this means that the orphan is no longer null after get() returns. In the case of lists, // no actual object is allocated since a simple empty ListBuilder can be returned. inline ReaderFor getReader() const; inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } inline void truncate(uint size); // Resize an object (which must be a list or a blob) to the given size. // // If the new size is less than the original, the remaining elements will be discarded. The // list is never moved in this case. If the list happens to be located at the end of its segment // (which is always true if the list was the last thing allocated), the removed memory will be // reclaimed (reducing the messag size), otherwise it is simply zeroed. The reclaiming behavior // is particularly useful for allocating buffer space when you aren't sure how much space you // actually need: you can pre-allocate, say, a 4k byte array, read() from a file into it, and // then truncate it back to the amount of space actually used. // // If the new size is greater than the original, the list is extended with default values. If // the list is the last object in its segment *and* there is enough space left in the segment to // extend it to cover the new values, then the list is extended in-place. Otherwise, it must be // moved to a new location, leaving a zero'd hole in the previous space that won't be filled. // This copy is shallow; sub-objects will simply be reparented, not copied. // // Any existing readers or builders pointing at the object are invalidated by this call (even if // it doesn't move). You must call `get()` or `getReader()` again to get the new, valid pointer. private: _::OrphanBuilder builder; template friend struct _::PointerHelpers; template friend struct List; template friend class Orphan; friend class Orphanage; friend class MessageBuilder; }; class Orphanage: private kj::DisallowConstCopy { // Use to directly allocate Orphan objects, without having a parent object allocate and then // disown the object. public: inline Orphanage(): arena(nullptr) {} template static Orphanage getForMessageContaining(BuilderType builder); // Construct an Orphanage that allocates within the message containing the given Builder. This // allows the constructed Orphans to be adopted by objects within said message. // // This constructor takes the builder rather than having the builder have a getOrphanage() method // because this is an advanced feature and we don't want to pollute the builder APIs with it. // // Note that if you have a direct pointer to the `MessageBuilder`, you can simply call its // `getOrphanage()` method. template Orphan newOrphan() const; // Allocate a new orphaned struct. template Orphan newOrphan(uint size) const; // Allocate a new orphaned list or blob. Orphan newOrphan(StructSchema schema) const; // Dynamically create an orphan struct with the given schema. You must // #include to use this. Orphan newOrphan(ListSchema schema, uint size) const; // Dynamically create an orphan list with the given schema. You must #include // to use this. template Orphan> newOrphanCopy(Reader copyFrom) const; // Allocate a new orphaned object (struct, list, or blob) and initialize it as a copy of the // given object. template Orphan>>> newOrphanConcat(kj::ArrayPtr lists) const; template Orphan>>> newOrphanConcat(kj::ArrayPtr lists) const; // Given an array of List readers, copy and concatenate the lists, creating a new Orphan. // // Note that compared to allocating the list yourself and using `setWithCaveats()` to set each // item, this method avoids the "caveats": the new list will be allocated with the element size // being the maximum of that from all the input lists. This is particularly important when // concatenating struct lists: if the lists were created using a newer version of the protocol // in which some new fields had been added to the struct, using `setWithCaveats()` would // truncate off those new fields. Orphan referenceExternalData(Data::Reader data) const; // Creates an Orphan that points at an existing region of memory (e.g. from another message) // without copying it. There are some SEVERE restrictions on how this can be used: // - The memory must remain valid until the `MessageBuilder` is destroyed (even if the orphan is // abandoned). // - Because the data is const, you will not be allowed to obtain a `Data::Builder` // for this blob. Any call which would return such a builder will throw an exception. You // can, however, obtain a Reader, e.g. via orphan.getReader() or from a parent Reader (once // the orphan is adopted). It is your responsibility to make sure your code can deal with // these problems when using this optimization; if you can't, allocate a copy instead. // - `data.begin()` must be aligned to a machine word boundary (32-bit or 64-bit depending on // the CPU). Any pointer returned by malloc() as well as any data blob obtained from another // Cap'n Proto message satisfies this. // - If `data.size()` is not a multiple of 8, extra bytes past data.end() up until the next 8-byte // boundary will be visible in the raw message when it is written out. Thus, there must be no // secrets in these bytes. Data blobs obtained from other Cap'n Proto messages should be safe // as these bytes should be zero (unless the sender had the same problem). // // The array will actually become one of the message's segments. The data can thus be adopted // into the message tree without copying it. This is particularly useful when referencing very // large blobs, such as whole mmap'd files. private: _::BuilderArena* arena; _::CapTableBuilder* capTable; inline explicit Orphanage(_::BuilderArena* arena, _::CapTableBuilder* capTable) : arena(arena), capTable(capTable) {} template struct GetInnerBuilder; template struct GetInnerReader; template struct NewOrphanListImpl; friend class MessageBuilder; friend struct _::OrphanageInternal; }; // ======================================================================================= // Inline implementation details. namespace _ { // private template struct OrphanGetImpl; template struct OrphanGetImpl { static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, _::elementSizeForType()); } }; template struct OrphanGetImpl { static inline typename T::Builder apply(_::OrphanBuilder& builder) { return typename T::Builder(builder.asStruct(_::structSize())); } static inline typename T::Reader applyReader(const _::OrphanBuilder& builder) { return typename T::Reader(builder.asStructReader(_::structSize())); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, _::structSize()); } }; #if !CAPNP_LITE template struct OrphanGetImpl { static inline typename T::Client apply(_::OrphanBuilder& builder) { return typename T::Client(builder.asCapability()); } static inline typename T::Client applyReader(const _::OrphanBuilder& builder) { return typename T::Client(builder.asCapability()); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, ElementSize::POINTER); } }; #endif // !CAPNP_LITE template struct OrphanGetImpl, Kind::LIST> { static inline typename List::Builder apply(_::OrphanBuilder& builder) { return typename List::Builder(builder.asList(_::ElementSizeForType::value)); } static inline typename List::Reader applyReader(const _::OrphanBuilder& builder) { return typename List::Reader(builder.asListReader(_::ElementSizeForType::value)); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, ElementSize::POINTER); } }; template struct OrphanGetImpl, Kind::LIST> { static inline typename List::Builder apply(_::OrphanBuilder& builder) { return typename List::Builder(builder.asStructList(_::structSize())); } static inline typename List::Reader applyReader(const _::OrphanBuilder& builder) { return typename List::Reader(builder.asListReader(_::ElementSizeForType::value)); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, ElementSize::POINTER); } }; template <> struct OrphanGetImpl { static inline Text::Builder apply(_::OrphanBuilder& builder) { return Text::Builder(builder.asText()); } static inline Text::Reader applyReader(const _::OrphanBuilder& builder) { return Text::Reader(builder.asTextReader()); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, ElementSize::POINTER); } }; template <> struct OrphanGetImpl { static inline Data::Builder apply(_::OrphanBuilder& builder) { return Data::Builder(builder.asData()); } static inline Data::Reader applyReader(const _::OrphanBuilder& builder) { return Data::Reader(builder.asDataReader()); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, ElementSize::POINTER); } }; struct OrphanageInternal { static inline _::BuilderArena* getArena(Orphanage orphanage) { return orphanage.arena; } static inline _::CapTableBuilder* getCapTable(Orphanage orphanage) { return orphanage.capTable; } }; } // namespace _ (private) template inline BuilderFor Orphan::get() { return _::OrphanGetImpl::apply(builder); } template inline ReaderFor Orphan::getReader() const { return _::OrphanGetImpl::applyReader(builder); } template inline void Orphan::truncate(uint size) { _::OrphanGetImpl>::truncateListOf(builder, bounded(size) * ELEMENTS); } template <> inline void Orphan::truncate(uint size) { builder.truncateText(bounded(size) * ELEMENTS); } template <> inline void Orphan::truncate(uint size) { builder.truncate(bounded(size) * ELEMENTS, ElementSize::BYTE); } template struct Orphanage::GetInnerBuilder { static inline _::StructBuilder apply(typename T::Builder& t) { return t._builder; } }; template struct Orphanage::GetInnerBuilder { static inline _::ListBuilder apply(typename T::Builder& t) { return t.builder; } }; template Orphanage Orphanage::getForMessageContaining(BuilderType builder) { auto inner = GetInnerBuilder>::apply(builder); return Orphanage(inner.getArena(), inner.getCapTable()); } template Orphan Orphanage::newOrphan() const { return Orphan(_::OrphanBuilder::initStruct(arena, capTable, _::structSize())); } template struct Orphanage::NewOrphanListImpl> { static inline _::OrphanBuilder apply( _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) { return _::OrphanBuilder::initList( arena, capTable, bounded(size) * ELEMENTS, _::ElementSizeForType::value); } }; template struct Orphanage::NewOrphanListImpl> { static inline _::OrphanBuilder apply( _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) { return _::OrphanBuilder::initStructList( arena, capTable, bounded(size) * ELEMENTS, _::structSize()); } }; template <> struct Orphanage::NewOrphanListImpl { static inline _::OrphanBuilder apply( _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) { return _::OrphanBuilder::initText(arena, capTable, bounded(size) * BYTES); } }; template <> struct Orphanage::NewOrphanListImpl { static inline _::OrphanBuilder apply( _::BuilderArena* arena, _::CapTableBuilder* capTable, uint size) { return _::OrphanBuilder::initData(arena, capTable, bounded(size) * BYTES); } }; template Orphan Orphanage::newOrphan(uint size) const { return Orphan(NewOrphanListImpl::apply(arena, capTable, size)); } template struct Orphanage::GetInnerReader { static inline _::StructReader apply(const typename T::Reader& t) { return t._reader; } }; template struct Orphanage::GetInnerReader { static inline _::ListReader apply(const typename T::Reader& t) { return t.reader; } }; template struct Orphanage::GetInnerReader { static inline const typename T::Reader& apply(const typename T::Reader& t) { return t; } }; template inline Orphan> Orphanage::newOrphanCopy(Reader copyFrom) const { return Orphan>(_::OrphanBuilder::copy( arena, capTable, GetInnerReader>::apply(copyFrom))); } template inline Orphan>>> Orphanage::newOrphanConcat(kj::ArrayPtr lists) const { return newOrphanConcat(kj::implicitCast>(lists)); } template inline Orphan>>> Orphanage::newOrphanConcat(kj::ArrayPtr lists) const { // Optimization / simplification: Rely on List::Reader containing nothing except a // _::ListReader. static_assert(sizeof(T) == sizeof(_::ListReader), "lists are not bare readers?"); kj::ArrayPtr raw( reinterpret_cast(lists.begin()), lists.size()); typedef ListElementType> Element; return Orphan>( _::OrphanBuilder::concat(arena, capTable, _::elementSizeForType(), _::minStructSizeForElement(), raw)); } inline Orphan Orphanage::referenceExternalData(Data::Reader data) const { return Orphan(_::OrphanBuilder::referenceExternalData(arena, data)); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/list.h0000644000175000017500000005125213650101756017775 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "layout.h" #include "orphan.h" #include CAPNP_BEGIN_HEADER namespace capnp { namespace _ { // private template class TemporaryPointer { // This class is a little hack which lets us define operator->() in cases where it needs to // return a pointer to a temporary value. We instead construct a TemporaryPointer and return that // (by value). The compiler then invokes operator->() on the TemporaryPointer, which itself is // able to return a real pointer to its member. public: TemporaryPointer(T&& value): value(kj::mv(value)) {} TemporaryPointer(const T& value): value(value) {} inline T* operator->() { return &value; } private: T value; }; // By default this isn't compatible with STL algorithms. To add STL support either define // KJ_STD_COMPAT at the top of your compilation unit or include capnp/compat/std-iterator.h. template class IndexingIterator { public: IndexingIterator() = default; inline Element operator*() const { return (*container)[index]; } inline TemporaryPointer operator->() const { return TemporaryPointer((*container)[index]); } inline Element operator[]( int off) const { return (*container)[index]; } inline Element operator[](uint off) const { return (*container)[index]; } inline IndexingIterator& operator++() { ++index; return *this; } inline IndexingIterator operator++(int) { IndexingIterator other = *this; ++index; return other; } inline IndexingIterator& operator--() { --index; return *this; } inline IndexingIterator operator--(int) { IndexingIterator other = *this; --index; return other; } inline IndexingIterator operator+(uint amount) const { return IndexingIterator(container, index + amount); } inline IndexingIterator operator-(uint amount) const { return IndexingIterator(container, index - amount); } inline IndexingIterator operator+( int amount) const { return IndexingIterator(container, index + amount); } inline IndexingIterator operator-( int amount) const { return IndexingIterator(container, index - amount); } inline int operator-(const IndexingIterator& other) const { return index - other.index; } inline IndexingIterator& operator+=(uint amount) { index += amount; return *this; } inline IndexingIterator& operator-=(uint amount) { index -= amount; return *this; } inline IndexingIterator& operator+=( int amount) { index += amount; return *this; } inline IndexingIterator& operator-=( int amount) { index -= amount; return *this; } // STL says comparing iterators of different containers is not allowed, so we only compare // indices here. inline bool operator==(const IndexingIterator& other) const { return index == other.index; } inline bool operator!=(const IndexingIterator& other) const { return index != other.index; } inline bool operator<=(const IndexingIterator& other) const { return index <= other.index; } inline bool operator>=(const IndexingIterator& other) const { return index >= other.index; } inline bool operator< (const IndexingIterator& other) const { return index < other.index; } inline bool operator> (const IndexingIterator& other) const { return index > other.index; } private: Container* container; uint index; friend Container; inline IndexingIterator(Container* container, uint index) : container(container), index(index) {} }; } // namespace _ (private) template struct List { // List of primitives. List() = delete; class Reader { public: typedef List Reads; inline Reader(): reader(_::elementSizeForType()) {} inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline T operator[](uint index) const { KJ_IREQUIRE(index < size()); return reader.template getDataElement(bounded(index) * ELEMENTS); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class Builder { public: typedef List Builds; inline Builder(): builder(_::elementSizeForType()) {} inline Builder(decltype(nullptr)): Builder() {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline T operator[](uint index) { KJ_IREQUIRE(index < size()); return builder.template getDataElement(bounded(index) * ELEMENTS); } inline void set(uint index, T value) { // Alas, it is not possible to make operator[] return a reference to which you can assign, // since the encoded representation does not necessarily match the compiler's representation // of the type. We can't even return a clever class that implements operator T() and // operator=() because it will lead to surprising behavior when using type inference (e.g. // calling a template function with inferred argument types, or using "auto" or "decltype"). builder.template setDataElement(bounded(index) * ELEMENTS, value); } typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend struct ToDynamic_; }; class Pipeline {}; private: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { return builder.initList(_::elementSizeForType(), bounded(size) * ELEMENTS); } inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { return builder.getList(_::elementSizeForType(), defaultValue); } inline static _::ListReader getFromPointer( const _::PointerReader& reader, const word* defaultValue) { return reader.getList(_::elementSizeForType(), defaultValue); } template friend struct List; template friend struct _::PointerHelpers; }; template struct List: public List {}; template struct List { // List of structs. List() = delete; class Reader { public: typedef List Reads; inline Reader(): reader(ElementSize::INLINE_COMPOSITE) {} inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline typename T::Reader operator[](uint index) const { KJ_IREQUIRE(index < size()); return typename T::Reader(reader.getStructElement(bounded(index) * ELEMENTS)); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class Builder { public: typedef List Builds; inline Builder(): builder(ElementSize::INLINE_COMPOSITE) {} inline Builder(decltype(nullptr)): Builder() {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline typename T::Builder operator[](uint index) { KJ_IREQUIRE(index < size()); return typename T::Builder(builder.getStructElement(bounded(index) * ELEMENTS)); } inline void adoptWithCaveats(uint index, Orphan&& orphan) { // Mostly behaves like you'd expect `adopt` to behave, but with two caveats originating from // the fact that structs in a struct list are allocated inline rather than by pointer: // * This actually performs a shallow copy, effectively adopting each of the orphan's // children rather than adopting the orphan itself. The orphan ends up being discarded, // possibly wasting space in the message object. // * If the orphan is larger than the target struct -- say, because the orphan was built // using a newer version of the schema that has additional fields -- it will be truncated, // losing data. KJ_IREQUIRE(index < size()); // We pass a zero-valued StructSize to asStruct() because we do not want the struct to be // expanded under any circumstances. We're just going to throw it away anyway, and // transferContentFrom() already carefully compares the struct sizes before transferring. builder.getStructElement(bounded(index) * ELEMENTS).transferContentFrom( orphan.builder.asStruct(_::StructSize(ZERO * WORDS, ZERO * POINTERS))); } inline void setWithCaveats(uint index, const typename T::Reader& reader) { // Mostly behaves like you'd expect `set` to behave, but with a caveat originating from // the fact that structs in a struct list are allocated inline rather than by pointer: // If the source struct is larger than the target struct -- say, because the source was built // using a newer version of the schema that has additional fields -- it will be truncated, // losing data. // // Note: If you are trying to concatenate some lists, use Orphanage::newOrphanConcat() to // do it without losing any data in case the source lists come from a newer version of the // protocol. (Plus, it's easier to use anyhow.) KJ_IREQUIRE(index < size()); builder.getStructElement(bounded(index) * ELEMENTS).copyContentFrom(reader._reader); } // There are no init(), set(), adopt(), or disown() methods for lists of structs because the // elements of the list are inlined and are initialized when the list is initialized. This // means that init() would be redundant, and set() would risk data loss if the input struct // were from a newer version of the protocol. typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend struct ToDynamic_; }; class Pipeline {}; private: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { return builder.initStructList(bounded(size) * ELEMENTS, _::structSize()); } inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { return builder.getStructList(_::structSize(), defaultValue); } inline static _::ListReader getFromPointer( const _::PointerReader& reader, const word* defaultValue) { return reader.getList(ElementSize::INLINE_COMPOSITE, defaultValue); } template friend struct List; template friend struct _::PointerHelpers; }; template struct List, Kind::LIST> { // List of lists. List() = delete; class Reader { public: typedef List> Reads; inline Reader(): reader(ElementSize::POINTER) {} inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline typename List::Reader operator[](uint index) const { KJ_IREQUIRE(index < size()); return typename List::Reader(_::PointerHelpers>::get( reader.getPointerElement(bounded(index) * ELEMENTS))); } typedef _::IndexingIterator::Reader> Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class Builder { public: typedef List> Builds; inline Builder(): builder(ElementSize::POINTER) {} inline Builder(decltype(nullptr)): Builder() {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline typename List::Builder operator[](uint index) { KJ_IREQUIRE(index < size()); return typename List::Builder(_::PointerHelpers>::get( builder.getPointerElement(bounded(index) * ELEMENTS))); } inline typename List::Builder init(uint index, uint size) { KJ_IREQUIRE(index < this->size()); return typename List::Builder(_::PointerHelpers>::init( builder.getPointerElement(bounded(index) * ELEMENTS), size)); } inline void set(uint index, typename List::Reader value) { KJ_IREQUIRE(index < size()); builder.getPointerElement(bounded(index) * ELEMENTS).setList(value.reader); } void set(uint index, std::initializer_list> value) { KJ_IREQUIRE(index < size()); auto l = init(index, value.size()); uint i = 0; for (auto& element: value) { l.set(i++, element); } } inline void adopt(uint index, Orphan&& value) { KJ_IREQUIRE(index < size()); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(value.builder)); } inline Orphan disown(uint index) { KJ_IREQUIRE(index < size()); return Orphan(builder.getPointerElement(bounded(index) * ELEMENTS).disown()); } typedef _::IndexingIterator::Builder> Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend struct ToDynamic_; }; class Pipeline {}; private: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { return builder.initList(ElementSize::POINTER, bounded(size) * ELEMENTS); } inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { return builder.getList(ElementSize::POINTER, defaultValue); } inline static _::ListReader getFromPointer( const _::PointerReader& reader, const word* defaultValue) { return reader.getList(ElementSize::POINTER, defaultValue); } template friend struct List; template friend struct _::PointerHelpers; }; template struct List { List() = delete; class Reader { public: typedef List Reads; inline Reader(): reader(ElementSize::POINTER) {} inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline typename T::Reader operator[](uint index) const { KJ_IREQUIRE(index < size()); return reader.getPointerElement(bounded(index) * ELEMENTS) .template getBlob(nullptr, ZERO * BYTES); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class Builder { public: typedef List Builds; inline Builder(): builder(ElementSize::POINTER) {} inline Builder(decltype(nullptr)): Builder() {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline typename T::Builder operator[](uint index) { KJ_IREQUIRE(index < size()); return builder.getPointerElement(bounded(index) * ELEMENTS) .template getBlob(nullptr, ZERO * BYTES); } inline void set(uint index, typename T::Reader value) { KJ_IREQUIRE(index < size()); builder.getPointerElement(bounded(index) * ELEMENTS).template setBlob(value); } inline typename T::Builder init(uint index, uint size) { KJ_IREQUIRE(index < this->size()); return builder.getPointerElement(bounded(index) * ELEMENTS) .template initBlob(bounded(size) * BYTES); } inline void adopt(uint index, Orphan&& value) { KJ_IREQUIRE(index < size()); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(value.builder)); } inline Orphan disown(uint index) { KJ_IREQUIRE(index < size()); return Orphan(builder.getPointerElement(bounded(index) * ELEMENTS).disown()); } typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend struct ToDynamic_; }; class Pipeline {}; private: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { return builder.initList(ElementSize::POINTER, bounded(size) * ELEMENTS); } inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { return builder.getList(ElementSize::POINTER, defaultValue); } inline static _::ListReader getFromPointer( const _::PointerReader& reader, const word* defaultValue) { return reader.getList(ElementSize::POINTER, defaultValue); } template friend struct List; template friend struct _::PointerHelpers; }; } // namespace capnp #ifdef KJ_STD_COMPAT #include "compat/std-iterator.h" #endif // KJ_STD_COMPAT CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/any.h0000644000175000017500000010747513650101756017622 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "layout.h" #include "pointer-helpers.h" #include "orphan.h" #include "list.h" #include // work-around macro conflict with `VOID` #include CAPNP_BEGIN_HEADER namespace capnp { class StructSchema; class ListSchema; class InterfaceSchema; class Orphanage; class ClientHook; class PipelineHook; struct PipelineOp; struct AnyPointer; struct AnyList { AnyList() = delete; class Reader; class Builder; }; struct AnyStruct { AnyStruct() = delete; class Reader; class Builder; class Pipeline; }; template<> struct List { List() = delete; class Reader; class Builder; }; namespace _ { // private template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; } // namespace _ (private) // ======================================================================================= // AnyPointer! enum class Equality { NOT_EQUAL, EQUAL, UNKNOWN_CONTAINS_CAPS }; kj::StringPtr KJ_STRINGIFY(Equality res); struct AnyPointer { // Reader/Builder for the `AnyPointer` field type, i.e. a pointer that can point to an arbitrary // object. AnyPointer() = delete; class Reader { public: typedef AnyPointer Reads; Reader() = default; inline Reader(_::PointerReader reader): reader(reader) {} inline MessageSize targetSize() const; // Get the total size of the target object and all its children. inline PointerType getPointerType() const; inline bool isNull() const { return getPointerType() == PointerType::NULL_; } inline bool isStruct() const { return getPointerType() == PointerType::STRUCT; } inline bool isList() const { return getPointerType() == PointerType::LIST; } inline bool isCapability() const { return getPointerType() == PointerType::CAPABILITY; } Equality equals(AnyPointer::Reader right) const; bool operator==(AnyPointer::Reader right) const; inline bool operator!=(AnyPointer::Reader right) const { return !(*this == right); } template inline ReaderFor getAs() const; // Valid for T = any generated struct type, interface type, List, Text, or Data. template inline ReaderFor getAs(StructSchema schema) const; // Only valid for T = DynamicStruct. Requires `#include `. template inline ReaderFor getAs(ListSchema schema) const; // Only valid for T = DynamicList. Requires `#include `. template inline ReaderFor getAs(InterfaceSchema schema) const; // Only valid for T = DynamicCapability. Requires `#include `. #if !CAPNP_LITE kj::Own getPipelinedCap(kj::ArrayPtr ops) const; // Used by RPC system to implement pipelining. Applications generally shouldn't use this // directly. #endif // !CAPNP_LITE private: _::PointerReader reader; friend struct AnyPointer; friend class Orphanage; friend class CapReaderContext; friend struct _::PointerHelpers; }; class Builder { public: typedef AnyPointer Builds; Builder() = delete; inline Builder(decltype(nullptr)) {} inline Builder(_::PointerBuilder builder): builder(builder) {} inline MessageSize targetSize() const; // Get the total size of the target object and all its children. inline PointerType getPointerType(); inline bool isNull() { return getPointerType() == PointerType::NULL_; } inline bool isStruct() { return getPointerType() == PointerType::STRUCT; } inline bool isList() { return getPointerType() == PointerType::LIST; } inline bool isCapability() { return getPointerType() == PointerType::CAPABILITY; } inline Equality equals(AnyPointer::Reader right) const { return asReader().equals(right); } inline bool operator==(AnyPointer::Reader right) const { return asReader() == right; } inline bool operator!=(AnyPointer::Reader right) const { return !(*this == right); } inline void clear(); // Set to null. template inline BuilderFor getAs(); // Valid for T = any generated struct type, List, Text, or Data. template inline BuilderFor getAs(StructSchema schema); // Only valid for T = DynamicStruct. Requires `#include `. template inline BuilderFor getAs(ListSchema schema); // Only valid for T = DynamicList. Requires `#include `. template inline BuilderFor getAs(InterfaceSchema schema); // Only valid for T = DynamicCapability. Requires `#include `. template inline BuilderFor initAs(); // Valid for T = any generated struct type. template inline BuilderFor initAs(uint elementCount); // Valid for T = List, Text, or Data. template inline BuilderFor initAs(StructSchema schema); // Only valid for T = DynamicStruct. Requires `#include `. template inline BuilderFor initAs(ListSchema schema, uint elementCount); // Only valid for T = DynamicList. Requires `#include `. inline AnyList::Builder initAsAnyList(ElementSize elementSize, uint elementCount); // Note: Does not accept INLINE_COMPOSITE for elementSize. inline List::Builder initAsListOfAnyStruct( uint16_t dataWordCount, uint16_t pointerCount, uint elementCount); inline AnyStruct::Builder initAsAnyStruct(uint16_t dataWordCount, uint16_t pointerCount); template inline void setAs(ReaderFor value); // Valid for ReaderType = T::Reader for T = any generated struct type, List, Text, Data, // DynamicStruct, or DynamicList (the dynamic types require `#include `). template inline void setAs(std::initializer_list>> list); // Valid for T = List. template inline void setCanonicalAs(ReaderFor value); inline void set(Reader value) { builder.copyFrom(value.reader); } // Set to a copy of another AnyPointer. inline void setCanonical(Reader value) { builder.copyFrom(value.reader, true); } template inline void adopt(Orphan&& orphan); // Valid for T = any generated struct type, List, Text, Data, DynamicList, DynamicStruct, // or DynamicValue (the dynamic types require `#include `). template inline Orphan disownAs(); // Valid for T = any generated struct type, List, Text, Data. template inline Orphan disownAs(StructSchema schema); // Only valid for T = DynamicStruct. Requires `#include `. template inline Orphan disownAs(ListSchema schema); // Only valid for T = DynamicList. Requires `#include `. template inline Orphan disownAs(InterfaceSchema schema); // Only valid for T = DynamicCapability. Requires `#include `. inline Orphan disown(); // Disown without a type. inline Reader asReader() const { return Reader(builder.asReader()); } inline operator Reader() const { return Reader(builder.asReader()); } private: _::PointerBuilder builder; friend class Orphanage; friend class CapBuilderContext; friend struct _::PointerHelpers; }; #if !CAPNP_LITE class Pipeline { public: typedef AnyPointer Pipelines; inline Pipeline(decltype(nullptr)) {} inline explicit Pipeline(kj::Own&& hook): hook(kj::mv(hook)) {} Pipeline noop(); // Just make a copy. Pipeline getPointerField(uint16_t pointerIndex); // Deprecated. In the future, we should use .asAnyStruct.getPointerField. inline AnyStruct::Pipeline asAnyStruct(); kj::Own asCap(); // Expect that the result is a capability and construct a pipelined version of it now. inline kj::Own releasePipelineHook() { return kj::mv(hook); } // For use by RPC implementations. template ) == Kind::INTERFACE>> inline operator T() { return T(asCap()); } private: kj::Own hook; kj::Array ops; inline Pipeline(kj::Own&& hook, kj::Array&& ops) : hook(kj::mv(hook)), ops(kj::mv(ops)) {} friend class LocalClient; friend class PipelineHook; friend class AnyStruct::Pipeline; }; #endif // !CAPNP_LITE }; template <> class Orphan { // An orphaned object of unknown type. public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; inline Orphan(_::OrphanBuilder&& builder) : builder(kj::mv(builder)) {} Orphan& operator=(Orphan&&) = default; template inline Orphan(Orphan&& other): builder(kj::mv(other.builder)) {} template inline Orphan& operator=(Orphan&& other) { builder = kj::mv(other.builder); return *this; } // Cast from typed orphan. // It's not possible to get an AnyPointer::{Reader,Builder} directly since there is no // underlying pointer (the pointer would normally live in the parent, but this object is // orphaned). It is possible, however, to request typed readers/builders. template inline BuilderFor getAs(); template inline BuilderFor getAs(StructSchema schema); template inline BuilderFor getAs(ListSchema schema); template inline typename T::Client getAs(InterfaceSchema schema); template inline ReaderFor getAsReader() const; template inline ReaderFor getAsReader(StructSchema schema) const; template inline ReaderFor getAsReader(ListSchema schema) const; template inline typename T::Client getAsReader(InterfaceSchema schema) const; template inline Orphan releaseAs(); template inline Orphan releaseAs(StructSchema schema); template inline Orphan releaseAs(ListSchema schema); template inline Orphan releaseAs(InterfaceSchema schema); // Down-cast the orphan to a specific type. inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: _::OrphanBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend class Orphan; friend class AnyPointer::Builder; }; template struct AnyTypeFor_; template <> struct AnyTypeFor_ { typedef AnyStruct Type; }; template <> struct AnyTypeFor_ { typedef AnyList Type; }; template using AnyTypeFor = typename AnyTypeFor_::Type; template inline ReaderFor>> toAny(T&& value) { return ReaderFor>>( _::PointerHelpers>::getInternalReader(value)); } template inline BuilderFor>> toAny(T&& value) { return BuilderFor>>( _::PointerHelpers>::getInternalBuilder(kj::mv(value))); } template <> struct List { // Note: This cannot be used for a list of structs, since such lists are not encoded as pointer // lists! Use List. List() = delete; class Reader { public: typedef List Reads; inline Reader(): reader(ElementSize::POINTER) {} inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline AnyPointer::Reader operator[](uint index) const { KJ_IREQUIRE(index < size()); return AnyPointer::Reader(reader.getPointerElement(bounded(index) * ELEMENTS)); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class Builder { public: typedef List Builds; Builder() = delete; inline Builder(decltype(nullptr)): builder(ElementSize::POINTER) {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline AnyPointer::Builder operator[](uint index) { KJ_IREQUIRE(index < size()); return AnyPointer::Builder(builder.getPointerElement(bounded(index) * ELEMENTS)); } typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend struct ToDynamic_; }; }; class AnyStruct::Reader { public: typedef AnyStruct Reads; Reader() = default; inline Reader(_::StructReader reader): _reader(reader) {} template ) == Kind::STRUCT>> inline Reader(T&& value) : _reader(_::PointerHelpers>::getInternalReader(kj::fwd(value))) {} inline MessageSize totalSize() const { return _reader.totalSize().asPublic(); } kj::ArrayPtr getDataSection() const { return _reader.getDataSectionAsBlob(); } List::Reader getPointerSection() const { return List::Reader(_reader.getPointerSectionAsList()); } kj::Array canonicalize() { return _reader.canonicalize(); } Equality equals(AnyStruct::Reader right) const; bool operator==(AnyStruct::Reader right) const; inline bool operator!=(AnyStruct::Reader right) const { return !(*this == right); } template ReaderFor as() const { // T must be a struct type. return typename T::Reader(_reader); } template ReaderFor as(StructSchema schema) const; // T must be DynamicStruct. Defined in dynamic.h. private: _::StructReader _reader; template friend struct _::PointerHelpers; friend class Orphanage; }; class AnyStruct::Builder { public: typedef AnyStruct Builds; inline Builder(decltype(nullptr)) {} inline Builder(_::StructBuilder builder): _builder(builder) {} #if !_MSC_VER // TODO(msvc): MSVC ICEs on this. Try restoring when compiler improves. template ) == Kind::STRUCT>> inline Builder(T&& value) : _builder(_::PointerHelpers>::getInternalBuilder(kj::fwd(value))) {} #endif inline kj::ArrayPtr getDataSection() { return _builder.getDataSectionAsBlob(); } List::Builder getPointerSection() { return List::Builder(_builder.getPointerSectionAsList()); } inline Equality equals(AnyStruct::Reader right) const { return asReader().equals(right); } inline bool operator==(AnyStruct::Reader right) const { return asReader() == right; } inline bool operator!=(AnyStruct::Reader right) const { return !(*this == right); } inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return Reader(_builder.asReader()); } template BuilderFor as() { // T must be a struct type. return typename T::Builder(_builder); } template BuilderFor as(StructSchema schema); // T must be DynamicStruct. Defined in dynamic.h. private: _::StructBuilder _builder; friend class Orphanage; friend class CapBuilderContext; }; #if !CAPNP_LITE class AnyStruct::Pipeline { public: inline Pipeline(decltype(nullptr)): typeless(nullptr) {} inline explicit Pipeline(AnyPointer::Pipeline&& typeless) : typeless(kj::mv(typeless)) {} inline AnyPointer::Pipeline getPointerField(uint16_t pointerIndex) { // Return a new Promise representing a sub-object of the result. `pointerIndex` is the index // of the sub-object within the pointer section of the result (the result must be a struct). // // TODO(perf): On GCC 4.8 / Clang 3.3, use rvalue qualifiers to avoid the need for copies. // Also make `ops` into a Vector to optimize this. return typeless.getPointerField(pointerIndex); } private: AnyPointer::Pipeline typeless; }; #endif // !CAPNP_LITE class List::Reader { public: typedef List Reads; inline Reader(): reader(ElementSize::INLINE_COMPOSITE) {} inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline AnyStruct::Reader operator[](uint index) const { KJ_IREQUIRE(index < size()); return AnyStruct::Reader(reader.getStructElement(bounded(index) * ELEMENTS)); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class List::Builder { public: typedef List Builds; Builder() = delete; inline Builder(decltype(nullptr)): builder(ElementSize::INLINE_COMPOSITE) {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline AnyStruct::Builder operator[](uint index) { KJ_IREQUIRE(index < size()); return AnyStruct::Builder(builder.getStructElement(bounded(index) * ELEMENTS)); } typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; template friend struct _::PointerHelpers; friend class Orphanage; template friend struct ToDynamic_; }; class AnyList::Reader { public: typedef AnyList Reads; inline Reader(): _reader(ElementSize::VOID) {} inline Reader(_::ListReader reader): _reader(reader) {} #if !_MSC_VER // TODO(msvc): MSVC ICEs on this. Try restoring when compiler improves. template ) == Kind::LIST>> inline Reader(T&& value) : _reader(_::PointerHelpers>::getInternalReader(kj::fwd(value))) {} #endif inline ElementSize getElementSize() const { return _reader.getElementSize(); } inline uint size() const { return unbound(_reader.size() / ELEMENTS); } inline kj::ArrayPtr getRawBytes() const { return _reader.asRawBytes(); } Equality equals(AnyList::Reader right) const; bool operator==(AnyList::Reader right) const; inline bool operator!=(AnyList::Reader right) const { return !(*this == right); } inline MessageSize totalSize() const { return _reader.totalSize().asPublic(); } template ReaderFor as() const { // T must be List. return ReaderFor(_reader); } private: _::ListReader _reader; template friend struct _::PointerHelpers; friend class Orphanage; }; class AnyList::Builder { public: typedef AnyList Builds; inline Builder(decltype(nullptr)): _builder(ElementSize::VOID) {} inline Builder(_::ListBuilder builder): _builder(builder) {} #if !_MSC_VER // TODO(msvc): MSVC ICEs on this. Try restoring when compiler improves. template ) == Kind::LIST>> inline Builder(T&& value) : _builder(_::PointerHelpers>::getInternalBuilder(kj::fwd(value))) {} #endif inline ElementSize getElementSize() { return _builder.getElementSize(); } inline uint size() { return unbound(_builder.size() / ELEMENTS); } Equality equals(AnyList::Reader right) const; inline bool operator==(AnyList::Reader right) const{ return asReader() == right; } inline bool operator!=(AnyList::Reader right) const{ return !(*this == right); } template BuilderFor as() { // T must be List. return BuilderFor(_builder); } inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return Reader(_builder.asReader()); } private: _::ListBuilder _builder; friend class Orphanage; }; // ======================================================================================= // Pipeline helpers // // These relate to capabilities, but we don't declare them in capability.h because generated code // for structs needs to know about these, even in files that contain no interfaces. #if !CAPNP_LITE struct PipelineOp { // Corresponds to rpc.capnp's PromisedAnswer.Op. enum Type { NOOP, // for convenience GET_POINTER_FIELD // There may be other types in the future... }; Type type; union { uint16_t pointerIndex; // for GET_POINTER_FIELD }; }; inline uint KJ_HASHCODE(const PipelineOp& op) { switch (op.type) { case PipelineOp::NOOP: return kj::hashCode(op.type); case PipelineOp::GET_POINTER_FIELD: return kj::hashCode(op.type, op.pointerIndex); } KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT } inline bool operator==(const PipelineOp& a, const PipelineOp& b) { if (a.type != b.type) return false; switch (a.type) { case PipelineOp::NOOP: return true; case PipelineOp::GET_POINTER_FIELD: return a.pointerIndex == b.pointerIndex; } KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT } inline bool operator!=(const PipelineOp& a, const PipelineOp& b) { return !(a == b); } class PipelineHook { // Represents a currently-running call, and implements pipelined requests on its result. public: virtual kj::Own addRef() = 0; // Increment this object's reference count. virtual kj::Own getPipelinedCap(kj::ArrayPtr ops) = 0; // Extract a promised Capability from the results. virtual kj::Own getPipelinedCap(kj::Array&& ops); // Version of getPipelinedCap() passing the array by move. May avoid a copy in some cases. // Default implementation just calls the other version. template > static inline kj::Own from(Pipeline&& pipeline); template > static inline PipelineHook& from(Pipeline& pipeline); private: template struct FromImpl; }; #endif // !CAPNP_LITE // ======================================================================================= // Inline implementation details inline MessageSize AnyPointer::Reader::targetSize() const { return reader.targetSize().asPublic(); } inline PointerType AnyPointer::Reader::getPointerType() const { return reader.getPointerType(); } template inline ReaderFor AnyPointer::Reader::getAs() const { return _::PointerHelpers::get(reader); } inline MessageSize AnyPointer::Builder::targetSize() const { return asReader().targetSize(); } inline PointerType AnyPointer::Builder::getPointerType() { return builder.getPointerType(); } inline void AnyPointer::Builder::clear() { return builder.clear(); } template inline BuilderFor AnyPointer::Builder::getAs() { return _::PointerHelpers::get(builder); } template inline BuilderFor AnyPointer::Builder::initAs() { return _::PointerHelpers::init(builder); } template inline BuilderFor AnyPointer::Builder::initAs(uint elementCount) { return _::PointerHelpers::init(builder, elementCount); } inline AnyList::Builder AnyPointer::Builder::initAsAnyList( ElementSize elementSize, uint elementCount) { return AnyList::Builder(builder.initList(elementSize, bounded(elementCount) * ELEMENTS)); } inline List::Builder AnyPointer::Builder::initAsListOfAnyStruct( uint16_t dataWordCount, uint16_t pointerCount, uint elementCount) { return List::Builder(builder.initStructList(bounded(elementCount) * ELEMENTS, _::StructSize(bounded(dataWordCount) * WORDS, bounded(pointerCount) * POINTERS))); } inline AnyStruct::Builder AnyPointer::Builder::initAsAnyStruct( uint16_t dataWordCount, uint16_t pointerCount) { return AnyStruct::Builder(builder.initStruct( _::StructSize(bounded(dataWordCount) * WORDS, bounded(pointerCount) * POINTERS))); } template inline void AnyPointer::Builder::setAs(ReaderFor value) { return _::PointerHelpers::set(builder, value); } template inline void AnyPointer::Builder::setCanonicalAs(ReaderFor value) { return _::PointerHelpers::setCanonical(builder, value); } template inline void AnyPointer::Builder::setAs( std::initializer_list>> list) { return _::PointerHelpers::set(builder, list); } template inline void AnyPointer::Builder::adopt(Orphan&& orphan) { _::PointerHelpers::adopt(builder, kj::mv(orphan)); } template inline Orphan AnyPointer::Builder::disownAs() { return _::PointerHelpers::disown(builder); } inline Orphan AnyPointer::Builder::disown() { return Orphan(builder.disown()); } template <> struct ReaderFor_ { typedef AnyPointer::Reader Type; }; template <> struct BuilderFor_ { typedef AnyPointer::Builder Type; }; template <> struct ReaderFor_ { typedef AnyStruct::Reader Type; }; template <> struct BuilderFor_ { typedef AnyStruct::Builder Type; }; template <> struct Orphanage::GetInnerReader { static inline _::PointerReader apply(const AnyPointer::Reader& t) { return t.reader; } }; template <> struct Orphanage::GetInnerBuilder { static inline _::PointerBuilder apply(AnyPointer::Builder& t) { return t.builder; } }; template <> struct Orphanage::GetInnerReader { static inline _::StructReader apply(const AnyStruct::Reader& t) { return t._reader; } }; template <> struct Orphanage::GetInnerBuilder { static inline _::StructBuilder apply(AnyStruct::Builder& t) { return t._builder; } }; template <> struct Orphanage::GetInnerReader { static inline _::ListReader apply(const AnyList::Reader& t) { return t._reader; } }; template <> struct Orphanage::GetInnerBuilder { static inline _::ListBuilder apply(AnyList::Builder& t) { return t._builder; } }; template inline BuilderFor Orphan::getAs() { return _::OrphanGetImpl::apply(builder); } template inline ReaderFor Orphan::getAsReader() const { return _::OrphanGetImpl::applyReader(builder); } template inline Orphan Orphan::releaseAs() { return Orphan(kj::mv(builder)); } // Using AnyPointer as the template type should work... template <> inline typename AnyPointer::Reader AnyPointer::Reader::getAs() const { return *this; } template <> inline typename AnyPointer::Builder AnyPointer::Builder::getAs() { return *this; } template <> inline typename AnyPointer::Builder AnyPointer::Builder::initAs() { clear(); return *this; } template <> inline void AnyPointer::Builder::setAs(AnyPointer::Reader value) { return builder.copyFrom(value.reader); } template <> inline void AnyPointer::Builder::adopt(Orphan&& orphan) { builder.adopt(kj::mv(orphan.builder)); } template <> inline Orphan AnyPointer::Builder::disownAs() { return Orphan(builder.disown()); } template <> inline Orphan Orphan::releaseAs() { return kj::mv(*this); } namespace _ { // private // Specialize PointerHelpers for AnyPointer. template <> struct PointerHelpers { static inline AnyPointer::Reader get(PointerReader reader, const void* defaultValue = nullptr, uint defaultBytes = 0) { return AnyPointer::Reader(reader); } static inline AnyPointer::Builder get(PointerBuilder builder, const void* defaultValue = nullptr, uint defaultBytes = 0) { return AnyPointer::Builder(builder); } static inline void set(PointerBuilder builder, AnyPointer::Reader value) { AnyPointer::Builder(builder).set(value); } static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder) { return Orphan(builder.disown()); } static inline _::PointerReader getInternalReader(const AnyPointer::Reader& reader) { return reader.reader; } static inline _::PointerBuilder getInternalBuilder(AnyPointer::Builder&& builder) { return builder.builder; } }; template <> struct PointerHelpers { static inline AnyStruct::Reader get( PointerReader reader, const word* defaultValue = nullptr) { return AnyStruct::Reader(reader.getStruct(defaultValue)); } static inline AnyStruct::Builder get( PointerBuilder builder, const word* defaultValue = nullptr) { // TODO(someday): Allow specifying the size somehow? return AnyStruct::Builder(builder.getStruct( _::StructSize(ZERO * WORDS, ZERO * POINTERS), defaultValue)); } static inline void set(PointerBuilder builder, AnyStruct::Reader value) { builder.setStruct(value._reader); } static inline AnyStruct::Builder init( PointerBuilder builder, uint16_t dataWordCount, uint16_t pointerCount) { return AnyStruct::Builder(builder.initStruct( StructSize(bounded(dataWordCount) * WORDS, bounded(pointerCount) * POINTERS))); } static void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static Orphan disown(PointerBuilder builder) { return Orphan(builder.disown()); } }; template <> struct PointerHelpers { static inline AnyList::Reader get( PointerReader reader, const word* defaultValue = nullptr) { return AnyList::Reader(reader.getListAnySize(defaultValue)); } static inline AnyList::Builder get( PointerBuilder builder, const word* defaultValue = nullptr) { return AnyList::Builder(builder.getListAnySize(defaultValue)); } static inline void set(PointerBuilder builder, AnyList::Reader value) { builder.setList(value._reader); } static inline AnyList::Builder init( PointerBuilder builder, ElementSize elementSize, uint elementCount) { return AnyList::Builder(builder.initList( elementSize, bounded(elementCount) * ELEMENTS)); } static inline AnyList::Builder init( PointerBuilder builder, uint16_t dataWordCount, uint16_t pointerCount, uint elementCount) { return AnyList::Builder(builder.initStructList( bounded(elementCount) * ELEMENTS, StructSize(bounded(dataWordCount) * WORDS, bounded(pointerCount) * POINTERS))); } static void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static Orphan disown(PointerBuilder builder) { return Orphan(builder.disown()); } }; template <> struct OrphanGetImpl { static inline AnyStruct::Builder apply(_::OrphanBuilder& builder) { return AnyStruct::Builder(builder.asStruct(_::StructSize(ZERO * WORDS, ZERO * POINTERS))); } static inline AnyStruct::Reader applyReader(const _::OrphanBuilder& builder) { return AnyStruct::Reader(builder.asStructReader(_::StructSize(ZERO * WORDS, ZERO * POINTERS))); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, _::StructSize(ZERO * WORDS, ZERO * POINTERS)); } }; template <> struct OrphanGetImpl { static inline AnyList::Builder apply(_::OrphanBuilder& builder) { return AnyList::Builder(builder.asListAnySize()); } static inline AnyList::Reader applyReader(const _::OrphanBuilder& builder) { return AnyList::Reader(builder.asListReaderAnySize()); } static inline void truncateListOf(_::OrphanBuilder& builder, ElementCount size) { builder.truncate(size, ElementSize::POINTER); } }; } // namespace _ (private) #if !CAPNP_LITE template struct PipelineHook::FromImpl { static inline kj::Own apply(typename T::Pipeline&& pipeline) { return from(kj::mv(pipeline._typeless)); } static inline PipelineHook& apply(typename T::Pipeline& pipeline) { return from(pipeline._typeless); } }; template <> struct PipelineHook::FromImpl { static inline kj::Own apply(AnyPointer::Pipeline&& pipeline) { return kj::mv(pipeline.hook); } static inline PipelineHook& apply(AnyPointer::Pipeline& pipeline) { return *pipeline.hook; } }; template inline kj::Own PipelineHook::from(Pipeline&& pipeline) { return FromImpl::apply(kj::fwd(pipeline)); } template inline PipelineHook& PipelineHook::from(Pipeline& pipeline) { return FromImpl::apply(pipeline); } #endif // !CAPNP_LITE } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/message.h0000644000175000017500000006177113650101756020455 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include #include #include #include "common.h" #include "layout.h" #include "any.h" CAPNP_BEGIN_HEADER namespace capnp { namespace _ { // private class ReaderArena; class BuilderArena; struct CloneImpl; } class StructSchema; class Orphanage; template class Orphan; // ======================================================================================= struct ReaderOptions { // Options controlling how data is read. uint64_t traversalLimitInWords = 8 * 1024 * 1024; // Limits how many total words of data are allowed to be traversed. Traversal is counted when // a new struct or list builder is obtained, e.g. from a get() accessor. This means that calling // the getter for the same sub-struct multiple times will cause it to be double-counted. Once // the traversal limit is reached, an error will be reported. // // This limit exists for security reasons. It is possible for an attacker to construct a message // in which multiple pointers point at the same location. This is technically invalid, but hard // to detect. Using such a message, an attacker could cause a message which is small on the wire // to appear much larger when actually traversed, possibly exhausting server resources leading to // denial-of-service. // // It makes sense to set a traversal limit that is much larger than the underlying message. // Together with sensible coding practices (e.g. trying to avoid calling sub-object getters // multiple times, which is expensive anyway), this should provide adequate protection without // inconvenience. // // The default limit is 64 MiB. This may or may not be a sensible number for any given use case, // but probably at least prevents easy exploitation while also avoiding causing problems in most // typical cases. int nestingLimit = 64; // Limits how deeply-nested a message structure can be, e.g. structs containing other structs or // lists of structs. // // Like the traversal limit, this limit exists for security reasons. Since it is common to use // recursive code to traverse recursive data structures, an attacker could easily cause a stack // overflow by sending a very-deeply-nested (or even cyclic) message, without the message even // being very large. The default limit of 64 is probably low enough to prevent any chance of // stack overflow, yet high enough that it is never a problem in practice. }; class MessageReader { // Abstract interface for an object used to read a Cap'n Proto message. Subclasses of // MessageReader are responsible for reading the raw, flat message content. Callers should // usually call `messageReader.getRoot()` to get a `MyStructType::Reader` // representing the root of the message, then use that to traverse the message content. // // Some common subclasses of `MessageReader` include `SegmentArrayMessageReader`, whose // constructor accepts pointers to the raw data, and `StreamFdMessageReader` (from // `serialize.h`), which reads the message from a file descriptor. One might implement other // subclasses to handle things like reading from shared memory segments, mmap()ed files, etc. public: MessageReader(ReaderOptions options); // It is suggested that subclasses take ReaderOptions as a constructor parameter, but give it a // default value of "ReaderOptions()". The base class constructor doesn't have a default value // in order to remind subclasses that they really need to give the user a way to provide this. virtual ~MessageReader() noexcept(false); virtual kj::ArrayPtr getSegment(uint id) = 0; // Gets the segment with the given ID, or returns null if no such segment exists. This method // will be called at most once for each segment ID. inline const ReaderOptions& getOptions(); // Get the options passed to the constructor. template typename RootType::Reader getRoot(); // Get the root struct of the message, interpreting it as the given struct type. template typename RootType::Reader getRoot(SchemaType schema); // Dynamically interpret the root struct of the message using the given schema (a StructSchema). // RootType in this case must be DynamicStruct, and you must #include to // use this. bool isCanonical(); // Returns whether the message encoded in the reader is in canonical form. size_t sizeInWords(); // Add up the size of all segments. private: ReaderOptions options; #if defined(__EMSCRIPTEN__) static constexpr size_t arenaSpacePadding = 19; #else static constexpr size_t arenaSpacePadding = 18; #endif // Space in which we can construct a ReaderArena. We don't use ReaderArena directly here // because we don't want clients to have to #include arena.h, which itself includes a bunch of // other headers. We don't use a pointer to a ReaderArena because that would require an // extra malloc on every message which could be expensive when processing small messages. void* arenaSpace[arenaSpacePadding + sizeof(kj::MutexGuarded) / sizeof(void*)]; bool allocatedArena; _::ReaderArena* arena() { return reinterpret_cast<_::ReaderArena*>(arenaSpace); } AnyPointer::Reader getRootInternal(); }; class MessageBuilder { // Abstract interface for an object used to allocate and build a message. Subclasses of // MessageBuilder are responsible for allocating the space in which the message will be written. // The most common subclass is `MallocMessageBuilder`, but other subclasses may be used to do // tricky things like allocate messages in shared memory or mmap()ed files. // // Creating a new message ususually means allocating a new MessageBuilder (ideally on the stack) // and then calling `messageBuilder.initRoot()` to get a `MyStructType::Builder`. // That, in turn, can be used to fill in the message content. When done, you can call // `messageBuilder.getSegmentsForOutput()` to get a list of flat data arrays containing the // message. public: MessageBuilder(); virtual ~MessageBuilder() noexcept(false); KJ_DISALLOW_COPY(MessageBuilder); struct SegmentInit { kj::ArrayPtr space; size_t wordsUsed; // Number of words in `space` which are used; the rest are free space in which additional // objects may be allocated. }; explicit MessageBuilder(kj::ArrayPtr segments); // Create a MessageBuilder backed by existing memory. This is an advanced interface that most // people should not use. THIS METHOD IS INSECURE; see below. // // This allows a MessageBuilder to be constructed to modify an in-memory message without first // making a copy of the content. This is especially useful in conjunction with mmap(). // // The contents of each segment must outlive the MessageBuilder, but the SegmentInit array itself // only need outlive the constructor. // // SECURITY: Do not use this in conjunction with untrusted data. This constructor assumes that // the input message is valid. This constructor is designed to be used with data you control, // e.g. an mmap'd file which is owned and accessed by only one program. When reading data you // do not trust, you *must* load it into a Reader and then copy into a Builder as a means of // validating the content. // // WARNING: It is NOT safe to initialize a MessageBuilder in this way from memory that is // currently in use by another MessageBuilder or MessageReader. Other readers/builders will // not observe changes to the segment sizes nor newly-allocated segments caused by allocating // new objects in this message. virtual kj::ArrayPtr allocateSegment(uint minimumSize) = 0; // Allocates an array of at least the given number of zero'd words, throwing an exception or // crashing if this is not possible. It is expected that this method will usually return more // space than requested, and the caller should use that extra space as much as possible before // allocating more. The returned space remains valid at least until the MessageBuilder is // destroyed. // // allocateSegment() is responsible for zeroing the memory before returning. This is required // because otherwise the Cap'n Proto implementation would have to zero the memory anyway, and // many allocators are able to provide already-zero'd memory more efficiently. template typename RootType::Builder initRoot(); // Initialize the root struct of the message as the given struct type. template void setRoot(Reader&& value); // Set the root struct to a deep copy of the given struct. template typename RootType::Builder getRoot(); // Get the root struct of the message, interpreting it as the given struct type. template typename RootType::Builder getRoot(SchemaType schema); // Dynamically interpret the root struct of the message using the given schema (a StructSchema). // RootType in this case must be DynamicStruct, and you must #include to // use this. template typename RootType::Builder initRoot(SchemaType schema); // Dynamically init the root struct of the message using the given schema (a StructSchema). // RootType in this case must be DynamicStruct, and you must #include to // use this. template void adoptRoot(Orphan&& orphan); // Like setRoot() but adopts the orphan without copying. kj::ArrayPtr> getSegmentsForOutput(); // Get the raw data that makes up the message. Orphanage getOrphanage(); bool isCanonical(); // Check whether the message builder is in canonical form size_t sizeInWords(); // Add up the allocated space from all segments. private: void* arenaSpace[22]; // Space in which we can construct a BuilderArena. We don't use BuilderArena directly here // because we don't want clients to have to #include arena.h, which itself includes a bunch of // big STL headers. We don't use a pointer to a BuilderArena because that would require an // extra malloc on every message which could be expensive when processing small messages. bool allocatedArena = false; // We have to initialize the arena lazily because when we do so we want to allocate the root // pointer immediately, and this will allocate a segment, which requires a virtual function // call on the MessageBuilder. We can't do such a call in the constructor since the subclass // isn't constructed yet. This is kind of annoying because it means that getOrphanage() is // not thread-safe, but that shouldn't be a huge deal... _::BuilderArena* arena() { return reinterpret_cast<_::BuilderArena*>(arenaSpace); } _::SegmentBuilder* getRootSegment(); AnyPointer::Builder getRootInternal(); kj::Own<_::CapTableBuilder> releaseBuiltinCapTable(); // Hack for clone() to extract the cap table. friend struct _::CloneImpl; // We can't declare clone() as a friend directly because old versions of GCC incorrectly demand // that the first declaration (even if it is a friend declaration) specify the default type args, // whereas correct compilers do not permit default type args to be specified on a friend decl. }; template typename RootType::Reader readMessageUnchecked(const word* data); // IF THE INPUT IS INVALID, THIS MAY CRASH, CORRUPT MEMORY, CREATE A SECURITY HOLE IN YOUR APP, // MURDER YOUR FIRST-BORN CHILD, AND/OR BRING ABOUT ETERNAL DAMNATION ON ALL OF HUMANITY. DO NOT // USE UNLESS YOU UNDERSTAND THE CONSEQUENCES. // // Given a pointer to a known-valid message located in a single contiguous memory segment, // returns a reader for that message. No bounds-checking will be done while traversing this // message. Use this only if you have already verified that all pointers are valid and in-bounds, // and there are no far pointers in the message. // // To create a message that can be passed to this function, build a message using a MallocAllocator // whose preferred segment size is larger than the message size. This guarantees that the message // will be allocated as a single segment, meaning getSegmentsForOutput() returns a single word // array. That word array is your message; you may pass a pointer to its first word into // readMessageUnchecked() to read the message. // // This can be particularly handy for embedding messages in generated code: you can // embed the raw bytes (using AlignedData) then make a Reader for it using this. This is the way // default values are embedded in code generated by the Cap'n Proto compiler. E.g., if you have // a message MyMessage, you can read its default value like so: // MyMessage::Reader reader = Message::readMessageUnchecked(MyMessage::DEFAULT.words); // // To sanitize a message from an untrusted source such that it can be safely passed to // readMessageUnchecked(), use copyToUnchecked(). template void copyToUnchecked(Reader&& reader, kj::ArrayPtr uncheckedBuffer); // Copy the content of the given reader into the given buffer, such that it can safely be passed to // readMessageUnchecked(). The buffer's size must be exactly reader.totalSizeInWords() + 1, // otherwise an exception will be thrown. The buffer must be zero'd before calling. template typename RootType::Reader readDataStruct(kj::ArrayPtr data); // Interprets the given data as a single, data-only struct. Only primitive fields (booleans, // numbers, and enums) will be readable; all pointers will be null. This is useful if you want // to use Cap'n Proto as a language/platform-neutral way to pack some bits. // // The input is a word array rather than a byte array to enforce alignment. If you have a byte // array which you know is word-aligned (or if your platform supports unaligned reads and you don't // mind the performance penalty), then you can use `reinterpret_cast` to convert a byte array into // a word array: // // kj::arrayPtr(reinterpret_cast(bytes.begin()), // reinterpret_cast(bytes.end())) template typename kj::ArrayPtr writeDataStruct(BuilderType builder); // Given a struct builder, get the underlying data section as a word array, suitable for passing // to `readDataStruct()`. // // Note that you may call `.toBytes()` on the returned value to convert to `ArrayPtr`. template static typename Type::Reader defaultValue(); // Get a default instance of the given struct or list type. // // TODO(cleanup): Find a better home for this function? template > kj::Own> clone(Reader&& reader); // Make a deep copy of the given Reader on the heap, producing an owned pointer. // ======================================================================================= class SegmentArrayMessageReader: public MessageReader { // A simple MessageReader that reads from an array of word arrays representing all segments. // In particular you can read directly from the output of MessageBuilder::getSegmentsForOutput() // (although it would probably make more sense to call builder.getRoot().asReader() in that case). public: SegmentArrayMessageReader(kj::ArrayPtr> segments, ReaderOptions options = ReaderOptions()); // Creates a message pointing at the given segment array, without taking ownership of the // segments. All arrays passed in must remain valid until the MessageReader is destroyed. KJ_DISALLOW_COPY(SegmentArrayMessageReader); ~SegmentArrayMessageReader() noexcept(false); virtual kj::ArrayPtr getSegment(uint id) override; private: kj::ArrayPtr> segments; }; enum class AllocationStrategy: uint8_t { FIXED_SIZE, // The builder will prefer to allocate the same amount of space for each segment with no // heuristic growth. It will still allocate larger segments when the preferred size is too small // for some single object. This mode is generally not recommended, but can be particularly useful // for testing in order to force a message to allocate a predictable number of segments. Note // that you can force every single object in the message to be located in a separate segment by // using this mode with firstSegmentWords = 0. GROW_HEURISTICALLY // The builder will heuristically decide how much space to allocate for each segment. Each // allocated segment will be progressively larger than the previous segments on the assumption // that message sizes are exponentially distributed. The total number of segments that will be // allocated for a message of size n is O(log n). }; constexpr uint SUGGESTED_FIRST_SEGMENT_WORDS = 1024; constexpr AllocationStrategy SUGGESTED_ALLOCATION_STRATEGY = AllocationStrategy::GROW_HEURISTICALLY; class MallocMessageBuilder: public MessageBuilder { // A simple MessageBuilder that uses malloc() (actually, calloc()) to allocate segments. This // implementation should be reasonable for any case that doesn't require writing the message to // a specific location in memory. public: explicit MallocMessageBuilder(uint firstSegmentWords = SUGGESTED_FIRST_SEGMENT_WORDS, AllocationStrategy allocationStrategy = SUGGESTED_ALLOCATION_STRATEGY); // Creates a BuilderContext which allocates at least the given number of words for the first // segment, and then uses the given strategy to decide how much to allocate for subsequent // segments. When choosing a value for firstSegmentWords, consider that: // 1) Reading and writing messages gets slower when multiple segments are involved, so it's good // if most messages fit in a single segment. // 2) Unused bytes will not be written to the wire, so generally it is not a big deal to allocate // more space than you need. It only becomes problematic if you are allocating many messages // in parallel and thus use lots of memory, or if you allocate so much extra space that just // zeroing it out becomes a bottleneck. // The defaults have been chosen to be reasonable for most people, so don't change them unless you // have reason to believe you need to. explicit MallocMessageBuilder(kj::ArrayPtr firstSegment, AllocationStrategy allocationStrategy = SUGGESTED_ALLOCATION_STRATEGY); // This version always returns the given array for the first segment, and then proceeds with the // allocation strategy. This is useful for optimization when building lots of small messages in // a tight loop: you can reuse the space for the first segment. // // firstSegment MUST be zero-initialized. MallocMessageBuilder's destructor will write new zeros // over any space that was used so that it can be reused. KJ_DISALLOW_COPY(MallocMessageBuilder); virtual ~MallocMessageBuilder() noexcept(false); virtual kj::ArrayPtr allocateSegment(uint minimumSize) override; private: uint nextSize; AllocationStrategy allocationStrategy; bool ownFirstSegment; bool returnedFirstSegment; void* firstSegment; kj::Vector moreSegments; }; class FlatMessageBuilder: public MessageBuilder { // THIS IS NOT THE CLASS YOU'RE LOOKING FOR. // // If you want to write a message into already-existing scratch space, use `MallocMessageBuilder` // and pass the scratch space to its constructor. It will then only fall back to malloc() if // the scratch space is not large enough. // // Do NOT use this class unless you really know what you're doing. This class is problematic // because it requires advance knowledge of the size of your message, which is usually impossible // to determine without actually building the message. The class was created primarily to // implement `copyToUnchecked()`, which itself exists only to support other internal parts of // the Cap'n Proto implementation. public: explicit FlatMessageBuilder(kj::ArrayPtr array); KJ_DISALLOW_COPY(FlatMessageBuilder); virtual ~FlatMessageBuilder() noexcept(false); void requireFilled(); // Throws an exception if the flat array is not exactly full. virtual kj::ArrayPtr allocateSegment(uint minimumSize) override; private: kj::ArrayPtr array; bool allocated; }; // ======================================================================================= // implementation details inline const ReaderOptions& MessageReader::getOptions() { return options; } template inline typename RootType::Reader MessageReader::getRoot() { return getRootInternal().getAs(); } template inline typename RootType::Builder MessageBuilder::initRoot() { return getRootInternal().initAs(); } template inline void MessageBuilder::setRoot(Reader&& value) { getRootInternal().setAs>(value); } template inline typename RootType::Builder MessageBuilder::getRoot() { return getRootInternal().getAs(); } template void MessageBuilder::adoptRoot(Orphan&& orphan) { return getRootInternal().adopt(kj::mv(orphan)); } template typename RootType::Reader MessageReader::getRoot(SchemaType schema) { return getRootInternal().getAs(schema); } template typename RootType::Builder MessageBuilder::getRoot(SchemaType schema) { return getRootInternal().getAs(schema); } template typename RootType::Builder MessageBuilder::initRoot(SchemaType schema) { return getRootInternal().initAs(schema); } template typename RootType::Reader readMessageUnchecked(const word* data) { return AnyPointer::Reader(_::PointerReader::getRootUnchecked(data)).getAs(); } template void copyToUnchecked(Reader&& reader, kj::ArrayPtr uncheckedBuffer) { FlatMessageBuilder builder(uncheckedBuffer); builder.setRoot(kj::fwd(reader)); builder.requireFilled(); } template typename RootType::Reader readDataStruct(kj::ArrayPtr data) { return typename RootType::Reader(_::StructReader(data)); } template typename kj::ArrayPtr writeDataStruct(BuilderType builder) { auto bytes = _::PointerHelpers>::getInternalBuilder(kj::mv(builder)) .getDataSectionAsBlob(); return kj::arrayPtr(reinterpret_cast(bytes.begin()), reinterpret_cast(bytes.end())); } template static typename Type::Reader defaultValue() { return typename Type::Reader(_::StructReader()); } namespace _ { struct CloneImpl { static inline kj::Own<_::CapTableBuilder> releaseBuiltinCapTable(MessageBuilder& message) { return message.releaseBuiltinCapTable(); } }; }; template kj::Own> clone(Reader&& reader) { auto size = reader.totalSize(); auto buffer = kj::heapArray(size.wordCount + 1); memset(buffer.asBytes().begin(), 0, buffer.asBytes().size()); if (size.capCount == 0) { copyToUnchecked(reader, buffer); auto result = readMessageUnchecked>(buffer.begin()); return kj::attachVal(result, kj::mv(buffer)); } else { FlatMessageBuilder builder(buffer); builder.setRoot(kj::fwd(reader)); builder.requireFilled(); auto capTable = _::CloneImpl::releaseBuiltinCapTable(builder); AnyPointer::Reader raw(_::PointerReader::getRootUnchecked(buffer.begin()).imbue(capTable)); return kj::attachVal(raw.getAs>(), kj::mv(buffer), kj::mv(capTable)); } } template kj::Array canonicalize(T&& reader) { return _::PointerHelpers>::getInternalReader(reader).canonicalize(); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/capability.h0000644000175000017500000012626313650101756021150 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #if CAPNP_LITE #error "RPC APIs, including this header, are not available in lite mode." #endif #include #include #include "raw-schema.h" #include "any.h" #include "pointer-helpers.h" CAPNP_BEGIN_HEADER namespace capnp { template class Response; template class RemotePromise: public kj::Promise>, public T::Pipeline { // A Promise which supports pipelined calls. T is typically a struct type. T must declare // an inner "mix-in" type "Pipeline" which implements pipelining; RemotePromise simply // multiply-inherits that type along with Promise>. T::Pipeline must be movable, // but does not need to be copyable (i.e. just like Promise). // // The promise is for an owned pointer so that the RPC system can allocate the MessageReader // itself. public: inline RemotePromise(kj::Promise>&& promise, typename T::Pipeline&& pipeline) : kj::Promise>(kj::mv(promise)), T::Pipeline(kj::mv(pipeline)) {} inline RemotePromise(decltype(nullptr)) : kj::Promise>(nullptr), T::Pipeline(nullptr) {} KJ_DISALLOW_COPY(RemotePromise); RemotePromise(RemotePromise&& other) = default; RemotePromise& operator=(RemotePromise&& other) = default; static RemotePromise reducePromise(kj::Promise&& promise); // Hook for KJ so that Promise> automatically reduces to RemotePromise. }; class LocalClient; namespace _ { // private extern const RawSchema NULL_INTERFACE_SCHEMA; // defined in schema.c++ class CapabilityServerSetBase; } // namespace _ (private) struct Capability { // A capability without type-safe methods. Typed capability clients wrap `Client` and typed // capability servers subclass `Server` to dispatch to the regular, typed methods. class Client; class Server; struct _capnpPrivate { struct IsInterface; static constexpr uint64_t typeId = 0x3; static constexpr Kind kind = Kind::INTERFACE; static constexpr _::RawSchema const* schema = &_::NULL_INTERFACE_SCHEMA; static const _::RawBrandedSchema* brand() { return &_::NULL_INTERFACE_SCHEMA.defaultBrand; } }; }; // ======================================================================================= // Capability clients class RequestHook; class ResponseHook; class PipelineHook; class ClientHook; template class Request: public Params::Builder { // A call that hasn't been sent yet. This class extends a Builder for the call's "Params" // structure with a method send() that actually sends it. // // Given a Cap'n Proto method `foo(a :A, b :B): C`, the generated client interface will have // a method `Request fooRequest()` (as well as a convenience method // `RemotePromise foo(A::Reader a, B::Reader b)`). public: inline Request(typename Params::Builder builder, kj::Own&& hook) : Params::Builder(builder), hook(kj::mv(hook)) {} inline Request(decltype(nullptr)): Params::Builder(nullptr) {} RemotePromise send() KJ_WARN_UNUSED_RESULT; // Send the call and return a promise for the results. private: kj::Own hook; friend class Capability::Client; friend struct DynamicCapability; template friend class CallContext; friend class RequestHook; }; template class StreamingRequest: public Params::Builder { // Like `Request` but for streaming requests. public: inline StreamingRequest(typename Params::Builder builder, kj::Own&& hook) : Params::Builder(builder), hook(kj::mv(hook)) {} inline StreamingRequest(decltype(nullptr)): Params::Builder(nullptr) {} kj::Promise send() KJ_WARN_UNUSED_RESULT; private: kj::Own hook; friend class Capability::Client; friend struct DynamicCapability; template friend class CallContext; friend class RequestHook; }; template class Response: public Results::Reader { // A completed call. This class extends a Reader for the call's answer structure. The Response // is move-only -- once it goes out-of-scope, the underlying message will be freed. public: inline Response(typename Results::Reader reader, kj::Own&& hook) : Results::Reader(reader), hook(kj::mv(hook)) {} private: kj::Own hook; template friend class Request; friend class ResponseHook; }; class Capability::Client { // Base type for capability clients. public: typedef Capability Reads; typedef Capability Calls; Client(decltype(nullptr)); // If you need to declare a Client before you have anything to assign to it (perhaps because // the assignment is going to occur in an if/else scope), you can start by initializing it to // `nullptr`. The resulting client is not meant to be called and throws exceptions from all // methods. template ()>> Client(kj::Own&& server); // Make a client capability that wraps the given server capability. The server's methods will // only be executed in the given EventLoop, regardless of what thread calls the client's methods. template ()>> Client(kj::Promise&& promise); // Make a client from a promise for a future client. The resulting client queues calls until the // promise resolves. Client(kj::Exception&& exception); // Make a broken client that throws the given exception from all calls. Client(Client& other); Client& operator=(Client& other); // Copies by reference counting. Warning: This refcounting is not thread-safe. All copies of // the client must remain in one thread. Client(Client&&) = default; Client& operator=(Client&&) = default; // Move constructor avoids reference counting. explicit Client(kj::Own&& hook); // For use by the RPC implementation: Wrap a ClientHook. template typename T::Client castAs(); // Reinterpret the capability as implementing the given interface. Note that no error will occur // here if the capability does not actually implement this interface, but later method calls will // fail. It's up to the application to decide how indicate that additional interfaces are // supported. // // TODO(perf): GCC 4.8 / Clang 3.3: rvalue-qualified version for better performance. template typename T::Client castAs(InterfaceSchema schema); // Dynamic version. `T` must be `DynamicCapability`, and you must `#include `. kj::Promise whenResolved(); // If the capability is actually only a promise, the returned promise resolves once the // capability itself has resolved to its final destination (or propagates the exception if // the capability promise is rejected). This is mainly useful for error-checking in the case // where no calls are being made. There is no reason to wait for this before making calls; if // the capability does not resolve, the call results will propagate the error. Request typelessRequest( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint); // Make a request without knowing the types of the params or results. You specify the type ID // and method number manually. kj::Promise> getFd(); // If the capability's server implemented Capability::Server::getFd() returning non-null, and all // RPC links between the client and server support FD passing, returns a file descriptor pointing // to the same underlying file description as the server did. Returns null if the server provided // no FD or if FD passing was unavailable at some intervening link. // // This returns a Promise to handle the case of an unresolved promise capability, e.g. a // pipelined capability. The promise resolves no later than when the capability settles, i.e. // the same time `whenResolved()` would complete. // // The file descriptor will remain open at least as long as the Capability::Client remains alive. // If you need it to last longer, you will need to `dup()` it. // TODO(someday): method(s) for Join protected: Client() = default; template Request newCall(uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint); template StreamingRequest newStreamingCall(uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint); private: kj::Own hook; static kj::Own makeLocalClient(kj::Own&& server); template friend struct _::PointerHelpers; friend struct DynamicCapability; friend class Orphanage; friend struct DynamicStruct; friend struct DynamicList; template friend struct List; friend class _::CapabilityServerSetBase; friend class ClientHook; }; // ======================================================================================= // Capability servers class CallContextHook; template class CallContext: public kj::DisallowConstCopy { // Wrapper around CallContextHook with a specific return type. // // Methods of this class may only be called from within the server's event loop, not from other // threads. // // The CallContext becomes invalid as soon as the call reports completion. public: explicit CallContext(CallContextHook& hook); typename Params::Reader getParams(); // Get the params payload. void releaseParams(); // Release the params payload. getParams() will throw an exception after this is called. // Releasing the params may allow the RPC system to free up buffer space to handle other // requests. Long-running asynchronous methods should try to call this as early as is // convenient. typename Results::Builder getResults(kj::Maybe sizeHint = nullptr); typename Results::Builder initResults(kj::Maybe sizeHint = nullptr); void setResults(typename Results::Reader value); void adoptResults(Orphan&& value); Orphanage getResultsOrphanage(kj::Maybe sizeHint = nullptr); // Manipulate the results payload. The "Return" message (part of the RPC protocol) will // typically be allocated the first time one of these is called. Some RPC systems may // allocate these messages in a limited space (such as a shared memory segment), therefore the // application should delay calling these as long as is convenient to do so (but don't delay // if doing so would require extra copies later). // // `sizeHint` indicates a guess at the message size. This will usually be used to decide how // much space to allocate for the first message segment (don't worry: only space that is actually // used will be sent on the wire). If omitted, the system decides. The message root pointer // should not be included in the size. So, if you are simply going to copy some existing message // directly into the results, just call `.totalSize()` and pass that in. template kj::Promise tailCall(Request&& tailRequest); // Resolve the call by making a tail call. `tailRequest` is a request that has been filled in // but not yet sent. The context will send the call, then fill in the results with the result // of the call. If tailCall() is used, {get,init,set,adopt}Results (above) *must not* be called. // // The RPC implementation may be able to optimize a tail call to another machine such that the // results never actually pass through this machine. Even if no such optimization is possible, // `tailCall()` may allow pipelined calls to be forwarded optimistically to the new call site. // // In general, this should be the last thing a method implementation calls, and the promise // returned from `tailCall()` should then be returned by the method implementation. void allowCancellation(); // Indicate that it is OK for the RPC system to discard its Promise for this call's result if // the caller cancels the call, thereby transitively canceling any asynchronous operations the // call implementation was performing. This is not done by default because it could represent a // security risk: applications must be carefully written to ensure that they do not end up in // a bad state if an operation is canceled at an arbitrary point. However, for long-running // method calls that hold significant resources, prompt cancellation is often useful. // // Keep in mind that asynchronous cancellation cannot occur while the method is synchronously // executing on a local thread. The method must perform an asynchronous operation or call // `EventLoop::current().evalLater()` to yield control. // // Note: You might think that we should offer `onCancel()` and/or `isCanceled()` methods that // provide notification when the caller cancels the request without forcefully killing off the // promise chain. Unfortunately, this composes poorly with promise forking: the canceled // path may be just one branch of a fork of the result promise. The other branches still want // the call to continue. Promise forking is used within the Cap'n Proto implementation -- in // particular each pipelined call forks the result promise. So, if a caller made a pipelined // call and then dropped the original object, the call should not be canceled, but it would be // excessively complicated for the framework to avoid notififying of cancellation as long as // pipelined calls still exist. private: CallContextHook* hook; friend class Capability::Server; friend struct DynamicCapability; }; template class StreamingCallContext: public kj::DisallowConstCopy { // Like CallContext but for streaming calls. public: explicit StreamingCallContext(CallContextHook& hook); typename Params::Reader getParams(); void releaseParams(); // Note: tailCall() is not supported because: // - It would significantly complicate the implementation of streaming. // - It wouldn't be particularly useful since streaming calls don't return anything, and they // already compensate for latency. void allowCancellation(); private: CallContextHook* hook; friend class Capability::Server; friend struct DynamicCapability; }; class Capability::Server { // Objects implementing a Cap'n Proto interface must subclass this. Typically, such objects // will instead subclass a typed Server interface which will take care of implementing // dispatchCall(). public: typedef Capability Serves; struct DispatchCallResult { kj::Promise promise; // Promise for completion of the call. bool isStreaming; // If true, this method was declared as `-> stream;`. No other calls should be permitted until // this call finishes, and if this call throws an exception, all future calls will throw the // same exception. }; virtual DispatchCallResult dispatchCall(uint64_t interfaceId, uint16_t methodId, CallContext context) = 0; // Call the given method. `params` is the input struct, and should be released as soon as it // is no longer needed. `context` may be used to allocate the output struct and deal with // cancellation. virtual kj::Maybe getFd() { return nullptr; } // If this capability is backed by a file descriptor that is safe to directly expose to clients, // returns that FD. When FD passing has been enabled in the RPC layer, this FD may be sent to // other processes along with the capability. virtual kj::Maybe> shortenPath(); // If this returns non-null, then it is a promise which, when resolved, points to a new // capability to which future calls can be sent. Use this in cases where an object implementation // might discover a more-optimized path some time after it starts. // // Implementing this (and returning non-null) will cause the capability to be advertised as a // promise at the RPC protocol level. Once the promise returned by shortenPath() resolves, the // remote client will receive a `Resolve` message updating it to point at the new destination. // // `shortenPath()` can also be used as a hack to shut up the client. If shortenPath() returns // a promise that resolves to an exception, then the client will be notified that the capability // is now broken. Assuming the client is using a correct RPC implemnetation, this should cause // all further calls initiated by the client to this capability to immediately fail client-side, // sparing the server's bandwidth. // // The default implementation always returns nullptr. // TODO(someday): Method which can optionally be overridden to implement Join when the object is // a proxy. protected: inline Capability::Client thisCap(); // Get a capability pointing to this object, much like the `this` keyword. // // The effect of this method is undefined if: // - No capability client has been created pointing to this object. (This is always the case in // the server's constructor.) // - The capability client pointing at this object has been destroyed. (This is always the case // in the server's destructor.) // - Multiple capability clients have been created around the same server (possible if the server // is refcounted, which is not recommended since the client itself provides refcounting). template CallContext internalGetTypedContext( CallContext typeless); template StreamingCallContext internalGetTypedStreamingContext( CallContext typeless); DispatchCallResult internalUnimplemented(const char* actualInterfaceName, uint64_t requestedTypeId); DispatchCallResult internalUnimplemented(const char* interfaceName, uint64_t typeId, uint16_t methodId); kj::Promise internalUnimplemented(const char* interfaceName, const char* methodName, uint64_t typeId, uint16_t methodId); private: ClientHook* thisHook = nullptr; friend class LocalClient; }; // ======================================================================================= class ReaderCapabilityTable: private _::CapTableReader { // Class which imbues Readers with the ability to read capabilities. // // In Cap'n Proto format, the encoding of a capability pointer is simply an integer index into // an external table. Since these pointers fundamentally point outside the message, a // MessageReader by default has no idea what they point at, and therefore reading capabilities // from such a reader will throw exceptions. // // In order to be able to read capabilities, you must first attach a capability table, using // this class. By "imbuing" a Reader, you get a new Reader which will interpret capability // pointers by treating them as indexes into the ReaderCapabilityTable. // // Note that when using Cap'n Proto's RPC system, this is handled automatically. public: explicit ReaderCapabilityTable(kj::Array>> table); KJ_DISALLOW_COPY(ReaderCapabilityTable); template T imbue(T reader); // Return a reader equivalent to `reader` except that when reading capability-valued fields, // the capabilities are looked up in this table. private: kj::Array>> table; kj::Maybe> extractCap(uint index) override; }; class BuilderCapabilityTable: private _::CapTableBuilder { // Class which imbues Builders with the ability to read and write capabilities. // // This is much like ReaderCapabilityTable, except for builders. The table starts out empty, // but capabilities can be added to it over time. public: BuilderCapabilityTable(); KJ_DISALLOW_COPY(BuilderCapabilityTable); inline kj::ArrayPtr>> getTable() { return table; } template T imbue(T builder); // Return a builder equivalent to `builder` except that when reading capability-valued fields, // the capabilities are looked up in this table. private: kj::Vector>> table; kj::Maybe> extractCap(uint index) override; uint injectCap(kj::Own&& cap) override; void dropCap(uint index) override; }; // ======================================================================================= namespace _ { // private class CapabilityServerSetBase { public: Capability::Client addInternal(kj::Own&& server, void* ptr); kj::Promise getLocalServerInternal(Capability::Client& client); }; } // namespace _ (private) template class CapabilityServerSet: private _::CapabilityServerSetBase { // Allows a server to recognize its own capabilities when passed back to it, and obtain the // underlying Server objects associated with them. // // All objects in the set must have the same interface type T. The objects may implement various // interfaces derived from T (and in fact T can be `capnp::Capability` to accept all objects), // but note that if you compile with RTTI disabled then you will not be able to down-cast through // virtual inheritance, and all inheritance between server interfaces is virtual. So, with RTTI // disabled, you will likely need to set T to be the most-derived Cap'n Proto interface type, // and you server class will need to be directly derived from that, so that you can use // static_cast (or kj::downcast) to cast to it after calling getLocalServer(). (If you compile // with RTTI, then you can freely dynamic_cast and ignore this issue!) public: CapabilityServerSet() = default; KJ_DISALLOW_COPY(CapabilityServerSet); typename T::Client add(kj::Own&& server); // Create a new capability Client for the given Server and also add this server to the set. kj::Promise> getLocalServer(typename T::Client& client); // Given a Client pointing to a server previously passed to add(), return the corresponding // Server. This returns a promise because if the input client is itself a promise, this must // wait for it to resolve. Keep in mind that the server will be deleted when all clients are // gone, so the caller should make sure to keep the client alive (hence why this method only // accepts an lvalue input). }; // ======================================================================================= // Hook interfaces which must be implemented by the RPC system. Applications never call these // directly; the RPC system implements them and the types defined earlier in this file wrap them. class RequestHook { // Hook interface implemented by RPC system representing a request being built. public: virtual RemotePromise send() = 0; // Send the call and return a promise for the result. virtual kj::Promise sendStreaming() = 0; // Send a streaming call. virtual const void* getBrand() = 0; // Returns a void* that identifies who made this request. This can be used by an RPC adapter to // discover when tail call is going to be sent over its own connection and therefore can be // optimized into a remote tail call. template inline static kj::Own from(Request&& request) { return kj::mv(request.hook); } }; class ResponseHook { // Hook interface implemented by RPC system representing a response. // // At present this class has no methods. It exists only for garbage collection -- when the // ResponseHook is destroyed, the results can be freed. public: virtual ~ResponseHook() noexcept(false); // Just here to make sure the type is dynamic. template inline static kj::Own from(Response&& response) { return kj::mv(response.hook); } }; // class PipelineHook is declared in any.h because it is needed there. class ClientHook { public: ClientHook(); virtual Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) = 0; // Start a new call, allowing the client to allocate request/response objects as it sees fit. // This version is used when calls are made from application code in the local process. struct VoidPromiseAndPipeline { kj::Promise promise; kj::Own pipeline; }; virtual VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) = 0; // Call the object, but the caller controls allocation of the request/response objects. If the // callee insists on allocating these objects itself, it must make a copy. This version is used // when calls come in over the network via an RPC system. Note that even if the returned // `Promise` is discarded, the call may continue executing if any pipelined calls are // waiting for it. // // Since the caller of this method chooses the CallContext implementation, it is the caller's // responsibility to ensure that the returned promise is not canceled unless allowed via // the context's `allowCancellation()`. // // The call must not begin synchronously; the callee must arrange for the call to begin in a // later turn of the event loop. Otherwise, application code may call back and affect the // callee's state in an unexpected way. virtual kj::Maybe getResolved() = 0; // If this ClientHook is a promise that has already resolved, returns the inner, resolved version // of the capability. The caller may permanently replace this client with the resolved one if // desired. Returns null if the client isn't a promise or hasn't resolved yet -- use // `whenMoreResolved()` to distinguish between them. virtual kj::Maybe>> whenMoreResolved() = 0; // If this client is a settled reference (not a promise), return nullptr. Otherwise, return a // promise that eventually resolves to a new client that is closer to being the final, settled // client (i.e. the value eventually returned by `getResolved()`). Calling this repeatedly // should eventually produce a settled client. kj::Promise whenResolved(); // Repeatedly calls whenMoreResolved() until it returns nullptr. virtual kj::Own addRef() = 0; // Return a new reference to the same capability. virtual const void* getBrand() = 0; // Returns a void* that identifies who made this client. This can be used by an RPC adapter to // discover when a capability it needs to marshal is one that it created in the first place, and // therefore it can transfer the capability without proxying. static const uint NULL_CAPABILITY_BRAND; static const uint BROKEN_CAPABILITY_BRAND; // Values are irrelevant; used for pointers. inline bool isNull() { return getBrand() == &NULL_CAPABILITY_BRAND; } // Returns true if the capability was created as a result of assigning a Client to null or by // reading a null pointer out of a Cap'n Proto message. inline bool isError() { return getBrand() == &BROKEN_CAPABILITY_BRAND; } // Returns true if the capability was created by newBrokenCap(). virtual kj::Maybe getFd() = 0; // Implements Capability::Client::getFd(). If this returns null but whenMoreResolved() returns // non-null, then Capability::Client::getFd() waits for resolution and tries again. static kj::Own from(Capability::Client client) { return kj::mv(client.hook); } }; class CallContextHook { // Hook interface implemented by RPC system to manage a call on the server side. See // CallContext. public: virtual AnyPointer::Reader getParams() = 0; virtual void releaseParams() = 0; virtual AnyPointer::Builder getResults(kj::Maybe sizeHint) = 0; virtual kj::Promise tailCall(kj::Own&& request) = 0; virtual void allowCancellation() = 0; virtual kj::Promise onTailCall() = 0; // If `tailCall()` is called, resolves to the PipelineHook from the tail call. An // implementation of `ClientHook::call()` is allowed to call this at most once. virtual ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own&& request) = 0; // Call this when you would otherwise call onTailCall() immediately followed by tailCall(). // Implementations of tailCall() should typically call directTailCall() and then fulfill the // promise fulfiller for onTailCall() with the returned pipeline. virtual kj::Own addRef() = 0; }; kj::Own newLocalPromiseClient(kj::Promise>&& promise); // Returns a ClientHook that queues up calls until `promise` resolves, then forwards them to // the new client. This hook's `getResolved()` and `whenMoreResolved()` methods will reflect the // redirection to the eventual replacement client. kj::Own newLocalPromisePipeline(kj::Promise>&& promise); // Returns a PipelineHook that queues up calls until `promise` resolves, then forwards them to // the new pipeline. kj::Own newBrokenCap(kj::StringPtr reason); kj::Own newBrokenCap(kj::Exception&& reason); // Helper function that creates a capability which simply throws exceptions when called. kj::Own newBrokenPipeline(kj::Exception&& reason); // Helper function that creates a pipeline which simply throws exceptions when called. Request newBrokenRequest( kj::Exception&& reason, kj::Maybe sizeHint); // Helper function that creates a Request object that simply throws exceptions when sent. // ======================================================================================= // Extend PointerHelpers for interfaces namespace _ { // private template struct PointerHelpers { static inline typename T::Client get(PointerReader reader) { return typename T::Client(reader.getCapability()); } static inline typename T::Client get(PointerBuilder builder) { return typename T::Client(builder.getCapability()); } static inline void set(PointerBuilder builder, typename T::Client&& value) { builder.setCapability(kj::mv(value.Capability::Client::hook)); } static inline void set(PointerBuilder builder, typename T::Client& value) { builder.setCapability(value.Capability::Client::hook->addRef()); } static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder) { return Orphan(builder.disown()); } }; } // namespace _ (private) // ======================================================================================= // Extend List for interfaces template struct List { List() = delete; class Reader { public: typedef List Reads; Reader() = default; inline explicit Reader(_::ListReader reader): reader(reader) {} inline uint size() const { return unbound(reader.size() / ELEMENTS); } inline typename T::Client operator[](uint index) const { KJ_IREQUIRE(index < size()); return typename T::Client(reader.getPointerElement( bounded(index) * ELEMENTS).getCapability()); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } private: _::ListReader reader; template friend struct _::PointerHelpers; template friend struct List; friend class Orphanage; template friend struct ToDynamic_; }; class Builder { public: typedef List Builds; Builder() = delete; inline Builder(decltype(nullptr)) {} inline explicit Builder(_::ListBuilder builder): builder(builder) {} inline operator Reader() const { return Reader(builder.asReader()); } inline Reader asReader() const { return Reader(builder.asReader()); } inline uint size() const { return unbound(builder.size() / ELEMENTS); } inline typename T::Client operator[](uint index) { KJ_IREQUIRE(index < size()); return typename T::Client(builder.getPointerElement( bounded(index) * ELEMENTS).getCapability()); } inline void set(uint index, typename T::Client value) { KJ_IREQUIRE(index < size()); builder.getPointerElement(bounded(index) * ELEMENTS).setCapability(kj::mv(value.hook)); } inline void adopt(uint index, Orphan&& value) { KJ_IREQUIRE(index < size()); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(value)); } inline Orphan disown(uint index) { KJ_IREQUIRE(index < size()); return Orphan(builder.getPointerElement(bounded(index) * ELEMENTS).disown()); } typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } private: _::ListBuilder builder; friend class Orphanage; template friend struct ToDynamic_; }; private: inline static _::ListBuilder initPointer(_::PointerBuilder builder, uint size) { return builder.initList(ElementSize::POINTER, bounded(size) * ELEMENTS); } inline static _::ListBuilder getFromPointer(_::PointerBuilder builder, const word* defaultValue) { return builder.getList(ElementSize::POINTER, defaultValue); } inline static _::ListReader getFromPointer( const _::PointerReader& reader, const word* defaultValue) { return reader.getList(ElementSize::POINTER, defaultValue); } template friend struct List; template friend struct _::PointerHelpers; }; // ======================================================================================= // Inline implementation details template RemotePromise RemotePromise::reducePromise(kj::Promise&& promise) { kj::Tuple>, kj::Promise>> splitPromise = promise.then([](RemotePromise&& inner) { // `inner` is multiply-inherited, and we want to move away each superclass separately. // Let's create two references to make clear what we're doing (though this is not strictly // necessary). kj::Promise>& innerPromise = inner; typename T::Pipeline& innerPipeline = inner; return kj::tuple(kj::mv(innerPromise), PipelineHook::from(kj::mv(innerPipeline))); }).split(); return RemotePromise(kj::mv(kj::get<0>(splitPromise)), typename T::Pipeline(AnyPointer::Pipeline( newLocalPromisePipeline(kj::mv(kj::get<1>(splitPromise)))))); } template RemotePromise Request::send() { auto typelessPromise = hook->send(); hook = nullptr; // prevent reuse // Convert the Promise to return the correct response type. // Explicitly upcast to kj::Promise to make clear that calling .then() doesn't invalidate the // Pipeline part of the RemotePromise. auto typedPromise = kj::implicitCast>&>(typelessPromise) .then([](Response&& response) -> Response { return Response(response.getAs(), kj::mv(response.hook)); }); // Wrap the typeless pipeline in a typed wrapper. typename Results::Pipeline typedPipeline( kj::mv(kj::implicitCast(typelessPromise))); return RemotePromise(kj::mv(typedPromise), kj::mv(typedPipeline)); } template kj::Promise StreamingRequest::send() { auto promise = hook->sendStreaming(); hook = nullptr; // prevent reuse return promise; } inline Capability::Client::Client(kj::Own&& hook): hook(kj::mv(hook)) {} template inline Capability::Client::Client(kj::Own&& server) : hook(makeLocalClient(kj::mv(server))) {} template inline Capability::Client::Client(kj::Promise&& promise) : hook(newLocalPromiseClient(promise.then([](T&& t) { return kj::mv(t.hook); }))) {} inline Capability::Client::Client(Client& other): hook(other.hook->addRef()) {} inline Capability::Client& Capability::Client::operator=(Client& other) { hook = other.hook->addRef(); return *this; } template inline typename T::Client Capability::Client::castAs() { return typename T::Client(hook->addRef()); } inline Request Capability::Client::typelessRequest( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) { return newCall(interfaceId, methodId, sizeHint); } template inline Request Capability::Client::newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) { auto typeless = hook->newCall(interfaceId, methodId, sizeHint); return Request(typeless.template getAs(), kj::mv(typeless.hook)); } template inline StreamingRequest Capability::Client::newStreamingCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) { auto typeless = hook->newCall(interfaceId, methodId, sizeHint); return StreamingRequest(typeless.template getAs(), kj::mv(typeless.hook)); } template inline CallContext::CallContext(CallContextHook& hook): hook(&hook) {} template inline StreamingCallContext::StreamingCallContext(CallContextHook& hook): hook(&hook) {} template inline typename Params::Reader CallContext::getParams() { return hook->getParams().template getAs(); } template inline typename Params::Reader StreamingCallContext::getParams() { return hook->getParams().template getAs(); } template inline void CallContext::releaseParams() { hook->releaseParams(); } template inline void StreamingCallContext::releaseParams() { hook->releaseParams(); } template inline typename Results::Builder CallContext::getResults( kj::Maybe sizeHint) { // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401 return hook->getResults(sizeHint).template getAs(); } template inline typename Results::Builder CallContext::initResults( kj::Maybe sizeHint) { // `template` keyword needed due to: http://llvm.org/bugs/show_bug.cgi?id=17401 return hook->getResults(sizeHint).template initAs(); } template inline void CallContext::setResults(typename Results::Reader value) { hook->getResults(value.totalSize()).template setAs(value); } template inline void CallContext::adoptResults(Orphan&& value) { hook->getResults(nullptr).adopt(kj::mv(value)); } template inline Orphanage CallContext::getResultsOrphanage( kj::Maybe sizeHint) { return Orphanage::getForMessageContaining(hook->getResults(sizeHint)); } template template inline kj::Promise CallContext::tailCall( Request&& tailRequest) { return hook->tailCall(kj::mv(tailRequest.hook)); } template inline void CallContext::allowCancellation() { hook->allowCancellation(); } template inline void StreamingCallContext::allowCancellation() { hook->allowCancellation(); } template CallContext Capability::Server::internalGetTypedContext( CallContext typeless) { return CallContext(*typeless.hook); } template StreamingCallContext Capability::Server::internalGetTypedStreamingContext( CallContext typeless) { return StreamingCallContext(*typeless.hook); } Capability::Client Capability::Server::thisCap() { return Client(thisHook->addRef()); } template T ReaderCapabilityTable::imbue(T reader) { return T(_::PointerHelpers>::getInternalReader(reader).imbue(this)); } template T BuilderCapabilityTable::imbue(T builder) { return T(_::PointerHelpers>::getInternalBuilder(kj::mv(builder)).imbue(this)); } template typename T::Client CapabilityServerSet::add(kj::Own&& server) { void* ptr = reinterpret_cast(server.get()); // Clang insists that `castAs` is a template-dependent member and therefore we need the // `template` keyword here, but AFAICT this is wrong: addImpl() is not a template. return addInternal(kj::mv(server), ptr).template castAs(); } template kj::Promise> CapabilityServerSet::getLocalServer( typename T::Client& client) { return getLocalServerInternal(client) .then([](void* server) -> kj::Maybe { if (server == nullptr) { return nullptr; } else { return *reinterpret_cast(server); } }); } template struct Orphanage::GetInnerReader { static inline kj::Own apply(typename T::Client t) { return ClientHook::from(kj::mv(t)); } }; #define CAPNP_CAPABILITY_H_INCLUDED // for testing includes in unit test } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/membrane.h0000644000175000017500000003375213340402540020604 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // In capability theory, a "membrane" is a wrapper around a capability which (usually) forwards // calls but recursively wraps capabilities in those calls in the same membrane. The purpose of a // membrane is to enforce a barrier between two capabilities that cannot be bypassed by merely // introducing new objects. // // The most common use case for a membrane is revocation: Say Alice wants to give Bob a capability // to access Carol, but wants to be able to revoke this capability later. Alice can accomplish this // by wrapping Carol in a revokable wrapper which passes through calls until such a time as Alice // indicates it should be revoked, after which all calls through the wrapper will throw exceptions. // However, a naive wrapper approach has a problem: if Bob makes a call to Carol and sends a new // capability in that call, or if Carol returns a capability to Bob in the response to a call, then // the two are now able to communicate using this new capability, which Alice cannot revoke. In // order to avoid this problem, Alice must use not just a wrapper but a "membrane", which // recursively wraps all objects that pass through it in either direction. Thus, all connections // formed between Bob and Carol (originating from Alice's original introduction) can be revoked // together by revoking the membrane. // // Note that when a capability is passed into a membrane and then passed back out, the result is // the original capability, not a double-membraned capability. This means that in our revocation // example, if Bob uses his capability to Carol to obtain another capability from her, then send // it back to her, the capability Carol receives back will NOT be revoked when Bob's access to // Carol is revoked. Thus Bob can create long-term irrevocable connections. In most practical use // cases, this is what you want. APIs commonly rely on the fact that a capability obtained and then // passed back can be recognized as the original capability. // // Mark Miller on membranes: http://www.eros-os.org/pipermail/e-lang/2003-January/008434.html #include "capability.h" namespace capnp { class MembranePolicy { // Applications may implement this interface to define a membrane policy, which allows some // calls crossing the membrane to be blocked or redirected. public: virtual kj::Maybe inboundCall( uint64_t interfaceId, uint16_t methodId, Capability::Client target) = 0; // Given an inbound call (a call originating "outside" the membrane destined for an object // "inside" the membrane), decides what to do with it. The policy may: // // - Return null to indicate that the call should proceed to the destination. All capabilities // in the parameters or result will be properly wrapped in the same membrane. // - Return a capability to have the call redirected to that capability. Note that the redirect // capability will be treated as outside the membrane, so the params and results will not be // auto-wrapped; however, the callee can easily wrap the returned capability in the membrane // itself before returning to achieve this effect. // - Throw an exception to cause the call to fail with that exception. // // `target` is the underlying capability (*inside* the membrane) for which the call is destined. // Generally, the only way you should use `target` is to wrap it in some capability which you // return as a redirect. The redirect capability may modify the call in some way and send it to // `target`. Be careful to use `copyIntoMembrane()` and `copyOutOfMembrane()` as appropriate when // copying parameters or results across the membrane. // // Note that since `target` is inside the capability, if you were to directly return it (rather // than return null), the effect would be that the membrane would be broken: the call would // proceed directly and any new capabilities introduced through it would not be membraned. You // generally should not do that. virtual kj::Maybe outboundCall( uint64_t interfaceId, uint16_t methodId, Capability::Client target) = 0; // Like `inboundCall()`, but applies to calls originating *inside* the membrane and terminating // outside. // // Note: It is strongly recommended that `outboundCall()` returns null in exactly the same cases // that `inboundCall()` return null. Conversely, for any case where `inboundCall()` would // redirect or throw, `outboundCall()` should also redirect or throw. Otherwise, you can run // into inconsistent behavion when a promise is returned across a membrane, and that promise // later resolves to a capability on the other side of the membrane: calls on the promise // will enter and then exit the membrane, but calls on the eventual resolution will not cross // the membrane at all, so it is important that these two cases behave the same. virtual kj::Own addRef() = 0; // Return a new owned pointer to the same policy. // // Typically an implementation of MembranePolicy should also inherit kj::Refcounted and implement // `addRef()` as `return kj::addRef(*this);`. // // Note that the membraning system considers two membranes created with the same MembranePolicy // object actually to be the *same* membrane. This is relevant when an object passes into the // membrane and then back out (or out and then back in): instead of double-wrapping the object, // the wrapping will be removed. virtual kj::Maybe> onRevoked() { return nullptr; } // If this returns non-null, then it is a promise that will reject (throw an exception) when the // membrane should be revoked. On revocation, all capabilities pointing across the membrane will // be dropped and all outstanding calls canceled. The exception thrown by the promise will be // propagated to all these calls. It is an error for the promise to resolve without throwing. // // After the revocation promise has rejected, inboundCall() and outboundCall() will still be // invoked for new calls, but the `target` passed to them will be a capability that always // rethrows the revocation exception. // --------------------------------------------------------------------------- // Control over importing and exporting. // // Most membranes should not override these methods. The default behavior is that a capability // that crosses the membrane is wrapped in it, and if the wrapped version crosses back the other // way, it is unwrapped. virtual Capability::Client importExternal(Capability::Client external); // An external capability is crossing into the membrane. Returns the capability that should // substitute for it when called from the inside. // // The default implementation creates a capability that invokes this MembranePolicy. E.g. all // calls will invoke outboundCall(). // // Note that reverseMembrane(cap, policy) normally calls policy->importExternal(cap), unless // `cap` itself was originally returned by the default implementation of exportInternal(), in // which case importInternal() is called instead. virtual Capability::Client exportInternal(Capability::Client internal); // An internal capability is crossing out of the membrane. Returns the capability that should // substitute for it when called from the outside. // // The default implementation creates a capability that invokes this MembranePolicy. E.g. all // calls will invoke inboundCall(). // // Note that membrane(cap, policy) normally calls policy->exportInternal(cap), unless `cap` // itself was originally returned by the default implementation of exportInternal(), in which // case importInternal() is called instead. virtual MembranePolicy& rootPolicy() { return *this; } // If two policies return the same value for rootPolicy(), then a capability imported through // one can be exported through the other, and vice versa. `importInternal()` and // `exportExternal()` will always be called on the root policy, passing the two child policies // as parameters. If you don't override rootPolicy(), then the policy references passed to // importInternal() and exportExternal() will always be references to *this. virtual Capability::Client importInternal( Capability::Client internal, MembranePolicy& exportPolicy, MembranePolicy& importPolicy); // An internal capability which was previously exported is now being re-imported, i.e. a // capability passed out of the membrane and then back in. // // The default implementation simply returns `internal`. virtual Capability::Client exportExternal( Capability::Client external, MembranePolicy& importPolicy, MembranePolicy& exportPolicy); // An external capability which was previously imported is now being re-exported, i.e. a // capability passed into the membrane and then back out. // // The default implementation simply returns `external`. }; Capability::Client membrane(Capability::Client inner, kj::Own policy); // Wrap `inner` in a membrane specified by `policy`. `inner` is considered "inside" the membrane, // while the returned capability should only be called from outside the membrane. Capability::Client reverseMembrane(Capability::Client outer, kj::Own policy); // Like `membrane` but treat the input capability as "outside" the membrane, and return a // capability appropriate for use inside. // // Applications typically won't use this directly; the membraning code automatically sets up // reverse membranes where needed. template ClientType membrane(ClientType inner, kj::Own policy); template ClientType reverseMembrane(ClientType inner, kj::Own policy); // Convenience templates which return the same interface type as the input. template typename ServerType::Serves::Client membrane( kj::Own inner, kj::Own policy); template typename ServerType::Serves::Client reverseMembrane( kj::Own inner, kj::Own policy); // Convenience templates which input a capability server type and return the appropriate client // type. template Orphan::Reads> copyIntoMembrane( Reader&& from, Orphanage to, kj::Own policy); // Copy a Cap'n Proto object (e.g. struct or list), adding the given membrane to any capabilities // found within it. `from` is interpreted as "outside" the membrane while `to` is "inside". template Orphan::Reads> copyOutOfMembrane( Reader&& from, Orphanage to, kj::Own policy); // Like copyIntoMembrane() except that `from` is "inside" the membrane and `to` is "outside". // ======================================================================================= // inline implementation details template ClientType membrane(ClientType inner, kj::Own policy) { return membrane(Capability::Client(kj::mv(inner)), kj::mv(policy)) .castAs(); } template ClientType reverseMembrane(ClientType inner, kj::Own policy) { return reverseMembrane(Capability::Client(kj::mv(inner)), kj::mv(policy)) .castAs(); } template typename ServerType::Serves::Client membrane( kj::Own inner, kj::Own policy) { return membrane(Capability::Client(kj::mv(inner)), kj::mv(policy)) .castAs(); } template typename ServerType::Serves::Client reverseMembrane( kj::Own inner, kj::Own policy) { return reverseMembrane(Capability::Client(kj::mv(inner)), kj::mv(policy)) .castAs(); } namespace _ { // private OrphanBuilder copyOutOfMembrane(PointerReader from, Orphanage to, kj::Own policy, bool reverse); OrphanBuilder copyOutOfMembrane(StructReader from, Orphanage to, kj::Own policy, bool reverse); OrphanBuilder copyOutOfMembrane(ListReader from, Orphanage to, kj::Own policy, bool reverse); } // namespace _ (private) template Orphan::Reads> copyIntoMembrane( Reader&& from, Orphanage to, kj::Own policy) { return _::copyOutOfMembrane( _::PointerHelpers::Reads>::getInternalReader(from), to, kj::mv(policy), true); } template Orphan::Reads> copyOutOfMembrane( Reader&& from, Orphanage to, kj::Own policy) { return _::copyOutOfMembrane( _::PointerHelpers::Reads>::getInternalReader(from), to, kj::mv(policy), false); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema.capnp.h0000644000175000017500000114705113650317744021374 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: schema.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(e682ab4cf923a417); CAPNP_DECLARE_SCHEMA(b9521bccf10fa3b1); CAPNP_DECLARE_SCHEMA(debf55bbfa0fc242); CAPNP_DECLARE_SCHEMA(f38e1de3041357ae); CAPNP_DECLARE_SCHEMA(c2ba9038898e1fa2); CAPNP_DECLARE_SCHEMA(9ea0b19b37fb4435); CAPNP_DECLARE_SCHEMA(b54ab3364333f598); CAPNP_DECLARE_SCHEMA(e82753cff0c2218f); CAPNP_DECLARE_SCHEMA(b18aa5ac7a0d9420); CAPNP_DECLARE_SCHEMA(ec1619d4400a0290); CAPNP_DECLARE_SCHEMA(9aad50a41f4af45f); CAPNP_DECLARE_SCHEMA(97b14cbe7cfec712); CAPNP_DECLARE_SCHEMA(c42305476bb4746f); CAPNP_DECLARE_SCHEMA(cafccddb68db1d11); CAPNP_DECLARE_SCHEMA(bb90d5c287870be6); CAPNP_DECLARE_SCHEMA(978a7cebdc549a4d); CAPNP_DECLARE_SCHEMA(a9962a9ed0a4d7f8); CAPNP_DECLARE_SCHEMA(9500cce23b334d80); CAPNP_DECLARE_SCHEMA(d07378ede1f9cc60); CAPNP_DECLARE_SCHEMA(87e739250a60ea97); CAPNP_DECLARE_SCHEMA(9e0e78711a7f87a9); CAPNP_DECLARE_SCHEMA(ac3a6f60ef4cc6d3); CAPNP_DECLARE_SCHEMA(ed8bca69f7fb0cbf); CAPNP_DECLARE_SCHEMA(c2573fe8a23e49f1); CAPNP_DECLARE_SCHEMA(8e3b5f79fe593656); CAPNP_DECLARE_SCHEMA(9dd1f724f4614a85); CAPNP_DECLARE_SCHEMA(baefc9120c56e274); CAPNP_DECLARE_SCHEMA(903455f06065422b); CAPNP_DECLARE_SCHEMA(abd73485a9636bc9); CAPNP_DECLARE_SCHEMA(c863cd16969ee7fc); CAPNP_DECLARE_SCHEMA(ce23dcd2d7b00c9b); CAPNP_DECLARE_SCHEMA(f1c8950dab257542); CAPNP_DECLARE_SCHEMA(d1958f7dba521926); enum class ElementSize_d1958f7dba521926: uint16_t { EMPTY, BIT, BYTE, TWO_BYTES, FOUR_BYTES, EIGHT_BYTES, POINTER, INLINE_COMPOSITE, }; CAPNP_DECLARE_ENUM(ElementSize, d1958f7dba521926); CAPNP_DECLARE_SCHEMA(d85d305b7d839963); CAPNP_DECLARE_SCHEMA(bfc546f6210ad7ce); CAPNP_DECLARE_SCHEMA(cfea0eb02e810062); CAPNP_DECLARE_SCHEMA(ae504193122357e5); } // namespace schemas } // namespace capnp namespace capnp { namespace schema { struct Node { Node() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { FILE, STRUCT, ENUM, INTERFACE, CONST, ANNOTATION, }; struct Parameter; struct NestedNode; struct SourceInfo; struct Struct; struct Enum; struct Interface; struct Const; struct Annotation; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e682ab4cf923a417, 5, 6) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::Parameter { Parameter() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b9521bccf10fa3b1, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::NestedNode { NestedNode() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(debf55bbfa0fc242, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::SourceInfo { SourceInfo() = delete; class Reader; class Builder; class Pipeline; struct Member; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(f38e1de3041357ae, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::SourceInfo::Member { Member() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c2ba9038898e1fa2, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::Struct { Struct() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9ea0b19b37fb4435, 5, 6) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::Enum { Enum() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b54ab3364333f598, 5, 6) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::Interface { Interface() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e82753cff0c2218f, 5, 6) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::Const { Const() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b18aa5ac7a0d9420, 5, 6) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Node::Annotation { Annotation() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ec1619d4400a0290, 5, 6) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Field { Field() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { SLOT, GROUP, }; static constexpr ::uint16_t NO_DISCRIMINANT = 65535u; struct Slot; struct Group; struct Ordinal; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9aad50a41f4af45f, 3, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Field::Slot { Slot() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c42305476bb4746f, 3, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Field::Group { Group() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(cafccddb68db1d11, 3, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Field::Ordinal { Ordinal() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { IMPLICIT, EXPLICIT, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(bb90d5c287870be6, 3, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Enumerant { Enumerant() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(978a7cebdc549a4d, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Superclass { Superclass() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(a9962a9ed0a4d7f8, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Method { Method() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9500cce23b334d80, 3, 5) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type { Type() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { VOID, BOOL, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64, FLOAT32, FLOAT64, TEXT, DATA, LIST, ENUM, STRUCT, INTERFACE, ANY_POINTER, }; struct List; struct Enum; struct Struct; struct Interface; struct AnyPointer; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d07378ede1f9cc60, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::List { List() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(87e739250a60ea97, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::Enum { Enum() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9e0e78711a7f87a9, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::Struct { Struct() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ac3a6f60ef4cc6d3, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::Interface { Interface() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ed8bca69f7fb0cbf, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::AnyPointer { AnyPointer() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { UNCONSTRAINED, PARAMETER, IMPLICIT_METHOD_PARAMETER, }; struct Unconstrained; struct Parameter; struct ImplicitMethodParameter; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c2573fe8a23e49f1, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::AnyPointer::Unconstrained { Unconstrained() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { ANY_KIND, STRUCT, LIST, CAPABILITY, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(8e3b5f79fe593656, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::AnyPointer::Parameter { Parameter() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9dd1f724f4614a85, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Type::AnyPointer::ImplicitMethodParameter { ImplicitMethodParameter() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(baefc9120c56e274, 3, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Brand { Brand() = delete; class Reader; class Builder; class Pipeline; struct Scope; struct Binding; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(903455f06065422b, 0, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Brand::Scope { Scope() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { BIND, INHERIT, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(abd73485a9636bc9, 2, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Brand::Binding { Binding() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { UNBOUND, TYPE, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(c863cd16969ee7fc, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Value { Value() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { VOID, BOOL, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64, FLOAT32, FLOAT64, TEXT, DATA, LIST, ENUM, STRUCT, INTERFACE, ANY_POINTER, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ce23dcd2d7b00c9b, 2, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Annotation { Annotation() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(f1c8950dab257542, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; typedef ::capnp::schemas::ElementSize_d1958f7dba521926 ElementSize; struct CapnpVersion { CapnpVersion() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d85d305b7d839963, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct CodeGeneratorRequest { CodeGeneratorRequest() = delete; class Reader; class Builder; class Pipeline; struct RequestedFile; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(bfc546f6210ad7ce, 0, 4) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct CodeGeneratorRequest::RequestedFile { RequestedFile() = delete; class Reader; class Builder; class Pipeline; struct Import; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(cfea0eb02e810062, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct CodeGeneratorRequest::RequestedFile::Import { Import() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ae504193122357e5, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class Node::Reader { public: typedef Node Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline ::uint64_t getId() const; inline bool hasDisplayName() const; inline ::capnp::Text::Reader getDisplayName() const; inline ::uint32_t getDisplayNamePrefixLength() const; inline ::uint64_t getScopeId() const; inline bool hasNestedNodes() const; inline ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Reader getNestedNodes() const; inline bool hasAnnotations() const; inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader getAnnotations() const; inline bool isFile() const; inline ::capnp::Void getFile() const; inline bool isStruct() const; inline typename Struct::Reader getStruct() const; inline bool isEnum() const; inline typename Enum::Reader getEnum() const; inline bool isInterface() const; inline typename Interface::Reader getInterface() const; inline bool isConst() const; inline typename Const::Reader getConst() const; inline bool isAnnotation() const; inline typename Annotation::Reader getAnnotation() const; inline bool hasParameters() const; inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader getParameters() const; inline bool getIsGeneric() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Builder { public: typedef Node Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline ::uint64_t getId(); inline void setId( ::uint64_t value); inline bool hasDisplayName(); inline ::capnp::Text::Builder getDisplayName(); inline void setDisplayName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initDisplayName(unsigned int size); inline void adoptDisplayName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownDisplayName(); inline ::uint32_t getDisplayNamePrefixLength(); inline void setDisplayNamePrefixLength( ::uint32_t value); inline ::uint64_t getScopeId(); inline void setScopeId( ::uint64_t value); inline bool hasNestedNodes(); inline ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Builder getNestedNodes(); inline void setNestedNodes( ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Builder initNestedNodes(unsigned int size); inline void adoptNestedNodes(::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>> disownNestedNodes(); inline bool hasAnnotations(); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder getAnnotations(); inline void setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder initAnnotations(unsigned int size); inline void adoptAnnotations(::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> disownAnnotations(); inline bool isFile(); inline ::capnp::Void getFile(); inline void setFile( ::capnp::Void value = ::capnp::VOID); inline bool isStruct(); inline typename Struct::Builder getStruct(); inline typename Struct::Builder initStruct(); inline bool isEnum(); inline typename Enum::Builder getEnum(); inline typename Enum::Builder initEnum(); inline bool isInterface(); inline typename Interface::Builder getInterface(); inline typename Interface::Builder initInterface(); inline bool isConst(); inline typename Const::Builder getConst(); inline typename Const::Builder initConst(); inline bool isAnnotation(); inline typename Annotation::Builder getAnnotation(); inline typename Annotation::Builder initAnnotation(); inline bool hasParameters(); inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder getParameters(); inline void setParameters( ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder initParameters(unsigned int size); inline void adoptParameters(::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>> disownParameters(); inline bool getIsGeneric(); inline void setIsGeneric(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Pipeline { public: typedef Node Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::Parameter::Reader { public: typedef Parameter Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Parameter::Builder { public: typedef Parameter Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Parameter::Pipeline { public: typedef Parameter Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::NestedNode::Reader { public: typedef NestedNode Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline ::uint64_t getId() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::NestedNode::Builder { public: typedef NestedNode Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline ::uint64_t getId(); inline void setId( ::uint64_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::NestedNode::Pipeline { public: typedef NestedNode Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::SourceInfo::Reader { public: typedef SourceInfo Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getId() const; inline bool hasDocComment() const; inline ::capnp::Text::Reader getDocComment() const; inline bool hasMembers() const; inline ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Reader getMembers() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::SourceInfo::Builder { public: typedef SourceInfo Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getId(); inline void setId( ::uint64_t value); inline bool hasDocComment(); inline ::capnp::Text::Builder getDocComment(); inline void setDocComment( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initDocComment(unsigned int size); inline void adoptDocComment(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownDocComment(); inline bool hasMembers(); inline ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Builder getMembers(); inline void setMembers( ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Builder initMembers(unsigned int size); inline void adoptMembers(::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>> disownMembers(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::SourceInfo::Pipeline { public: typedef SourceInfo Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::SourceInfo::Member::Reader { public: typedef Member Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasDocComment() const; inline ::capnp::Text::Reader getDocComment() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::SourceInfo::Member::Builder { public: typedef Member Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasDocComment(); inline ::capnp::Text::Builder getDocComment(); inline void setDocComment( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initDocComment(unsigned int size); inline void adoptDocComment(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownDocComment(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::SourceInfo::Member::Pipeline { public: typedef Member Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::Struct::Reader { public: typedef Struct Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint16_t getDataWordCount() const; inline ::uint16_t getPointerCount() const; inline ::capnp::schema::ElementSize getPreferredListEncoding() const; inline bool getIsGroup() const; inline ::uint16_t getDiscriminantCount() const; inline ::uint32_t getDiscriminantOffset() const; inline bool hasFields() const; inline ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Reader getFields() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Struct::Builder { public: typedef Struct Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint16_t getDataWordCount(); inline void setDataWordCount( ::uint16_t value); inline ::uint16_t getPointerCount(); inline void setPointerCount( ::uint16_t value); inline ::capnp::schema::ElementSize getPreferredListEncoding(); inline void setPreferredListEncoding( ::capnp::schema::ElementSize value); inline bool getIsGroup(); inline void setIsGroup(bool value); inline ::uint16_t getDiscriminantCount(); inline void setDiscriminantCount( ::uint16_t value); inline ::uint32_t getDiscriminantOffset(); inline void setDiscriminantOffset( ::uint32_t value); inline bool hasFields(); inline ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Builder getFields(); inline void setFields( ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Builder initFields(unsigned int size); inline void adoptFields(::capnp::Orphan< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>> disownFields(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Struct::Pipeline { public: typedef Struct Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::Enum::Reader { public: typedef Enum Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasEnumerants() const; inline ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Reader getEnumerants() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Enum::Builder { public: typedef Enum Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasEnumerants(); inline ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Builder getEnumerants(); inline void setEnumerants( ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Builder initEnumerants(unsigned int size); inline void adoptEnumerants(::capnp::Orphan< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>> disownEnumerants(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Enum::Pipeline { public: typedef Enum Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::Interface::Reader { public: typedef Interface Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasMethods() const; inline ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Reader getMethods() const; inline bool hasSuperclasses() const; inline ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Reader getSuperclasses() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Interface::Builder { public: typedef Interface Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasMethods(); inline ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Builder getMethods(); inline void setMethods( ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Builder initMethods(unsigned int size); inline void adoptMethods(::capnp::Orphan< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>> disownMethods(); inline bool hasSuperclasses(); inline ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Builder getSuperclasses(); inline void setSuperclasses( ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Builder initSuperclasses(unsigned int size); inline void adoptSuperclasses(::capnp::Orphan< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>> disownSuperclasses(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Interface::Pipeline { public: typedef Interface Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::Const::Reader { public: typedef Const Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasType() const; inline ::capnp::schema::Type::Reader getType() const; inline bool hasValue() const; inline ::capnp::schema::Value::Reader getValue() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Const::Builder { public: typedef Const Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasType(); inline ::capnp::schema::Type::Builder getType(); inline void setType( ::capnp::schema::Type::Reader value); inline ::capnp::schema::Type::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::schema::Type>&& value); inline ::capnp::Orphan< ::capnp::schema::Type> disownType(); inline bool hasValue(); inline ::capnp::schema::Value::Builder getValue(); inline void setValue( ::capnp::schema::Value::Reader value); inline ::capnp::schema::Value::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::schema::Value>&& value); inline ::capnp::Orphan< ::capnp::schema::Value> disownValue(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Const::Pipeline { public: typedef Const Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Type::Pipeline getType(); inline ::capnp::schema::Value::Pipeline getValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Node::Annotation::Reader { public: typedef Annotation Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasType() const; inline ::capnp::schema::Type::Reader getType() const; inline bool getTargetsFile() const; inline bool getTargetsConst() const; inline bool getTargetsEnum() const; inline bool getTargetsEnumerant() const; inline bool getTargetsStruct() const; inline bool getTargetsField() const; inline bool getTargetsUnion() const; inline bool getTargetsGroup() const; inline bool getTargetsInterface() const; inline bool getTargetsMethod() const; inline bool getTargetsParam() const; inline bool getTargetsAnnotation() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Node::Annotation::Builder { public: typedef Annotation Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasType(); inline ::capnp::schema::Type::Builder getType(); inline void setType( ::capnp::schema::Type::Reader value); inline ::capnp::schema::Type::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::schema::Type>&& value); inline ::capnp::Orphan< ::capnp::schema::Type> disownType(); inline bool getTargetsFile(); inline void setTargetsFile(bool value); inline bool getTargetsConst(); inline void setTargetsConst(bool value); inline bool getTargetsEnum(); inline void setTargetsEnum(bool value); inline bool getTargetsEnumerant(); inline void setTargetsEnumerant(bool value); inline bool getTargetsStruct(); inline void setTargetsStruct(bool value); inline bool getTargetsField(); inline void setTargetsField(bool value); inline bool getTargetsUnion(); inline void setTargetsUnion(bool value); inline bool getTargetsGroup(); inline void setTargetsGroup(bool value); inline bool getTargetsInterface(); inline void setTargetsInterface(bool value); inline bool getTargetsMethod(); inline void setTargetsMethod(bool value); inline bool getTargetsParam(); inline void setTargetsParam(bool value); inline bool getTargetsAnnotation(); inline void setTargetsAnnotation(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Node::Annotation::Pipeline { public: typedef Annotation Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Type::Pipeline getType(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Field::Reader { public: typedef Field Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline ::uint16_t getCodeOrder() const; inline bool hasAnnotations() const; inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader getAnnotations() const; inline ::uint16_t getDiscriminantValue() const; inline bool isSlot() const; inline typename Slot::Reader getSlot() const; inline bool isGroup() const; inline typename Group::Reader getGroup() const; inline typename Ordinal::Reader getOrdinal() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Field::Builder { public: typedef Field Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline ::uint16_t getCodeOrder(); inline void setCodeOrder( ::uint16_t value); inline bool hasAnnotations(); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder getAnnotations(); inline void setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder initAnnotations(unsigned int size); inline void adoptAnnotations(::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> disownAnnotations(); inline ::uint16_t getDiscriminantValue(); inline void setDiscriminantValue( ::uint16_t value); inline bool isSlot(); inline typename Slot::Builder getSlot(); inline typename Slot::Builder initSlot(); inline bool isGroup(); inline typename Group::Builder getGroup(); inline typename Group::Builder initGroup(); inline typename Ordinal::Builder getOrdinal(); inline typename Ordinal::Builder initOrdinal(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Field::Pipeline { public: typedef Field Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline typename Ordinal::Pipeline getOrdinal(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Field::Slot::Reader { public: typedef Slot Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getOffset() const; inline bool hasType() const; inline ::capnp::schema::Type::Reader getType() const; inline bool hasDefaultValue() const; inline ::capnp::schema::Value::Reader getDefaultValue() const; inline bool getHadExplicitDefault() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Field::Slot::Builder { public: typedef Slot Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getOffset(); inline void setOffset( ::uint32_t value); inline bool hasType(); inline ::capnp::schema::Type::Builder getType(); inline void setType( ::capnp::schema::Type::Reader value); inline ::capnp::schema::Type::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::schema::Type>&& value); inline ::capnp::Orphan< ::capnp::schema::Type> disownType(); inline bool hasDefaultValue(); inline ::capnp::schema::Value::Builder getDefaultValue(); inline void setDefaultValue( ::capnp::schema::Value::Reader value); inline ::capnp::schema::Value::Builder initDefaultValue(); inline void adoptDefaultValue(::capnp::Orphan< ::capnp::schema::Value>&& value); inline ::capnp::Orphan< ::capnp::schema::Value> disownDefaultValue(); inline bool getHadExplicitDefault(); inline void setHadExplicitDefault(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Field::Slot::Pipeline { public: typedef Slot Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Type::Pipeline getType(); inline ::capnp::schema::Value::Pipeline getDefaultValue(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Field::Group::Reader { public: typedef Group Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Field::Group::Builder { public: typedef Group Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId(); inline void setTypeId( ::uint64_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Field::Group::Pipeline { public: typedef Group Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Field::Ordinal::Reader { public: typedef Ordinal Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isImplicit() const; inline ::capnp::Void getImplicit() const; inline bool isExplicit() const; inline ::uint16_t getExplicit() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Field::Ordinal::Builder { public: typedef Ordinal Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isImplicit(); inline ::capnp::Void getImplicit(); inline void setImplicit( ::capnp::Void value = ::capnp::VOID); inline bool isExplicit(); inline ::uint16_t getExplicit(); inline void setExplicit( ::uint16_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Field::Ordinal::Pipeline { public: typedef Ordinal Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Enumerant::Reader { public: typedef Enumerant Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline ::uint16_t getCodeOrder() const; inline bool hasAnnotations() const; inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader getAnnotations() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Enumerant::Builder { public: typedef Enumerant Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline ::uint16_t getCodeOrder(); inline void setCodeOrder( ::uint16_t value); inline bool hasAnnotations(); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder getAnnotations(); inline void setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder initAnnotations(unsigned int size); inline void adoptAnnotations(::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> disownAnnotations(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Enumerant::Pipeline { public: typedef Enumerant Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Superclass::Reader { public: typedef Superclass Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getId() const; inline bool hasBrand() const; inline ::capnp::schema::Brand::Reader getBrand() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Superclass::Builder { public: typedef Superclass Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getId(); inline void setId( ::uint64_t value); inline bool hasBrand(); inline ::capnp::schema::Brand::Builder getBrand(); inline void setBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initBrand(); inline void adoptBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownBrand(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Superclass::Pipeline { public: typedef Superclass Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Brand::Pipeline getBrand(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Method::Reader { public: typedef Method Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasName() const; inline ::capnp::Text::Reader getName() const; inline ::uint16_t getCodeOrder() const; inline ::uint64_t getParamStructType() const; inline ::uint64_t getResultStructType() const; inline bool hasAnnotations() const; inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader getAnnotations() const; inline bool hasParamBrand() const; inline ::capnp::schema::Brand::Reader getParamBrand() const; inline bool hasResultBrand() const; inline ::capnp::schema::Brand::Reader getResultBrand() const; inline bool hasImplicitParameters() const; inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader getImplicitParameters() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Method::Builder { public: typedef Method Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); inline ::uint16_t getCodeOrder(); inline void setCodeOrder( ::uint16_t value); inline ::uint64_t getParamStructType(); inline void setParamStructType( ::uint64_t value); inline ::uint64_t getResultStructType(); inline void setResultStructType( ::uint64_t value); inline bool hasAnnotations(); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder getAnnotations(); inline void setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder initAnnotations(unsigned int size); inline void adoptAnnotations(::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> disownAnnotations(); inline bool hasParamBrand(); inline ::capnp::schema::Brand::Builder getParamBrand(); inline void setParamBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initParamBrand(); inline void adoptParamBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownParamBrand(); inline bool hasResultBrand(); inline ::capnp::schema::Brand::Builder getResultBrand(); inline void setResultBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initResultBrand(); inline void adoptResultBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownResultBrand(); inline bool hasImplicitParameters(); inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder getImplicitParameters(); inline void setImplicitParameters( ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder initImplicitParameters(unsigned int size); inline void adoptImplicitParameters(::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>> disownImplicitParameters(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Method::Pipeline { public: typedef Method Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Brand::Pipeline getParamBrand(); inline ::capnp::schema::Brand::Pipeline getResultBrand(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::Reader { public: typedef Type Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isVoid() const; inline ::capnp::Void getVoid() const; inline bool isBool() const; inline ::capnp::Void getBool() const; inline bool isInt8() const; inline ::capnp::Void getInt8() const; inline bool isInt16() const; inline ::capnp::Void getInt16() const; inline bool isInt32() const; inline ::capnp::Void getInt32() const; inline bool isInt64() const; inline ::capnp::Void getInt64() const; inline bool isUint8() const; inline ::capnp::Void getUint8() const; inline bool isUint16() const; inline ::capnp::Void getUint16() const; inline bool isUint32() const; inline ::capnp::Void getUint32() const; inline bool isUint64() const; inline ::capnp::Void getUint64() const; inline bool isFloat32() const; inline ::capnp::Void getFloat32() const; inline bool isFloat64() const; inline ::capnp::Void getFloat64() const; inline bool isText() const; inline ::capnp::Void getText() const; inline bool isData() const; inline ::capnp::Void getData() const; inline bool isList() const; inline typename List::Reader getList() const; inline bool isEnum() const; inline typename Enum::Reader getEnum() const; inline bool isStruct() const; inline typename Struct::Reader getStruct() const; inline bool isInterface() const; inline typename Interface::Reader getInterface() const; inline bool isAnyPointer() const; inline typename AnyPointer::Reader getAnyPointer() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::Builder { public: typedef Type Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isVoid(); inline ::capnp::Void getVoid(); inline void setVoid( ::capnp::Void value = ::capnp::VOID); inline bool isBool(); inline ::capnp::Void getBool(); inline void setBool( ::capnp::Void value = ::capnp::VOID); inline bool isInt8(); inline ::capnp::Void getInt8(); inline void setInt8( ::capnp::Void value = ::capnp::VOID); inline bool isInt16(); inline ::capnp::Void getInt16(); inline void setInt16( ::capnp::Void value = ::capnp::VOID); inline bool isInt32(); inline ::capnp::Void getInt32(); inline void setInt32( ::capnp::Void value = ::capnp::VOID); inline bool isInt64(); inline ::capnp::Void getInt64(); inline void setInt64( ::capnp::Void value = ::capnp::VOID); inline bool isUint8(); inline ::capnp::Void getUint8(); inline void setUint8( ::capnp::Void value = ::capnp::VOID); inline bool isUint16(); inline ::capnp::Void getUint16(); inline void setUint16( ::capnp::Void value = ::capnp::VOID); inline bool isUint32(); inline ::capnp::Void getUint32(); inline void setUint32( ::capnp::Void value = ::capnp::VOID); inline bool isUint64(); inline ::capnp::Void getUint64(); inline void setUint64( ::capnp::Void value = ::capnp::VOID); inline bool isFloat32(); inline ::capnp::Void getFloat32(); inline void setFloat32( ::capnp::Void value = ::capnp::VOID); inline bool isFloat64(); inline ::capnp::Void getFloat64(); inline void setFloat64( ::capnp::Void value = ::capnp::VOID); inline bool isText(); inline ::capnp::Void getText(); inline void setText( ::capnp::Void value = ::capnp::VOID); inline bool isData(); inline ::capnp::Void getData(); inline void setData( ::capnp::Void value = ::capnp::VOID); inline bool isList(); inline typename List::Builder getList(); inline typename List::Builder initList(); inline bool isEnum(); inline typename Enum::Builder getEnum(); inline typename Enum::Builder initEnum(); inline bool isStruct(); inline typename Struct::Builder getStruct(); inline typename Struct::Builder initStruct(); inline bool isInterface(); inline typename Interface::Builder getInterface(); inline typename Interface::Builder initInterface(); inline bool isAnyPointer(); inline typename AnyPointer::Builder getAnyPointer(); inline typename AnyPointer::Builder initAnyPointer(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::Pipeline { public: typedef Type Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::List::Reader { public: typedef List Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasElementType() const; inline ::capnp::schema::Type::Reader getElementType() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::List::Builder { public: typedef List Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasElementType(); inline ::capnp::schema::Type::Builder getElementType(); inline void setElementType( ::capnp::schema::Type::Reader value); inline ::capnp::schema::Type::Builder initElementType(); inline void adoptElementType(::capnp::Orphan< ::capnp::schema::Type>&& value); inline ::capnp::Orphan< ::capnp::schema::Type> disownElementType(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::List::Pipeline { public: typedef List Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Type::Pipeline getElementType(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::Enum::Reader { public: typedef Enum Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId() const; inline bool hasBrand() const; inline ::capnp::schema::Brand::Reader getBrand() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::Enum::Builder { public: typedef Enum Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId(); inline void setTypeId( ::uint64_t value); inline bool hasBrand(); inline ::capnp::schema::Brand::Builder getBrand(); inline void setBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initBrand(); inline void adoptBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownBrand(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::Enum::Pipeline { public: typedef Enum Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Brand::Pipeline getBrand(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::Struct::Reader { public: typedef Struct Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId() const; inline bool hasBrand() const; inline ::capnp::schema::Brand::Reader getBrand() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::Struct::Builder { public: typedef Struct Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId(); inline void setTypeId( ::uint64_t value); inline bool hasBrand(); inline ::capnp::schema::Brand::Builder getBrand(); inline void setBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initBrand(); inline void adoptBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownBrand(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::Struct::Pipeline { public: typedef Struct Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Brand::Pipeline getBrand(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::Interface::Reader { public: typedef Interface Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId() const; inline bool hasBrand() const; inline ::capnp::schema::Brand::Reader getBrand() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::Interface::Builder { public: typedef Interface Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getTypeId(); inline void setTypeId( ::uint64_t value); inline bool hasBrand(); inline ::capnp::schema::Brand::Builder getBrand(); inline void setBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initBrand(); inline void adoptBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownBrand(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::Interface::Pipeline { public: typedef Interface Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Brand::Pipeline getBrand(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::AnyPointer::Reader { public: typedef AnyPointer Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isUnconstrained() const; inline typename Unconstrained::Reader getUnconstrained() const; inline bool isParameter() const; inline typename Parameter::Reader getParameter() const; inline bool isImplicitMethodParameter() const; inline typename ImplicitMethodParameter::Reader getImplicitMethodParameter() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::AnyPointer::Builder { public: typedef AnyPointer Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isUnconstrained(); inline typename Unconstrained::Builder getUnconstrained(); inline typename Unconstrained::Builder initUnconstrained(); inline bool isParameter(); inline typename Parameter::Builder getParameter(); inline typename Parameter::Builder initParameter(); inline bool isImplicitMethodParameter(); inline typename ImplicitMethodParameter::Builder getImplicitMethodParameter(); inline typename ImplicitMethodParameter::Builder initImplicitMethodParameter(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::AnyPointer::Pipeline { public: typedef AnyPointer Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::AnyPointer::Unconstrained::Reader { public: typedef Unconstrained Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isAnyKind() const; inline ::capnp::Void getAnyKind() const; inline bool isStruct() const; inline ::capnp::Void getStruct() const; inline bool isList() const; inline ::capnp::Void getList() const; inline bool isCapability() const; inline ::capnp::Void getCapability() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::AnyPointer::Unconstrained::Builder { public: typedef Unconstrained Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isAnyKind(); inline ::capnp::Void getAnyKind(); inline void setAnyKind( ::capnp::Void value = ::capnp::VOID); inline bool isStruct(); inline ::capnp::Void getStruct(); inline void setStruct( ::capnp::Void value = ::capnp::VOID); inline bool isList(); inline ::capnp::Void getList(); inline void setList( ::capnp::Void value = ::capnp::VOID); inline bool isCapability(); inline ::capnp::Void getCapability(); inline void setCapability( ::capnp::Void value = ::capnp::VOID); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::AnyPointer::Unconstrained::Pipeline { public: typedef Unconstrained Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::AnyPointer::Parameter::Reader { public: typedef Parameter Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getScopeId() const; inline ::uint16_t getParameterIndex() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::AnyPointer::Parameter::Builder { public: typedef Parameter Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getScopeId(); inline void setScopeId( ::uint64_t value); inline ::uint16_t getParameterIndex(); inline void setParameterIndex( ::uint16_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::AnyPointer::Parameter::Pipeline { public: typedef Parameter Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Type::AnyPointer::ImplicitMethodParameter::Reader { public: typedef ImplicitMethodParameter Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint16_t getParameterIndex() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Type::AnyPointer::ImplicitMethodParameter::Builder { public: typedef ImplicitMethodParameter Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint16_t getParameterIndex(); inline void setParameterIndex( ::uint16_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Type::AnyPointer::ImplicitMethodParameter::Pipeline { public: typedef ImplicitMethodParameter Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Brand::Reader { public: typedef Brand Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasScopes() const; inline ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Reader getScopes() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Brand::Builder { public: typedef Brand Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasScopes(); inline ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Builder getScopes(); inline void setScopes( ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Builder initScopes(unsigned int size); inline void adoptScopes(::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>> disownScopes(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Brand::Pipeline { public: typedef Brand Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Brand::Scope::Reader { public: typedef Scope Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline ::uint64_t getScopeId() const; inline bool isBind() const; inline bool hasBind() const; inline ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Reader getBind() const; inline bool isInherit() const; inline ::capnp::Void getInherit() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Brand::Scope::Builder { public: typedef Scope Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline ::uint64_t getScopeId(); inline void setScopeId( ::uint64_t value); inline bool isBind(); inline bool hasBind(); inline ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Builder getBind(); inline void setBind( ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Builder initBind(unsigned int size); inline void adoptBind(::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>> disownBind(); inline bool isInherit(); inline ::capnp::Void getInherit(); inline void setInherit( ::capnp::Void value = ::capnp::VOID); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Brand::Scope::Pipeline { public: typedef Scope Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Brand::Binding::Reader { public: typedef Binding Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isUnbound() const; inline ::capnp::Void getUnbound() const; inline bool isType() const; inline bool hasType() const; inline ::capnp::schema::Type::Reader getType() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Brand::Binding::Builder { public: typedef Binding Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isUnbound(); inline ::capnp::Void getUnbound(); inline void setUnbound( ::capnp::Void value = ::capnp::VOID); inline bool isType(); inline bool hasType(); inline ::capnp::schema::Type::Builder getType(); inline void setType( ::capnp::schema::Type::Reader value); inline ::capnp::schema::Type::Builder initType(); inline void adoptType(::capnp::Orphan< ::capnp::schema::Type>&& value); inline ::capnp::Orphan< ::capnp::schema::Type> disownType(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Brand::Binding::Pipeline { public: typedef Binding Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Value::Reader { public: typedef Value Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isVoid() const; inline ::capnp::Void getVoid() const; inline bool isBool() const; inline bool getBool() const; inline bool isInt8() const; inline ::int8_t getInt8() const; inline bool isInt16() const; inline ::int16_t getInt16() const; inline bool isInt32() const; inline ::int32_t getInt32() const; inline bool isInt64() const; inline ::int64_t getInt64() const; inline bool isUint8() const; inline ::uint8_t getUint8() const; inline bool isUint16() const; inline ::uint16_t getUint16() const; inline bool isUint32() const; inline ::uint32_t getUint32() const; inline bool isUint64() const; inline ::uint64_t getUint64() const; inline bool isFloat32() const; inline float getFloat32() const; inline bool isFloat64() const; inline double getFloat64() const; inline bool isText() const; inline bool hasText() const; inline ::capnp::Text::Reader getText() const; inline bool isData() const; inline bool hasData() const; inline ::capnp::Data::Reader getData() const; inline bool isList() const; inline bool hasList() const; inline ::capnp::AnyPointer::Reader getList() const; inline bool isEnum() const; inline ::uint16_t getEnum() const; inline bool isStruct() const; inline bool hasStruct() const; inline ::capnp::AnyPointer::Reader getStruct() const; inline bool isInterface() const; inline ::capnp::Void getInterface() const; inline bool isAnyPointer() const; inline bool hasAnyPointer() const; inline ::capnp::AnyPointer::Reader getAnyPointer() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Value::Builder { public: typedef Value Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isVoid(); inline ::capnp::Void getVoid(); inline void setVoid( ::capnp::Void value = ::capnp::VOID); inline bool isBool(); inline bool getBool(); inline void setBool(bool value); inline bool isInt8(); inline ::int8_t getInt8(); inline void setInt8( ::int8_t value); inline bool isInt16(); inline ::int16_t getInt16(); inline void setInt16( ::int16_t value); inline bool isInt32(); inline ::int32_t getInt32(); inline void setInt32( ::int32_t value); inline bool isInt64(); inline ::int64_t getInt64(); inline void setInt64( ::int64_t value); inline bool isUint8(); inline ::uint8_t getUint8(); inline void setUint8( ::uint8_t value); inline bool isUint16(); inline ::uint16_t getUint16(); inline void setUint16( ::uint16_t value); inline bool isUint32(); inline ::uint32_t getUint32(); inline void setUint32( ::uint32_t value); inline bool isUint64(); inline ::uint64_t getUint64(); inline void setUint64( ::uint64_t value); inline bool isFloat32(); inline float getFloat32(); inline void setFloat32(float value); inline bool isFloat64(); inline double getFloat64(); inline void setFloat64(double value); inline bool isText(); inline bool hasText(); inline ::capnp::Text::Builder getText(); inline void setText( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initText(unsigned int size); inline void adoptText(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownText(); inline bool isData(); inline bool hasData(); inline ::capnp::Data::Builder getData(); inline void setData( ::capnp::Data::Reader value); inline ::capnp::Data::Builder initData(unsigned int size); inline void adoptData(::capnp::Orphan< ::capnp::Data>&& value); inline ::capnp::Orphan< ::capnp::Data> disownData(); inline bool isList(); inline bool hasList(); inline ::capnp::AnyPointer::Builder getList(); inline ::capnp::AnyPointer::Builder initList(); inline bool isEnum(); inline ::uint16_t getEnum(); inline void setEnum( ::uint16_t value); inline bool isStruct(); inline bool hasStruct(); inline ::capnp::AnyPointer::Builder getStruct(); inline ::capnp::AnyPointer::Builder initStruct(); inline bool isInterface(); inline ::capnp::Void getInterface(); inline void setInterface( ::capnp::Void value = ::capnp::VOID); inline bool isAnyPointer(); inline bool hasAnyPointer(); inline ::capnp::AnyPointer::Builder getAnyPointer(); inline ::capnp::AnyPointer::Builder initAnyPointer(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Value::Pipeline { public: typedef Value Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Annotation::Reader { public: typedef Annotation Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getId() const; inline bool hasValue() const; inline ::capnp::schema::Value::Reader getValue() const; inline bool hasBrand() const; inline ::capnp::schema::Brand::Reader getBrand() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Annotation::Builder { public: typedef Annotation Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getId(); inline void setId( ::uint64_t value); inline bool hasValue(); inline ::capnp::schema::Value::Builder getValue(); inline void setValue( ::capnp::schema::Value::Reader value); inline ::capnp::schema::Value::Builder initValue(); inline void adoptValue(::capnp::Orphan< ::capnp::schema::Value>&& value); inline ::capnp::Orphan< ::capnp::schema::Value> disownValue(); inline bool hasBrand(); inline ::capnp::schema::Brand::Builder getBrand(); inline void setBrand( ::capnp::schema::Brand::Reader value); inline ::capnp::schema::Brand::Builder initBrand(); inline void adoptBrand(::capnp::Orphan< ::capnp::schema::Brand>&& value); inline ::capnp::Orphan< ::capnp::schema::Brand> disownBrand(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Annotation::Pipeline { public: typedef Annotation Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::Value::Pipeline getValue(); inline ::capnp::schema::Brand::Pipeline getBrand(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class CapnpVersion::Reader { public: typedef CapnpVersion Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint16_t getMajor() const; inline ::uint8_t getMinor() const; inline ::uint8_t getMicro() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class CapnpVersion::Builder { public: typedef CapnpVersion Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint16_t getMajor(); inline void setMajor( ::uint16_t value); inline ::uint8_t getMinor(); inline void setMinor( ::uint8_t value); inline ::uint8_t getMicro(); inline void setMicro( ::uint8_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class CapnpVersion::Pipeline { public: typedef CapnpVersion Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class CodeGeneratorRequest::Reader { public: typedef CodeGeneratorRequest Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasNodes() const; inline ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Reader getNodes() const; inline bool hasRequestedFiles() const; inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Reader getRequestedFiles() const; inline bool hasCapnpVersion() const; inline ::capnp::schema::CapnpVersion::Reader getCapnpVersion() const; inline bool hasSourceInfo() const; inline ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Reader getSourceInfo() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class CodeGeneratorRequest::Builder { public: typedef CodeGeneratorRequest Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasNodes(); inline ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Builder getNodes(); inline void setNodes( ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Builder initNodes(unsigned int size); inline void adoptNodes(::capnp::Orphan< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>> disownNodes(); inline bool hasRequestedFiles(); inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Builder getRequestedFiles(); inline void setRequestedFiles( ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Builder initRequestedFiles(unsigned int size); inline void adoptRequestedFiles(::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>> disownRequestedFiles(); inline bool hasCapnpVersion(); inline ::capnp::schema::CapnpVersion::Builder getCapnpVersion(); inline void setCapnpVersion( ::capnp::schema::CapnpVersion::Reader value); inline ::capnp::schema::CapnpVersion::Builder initCapnpVersion(); inline void adoptCapnpVersion(::capnp::Orphan< ::capnp::schema::CapnpVersion>&& value); inline ::capnp::Orphan< ::capnp::schema::CapnpVersion> disownCapnpVersion(); inline bool hasSourceInfo(); inline ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Builder getSourceInfo(); inline void setSourceInfo( ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Builder initSourceInfo(unsigned int size); inline void adoptSourceInfo(::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>> disownSourceInfo(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class CodeGeneratorRequest::Pipeline { public: typedef CodeGeneratorRequest Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::schema::CapnpVersion::Pipeline getCapnpVersion(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class CodeGeneratorRequest::RequestedFile::Reader { public: typedef RequestedFile Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getId() const; inline bool hasFilename() const; inline ::capnp::Text::Reader getFilename() const; inline bool hasImports() const; inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Reader getImports() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class CodeGeneratorRequest::RequestedFile::Builder { public: typedef RequestedFile Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getId(); inline void setId( ::uint64_t value); inline bool hasFilename(); inline ::capnp::Text::Builder getFilename(); inline void setFilename( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initFilename(unsigned int size); inline void adoptFilename(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownFilename(); inline bool hasImports(); inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Builder getImports(); inline void setImports( ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Builder initImports(unsigned int size); inline void adoptImports(::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>> disownImports(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class CodeGeneratorRequest::RequestedFile::Pipeline { public: typedef RequestedFile Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class CodeGeneratorRequest::RequestedFile::Import::Reader { public: typedef Import Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint64_t getId() const; inline bool hasName() const; inline ::capnp::Text::Reader getName() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class CodeGeneratorRequest::RequestedFile::Import::Builder { public: typedef Import Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint64_t getId(); inline void setId( ::uint64_t value); inline bool hasName(); inline ::capnp::Text::Builder getName(); inline void setName( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initName(unsigned int size); inline void adoptName(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownName(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class CodeGeneratorRequest::RequestedFile::Import::Pipeline { public: typedef Import Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= inline ::capnp::schema::Node::Which Node::Reader::which() const { return _reader.getDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Node::Which Node::Builder::which() { return _builder.getDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline ::uint64_t Node::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t Node::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Node::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Node::Reader::hasDisplayName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Node::Builder::hasDisplayName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Node::Reader::getDisplayName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Node::Builder::getDisplayName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Node::Builder::setDisplayName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Node::Builder::initDisplayName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Node::Builder::adoptDisplayName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Node::Builder::disownDisplayName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Node::Reader::getDisplayNamePrefixLength() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Node::Builder::getDisplayNamePrefixLength() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Node::Builder::setDisplayNamePrefixLength( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::uint64_t Node::Reader::getScopeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint64_t Node::Builder::getScopeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Node::Builder::setScopeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Node::Reader::hasNestedNodes() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Node::Builder::hasNestedNodes() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Reader Node::Reader::getNestedNodes() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Builder Node::Builder::getNestedNodes() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Node::Builder::setNestedNodes( ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>::Builder Node::Builder::initNestedNodes(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Node::Builder::adoptNestedNodes( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>> Node::Builder::disownNestedNodes() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::NestedNode, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Node::Reader::hasAnnotations() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Node::Builder::hasAnnotations() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader Node::Reader::getAnnotations() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Node::Builder::getAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void Node::Builder::setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Node::Builder::initAnnotations(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), size); } inline void Node::Builder::adoptAnnotations( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> Node::Builder::disownAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline bool Node::Reader::isFile() const { return which() == Node::FILE; } inline bool Node::Builder::isFile() { return which() == Node::FILE; } inline ::capnp::Void Node::Reader::getFile() const { KJ_IREQUIRE((which() == Node::FILE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Node::Builder::getFile() { KJ_IREQUIRE((which() == Node::FILE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Node::Builder::setFile( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Node::FILE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Node::Reader::isStruct() const { return which() == Node::STRUCT; } inline bool Node::Builder::isStruct() { return which() == Node::STRUCT; } inline typename Node::Struct::Reader Node::Reader::getStruct() const { KJ_IREQUIRE((which() == Node::STRUCT), "Must check which() before get()ing a union member."); return typename Node::Struct::Reader(_reader); } inline typename Node::Struct::Builder Node::Builder::getStruct() { KJ_IREQUIRE((which() == Node::STRUCT), "Must check which() before get()ing a union member."); return typename Node::Struct::Builder(_builder); } inline typename Node::Struct::Builder Node::Builder::initStruct() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Node::STRUCT); _builder.setDataField< ::uint16_t>(::capnp::bounded<7>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint16_t>(::capnp::bounded<12>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint16_t>(::capnp::bounded<13>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<224>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint16_t>(::capnp::bounded<15>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint32_t>(::capnp::bounded<8>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); return typename Node::Struct::Builder(_builder); } inline bool Node::Reader::isEnum() const { return which() == Node::ENUM; } inline bool Node::Builder::isEnum() { return which() == Node::ENUM; } inline typename Node::Enum::Reader Node::Reader::getEnum() const { KJ_IREQUIRE((which() == Node::ENUM), "Must check which() before get()ing a union member."); return typename Node::Enum::Reader(_reader); } inline typename Node::Enum::Builder Node::Builder::getEnum() { KJ_IREQUIRE((which() == Node::ENUM), "Must check which() before get()ing a union member."); return typename Node::Enum::Builder(_builder); } inline typename Node::Enum::Builder Node::Builder::initEnum() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Node::ENUM); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); return typename Node::Enum::Builder(_builder); } inline bool Node::Reader::isInterface() const { return which() == Node::INTERFACE; } inline bool Node::Builder::isInterface() { return which() == Node::INTERFACE; } inline typename Node::Interface::Reader Node::Reader::getInterface() const { KJ_IREQUIRE((which() == Node::INTERFACE), "Must check which() before get()ing a union member."); return typename Node::Interface::Reader(_reader); } inline typename Node::Interface::Builder Node::Builder::getInterface() { KJ_IREQUIRE((which() == Node::INTERFACE), "Must check which() before get()ing a union member."); return typename Node::Interface::Builder(_builder); } inline typename Node::Interface::Builder Node::Builder::initInterface() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Node::INTERFACE); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<4>() * ::capnp::POINTERS).clear(); return typename Node::Interface::Builder(_builder); } inline bool Node::Reader::isConst() const { return which() == Node::CONST; } inline bool Node::Builder::isConst() { return which() == Node::CONST; } inline typename Node::Const::Reader Node::Reader::getConst() const { KJ_IREQUIRE((which() == Node::CONST), "Must check which() before get()ing a union member."); return typename Node::Const::Reader(_reader); } inline typename Node::Const::Builder Node::Builder::getConst() { KJ_IREQUIRE((which() == Node::CONST), "Must check which() before get()ing a union member."); return typename Node::Const::Builder(_builder); } inline typename Node::Const::Builder Node::Builder::initConst() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Node::CONST); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<4>() * ::capnp::POINTERS).clear(); return typename Node::Const::Builder(_builder); } inline bool Node::Reader::isAnnotation() const { return which() == Node::ANNOTATION; } inline bool Node::Builder::isAnnotation() { return which() == Node::ANNOTATION; } inline typename Node::Annotation::Reader Node::Reader::getAnnotation() const { KJ_IREQUIRE((which() == Node::ANNOTATION), "Must check which() before get()ing a union member."); return typename Node::Annotation::Reader(_reader); } inline typename Node::Annotation::Builder Node::Builder::getAnnotation() { KJ_IREQUIRE((which() == Node::ANNOTATION), "Must check which() before get()ing a union member."); return typename Node::Annotation::Builder(_builder); } inline typename Node::Annotation::Builder Node::Builder::initAnnotation() { _builder.setDataField( ::capnp::bounded<6>() * ::capnp::ELEMENTS, Node::ANNOTATION); _builder.setDataField(::capnp::bounded<112>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<113>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<114>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<115>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<116>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<117>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<118>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<119>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<120>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<121>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<122>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<123>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); return typename Node::Annotation::Builder(_builder); } inline bool Node::Reader::hasParameters() const { return !_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline bool Node::Builder::hasParameters() { return !_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader Node::Reader::getParameters() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder Node::Builder::getParameters() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline void Node::Builder::setParameters( ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder Node::Builder::initParameters(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), size); } inline void Node::Builder::adoptParameters( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>> Node::Builder::disownParameters() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<5>() * ::capnp::POINTERS)); } inline bool Node::Reader::getIsGeneric() const { return _reader.getDataField( ::capnp::bounded<288>() * ::capnp::ELEMENTS); } inline bool Node::Builder::getIsGeneric() { return _builder.getDataField( ::capnp::bounded<288>() * ::capnp::ELEMENTS); } inline void Node::Builder::setIsGeneric(bool value) { _builder.setDataField( ::capnp::bounded<288>() * ::capnp::ELEMENTS, value); } inline bool Node::Parameter::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Node::Parameter::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Node::Parameter::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Node::Parameter::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Node::Parameter::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Node::Parameter::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Node::Parameter::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Node::Parameter::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Node::NestedNode::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Node::NestedNode::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Node::NestedNode::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Node::NestedNode::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Node::NestedNode::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Node::NestedNode::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Node::NestedNode::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Node::NestedNode::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint64_t Node::NestedNode::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t Node::NestedNode::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Node::NestedNode::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint64_t Node::SourceInfo::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t Node::SourceInfo::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Node::SourceInfo::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Node::SourceInfo::Reader::hasDocComment() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Node::SourceInfo::Builder::hasDocComment() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Node::SourceInfo::Reader::getDocComment() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Node::SourceInfo::Builder::getDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Node::SourceInfo::Builder::setDocComment( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Node::SourceInfo::Builder::initDocComment(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Node::SourceInfo::Builder::adoptDocComment( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Node::SourceInfo::Builder::disownDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Node::SourceInfo::Reader::hasMembers() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Node::SourceInfo::Builder::hasMembers() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Reader Node::SourceInfo::Reader::getMembers() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Builder Node::SourceInfo::Builder::getMembers() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Node::SourceInfo::Builder::setMembers( ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>::Builder Node::SourceInfo::Builder::initMembers(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Node::SourceInfo::Builder::adoptMembers( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>> Node::SourceInfo::Builder::disownMembers() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo::Member, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Node::SourceInfo::Member::Reader::hasDocComment() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Node::SourceInfo::Member::Builder::hasDocComment() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Node::SourceInfo::Member::Reader::getDocComment() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Node::SourceInfo::Member::Builder::getDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Node::SourceInfo::Member::Builder::setDocComment( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Node::SourceInfo::Member::Builder::initDocComment(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Node::SourceInfo::Member::Builder::adoptDocComment( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Node::SourceInfo::Member::Builder::disownDocComment() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint16_t Node::Struct::Reader::getDataWordCount() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<7>() * ::capnp::ELEMENTS); } inline ::uint16_t Node::Struct::Builder::getDataWordCount() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<7>() * ::capnp::ELEMENTS); } inline void Node::Struct::Builder::setDataWordCount( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<7>() * ::capnp::ELEMENTS, value); } inline ::uint16_t Node::Struct::Reader::getPointerCount() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<12>() * ::capnp::ELEMENTS); } inline ::uint16_t Node::Struct::Builder::getPointerCount() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<12>() * ::capnp::ELEMENTS); } inline void Node::Struct::Builder::setPointerCount( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<12>() * ::capnp::ELEMENTS, value); } inline ::capnp::schema::ElementSize Node::Struct::Reader::getPreferredListEncoding() const { return _reader.getDataField< ::capnp::schema::ElementSize>( ::capnp::bounded<13>() * ::capnp::ELEMENTS); } inline ::capnp::schema::ElementSize Node::Struct::Builder::getPreferredListEncoding() { return _builder.getDataField< ::capnp::schema::ElementSize>( ::capnp::bounded<13>() * ::capnp::ELEMENTS); } inline void Node::Struct::Builder::setPreferredListEncoding( ::capnp::schema::ElementSize value) { _builder.setDataField< ::capnp::schema::ElementSize>( ::capnp::bounded<13>() * ::capnp::ELEMENTS, value); } inline bool Node::Struct::Reader::getIsGroup() const { return _reader.getDataField( ::capnp::bounded<224>() * ::capnp::ELEMENTS); } inline bool Node::Struct::Builder::getIsGroup() { return _builder.getDataField( ::capnp::bounded<224>() * ::capnp::ELEMENTS); } inline void Node::Struct::Builder::setIsGroup(bool value) { _builder.setDataField( ::capnp::bounded<224>() * ::capnp::ELEMENTS, value); } inline ::uint16_t Node::Struct::Reader::getDiscriminantCount() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<15>() * ::capnp::ELEMENTS); } inline ::uint16_t Node::Struct::Builder::getDiscriminantCount() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<15>() * ::capnp::ELEMENTS); } inline void Node::Struct::Builder::setDiscriminantCount( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<15>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Node::Struct::Reader::getDiscriminantOffset() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<8>() * ::capnp::ELEMENTS); } inline ::uint32_t Node::Struct::Builder::getDiscriminantOffset() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<8>() * ::capnp::ELEMENTS); } inline void Node::Struct::Builder::setDiscriminantOffset( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<8>() * ::capnp::ELEMENTS, value); } inline bool Node::Struct::Reader::hasFields() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Node::Struct::Builder::hasFields() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Reader Node::Struct::Reader::getFields() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Builder Node::Struct::Builder::getFields() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Node::Struct::Builder::setFields( ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>::Builder Node::Struct::Builder::initFields(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), size); } inline void Node::Struct::Builder::adoptFields( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>> Node::Struct::Builder::disownFields() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Field, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Node::Enum::Reader::hasEnumerants() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Node::Enum::Builder::hasEnumerants() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Reader Node::Enum::Reader::getEnumerants() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Builder Node::Enum::Builder::getEnumerants() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Node::Enum::Builder::setEnumerants( ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>::Builder Node::Enum::Builder::initEnumerants(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), size); } inline void Node::Enum::Builder::adoptEnumerants( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>> Node::Enum::Builder::disownEnumerants() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Enumerant, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Node::Interface::Reader::hasMethods() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Node::Interface::Builder::hasMethods() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Reader Node::Interface::Reader::getMethods() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Builder Node::Interface::Builder::getMethods() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Node::Interface::Builder::setMethods( ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>::Builder Node::Interface::Builder::initMethods(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), size); } inline void Node::Interface::Builder::adoptMethods( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>> Node::Interface::Builder::disownMethods() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Method, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Node::Interface::Reader::hasSuperclasses() const { return !_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline bool Node::Interface::Builder::hasSuperclasses() { return !_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Reader Node::Interface::Reader::getSuperclasses() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Builder Node::Interface::Builder::getSuperclasses() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline void Node::Interface::Builder::setSuperclasses( ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>::Builder Node::Interface::Builder::initSuperclasses(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), size); } inline void Node::Interface::Builder::adoptSuperclasses( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>> Node::Interface::Builder::disownSuperclasses() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Superclass, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline bool Node::Const::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Node::Const::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Type::Reader Node::Const::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::Builder Node::Const::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Type::Pipeline Node::Const::Pipeline::getType() { return ::capnp::schema::Type::Pipeline(_typeless.getPointerField(3)); } #endif // !CAPNP_LITE inline void Node::Const::Builder::setType( ::capnp::schema::Type::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Type::Builder Node::Const::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Node::Const::Builder::adoptType( ::capnp::Orphan< ::capnp::schema::Type>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Type> Node::Const::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Node::Const::Reader::hasValue() const { return !_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline bool Node::Const::Builder::hasValue() { return !_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Value::Reader Node::Const::Reader::getValue() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::get(_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline ::capnp::schema::Value::Builder Node::Const::Builder::getValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::get(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Value::Pipeline Node::Const::Pipeline::getValue() { return ::capnp::schema::Value::Pipeline(_typeless.getPointerField(4)); } #endif // !CAPNP_LITE inline void Node::Const::Builder::setValue( ::capnp::schema::Value::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Value>::set(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Value::Builder Node::Const::Builder::initValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::init(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline void Node::Const::Builder::adoptValue( ::capnp::Orphan< ::capnp::schema::Value>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Value>::adopt(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Value> Node::Const::Builder::disownValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::disown(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline bool Node::Annotation::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Node::Annotation::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Type::Reader Node::Annotation::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::Builder Node::Annotation::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Type::Pipeline Node::Annotation::Pipeline::getType() { return ::capnp::schema::Type::Pipeline(_typeless.getPointerField(3)); } #endif // !CAPNP_LITE inline void Node::Annotation::Builder::setType( ::capnp::schema::Type::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Type::Builder Node::Annotation::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Node::Annotation::Builder::adoptType( ::capnp::Orphan< ::capnp::schema::Type>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Type> Node::Annotation::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Node::Annotation::Reader::getTargetsFile() const { return _reader.getDataField( ::capnp::bounded<112>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsFile() { return _builder.getDataField( ::capnp::bounded<112>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsFile(bool value) { _builder.setDataField( ::capnp::bounded<112>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsConst() const { return _reader.getDataField( ::capnp::bounded<113>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsConst() { return _builder.getDataField( ::capnp::bounded<113>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsConst(bool value) { _builder.setDataField( ::capnp::bounded<113>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsEnum() const { return _reader.getDataField( ::capnp::bounded<114>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsEnum() { return _builder.getDataField( ::capnp::bounded<114>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsEnum(bool value) { _builder.setDataField( ::capnp::bounded<114>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsEnumerant() const { return _reader.getDataField( ::capnp::bounded<115>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsEnumerant() { return _builder.getDataField( ::capnp::bounded<115>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsEnumerant(bool value) { _builder.setDataField( ::capnp::bounded<115>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsStruct() const { return _reader.getDataField( ::capnp::bounded<116>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsStruct() { return _builder.getDataField( ::capnp::bounded<116>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsStruct(bool value) { _builder.setDataField( ::capnp::bounded<116>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsField() const { return _reader.getDataField( ::capnp::bounded<117>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsField() { return _builder.getDataField( ::capnp::bounded<117>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsField(bool value) { _builder.setDataField( ::capnp::bounded<117>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsUnion() const { return _reader.getDataField( ::capnp::bounded<118>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsUnion() { return _builder.getDataField( ::capnp::bounded<118>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsUnion(bool value) { _builder.setDataField( ::capnp::bounded<118>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsGroup() const { return _reader.getDataField( ::capnp::bounded<119>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsGroup() { return _builder.getDataField( ::capnp::bounded<119>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsGroup(bool value) { _builder.setDataField( ::capnp::bounded<119>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsInterface() const { return _reader.getDataField( ::capnp::bounded<120>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsInterface() { return _builder.getDataField( ::capnp::bounded<120>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsInterface(bool value) { _builder.setDataField( ::capnp::bounded<120>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsMethod() const { return _reader.getDataField( ::capnp::bounded<121>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsMethod() { return _builder.getDataField( ::capnp::bounded<121>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsMethod(bool value) { _builder.setDataField( ::capnp::bounded<121>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsParam() const { return _reader.getDataField( ::capnp::bounded<122>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsParam() { return _builder.getDataField( ::capnp::bounded<122>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsParam(bool value) { _builder.setDataField( ::capnp::bounded<122>() * ::capnp::ELEMENTS, value); } inline bool Node::Annotation::Reader::getTargetsAnnotation() const { return _reader.getDataField( ::capnp::bounded<123>() * ::capnp::ELEMENTS); } inline bool Node::Annotation::Builder::getTargetsAnnotation() { return _builder.getDataField( ::capnp::bounded<123>() * ::capnp::ELEMENTS); } inline void Node::Annotation::Builder::setTargetsAnnotation(bool value) { _builder.setDataField( ::capnp::bounded<123>() * ::capnp::ELEMENTS, value); } inline ::capnp::schema::Field::Which Field::Reader::which() const { return _reader.getDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Field::Which Field::Builder::which() { return _builder.getDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline bool Field::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Field::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Field::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Field::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Field::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Field::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Field::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Field::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint16_t Field::Reader::getCodeOrder() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint16_t Field::Builder::getCodeOrder() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Field::Builder::setCodeOrder( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Field::Reader::hasAnnotations() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Field::Builder::hasAnnotations() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader Field::Reader::getAnnotations() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Field::Builder::getAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Field::Builder::setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Field::Builder::initAnnotations(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Field::Builder::adoptAnnotations( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> Field::Builder::disownAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::uint16_t Field::Reader::getDiscriminantValue() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, 65535u); } inline ::uint16_t Field::Builder::getDiscriminantValue() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, 65535u); } inline void Field::Builder::setDiscriminantValue( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value, 65535u); } inline bool Field::Reader::isSlot() const { return which() == Field::SLOT; } inline bool Field::Builder::isSlot() { return which() == Field::SLOT; } inline typename Field::Slot::Reader Field::Reader::getSlot() const { KJ_IREQUIRE((which() == Field::SLOT), "Must check which() before get()ing a union member."); return typename Field::Slot::Reader(_reader); } inline typename Field::Slot::Builder Field::Builder::getSlot() { KJ_IREQUIRE((which() == Field::SLOT), "Must check which() before get()ing a union member."); return typename Field::Slot::Builder(_builder); } inline typename Field::Slot::Builder Field::Builder::initSlot() { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Field::SLOT); _builder.setDataField< ::uint32_t>(::capnp::bounded<1>() * ::capnp::ELEMENTS, 0); _builder.setDataField(::capnp::bounded<128>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<2>() * ::capnp::POINTERS).clear(); _builder.getPointerField(::capnp::bounded<3>() * ::capnp::POINTERS).clear(); return typename Field::Slot::Builder(_builder); } inline bool Field::Reader::isGroup() const { return which() == Field::GROUP; } inline bool Field::Builder::isGroup() { return which() == Field::GROUP; } inline typename Field::Group::Reader Field::Reader::getGroup() const { KJ_IREQUIRE((which() == Field::GROUP), "Must check which() before get()ing a union member."); return typename Field::Group::Reader(_reader); } inline typename Field::Group::Builder Field::Builder::getGroup() { KJ_IREQUIRE((which() == Field::GROUP), "Must check which() before get()ing a union member."); return typename Field::Group::Builder(_builder); } inline typename Field::Group::Builder Field::Builder::initGroup() { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Field::GROUP); _builder.setDataField< ::uint64_t>(::capnp::bounded<2>() * ::capnp::ELEMENTS, 0); return typename Field::Group::Builder(_builder); } inline typename Field::Ordinal::Reader Field::Reader::getOrdinal() const { return typename Field::Ordinal::Reader(_reader); } inline typename Field::Ordinal::Builder Field::Builder::getOrdinal() { return typename Field::Ordinal::Builder(_builder); } #if !CAPNP_LITE inline typename Field::Ordinal::Pipeline Field::Pipeline::getOrdinal() { return typename Field::Ordinal::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Field::Ordinal::Builder Field::Builder::initOrdinal() { _builder.setDataField< ::uint16_t>(::capnp::bounded<5>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint16_t>(::capnp::bounded<6>() * ::capnp::ELEMENTS, 0); return typename Field::Ordinal::Builder(_builder); } inline ::uint32_t Field::Slot::Reader::getOffset() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Field::Slot::Builder::getOffset() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Field::Slot::Builder::setOffset( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Field::Slot::Reader::hasType() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Field::Slot::Builder::hasType() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Type::Reader Field::Slot::Reader::getType() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::Builder Field::Slot::Builder::getType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Type::Pipeline Field::Slot::Pipeline::getType() { return ::capnp::schema::Type::Pipeline(_typeless.getPointerField(2)); } #endif // !CAPNP_LITE inline void Field::Slot::Builder::setType( ::capnp::schema::Type::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Type::Builder Field::Slot::Builder::initType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void Field::Slot::Builder::adoptType( ::capnp::Orphan< ::capnp::schema::Type>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Type> Field::Slot::Builder::disownType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline bool Field::Slot::Reader::hasDefaultValue() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Field::Slot::Builder::hasDefaultValue() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Value::Reader Field::Slot::Reader::getDefaultValue() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::schema::Value::Builder Field::Slot::Builder::getDefaultValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Value::Pipeline Field::Slot::Pipeline::getDefaultValue() { return ::capnp::schema::Value::Pipeline(_typeless.getPointerField(3)); } #endif // !CAPNP_LITE inline void Field::Slot::Builder::setDefaultValue( ::capnp::schema::Value::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Value>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Value::Builder Field::Slot::Builder::initDefaultValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Field::Slot::Builder::adoptDefaultValue( ::capnp::Orphan< ::capnp::schema::Value>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Value>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Value> Field::Slot::Builder::disownDefaultValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Field::Slot::Reader::getHadExplicitDefault() const { return _reader.getDataField( ::capnp::bounded<128>() * ::capnp::ELEMENTS); } inline bool Field::Slot::Builder::getHadExplicitDefault() { return _builder.getDataField( ::capnp::bounded<128>() * ::capnp::ELEMENTS); } inline void Field::Slot::Builder::setHadExplicitDefault(bool value) { _builder.setDataField( ::capnp::bounded<128>() * ::capnp::ELEMENTS, value); } inline ::uint64_t Field::Group::Reader::getTypeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint64_t Field::Group::Builder::getTypeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Field::Group::Builder::setTypeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::capnp::schema::Field::Ordinal::Which Field::Ordinal::Reader::which() const { return _reader.getDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Field::Ordinal::Which Field::Ordinal::Builder::which() { return _builder.getDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline bool Field::Ordinal::Reader::isImplicit() const { return which() == Field::Ordinal::IMPLICIT; } inline bool Field::Ordinal::Builder::isImplicit() { return which() == Field::Ordinal::IMPLICIT; } inline ::capnp::Void Field::Ordinal::Reader::getImplicit() const { KJ_IREQUIRE((which() == Field::Ordinal::IMPLICIT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Field::Ordinal::Builder::getImplicit() { KJ_IREQUIRE((which() == Field::Ordinal::IMPLICIT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Field::Ordinal::Builder::setImplicit( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS, Field::Ordinal::IMPLICIT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Field::Ordinal::Reader::isExplicit() const { return which() == Field::Ordinal::EXPLICIT; } inline bool Field::Ordinal::Builder::isExplicit() { return which() == Field::Ordinal::EXPLICIT; } inline ::uint16_t Field::Ordinal::Reader::getExplicit() const { KJ_IREQUIRE((which() == Field::Ordinal::EXPLICIT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint16_t>( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline ::uint16_t Field::Ordinal::Builder::getExplicit() { KJ_IREQUIRE((which() == Field::Ordinal::EXPLICIT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint16_t>( ::capnp::bounded<6>() * ::capnp::ELEMENTS); } inline void Field::Ordinal::Builder::setExplicit( ::uint16_t value) { _builder.setDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS, Field::Ordinal::EXPLICIT); _builder.setDataField< ::uint16_t>( ::capnp::bounded<6>() * ::capnp::ELEMENTS, value); } inline bool Enumerant::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Enumerant::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Enumerant::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Enumerant::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Enumerant::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Enumerant::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Enumerant::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Enumerant::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint16_t Enumerant::Reader::getCodeOrder() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint16_t Enumerant::Builder::getCodeOrder() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Enumerant::Builder::setCodeOrder( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Enumerant::Reader::hasAnnotations() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Enumerant::Builder::hasAnnotations() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader Enumerant::Reader::getAnnotations() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Enumerant::Builder::getAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Enumerant::Builder::setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Enumerant::Builder::initAnnotations(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Enumerant::Builder::adoptAnnotations( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> Enumerant::Builder::disownAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::uint64_t Superclass::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t Superclass::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Superclass::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Superclass::Reader::hasBrand() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Superclass::Builder::hasBrand() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Superclass::Reader::getBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Superclass::Builder::getBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Superclass::Pipeline::getBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Superclass::Builder::setBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Superclass::Builder::initBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Superclass::Builder::adoptBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Superclass::Builder::disownBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Method::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Method::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Method::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Method::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Method::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Method::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Method::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Method::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint16_t Method::Reader::getCodeOrder() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint16_t Method::Builder::getCodeOrder() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Method::Builder::setCodeOrder( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint64_t Method::Reader::getParamStructType() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Method::Builder::getParamStructType() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Method::Builder::setParamStructType( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint64_t Method::Reader::getResultStructType() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint64_t Method::Builder::getResultStructType() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Method::Builder::setResultStructType( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Method::Reader::hasAnnotations() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Method::Builder::hasAnnotations() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader Method::Reader::getAnnotations() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Method::Builder::getAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Method::Builder::setAnnotations( ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>::Builder Method::Builder::initAnnotations(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Method::Builder::adoptAnnotations( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>> Method::Builder::disownAnnotations() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Annotation, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool Method::Reader::hasParamBrand() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Method::Builder::hasParamBrand() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Method::Reader::getParamBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Method::Builder::getParamBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Method::Pipeline::getParamBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(2)); } #endif // !CAPNP_LITE inline void Method::Builder::setParamBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Method::Builder::initParamBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void Method::Builder::adoptParamBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Method::Builder::disownParamBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline bool Method::Reader::hasResultBrand() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool Method::Builder::hasResultBrand() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Method::Reader::getResultBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Method::Builder::getResultBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Method::Pipeline::getResultBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(3)); } #endif // !CAPNP_LITE inline void Method::Builder::setResultBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Method::Builder::initResultBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void Method::Builder::adoptResultBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Method::Builder::disownResultBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline bool Method::Reader::hasImplicitParameters() const { return !_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline bool Method::Builder::hasImplicitParameters() { return !_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader Method::Reader::getImplicitParameters() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder Method::Builder::getImplicitParameters() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline void Method::Builder::setImplicitParameters( ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>::Builder Method::Builder::initImplicitParameters(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), size); } inline void Method::Builder::adoptImplicitParameters( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>> Method::Builder::disownImplicitParameters() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::Parameter, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<4>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::Which Type::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Type::Which Type::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Type::Reader::isVoid() const { return which() == Type::VOID; } inline bool Type::Builder::isVoid() { return which() == Type::VOID; } inline ::capnp::Void Type::Reader::getVoid() const { KJ_IREQUIRE((which() == Type::VOID), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getVoid() { KJ_IREQUIRE((which() == Type::VOID), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setVoid( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::VOID); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isBool() const { return which() == Type::BOOL; } inline bool Type::Builder::isBool() { return which() == Type::BOOL; } inline ::capnp::Void Type::Reader::getBool() const { KJ_IREQUIRE((which() == Type::BOOL), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getBool() { KJ_IREQUIRE((which() == Type::BOOL), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setBool( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::BOOL); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isInt8() const { return which() == Type::INT8; } inline bool Type::Builder::isInt8() { return which() == Type::INT8; } inline ::capnp::Void Type::Reader::getInt8() const { KJ_IREQUIRE((which() == Type::INT8), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getInt8() { KJ_IREQUIRE((which() == Type::INT8), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setInt8( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::INT8); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isInt16() const { return which() == Type::INT16; } inline bool Type::Builder::isInt16() { return which() == Type::INT16; } inline ::capnp::Void Type::Reader::getInt16() const { KJ_IREQUIRE((which() == Type::INT16), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getInt16() { KJ_IREQUIRE((which() == Type::INT16), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setInt16( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::INT16); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isInt32() const { return which() == Type::INT32; } inline bool Type::Builder::isInt32() { return which() == Type::INT32; } inline ::capnp::Void Type::Reader::getInt32() const { KJ_IREQUIRE((which() == Type::INT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getInt32() { KJ_IREQUIRE((which() == Type::INT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setInt32( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::INT32); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isInt64() const { return which() == Type::INT64; } inline bool Type::Builder::isInt64() { return which() == Type::INT64; } inline ::capnp::Void Type::Reader::getInt64() const { KJ_IREQUIRE((which() == Type::INT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getInt64() { KJ_IREQUIRE((which() == Type::INT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setInt64( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::INT64); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isUint8() const { return which() == Type::UINT8; } inline bool Type::Builder::isUint8() { return which() == Type::UINT8; } inline ::capnp::Void Type::Reader::getUint8() const { KJ_IREQUIRE((which() == Type::UINT8), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getUint8() { KJ_IREQUIRE((which() == Type::UINT8), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setUint8( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::UINT8); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isUint16() const { return which() == Type::UINT16; } inline bool Type::Builder::isUint16() { return which() == Type::UINT16; } inline ::capnp::Void Type::Reader::getUint16() const { KJ_IREQUIRE((which() == Type::UINT16), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getUint16() { KJ_IREQUIRE((which() == Type::UINT16), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setUint16( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::UINT16); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isUint32() const { return which() == Type::UINT32; } inline bool Type::Builder::isUint32() { return which() == Type::UINT32; } inline ::capnp::Void Type::Reader::getUint32() const { KJ_IREQUIRE((which() == Type::UINT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getUint32() { KJ_IREQUIRE((which() == Type::UINT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setUint32( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::UINT32); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isUint64() const { return which() == Type::UINT64; } inline bool Type::Builder::isUint64() { return which() == Type::UINT64; } inline ::capnp::Void Type::Reader::getUint64() const { KJ_IREQUIRE((which() == Type::UINT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getUint64() { KJ_IREQUIRE((which() == Type::UINT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setUint64( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::UINT64); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isFloat32() const { return which() == Type::FLOAT32; } inline bool Type::Builder::isFloat32() { return which() == Type::FLOAT32; } inline ::capnp::Void Type::Reader::getFloat32() const { KJ_IREQUIRE((which() == Type::FLOAT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getFloat32() { KJ_IREQUIRE((which() == Type::FLOAT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setFloat32( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::FLOAT32); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isFloat64() const { return which() == Type::FLOAT64; } inline bool Type::Builder::isFloat64() { return which() == Type::FLOAT64; } inline ::capnp::Void Type::Reader::getFloat64() const { KJ_IREQUIRE((which() == Type::FLOAT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getFloat64() { KJ_IREQUIRE((which() == Type::FLOAT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setFloat64( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::FLOAT64); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isText() const { return which() == Type::TEXT; } inline bool Type::Builder::isText() { return which() == Type::TEXT; } inline ::capnp::Void Type::Reader::getText() const { KJ_IREQUIRE((which() == Type::TEXT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getText() { KJ_IREQUIRE((which() == Type::TEXT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setText( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::TEXT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isData() const { return which() == Type::DATA; } inline bool Type::Builder::isData() { return which() == Type::DATA; } inline ::capnp::Void Type::Reader::getData() const { KJ_IREQUIRE((which() == Type::DATA), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::Builder::getData() { KJ_IREQUIRE((which() == Type::DATA), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::Builder::setData( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::DATA); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::Reader::isList() const { return which() == Type::LIST; } inline bool Type::Builder::isList() { return which() == Type::LIST; } inline typename Type::List::Reader Type::Reader::getList() const { KJ_IREQUIRE((which() == Type::LIST), "Must check which() before get()ing a union member."); return typename Type::List::Reader(_reader); } inline typename Type::List::Builder Type::Builder::getList() { KJ_IREQUIRE((which() == Type::LIST), "Must check which() before get()ing a union member."); return typename Type::List::Builder(_builder); } inline typename Type::List::Builder Type::Builder::initList() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::LIST); _builder.getPointerField(::capnp::bounded<0>() * ::capnp::POINTERS).clear(); return typename Type::List::Builder(_builder); } inline bool Type::Reader::isEnum() const { return which() == Type::ENUM; } inline bool Type::Builder::isEnum() { return which() == Type::ENUM; } inline typename Type::Enum::Reader Type::Reader::getEnum() const { KJ_IREQUIRE((which() == Type::ENUM), "Must check which() before get()ing a union member."); return typename Type::Enum::Reader(_reader); } inline typename Type::Enum::Builder Type::Builder::getEnum() { KJ_IREQUIRE((which() == Type::ENUM), "Must check which() before get()ing a union member."); return typename Type::Enum::Builder(_builder); } inline typename Type::Enum::Builder Type::Builder::initEnum() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::ENUM); _builder.setDataField< ::uint64_t>(::capnp::bounded<1>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<0>() * ::capnp::POINTERS).clear(); return typename Type::Enum::Builder(_builder); } inline bool Type::Reader::isStruct() const { return which() == Type::STRUCT; } inline bool Type::Builder::isStruct() { return which() == Type::STRUCT; } inline typename Type::Struct::Reader Type::Reader::getStruct() const { KJ_IREQUIRE((which() == Type::STRUCT), "Must check which() before get()ing a union member."); return typename Type::Struct::Reader(_reader); } inline typename Type::Struct::Builder Type::Builder::getStruct() { KJ_IREQUIRE((which() == Type::STRUCT), "Must check which() before get()ing a union member."); return typename Type::Struct::Builder(_builder); } inline typename Type::Struct::Builder Type::Builder::initStruct() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::STRUCT); _builder.setDataField< ::uint64_t>(::capnp::bounded<1>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<0>() * ::capnp::POINTERS).clear(); return typename Type::Struct::Builder(_builder); } inline bool Type::Reader::isInterface() const { return which() == Type::INTERFACE; } inline bool Type::Builder::isInterface() { return which() == Type::INTERFACE; } inline typename Type::Interface::Reader Type::Reader::getInterface() const { KJ_IREQUIRE((which() == Type::INTERFACE), "Must check which() before get()ing a union member."); return typename Type::Interface::Reader(_reader); } inline typename Type::Interface::Builder Type::Builder::getInterface() { KJ_IREQUIRE((which() == Type::INTERFACE), "Must check which() before get()ing a union member."); return typename Type::Interface::Builder(_builder); } inline typename Type::Interface::Builder Type::Builder::initInterface() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::INTERFACE); _builder.setDataField< ::uint64_t>(::capnp::bounded<1>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<0>() * ::capnp::POINTERS).clear(); return typename Type::Interface::Builder(_builder); } inline bool Type::Reader::isAnyPointer() const { return which() == Type::ANY_POINTER; } inline bool Type::Builder::isAnyPointer() { return which() == Type::ANY_POINTER; } inline typename Type::AnyPointer::Reader Type::Reader::getAnyPointer() const { KJ_IREQUIRE((which() == Type::ANY_POINTER), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::Reader(_reader); } inline typename Type::AnyPointer::Builder Type::Builder::getAnyPointer() { KJ_IREQUIRE((which() == Type::ANY_POINTER), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::Builder(_builder); } inline typename Type::AnyPointer::Builder Type::Builder::initAnyPointer() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Type::ANY_POINTER); _builder.setDataField< ::uint16_t>(::capnp::bounded<4>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint16_t>(::capnp::bounded<5>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint64_t>(::capnp::bounded<2>() * ::capnp::ELEMENTS, 0); return typename Type::AnyPointer::Builder(_builder); } inline bool Type::List::Reader::hasElementType() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Type::List::Builder::hasElementType() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Type::Reader Type::List::Reader::getElementType() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::Builder Type::List::Builder::getElementType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Type::Pipeline Type::List::Pipeline::getElementType() { return ::capnp::schema::Type::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Type::List::Builder::setElementType( ::capnp::schema::Type::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Type::Builder Type::List::Builder::initElementType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Type::List::Builder::adoptElementType( ::capnp::Orphan< ::capnp::schema::Type>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Type>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Type> Type::List::Builder::disownElementType() { return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint64_t Type::Enum::Reader::getTypeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Type::Enum::Builder::getTypeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Type::Enum::Builder::setTypeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Type::Enum::Reader::hasBrand() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Type::Enum::Builder::hasBrand() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Type::Enum::Reader::getBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Type::Enum::Builder::getBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Type::Enum::Pipeline::getBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Type::Enum::Builder::setBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Type::Enum::Builder::initBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Type::Enum::Builder::adoptBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Type::Enum::Builder::disownBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint64_t Type::Struct::Reader::getTypeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Type::Struct::Builder::getTypeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Type::Struct::Builder::setTypeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Type::Struct::Reader::hasBrand() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Type::Struct::Builder::hasBrand() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Type::Struct::Reader::getBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Type::Struct::Builder::getBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Type::Struct::Pipeline::getBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Type::Struct::Builder::setBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Type::Struct::Builder::initBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Type::Struct::Builder::adoptBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Type::Struct::Builder::disownBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint64_t Type::Interface::Reader::getTypeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Type::Interface::Builder::getTypeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Type::Interface::Builder::setTypeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Type::Interface::Reader::hasBrand() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Type::Interface::Builder::hasBrand() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Type::Interface::Reader::getBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Type::Interface::Builder::getBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Type::Interface::Pipeline::getBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Type::Interface::Builder::setBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Type::Interface::Builder::initBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Type::Interface::Builder::adoptBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Type::Interface::Builder::disownBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::AnyPointer::Which Type::AnyPointer::Reader::which() const { return _reader.getDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Type::AnyPointer::Which Type::AnyPointer::Builder::which() { return _builder.getDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline bool Type::AnyPointer::Reader::isUnconstrained() const { return which() == Type::AnyPointer::UNCONSTRAINED; } inline bool Type::AnyPointer::Builder::isUnconstrained() { return which() == Type::AnyPointer::UNCONSTRAINED; } inline typename Type::AnyPointer::Unconstrained::Reader Type::AnyPointer::Reader::getUnconstrained() const { KJ_IREQUIRE((which() == Type::AnyPointer::UNCONSTRAINED), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::Unconstrained::Reader(_reader); } inline typename Type::AnyPointer::Unconstrained::Builder Type::AnyPointer::Builder::getUnconstrained() { KJ_IREQUIRE((which() == Type::AnyPointer::UNCONSTRAINED), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::Unconstrained::Builder(_builder); } inline typename Type::AnyPointer::Unconstrained::Builder Type::AnyPointer::Builder::initUnconstrained() { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Type::AnyPointer::UNCONSTRAINED); _builder.setDataField< ::uint16_t>(::capnp::bounded<5>() * ::capnp::ELEMENTS, 0); return typename Type::AnyPointer::Unconstrained::Builder(_builder); } inline bool Type::AnyPointer::Reader::isParameter() const { return which() == Type::AnyPointer::PARAMETER; } inline bool Type::AnyPointer::Builder::isParameter() { return which() == Type::AnyPointer::PARAMETER; } inline typename Type::AnyPointer::Parameter::Reader Type::AnyPointer::Reader::getParameter() const { KJ_IREQUIRE((which() == Type::AnyPointer::PARAMETER), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::Parameter::Reader(_reader); } inline typename Type::AnyPointer::Parameter::Builder Type::AnyPointer::Builder::getParameter() { KJ_IREQUIRE((which() == Type::AnyPointer::PARAMETER), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::Parameter::Builder(_builder); } inline typename Type::AnyPointer::Parameter::Builder Type::AnyPointer::Builder::initParameter() { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Type::AnyPointer::PARAMETER); _builder.setDataField< ::uint16_t>(::capnp::bounded<5>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint64_t>(::capnp::bounded<2>() * ::capnp::ELEMENTS, 0); return typename Type::AnyPointer::Parameter::Builder(_builder); } inline bool Type::AnyPointer::Reader::isImplicitMethodParameter() const { return which() == Type::AnyPointer::IMPLICIT_METHOD_PARAMETER; } inline bool Type::AnyPointer::Builder::isImplicitMethodParameter() { return which() == Type::AnyPointer::IMPLICIT_METHOD_PARAMETER; } inline typename Type::AnyPointer::ImplicitMethodParameter::Reader Type::AnyPointer::Reader::getImplicitMethodParameter() const { KJ_IREQUIRE((which() == Type::AnyPointer::IMPLICIT_METHOD_PARAMETER), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::ImplicitMethodParameter::Reader(_reader); } inline typename Type::AnyPointer::ImplicitMethodParameter::Builder Type::AnyPointer::Builder::getImplicitMethodParameter() { KJ_IREQUIRE((which() == Type::AnyPointer::IMPLICIT_METHOD_PARAMETER), "Must check which() before get()ing a union member."); return typename Type::AnyPointer::ImplicitMethodParameter::Builder(_builder); } inline typename Type::AnyPointer::ImplicitMethodParameter::Builder Type::AnyPointer::Builder::initImplicitMethodParameter() { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Type::AnyPointer::IMPLICIT_METHOD_PARAMETER); _builder.setDataField< ::uint16_t>(::capnp::bounded<5>() * ::capnp::ELEMENTS, 0); return typename Type::AnyPointer::ImplicitMethodParameter::Builder(_builder); } inline ::capnp::schema::Type::AnyPointer::Unconstrained::Which Type::AnyPointer::Unconstrained::Reader::which() const { return _reader.getDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Type::AnyPointer::Unconstrained::Which Type::AnyPointer::Unconstrained::Builder::which() { return _builder.getDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline bool Type::AnyPointer::Unconstrained::Reader::isAnyKind() const { return which() == Type::AnyPointer::Unconstrained::ANY_KIND; } inline bool Type::AnyPointer::Unconstrained::Builder::isAnyKind() { return which() == Type::AnyPointer::Unconstrained::ANY_KIND; } inline ::capnp::Void Type::AnyPointer::Unconstrained::Reader::getAnyKind() const { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::ANY_KIND), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::AnyPointer::Unconstrained::Builder::getAnyKind() { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::ANY_KIND), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::Unconstrained::Builder::setAnyKind( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS, Type::AnyPointer::Unconstrained::ANY_KIND); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::AnyPointer::Unconstrained::Reader::isStruct() const { return which() == Type::AnyPointer::Unconstrained::STRUCT; } inline bool Type::AnyPointer::Unconstrained::Builder::isStruct() { return which() == Type::AnyPointer::Unconstrained::STRUCT; } inline ::capnp::Void Type::AnyPointer::Unconstrained::Reader::getStruct() const { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::STRUCT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::AnyPointer::Unconstrained::Builder::getStruct() { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::STRUCT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::Unconstrained::Builder::setStruct( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS, Type::AnyPointer::Unconstrained::STRUCT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::AnyPointer::Unconstrained::Reader::isList() const { return which() == Type::AnyPointer::Unconstrained::LIST; } inline bool Type::AnyPointer::Unconstrained::Builder::isList() { return which() == Type::AnyPointer::Unconstrained::LIST; } inline ::capnp::Void Type::AnyPointer::Unconstrained::Reader::getList() const { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::LIST), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::AnyPointer::Unconstrained::Builder::getList() { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::LIST), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::Unconstrained::Builder::setList( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS, Type::AnyPointer::Unconstrained::LIST); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Type::AnyPointer::Unconstrained::Reader::isCapability() const { return which() == Type::AnyPointer::Unconstrained::CAPABILITY; } inline bool Type::AnyPointer::Unconstrained::Builder::isCapability() { return which() == Type::AnyPointer::Unconstrained::CAPABILITY; } inline ::capnp::Void Type::AnyPointer::Unconstrained::Reader::getCapability() const { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::CAPABILITY), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Type::AnyPointer::Unconstrained::Builder::getCapability() { KJ_IREQUIRE((which() == Type::AnyPointer::Unconstrained::CAPABILITY), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::Unconstrained::Builder::setCapability( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<5>() * ::capnp::ELEMENTS, Type::AnyPointer::Unconstrained::CAPABILITY); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint64_t Type::AnyPointer::Parameter::Reader::getScopeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint64_t Type::AnyPointer::Parameter::Builder::getScopeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::Parameter::Builder::setScopeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::uint16_t Type::AnyPointer::Parameter::Reader::getParameterIndex() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline ::uint16_t Type::AnyPointer::Parameter::Builder::getParameterIndex() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::Parameter::Builder::setParameterIndex( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<5>() * ::capnp::ELEMENTS, value); } inline ::uint16_t Type::AnyPointer::ImplicitMethodParameter::Reader::getParameterIndex() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline ::uint16_t Type::AnyPointer::ImplicitMethodParameter::Builder::getParameterIndex() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<5>() * ::capnp::ELEMENTS); } inline void Type::AnyPointer::ImplicitMethodParameter::Builder::setParameterIndex( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<5>() * ::capnp::ELEMENTS, value); } inline bool Brand::Reader::hasScopes() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Brand::Builder::hasScopes() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Reader Brand::Reader::getScopes() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Builder Brand::Builder::getScopes() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Brand::Builder::setScopes( ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>::Builder Brand::Builder::initScopes(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Brand::Builder::adoptScopes( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>> Brand::Builder::disownScopes() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Scope, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Scope::Which Brand::Scope::Reader::which() const { return _reader.getDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Brand::Scope::Which Brand::Scope::Builder::which() { return _builder.getDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS); } inline ::uint64_t Brand::Scope::Reader::getScopeId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t Brand::Scope::Builder::getScopeId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Brand::Scope::Builder::setScopeId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Brand::Scope::Reader::isBind() const { return which() == Brand::Scope::BIND; } inline bool Brand::Scope::Builder::isBind() { return which() == Brand::Scope::BIND; } inline bool Brand::Scope::Reader::hasBind() const { if (which() != Brand::Scope::BIND) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Brand::Scope::Builder::hasBind() { if (which() != Brand::Scope::BIND) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Reader Brand::Scope::Reader::getBind() const { KJ_IREQUIRE((which() == Brand::Scope::BIND), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Builder Brand::Scope::Builder::getBind() { KJ_IREQUIRE((which() == Brand::Scope::BIND), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Brand::Scope::Builder::setBind( ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Reader value) { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Brand::Scope::BIND); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>::Builder Brand::Scope::Builder::initBind(unsigned int size) { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Brand::Scope::BIND); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Brand::Scope::Builder::adoptBind( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>&& value) { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Brand::Scope::BIND); ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>> Brand::Scope::Builder::disownBind() { KJ_IREQUIRE((which() == Brand::Scope::BIND), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Brand::Binding, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Brand::Scope::Reader::isInherit() const { return which() == Brand::Scope::INHERIT; } inline bool Brand::Scope::Builder::isInherit() { return which() == Brand::Scope::INHERIT; } inline ::capnp::Void Brand::Scope::Reader::getInherit() const { KJ_IREQUIRE((which() == Brand::Scope::INHERIT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Brand::Scope::Builder::getInherit() { KJ_IREQUIRE((which() == Brand::Scope::INHERIT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Brand::Scope::Builder::setInherit( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<4>() * ::capnp::ELEMENTS, Brand::Scope::INHERIT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::capnp::schema::Brand::Binding::Which Brand::Binding::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Brand::Binding::Which Brand::Binding::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Brand::Binding::Reader::isUnbound() const { return which() == Brand::Binding::UNBOUND; } inline bool Brand::Binding::Builder::isUnbound() { return which() == Brand::Binding::UNBOUND; } inline ::capnp::Void Brand::Binding::Reader::getUnbound() const { KJ_IREQUIRE((which() == Brand::Binding::UNBOUND), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Brand::Binding::Builder::getUnbound() { KJ_IREQUIRE((which() == Brand::Binding::UNBOUND), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Brand::Binding::Builder::setUnbound( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Brand::Binding::UNBOUND); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Brand::Binding::Reader::isType() const { return which() == Brand::Binding::TYPE; } inline bool Brand::Binding::Builder::isType() { return which() == Brand::Binding::TYPE; } inline bool Brand::Binding::Reader::hasType() const { if (which() != Brand::Binding::TYPE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Brand::Binding::Builder::hasType() { if (which() != Brand::Binding::TYPE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Type::Reader Brand::Binding::Reader::getType() const { KJ_IREQUIRE((which() == Brand::Binding::TYPE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Type::Builder Brand::Binding::Builder::getType() { KJ_IREQUIRE((which() == Brand::Binding::TYPE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Brand::Binding::Builder::setType( ::capnp::schema::Type::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Brand::Binding::TYPE); ::capnp::_::PointerHelpers< ::capnp::schema::Type>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Type::Builder Brand::Binding::Builder::initType() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Brand::Binding::TYPE); return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Brand::Binding::Builder::adoptType( ::capnp::Orphan< ::capnp::schema::Type>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Brand::Binding::TYPE); ::capnp::_::PointerHelpers< ::capnp::schema::Type>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Type> Brand::Binding::Builder::disownType() { KJ_IREQUIRE((which() == Brand::Binding::TYPE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::schema::Type>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Value::Which Value::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::schema::Value::Which Value::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Value::Reader::isVoid() const { return which() == Value::VOID; } inline bool Value::Builder::isVoid() { return which() == Value::VOID; } inline ::capnp::Void Value::Reader::getVoid() const { KJ_IREQUIRE((which() == Value::VOID), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Value::Builder::getVoid() { KJ_IREQUIRE((which() == Value::VOID), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Value::Builder::setVoid( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::VOID); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isBool() const { return which() == Value::BOOL; } inline bool Value::Builder::isBool() { return which() == Value::BOOL; } inline bool Value::Reader::getBool() const { KJ_IREQUIRE((which() == Value::BOOL), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<16>() * ::capnp::ELEMENTS); } inline bool Value::Builder::getBool() { KJ_IREQUIRE((which() == Value::BOOL), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<16>() * ::capnp::ELEMENTS); } inline void Value::Builder::setBool(bool value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::BOOL); _builder.setDataField( ::capnp::bounded<16>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isInt8() const { return which() == Value::INT8; } inline bool Value::Builder::isInt8() { return which() == Value::INT8; } inline ::int8_t Value::Reader::getInt8() const { KJ_IREQUIRE((which() == Value::INT8), "Must check which() before get()ing a union member."); return _reader.getDataField< ::int8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::int8_t Value::Builder::getInt8() { KJ_IREQUIRE((which() == Value::INT8), "Must check which() before get()ing a union member."); return _builder.getDataField< ::int8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Value::Builder::setInt8( ::int8_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::INT8); _builder.setDataField< ::int8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isInt16() const { return which() == Value::INT16; } inline bool Value::Builder::isInt16() { return which() == Value::INT16; } inline ::int16_t Value::Reader::getInt16() const { KJ_IREQUIRE((which() == Value::INT16), "Must check which() before get()ing a union member."); return _reader.getDataField< ::int16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::int16_t Value::Builder::getInt16() { KJ_IREQUIRE((which() == Value::INT16), "Must check which() before get()ing a union member."); return _builder.getDataField< ::int16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setInt16( ::int16_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::INT16); _builder.setDataField< ::int16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isInt32() const { return which() == Value::INT32; } inline bool Value::Builder::isInt32() { return which() == Value::INT32; } inline ::int32_t Value::Reader::getInt32() const { KJ_IREQUIRE((which() == Value::INT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::int32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::int32_t Value::Builder::getInt32() { KJ_IREQUIRE((which() == Value::INT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::int32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setInt32( ::int32_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::INT32); _builder.setDataField< ::int32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isInt64() const { return which() == Value::INT64; } inline bool Value::Builder::isInt64() { return which() == Value::INT64; } inline ::int64_t Value::Reader::getInt64() const { KJ_IREQUIRE((which() == Value::INT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::int64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::int64_t Value::Builder::getInt64() { KJ_IREQUIRE((which() == Value::INT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::int64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setInt64( ::int64_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::INT64); _builder.setDataField< ::int64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isUint8() const { return which() == Value::UINT8; } inline bool Value::Builder::isUint8() { return which() == Value::UINT8; } inline ::uint8_t Value::Reader::getUint8() const { KJ_IREQUIRE((which() == Value::UINT8), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint8_t Value::Builder::getUint8() { KJ_IREQUIRE((which() == Value::UINT8), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Value::Builder::setUint8( ::uint8_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::UINT8); _builder.setDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isUint16() const { return which() == Value::UINT16; } inline bool Value::Builder::isUint16() { return which() == Value::UINT16; } inline ::uint16_t Value::Reader::getUint16() const { KJ_IREQUIRE((which() == Value::UINT16), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint16_t Value::Builder::getUint16() { KJ_IREQUIRE((which() == Value::UINT16), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setUint16( ::uint16_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::UINT16); _builder.setDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isUint32() const { return which() == Value::UINT32; } inline bool Value::Builder::isUint32() { return which() == Value::UINT32; } inline ::uint32_t Value::Reader::getUint32() const { KJ_IREQUIRE((which() == Value::UINT32), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Value::Builder::getUint32() { KJ_IREQUIRE((which() == Value::UINT32), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setUint32( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::UINT32); _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isUint64() const { return which() == Value::UINT64; } inline bool Value::Builder::isUint64() { return which() == Value::UINT64; } inline ::uint64_t Value::Reader::getUint64() const { KJ_IREQUIRE((which() == Value::UINT64), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Value::Builder::getUint64() { KJ_IREQUIRE((which() == Value::UINT64), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setUint64( ::uint64_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::UINT64); _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isFloat32() const { return which() == Value::FLOAT32; } inline bool Value::Builder::isFloat32() { return which() == Value::FLOAT32; } inline float Value::Reader::getFloat32() const { KJ_IREQUIRE((which() == Value::FLOAT32), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline float Value::Builder::getFloat32() { KJ_IREQUIRE((which() == Value::FLOAT32), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setFloat32(float value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::FLOAT32); _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isFloat64() const { return which() == Value::FLOAT64; } inline bool Value::Builder::isFloat64() { return which() == Value::FLOAT64; } inline double Value::Reader::getFloat64() const { KJ_IREQUIRE((which() == Value::FLOAT64), "Must check which() before get()ing a union member."); return _reader.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline double Value::Builder::getFloat64() { KJ_IREQUIRE((which() == Value::FLOAT64), "Must check which() before get()ing a union member."); return _builder.getDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setFloat64(double value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::FLOAT64); _builder.setDataField( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isText() const { return which() == Value::TEXT; } inline bool Value::Builder::isText() { return which() == Value::TEXT; } inline bool Value::Reader::hasText() const { if (which() != Value::TEXT) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasText() { if (which() != Value::TEXT) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Value::Reader::getText() const { KJ_IREQUIRE((which() == Value::TEXT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Value::Builder::getText() { KJ_IREQUIRE((which() == Value::TEXT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::setText( ::capnp::Text::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::TEXT); ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Value::Builder::initText(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::TEXT); return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Builder::adoptText( ::capnp::Orphan< ::capnp::Text>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::TEXT); ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Value::Builder::disownText() { KJ_IREQUIRE((which() == Value::TEXT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Reader::isData() const { return which() == Value::DATA; } inline bool Value::Builder::isData() { return which() == Value::DATA; } inline bool Value::Reader::hasData() const { if (which() != Value::DATA) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasData() { if (which() != Value::DATA) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Data::Reader Value::Reader::getData() const { KJ_IREQUIRE((which() == Value::DATA), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Data::Builder Value::Builder::getData() { KJ_IREQUIRE((which() == Value::DATA), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Value::Builder::setData( ::capnp::Data::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::DATA); ::capnp::_::PointerHelpers< ::capnp::Data>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Data::Builder Value::Builder::initData(unsigned int size) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::DATA); return ::capnp::_::PointerHelpers< ::capnp::Data>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Value::Builder::adoptData( ::capnp::Orphan< ::capnp::Data>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::DATA); ::capnp::_::PointerHelpers< ::capnp::Data>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Data> Value::Builder::disownData() { KJ_IREQUIRE((which() == Value::DATA), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::Data>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Value::Reader::isList() const { return which() == Value::LIST; } inline bool Value::Builder::isList() { return which() == Value::LIST; } inline bool Value::Reader::hasList() const { if (which() != Value::LIST) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasList() { if (which() != Value::LIST) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Value::Reader::getList() const { KJ_IREQUIRE((which() == Value::LIST), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Value::Builder::getList() { KJ_IREQUIRE((which() == Value::LIST), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Value::Builder::initList() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::LIST); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline bool Value::Reader::isEnum() const { return which() == Value::ENUM; } inline bool Value::Builder::isEnum() { return which() == Value::ENUM; } inline ::uint16_t Value::Reader::getEnum() const { KJ_IREQUIRE((which() == Value::ENUM), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint16_t Value::Builder::getEnum() { KJ_IREQUIRE((which() == Value::ENUM), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Value::Builder::setEnum( ::uint16_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::ENUM); _builder.setDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isStruct() const { return which() == Value::STRUCT; } inline bool Value::Builder::isStruct() { return which() == Value::STRUCT; } inline bool Value::Reader::hasStruct() const { if (which() != Value::STRUCT) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasStruct() { if (which() != Value::STRUCT) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Value::Reader::getStruct() const { KJ_IREQUIRE((which() == Value::STRUCT), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Value::Builder::getStruct() { KJ_IREQUIRE((which() == Value::STRUCT), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Value::Builder::initStruct() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::STRUCT); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline bool Value::Reader::isInterface() const { return which() == Value::INTERFACE; } inline bool Value::Builder::isInterface() { return which() == Value::INTERFACE; } inline ::capnp::Void Value::Reader::getInterface() const { KJ_IREQUIRE((which() == Value::INTERFACE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Value::Builder::getInterface() { KJ_IREQUIRE((which() == Value::INTERFACE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Value::Builder::setInterface( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::INTERFACE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Value::Reader::isAnyPointer() const { return which() == Value::ANY_POINTER; } inline bool Value::Builder::isAnyPointer() { return which() == Value::ANY_POINTER; } inline bool Value::Reader::hasAnyPointer() const { if (which() != Value::ANY_POINTER) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Value::Builder::hasAnyPointer() { if (which() != Value::ANY_POINTER) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Value::Reader::getAnyPointer() const { KJ_IREQUIRE((which() == Value::ANY_POINTER), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Value::Builder::getAnyPointer() { KJ_IREQUIRE((which() == Value::ANY_POINTER), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Value::Builder::initAnyPointer() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Value::ANY_POINTER); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::uint64_t Annotation::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t Annotation::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Annotation::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Annotation::Reader::hasValue() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Annotation::Builder::hasValue() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Value::Reader Annotation::Reader::getValue() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::schema::Value::Builder Annotation::Builder::getValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Value::Pipeline Annotation::Pipeline::getValue() { return ::capnp::schema::Value::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Annotation::Builder::setValue( ::capnp::schema::Value::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Value>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Value::Builder Annotation::Builder::initValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Annotation::Builder::adoptValue( ::capnp::Orphan< ::capnp::schema::Value>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Value>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Value> Annotation::Builder::disownValue() { return ::capnp::_::PointerHelpers< ::capnp::schema::Value>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Annotation::Reader::hasBrand() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Annotation::Builder::hasBrand() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::Brand::Reader Annotation::Reader::getBrand() const { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::schema::Brand::Builder Annotation::Builder::getBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::Brand::Pipeline Annotation::Pipeline::getBrand() { return ::capnp::schema::Brand::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE inline void Annotation::Builder::setBrand( ::capnp::schema::Brand::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::schema::Brand::Builder Annotation::Builder::initBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Annotation::Builder::adoptBrand( ::capnp::Orphan< ::capnp::schema::Brand>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::Brand> Annotation::Builder::disownBrand() { return ::capnp::_::PointerHelpers< ::capnp::schema::Brand>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::uint16_t CapnpVersion::Reader::getMajor() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint16_t CapnpVersion::Builder::getMajor() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void CapnpVersion::Builder::setMajor( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint8_t CapnpVersion::Reader::getMinor() const { return _reader.getDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint8_t CapnpVersion::Builder::getMinor() { return _builder.getDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void CapnpVersion::Builder::setMinor( ::uint8_t value) { _builder.setDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::uint8_t CapnpVersion::Reader::getMicro() const { return _reader.getDataField< ::uint8_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::uint8_t CapnpVersion::Builder::getMicro() { return _builder.getDataField< ::uint8_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline void CapnpVersion::Builder::setMicro( ::uint8_t value) { _builder.setDataField< ::uint8_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS, value); } inline bool CodeGeneratorRequest::Reader::hasNodes() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::Builder::hasNodes() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Reader CodeGeneratorRequest::Reader::getNodes() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::Builder::getNodes() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::Builder::setNodes( ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::Builder::initNodes(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void CodeGeneratorRequest::Builder::adoptNodes( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>> CodeGeneratorRequest::Builder::disownNodes() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool CodeGeneratorRequest::Reader::hasRequestedFiles() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::Builder::hasRequestedFiles() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Reader CodeGeneratorRequest::Reader::getRequestedFiles() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::Builder::getRequestedFiles() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::Builder::setRequestedFiles( ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::Builder::initRequestedFiles(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void CodeGeneratorRequest::Builder::adoptRequestedFiles( ::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>> CodeGeneratorRequest::Builder::disownRequestedFiles() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline bool CodeGeneratorRequest::Reader::hasCapnpVersion() const { return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::Builder::hasCapnpVersion() { return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::schema::CapnpVersion::Reader CodeGeneratorRequest::Reader::getCapnpVersion() const { return ::capnp::_::PointerHelpers< ::capnp::schema::CapnpVersion>::get(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::schema::CapnpVersion::Builder CodeGeneratorRequest::Builder::getCapnpVersion() { return ::capnp::_::PointerHelpers< ::capnp::schema::CapnpVersion>::get(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::schema::CapnpVersion::Pipeline CodeGeneratorRequest::Pipeline::getCapnpVersion() { return ::capnp::schema::CapnpVersion::Pipeline(_typeless.getPointerField(2)); } #endif // !CAPNP_LITE inline void CodeGeneratorRequest::Builder::setCapnpVersion( ::capnp::schema::CapnpVersion::Reader value) { ::capnp::_::PointerHelpers< ::capnp::schema::CapnpVersion>::set(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), value); } inline ::capnp::schema::CapnpVersion::Builder CodeGeneratorRequest::Builder::initCapnpVersion() { return ::capnp::_::PointerHelpers< ::capnp::schema::CapnpVersion>::init(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::Builder::adoptCapnpVersion( ::capnp::Orphan< ::capnp::schema::CapnpVersion>&& value) { ::capnp::_::PointerHelpers< ::capnp::schema::CapnpVersion>::adopt(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::schema::CapnpVersion> CodeGeneratorRequest::Builder::disownCapnpVersion() { return ::capnp::_::PointerHelpers< ::capnp::schema::CapnpVersion>::disown(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline bool CodeGeneratorRequest::Reader::hasSourceInfo() const { return !_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::Builder::hasSourceInfo() { return !_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Reader CodeGeneratorRequest::Reader::getSourceInfo() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::Builder::getSourceInfo() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::Builder::setSourceInfo( ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::Builder::initSourceInfo(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), size); } inline void CodeGeneratorRequest::Builder::adoptSourceInfo( ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>> CodeGeneratorRequest::Builder::disownSourceInfo() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::Node::SourceInfo, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<3>() * ::capnp::POINTERS)); } inline ::uint64_t CodeGeneratorRequest::RequestedFile::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t CodeGeneratorRequest::RequestedFile::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void CodeGeneratorRequest::RequestedFile::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool CodeGeneratorRequest::RequestedFile::Reader::hasFilename() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::RequestedFile::Builder::hasFilename() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader CodeGeneratorRequest::RequestedFile::Reader::getFilename() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder CodeGeneratorRequest::RequestedFile::Builder::getFilename() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::RequestedFile::Builder::setFilename( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder CodeGeneratorRequest::RequestedFile::Builder::initFilename(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void CodeGeneratorRequest::RequestedFile::Builder::adoptFilename( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> CodeGeneratorRequest::RequestedFile::Builder::disownFilename() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool CodeGeneratorRequest::RequestedFile::Reader::hasImports() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::RequestedFile::Builder::hasImports() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Reader CodeGeneratorRequest::RequestedFile::Reader::getImports() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::RequestedFile::Builder::getImports() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::RequestedFile::Builder::setImports( ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>::Builder CodeGeneratorRequest::RequestedFile::Builder::initImports(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void CodeGeneratorRequest::RequestedFile::Builder::adoptImports( ::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>> CodeGeneratorRequest::RequestedFile::Builder::disownImports() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::schema::CodeGeneratorRequest::RequestedFile::Import, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::uint64_t CodeGeneratorRequest::RequestedFile::Import::Reader::getId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint64_t CodeGeneratorRequest::RequestedFile::Import::Builder::getId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void CodeGeneratorRequest::RequestedFile::Import::Builder::setId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool CodeGeneratorRequest::RequestedFile::Import::Reader::hasName() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool CodeGeneratorRequest::RequestedFile::Import::Builder::hasName() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader CodeGeneratorRequest::RequestedFile::Import::Reader::getName() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder CodeGeneratorRequest::RequestedFile::Import::Builder::getName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CodeGeneratorRequest::RequestedFile::Import::Builder::setName( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder CodeGeneratorRequest::RequestedFile::Import::Builder::initName(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void CodeGeneratorRequest::RequestedFile::Import::Builder::adoptName( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> CodeGeneratorRequest::RequestedFile::Import::Builder::disownName() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/stream.capnp.h0000644000175000017500000000610413650317744021417 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: stream.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(995f9a3377c0b16e); } // namespace schemas } // namespace capnp namespace capnp { struct StreamResult { StreamResult() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(995f9a3377c0b16e, 0, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class StreamResult::Reader { public: typedef StreamResult Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class StreamResult::Builder { public: typedef StreamResult Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class StreamResult::Pipeline { public: typedef StreamResult Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= } // namespace capnproto-c++-0.8.0/src/capnp/schema-lite.h0000644000175000017500000000356313650101756021217 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include "message.h" CAPNP_BEGIN_HEADER namespace capnp { template inline schema::Node::Reader schemaProto() { // Get the schema::Node for this type's schema. This function works even in lite mode. return readMessageUnchecked(CapnpPrivate::encodedSchema()); } template ::typeId> inline schema::Node::Reader schemaProto() { // Get the schema::Node for this type's schema. This function works even in lite mode. return readMessageUnchecked(schemas::EnumInfo::encodedSchema()); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/schema.h0000644000175000017500000011250613650101756020262 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #if CAPNP_LITE #error "Reflection APIs, including this header, are not available in lite mode." #endif #undef CONST // For some ridiculous reason, Windows defines CONST to const. We have an enum value called CONST // in schema.capnp.h, so if this is defined, compilation is gonna fail. So we undef it because // that seems strictly better than failing entirely. But this could cause trouble for people later // on if they, say, include windows.h, then include schema.h, then include another windows API // header that uses CONST. I suppose they may have to re-#define CONST in between, or change the // header ordering. Sorry. // // Please don't file a bug report telling us to change our enum naming style. You are at least // seven years too late. #include #include #include // work-around macro conflict with `VOID` CAPNP_BEGIN_HEADER namespace capnp { class Schema; class StructSchema; class EnumSchema; class InterfaceSchema; class ConstSchema; class ListSchema; class Type; template ()> struct SchemaType_ { typedef Schema Type; }; template struct SchemaType_ { typedef schema::Type::Which Type; }; template struct SchemaType_ { typedef schema::Type::Which Type; }; template struct SchemaType_ { typedef EnumSchema Type; }; template struct SchemaType_ { typedef StructSchema Type; }; template struct SchemaType_ { typedef InterfaceSchema Type; }; template struct SchemaType_ { typedef ListSchema Type; }; template using SchemaType = typename SchemaType_::Type; // SchemaType is the type of T's schema, e.g. StructSchema if T is a struct. namespace _ { // private extern const RawSchema NULL_SCHEMA; extern const RawSchema NULL_STRUCT_SCHEMA; extern const RawSchema NULL_ENUM_SCHEMA; extern const RawSchema NULL_INTERFACE_SCHEMA; extern const RawSchema NULL_CONST_SCHEMA; // The schema types default to these null (empty) schemas in case of error, especially when // exceptions are disabled. } // namespace _ (private) class Schema { // Convenience wrapper around capnp::schema::Node. public: inline Schema(): raw(&_::NULL_SCHEMA.defaultBrand) {} template static inline SchemaType from() { return SchemaType::template fromImpl(); } // Get the Schema for a particular compiled-in type. schema::Node::Reader getProto() const; // Get the underlying Cap'n Proto representation of the schema node. (Note that this accessor // has performance comparable to accessors of struct-typed fields on Reader classes.) kj::ArrayPtr asUncheckedMessage() const; // Get the encoded schema node content as a single message segment. It is safe to read as an // unchecked message. Schema getDependency(uint64_t id) const CAPNP_DEPRECATED("Does not handle generics correctly."); // DEPRECATED: This method cannot correctly account for generic type parameter bindings that // may apply to the dependency. Instead of using this method, use a method of the Schema API // that corresponds to the exact kind of dependency. For example, to get a field type, use // StructSchema::Field::getType(). // // Gets the Schema for one of this Schema's dependencies. For example, if this Schema is for a // struct, you could look up the schema for one of its fields' types. Throws an exception if this // schema doesn't actually depend on the given id. // // Note that not all type IDs found in the schema node are considered "dependencies" -- only the // ones that are needed to implement the dynamic API are. That includes: // - Field types. // - Group types. // - scopeId for group nodes, but NOT otherwise. // - Method parameter and return types. // // The following are NOT considered dependencies: // - Nested nodes. // - scopeId for a non-group node. // - Annotations. // // To obtain schemas for those, you would need a SchemaLoader. bool isBranded() const; // Returns true if this schema represents a non-default parameterization of this type. Schema getGeneric() const; // Get the version of this schema with any brands removed. class BrandArgumentList; BrandArgumentList getBrandArgumentsAtScope(uint64_t scopeId) const; // Gets the values bound to the brand parameters at the given scope. StructSchema asStruct() const; EnumSchema asEnum() const; InterfaceSchema asInterface() const; ConstSchema asConst() const; // Cast the Schema to a specific type. Throws an exception if the type doesn't match. Use // getProto() to determine type, e.g. getProto().isStruct(). inline bool operator==(const Schema& other) const { return raw == other.raw; } inline bool operator!=(const Schema& other) const { return raw != other.raw; } // Determine whether two Schemas are wrapping the exact same underlying data, by identity. If // you want to check if two Schemas represent the same type (but possibly different versions of // it), compare their IDs instead. inline uint hashCode() const { return kj::hashCode(raw); } template void requireUsableAs() const; // Throws an exception if a value with this Schema cannot safely be cast to a native value of // the given type. This passes if either: // - *this == from() // - This schema was loaded with SchemaLoader, the type ID matches typeId(), and // loadCompiledTypeAndDependencies() was called on the SchemaLoader. kj::StringPtr getShortDisplayName() const; // Get the short version of the node's display name. private: const _::RawBrandedSchema* raw; inline explicit Schema(const _::RawBrandedSchema* raw): raw(raw) { KJ_IREQUIRE(raw->lazyInitializer == nullptr, "Must call ensureInitialized() on RawSchema before constructing Schema."); } template static inline Schema fromImpl() { return Schema(&_::rawSchema()); } void requireUsableAs(const _::RawSchema* expected) const; uint32_t getSchemaOffset(const schema::Value::Reader& value) const; Type getBrandBinding(uint64_t scopeId, uint index) const; // Look up the binding for a brand parameter used by this Schema. Returns `AnyPointer` if the // parameter is not bound. // // TODO(someday): Public interface for iterating over all bindings? Schema getDependency(uint64_t id, uint location) const; // Look up schema for a particular dependency of this schema. `location` is the dependency // location number as defined in _::RawBrandedSchema. Type interpretType(schema::Type::Reader proto, uint location) const; // Interpret a schema::Type in the given location within the schema, compiling it into a // Type object. friend class StructSchema; friend class EnumSchema; friend class InterfaceSchema; friend class ConstSchema; friend class ListSchema; friend class SchemaLoader; friend class Type; friend kj::StringTree _::structString( _::StructReader reader, const _::RawBrandedSchema& schema); friend kj::String _::enumString(uint16_t value, const _::RawBrandedSchema& schema); }; kj::StringPtr KJ_STRINGIFY(const Schema& schema); class Schema::BrandArgumentList { // A list of generic parameter bindings for parameters of some particular type. Note that since // parameters on an outer type apply to all inner types as well, a deeply-nested type can have // multiple BrandArgumentLists that apply to it. // // A BrandArgumentList only represents the arguments that the client of the type specified. Since // new parameters can be added over time, this list may not cover all defined parameters for the // type. Missing parameters should be treated as AnyPointer. This class's implementation of // operator[] already does this for you; out-of-bounds access will safely return AnyPointer. public: inline BrandArgumentList(): scopeId(0), size_(0), bindings(nullptr) {} inline uint size() const { return size_; } Type operator[](uint index) const; typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: uint64_t scopeId; uint size_; bool isUnbound; const _::RawBrandedSchema::Binding* bindings; inline BrandArgumentList(uint64_t scopeId, bool isUnbound) : scopeId(scopeId), size_(0), isUnbound(isUnbound), bindings(nullptr) {} inline BrandArgumentList(uint64_t scopeId, uint size, const _::RawBrandedSchema::Binding* bindings) : scopeId(scopeId), size_(size), isUnbound(false), bindings(bindings) {} friend class Schema; }; // ------------------------------------------------------------------- class StructSchema: public Schema { public: inline StructSchema(): Schema(&_::NULL_STRUCT_SCHEMA.defaultBrand) {} class Field; class FieldList; class FieldSubset; FieldList getFields() const; // List top-level fields of this struct. This list will contain top-level groups (including // named unions) but not the members of those groups. The list does, however, contain the // members of the unnamed union, if there is one. FieldSubset getUnionFields() const; // If the field contains an unnamed union, get a list of fields in the union, ordered by // ordinal. Since discriminant values are assigned sequentially by ordinal, you may index this // list by discriminant value. FieldSubset getNonUnionFields() const; // Get the fields of this struct which are not in an unnamed union, ordered by ordinal. kj::Maybe findFieldByName(kj::StringPtr name) const; // Find the field with the given name, or return null if there is no such field. If the struct // contains an unnamed union, then this will find fields of that union in addition to fields // of the outer struct, since they exist in the same namespace. It will not, however, find // members of groups (including named unions) -- you must first look up the group itself, // then dig into its type. Field getFieldByName(kj::StringPtr name) const; // Like findFieldByName() but throws an exception on failure. kj::Maybe getFieldByDiscriminant(uint16_t discriminant) const; // Finds the field whose `discriminantValue` is equal to the given value, or returns null if // there is no such field. (If the schema does not represent a union or a struct containing // an unnamed union, then this always returns null.) bool isStreamResult() const; // Convenience method to check if this is the result type of a streaming RPC method. private: StructSchema(Schema base): Schema(base) {} template static inline StructSchema fromImpl() { return StructSchema(Schema(&_::rawBrandedSchema())); } friend class Schema; friend class Type; }; class StructSchema::Field { public: Field() = default; inline schema::Field::Reader getProto() const { return proto; } inline StructSchema getContainingStruct() const { return parent; } inline uint getIndex() const { return index; } // Get the index of this field within the containing struct or union. Type getType() const; // Get the type of this field. Note that this is preferred over getProto().getType() as this // method will apply generics. uint32_t getDefaultValueSchemaOffset() const; // For struct, list, and object fields, returns the offset, in words, within the first segment of // the struct's schema, where this field's default value pointer is located. The schema is // always stored as a single-segment unchecked message, which in turn means that the default // value pointer itself can be treated as the root of an unchecked message -- if you know where // to find it, which is what this method helps you with. // // For blobs, returns the offset of the beginning of the blob's content within the first segment // of the struct's schema. // // This is primarily useful for code generators. The C++ code generator, for example, embeds // the entire schema as a raw word array within the generated code. Of course, to implement // field accessors, it needs access to those fields' default values. Embedding separate copies // of those default values would be redundant since they are already included in the schema, but // seeking through the schema at runtime to find the default values would be ugly. Instead, // the code generator can use getDefaultValueSchemaOffset() to find the offset of the default // value within the schema, and can simply apply that offset at runtime. // // If the above does not make sense, you probably don't need this method. inline bool operator==(const Field& other) const; inline bool operator!=(const Field& other) const { return !(*this == other); } inline uint hashCode() const; private: StructSchema parent; uint index; schema::Field::Reader proto; inline Field(StructSchema parent, uint index, schema::Field::Reader proto) : parent(parent), index(index), proto(proto) {} friend class StructSchema; }; kj::StringPtr KJ_STRINGIFY(const StructSchema::Field& field); class StructSchema::FieldList { public: FieldList() = default; // empty list inline uint size() const { return list.size(); } inline Field operator[](uint index) const { return Field(parent, index, list[index]); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: StructSchema parent; List::Reader list; inline FieldList(StructSchema parent, List::Reader list) : parent(parent), list(list) {} friend class StructSchema; }; class StructSchema::FieldSubset { public: FieldSubset() = default; // empty list inline uint size() const { return size_; } inline Field operator[](uint index) const { return Field(parent, indices[index], list[indices[index]]); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: StructSchema parent; List::Reader list; const uint16_t* indices; uint size_; inline FieldSubset(StructSchema parent, List::Reader list, const uint16_t* indices, uint size) : parent(parent), list(list), indices(indices), size_(size) {} friend class StructSchema; }; // ------------------------------------------------------------------- class EnumSchema: public Schema { public: inline EnumSchema(): Schema(&_::NULL_ENUM_SCHEMA.defaultBrand) {} class Enumerant; class EnumerantList; EnumerantList getEnumerants() const; kj::Maybe findEnumerantByName(kj::StringPtr name) const; Enumerant getEnumerantByName(kj::StringPtr name) const; // Like findEnumerantByName() but throws an exception on failure. private: EnumSchema(Schema base): Schema(base) {} template static inline EnumSchema fromImpl() { return EnumSchema(Schema(&_::rawBrandedSchema())); } friend class Schema; friend class Type; }; class EnumSchema::Enumerant { public: Enumerant() = default; inline schema::Enumerant::Reader getProto() const { return proto; } inline EnumSchema getContainingEnum() const { return parent; } inline uint16_t getOrdinal() const { return ordinal; } inline uint getIndex() const { return ordinal; } inline bool operator==(const Enumerant& other) const; inline bool operator!=(const Enumerant& other) const { return !(*this == other); } inline uint hashCode() const; private: EnumSchema parent; uint16_t ordinal; schema::Enumerant::Reader proto; inline Enumerant(EnumSchema parent, uint16_t ordinal, schema::Enumerant::Reader proto) : parent(parent), ordinal(ordinal), proto(proto) {} friend class EnumSchema; }; class EnumSchema::EnumerantList { public: EnumerantList() = default; // empty list inline uint size() const { return list.size(); } inline Enumerant operator[](uint index) const { return Enumerant(parent, index, list[index]); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: EnumSchema parent; List::Reader list; inline EnumerantList(EnumSchema parent, List::Reader list) : parent(parent), list(list) {} friend class EnumSchema; }; // ------------------------------------------------------------------- class InterfaceSchema: public Schema { public: inline InterfaceSchema(): Schema(&_::NULL_INTERFACE_SCHEMA.defaultBrand) {} class Method; class MethodList; MethodList getMethods() const; kj::Maybe findMethodByName(kj::StringPtr name) const; Method getMethodByName(kj::StringPtr name) const; // Like findMethodByName() but throws an exception on failure. class SuperclassList; SuperclassList getSuperclasses() const; // Get the immediate superclasses of this type, after applying generics. bool extends(InterfaceSchema other) const; // Returns true if `other` is a superclass of this interface (including if `other == *this`). kj::Maybe findSuperclass(uint64_t typeId) const; // Find the superclass of this interface with the given type ID. Returns null if the interface // extends no such type. private: InterfaceSchema(Schema base): Schema(base) {} template static inline InterfaceSchema fromImpl() { return InterfaceSchema(Schema(&_::rawBrandedSchema())); } friend class Schema; friend class Type; kj::Maybe findMethodByName(kj::StringPtr name, uint& counter) const; bool extends(InterfaceSchema other, uint& counter) const; kj::Maybe findSuperclass(uint64_t typeId, uint& counter) const; // We protect against malicious schemas with large or cyclic hierarchies by cutting off the // search when the counter reaches a threshold. }; class InterfaceSchema::Method { public: Method() = default; inline schema::Method::Reader getProto() const { return proto; } inline InterfaceSchema getContainingInterface() const { return parent; } inline uint16_t getOrdinal() const { return ordinal; } inline uint getIndex() const { return ordinal; } bool isStreaming() const { return getResultType().isStreamResult(); } // Check if this is a streaming method. StructSchema getParamType() const; StructSchema getResultType() const; // Get the parameter and result types, including substituting generic parameters. inline bool operator==(const Method& other) const; inline bool operator!=(const Method& other) const { return !(*this == other); } inline uint hashCode() const; private: InterfaceSchema parent; uint16_t ordinal; schema::Method::Reader proto; inline Method(InterfaceSchema parent, uint16_t ordinal, schema::Method::Reader proto) : parent(parent), ordinal(ordinal), proto(proto) {} friend class InterfaceSchema; }; class InterfaceSchema::MethodList { public: MethodList() = default; // empty list inline uint size() const { return list.size(); } inline Method operator[](uint index) const { return Method(parent, index, list[index]); } typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: InterfaceSchema parent; List::Reader list; inline MethodList(InterfaceSchema parent, List::Reader list) : parent(parent), list(list) {} friend class InterfaceSchema; }; class InterfaceSchema::SuperclassList { public: SuperclassList() = default; // empty list inline uint size() const { return list.size(); } InterfaceSchema operator[](uint index) const; typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: InterfaceSchema parent; List::Reader list; inline SuperclassList(InterfaceSchema parent, List::Reader list) : parent(parent), list(list) {} friend class InterfaceSchema; }; // ------------------------------------------------------------------- class ConstSchema: public Schema { // Represents a constant declaration. // // `ConstSchema` can be implicitly cast to DynamicValue to read its value. public: inline ConstSchema(): Schema(&_::NULL_CONST_SCHEMA.defaultBrand) {} template ReaderFor as() const; // Read the constant's value. This is a convenience method equivalent to casting the ConstSchema // to a DynamicValue and then calling its `as()` method. For dependency reasons, this method // is defined in , which you must #include explicitly. uint32_t getValueSchemaOffset() const; // Much like StructSchema::Field::getDefaultValueSchemaOffset(), if the constant has pointer // type, this gets the offset from the beginning of the constant's schema node to a pointer // representing the constant value. Type getType() const; private: ConstSchema(Schema base): Schema(base) {} friend class Schema; }; // ------------------------------------------------------------------- class Type { public: struct BrandParameter { uint64_t scopeId; uint index; }; struct ImplicitParameter { uint index; }; inline Type(); inline Type(schema::Type::Which primitive); inline Type(StructSchema schema); inline Type(EnumSchema schema); inline Type(InterfaceSchema schema); inline Type(ListSchema schema); inline Type(schema::Type::AnyPointer::Unconstrained::Which anyPointerKind); inline Type(BrandParameter param); inline Type(ImplicitParameter param); template inline static Type from(); template inline static Type from(T&& value); inline schema::Type::Which which() const; StructSchema asStruct() const; EnumSchema asEnum() const; InterfaceSchema asInterface() const; ListSchema asList() const; // Each of these methods may only be called if which() returns the corresponding type. kj::Maybe getBrandParameter() const; // Only callable if which() returns ANY_POINTER. Returns null if the type is just a regular // AnyPointer and not a parameter. kj::Maybe getImplicitParameter() const; // Only callable if which() returns ANY_POINTER. Returns null if the type is just a regular // AnyPointer and not a parameter. "Implicit parameters" refer to type parameters on methods. inline schema::Type::AnyPointer::Unconstrained::Which whichAnyPointerKind() const; // Only callable if which() returns ANY_POINTER. inline bool isVoid() const; inline bool isBool() const; inline bool isInt8() const; inline bool isInt16() const; inline bool isInt32() const; inline bool isInt64() const; inline bool isUInt8() const; inline bool isUInt16() const; inline bool isUInt32() const; inline bool isUInt64() const; inline bool isFloat32() const; inline bool isFloat64() const; inline bool isText() const; inline bool isData() const; inline bool isList() const; inline bool isEnum() const; inline bool isStruct() const; inline bool isInterface() const; inline bool isAnyPointer() const; bool operator==(const Type& other) const; inline bool operator!=(const Type& other) const { return !(*this == other); } uint hashCode() const; inline Type wrapInList(uint depth = 1) const; // Return the Type formed by wrapping this type in List() `depth` times. inline Type(schema::Type::Which derived, const _::RawBrandedSchema* schema); // For internal use. private: schema::Type::Which baseType; // type not including applications of List() uint8_t listDepth; // 0 for T, 1 for List(T), 2 for List(List(T)), ... bool isImplicitParam; // If true, this refers to an implicit method parameter. baseType must be ANY_POINTER, scopeId // must be zero, and paramIndex indicates the parameter index. union { uint16_t paramIndex; // If baseType is ANY_POINTER but this Type actually refers to a type parameter, this is the // index of the parameter among the parameters at its scope, and `scopeId` below is the type ID // of the scope where the parameter was defined. schema::Type::AnyPointer::Unconstrained::Which anyPointerKind; // If scopeId is zero and isImplicitParam is false. }; union { const _::RawBrandedSchema* schema; // if type is struct, enum, interface... uint64_t scopeId; // if type is AnyPointer but it's actually a type parameter... }; Type(schema::Type::Which baseType, uint8_t listDepth, const _::RawBrandedSchema* schema) : baseType(baseType), listDepth(listDepth), schema(schema) { KJ_IREQUIRE(baseType != schema::Type::ANY_POINTER); } void requireUsableAs(Type expected) const; template struct FromValueImpl; friend class ListSchema; // only for requireUsableAs() }; // ------------------------------------------------------------------- class ListSchema { // ListSchema is a little different because list types are not described by schema nodes. So, // ListSchema doesn't subclass Schema. public: ListSchema() = default; static ListSchema of(schema::Type::Which primitiveType); static ListSchema of(StructSchema elementType); static ListSchema of(EnumSchema elementType); static ListSchema of(InterfaceSchema elementType); static ListSchema of(ListSchema elementType); static ListSchema of(Type elementType); // Construct the schema for a list of the given type. static ListSchema of(schema::Type::Reader elementType, Schema context) CAPNP_DEPRECATED("Does not handle generics correctly."); // DEPRECATED: This method cannot correctly account for generic type parameter bindings that // may apply to the input type. Instead of using this method, use a method of the Schema API // that corresponds to the exact kind of dependency. For example, to get a field type, use // StructSchema::Field::getType(). // // Construct from an element type schema. Requires a context which can handle getDependency() // requests for any type ID found in the schema. Type getElementType() const; inline schema::Type::Which whichElementType() const; // Get the element type's "which()". ListSchema does not actually store a schema::Type::Reader // describing the element type, but if it did, this would be equivalent to calling // .getBody().which() on that type. StructSchema getStructElementType() const; EnumSchema getEnumElementType() const; InterfaceSchema getInterfaceElementType() const; ListSchema getListElementType() const; // Get the schema for complex element types. Each of these throws an exception if the element // type is not of the requested kind. inline bool operator==(const ListSchema& other) const { return elementType == other.elementType; } inline bool operator!=(const ListSchema& other) const { return elementType != other.elementType; } template void requireUsableAs() const; private: Type elementType; inline explicit ListSchema(Type elementType): elementType(elementType) {} template struct FromImpl; template static inline ListSchema fromImpl() { return FromImpl::get(); } void requireUsableAs(ListSchema expected) const; friend class Schema; }; // ======================================================================================= // inline implementation template <> inline schema::Type::Which Schema::from() { return schema::Type::VOID; } template <> inline schema::Type::Which Schema::from() { return schema::Type::BOOL; } template <> inline schema::Type::Which Schema::from() { return schema::Type::INT8; } template <> inline schema::Type::Which Schema::from() { return schema::Type::INT16; } template <> inline schema::Type::Which Schema::from() { return schema::Type::INT32; } template <> inline schema::Type::Which Schema::from() { return schema::Type::INT64; } template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT8; } template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT16; } template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT32; } template <> inline schema::Type::Which Schema::from() { return schema::Type::UINT64; } template <> inline schema::Type::Which Schema::from() { return schema::Type::FLOAT32; } template <> inline schema::Type::Which Schema::from() { return schema::Type::FLOAT64; } template <> inline schema::Type::Which Schema::from() { return schema::Type::TEXT; } template <> inline schema::Type::Which Schema::from() { return schema::Type::DATA; } inline Schema Schema::getDependency(uint64_t id) const { return getDependency(id, 0); } inline bool Schema::isBranded() const { return raw != &raw->generic->defaultBrand; } inline Schema Schema::getGeneric() const { return Schema(&raw->generic->defaultBrand); } template inline void Schema::requireUsableAs() const { requireUsableAs(&_::rawSchema()); } inline bool StructSchema::Field::operator==(const Field& other) const { return parent == other.parent && index == other.index; } inline bool EnumSchema::Enumerant::operator==(const Enumerant& other) const { return parent == other.parent && ordinal == other.ordinal; } inline bool InterfaceSchema::Method::operator==(const Method& other) const { return parent == other.parent && ordinal == other.ordinal; } inline uint StructSchema::Field::hashCode() const { return kj::hashCode(parent, index); } inline uint EnumSchema::Enumerant::hashCode() const { return kj::hashCode(parent, ordinal); } inline uint InterfaceSchema::Method::hashCode() const { return kj::hashCode(parent, ordinal); } inline ListSchema ListSchema::of(StructSchema elementType) { return ListSchema(Type(elementType)); } inline ListSchema ListSchema::of(EnumSchema elementType) { return ListSchema(Type(elementType)); } inline ListSchema ListSchema::of(InterfaceSchema elementType) { return ListSchema(Type(elementType)); } inline ListSchema ListSchema::of(ListSchema elementType) { return ListSchema(Type(elementType)); } inline ListSchema ListSchema::of(Type elementType) { return ListSchema(elementType); } inline Type ListSchema::getElementType() const { return elementType; } inline schema::Type::Which ListSchema::whichElementType() const { return elementType.which(); } inline StructSchema ListSchema::getStructElementType() const { return elementType.asStruct(); } inline EnumSchema ListSchema::getEnumElementType() const { return elementType.asEnum(); } inline InterfaceSchema ListSchema::getInterfaceElementType() const { return elementType.asInterface(); } inline ListSchema ListSchema::getListElementType() const { return elementType.asList(); } template inline void ListSchema::requireUsableAs() const { static_assert(kind() == Kind::LIST, "ListSchema::requireUsableAs() requires T is a list type."); requireUsableAs(Schema::from()); } inline void ListSchema::requireUsableAs(ListSchema expected) const { elementType.requireUsableAs(expected.elementType); } template struct ListSchema::FromImpl> { static inline ListSchema get() { return of(Schema::from()); } }; inline Type::Type(): baseType(schema::Type::VOID), listDepth(0), schema(nullptr) {} inline Type::Type(schema::Type::Which primitive) : baseType(primitive), listDepth(0), isImplicitParam(false) { KJ_IREQUIRE(primitive != schema::Type::STRUCT && primitive != schema::Type::ENUM && primitive != schema::Type::INTERFACE && primitive != schema::Type::LIST); if (primitive == schema::Type::ANY_POINTER) { scopeId = 0; anyPointerKind = schema::Type::AnyPointer::Unconstrained::ANY_KIND; } else { schema = nullptr; } } inline Type::Type(schema::Type::Which derived, const _::RawBrandedSchema* schema) : baseType(derived), listDepth(0), isImplicitParam(false), schema(schema) { KJ_IREQUIRE(derived == schema::Type::STRUCT || derived == schema::Type::ENUM || derived == schema::Type::INTERFACE); } inline Type::Type(StructSchema schema) : baseType(schema::Type::STRUCT), listDepth(0), schema(schema.raw) {} inline Type::Type(EnumSchema schema) : baseType(schema::Type::ENUM), listDepth(0), schema(schema.raw) {} inline Type::Type(InterfaceSchema schema) : baseType(schema::Type::INTERFACE), listDepth(0), schema(schema.raw) {} inline Type::Type(ListSchema schema) : Type(schema.getElementType()) { ++listDepth; } inline Type::Type(schema::Type::AnyPointer::Unconstrained::Which anyPointerKind) : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(false), anyPointerKind(anyPointerKind), scopeId(0) {} inline Type::Type(BrandParameter param) : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(false), paramIndex(param.index), scopeId(param.scopeId) {} inline Type::Type(ImplicitParameter param) : baseType(schema::Type::ANY_POINTER), listDepth(0), isImplicitParam(true), paramIndex(param.index), scopeId(0) {} inline schema::Type::Which Type::which() const { return listDepth > 0 ? schema::Type::LIST : baseType; } inline schema::Type::AnyPointer::Unconstrained::Which Type::whichAnyPointerKind() const { KJ_IREQUIRE(baseType == schema::Type::ANY_POINTER); return !isImplicitParam && scopeId == 0 ? anyPointerKind : schema::Type::AnyPointer::Unconstrained::ANY_KIND; } template inline Type Type::from() { return Type(Schema::from()); } template struct Type::FromValueImpl { template static inline Type type(U&& value) { return Type::from(); } }; template struct Type::FromValueImpl { template static inline Type type(U&& value) { // All dynamic types have getSchema(). return value.getSchema(); } }; template inline Type Type::from(T&& value) { typedef FromAny> Base; return Type::FromValueImpl()>::type(kj::fwd(value)); } inline bool Type::isVoid () const { return baseType == schema::Type::VOID && listDepth == 0; } inline bool Type::isBool () const { return baseType == schema::Type::BOOL && listDepth == 0; } inline bool Type::isInt8 () const { return baseType == schema::Type::INT8 && listDepth == 0; } inline bool Type::isInt16 () const { return baseType == schema::Type::INT16 && listDepth == 0; } inline bool Type::isInt32 () const { return baseType == schema::Type::INT32 && listDepth == 0; } inline bool Type::isInt64 () const { return baseType == schema::Type::INT64 && listDepth == 0; } inline bool Type::isUInt8 () const { return baseType == schema::Type::UINT8 && listDepth == 0; } inline bool Type::isUInt16 () const { return baseType == schema::Type::UINT16 && listDepth == 0; } inline bool Type::isUInt32 () const { return baseType == schema::Type::UINT32 && listDepth == 0; } inline bool Type::isUInt64 () const { return baseType == schema::Type::UINT64 && listDepth == 0; } inline bool Type::isFloat32() const { return baseType == schema::Type::FLOAT32 && listDepth == 0; } inline bool Type::isFloat64() const { return baseType == schema::Type::FLOAT64 && listDepth == 0; } inline bool Type::isText () const { return baseType == schema::Type::TEXT && listDepth == 0; } inline bool Type::isData () const { return baseType == schema::Type::DATA && listDepth == 0; } inline bool Type::isList () const { return listDepth > 0; } inline bool Type::isEnum () const { return baseType == schema::Type::ENUM && listDepth == 0; } inline bool Type::isStruct () const { return baseType == schema::Type::STRUCT && listDepth == 0; } inline bool Type::isInterface() const { return baseType == schema::Type::INTERFACE && listDepth == 0; } inline bool Type::isAnyPointer() const { return baseType == schema::Type::ANY_POINTER && listDepth == 0; } inline Type Type::wrapInList(uint depth) const { Type result = *this; result.listDepth += depth; return result; } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/schema-loader.h0000644000175000017500000002054013650101756021522 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "schema.h" #include #include CAPNP_BEGIN_HEADER namespace capnp { class SchemaLoader { // Class which can be used to construct Schema objects from schema::Nodes as defined in // schema.capnp. // // It is a bad idea to use this class on untrusted input with exceptions disabled -- you may // be exposing yourself to denial-of-service attacks, as attackers can easily construct schemas // that are subtly inconsistent in a way that causes exceptions to be thrown either by // SchemaLoader or by the dynamic API when the schemas are subsequently used. If you enable and // properly catch exceptions, you should be OK -- assuming no bugs in the Cap'n Proto // implementation, of course. public: class LazyLoadCallback { public: virtual void load(const SchemaLoader& loader, uint64_t id) const = 0; // Request that the schema node with the given ID be loaded into the given SchemaLoader. If // the callback is able to find a schema for this ID, it should invoke `loadOnce()` on // `loader` to load it. If no such node exists, it should simply do nothing and return. // // The callback is allowed to load schema nodes other than the one requested, e.g. because it // expects they will be needed soon. // // If the `SchemaLoader` is used from multiple threads, the callback must be thread-safe. // In particular, it's possible for multiple threads to invoke `load()` with the same ID. // If the callback performs a large amount of work to look up IDs, it should be sure to // de-dup these requests. }; SchemaLoader(); SchemaLoader(const LazyLoadCallback& callback); // Construct a SchemaLoader which will invoke the given callback when a schema node is requested // that isn't already loaded. ~SchemaLoader() noexcept(false); KJ_DISALLOW_COPY(SchemaLoader); Schema get(uint64_t id, schema::Brand::Reader brand = schema::Brand::Reader(), Schema scope = Schema()) const; // Gets the schema for the given ID, throwing an exception if it isn't present. // // The returned schema may be invalidated if load() is called with a new schema for the same ID. // In general, you should not call load() while a schema from this loader is in-use. // // `brand` and `scope` are used to determine brand bindings where relevant. `brand` gives // parameter bindings for the target type's brand parameters that were specified at the reference // site. `scope` specifies the scope in which the type ID appeared -- if `brand` itself contains // parameter references or indicates that some parameters will be inherited, these will be // interpreted within / inherited from `scope`. kj::Maybe tryGet(uint64_t id, schema::Brand::Reader bindings = schema::Brand::Reader(), Schema scope = Schema()) const; // Like get() but doesn't throw. Schema getUnbound(uint64_t id) const; // Gets a special version of the schema in which all brand parameters are "unbound". This means // that if you look up a type via the Schema API, and it resolves to a brand parameter, the // returned Type's getBrandParameter() method will return info about that parameter. Otherwise, // normally, all brand parameters that aren't otherwise bound are assumed to simply be // "AnyPointer". Type getType(schema::Type::Reader type, Schema scope = Schema()) const; // Convenience method which interprets a schema::Type to produce a Type object. Implemented in // terms of get(). Schema load(const schema::Node::Reader& reader); // Loads the given schema node. Validates the node and throws an exception if invalid. This // makes a copy of the schema, so the object passed in can be destroyed after this returns. // // If the node has any dependencies which are not already loaded, they will be initialized as // stubs -- empty schemas of whichever kind is expected. // // If another schema for the given reader has already been seen, the loader will inspect both // schemas to determine which one is newer, and use that that one. If the two versions are // found to be incompatible, an exception is thrown. If the two versions differ but are // compatible and the loader cannot determine which is newer (e.g., the only changes are renames), // the existing schema will be preferred. Note that in any case, the loader will end up keeping // around copies of both schemas, so you shouldn't repeatedly reload schemas into the same loader. // // The following properties of the schema node are validated: // - Struct size and preferred list encoding are valid and consistent. // - Struct members are fields or unions. // - Union members are fields. // - Field offsets are in-bounds. // - Ordinals and codeOrders are sequential starting from zero. // - Values are of the right union case to match their types. // // You should assume anything not listed above is NOT validated. In particular, things that are // not validated now, but could be in the future, include but are not limited to: // - Names. // - Annotation values. (This is hard because the annotation declaration is not always // available.) // - Content of default/constant values of pointer type. (Validating these would require knowing // their schema, but even if the schemas are available at validation time, they could be // updated by a subsequent load(), invalidating existing values. Instead, these values are // validated at the time they are used, as usual for Cap'n Proto objects.) // // Also note that unknown types are not considered invalid. Instead, the dynamic API returns // a DynamicValue with type UNKNOWN for these. Schema loadOnce(const schema::Node::Reader& reader) const; // Like `load()` but does nothing if a schema with the same ID is already loaded. In contrast, // `load()` would attempt to compare the schemas and take the newer one. `loadOnce()` is safe // to call even while concurrently using schemas from this loader. It should be considered an // error to call `loadOnce()` with two non-identical schemas that share the same ID, although // this error may or may not actually be detected by the implementation. template void loadCompiledTypeAndDependencies(); // Load the schema for the given compiled-in type and all of its dependencies. // // If you want to be able to cast a DynamicValue built from this SchemaLoader to the compiled-in // type using as(), you must call this method before constructing the DynamicValue. Otherwise, // as() will throw an exception complaining about type mismatch. kj::Array getAllLoaded() const; // Get a complete list of all loaded schema nodes. It is particularly useful to call this after // loadCompiledTypeAndDependencies() in order to get a flat list of all of T's transitive // dependencies. private: class Validator; class CompatibilityChecker; class Impl; class InitializerImpl; class BrandedInitializerImpl; kj::MutexGuarded> impl; void loadNative(const _::RawSchema* nativeSchema); }; template inline void SchemaLoader::loadCompiledTypeAndDependencies() { loadNative(&_::rawSchema()); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/schema-parser.h0000644000175000017500000002615013650101756021553 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "schema-loader.h" #include #include CAPNP_BEGIN_HEADER namespace capnp { class ParsedSchema; class SchemaFile; class SchemaParser { // Parses `.capnp` files to produce `Schema` objects. // // This class is thread-safe, hence all its methods are const. public: SchemaParser(); ~SchemaParser() noexcept(false); ParsedSchema parseFromDirectory( const kj::ReadableDirectory& baseDir, kj::Path path, kj::ArrayPtr importPath) const; // Parse a file from the KJ filesystem API. Throws an exception if the file doesn't exist. // // `baseDir` and `path` are used together to resolve relative imports. `path` is the source // file's path within `baseDir`. Relative imports will be interpreted relative to `path` and // will be opened using `baseDir`. Note that the KJ filesystem API prohibits "breaking out" of // a directory using "..", so relative imports will be restricted to children of `baseDir`. // // `importPath` is used for absolute imports (imports that start with a '/'). Each directory in // the array will be searched in order until a file is found. // // All `ReadableDirectory` objects must remain valid until the `SchemaParser` is destroyed. Also, // the `importPath` array must remain valid. `path` will be copied; it need not remain valid. // // This method is a shortcut, equivalent to: // parser.parseFile(SchemaFile::newDiskFile(baseDir, path, importPath))`; // // This method throws an exception if any errors are encountered in the file or in anything the // file depends on. Note that merely importing another file does not count as a dependency on // anything in the imported file -- only the imported types which are actually used are // "dependencies". // // Hint: Use kj::newDiskFilesystem() to initialize the KJ filesystem API. Usually you should do // this at a high level in your program, e.g. the main() function, and then pass down the // appropriate File/Directory objects to the components that need them. Example: // // auto fs = kj::newDiskFilesystem(); // SchemaParser parser; // auto schema = parser.parseFromDirectory(fs->getCurrent(), // kj::Path::parse("foo/bar.capnp"), nullptr); // // Hint: To use in-memory data rather than real disk, you can use kj::newInMemoryDirectory(), // write the files you want, then pass it to SchemaParser. Example: // // auto dir = kj::newInMemoryDirectory(kj::nullClock()); // auto path = kj::Path::parse("foo/bar.capnp"); // dir->openFile(path, kj::WriteMode::CREATE | kj::WriteMode::CREATE_PARENT) // ->writeAll("struct Foo {}"); // auto schema = parser.parseFromDirectory(*dir, path, nullptr); // // Hint: You can create an in-memory directory but then populate it with real files from disk, // in order to control what is visible while also avoiding reading files yourself or making // extra copies. Example: // // auto fs = kj::newDiskFilesystem(); // auto dir = kj::newInMemoryDirectory(kj::nullClock()); // auto fakePath = kj::Path::parse("foo/bar.capnp"); // auto realPath = kj::Path::parse("path/to/some/file.capnp"); // dir->transfer(fakePath, kj::WriteMode::CREATE | kj::WriteMode::CREATE_PARENT, // fs->getCurrent(), realPath, kj::TransferMode::LINK); // auto schema = parser.parseFromDirectory(*dir, fakePath, nullptr); // // In this example, note that any imports in the file will fail, since the in-memory directory // you created contains no files except the specific one you linked in. ParsedSchema parseDiskFile(kj::StringPtr displayName, kj::StringPtr diskPath, kj::ArrayPtr importPath) const CAPNP_DEPRECATED("Use parseFromDirectory() instead."); // Creates a private kj::Filesystem and uses it to parse files from the real disk. // // DO NOT USE in new code. Use parseFromDirectory() instead. // // This API has a serious problem: the file can import and embed files located anywhere on disk // using relative paths. Even if you specify no `importPath`, relative imports still work. By // using `parseFromDirectory()`, you can arrange so that imports are only allowed within a // particular directory, or even set up a dummy filesystem where other files are not visible. void setDiskFilesystem(kj::Filesystem& fs) CAPNP_DEPRECATED("Use parseFromDirectory() instead."); // Call before calling parseDiskFile() to choose an alternative disk filesystem implementation. // This exists mostly for testing purposes; new code should use parseFromDirectory() instead. // // If parseDiskFile() is called without having called setDiskFilesystem(), then // kj::newDiskFilesystem() will be used instead. ParsedSchema parseFile(kj::Own&& file) const; // Advanced interface for parsing a file that may or may not be located in any global namespace. // Most users will prefer `parseFromDirectory()`. // // If the file has already been parsed (that is, a SchemaFile that compares equal to this one // was parsed previously), the existing schema will be returned again. // // This method reports errors by calling SchemaFile::reportError() on the file where the error // is located. If that call does not throw an exception, `parseFile()` may in fact return // normally. In this case, the result is a best-effort attempt to compile the schema, but it // may be invalid or corrupt, and using it for anything may cause exceptions to be thrown. kj::Maybe getSourceInfo(Schema schema) const; // Look up source info (e.g. doc comments) for the given schema, which must have come from this // SchemaParser. Note that this will also work for implicit group and param types that don't have // a type name hence don't have a `ParsedSchema`. template inline void loadCompiledTypeAndDependencies() { // See SchemaLoader::loadCompiledTypeAndDependencies(). getLoader().loadCompiledTypeAndDependencies(); } private: struct Impl; struct DiskFileCompat; class ModuleImpl; kj::Own impl; mutable bool hadErrors = false; ModuleImpl& getModuleImpl(kj::Own&& file) const; SchemaLoader& getLoader(); friend class ParsedSchema; }; class ParsedSchema: public Schema { // ParsedSchema is an extension of Schema which also has the ability to look up nested nodes // by name. See `SchemaParser`. public: inline ParsedSchema(): parser(nullptr) {} kj::Maybe findNested(kj::StringPtr name) const; // Gets the nested node with the given name, or returns null if there is no such nested // declaration. ParsedSchema getNested(kj::StringPtr name) const; // Gets the nested node with the given name, or throws an exception if there is no such nested // declaration. schema::Node::SourceInfo::Reader getSourceInfo() const; // Get the source info for this schema. private: inline ParsedSchema(Schema inner, const SchemaParser& parser): Schema(inner), parser(&parser) {} const SchemaParser* parser; friend class SchemaParser; }; // ======================================================================================= // Advanced API class SchemaFile { // Abstract interface representing a schema file. You can implement this yourself in order to // gain more control over how the compiler resolves imports and reads files. For the // common case of files on disk or other global filesystem-like namespaces, use // `SchemaFile::newDiskFile()`. public: // Note: Cap'n Proto 0.6.x and below had classes FileReader and DiskFileReader and a method // newDiskFile() defined here. These were removed when SchemaParser was transitioned to use the // KJ filesystem API. You should be able to get the same effect by subclassing // kj::ReadableDirectory, or using kj::newInMemoryDirectory(). static kj::Own newFromDirectory( const kj::ReadableDirectory& baseDir, kj::Path path, kj::ArrayPtr importPath, kj::Maybe displayNameOverride = nullptr); // Construct a SchemaFile representing a file in a kj::ReadableDirectory. This is used to // implement SchemaParser::parseFromDirectory(); see there for details. // // The SchemaFile compares equal to any other SchemaFile that has exactly the same `baseDir` // object (by identity) and `path` (by value). // ----------------------------------------------------------------- // For more control, you can implement this interface. virtual kj::StringPtr getDisplayName() const = 0; // Get the file's name, as it should appear in the schema. virtual kj::Array readContent() const = 0; // Read the file's entire content and return it as a byte array. virtual kj::Maybe> import(kj::StringPtr path) const = 0; // Resolve an import, relative to this file. // // `path` is exactly what appears between quotes after the `import` keyword in the source code. // It is entirely up to the `SchemaFile` to decide how to map this to another file. Typically, // a leading '/' means that the file is an "absolute" path and is searched for in some list of // schema file repositories. On the other hand, a path that doesn't start with '/' is relative // to the importing file. virtual bool operator==(const SchemaFile& other) const = 0; virtual bool operator!=(const SchemaFile& other) const = 0; virtual size_t hashCode() const = 0; // Compare two SchemaFiles to see if they refer to the same underlying file. This is an // optimization used to avoid the need to re-parse a file to check its ID. struct SourcePos { uint byte; uint line; uint column; }; virtual void reportError(SourcePos start, SourcePos end, kj::StringPtr message) const = 0; // Report that the file contains an error at the given interval. private: class DiskSchemaFile; }; } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/dynamic.h0000644000175000017500000017030613650101756020450 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file defines classes that can be used to manipulate messages based on schemas that are not // known until runtime. This is also useful for writing generic code that uses schemas to handle // arbitrary types in a generic way. // // Each of the classes defined here has a to() template method which converts an instance back to a // native type. This method will throw an exception if the requested type does not match the // schema. To convert native types to dynamic, use DynamicFactory. // // As always, underlying data is validated lazily, so you have to actually traverse the whole // message if you want to validate all content. #pragma once #include "schema.h" #include "layout.h" #include "message.h" #include "any.h" #include "capability.h" #include // work-around macro conflict with `VOID` CAPNP_BEGIN_HEADER namespace capnp { class MessageReader; class MessageBuilder; struct DynamicValue { DynamicValue() = delete; enum Type { UNKNOWN, // Means that the value has unknown type and content because it comes from a newer version of // the schema, or from a newer version of Cap'n Proto that has new features that this version // doesn't understand. VOID, BOOL, INT, UINT, FLOAT, TEXT, DATA, LIST, ENUM, STRUCT, CAPABILITY, ANY_POINTER }; class Reader; class Builder; class Pipeline; }; class DynamicEnum; struct DynamicStruct { DynamicStruct() = delete; class Reader; class Builder; class Pipeline; }; struct DynamicList { DynamicList() = delete; class Reader; class Builder; }; struct DynamicCapability { DynamicCapability() = delete; class Client; class Server; }; template <> class Orphan; template struct DynamicTypeFor_; template <> struct DynamicTypeFor_ { typedef DynamicEnum Type; }; template <> struct DynamicTypeFor_ { typedef DynamicStruct Type; }; template <> struct DynamicTypeFor_ { typedef DynamicList Type; }; template <> struct DynamicTypeFor_ { typedef DynamicCapability Type; }; template using DynamicTypeFor = typename DynamicTypeFor_()>::Type; template ReaderFor>> toDynamic(T&& value); template BuilderFor>> toDynamic(T&& value); template DynamicTypeFor> toDynamic(T&& value); template typename DynamicTypeFor>::Client toDynamic(kj::Own&& value); namespace _ { // private template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; } // namespace _ (private) template <> inline constexpr Style style() { return Style::POINTER; } template <> inline constexpr Style style() { return Style::PRIMITIVE; } template <> inline constexpr Style style() { return Style::STRUCT; } template <> inline constexpr Style style() { return Style::POINTER; } template <> inline constexpr Style style() { return Style::CAPABILITY; } // ------------------------------------------------------------------- class DynamicEnum { public: DynamicEnum() = default; inline DynamicEnum(EnumSchema::Enumerant enumerant) : schema(enumerant.getContainingEnum()), value(enumerant.getOrdinal()) {} inline DynamicEnum(EnumSchema schema, uint16_t value) : schema(schema), value(value) {} template () == Kind::ENUM>> inline DynamicEnum(T&& value): DynamicEnum(toDynamic(value)) {} template inline T as() const { return static_cast(asImpl(typeId())); } // Cast to a native enum type. inline EnumSchema getSchema() const { return schema; } kj::Maybe getEnumerant() const; // Get which enumerant this enum value represents. Returns nullptr if the numeric value does not // correspond to any enumerant in the schema -- this can happen if the data was built using a // newer schema that has more values defined. inline uint16_t getRaw() const { return value; } // Returns the raw underlying enum value. private: EnumSchema schema; uint16_t value; uint16_t asImpl(uint64_t requestedTypeId) const; friend struct DynamicStruct; friend struct DynamicList; friend struct DynamicValue; template friend DynamicTypeFor> toDynamic(T&& value); }; // ------------------------------------------------------------------- enum class HasMode: uint8_t { // Specifies the meaning of "has(field)". NON_NULL, // "has(field)" only returns false if the field is a pointer and the pointer is null. This is the // default behavior. NON_DEFAULT // "has(field)" returns false if the field is set to its default value. This differs from // NON_NULL only in the handling of primitive values. // // "Equal to default value" is technically defined as the field value being encoded as all-zero // on the wire (since primitive values are XORed by their defined default value when encoded). }; class DynamicStruct::Reader { public: typedef DynamicStruct Reads; Reader() = default; template >() == Kind::STRUCT>> inline Reader(T&& value): Reader(toDynamic(value)) {} inline operator AnyStruct::Reader() const { return AnyStruct::Reader(reader); } inline MessageSize totalSize() const { return reader.totalSize().asPublic(); } template typename T::Reader as() const; // Convert the dynamic struct to its compiled-in type. inline StructSchema getSchema() const { return schema; } DynamicValue::Reader get(StructSchema::Field field) const; // Read the given field value. bool has(StructSchema::Field field, HasMode mode = HasMode::NON_NULL) const; // Tests whether the given field is "present". If the field is a union member and is not the // active member, this always returns false. Otherwise, the field's value is interpreted // according to `mode`. kj::Maybe which() const; // If the struct contains an (unnamed) union, and the currently-active field within that union // is known, this returns that field. Otherwise, it returns null. In other words, this returns // null if there is no union present _or_ if the union's discriminant is set to an unrecognized // value. This could happen in particular when receiving a message from a sender who has a // newer version of the protocol and is using a field of the union that you don't know about yet. DynamicValue::Reader get(kj::StringPtr name) const; bool has(kj::StringPtr name, HasMode mode = HasMode::NON_NULL) const; // Shortcuts to access fields by name. These throw exceptions if no such field exists. private: StructSchema schema; _::StructReader reader; inline Reader(StructSchema schema, _::StructReader reader) : schema(schema), reader(reader) {} Reader(StructSchema schema, const _::OrphanBuilder& orphan); bool isSetInUnion(StructSchema::Field field) const; void verifySetInUnion(StructSchema::Field field) const; static DynamicValue::Reader getImpl(_::StructReader reader, StructSchema::Field field); template friend struct _::PointerHelpers; friend class DynamicStruct::Builder; friend struct DynamicList; friend class MessageReader; friend class MessageBuilder; template friend struct ::capnp::ToDynamic_; friend kj::StringTree _::structString( _::StructReader reader, const _::RawBrandedSchema& schema); friend class Orphanage; friend class Orphan; friend class Orphan; friend class Orphan; friend class AnyStruct::Reader; }; class DynamicStruct::Builder { public: typedef DynamicStruct Builds; Builder() = default; inline Builder(decltype(nullptr)) {} template >() == Kind::STRUCT>> inline Builder(T&& value): Builder(toDynamic(value)) {} inline operator AnyStruct::Builder() { return AnyStruct::Builder(builder); } inline MessageSize totalSize() const { return asReader().totalSize(); } template typename T::Builder as(); // Cast to a particular struct type. inline StructSchema getSchema() const { return schema; } DynamicValue::Builder get(StructSchema::Field field); // Read the given field value. inline bool has(StructSchema::Field field, HasMode mode = HasMode::NON_NULL) { return asReader().has(field, mode); } // Tests whether the given field is "present". If the field is a union member and is not the // active member, this always returns false. Otherwise, the field's value is interpreted // according to `mode`. kj::Maybe which(); // If the struct contains an (unnamed) union, and the currently-active field within that union // is known, this returns that field. Otherwise, it returns null. In other words, this returns // null if there is no union present _or_ if the union's discriminant is set to an unrecognized // value. This could happen in particular when receiving a message from a sender who has a // newer version of the protocol and is using a field of the union that you don't know about yet. void set(StructSchema::Field field, const DynamicValue::Reader& value); // Set the given field value. DynamicValue::Builder init(StructSchema::Field field); DynamicValue::Builder init(StructSchema::Field field, uint size); // Init a struct, list, or blob field. void adopt(StructSchema::Field field, Orphan&& orphan); Orphan disown(StructSchema::Field field); // Adopt/disown. This works even for non-pointer fields: adopt() becomes equivalent to set() // and disown() becomes like get() followed by clear(). void clear(StructSchema::Field field); // Clear a field, setting it to its default value. For pointer fields, this actually makes the // field null. DynamicValue::Builder get(kj::StringPtr name); bool has(kj::StringPtr name, HasMode mode = HasMode::NON_NULL); void set(kj::StringPtr name, const DynamicValue::Reader& value); void set(kj::StringPtr name, std::initializer_list value); DynamicValue::Builder init(kj::StringPtr name); DynamicValue::Builder init(kj::StringPtr name, uint size); void adopt(kj::StringPtr name, Orphan&& orphan); Orphan disown(kj::StringPtr name); void clear(kj::StringPtr name); // Shortcuts to access fields by name. These throw exceptions if no such field exists. Reader asReader() const; private: StructSchema schema; _::StructBuilder builder; inline Builder(StructSchema schema, _::StructBuilder builder) : schema(schema), builder(builder) {} Builder(StructSchema schema, _::OrphanBuilder& orphan); bool isSetInUnion(StructSchema::Field field); void verifySetInUnion(StructSchema::Field field); void setInUnion(StructSchema::Field field); template friend struct _::PointerHelpers; friend struct DynamicList; friend class MessageReader; friend class MessageBuilder; template friend struct ::capnp::ToDynamic_; friend class Orphanage; friend class Orphan; friend class Orphan; friend class Orphan; friend class AnyStruct::Builder; }; class DynamicStruct::Pipeline { public: typedef DynamicStruct Pipelines; inline Pipeline(decltype(nullptr)): typeless(nullptr) {} template typename T::Pipeline releaseAs(); // Convert the dynamic pipeline to its compiled-in type. inline StructSchema getSchema() { return schema; } DynamicValue::Pipeline get(StructSchema::Field field); // Read the given field value. DynamicValue::Pipeline get(kj::StringPtr name); // Get by string name. private: StructSchema schema; AnyPointer::Pipeline typeless; inline explicit Pipeline(StructSchema schema, AnyPointer::Pipeline&& typeless) : schema(schema), typeless(kj::mv(typeless)) {} friend class Request; }; // ------------------------------------------------------------------- class DynamicList::Reader { public: typedef DynamicList Reads; inline Reader(): reader(ElementSize::VOID) {} template >() == Kind::LIST>> inline Reader(T&& value): Reader(toDynamic(value)) {} inline operator AnyList::Reader() const { return AnyList::Reader(reader); } template typename T::Reader as() const; // Try to convert to any List, Data, or Text. Throws an exception if the underlying data // can't possibly represent the requested type. inline ListSchema getSchema() const { return schema; } inline uint size() const { return unbound(reader.size() / ELEMENTS); } DynamicValue::Reader operator[](uint index) const; typedef _::IndexingIterator Iterator; inline Iterator begin() const { return Iterator(this, 0); } inline Iterator end() const { return Iterator(this, size()); } private: ListSchema schema; _::ListReader reader; Reader(ListSchema schema, _::ListReader reader): schema(schema), reader(reader) {} Reader(ListSchema schema, const _::OrphanBuilder& orphan); template friend struct _::PointerHelpers; friend struct DynamicStruct; friend class DynamicList::Builder; template friend struct ::capnp::ToDynamic_; friend class Orphanage; friend class Orphan; friend class Orphan; friend class Orphan; }; class DynamicList::Builder { public: typedef DynamicList Builds; inline Builder(): builder(ElementSize::VOID) {} inline Builder(decltype(nullptr)): builder(ElementSize::VOID) {} template >() == Kind::LIST>> inline Builder(T&& value): Builder(toDynamic(value)) {} inline operator AnyList::Builder() { return AnyList::Builder(builder); } template typename T::Builder as(); // Try to convert to any List, Data, or Text. Throws an exception if the underlying data // can't possibly represent the requested type. inline ListSchema getSchema() const { return schema; } inline uint size() const { return unbound(builder.size() / ELEMENTS); } DynamicValue::Builder operator[](uint index); void set(uint index, const DynamicValue::Reader& value); DynamicValue::Builder init(uint index, uint size); void adopt(uint index, Orphan&& orphan); Orphan disown(uint index); typedef _::IndexingIterator Iterator; inline Iterator begin() { return Iterator(this, 0); } inline Iterator end() { return Iterator(this, size()); } void copyFrom(std::initializer_list value); Reader asReader() const; private: ListSchema schema; _::ListBuilder builder; Builder(ListSchema schema, _::ListBuilder builder): schema(schema), builder(builder) {} Builder(ListSchema schema, _::OrphanBuilder& orphan); template friend struct _::PointerHelpers; friend struct DynamicStruct; template friend struct ::capnp::ToDynamic_; friend class Orphanage; template friend struct _::OrphanGetImpl; friend class Orphan; friend class Orphan; friend class Orphan; }; // ------------------------------------------------------------------- class DynamicCapability::Client: public Capability::Client { public: typedef DynamicCapability Calls; typedef DynamicCapability Reads; Client() = default; template >() == Kind::INTERFACE>> inline Client(T&& client); template ()>> inline Client(kj::Own&& server); template () == Kind::INTERFACE>> typename T::Client as(); template () == Kind::INTERFACE>> typename T::Client releaseAs(); // Convert to any client type. Client upcast(InterfaceSchema requestedSchema); // Upcast to a superclass. Throws an exception if `schema` is not a superclass. inline InterfaceSchema getSchema() { return schema; } Request newRequest( InterfaceSchema::Method method, kj::Maybe sizeHint = nullptr); Request newRequest( kj::StringPtr methodName, kj::Maybe sizeHint = nullptr); private: InterfaceSchema schema; Client(InterfaceSchema schema, kj::Own&& hook) : Capability::Client(kj::mv(hook)), schema(schema) {} template inline Client(InterfaceSchema schema, kj::Own&& server); friend struct Capability; friend struct DynamicStruct; friend struct DynamicList; friend struct DynamicValue; friend class Orphan; friend class Orphan; friend class Orphan; template friend struct _::PointerHelpers; }; class DynamicCapability::Server: public Capability::Server { public: typedef DynamicCapability Serves; Server(InterfaceSchema schema): schema(schema) {} virtual kj::Promise call(InterfaceSchema::Method method, CallContext context) = 0; DispatchCallResult dispatchCall(uint64_t interfaceId, uint16_t methodId, CallContext context) override final; inline InterfaceSchema getSchema() const { return schema; } private: InterfaceSchema schema; }; template <> class Request: public DynamicStruct::Builder { // Specialization of `Request` for DynamicStruct. public: inline Request(DynamicStruct::Builder builder, kj::Own&& hook, StructSchema resultSchema) : DynamicStruct::Builder(builder), hook(kj::mv(hook)), resultSchema(resultSchema) {} RemotePromise send(); // Send the call and return a promise for the results. kj::Promise sendStreaming(); // Use when the caller is aware that the response type is StreamResult and wants to invoke // streaming behavior. It is an error to call this if the response type is not StreamResult. private: kj::Own hook; StructSchema resultSchema; friend class Capability::Client; friend struct DynamicCapability; template friend class CallContext; friend class RequestHook; }; template <> class CallContext: public kj::DisallowConstCopy { // Wrapper around CallContextHook with a specific return type. // // Methods of this class may only be called from within the server's event loop, not from other // threads. public: explicit CallContext(CallContextHook& hook, StructSchema paramType, StructSchema resultType); DynamicStruct::Reader getParams(); void releaseParams(); DynamicStruct::Builder getResults(kj::Maybe sizeHint = nullptr); DynamicStruct::Builder initResults(kj::Maybe sizeHint = nullptr); void setResults(DynamicStruct::Reader value); void adoptResults(Orphan&& value); Orphanage getResultsOrphanage(kj::Maybe sizeHint = nullptr); template kj::Promise tailCall(Request&& tailRequest); void allowCancellation(); StructSchema getParamsType() const { return paramType; } StructSchema getResultsType() const { return resultType; } private: CallContextHook* hook; StructSchema paramType; StructSchema resultType; friend class DynamicCapability::Server; }; // ------------------------------------------------------------------- // Make sure ReaderFor and BuilderFor work for DynamicEnum, DynamicStruct, and // DynamicList, so that we can define DynamicValue::as(). template <> struct ReaderFor_ { typedef DynamicEnum Type; }; template <> struct BuilderFor_ { typedef DynamicEnum Type; }; template <> struct ReaderFor_ { typedef DynamicStruct::Reader Type; }; template <> struct BuilderFor_ { typedef DynamicStruct::Builder Type; }; template <> struct ReaderFor_ { typedef DynamicList::Reader Type; }; template <> struct BuilderFor_ { typedef DynamicList::Builder Type; }; template <> struct ReaderFor_ { typedef DynamicCapability::Client Type; }; template <> struct BuilderFor_ { typedef DynamicCapability::Client Type; }; template <> struct PipelineFor_ { typedef DynamicCapability::Client Type; }; class DynamicValue::Reader { public: typedef DynamicValue Reads; inline Reader(decltype(nullptr) n = nullptr); // UNKNOWN inline Reader(Void value); inline Reader(bool value); inline Reader(char value); inline Reader(signed char value); inline Reader(short value); inline Reader(int value); inline Reader(long value); inline Reader(long long value); inline Reader(unsigned char value); inline Reader(unsigned short value); inline Reader(unsigned int value); inline Reader(unsigned long value); inline Reader(unsigned long long value); inline Reader(float value); inline Reader(double value); inline Reader(const char* value); // Text inline Reader(const Text::Reader& value); inline Reader(const Data::Reader& value); inline Reader(const DynamicList::Reader& value); inline Reader(DynamicEnum value); inline Reader(const DynamicStruct::Reader& value); inline Reader(const AnyPointer::Reader& value); inline Reader(DynamicCapability::Client& value); inline Reader(DynamicCapability::Client&& value); template ()>> inline Reader(kj::Own&& value); Reader(ConstSchema constant); template ()))> inline Reader(T&& value): Reader(toDynamic(kj::mv(value))) {} Reader(const Reader& other); Reader(Reader&& other) noexcept; ~Reader() noexcept(false); Reader& operator=(const Reader& other); Reader& operator=(Reader&& other); // Unfortunately, we cannot use the implicit definitions of these since DynamicCapability is not // trivially copyable. template inline ReaderFor as() const { return AsImpl::apply(*this); } // Use to interpret the value as some Cap'n Proto type. Allowed types are: // - Void, bool, [u]int{8,16,32,64}_t, float, double, any enum: Returns the raw value. // - Text, Data, AnyPointer, any struct type: Returns the corresponding Reader. // - List for any T listed above: Returns List::Reader. // - DynamicEnum: Returns the corresponding type. // - DynamicStruct, DynamicList: Returns the corresponding Reader. // - Any capability type, including DynamicCapability: Returns the corresponding Client. // - DynamicValue: Returns an identical Reader. Useful to avoid special-casing in generic code. // (TODO(perf): On GCC 4.8 / Clang 3.3, provide rvalue-qualified version that avoids // refcounting.) // // DynamicValue allows various implicit conversions, mostly just to make the interface friendlier. // - Any integer can be converted to any other integer type so long as the actual value is within // the new type's range. // - Floating-point types can be converted to integers as long as no information would be lost // in the conversion. // - Integers can be converted to floating points. This may lose information, but won't throw. // - Float32/Float64 can be converted between each other. Converting Float64 -> Float32 may lose // information, but won't throw. // - Text can be converted to an enum, if the Text matches one of the enumerant names (but not // vice-versa). // - Capabilities can be upcast (cast to a supertype), but not downcast. // // Any other conversion attempt will throw an exception. inline Type getType() const { return type; } // Get the type of this value. private: Type type; union { Void voidValue; bool boolValue; int64_t intValue; uint64_t uintValue; double floatValue; Text::Reader textValue; Data::Reader dataValue; DynamicList::Reader listValue; DynamicEnum enumValue; DynamicStruct::Reader structValue; AnyPointer::Reader anyPointerValue; mutable DynamicCapability::Client capabilityValue; // Declared mutable because `Client`s normally cannot be const. // Warning: Copy/move constructors assume all these types are trivially copyable except // Capability. }; template ()> struct AsImpl; // Implementation backing the as() method. Needs to be a struct to allow partial // specialization. Has a method apply() which does the work. friend class Orphanage; // to speed up newOrphanCopy(DynamicValue::Reader) }; class DynamicValue::Builder { public: typedef DynamicValue Builds; inline Builder(decltype(nullptr) n = nullptr); // UNKNOWN inline Builder(Void value); inline Builder(bool value); inline Builder(char value); inline Builder(signed char value); inline Builder(short value); inline Builder(int value); inline Builder(long value); inline Builder(long long value); inline Builder(unsigned char value); inline Builder(unsigned short value); inline Builder(unsigned int value); inline Builder(unsigned long value); inline Builder(unsigned long long value); inline Builder(float value); inline Builder(double value); inline Builder(Text::Builder value); inline Builder(Data::Builder value); inline Builder(DynamicList::Builder value); inline Builder(DynamicEnum value); inline Builder(DynamicStruct::Builder value); inline Builder(AnyPointer::Builder value); inline Builder(DynamicCapability::Client& value); inline Builder(DynamicCapability::Client&& value); template ()))> inline Builder(T value): Builder(toDynamic(value)) {} Builder(Builder& other); Builder(Builder&& other) noexcept; ~Builder() noexcept(false); Builder& operator=(Builder& other); Builder& operator=(Builder&& other); // Unfortunately, we cannot use the implicit definitions of these since DynamicCapability is not // trivially copyable. template inline BuilderFor as() { return AsImpl::apply(*this); } // See DynamicValue::Reader::as(). inline Type getType() { return type; } // Get the type of this value. Reader asReader() const; private: Type type; union { Void voidValue; bool boolValue; int64_t intValue; uint64_t uintValue; double floatValue; Text::Builder textValue; Data::Builder dataValue; DynamicList::Builder listValue; DynamicEnum enumValue; DynamicStruct::Builder structValue; AnyPointer::Builder anyPointerValue; mutable DynamicCapability::Client capabilityValue; // Declared mutable because `Client`s normally cannot be const. }; template ()> struct AsImpl; // Implementation backing the as() method. Needs to be a struct to allow partial // specialization. Has a method apply() which does the work. friend class Orphan; }; class DynamicValue::Pipeline { public: typedef DynamicValue Pipelines; inline Pipeline(decltype(nullptr) n = nullptr); inline Pipeline(DynamicStruct::Pipeline&& value); inline Pipeline(DynamicCapability::Client&& value); Pipeline(Pipeline&& other) noexcept; Pipeline& operator=(Pipeline&& other); ~Pipeline() noexcept(false); template inline PipelineFor releaseAs() { return AsImpl::apply(*this); } inline Type getType() { return type; } // Get the type of this value. private: Type type; union { DynamicStruct::Pipeline structValue; DynamicCapability::Client capabilityValue; }; template ()> struct AsImpl; // Implementation backing the releaseAs() method. Needs to be a struct to allow partial // specialization. Has a method apply() which does the work. }; kj::StringTree KJ_STRINGIFY(const DynamicValue::Reader& value); kj::StringTree KJ_STRINGIFY(const DynamicValue::Builder& value); kj::StringTree KJ_STRINGIFY(DynamicEnum value); kj::StringTree KJ_STRINGIFY(const DynamicStruct::Reader& value); kj::StringTree KJ_STRINGIFY(const DynamicStruct::Builder& value); kj::StringTree KJ_STRINGIFY(const DynamicList::Reader& value); kj::StringTree KJ_STRINGIFY(const DynamicList::Builder& value); // ------------------------------------------------------------------- // Orphan <-> Dynamic glue template <> class Orphan { public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; template () == Kind::STRUCT>> inline Orphan(Orphan&& other): schema(Schema::from()), builder(kj::mv(other.builder)) {} DynamicStruct::Builder get(); DynamicStruct::Reader getReader() const; template Orphan releaseAs(); // Like DynamicStruct::Builder::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: StructSchema schema; _::OrphanBuilder builder; inline Orphan(StructSchema schema, _::OrphanBuilder&& builder) : schema(schema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicList; friend class Orphanage; friend class Orphan; friend class Orphan; friend class MessageBuilder; }; template <> class Orphan { public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; template () == Kind::LIST>> inline Orphan(Orphan&& other): schema(Schema::from()), builder(kj::mv(other.builder)) {} DynamicList::Builder get(); DynamicList::Reader getReader() const; template Orphan releaseAs(); // Like DynamicList::Builder::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. // TODO(someday): Support truncate(). inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: ListSchema schema; _::OrphanBuilder builder; inline Orphan(ListSchema schema, _::OrphanBuilder&& builder) : schema(schema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicList; friend class Orphanage; friend class Orphan; friend class Orphan; }; template <> class Orphan { public: Orphan() = default; KJ_DISALLOW_COPY(Orphan); Orphan(Orphan&&) = default; Orphan& operator=(Orphan&&) = default; template () == Kind::INTERFACE>> inline Orphan(Orphan&& other): schema(Schema::from()), builder(kj::mv(other.builder)) {} DynamicCapability::Client get(); DynamicCapability::Client getReader() const; template Orphan releaseAs(); // Like DynamicCapability::Client::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. inline bool operator==(decltype(nullptr)) const { return builder == nullptr; } inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; } private: InterfaceSchema schema; _::OrphanBuilder builder; inline Orphan(InterfaceSchema schema, _::OrphanBuilder&& builder) : schema(schema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicList; friend class Orphanage; friend class Orphan; friend class Orphan; }; template <> class Orphan { public: inline Orphan(decltype(nullptr) n = nullptr): type(DynamicValue::UNKNOWN) {} inline Orphan(Void value); inline Orphan(bool value); inline Orphan(char value); inline Orphan(signed char value); inline Orphan(short value); inline Orphan(int value); inline Orphan(long value); inline Orphan(long long value); inline Orphan(unsigned char value); inline Orphan(unsigned short value); inline Orphan(unsigned int value); inline Orphan(unsigned long value); inline Orphan(unsigned long long value); inline Orphan(float value); inline Orphan(double value); inline Orphan(DynamicEnum value); Orphan(Orphan&&) = default; template Orphan(Orphan&&); Orphan(Orphan&&); Orphan(void*) = delete; // So Orphan(bool) doesn't accept pointers. KJ_DISALLOW_COPY(Orphan); Orphan& operator=(Orphan&&) = default; inline DynamicValue::Type getType() { return type; } DynamicValue::Builder get(); DynamicValue::Reader getReader() const; template Orphan releaseAs(); // Like DynamicValue::Builder::as(), but coerces the Orphan type. Since Orphans are move-only, // the original Orphan is no longer valid after this call; ownership is // transferred to the returned Orphan. private: DynamicValue::Type type; union { Void voidValue; bool boolValue; int64_t intValue; uint64_t uintValue; double floatValue; DynamicEnum enumValue; StructSchema structSchema; ListSchema listSchema; InterfaceSchema interfaceSchema; }; _::OrphanBuilder builder; // Only used if `type` is a pointer type. Orphan(DynamicValue::Builder value, _::OrphanBuilder&& builder); Orphan(DynamicValue::Type type, _::OrphanBuilder&& builder) : type(type), builder(kj::mv(builder)) {} Orphan(StructSchema structSchema, _::OrphanBuilder&& builder) : type(DynamicValue::STRUCT), structSchema(structSchema), builder(kj::mv(builder)) {} Orphan(ListSchema listSchema, _::OrphanBuilder&& builder) : type(DynamicValue::LIST), listSchema(listSchema), builder(kj::mv(builder)) {} template friend struct _::PointerHelpers; friend struct DynamicStruct; friend struct DynamicList; friend struct AnyPointer; friend class Orphanage; }; template inline Orphan::Orphan(Orphan&& other) : Orphan(other.get(), kj::mv(other.builder)) {} inline Orphan::Orphan(Orphan&& other) : type(DynamicValue::ANY_POINTER), builder(kj::mv(other.builder)) {} template Orphan Orphan::releaseAs() { get().as(); // type check return Orphan(kj::mv(builder)); } template Orphan Orphan::releaseAs() { get().as(); // type check return Orphan(kj::mv(builder)); } template Orphan Orphan::releaseAs() { get().as(); // type check return Orphan(kj::mv(builder)); } template Orphan Orphan::releaseAs() { get().as(); // type check type = DynamicValue::UNKNOWN; return Orphan(kj::mv(builder)); } template <> Orphan Orphan::releaseAs(); template <> Orphan Orphan::releaseAs(); template <> Orphan Orphan::releaseAs(); template <> Orphan Orphan::releaseAs(); template <> struct Orphanage::GetInnerBuilder { static inline _::StructBuilder apply(DynamicStruct::Builder& t) { return t.builder; } }; template <> struct Orphanage::GetInnerBuilder { static inline _::ListBuilder apply(DynamicList::Builder& t) { return t.builder; } }; template <> inline Orphan Orphanage::newOrphanCopy( DynamicStruct::Reader copyFrom) const { return Orphan( copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.reader)); } template <> inline Orphan Orphanage::newOrphanCopy( DynamicList::Reader copyFrom) const { return Orphan(copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.reader)); } template <> inline Orphan Orphanage::newOrphanCopy( DynamicCapability::Client copyFrom) const { return Orphan( copyFrom.getSchema(), _::OrphanBuilder::copy(arena, capTable, copyFrom.hook->addRef())); } template <> Orphan Orphanage::newOrphanCopy( DynamicValue::Reader copyFrom) const; namespace _ { // private template <> struct PointerHelpers { // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we // don't want people to accidentally be able to provide their own default value. static DynamicStruct::Reader getDynamic(PointerReader reader, StructSchema schema); static DynamicStruct::Builder getDynamic(PointerBuilder builder, StructSchema schema); static void set(PointerBuilder builder, const DynamicStruct::Reader& value); static DynamicStruct::Builder init(PointerBuilder builder, StructSchema schema); static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder, StructSchema schema) { return Orphan(schema, builder.disown()); } }; template <> struct PointerHelpers { // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we // don't want people to accidentally be able to provide their own default value. static DynamicList::Reader getDynamic(PointerReader reader, ListSchema schema); static DynamicList::Builder getDynamic(PointerBuilder builder, ListSchema schema); static void set(PointerBuilder builder, const DynamicList::Reader& value); static DynamicList::Builder init(PointerBuilder builder, ListSchema schema, uint size); static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder, ListSchema schema) { return Orphan(schema, builder.disown()); } }; template <> struct PointerHelpers { // getDynamic() is used when an AnyPointer's get() accessor is passed arguments, because for // non-dynamic types PointerHelpers::get() takes a default value as the third argument, and we // don't want people to accidentally be able to provide their own default value. static DynamicCapability::Client getDynamic(PointerReader reader, InterfaceSchema schema); static DynamicCapability::Client getDynamic(PointerBuilder builder, InterfaceSchema schema); static void set(PointerBuilder builder, DynamicCapability::Client& value); static void set(PointerBuilder builder, DynamicCapability::Client&& value); static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder, InterfaceSchema schema) { return Orphan(schema, builder.disown()); } }; } // namespace _ (private) template inline ReaderFor AnyPointer::Reader::getAs(StructSchema schema) const { return _::PointerHelpers::getDynamic(reader, schema); } template inline ReaderFor AnyPointer::Reader::getAs(ListSchema schema) const { return _::PointerHelpers::getDynamic(reader, schema); } template inline ReaderFor AnyPointer::Reader::getAs(InterfaceSchema schema) const { return _::PointerHelpers::getDynamic(reader, schema); } template inline BuilderFor AnyPointer::Builder::getAs(StructSchema schema) { return _::PointerHelpers::getDynamic(builder, schema); } template inline BuilderFor AnyPointer::Builder::getAs(ListSchema schema) { return _::PointerHelpers::getDynamic(builder, schema); } template inline BuilderFor AnyPointer::Builder::getAs(InterfaceSchema schema) { return _::PointerHelpers::getDynamic(builder, schema); } template inline BuilderFor AnyPointer::Builder::initAs(StructSchema schema) { return _::PointerHelpers::init(builder, schema); } template inline BuilderFor AnyPointer::Builder::initAs(ListSchema schema, uint elementCount) { return _::PointerHelpers::init(builder, schema, elementCount); } template <> inline void AnyPointer::Builder::setAs(DynamicStruct::Reader value) { return _::PointerHelpers::set(builder, value); } template <> inline void AnyPointer::Builder::setAs(DynamicList::Reader value) { return _::PointerHelpers::set(builder, value); } template <> inline void AnyPointer::Builder::setAs(DynamicCapability::Client value) { return _::PointerHelpers::set(builder, kj::mv(value)); } template <> void AnyPointer::Builder::adopt(Orphan&& orphan); template inline Orphan AnyPointer::Builder::disownAs(StructSchema schema) { return _::PointerHelpers::disown(builder, schema); } template inline Orphan AnyPointer::Builder::disownAs(ListSchema schema) { return _::PointerHelpers::disown(builder, schema); } template inline Orphan AnyPointer::Builder::disownAs(InterfaceSchema schema) { return _::PointerHelpers::disown(builder, schema); } // We have to declare the methods below inline because Clang and GCC disagree about how to mangle // their symbol names. template <> inline DynamicStruct::Builder Orphan::getAs(StructSchema schema) { return DynamicStruct::Builder(schema, builder); } template <> inline DynamicStruct::Reader Orphan::getAsReader( StructSchema schema) const { return DynamicStruct::Reader(schema, builder); } template <> inline Orphan Orphan::releaseAs(StructSchema schema) { return Orphan(schema, kj::mv(builder)); } template <> inline DynamicList::Builder Orphan::getAs(ListSchema schema) { return DynamicList::Builder(schema, builder); } template <> inline DynamicList::Reader Orphan::getAsReader(ListSchema schema) const { return DynamicList::Reader(schema, builder); } template <> inline Orphan Orphan::releaseAs(ListSchema schema) { return Orphan(schema, kj::mv(builder)); } template <> inline DynamicCapability::Client Orphan::getAs( InterfaceSchema schema) { return DynamicCapability::Client(schema, builder.asCapability()); } template <> inline DynamicCapability::Client Orphan::getAsReader( InterfaceSchema schema) const { return DynamicCapability::Client(schema, builder.asCapability()); } template <> inline Orphan Orphan::releaseAs( InterfaceSchema schema) { return Orphan(schema, kj::mv(builder)); } // ======================================================================================= // Inline implementation details. template struct ToDynamic_ { static inline DynamicStruct::Reader apply(const typename T::Reader& value) { return DynamicStruct::Reader(Schema::from(), value._reader); } static inline DynamicStruct::Builder apply(typename T::Builder& value) { return DynamicStruct::Builder(Schema::from(), value._builder); } }; template struct ToDynamic_ { static inline DynamicList::Reader apply(const typename T::Reader& value) { return DynamicList::Reader(Schema::from(), value.reader); } static inline DynamicList::Builder apply(typename T::Builder& value) { return DynamicList::Builder(Schema::from(), value.builder); } }; template struct ToDynamic_ { static inline DynamicCapability::Client apply(typename T::Client value) { return DynamicCapability::Client(kj::mv(value)); } static inline DynamicCapability::Client apply(typename T::Client&& value) { return DynamicCapability::Client(kj::mv(value)); } }; template ReaderFor>> toDynamic(T&& value) { return ToDynamic_>::apply(value); } template BuilderFor>> toDynamic(T&& value) { return ToDynamic_>::apply(value); } template DynamicTypeFor> toDynamic(T&& value) { return DynamicEnum(Schema::from>(), static_cast(value)); } template typename DynamicTypeFor>::Client toDynamic(kj::Own&& value) { return typename FromServer::Client(kj::mv(value)); } inline DynamicValue::Reader::Reader(std::nullptr_t n): type(UNKNOWN) {} inline DynamicValue::Builder::Builder(std::nullptr_t n): type(UNKNOWN) {} #define CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \ inline DynamicValue::Reader::Reader(cppType value) \ : type(typeTag), fieldName##Value(value) {} \ inline DynamicValue::Builder::Builder(cppType value) \ : type(typeTag), fieldName##Value(value) {} \ inline Orphan::Orphan(cppType value) \ : type(DynamicValue::typeTag), fieldName##Value(value) {} CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Void, VOID, void); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(bool, BOOL, bool); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(char, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(signed char, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(short, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(int, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(long, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(long long, INT, int); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned char, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned short, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned int, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned long, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(unsigned long long, UINT, uint); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(float, FLOAT, float); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(double, FLOAT, float); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicEnum, ENUM, enum); #undef CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR #define CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(cppType, typeTag, fieldName) \ inline DynamicValue::Reader::Reader(const cppType::Reader& value) \ : type(typeTag), fieldName##Value(value) {} \ inline DynamicValue::Builder::Builder(cppType::Builder value) \ : type(typeTag), fieldName##Value(value) {} CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Text, TEXT, text); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(Data, DATA, data); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicList, LIST, list); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(DynamicStruct, STRUCT, struct); CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR(AnyPointer, ANY_POINTER, anyPointer); #undef CAPNP_DECLARE_DYNAMIC_VALUE_CONSTRUCTOR inline DynamicValue::Reader::Reader(DynamicCapability::Client& value) : type(CAPABILITY), capabilityValue(value) {} inline DynamicValue::Reader::Reader(DynamicCapability::Client&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} template inline DynamicValue::Reader::Reader(kj::Own&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} inline DynamicValue::Builder::Builder(DynamicCapability::Client& value) : type(CAPABILITY), capabilityValue(value) {} inline DynamicValue::Builder::Builder(DynamicCapability::Client&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} inline DynamicValue::Reader::Reader(const char* value): Reader(Text::Reader(value)) {} #define CAPNP_DECLARE_TYPE(discrim, typeName) \ template <> \ struct DynamicValue::Reader::AsImpl { \ static ReaderFor apply(const Reader& reader); \ }; \ template <> \ struct DynamicValue::Builder::AsImpl { \ static BuilderFor apply(Builder& builder); \ }; //CAPNP_DECLARE_TYPE(VOID, Void) CAPNP_DECLARE_TYPE(BOOL, bool) CAPNP_DECLARE_TYPE(INT8, int8_t) CAPNP_DECLARE_TYPE(INT16, int16_t) CAPNP_DECLARE_TYPE(INT32, int32_t) CAPNP_DECLARE_TYPE(INT64, int64_t) CAPNP_DECLARE_TYPE(UINT8, uint8_t) CAPNP_DECLARE_TYPE(UINT16, uint16_t) CAPNP_DECLARE_TYPE(UINT32, uint32_t) CAPNP_DECLARE_TYPE(UINT64, uint64_t) CAPNP_DECLARE_TYPE(FLOAT32, float) CAPNP_DECLARE_TYPE(FLOAT64, double) CAPNP_DECLARE_TYPE(TEXT, Text) CAPNP_DECLARE_TYPE(DATA, Data) CAPNP_DECLARE_TYPE(LIST, DynamicList) CAPNP_DECLARE_TYPE(STRUCT, DynamicStruct) CAPNP_DECLARE_TYPE(INTERFACE, DynamicCapability) CAPNP_DECLARE_TYPE(ENUM, DynamicEnum) CAPNP_DECLARE_TYPE(ANY_POINTER, AnyPointer) #undef CAPNP_DECLARE_TYPE // CAPNP_DECLARE_TYPE(Void) causes gcc 4.7 to segfault. If I do it manually and remove the // ReaderFor<> and BuilderFor<> wrappers, it works. template <> struct DynamicValue::Reader::AsImpl { static Void apply(const Reader& reader); }; template <> struct DynamicValue::Builder::AsImpl { static Void apply(Builder& builder); }; template struct DynamicValue::Reader::AsImpl { static T apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static T apply(Builder& builder) { return builder.as().as(); } }; template struct DynamicValue::Reader::AsImpl { static typename T::Reader apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static typename T::Builder apply(Builder& builder) { return builder.as().as(); } }; template struct DynamicValue::Reader::AsImpl { static typename T::Reader apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static typename T::Builder apply(Builder& builder) { return builder.as().as(); } }; template struct DynamicValue::Reader::AsImpl { static typename T::Client apply(const Reader& reader) { return reader.as().as(); } }; template struct DynamicValue::Builder::AsImpl { static typename T::Client apply(Builder& builder) { return builder.as().as(); } }; template <> struct DynamicValue::Reader::AsImpl { static DynamicValue::Reader apply(const Reader& reader) { return reader; } }; template <> struct DynamicValue::Builder::AsImpl { static DynamicValue::Builder apply(Builder& builder) { return builder; } }; inline DynamicValue::Pipeline::Pipeline(std::nullptr_t n): type(UNKNOWN) {} inline DynamicValue::Pipeline::Pipeline(DynamicStruct::Pipeline&& value) : type(STRUCT), structValue(kj::mv(value)) {} inline DynamicValue::Pipeline::Pipeline(DynamicCapability::Client&& value) : type(CAPABILITY), capabilityValue(kj::mv(value)) {} template struct DynamicValue::Pipeline::AsImpl { static typename T::Pipeline apply(Pipeline& pipeline) { return pipeline.releaseAs().releaseAs(); } }; template struct DynamicValue::Pipeline::AsImpl { static typename T::Client apply(Pipeline& pipeline) { return pipeline.releaseAs().releaseAs(); } }; template <> struct DynamicValue::Pipeline::AsImpl { static PipelineFor apply(Pipeline& pipeline); }; template <> struct DynamicValue::Pipeline::AsImpl { static PipelineFor apply(Pipeline& pipeline); }; // ------------------------------------------------------------------- template typename T::Reader DynamicStruct::Reader::as() const { static_assert(kind() == Kind::STRUCT, "DynamicStruct::Reader::as() can only convert to struct types."); schema.requireUsableAs(); return typename T::Reader(reader); } template typename T::Builder DynamicStruct::Builder::as() { static_assert(kind() == Kind::STRUCT, "DynamicStruct::Builder::as() can only convert to struct types."); schema.requireUsableAs(); return typename T::Builder(builder); } template <> inline DynamicStruct::Reader DynamicStruct::Reader::as() const { return *this; } template <> inline DynamicStruct::Builder DynamicStruct::Builder::as() { return *this; } inline DynamicStruct::Reader DynamicStruct::Builder::asReader() const { return DynamicStruct::Reader(schema, builder.asReader()); } template <> inline AnyStruct::Reader DynamicStruct::Reader::as() const { return AnyStruct::Reader(reader); } template <> inline AnyStruct::Builder DynamicStruct::Builder::as() { return AnyStruct::Builder(builder); } template <> inline DynamicStruct::Reader AnyStruct::Reader::as(StructSchema schema) const { return DynamicStruct::Reader(schema, _reader); } template <> inline DynamicStruct::Builder AnyStruct::Builder::as(StructSchema schema) { return DynamicStruct::Builder(schema, _builder); } template typename T::Pipeline DynamicStruct::Pipeline::releaseAs() { static_assert(kind() == Kind::STRUCT, "DynamicStruct::Pipeline::releaseAs() can only convert to struct types."); schema.requireUsableAs(); return typename T::Pipeline(kj::mv(typeless)); } // ------------------------------------------------------------------- template typename T::Reader DynamicList::Reader::as() const { static_assert(kind() == Kind::LIST, "DynamicStruct::Reader::as() can only convert to list types."); schema.requireUsableAs(); return typename T::Reader(reader); } template typename T::Builder DynamicList::Builder::as() { static_assert(kind() == Kind::LIST, "DynamicStruct::Builder::as() can only convert to list types."); schema.requireUsableAs(); return typename T::Builder(builder); } template <> inline DynamicList::Reader DynamicList::Reader::as() const { return *this; } template <> inline DynamicList::Builder DynamicList::Builder::as() { return *this; } template <> inline AnyList::Reader DynamicList::Reader::as() const { return AnyList::Reader(reader); } template <> inline AnyList::Builder DynamicList::Builder::as() { return AnyList::Builder(builder); } // ------------------------------------------------------------------- template inline DynamicCapability::Client::Client(T&& client) : Capability::Client(kj::mv(client)), schema(Schema::from>()) {} template inline DynamicCapability::Client::Client(kj::Own&& server) : Client(server->getSchema(), kj::mv(server)) {} template inline DynamicCapability::Client::Client(InterfaceSchema schema, kj::Own&& server) : Capability::Client(kj::mv(server)), schema(schema) {} template typename T::Client DynamicCapability::Client::as() { static_assert(kind() == Kind::INTERFACE, "DynamicCapability::Client::as() can only convert to interface types."); schema.requireUsableAs(); return typename T::Client(hook->addRef()); } template typename T::Client DynamicCapability::Client::releaseAs() { static_assert(kind() == Kind::INTERFACE, "DynamicCapability::Client::as() can only convert to interface types."); schema.requireUsableAs(); return typename T::Client(kj::mv(hook)); } inline CallContext::CallContext( CallContextHook& hook, StructSchema paramType, StructSchema resultType) : hook(&hook), paramType(paramType), resultType(resultType) {} inline DynamicStruct::Reader CallContext::getParams() { return hook->getParams().getAs(paramType); } inline void CallContext::releaseParams() { hook->releaseParams(); } inline DynamicStruct::Builder CallContext::getResults( kj::Maybe sizeHint) { return hook->getResults(sizeHint).getAs(resultType); } inline DynamicStruct::Builder CallContext::initResults( kj::Maybe sizeHint) { return hook->getResults(sizeHint).initAs(resultType); } inline void CallContext::setResults(DynamicStruct::Reader value) { hook->getResults(value.totalSize()).setAs(value); } inline void CallContext::adoptResults(Orphan&& value) { hook->getResults(MessageSize { 0, 0 }).adopt(kj::mv(value)); } inline Orphanage CallContext::getResultsOrphanage( kj::Maybe sizeHint) { return Orphanage::getForMessageContaining(hook->getResults(sizeHint)); } template inline kj::Promise CallContext::tailCall( Request&& tailRequest) { return hook->tailCall(kj::mv(tailRequest.hook)); } inline void CallContext::allowCancellation() { hook->allowCancellation(); } template <> inline DynamicCapability::Client Capability::Client::castAs( InterfaceSchema schema) { return DynamicCapability::Client(schema, hook->addRef()); } // ------------------------------------------------------------------- template ReaderFor ConstSchema::as() const { return DynamicValue::Reader(*this).as(); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/pretty-print.h0000644000175000017500000000360313650101756021500 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "dynamic.h" #include CAPNP_BEGIN_HEADER namespace capnp { kj::StringTree prettyPrint(DynamicStruct::Reader value); kj::StringTree prettyPrint(DynamicStruct::Builder value); kj::StringTree prettyPrint(DynamicList::Reader value); kj::StringTree prettyPrint(DynamicList::Builder value); // Print the given Cap'n Proto struct or list with nice indentation. Note that you can pass any // struct or list reader or builder type to this method, since they can be implicitly converted // to one of the dynamic types. // // If you don't want indentation, just use the value's KJ stringifier (e.g. pass it to kj::str(), // any of the KJ debug macros, etc.). } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/serialize.h0000644000175000017500000002643713650101756021020 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file implements a simple serialization format for Cap'n Proto messages. The format // is as follows: // // * 32-bit little-endian segment count (4 bytes). // * 32-bit little-endian size of each segment (4*(segment count) bytes). // * Padding so that subsequent data is 64-bit-aligned (0 or 4 bytes). (I.e., if there are an even // number of segments, there are 4 bytes of zeros here, otherwise there is no padding.) // * Data from each segment, in order (8*sum(segment sizes) bytes) // // This format has some important properties: // - It is self-delimiting, so multiple messages may be written to a stream without any external // delimiter. // - The total size and position of each segment can be determined by reading only the first part // of the message, allowing lazy and random-access reading of the segment data. // - A message is always at least 8 bytes. // - A single-segment message can be read entirely in two system calls with no buffering. // - A multi-segment message can be read entirely in three system calls with no buffering. // - The format is appropriate for mmap()ing since all data is aligned. #pragma once #include "message.h" #include CAPNP_BEGIN_HEADER namespace capnp { class FlatArrayMessageReader: public MessageReader { // Parses a message from a flat array. Note that it makes sense to use this together with mmap() // for extremely fast parsing. public: FlatArrayMessageReader(kj::ArrayPtr array, ReaderOptions options = ReaderOptions()); // The array must remain valid until the MessageReader is destroyed. kj::ArrayPtr getSegment(uint id) override; const word* getEnd() const { return end; } // Get a pointer just past the end of the message as determined by reading the message header. // This could actually be before the end of the input array. This pointer is useful e.g. if // you know that the input array has extra stuff appended after the message and you want to // get at it. private: // Optimize for single-segment case. kj::ArrayPtr segment0; kj::Array> moreSegments; const word* end; }; kj::ArrayPtr initMessageBuilderFromFlatArrayCopy( kj::ArrayPtr array, MessageBuilder& target, ReaderOptions options = ReaderOptions()); // Convenience function which reads a message using `FlatArrayMessageReader` then copies the // content into the target `MessageBuilder`, verifying that the message structure is valid // (although not necessarily that it matches the desired schema). // // Returns an ArrayPtr containing any words left over in the array after consuming the whole // message. This is useful when reading multiple messages that have been concatenated. See also // FlatArrayMessageReader::getEnd(). // // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.) kj::Array messageToFlatArray(MessageBuilder& builder); // Constructs a flat array containing the entire content of the given message. // // To output the message as bytes, use `.asBytes()` on the returned word array. Keep in mind that // `asBytes()` returns an ArrayPtr, so you have to save the Array as well to prevent it from being // deleted. For example: // // kj::Array words = messageToFlatArray(myMessage); // kj::ArrayPtr bytes = words.asBytes(); // write(fd, bytes.begin(), bytes.size()); kj::Array messageToFlatArray(kj::ArrayPtr> segments); // Version of messageToFlatArray that takes a raw segment array. size_t computeSerializedSizeInWords(MessageBuilder& builder); // Returns the size, in words, that will be needed to serialize the message, including the header. size_t computeSerializedSizeInWords(kj::ArrayPtr> segments); // Version of computeSerializedSizeInWords that takes a raw segment array. size_t expectedSizeInWordsFromPrefix(kj::ArrayPtr messagePrefix); // Given a prefix of a serialized message, try to determine the expected total size of the message, // in words. The returned size is based on the information known so far; it may be an underestimate // if the prefix doesn't contain the full segment table. // // If the returned value is greater than `messagePrefix.size()`, then the message is not yet // complete and the app cannot parse it yet. If the returned value is less than or equal to // `messagePrefix.size()`, then the returned value is the exact total size of the message; any // remaining bytes are part of the next message. // // This function is useful when reading messages from a stream in an asynchronous way, but when // using the full KJ async infrastructure would be too difficult. Each time bytes are received, // use this function to determine if an entire message is ready to be parsed. // ======================================================================================= class InputStreamMessageReader: public MessageReader { // A MessageReader that reads from an abstract kj::InputStream. See also StreamFdMessageReader // for a subclass specific to file descriptors. public: InputStreamMessageReader(kj::InputStream& inputStream, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); ~InputStreamMessageReader() noexcept(false); // implements MessageReader ---------------------------------------- kj::ArrayPtr getSegment(uint id) override; private: kj::InputStream& inputStream; byte* readPos; // Optimize for single-segment case. kj::ArrayPtr segment0; kj::Array> moreSegments; kj::Array ownedSpace; // Only if scratchSpace wasn't big enough. kj::UnwindDetector unwindDetector; }; void readMessageCopy(kj::InputStream& input, MessageBuilder& target, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Convenience function which reads a message using `InputStreamMessageReader` then copies the // content into the target `MessageBuilder`, verifying that the message structure is valid // (although not necessarily that it matches the desired schema). // // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.) void writeMessage(kj::OutputStream& output, MessageBuilder& builder); // Write the message to the given output stream. void writeMessage(kj::OutputStream& output, kj::ArrayPtr> segments); // Write the segment array to the given output stream. // ======================================================================================= // Specializations for reading from / writing to file descriptors. class StreamFdMessageReader: private kj::FdInputStream, public InputStreamMessageReader { // A MessageReader that reads from a stream-based file descriptor. public: StreamFdMessageReader(int fd, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr) : FdInputStream(fd), InputStreamMessageReader(*this, options, scratchSpace) {} // Read message from a file descriptor, without taking ownership of the descriptor. StreamFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr) : FdInputStream(kj::mv(fd)), InputStreamMessageReader(*this, options, scratchSpace) {} // Read a message from a file descriptor, taking ownership of the descriptor. ~StreamFdMessageReader() noexcept(false); }; void readMessageCopyFromFd(int fd, MessageBuilder& target, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Convenience function which reads a message using `StreamFdMessageReader` then copies the // content into the target `MessageBuilder`, verifying that the message structure is valid // (although not necessarily that it matches the desired schema). // // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.) void writeMessageToFd(int fd, MessageBuilder& builder); // Write the message to the given file descriptor. // // This function throws an exception on any I/O error. If your code is not exception-safe, be sure // you catch this exception at the call site. If throwing an exception is not acceptable, you // can implement your own OutputStream with arbitrary error handling and then use writeMessage(). void writeMessageToFd(int fd, kj::ArrayPtr> segments); // Write the segment array to the given file descriptor. // // This function throws an exception on any I/O error. If your code is not exception-safe, be sure // you catch this exception at the call site. If throwing an exception is not acceptable, you // can implement your own OutputStream with arbitrary error handling and then use writeMessage(). // ======================================================================================= // inline stuff inline kj::Array messageToFlatArray(MessageBuilder& builder) { return messageToFlatArray(builder.getSegmentsForOutput()); } inline size_t computeSerializedSizeInWords(MessageBuilder& builder) { return computeSerializedSizeInWords(builder.getSegmentsForOutput()); } inline void writeMessage(kj::OutputStream& output, MessageBuilder& builder) { writeMessage(output, builder.getSegmentsForOutput()); } inline void writeMessageToFd(int fd, MessageBuilder& builder) { writeMessageToFd(fd, builder.getSegmentsForOutput()); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/serialize-async.h0000644000175000017500000001002013650101756022110 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include "message.h" CAPNP_BEGIN_HEADER namespace capnp { kj::Promise> readMessage( kj::AsyncInputStream& input, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Read a message asynchronously. // // `input` must remain valid until the returned promise resolves (or is canceled). // // `scratchSpace`, if provided, must remain valid until the returned MessageReader is destroyed. kj::Promise>> tryReadMessage( kj::AsyncInputStream& input, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Like `readMessage` but returns null on EOF. kj::Promise writeMessage(kj::AsyncOutputStream& output, kj::ArrayPtr> segments) KJ_WARN_UNUSED_RESULT; kj::Promise writeMessage(kj::AsyncOutputStream& output, MessageBuilder& builder) KJ_WARN_UNUSED_RESULT; // Write asynchronously. The parameters must remain valid until the returned promise resolves. // ----------------------------------------------------------------------------- // Versions that support FD passing. struct MessageReaderAndFds { kj::Own reader; kj::ArrayPtr fds; }; kj::Promise readMessage( kj::AsyncCapabilityStream& input, kj::ArrayPtr fdSpace, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Read a message that may also have file descriptors attached, e.g. from a Unix socket with // SCM_RIGHTS. kj::Promise> tryReadMessage( kj::AsyncCapabilityStream& input, kj::ArrayPtr fdSpace, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Like `readMessage` but returns null on EOF. kj::Promise writeMessage(kj::AsyncCapabilityStream& output, kj::ArrayPtr fds, kj::ArrayPtr> segments) KJ_WARN_UNUSED_RESULT; kj::Promise writeMessage(kj::AsyncCapabilityStream& output, kj::ArrayPtr fds, MessageBuilder& builder) KJ_WARN_UNUSED_RESULT; // Write a message with FDs attached, e.g. to a Unix socket with SCM_RIGHTS. // ======================================================================================= // inline implementation details inline kj::Promise writeMessage(kj::AsyncOutputStream& output, MessageBuilder& builder) { return writeMessage(output, builder.getSegmentsForOutput()); } inline kj::Promise writeMessage( kj::AsyncCapabilityStream& output, kj::ArrayPtr fds, MessageBuilder& builder) { return writeMessage(output, fds, builder.getSegmentsForOutput()); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/serialize-packed.h0000644000175000017500000001234313650101756022234 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "serialize.h" CAPNP_BEGIN_HEADER namespace capnp { namespace _ { // private class PackedInputStream: public kj::InputStream { // An input stream that unpacks packed data with a picky constraint: The caller must read data // in the exact same size and sequence as the data was written to PackedOutputStream. public: explicit PackedInputStream(kj::BufferedInputStream& inner); KJ_DISALLOW_COPY(PackedInputStream); ~PackedInputStream() noexcept(false); // implements InputStream ------------------------------------------ size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; void skip(size_t bytes) override; private: kj::BufferedInputStream& inner; }; class PackedOutputStream: public kj::OutputStream { // An output stream that packs data. Buffers passed to `write()` must be word-aligned. public: explicit PackedOutputStream(kj::BufferedOutputStream& inner); KJ_DISALLOW_COPY(PackedOutputStream); ~PackedOutputStream() noexcept(false); // implements OutputStream ----------------------------------------- void write(const void* buffer, size_t bytes) override; private: kj::BufferedOutputStream& inner; }; } // namespace _ (private) class PackedMessageReader: private _::PackedInputStream, public InputStreamMessageReader { public: PackedMessageReader(kj::BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); KJ_DISALLOW_COPY(PackedMessageReader); ~PackedMessageReader() noexcept(false); }; class PackedFdMessageReader: private kj::FdInputStream, private kj::BufferedInputStreamWrapper, public PackedMessageReader { public: PackedFdMessageReader(int fd, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Read message from a file descriptor, without taking ownership of the descriptor. // Note that if you want to reuse the descriptor after the reader is destroyed, you'll need to // seek it, since otherwise the position is unspecified. PackedFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(), kj::ArrayPtr scratchSpace = nullptr); // Read a message from a file descriptor, taking ownership of the descriptor. KJ_DISALLOW_COPY(PackedFdMessageReader); ~PackedFdMessageReader() noexcept(false); }; void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder); void writePackedMessage(kj::BufferedOutputStream& output, kj::ArrayPtr> segments); // Write a packed message to a buffered output stream. void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder); void writePackedMessage(kj::OutputStream& output, kj::ArrayPtr> segments); // Write a packed message to an unbuffered output stream. If you intend to write multiple messages // in succession, consider wrapping your output in a buffered stream in order to reduce system // call overhead. void writePackedMessageToFd(int fd, MessageBuilder& builder); void writePackedMessageToFd(int fd, kj::ArrayPtr> segments); // Write a single packed message to the file descriptor. size_t computeUnpackedSizeInWords(kj::ArrayPtr packedBytes); // Computes the number of words to which the given packed bytes will unpack. Not intended for use // in performance-sensitive situations. // ======================================================================================= // inline stuff inline void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder) { writePackedMessage(output, builder.getSegmentsForOutput()); } inline void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder) { writePackedMessage(output, builder.getSegmentsForOutput()); } inline void writePackedMessageToFd(int fd, MessageBuilder& builder) { writePackedMessageToFd(fd, builder.getSegmentsForOutput()); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/serialize-text.h0000644000175000017500000000703513650101756021773 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Philip Quinn. // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include "dynamic.h" #include "orphan.h" #include "schema.h" CAPNP_BEGIN_HEADER namespace capnp { class TextCodec { // Reads and writes Cap'n Proto objects in a plain text format (as used in the schema // language for constants, and read/written by the 'decode' and 'encode' commands of // the capnp tool). // // This format is useful for debugging or human input, but it is not a robust alternative // to the binary format. Changes to a schema's types or names that are permitted in a // schema's binary evolution will likely break messages stored in this format. // // Note that definitions or references (to constants, other fields, or files) are not // permitted in this format. To evaluate declarations with the full expressiveness of the // schema language, see `capnp::SchemaParser`. // // Requires linking with the capnpc library. public: TextCodec(); ~TextCodec() noexcept(true); void setPrettyPrint(bool enabled); // If enabled, pads the output of `encode()` with spaces and newlines to make it more // human-readable. template kj::String encode(T&& value) const; kj::String encode(DynamicValue::Reader value) const; // Encode any Cap'n Proto value. template Orphan decode(kj::StringPtr input, Orphanage orphanage) const; // Decode a text message into a Cap'n Proto object of type T, allocated in the given // orphanage. Any errors parsing the input or assigning the fields of T are thrown as // exceptions. void decode(kj::StringPtr input, DynamicStruct::Builder output) const; // Decode a text message for a struct into the given builder. Any errors parsing the // input or assigning the fields of the output are thrown as exceptions. // TODO(someday): expose some control over the error handling? private: Orphan decode(kj::StringPtr input, Type type, Orphanage orphanage) const; bool prettyPrint; }; // ======================================================================================= // inline stuff template inline kj::String TextCodec::encode(T&& value) const { return encode(DynamicValue::Reader(ReaderFor>(kj::fwd(value)))); } template inline Orphan TextCodec::decode(kj::StringPtr input, Orphanage orphanage) const { return decode(input, Type::from(), orphanage).template releaseAs(); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/pointer-helpers.h0000644000175000017500000001434113650101756022140 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "layout.h" #include "list.h" CAPNP_BEGIN_HEADER namespace capnp { namespace _ { // private // PointerHelpers is a template class that assists in wrapping/unwrapping the low-level types in // layout.h with the high-level public API and generated types. This way, the code generator // and other templates do not have to specialize on each kind of pointer. template struct PointerHelpers { static inline typename T::Reader get(PointerReader reader, const word* defaultValue = nullptr) { return typename T::Reader(reader.getStruct(defaultValue)); } static inline typename T::Builder get(PointerBuilder builder, const word* defaultValue = nullptr) { return typename T::Builder(builder.getStruct(structSize(), defaultValue)); } static inline void set(PointerBuilder builder, typename T::Reader value) { builder.setStruct(value._reader); } static inline void setCanonical(PointerBuilder builder, typename T::Reader value) { builder.setStruct(value._reader, true); } static inline typename T::Builder init(PointerBuilder builder) { return typename T::Builder(builder.initStruct(structSize())); } static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder) { return Orphan(builder.disown()); } static inline _::StructReader getInternalReader(const typename T::Reader& reader) { return reader._reader; } static inline _::StructBuilder getInternalBuilder(typename T::Builder&& builder) { return builder._builder; } }; template struct PointerHelpers, Kind::LIST> { static inline typename List::Reader get(PointerReader reader, const word* defaultValue = nullptr) { return typename List::Reader(List::getFromPointer(reader, defaultValue)); } static inline typename List::Builder get(PointerBuilder builder, const word* defaultValue = nullptr) { return typename List::Builder(List::getFromPointer(builder, defaultValue)); } static inline void set(PointerBuilder builder, typename List::Reader value) { builder.setList(value.reader); } static inline void setCanonical(PointerBuilder builder, typename List::Reader value) { builder.setList(value.reader, true); } static void set(PointerBuilder builder, kj::ArrayPtr> value) { auto l = init(builder, value.size()); uint i = 0; for (auto& element: value) { l.set(i++, element); } } static inline typename List::Builder init(PointerBuilder builder, uint size) { return typename List::Builder(List::initPointer(builder, size)); } static inline void adopt(PointerBuilder builder, Orphan>&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan> disown(PointerBuilder builder) { return Orphan>(builder.disown()); } static inline _::ListReader getInternalReader(const typename List::Reader& reader) { return reader.reader; } static inline _::ListBuilder getInternalBuilder(typename List::Builder&& builder) { return builder.builder; } }; template struct PointerHelpers { static inline typename T::Reader get(PointerReader reader, const void* defaultValue = nullptr, uint defaultBytes = 0) { return reader.getBlob(defaultValue, bounded(defaultBytes) * BYTES); } static inline typename T::Builder get(PointerBuilder builder, const void* defaultValue = nullptr, uint defaultBytes = 0) { return builder.getBlob(defaultValue, bounded(defaultBytes) * BYTES); } static inline void set(PointerBuilder builder, typename T::Reader value) { builder.setBlob(value); } static inline void setCanonical(PointerBuilder builder, typename T::Reader value) { builder.setBlob(value); } static inline typename T::Builder init(PointerBuilder builder, uint size) { return builder.initBlob(bounded(size) * BYTES); } static inline void adopt(PointerBuilder builder, Orphan&& value) { builder.adopt(kj::mv(value.builder)); } static inline Orphan disown(PointerBuilder builder) { return Orphan(builder.disown()); } }; struct UncheckedMessage { typedef const word* Reader; }; template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; template <> struct PointerHelpers { // Reads an AnyPointer field as an unchecked message pointer. Requires that the containing // message is itself unchecked. This hack is currently private. It is used to locate default // values within encoded schemas. static inline const word* get(PointerReader reader) { return reader.getUnchecked(); } }; } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/generated-header-support.h0000644000175000017500000003245513650101756023724 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file is included from all generated headers. #pragma once #include "raw-schema.h" #include "layout.h" #include "list.h" #include "orphan.h" #include "pointer-helpers.h" #include "any.h" #include #include #include CAPNP_BEGIN_HEADER namespace capnp { class MessageBuilder; // So that it can be declared a friend. template struct ToDynamic_; // Defined in dynamic.h, needs to be declared as everyone's friend. struct DynamicStruct; // So that it can be declared a friend. struct Capability; // To declare brandBindingFor() namespace _ { // private #if !CAPNP_LITE template inline const RawSchema& rawSchema() { return *CapnpPrivate::schema; } template ::typeId> inline const RawSchema& rawSchema() { return *schemas::EnumInfo::schema; } template inline const RawBrandedSchema& rawBrandedSchema() { return *CapnpPrivate::brand(); } template ::typeId> inline const RawBrandedSchema& rawBrandedSchema() { return schemas::EnumInfo::schema->defaultBrand; } template struct ChooseBrand; // If all of `Params` are `AnyPointer`, return the type's default brand. Otherwise, return a // specific brand instance. TypeTag is the _capnpPrivate struct for the type in question. template struct ChooseBrand { // All params were AnyPointer. No specific brand needed. static constexpr _::RawBrandedSchema const* brand() { return &TypeTag::schema->defaultBrand; } }; template struct ChooseBrand: public ChooseBrand {}; // The first parameter is AnyPointer, so recurse to check the rest. template struct ChooseBrand { // At least one parameter is not AnyPointer, so use the specificBrand constant. static constexpr _::RawBrandedSchema const* brand() { return &TypeTag::specificBrand; } }; template ()> struct BrandBindingFor_; #define HANDLE_TYPE(Type, which) \ template <> \ struct BrandBindingFor_ { \ static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { \ return { which, listDepth, nullptr }; \ } \ } HANDLE_TYPE(Void, 0); HANDLE_TYPE(bool, 1); HANDLE_TYPE(int8_t, 2); HANDLE_TYPE(int16_t, 3); HANDLE_TYPE(int32_t, 4); HANDLE_TYPE(int64_t, 5); HANDLE_TYPE(uint8_t, 6); HANDLE_TYPE(uint16_t, 7); HANDLE_TYPE(uint32_t, 8); HANDLE_TYPE(uint64_t, 9); HANDLE_TYPE(float, 10); HANDLE_TYPE(double, 11); #undef HANDLE_TYPE template <> struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 12, listDepth, nullptr }; } }; template <> struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 13, listDepth, nullptr }; } }; template struct BrandBindingFor_, Kind::LIST> { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return BrandBindingFor_::get(listDepth + 1); } }; template struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 15, listDepth, nullptr }; } }; template struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 16, listDepth, T::_capnpPrivate::brand() }; } }; template struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 17, listDepth, T::_capnpPrivate::brand() }; } }; template <> struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 18, listDepth, 0, 0 }; } }; template <> struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 18, listDepth, 0, 1 }; } }; template <> struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 18, listDepth, 0, 2 }; } }; template <> struct BrandBindingFor_ { static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { return { 18, listDepth, 0, 3 }; } }; template constexpr RawBrandedSchema::Binding brandBindingFor() { return BrandBindingFor_::get(0); } kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema); kj::String enumString(uint16_t value, const RawBrandedSchema& schema); // Declared here so that we can declare inline stringify methods on generated types. // Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in. template inline kj::StringTree structString(StructReader reader) { return structString(reader, rawBrandedSchema()); } template inline kj::String enumString(T value) { return enumString(static_cast(value), rawBrandedSchema()); } #endif // !CAPNP_LITE // TODO(cleanup): Unify ConstStruct and ConstList. template class ConstStruct { public: ConstStruct() = delete; KJ_DISALLOW_COPY(ConstStruct); inline explicit constexpr ConstStruct(const word* ptr): ptr(ptr) {} inline typename T::Reader get() const { return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs(); } inline operator typename T::Reader() const { return get(); } inline typename T::Reader operator*() const { return get(); } inline TemporaryPointer operator->() const { return get(); } private: const word* ptr; }; template class ConstList { public: ConstList() = delete; KJ_DISALLOW_COPY(ConstList); inline explicit constexpr ConstList(const word* ptr): ptr(ptr) {} inline typename List::Reader get() const { return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs>(); } inline operator typename List::Reader() const { return get(); } inline typename List::Reader operator*() const { return get(); } inline TemporaryPointer::Reader> operator->() const { return get(); } private: const word* ptr; }; template class ConstText { public: ConstText() = delete; KJ_DISALLOW_COPY(ConstText); inline explicit constexpr ConstText(const word* ptr): ptr(ptr) {} inline Text::Reader get() const { return Text::Reader(reinterpret_cast(ptr), size); } inline operator Text::Reader() const { return get(); } inline Text::Reader operator*() const { return get(); } inline TemporaryPointer operator->() const { return get(); } inline kj::StringPtr toString() const { return get(); } private: const word* ptr; }; template inline kj::StringPtr KJ_STRINGIFY(const ConstText& s) { return s.get(); } template class ConstData { public: ConstData() = delete; KJ_DISALLOW_COPY(ConstData); inline explicit constexpr ConstData(const word* ptr): ptr(ptr) {} inline Data::Reader get() const { return Data::Reader(reinterpret_cast(ptr), size); } inline operator Data::Reader() const { return get(); } inline Data::Reader operator*() const { return get(); } inline TemporaryPointer operator->() const { return get(); } private: const word* ptr; }; template inline auto KJ_STRINGIFY(const ConstData& s) -> decltype(kj::toCharSequence(s.get())) { return kj::toCharSequence(s.get()); } } // namespace _ (private) template inline constexpr uint64_t typeId() { return CapnpPrivate::typeId; } template ::typeId> inline constexpr uint64_t typeId() { return id; } // typeId() returns the type ID as defined in the schema. Works with structs, enums, and // interfaces. template inline constexpr uint sizeInWords() { // Return the size, in words, of a Struct type, if allocated free-standing (not in a list). // May be useful for pre-computing space needed in order to precisely allocate messages. return unbound((upgradeBound(_::structSize().data) + _::structSize().pointers * WORDS_PER_POINTER) / WORDS); } } // namespace capnp #if _MSC_VER // MSVC doesn't understand floating-point constexpr yet. // // TODO(msvc): Remove this hack when MSVC is fixed. #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) = value #else #define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) = value #define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) #endif #if _MSC_VER // TODO(msvc): A little hack to allow MSVC to use C++14 return type deduction in cases where the // explicit type exposes bugs in the compiler. #define CAPNP_AUTO_IF_MSVC(...) auto #else #define CAPNP_AUTO_IF_MSVC(...) __VA_ARGS__ #endif #if CAPNP_LITE #define CAPNP_DECLARE_SCHEMA(id) \ extern ::capnp::word const* const bp_##id #define CAPNP_DECLARE_ENUM(type, id) \ inline ::kj::String KJ_STRINGIFY(type##_##id value) { \ return ::kj::str(static_cast(value)); \ } \ template <> struct EnumInfo { \ struct IsEnum; \ static constexpr uint64_t typeId = 0x##id; \ static inline ::capnp::word const* encodedSchema() { return bp_##id; } \ } #if _MSC_VER // TODO(msvc): MSVC doesn't expect constexprs to have definitions. #define CAPNP_DEFINE_ENUM(type, id) #else #define CAPNP_DEFINE_ENUM(type, id) \ constexpr uint64_t EnumInfo::typeId #endif #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \ struct IsStruct; \ static constexpr uint64_t typeId = 0x##id; \ static constexpr uint16_t dataWordSize = dataWordSize_; \ static constexpr uint16_t pointerCount = pointerCount_; \ static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } #else // CAPNP_LITE #define CAPNP_DECLARE_SCHEMA(id) \ extern ::capnp::word const* const bp_##id; \ extern const ::capnp::_::RawSchema s_##id #define CAPNP_DECLARE_ENUM(type, id) \ inline ::kj::String KJ_STRINGIFY(type##_##id value) { \ return ::capnp::_::enumString(value); \ } \ template <> struct EnumInfo { \ struct IsEnum; \ static constexpr uint64_t typeId = 0x##id; \ static inline ::capnp::word const* encodedSchema() { return bp_##id; } \ static constexpr ::capnp::_::RawSchema const* schema = &s_##id; \ } #define CAPNP_DEFINE_ENUM(type, id) \ constexpr uint64_t EnumInfo::typeId; \ constexpr ::capnp::_::RawSchema const* EnumInfo::schema #define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \ struct IsStruct; \ static constexpr uint64_t typeId = 0x##id; \ static constexpr ::capnp::Kind kind = ::capnp::Kind::STRUCT; \ static constexpr uint16_t dataWordSize = dataWordSize_; \ static constexpr uint16_t pointerCount = pointerCount_; \ static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \ static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id; #define CAPNP_DECLARE_INTERFACE_HEADER(id) \ struct IsInterface; \ static constexpr uint64_t typeId = 0x##id; \ static constexpr ::capnp::Kind kind = ::capnp::Kind::INTERFACE; \ static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \ static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id; #endif // CAPNP_LITE, else namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(995f9a3377c0b16e); // HACK: Forward-declare the RawSchema for StreamResult, from stream.capnp. This allows capnp // files which declare streaming methods to avoid including stream.capnp.h. } } CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/raw-schema.h0000644000175000017500000002112013650101756021040 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "common.h" // for uint and friends #if _MSC_VER #include #endif CAPNP_BEGIN_HEADER namespace capnp { namespace _ { // private struct RawSchema; struct RawBrandedSchema { // Represents a combination of a schema and bindings for its generic parameters. // // Note that while we generate one `RawSchema` per type, we generate a `RawBrandedSchema` for // every _instance_ of a generic type -- or, at least, every instance that is actually used. For // generated-code types, we use template magic to initialize these. const RawSchema* generic; // Generic type which we're branding. struct Binding { uint8_t which; // Numeric value of one of schema::Type::Which. bool isImplicitParameter; // For AnyPointer, true if it's an implicit method parameter. uint16_t listDepth; // Number of times to wrap the base type in List(). uint16_t paramIndex; // For AnyPointer. If it's a type parameter (scopeId is non-zero) or it's an implicit parameter // (isImplicitParameter is true), then this is the parameter index. Otherwise this is a numeric // value of one of schema::Type::AnyPointer::Unconstrained::Which. union { const RawBrandedSchema* schema; // for struct, enum, interface uint64_t scopeId; // for AnyPointer, if it's a type parameter }; Binding() = default; inline constexpr Binding(uint8_t which, uint16_t listDepth, const RawBrandedSchema* schema) : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(0), schema(schema) {} inline constexpr Binding(uint8_t which, uint16_t listDepth, uint64_t scopeId, uint16_t paramIndex) : which(which), isImplicitParameter(false), listDepth(listDepth), paramIndex(paramIndex), scopeId(scopeId) {} inline constexpr Binding(uint8_t which, uint16_t listDepth, uint16_t implicitParamIndex) : which(which), isImplicitParameter(true), listDepth(listDepth), paramIndex(implicitParamIndex), scopeId(0) {} }; struct Scope { uint64_t typeId; // Type ID whose parameters are being bound. const Binding* bindings; uint bindingCount; // Bindings for those parameters. bool isUnbound; // This scope is unbound, in the sense of SchemaLoader::getUnbound(). }; const Scope* scopes; // Array of enclosing scopes for which generic variables have been bound, sorted by type ID. struct Dependency { uint location; const RawBrandedSchema* schema; }; const Dependency* dependencies; // Map of branded schemas for dependencies of this type, given our brand. Only dependencies that // are branded are included in this map; if a dependency is missing, use its `defaultBrand`. uint32_t scopeCount; uint32_t dependencyCount; enum class DepKind { // Component of a Dependency::location. Specifies what sort of dependency this is. INVALID, // Mostly defined to ensure that zero is not a valid location. FIELD, // Binding needed for a field's type. The index is the field index (NOT ordinal!). METHOD_PARAMS, // Bindings needed for a method's params type. The index is the method number. METHOD_RESULTS, // Bindings needed for a method's results type. The index is the method ordinal. SUPERCLASS, // Bindings needed for a superclass type. The index is the superclass's index in the // "extends" list. CONST_TYPE // Bindings needed for the type of a constant. The index is zero. }; static inline uint makeDepLocation(DepKind kind, uint index) { // Make a number representing the location of a particular dependency within its parent // schema. return (static_cast(kind) << 24) | index; } class Initializer { public: virtual void init(const RawBrandedSchema* generic) const = 0; }; const Initializer* lazyInitializer; // Lazy initializer, invoked by ensureInitialized(). inline void ensureInitialized() const { // Lazy initialization support. Invoke to ensure that initialization has taken place. This // is required in particular when traversing the dependency list. RawSchemas for compiled-in // types are always initialized; only dynamically-loaded schemas may be lazy. #if __GNUC__ const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE); #elif _MSC_VER const Initializer* i = *static_cast(&lazyInitializer); std::atomic_thread_fence(std::memory_order_acquire); #else #error "Platform not supported" #endif if (i != nullptr) i->init(this); } inline bool isUnbound() const; // Checks if this schema is the result of calling SchemaLoader::getUnbound(), in which case // binding lookups need to be handled specially. }; struct RawSchema { // The generated code defines a constant RawSchema for every compiled declaration. // // This is an internal structure which could change in the future. uint64_t id; const word* encodedNode; // Encoded SchemaNode, readable via readMessageUnchecked(encodedNode). uint32_t encodedSize; // Size of encodedNode, in words. const RawSchema* const* dependencies; // Pointers to other types on which this one depends, sorted by ID. The schemas in this table // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before // using it. // // TODO(someday): Make this a hashtable. const uint16_t* membersByName; // Indexes of members sorted by name. Used to implement name lookup. // TODO(someday): Make this a hashtable. uint32_t dependencyCount; uint32_t memberCount; // Sizes of above tables. const uint16_t* membersByDiscriminant; // List of all member indexes ordered by discriminant value. Those which don't have a // discriminant value are listed at the end, in order by ordinal. const RawSchema* canCastTo; // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue // with this schema. This is null for all compiled-in types; it is only set by SchemaLoader on // dynamically-loaded types. class Initializer { public: virtual void init(const RawSchema* schema) const = 0; }; const Initializer* lazyInitializer; // Lazy initializer, invoked by ensureInitialized(). inline void ensureInitialized() const { // Lazy initialization support. Invoke to ensure that initialization has taken place. This // is required in particular when traversing the dependency list. RawSchemas for compiled-in // types are always initialized; only dynamically-loaded schemas may be lazy. #if __GNUC__ const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE); #elif _MSC_VER const Initializer* i = *static_cast(&lazyInitializer); std::atomic_thread_fence(std::memory_order_acquire); #else #error "Platform not supported" #endif if (i != nullptr) i->init(this); } RawBrandedSchema defaultBrand; // Specifies the brand to use for this schema if no generic parameters have been bound to // anything. Generally, in the default brand, all generic parameters are treated as if they were // bound to `AnyPointer`. }; inline bool RawBrandedSchema::isUnbound() const { // The unbound schema is the only one that has no scopes but is not the default schema. return scopeCount == 0 && this != &generic->defaultBrand; } } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/rpc-prelude.h0000644000175000017500000001135713650101756021246 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains a bunch of internal declarations that must appear before rpc.h can start. // We don't define these directly in rpc.h because it makes the file hard to read. #pragma once #include "capability.h" #include "persistent.capnp.h" CAPNP_BEGIN_HEADER namespace capnp { class OutgoingRpcMessage; class IncomingRpcMessage; class RpcFlowController; template class RpcSystem; namespace _ { // private class VatNetworkBase { // Non-template version of VatNetwork. Ignore this class; see VatNetwork in rpc.h. public: class Connection; struct ConnectionAndProvisionId { kj::Own connection; kj::Own firstMessage; Orphan provisionId; }; class Connection { public: virtual kj::Own newOutgoingMessage(uint firstSegmentWordSize) = 0; virtual kj::Promise>> receiveIncomingMessage() = 0; virtual kj::Promise shutdown() = 0; virtual AnyStruct::Reader baseGetPeerVatId() = 0; virtual kj::Own newStream() = 0; }; virtual kj::Maybe> baseConnect(AnyStruct::Reader vatId) = 0; virtual kj::Promise> baseAccept() = 0; }; class SturdyRefRestorerBase { public: virtual Capability::Client baseRestore(AnyPointer::Reader ref) = 0; }; class BootstrapFactoryBase { // Non-template version of BootstrapFactory. Ignore this class; see BootstrapFactory in rpc.h. public: virtual Capability::Client baseCreateFor(AnyStruct::Reader clientId) = 0; }; class RpcSystemBase { // Non-template version of RpcSystem. Ignore this class; see RpcSystem in rpc.h. public: RpcSystemBase(VatNetworkBase& network, kj::Maybe bootstrapInterface, kj::Maybe::Client> gateway); RpcSystemBase(VatNetworkBase& network, BootstrapFactoryBase& bootstrapFactory, kj::Maybe::Client> gateway); RpcSystemBase(VatNetworkBase& network, SturdyRefRestorerBase& restorer); RpcSystemBase(RpcSystemBase&& other) noexcept; ~RpcSystemBase() noexcept(false); private: class Impl; kj::Own impl; Capability::Client baseBootstrap(AnyStruct::Reader vatId); Capability::Client baseRestore(AnyStruct::Reader vatId, AnyPointer::Reader objectId); void baseSetFlowLimit(size_t words); template friend class capnp::RpcSystem; }; template struct InternalRefFromRealmGateway_; template struct InternalRefFromRealmGateway_> { typedef InternalRef Type; }; template using InternalRefFromRealmGateway = typename InternalRefFromRealmGateway_::Type; template using InternalRefFromRealmGatewayClient = InternalRefFromRealmGateway; template struct ExternalRefFromRealmGateway_; template struct ExternalRefFromRealmGateway_> { typedef ExternalRef Type; }; template using ExternalRefFromRealmGateway = typename ExternalRefFromRealmGateway_::Type; template using ExternalRefFromRealmGatewayClient = ExternalRefFromRealmGateway; } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/rpc.h0000644000175000017500000010212613650101756017603 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "capability.h" #include "rpc-prelude.h" CAPNP_BEGIN_HEADER namespace kj { class AutoCloseFd; } namespace capnp { template class VatNetwork; template class SturdyRefRestorer; template class BootstrapFactory: public _::BootstrapFactoryBase { // Interface that constructs per-client bootstrap interfaces. Use this if you want each client // who connects to see a different bootstrap interface based on their (authenticated) VatId. // This allows an application to bootstrap off of the authentication performed at the VatNetwork // level. (Typically VatId is some sort of public key.) // // This is only useful for multi-party networks. For TwoPartyVatNetwork, there's no reason to // use a BootstrapFactory; just specify a single bootstrap capability in this case. public: virtual Capability::Client createFor(typename VatId::Reader clientId) = 0; // Create a bootstrap capability appropriate for exposing to the given client. VatNetwork will // have authenticated the client VatId before this is called. private: Capability::Client baseCreateFor(AnyStruct::Reader clientId) override; }; template class RpcSystem: public _::RpcSystemBase { // Represents the RPC system, which is the portal to objects available on the network. // // The RPC implementation sits on top of an implementation of `VatNetwork`. The `VatNetwork` // determines how to form connections between vats -- specifically, two-way, private, reliable, // sequenced datagram connections. The RPC implementation determines how to use such connections // to manage object references and make method calls. // // See `makeRpcServer()` and `makeRpcClient()` below for convenient syntax for setting up an // `RpcSystem` given a `VatNetwork`. // // See `ez-rpc.h` for an even simpler interface for setting up RPC in a typical two-party // client/server scenario. public: template RpcSystem( VatNetwork& network, kj::Maybe bootstrapInterface, kj::Maybe::Client> gateway = nullptr); template RpcSystem( VatNetwork& network, BootstrapFactory& bootstrapFactory, kj::Maybe::Client> gateway = nullptr); template RpcSystem( VatNetwork& network, SturdyRefRestorer& restorer); RpcSystem(RpcSystem&& other) = default; Capability::Client bootstrap(typename VatId::Reader vatId); // Connect to the given vat and return its bootstrap interface. Capability::Client restore(typename VatId::Reader hostId, AnyPointer::Reader objectId) CAPNP_DEPRECATED("Please transition to using a bootstrap interface instead."); // ** DEPRECATED ** // // Restores the given SturdyRef from the network and return the capability representing it. // // `hostId` identifies the host from which to request the ref, in the format specified by the // `VatNetwork` in use. `objectId` is the object ID in whatever format is expected by said host. // // This method will be removed in a future version of Cap'n Proto. Instead, please transition // to using bootstrap(), which is equivalent to calling restore() with a null `objectId`. // You may emulate the old concept of object IDs by exporting a bootstrap interface which has // methods that can be used to obtain other capabilities by ID. void setFlowLimit(size_t words); // Sets the incoming call flow limit. If more than `words` worth of call messages have not yet // received responses, the RpcSystem will not read further messages from the stream. This can be // used as a crude way to prevent a resource exhaustion attack (or bug) in which a peer makes an // excessive number of simultaneous calls that consume the receiver's RAM. // // There are some caveats. When over the flow limit, all messages are blocked, including returns. // If the outstanding calls are themselves waiting on calls going in the opposite direction, the // flow limit may prevent those calls from completing, leading to deadlock. However, a // sufficiently high limit should make this unlikely. // // Note that a call's parameter size counts against the flow limit until the call returns, even // if the recipient calls releaseParams() to free the parameter memory early. This is because // releaseParams() may simply indicate that the parameters have been forwarded to another // machine, but are still in-memory there. For illustration, say that Alice made a call to Bob // who forwarded the call to Carol. Bob has imposed a flow limit on Alice. Alice's calls are // being forwarded to Carol, so Bob never keeps the parameters in-memory for more than a brief // period. However, the flow limit counts all calls that haven't returned, even if Bob has // already freed the memory they consumed. You might argue that the right solution here is // instead for Carol to impose her own flow limit on Bob. This has a serious problem, though: // Bob might be forwarding requests to Carol on behalf of many different parties, not just Alice. // If Alice can pump enough data to hit the Bob -> Carol flow limit, then those other parties // will be disrupted. Thus, we can only really impose the limit on the Alice -> Bob link, which // only affects Alice. We need that one flow limit to limit Alice's impact on the whole system, // so it has to count all in-flight calls. // // In Sandstorm, flow limits are imposed by the supervisor on calls coming out of a grain, in // order to prevent a grain from inundating the system with in-flight calls. In practice, the // main time this happens is when a grain is pushing a large file download and doesn't implement // proper cooperative flow control. }; template RpcSystem makeRpcServer( VatNetwork& network, Capability::Client bootstrapInterface); // Make an RPC server. Typical usage (e.g. in a main() function): // // MyEventLoop eventLoop; // kj::WaitScope waitScope(eventLoop); // MyNetwork network; // MyMainInterface::Client bootstrap = makeMain(); // auto server = makeRpcServer(network, bootstrap); // kj::NEVER_DONE.wait(waitScope); // run forever // // See also ez-rpc.h, which has simpler instructions for the common case of a two-party // client-server RPC connection. template , typename ExternalRef = _::ExternalRefFromRealmGatewayClient> RpcSystem makeRpcServer( VatNetwork& network, Capability::Client bootstrapInterface, RealmGatewayClient gateway) CAPNP_DEPRECATED("Please transition to using MembranePolicy instead of RealmGateway."); // ** DEPRECATED ** // // This uses a RealmGateway to create a membrane between the external network and internal // capabilites to translate save() requests. However, MembranePolicy (membrane.h) allows for the // creation of much more powerful membranes and doesn't need to be tied to an RpcSystem. // Applications should transition to using membranes instead of RealmGateway. RealmGateway will be // removed in a future version of Cap'n Proto. // // Original description: // // Make an RPC server for a VatNetwork that resides in a different realm from the application. // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format // and the network's ("external") format. template RpcSystem makeRpcServer( VatNetwork& network, BootstrapFactory& bootstrapFactory); // Make an RPC server that can serve different bootstrap interfaces to different clients via a // BootstrapInterface. template , typename ExternalRef = _::ExternalRefFromRealmGatewayClient> RpcSystem makeRpcServer( VatNetwork& network, BootstrapFactory& bootstrapFactory, RealmGatewayClient gateway) CAPNP_DEPRECATED("Please transition to using MembranePolicy instead of RealmGateway."); // ** DEPRECATED ** // // This uses a RealmGateway to create a membrane between the external network and internal // capabilites to translate save() requests. However, MembranePolicy (membrane.h) allows for the // creation of much more powerful membranes and doesn't need to be tied to an RpcSystem. // Applications should transition to using membranes instead of RealmGateway. RealmGateway will be // removed in a future version of Cap'n Proto. // // Original description: // // Make an RPC server that can serve different bootstrap interfaces to different clients via a // BootstrapInterface and communicates with a different realm than the application is in via a // RealmGateway. template RpcSystem makeRpcServer( VatNetwork& network, SturdyRefRestorer& restorer) CAPNP_DEPRECATED("Please transition to using a bootstrap interface instead."); // ** DEPRECATED ** // // Create an RPC server which exports multiple main interfaces by object ID. The `restorer` object // can be used to look up objects by ID. // // Please transition to exporting only one interface, which is known as the "bootstrap" interface. // For backwards-compatibility with old clients, continue to implement SturdyRefRestorer, but // return the new bootstrap interface when the request object ID is null. When new clients connect // and request the bootstrap interface, they will get that interface. Eventually, once all clients // are updated to request only the bootstrap interface, stop implementing SturdyRefRestorer and // switch to passing the bootstrap capability itself as the second parameter to `makeRpcServer()`. template RpcSystem makeRpcClient( VatNetwork& network); // Make an RPC client. Typical usage (e.g. in a main() function): // // MyEventLoop eventLoop; // kj::WaitScope waitScope(eventLoop); // MyNetwork network; // auto client = makeRpcClient(network); // MyCapability::Client cap = client.restore(hostId, objId).castAs(); // auto response = cap.fooRequest().send().wait(waitScope); // handleMyResponse(response); // // See also ez-rpc.h, which has simpler instructions for the common case of a two-party // client-server RPC connection. template , typename ExternalRef = _::ExternalRefFromRealmGatewayClient> RpcSystem makeRpcClient( VatNetwork& network, RealmGatewayClient gateway) CAPNP_DEPRECATED("Please transition to using MembranePolicy instead of RealmGateway."); // ** DEPRECATED ** // // This uses a RealmGateway to create a membrane between the external network and internal // capabilites to translate save() requests. However, MembranePolicy (membrane.h) allows for the // creation of much more powerful membranes and doesn't need to be tied to an RpcSystem. // Applications should transition to using membranes instead of RealmGateway. RealmGateway will be // removed in a future version of Cap'n Proto. // // Original description: // // Make an RPC client for a VatNetwork that resides in a different realm from the application. // The given RealmGateway is used to translate SturdyRefs between the app's ("internal") format // and the network's ("external") format. template class SturdyRefRestorer: public _::SturdyRefRestorerBase { // ** DEPRECATED ** // // In Cap'n Proto 0.4.x, applications could export multiple main interfaces identified by // object IDs. The callback used to map object IDs to objects was `SturdyRefRestorer`, as we // imagined this would eventually be used for restoring SturdyRefs as well. In practice, it was // never used for real SturdyRefs, only for exporting singleton objects under well-known names. // // The new preferred strategy is to export only a _single_ such interface, called the // "bootstrap interface". That interface can itself have methods for obtaining other objects, of // course, but that is up to the app. `SturdyRefRestorer` exists for backwards-compatibility. // // Hint: Use SturdyRefRestorer to define a server that exports services under // string names. public: virtual Capability::Client restore(typename SturdyRefObjectId::Reader ref) CAPNP_DEPRECATED( "Please transition to using bootstrap interfaces instead of SturdyRefRestorer.") = 0; // Restore the given object, returning a capability representing it. private: Capability::Client baseRestore(AnyPointer::Reader ref) override final; }; // ======================================================================================= // VatNetwork class OutgoingRpcMessage { // A message to be sent by a `VatNetwork`. public: virtual AnyPointer::Builder getBody() = 0; // Get the message body, which the caller may fill in any way it wants. (The standard RPC // implementation initializes it as a Message as defined in rpc.capnp.) virtual void setFds(kj::Array fds) {} // Set the list of file descriptors to send along with this message, if FD passing is supported. // An implementation may ignore this. virtual void send() = 0; // Send the message, or at least put it in a queue to be sent later. Note that the builder // returned by `getBody()` remains valid at least until the `OutgoingRpcMessage` is destroyed. virtual size_t sizeInWords() = 0; // Get the total size of the message, for flow control purposes. Although the caller could // also call getBody().targetSize(), doing that would walk the message tree, whereas typical // implementations can compute the size more cheaply by summing segment sizes. }; class IncomingRpcMessage { // A message received from a `VatNetwork`. public: virtual AnyPointer::Reader getBody() = 0; // Get the message body, to be interpreted by the caller. (The standard RPC implementation // interprets it as a Message as defined in rpc.capnp.) virtual kj::ArrayPtr getAttachedFds() { return nullptr; } // If the transport supports attached file descriptors and some were attached to this message, // returns them. Otherwise returns an empty array. It is intended that the caller will move the // FDs out of this table when they are consumed, possibly leaving behind a null slot. Callers // should be careful to check if an FD was already consumed by comparing the slot with `nullptr`. // (We don't use Maybe here because moving from a Maybe doesn't make it null, so it would only // add confusion. Moving from an AutoCloseFd does in fact make it null.) virtual size_t sizeInWords() = 0; // Get the total size of the message, for flow control purposes. Although the caller could // also call getBody().targetSize(), doing that would walk the message tree, whereas typical // implementations can compute the size more cheaply by summing segment sizes. }; class RpcFlowController { // Tracks a particular RPC stream in order to implement a flow control algorithm. public: virtual kj::Promise send(kj::Own message, kj::Promise ack) = 0; // Like calling message->send(), but the promise resolves when it's a good time to send the // next message. // // `ack` is a promise that resolves when the message has been acknowledged from the other side. // In practice, `message` is typically a `Call` message and `ack` is a `Return`. Note that this // means `ack` counts not only time to transmit the message but also time for the remote // application to process the message. The flow controller is expected to apply backpressure if // the remote application responds slowly. If `ack` rejects, then all outstanding and future // sends will propagate the exception. // // Note that messages sent with this method must still be delivered in the same order as if they // had been sent with `message->send()`; they cannot be delayed until later. This is important // because the message may introduce state changes in the RPC system that later messages rely on, // such as introducing a new Question ID that a later message may reference. Thus, the controller // can only create backpressure by having the returned promise resolve slowly. // // Dropping the returned promise does not cancel the send. Once send() is called, there's no way // to stop it. virtual kj::Promise waitAllAcked() = 0; // Wait for all `ack`s previously passed to send() to finish. It is an error to call send() again // after this. // --------------------------------------------------------------------------- // Common implementations. static kj::Own newFixedWindowController(size_t windowSize); // Constructs a flow controller that implements a strict fixed window of the given size. In other // words, the controller will throttle the stream when the total bytes in-flight exceeds the // window. class WindowGetter { public: virtual size_t getWindow() = 0; }; static kj::Own newVariableWindowController(WindowGetter& getter); // Like newFixedWindowController(), but the window size is allowed to vary over time. Useful if // you have a technique for estimating one good window size for the connection as a whole but not // for individual streams. Keep in mind, though, that in situations where the other end of the // connection is merely proxying capabilities from a variety of final destinations across a // variety of networks, no single window will be appropriate for all streams. static constexpr size_t DEFAULT_WINDOW_SIZE = 65536; // The window size used by the default implementation of Connection::newStream(). }; template class VatNetwork: public _::VatNetworkBase { // Cap'n Proto RPC operates between vats, where a "vat" is some sort of host of objects. // Typically one Cap'n Proto process (in the Unix sense) is one vat. The RPC system is what // allows calls between objects hosted in different vats. // // The RPC implementation sits on top of an implementation of `VatNetwork`. The `VatNetwork` // determines how to form connections between vats -- specifically, two-way, private, reliable, // sequenced datagram connections. The RPC implementation determines how to use such connections // to manage object references and make method calls. // // The most common implementation of VatNetwork is TwoPartyVatNetwork (rpc-twoparty.h). Most // simple client-server apps will want to use it. (You may even want to use the EZ RPC // interfaces in `ez-rpc.h` and avoid all of this.) // // TODO(someday): Provide a standard implementation for the public internet. public: class Connection; struct ConnectionAndProvisionId { // Result of connecting to a vat introduced by another vat. kj::Own connection; // Connection to the new vat. kj::Own firstMessage; // An already-allocated `OutgoingRpcMessage` associated with `connection`. The RPC system will // construct this as an `Accept` message and send it. Orphan provisionId; // A `ProvisionId` already allocated inside `firstMessage`, which the RPC system will use to // build the `Accept` message. }; class Connection: public _::VatNetworkBase::Connection { // A two-way RPC connection. // // This object may represent a connection that doesn't exist yet, but is expected to exist // in the future. In this case, sent messages will automatically be queued and sent once the // connection is ready, so that the caller doesn't need to know the difference. public: virtual kj::Own newStream() override { return RpcFlowController::newFixedWindowController(65536); } // Construct a flow controller for a new stream on this connection. The controller can be // passed into OutgoingRpcMessage::sendStreaming(). // // The default implementation returns a dummy stream controller that just applies a fixed // window of 64k to everything. This always works but may constrain throughput on networks // where the bandwidth-delay product is high, while conversely providing too much buffer when // the bandwidth-delay product is low. // // WARNING: The RPC system may keep the `RpcFlowController` object alive past the lifetime of // the `Connection` itself. However, it will not call `send()` any more after the // `Connection` is destroyed. // // TODO(perf): We should introduce a flow controller implementation that uses a clock to // measure RTT and bandwidth and dynamically update the window size, like BBR. // Level 0 features ---------------------------------------------- virtual typename VatId::Reader getPeerVatId() = 0; // Returns the connected vat's authenticated VatId. It is the VatNetwork's responsibility to // authenticate this, so that the caller can be assured that they are really talking to the // identified vat and not an imposter. virtual kj::Own newOutgoingMessage(uint firstSegmentWordSize) override = 0; // Allocate a new message to be sent on this connection. // // If `firstSegmentWordSize` is non-zero, it should be treated as a hint suggesting how large // to make the first segment. This is entirely a hint and the connection may adjust it up or // down. If it is zero, the connection should choose the size itself. // // WARNING: The RPC system may keep the `OutgoingRpcMessage` object alive past the lifetime of // the `Connection` itself. However, it will not call `send()` any more after the // `Connection` is destroyed. virtual kj::Promise>> receiveIncomingMessage() override = 0; // Wait for a message to be received and return it. If the read stream cleanly terminates, // return null. If any other problem occurs, throw an exception. // // WARNING: The RPC system may keep the `IncomingRpcMessage` object alive past the lifetime of // the `Connection` itself. virtual kj::Promise shutdown() override KJ_WARN_UNUSED_RESULT = 0; // Waits until all outgoing messages have been sent, then shuts down the outgoing stream. The // returned promise resolves after shutdown is complete. private: AnyStruct::Reader baseGetPeerVatId() override; }; // Level 0 features ------------------------------------------------ virtual kj::Maybe> connect(typename VatId::Reader hostId) = 0; // Connect to a VatId. Note that this method immediately returns a `Connection`, even // if the network connection has not yet been established. Messages can be queued to this // connection and will be delivered once it is open. The caller must attempt to read from the // connection to verify that it actually succeeded; the read will fail if the connection // couldn't be opened. Some network implementations may actually start sending messages before // hearing back from the server at all, to avoid a round trip. // // Returns nullptr if `hostId` refers to the local host. virtual kj::Promise> accept() = 0; // Wait for the next incoming connection and return it. // Level 4 features ------------------------------------------------ // TODO(someday) private: kj::Maybe> baseConnect(AnyStruct::Reader hostId) override final; kj::Promise> baseAccept() override final; }; // ======================================================================================= // *************************************************************************************** // Inline implementation details start here // *************************************************************************************** // ======================================================================================= template Capability::Client BootstrapFactory::baseCreateFor(AnyStruct::Reader clientId) { return createFor(clientId.as()); } template kj::Maybe> VatNetwork:: baseConnect(AnyStruct::Reader ref) { auto maybe = connect(ref.as()); return maybe.map([](kj::Own& conn) -> kj::Own<_::VatNetworkBase::Connection> { return kj::mv(conn); }); } template kj::Promise> VatNetwork::baseAccept() { return accept().then( [](kj::Own&& connection) -> kj::Own<_::VatNetworkBase::Connection> { return kj::mv(connection); }); } template AnyStruct::Reader VatNetwork< SturdyRef, ProvisionId, RecipientId, ThirdPartyCapId, JoinResult>:: Connection::baseGetPeerVatId() { return getPeerVatId(); } template Capability::Client SturdyRefRestorer::baseRestore(AnyPointer::Reader ref) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" return restore(ref.getAs()); #pragma GCC diagnostic pop } template template RpcSystem::RpcSystem( VatNetwork& network, kj::Maybe bootstrap, kj::Maybe::Client> gateway) : _::RpcSystemBase(network, kj::mv(bootstrap), kj::mv(gateway)) {} template template RpcSystem::RpcSystem( VatNetwork& network, BootstrapFactory& bootstrapFactory, kj::Maybe::Client> gateway) : _::RpcSystemBase(network, bootstrapFactory, kj::mv(gateway)) {} template template RpcSystem::RpcSystem( VatNetwork& network, SturdyRefRestorer& restorer) : _::RpcSystemBase(network, restorer) {} template Capability::Client RpcSystem::bootstrap(typename VatId::Reader vatId) { return baseBootstrap(_::PointerHelpers::getInternalReader(vatId)); } template Capability::Client RpcSystem::restore( typename VatId::Reader hostId, AnyPointer::Reader objectId) { return baseRestore(_::PointerHelpers::getInternalReader(hostId), objectId); } template inline void RpcSystem::setFlowLimit(size_t words) { baseSetFlowLimit(words); } template RpcSystem makeRpcServer( VatNetwork& network, Capability::Client bootstrapInterface) { return RpcSystem(network, kj::mv(bootstrapInterface)); } template RpcSystem makeRpcServer( VatNetwork& network, Capability::Client bootstrapInterface, RealmGatewayClient gateway) { return RpcSystem(network, kj::mv(bootstrapInterface), gateway.template castAs>()); } template RpcSystem makeRpcServer( VatNetwork& network, BootstrapFactory& bootstrapFactory) { return RpcSystem(network, bootstrapFactory); } template RpcSystem makeRpcServer( VatNetwork& network, BootstrapFactory& bootstrapFactory, RealmGatewayClient gateway) { return RpcSystem(network, bootstrapFactory, gateway.template castAs>()); } template RpcSystem makeRpcServer( VatNetwork& network, SturdyRefRestorer& restorer) { return RpcSystem(network, restorer); } template RpcSystem makeRpcClient( VatNetwork& network) { return RpcSystem(network, nullptr); } template RpcSystem makeRpcClient( VatNetwork& network, RealmGatewayClient gateway) { return RpcSystem(network, nullptr, gateway.template castAs>()); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/rpc-twoparty.h0000644000175000017500000002015413650101756021472 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "rpc.h" #include "message.h" #include #include #include CAPNP_BEGIN_HEADER namespace capnp { namespace rpc { namespace twoparty { typedef VatId SturdyRefHostId; // For backwards-compatibility with version 0.4. } } typedef VatNetwork TwoPartyVatNetworkBase; class TwoPartyVatNetwork: public TwoPartyVatNetworkBase, private TwoPartyVatNetworkBase::Connection, private RpcFlowController::WindowGetter { // A `VatNetwork` that consists of exactly two parties communicating over an arbitrary byte // stream. This is used to implement the common case of a client/server network. // // See `ez-rpc.h` for a simple interface for setting up two-party clients and servers. // Use `TwoPartyVatNetwork` only if you need the advanced features. public: TwoPartyVatNetwork(kj::AsyncIoStream& stream, rpc::twoparty::Side side, ReaderOptions receiveOptions = ReaderOptions()); TwoPartyVatNetwork(kj::AsyncCapabilityStream& stream, uint maxFdsPerMessage, rpc::twoparty::Side side, ReaderOptions receiveOptions = ReaderOptions()); // To support FD passing, pass an AsyncCapabilityStream and `maxFdsPerMessage`, which specifies // the maximum number of file descriptors to accept from the peer in any one RPC message. It is // important to keep maxFdsPerMessage low in order to stop DoS attacks that fill up your FD table. // // Note that this limit applies only to incoming messages; outgoing messages are allowed to have // more FDs. Sometimes it makes sense to enforce a limit of zero in one direction while having // a non-zero limit in the other. For example, in a supervisor/sandbox scenario, typically there // are many use cases for passing FDs from supervisor to sandbox but no use case for vice versa. // The supervisor may be configured not to accept any FDs from the sandbox in order to reduce // risk of DoS attacks. KJ_DISALLOW_COPY(TwoPartyVatNetwork); kj::Promise onDisconnect() { return disconnectPromise.addBranch(); } // Returns a promise that resolves when the peer disconnects. rpc::twoparty::Side getSide() { return side; } // implements VatNetwork ----------------------------------------------------- kj::Maybe> connect( rpc::twoparty::VatId::Reader ref) override; kj::Promise> accept() override; private: class OutgoingMessageImpl; class IncomingMessageImpl; kj::OneOf stream; uint maxFdsPerMessage; rpc::twoparty::Side side; MallocMessageBuilder peerVatId; ReaderOptions receiveOptions; bool accepted = false; bool solSndbufUnimplemented = false; // Whether stream.getsockopt(SO_SNDBUF) has been observed to throw UNIMPLEMENTED. kj::Maybe> previousWrite; // Resolves when the previous write completes. This effectively serves as the write queue. // Becomes null when shutdown() is called. kj::Own>> acceptFulfiller; // Fulfiller for the promise returned by acceptConnectionAsRefHost() on the client side, or the // second call on the server side. Never fulfilled, because there is only one connection. kj::ForkedPromise disconnectPromise = nullptr; class FulfillerDisposer: public kj::Disposer { // Hack: TwoPartyVatNetwork is both a VatNetwork and a VatNetwork::Connection. When the RPC // system detects (or initiates) a disconnection, it drops its reference to the Connection. // When all references have been dropped, then we want disconnectPromise to be fulfilled. // So we hand out Owns with this disposer attached, so that we can detect when // they are dropped. public: mutable kj::Own> fulfiller; mutable uint refcount = 0; void disposeImpl(void* pointer) const override; }; FulfillerDisposer disconnectFulfiller; kj::Own asConnection(); // Returns a pointer to this with the disposer set to disconnectFulfiller. // implements Connection ----------------------------------------------------- kj::Own newStream() override; rpc::twoparty::VatId::Reader getPeerVatId() override; kj::Own newOutgoingMessage(uint firstSegmentWordSize) override; kj::Promise>> receiveIncomingMessage() override; kj::Promise shutdown() override; // implements WindowGetter --------------------------------------------------- size_t getWindow() override; }; class TwoPartyServer: private kj::TaskSet::ErrorHandler { // Convenience class which implements a simple server which accepts connections on a listener // socket and serices them as two-party connections. public: explicit TwoPartyServer(Capability::Client bootstrapInterface); void accept(kj::Own&& connection); void accept(kj::Own&& connection, uint maxFdsPerMessage); // Accepts the connection for servicing. kj::Promise listen(kj::ConnectionReceiver& listener); // Listens for connections on the given listener. The returned promise never resolves unless an // exception is thrown while trying to accept. You may discard the returned promise to cancel // listening. kj::Promise listenCapStreamReceiver( kj::ConnectionReceiver& listener, uint maxFdsPerMessage); // Listen with support for FD transfers. `listener.accept()` must return instances of // AsyncCapabilityStream, otherwise this will crash. kj::Promise drain() { return tasks.onEmpty(); } // Resolves when all clients have disconnected. private: Capability::Client bootstrapInterface; kj::TaskSet tasks; struct AcceptedConnection; void taskFailed(kj::Exception&& exception) override; }; class TwoPartyClient { // Convenience class which implements a simple client. public: explicit TwoPartyClient(kj::AsyncIoStream& connection); explicit TwoPartyClient(kj::AsyncCapabilityStream& connection, uint maxFdsPerMessage); TwoPartyClient(kj::AsyncIoStream& connection, Capability::Client bootstrapInterface, rpc::twoparty::Side side = rpc::twoparty::Side::CLIENT); TwoPartyClient(kj::AsyncCapabilityStream& connection, uint maxFdsPerMessage, Capability::Client bootstrapInterface, rpc::twoparty::Side side = rpc::twoparty::Side::CLIENT); Capability::Client bootstrap(); // Get the server's bootstrap interface. inline kj::Promise onDisconnect() { return network.onDisconnect(); } private: TwoPartyVatNetwork network; RpcSystem rpcSystem; }; } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/rpc.capnp.h0000644000175000017500000055427713650317744020733 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: rpc.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(91b79f1f808db032); CAPNP_DECLARE_SCHEMA(e94ccf8031176ec4); CAPNP_DECLARE_SCHEMA(836a53ce789d4cd4); CAPNP_DECLARE_SCHEMA(dae8b0f61aab5f99); CAPNP_DECLARE_SCHEMA(9e19b28d3db3573a); CAPNP_DECLARE_SCHEMA(d37d2eb2c2f80e63); CAPNP_DECLARE_SCHEMA(bbc29655fa89086e); CAPNP_DECLARE_SCHEMA(ad1a6c0d7dd07497); CAPNP_DECLARE_SCHEMA(f964368b0fbd3711); CAPNP_DECLARE_SCHEMA(d562b4df655bdd4d); CAPNP_DECLARE_SCHEMA(9c6a046bfbc1ac5a); CAPNP_DECLARE_SCHEMA(d4c9b56290554016); CAPNP_DECLARE_SCHEMA(fbe1980490e001af); CAPNP_DECLARE_SCHEMA(95bc14545813fbc1); CAPNP_DECLARE_SCHEMA(9a0e61223d96743b); CAPNP_DECLARE_SCHEMA(8523ddc40b86b8b0); CAPNP_DECLARE_SCHEMA(d800b1d6cd6f1ca0); CAPNP_DECLARE_SCHEMA(f316944415569081); CAPNP_DECLARE_SCHEMA(d37007fde1f0027d); CAPNP_DECLARE_SCHEMA(d625b7063acf691a); CAPNP_DECLARE_SCHEMA(b28c96e23f4cbd58); enum class Type_b28c96e23f4cbd58: uint16_t { FAILED, OVERLOADED, DISCONNECTED, UNIMPLEMENTED, }; CAPNP_DECLARE_ENUM(Type, b28c96e23f4cbd58); } // namespace schemas } // namespace capnp namespace capnp { namespace rpc { struct Message { Message() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { UNIMPLEMENTED, ABORT, CALL, RETURN, FINISH, RESOLVE, RELEASE, OBSOLETE_SAVE, BOOTSTRAP, OBSOLETE_DELETE, PROVIDE, ACCEPT, JOIN, DISEMBARGO, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(91b79f1f808db032, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Bootstrap { Bootstrap() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(e94ccf8031176ec4, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Call { Call() = delete; class Reader; class Builder; class Pipeline; struct SendResultsTo; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(836a53ce789d4cd4, 3, 3) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Call::SendResultsTo { SendResultsTo() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { CALLER, YOURSELF, THIRD_PARTY, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(dae8b0f61aab5f99, 3, 3) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Return { Return() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { RESULTS, EXCEPTION, CANCELED, RESULTS_SENT_ELSEWHERE, TAKE_FROM_OTHER_QUESTION, ACCEPT_FROM_THIRD_PARTY, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9e19b28d3db3573a, 2, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Finish { Finish() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d37d2eb2c2f80e63, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Resolve { Resolve() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { CAP, EXCEPTION, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(bbc29655fa89086e, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Release { Release() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ad1a6c0d7dd07497, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Disembargo { Disembargo() = delete; class Reader; class Builder; class Pipeline; struct Context; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(f964368b0fbd3711, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Disembargo::Context { Context() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { SENDER_LOOPBACK, RECEIVER_LOOPBACK, ACCEPT, PROVIDE, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d562b4df655bdd4d, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Provide { Provide() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9c6a046bfbc1ac5a, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Accept { Accept() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d4c9b56290554016, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Join { Join() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(fbe1980490e001af, 1, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct MessageTarget { MessageTarget() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { IMPORTED_CAP, PROMISED_ANSWER, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(95bc14545813fbc1, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Payload { Payload() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9a0e61223d96743b, 0, 2) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct CapDescriptor { CapDescriptor() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NONE, SENDER_HOSTED, SENDER_PROMISE, RECEIVER_HOSTED, RECEIVER_ANSWER, THIRD_PARTY_HOSTED, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(8523ddc40b86b8b0, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct PromisedAnswer { PromisedAnswer() = delete; class Reader; class Builder; class Pipeline; struct Op; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d800b1d6cd6f1ca0, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct PromisedAnswer::Op { Op() = delete; class Reader; class Builder; class Pipeline; enum Which: uint16_t { NOOP, GET_POINTER_FIELD, }; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(f316944415569081, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct ThirdPartyCapDescriptor { ThirdPartyCapDescriptor() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d37007fde1f0027d, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct Exception { Exception() = delete; class Reader; class Builder; class Pipeline; typedef ::capnp::schemas::Type_b28c96e23f4cbd58 Type; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d625b7063acf691a, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class Message::Reader { public: typedef Message Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isUnimplemented() const; inline bool hasUnimplemented() const; inline ::capnp::rpc::Message::Reader getUnimplemented() const; inline bool isAbort() const; inline bool hasAbort() const; inline ::capnp::rpc::Exception::Reader getAbort() const; inline bool isCall() const; inline bool hasCall() const; inline ::capnp::rpc::Call::Reader getCall() const; inline bool isReturn() const; inline bool hasReturn() const; inline ::capnp::rpc::Return::Reader getReturn() const; inline bool isFinish() const; inline bool hasFinish() const; inline ::capnp::rpc::Finish::Reader getFinish() const; inline bool isResolve() const; inline bool hasResolve() const; inline ::capnp::rpc::Resolve::Reader getResolve() const; inline bool isRelease() const; inline bool hasRelease() const; inline ::capnp::rpc::Release::Reader getRelease() const; inline bool isObsoleteSave() const; inline bool hasObsoleteSave() const; inline ::capnp::AnyPointer::Reader getObsoleteSave() const; inline bool isBootstrap() const; inline bool hasBootstrap() const; inline ::capnp::rpc::Bootstrap::Reader getBootstrap() const; inline bool isObsoleteDelete() const; inline bool hasObsoleteDelete() const; inline ::capnp::AnyPointer::Reader getObsoleteDelete() const; inline bool isProvide() const; inline bool hasProvide() const; inline ::capnp::rpc::Provide::Reader getProvide() const; inline bool isAccept() const; inline bool hasAccept() const; inline ::capnp::rpc::Accept::Reader getAccept() const; inline bool isJoin() const; inline bool hasJoin() const; inline ::capnp::rpc::Join::Reader getJoin() const; inline bool isDisembargo() const; inline bool hasDisembargo() const; inline ::capnp::rpc::Disembargo::Reader getDisembargo() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Message::Builder { public: typedef Message Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isUnimplemented(); inline bool hasUnimplemented(); inline ::capnp::rpc::Message::Builder getUnimplemented(); inline void setUnimplemented( ::capnp::rpc::Message::Reader value); inline ::capnp::rpc::Message::Builder initUnimplemented(); inline void adoptUnimplemented(::capnp::Orphan< ::capnp::rpc::Message>&& value); inline ::capnp::Orphan< ::capnp::rpc::Message> disownUnimplemented(); inline bool isAbort(); inline bool hasAbort(); inline ::capnp::rpc::Exception::Builder getAbort(); inline void setAbort( ::capnp::rpc::Exception::Reader value); inline ::capnp::rpc::Exception::Builder initAbort(); inline void adoptAbort(::capnp::Orphan< ::capnp::rpc::Exception>&& value); inline ::capnp::Orphan< ::capnp::rpc::Exception> disownAbort(); inline bool isCall(); inline bool hasCall(); inline ::capnp::rpc::Call::Builder getCall(); inline void setCall( ::capnp::rpc::Call::Reader value); inline ::capnp::rpc::Call::Builder initCall(); inline void adoptCall(::capnp::Orphan< ::capnp::rpc::Call>&& value); inline ::capnp::Orphan< ::capnp::rpc::Call> disownCall(); inline bool isReturn(); inline bool hasReturn(); inline ::capnp::rpc::Return::Builder getReturn(); inline void setReturn( ::capnp::rpc::Return::Reader value); inline ::capnp::rpc::Return::Builder initReturn(); inline void adoptReturn(::capnp::Orphan< ::capnp::rpc::Return>&& value); inline ::capnp::Orphan< ::capnp::rpc::Return> disownReturn(); inline bool isFinish(); inline bool hasFinish(); inline ::capnp::rpc::Finish::Builder getFinish(); inline void setFinish( ::capnp::rpc::Finish::Reader value); inline ::capnp::rpc::Finish::Builder initFinish(); inline void adoptFinish(::capnp::Orphan< ::capnp::rpc::Finish>&& value); inline ::capnp::Orphan< ::capnp::rpc::Finish> disownFinish(); inline bool isResolve(); inline bool hasResolve(); inline ::capnp::rpc::Resolve::Builder getResolve(); inline void setResolve( ::capnp::rpc::Resolve::Reader value); inline ::capnp::rpc::Resolve::Builder initResolve(); inline void adoptResolve(::capnp::Orphan< ::capnp::rpc::Resolve>&& value); inline ::capnp::Orphan< ::capnp::rpc::Resolve> disownResolve(); inline bool isRelease(); inline bool hasRelease(); inline ::capnp::rpc::Release::Builder getRelease(); inline void setRelease( ::capnp::rpc::Release::Reader value); inline ::capnp::rpc::Release::Builder initRelease(); inline void adoptRelease(::capnp::Orphan< ::capnp::rpc::Release>&& value); inline ::capnp::Orphan< ::capnp::rpc::Release> disownRelease(); inline bool isObsoleteSave(); inline bool hasObsoleteSave(); inline ::capnp::AnyPointer::Builder getObsoleteSave(); inline ::capnp::AnyPointer::Builder initObsoleteSave(); inline bool isBootstrap(); inline bool hasBootstrap(); inline ::capnp::rpc::Bootstrap::Builder getBootstrap(); inline void setBootstrap( ::capnp::rpc::Bootstrap::Reader value); inline ::capnp::rpc::Bootstrap::Builder initBootstrap(); inline void adoptBootstrap(::capnp::Orphan< ::capnp::rpc::Bootstrap>&& value); inline ::capnp::Orphan< ::capnp::rpc::Bootstrap> disownBootstrap(); inline bool isObsoleteDelete(); inline bool hasObsoleteDelete(); inline ::capnp::AnyPointer::Builder getObsoleteDelete(); inline ::capnp::AnyPointer::Builder initObsoleteDelete(); inline bool isProvide(); inline bool hasProvide(); inline ::capnp::rpc::Provide::Builder getProvide(); inline void setProvide( ::capnp::rpc::Provide::Reader value); inline ::capnp::rpc::Provide::Builder initProvide(); inline void adoptProvide(::capnp::Orphan< ::capnp::rpc::Provide>&& value); inline ::capnp::Orphan< ::capnp::rpc::Provide> disownProvide(); inline bool isAccept(); inline bool hasAccept(); inline ::capnp::rpc::Accept::Builder getAccept(); inline void setAccept( ::capnp::rpc::Accept::Reader value); inline ::capnp::rpc::Accept::Builder initAccept(); inline void adoptAccept(::capnp::Orphan< ::capnp::rpc::Accept>&& value); inline ::capnp::Orphan< ::capnp::rpc::Accept> disownAccept(); inline bool isJoin(); inline bool hasJoin(); inline ::capnp::rpc::Join::Builder getJoin(); inline void setJoin( ::capnp::rpc::Join::Reader value); inline ::capnp::rpc::Join::Builder initJoin(); inline void adoptJoin(::capnp::Orphan< ::capnp::rpc::Join>&& value); inline ::capnp::Orphan< ::capnp::rpc::Join> disownJoin(); inline bool isDisembargo(); inline bool hasDisembargo(); inline ::capnp::rpc::Disembargo::Builder getDisembargo(); inline void setDisembargo( ::capnp::rpc::Disembargo::Reader value); inline ::capnp::rpc::Disembargo::Builder initDisembargo(); inline void adoptDisembargo(::capnp::Orphan< ::capnp::rpc::Disembargo>&& value); inline ::capnp::Orphan< ::capnp::rpc::Disembargo> disownDisembargo(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Message::Pipeline { public: typedef Message Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Bootstrap::Reader { public: typedef Bootstrap Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool hasDeprecatedObjectId() const; inline ::capnp::AnyPointer::Reader getDeprecatedObjectId() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Bootstrap::Builder { public: typedef Bootstrap Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool hasDeprecatedObjectId(); inline ::capnp::AnyPointer::Builder getDeprecatedObjectId(); inline ::capnp::AnyPointer::Builder initDeprecatedObjectId(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Bootstrap::Pipeline { public: typedef Bootstrap Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Call::Reader { public: typedef Call Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool hasTarget() const; inline ::capnp::rpc::MessageTarget::Reader getTarget() const; inline ::uint64_t getInterfaceId() const; inline ::uint16_t getMethodId() const; inline bool hasParams() const; inline ::capnp::rpc::Payload::Reader getParams() const; inline typename SendResultsTo::Reader getSendResultsTo() const; inline bool getAllowThirdPartyTailCall() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Call::Builder { public: typedef Call Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool hasTarget(); inline ::capnp::rpc::MessageTarget::Builder getTarget(); inline void setTarget( ::capnp::rpc::MessageTarget::Reader value); inline ::capnp::rpc::MessageTarget::Builder initTarget(); inline void adoptTarget(::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value); inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> disownTarget(); inline ::uint64_t getInterfaceId(); inline void setInterfaceId( ::uint64_t value); inline ::uint16_t getMethodId(); inline void setMethodId( ::uint16_t value); inline bool hasParams(); inline ::capnp::rpc::Payload::Builder getParams(); inline void setParams( ::capnp::rpc::Payload::Reader value); inline ::capnp::rpc::Payload::Builder initParams(); inline void adoptParams(::capnp::Orphan< ::capnp::rpc::Payload>&& value); inline ::capnp::Orphan< ::capnp::rpc::Payload> disownParams(); inline typename SendResultsTo::Builder getSendResultsTo(); inline typename SendResultsTo::Builder initSendResultsTo(); inline bool getAllowThirdPartyTailCall(); inline void setAllowThirdPartyTailCall(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Call::Pipeline { public: typedef Call Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::rpc::MessageTarget::Pipeline getTarget(); inline ::capnp::rpc::Payload::Pipeline getParams(); inline typename SendResultsTo::Pipeline getSendResultsTo(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Call::SendResultsTo::Reader { public: typedef SendResultsTo Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isCaller() const; inline ::capnp::Void getCaller() const; inline bool isYourself() const; inline ::capnp::Void getYourself() const; inline bool isThirdParty() const; inline bool hasThirdParty() const; inline ::capnp::AnyPointer::Reader getThirdParty() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Call::SendResultsTo::Builder { public: typedef SendResultsTo Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isCaller(); inline ::capnp::Void getCaller(); inline void setCaller( ::capnp::Void value = ::capnp::VOID); inline bool isYourself(); inline ::capnp::Void getYourself(); inline void setYourself( ::capnp::Void value = ::capnp::VOID); inline bool isThirdParty(); inline bool hasThirdParty(); inline ::capnp::AnyPointer::Builder getThirdParty(); inline ::capnp::AnyPointer::Builder initThirdParty(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Call::SendResultsTo::Pipeline { public: typedef SendResultsTo Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Return::Reader { public: typedef Return Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline ::uint32_t getAnswerId() const; inline bool getReleaseParamCaps() const; inline bool isResults() const; inline bool hasResults() const; inline ::capnp::rpc::Payload::Reader getResults() const; inline bool isException() const; inline bool hasException() const; inline ::capnp::rpc::Exception::Reader getException() const; inline bool isCanceled() const; inline ::capnp::Void getCanceled() const; inline bool isResultsSentElsewhere() const; inline ::capnp::Void getResultsSentElsewhere() const; inline bool isTakeFromOtherQuestion() const; inline ::uint32_t getTakeFromOtherQuestion() const; inline bool isAcceptFromThirdParty() const; inline bool hasAcceptFromThirdParty() const; inline ::capnp::AnyPointer::Reader getAcceptFromThirdParty() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Return::Builder { public: typedef Return Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline ::uint32_t getAnswerId(); inline void setAnswerId( ::uint32_t value); inline bool getReleaseParamCaps(); inline void setReleaseParamCaps(bool value); inline bool isResults(); inline bool hasResults(); inline ::capnp::rpc::Payload::Builder getResults(); inline void setResults( ::capnp::rpc::Payload::Reader value); inline ::capnp::rpc::Payload::Builder initResults(); inline void adoptResults(::capnp::Orphan< ::capnp::rpc::Payload>&& value); inline ::capnp::Orphan< ::capnp::rpc::Payload> disownResults(); inline bool isException(); inline bool hasException(); inline ::capnp::rpc::Exception::Builder getException(); inline void setException( ::capnp::rpc::Exception::Reader value); inline ::capnp::rpc::Exception::Builder initException(); inline void adoptException(::capnp::Orphan< ::capnp::rpc::Exception>&& value); inline ::capnp::Orphan< ::capnp::rpc::Exception> disownException(); inline bool isCanceled(); inline ::capnp::Void getCanceled(); inline void setCanceled( ::capnp::Void value = ::capnp::VOID); inline bool isResultsSentElsewhere(); inline ::capnp::Void getResultsSentElsewhere(); inline void setResultsSentElsewhere( ::capnp::Void value = ::capnp::VOID); inline bool isTakeFromOtherQuestion(); inline ::uint32_t getTakeFromOtherQuestion(); inline void setTakeFromOtherQuestion( ::uint32_t value); inline bool isAcceptFromThirdParty(); inline bool hasAcceptFromThirdParty(); inline ::capnp::AnyPointer::Builder getAcceptFromThirdParty(); inline ::capnp::AnyPointer::Builder initAcceptFromThirdParty(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Return::Pipeline { public: typedef Return Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Finish::Reader { public: typedef Finish Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool getReleaseResultCaps() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Finish::Builder { public: typedef Finish Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool getReleaseResultCaps(); inline void setReleaseResultCaps(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Finish::Pipeline { public: typedef Finish Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Resolve::Reader { public: typedef Resolve Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline ::uint32_t getPromiseId() const; inline bool isCap() const; inline bool hasCap() const; inline ::capnp::rpc::CapDescriptor::Reader getCap() const; inline bool isException() const; inline bool hasException() const; inline ::capnp::rpc::Exception::Reader getException() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Resolve::Builder { public: typedef Resolve Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline ::uint32_t getPromiseId(); inline void setPromiseId( ::uint32_t value); inline bool isCap(); inline bool hasCap(); inline ::capnp::rpc::CapDescriptor::Builder getCap(); inline void setCap( ::capnp::rpc::CapDescriptor::Reader value); inline ::capnp::rpc::CapDescriptor::Builder initCap(); inline void adoptCap(::capnp::Orphan< ::capnp::rpc::CapDescriptor>&& value); inline ::capnp::Orphan< ::capnp::rpc::CapDescriptor> disownCap(); inline bool isException(); inline bool hasException(); inline ::capnp::rpc::Exception::Builder getException(); inline void setException( ::capnp::rpc::Exception::Reader value); inline ::capnp::rpc::Exception::Builder initException(); inline void adoptException(::capnp::Orphan< ::capnp::rpc::Exception>&& value); inline ::capnp::Orphan< ::capnp::rpc::Exception> disownException(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Resolve::Pipeline { public: typedef Resolve Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Release::Reader { public: typedef Release Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getId() const; inline ::uint32_t getReferenceCount() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Release::Builder { public: typedef Release Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getId(); inline void setId( ::uint32_t value); inline ::uint32_t getReferenceCount(); inline void setReferenceCount( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Release::Pipeline { public: typedef Release Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Disembargo::Reader { public: typedef Disembargo Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasTarget() const; inline ::capnp::rpc::MessageTarget::Reader getTarget() const; inline typename Context::Reader getContext() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Disembargo::Builder { public: typedef Disembargo Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasTarget(); inline ::capnp::rpc::MessageTarget::Builder getTarget(); inline void setTarget( ::capnp::rpc::MessageTarget::Reader value); inline ::capnp::rpc::MessageTarget::Builder initTarget(); inline void adoptTarget(::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value); inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> disownTarget(); inline typename Context::Builder getContext(); inline typename Context::Builder initContext(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Disembargo::Pipeline { public: typedef Disembargo Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::rpc::MessageTarget::Pipeline getTarget(); inline typename Context::Pipeline getContext(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Disembargo::Context::Reader { public: typedef Context Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isSenderLoopback() const; inline ::uint32_t getSenderLoopback() const; inline bool isReceiverLoopback() const; inline ::uint32_t getReceiverLoopback() const; inline bool isAccept() const; inline ::capnp::Void getAccept() const; inline bool isProvide() const; inline ::uint32_t getProvide() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Disembargo::Context::Builder { public: typedef Context Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isSenderLoopback(); inline ::uint32_t getSenderLoopback(); inline void setSenderLoopback( ::uint32_t value); inline bool isReceiverLoopback(); inline ::uint32_t getReceiverLoopback(); inline void setReceiverLoopback( ::uint32_t value); inline bool isAccept(); inline ::capnp::Void getAccept(); inline void setAccept( ::capnp::Void value = ::capnp::VOID); inline bool isProvide(); inline ::uint32_t getProvide(); inline void setProvide( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Disembargo::Context::Pipeline { public: typedef Context Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Provide::Reader { public: typedef Provide Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool hasTarget() const; inline ::capnp::rpc::MessageTarget::Reader getTarget() const; inline bool hasRecipient() const; inline ::capnp::AnyPointer::Reader getRecipient() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Provide::Builder { public: typedef Provide Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool hasTarget(); inline ::capnp::rpc::MessageTarget::Builder getTarget(); inline void setTarget( ::capnp::rpc::MessageTarget::Reader value); inline ::capnp::rpc::MessageTarget::Builder initTarget(); inline void adoptTarget(::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value); inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> disownTarget(); inline bool hasRecipient(); inline ::capnp::AnyPointer::Builder getRecipient(); inline ::capnp::AnyPointer::Builder initRecipient(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Provide::Pipeline { public: typedef Provide Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::rpc::MessageTarget::Pipeline getTarget(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Accept::Reader { public: typedef Accept Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool hasProvision() const; inline ::capnp::AnyPointer::Reader getProvision() const; inline bool getEmbargo() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Accept::Builder { public: typedef Accept Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool hasProvision(); inline ::capnp::AnyPointer::Builder getProvision(); inline ::capnp::AnyPointer::Builder initProvision(); inline bool getEmbargo(); inline void setEmbargo(bool value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Accept::Pipeline { public: typedef Accept Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Join::Reader { public: typedef Join Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool hasTarget() const; inline ::capnp::rpc::MessageTarget::Reader getTarget() const; inline bool hasKeyPart() const; inline ::capnp::AnyPointer::Reader getKeyPart() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Join::Builder { public: typedef Join Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool hasTarget(); inline ::capnp::rpc::MessageTarget::Builder getTarget(); inline void setTarget( ::capnp::rpc::MessageTarget::Reader value); inline ::capnp::rpc::MessageTarget::Builder initTarget(); inline void adoptTarget(::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value); inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> disownTarget(); inline bool hasKeyPart(); inline ::capnp::AnyPointer::Builder getKeyPart(); inline ::capnp::AnyPointer::Builder initKeyPart(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Join::Pipeline { public: typedef Join Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::rpc::MessageTarget::Pipeline getTarget(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class MessageTarget::Reader { public: typedef MessageTarget Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isImportedCap() const; inline ::uint32_t getImportedCap() const; inline bool isPromisedAnswer() const; inline bool hasPromisedAnswer() const; inline ::capnp::rpc::PromisedAnswer::Reader getPromisedAnswer() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class MessageTarget::Builder { public: typedef MessageTarget Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isImportedCap(); inline ::uint32_t getImportedCap(); inline void setImportedCap( ::uint32_t value); inline bool isPromisedAnswer(); inline bool hasPromisedAnswer(); inline ::capnp::rpc::PromisedAnswer::Builder getPromisedAnswer(); inline void setPromisedAnswer( ::capnp::rpc::PromisedAnswer::Reader value); inline ::capnp::rpc::PromisedAnswer::Builder initPromisedAnswer(); inline void adoptPromisedAnswer(::capnp::Orphan< ::capnp::rpc::PromisedAnswer>&& value); inline ::capnp::Orphan< ::capnp::rpc::PromisedAnswer> disownPromisedAnswer(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class MessageTarget::Pipeline { public: typedef MessageTarget Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Payload::Reader { public: typedef Payload Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasContent() const; inline ::capnp::AnyPointer::Reader getContent() const; inline bool hasCapTable() const; inline ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Reader getCapTable() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Payload::Builder { public: typedef Payload Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasContent(); inline ::capnp::AnyPointer::Builder getContent(); inline ::capnp::AnyPointer::Builder initContent(); inline bool hasCapTable(); inline ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Builder getCapTable(); inline void setCapTable( ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Builder initCapTable(unsigned int size); inline void adoptCapTable(::capnp::Orphan< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>> disownCapTable(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Payload::Pipeline { public: typedef Payload Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class CapDescriptor::Reader { public: typedef CapDescriptor Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNone() const; inline ::capnp::Void getNone() const; inline bool isSenderHosted() const; inline ::uint32_t getSenderHosted() const; inline bool isSenderPromise() const; inline ::uint32_t getSenderPromise() const; inline bool isReceiverHosted() const; inline ::uint32_t getReceiverHosted() const; inline bool isReceiverAnswer() const; inline bool hasReceiverAnswer() const; inline ::capnp::rpc::PromisedAnswer::Reader getReceiverAnswer() const; inline bool isThirdPartyHosted() const; inline bool hasThirdPartyHosted() const; inline ::capnp::rpc::ThirdPartyCapDescriptor::Reader getThirdPartyHosted() const; inline ::uint8_t getAttachedFd() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class CapDescriptor::Builder { public: typedef CapDescriptor Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNone(); inline ::capnp::Void getNone(); inline void setNone( ::capnp::Void value = ::capnp::VOID); inline bool isSenderHosted(); inline ::uint32_t getSenderHosted(); inline void setSenderHosted( ::uint32_t value); inline bool isSenderPromise(); inline ::uint32_t getSenderPromise(); inline void setSenderPromise( ::uint32_t value); inline bool isReceiverHosted(); inline ::uint32_t getReceiverHosted(); inline void setReceiverHosted( ::uint32_t value); inline bool isReceiverAnswer(); inline bool hasReceiverAnswer(); inline ::capnp::rpc::PromisedAnswer::Builder getReceiverAnswer(); inline void setReceiverAnswer( ::capnp::rpc::PromisedAnswer::Reader value); inline ::capnp::rpc::PromisedAnswer::Builder initReceiverAnswer(); inline void adoptReceiverAnswer(::capnp::Orphan< ::capnp::rpc::PromisedAnswer>&& value); inline ::capnp::Orphan< ::capnp::rpc::PromisedAnswer> disownReceiverAnswer(); inline bool isThirdPartyHosted(); inline bool hasThirdPartyHosted(); inline ::capnp::rpc::ThirdPartyCapDescriptor::Builder getThirdPartyHosted(); inline void setThirdPartyHosted( ::capnp::rpc::ThirdPartyCapDescriptor::Reader value); inline ::capnp::rpc::ThirdPartyCapDescriptor::Builder initThirdPartyHosted(); inline void adoptThirdPartyHosted(::capnp::Orphan< ::capnp::rpc::ThirdPartyCapDescriptor>&& value); inline ::capnp::Orphan< ::capnp::rpc::ThirdPartyCapDescriptor> disownThirdPartyHosted(); inline ::uint8_t getAttachedFd(); inline void setAttachedFd( ::uint8_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class CapDescriptor::Pipeline { public: typedef CapDescriptor Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class PromisedAnswer::Reader { public: typedef PromisedAnswer Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId() const; inline bool hasTransform() const; inline ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Reader getTransform() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class PromisedAnswer::Builder { public: typedef PromisedAnswer Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getQuestionId(); inline void setQuestionId( ::uint32_t value); inline bool hasTransform(); inline ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Builder getTransform(); inline void setTransform( ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Reader value); inline ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Builder initTransform(unsigned int size); inline void adoptTransform(::capnp::Orphan< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>&& value); inline ::capnp::Orphan< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>> disownTransform(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class PromisedAnswer::Pipeline { public: typedef PromisedAnswer Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class PromisedAnswer::Op::Reader { public: typedef Op Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline Which which() const; inline bool isNoop() const; inline ::capnp::Void getNoop() const; inline bool isGetPointerField() const; inline ::uint16_t getGetPointerField() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class PromisedAnswer::Op::Builder { public: typedef Op Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline Which which(); inline bool isNoop(); inline ::capnp::Void getNoop(); inline void setNoop( ::capnp::Void value = ::capnp::VOID); inline bool isGetPointerField(); inline ::uint16_t getGetPointerField(); inline void setGetPointerField( ::uint16_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class PromisedAnswer::Op::Pipeline { public: typedef Op Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class ThirdPartyCapDescriptor::Reader { public: typedef ThirdPartyCapDescriptor Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasId() const; inline ::capnp::AnyPointer::Reader getId() const; inline ::uint32_t getVineId() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class ThirdPartyCapDescriptor::Builder { public: typedef ThirdPartyCapDescriptor Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasId(); inline ::capnp::AnyPointer::Builder getId(); inline ::capnp::AnyPointer::Builder initId(); inline ::uint32_t getVineId(); inline void setVineId( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class ThirdPartyCapDescriptor::Pipeline { public: typedef ThirdPartyCapDescriptor Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class Exception::Reader { public: typedef Exception Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline bool hasReason() const; inline ::capnp::Text::Reader getReason() const; inline bool getObsoleteIsCallersFault() const; inline ::uint16_t getObsoleteDurability() const; inline ::capnp::rpc::Exception::Type getType() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class Exception::Builder { public: typedef Exception Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline bool hasReason(); inline ::capnp::Text::Builder getReason(); inline void setReason( ::capnp::Text::Reader value); inline ::capnp::Text::Builder initReason(unsigned int size); inline void adoptReason(::capnp::Orphan< ::capnp::Text>&& value); inline ::capnp::Orphan< ::capnp::Text> disownReason(); inline bool getObsoleteIsCallersFault(); inline void setObsoleteIsCallersFault(bool value); inline ::uint16_t getObsoleteDurability(); inline void setObsoleteDurability( ::uint16_t value); inline ::capnp::rpc::Exception::Type getType(); inline void setType( ::capnp::rpc::Exception::Type value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class Exception::Pipeline { public: typedef Exception Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= inline ::capnp::rpc::Message::Which Message::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::Message::Which Message::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Message::Reader::isUnimplemented() const { return which() == Message::UNIMPLEMENTED; } inline bool Message::Builder::isUnimplemented() { return which() == Message::UNIMPLEMENTED; } inline bool Message::Reader::hasUnimplemented() const { if (which() != Message::UNIMPLEMENTED) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasUnimplemented() { if (which() != Message::UNIMPLEMENTED) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Message::Reader Message::Reader::getUnimplemented() const { KJ_IREQUIRE((which() == Message::UNIMPLEMENTED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Message>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Message::Builder Message::Builder::getUnimplemented() { KJ_IREQUIRE((which() == Message::UNIMPLEMENTED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Message>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setUnimplemented( ::capnp::rpc::Message::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::UNIMPLEMENTED); ::capnp::_::PointerHelpers< ::capnp::rpc::Message>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Message::Builder Message::Builder::initUnimplemented() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::UNIMPLEMENTED); return ::capnp::_::PointerHelpers< ::capnp::rpc::Message>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptUnimplemented( ::capnp::Orphan< ::capnp::rpc::Message>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::UNIMPLEMENTED); ::capnp::_::PointerHelpers< ::capnp::rpc::Message>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Message> Message::Builder::disownUnimplemented() { KJ_IREQUIRE((which() == Message::UNIMPLEMENTED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Message>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isAbort() const { return which() == Message::ABORT; } inline bool Message::Builder::isAbort() { return which() == Message::ABORT; } inline bool Message::Reader::hasAbort() const { if (which() != Message::ABORT) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasAbort() { if (which() != Message::ABORT) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Exception::Reader Message::Reader::getAbort() const { KJ_IREQUIRE((which() == Message::ABORT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Exception::Builder Message::Builder::getAbort() { KJ_IREQUIRE((which() == Message::ABORT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setAbort( ::capnp::rpc::Exception::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::ABORT); ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Exception::Builder Message::Builder::initAbort() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::ABORT); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptAbort( ::capnp::Orphan< ::capnp::rpc::Exception>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::ABORT); ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Exception> Message::Builder::disownAbort() { KJ_IREQUIRE((which() == Message::ABORT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isCall() const { return which() == Message::CALL; } inline bool Message::Builder::isCall() { return which() == Message::CALL; } inline bool Message::Reader::hasCall() const { if (which() != Message::CALL) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasCall() { if (which() != Message::CALL) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Call::Reader Message::Reader::getCall() const { KJ_IREQUIRE((which() == Message::CALL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Call>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Call::Builder Message::Builder::getCall() { KJ_IREQUIRE((which() == Message::CALL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Call>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setCall( ::capnp::rpc::Call::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::CALL); ::capnp::_::PointerHelpers< ::capnp::rpc::Call>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Call::Builder Message::Builder::initCall() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::CALL); return ::capnp::_::PointerHelpers< ::capnp::rpc::Call>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptCall( ::capnp::Orphan< ::capnp::rpc::Call>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::CALL); ::capnp::_::PointerHelpers< ::capnp::rpc::Call>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Call> Message::Builder::disownCall() { KJ_IREQUIRE((which() == Message::CALL), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Call>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isReturn() const { return which() == Message::RETURN; } inline bool Message::Builder::isReturn() { return which() == Message::RETURN; } inline bool Message::Reader::hasReturn() const { if (which() != Message::RETURN) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasReturn() { if (which() != Message::RETURN) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Return::Reader Message::Reader::getReturn() const { KJ_IREQUIRE((which() == Message::RETURN), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Return>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Return::Builder Message::Builder::getReturn() { KJ_IREQUIRE((which() == Message::RETURN), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Return>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setReturn( ::capnp::rpc::Return::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RETURN); ::capnp::_::PointerHelpers< ::capnp::rpc::Return>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Return::Builder Message::Builder::initReturn() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RETURN); return ::capnp::_::PointerHelpers< ::capnp::rpc::Return>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptReturn( ::capnp::Orphan< ::capnp::rpc::Return>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RETURN); ::capnp::_::PointerHelpers< ::capnp::rpc::Return>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Return> Message::Builder::disownReturn() { KJ_IREQUIRE((which() == Message::RETURN), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Return>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isFinish() const { return which() == Message::FINISH; } inline bool Message::Builder::isFinish() { return which() == Message::FINISH; } inline bool Message::Reader::hasFinish() const { if (which() != Message::FINISH) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasFinish() { if (which() != Message::FINISH) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Finish::Reader Message::Reader::getFinish() const { KJ_IREQUIRE((which() == Message::FINISH), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Finish>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Finish::Builder Message::Builder::getFinish() { KJ_IREQUIRE((which() == Message::FINISH), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Finish>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setFinish( ::capnp::rpc::Finish::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::FINISH); ::capnp::_::PointerHelpers< ::capnp::rpc::Finish>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Finish::Builder Message::Builder::initFinish() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::FINISH); return ::capnp::_::PointerHelpers< ::capnp::rpc::Finish>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptFinish( ::capnp::Orphan< ::capnp::rpc::Finish>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::FINISH); ::capnp::_::PointerHelpers< ::capnp::rpc::Finish>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Finish> Message::Builder::disownFinish() { KJ_IREQUIRE((which() == Message::FINISH), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Finish>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isResolve() const { return which() == Message::RESOLVE; } inline bool Message::Builder::isResolve() { return which() == Message::RESOLVE; } inline bool Message::Reader::hasResolve() const { if (which() != Message::RESOLVE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasResolve() { if (which() != Message::RESOLVE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Resolve::Reader Message::Reader::getResolve() const { KJ_IREQUIRE((which() == Message::RESOLVE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Resolve>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Resolve::Builder Message::Builder::getResolve() { KJ_IREQUIRE((which() == Message::RESOLVE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Resolve>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setResolve( ::capnp::rpc::Resolve::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RESOLVE); ::capnp::_::PointerHelpers< ::capnp::rpc::Resolve>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Resolve::Builder Message::Builder::initResolve() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RESOLVE); return ::capnp::_::PointerHelpers< ::capnp::rpc::Resolve>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptResolve( ::capnp::Orphan< ::capnp::rpc::Resolve>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RESOLVE); ::capnp::_::PointerHelpers< ::capnp::rpc::Resolve>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Resolve> Message::Builder::disownResolve() { KJ_IREQUIRE((which() == Message::RESOLVE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Resolve>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isRelease() const { return which() == Message::RELEASE; } inline bool Message::Builder::isRelease() { return which() == Message::RELEASE; } inline bool Message::Reader::hasRelease() const { if (which() != Message::RELEASE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasRelease() { if (which() != Message::RELEASE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Release::Reader Message::Reader::getRelease() const { KJ_IREQUIRE((which() == Message::RELEASE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Release>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Release::Builder Message::Builder::getRelease() { KJ_IREQUIRE((which() == Message::RELEASE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Release>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setRelease( ::capnp::rpc::Release::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RELEASE); ::capnp::_::PointerHelpers< ::capnp::rpc::Release>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Release::Builder Message::Builder::initRelease() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RELEASE); return ::capnp::_::PointerHelpers< ::capnp::rpc::Release>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptRelease( ::capnp::Orphan< ::capnp::rpc::Release>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::RELEASE); ::capnp::_::PointerHelpers< ::capnp::rpc::Release>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Release> Message::Builder::disownRelease() { KJ_IREQUIRE((which() == Message::RELEASE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Release>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isObsoleteSave() const { return which() == Message::OBSOLETE_SAVE; } inline bool Message::Builder::isObsoleteSave() { return which() == Message::OBSOLETE_SAVE; } inline bool Message::Reader::hasObsoleteSave() const { if (which() != Message::OBSOLETE_SAVE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasObsoleteSave() { if (which() != Message::OBSOLETE_SAVE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Message::Reader::getObsoleteSave() const { KJ_IREQUIRE((which() == Message::OBSOLETE_SAVE), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Message::Builder::getObsoleteSave() { KJ_IREQUIRE((which() == Message::OBSOLETE_SAVE), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Message::Builder::initObsoleteSave() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::OBSOLETE_SAVE); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline bool Message::Reader::isBootstrap() const { return which() == Message::BOOTSTRAP; } inline bool Message::Builder::isBootstrap() { return which() == Message::BOOTSTRAP; } inline bool Message::Reader::hasBootstrap() const { if (which() != Message::BOOTSTRAP) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasBootstrap() { if (which() != Message::BOOTSTRAP) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Bootstrap::Reader Message::Reader::getBootstrap() const { KJ_IREQUIRE((which() == Message::BOOTSTRAP), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Bootstrap>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Bootstrap::Builder Message::Builder::getBootstrap() { KJ_IREQUIRE((which() == Message::BOOTSTRAP), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Bootstrap>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setBootstrap( ::capnp::rpc::Bootstrap::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::BOOTSTRAP); ::capnp::_::PointerHelpers< ::capnp::rpc::Bootstrap>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Bootstrap::Builder Message::Builder::initBootstrap() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::BOOTSTRAP); return ::capnp::_::PointerHelpers< ::capnp::rpc::Bootstrap>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptBootstrap( ::capnp::Orphan< ::capnp::rpc::Bootstrap>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::BOOTSTRAP); ::capnp::_::PointerHelpers< ::capnp::rpc::Bootstrap>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Bootstrap> Message::Builder::disownBootstrap() { KJ_IREQUIRE((which() == Message::BOOTSTRAP), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Bootstrap>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isObsoleteDelete() const { return which() == Message::OBSOLETE_DELETE; } inline bool Message::Builder::isObsoleteDelete() { return which() == Message::OBSOLETE_DELETE; } inline bool Message::Reader::hasObsoleteDelete() const { if (which() != Message::OBSOLETE_DELETE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasObsoleteDelete() { if (which() != Message::OBSOLETE_DELETE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Message::Reader::getObsoleteDelete() const { KJ_IREQUIRE((which() == Message::OBSOLETE_DELETE), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Message::Builder::getObsoleteDelete() { KJ_IREQUIRE((which() == Message::OBSOLETE_DELETE), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Message::Builder::initObsoleteDelete() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::OBSOLETE_DELETE); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline bool Message::Reader::isProvide() const { return which() == Message::PROVIDE; } inline bool Message::Builder::isProvide() { return which() == Message::PROVIDE; } inline bool Message::Reader::hasProvide() const { if (which() != Message::PROVIDE) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasProvide() { if (which() != Message::PROVIDE) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Provide::Reader Message::Reader::getProvide() const { KJ_IREQUIRE((which() == Message::PROVIDE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Provide>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Provide::Builder Message::Builder::getProvide() { KJ_IREQUIRE((which() == Message::PROVIDE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Provide>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setProvide( ::capnp::rpc::Provide::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::PROVIDE); ::capnp::_::PointerHelpers< ::capnp::rpc::Provide>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Provide::Builder Message::Builder::initProvide() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::PROVIDE); return ::capnp::_::PointerHelpers< ::capnp::rpc::Provide>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptProvide( ::capnp::Orphan< ::capnp::rpc::Provide>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::PROVIDE); ::capnp::_::PointerHelpers< ::capnp::rpc::Provide>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Provide> Message::Builder::disownProvide() { KJ_IREQUIRE((which() == Message::PROVIDE), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Provide>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isAccept() const { return which() == Message::ACCEPT; } inline bool Message::Builder::isAccept() { return which() == Message::ACCEPT; } inline bool Message::Reader::hasAccept() const { if (which() != Message::ACCEPT) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasAccept() { if (which() != Message::ACCEPT) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Accept::Reader Message::Reader::getAccept() const { KJ_IREQUIRE((which() == Message::ACCEPT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Accept>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Accept::Builder Message::Builder::getAccept() { KJ_IREQUIRE((which() == Message::ACCEPT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Accept>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setAccept( ::capnp::rpc::Accept::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::ACCEPT); ::capnp::_::PointerHelpers< ::capnp::rpc::Accept>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Accept::Builder Message::Builder::initAccept() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::ACCEPT); return ::capnp::_::PointerHelpers< ::capnp::rpc::Accept>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptAccept( ::capnp::Orphan< ::capnp::rpc::Accept>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::ACCEPT); ::capnp::_::PointerHelpers< ::capnp::rpc::Accept>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Accept> Message::Builder::disownAccept() { KJ_IREQUIRE((which() == Message::ACCEPT), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Accept>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isJoin() const { return which() == Message::JOIN; } inline bool Message::Builder::isJoin() { return which() == Message::JOIN; } inline bool Message::Reader::hasJoin() const { if (which() != Message::JOIN) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasJoin() { if (which() != Message::JOIN) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Join::Reader Message::Reader::getJoin() const { KJ_IREQUIRE((which() == Message::JOIN), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Join>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Join::Builder Message::Builder::getJoin() { KJ_IREQUIRE((which() == Message::JOIN), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Join>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setJoin( ::capnp::rpc::Join::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::JOIN); ::capnp::_::PointerHelpers< ::capnp::rpc::Join>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Join::Builder Message::Builder::initJoin() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::JOIN); return ::capnp::_::PointerHelpers< ::capnp::rpc::Join>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptJoin( ::capnp::Orphan< ::capnp::rpc::Join>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::JOIN); ::capnp::_::PointerHelpers< ::capnp::rpc::Join>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Join> Message::Builder::disownJoin() { KJ_IREQUIRE((which() == Message::JOIN), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Join>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Message::Reader::isDisembargo() const { return which() == Message::DISEMBARGO; } inline bool Message::Builder::isDisembargo() { return which() == Message::DISEMBARGO; } inline bool Message::Reader::hasDisembargo() const { if (which() != Message::DISEMBARGO) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Message::Builder::hasDisembargo() { if (which() != Message::DISEMBARGO) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Disembargo::Reader Message::Reader::getDisembargo() const { KJ_IREQUIRE((which() == Message::DISEMBARGO), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Disembargo>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Disembargo::Builder Message::Builder::getDisembargo() { KJ_IREQUIRE((which() == Message::DISEMBARGO), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Disembargo>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::setDisembargo( ::capnp::rpc::Disembargo::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::DISEMBARGO); ::capnp::_::PointerHelpers< ::capnp::rpc::Disembargo>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Disembargo::Builder Message::Builder::initDisembargo() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::DISEMBARGO); return ::capnp::_::PointerHelpers< ::capnp::rpc::Disembargo>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Message::Builder::adoptDisembargo( ::capnp::Orphan< ::capnp::rpc::Disembargo>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, Message::DISEMBARGO); ::capnp::_::PointerHelpers< ::capnp::rpc::Disembargo>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Disembargo> Message::Builder::disownDisembargo() { KJ_IREQUIRE((which() == Message::DISEMBARGO), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Disembargo>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Bootstrap::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Bootstrap::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Bootstrap::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Bootstrap::Reader::hasDeprecatedObjectId() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Bootstrap::Builder::hasDeprecatedObjectId() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Bootstrap::Reader::getDeprecatedObjectId() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Bootstrap::Builder::getDeprecatedObjectId() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Bootstrap::Builder::initDeprecatedObjectId() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::uint32_t Call::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Call::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Call::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Call::Reader::hasTarget() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Call::Builder::hasTarget() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::MessageTarget::Reader Call::Reader::getTarget() const { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::MessageTarget::Builder Call::Builder::getTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::rpc::MessageTarget::Pipeline Call::Pipeline::getTarget() { return ::capnp::rpc::MessageTarget::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Call::Builder::setTarget( ::capnp::rpc::MessageTarget::Reader value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::MessageTarget::Builder Call::Builder::initTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Call::Builder::adoptTarget( ::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> Call::Builder::disownTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint64_t Call::Reader::getInterfaceId() const { return _reader.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint64_t Call::Builder::getInterfaceId() { return _builder.getDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Call::Builder::setInterfaceId( ::uint64_t value) { _builder.setDataField< ::uint64_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::uint16_t Call::Reader::getMethodId() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint16_t Call::Builder::getMethodId() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Call::Builder::setMethodId( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Call::Reader::hasParams() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Call::Builder::hasParams() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Payload::Reader Call::Reader::getParams() const { return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Payload::Builder Call::Builder::getParams() { return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::rpc::Payload::Pipeline Call::Pipeline::getParams() { return ::capnp::rpc::Payload::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE inline void Call::Builder::setParams( ::capnp::rpc::Payload::Reader value) { ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Payload::Builder Call::Builder::initParams() { return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Call::Builder::adoptParams( ::capnp::Orphan< ::capnp::rpc::Payload>&& value) { ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Payload> Call::Builder::disownParams() { return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline typename Call::SendResultsTo::Reader Call::Reader::getSendResultsTo() const { return typename Call::SendResultsTo::Reader(_reader); } inline typename Call::SendResultsTo::Builder Call::Builder::getSendResultsTo() { return typename Call::SendResultsTo::Builder(_builder); } #if !CAPNP_LITE inline typename Call::SendResultsTo::Pipeline Call::Pipeline::getSendResultsTo() { return typename Call::SendResultsTo::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Call::SendResultsTo::Builder Call::Builder::initSendResultsTo() { _builder.setDataField< ::uint16_t>(::capnp::bounded<3>() * ::capnp::ELEMENTS, 0); _builder.getPointerField(::capnp::bounded<2>() * ::capnp::POINTERS).clear(); return typename Call::SendResultsTo::Builder(_builder); } inline bool Call::Reader::getAllowThirdPartyTailCall() const { return _reader.getDataField( ::capnp::bounded<128>() * ::capnp::ELEMENTS); } inline bool Call::Builder::getAllowThirdPartyTailCall() { return _builder.getDataField( ::capnp::bounded<128>() * ::capnp::ELEMENTS); } inline void Call::Builder::setAllowThirdPartyTailCall(bool value) { _builder.setDataField( ::capnp::bounded<128>() * ::capnp::ELEMENTS, value); } inline ::capnp::rpc::Call::SendResultsTo::Which Call::SendResultsTo::Reader::which() const { return _reader.getDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::Call::SendResultsTo::Which Call::SendResultsTo::Builder::which() { return _builder.getDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline bool Call::SendResultsTo::Reader::isCaller() const { return which() == Call::SendResultsTo::CALLER; } inline bool Call::SendResultsTo::Builder::isCaller() { return which() == Call::SendResultsTo::CALLER; } inline ::capnp::Void Call::SendResultsTo::Reader::getCaller() const { KJ_IREQUIRE((which() == Call::SendResultsTo::CALLER), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Call::SendResultsTo::Builder::getCaller() { KJ_IREQUIRE((which() == Call::SendResultsTo::CALLER), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Call::SendResultsTo::Builder::setCaller( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Call::SendResultsTo::CALLER); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Call::SendResultsTo::Reader::isYourself() const { return which() == Call::SendResultsTo::YOURSELF; } inline bool Call::SendResultsTo::Builder::isYourself() { return which() == Call::SendResultsTo::YOURSELF; } inline ::capnp::Void Call::SendResultsTo::Reader::getYourself() const { KJ_IREQUIRE((which() == Call::SendResultsTo::YOURSELF), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Call::SendResultsTo::Builder::getYourself() { KJ_IREQUIRE((which() == Call::SendResultsTo::YOURSELF), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Call::SendResultsTo::Builder::setYourself( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Call::SendResultsTo::YOURSELF); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Call::SendResultsTo::Reader::isThirdParty() const { return which() == Call::SendResultsTo::THIRD_PARTY; } inline bool Call::SendResultsTo::Builder::isThirdParty() { return which() == Call::SendResultsTo::THIRD_PARTY; } inline bool Call::SendResultsTo::Reader::hasThirdParty() const { if (which() != Call::SendResultsTo::THIRD_PARTY) return false; return !_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline bool Call::SendResultsTo::Builder::hasThirdParty() { if (which() != Call::SendResultsTo::THIRD_PARTY) return false; return !_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Call::SendResultsTo::Reader::getThirdParty() const { KJ_IREQUIRE((which() == Call::SendResultsTo::THIRD_PARTY), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Call::SendResultsTo::Builder::getThirdParty() { KJ_IREQUIRE((which() == Call::SendResultsTo::THIRD_PARTY), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Call::SendResultsTo::Builder::initThirdParty() { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Call::SendResultsTo::THIRD_PARTY); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<2>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::capnp::rpc::Return::Which Return::Reader::which() const { return _reader.getDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::Return::Which Return::Builder::which() { return _builder.getDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::uint32_t Return::Reader::getAnswerId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Return::Builder::getAnswerId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Return::Builder::setAnswerId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Return::Reader::getReleaseParamCaps() const { return _reader.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, true); } inline bool Return::Builder::getReleaseParamCaps() { return _builder.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, true); } inline void Return::Builder::setReleaseParamCaps(bool value) { _builder.setDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, value, true); } inline bool Return::Reader::isResults() const { return which() == Return::RESULTS; } inline bool Return::Builder::isResults() { return which() == Return::RESULTS; } inline bool Return::Reader::hasResults() const { if (which() != Return::RESULTS) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Return::Builder::hasResults() { if (which() != Return::RESULTS) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Payload::Reader Return::Reader::getResults() const { KJ_IREQUIRE((which() == Return::RESULTS), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Payload::Builder Return::Builder::getResults() { KJ_IREQUIRE((which() == Return::RESULTS), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Return::Builder::setResults( ::capnp::rpc::Payload::Reader value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::RESULTS); ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Payload::Builder Return::Builder::initResults() { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::RESULTS); return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Return::Builder::adoptResults( ::capnp::Orphan< ::capnp::rpc::Payload>&& value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::RESULTS); ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Payload> Return::Builder::disownResults() { KJ_IREQUIRE((which() == Return::RESULTS), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Payload>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Return::Reader::isException() const { return which() == Return::EXCEPTION; } inline bool Return::Builder::isException() { return which() == Return::EXCEPTION; } inline bool Return::Reader::hasException() const { if (which() != Return::EXCEPTION) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Return::Builder::hasException() { if (which() != Return::EXCEPTION) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Exception::Reader Return::Reader::getException() const { KJ_IREQUIRE((which() == Return::EXCEPTION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Exception::Builder Return::Builder::getException() { KJ_IREQUIRE((which() == Return::EXCEPTION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Return::Builder::setException( ::capnp::rpc::Exception::Reader value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::EXCEPTION); ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Exception::Builder Return::Builder::initException() { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::EXCEPTION); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Return::Builder::adoptException( ::capnp::Orphan< ::capnp::rpc::Exception>&& value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::EXCEPTION); ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Exception> Return::Builder::disownException() { KJ_IREQUIRE((which() == Return::EXCEPTION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Return::Reader::isCanceled() const { return which() == Return::CANCELED; } inline bool Return::Builder::isCanceled() { return which() == Return::CANCELED; } inline ::capnp::Void Return::Reader::getCanceled() const { KJ_IREQUIRE((which() == Return::CANCELED), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Return::Builder::getCanceled() { KJ_IREQUIRE((which() == Return::CANCELED), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Return::Builder::setCanceled( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::CANCELED); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Return::Reader::isResultsSentElsewhere() const { return which() == Return::RESULTS_SENT_ELSEWHERE; } inline bool Return::Builder::isResultsSentElsewhere() { return which() == Return::RESULTS_SENT_ELSEWHERE; } inline ::capnp::Void Return::Reader::getResultsSentElsewhere() const { KJ_IREQUIRE((which() == Return::RESULTS_SENT_ELSEWHERE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Return::Builder::getResultsSentElsewhere() { KJ_IREQUIRE((which() == Return::RESULTS_SENT_ELSEWHERE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Return::Builder::setResultsSentElsewhere( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::RESULTS_SENT_ELSEWHERE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Return::Reader::isTakeFromOtherQuestion() const { return which() == Return::TAKE_FROM_OTHER_QUESTION; } inline bool Return::Builder::isTakeFromOtherQuestion() { return which() == Return::TAKE_FROM_OTHER_QUESTION; } inline ::uint32_t Return::Reader::getTakeFromOtherQuestion() const { KJ_IREQUIRE((which() == Return::TAKE_FROM_OTHER_QUESTION), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Return::Builder::getTakeFromOtherQuestion() { KJ_IREQUIRE((which() == Return::TAKE_FROM_OTHER_QUESTION), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Return::Builder::setTakeFromOtherQuestion( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::TAKE_FROM_OTHER_QUESTION); _builder.setDataField< ::uint32_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline bool Return::Reader::isAcceptFromThirdParty() const { return which() == Return::ACCEPT_FROM_THIRD_PARTY; } inline bool Return::Builder::isAcceptFromThirdParty() { return which() == Return::ACCEPT_FROM_THIRD_PARTY; } inline bool Return::Reader::hasAcceptFromThirdParty() const { if (which() != Return::ACCEPT_FROM_THIRD_PARTY) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Return::Builder::hasAcceptFromThirdParty() { if (which() != Return::ACCEPT_FROM_THIRD_PARTY) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Return::Reader::getAcceptFromThirdParty() const { KJ_IREQUIRE((which() == Return::ACCEPT_FROM_THIRD_PARTY), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Return::Builder::getAcceptFromThirdParty() { KJ_IREQUIRE((which() == Return::ACCEPT_FROM_THIRD_PARTY), "Must check which() before get()ing a union member."); return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Return::Builder::initAcceptFromThirdParty() { _builder.setDataField( ::capnp::bounded<3>() * ::capnp::ELEMENTS, Return::ACCEPT_FROM_THIRD_PARTY); auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::uint32_t Finish::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Finish::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Finish::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Finish::Reader::getReleaseResultCaps() const { return _reader.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, true); } inline bool Finish::Builder::getReleaseResultCaps() { return _builder.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, true); } inline void Finish::Builder::setReleaseResultCaps(bool value) { _builder.setDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, value, true); } inline ::capnp::rpc::Resolve::Which Resolve::Reader::which() const { return _reader.getDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::Resolve::Which Resolve::Builder::which() { return _builder.getDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint32_t Resolve::Reader::getPromiseId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Resolve::Builder::getPromiseId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Resolve::Builder::setPromiseId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Resolve::Reader::isCap() const { return which() == Resolve::CAP; } inline bool Resolve::Builder::isCap() { return which() == Resolve::CAP; } inline bool Resolve::Reader::hasCap() const { if (which() != Resolve::CAP) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Resolve::Builder::hasCap() { if (which() != Resolve::CAP) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::CapDescriptor::Reader Resolve::Reader::getCap() const { KJ_IREQUIRE((which() == Resolve::CAP), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::CapDescriptor>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::CapDescriptor::Builder Resolve::Builder::getCap() { KJ_IREQUIRE((which() == Resolve::CAP), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::CapDescriptor>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Resolve::Builder::setCap( ::capnp::rpc::CapDescriptor::Reader value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Resolve::CAP); ::capnp::_::PointerHelpers< ::capnp::rpc::CapDescriptor>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::CapDescriptor::Builder Resolve::Builder::initCap() { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Resolve::CAP); return ::capnp::_::PointerHelpers< ::capnp::rpc::CapDescriptor>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Resolve::Builder::adoptCap( ::capnp::Orphan< ::capnp::rpc::CapDescriptor>&& value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Resolve::CAP); ::capnp::_::PointerHelpers< ::capnp::rpc::CapDescriptor>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::CapDescriptor> Resolve::Builder::disownCap() { KJ_IREQUIRE((which() == Resolve::CAP), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::CapDescriptor>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Resolve::Reader::isException() const { return which() == Resolve::EXCEPTION; } inline bool Resolve::Builder::isException() { return which() == Resolve::EXCEPTION; } inline bool Resolve::Reader::hasException() const { if (which() != Resolve::EXCEPTION) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Resolve::Builder::hasException() { if (which() != Resolve::EXCEPTION) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::Exception::Reader Resolve::Reader::getException() const { KJ_IREQUIRE((which() == Resolve::EXCEPTION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::Exception::Builder Resolve::Builder::getException() { KJ_IREQUIRE((which() == Resolve::EXCEPTION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Resolve::Builder::setException( ::capnp::rpc::Exception::Reader value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Resolve::EXCEPTION); ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::Exception::Builder Resolve::Builder::initException() { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Resolve::EXCEPTION); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Resolve::Builder::adoptException( ::capnp::Orphan< ::capnp::rpc::Exception>&& value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Resolve::EXCEPTION); ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::Exception> Resolve::Builder::disownException() { KJ_IREQUIRE((which() == Resolve::EXCEPTION), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::Exception>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint32_t Release::Reader::getId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Release::Builder::getId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Release::Builder::setId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Release::Reader::getReferenceCount() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t Release::Builder::getReferenceCount() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Release::Builder::setReferenceCount( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool Disembargo::Reader::hasTarget() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Disembargo::Builder::hasTarget() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::MessageTarget::Reader Disembargo::Reader::getTarget() const { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::MessageTarget::Builder Disembargo::Builder::getTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::rpc::MessageTarget::Pipeline Disembargo::Pipeline::getTarget() { return ::capnp::rpc::MessageTarget::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Disembargo::Builder::setTarget( ::capnp::rpc::MessageTarget::Reader value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::MessageTarget::Builder Disembargo::Builder::initTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Disembargo::Builder::adoptTarget( ::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> Disembargo::Builder::disownTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline typename Disembargo::Context::Reader Disembargo::Reader::getContext() const { return typename Disembargo::Context::Reader(_reader); } inline typename Disembargo::Context::Builder Disembargo::Builder::getContext() { return typename Disembargo::Context::Builder(_builder); } #if !CAPNP_LITE inline typename Disembargo::Context::Pipeline Disembargo::Pipeline::getContext() { return typename Disembargo::Context::Pipeline(_typeless.noop()); } #endif // !CAPNP_LITE inline typename Disembargo::Context::Builder Disembargo::Builder::initContext() { _builder.setDataField< ::uint32_t>(::capnp::bounded<0>() * ::capnp::ELEMENTS, 0); _builder.setDataField< ::uint16_t>(::capnp::bounded<2>() * ::capnp::ELEMENTS, 0); return typename Disembargo::Context::Builder(_builder); } inline ::capnp::rpc::Disembargo::Context::Which Disembargo::Context::Reader::which() const { return _reader.getDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::Disembargo::Context::Which Disembargo::Context::Builder::which() { return _builder.getDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline bool Disembargo::Context::Reader::isSenderLoopback() const { return which() == Disembargo::Context::SENDER_LOOPBACK; } inline bool Disembargo::Context::Builder::isSenderLoopback() { return which() == Disembargo::Context::SENDER_LOOPBACK; } inline ::uint32_t Disembargo::Context::Reader::getSenderLoopback() const { KJ_IREQUIRE((which() == Disembargo::Context::SENDER_LOOPBACK), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Disembargo::Context::Builder::getSenderLoopback() { KJ_IREQUIRE((which() == Disembargo::Context::SENDER_LOOPBACK), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Disembargo::Context::Builder::setSenderLoopback( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Disembargo::Context::SENDER_LOOPBACK); _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Disembargo::Context::Reader::isReceiverLoopback() const { return which() == Disembargo::Context::RECEIVER_LOOPBACK; } inline bool Disembargo::Context::Builder::isReceiverLoopback() { return which() == Disembargo::Context::RECEIVER_LOOPBACK; } inline ::uint32_t Disembargo::Context::Reader::getReceiverLoopback() const { KJ_IREQUIRE((which() == Disembargo::Context::RECEIVER_LOOPBACK), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Disembargo::Context::Builder::getReceiverLoopback() { KJ_IREQUIRE((which() == Disembargo::Context::RECEIVER_LOOPBACK), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Disembargo::Context::Builder::setReceiverLoopback( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Disembargo::Context::RECEIVER_LOOPBACK); _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Disembargo::Context::Reader::isAccept() const { return which() == Disembargo::Context::ACCEPT; } inline bool Disembargo::Context::Builder::isAccept() { return which() == Disembargo::Context::ACCEPT; } inline ::capnp::Void Disembargo::Context::Reader::getAccept() const { KJ_IREQUIRE((which() == Disembargo::Context::ACCEPT), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void Disembargo::Context::Builder::getAccept() { KJ_IREQUIRE((which() == Disembargo::Context::ACCEPT), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Disembargo::Context::Builder::setAccept( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Disembargo::Context::ACCEPT); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Disembargo::Context::Reader::isProvide() const { return which() == Disembargo::Context::PROVIDE; } inline bool Disembargo::Context::Builder::isProvide() { return which() == Disembargo::Context::PROVIDE; } inline ::uint32_t Disembargo::Context::Reader::getProvide() const { KJ_IREQUIRE((which() == Disembargo::Context::PROVIDE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Disembargo::Context::Builder::getProvide() { KJ_IREQUIRE((which() == Disembargo::Context::PROVIDE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Disembargo::Context::Builder::setProvide( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, Disembargo::Context::PROVIDE); _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Provide::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Provide::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Provide::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Provide::Reader::hasTarget() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Provide::Builder::hasTarget() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::MessageTarget::Reader Provide::Reader::getTarget() const { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::MessageTarget::Builder Provide::Builder::getTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::rpc::MessageTarget::Pipeline Provide::Pipeline::getTarget() { return ::capnp::rpc::MessageTarget::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Provide::Builder::setTarget( ::capnp::rpc::MessageTarget::Reader value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::MessageTarget::Builder Provide::Builder::initTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Provide::Builder::adoptTarget( ::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> Provide::Builder::disownTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Provide::Reader::hasRecipient() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Provide::Builder::hasRecipient() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Provide::Reader::getRecipient() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Provide::Builder::getRecipient() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Provide::Builder::initRecipient() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::uint32_t Accept::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Accept::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Accept::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Accept::Reader::hasProvision() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Accept::Builder::hasProvision() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Accept::Reader::getProvision() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Accept::Builder::getProvision() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Accept::Builder::initProvision() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline bool Accept::Reader::getEmbargo() const { return _reader.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS); } inline bool Accept::Builder::getEmbargo() { return _builder.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS); } inline void Accept::Builder::setEmbargo(bool value) { _builder.setDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, value); } inline ::uint32_t Join::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t Join::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Join::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Join::Reader::hasTarget() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Join::Builder::hasTarget() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::MessageTarget::Reader Join::Reader::getTarget() const { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::MessageTarget::Builder Join::Builder::getTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE inline ::capnp::rpc::MessageTarget::Pipeline Join::Pipeline::getTarget() { return ::capnp::rpc::MessageTarget::Pipeline(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE inline void Join::Builder::setTarget( ::capnp::rpc::MessageTarget::Reader value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::MessageTarget::Builder Join::Builder::initTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Join::Builder::adoptTarget( ::capnp::Orphan< ::capnp::rpc::MessageTarget>&& value) { ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::MessageTarget> Join::Builder::disownTarget() { return ::capnp::_::PointerHelpers< ::capnp::rpc::MessageTarget>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Join::Reader::hasKeyPart() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Join::Builder::hasKeyPart() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Join::Reader::getKeyPart() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Join::Builder::getKeyPart() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Join::Builder::initKeyPart() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::capnp::rpc::MessageTarget::Which MessageTarget::Reader::which() const { return _reader.getDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::MessageTarget::Which MessageTarget::Builder::which() { return _builder.getDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline bool MessageTarget::Reader::isImportedCap() const { return which() == MessageTarget::IMPORTED_CAP; } inline bool MessageTarget::Builder::isImportedCap() { return which() == MessageTarget::IMPORTED_CAP; } inline ::uint32_t MessageTarget::Reader::getImportedCap() const { KJ_IREQUIRE((which() == MessageTarget::IMPORTED_CAP), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t MessageTarget::Builder::getImportedCap() { KJ_IREQUIRE((which() == MessageTarget::IMPORTED_CAP), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void MessageTarget::Builder::setImportedCap( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, MessageTarget::IMPORTED_CAP); _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool MessageTarget::Reader::isPromisedAnswer() const { return which() == MessageTarget::PROMISED_ANSWER; } inline bool MessageTarget::Builder::isPromisedAnswer() { return which() == MessageTarget::PROMISED_ANSWER; } inline bool MessageTarget::Reader::hasPromisedAnswer() const { if (which() != MessageTarget::PROMISED_ANSWER) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool MessageTarget::Builder::hasPromisedAnswer() { if (which() != MessageTarget::PROMISED_ANSWER) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::PromisedAnswer::Reader MessageTarget::Reader::getPromisedAnswer() const { KJ_IREQUIRE((which() == MessageTarget::PROMISED_ANSWER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::PromisedAnswer::Builder MessageTarget::Builder::getPromisedAnswer() { KJ_IREQUIRE((which() == MessageTarget::PROMISED_ANSWER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void MessageTarget::Builder::setPromisedAnswer( ::capnp::rpc::PromisedAnswer::Reader value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, MessageTarget::PROMISED_ANSWER); ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::PromisedAnswer::Builder MessageTarget::Builder::initPromisedAnswer() { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, MessageTarget::PROMISED_ANSWER); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void MessageTarget::Builder::adoptPromisedAnswer( ::capnp::Orphan< ::capnp::rpc::PromisedAnswer>&& value) { _builder.setDataField( ::capnp::bounded<2>() * ::capnp::ELEMENTS, MessageTarget::PROMISED_ANSWER); ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::PromisedAnswer> MessageTarget::Builder::disownPromisedAnswer() { KJ_IREQUIRE((which() == MessageTarget::PROMISED_ANSWER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Payload::Reader::hasContent() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Payload::Builder::hasContent() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader Payload::Reader::getContent() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Payload::Builder::getContent() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder Payload::Builder::initContent() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline bool Payload::Reader::hasCapTable() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline bool Payload::Builder::hasCapTable() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Reader Payload::Reader::getCapTable() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Builder Payload::Builder::getCapTable() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline void Payload::Builder::setCapTable( ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>::Builder Payload::Builder::initCapTable(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), size); } inline void Payload::Builder::adoptCapTable( ::capnp::Orphan< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>> Payload::Builder::disownCapTable() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::CapDescriptor, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } inline ::capnp::rpc::CapDescriptor::Which CapDescriptor::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::CapDescriptor::Which CapDescriptor::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool CapDescriptor::Reader::isNone() const { return which() == CapDescriptor::NONE; } inline bool CapDescriptor::Builder::isNone() { return which() == CapDescriptor::NONE; } inline ::capnp::Void CapDescriptor::Reader::getNone() const { KJ_IREQUIRE((which() == CapDescriptor::NONE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void CapDescriptor::Builder::getNone() { KJ_IREQUIRE((which() == CapDescriptor::NONE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void CapDescriptor::Builder::setNone( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::NONE); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool CapDescriptor::Reader::isSenderHosted() const { return which() == CapDescriptor::SENDER_HOSTED; } inline bool CapDescriptor::Builder::isSenderHosted() { return which() == CapDescriptor::SENDER_HOSTED; } inline ::uint32_t CapDescriptor::Reader::getSenderHosted() const { KJ_IREQUIRE((which() == CapDescriptor::SENDER_HOSTED), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t CapDescriptor::Builder::getSenderHosted() { KJ_IREQUIRE((which() == CapDescriptor::SENDER_HOSTED), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void CapDescriptor::Builder::setSenderHosted( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::SENDER_HOSTED); _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool CapDescriptor::Reader::isSenderPromise() const { return which() == CapDescriptor::SENDER_PROMISE; } inline bool CapDescriptor::Builder::isSenderPromise() { return which() == CapDescriptor::SENDER_PROMISE; } inline ::uint32_t CapDescriptor::Reader::getSenderPromise() const { KJ_IREQUIRE((which() == CapDescriptor::SENDER_PROMISE), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t CapDescriptor::Builder::getSenderPromise() { KJ_IREQUIRE((which() == CapDescriptor::SENDER_PROMISE), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void CapDescriptor::Builder::setSenderPromise( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::SENDER_PROMISE); _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool CapDescriptor::Reader::isReceiverHosted() const { return which() == CapDescriptor::RECEIVER_HOSTED; } inline bool CapDescriptor::Builder::isReceiverHosted() { return which() == CapDescriptor::RECEIVER_HOSTED; } inline ::uint32_t CapDescriptor::Reader::getReceiverHosted() const { KJ_IREQUIRE((which() == CapDescriptor::RECEIVER_HOSTED), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint32_t CapDescriptor::Builder::getReceiverHosted() { KJ_IREQUIRE((which() == CapDescriptor::RECEIVER_HOSTED), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void CapDescriptor::Builder::setReceiverHosted( ::uint32_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::RECEIVER_HOSTED); _builder.setDataField< ::uint32_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool CapDescriptor::Reader::isReceiverAnswer() const { return which() == CapDescriptor::RECEIVER_ANSWER; } inline bool CapDescriptor::Builder::isReceiverAnswer() { return which() == CapDescriptor::RECEIVER_ANSWER; } inline bool CapDescriptor::Reader::hasReceiverAnswer() const { if (which() != CapDescriptor::RECEIVER_ANSWER) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool CapDescriptor::Builder::hasReceiverAnswer() { if (which() != CapDescriptor::RECEIVER_ANSWER) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::PromisedAnswer::Reader CapDescriptor::Reader::getReceiverAnswer() const { KJ_IREQUIRE((which() == CapDescriptor::RECEIVER_ANSWER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::PromisedAnswer::Builder CapDescriptor::Builder::getReceiverAnswer() { KJ_IREQUIRE((which() == CapDescriptor::RECEIVER_ANSWER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CapDescriptor::Builder::setReceiverAnswer( ::capnp::rpc::PromisedAnswer::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::RECEIVER_ANSWER); ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::PromisedAnswer::Builder CapDescriptor::Builder::initReceiverAnswer() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::RECEIVER_ANSWER); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CapDescriptor::Builder::adoptReceiverAnswer( ::capnp::Orphan< ::capnp::rpc::PromisedAnswer>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::RECEIVER_ANSWER); ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::PromisedAnswer> CapDescriptor::Builder::disownReceiverAnswer() { KJ_IREQUIRE((which() == CapDescriptor::RECEIVER_ANSWER), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::PromisedAnswer>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool CapDescriptor::Reader::isThirdPartyHosted() const { return which() == CapDescriptor::THIRD_PARTY_HOSTED; } inline bool CapDescriptor::Builder::isThirdPartyHosted() { return which() == CapDescriptor::THIRD_PARTY_HOSTED; } inline bool CapDescriptor::Reader::hasThirdPartyHosted() const { if (which() != CapDescriptor::THIRD_PARTY_HOSTED) return false; return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool CapDescriptor::Builder::hasThirdPartyHosted() { if (which() != CapDescriptor::THIRD_PARTY_HOSTED) return false; return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::rpc::ThirdPartyCapDescriptor::Reader CapDescriptor::Reader::getThirdPartyHosted() const { KJ_IREQUIRE((which() == CapDescriptor::THIRD_PARTY_HOSTED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::ThirdPartyCapDescriptor>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::ThirdPartyCapDescriptor::Builder CapDescriptor::Builder::getThirdPartyHosted() { KJ_IREQUIRE((which() == CapDescriptor::THIRD_PARTY_HOSTED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::ThirdPartyCapDescriptor>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CapDescriptor::Builder::setThirdPartyHosted( ::capnp::rpc::ThirdPartyCapDescriptor::Reader value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::THIRD_PARTY_HOSTED); ::capnp::_::PointerHelpers< ::capnp::rpc::ThirdPartyCapDescriptor>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::rpc::ThirdPartyCapDescriptor::Builder CapDescriptor::Builder::initThirdPartyHosted() { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::THIRD_PARTY_HOSTED); return ::capnp::_::PointerHelpers< ::capnp::rpc::ThirdPartyCapDescriptor>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void CapDescriptor::Builder::adoptThirdPartyHosted( ::capnp::Orphan< ::capnp::rpc::ThirdPartyCapDescriptor>&& value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, CapDescriptor::THIRD_PARTY_HOSTED); ::capnp::_::PointerHelpers< ::capnp::rpc::ThirdPartyCapDescriptor>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::rpc::ThirdPartyCapDescriptor> CapDescriptor::Builder::disownThirdPartyHosted() { KJ_IREQUIRE((which() == CapDescriptor::THIRD_PARTY_HOSTED), "Must check which() before get()ing a union member."); return ::capnp::_::PointerHelpers< ::capnp::rpc::ThirdPartyCapDescriptor>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::uint8_t CapDescriptor::Reader::getAttachedFd() const { return _reader.getDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, 255u); } inline ::uint8_t CapDescriptor::Builder::getAttachedFd() { return _builder.getDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, 255u); } inline void CapDescriptor::Builder::setAttachedFd( ::uint8_t value) { _builder.setDataField< ::uint8_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value, 255u); } inline ::uint32_t PromisedAnswer::Reader::getQuestionId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t PromisedAnswer::Builder::getQuestionId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void PromisedAnswer::Builder::setQuestionId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool PromisedAnswer::Reader::hasTransform() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool PromisedAnswer::Builder::hasTransform() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Reader PromisedAnswer::Reader::getTransform() const { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Builder PromisedAnswer::Builder::getTransform() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void PromisedAnswer::Builder::setTransform( ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Reader value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>::Builder PromisedAnswer::Builder::initTransform(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void PromisedAnswer::Builder::adoptTransform( ::capnp::Orphan< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>&& value) { ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>> PromisedAnswer::Builder::disownTransform() { return ::capnp::_::PointerHelpers< ::capnp::List< ::capnp::rpc::PromisedAnswer::Op, ::capnp::Kind::STRUCT>>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::rpc::PromisedAnswer::Op::Which PromisedAnswer::Op::Reader::which() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::PromisedAnswer::Op::Which PromisedAnswer::Op::Builder::which() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool PromisedAnswer::Op::Reader::isNoop() const { return which() == PromisedAnswer::Op::NOOP; } inline bool PromisedAnswer::Op::Builder::isNoop() { return which() == PromisedAnswer::Op::NOOP; } inline ::capnp::Void PromisedAnswer::Op::Reader::getNoop() const { KJ_IREQUIRE((which() == PromisedAnswer::Op::NOOP), "Must check which() before get()ing a union member."); return _reader.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::Void PromisedAnswer::Op::Builder::getNoop() { KJ_IREQUIRE((which() == PromisedAnswer::Op::NOOP), "Must check which() before get()ing a union member."); return _builder.getDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void PromisedAnswer::Op::Builder::setNoop( ::capnp::Void value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, PromisedAnswer::Op::NOOP); _builder.setDataField< ::capnp::Void>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool PromisedAnswer::Op::Reader::isGetPointerField() const { return which() == PromisedAnswer::Op::GET_POINTER_FIELD; } inline bool PromisedAnswer::Op::Builder::isGetPointerField() { return which() == PromisedAnswer::Op::GET_POINTER_FIELD; } inline ::uint16_t PromisedAnswer::Op::Reader::getGetPointerField() const { KJ_IREQUIRE((which() == PromisedAnswer::Op::GET_POINTER_FIELD), "Must check which() before get()ing a union member."); return _reader.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint16_t PromisedAnswer::Op::Builder::getGetPointerField() { KJ_IREQUIRE((which() == PromisedAnswer::Op::GET_POINTER_FIELD), "Must check which() before get()ing a union member."); return _builder.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void PromisedAnswer::Op::Builder::setGetPointerField( ::uint16_t value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, PromisedAnswer::Op::GET_POINTER_FIELD); _builder.setDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline bool ThirdPartyCapDescriptor::Reader::hasId() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool ThirdPartyCapDescriptor::Builder::hasId() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader ThirdPartyCapDescriptor::Reader::getId() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder ThirdPartyCapDescriptor::Builder::getId() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder ThirdPartyCapDescriptor::Builder::initId() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } inline ::uint32_t ThirdPartyCapDescriptor::Reader::getVineId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t ThirdPartyCapDescriptor::Builder::getVineId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void ThirdPartyCapDescriptor::Builder::setVineId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool Exception::Reader::hasReason() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool Exception::Builder::hasReason() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::Text::Reader Exception::Reader::getReason() const { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::Text::Builder Exception::Builder::getReason() { return ::capnp::_::PointerHelpers< ::capnp::Text>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline void Exception::Builder::setReason( ::capnp::Text::Reader value) { ::capnp::_::PointerHelpers< ::capnp::Text>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } inline ::capnp::Text::Builder Exception::Builder::initReason(unsigned int size) { return ::capnp::_::PointerHelpers< ::capnp::Text>::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } inline void Exception::Builder::adoptReason( ::capnp::Orphan< ::capnp::Text>&& value) { ::capnp::_::PointerHelpers< ::capnp::Text>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } inline ::capnp::Orphan< ::capnp::Text> Exception::Builder::disownReason() { return ::capnp::_::PointerHelpers< ::capnp::Text>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline bool Exception::Reader::getObsoleteIsCallersFault() const { return _reader.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline bool Exception::Builder::getObsoleteIsCallersFault() { return _builder.getDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void Exception::Builder::setObsoleteIsCallersFault(bool value) { _builder.setDataField( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint16_t Exception::Reader::getObsoleteDurability() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline ::uint16_t Exception::Builder::getObsoleteDurability() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS); } inline void Exception::Builder::setObsoleteDurability( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<1>() * ::capnp::ELEMENTS, value); } inline ::capnp::rpc::Exception::Type Exception::Reader::getType() const { return _reader.getDataField< ::capnp::rpc::Exception::Type>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::Exception::Type Exception::Builder::getType() { return _builder.getDataField< ::capnp::rpc::Exception::Type>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void Exception::Builder::setType( ::capnp::rpc::Exception::Type value) { _builder.setDataField< ::capnp::rpc::Exception::Type>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/rpc-twoparty.capnp.h0000644000175000017500000005123613650317744022605 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: rpc-twoparty.capnp #pragma once #include #include #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(9fd69ebc87b9719c); enum class Side_9fd69ebc87b9719c: uint16_t { SERVER, CLIENT, }; CAPNP_DECLARE_ENUM(Side, 9fd69ebc87b9719c); CAPNP_DECLARE_SCHEMA(d20b909fee733a8e); CAPNP_DECLARE_SCHEMA(b88d09a9c5f39817); CAPNP_DECLARE_SCHEMA(89f389b6fd4082c1); CAPNP_DECLARE_SCHEMA(b47f4979672cb59d); CAPNP_DECLARE_SCHEMA(95b29059097fca83); CAPNP_DECLARE_SCHEMA(9d263a3630b7ebee); } // namespace schemas } // namespace capnp namespace capnp { namespace rpc { namespace twoparty { typedef ::capnp::schemas::Side_9fd69ebc87b9719c Side; struct VatId { VatId() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(d20b909fee733a8e, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct ProvisionId { ProvisionId() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b88d09a9c5f39817, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct RecipientId { RecipientId() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(89f389b6fd4082c1, 0, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct ThirdPartyCapId { ThirdPartyCapId() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b47f4979672cb59d, 0, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct JoinKeyPart { JoinKeyPart() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(95b29059097fca83, 1, 0) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; struct JoinResult { JoinResult() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(9d263a3630b7ebee, 1, 1) #if !CAPNP_LITE static constexpr ::capnp::_::RawBrandedSchema const* brand() { return &schema->defaultBrand; } #endif // !CAPNP_LITE }; }; // ======================================================================================= class VatId::Reader { public: typedef VatId Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::capnp::rpc::twoparty::Side getSide() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class VatId::Builder { public: typedef VatId Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::capnp::rpc::twoparty::Side getSide(); inline void setSide( ::capnp::rpc::twoparty::Side value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class VatId::Pipeline { public: typedef VatId Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class ProvisionId::Reader { public: typedef ProvisionId Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getJoinId() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class ProvisionId::Builder { public: typedef ProvisionId Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getJoinId(); inline void setJoinId( ::uint32_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class ProvisionId::Pipeline { public: typedef ProvisionId Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class RecipientId::Reader { public: typedef RecipientId Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class RecipientId::Builder { public: typedef RecipientId Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class RecipientId::Pipeline { public: typedef RecipientId Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class ThirdPartyCapId::Reader { public: typedef ThirdPartyCapId Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class ThirdPartyCapId::Builder { public: typedef ThirdPartyCapId Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class ThirdPartyCapId::Pipeline { public: typedef ThirdPartyCapId Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class JoinKeyPart::Reader { public: typedef JoinKeyPart Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getJoinId() const; inline ::uint16_t getPartCount() const; inline ::uint16_t getPartNum() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class JoinKeyPart::Builder { public: typedef JoinKeyPart Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getJoinId(); inline void setJoinId( ::uint32_t value); inline ::uint16_t getPartCount(); inline void setPartCount( ::uint16_t value); inline ::uint16_t getPartNum(); inline void setPartNum( ::uint16_t value); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class JoinKeyPart::Pipeline { public: typedef JoinKeyPart Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE class JoinResult::Reader { public: typedef JoinResult Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE inline ::uint32_t getJoinId() const; inline bool getSucceeded() const; inline bool hasCap() const; inline ::capnp::AnyPointer::Reader getCap() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; class JoinResult::Builder { public: typedef JoinResult Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE inline ::uint32_t getJoinId(); inline void setJoinId( ::uint32_t value); inline bool getSucceeded(); inline void setSucceeded(bool value); inline bool hasCap(); inline ::capnp::AnyPointer::Builder getCap(); inline ::capnp::AnyPointer::Builder initCap(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE class JoinResult::Pipeline { public: typedef JoinResult Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= inline ::capnp::rpc::twoparty::Side VatId::Reader::getSide() const { return _reader.getDataField< ::capnp::rpc::twoparty::Side>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::capnp::rpc::twoparty::Side VatId::Builder::getSide() { return _builder.getDataField< ::capnp::rpc::twoparty::Side>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void VatId::Builder::setSide( ::capnp::rpc::twoparty::Side value) { _builder.setDataField< ::capnp::rpc::twoparty::Side>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t ProvisionId::Reader::getJoinId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t ProvisionId::Builder::getJoinId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void ProvisionId::Builder::setJoinId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint32_t JoinKeyPart::Reader::getJoinId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t JoinKeyPart::Builder::getJoinId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void JoinKeyPart::Builder::setJoinId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline ::uint16_t JoinKeyPart::Reader::getPartCount() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline ::uint16_t JoinKeyPart::Builder::getPartCount() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS); } inline void JoinKeyPart::Builder::setPartCount( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<2>() * ::capnp::ELEMENTS, value); } inline ::uint16_t JoinKeyPart::Reader::getPartNum() const { return _reader.getDataField< ::uint16_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline ::uint16_t JoinKeyPart::Builder::getPartNum() { return _builder.getDataField< ::uint16_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS); } inline void JoinKeyPart::Builder::setPartNum( ::uint16_t value) { _builder.setDataField< ::uint16_t>( ::capnp::bounded<3>() * ::capnp::ELEMENTS, value); } inline ::uint32_t JoinResult::Reader::getJoinId() const { return _reader.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline ::uint32_t JoinResult::Builder::getJoinId() { return _builder.getDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS); } inline void JoinResult::Builder::setJoinId( ::uint32_t value) { _builder.setDataField< ::uint32_t>( ::capnp::bounded<0>() * ::capnp::ELEMENTS, value); } inline bool JoinResult::Reader::getSucceeded() const { return _reader.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS); } inline bool JoinResult::Builder::getSucceeded() { return _builder.getDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS); } inline void JoinResult::Builder::setSucceeded(bool value) { _builder.setDataField( ::capnp::bounded<32>() * ::capnp::ELEMENTS, value); } inline bool JoinResult::Reader::hasCap() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline bool JoinResult::Builder::hasCap() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } inline ::capnp::AnyPointer::Reader JoinResult::Reader::getCap() const { return ::capnp::AnyPointer::Reader(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder JoinResult::Builder::getCap() { return ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } inline ::capnp::AnyPointer::Builder JoinResult::Builder::initCap() { auto result = ::capnp::AnyPointer::Builder(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); result.clear(); return result; } } // namespace } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/persistent.capnp.h0000644000175000017500000021337713650317744022340 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: persistent.capnp #pragma once #include #include #if !CAPNP_LITE #include #endif // !CAPNP_LITE #if CAPNP_VERSION != 8000 #error "Version mismatch between generated code and library headers. You must use the same version of the Cap'n Proto compiler and library." #endif namespace capnp { namespace schemas { CAPNP_DECLARE_SCHEMA(c8cb212fcd9f5691); CAPNP_DECLARE_SCHEMA(f76fba59183073a5); CAPNP_DECLARE_SCHEMA(b76848c18c40efbf); CAPNP_DECLARE_SCHEMA(84ff286cd00a3ed4); CAPNP_DECLARE_SCHEMA(f0c2cc1d3909574d); CAPNP_DECLARE_SCHEMA(ecafa18b482da3aa); CAPNP_DECLARE_SCHEMA(f622595091cafb67); } // namespace schemas } // namespace capnp namespace capnp { template struct Persistent { Persistent() = delete; #if !CAPNP_LITE class Client; class Server; #endif // !CAPNP_LITE struct SaveParams; struct SaveResults; #if !CAPNP_LITE struct _capnpPrivate { CAPNP_DECLARE_INTERFACE_HEADER(c8cb212fcd9f5691) static const ::capnp::_::RawBrandedSchema::Scope brandScopes[]; static const ::capnp::_::RawBrandedSchema::Binding brandBindings[]; static const ::capnp::_::RawBrandedSchema::Dependency brandDependencies[]; static const ::capnp::_::RawBrandedSchema specificBrand; static constexpr ::capnp::_::RawBrandedSchema const* brand() { return ::capnp::_::ChooseBrand<_capnpPrivate, SturdyRef, Owner>::brand(); } }; #endif // !CAPNP_LITE }; template struct Persistent::SaveParams { SaveParams() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(f76fba59183073a5, 0, 1) #if !CAPNP_LITE static const ::capnp::_::RawBrandedSchema::Scope brandScopes[]; static const ::capnp::_::RawBrandedSchema::Binding brandBindings[]; static const ::capnp::_::RawBrandedSchema specificBrand; static constexpr ::capnp::_::RawBrandedSchema const* brand() { return ::capnp::_::ChooseBrand<_capnpPrivate, SturdyRef, Owner>::brand(); } #endif // !CAPNP_LITE }; }; template struct Persistent::SaveResults { SaveResults() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(b76848c18c40efbf, 0, 1) #if !CAPNP_LITE static const ::capnp::_::RawBrandedSchema::Scope brandScopes[]; static const ::capnp::_::RawBrandedSchema::Binding brandBindings[]; static const ::capnp::_::RawBrandedSchema specificBrand; static constexpr ::capnp::_::RawBrandedSchema const* brand() { return ::capnp::_::ChooseBrand<_capnpPrivate, SturdyRef, Owner>::brand(); } #endif // !CAPNP_LITE }; }; template struct RealmGateway { RealmGateway() = delete; #if !CAPNP_LITE class Client; class Server; #endif // !CAPNP_LITE struct ImportParams; struct ExportParams; #if !CAPNP_LITE struct _capnpPrivate { CAPNP_DECLARE_INTERFACE_HEADER(84ff286cd00a3ed4) static const ::capnp::_::RawBrandedSchema::Scope brandScopes[]; static const ::capnp::_::RawBrandedSchema::Binding brandBindings[]; static const ::capnp::_::RawBrandedSchema::Dependency brandDependencies[]; static const ::capnp::_::RawBrandedSchema specificBrand; static constexpr ::capnp::_::RawBrandedSchema const* brand() { return ::capnp::_::ChooseBrand<_capnpPrivate, InternalRef, ExternalRef, InternalOwner, ExternalOwner>::brand(); } }; #endif // !CAPNP_LITE }; template struct RealmGateway::ImportParams { ImportParams() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(f0c2cc1d3909574d, 0, 2) #if !CAPNP_LITE static const ::capnp::_::RawBrandedSchema::Scope brandScopes[]; static const ::capnp::_::RawBrandedSchema::Binding brandBindings[]; static const ::capnp::_::RawBrandedSchema::Dependency brandDependencies[]; static const ::capnp::_::RawBrandedSchema specificBrand; static constexpr ::capnp::_::RawBrandedSchema const* brand() { return ::capnp::_::ChooseBrand<_capnpPrivate, InternalRef, ExternalRef, InternalOwner, ExternalOwner>::brand(); } #endif // !CAPNP_LITE }; }; template struct RealmGateway::ExportParams { ExportParams() = delete; class Reader; class Builder; class Pipeline; struct _capnpPrivate { CAPNP_DECLARE_STRUCT_HEADER(ecafa18b482da3aa, 0, 2) #if !CAPNP_LITE static const ::capnp::_::RawBrandedSchema::Scope brandScopes[]; static const ::capnp::_::RawBrandedSchema::Binding brandBindings[]; static const ::capnp::_::RawBrandedSchema::Dependency brandDependencies[]; static const ::capnp::_::RawBrandedSchema specificBrand; static constexpr ::capnp::_::RawBrandedSchema const* brand() { return ::capnp::_::ChooseBrand<_capnpPrivate, InternalRef, ExternalRef, InternalOwner, ExternalOwner>::brand(); } #endif // !CAPNP_LITE }; }; // ======================================================================================= #if !CAPNP_LITE template class Persistent::Client : public virtual ::capnp::Capability::Client { public: typedef Persistent Calls; typedef Persistent Reads; Client(decltype(nullptr)); explicit Client(::kj::Own< ::capnp::ClientHook>&& hook); template ()>> Client(::kj::Own<_t>&& server); template ()>> Client(::kj::Promise<_t>&& promise); Client(::kj::Exception&& exception); Client(Client&) = default; Client(Client&&) = default; Client& operator=(Client& other); Client& operator=(Client&& other); template typename Persistent::Client asGeneric() { return castAs>(); } CAPNP_AUTO_IF_MSVC(::capnp::Request::SaveParams, typename ::capnp::Persistent::SaveResults>) saveRequest( ::kj::Maybe< ::capnp::MessageSize> sizeHint = nullptr); protected: Client() = default; }; template class Persistent::Server : public virtual ::capnp::Capability::Server { public: typedef Persistent Serves; ::capnp::Capability::Server::DispatchCallResult dispatchCall( uint64_t interfaceId, uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) override; protected: typedef ::capnp::CallContext::SaveParams, typename ::capnp::Persistent::SaveResults> SaveContext; virtual ::kj::Promise save(SaveContext context); inline typename ::capnp::Persistent::Client thisCap() { return ::capnp::Capability::Server::thisCap() .template castAs< ::capnp::Persistent>(); } ::capnp::Capability::Server::DispatchCallResult dispatchCallInternal( uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context); }; #endif // !CAPNP_LITE template class Persistent::SaveParams::Reader { public: typedef SaveParams Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE template typename Persistent::SaveParams::Reader asPersistentGeneric() { return typename Persistent::SaveParams::Reader(_reader); } inline bool hasSealFor() const; inline ::capnp::ReaderFor getSealFor() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; template class Persistent::SaveParams::Builder { public: typedef SaveParams Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE template typename Persistent::SaveParams::Builder asPersistentGeneric() { return typename Persistent::SaveParams::Builder(_builder); } inline bool hasSealFor(); inline ::capnp::BuilderFor getSealFor(); inline void setSealFor( ::capnp::ReaderFor value); inline ::capnp::BuilderFor initSealFor(); inline ::capnp::BuilderFor initSealFor(unsigned int size); inline void adoptSealFor(::capnp::Orphan&& value); inline ::capnp::Orphan disownSealFor(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE template class Persistent::SaveParams::Pipeline { public: typedef SaveParams Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::PipelineFor getSealFor(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE template class Persistent::SaveResults::Reader { public: typedef SaveResults Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE template typename Persistent::SaveResults::Reader asPersistentGeneric() { return typename Persistent::SaveResults::Reader(_reader); } inline bool hasSturdyRef() const; inline ::capnp::ReaderFor getSturdyRef() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; template class Persistent::SaveResults::Builder { public: typedef SaveResults Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE template typename Persistent::SaveResults::Builder asPersistentGeneric() { return typename Persistent::SaveResults::Builder(_builder); } inline bool hasSturdyRef(); inline ::capnp::BuilderFor getSturdyRef(); inline void setSturdyRef( ::capnp::ReaderFor value); inline ::capnp::BuilderFor initSturdyRef(); inline ::capnp::BuilderFor initSturdyRef(unsigned int size); inline void adoptSturdyRef(::capnp::Orphan&& value); inline ::capnp::Orphan disownSturdyRef(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE template class Persistent::SaveResults::Pipeline { public: typedef SaveResults Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline ::capnp::PipelineFor getSturdyRef(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE #if !CAPNP_LITE template class RealmGateway::Client : public virtual ::capnp::Capability::Client { public: typedef RealmGateway Calls; typedef RealmGateway Reads; Client(decltype(nullptr)); explicit Client(::kj::Own< ::capnp::ClientHook>&& hook); template ()>> Client(::kj::Own<_t>&& server); template ()>> Client(::kj::Promise<_t>&& promise); Client(::kj::Exception&& exception); Client(Client&) = default; Client(Client&&) = default; Client& operator=(Client& other); Client& operator=(Client&& other); template typename RealmGateway::Client asGeneric() { return castAs>(); } CAPNP_AUTO_IF_MSVC(::capnp::Request::ImportParams, typename ::capnp::Persistent::SaveResults>) importRequest( ::kj::Maybe< ::capnp::MessageSize> sizeHint = nullptr); CAPNP_AUTO_IF_MSVC(::capnp::Request::ExportParams, typename ::capnp::Persistent::SaveResults>) exportRequest( ::kj::Maybe< ::capnp::MessageSize> sizeHint = nullptr); protected: Client() = default; }; template class RealmGateway::Server : public virtual ::capnp::Capability::Server { public: typedef RealmGateway Serves; ::capnp::Capability::Server::DispatchCallResult dispatchCall( uint64_t interfaceId, uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) override; protected: typedef typename ::capnp::RealmGateway::ImportParams ImportParams; typedef ::capnp::CallContext::SaveResults> ImportContext; virtual ::kj::Promise import(ImportContext context); typedef typename ::capnp::RealmGateway::ExportParams ExportParams; typedef ::capnp::CallContext::SaveResults> ExportContext; virtual ::kj::Promise export_(ExportContext context); inline typename ::capnp::RealmGateway::Client thisCap() { return ::capnp::Capability::Server::thisCap() .template castAs< ::capnp::RealmGateway>(); } ::capnp::Capability::Server::DispatchCallResult dispatchCallInternal( uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context); }; #endif // !CAPNP_LITE template class RealmGateway::ImportParams::Reader { public: typedef ImportParams Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE template typename RealmGateway::ImportParams::Reader asRealmGatewayGeneric() { return typename RealmGateway::ImportParams::Reader(_reader); } inline bool hasCap() const; #if !CAPNP_LITE inline typename ::capnp::Persistent::Client getCap() const; #endif // !CAPNP_LITE inline bool hasParams() const; inline typename ::capnp::Persistent::SaveParams::Reader getParams() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; template class RealmGateway::ImportParams::Builder { public: typedef ImportParams Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE template typename RealmGateway::ImportParams::Builder asRealmGatewayGeneric() { return typename RealmGateway::ImportParams::Builder(_builder); } inline bool hasCap(); #if !CAPNP_LITE inline typename ::capnp::Persistent::Client getCap(); inline void setCap(typename ::capnp::Persistent::Client&& value); inline void setCap(typename ::capnp::Persistent::Client& value); inline void adoptCap(::capnp::Orphan< ::capnp::Persistent>&& value); inline ::capnp::Orphan< ::capnp::Persistent> disownCap(); #endif // !CAPNP_LITE inline bool hasParams(); inline typename ::capnp::Persistent::SaveParams::Builder getParams(); inline void setParams(typename ::capnp::Persistent::SaveParams::Reader value); inline typename ::capnp::Persistent::SaveParams::Builder initParams(); inline void adoptParams(::capnp::Orphan::SaveParams>&& value); inline ::capnp::Orphan::SaveParams> disownParams(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE template class RealmGateway::ImportParams::Pipeline { public: typedef ImportParams Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline typename ::capnp::Persistent::Client getCap(); inline typename ::capnp::Persistent::SaveParams::Pipeline getParams(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE template class RealmGateway::ExportParams::Reader { public: typedef ExportParams Reads; Reader() = default; inline explicit Reader(::capnp::_::StructReader base): _reader(base) {} inline ::capnp::MessageSize totalSize() const { return _reader.totalSize().asPublic(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return ::capnp::_::structString(_reader, *_capnpPrivate::brand()); } #endif // !CAPNP_LITE template typename RealmGateway::ExportParams::Reader asRealmGatewayGeneric() { return typename RealmGateway::ExportParams::Reader(_reader); } inline bool hasCap() const; #if !CAPNP_LITE inline typename ::capnp::Persistent::Client getCap() const; #endif // !CAPNP_LITE inline bool hasParams() const; inline typename ::capnp::Persistent::SaveParams::Reader getParams() const; private: ::capnp::_::StructReader _reader; template friend struct ::capnp::ToDynamic_; template friend struct ::capnp::_::PointerHelpers; template friend struct ::capnp::List; friend class ::capnp::MessageBuilder; friend class ::capnp::Orphanage; }; template class RealmGateway::ExportParams::Builder { public: typedef ExportParams Builds; Builder() = delete; // Deleted to discourage incorrect usage. // You can explicitly initialize to nullptr instead. inline Builder(decltype(nullptr)) {} inline explicit Builder(::capnp::_::StructBuilder base): _builder(base) {} inline operator Reader() const { return Reader(_builder.asReader()); } inline Reader asReader() const { return *this; } inline ::capnp::MessageSize totalSize() const { return asReader().totalSize(); } #if !CAPNP_LITE inline ::kj::StringTree toString() const { return asReader().toString(); } #endif // !CAPNP_LITE template typename RealmGateway::ExportParams::Builder asRealmGatewayGeneric() { return typename RealmGateway::ExportParams::Builder(_builder); } inline bool hasCap(); #if !CAPNP_LITE inline typename ::capnp::Persistent::Client getCap(); inline void setCap(typename ::capnp::Persistent::Client&& value); inline void setCap(typename ::capnp::Persistent::Client& value); inline void adoptCap(::capnp::Orphan< ::capnp::Persistent>&& value); inline ::capnp::Orphan< ::capnp::Persistent> disownCap(); #endif // !CAPNP_LITE inline bool hasParams(); inline typename ::capnp::Persistent::SaveParams::Builder getParams(); inline void setParams(typename ::capnp::Persistent::SaveParams::Reader value); inline typename ::capnp::Persistent::SaveParams::Builder initParams(); inline void adoptParams(::capnp::Orphan::SaveParams>&& value); inline ::capnp::Orphan::SaveParams> disownParams(); private: ::capnp::_::StructBuilder _builder; template friend struct ::capnp::ToDynamic_; friend class ::capnp::Orphanage; template friend struct ::capnp::_::PointerHelpers; }; #if !CAPNP_LITE template class RealmGateway::ExportParams::Pipeline { public: typedef ExportParams Pipelines; inline Pipeline(decltype(nullptr)): _typeless(nullptr) {} inline explicit Pipeline(::capnp::AnyPointer::Pipeline&& typeless) : _typeless(kj::mv(typeless)) {} inline typename ::capnp::Persistent::Client getCap(); inline typename ::capnp::Persistent::SaveParams::Pipeline getParams(); private: ::capnp::AnyPointer::Pipeline _typeless; friend class ::capnp::PipelineHook; template friend struct ::capnp::ToDynamic_; }; #endif // !CAPNP_LITE // ======================================================================================= #if !CAPNP_LITE template inline Persistent::Client::Client(decltype(nullptr)) : ::capnp::Capability::Client(nullptr) {} template inline Persistent::Client::Client( ::kj::Own< ::capnp::ClientHook>&& hook) : ::capnp::Capability::Client(::kj::mv(hook)) {} template template inline Persistent::Client::Client(::kj::Own<_t>&& server) : ::capnp::Capability::Client(::kj::mv(server)) {} template template inline Persistent::Client::Client(::kj::Promise<_t>&& promise) : ::capnp::Capability::Client(::kj::mv(promise)) {} template inline Persistent::Client::Client(::kj::Exception&& exception) : ::capnp::Capability::Client(::kj::mv(exception)) {} template inline typename ::capnp::Persistent::Client& Persistent::Client::operator=(Client& other) { ::capnp::Capability::Client::operator=(other); return *this; } template inline typename ::capnp::Persistent::Client& Persistent::Client::operator=(Client&& other) { ::capnp::Capability::Client::operator=(kj::mv(other)); return *this; } #endif // !CAPNP_LITE template inline bool Persistent::SaveParams::Reader::hasSealFor() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } template inline bool Persistent::SaveParams::Builder::hasSealFor() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } template inline ::capnp::ReaderFor Persistent::SaveParams::Reader::getSealFor() const { return ::capnp::_::PointerHelpers::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline ::capnp::BuilderFor Persistent::SaveParams::Builder::getSealFor() { return ::capnp::_::PointerHelpers::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE template inline ::capnp::PipelineFor Persistent::SaveParams::Pipeline::getSealFor() { return ::capnp::PipelineFor(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE template inline void Persistent::SaveParams::Builder::setSealFor( ::capnp::ReaderFor value) { ::capnp::_::PointerHelpers::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } template inline ::capnp::BuilderFor Persistent::SaveParams::Builder::initSealFor() { return ::capnp::_::PointerHelpers::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline ::capnp::BuilderFor Persistent::SaveParams::Builder::initSealFor(unsigned int size) { return ::capnp::_::PointerHelpers::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } template inline void Persistent::SaveParams::Builder::adoptSealFor( ::capnp::Orphan&& value) { ::capnp::_::PointerHelpers::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } template inline ::capnp::Orphan Persistent::SaveParams::Builder::disownSealFor() { return ::capnp::_::PointerHelpers::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } // Persistent::SaveParams template constexpr uint16_t Persistent::SaveParams::_capnpPrivate::dataWordSize; template constexpr uint16_t Persistent::SaveParams::_capnpPrivate::pointerCount; #if !CAPNP_LITE template constexpr ::capnp::Kind Persistent::SaveParams::_capnpPrivate::kind; template constexpr ::capnp::_::RawSchema const* Persistent::SaveParams::_capnpPrivate::schema; template const ::capnp::_::RawBrandedSchema::Scope Persistent::SaveParams::_capnpPrivate::brandScopes[] = { { 0xc8cb212fcd9f5691, brandBindings + 0, 2, false}, }; template const ::capnp::_::RawBrandedSchema::Binding Persistent::SaveParams::_capnpPrivate::brandBindings[] = { ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), }; template const ::capnp::_::RawBrandedSchema Persistent::SaveParams::_capnpPrivate::specificBrand = { &::capnp::schemas::s_f76fba59183073a5, brandScopes, nullptr, 1, 0, nullptr }; #endif // !CAPNP_LITE template inline bool Persistent::SaveResults::Reader::hasSturdyRef() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } template inline bool Persistent::SaveResults::Builder::hasSturdyRef() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } template inline ::capnp::ReaderFor Persistent::SaveResults::Reader::getSturdyRef() const { return ::capnp::_::PointerHelpers::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline ::capnp::BuilderFor Persistent::SaveResults::Builder::getSturdyRef() { return ::capnp::_::PointerHelpers::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #if !CAPNP_LITE template inline ::capnp::PipelineFor Persistent::SaveResults::Pipeline::getSturdyRef() { return ::capnp::PipelineFor(_typeless.getPointerField(0)); } #endif // !CAPNP_LITE template inline void Persistent::SaveResults::Builder::setSturdyRef( ::capnp::ReaderFor value) { ::capnp::_::PointerHelpers::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), value); } template inline ::capnp::BuilderFor Persistent::SaveResults::Builder::initSturdyRef() { return ::capnp::_::PointerHelpers::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline ::capnp::BuilderFor Persistent::SaveResults::Builder::initSturdyRef(unsigned int size) { return ::capnp::_::PointerHelpers::init(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), size); } template inline void Persistent::SaveResults::Builder::adoptSturdyRef( ::capnp::Orphan&& value) { ::capnp::_::PointerHelpers::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } template inline ::capnp::Orphan Persistent::SaveResults::Builder::disownSturdyRef() { return ::capnp::_::PointerHelpers::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } // Persistent::SaveResults template constexpr uint16_t Persistent::SaveResults::_capnpPrivate::dataWordSize; template constexpr uint16_t Persistent::SaveResults::_capnpPrivate::pointerCount; #if !CAPNP_LITE template constexpr ::capnp::Kind Persistent::SaveResults::_capnpPrivate::kind; template constexpr ::capnp::_::RawSchema const* Persistent::SaveResults::_capnpPrivate::schema; template const ::capnp::_::RawBrandedSchema::Scope Persistent::SaveResults::_capnpPrivate::brandScopes[] = { { 0xc8cb212fcd9f5691, brandBindings + 0, 2, false}, }; template const ::capnp::_::RawBrandedSchema::Binding Persistent::SaveResults::_capnpPrivate::brandBindings[] = { ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), }; template const ::capnp::_::RawBrandedSchema Persistent::SaveResults::_capnpPrivate::specificBrand = { &::capnp::schemas::s_b76848c18c40efbf, brandScopes, nullptr, 1, 0, nullptr }; #endif // !CAPNP_LITE #if !CAPNP_LITE template CAPNP_AUTO_IF_MSVC(::capnp::Request::SaveParams, typename ::capnp::Persistent::SaveResults>) Persistent::Client::saveRequest(::kj::Maybe< ::capnp::MessageSize> sizeHint) { return newCall::SaveParams, typename ::capnp::Persistent::SaveResults>( 0xc8cb212fcd9f5691ull, 0, sizeHint); } template ::kj::Promise Persistent::Server::save(SaveContext) { return ::capnp::Capability::Server::internalUnimplemented( "capnp/persistent.capnp:Persistent", "save", 0xc8cb212fcd9f5691ull, 0); } template ::capnp::Capability::Server::DispatchCallResult Persistent::Server::dispatchCall( uint64_t interfaceId, uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) { switch (interfaceId) { case 0xc8cb212fcd9f5691ull: return dispatchCallInternal(methodId, context); default: return internalUnimplemented("capnp/persistent.capnp:Persistent", interfaceId); } } template ::capnp::Capability::Server::DispatchCallResult Persistent::Server::dispatchCallInternal( uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) { switch (methodId) { case 0: return { save(::capnp::Capability::Server::internalGetTypedContext< typename ::capnp::Persistent::SaveParams, typename ::capnp::Persistent::SaveResults>(context)), false }; default: (void)context; return ::capnp::Capability::Server::internalUnimplemented( "capnp/persistent.capnp:Persistent", 0xc8cb212fcd9f5691ull, methodId); } } #endif // !CAPNP_LITE // Persistent #if !CAPNP_LITE template constexpr ::capnp::Kind Persistent::_capnpPrivate::kind; template constexpr ::capnp::_::RawSchema const* Persistent::_capnpPrivate::schema; template const ::capnp::_::RawBrandedSchema::Scope Persistent::_capnpPrivate::brandScopes[] = { { 0xc8cb212fcd9f5691, brandBindings + 0, 2, false}, }; template const ::capnp::_::RawBrandedSchema::Binding Persistent::_capnpPrivate::brandBindings[] = { ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), }; template const ::capnp::_::RawBrandedSchema::Dependency Persistent::_capnpPrivate::brandDependencies[] = { { 33554432, ::capnp::Persistent::SaveParams::_capnpPrivate::brand() }, { 50331648, ::capnp::Persistent::SaveResults::_capnpPrivate::brand() }, }; template const ::capnp::_::RawBrandedSchema Persistent::_capnpPrivate::specificBrand = { &::capnp::schemas::s_c8cb212fcd9f5691, brandScopes, brandDependencies, 1, 2, nullptr }; #endif // !CAPNP_LITE #if !CAPNP_LITE template inline RealmGateway::Client::Client(decltype(nullptr)) : ::capnp::Capability::Client(nullptr) {} template inline RealmGateway::Client::Client( ::kj::Own< ::capnp::ClientHook>&& hook) : ::capnp::Capability::Client(::kj::mv(hook)) {} template template inline RealmGateway::Client::Client(::kj::Own<_t>&& server) : ::capnp::Capability::Client(::kj::mv(server)) {} template template inline RealmGateway::Client::Client(::kj::Promise<_t>&& promise) : ::capnp::Capability::Client(::kj::mv(promise)) {} template inline RealmGateway::Client::Client(::kj::Exception&& exception) : ::capnp::Capability::Client(::kj::mv(exception)) {} template inline typename ::capnp::RealmGateway::Client& RealmGateway::Client::operator=(Client& other) { ::capnp::Capability::Client::operator=(other); return *this; } template inline typename ::capnp::RealmGateway::Client& RealmGateway::Client::operator=(Client&& other) { ::capnp::Capability::Client::operator=(kj::mv(other)); return *this; } #endif // !CAPNP_LITE template inline bool RealmGateway::ImportParams::Reader::hasCap() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } template inline bool RealmGateway::ImportParams::Builder::hasCap() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } #if !CAPNP_LITE template inline typename ::capnp::Persistent::Client RealmGateway::ImportParams::Reader::getCap() const { return ::capnp::_::PointerHelpers< ::capnp::Persistent>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline typename ::capnp::Persistent::Client RealmGateway::ImportParams::Builder::getCap() { return ::capnp::_::PointerHelpers< ::capnp::Persistent>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline typename ::capnp::Persistent::Client RealmGateway::ImportParams::Pipeline::getCap() { return typename ::capnp::Persistent::Client(_typeless.getPointerField(0).asCap()); } template inline void RealmGateway::ImportParams::Builder::setCap(typename ::capnp::Persistent::Client&& cap) { ::capnp::_::PointerHelpers< ::capnp::Persistent>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(cap)); } template inline void RealmGateway::ImportParams::Builder::setCap(typename ::capnp::Persistent::Client& cap) { ::capnp::_::PointerHelpers< ::capnp::Persistent>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), cap); } template inline void RealmGateway::ImportParams::Builder::adoptCap( ::capnp::Orphan< ::capnp::Persistent>&& value) { ::capnp::_::PointerHelpers< ::capnp::Persistent>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } template inline ::capnp::Orphan< ::capnp::Persistent> RealmGateway::ImportParams::Builder::disownCap() { return ::capnp::_::PointerHelpers< ::capnp::Persistent>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #endif // !CAPNP_LITE template inline bool RealmGateway::ImportParams::Reader::hasParams() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } template inline bool RealmGateway::ImportParams::Builder::hasParams() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } template inline typename ::capnp::Persistent::SaveParams::Reader RealmGateway::ImportParams::Reader::getParams() const { return ::capnp::_::PointerHelpers::SaveParams>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } template inline typename ::capnp::Persistent::SaveParams::Builder RealmGateway::ImportParams::Builder::getParams() { return ::capnp::_::PointerHelpers::SaveParams>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE template inline typename ::capnp::Persistent::SaveParams::Pipeline RealmGateway::ImportParams::Pipeline::getParams() { return typename ::capnp::Persistent::SaveParams::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE template inline void RealmGateway::ImportParams::Builder::setParams(typename ::capnp::Persistent::SaveParams::Reader value) { ::capnp::_::PointerHelpers::SaveParams>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } template inline typename ::capnp::Persistent::SaveParams::Builder RealmGateway::ImportParams::Builder::initParams() { return ::capnp::_::PointerHelpers::SaveParams>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } template inline void RealmGateway::ImportParams::Builder::adoptParams( ::capnp::Orphan::SaveParams>&& value) { ::capnp::_::PointerHelpers::SaveParams>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } template inline ::capnp::Orphan::SaveParams> RealmGateway::ImportParams::Builder::disownParams() { return ::capnp::_::PointerHelpers::SaveParams>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } // RealmGateway::ImportParams template constexpr uint16_t RealmGateway::ImportParams::_capnpPrivate::dataWordSize; template constexpr uint16_t RealmGateway::ImportParams::_capnpPrivate::pointerCount; #if !CAPNP_LITE template constexpr ::capnp::Kind RealmGateway::ImportParams::_capnpPrivate::kind; template constexpr ::capnp::_::RawSchema const* RealmGateway::ImportParams::_capnpPrivate::schema; template const ::capnp::_::RawBrandedSchema::Scope RealmGateway::ImportParams::_capnpPrivate::brandScopes[] = { { 0x84ff286cd00a3ed4, brandBindings + 0, 4, false}, }; template const ::capnp::_::RawBrandedSchema::Binding RealmGateway::ImportParams::_capnpPrivate::brandBindings[] = { ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), }; template const ::capnp::_::RawBrandedSchema::Dependency RealmGateway::ImportParams::_capnpPrivate::brandDependencies[] = { { 16777216, ::capnp::Persistent::_capnpPrivate::brand() }, { 16777217, ::capnp::Persistent::SaveParams::_capnpPrivate::brand() }, }; template const ::capnp::_::RawBrandedSchema RealmGateway::ImportParams::_capnpPrivate::specificBrand = { &::capnp::schemas::s_f0c2cc1d3909574d, brandScopes, brandDependencies, 1, 2, nullptr }; #endif // !CAPNP_LITE template inline bool RealmGateway::ExportParams::Reader::hasCap() const { return !_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } template inline bool RealmGateway::ExportParams::Builder::hasCap() { return !_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS).isNull(); } #if !CAPNP_LITE template inline typename ::capnp::Persistent::Client RealmGateway::ExportParams::Reader::getCap() const { return ::capnp::_::PointerHelpers< ::capnp::Persistent>::get(_reader.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline typename ::capnp::Persistent::Client RealmGateway::ExportParams::Builder::getCap() { return ::capnp::_::PointerHelpers< ::capnp::Persistent>::get(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } template inline typename ::capnp::Persistent::Client RealmGateway::ExportParams::Pipeline::getCap() { return typename ::capnp::Persistent::Client(_typeless.getPointerField(0).asCap()); } template inline void RealmGateway::ExportParams::Builder::setCap(typename ::capnp::Persistent::Client&& cap) { ::capnp::_::PointerHelpers< ::capnp::Persistent>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(cap)); } template inline void RealmGateway::ExportParams::Builder::setCap(typename ::capnp::Persistent::Client& cap) { ::capnp::_::PointerHelpers< ::capnp::Persistent>::set(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), cap); } template inline void RealmGateway::ExportParams::Builder::adoptCap( ::capnp::Orphan< ::capnp::Persistent>&& value) { ::capnp::_::PointerHelpers< ::capnp::Persistent>::adopt(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS), kj::mv(value)); } template inline ::capnp::Orphan< ::capnp::Persistent> RealmGateway::ExportParams::Builder::disownCap() { return ::capnp::_::PointerHelpers< ::capnp::Persistent>::disown(_builder.getPointerField( ::capnp::bounded<0>() * ::capnp::POINTERS)); } #endif // !CAPNP_LITE template inline bool RealmGateway::ExportParams::Reader::hasParams() const { return !_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } template inline bool RealmGateway::ExportParams::Builder::hasParams() { return !_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS).isNull(); } template inline typename ::capnp::Persistent::SaveParams::Reader RealmGateway::ExportParams::Reader::getParams() const { return ::capnp::_::PointerHelpers::SaveParams>::get(_reader.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } template inline typename ::capnp::Persistent::SaveParams::Builder RealmGateway::ExportParams::Builder::getParams() { return ::capnp::_::PointerHelpers::SaveParams>::get(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } #if !CAPNP_LITE template inline typename ::capnp::Persistent::SaveParams::Pipeline RealmGateway::ExportParams::Pipeline::getParams() { return typename ::capnp::Persistent::SaveParams::Pipeline(_typeless.getPointerField(1)); } #endif // !CAPNP_LITE template inline void RealmGateway::ExportParams::Builder::setParams(typename ::capnp::Persistent::SaveParams::Reader value) { ::capnp::_::PointerHelpers::SaveParams>::set(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), value); } template inline typename ::capnp::Persistent::SaveParams::Builder RealmGateway::ExportParams::Builder::initParams() { return ::capnp::_::PointerHelpers::SaveParams>::init(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } template inline void RealmGateway::ExportParams::Builder::adoptParams( ::capnp::Orphan::SaveParams>&& value) { ::capnp::_::PointerHelpers::SaveParams>::adopt(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS), kj::mv(value)); } template inline ::capnp::Orphan::SaveParams> RealmGateway::ExportParams::Builder::disownParams() { return ::capnp::_::PointerHelpers::SaveParams>::disown(_builder.getPointerField( ::capnp::bounded<1>() * ::capnp::POINTERS)); } // RealmGateway::ExportParams template constexpr uint16_t RealmGateway::ExportParams::_capnpPrivate::dataWordSize; template constexpr uint16_t RealmGateway::ExportParams::_capnpPrivate::pointerCount; #if !CAPNP_LITE template constexpr ::capnp::Kind RealmGateway::ExportParams::_capnpPrivate::kind; template constexpr ::capnp::_::RawSchema const* RealmGateway::ExportParams::_capnpPrivate::schema; template const ::capnp::_::RawBrandedSchema::Scope RealmGateway::ExportParams::_capnpPrivate::brandScopes[] = { { 0x84ff286cd00a3ed4, brandBindings + 0, 4, false}, }; template const ::capnp::_::RawBrandedSchema::Binding RealmGateway::ExportParams::_capnpPrivate::brandBindings[] = { ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), }; template const ::capnp::_::RawBrandedSchema::Dependency RealmGateway::ExportParams::_capnpPrivate::brandDependencies[] = { { 16777216, ::capnp::Persistent::_capnpPrivate::brand() }, { 16777217, ::capnp::Persistent::SaveParams::_capnpPrivate::brand() }, }; template const ::capnp::_::RawBrandedSchema RealmGateway::ExportParams::_capnpPrivate::specificBrand = { &::capnp::schemas::s_ecafa18b482da3aa, brandScopes, brandDependencies, 1, 2, nullptr }; #endif // !CAPNP_LITE #if !CAPNP_LITE template CAPNP_AUTO_IF_MSVC(::capnp::Request::ImportParams, typename ::capnp::Persistent::SaveResults>) RealmGateway::Client::importRequest(::kj::Maybe< ::capnp::MessageSize> sizeHint) { return newCall::ImportParams, typename ::capnp::Persistent::SaveResults>( 0x84ff286cd00a3ed4ull, 0, sizeHint); } template ::kj::Promise RealmGateway::Server::import(ImportContext) { return ::capnp::Capability::Server::internalUnimplemented( "capnp/persistent.capnp:RealmGateway", "import", 0x84ff286cd00a3ed4ull, 0); } template CAPNP_AUTO_IF_MSVC(::capnp::Request::ExportParams, typename ::capnp::Persistent::SaveResults>) RealmGateway::Client::exportRequest(::kj::Maybe< ::capnp::MessageSize> sizeHint) { return newCall::ExportParams, typename ::capnp::Persistent::SaveResults>( 0x84ff286cd00a3ed4ull, 1, sizeHint); } template ::kj::Promise RealmGateway::Server::export_(ExportContext) { return ::capnp::Capability::Server::internalUnimplemented( "capnp/persistent.capnp:RealmGateway", "export", 0x84ff286cd00a3ed4ull, 1); } template ::capnp::Capability::Server::DispatchCallResult RealmGateway::Server::dispatchCall( uint64_t interfaceId, uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) { switch (interfaceId) { case 0x84ff286cd00a3ed4ull: return dispatchCallInternal(methodId, context); default: return internalUnimplemented("capnp/persistent.capnp:RealmGateway", interfaceId); } } template ::capnp::Capability::Server::DispatchCallResult RealmGateway::Server::dispatchCallInternal( uint16_t methodId, ::capnp::CallContext< ::capnp::AnyPointer, ::capnp::AnyPointer> context) { switch (methodId) { case 0: return { import(::capnp::Capability::Server::internalGetTypedContext< typename ::capnp::RealmGateway::ImportParams, typename ::capnp::Persistent::SaveResults>(context)), false }; case 1: return { export_(::capnp::Capability::Server::internalGetTypedContext< typename ::capnp::RealmGateway::ExportParams, typename ::capnp::Persistent::SaveResults>(context)), false }; default: (void)context; return ::capnp::Capability::Server::internalUnimplemented( "capnp/persistent.capnp:RealmGateway", 0x84ff286cd00a3ed4ull, methodId); } } #endif // !CAPNP_LITE // RealmGateway #if !CAPNP_LITE template constexpr ::capnp::Kind RealmGateway::_capnpPrivate::kind; template constexpr ::capnp::_::RawSchema const* RealmGateway::_capnpPrivate::schema; template const ::capnp::_::RawBrandedSchema::Scope RealmGateway::_capnpPrivate::brandScopes[] = { { 0x84ff286cd00a3ed4, brandBindings + 0, 4, false}, }; template const ::capnp::_::RawBrandedSchema::Binding RealmGateway::_capnpPrivate::brandBindings[] = { ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), ::capnp::_::brandBindingFor(), }; template const ::capnp::_::RawBrandedSchema::Dependency RealmGateway::_capnpPrivate::brandDependencies[] = { { 33554432, ::capnp::RealmGateway::ImportParams::_capnpPrivate::brand() }, { 33554433, ::capnp::RealmGateway::ExportParams::_capnpPrivate::brand() }, { 50331648, ::capnp::Persistent::SaveResults::_capnpPrivate::brand() }, { 50331649, ::capnp::Persistent::SaveResults::_capnpPrivate::brand() }, }; template const ::capnp::_::RawBrandedSchema RealmGateway::_capnpPrivate::specificBrand = { &::capnp::schemas::s_84ff286cd00a3ed4, brandScopes, brandDependencies, 1, 4, nullptr }; #endif // !CAPNP_LITE } // namespace capnproto-c++-0.8.0/src/capnp/ez-rpc.h0000644000175000017500000002675413650101756020233 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "rpc.h" #include "message.h" CAPNP_BEGIN_HEADER struct sockaddr; namespace kj { class AsyncIoProvider; class LowLevelAsyncIoProvider; } namespace capnp { class EzRpcContext; class EzRpcClient { // Super-simple interface for setting up a Cap'n Proto RPC client. Example: // // # Cap'n Proto schema // interface Adder { // add @0 (left :Int32, right :Int32) -> (value :Int32); // } // // // C++ client // int main() { // capnp::EzRpcClient client("localhost:3456"); // Adder::Client adder = client.getMain(); // auto request = adder.addRequest(); // request.setLeft(12); // request.setRight(34); // auto response = request.send().wait(client.getWaitScope()); // assert(response.getValue() == 46); // return 0; // } // // // C++ server // class AdderImpl final: public Adder::Server { // public: // kj::Promise add(AddContext context) override { // auto params = context.getParams(); // context.getResults().setValue(params.getLeft() + params.getRight()); // return kj::READY_NOW; // } // }; // // int main() { // capnp::EzRpcServer server(kj::heap(), "*:3456"); // kj::NEVER_DONE.wait(server.getWaitScope()); // } // // This interface is easy, but it hides a lot of useful features available from the lower-level // classes: // - The server can only export a small set of public, singleton capabilities under well-known // string names. This is fine for transient services where no state needs to be kept between // connections, but hides the power of Cap'n Proto when it comes to long-lived resources. // - EzRpcClient/EzRpcServer automatically set up a `kj::EventLoop` and make it current for the // thread. Only one `kj::EventLoop` can exist per thread, so you cannot use these interfaces // if you wish to set up your own event loop. (However, you can safely create multiple // EzRpcClient / EzRpcServer objects in a single thread; they will make sure to make no more // than one EventLoop.) // - These classes only support simple two-party connections, not multilateral VatNetworks. // - These classes only support communication over a raw, unencrypted socket. If you want to // build on an abstract stream (perhaps one which supports encryption), you must use the // lower-level interfaces. // // Some of these restrictions will probably be lifted in future versions, but some things will // always require using the low-level interfaces directly. If you are interested in working // at a lower level, start by looking at these interfaces: // - `kj::setupAsyncIo()` in `kj/async-io.h`. // - `RpcSystem` in `capnp/rpc.h`. // - `TwoPartyVatNetwork` in `capnp/rpc-twoparty.h`. public: explicit EzRpcClient(kj::StringPtr serverAddress, uint defaultPort = 0, ReaderOptions readerOpts = ReaderOptions()); // Construct a new EzRpcClient and connect to the given address. The connection is formed in // the background -- if it fails, calls to capabilities returned by importCap() will fail with an // appropriate exception. // // `defaultPort` is the IP port number to use if `serverAddress` does not include it explicitly. // If unspecified, the port is required in `serverAddress`. // // The address is parsed by `kj::Network` in `kj/async-io.h`. See that interface for more info // on the address format, but basically it's what you'd expect. // // `readerOpts` is the ReaderOptions structure used to read each incoming message on the // connection. Setting this may be necessary if you need to receive very large individual // messages or messages. However, it is recommended that you instead think about how to change // your protocol to send large data blobs in multiple small chunks -- this is much better for // both security and performance. See `ReaderOptions` in `message.h` for more details. EzRpcClient(const struct sockaddr* serverAddress, uint addrSize, ReaderOptions readerOpts = ReaderOptions()); // Like the above constructor, but connects to an already-resolved socket address. Any address // format supported by `kj::Network` in `kj/async-io.h` is accepted. explicit EzRpcClient(int socketFd, ReaderOptions readerOpts = ReaderOptions()); // Create a client on top of an already-connected socket. // `readerOpts` acts as in the first constructor. ~EzRpcClient() noexcept(false); template typename Type::Client getMain(); Capability::Client getMain(); // Get the server's main (aka "bootstrap") interface. template typename Type::Client importCap(kj::StringPtr name) CAPNP_DEPRECATED( "Change your server to export a main interface, then use getMain() instead."); Capability::Client importCap(kj::StringPtr name) CAPNP_DEPRECATED( "Change your server to export a main interface, then use getMain() instead."); // ** DEPRECATED ** // // Ask the sever for the capability with the given name. You may specify a type to automatically // down-cast to that type. It is up to you to specify the correct expected type. // // Named interfaces are deprecated. The new preferred usage pattern is for the server to export // a "main" interface which itself has methods for getting any other interfaces. kj::WaitScope& getWaitScope(); // Get the `WaitScope` for the client's `EventLoop`, which allows you to synchronously wait on // promises. kj::AsyncIoProvider& getIoProvider(); // Get the underlying AsyncIoProvider set up by the RPC system. This is useful if you want // to do some non-RPC I/O in asynchronous fashion. kj::LowLevelAsyncIoProvider& getLowLevelIoProvider(); // Get the underlying LowLevelAsyncIoProvider set up by the RPC system. This is useful if you // want to do some non-RPC I/O in asynchronous fashion. private: struct Impl; kj::Own impl; }; class EzRpcServer { // The server counterpart to `EzRpcClient`. See `EzRpcClient` for an example. public: explicit EzRpcServer(Capability::Client mainInterface, kj::StringPtr bindAddress, uint defaultPort = 0, ReaderOptions readerOpts = ReaderOptions()); // Construct a new `EzRpcServer` that binds to the given address. An address of "*" means to // bind to all local addresses. // // `defaultPort` is the IP port number to use if `serverAddress` does not include it explicitly. // If unspecified, a port is chosen automatically, and you must call getPort() to find out what // it is. // // The address is parsed by `kj::Network` in `kj/async-io.h`. See that interface for more info // on the address format, but basically it's what you'd expect. // // The server might not begin listening immediately, especially if `bindAddress` needs to be // resolved. If you need to wait until the server is definitely up, wait on the promise returned // by `getPort()`. // // `readerOpts` is the ReaderOptions structure used to read each incoming message on the // connection. Setting this may be necessary if you need to receive very large individual // messages or messages. However, it is recommended that you instead think about how to change // your protocol to send large data blobs in multiple small chunks -- this is much better for // both security and performance. See `ReaderOptions` in `message.h` for more details. EzRpcServer(Capability::Client mainInterface, struct sockaddr* bindAddress, uint addrSize, ReaderOptions readerOpts = ReaderOptions()); // Like the above constructor, but binds to an already-resolved socket address. Any address // format supported by `kj::Network` in `kj/async-io.h` is accepted. EzRpcServer(Capability::Client mainInterface, int socketFd, uint port, ReaderOptions readerOpts = ReaderOptions()); // Create a server on top of an already-listening socket (i.e. one on which accept() may be // called). `port` is returned by `getPort()` -- it serves no other purpose. // `readerOpts` acts as in the other two above constructors. explicit EzRpcServer(kj::StringPtr bindAddress, uint defaultPort = 0, ReaderOptions readerOpts = ReaderOptions()) CAPNP_DEPRECATED("Please specify a main interface for your server."); EzRpcServer(struct sockaddr* bindAddress, uint addrSize, ReaderOptions readerOpts = ReaderOptions()) CAPNP_DEPRECATED("Please specify a main interface for your server."); EzRpcServer(int socketFd, uint port, ReaderOptions readerOpts = ReaderOptions()) CAPNP_DEPRECATED("Please specify a main interface for your server."); ~EzRpcServer() noexcept(false); void exportCap(kj::StringPtr name, Capability::Client cap); // Export a capability publicly under the given name, so that clients can import it. // // Keep in mind that you can implicitly convert `kj::Own&&` to // `Capability::Client`, so it's typical to pass something like // `kj::heap()` as the second parameter. kj::Promise getPort(); // Get the IP port number on which this server is listening. This promise won't resolve until // the server is actually listening. If the address was not an IP address (e.g. it was a Unix // domain socket) then getPort() resolves to zero. kj::WaitScope& getWaitScope(); // Get the `WaitScope` for the client's `EventLoop`, which allows you to synchronously wait on // promises. kj::AsyncIoProvider& getIoProvider(); // Get the underlying AsyncIoProvider set up by the RPC system. This is useful if you want // to do some non-RPC I/O in asynchronous fashion. kj::LowLevelAsyncIoProvider& getLowLevelIoProvider(); // Get the underlying LowLevelAsyncIoProvider set up by the RPC system. This is useful if you // want to do some non-RPC I/O in asynchronous fashion. private: struct Impl; kj::Own impl; }; // ======================================================================================= // inline implementation details template inline typename Type::Client EzRpcClient::getMain() { return getMain().castAs(); } template inline typename Type::Client EzRpcClient::importCap(kj::StringPtr name) { return importCap(name).castAs(); } } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/test-util.c++0000644000175000017500000013636113650101756021102 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "test-util.h" #include #include #include #include namespace capnp { namespace _ { // private namespace { template void genericInitTestMessage(Builder builder) { builder.setVoidField(VOID); builder.setVoidField(); // Means the same as above. builder.setBoolField(true); builder.setInt8Field(-123); builder.setInt16Field(-12345); builder.setInt32Field(-12345678); builder.setInt64Field(-123456789012345ll); builder.setUInt8Field(234u); builder.setUInt16Field(45678u); builder.setUInt32Field(3456789012u); builder.setUInt64Field(12345678901234567890ull); builder.setFloat32Field(1234.5); builder.setFloat64Field(-123e45); builder.setTextField("foo"); builder.setDataField(data("bar")); { auto subBuilder = builder.initStructField(); subBuilder.setVoidField(VOID); subBuilder.setBoolField(true); subBuilder.setInt8Field(-12); subBuilder.setInt16Field(3456); subBuilder.setInt32Field(-78901234); subBuilder.setInt64Field(56789012345678ll); subBuilder.setUInt8Field(90u); subBuilder.setUInt16Field(1234u); subBuilder.setUInt32Field(56789012u); subBuilder.setUInt64Field(345678901234567890ull); subBuilder.setFloat32Field(-1.25e-10f); subBuilder.setFloat64Field(345); subBuilder.setTextField("baz"); subBuilder.setDataField(data("qux")); { auto subSubBuilder = subBuilder.initStructField(); subSubBuilder.setTextField("nested"); subSubBuilder.initStructField().setTextField("really nested"); } subBuilder.setEnumField(TestEnum::BAZ); subBuilder.setVoidList({VOID, VOID, VOID}); subBuilder.setBoolList({false, true, false, true, true}); subBuilder.setInt8List({12, -34, -0x80, 0x7f}); subBuilder.setInt16List({1234, -5678, -0x8000, 0x7fff}); // gcc warns on -0x800... and the only work-around I could find was to do -0x7ff...-1. subBuilder.setInt32List({12345678, -90123456, -0x7fffffff - 1, 0x7fffffff}); subBuilder.setInt64List({123456789012345ll, -678901234567890ll, -0x7fffffffffffffffll-1, 0x7fffffffffffffffll}); subBuilder.setUInt8List({12u, 34u, 0u, 0xffu}); subBuilder.setUInt16List({1234u, 5678u, 0u, 0xffffu}); subBuilder.setUInt32List({12345678u, 90123456u, 0u, 0xffffffffu}); subBuilder.setUInt64List({123456789012345ull, 678901234567890ull, 0ull, 0xffffffffffffffffull}); subBuilder.setFloat32List({0, 1234567, 1e37f, -1e37f, 1e-37f, -1e-37f}); subBuilder.setFloat64List({0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306}); subBuilder.setTextList({"quux", "corge", "grault"}); subBuilder.setDataList({data("garply"), data("waldo"), data("fred")}); { auto listBuilder = subBuilder.initStructList(3); listBuilder[0].setTextField("x structlist 1"); listBuilder[1].setTextField("x structlist 2"); listBuilder[2].setTextField("x structlist 3"); } subBuilder.setEnumList({TestEnum::QUX, TestEnum::BAR, TestEnum::GRAULT}); } builder.setEnumField(TestEnum::CORGE); builder.initVoidList(6); builder.setBoolList({true, false, false, true}); builder.setInt8List({111, -111}); builder.setInt16List({11111, -11111}); builder.setInt32List({111111111, -111111111}); builder.setInt64List({1111111111111111111ll, -1111111111111111111ll}); builder.setUInt8List({111u, 222u}); builder.setUInt16List({33333u, 44444u}); builder.setUInt32List({3333333333u}); builder.setUInt64List({11111111111111111111ull}); builder.setFloat32List({5555.5, kj::inf(), -kj::inf(), kj::nan()}); builder.setFloat64List({7777.75, kj::inf(), -kj::inf(), kj::nan()}); builder.setTextList({"plugh", "xyzzy", "thud"}); builder.setDataList({data("oops"), data("exhausted"), data("rfc3092")}); { auto listBuilder = builder.initStructList(3); listBuilder[0].setTextField("structlist 1"); listBuilder[1].setTextField("structlist 2"); listBuilder[2].setTextField("structlist 3"); } builder.setEnumList({TestEnum::FOO, TestEnum::GARPLY}); } #if !CAPNP_LITE void dynamicInitTestMessage(DynamicStruct::Builder builder) { builder.set("voidField", VOID); builder.set("boolField", true); builder.set("int8Field", -123); builder.set("int16Field", -12345); builder.set("int32Field", -12345678); builder.set("int64Field", -123456789012345ll); builder.set("uInt8Field", 234u); builder.set("uInt16Field", 45678u); builder.set("uInt32Field", 3456789012u); builder.set("uInt64Field", 12345678901234567890ull); builder.set("float32Field", 1234.5); builder.set("float64Field", -123e45); builder.set("textField", "foo"); builder.set("dataField", data("bar")); { auto subBuilder = builder.init("structField").as(); subBuilder.set("voidField", VOID); subBuilder.set("boolField", true); subBuilder.set("int8Field", -12); subBuilder.set("int16Field", 3456); subBuilder.set("int32Field", -78901234); subBuilder.set("int64Field", 56789012345678ll); subBuilder.set("uInt8Field", 90u); subBuilder.set("uInt16Field", 1234u); subBuilder.set("uInt32Field", 56789012u); subBuilder.set("uInt64Field", 345678901234567890ull); subBuilder.set("float32Field", -1.25e-10); subBuilder.set("float64Field", 345); subBuilder.set("textField", "baz"); subBuilder.set("dataField", data("qux")); { auto subSubBuilder = subBuilder.init("structField").as(); subSubBuilder.set("textField", "nested"); subSubBuilder.init("structField").as().set("textField", "really nested"); } subBuilder.set("enumField", "baz"); subBuilder.set("voidList", {VOID, VOID, VOID}); subBuilder.set("boolList", {false, true, false, true, true}); subBuilder.set("int8List", {12, -34, -0x80, 0x7f}); subBuilder.set("int16List", {1234, -5678, -0x8000, 0x7fff}); // gcc warns on -0x800... and the only work-around I could find was to do -0x7ff...-1. subBuilder.set("int32List", {12345678, -90123456, -0x7fffffff - 1, 0x7fffffff}); subBuilder.set("int64List", {123456789012345ll, -678901234567890ll, -0x7fffffffffffffffll-1, 0x7fffffffffffffffll}); subBuilder.set("uInt8List", {12u, 34u, 0u, 0xffu}); subBuilder.set("uInt16List", {1234u, 5678u, 0u, 0xffffu}); subBuilder.set("uInt32List", {12345678u, 90123456u, 0u, 0xffffffffu}); subBuilder.set("uInt64List", {123456789012345ull, 678901234567890ull, 0ull, 0xffffffffffffffffull}); subBuilder.set("float32List", {0, 1234567, 1e37, -1e37, 1e-37, -1e-37}); subBuilder.set("float64List", {0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306}); subBuilder.set("textList", {"quux", "corge", "grault"}); subBuilder.set("dataList", {data("garply"), data("waldo"), data("fred")}); { auto listBuilder = subBuilder.init("structList", 3).as(); listBuilder[0].as().set("textField", "x structlist 1"); listBuilder[1].as().set("textField", "x structlist 2"); listBuilder[2].as().set("textField", "x structlist 3"); } subBuilder.set("enumList", {"qux", "bar", "grault"}); } builder.set("enumField", "corge"); builder.init("voidList", 6); builder.set("boolList", {true, false, false, true}); builder.set("int8List", {111, -111}); builder.set("int16List", {11111, -11111}); builder.set("int32List", {111111111, -111111111}); builder.set("int64List", {1111111111111111111ll, -1111111111111111111ll}); builder.set("uInt8List", {111u, 222u}); builder.set("uInt16List", {33333u, 44444u}); builder.set("uInt32List", {3333333333u}); builder.set("uInt64List", {11111111111111111111ull}); builder.set("float32List", {5555.5, kj::inf(), -kj::inf(), kj::nan()}); builder.set("float64List", {7777.75, kj::inf(), -kj::inf(), kj::nan()}); builder.set("textList", {"plugh", "xyzzy", "thud"}); builder.set("dataList", {data("oops"), data("exhausted"), data("rfc3092")}); { auto listBuilder = builder.init("structList", 3).as(); listBuilder[0].as().set("textField", "structlist 1"); listBuilder[1].as().set("textField", "structlist 2"); listBuilder[2].as().set("textField", "structlist 3"); } builder.set("enumList", {"foo", "garply"}); } #endif // !CAPNP_LITE inline bool isNaN(float f) { return f != f; } inline bool isNaN(double f) { return f != f; } template void genericCheckTestMessage(Reader reader) { EXPECT_EQ(VOID, reader.getVoidField()); EXPECT_EQ(true, reader.getBoolField()); EXPECT_EQ(-123, reader.getInt8Field()); EXPECT_EQ(-12345, reader.getInt16Field()); EXPECT_EQ(-12345678, reader.getInt32Field()); EXPECT_EQ(-123456789012345ll, reader.getInt64Field()); EXPECT_EQ(234u, reader.getUInt8Field()); EXPECT_EQ(45678u, reader.getUInt16Field()); EXPECT_EQ(3456789012u, reader.getUInt32Field()); EXPECT_EQ(12345678901234567890ull, reader.getUInt64Field()); EXPECT_FLOAT_EQ(1234.5f, reader.getFloat32Field()); EXPECT_DOUBLE_EQ(-123e45, reader.getFloat64Field()); EXPECT_EQ("foo", reader.getTextField()); EXPECT_EQ(data("bar"), reader.getDataField()); { auto subReader = reader.getStructField(); EXPECT_EQ(VOID, subReader.getVoidField()); EXPECT_EQ(true, subReader.getBoolField()); EXPECT_EQ(-12, subReader.getInt8Field()); EXPECT_EQ(3456, subReader.getInt16Field()); EXPECT_EQ(-78901234, subReader.getInt32Field()); EXPECT_EQ(56789012345678ll, subReader.getInt64Field()); EXPECT_EQ(90u, subReader.getUInt8Field()); EXPECT_EQ(1234u, subReader.getUInt16Field()); EXPECT_EQ(56789012u, subReader.getUInt32Field()); EXPECT_EQ(345678901234567890ull, subReader.getUInt64Field()); EXPECT_FLOAT_EQ(-1.25e-10f, subReader.getFloat32Field()); EXPECT_DOUBLE_EQ(345, subReader.getFloat64Field()); EXPECT_EQ("baz", subReader.getTextField()); EXPECT_EQ(data("qux"), subReader.getDataField()); { auto subSubReader = subReader.getStructField(); EXPECT_EQ("nested", subSubReader.getTextField()); EXPECT_EQ("really nested", subSubReader.getStructField().getTextField()); } EXPECT_EQ(TestEnum::BAZ, subReader.getEnumField()); checkList(subReader.getVoidList(), {VOID, VOID, VOID}); checkList(subReader.getBoolList(), {false, true, false, true, true}); checkList(subReader.getInt8List(), {12, -34, -0x80, 0x7f}); checkList(subReader.getInt16List(), {1234, -5678, -0x8000, 0x7fff}); // gcc warns on -0x800... and the only work-around I could find was to do -0x7ff...-1. checkList(subReader.getInt32List(), {12345678, -90123456, -0x7fffffff - 1, 0x7fffffff}); checkList(subReader.getInt64List(), {123456789012345ll, -678901234567890ll, -0x7fffffffffffffffll-1, 0x7fffffffffffffffll}); checkList(subReader.getUInt8List(), {12u, 34u, 0u, 0xffu}); checkList(subReader.getUInt16List(), {1234u, 5678u, 0u, 0xffffu}); checkList(subReader.getUInt32List(), {12345678u, 90123456u, 0u, 0xffffffffu}); checkList(subReader.getUInt64List(), {123456789012345ull, 678901234567890ull, 0ull, 0xffffffffffffffffull}); checkList(subReader.getFloat32List(), {0.0f, 1234567.0f, 1e37f, -1e37f, 1e-37f, -1e-37f}); checkList(subReader.getFloat64List(), {0.0, 123456789012345.0, 1e306, -1e306, 1e-306, -1e-306}); checkList(subReader.getTextList(), {"quux", "corge", "grault"}); checkList(subReader.getDataList(), {data("garply"), data("waldo"), data("fred")}); { auto listReader = subReader.getStructList(); ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("x structlist 1", listReader[0].getTextField()); EXPECT_EQ("x structlist 2", listReader[1].getTextField()); EXPECT_EQ("x structlist 3", listReader[2].getTextField()); } checkList(subReader.getEnumList(), {TestEnum::QUX, TestEnum::BAR, TestEnum::GRAULT}); } EXPECT_EQ(TestEnum::CORGE, reader.getEnumField()); EXPECT_EQ(6u, reader.getVoidList().size()); checkList(reader.getBoolList(), {true, false, false, true}); checkList(reader.getInt8List(), {111, -111}); checkList(reader.getInt16List(), {11111, -11111}); checkList(reader.getInt32List(), {111111111, -111111111}); checkList(reader.getInt64List(), {1111111111111111111ll, -1111111111111111111ll}); checkList(reader.getUInt8List(), {111u, 222u}); checkList(reader.getUInt16List(), {33333u, 44444u}); checkList(reader.getUInt32List(), {3333333333u}); checkList(reader.getUInt64List(), {11111111111111111111ull}); { auto listReader = reader.getFloat32List(); ASSERT_EQ(4u, listReader.size()); EXPECT_EQ(5555.5f, listReader[0]); EXPECT_EQ(kj::inf(), listReader[1]); EXPECT_EQ(-kj::inf(), listReader[2]); EXPECT_TRUE(isNaN(listReader[3])); } { auto listReader = reader.getFloat64List(); ASSERT_EQ(4u, listReader.size()); EXPECT_EQ(7777.75, listReader[0]); EXPECT_EQ(kj::inf(), listReader[1]); EXPECT_EQ(-kj::inf(), listReader[2]); EXPECT_TRUE(isNaN(listReader[3])); } checkList(reader.getTextList(), {"plugh", "xyzzy", "thud"}); checkList(reader.getDataList(), {data("oops"), data("exhausted"), data("rfc3092")}); { auto listReader = reader.getStructList(); ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("structlist 1", listReader[0].getTextField()); EXPECT_EQ("structlist 2", listReader[1].getTextField()); EXPECT_EQ("structlist 3", listReader[2].getTextField()); } checkList(reader.getEnumList(), {TestEnum::FOO, TestEnum::GARPLY}); } #if !CAPNP_LITE // Hack because as<>() is a template-parameter-dependent lookup everywhere below... #define as template as Text::Reader name(DynamicEnum e) { KJ_IF_MAYBE(schema, e.getEnumerant()) { return schema->getProto().getName(); } else { return "(unknown enumerant)"; } } template void checkEnumList(T reader, std::initializer_list expected) { auto list = reader.as(); ASSERT_EQ(expected.size(), list.size()); for (uint i = 0; i < expected.size(); i++) { EXPECT_EQ(expected.begin()[i], name(list[i].as())); } } template void dynamicCheckTestMessage(Reader reader) { EXPECT_EQ(VOID, reader.get("voidField").as()); EXPECT_EQ(true, reader.get("boolField").as()); EXPECT_EQ(-123, reader.get("int8Field").as()); EXPECT_EQ(-12345, reader.get("int16Field").as()); EXPECT_EQ(-12345678, reader.get("int32Field").as()); EXPECT_EQ(-123456789012345ll, reader.get("int64Field").as()); EXPECT_EQ(234u, reader.get("uInt8Field").as()); EXPECT_EQ(45678u, reader.get("uInt16Field").as()); EXPECT_EQ(3456789012u, reader.get("uInt32Field").as()); EXPECT_EQ(12345678901234567890ull, reader.get("uInt64Field").as()); EXPECT_FLOAT_EQ(1234.5f, reader.get("float32Field").as()); EXPECT_DOUBLE_EQ(-123e45, reader.get("float64Field").as()); EXPECT_EQ("foo", reader.get("textField").as()); EXPECT_EQ(data("bar"), reader.get("dataField").as()); { auto subReader = reader.get("structField").as(); EXPECT_EQ(VOID, subReader.get("voidField").as()); EXPECT_EQ(true, subReader.get("boolField").as()); EXPECT_EQ(-12, subReader.get("int8Field").as()); EXPECT_EQ(3456, subReader.get("int16Field").as()); EXPECT_EQ(-78901234, subReader.get("int32Field").as()); EXPECT_EQ(56789012345678ll, subReader.get("int64Field").as()); EXPECT_EQ(90u, subReader.get("uInt8Field").as()); EXPECT_EQ(1234u, subReader.get("uInt16Field").as()); EXPECT_EQ(56789012u, subReader.get("uInt32Field").as()); EXPECT_EQ(345678901234567890ull, subReader.get("uInt64Field").as()); EXPECT_FLOAT_EQ(-1.25e-10f, subReader.get("float32Field").as()); EXPECT_DOUBLE_EQ(345, subReader.get("float64Field").as()); EXPECT_EQ("baz", subReader.get("textField").as()); EXPECT_EQ(data("qux"), subReader.get("dataField").as()); { auto subSubReader = subReader.get("structField").as(); EXPECT_EQ("nested", subSubReader.get("textField").as()); EXPECT_EQ("really nested", subSubReader.get("structField").as() .get("textField").as()); } EXPECT_EQ("baz", name(subReader.get("enumField").as())); checkList(subReader.get("voidList"), {VOID, VOID, VOID}); checkList(subReader.get("boolList"), {false, true, false, true, true}); checkList(subReader.get("int8List"), {12, -34, -0x80, 0x7f}); checkList(subReader.get("int16List"), {1234, -5678, -0x8000, 0x7fff}); // gcc warns on -0x800... and the only work-around I could find was to do -0x7ff...-1. checkList(subReader.get("int32List"), {12345678, -90123456, -0x7fffffff-1, 0x7fffffff}); checkList(subReader.get("int64List"), {123456789012345ll, -678901234567890ll, -0x7fffffffffffffffll-1, 0x7fffffffffffffffll}); checkList(subReader.get("uInt8List"), {12u, 34u, 0u, 0xffu}); checkList(subReader.get("uInt16List"), {1234u, 5678u, 0u, 0xffffu}); checkList(subReader.get("uInt32List"), {12345678u, 90123456u, 0u, 0xffffffffu}); checkList(subReader.get("uInt64List"), {123456789012345ull, 678901234567890ull, 0ull, 0xffffffffffffffffull}); checkList(subReader.get("float32List"), {0.0f, 1234567.0f, 1e37f, -1e37f, 1e-37f, -1e-37f}); checkList(subReader.get("float64List"), {0.0, 123456789012345.0, 1e306, -1e306, 1e-306, -1e-306}); checkList(subReader.get("textList"), {"quux", "corge", "grault"}); checkList(subReader.get("dataList"), {data("garply"), data("waldo"), data("fred")}); { auto listReader = subReader.get("structList").as(); ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("x structlist 1", listReader[0].as().get("textField").as()); EXPECT_EQ("x structlist 2", listReader[1].as().get("textField").as()); EXPECT_EQ("x structlist 3", listReader[2].as().get("textField").as()); } checkEnumList(subReader.get("enumList"), {"qux", "bar", "grault"}); } EXPECT_EQ("corge", name(reader.get("enumField").as())); EXPECT_EQ(6u, reader.get("voidList").as().size()); checkList(reader.get("boolList"), {true, false, false, true}); checkList(reader.get("int8List"), {111, -111}); checkList(reader.get("int16List"), {11111, -11111}); checkList(reader.get("int32List"), {111111111, -111111111}); checkList(reader.get("int64List"), {1111111111111111111ll, -1111111111111111111ll}); checkList(reader.get("uInt8List"), {111u, 222u}); checkList(reader.get("uInt16List"), {33333u, 44444u}); checkList(reader.get("uInt32List"), {3333333333u}); checkList(reader.get("uInt64List"), {11111111111111111111ull}); { auto listReader = reader.get("float32List").as(); ASSERT_EQ(4u, listReader.size()); EXPECT_EQ(5555.5f, listReader[0].as()); EXPECT_EQ(kj::inf(), listReader[1].as()); EXPECT_EQ(-kj::inf(), listReader[2].as()); EXPECT_TRUE(isNaN(listReader[3].as())); } { auto listReader = reader.get("float64List").as(); ASSERT_EQ(4u, listReader.size()); EXPECT_EQ(7777.75, listReader[0].as()); EXPECT_EQ(kj::inf(), listReader[1].as()); EXPECT_EQ(-kj::inf(), listReader[2].as()); EXPECT_TRUE(isNaN(listReader[3].as())); } checkList(reader.get("textList"), {"plugh", "xyzzy", "thud"}); checkList(reader.get("dataList"), {data("oops"), data("exhausted"), data("rfc3092")}); { auto listReader = reader.get("structList").as(); ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("structlist 1", listReader[0].as().get("textField").as()); EXPECT_EQ("structlist 2", listReader[1].as().get("textField").as()); EXPECT_EQ("structlist 3", listReader[2].as().get("textField").as()); } checkEnumList(reader.get("enumList"), {"foo", "garply"}); } #undef as #endif // !CAPNP_LITE template void genericCheckTestMessageAllZero(Reader reader) { EXPECT_EQ(VOID, reader.getVoidField()); EXPECT_EQ(false, reader.getBoolField()); EXPECT_EQ(0, reader.getInt8Field()); EXPECT_EQ(0, reader.getInt16Field()); EXPECT_EQ(0, reader.getInt32Field()); EXPECT_EQ(0, reader.getInt64Field()); EXPECT_EQ(0u, reader.getUInt8Field()); EXPECT_EQ(0u, reader.getUInt16Field()); EXPECT_EQ(0u, reader.getUInt32Field()); EXPECT_EQ(0u, reader.getUInt64Field()); EXPECT_FLOAT_EQ(0, reader.getFloat32Field()); EXPECT_DOUBLE_EQ(0, reader.getFloat64Field()); EXPECT_EQ("", reader.getTextField()); EXPECT_EQ(data(""), reader.getDataField()); { auto subReader = reader.getStructField(); EXPECT_EQ(VOID, subReader.getVoidField()); EXPECT_EQ(false, subReader.getBoolField()); EXPECT_EQ(0, subReader.getInt8Field()); EXPECT_EQ(0, subReader.getInt16Field()); EXPECT_EQ(0, subReader.getInt32Field()); EXPECT_EQ(0, subReader.getInt64Field()); EXPECT_EQ(0u, subReader.getUInt8Field()); EXPECT_EQ(0u, subReader.getUInt16Field()); EXPECT_EQ(0u, subReader.getUInt32Field()); EXPECT_EQ(0u, subReader.getUInt64Field()); EXPECT_FLOAT_EQ(0, subReader.getFloat32Field()); EXPECT_DOUBLE_EQ(0, subReader.getFloat64Field()); EXPECT_EQ("", subReader.getTextField()); EXPECT_EQ(data(""), subReader.getDataField()); { auto subSubReader = subReader.getStructField(); EXPECT_EQ("", subSubReader.getTextField()); EXPECT_EQ("", subSubReader.getStructField().getTextField()); } EXPECT_EQ(0u, subReader.getVoidList().size()); EXPECT_EQ(0u, subReader.getBoolList().size()); EXPECT_EQ(0u, subReader.getInt8List().size()); EXPECT_EQ(0u, subReader.getInt16List().size()); EXPECT_EQ(0u, subReader.getInt32List().size()); EXPECT_EQ(0u, subReader.getInt64List().size()); EXPECT_EQ(0u, subReader.getUInt8List().size()); EXPECT_EQ(0u, subReader.getUInt16List().size()); EXPECT_EQ(0u, subReader.getUInt32List().size()); EXPECT_EQ(0u, subReader.getUInt64List().size()); EXPECT_EQ(0u, subReader.getFloat32List().size()); EXPECT_EQ(0u, subReader.getFloat64List().size()); EXPECT_EQ(0u, subReader.getTextList().size()); EXPECT_EQ(0u, subReader.getDataList().size()); EXPECT_EQ(0u, subReader.getStructList().size()); } EXPECT_EQ(0u, reader.getVoidList().size()); EXPECT_EQ(0u, reader.getBoolList().size()); EXPECT_EQ(0u, reader.getInt8List().size()); EXPECT_EQ(0u, reader.getInt16List().size()); EXPECT_EQ(0u, reader.getInt32List().size()); EXPECT_EQ(0u, reader.getInt64List().size()); EXPECT_EQ(0u, reader.getUInt8List().size()); EXPECT_EQ(0u, reader.getUInt16List().size()); EXPECT_EQ(0u, reader.getUInt32List().size()); EXPECT_EQ(0u, reader.getUInt64List().size()); EXPECT_EQ(0u, reader.getFloat32List().size()); EXPECT_EQ(0u, reader.getFloat64List().size()); EXPECT_EQ(0u, reader.getTextList().size()); EXPECT_EQ(0u, reader.getDataList().size()); EXPECT_EQ(0u, reader.getStructList().size()); } #if !CAPNP_LITE // Hack because as<>() is a template-parameter-dependent lookup everywhere below... #define as template as template void dynamicCheckTestMessageAllZero(Reader reader) { EXPECT_EQ(VOID, reader.get("voidField").as()); EXPECT_EQ(false, reader.get("boolField").as()); EXPECT_EQ(0, reader.get("int8Field").as()); EXPECT_EQ(0, reader.get("int16Field").as()); EXPECT_EQ(0, reader.get("int32Field").as()); EXPECT_EQ(0, reader.get("int64Field").as()); EXPECT_EQ(0u, reader.get("uInt8Field").as()); EXPECT_EQ(0u, reader.get("uInt16Field").as()); EXPECT_EQ(0u, reader.get("uInt32Field").as()); EXPECT_EQ(0u, reader.get("uInt64Field").as()); EXPECT_FLOAT_EQ(0, reader.get("float32Field").as()); EXPECT_DOUBLE_EQ(0, reader.get("float64Field").as()); EXPECT_EQ("", reader.get("textField").as()); EXPECT_EQ(data(""), reader.get("dataField").as()); { auto subReader = reader.get("structField").as(); EXPECT_EQ(VOID, subReader.get("voidField").as()); EXPECT_EQ(false, subReader.get("boolField").as()); EXPECT_EQ(0, subReader.get("int8Field").as()); EXPECT_EQ(0, subReader.get("int16Field").as()); EXPECT_EQ(0, subReader.get("int32Field").as()); EXPECT_EQ(0, subReader.get("int64Field").as()); EXPECT_EQ(0u, subReader.get("uInt8Field").as()); EXPECT_EQ(0u, subReader.get("uInt16Field").as()); EXPECT_EQ(0u, subReader.get("uInt32Field").as()); EXPECT_EQ(0u, subReader.get("uInt64Field").as()); EXPECT_FLOAT_EQ(0, subReader.get("float32Field").as()); EXPECT_DOUBLE_EQ(0, subReader.get("float64Field").as()); EXPECT_EQ("", subReader.get("textField").as()); EXPECT_EQ(data(""), subReader.get("dataField").as()); { auto subSubReader = subReader.get("structField").as(); EXPECT_EQ("", subSubReader.get("textField").as()); EXPECT_EQ("", subSubReader.get("structField").as() .get("textField").as()); } EXPECT_EQ(0u, subReader.get("voidList").as().size()); EXPECT_EQ(0u, subReader.get("boolList").as().size()); EXPECT_EQ(0u, subReader.get("int8List").as().size()); EXPECT_EQ(0u, subReader.get("int16List").as().size()); EXPECT_EQ(0u, subReader.get("int32List").as().size()); EXPECT_EQ(0u, subReader.get("int64List").as().size()); EXPECT_EQ(0u, subReader.get("uInt8List").as().size()); EXPECT_EQ(0u, subReader.get("uInt16List").as().size()); EXPECT_EQ(0u, subReader.get("uInt32List").as().size()); EXPECT_EQ(0u, subReader.get("uInt64List").as().size()); EXPECT_EQ(0u, subReader.get("float32List").as().size()); EXPECT_EQ(0u, subReader.get("float64List").as().size()); EXPECT_EQ(0u, subReader.get("textList").as().size()); EXPECT_EQ(0u, subReader.get("dataList").as().size()); EXPECT_EQ(0u, subReader.get("structList").as().size()); } EXPECT_EQ(0u, reader.get("voidList").as().size()); EXPECT_EQ(0u, reader.get("boolList").as().size()); EXPECT_EQ(0u, reader.get("int8List").as().size()); EXPECT_EQ(0u, reader.get("int16List").as().size()); EXPECT_EQ(0u, reader.get("int32List").as().size()); EXPECT_EQ(0u, reader.get("int64List").as().size()); EXPECT_EQ(0u, reader.get("uInt8List").as().size()); EXPECT_EQ(0u, reader.get("uInt16List").as().size()); EXPECT_EQ(0u, reader.get("uInt32List").as().size()); EXPECT_EQ(0u, reader.get("uInt64List").as().size()); EXPECT_EQ(0u, reader.get("float32List").as().size()); EXPECT_EQ(0u, reader.get("float64List").as().size()); EXPECT_EQ(0u, reader.get("textList").as().size()); EXPECT_EQ(0u, reader.get("dataList").as().size()); EXPECT_EQ(0u, reader.get("structList").as().size()); } #undef as #endif // !CAPNP_LITE template void genericInitListDefaults(Builder builder) { auto lists = builder.initLists(); lists.initList0(2); lists.initList1(4); lists.initList8(2); lists.initList16(2); lists.initList32(2); lists.initList64(2); lists.initListP(2); lists.getList0()[0].setF(VOID); lists.getList0()[1].setF(VOID); lists.getList1()[0].setF(true); lists.getList1()[1].setF(false); lists.getList1()[2].setF(true); lists.getList1()[3].setF(true); lists.getList8()[0].setF(123u); lists.getList8()[1].setF(45u); lists.getList16()[0].setF(12345u); lists.getList16()[1].setF(6789u); lists.getList32()[0].setF(123456789u); lists.getList32()[1].setF(234567890u); lists.getList64()[0].setF(1234567890123456u); lists.getList64()[1].setF(2345678901234567u); lists.getListP()[0].setF("foo"); lists.getListP()[1].setF("bar"); { auto l = lists.initInt32ListList(3); l.set(0, {1, 2, 3}); l.set(1, {4, 5}); l.set(2, {12341234}); } { auto l = lists.initTextListList(3); l.set(0, {"foo", "bar"}); l.set(1, {"baz"}); l.set(2, {"qux", "corge"}); } { auto l = lists.initStructListList(2); auto e = l.init(0, 2); e[0].setInt32Field(123); e[1].setInt32Field(456); e = l.init(1, 1); e[0].setInt32Field(789); } } #if !CAPNP_LITE void dynamicInitListDefaults(DynamicStruct::Builder builder) { auto lists = builder.init("lists").as(); lists.init("list0", 2); lists.init("list1", 4); lists.init("list8", 2); lists.init("list16", 2); lists.init("list32", 2); lists.init("list64", 2); lists.init("listP", 2); lists.get("list0").as()[0].as().set("f", VOID); lists.get("list0").as()[1].as().set("f", VOID); lists.get("list1").as()[0].as().set("f", true); lists.get("list1").as()[1].as().set("f", false); lists.get("list1").as()[2].as().set("f", true); lists.get("list1").as()[3].as().set("f", true); lists.get("list8").as()[0].as().set("f", 123u); lists.get("list8").as()[1].as().set("f", 45u); lists.get("list16").as()[0].as().set("f", 12345u); lists.get("list16").as()[1].as().set("f", 6789u); lists.get("list32").as()[0].as().set("f", 123456789u); lists.get("list32").as()[1].as().set("f", 234567890u); lists.get("list64").as()[0].as().set("f", 1234567890123456u); lists.get("list64").as()[1].as().set("f", 2345678901234567u); lists.get("listP").as()[0].as().set("f", "foo"); lists.get("listP").as()[1].as().set("f", "bar"); { auto l = lists.init("int32ListList", 3).as(); l.init(0, 3).as().copyFrom({1, 2, 3}); l.init(1, 2).as().copyFrom({4, 5}); l.init(2, 1).as().copyFrom({12341234}); } { auto l = lists.init("textListList", 3).as(); l.init(0, 2).as().copyFrom({"foo", "bar"}); l.init(1, 1).as().copyFrom({"baz"}); l.init(2, 2).as().copyFrom({"qux", "corge"}); } { auto l = lists.init("structListList", 2).as(); auto e = l.init(0, 2).as(); e[0].as().setInt32Field(123); e[1].as().setInt32Field(456); e = l.init(1, 1).as(); e[0].as().setInt32Field(789); } } #endif // !CAPNP_LITE template void genericCheckListDefaults(Reader reader) { auto lists = reader.getLists(); ASSERT_EQ(2u, lists.getList0().size()); ASSERT_EQ(4u, lists.getList1().size()); ASSERT_EQ(2u, lists.getList8().size()); ASSERT_EQ(2u, lists.getList16().size()); ASSERT_EQ(2u, lists.getList32().size()); ASSERT_EQ(2u, lists.getList64().size()); ASSERT_EQ(2u, lists.getListP().size()); EXPECT_EQ(VOID, lists.getList0()[0].getF()); EXPECT_EQ(VOID, lists.getList0()[1].getF()); EXPECT_TRUE(lists.getList1()[0].getF()); EXPECT_FALSE(lists.getList1()[1].getF()); EXPECT_TRUE(lists.getList1()[2].getF()); EXPECT_TRUE(lists.getList1()[3].getF()); EXPECT_EQ(123u, lists.getList8()[0].getF()); EXPECT_EQ(45u, lists.getList8()[1].getF()); EXPECT_EQ(12345u, lists.getList16()[0].getF()); EXPECT_EQ(6789u, lists.getList16()[1].getF()); EXPECT_EQ(123456789u, lists.getList32()[0].getF()); EXPECT_EQ(234567890u, lists.getList32()[1].getF()); EXPECT_EQ(1234567890123456u, lists.getList64()[0].getF()); EXPECT_EQ(2345678901234567u, lists.getList64()[1].getF()); EXPECT_EQ("foo", lists.getListP()[0].getF()); EXPECT_EQ("bar", lists.getListP()[1].getF()); { auto l = lists.getInt32ListList(); ASSERT_EQ(3u, l.size()); checkList(l[0], {1, 2, 3}); checkList(l[1], {4, 5}); checkList(l[2], {12341234}); } { auto l = lists.getTextListList(); ASSERT_EQ(3u, l.size()); checkList(l[0], {"foo", "bar"}); checkList(l[1], {"baz"}); checkList(l[2], {"qux", "corge"}); } { auto l = lists.getStructListList(); ASSERT_EQ(2u, l.size()); auto e = l[0]; ASSERT_EQ(2u, e.size()); EXPECT_EQ(123, e[0].getInt32Field()); EXPECT_EQ(456, e[1].getInt32Field()); e = l[1]; ASSERT_EQ(1u, e.size()); EXPECT_EQ(789, e[0].getInt32Field()); } } #if !CAPNP_LITE // Hack because as<>() is a template-parameter-dependent lookup everywhere below... #define as template as template void dynamicCheckListDefaults(Reader reader) { auto lists = reader.get("lists").as(); ASSERT_EQ(2u, lists.get("list0").as().size()); ASSERT_EQ(4u, lists.get("list1").as().size()); ASSERT_EQ(2u, lists.get("list8").as().size()); ASSERT_EQ(2u, lists.get("list16").as().size()); ASSERT_EQ(2u, lists.get("list32").as().size()); ASSERT_EQ(2u, lists.get("list64").as().size()); ASSERT_EQ(2u, lists.get("listP").as().size()); EXPECT_EQ(VOID, lists.get("list0").as()[0].as().get("f").as()); EXPECT_EQ(VOID, lists.get("list0").as()[1].as().get("f").as()); EXPECT_TRUE(lists.get("list1").as()[0].as().get("f").as()); EXPECT_FALSE(lists.get("list1").as()[1].as().get("f").as()); EXPECT_TRUE(lists.get("list1").as()[2].as().get("f").as()); EXPECT_TRUE(lists.get("list1").as()[3].as().get("f").as()); EXPECT_EQ(123u, lists.get("list8").as()[0].as().get("f").as()); EXPECT_EQ(45u, lists.get("list8").as()[1].as().get("f").as()); EXPECT_EQ(12345u, lists.get("list16").as()[0].as().get("f").as()); EXPECT_EQ(6789u, lists.get("list16").as()[1].as().get("f").as()); EXPECT_EQ(123456789u, lists.get("list32").as()[0].as().get("f").as()); EXPECT_EQ(234567890u, lists.get("list32").as()[1].as().get("f").as()); EXPECT_EQ(1234567890123456u, lists.get("list64").as()[0].as().get("f").as()); EXPECT_EQ(2345678901234567u, lists.get("list64").as()[1].as().get("f").as()); EXPECT_EQ("foo", lists.get("listP").as()[0].as().get("f").as()); EXPECT_EQ("bar", lists.get("listP").as()[1].as().get("f").as()); { auto l = lists.get("int32ListList").as(); ASSERT_EQ(3u, l.size()); checkList(l[0], {1, 2, 3}); checkList(l[1], {4, 5}); checkList(l[2], {12341234}); } { auto l = lists.get("textListList").as(); ASSERT_EQ(3u, l.size()); checkList(l[0], {"foo", "bar"}); checkList(l[1], {"baz"}); checkList(l[2], {"qux", "corge"}); } { auto l = lists.get("structListList").as(); ASSERT_EQ(2u, l.size()); auto e = l[0].as(); ASSERT_EQ(2u, e.size()); EXPECT_EQ(123, e[0].as().getInt32Field()); EXPECT_EQ(456, e[1].as().getInt32Field()); e = l[1].as(); ASSERT_EQ(1u, e.size()); EXPECT_EQ(789, e[0].as().getInt32Field()); } } #undef as #endif // !CAPNP_LITE } // namespace void initTestMessage(TestAllTypes::Builder builder) { genericInitTestMessage(builder); } void initTestMessage(TestDefaults::Builder builder) { genericInitTestMessage(builder); } void initTestMessage(TestListDefaults::Builder builder) { genericInitListDefaults(builder); } void checkTestMessage(TestAllTypes::Builder builder) { genericCheckTestMessage(builder); } void checkTestMessage(TestDefaults::Builder builder) { genericCheckTestMessage(builder); } void checkTestMessage(TestListDefaults::Builder builder) { genericCheckListDefaults(builder); } void checkTestMessage(TestAllTypes::Reader reader) { genericCheckTestMessage(reader); } void checkTestMessage(TestDefaults::Reader reader) { genericCheckTestMessage(reader); } void checkTestMessage(TestListDefaults::Reader reader) { genericCheckListDefaults(reader); } void checkTestMessageAllZero(TestAllTypes::Builder builder) { genericCheckTestMessageAllZero(builder); } void checkTestMessageAllZero(TestAllTypes::Reader reader) { genericCheckTestMessageAllZero(reader); } #if !CAPNP_LITE void initDynamicTestMessage(DynamicStruct::Builder builder) { dynamicInitTestMessage(builder); } void initDynamicTestLists(DynamicStruct::Builder builder) { dynamicInitListDefaults(builder); } void checkDynamicTestMessage(DynamicStruct::Builder builder) { dynamicCheckTestMessage(builder); } void checkDynamicTestLists(DynamicStruct::Builder builder) { dynamicCheckListDefaults(builder); } void checkDynamicTestMessage(DynamicStruct::Reader reader) { dynamicCheckTestMessage(reader); } void checkDynamicTestLists(DynamicStruct::Reader reader) { dynamicCheckListDefaults(reader); } void checkDynamicTestMessageAllZero(DynamicStruct::Builder builder) { dynamicCheckTestMessageAllZero(builder); } void checkDynamicTestMessageAllZero(DynamicStruct::Reader reader) { dynamicCheckTestMessageAllZero(reader); } #endif // !CAPNP_LITE // ======================================================================================= // Interface implementations. #if !CAPNP_LITE TestInterfaceImpl::TestInterfaceImpl(int& callCount): callCount(callCount) {} kj::Promise TestInterfaceImpl::foo(FooContext context) { ++callCount; auto params = context.getParams(); auto result = context.getResults(); EXPECT_EQ(123, params.getI()); EXPECT_TRUE(params.getJ()); result.setX("foo"); return kj::READY_NOW; } kj::Promise TestInterfaceImpl::baz(BazContext context) { ++callCount; auto params = context.getParams(); checkTestMessage(params.getS()); context.releaseParams(); EXPECT_ANY_THROW(context.getParams()); return kj::READY_NOW; } TestExtendsImpl::TestExtendsImpl(int& callCount): callCount(callCount) {} kj::Promise TestExtendsImpl::foo(FooContext context) { ++callCount; auto params = context.getParams(); auto result = context.getResults(); EXPECT_EQ(321, params.getI()); EXPECT_FALSE(params.getJ()); result.setX("bar"); return kj::READY_NOW; } kj::Promise TestExtendsImpl::grault(GraultContext context) { ++callCount; context.releaseParams(); initTestMessage(context.getResults()); return kj::READY_NOW; } TestPipelineImpl::TestPipelineImpl(int& callCount): callCount(callCount) {} kj::Promise TestPipelineImpl::getCap(GetCapContext context) { ++callCount; auto params = context.getParams(); EXPECT_EQ(234, params.getN()); auto cap = params.getInCap(); context.releaseParams(); auto request = cap.fooRequest(); request.setI(123); request.setJ(true); return request.send().then( [this,KJ_CPCAP(context)](Response&& response) mutable { EXPECT_EQ("foo", response.getX()); auto result = context.getResults(); result.setS("bar"); result.initOutBox().setCap(kj::heap(callCount)); }); } kj::Promise TestPipelineImpl::getAnyCap(GetAnyCapContext context) { ++callCount; auto params = context.getParams(); EXPECT_EQ(234, params.getN()); auto cap = params.getInCap(); context.releaseParams(); auto request = cap.castAs().fooRequest(); request.setI(123); request.setJ(true); return request.send().then( [this,KJ_CPCAP(context)](Response&& response) mutable { EXPECT_EQ("foo", response.getX()); auto result = context.getResults(); result.setS("bar"); result.initOutBox().setCap(kj::heap(callCount)); }); } kj::Promise TestCallOrderImpl::getCallSequence(GetCallSequenceContext context) { auto result = context.getResults(); result.setN(count++); return kj::READY_NOW; } TestTailCallerImpl::TestTailCallerImpl(int& callCount): callCount(callCount) {} kj::Promise TestTailCallerImpl::foo(FooContext context) { ++callCount; auto params = context.getParams(); auto tailRequest = params.getCallee().fooRequest(); tailRequest.setI(params.getI()); tailRequest.setT("from TestTailCaller"); return context.tailCall(kj::mv(tailRequest)); } TestTailCalleeImpl::TestTailCalleeImpl(int& callCount): callCount(callCount) {} kj::Promise TestTailCalleeImpl::foo(FooContext context) { ++callCount; auto params = context.getParams(); auto results = context.getResults(); results.setI(params.getI()); results.setT(params.getT()); results.setC(kj::heap()); return kj::READY_NOW; } TestMoreStuffImpl::TestMoreStuffImpl(int& callCount, int& handleCount) : callCount(callCount), handleCount(handleCount) {} kj::Promise TestMoreStuffImpl::getCallSequence(GetCallSequenceContext context) { auto result = context.getResults(); result.setN(callCount++); return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::callFoo(CallFooContext context) { ++callCount; auto params = context.getParams(); auto cap = params.getCap(); auto request = cap.fooRequest(); request.setI(123); request.setJ(true); return request.send().then( [KJ_CPCAP(context)](Response&& response) mutable { EXPECT_EQ("foo", response.getX()); context.getResults().setS("bar"); }); } kj::Promise TestMoreStuffImpl::callFooWhenResolved(CallFooWhenResolvedContext context) { ++callCount; auto params = context.getParams(); auto cap = params.getCap(); return cap.whenResolved().then([KJ_CPCAP(cap),KJ_CPCAP(context)]() mutable { auto request = cap.fooRequest(); request.setI(123); request.setJ(true); return request.send().then( [KJ_CPCAP(context)](Response&& response) mutable { EXPECT_EQ("foo", response.getX()); context.getResults().setS("bar"); }); }); } kj::Promise TestMoreStuffImpl::neverReturn(NeverReturnContext context) { ++callCount; // Attach `cap` to the promise to make sure it is released. auto promise = kj::Promise(kj::NEVER_DONE).attach(context.getParams().getCap()); // Also attach `cap` to the result struct to make sure that is released. context.getResults().setCapCopy(context.getParams().getCap()); context.allowCancellation(); return kj::mv(promise); } kj::Promise TestMoreStuffImpl::hold(HoldContext context) { ++callCount; auto params = context.getParams(); clientToHold = params.getCap(); return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::callHeld(CallHeldContext context) { ++callCount; auto request = clientToHold.fooRequest(); request.setI(123); request.setJ(true); return request.send().then( [KJ_CPCAP(context)](Response&& response) mutable { EXPECT_EQ("foo", response.getX()); context.getResults().setS("bar"); }); } kj::Promise TestMoreStuffImpl::getHeld(GetHeldContext context) { ++callCount; auto result = context.getResults(); result.setCap(clientToHold); return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::echo(EchoContext context) { ++callCount; auto params = context.getParams(); auto result = context.getResults(); result.setCap(params.getCap()); return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::expectCancel(ExpectCancelContext context) { auto cap = context.getParams().getCap(); context.allowCancellation(); return loop(0, cap, context); } kj::Promise TestMoreStuffImpl::loop(uint depth, test::TestInterface::Client cap, ExpectCancelContext context) { if (depth > 100) { ADD_FAILURE() << "Looped too long, giving up."; return kj::READY_NOW; } else { return kj::evalLater([this,depth,KJ_CPCAP(cap),KJ_CPCAP(context)]() mutable { return loop(depth + 1, cap, context); }); } } class HandleImpl final: public test::TestHandle::Server { public: HandleImpl(int& count): count(count) { ++count; } ~HandleImpl() { --count; } private: int& count; }; kj::Promise TestMoreStuffImpl::getHandle(GetHandleContext context) { context.getResults().setHandle(kj::heap(handleCount)); return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::getNull(GetNullContext context) { return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::getEnormousString(GetEnormousStringContext context) { context.getResults().initStr(100000000); // 100MB return kj::READY_NOW; } kj::Promise TestMoreStuffImpl::writeToFd(WriteToFdContext context) { auto params = context.getParams(); auto promises = kj::heapArrayBuilder>(2); promises.add(params.getFdCap1().getFd() .then([](kj::Maybe fd) { kj::FdOutputStream(KJ_ASSERT_NONNULL(fd)).write("foo", 3); })); promises.add(params.getFdCap2().getFd() .then([context](kj::Maybe fd) mutable { context.getResults().setSecondFdPresent(fd != nullptr); KJ_IF_MAYBE(f, fd) { kj::FdOutputStream(*f).write("bar", 3); } })); int pair[2]; KJ_SYSCALL(kj::miniposix::pipe(pair)); kj::AutoCloseFd in(pair[0]); kj::AutoCloseFd out(pair[1]); kj::FdOutputStream(kj::mv(out)).write("baz", 3); context.getResults().setFdCap3(kj::heap(kj::mv(in))); return kj::joinPromises(promises.finish()); } #endif // !CAPNP_LITE } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/test-util.h0000644000175000017500000002633113650101756020754 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include "blob.h" #include #if !CAPNP_LITE #include "dynamic.h" #include #endif // !CAPNP_LITE CAPNP_BEGIN_HEADER // TODO(cleanup): Auto-generate stringification functions for union discriminants. namespace capnproto_test { namespace capnp { namespace test { inline kj::String KJ_STRINGIFY(TestUnion::Union0::Which which) { return kj::str(static_cast(which)); } inline kj::String KJ_STRINGIFY(TestUnion::Union1::Which which) { return kj::str(static_cast(which)); } inline kj::String KJ_STRINGIFY(TestUnion::Union2::Which which) { return kj::str(static_cast(which)); } inline kj::String KJ_STRINGIFY(TestUnion::Union3::Which which) { return kj::str(static_cast(which)); } inline kj::String KJ_STRINGIFY(TestUnnamedUnion::Which which) { return kj::str(static_cast(which)); } inline kj::String KJ_STRINGIFY(TestGroups::Groups::Which which) { return kj::str(static_cast(which)); } inline kj::String KJ_STRINGIFY(TestInterleavedGroups::Group1::Which which) { return kj::str(static_cast(which)); } } // namespace test } // namespace capnp } // namespace capnproto_test namespace capnp { namespace _ { // private inline Data::Reader data(const char* str) { return Data::Reader(reinterpret_cast(str), strlen(str)); } namespace test = capnproto_test::capnp::test; // We don't use "using namespace" to pull these in because then things would still compile // correctly if they were generated in the global namespace. using ::capnproto_test::capnp::test::TestAllTypes; using ::capnproto_test::capnp::test::TestDefaults; using ::capnproto_test::capnp::test::TestEnum; using ::capnproto_test::capnp::test::TestUnion; using ::capnproto_test::capnp::test::TestUnionDefaults; using ::capnproto_test::capnp::test::TestNestedTypes; using ::capnproto_test::capnp::test::TestUsing; using ::capnproto_test::capnp::test::TestListDefaults; void initTestMessage(TestAllTypes::Builder builder); void initTestMessage(TestDefaults::Builder builder); void initTestMessage(TestListDefaults::Builder builder); void checkTestMessage(TestAllTypes::Builder builder); void checkTestMessage(TestDefaults::Builder builder); void checkTestMessage(TestListDefaults::Builder builder); void checkTestMessage(TestAllTypes::Reader reader); void checkTestMessage(TestDefaults::Reader reader); void checkTestMessage(TestListDefaults::Reader reader); void checkTestMessageAllZero(TestAllTypes::Builder builder); void checkTestMessageAllZero(TestAllTypes::Reader reader); #if !CAPNP_LITE void initDynamicTestMessage(DynamicStruct::Builder builder); void initDynamicTestLists(DynamicStruct::Builder builder); void checkDynamicTestMessage(DynamicStruct::Builder builder); void checkDynamicTestLists(DynamicStruct::Builder builder); void checkDynamicTestMessage(DynamicStruct::Reader reader); void checkDynamicTestLists(DynamicStruct::Reader reader); void checkDynamicTestMessageAllZero(DynamicStruct::Builder builder); void checkDynamicTestMessageAllZero(DynamicStruct::Reader reader); #endif // !CAPNP_LITE template inline void checkElement(T a, T b) { EXPECT_EQ(a, b); } template <> inline void checkElement(float a, float b) { EXPECT_FLOAT_EQ(a, b); } template <> inline void checkElement(double a, double b) { EXPECT_DOUBLE_EQ(a, b); } template void checkList(T reader, std::initializer_list expected) { ASSERT_EQ(expected.size(), reader.size()); for (uint i = 0; i < expected.size(); i++) { checkElement(expected.begin()[i], reader[i]); } } template void checkList(T reader, std::initializer_list expected) { ASSERT_EQ(expected.size(), reader.size()); for (uint i = 0; i < expected.size(); i++) { checkElement(expected.begin()[i], reader[i]); } } inline void checkList(List::Reader reader, std::initializer_list expectedData, std::initializer_list expectedPointers) { ASSERT_EQ(expectedData.size(), reader.size()); for (uint i = 0; i < expectedData.size(); i++) { EXPECT_EQ(expectedData.begin()[i], reader[i].getOld1()); EXPECT_EQ(expectedPointers.begin()[i], reader[i].getOld2()); } } // Hack because as<>() is a template-parameter-dependent lookup everywhere below... #define as template as template void expectPrimitiveEq(T a, T b) { EXPECT_EQ(a, b); } inline void expectPrimitiveEq(float a, float b) { EXPECT_FLOAT_EQ(a, b); } inline void expectPrimitiveEq(double a, double b) { EXPECT_DOUBLE_EQ(a, b); } inline void expectPrimitiveEq(Text::Reader a, Text::Builder b) { EXPECT_EQ(a, b); } inline void expectPrimitiveEq(Data::Reader a, Data::Builder b) { EXPECT_EQ(a, b); } #if !CAPNP_LITE template void checkList(T reader, std::initializer_list> expected) { auto list = reader.as(); ASSERT_EQ(expected.size(), list.size()); for (uint i = 0; i < expected.size(); i++) { expectPrimitiveEq(expected.begin()[i], list[i].as()); } auto typed = reader.as>(); ASSERT_EQ(expected.size(), typed.size()); for (uint i = 0; i < expected.size(); i++) { expectPrimitiveEq(expected.begin()[i], typed[i]); } } #endif // !CAPNP_LITE #undef as // ======================================================================================= // Interface implementations. #if !CAPNP_LITE class TestInterfaceImpl final: public test::TestInterface::Server { public: TestInterfaceImpl(int& callCount); kj::Promise foo(FooContext context) override; kj::Promise baz(BazContext context) override; private: int& callCount; }; class TestExtendsImpl final: public test::TestExtends2::Server { public: TestExtendsImpl(int& callCount); kj::Promise foo(FooContext context) override; kj::Promise grault(GraultContext context) override; private: int& callCount; }; class TestPipelineImpl final: public test::TestPipeline::Server { public: TestPipelineImpl(int& callCount); kj::Promise getCap(GetCapContext context) override; kj::Promise getAnyCap(GetAnyCapContext context) override; private: int& callCount; }; class TestCallOrderImpl final: public test::TestCallOrder::Server { public: kj::Promise getCallSequence(GetCallSequenceContext context) override; uint getCount() { return count; } private: uint count = 0; }; class TestTailCallerImpl final: public test::TestTailCaller::Server { public: TestTailCallerImpl(int& callCount); kj::Promise foo(FooContext context) override; private: int& callCount; }; class TestTailCalleeImpl final: public test::TestTailCallee::Server { public: TestTailCalleeImpl(int& callCount); kj::Promise foo(FooContext context) override; private: int& callCount; }; class TestMoreStuffImpl final: public test::TestMoreStuff::Server { public: TestMoreStuffImpl(int& callCount, int& handleCount); kj::Promise getCallSequence(GetCallSequenceContext context) override; kj::Promise callFoo(CallFooContext context) override; kj::Promise callFooWhenResolved(CallFooWhenResolvedContext context) override; kj::Promise neverReturn(NeverReturnContext context) override; kj::Promise hold(HoldContext context) override; kj::Promise callHeld(CallHeldContext context) override; kj::Promise getHeld(GetHeldContext context) override; kj::Promise echo(EchoContext context) override; kj::Promise expectCancel(ExpectCancelContext context) override; kj::Promise getHandle(GetHandleContext context) override; kj::Promise getNull(GetNullContext context) override; kj::Promise getEnormousString(GetEnormousStringContext context) override; kj::Promise writeToFd(WriteToFdContext context) override; private: int& callCount; int& handleCount; test::TestInterface::Client clientToHold = nullptr; kj::Promise loop(uint depth, test::TestInterface::Client cap, ExpectCancelContext context); }; class TestCapDestructor final: public test::TestInterface::Server { // Implementation of TestInterface that notifies when it is destroyed. public: TestCapDestructor(kj::Own>&& fulfiller) : fulfiller(kj::mv(fulfiller)), impl(dummy) {} ~TestCapDestructor() { fulfiller->fulfill(); } kj::Promise foo(FooContext context) { return impl.foo(context); } private: kj::Own> fulfiller; int dummy = 0; TestInterfaceImpl impl; }; class TestFdCap final: public test::TestInterface::Server { // Implementation of TestInterface that wraps a file descriptor. public: TestFdCap(kj::AutoCloseFd fd): fd(kj::mv(fd)) {} kj::Maybe getFd() override { return fd.get(); } private: kj::AutoCloseFd fd; }; class TestStreamingImpl final: public test::TestStreaming::Server { public: uint iSum = 0; uint jSum = 0; kj::Maybe>> fulfiller; bool jShouldThrow = false; kj::Promise doStreamI(DoStreamIContext context) override { iSum += context.getParams().getI(); auto paf = kj::newPromiseAndFulfiller(); fulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } kj::Promise doStreamJ(DoStreamJContext context) override { context.allowCancellation(); jSum += context.getParams().getJ(); if (jShouldThrow) { KJ_FAIL_ASSERT("throw requested") { break; } return kj::READY_NOW; } auto paf = kj::newPromiseAndFulfiller(); fulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } kj::Promise finishStream(FinishStreamContext context) override { auto results = context.getResults(); results.setTotalI(iSum); results.setTotalJ(jSum); return kj::READY_NOW; } }; #endif // !CAPNP_LITE } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/serialize-async.c++0000644000175000017500000002575413650101756022255 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "serialize-async.h" #include #include namespace capnp { namespace { class AsyncMessageReader: public MessageReader { public: inline AsyncMessageReader(ReaderOptions options): MessageReader(options) { memset(firstWord, 0, sizeof(firstWord)); } ~AsyncMessageReader() noexcept(false) {} kj::Promise read(kj::AsyncInputStream& inputStream, kj::ArrayPtr scratchSpace); kj::Promise> readWithFds( kj::AsyncCapabilityStream& inputStream, kj::ArrayPtr fds, kj::ArrayPtr scratchSpace); // implements MessageReader ---------------------------------------- kj::ArrayPtr getSegment(uint id) override { if (id >= segmentCount()) { return nullptr; } else { uint32_t size = id == 0 ? segment0Size() : moreSizes[id - 1].get(); return kj::arrayPtr(segmentStarts[id], size); } } private: _::WireValue firstWord[2]; kj::Array<_::WireValue> moreSizes; kj::Array segmentStarts; kj::Array ownedSpace; // Only if scratchSpace wasn't big enough. inline uint segmentCount() { return firstWord[0].get() + 1; } inline uint segment0Size() { return firstWord[1].get(); } kj::Promise readAfterFirstWord( kj::AsyncInputStream& inputStream, kj::ArrayPtr scratchSpace); kj::Promise readSegments( kj::AsyncInputStream& inputStream, kj::ArrayPtr scratchSpace); }; kj::Promise AsyncMessageReader::read(kj::AsyncInputStream& inputStream, kj::ArrayPtr scratchSpace) { return inputStream.tryRead(firstWord, sizeof(firstWord), sizeof(firstWord)) .then([this,&inputStream,KJ_CPCAP(scratchSpace)](size_t n) mutable -> kj::Promise { if (n == 0) { return false; } else if (n < sizeof(firstWord)) { // EOF in first word. kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "Premature EOF.")); return false; } return readAfterFirstWord(inputStream, scratchSpace).then([]() { return true; }); }); } kj::Promise> AsyncMessageReader::readWithFds( kj::AsyncCapabilityStream& inputStream, kj::ArrayPtr fds, kj::ArrayPtr scratchSpace) { return inputStream.tryReadWithFds(firstWord, sizeof(firstWord), sizeof(firstWord), fds.begin(), fds.size()) .then([this,&inputStream,KJ_CPCAP(scratchSpace)] (kj::AsyncCapabilityStream::ReadResult result) mutable -> kj::Promise> { if (result.byteCount == 0) { return kj::Maybe(nullptr); } else if (result.byteCount < sizeof(firstWord)) { // EOF in first word. kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "Premature EOF.")); return kj::Maybe(nullptr); } return readAfterFirstWord(inputStream, scratchSpace) .then([result]() -> kj::Maybe { return result.capCount; }); }); } kj::Promise AsyncMessageReader::readAfterFirstWord(kj::AsyncInputStream& inputStream, kj::ArrayPtr scratchSpace) { if (segmentCount() == 0) { firstWord[1].set(0); } // Reject messages with too many segments for security reasons. KJ_REQUIRE(segmentCount() < 512, "Message has too many segments.") { return kj::READY_NOW; // exception will be propagated } if (segmentCount() > 1) { // Read sizes for all segments except the first. Include padding if necessary. moreSizes = kj::heapArray<_::WireValue>(segmentCount() & ~1); return inputStream.read(moreSizes.begin(), moreSizes.size() * sizeof(moreSizes[0])) .then([this,&inputStream,KJ_CPCAP(scratchSpace)]() mutable { return readSegments(inputStream, scratchSpace); }); } else { return readSegments(inputStream, scratchSpace); } } kj::Promise AsyncMessageReader::readSegments(kj::AsyncInputStream& inputStream, kj::ArrayPtr scratchSpace) { size_t totalWords = segment0Size(); if (segmentCount() > 1) { for (uint i = 0; i < segmentCount() - 1; i++) { totalWords += moreSizes[i].get(); } } // Don't accept a message which the receiver couldn't possibly traverse without hitting the // traversal limit. Without this check, a malicious client could transmit a very large segment // size to make the receiver allocate excessive space and possibly crash. KJ_REQUIRE(totalWords <= getOptions().traversalLimitInWords, "Message is too large. To increase the limit on the receiving end, see " "capnp::ReaderOptions.") { return kj::READY_NOW; // exception will be propagated } if (scratchSpace.size() < totalWords) { // TODO(perf): Consider allocating each segment as a separate chunk to reduce memory // fragmentation. ownedSpace = kj::heapArray(totalWords); scratchSpace = ownedSpace; } segmentStarts = kj::heapArray(segmentCount()); segmentStarts[0] = scratchSpace.begin(); if (segmentCount() > 1) { size_t offset = segment0Size(); for (uint i = 1; i < segmentCount(); i++) { segmentStarts[i] = scratchSpace.begin() + offset; offset += moreSizes[i-1].get(); } } return inputStream.read(scratchSpace.begin(), totalWords * sizeof(word)); } } // namespace kj::Promise> readMessage( kj::AsyncInputStream& input, ReaderOptions options, kj::ArrayPtr scratchSpace) { auto reader = kj::heap(options); auto promise = reader->read(input, scratchSpace); return promise.then([reader = kj::mv(reader)](bool success) mutable -> kj::Own { if (!success) { kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "Premature EOF.")); } return kj::mv(reader); }); } kj::Promise>> tryReadMessage( kj::AsyncInputStream& input, ReaderOptions options, kj::ArrayPtr scratchSpace) { auto reader = kj::heap(options); auto promise = reader->read(input, scratchSpace); return promise.then([reader = kj::mv(reader)](bool success) mutable -> kj::Maybe> { if (success) { return kj::mv(reader); } else { return nullptr; } }); } kj::Promise readMessage( kj::AsyncCapabilityStream& input, kj::ArrayPtr fdSpace, ReaderOptions options, kj::ArrayPtr scratchSpace) { auto reader = kj::heap(options); auto promise = reader->readWithFds(input, fdSpace, scratchSpace); return promise.then([reader = kj::mv(reader), fdSpace](kj::Maybe nfds) mutable -> MessageReaderAndFds { KJ_IF_MAYBE(n, nfds) { return { kj::mv(reader), fdSpace.slice(0, *n) }; } else { kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "Premature EOF.")); return { kj::mv(reader), nullptr }; } }); } kj::Promise> tryReadMessage( kj::AsyncCapabilityStream& input, kj::ArrayPtr fdSpace, ReaderOptions options, kj::ArrayPtr scratchSpace) { auto reader = kj::heap(options); auto promise = reader->readWithFds(input, fdSpace, scratchSpace); return promise.then([reader = kj::mv(reader), fdSpace](kj::Maybe nfds) mutable -> kj::Maybe { KJ_IF_MAYBE(n, nfds) { return MessageReaderAndFds { kj::mv(reader), fdSpace.slice(0, *n) }; } else { return nullptr; } }); } // ======================================================================================= namespace { struct WriteArrays { // Holds arrays that must remain valid until a write completes. kj::Array<_::WireValue> table; kj::Array> pieces; }; template kj::Promise writeMessageImpl(kj::ArrayPtr> segments, WriteFunc&& writeFunc) { KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message."); WriteArrays arrays; arrays.table = kj::heapArray<_::WireValue>((segments.size() + 2) & ~size_t(1)); // We write the segment count - 1 because this makes the first word zero for single-segment // messages, improving compression. We don't bother doing this with segment sizes because // one-word segments are rare anyway. arrays.table[0].set(segments.size() - 1); for (uint i = 0; i < segments.size(); i++) { arrays.table[i + 1].set(segments[i].size()); } if (segments.size() % 2 == 0) { // Set padding byte. arrays.table[segments.size() + 1].set(0); } arrays.pieces = kj::heapArray>(segments.size() + 1); arrays.pieces[0] = arrays.table.asBytes(); for (uint i = 0; i < segments.size(); i++) { arrays.pieces[i + 1] = segments[i].asBytes(); } auto promise = writeFunc(arrays.pieces); // Make sure the arrays aren't freed until the write completes. return promise.then(kj::mvCapture(arrays, [](WriteArrays&&) {})); } } // namespace kj::Promise writeMessage(kj::AsyncOutputStream& output, kj::ArrayPtr> segments) { return writeMessageImpl(segments, [&](kj::ArrayPtr> pieces) { return output.write(pieces); }); } kj::Promise writeMessage(kj::AsyncCapabilityStream& output, kj::ArrayPtr fds, kj::ArrayPtr> segments) { return writeMessageImpl(segments, [&](kj::ArrayPtr> pieces) { return output.writeWithFds(pieces[0], pieces.slice(1, pieces.size()), fds); }); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/capability.c++0000644000175000017500000010765213650101756021272 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_PRIVATE #include "capability.h" #include "message.h" #include "arena.h" #include #include #include #include #include "generated-header-support.h" namespace capnp { namespace _ { void setGlobalBrokenCapFactoryForLayoutCpp(BrokenCapFactory& factory); // Defined in layout.c++. } // namespace _ namespace { static kj::Own newNullCap(); class BrokenCapFactoryImpl: public _::BrokenCapFactory { public: kj::Own newBrokenCap(kj::StringPtr description) override { return capnp::newBrokenCap(description); } kj::Own newNullCap() override { return capnp::newNullCap(); } }; static BrokenCapFactoryImpl brokenCapFactory; } // namespace ClientHook::ClientHook() { setGlobalBrokenCapFactoryForLayoutCpp(brokenCapFactory); } // ======================================================================================= Capability::Client::Client(decltype(nullptr)) : hook(newNullCap()) {} Capability::Client::Client(kj::Exception&& exception) : hook(newBrokenCap(kj::mv(exception))) {} kj::Promise> Capability::Client::getFd() { auto fd = hook->getFd(); if (fd != nullptr) { return fd; } else KJ_IF_MAYBE(promise, hook->whenMoreResolved()) { return promise->attach(hook->addRef()).then([](kj::Own newHook) { return Client(kj::mv(newHook)).getFd(); }); } else { return kj::Maybe(nullptr); } } kj::Maybe> Capability::Server::shortenPath() { return nullptr; } Capability::Server::DispatchCallResult Capability::Server::internalUnimplemented( const char* actualInterfaceName, uint64_t requestedTypeId) { return { KJ_EXCEPTION(UNIMPLEMENTED, "Requested interface not implemented.", actualInterfaceName, requestedTypeId), false }; } Capability::Server::DispatchCallResult Capability::Server::internalUnimplemented( const char* interfaceName, uint64_t typeId, uint16_t methodId) { return { KJ_EXCEPTION(UNIMPLEMENTED, "Method not implemented.", interfaceName, typeId, methodId), false }; } kj::Promise Capability::Server::internalUnimplemented( const char* interfaceName, const char* methodName, uint64_t typeId, uint16_t methodId) { return KJ_EXCEPTION(UNIMPLEMENTED, "Method not implemented.", interfaceName, typeId, methodName, methodId); } ResponseHook::~ResponseHook() noexcept(false) {} kj::Promise ClientHook::whenResolved() { KJ_IF_MAYBE(promise, whenMoreResolved()) { return promise->then([](kj::Own&& resolution) { return resolution->whenResolved(); }); } else { return kj::READY_NOW; } } kj::Promise Capability::Client::whenResolved() { return hook->whenResolved().attach(hook->addRef()); } // ======================================================================================= static inline uint firstSegmentSize(kj::Maybe sizeHint) { KJ_IF_MAYBE(s, sizeHint) { return s->wordCount; } else { return SUGGESTED_FIRST_SEGMENT_WORDS; } } class LocalResponse final: public ResponseHook, public kj::Refcounted { public: LocalResponse(kj::Maybe sizeHint) : message(firstSegmentSize(sizeHint)) {} MallocMessageBuilder message; }; class LocalCallContext final: public CallContextHook, public kj::Refcounted { public: LocalCallContext(kj::Own&& request, kj::Own clientRef, kj::Own> cancelAllowedFulfiller) : request(kj::mv(request)), clientRef(kj::mv(clientRef)), cancelAllowedFulfiller(kj::mv(cancelAllowedFulfiller)) {} AnyPointer::Reader getParams() override { KJ_IF_MAYBE(r, request) { return r->get()->getRoot(); } else { KJ_FAIL_REQUIRE("Can't call getParams() after releaseParams()."); } } void releaseParams() override { request = nullptr; } AnyPointer::Builder getResults(kj::Maybe sizeHint) override { if (response == nullptr) { auto localResponse = kj::refcounted(sizeHint); responseBuilder = localResponse->message.getRoot(); response = Response(responseBuilder.asReader(), kj::mv(localResponse)); } return responseBuilder; } kj::Promise tailCall(kj::Own&& request) override { auto result = directTailCall(kj::mv(request)); KJ_IF_MAYBE(f, tailCallPipelineFulfiller) { f->get()->fulfill(AnyPointer::Pipeline(kj::mv(result.pipeline))); } return kj::mv(result.promise); } ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own&& request) override { KJ_REQUIRE(response == nullptr, "Can't call tailCall() after initializing the results struct."); auto promise = request->send(); auto voidPromise = promise.then([this](Response&& tailResponse) { response = kj::mv(tailResponse); }); return { kj::mv(voidPromise), PipelineHook::from(kj::mv(promise)) }; } kj::Promise onTailCall() override { auto paf = kj::newPromiseAndFulfiller(); tailCallPipelineFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } void allowCancellation() override { cancelAllowedFulfiller->fulfill(); } kj::Own addRef() override { return kj::addRef(*this); } kj::Maybe> request; kj::Maybe> response; AnyPointer::Builder responseBuilder = nullptr; // only valid if `response` is non-null kj::Own clientRef; kj::Maybe>> tailCallPipelineFulfiller; kj::Own> cancelAllowedFulfiller; }; class LocalRequest final: public RequestHook { public: inline LocalRequest(uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint, kj::Own client) : message(kj::heap(firstSegmentSize(sizeHint))), interfaceId(interfaceId), methodId(methodId), client(kj::mv(client)) {} RemotePromise send() override { KJ_REQUIRE(message.get() != nullptr, "Already called send() on this request."); auto cancelPaf = kj::newPromiseAndFulfiller(); auto context = kj::refcounted( kj::mv(message), client->addRef(), kj::mv(cancelPaf.fulfiller)); auto promiseAndPipeline = client->call(interfaceId, methodId, kj::addRef(*context)); // We have to make sure the call is not canceled unless permitted. We need to fork the promise // so that if the client drops their copy, the promise isn't necessarily canceled. auto forked = promiseAndPipeline.promise.fork(); // We daemonize one branch, but only after joining it with the promise that fires if // cancellation is allowed. forked.addBranch() .attach(kj::addRef(*context)) .exclusiveJoin(kj::mv(cancelPaf.promise)) .detach([](kj::Exception&&) {}); // ignore exceptions // Now the other branch returns the response from the context. auto promise = forked.addBranch().then(kj::mvCapture(context, [](kj::Own&& context) { context->getResults(MessageSize { 0, 0 }); // force response allocation return kj::mv(KJ_ASSERT_NONNULL(context->response)); })); // We return the other branch. return RemotePromise( kj::mv(promise), AnyPointer::Pipeline(kj::mv(promiseAndPipeline.pipeline))); } kj::Promise sendStreaming() override { // We don't do any special handling of streaming in RequestHook for local requests, because // there is no latency to compensate for between the client and server in this case. return send().ignoreResult(); } const void* getBrand() override { return nullptr; } kj::Own message; private: uint64_t interfaceId; uint16_t methodId; kj::Own client; }; // ======================================================================================= // Call queues // // These classes handle pipelining in the case where calls need to be queued in-memory until some // local operation completes. class QueuedPipeline final: public PipelineHook, public kj::Refcounted { // A PipelineHook which simply queues calls while waiting for a PipelineHook to which to forward // them. public: QueuedPipeline(kj::Promise>&& promiseParam) : promise(promiseParam.fork()), selfResolutionOp(promise.addBranch().then([this](kj::Own&& inner) { redirect = kj::mv(inner); }, [this](kj::Exception&& exception) { redirect = newBrokenPipeline(kj::mv(exception)); }).eagerlyEvaluate(nullptr)) {} kj::Own addRef() override { return kj::addRef(*this); } kj::Own getPipelinedCap(kj::ArrayPtr ops) override { auto copy = kj::heapArrayBuilder(ops.size()); for (auto& op: ops) { copy.add(op); } return getPipelinedCap(copy.finish()); } kj::Own getPipelinedCap(kj::Array&& ops) override; private: kj::ForkedPromise> promise; kj::Maybe> redirect; // Once the promise resolves, this will become non-null and point to the underlying object. kj::Promise selfResolutionOp; // Represents the operation which will set `redirect` when possible. kj::HashMap, kj::Own> clientMap; // If the same pipelined cap is requested twice, we have to return the same object. This is // necessary because each ClientHook we create is a QueuedClient which queues up calls. If we // return a new one each time, there will be several queues, and ordering of calls will be lost // between the queues. // // One case where this is particularly problematic is with promises resolved over RPC. Consider // this case: // // * Alice holds a promise capability P pointing towards Bob. // * Bob makes a call Q on an object hosted by Alice. // * Without waiting for Q to complete, Bob obtains a pipelined-promise capability for Q's // eventual result, P2. // * Alice invokes a method M on P. The call is sent to Bob. // * Bob resolves Alice's original promise P to P2. // * Alice receives a Resolve message from Bob resolving P to Q's eventual result. // * As a result, Alice calls getPipelinedCap() on the QueuedPipeline for Q's result, which // returns a QueuedClient for that result, which we'll call QR1. // * Alice also sends a Disembargo to Bob. // * Alice calls a method M2 on P. This call is blocked locally waiting for the disembargo to // complete. // * Bob receives Alice's first method call, M. Since it's addressed to P, which later resolved // to Q's result, Bob reflects the call back to Alice. // * Alice receives the reflected call, which is addressed to Q's result. // * Alice calls getPipelinedCap() on the QueuedPipeline for Q's result, which returns a // QueuedClient for that result, which we'll call QR2. // * Alice enqueues the call M on QR2. // * Bob receives Alice's Disembargo message, and reflects it back. // * Alices receives the Disembrago. // * Alice unblocks the method cgall M2, which had been blocked on the embargo. // * The call M2 is then equeued onto QR1. // * Finally, the call Q completes. // * This causes QR1 and QR2 to resolve to their final destinations. But if QR1 and QR2 are // separate objects, then one of them must resolve first. QR1 was created first, so naturally // it resolves first, followed by QR2. // * Because QR1 resolves first, method call M2 is delivered first. // * QR2 resolves second, so method call M1 is delivered next. // * THIS IS THE WRONG ORDER! // // In order to avoid this problem, it's necessary for QR1 and QR2 to be the same object, so that // they share the same call queue. In this case, M2 is correctly enqueued onto QR2 *after* M1 was // enqueued on QR1, and so the method calls are delivered in the correct order. }; class QueuedClient final: public ClientHook, public kj::Refcounted { // A ClientHook which simply queues calls while waiting for a ClientHook to which to forward // them. public: QueuedClient(kj::Promise>&& promiseParam) : promise(promiseParam.fork()), selfResolutionOp(promise.addBranch().then([this](kj::Own&& inner) { redirect = kj::mv(inner); }, [this](kj::Exception&& exception) { redirect = newBrokenCap(kj::mv(exception)); }).eagerlyEvaluate(nullptr)), promiseForCallForwarding(promise.addBranch().fork()), promiseForClientResolution(promise.addBranch().fork()) {} Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { auto hook = kj::heap( interfaceId, methodId, sizeHint, kj::addRef(*this)); auto root = hook->message->getRoot(); return Request(root, kj::mv(hook)); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { // This is a bit complicated. We need to initiate this call later on. When we initiate the // call, we'll get a void promise for its completion and a pipeline object. Right now, we have // to produce a similar void promise and pipeline that will eventually be chained to those. // The problem is, these are two independent objects, but they both depend on the result of // one future call. // // So, we need to set up a continuation that will initiate the call later, then we need to // fork the promise for that continuation in order to send the completion promise and the // pipeline to their respective places. // // TODO(perf): Too much reference counting? Can we do better? Maybe a way to fork // Promise> into Tuple, Promise>? struct CallResultHolder: public kj::Refcounted { // Essentially acts as a refcounted \VoidPromiseAndPipeline, so that we can create a promise // for it and fork that promise. VoidPromiseAndPipeline content; // One branch of the fork will use content.promise, the other branch will use // content.pipeline. Neither branch will touch the other's piece. inline CallResultHolder(VoidPromiseAndPipeline&& content): content(kj::mv(content)) {} kj::Own addRef() { return kj::addRef(*this); } }; // Create a promise for the call initiation. kj::ForkedPromise> callResultPromise = promiseForCallForwarding.addBranch().then(kj::mvCapture(context, [=](kj::Own&& context, kj::Own&& client){ return kj::refcounted( client->call(interfaceId, methodId, kj::mv(context))); })).fork(); // Create a promise that extracts the pipeline from the call initiation, and construct our // QueuedPipeline to chain to it. auto pipelinePromise = callResultPromise.addBranch().then( [](kj::Own&& callResult){ return kj::mv(callResult->content.pipeline); }); auto pipeline = kj::refcounted(kj::mv(pipelinePromise)); // Create a promise that simply chains to the void promise produced by the call initiation. auto completionPromise = callResultPromise.addBranch().then( [](kj::Own&& callResult){ return kj::mv(callResult->content.promise); }); // OK, now we can actually return our thing. return VoidPromiseAndPipeline { kj::mv(completionPromise), kj::mv(pipeline) }; } kj::Maybe getResolved() override { KJ_IF_MAYBE(inner, redirect) { return **inner; } else { return nullptr; } } kj::Maybe>> whenMoreResolved() override { return promiseForClientResolution.addBranch(); } kj::Own addRef() override { return kj::addRef(*this); } const void* getBrand() override { return nullptr; } kj::Maybe getFd() override { KJ_IF_MAYBE(r, redirect) { return r->get()->getFd(); } else { return nullptr; } } private: typedef kj::ForkedPromise> ClientHookPromiseFork; kj::Maybe> redirect; // Once the promise resolves, this will become non-null and point to the underlying object. ClientHookPromiseFork promise; // Promise that resolves when we have a new ClientHook to forward to. // // This fork shall only have three branches: `selfResolutionOp`, `promiseForCallForwarding`, and // `promiseForClientResolution`, in that order. kj::Promise selfResolutionOp; // Represents the operation which will set `redirect` when possible. ClientHookPromiseFork promiseForCallForwarding; // When this promise resolves, each queued call will be forwarded to the real client. This needs // to occur *before* any 'whenMoreResolved()' promises resolve, because we want to make sure // previously-queued calls are delivered before any new calls made in response to the resolution. ClientHookPromiseFork promiseForClientResolution; // whenMoreResolved() returns forks of this promise. These must resolve *after* queued calls // have been initiated (so that any calls made in the whenMoreResolved() handler are correctly // delivered after calls made earlier), but *before* any queued calls return (because it might // confuse the application if a queued call returns before the capability on which it was made // resolves). Luckily, we know that queued calls will involve, at the very least, an // eventLoop.evalLater. }; kj::Own QueuedPipeline::getPipelinedCap(kj::Array&& ops) { KJ_IF_MAYBE(r, redirect) { return r->get()->getPipelinedCap(kj::mv(ops)); } else { return clientMap.findOrCreate(ops.asPtr(), [&]() { auto clientPromise = promise.addBranch() .then([ops = KJ_MAP(op, ops) { return op; }](kj::Own pipeline) { return pipeline->getPipelinedCap(kj::mv(ops)); }); return kj::HashMap, kj::Own>::Entry { kj::mv(ops), kj::refcounted(kj::mv(clientPromise)) }; })->addRef(); } } // ======================================================================================= class LocalPipeline final: public PipelineHook, public kj::Refcounted { public: inline LocalPipeline(kj::Own&& contextParam) : context(kj::mv(contextParam)), results(context->getResults(MessageSize { 0, 0 })) {} kj::Own addRef() { return kj::addRef(*this); } kj::Own getPipelinedCap(kj::ArrayPtr ops) { return results.getPipelinedCap(ops); } private: kj::Own context; AnyPointer::Reader results; }; class LocalClient final: public ClientHook, public kj::Refcounted { public: LocalClient(kj::Own&& serverParam) : server(kj::mv(serverParam)) { server->thisHook = this; startResolveTask(); } LocalClient(kj::Own&& serverParam, _::CapabilityServerSetBase& capServerSet, void* ptr) : server(kj::mv(serverParam)), capServerSet(&capServerSet), ptr(ptr) { server->thisHook = this; startResolveTask(); } ~LocalClient() noexcept(false) { server->thisHook = nullptr; } Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { auto hook = kj::heap( interfaceId, methodId, sizeHint, kj::addRef(*this)); auto root = hook->message->getRoot(); return Request(root, kj::mv(hook)); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { auto contextPtr = context.get(); // We don't want to actually dispatch the call synchronously, because we don't want the callee // to have any side effects before the promise is returned to the caller. This helps avoid // race conditions. // // So, we do an evalLater() here. // // Note also that QueuedClient depends on this evalLater() to ensure that pipelined calls don't // complete before 'whenMoreResolved()' promises resolve. auto promise = kj::evalLater([this,interfaceId,methodId,contextPtr]() { if (blocked) { return kj::newAdaptedPromise, BlockedCall>( *this, interfaceId, methodId, *contextPtr); } else { return callInternal(interfaceId, methodId, *contextPtr); } }).attach(kj::addRef(*this)); // We have to fork this promise for the pipeline to receive a copy of the answer. auto forked = promise.fork(); auto pipelinePromise = forked.addBranch().then(kj::mvCapture(context->addRef(), [=](kj::Own&& context) -> kj::Own { context->releaseParams(); return kj::refcounted(kj::mv(context)); })); auto tailPipelinePromise = context->onTailCall().then([](AnyPointer::Pipeline&& pipeline) { return kj::mv(pipeline.hook); }); pipelinePromise = pipelinePromise.exclusiveJoin(kj::mv(tailPipelinePromise)); auto completionPromise = forked.addBranch().attach(kj::mv(context)); return VoidPromiseAndPipeline { kj::mv(completionPromise), kj::refcounted(kj::mv(pipelinePromise)) }; } kj::Maybe getResolved() override { return resolved.map([](kj::Own& hook) -> ClientHook& { return *hook; }); } kj::Maybe>> whenMoreResolved() override { KJ_IF_MAYBE(r, resolved) { return kj::Promise>(r->get()->addRef()); } else KJ_IF_MAYBE(t, resolveTask) { return t->addBranch().then([this]() { return KJ_ASSERT_NONNULL(resolved)->addRef(); }); } else { return nullptr; } } kj::Own addRef() override { return kj::addRef(*this); } static const uint BRAND; // Value is irrelevant; used for pointer. const void* getBrand() override { return &BRAND; } kj::Maybe> getLocalServer(_::CapabilityServerSetBase& capServerSet) { // If this is a local capability created through `capServerSet`, return the underlying Server. // Otherwise, return nullptr. Default implementation (which everyone except LocalClient should // use) always returns nullptr. if (this->capServerSet == &capServerSet) { if (blocked) { // If streaming calls are in-flight, it could be the case that they were originally sent // over RPC and reflected back, before the capability had resolved to a local object. In // that case, the client may already perceive these calls as "done" because the RPC // implementation caused the client promise to resolve early. However, the capability is // now local, and the app is trying to break through the LocalClient wrapper and access // the server directly, bypassing the stream queue. Since the app thinks that all // previous calls already completed, it may then try to queue a new call directly on the // server, jumping the queue. // // We can solve this by delaying getLocalServer() until all current streaming calls have // finished. Note that if a new streaming call is started *after* this point, we need not // worry about that, because in this case it is presumably a local call and the caller // won't be informed of completion until the call actually does complete. Thus the caller // is well-aware that this call is still in-flight. // // However, the app still cannot assume that there aren't multiple clients, perhaps even // a malicious client that tries to send stream requests that overlap with the app's // direct use of the server... so it's up to the app to check for and guard against // concurrent calls after using getLocalServer(). return kj::newAdaptedPromise, BlockedCall>(*this) .then([this]() { return ptr; }); } else { return kj::Promise(ptr); } } else { return nullptr; } } kj::Maybe getFd() override { return server->getFd(); } private: kj::Own server; _::CapabilityServerSetBase* capServerSet = nullptr; void* ptr = nullptr; kj::Maybe> resolveTask; kj::Maybe> resolved; void startResolveTask() { resolveTask = server->shortenPath().map([this](kj::Promise promise) { return promise.then([this](Capability::Client&& cap) { auto hook = ClientHook::from(kj::mv(cap)); resolved = hook->addRef(); }).fork(); }); } class BlockedCall { public: BlockedCall(kj::PromiseFulfiller>& fulfiller, LocalClient& client, uint64_t interfaceId, uint16_t methodId, CallContextHook& context) : fulfiller(fulfiller), client(client), interfaceId(interfaceId), methodId(methodId), context(context), prev(client.blockedCallsEnd) { *prev = *this; client.blockedCallsEnd = &next; } BlockedCall(kj::PromiseFulfiller>& fulfiller, LocalClient& client) : fulfiller(fulfiller), client(client), prev(client.blockedCallsEnd) { *prev = *this; client.blockedCallsEnd = &next; } ~BlockedCall() noexcept(false) { unlink(); } void unblock() { unlink(); KJ_IF_MAYBE(c, context) { fulfiller.fulfill(kj::evalNow([&]() { return client.callInternal(interfaceId, methodId, *c); })); } else { // This is just a barrier. fulfiller.fulfill(kj::READY_NOW); } } private: kj::PromiseFulfiller>& fulfiller; LocalClient& client; uint64_t interfaceId; uint16_t methodId; kj::Maybe context; kj::Maybe next; kj::Maybe* prev; void unlink() { if (prev != nullptr) { *prev = next; KJ_IF_MAYBE(n, next) { n->prev = prev; } else { client.blockedCallsEnd = prev; } prev = nullptr; } } }; class BlockingScope { public: BlockingScope(LocalClient& client): client(client) { client.blocked = true; } BlockingScope(): client(nullptr) {} BlockingScope(BlockingScope&& other): client(other.client) { other.client = nullptr; } KJ_DISALLOW_COPY(BlockingScope); ~BlockingScope() noexcept(false) { KJ_IF_MAYBE(c, client) { c->unblock(); } } private: kj::Maybe client; }; bool blocked = false; kj::Maybe brokenException; kj::Maybe blockedCalls; kj::Maybe* blockedCallsEnd = &blockedCalls; void unblock() { blocked = false; while (!blocked) { KJ_IF_MAYBE(t, blockedCalls) { t->unblock(); } else { break; } } } kj::Promise callInternal(uint64_t interfaceId, uint16_t methodId, CallContextHook& context) { KJ_ASSERT(!blocked); KJ_IF_MAYBE(e, brokenException) { // Previous streaming call threw, so everything fails from now on. return kj::cp(*e); } auto result = server->dispatchCall(interfaceId, methodId, CallContext(context)); if (result.isStreaming) { return result.promise .catch_([this](kj::Exception&& e) { brokenException = kj::cp(e); kj::throwRecoverableException(kj::mv(e)); }).attach(BlockingScope(*this)); } else { return kj::mv(result.promise); } } }; const uint LocalClient::BRAND = 0; kj::Own Capability::Client::makeLocalClient(kj::Own&& server) { return kj::refcounted(kj::mv(server)); } kj::Own newLocalPromiseClient(kj::Promise>&& promise) { return kj::refcounted(kj::mv(promise)); } kj::Own newLocalPromisePipeline(kj::Promise>&& promise) { return kj::refcounted(kj::mv(promise)); } // ======================================================================================= namespace { class BrokenPipeline final: public PipelineHook, public kj::Refcounted { public: BrokenPipeline(const kj::Exception& exception): exception(exception) {} kj::Own addRef() override { return kj::addRef(*this); } kj::Own getPipelinedCap(kj::ArrayPtr ops) override; private: kj::Exception exception; }; class BrokenRequest final: public RequestHook { public: BrokenRequest(const kj::Exception& exception, kj::Maybe sizeHint) : exception(exception), message(firstSegmentSize(sizeHint)) {} RemotePromise send() override { return RemotePromise(kj::cp(exception), AnyPointer::Pipeline(kj::refcounted(exception))); } kj::Promise sendStreaming() override { return kj::cp(exception); } const void* getBrand() override { return nullptr; } kj::Exception exception; MallocMessageBuilder message; }; class BrokenClient final: public ClientHook, public kj::Refcounted { public: BrokenClient(const kj::Exception& exception, bool resolved, const void* brand) : exception(exception), resolved(resolved), brand(brand) {} BrokenClient(const kj::StringPtr description, bool resolved, const void* brand) : exception(kj::Exception::Type::FAILED, "", 0, kj::str(description)), resolved(resolved), brand(brand) {} Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { return newBrokenRequest(kj::cp(exception), sizeHint); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { return VoidPromiseAndPipeline { kj::cp(exception), kj::refcounted(exception) }; } kj::Maybe getResolved() override { return nullptr; } kj::Maybe>> whenMoreResolved() override { if (resolved) { return nullptr; } else { return kj::Promise>(kj::cp(exception)); } } kj::Own addRef() override { return kj::addRef(*this); } const void* getBrand() override { return brand; } kj::Maybe getFd() override { return nullptr; } private: kj::Exception exception; bool resolved; const void* brand; }; kj::Own BrokenPipeline::getPipelinedCap(kj::ArrayPtr ops) { return kj::refcounted(exception, false, &ClientHook::BROKEN_CAPABILITY_BRAND); } kj::Own newNullCap() { // A null capability, unlike other broken capabilities, is considered resolved. return kj::refcounted("Called null capability.", true, &ClientHook::NULL_CAPABILITY_BRAND); } } // namespace kj::Own newBrokenCap(kj::StringPtr reason) { return kj::refcounted(reason, false, &ClientHook::BROKEN_CAPABILITY_BRAND); } kj::Own newBrokenCap(kj::Exception&& reason) { return kj::refcounted(kj::mv(reason), false, &ClientHook::BROKEN_CAPABILITY_BRAND); } kj::Own newBrokenPipeline(kj::Exception&& reason) { return kj::refcounted(kj::mv(reason)); } Request newBrokenRequest( kj::Exception&& reason, kj::Maybe sizeHint) { auto hook = kj::heap(kj::mv(reason), sizeHint); auto root = hook->message.getRoot(); return Request(root, kj::mv(hook)); } // ======================================================================================= ReaderCapabilityTable::ReaderCapabilityTable( kj::Array>> table) : table(kj::mv(table)) { setGlobalBrokenCapFactoryForLayoutCpp(brokenCapFactory); } kj::Maybe> ReaderCapabilityTable::extractCap(uint index) { if (index < table.size()) { return table[index].map([](kj::Own& cap) { return cap->addRef(); }); } else { return nullptr; } } BuilderCapabilityTable::BuilderCapabilityTable() { setGlobalBrokenCapFactoryForLayoutCpp(brokenCapFactory); } kj::Maybe> BuilderCapabilityTable::extractCap(uint index) { if (index < table.size()) { return table[index].map([](kj::Own& cap) { return cap->addRef(); }); } else { return nullptr; } } uint BuilderCapabilityTable::injectCap(kj::Own&& cap) { uint result = table.size(); table.add(kj::mv(cap)); return result; } void BuilderCapabilityTable::dropCap(uint index) { KJ_ASSERT(index < table.size(), "Invalid capability descriptor in message.") { return; } table[index] = nullptr; } // ======================================================================================= // CapabilityServerSet namespace _ { // private Capability::Client CapabilityServerSetBase::addInternal( kj::Own&& server, void* ptr) { return Capability::Client(kj::refcounted(kj::mv(server), *this, ptr)); } kj::Promise CapabilityServerSetBase::getLocalServerInternal(Capability::Client& client) { ClientHook* hook = client.hook.get(); // Get the most-resolved-so-far version of the hook. for (;;) { KJ_IF_MAYBE(h, hook->getResolved()) { hook = h; } else { break; } } // Try to unwrap that. if (hook->getBrand() == &LocalClient::BRAND) { KJ_IF_MAYBE(promise, kj::downcast(*hook).getLocalServer(*this)) { // This is definitely a member of our set and will resolve to non-null. We just have to wait // for any existing streaming calls to complete. return kj::mv(*promise); } } // OK, the capability isn't part of this set. KJ_IF_MAYBE(p, hook->whenMoreResolved()) { // This hook is an unresolved promise. It might resolve eventually to a local server, so wait // for it. return p->attach(hook->addRef()) .then([this](kj::Own&& resolved) { Capability::Client client(kj::mv(resolved)); return getLocalServerInternal(client); }); } else { // Cap is settled, so it definitely will never resolve to a member of this set. return kj::implicitCast(nullptr); } } } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/membrane.c++0000644000175000017500000005125313650101756020732 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "membrane.h" #include namespace capnp { namespace { static const char DUMMY = 0; static constexpr const void* MEMBRANE_BRAND = &DUMMY; kj::Own membrane(kj::Own inner, MembranePolicy& policy, bool reverse); class MembraneCapTableReader final: public _::CapTableReader { public: MembraneCapTableReader(MembranePolicy& policy, bool reverse) : policy(policy), reverse(reverse) {} AnyPointer::Reader imbue(AnyPointer::Reader reader) { return AnyPointer::Reader(imbue( _::PointerHelpers::getInternalReader(kj::mv(reader)))); } _::PointerReader imbue(_::PointerReader reader) { KJ_REQUIRE(inner == nullptr, "can only call this once"); inner = reader.getCapTable(); return reader.imbue(this); } _::StructReader imbue(_::StructReader reader) { KJ_REQUIRE(inner == nullptr, "can only call this once"); inner = reader.getCapTable(); return reader.imbue(this); } _::ListReader imbue(_::ListReader reader) { KJ_REQUIRE(inner == nullptr, "can only call this once"); inner = reader.getCapTable(); return reader.imbue(this); } kj::Maybe> extractCap(uint index) override { // The underlying message is inside the membrane, and we're pulling a cap out of it. Therefore, // we want to wrap the extracted capability in the membrane. return inner->extractCap(index).map([this](kj::Own&& cap) { return membrane(kj::mv(cap), policy, reverse); }); } private: _::CapTableReader* inner = nullptr; MembranePolicy& policy; bool reverse; }; class MembraneCapTableBuilder final: public _::CapTableBuilder { public: MembraneCapTableBuilder(MembranePolicy& policy, bool reverse) : policy(policy), reverse(reverse) {} AnyPointer::Builder imbue(AnyPointer::Builder builder) { KJ_REQUIRE(inner == nullptr, "can only call this once"); auto pointerBuilder = _::PointerHelpers::getInternalBuilder(kj::mv(builder)); inner = pointerBuilder.getCapTable(); return AnyPointer::Builder(pointerBuilder.imbue(this)); } AnyPointer::Builder unimbue(AnyPointer::Builder builder) { auto pointerBuilder = _::PointerHelpers::getInternalBuilder(kj::mv(builder)); KJ_REQUIRE(pointerBuilder.getCapTable() == this); return AnyPointer::Builder(pointerBuilder.imbue(inner)); } kj::Maybe> extractCap(uint index) override { // The underlying message is inside the membrane, and we're pulling a cap out of it. Therefore, // we want to wrap the extracted capability in the membrane. return inner->extractCap(index).map([this](kj::Own&& cap) { return membrane(kj::mv(cap), policy, reverse); }); } uint injectCap(kj::Own&& cap) override { // The underlying message is inside the membrane, and we're inserting a cap from outside into // it. Therefore we want to add a reverse membrane. return inner->injectCap(membrane(kj::mv(cap), policy, !reverse)); } void dropCap(uint index) override { inner->dropCap(index); } private: _::CapTableBuilder* inner = nullptr; MembranePolicy& policy; bool reverse; }; class MembranePipelineHook final: public PipelineHook, public kj::Refcounted { public: MembranePipelineHook( kj::Own&& inner, kj::Own&& policy, bool reverse) : inner(kj::mv(inner)), policy(kj::mv(policy)), reverse(reverse) {} kj::Own addRef() override { return kj::addRef(*this); } kj::Own getPipelinedCap(kj::ArrayPtr ops) override { return membrane(inner->getPipelinedCap(ops), *policy, reverse); } kj::Own getPipelinedCap(kj::Array&& ops) override { return membrane(inner->getPipelinedCap(kj::mv(ops)), *policy, reverse); } private: kj::Own inner; kj::Own policy; bool reverse; }; class MembraneResponseHook final: public ResponseHook { public: MembraneResponseHook( kj::Own&& inner, kj::Own&& policy, bool reverse) : inner(kj::mv(inner)), policy(kj::mv(policy)), capTable(*this->policy, reverse) {} AnyPointer::Reader imbue(AnyPointer::Reader reader) { return capTable.imbue(reader); } private: kj::Own inner; kj::Own policy; MembraneCapTableReader capTable; }; class MembraneRequestHook final: public RequestHook { public: MembraneRequestHook(kj::Own&& inner, kj::Own&& policy, bool reverse) : inner(kj::mv(inner)), policy(kj::mv(policy)), reverse(reverse), capTable(*this->policy, reverse) {} static Request wrap( Request&& inner, MembranePolicy& policy, bool reverse) { AnyPointer::Builder builder = inner; auto innerHook = RequestHook::from(kj::mv(inner)); if (innerHook->getBrand() == MEMBRANE_BRAND) { auto& otherMembrane = kj::downcast(*innerHook); if (otherMembrane.policy.get() == &policy && otherMembrane.reverse == !reverse) { // Request that passed across the membrane one way is now passing back the other way. // Unwrap it rather than double-wrap it. builder = otherMembrane.capTable.unimbue(builder); return { builder, kj::mv(otherMembrane.inner) }; } } auto newHook = kj::heap(kj::mv(innerHook), policy.addRef(), reverse); builder = newHook->capTable.imbue(builder); return { builder, kj::mv(newHook) }; } static kj::Own wrap( kj::Own&& inner, MembranePolicy& policy, bool reverse) { if (inner->getBrand() == MEMBRANE_BRAND) { auto& otherMembrane = kj::downcast(*inner); if (otherMembrane.policy.get() == &policy && otherMembrane.reverse == !reverse) { // Request that passed across the membrane one way is now passing back the other way. // Unwrap it rather than double-wrap it. return kj::mv(otherMembrane.inner); } } return kj::heap(kj::mv(inner), policy.addRef(), reverse); } RemotePromise send() override { auto promise = inner->send(); auto newPipeline = AnyPointer::Pipeline(kj::refcounted( PipelineHook::from(kj::mv(promise)), policy->addRef(), reverse)); auto onRevoked = policy->onRevoked(); bool reverse = this->reverse; // for capture auto newPromise = promise.then(kj::mvCapture(policy, [reverse](kj::Own&& policy, Response&& response) { AnyPointer::Reader reader = response; auto newRespHook = kj::heap( ResponseHook::from(kj::mv(response)), policy->addRef(), reverse); reader = newRespHook->imbue(reader); return Response(reader, kj::mv(newRespHook)); })); KJ_IF_MAYBE(r, kj::mv(onRevoked)) { newPromise = newPromise.exclusiveJoin(r->then([]() -> Response { KJ_FAIL_REQUIRE("onRevoked() promise resolved; it should only reject"); })); } return RemotePromise(kj::mv(newPromise), kj::mv(newPipeline)); } kj::Promise sendStreaming() override { auto promise = inner->sendStreaming(); KJ_IF_MAYBE(r, policy->onRevoked()) { promise = promise.exclusiveJoin(r->then([]() { KJ_FAIL_REQUIRE("onRevoked() promise resolved; it should only reject"); })); } return promise; } const void* getBrand() override { return MEMBRANE_BRAND; } private: kj::Own inner; kj::Own policy; bool reverse; MembraneCapTableBuilder capTable; }; class MembraneCallContextHook final: public CallContextHook, public kj::Refcounted { public: MembraneCallContextHook(kj::Own&& inner, kj::Own&& policy, bool reverse) : inner(kj::mv(inner)), policy(kj::mv(policy)), reverse(reverse), paramsCapTable(*this->policy, reverse), resultsCapTable(*this->policy, reverse) {} AnyPointer::Reader getParams() override { KJ_REQUIRE(!releasedParams); KJ_IF_MAYBE(p, params) { return *p; } else { auto result = paramsCapTable.imbue(inner->getParams()); params = result; return result; } } void releaseParams() override { // Note that releaseParams() is idempotent -- it can be called multiple times. releasedParams = true; inner->releaseParams(); } AnyPointer::Builder getResults(kj::Maybe sizeHint) override { KJ_IF_MAYBE(r, results) { return *r; } else { auto result = resultsCapTable.imbue(inner->getResults(sizeHint)); results = result; return result; } } kj::Promise tailCall(kj::Own&& request) override { return inner->tailCall(MembraneRequestHook::wrap(kj::mv(request), *policy, !reverse)); } void allowCancellation() override { inner->allowCancellation(); } kj::Promise onTailCall() override { return inner->onTailCall().then([this](AnyPointer::Pipeline&& innerPipeline) { return AnyPointer::Pipeline(kj::refcounted( PipelineHook::from(kj::mv(innerPipeline)), policy->addRef(), reverse)); }); } ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own&& request) override { auto pair = inner->directTailCall( MembraneRequestHook::wrap(kj::mv(request), *policy, !reverse)); return { kj::mv(pair.promise), kj::refcounted(kj::mv(pair.pipeline), policy->addRef(), reverse) }; } kj::Own addRef() override { return kj::addRef(*this); } private: kj::Own inner; kj::Own policy; bool reverse; MembraneCapTableReader paramsCapTable; kj::Maybe params; bool releasedParams = false; MembraneCapTableBuilder resultsCapTable; kj::Maybe results; }; class MembraneHook final: public ClientHook, public kj::Refcounted { public: MembraneHook(kj::Own&& inner, kj::Own&& policyParam, bool reverse) : inner(kj::mv(inner)), policy(kj::mv(policyParam)), reverse(reverse) { KJ_IF_MAYBE(r, policy->onRevoked()) { revocationTask = r->eagerlyEvaluate([this](kj::Exception&& exception) { this->inner = newBrokenCap(kj::mv(exception)); }); } } static kj::Own wrap(ClientHook& cap, MembranePolicy& policy, bool reverse) { if (cap.getBrand() == MEMBRANE_BRAND) { auto& otherMembrane = kj::downcast(cap); auto& rootPolicy = policy.rootPolicy(); if (&otherMembrane.policy->rootPolicy() == &rootPolicy && otherMembrane.reverse == !reverse) { // Capability that passed across the membrane one way is now passing back the other way. // Unwrap it rather than double-wrap it. Capability::Client unwrapped(otherMembrane.inner->addRef()); return ClientHook::from( reverse ? rootPolicy.importInternal(kj::mv(unwrapped), *otherMembrane.policy, policy) : rootPolicy.exportExternal(kj::mv(unwrapped), *otherMembrane.policy, policy)); } } return ClientHook::from( reverse ? policy.importExternal(Capability::Client(cap.addRef())) : policy.exportInternal(Capability::Client(cap.addRef()))); } static kj::Own wrap(kj::Own cap, MembranePolicy& policy, bool reverse) { if (cap->getBrand() == MEMBRANE_BRAND) { auto& otherMembrane = kj::downcast(*cap); auto& rootPolicy = policy.rootPolicy(); if (&otherMembrane.policy->rootPolicy() == &rootPolicy && otherMembrane.reverse == !reverse) { // Capability that passed across the membrane one way is now passing back the other way. // Unwrap it rather than double-wrap it. Capability::Client unwrapped(otherMembrane.inner->addRef()); return ClientHook::from( reverse ? rootPolicy.importInternal(kj::mv(unwrapped), *otherMembrane.policy, policy) : rootPolicy.exportExternal(kj::mv(unwrapped), *otherMembrane.policy, policy)); } } return ClientHook::from( reverse ? policy.importExternal(Capability::Client(kj::mv(cap))) : policy.exportInternal(Capability::Client(kj::mv(cap)))); } Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { KJ_IF_MAYBE(r, resolved) { return r->get()->newCall(interfaceId, methodId, sizeHint); } auto redirect = reverse ? policy->outboundCall(interfaceId, methodId, Capability::Client(inner->addRef())) : policy->inboundCall(interfaceId, methodId, Capability::Client(inner->addRef())); KJ_IF_MAYBE(r, redirect) { // The policy says that *if* this capability points into the membrane, then we want to // redirect the call. However, if this capability is a promise, then it could resolve to // something outside the membrane later. We have to wait before we actually redirect, // otherwise behavior will differ depending on whether the promise is resolved. KJ_IF_MAYBE(p, whenMoreResolved()) { return newLocalPromiseClient(p->attach(addRef())) ->newCall(interfaceId, methodId, sizeHint); } return ClientHook::from(kj::mv(*r))->newCall(interfaceId, methodId, sizeHint); } else { // For pass-through calls, we don't worry about promises, because if the capability resolves // to something outside the membrane, then the call will pass back out of the membrane too. return MembraneRequestHook::wrap( inner->newCall(interfaceId, methodId, sizeHint), *policy, reverse); } } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { KJ_IF_MAYBE(r, resolved) { return r->get()->call(interfaceId, methodId, kj::mv(context)); } auto redirect = reverse ? policy->outboundCall(interfaceId, methodId, Capability::Client(inner->addRef())) : policy->inboundCall(interfaceId, methodId, Capability::Client(inner->addRef())); KJ_IF_MAYBE(r, redirect) { // The policy says that *if* this capability points into the membrane, then we want to // redirect the call. However, if this capability is a promise, then it could resolve to // something outside the membrane later. We have to wait before we actually redirect, // otherwise behavior will differ depending on whether the promise is resolved. KJ_IF_MAYBE(p, whenMoreResolved()) { return newLocalPromiseClient(p->attach(addRef())) ->call(interfaceId, methodId, kj::mv(context)); } return ClientHook::from(kj::mv(*r))->call(interfaceId, methodId, kj::mv(context)); } else { // !reverse because calls to the CallContext go in the opposite direction. auto result = inner->call(interfaceId, methodId, kj::refcounted(kj::mv(context), policy->addRef(), !reverse)); KJ_IF_MAYBE(r, policy->onRevoked()) { result.promise = result.promise.exclusiveJoin(kj::mv(*r)); } return { kj::mv(result.promise), kj::refcounted(kj::mv(result.pipeline), policy->addRef(), reverse) }; } } kj::Maybe getResolved() override { KJ_IF_MAYBE(r, resolved) { return **r; } KJ_IF_MAYBE(newInner, inner->getResolved()) { kj::Own newResolved = wrap(*newInner, *policy, reverse); ClientHook& result = *newResolved; resolved = kj::mv(newResolved); return result; } else { return nullptr; } } kj::Maybe>> whenMoreResolved() override { KJ_IF_MAYBE(r, resolved) { return kj::Promise>(r->get()->addRef()); } KJ_IF_MAYBE(promise, inner->whenMoreResolved()) { KJ_IF_MAYBE(r, policy->onRevoked()) { *promise = promise->exclusiveJoin(r->then([]() -> kj::Own { KJ_FAIL_REQUIRE("onRevoked() promise resolved; it should only reject"); })); } return promise->then([this](kj::Own&& newInner) { kj::Own newResolved = wrap(*newInner, *policy, reverse); if (resolved == nullptr) { resolved = newResolved->addRef(); } return newResolved; }); } else { return nullptr; } } kj::Own addRef() override { return kj::addRef(*this); } const void* getBrand() override { return MEMBRANE_BRAND; } kj::Maybe getFd() override { // We can't let FDs pass over membranes because we have no way to enforce the membrane policy // on them. If the MembranePolicy wishes to explicitly permit certain FDs to pass, it can // always do so by overriding the appropriate policy methods. return nullptr; } private: kj::Own inner; kj::Own policy; bool reverse; kj::Maybe> resolved; kj::Promise revocationTask = nullptr; }; kj::Own membrane(kj::Own inner, MembranePolicy& policy, bool reverse) { return MembraneHook::wrap(kj::mv(inner), policy, reverse); } } // namespace Capability::Client MembranePolicy::importExternal(Capability::Client external) { return Capability::Client(kj::refcounted( ClientHook::from(kj::mv(external)), addRef(), true)); } Capability::Client MembranePolicy::exportInternal(Capability::Client internal) { return Capability::Client(kj::refcounted( ClientHook::from(kj::mv(internal)), addRef(), false)); } Capability::Client MembranePolicy::importInternal( Capability::Client internal, MembranePolicy& exportPolicy, MembranePolicy& importPolicy) { return kj::mv(internal); } Capability::Client MembranePolicy::exportExternal( Capability::Client external, MembranePolicy& importPolicy, MembranePolicy& exportPolicy) { return kj::mv(external); } Capability::Client membrane(Capability::Client inner, kj::Own policy) { return Capability::Client(membrane( ClientHook::from(kj::mv(inner)), *policy, false)); } Capability::Client reverseMembrane(Capability::Client inner, kj::Own policy) { return Capability::Client(membrane( ClientHook::from(kj::mv(inner)), *policy, true)); } namespace _ { // private _::OrphanBuilder copyOutOfMembrane(PointerReader from, Orphanage to, kj::Own policy, bool reverse) { MembraneCapTableReader capTable(*policy, reverse); return _::OrphanBuilder::copy( OrphanageInternal::getArena(to), OrphanageInternal::getCapTable(to), capTable.imbue(from)); } _::OrphanBuilder copyOutOfMembrane(StructReader from, Orphanage to, kj::Own policy, bool reverse) { MembraneCapTableReader capTable(*policy, reverse); return _::OrphanBuilder::copy( OrphanageInternal::getArena(to), OrphanageInternal::getCapTable(to), capTable.imbue(from)); } _::OrphanBuilder copyOutOfMembrane(ListReader from, Orphanage to, kj::Own policy, bool reverse) { MembraneCapTableReader capTable(*policy, reverse); return _::OrphanBuilder::copy( OrphanageInternal::getArena(to), OrphanageInternal::getCapTable(to), capTable.imbue(from)); } } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/dynamic-capability.c++0000644000175000017500000001060513650101756022703 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains the parts of dynamic.h that live in capnp-rpc.so. #include "dynamic.h" #include namespace capnp { DynamicCapability::Client DynamicCapability::Client::upcast(InterfaceSchema requestedSchema) { KJ_REQUIRE(schema.extends(requestedSchema), "Can't upcast to non-superclass.") {} return DynamicCapability::Client(requestedSchema, hook->addRef()); } Request DynamicCapability::Client::newRequest( InterfaceSchema::Method method, kj::Maybe sizeHint) { auto methodInterface = method.getContainingInterface(); KJ_REQUIRE(schema.extends(methodInterface), "Interface does not implement this method."); auto paramType = method.getParamType(); auto resultType = method.getResultType(); auto typeless = hook->newCall( methodInterface.getProto().getId(), method.getIndex(), sizeHint); return Request( typeless.getAs(paramType), kj::mv(typeless.hook), resultType); } Request DynamicCapability::Client::newRequest( kj::StringPtr methodName, kj::Maybe sizeHint) { return newRequest(schema.getMethodByName(methodName), sizeHint); } Capability::Server::DispatchCallResult DynamicCapability::Server::dispatchCall( uint64_t interfaceId, uint16_t methodId, CallContext context) { KJ_IF_MAYBE(interface, schema.findSuperclass(interfaceId)) { auto methods = interface->getMethods(); if (methodId < methods.size()) { auto method = methods[methodId]; auto resultType = method.getResultType(); return { call(method, CallContext(*context.hook, method.getParamType(), resultType)), resultType.isStreamResult() }; } else { return internalUnimplemented( interface->getProto().getDisplayName().cStr(), interfaceId, methodId); } } else { return internalUnimplemented(schema.getProto().getDisplayName().cStr(), interfaceId); } } RemotePromise Request::send() { auto typelessPromise = hook->send(); hook = nullptr; // prevent reuse auto resultSchemaCopy = resultSchema; // Convert the Promise to return the correct response type. // Explicitly upcast to kj::Promise to make clear that calling .then() doesn't invalidate the // Pipeline part of the RemotePromise. auto typedPromise = kj::implicitCast>&>(typelessPromise) .then([=](Response&& response) -> Response { return Response(response.getAs(resultSchemaCopy), kj::mv(response.hook)); }); // Wrap the typeless pipeline in a typed wrapper. DynamicStruct::Pipeline typedPipeline(resultSchema, kj::mv(kj::implicitCast(typelessPromise))); return RemotePromise(kj::mv(typedPromise), kj::mv(typedPipeline)); } kj::Promise Request::sendStreaming() { KJ_REQUIRE(resultSchema.isStreamResult()); auto promise = hook->sendStreaming(); hook = nullptr; // prevent reuse return promise; } } // namespace capnp capnproto-c++-0.8.0/src/capnp/rpc.c++0000644000175000017500000040647513650101756017742 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "rpc.h" #include "message.h" #include #include #include #include #include #include // std::greater #include #include #include #include #include #include namespace capnp { namespace _ { // private namespace { template inline constexpr uint messageSizeHint() { return 1 + sizeInWords() + sizeInWords(); } template <> inline constexpr uint messageSizeHint() { return 1 + sizeInWords(); } constexpr const uint MESSAGE_TARGET_SIZE_HINT = sizeInWords() + sizeInWords() + 16; // +16 for ops; hope that's enough constexpr const uint CAP_DESCRIPTOR_SIZE_HINT = sizeInWords() + sizeInWords(); constexpr const uint64_t MAX_SIZE_HINT = 1 << 20; uint copySizeHint(MessageSize size) { uint64_t sizeHint = size.wordCount + size.capCount * CAP_DESCRIPTOR_SIZE_HINT // if capCount > 0, the cap descriptor list has a 1-word tag + (size.capCount > 0); return kj::min(MAX_SIZE_HINT, sizeHint); } uint firstSegmentSize(kj::Maybe sizeHint, uint additional) { KJ_IF_MAYBE(s, sizeHint) { return copySizeHint(*s) + additional; } else { return 0; } } kj::Maybe> toPipelineOps(List::Reader ops) { auto result = kj::heapArrayBuilder(ops.size()); for (auto opReader: ops) { PipelineOp op; switch (opReader.which()) { case rpc::PromisedAnswer::Op::NOOP: op.type = PipelineOp::NOOP; break; case rpc::PromisedAnswer::Op::GET_POINTER_FIELD: op.type = PipelineOp::GET_POINTER_FIELD; op.pointerIndex = opReader.getGetPointerField(); break; default: KJ_FAIL_REQUIRE("Unsupported pipeline op.", (uint)opReader.which()) { return nullptr; } } result.add(op); } return result.finish(); } Orphan> fromPipelineOps( Orphanage orphanage, kj::ArrayPtr ops) { auto result = orphanage.newOrphan>(ops.size()); auto builder = result.get(); for (uint i: kj::indices(ops)) { rpc::PromisedAnswer::Op::Builder opBuilder = builder[i]; switch (ops[i].type) { case PipelineOp::NOOP: opBuilder.setNoop(); break; case PipelineOp::GET_POINTER_FIELD: opBuilder.setGetPointerField(ops[i].pointerIndex); break; } } return result; } kj::Exception toException(const rpc::Exception::Reader& exception) { return kj::Exception(static_cast(exception.getType()), "(remote)", 0, kj::str("remote exception: ", exception.getReason())); } void fromException(const kj::Exception& exception, rpc::Exception::Builder builder) { // TODO(someday): Indicate the remote server name as part of the stack trace. Maybe even // transmit stack traces? kj::StringPtr description = exception.getDescription(); // Include context, if any. kj::Vector contextLines; for (auto context = exception.getContext();;) { KJ_IF_MAYBE(c, context) { contextLines.add(kj::str("context: ", c->file, ": ", c->line, ": ", c->description)); context = c->next; } else { break; } } kj::String scratch; if (contextLines.size() > 0) { scratch = kj::str(description, '\n', kj::strArray(contextLines, "\n")); description = scratch; } builder.setReason(description); builder.setType(static_cast(exception.getType())); if (exception.getType() == kj::Exception::Type::FAILED && !exception.getDescription().startsWith("remote exception:")) { KJ_LOG(INFO, "returning failure over rpc", exception); } } uint exceptionSizeHint(const kj::Exception& exception) { return sizeInWords() + exception.getDescription().size() / sizeof(word) + 1; } // ======================================================================================= template class ExportTable { // Table mapping integers to T, where the integers are chosen locally. public: kj::Maybe find(Id id) { if (id < slots.size() && slots[id] != nullptr) { return slots[id]; } else { return nullptr; } } T erase(Id id, T& entry) { // Remove an entry from the table and return it. We return it so that the caller can be // careful to release it (possibly invoking arbitrary destructors) at a time that makes sense. // `entry` is a reference to the entry being released -- we require this in order to prove // that the caller has already done a find() to check that this entry exists. We can't check // ourselves because the caller may have nullified the entry in the meantime. KJ_DREQUIRE(&entry == &slots[id]); T toRelease = kj::mv(slots[id]); slots[id] = T(); freeIds.push(id); return toRelease; } T& next(Id& id) { if (freeIds.empty()) { id = slots.size(); return slots.add(); } else { id = freeIds.top(); freeIds.pop(); return slots[id]; } } template void forEach(Func&& func) { for (Id i = 0; i < slots.size(); i++) { if (slots[i] != nullptr) { func(i, slots[i]); } } } private: kj::Vector slots; std::priority_queue, std::greater> freeIds; }; template class ImportTable { // Table mapping integers to T, where the integers are chosen remotely. public: T& operator[](Id id) { if (id < kj::size(low)) { return low[id]; } else { return high[id]; } } kj::Maybe find(Id id) { if (id < kj::size(low)) { return low[id]; } else { auto iter = high.find(id); if (iter == high.end()) { return nullptr; } else { return iter->second; } } } T erase(Id id) { // Remove an entry from the table and return it. We return it so that the caller can be // careful to release it (possibly invoking arbitrary destructors) at a time that makes sense. if (id < kj::size(low)) { T toRelease = kj::mv(low[id]); low[id] = T(); return toRelease; } else { T toRelease = kj::mv(high[id]); high.erase(id); return toRelease; } } template void forEach(Func&& func) { for (Id i: kj::indices(low)) { func(i, low[i]); } for (auto& entry: high) { func(entry.first, entry.second); } } private: T low[16]; std::unordered_map high; }; // ======================================================================================= class RpcConnectionState final: public kj::TaskSet::ErrorHandler, public kj::Refcounted { public: struct DisconnectInfo { kj::Promise shutdownPromise; // Task which is working on sending an abort message and cleanly ending the connection. }; RpcConnectionState(BootstrapFactoryBase& bootstrapFactory, kj::Maybe::Client> gateway, kj::Maybe restorer, kj::Own&& connectionParam, kj::Own>&& disconnectFulfiller, size_t flowLimit) : bootstrapFactory(bootstrapFactory), gateway(kj::mv(gateway)), restorer(restorer), disconnectFulfiller(kj::mv(disconnectFulfiller)), flowLimit(flowLimit), tasks(*this) { connection.init(kj::mv(connectionParam)); tasks.add(messageLoop()); } kj::Own restore(AnyPointer::Reader objectId) { if (connection.is()) { return newBrokenCap(kj::cp(connection.get())); } QuestionId questionId; auto& question = questions.next(questionId); question.isAwaitingReturn = true; auto paf = kj::newPromiseAndFulfiller>>(); auto questionRef = kj::refcounted(*this, questionId, kj::mv(paf.fulfiller)); question.selfRef = *questionRef; paf.promise = paf.promise.attach(kj::addRef(*questionRef)); { auto message = connection.get()->newOutgoingMessage( objectId.targetSize().wordCount + messageSizeHint()); auto builder = message->getBody().initAs().initBootstrap(); builder.setQuestionId(questionId); builder.getDeprecatedObjectId().set(objectId); message->send(); } auto pipeline = kj::refcounted(*this, kj::mv(questionRef), kj::mv(paf.promise)); return pipeline->getPipelinedCap(kj::Array(nullptr)); } void taskFailed(kj::Exception&& exception) override { disconnect(kj::mv(exception)); } void disconnect(kj::Exception&& exception) { if (!connection.is()) { // Already disconnected. return; } kj::Exception networkException(kj::Exception::Type::DISCONNECTED, exception.getFile(), exception.getLine(), kj::heapString(exception.getDescription())); KJ_IF_MAYBE(newException, kj::runCatchingExceptions([&]() { // Carefully pull all the objects out of the tables prior to releasing them because their // destructors could come back and mess with the tables. kj::Vector> pipelinesToRelease; kj::Vector> clientsToRelease; kj::Vector>> tailCallsToRelease; kj::Vector> resolveOpsToRelease; // All current questions complete with exceptions. questions.forEach([&](QuestionId id, Question& question) { KJ_IF_MAYBE(questionRef, question.selfRef) { // QuestionRef still present. questionRef->reject(kj::cp(networkException)); } }); answers.forEach([&](AnswerId id, Answer& answer) { KJ_IF_MAYBE(p, answer.pipeline) { pipelinesToRelease.add(kj::mv(*p)); } KJ_IF_MAYBE(promise, answer.redirectedResults) { tailCallsToRelease.add(kj::mv(*promise)); } KJ_IF_MAYBE(context, answer.callContext) { context->requestCancel(); } }); exports.forEach([&](ExportId id, Export& exp) { clientsToRelease.add(kj::mv(exp.clientHook)); resolveOpsToRelease.add(kj::mv(exp.resolveOp)); exp = Export(); }); imports.forEach([&](ImportId id, Import& import) { KJ_IF_MAYBE(f, import.promiseFulfiller) { f->get()->reject(kj::cp(networkException)); } }); embargoes.forEach([&](EmbargoId id, Embargo& embargo) { KJ_IF_MAYBE(f, embargo.fulfiller) { f->get()->reject(kj::cp(networkException)); } }); })) { // Some destructor must have thrown an exception. There is no appropriate place to report // these errors. KJ_LOG(ERROR, "Uncaught exception when destroying capabilities dropped by disconnect.", *newException); } // Send an abort message, but ignore failure. kj::runCatchingExceptions([&]() { auto message = connection.get()->newOutgoingMessage( messageSizeHint() + exceptionSizeHint(exception)); fromException(exception, message->getBody().getAs().initAbort()); message->send(); }); // Indicate disconnect. auto shutdownPromise = connection.get()->shutdown() .attach(kj::mv(connection.get())) .then([]() -> kj::Promise { return kj::READY_NOW; }, [](kj::Exception&& e) -> kj::Promise { // Don't report disconnects as an error. if (e.getType() != kj::Exception::Type::DISCONNECTED) { return kj::mv(e); } return kj::READY_NOW; }); disconnectFulfiller->fulfill(DisconnectInfo { kj::mv(shutdownPromise) }); connection.init(kj::mv(networkException)); canceler.cancel(networkException); } void setFlowLimit(size_t words) { flowLimit = words; maybeUnblockFlow(); } private: class RpcClient; class ImportClient; class PromiseClient; class QuestionRef; class RpcPipeline; class RpcCallContext; class RpcResponse; // ======================================================================================= // The Four Tables entry types // // We have to define these before we can define the class's fields. typedef uint32_t QuestionId; typedef QuestionId AnswerId; typedef uint32_t ExportId; typedef ExportId ImportId; // See equivalent definitions in rpc.capnp. // // We always use the type that refers to the local table of the same name. So e.g. although // QuestionId and AnswerId are the same type, we use QuestionId when referring to an entry in // the local question table (which corresponds to the peer's answer table) and use AnswerId // to refer to an entry in our answer table (which corresponds to the peer's question table). // Since all messages in the RPC protocol are defined from the sender's point of view, this // means that any time we read an ID from a received message, its type should invert. // TODO(cleanup): Perhaps we could enforce that in a type-safe way? Hmm... struct Question { kj::Array paramExports; // List of exports that were sent in the request. If the response has `releaseParamCaps` these // will need to be released. kj::Maybe selfRef; // The local QuestionRef, set to nullptr when it is destroyed, which is also when `Finish` is // sent. bool isAwaitingReturn = false; // True from when `Call` is sent until `Return` is received. bool isTailCall = false; // Is this a tail call? If so, we don't expect to receive results in the `Return`. bool skipFinish = false; // If true, don't send a Finish message. inline bool operator==(decltype(nullptr)) const { return !isAwaitingReturn && selfRef == nullptr; } inline bool operator!=(decltype(nullptr)) const { return !operator==(nullptr); } }; struct Answer { Answer() = default; Answer(const Answer&) = delete; Answer(Answer&&) = default; Answer& operator=(Answer&&) = default; // If we don't explicitly write all this, we get some stupid error deep in STL. bool active = false; // True from the point when the Call message is received to the point when both the `Finish` // message has been received and the `Return` has been sent. kj::Maybe> pipeline; // Send pipelined calls here. Becomes null as soon as a `Finish` is received. kj::Maybe>> redirectedResults; // For locally-redirected calls (Call.sendResultsTo.yourself), this is a promise for the call // result, to be picked up by a subsequent `Return`. kj::Maybe callContext; // The call context, if it's still active. Becomes null when the `Return` message is sent. // This object, if non-null, is owned by `asyncOp`. kj::Array resultExports; // List of exports that were sent in the results. If the finish has `releaseResultCaps` these // will need to be released. }; struct Export { uint refcount = 0; // When this reaches 0, drop `clientHook` and free this export. kj::Own clientHook; kj::Promise resolveOp = nullptr; // If this export is a promise (not a settled capability), the `resolveOp` represents the // ongoing operation to wait for that promise to resolve and then send a `Resolve` message. inline bool operator==(decltype(nullptr)) const { return refcount == 0; } inline bool operator!=(decltype(nullptr)) const { return refcount != 0; } }; struct Import { Import() = default; Import(const Import&) = delete; Import(Import&&) = default; Import& operator=(Import&&) = default; // If we don't explicitly write all this, we get some stupid error deep in STL. kj::Maybe importClient; // Becomes null when the import is destroyed. kj::Maybe appClient; // Either a copy of importClient, or, in the case of promises, the wrapping PromiseClient. // Becomes null when it is discarded *or* when the import is destroyed (e.g. the promise is // resolved and the import is no longer needed). kj::Maybe>>> promiseFulfiller; // If non-null, the import is a promise. }; typedef uint32_t EmbargoId; struct Embargo { // For handling the `Disembargo` message when looping back to self. kj::Maybe>> fulfiller; // Fulfill this when the Disembargo arrives. inline bool operator==(decltype(nullptr)) const { return fulfiller == nullptr; } inline bool operator!=(decltype(nullptr)) const { return fulfiller != nullptr; } }; // ======================================================================================= // OK, now we can define RpcConnectionState's member data. BootstrapFactoryBase& bootstrapFactory; kj::Maybe::Client> gateway; kj::Maybe restorer; typedef kj::Own Connected; typedef kj::Exception Disconnected; kj::OneOf connection; // Once the connection has failed, we drop it and replace it with an exception, which will be // thrown from all further calls. kj::Canceler canceler; // Will be canceled if and when `connection` is changed from `Connected` to `Disconnected`. // TODO(cleanup): `Connected` should be a struct that contains the connection and the Canceler, // but that's more refactoring than I want to do right now. kj::Own> disconnectFulfiller; ExportTable exports; ExportTable questions; ImportTable answers; ImportTable imports; // The Four Tables! // The order of the tables is important for correct destruction. std::unordered_map exportsByCap; // Maps already-exported ClientHook objects to their ID in the export table. ExportTable embargoes; // There are only four tables. This definitely isn't a fifth table. I don't know what you're // talking about. size_t flowLimit; size_t callWordsInFlight = 0; kj::Maybe>> flowWaiter; // If non-null, we're currently blocking incoming messages waiting for callWordsInFlight to drop // below flowLimit. Fulfill this to un-block. kj::TaskSet tasks; // ===================================================================================== // ClientHook implementations class RpcClient: public ClientHook, public kj::Refcounted { public: RpcClient(RpcConnectionState& connectionState) : connectionState(kj::addRef(connectionState)) {} virtual kj::Maybe writeDescriptor(rpc::CapDescriptor::Builder descriptor, kj::Vector& fds) = 0; // Writes a CapDescriptor referencing this client. The CapDescriptor must be sent as part of // the very next message sent on the connection, as it may become invalid if other things // happen. // // If writing the descriptor adds a new export to the export table, or increments the refcount // on an existing one, then the ID is returned and the caller is responsible for removing it // later. virtual kj::Maybe> writeTarget( rpc::MessageTarget::Builder target) = 0; // Writes the appropriate call target for calls to this capability and returns null. // // - OR - // // If calls have been redirected to some other local ClientHook, returns that hook instead. // This can happen if the capability represents a promise that has been resolved. virtual kj::Own getInnermostClient() = 0; // If this client just wraps some other client -- even if it is only *temporarily* wrapping // that other client -- return a reference to the other client, transitively. Otherwise, // return a new reference to *this. virtual void adoptFlowController(kj::Own flowController) { // Called when a PromiseClient resolves to another RpcClient. If streaming calls were // outstanding on the old client, we'd like to keep using the same FlowController on the new // client, so as to keep the flow steady. if (this->flowController == nullptr) { // We don't have any existing flowController so we can adopt this one, yay! this->flowController = kj::mv(flowController); } else { // Apparently, there is an existing flowController. This is an unusual scenario: Apparently // we had two stream capabilities, we were streaming to both of them, and they later // resolved to the same capability. This probably never happens because streaming use cases // normally call for there to be only one client. But, it's certainly possible, and we need // to handle it. We'll do the conservative thing and just make sure that all the calls // finish. This may mean we'll over-buffer temporarily; oh well. connectionState->tasks.add(flowController->waitAllAcked().attach(kj::mv(flowController))); } } // implements ClientHook ----------------------------------------- Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { if (interfaceId == typeId>() && methodId == 0) { KJ_IF_MAYBE(g, connectionState->gateway) { // Wait, this is a call to Persistent.save() and we need to translate it through our // gateway. // // We pull a neat trick here: We actually end up returning a RequestHook for an import // request on the gateway cap, but with the "root" of the request actually pointing // to the "params" field of the real request. sizeHint = sizeHint.map([](MessageSize hint) { ++hint.capCount; hint.wordCount += sizeInWords::ImportParams>(); return hint; }); auto request = g->importRequest(sizeHint); request.setCap(Persistent<>::Client(kj::refcounted(*this))); // Awkwardly, request.initParams() would return a SaveParams struct, but to construct // the Request to return we need an AnyPointer::Builder, and you // can't go backwards from a struct builder to an AnyPointer builder. So instead we // manually get at the pointer by converting the outer request to AnyStruct and then // pulling the pointer from the pointer section. auto pointers = toAny(request).getPointerSection(); KJ_ASSERT(pointers.size() >= 2); auto paramsPtr = pointers[1]; KJ_ASSERT(paramsPtr.isNull()); return Request(paramsPtr, RequestHook::from(kj::mv(request))); } } return newCallNoIntercept(interfaceId, methodId, sizeHint); } Request newCallNoIntercept( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) { if (!connectionState->connection.is()) { return newBrokenRequest(kj::cp(connectionState->connection.get()), sizeHint); } auto request = kj::heap( *connectionState, *connectionState->connection.get(), sizeHint, kj::addRef(*this)); auto callBuilder = request->getCall(); callBuilder.setInterfaceId(interfaceId); callBuilder.setMethodId(methodId); auto root = request->getRoot(); return Request(root, kj::mv(request)); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { if (interfaceId == typeId>() && methodId == 0) { KJ_IF_MAYBE(g, connectionState->gateway) { // Wait, this is a call to Persistent.save() and we need to translate it through our // gateway. auto params = context->getParams().getAs::SaveParams>(); auto requestSize = params.totalSize(); ++requestSize.capCount; requestSize.wordCount += sizeInWords::ImportParams>(); auto request = g->importRequest(requestSize); request.setCap(Persistent<>::Client(kj::refcounted(*this))); request.setParams(params); context->allowCancellation(); context->releaseParams(); return context->directTailCall(RequestHook::from(kj::mv(request))); } } return callNoIntercept(interfaceId, methodId, kj::mv(context)); } VoidPromiseAndPipeline callNoIntercept(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) { // Implement call() by copying params and results messages. auto params = context->getParams(); auto request = newCallNoIntercept(interfaceId, methodId, params.targetSize()); request.set(params); context->releaseParams(); // We can and should propagate cancellation. context->allowCancellation(); return context->directTailCall(RequestHook::from(kj::mv(request))); } kj::Own addRef() override { return kj::addRef(*this); } const void* getBrand() override { return connectionState.get(); } kj::Own connectionState; kj::Maybe> flowController; // Becomes non-null the first time a streaming call is made on this capability. }; class ImportClient final: public RpcClient { // A ClientHook that wraps an entry in the import table. public: ImportClient(RpcConnectionState& connectionState, ImportId importId, kj::Maybe fd) : RpcClient(connectionState), importId(importId), fd(kj::mv(fd)) {} ~ImportClient() noexcept(false) { unwindDetector.catchExceptionsIfUnwinding([&]() { // Remove self from the import table, if the table is still pointing at us. KJ_IF_MAYBE(import, connectionState->imports.find(importId)) { KJ_IF_MAYBE(i, import->importClient) { if (i == this) { connectionState->imports.erase(importId); } } } // Send a message releasing our remote references. if (remoteRefcount > 0 && connectionState->connection.is()) { auto message = connectionState->connection.get()->newOutgoingMessage( messageSizeHint()); rpc::Release::Builder builder = message->getBody().initAs().initRelease(); builder.setId(importId); builder.setReferenceCount(remoteRefcount); message->send(); } }); } void setFdIfMissing(kj::Maybe newFd) { if (fd == nullptr) { fd = kj::mv(newFd); } } void addRemoteRef() { // Add a new RemoteRef and return a new ref to this client representing it. ++remoteRefcount; } kj::Maybe writeDescriptor(rpc::CapDescriptor::Builder descriptor, kj::Vector& fds) override { descriptor.setReceiverHosted(importId); return nullptr; } kj::Maybe> writeTarget( rpc::MessageTarget::Builder target) override { target.setImportedCap(importId); return nullptr; } kj::Own getInnermostClient() override { return kj::addRef(*this); } // implements ClientHook ----------------------------------------- kj::Maybe getResolved() override { return nullptr; } kj::Maybe>> whenMoreResolved() override { return nullptr; } kj::Maybe getFd() override { return fd.map([](auto& f) { return f.get(); }); } private: ImportId importId; kj::Maybe fd; uint remoteRefcount = 0; // Number of times we've received this import from the peer. kj::UnwindDetector unwindDetector; }; class PipelineClient final: public RpcClient { // A ClientHook representing a pipelined promise. Always wrapped in PromiseClient. public: PipelineClient(RpcConnectionState& connectionState, kj::Own&& questionRef, kj::Array&& ops) : RpcClient(connectionState), questionRef(kj::mv(questionRef)), ops(kj::mv(ops)) {} kj::Maybe writeDescriptor(rpc::CapDescriptor::Builder descriptor, kj::Vector& fds) override { auto promisedAnswer = descriptor.initReceiverAnswer(); promisedAnswer.setQuestionId(questionRef->getId()); promisedAnswer.adoptTransform(fromPipelineOps( Orphanage::getForMessageContaining(descriptor), ops)); return nullptr; } kj::Maybe> writeTarget( rpc::MessageTarget::Builder target) override { auto builder = target.initPromisedAnswer(); builder.setQuestionId(questionRef->getId()); builder.adoptTransform(fromPipelineOps(Orphanage::getForMessageContaining(builder), ops)); return nullptr; } kj::Own getInnermostClient() override { return kj::addRef(*this); } // implements ClientHook ----------------------------------------- kj::Maybe getResolved() override { return nullptr; } kj::Maybe>> whenMoreResolved() override { return nullptr; } kj::Maybe getFd() override { return nullptr; } private: kj::Own questionRef; kj::Array ops; }; class PromiseClient final: public RpcClient { // A ClientHook that initially wraps one client (in practice, an ImportClient or a // PipelineClient) and then, later on, redirects to some other client. public: PromiseClient(RpcConnectionState& connectionState, kj::Own initial, kj::Promise> eventual, kj::Maybe importId) : RpcClient(connectionState), cap(kj::mv(initial)), importId(importId), fork(eventual.then( [this](kj::Own&& resolution) { return resolve(kj::mv(resolution)); }, [this](kj::Exception&& exception) { return resolve(newBrokenCap(kj::mv(exception))); }).catch_([&](kj::Exception&& e) { // Make any exceptions thrown from resolve() go to the connection's TaskSet which // will cause the connection to be terminated. connectionState.tasks.add(kj::cp(e)); return newBrokenCap(kj::mv(e)); }).fork()) {} // Create a client that starts out forwarding all calls to `initial` but, once `eventual` // resolves, will forward there instead. ~PromiseClient() noexcept(false) { KJ_IF_MAYBE(id, importId) { // This object is representing an import promise. That means the import table may still // contain a pointer back to it. Remove that pointer. Note that we have to verify that // the import still exists and the pointer still points back to this object because this // object may actually outlive the import. KJ_IF_MAYBE(import, connectionState->imports.find(*id)) { KJ_IF_MAYBE(c, import->appClient) { if (c == this) { import->appClient = nullptr; } } } } } kj::Maybe writeDescriptor(rpc::CapDescriptor::Builder descriptor, kj::Vector& fds) override { receivedCall = true; return connectionState->writeDescriptor(*cap, descriptor, fds); } kj::Maybe> writeTarget( rpc::MessageTarget::Builder target) override { receivedCall = true; return connectionState->writeTarget(*cap, target); } kj::Own getInnermostClient() override { receivedCall = true; return connectionState->getInnermostClient(*cap); } void adoptFlowController(kj::Own flowController) override { if (cap->getBrand() == connectionState.get()) { // Pass the flow controller on to our inner cap. kj::downcast(*cap).adoptFlowController(kj::mv(flowController)); } else { // We resolved to a capability that isn't another RPC capability. We should simply make // sure that all the calls complete. connectionState->tasks.add(flowController->waitAllAcked().attach(kj::mv(flowController))); } } // implements ClientHook ----------------------------------------- Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { if (!isResolved() && interfaceId == typeId>() && methodId == 0 && connectionState->gateway != nullptr) { // This is a call to Persistent.save(), and we're not resolved yet, and the underlying // remote capability will perform a gateway translation. This isn't right if the promise // ultimately resolves to a local capability. Instead, we'll need to queue the call until // the promise resolves. return newLocalPromiseClient(fork.addBranch()) ->newCall(interfaceId, methodId, sizeHint); } receivedCall = true; return cap->newCall(interfaceId, methodId, sizeHint); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { if (!isResolved() && interfaceId == typeId>() && methodId == 0 && connectionState->gateway != nullptr) { // This is a call to Persistent.save(), and we're not resolved yet, and the underlying // remote capability will perform a gateway translation. This isn't right if the promise // ultimately resolves to a local capability. Instead, we'll need to queue the call until // the promise resolves. auto vpapPromises = fork.addBranch().then(kj::mvCapture(context, [interfaceId,methodId](kj::Own&& context, kj::Own resolvedCap) { auto vpap = resolvedCap->call(interfaceId, methodId, kj::mv(context)); return kj::tuple(kj::mv(vpap.promise), kj::mv(vpap.pipeline)); })).split(); return { kj::mv(kj::get<0>(vpapPromises)), newLocalPromisePipeline(kj::mv(kj::get<1>(vpapPromises))), }; } receivedCall = true; return cap->call(interfaceId, methodId, kj::mv(context)); } kj::Maybe getResolved() override { if (isResolved()) { return *cap; } else { return nullptr; } } kj::Maybe>> whenMoreResolved() override { return fork.addBranch(); } kj::Maybe getFd() override { if (isResolved()) { return cap->getFd(); } else { // In theory, before resolution, the ImportClient for the promise could have an FD // attached, if the promise itself was presented with an attached FD. However, we can't // really return that one here because it may be closed when we get the Resolve message // later. In theory we could have the PromiseClient itself take ownership of an FD that // arrived attached to a promise cap, but the use case for that is questionable. I'm // keeping it simple for now. return nullptr; } } private: kj::Own cap; kj::Maybe importId; kj::ForkedPromise> fork; bool receivedCall = false; enum { UNRESOLVED, // Not resolved at all yet. REMOTE, // Remote promise resolved to a remote settled capability (or null/error). REFLECTED, // Remote promise resolved to one of our own exports. MERGED, // Remote promise resolved to another remote promise which itself wasn't resolved yet, so we // merged them. In this case, `cap` is guaranteed to point to another PromiseClient. BROKEN // Resolved to null or error. } resolutionType = UNRESOLVED; inline bool isResolved() { return resolutionType != UNRESOLVED; } kj::Promise> resolve(kj::Own replacement) { KJ_DASSERT(!isResolved()); const void* replacementBrand = replacement->getBrand(); bool isSameConnection = replacementBrand == connectionState.get(); if (isSameConnection) { // We resolved to some other RPC capability hosted by the same peer. KJ_IF_MAYBE(promise, replacement->whenMoreResolved()) { // We resolved to another remote promise. If *that* promise eventually resolves back // to us, we'll need a disembargo. Possibilities: // 1. The other promise hasn't resolved at all yet. In that case we can simply set its // `receivedCall` flag and let it handle the disembargo later. // 2. The other promise has received a Resolve message and decided to initiate a // disembargo which it is still waiting for. In that case we will certainly also need // a disembargo for the same reason that the other promise did. And, we can't simply // wait for their disembargo; we need to start a new one of our own. // 3. The other promise has resolved already (with or without a disembargo). In this // case we should treat it as if we resolved directly to the other promise's result, // possibly requiring a disembargo under the same conditions. // We know the other object is a PromiseClient because it's the only ClientHook // type in the RPC implementation which returns non-null for `whenMoreResolved()`. PromiseClient* other = &kj::downcast(*replacement); while (other->resolutionType == MERGED) { // There's no need to resolve to a thing that's just going to resolve to another thing. replacement = other->cap->addRef(); other = &kj::downcast(*replacement); // Note that replacementBrand is unchanged since we'd only merge with other // PromiseClients on the same connection. KJ_DASSERT(replacement->getBrand() == replacementBrand); } if (other->isResolved()) { // The other capability resolved already. If it determined that it resolved as // relfected, then we determine the same. resolutionType = other->resolutionType; } else { // The other capability hasn't resolved yet, so we can safely merge with it and do a // single combined disembargo if needed later. other->receivedCall = other->receivedCall || receivedCall; resolutionType = MERGED; } } else { resolutionType = REMOTE; } } else { if (replacementBrand == &ClientHook::NULL_CAPABILITY_BRAND || replacementBrand == &ClientHook::BROKEN_CAPABILITY_BRAND) { // We don't consider null or broken capabilities as "reflected" because they may have // been communicated to us literally as a null pointer or an exception on the wire, // rather than as a reference to one of our exports, in which case a disembargo won't // work. But also, call ordering is completely irrelevant with these so there's no need // to disembargo anyway. resolutionType = BROKEN; } else { resolutionType = REFLECTED; } } // Every branch above ends by setting resolutionType to something other than UNRESOLVED. KJ_DASSERT(isResolved()); // If the original capability was used for streaming calls, it will have a // `flowController` that might still be shepherding those calls. We'll need make sure that // it doesn't get thrown away. Note that we know that *cap is an RpcClient because resolve() // is only called once and our constructor required that the initial capability is an // RpcClient. KJ_IF_MAYBE(f, kj::downcast(*cap).flowController) { if (isSameConnection) { // The new target is on the same connection. It would make a lot of sense to keep using // the same flow controller if possible. kj::downcast(*replacement).adoptFlowController(kj::mv(*f)); } else { // The new target is something else. The best we can do is wait for the controller to // drain. New calls will be flow-controlled in a new way without knowing about the old // controller. connectionState->tasks.add(f->get()->waitAllAcked().attach(kj::mv(*f))); } } if (resolutionType == REFLECTED && receivedCall && connectionState->connection.is()) { // The new capability is hosted locally, not on the remote machine. And, we had made calls // to the promise. We need to make sure those calls echo back to us before we allow new // calls to go directly to the local capability, so we need to set a local embargo and send // a `Disembargo` to echo through the peer. auto message = connectionState->connection.get()->newOutgoingMessage( messageSizeHint() + MESSAGE_TARGET_SIZE_HINT); auto disembargo = message->getBody().initAs().initDisembargo(); { auto redirect = connectionState->writeTarget(*cap, disembargo.initTarget()); KJ_ASSERT(redirect == nullptr, "Original promise target should always be from this RPC connection."); } EmbargoId embargoId; Embargo& embargo = connectionState->embargoes.next(embargoId); disembargo.getContext().setSenderLoopback(embargoId); auto paf = kj::newPromiseAndFulfiller(); embargo.fulfiller = kj::mv(paf.fulfiller); // Make a promise which resolves to `replacement` as soon as the `Disembargo` comes back. auto embargoPromise = paf.promise.then([replacement = kj::mv(replacement)]() mutable { return kj::mv(replacement); }); // We need to queue up calls in the meantime, so we'll resolve ourselves to a local promise // client instead. replacement = newLocalPromiseClient(kj::mv(embargoPromise)); // Send the `Disembargo`. message->send(); } cap = replacement->addRef(); return kj::mv(replacement); } }; class NoInterceptClient final: public RpcClient { // A wrapper around an RpcClient which bypasses special handling of "save" requests. When we // intercept a "save" request and invoke a RealmGateway, we give it a version of the capability // with intercepting disabled, since usually the first thing the RealmGateway will do is turn // around and call save() again. // // This is admittedly sort of backwards: the interception of "save" ought to be the part // implemented by a wrapper. However, that would require placing a wrapper around every // RpcClient we create whereas NoInterceptClient only needs to be injected after a save() // request occurs and is intercepted. public: NoInterceptClient(RpcClient& inner) : RpcClient(*inner.connectionState), inner(kj::addRef(inner)) {} kj::Maybe writeDescriptor(rpc::CapDescriptor::Builder descriptor, kj::Vector& fds) override { return inner->writeDescriptor(descriptor, fds); } kj::Maybe> writeTarget(rpc::MessageTarget::Builder target) override { return inner->writeTarget(target); } kj::Own getInnermostClient() override { return inner->getInnermostClient(); } Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { return inner->newCallNoIntercept(interfaceId, methodId, sizeHint); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { return inner->callNoIntercept(interfaceId, methodId, kj::mv(context)); } kj::Maybe getResolved() override { return nullptr; } kj::Maybe>> whenMoreResolved() override { return nullptr; } kj::Maybe getFd() override { return nullptr; } private: kj::Own inner; }; kj::Maybe writeDescriptor(ClientHook& cap, rpc::CapDescriptor::Builder descriptor, kj::Vector& fds) { // Write a descriptor for the given capability. // Find the innermost wrapped capability. ClientHook* inner = ∩ for (;;) { KJ_IF_MAYBE(resolved, inner->getResolved()) { inner = resolved; } else { break; } } KJ_IF_MAYBE(fd, inner->getFd()) { descriptor.setAttachedFd(fds.size()); fds.add(kj::mv(*fd)); } if (inner->getBrand() == this) { return kj::downcast(*inner).writeDescriptor(descriptor, fds); } else { auto iter = exportsByCap.find(inner); if (iter != exportsByCap.end()) { // We've already seen and exported this capability before. Just up the refcount. auto& exp = KJ_ASSERT_NONNULL(exports.find(iter->second)); ++exp.refcount; descriptor.setSenderHosted(iter->second); return iter->second; } else { // This is the first time we've seen this capability. ExportId exportId; auto& exp = exports.next(exportId); exportsByCap[inner] = exportId; exp.refcount = 1; exp.clientHook = inner->addRef(); KJ_IF_MAYBE(wrapped, inner->whenMoreResolved()) { // This is a promise. Arrange for the `Resolve` message to be sent later. exp.resolveOp = resolveExportedPromise(exportId, kj::mv(*wrapped)); descriptor.setSenderPromise(exportId); } else { descriptor.setSenderHosted(exportId); } return exportId; } } } kj::Array writeDescriptors(kj::ArrayPtr>> capTable, rpc::Payload::Builder payload, kj::Vector& fds) { if (capTable.size() == 0) { // Calling initCapTable(0) will still allocate a 1-word tag, which we'd like to avoid... return nullptr; } auto capTableBuilder = payload.initCapTable(capTable.size()); kj::Vector exports(capTable.size()); for (uint i: kj::indices(capTable)) { KJ_IF_MAYBE(cap, capTable[i]) { KJ_IF_MAYBE(exportId, writeDescriptor(**cap, capTableBuilder[i], fds)) { exports.add(*exportId); } } else { capTableBuilder[i].setNone(); } } return exports.releaseAsArray(); } kj::Maybe> writeTarget(ClientHook& cap, rpc::MessageTarget::Builder target) { // If calls to the given capability should pass over this connection, fill in `target` // appropriately for such a call and return nullptr. Otherwise, return a `ClientHook` to which // the call should be forwarded; the caller should then delegate the call to that `ClientHook`. // // The main case where this ends up returning non-null is if `cap` is a promise that has // recently resolved. The application might have started building a request before the promise // resolved, and so the request may have been built on the assumption that it would be sent over // this network connection, but then the promise resolved to point somewhere else before the // request was sent. Now the request has to be redirected to the new target instead. if (cap.getBrand() == this) { return kj::downcast(cap).writeTarget(target); } else { return cap.addRef(); } } kj::Own getInnermostClient(ClientHook& client) { ClientHook* ptr = &client; for (;;) { KJ_IF_MAYBE(inner, ptr->getResolved()) { ptr = inner; } else { break; } } if (ptr->getBrand() == this) { return kj::downcast(*ptr).getInnermostClient(); } else { return ptr->addRef(); } } kj::Promise resolveExportedPromise( ExportId exportId, kj::Promise>&& promise) { // Implements exporting of a promise. The promise has been exported under the given ID, and is // to eventually resolve to the ClientHook produced by `promise`. This method waits for that // resolve to happen and then sends the appropriate `Resolve` message to the peer. return promise.then( [this,exportId](kj::Own&& resolution) -> kj::Promise { // Successful resolution. KJ_ASSERT(connection.is(), "Resolving export should have been canceled on disconnect.") { return kj::READY_NOW; } // Get the innermost ClientHook backing the resolved client. This includes traversing // PromiseClients that haven't resolved yet to their underlying ImportClient or // PipelineClient, so that we get a remote promise that might resolve later. This is // important to make sure that if the peer sends a `Disembargo` back to us, it bounces back // correctly instead of going to the result of some future resolution. See the documentation // for `Disembargo` in `rpc.capnp`. resolution = getInnermostClient(*resolution); // Update the export table to point at this object instead. We know that our entry in the // export table is still live because when it is destroyed the asynchronous resolution task // (i.e. this code) is canceled. auto& exp = KJ_ASSERT_NONNULL(exports.find(exportId)); exportsByCap.erase(exp.clientHook); exp.clientHook = kj::mv(resolution); if (exp.clientHook->getBrand() != this) { // We're resolving to a local capability. If we're resolving to a promise, we might be // able to reuse our export table entry and avoid sending a message. KJ_IF_MAYBE(promise, exp.clientHook->whenMoreResolved()) { // We're replacing a promise with another local promise. In this case, we might actually // be able to just reuse the existing export table entry to represent the new promise -- // unless it already has an entry. Let's check. auto insertResult = exportsByCap.insert(std::make_pair(exp.clientHook.get(), exportId)); if (insertResult.second) { // The new promise was not already in the table, therefore the existing export table // entry has now been repurposed to represent it. There is no need to send a resolve // message at all. We do, however, have to start resolving the next promise. return resolveExportedPromise(exportId, kj::mv(*promise)); } } } // OK, we have to send a `Resolve` message. auto message = connection.get()->newOutgoingMessage( messageSizeHint() + sizeInWords() + 16); auto resolve = message->getBody().initAs().initResolve(); resolve.setPromiseId(exportId); kj::Vector fds; writeDescriptor(*exp.clientHook, resolve.initCap(), fds); message->setFds(fds.releaseAsArray()); message->send(); return kj::READY_NOW; }, [this,exportId](kj::Exception&& exception) { // send error resolution auto message = connection.get()->newOutgoingMessage( messageSizeHint() + exceptionSizeHint(exception) + 8); auto resolve = message->getBody().initAs().initResolve(); resolve.setPromiseId(exportId); fromException(exception, resolve.initException()); message->send(); }).eagerlyEvaluate([this](kj::Exception&& exception) { // Put the exception on the TaskSet which will cause the connection to be terminated. tasks.add(kj::mv(exception)); }); } // ===================================================================================== // Interpreting CapDescriptor kj::Own import(ImportId importId, bool isPromise, kj::Maybe fd) { // Receive a new import. auto& import = imports[importId]; kj::Own importClient; // Create the ImportClient, or if one already exists, use it. KJ_IF_MAYBE(c, import.importClient) { importClient = kj::addRef(*c); // If the same import is introduced multiple times, and it is missing an FD the first time, // but it has one on a later attempt, we want to attach the later one. This could happen // because the first introduction was part of a message that had too many other FDs and went // over the per-message limit. Perhaps the protocol design is such that this other message // doesn't really care if the FDs are transferred or not, but the later message really does // care; it would be bad if the previous message blocked later messages from delivering the // FD just because it happened to reference the same capability. importClient->setFdIfMissing(kj::mv(fd)); } else { importClient = kj::refcounted(*this, importId, kj::mv(fd)); import.importClient = *importClient; } // We just received a copy of this import ID, so the remote refcount has gone up. importClient->addRemoteRef(); if (isPromise) { // We need to construct a PromiseClient around this import, if we haven't already. KJ_IF_MAYBE(c, import.appClient) { // Use the existing one. return kj::addRef(*c); } else { // Create a promise for this import's resolution. auto paf = kj::newPromiseAndFulfiller>(); import.promiseFulfiller = kj::mv(paf.fulfiller); // Make sure the import is not destroyed while this promise exists. paf.promise = paf.promise.attach(kj::addRef(*importClient)); // Create a PromiseClient around it and return it. auto result = kj::refcounted( *this, kj::mv(importClient), kj::mv(paf.promise), importId); import.appClient = *result; return kj::mv(result); } } else { import.appClient = *importClient; return kj::mv(importClient); } } class TribbleRaceBlocker: public ClientHook, public kj::Refcounted { // Hack to work around a problem that arises during the Tribble 4-way Race Condition as // described in rpc.capnp in the documentation for the `Disembargo` message. // // Consider a remote promise that is resolved by a `Resolve` message. PromiseClient::resolve() // is eventually called and given the `ClientHook` for the resolution. Imagine that the // `ClientHook` it receives turns out to be an `ImportClient`. There are two ways this could // have happened: // // 1. The `Resolve` message contained a `CapDescriptor` of type `senderHosted`, naming an entry // in the sender's export table, and the `ImportClient` refers to the corresponding slot on // the receiver's import table. In this case, no embargo is needed, because messages to the // resolved location traverse the same path as messages to the promise would have. // // 2. The `Resolve` message contained a `CapDescriptor` of type `receiverHosted`, naming an // entry in the receiver's export table. That entry just happened to contain an // `ImportClient` refering back to the sender. This specifically happens when the entry // in question had previously itself referred to a promise, and that promise has since // resolved to a remote capability, at which point the export table entry was replaced by // the appropriate `ImportClient` representing that. Presumably, the peer *did not yet know* // about this resolution, which is why it sent a `receiverHosted` pointing to something that // reflects back to the sender, rather than sending `senderHosted` in the first place. // // In this case, an embargo *is* required, because peer may still be reflecting messages // sent to this promise back to us. In fact, the peer *must* continue reflecting messages, // even when it eventually learns that the eventual destination is one of its own // capabilities, due to the Tribble 4-way Race Condition rule. // // Since this case requires an embargo, somehow PromiseClient::resolve() must be able to // distinguish it from the case (1). One solution would be for us to pass some extra flag // all the way from where the `Resolve` messages is received to `PromiseClient::resolve()`. // That solution is reasonably easy in the `Resolve` case, but gets notably more difficult // in the case of `Return`s, which also resolve promises and are subject to all the same // problems. In the case of a `Return`, some non-RPC-specific code is involved in the // resolution, making it harder to pass along a flag. // // Instead, we use this hack: When we read an entry in the export table and discover that // it actually contains an `ImportClient` or a `PipelineClient` reflecting back over our // own connection, then we wrap it in a `TribbleRaceBlocker`. This wrapper prevents // `PromiseClient` from recognizing the capability as being remote, so it instead treats it // as local. That causes it to set up an embargo as desired. // // TODO(perf): This actually blocks further promise resolution in the case where the // ImportClient or PipelineClient itself ends up being yet another promise that resolves // back over the connection again. What we probably really need to do here is, instead of // placing `ImportClient` or `PipelineClient` on the export table, place a special type there // that both knows what to do with future incoming messages to that export ID, but also knows // what to do when that export is the subject of a `Resolve`. public: TribbleRaceBlocker(kj::Own inner): inner(kj::mv(inner)) {} Request newCall( uint64_t interfaceId, uint16_t methodId, kj::Maybe sizeHint) override { return inner->newCall(interfaceId, methodId, sizeHint); } VoidPromiseAndPipeline call(uint64_t interfaceId, uint16_t methodId, kj::Own&& context) override { return inner->call(interfaceId, methodId, kj::mv(context)); } kj::Maybe getResolved() override { // We always wrap either PipelineClient or ImportClient, both of which return null for this // anyway. return nullptr; } kj::Maybe>> whenMoreResolved() override { // We always wrap either PipelineClient or ImportClient, both of which return null for this // anyway. return nullptr; } kj::Own addRef() override { return kj::addRef(*this); } const void* getBrand() override { return nullptr; } kj::Maybe getFd() override { return inner->getFd(); } private: kj::Own inner; }; kj::Maybe> receiveCap(rpc::CapDescriptor::Reader descriptor, kj::ArrayPtr fds) { uint fdIndex = descriptor.getAttachedFd(); kj::Maybe fd; if (fdIndex < fds.size() && fds[fdIndex] != nullptr) { fd = kj::mv(fds[fdIndex]); } switch (descriptor.which()) { case rpc::CapDescriptor::NONE: return nullptr; case rpc::CapDescriptor::SENDER_HOSTED: return import(descriptor.getSenderHosted(), false, kj::mv(fd)); case rpc::CapDescriptor::SENDER_PROMISE: return import(descriptor.getSenderPromise(), true, kj::mv(fd)); case rpc::CapDescriptor::RECEIVER_HOSTED: KJ_IF_MAYBE(exp, exports.find(descriptor.getReceiverHosted())) { auto result = exp->clientHook->addRef(); if (result->getBrand() == this) { result = kj::refcounted(kj::mv(result)); } return kj::mv(result); } else { return newBrokenCap("invalid 'receiverHosted' export ID"); } case rpc::CapDescriptor::RECEIVER_ANSWER: { auto promisedAnswer = descriptor.getReceiverAnswer(); KJ_IF_MAYBE(answer, answers.find(promisedAnswer.getQuestionId())) { if (answer->active) { KJ_IF_MAYBE(pipeline, answer->pipeline) { KJ_IF_MAYBE(ops, toPipelineOps(promisedAnswer.getTransform())) { auto result = pipeline->get()->getPipelinedCap(*ops); if (result->getBrand() == this) { result = kj::refcounted(kj::mv(result)); } return kj::mv(result); } else { return newBrokenCap("unrecognized pipeline ops"); } } } } return newBrokenCap("invalid 'receiverAnswer'"); } case rpc::CapDescriptor::THIRD_PARTY_HOSTED: // We don't support third-party caps, so use the vine instead. return import(descriptor.getThirdPartyHosted().getVineId(), false, kj::mv(fd)); default: KJ_FAIL_REQUIRE("unknown CapDescriptor type") { break; } return newBrokenCap("unknown CapDescriptor type"); } } kj::Array>> receiveCaps(List::Reader capTable, kj::ArrayPtr fds) { auto result = kj::heapArrayBuilder>>(capTable.size()); for (auto cap: capTable) { result.add(receiveCap(cap, fds)); } return result.finish(); } // ===================================================================================== // RequestHook/PipelineHook/ResponseHook implementations class QuestionRef: public kj::Refcounted { // A reference to an entry on the question table. Used to detect when the `Finish` message // can be sent. public: inline QuestionRef( RpcConnectionState& connectionState, QuestionId id, kj::Own>>> fulfiller) : connectionState(kj::addRef(connectionState)), id(id), fulfiller(kj::mv(fulfiller)) {} ~QuestionRef() { unwindDetector.catchExceptionsIfUnwinding([&]() { auto& question = KJ_ASSERT_NONNULL( connectionState->questions.find(id), "Question ID no longer on table?"); // Send the "Finish" message (if the connection is not already broken). if (connectionState->connection.is() && !question.skipFinish) { auto message = connectionState->connection.get()->newOutgoingMessage( messageSizeHint()); auto builder = message->getBody().getAs().initFinish(); builder.setQuestionId(id); // If we're still awaiting a return, then this request is being canceled, and we're going // to ignore any capabilities in the return message, so set releaseResultCaps true. If we // already received the return, then we've already built local proxies for the caps and // will send Release messages when those are destroyed. builder.setReleaseResultCaps(question.isAwaitingReturn); message->send(); } // Check if the question has returned and, if so, remove it from the table. // Remove question ID from the table. Must do this *after* sending `Finish` to ensure that // the ID is not re-allocated before the `Finish` message can be sent. if (question.isAwaitingReturn) { // Still waiting for return, so just remove the QuestionRef pointer from the table. question.selfRef = nullptr; } else { // Call has already returned, so we can now remove it from the table. connectionState->questions.erase(id, question); } }); } inline QuestionId getId() const { return id; } void fulfill(kj::Own&& response) { fulfiller->fulfill(kj::mv(response)); } void fulfill(kj::Promise>&& promise) { fulfiller->fulfill(kj::mv(promise)); } void reject(kj::Exception&& exception) { fulfiller->reject(kj::mv(exception)); } private: kj::Own connectionState; QuestionId id; kj::Own>>> fulfiller; kj::UnwindDetector unwindDetector; }; class RpcRequest final: public RequestHook { public: RpcRequest(RpcConnectionState& connectionState, VatNetworkBase::Connection& connection, kj::Maybe sizeHint, kj::Own&& target) : connectionState(kj::addRef(connectionState)), target(kj::mv(target)), message(connection.newOutgoingMessage( firstSegmentSize(sizeHint, messageSizeHint() + sizeInWords() + MESSAGE_TARGET_SIZE_HINT))), callBuilder(message->getBody().getAs().initCall()), paramsBuilder(capTable.imbue(callBuilder.getParams().getContent())) {} inline AnyPointer::Builder getRoot() { return paramsBuilder; } inline rpc::Call::Builder getCall() { return callBuilder; } RemotePromise send() override { if (!connectionState->connection.is()) { // Connection is broken. const kj::Exception& e = connectionState->connection.get(); return RemotePromise( kj::Promise>(kj::cp(e)), AnyPointer::Pipeline(newBrokenPipeline(kj::cp(e)))); } KJ_IF_MAYBE(redirect, target->writeTarget(callBuilder.getTarget())) { // Whoops, this capability has been redirected while we were building the request! // We'll have to make a new request and do a copy. Ick. auto replacement = redirect->get()->newCall( callBuilder.getInterfaceId(), callBuilder.getMethodId(), paramsBuilder.targetSize()); replacement.set(paramsBuilder); return replacement.send(); } else { auto sendResult = sendInternal(false); auto forkedPromise = sendResult.promise.fork(); // The pipeline must get notified of resolution before the app does to maintain ordering. auto pipeline = kj::refcounted( *connectionState, kj::mv(sendResult.questionRef), forkedPromise.addBranch()); auto appPromise = forkedPromise.addBranch().then( [=](kj::Own&& response) { auto reader = response->getResults(); return Response(reader, kj::mv(response)); }); return RemotePromise( kj::mv(appPromise), AnyPointer::Pipeline(kj::mv(pipeline))); } } kj::Promise sendStreaming() override { if (!connectionState->connection.is()) { // Connection is broken. return kj::cp(connectionState->connection.get()); } KJ_IF_MAYBE(redirect, target->writeTarget(callBuilder.getTarget())) { // Whoops, this capability has been redirected while we were building the request! // We'll have to make a new request and do a copy. Ick. auto replacement = redirect->get()->newCall( callBuilder.getInterfaceId(), callBuilder.getMethodId(), paramsBuilder.targetSize()); replacement.set(paramsBuilder); return RequestHook::from(kj::mv(replacement))->sendStreaming(); } else { return sendStreamingInternal(false); } } struct TailInfo { QuestionId questionId; kj::Promise promise; kj::Own pipeline; }; kj::Maybe tailSend() { // Send the request as a tail call. // // Returns null if for some reason a tail call is not possible and the caller should fall // back to using send() and copying the response. SendInternalResult sendResult; if (!connectionState->connection.is()) { // Disconnected; fall back to a regular send() which will fail appropriately. return nullptr; } KJ_IF_MAYBE(redirect, target->writeTarget(callBuilder.getTarget())) { // Whoops, this capability has been redirected while we were building the request! // Fall back to regular send(). return nullptr; } else { sendResult = sendInternal(true); } auto promise = sendResult.promise.then([](kj::Own&& response) { // Response should be null if `Return` handling code is correct. KJ_ASSERT(!response) { break; } }); QuestionId questionId = sendResult.questionRef->getId(); auto pipeline = kj::refcounted(*connectionState, kj::mv(sendResult.questionRef)); return TailInfo { questionId, kj::mv(promise), kj::mv(pipeline) }; } const void* getBrand() override { return connectionState.get(); } private: kj::Own connectionState; kj::Own target; kj::Own message; BuilderCapabilityTable capTable; rpc::Call::Builder callBuilder; AnyPointer::Builder paramsBuilder; struct SendInternalResult { kj::Own questionRef; kj::Promise> promise = nullptr; }; struct SetupSendResult: public SendInternalResult { QuestionId questionId; Question& question; SetupSendResult(SendInternalResult&& super, QuestionId questionId, Question& question) : SendInternalResult(kj::mv(super)), questionId(questionId), question(question) {} // TODO(cleanup): This constructor is implicit in C++17. }; SetupSendResult setupSend(bool isTailCall) { // Build the cap table. kj::Vector fds; auto exports = connectionState->writeDescriptors( capTable.getTable(), callBuilder.getParams(), fds); message->setFds(fds.releaseAsArray()); // Init the question table. Do this after writing descriptors to avoid interference. QuestionId questionId; auto& question = connectionState->questions.next(questionId); question.isAwaitingReturn = true; question.paramExports = kj::mv(exports); question.isTailCall = isTailCall; // Make the QuentionRef and result promise. SendInternalResult result; auto paf = kj::newPromiseAndFulfiller>>(); result.questionRef = kj::refcounted( *connectionState, questionId, kj::mv(paf.fulfiller)); question.selfRef = *result.questionRef; result.promise = paf.promise.attach(kj::addRef(*result.questionRef)); return { kj::mv(result), questionId, question }; } SendInternalResult sendInternal(bool isTailCall) { auto result = setupSend(isTailCall); // Finish and send. callBuilder.setQuestionId(result.questionId); if (isTailCall) { callBuilder.getSendResultsTo().setYourself(); } KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { KJ_CONTEXT("sending RPC call", callBuilder.getInterfaceId(), callBuilder.getMethodId()); message->send(); })) { // We can't safely throw the exception from here since we've already modified the question // table state. We'll have to reject the promise instead. result.question.isAwaitingReturn = false; result.question.skipFinish = true; result.questionRef->reject(kj::mv(*exception)); } // Send and return. return kj::mv(result); } kj::Promise sendStreamingInternal(bool isTailCall) { auto setup = setupSend(isTailCall); // Finish and send. callBuilder.setQuestionId(setup.questionId); if (isTailCall) { callBuilder.getSendResultsTo().setYourself(); } kj::Promise flowPromise = nullptr; KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { KJ_CONTEXT("sending RPC call", callBuilder.getInterfaceId(), callBuilder.getMethodId()); RpcFlowController* flow; KJ_IF_MAYBE(f, target->flowController) { flow = *f; } else { flow = target->flowController.emplace( connectionState->connection.get()->newStream()); } flowPromise = flow->send(kj::mv(message), setup.promise.ignoreResult()); })) { // We can't safely throw the exception from here since we've already modified the question // table state. We'll have to reject the promise instead. setup.question.isAwaitingReturn = false; setup.question.skipFinish = true; setup.questionRef->reject(kj::cp(*exception)); return kj::mv(*exception); } return kj::mv(flowPromise); } }; class RpcPipeline final: public PipelineHook, public kj::Refcounted { public: RpcPipeline(RpcConnectionState& connectionState, kj::Own&& questionRef, kj::Promise>&& redirectLaterParam) : connectionState(kj::addRef(connectionState)), redirectLater(redirectLaterParam.fork()), resolveSelfPromise(KJ_ASSERT_NONNULL(redirectLater).addBranch().then( [this](kj::Own&& response) { resolve(kj::mv(response)); }, [this](kj::Exception&& exception) { resolve(kj::mv(exception)); }).eagerlyEvaluate([&](kj::Exception&& e) { // Make any exceptions thrown from resolve() go to the connection's TaskSet which // will cause the connection to be terminated. connectionState.tasks.add(kj::mv(e)); })) { // Construct a new RpcPipeline. state.init(kj::mv(questionRef)); } RpcPipeline(RpcConnectionState& connectionState, kj::Own&& questionRef) : connectionState(kj::addRef(connectionState)), resolveSelfPromise(nullptr) { // Construct a new RpcPipeline that is never expected to resolve. state.init(kj::mv(questionRef)); } // implements PipelineHook --------------------------------------- kj::Own addRef() override { return kj::addRef(*this); } kj::Own getPipelinedCap(kj::ArrayPtr ops) override { auto copy = kj::heapArrayBuilder(ops.size()); for (auto& op: ops) { copy.add(op); } return getPipelinedCap(copy.finish()); } kj::Own getPipelinedCap(kj::Array&& ops) override { return clientMap.findOrCreate(ops.asPtr(), [&]() { if (state.is()) { // Wrap a PipelineClient in a PromiseClient. auto pipelineClient = kj::refcounted( *connectionState, kj::addRef(*state.get()), kj::heapArray(ops.asPtr())); KJ_IF_MAYBE(r, redirectLater) { auto resolutionPromise = r->addBranch().then( [ops = kj::heapArray(ops.asPtr())](kj::Own&& response) { return response->getResults().getPipelinedCap(kj::mv(ops)); }); return kj::HashMap, kj::Own>::Entry { kj::mv(ops), kj::refcounted( *connectionState, kj::mv(pipelineClient), kj::mv(resolutionPromise), nullptr) }; } else { // Oh, this pipeline will never get redirected, so just return the PipelineClient. return kj::HashMap, kj::Own>::Entry { kj::mv(ops), kj::mv(pipelineClient) }; } } else if (state.is()) { auto pipelineClient = state.get()->getResults().getPipelinedCap(ops); return kj::HashMap, kj::Own>::Entry { kj::mv(ops), kj::mv(pipelineClient) }; } else { return kj::HashMap, kj::Own>::Entry { kj::mv(ops), newBrokenCap(kj::cp(state.get())) }; } })->addRef(); } private: kj::Own connectionState; kj::Maybe>> redirectLater; typedef kj::Own Waiting; typedef kj::Own Resolved; typedef kj::Exception Broken; kj::OneOf state; kj::HashMap, kj::Own> clientMap; // See QueuedPipeline::clientMap in capability.c++ for a discussion of why we must memoize // the results of getPipelinedCap(). RpcPipeline has a similar problem when a capability we // return is later subject to an embargo. It's important that the embargo is correctly applied // across all calls to the same capability. // Keep this last, because the continuation uses *this, so it should be destroyed first to // ensure the continuation is not still running. kj::Promise resolveSelfPromise; void resolve(kj::Own&& response) { KJ_ASSERT(state.is(), "Already resolved?"); state.init(kj::mv(response)); } void resolve(const kj::Exception&& exception) { KJ_ASSERT(state.is(), "Already resolved?"); state.init(kj::mv(exception)); } }; class RpcResponse: public ResponseHook { public: virtual AnyPointer::Reader getResults() = 0; virtual kj::Own addRef() = 0; }; class RpcResponseImpl final: public RpcResponse, public kj::Refcounted { public: RpcResponseImpl(RpcConnectionState& connectionState, kj::Own&& questionRef, kj::Own&& message, kj::Array>> capTableArray, AnyPointer::Reader results) : connectionState(kj::addRef(connectionState)), message(kj::mv(message)), capTable(kj::mv(capTableArray)), reader(capTable.imbue(results)), questionRef(kj::mv(questionRef)) {} AnyPointer::Reader getResults() override { return reader; } kj::Own addRef() override { return kj::addRef(*this); } private: kj::Own connectionState; kj::Own message; ReaderCapabilityTable capTable; AnyPointer::Reader reader; kj::Own questionRef; }; // ===================================================================================== // CallContextHook implementation class RpcServerResponse { public: virtual AnyPointer::Builder getResultsBuilder() = 0; }; class RpcServerResponseImpl final: public RpcServerResponse { public: RpcServerResponseImpl(RpcConnectionState& connectionState, kj::Own&& message, rpc::Payload::Builder payload) : connectionState(connectionState), message(kj::mv(message)), payload(payload) {} AnyPointer::Builder getResultsBuilder() override { return capTable.imbue(payload.getContent()); } kj::Maybe> send() { // Send the response and return the export list. Returns nullptr if there were no caps. // (Could return a non-null empty array if there were caps but none of them were exports.) // Build the cap table. auto capTable = this->capTable.getTable(); kj::Vector fds; auto exports = connectionState.writeDescriptors(capTable, payload, fds); message->setFds(fds.releaseAsArray()); // Capabilities that we are returning are subject to embargos. See `Disembargo` in rpc.capnp. // As explained there, in order to deal with the Tribble 4-way race condition, we need to // make sure that if we're returning any remote promises, that we ignore any subsequent // resolution of those promises for the purpose of pipelined requests on this answer. Luckily, // we can modify the cap table in-place. for (auto& slot: capTable) { KJ_IF_MAYBE(cap, slot) { slot = connectionState.getInnermostClient(**cap); } } message->send(); if (capTable.size() == 0) { return nullptr; } else { return kj::mv(exports); } } private: RpcConnectionState& connectionState; kj::Own message; BuilderCapabilityTable capTable; rpc::Payload::Builder payload; }; class LocallyRedirectedRpcResponse final : public RpcResponse, public RpcServerResponse, public kj::Refcounted{ public: LocallyRedirectedRpcResponse(kj::Maybe sizeHint) : message(sizeHint.map([](MessageSize size) { return size.wordCount; }) .orDefault(SUGGESTED_FIRST_SEGMENT_WORDS)) {} AnyPointer::Builder getResultsBuilder() override { return message.getRoot(); } AnyPointer::Reader getResults() override { return message.getRoot(); } kj::Own addRef() override { return kj::addRef(*this); } private: MallocMessageBuilder message; }; class RpcCallContext final: public CallContextHook, public kj::Refcounted { public: RpcCallContext(RpcConnectionState& connectionState, AnswerId answerId, kj::Own&& request, kj::Array>> capTableArray, const AnyPointer::Reader& params, bool redirectResults, kj::Own>&& cancelFulfiller, uint64_t interfaceId, uint16_t methodId) : connectionState(kj::addRef(connectionState)), answerId(answerId), interfaceId(interfaceId), methodId(methodId), requestSize(request->sizeInWords()), request(kj::mv(request)), paramsCapTable(kj::mv(capTableArray)), params(paramsCapTable.imbue(params)), returnMessage(nullptr), redirectResults(redirectResults), cancelFulfiller(kj::mv(cancelFulfiller)) { connectionState.callWordsInFlight += requestSize; } ~RpcCallContext() noexcept(false) { if (isFirstResponder()) { // We haven't sent a return yet, so we must have been canceled. Send a cancellation return. unwindDetector.catchExceptionsIfUnwinding([&]() { // Don't send anything if the connection is broken. bool shouldFreePipeline = true; if (connectionState->connection.is()) { auto message = connectionState->connection.get()->newOutgoingMessage( messageSizeHint() + sizeInWords()); auto builder = message->getBody().initAs().initReturn(); builder.setAnswerId(answerId); builder.setReleaseParamCaps(false); if (redirectResults) { // The reason we haven't sent a return is because the results were sent somewhere // else. builder.setResultsSentElsewhere(); // The pipeline could still be valid and in-use in this case. shouldFreePipeline = false; } else { builder.setCanceled(); } message->send(); } cleanupAnswerTable(nullptr, shouldFreePipeline); }); } } kj::Own consumeRedirectedResponse() { KJ_ASSERT(redirectResults); if (response == nullptr) getResults(MessageSize{0, 0}); // force initialization of response // Note that the context needs to keep its own reference to the response so that it doesn't // get GC'd until the PipelineHook drops its reference to the context. return kj::downcast(*KJ_ASSERT_NONNULL(response)).addRef(); } void sendReturn() { KJ_ASSERT(!redirectResults); // Avoid sending results if canceled so that we don't have to figure out whether or not // `releaseResultCaps` was set in the already-received `Finish`. if (!(cancellationFlags & CANCEL_REQUESTED) && isFirstResponder()) { KJ_ASSERT(connectionState->connection.is(), "Cancellation should have been requested on disconnect.") { return; } if (response == nullptr) getResults(MessageSize{0, 0}); // force initialization of response returnMessage.setAnswerId(answerId); returnMessage.setReleaseParamCaps(false); kj::Maybe> exports; KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { // Debug info incase send() fails due to overside message. KJ_CONTEXT("returning from RPC call", interfaceId, methodId); exports = kj::downcast(*KJ_ASSERT_NONNULL(response)).send(); })) { responseSent = false; sendErrorReturn(kj::mv(*exception)); return; } KJ_IF_MAYBE(e, exports) { // Caps were returned, so we can't free the pipeline yet. cleanupAnswerTable(kj::mv(*e), false); } else { // No caps in the results, therefore the pipeline is irrelevant. cleanupAnswerTable(nullptr, true); } } } void sendErrorReturn(kj::Exception&& exception) { KJ_ASSERT(!redirectResults); if (isFirstResponder()) { if (connectionState->connection.is()) { auto message = connectionState->connection.get()->newOutgoingMessage( messageSizeHint() + exceptionSizeHint(exception)); auto builder = message->getBody().initAs().initReturn(); builder.setAnswerId(answerId); builder.setReleaseParamCaps(false); fromException(exception, builder.initException()); message->send(); } // Do not allow releasing the pipeline because we want pipelined calls to propagate the // exception rather than fail with a "no such field" exception. cleanupAnswerTable(nullptr, false); } } void requestCancel() { // Hints that the caller wishes to cancel this call. At the next time when cancellation is // deemed safe, the RpcCallContext shall send a canceled Return -- or if it never becomes // safe, the RpcCallContext will send a normal return when the call completes. Either way // the RpcCallContext is now responsible for cleaning up the entry in the answer table, since // a Finish message was already received. bool previouslyAllowedButNotRequested = cancellationFlags == CANCEL_ALLOWED; cancellationFlags |= CANCEL_REQUESTED; if (previouslyAllowedButNotRequested) { // We just set CANCEL_REQUESTED, and CANCEL_ALLOWED was already set previously. Initiate // the cancellation. cancelFulfiller->fulfill(); } } // implements CallContextHook ------------------------------------ AnyPointer::Reader getParams() override { KJ_REQUIRE(request != nullptr, "Can't call getParams() after releaseParams()."); return params; } void releaseParams() override { request = nullptr; } AnyPointer::Builder getResults(kj::Maybe sizeHint) override { KJ_IF_MAYBE(r, response) { return r->get()->getResultsBuilder(); } else { kj::Own response; if (redirectResults || !connectionState->connection.is()) { response = kj::refcounted(sizeHint); } else { auto message = connectionState->connection.get()->newOutgoingMessage( firstSegmentSize(sizeHint, messageSizeHint() + sizeInWords())); returnMessage = message->getBody().initAs().initReturn(); response = kj::heap( *connectionState, kj::mv(message), returnMessage.getResults()); } auto results = response->getResultsBuilder(); this->response = kj::mv(response); return results; } } kj::Promise tailCall(kj::Own&& request) override { auto result = directTailCall(kj::mv(request)); KJ_IF_MAYBE(f, tailCallPipelineFulfiller) { f->get()->fulfill(AnyPointer::Pipeline(kj::mv(result.pipeline))); } return kj::mv(result.promise); } ClientHook::VoidPromiseAndPipeline directTailCall(kj::Own&& request) override { KJ_REQUIRE(response == nullptr, "Can't call tailCall() after initializing the results struct."); if (request->getBrand() == connectionState.get() && !redirectResults) { // The tail call is headed towards the peer that called us in the first place, so we can // optimize out the return trip. KJ_IF_MAYBE(tailInfo, kj::downcast(*request).tailSend()) { if (isFirstResponder()) { if (connectionState->connection.is()) { auto message = connectionState->connection.get()->newOutgoingMessage( messageSizeHint()); auto builder = message->getBody().initAs().initReturn(); builder.setAnswerId(answerId); builder.setReleaseParamCaps(false); builder.setTakeFromOtherQuestion(tailInfo->questionId); message->send(); } // There are no caps in our return message, but of course the tail results could have // caps, so we must continue to honor pipeline calls (and just bounce them back). cleanupAnswerTable(nullptr, false); } return { kj::mv(tailInfo->promise), kj::mv(tailInfo->pipeline) }; } } // Just forwarding to another local call. auto promise = request->send(); // Wait for response. auto voidPromise = promise.then([this](Response&& tailResponse) { // Copy the response. // TODO(perf): It would be nice if we could somehow make the response get built in-place // but requires some refactoring. getResults(tailResponse.targetSize()).set(tailResponse); }); return { kj::mv(voidPromise), PipelineHook::from(kj::mv(promise)) }; } kj::Promise onTailCall() override { auto paf = kj::newPromiseAndFulfiller(); tailCallPipelineFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } void allowCancellation() override { bool previouslyRequestedButNotAllowed = cancellationFlags == CANCEL_REQUESTED; cancellationFlags |= CANCEL_ALLOWED; if (previouslyRequestedButNotAllowed) { // We just set CANCEL_ALLOWED, and CANCEL_REQUESTED was already set previously. Initiate // the cancellation. cancelFulfiller->fulfill(); } } kj::Own addRef() override { return kj::addRef(*this); } private: kj::Own connectionState; AnswerId answerId; uint64_t interfaceId; uint16_t methodId; // For debugging. // Request --------------------------------------------- size_t requestSize; // for flow limit purposes kj::Maybe> request; ReaderCapabilityTable paramsCapTable; AnyPointer::Reader params; // Response -------------------------------------------- kj::Maybe> response; rpc::Return::Builder returnMessage; bool redirectResults = false; bool responseSent = false; kj::Maybe>> tailCallPipelineFulfiller; // Cancellation state ---------------------------------- enum CancellationFlags { CANCEL_REQUESTED = 1, CANCEL_ALLOWED = 2 }; uint8_t cancellationFlags = 0; // When both flags are set, the cancellation process will begin. kj::Own> cancelFulfiller; // Fulfilled when cancellation has been both requested and permitted. The fulfilled promise is // exclusive-joined with the outermost promise waiting on the call return, so fulfilling it // cancels that promise. kj::UnwindDetector unwindDetector; // ----------------------------------------------------- bool isFirstResponder() { if (responseSent) { return false; } else { responseSent = true; return true; } } void cleanupAnswerTable(kj::Array resultExports, bool shouldFreePipeline) { // We need to remove the `callContext` pointer -- which points back to us -- from the // answer table. Or we might even be responsible for removing the entire answer table // entry. if (cancellationFlags & CANCEL_REQUESTED) { // Already received `Finish` so it's our job to erase the table entry. We shouldn't have // sent results if canceled, so we shouldn't have an export list to deal with. KJ_ASSERT(resultExports.size() == 0); connectionState->answers.erase(answerId); } else { // We just have to null out callContext and set the exports. auto& answer = connectionState->answers[answerId]; answer.callContext = nullptr; answer.resultExports = kj::mv(resultExports); if (shouldFreePipeline) { // We can free the pipeline early, because we know all pipeline calls are invalid (e.g. // because there are no caps in the result to receive pipeline requests). KJ_ASSERT(resultExports.size() == 0); answer.pipeline = nullptr; } } // Also, this is the right time to stop counting the call against the flow limit. connectionState->callWordsInFlight -= requestSize; connectionState->maybeUnblockFlow(); } }; // ===================================================================================== // Message handling void maybeUnblockFlow() { if (callWordsInFlight < flowLimit) { KJ_IF_MAYBE(w, flowWaiter) { w->get()->fulfill(); flowWaiter = nullptr; } } } kj::Promise messageLoop() { if (!connection.is()) { return kj::READY_NOW; } if (callWordsInFlight > flowLimit) { auto paf = kj::newPromiseAndFulfiller(); flowWaiter = kj::mv(paf.fulfiller); return paf.promise.then([this]() { return messageLoop(); }); } return canceler.wrap(connection.get()->receiveIncomingMessage()).then( [this](kj::Maybe>&& message) { KJ_IF_MAYBE(m, message) { handleMessage(kj::mv(*m)); return true; } else { disconnect(KJ_EXCEPTION(DISCONNECTED, "Peer disconnected.")); return false; } }).then([this](bool keepGoing) { // No exceptions; continue loop. // // (We do this in a separate continuation to handle the case where exceptions are // disabled.) // // TODO(perf): We add an evalLater() here so that anything we needed to do in reaction to // the previous message has a chance to complete before the next message is handled. In // paticular, without this, I observed an ordering problem: I saw a case where a `Return` // message was followed by a `Resolve` message, but the `PromiseClient` associated with the // `Resolve` had its `resolve()` method invoked _before_ any `PromiseClient`s associated // with pipelined capabilities resolved by the `Return`. This could lead to an // incorrectly-ordered interaction between `PromiseClient`s when they resolve to each // other. This is probably really a bug in the way `Return`s are handled -- apparently, // resolution of `PromiseClient`s based on returned capabilites does not occur in a // depth-first way, when it should. If we could fix that then we can probably remove this // `evalLater()`. However, the `evalLater()` is not that bad and solves the problem... if (keepGoing) tasks.add(kj::evalLater([this]() { return messageLoop(); })); }); } void handleMessage(kj::Own message) { auto reader = message->getBody().getAs(); switch (reader.which()) { case rpc::Message::UNIMPLEMENTED: handleUnimplemented(reader.getUnimplemented()); break; case rpc::Message::ABORT: handleAbort(reader.getAbort()); break; case rpc::Message::BOOTSTRAP: handleBootstrap(kj::mv(message), reader.getBootstrap()); break; case rpc::Message::CALL: handleCall(kj::mv(message), reader.getCall()); break; case rpc::Message::RETURN: handleReturn(kj::mv(message), reader.getReturn()); break; case rpc::Message::FINISH: handleFinish(reader.getFinish()); break; case rpc::Message::RESOLVE: handleResolve(kj::mv(message), reader.getResolve()); break; case rpc::Message::RELEASE: handleRelease(reader.getRelease()); break; case rpc::Message::DISEMBARGO: handleDisembargo(reader.getDisembargo()); break; default: { if (connection.is()) { auto message = connection.get()->newOutgoingMessage( firstSegmentSize(reader.totalSize(), messageSizeHint())); message->getBody().initAs().setUnimplemented(reader); message->send(); } break; } } } void handleUnimplemented(const rpc::Message::Reader& message) { switch (message.which()) { case rpc::Message::RESOLVE: { auto resolve = message.getResolve(); switch (resolve.which()) { case rpc::Resolve::CAP: { auto cap = resolve.getCap(); switch (cap.which()) { case rpc::CapDescriptor::NONE: // Nothing to do (but this ought never to happen). break; case rpc::CapDescriptor::SENDER_HOSTED: releaseExport(cap.getSenderHosted(), 1); break; case rpc::CapDescriptor::SENDER_PROMISE: releaseExport(cap.getSenderPromise(), 1); break; case rpc::CapDescriptor::RECEIVER_ANSWER: case rpc::CapDescriptor::RECEIVER_HOSTED: // Nothing to do. break; case rpc::CapDescriptor::THIRD_PARTY_HOSTED: releaseExport(cap.getThirdPartyHosted().getVineId(), 1); break; } break; } case rpc::Resolve::EXCEPTION: // Nothing to do. break; } break; } default: KJ_FAIL_ASSERT("Peer did not implement required RPC message type.", (uint)message.which()); break; } } void handleAbort(const rpc::Exception::Reader& exception) { kj::throwRecoverableException(toException(exception)); } // --------------------------------------------------------------------------- // Level 0 class SingleCapPipeline: public PipelineHook, public kj::Refcounted { public: SingleCapPipeline(kj::Own&& cap) : cap(kj::mv(cap)) {} kj::Own addRef() override { return kj::addRef(*this); } kj::Own getPipelinedCap(kj::ArrayPtr ops) override { if (ops.size() == 0) { return cap->addRef(); } else { return newBrokenCap("Invalid pipeline transform."); } } private: kj::Own cap; }; void handleBootstrap(kj::Own&& message, const rpc::Bootstrap::Reader& bootstrap) { AnswerId answerId = bootstrap.getQuestionId(); if (!connection.is()) { // Disconnected; ignore. return; } VatNetworkBase::Connection& conn = *connection.get(); auto response = conn.newOutgoingMessage( messageSizeHint() + sizeInWords() + 32); rpc::Return::Builder ret = response->getBody().getAs().initReturn(); ret.setAnswerId(answerId); kj::Own capHook; kj::Array resultExports; KJ_DEFER(releaseExports(resultExports)); // in case something goes wrong // Call the restorer and initialize the answer. KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { Capability::Client cap = nullptr; if (bootstrap.hasDeprecatedObjectId()) { KJ_IF_MAYBE(r, restorer) { cap = r->baseRestore(bootstrap.getDeprecatedObjectId()); } else { KJ_FAIL_REQUIRE("This vat only supports a bootstrap interface, not the old " "Cap'n-Proto-0.4-style named exports.") { return; } } } else { cap = bootstrapFactory.baseCreateFor(conn.baseGetPeerVatId()); } BuilderCapabilityTable capTable; auto payload = ret.initResults(); capTable.imbue(payload.getContent()).setAs(kj::mv(cap)); auto capTableArray = capTable.getTable(); KJ_DASSERT(capTableArray.size() == 1); kj::Vector fds; resultExports = writeDescriptors(capTableArray, payload, fds); response->setFds(fds.releaseAsArray()); capHook = KJ_ASSERT_NONNULL(capTableArray[0])->addRef(); })) { fromException(*exception, ret.initException()); capHook = newBrokenCap(kj::mv(*exception)); } message = nullptr; // Add the answer to the answer table for pipelining and send the response. auto& answer = answers[answerId]; KJ_REQUIRE(!answer.active, "questionId is already in use", answerId) { return; } answer.resultExports = kj::mv(resultExports); answer.active = true; answer.pipeline = kj::Own(kj::refcounted(kj::mv(capHook))); response->send(); } void handleCall(kj::Own&& message, const rpc::Call::Reader& call) { kj::Own capability; KJ_IF_MAYBE(t, getMessageTarget(call.getTarget())) { capability = kj::mv(*t); } else { // Exception already reported. return; } bool redirectResults; switch (call.getSendResultsTo().which()) { case rpc::Call::SendResultsTo::CALLER: redirectResults = false; break; case rpc::Call::SendResultsTo::YOURSELF: redirectResults = true; break; default: KJ_FAIL_REQUIRE("Unsupported `Call.sendResultsTo`.") { return; } } auto payload = call.getParams(); auto capTableArray = receiveCaps(payload.getCapTable(), message->getAttachedFds()); auto cancelPaf = kj::newPromiseAndFulfiller(); AnswerId answerId = call.getQuestionId(); auto context = kj::refcounted( *this, answerId, kj::mv(message), kj::mv(capTableArray), payload.getContent(), redirectResults, kj::mv(cancelPaf.fulfiller), call.getInterfaceId(), call.getMethodId()); // No more using `call` after this point, as it now belongs to the context. { auto& answer = answers[answerId]; KJ_REQUIRE(!answer.active, "questionId is already in use") { return; } answer.active = true; answer.callContext = *context; } auto promiseAndPipeline = startCall( call.getInterfaceId(), call.getMethodId(), kj::mv(capability), context->addRef()); // Things may have changed -- in particular if startCall() immediately called // context->directTailCall(). { auto& answer = answers[answerId]; answer.pipeline = kj::mv(promiseAndPipeline.pipeline); if (redirectResults) { auto resultsPromise = promiseAndPipeline.promise.then( kj::mvCapture(context, [](kj::Own&& context) { return context->consumeRedirectedResponse(); })); // If the call that later picks up `redirectedResults` decides to discard it, we need to // make sure our call is not itself canceled unless it has called allowCancellation(). // So we fork the promise and join one branch with the cancellation promise, in order to // hold on to it. auto forked = resultsPromise.fork(); answer.redirectedResults = forked.addBranch(); cancelPaf.promise .exclusiveJoin(forked.addBranch().then([](kj::Own&&){})) .detach([](kj::Exception&&) {}); } else { // Hack: Both the success and error continuations need to use the context. We could // refcount, but both will be destroyed at the same time anyway. RpcCallContext* contextPtr = context; promiseAndPipeline.promise.then( [contextPtr]() { contextPtr->sendReturn(); }, [contextPtr](kj::Exception&& exception) { contextPtr->sendErrorReturn(kj::mv(exception)); }).catch_([&](kj::Exception&& exception) { // Handle exceptions that occur in sendReturn()/sendErrorReturn(). taskFailed(kj::mv(exception)); }).attach(kj::mv(context)) .exclusiveJoin(kj::mv(cancelPaf.promise)) .detach([](kj::Exception&&) {}); } } } ClientHook::VoidPromiseAndPipeline startCall( uint64_t interfaceId, uint64_t methodId, kj::Own&& capability, kj::Own&& context) { if (interfaceId == typeId>() && methodId == 0) { KJ_IF_MAYBE(g, gateway) { // Wait, this is a call to Persistent.save() and we need to translate it through our // gateway. KJ_IF_MAYBE(resolvedPromise, capability->whenMoreResolved()) { // The plot thickens: We're looking at a promise capability. It could end up resolving // to a capability outside the gateway, in which case we don't want to translate at all. auto promises = resolvedPromise->then(kj::mvCapture(context, [this,interfaceId,methodId](kj::Own&& context, kj::Own resolvedCap) { auto vpap = startCall(interfaceId, methodId, kj::mv(resolvedCap), kj::mv(context)); return kj::tuple(kj::mv(vpap.promise), kj::mv(vpap.pipeline)); })).attach(addRef(*this), kj::mv(capability)).split(); return { kj::mv(kj::get<0>(promises)), newLocalPromisePipeline(kj::mv(kj::get<1>(promises))), }; } if (capability->getBrand() == this) { // This capability is one of our own, pointing back out over the network. That means // that it would be inappropriate to apply the gateway transformation. We just want to // reflect the call back. return kj::downcast(*capability) .callNoIntercept(interfaceId, methodId, kj::mv(context)); } auto params = context->getParams().getAs::SaveParams>(); auto requestSize = params.totalSize(); ++requestSize.capCount; requestSize.wordCount += sizeInWords::ExportParams>(); auto request = g->exportRequest(requestSize); request.setCap(Persistent<>::Client(capability->addRef())); request.setParams(params); context->allowCancellation(); context->releaseParams(); return context->directTailCall(RequestHook::from(kj::mv(request))); } } return capability->call(interfaceId, methodId, kj::mv(context)); } kj::Maybe> getMessageTarget(const rpc::MessageTarget::Reader& target) { switch (target.which()) { case rpc::MessageTarget::IMPORTED_CAP: { KJ_IF_MAYBE(exp, exports.find(target.getImportedCap())) { return exp->clientHook->addRef(); } else { KJ_FAIL_REQUIRE("Message target is not a current export ID.") { return nullptr; } } break; } case rpc::MessageTarget::PROMISED_ANSWER: { auto promisedAnswer = target.getPromisedAnswer(); kj::Own pipeline; auto& base = answers[promisedAnswer.getQuestionId()]; KJ_REQUIRE(base.active, "PromisedAnswer.questionId is not a current question.") { return nullptr; } KJ_IF_MAYBE(p, base.pipeline) { pipeline = p->get()->addRef(); } else { pipeline = newBrokenPipeline(KJ_EXCEPTION(FAILED, "Pipeline call on a request that returned no capabilities or was already closed.")); } KJ_IF_MAYBE(ops, toPipelineOps(promisedAnswer.getTransform())) { return pipeline->getPipelinedCap(*ops); } else { // Exception already thrown. return nullptr; } } default: KJ_FAIL_REQUIRE("Unknown message target type.", target) { return nullptr; } } KJ_UNREACHABLE; } void handleReturn(kj::Own&& message, const rpc::Return::Reader& ret) { // Transitive destructors can end up manipulating the question table and invalidating our // pointer into it, so make sure these destructors run later. kj::Array exportsToRelease; KJ_DEFER(releaseExports(exportsToRelease)); kj::Maybe>> promiseToRelease; KJ_IF_MAYBE(question, questions.find(ret.getAnswerId())) { KJ_REQUIRE(question->isAwaitingReturn, "Duplicate Return.") { return; } question->isAwaitingReturn = false; if (ret.getReleaseParamCaps()) { exportsToRelease = kj::mv(question->paramExports); } else { question->paramExports = nullptr; } KJ_IF_MAYBE(questionRef, question->selfRef) { switch (ret.which()) { case rpc::Return::RESULTS: { KJ_REQUIRE(!question->isTailCall, "Tail call `Return` must set `resultsSentElsewhere`, not `results`.") { return; } auto payload = ret.getResults(); auto capTableArray = receiveCaps(payload.getCapTable(), message->getAttachedFds()); questionRef->fulfill(kj::refcounted( *this, kj::addRef(*questionRef), kj::mv(message), kj::mv(capTableArray), payload.getContent())); break; } case rpc::Return::EXCEPTION: KJ_REQUIRE(!question->isTailCall, "Tail call `Return` must set `resultsSentElsewhere`, not `exception`.") { return; } questionRef->reject(toException(ret.getException())); break; case rpc::Return::CANCELED: KJ_FAIL_REQUIRE("Return message falsely claims call was canceled.") { return; } break; case rpc::Return::RESULTS_SENT_ELSEWHERE: KJ_REQUIRE(question->isTailCall, "`Return` had `resultsSentElsewhere` but this was not a tail call.") { return; } // Tail calls are fulfilled with a null pointer. questionRef->fulfill(kj::Own()); break; case rpc::Return::TAKE_FROM_OTHER_QUESTION: KJ_IF_MAYBE(answer, answers.find(ret.getTakeFromOtherQuestion())) { KJ_IF_MAYBE(response, answer->redirectedResults) { questionRef->fulfill(kj::mv(*response)); answer->redirectedResults = nullptr; } else { KJ_FAIL_REQUIRE("`Return.takeFromOtherQuestion` referenced a call that did not " "use `sendResultsTo.yourself`.") { return; } } } else { KJ_FAIL_REQUIRE("`Return.takeFromOtherQuestion` had invalid answer ID.") { return; } } break; default: KJ_FAIL_REQUIRE("Unknown 'Return' type.") { return; } } } else { if (ret.isTakeFromOtherQuestion()) { // Be sure to release the tail call's promise. KJ_IF_MAYBE(answer, answers.find(ret.getTakeFromOtherQuestion())) { promiseToRelease = kj::mv(answer->redirectedResults); } } // Looks like this question was canceled earlier, so `Finish` was already sent, with // `releaseResultCaps` set true so that we don't have to release them here. We can go // ahead and delete it from the table. questions.erase(ret.getAnswerId(), *question); } } else { KJ_FAIL_REQUIRE("Invalid question ID in Return message.") { return; } } } void handleFinish(const rpc::Finish::Reader& finish) { // Delay release of these things until return so that transitive destructors don't accidentally // modify the answer table and invalidate our pointer into it. kj::Array exportsToRelease; KJ_DEFER(releaseExports(exportsToRelease)); Answer answerToRelease; kj::Maybe> pipelineToRelease; KJ_IF_MAYBE(answer, answers.find(finish.getQuestionId())) { KJ_REQUIRE(answer->active, "'Finish' for invalid question ID.") { return; } if (finish.getReleaseResultCaps()) { exportsToRelease = kj::mv(answer->resultExports); } else { answer->resultExports = nullptr; } pipelineToRelease = kj::mv(answer->pipeline); // If the call isn't actually done yet, cancel it. Otherwise, we can go ahead and erase the // question from the table. KJ_IF_MAYBE(context, answer->callContext) { context->requestCancel(); } else { answerToRelease = answers.erase(finish.getQuestionId()); } } else { KJ_FAIL_REQUIRE("'Finish' for invalid question ID.") { return; } } } // --------------------------------------------------------------------------- // Level 1 void handleResolve(kj::Own&& message, const rpc::Resolve::Reader& resolve) { kj::Own replacement; kj::Maybe exception; // Extract the replacement capability. switch (resolve.which()) { case rpc::Resolve::CAP: KJ_IF_MAYBE(cap, receiveCap(resolve.getCap(), message->getAttachedFds())) { replacement = kj::mv(*cap); } else { KJ_FAIL_REQUIRE("'Resolve' contained 'CapDescriptor.none'.") { return; } } break; case rpc::Resolve::EXCEPTION: // We can't set `replacement` to a new broken cap here because this will confuse // PromiseClient::Resolve() into thinking that the remote promise resolved to a local // capability and therefore a Disembargo is needed. We must actually reject the promise. exception = toException(resolve.getException()); break; default: KJ_FAIL_REQUIRE("Unknown 'Resolve' type.") { return; } } // If the import is on the table, fulfill it. KJ_IF_MAYBE(import, imports.find(resolve.getPromiseId())) { KJ_IF_MAYBE(fulfiller, import->promiseFulfiller) { // OK, this is in fact an unfulfilled promise! KJ_IF_MAYBE(e, exception) { fulfiller->get()->reject(kj::mv(*e)); } else { fulfiller->get()->fulfill(kj::mv(replacement)); } } else if (import->importClient != nullptr) { // It appears this is a valid entry on the import table, but was not expected to be a // promise. KJ_FAIL_REQUIRE("Got 'Resolve' for a non-promise import.") { break; } } } } void handleRelease(const rpc::Release::Reader& release) { releaseExport(release.getId(), release.getReferenceCount()); } void releaseExport(ExportId id, uint refcount) { KJ_IF_MAYBE(exp, exports.find(id)) { KJ_REQUIRE(refcount <= exp->refcount, "Tried to drop export's refcount below zero.") { return; } exp->refcount -= refcount; if (exp->refcount == 0) { exportsByCap.erase(exp->clientHook); exports.erase(id, *exp); } } else { KJ_FAIL_REQUIRE("Tried to release invalid export ID.") { return; } } } void releaseExports(kj::ArrayPtr exports) { for (auto exportId: exports) { releaseExport(exportId, 1); } } void handleDisembargo(const rpc::Disembargo::Reader& disembargo) { auto context = disembargo.getContext(); switch (context.which()) { case rpc::Disembargo::Context::SENDER_LOOPBACK: { kj::Own target; KJ_IF_MAYBE(t, getMessageTarget(disembargo.getTarget())) { target = kj::mv(*t); } else { // Exception already reported. return; } for (;;) { KJ_IF_MAYBE(r, target->getResolved()) { target = r->addRef(); } else { break; } } KJ_REQUIRE(target->getBrand() == this, "'Disembargo' of type 'senderLoopback' sent to an object that does not point " "back to the sender.") { return; } EmbargoId embargoId = context.getSenderLoopback(); // We need to insert an evalLast() here to make sure that any pending calls towards this // cap have had time to find their way through the event loop. tasks.add(canceler.wrap(kj::evalLast(kj::mvCapture( target, [this,embargoId](kj::Own&& target) { if (!connection.is()) { return; } RpcClient& downcasted = kj::downcast(*target); auto message = connection.get()->newOutgoingMessage( messageSizeHint() + MESSAGE_TARGET_SIZE_HINT); auto builder = message->getBody().initAs().initDisembargo(); { auto redirect = downcasted.writeTarget(builder.initTarget()); // Disembargoes should only be sent to capabilities that were previously the subject of // a `Resolve` message. But `writeTarget` only ever returns non-null when called on // a PromiseClient. The code which sends `Resolve` and `Return` should have replaced // any promise with a direct node in order to solve the Tribble 4-way race condition. // See the documentation of Disembargo in rpc.capnp for more. KJ_REQUIRE(redirect == nullptr, "'Disembargo' of type 'senderLoopback' sent to an object that does not " "appear to have been the subject of a previous 'Resolve' message.") { return; } } builder.getContext().setReceiverLoopback(embargoId); message->send(); })))); break; } case rpc::Disembargo::Context::RECEIVER_LOOPBACK: { KJ_IF_MAYBE(embargo, embargoes.find(context.getReceiverLoopback())) { KJ_ASSERT_NONNULL(embargo->fulfiller)->fulfill(); embargoes.erase(context.getReceiverLoopback(), *embargo); } else { KJ_FAIL_REQUIRE("Invalid embargo ID in 'Disembargo.context.receiverLoopback'.") { return; } } break; } default: KJ_FAIL_REQUIRE("Unimplemented Disembargo type.", disembargo) { return; } } } // --------------------------------------------------------------------------- // Level 2 }; } // namespace class RpcSystemBase::Impl final: private BootstrapFactoryBase, private kj::TaskSet::ErrorHandler { public: Impl(VatNetworkBase& network, kj::Maybe bootstrapInterface, kj::Maybe::Client> gateway) : network(network), bootstrapInterface(kj::mv(bootstrapInterface)), bootstrapFactory(*this), gateway(kj::mv(gateway)), tasks(*this) { tasks.add(acceptLoop()); } Impl(VatNetworkBase& network, BootstrapFactoryBase& bootstrapFactory, kj::Maybe::Client> gateway) : network(network), bootstrapFactory(bootstrapFactory), gateway(kj::mv(gateway)), tasks(*this) { tasks.add(acceptLoop()); } Impl(VatNetworkBase& network, SturdyRefRestorerBase& restorer) : network(network), bootstrapFactory(*this), restorer(restorer), tasks(*this) { tasks.add(acceptLoop()); } ~Impl() noexcept(false) { unwindDetector.catchExceptionsIfUnwinding([&]() { // std::unordered_map doesn't like it when elements' destructors throw, so carefully // disassemble it. if (!connections.empty()) { kj::Vector> deleteMe(connections.size()); kj::Exception shutdownException = KJ_EXCEPTION(FAILED, "RpcSystem was destroyed."); for (auto& entry: connections) { entry.second->disconnect(kj::cp(shutdownException)); deleteMe.add(kj::mv(entry.second)); } } }); } Capability::Client bootstrap(AnyStruct::Reader vatId) { // For now we delegate to restore() since it's equivalent, but eventually we'll remove restore() // and implement bootstrap() directly. return restore(vatId, AnyPointer::Reader()); } Capability::Client restore(AnyStruct::Reader vatId, AnyPointer::Reader objectId) { KJ_IF_MAYBE(connection, network.baseConnect(vatId)) { auto& state = getConnectionState(kj::mv(*connection)); return Capability::Client(state.restore(objectId)); } else if (objectId.isNull()) { // Turns out `vatId` refers to ourselves, so we can also pass it as the client ID for // baseCreateFor(). return bootstrapFactory.baseCreateFor(vatId); } else KJ_IF_MAYBE(r, restorer) { return r->baseRestore(objectId); } else { return Capability::Client(newBrokenCap( "This vat only supports a bootstrap interface, not the old Cap'n-Proto-0.4-style " "named exports.")); } } void setFlowLimit(size_t words) { flowLimit = words; for (auto& conn: connections) { conn.second->setFlowLimit(words); } } private: VatNetworkBase& network; kj::Maybe bootstrapInterface; BootstrapFactoryBase& bootstrapFactory; kj::Maybe::Client> gateway; kj::Maybe restorer; size_t flowLimit = kj::maxValue; kj::TaskSet tasks; typedef std::unordered_map> ConnectionMap; ConnectionMap connections; kj::UnwindDetector unwindDetector; RpcConnectionState& getConnectionState(kj::Own&& connection) { auto iter = connections.find(connection); if (iter == connections.end()) { VatNetworkBase::Connection* connectionPtr = connection; auto onDisconnect = kj::newPromiseAndFulfiller(); tasks.add(onDisconnect.promise .then([this,connectionPtr](RpcConnectionState::DisconnectInfo info) { connections.erase(connectionPtr); tasks.add(kj::mv(info.shutdownPromise)); })); auto newState = kj::refcounted( bootstrapFactory, gateway, restorer, kj::mv(connection), kj::mv(onDisconnect.fulfiller), flowLimit); RpcConnectionState& result = *newState; connections.insert(std::make_pair(connectionPtr, kj::mv(newState))); return result; } else { return *iter->second; } } kj::Promise acceptLoop() { auto receive = network.baseAccept().then( [this](kj::Own&& connection) { getConnectionState(kj::mv(connection)); }); return receive.then([this]() { // No exceptions; continue loop. // // (We do this in a separate continuation to handle the case where exceptions are // disabled.) tasks.add(acceptLoop()); }); } Capability::Client baseCreateFor(AnyStruct::Reader clientId) override { // Implements BootstrapFactory::baseCreateFor() in terms of `bootstrapInterface` or `restorer`, // for use when we were given one of those instead of an actual `bootstrapFactory`. KJ_IF_MAYBE(cap, bootstrapInterface) { return *cap; } else KJ_IF_MAYBE(r, restorer) { return r->baseRestore(AnyPointer::Reader()); } else { return KJ_EXCEPTION(FAILED, "This vat does not expose any public/bootstrap interfaces."); } } void taskFailed(kj::Exception&& exception) override { KJ_LOG(ERROR, exception); } }; RpcSystemBase::RpcSystemBase(VatNetworkBase& network, kj::Maybe bootstrapInterface, kj::Maybe::Client> gateway) : impl(kj::heap(network, kj::mv(bootstrapInterface), kj::mv(gateway))) {} RpcSystemBase::RpcSystemBase(VatNetworkBase& network, BootstrapFactoryBase& bootstrapFactory, kj::Maybe::Client> gateway) : impl(kj::heap(network, bootstrapFactory, kj::mv(gateway))) {} RpcSystemBase::RpcSystemBase(VatNetworkBase& network, SturdyRefRestorerBase& restorer) : impl(kj::heap(network, restorer)) {} RpcSystemBase::RpcSystemBase(RpcSystemBase&& other) noexcept = default; RpcSystemBase::~RpcSystemBase() noexcept(false) {} Capability::Client RpcSystemBase::baseBootstrap(AnyStruct::Reader vatId) { return impl->bootstrap(vatId); } Capability::Client RpcSystemBase::baseRestore( AnyStruct::Reader hostId, AnyPointer::Reader objectId) { return impl->restore(hostId, objectId); } void RpcSystemBase::baseSetFlowLimit(size_t words) { return impl->setFlowLimit(words); } } // namespace _ (private) // ======================================================================================= namespace { class WindowFlowController final: public RpcFlowController, private kj::TaskSet::ErrorHandler { public: WindowFlowController(RpcFlowController::WindowGetter& windowGetter) : windowGetter(windowGetter), tasks(*this) { state.init(); } kj::Promise send(kj::Own message, kj::Promise ack) override { auto size = message->sizeInWords() * sizeof(capnp::word); maxMessageSize = kj::max(size, maxMessageSize); // We are REQUIRED to send the message NOW to maintain correct ordering. message->send(); inFlight += size; tasks.add(ack.then([this, size]() { inFlight -= size; KJ_SWITCH_ONEOF(state) { KJ_CASE_ONEOF(blockedSends, Running) { if (isReady()) { // Release all fulfillers. for (auto& fulfiller: blockedSends) { fulfiller->fulfill(); } blockedSends.clear(); } KJ_IF_MAYBE(f, emptyFulfiller) { if (inFlight == 0) { f->get()->fulfill(tasks.onEmpty()); } } } KJ_CASE_ONEOF(exception, kj::Exception) { // A previous call failed, but this one -- which was already in-flight at the time -- // ended up succeeding. That may indicate that the server side is not properly // handling streaming error propagation. Nothing much we can do about it here though. } } })); KJ_SWITCH_ONEOF(state) { KJ_CASE_ONEOF(blockedSends, Running) { if (isReady()) { return kj::READY_NOW; } else { auto paf = kj::newPromiseAndFulfiller(); blockedSends.add(kj::mv(paf.fulfiller)); return kj::mv(paf.promise); } } KJ_CASE_ONEOF(exception, kj::Exception) { return kj::cp(exception); } } KJ_UNREACHABLE; } kj::Promise waitAllAcked() override { KJ_IF_MAYBE(q, state.tryGet()) { if (!q->empty()) { auto paf = kj::newPromiseAndFulfiller>(); emptyFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } } return tasks.onEmpty(); } private: RpcFlowController::WindowGetter& windowGetter; size_t inFlight = 0; size_t maxMessageSize = 0; typedef kj::Vector>> Running; kj::OneOf state; kj::Maybe>>> emptyFulfiller; kj::TaskSet tasks; void taskFailed(kj::Exception&& exception) override { KJ_SWITCH_ONEOF(state) { KJ_CASE_ONEOF(blockedSends, Running) { // Fail out all pending sends. for (auto& fulfiller: blockedSends) { fulfiller->reject(kj::cp(exception)); } // Fail out all future sends. state = kj::mv(exception); } KJ_CASE_ONEOF(exception, kj::Exception) { // ignore redundant exception } } } bool isReady() { // We extend the window by maxMessageSize to avoid a pathological situation when a message // is larger than the window size. Otherwise, after sending that message, we would end up // not sending any others until the ack was received, wasting a round trip's worth of // bandwidth. return inFlight <= maxMessageSize // avoid getWindow() call if unnecessary || inFlight < windowGetter.getWindow() + maxMessageSize; } }; class FixedWindowFlowController final : public RpcFlowController, public RpcFlowController::WindowGetter { public: FixedWindowFlowController(size_t windowSize): windowSize(windowSize), inner(*this) {} kj::Promise send(kj::Own message, kj::Promise ack) override { return inner.send(kj::mv(message), kj::mv(ack)); } kj::Promise waitAllAcked() override { return inner.waitAllAcked(); } size_t getWindow() override { return windowSize; } private: size_t windowSize; WindowFlowController inner; }; } // namespace kj::Own RpcFlowController::newFixedWindowController(size_t windowSize) { return kj::heap(windowSize); } kj::Own RpcFlowController::newVariableWindowController(WindowGetter& getter) { return kj::heap(getter); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/rpc.capnp.c++0000644000175000017500000026001613650101756021027 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: rpc.capnp #include "rpc.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<232> b_91b79f1f808db032 = { { 0, 0, 0, 0, 5, 0, 6, 0, 50, 176, 141, 128, 31, 159, 183, 145, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 23, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 77, 101, 115, 115, 97, 103, 101, 0, 0, 0, 0, 0, 1, 0, 1, 0, 56, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 1, 0, 0, 3, 0, 1, 0, 132, 1, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 129, 1, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 1, 0, 0, 3, 0, 1, 0, 136, 1, 0, 0, 2, 0, 1, 0, 3, 0, 253, 255, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 1, 0, 0, 3, 0, 1, 0, 140, 1, 0, 0, 2, 0, 1, 0, 4, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 1, 0, 0, 3, 0, 1, 0, 144, 1, 0, 0, 2, 0, 1, 0, 5, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 1, 0, 0, 3, 0, 1, 0, 148, 1, 0, 0, 2, 0, 1, 0, 6, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 1, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 1, 0, 0, 3, 0, 1, 0, 152, 1, 0, 0, 2, 0, 1, 0, 7, 0, 249, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 1, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 1, 0, 0, 3, 0, 1, 0, 156, 1, 0, 0, 2, 0, 1, 0, 9, 0, 248, 255, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 1, 0, 0, 3, 0, 1, 0, 164, 1, 0, 0, 2, 0, 1, 0, 2, 0, 247, 255, 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161, 1, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 1, 0, 0, 3, 0, 1, 0, 172, 1, 0, 0, 2, 0, 1, 0, 10, 0, 246, 255, 0, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 169, 1, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 1, 0, 0, 3, 0, 1, 0, 180, 1, 0, 0, 2, 0, 1, 0, 11, 0, 245, 255, 0, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 1, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 1, 0, 0, 3, 0, 1, 0, 184, 1, 0, 0, 2, 0, 1, 0, 12, 0, 244, 255, 0, 0, 0, 0, 0, 0, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 1, 0, 0, 3, 0, 1, 0, 188, 1, 0, 0, 2, 0, 1, 0, 13, 0, 243, 255, 0, 0, 0, 0, 0, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 1, 0, 0, 3, 0, 1, 0, 192, 1, 0, 0, 2, 0, 1, 0, 8, 0, 242, 255, 0, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 1, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 188, 1, 0, 0, 3, 0, 1, 0, 200, 1, 0, 0, 2, 0, 1, 0, 117, 110, 105, 109, 112, 108, 101, 109, 101, 110, 116, 101, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 50, 176, 141, 128, 31, 159, 183, 145, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 98, 111, 114, 116, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 26, 105, 207, 58, 6, 183, 37, 214, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 108, 108, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 212, 76, 157, 120, 206, 83, 106, 131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 116, 117, 114, 110, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 58, 87, 179, 61, 141, 178, 25, 158, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 105, 110, 105, 115, 104, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 99, 14, 248, 194, 178, 46, 125, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 115, 111, 108, 118, 101, 0, 16, 0, 0, 0, 0, 0, 0, 0, 110, 8, 137, 250, 85, 150, 194, 187, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 108, 101, 97, 115, 101, 0, 16, 0, 0, 0, 0, 0, 0, 0, 151, 116, 208, 125, 13, 108, 26, 173, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 98, 115, 111, 108, 101, 116, 101, 83, 97, 118, 101, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 111, 111, 116, 115, 116, 114, 97, 112, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 196, 110, 23, 49, 128, 207, 76, 233, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 98, 115, 111, 108, 101, 116, 101, 68, 101, 108, 101, 116, 101, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 114, 111, 118, 105, 100, 101, 0, 16, 0, 0, 0, 0, 0, 0, 0, 90, 172, 193, 251, 107, 4, 106, 156, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 99, 99, 101, 112, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 22, 64, 85, 144, 98, 181, 201, 212, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 106, 111, 105, 110, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 175, 1, 224, 144, 4, 152, 225, 251, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 105, 115, 101, 109, 98, 97, 114, 103, 111, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 17, 55, 189, 15, 139, 54, 100, 249, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_91b79f1f808db032 = b_91b79f1f808db032.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_91b79f1f808db032[] = { &s_836a53ce789d4cd4, &s_91b79f1f808db032, &s_9c6a046bfbc1ac5a, &s_9e19b28d3db3573a, &s_ad1a6c0d7dd07497, &s_bbc29655fa89086e, &s_d37d2eb2c2f80e63, &s_d4c9b56290554016, &s_d625b7063acf691a, &s_e94ccf8031176ec4, &s_f964368b0fbd3711, &s_fbe1980490e001af, }; static const uint16_t m_91b79f1f808db032[] = {1, 11, 8, 2, 13, 4, 12, 9, 7, 10, 6, 5, 3, 0}; static const uint16_t i_91b79f1f808db032[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; const ::capnp::_::RawSchema s_91b79f1f808db032 = { 0x91b79f1f808db032, b_91b79f1f808db032.words, 232, d_91b79f1f808db032, m_91b79f1f808db032, 12, 14, i_91b79f1f808db032, nullptr, nullptr, { &s_91b79f1f808db032, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<51> b_e94ccf8031176ec4 = { { 0, 0, 0, 0, 5, 0, 6, 0, 196, 110, 23, 49, 128, 207, 76, 233, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 210, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 66, 111, 111, 116, 115, 116, 114, 97, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 3, 0, 1, 0, 64, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 101, 112, 114, 101, 99, 97, 116, 101, 100, 79, 98, 106, 101, 99, 116, 73, 100, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e94ccf8031176ec4 = b_e94ccf8031176ec4.words; #if !CAPNP_LITE static const uint16_t m_e94ccf8031176ec4[] = {1, 0}; static const uint16_t i_e94ccf8031176ec4[] = {0, 1}; const ::capnp::_::RawSchema s_e94ccf8031176ec4 = { 0xe94ccf8031176ec4, b_e94ccf8031176ec4.words, 51, nullptr, m_e94ccf8031176ec4, 0, 2, i_e94ccf8031176ec4, nullptr, nullptr, { &s_e94ccf8031176ec4, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<121> b_836a53ce789d4cd4 = { { 0, 0, 0, 0, 5, 0, 6, 0, 212, 76, 157, 120, 206, 83, 106, 131, 16, 0, 0, 0, 1, 0, 3, 0, 80, 162, 82, 37, 27, 152, 18, 179, 3, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 170, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 143, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 67, 97, 108, 108, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 28, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 0, 0, 0, 3, 0, 1, 0, 192, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 3, 0, 1, 0, 196, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 3, 0, 1, 0, 204, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 201, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 3, 0, 1, 0, 212, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 209, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 0, 0, 0, 3, 0, 1, 0, 216, 0, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 153, 95, 171, 26, 246, 176, 232, 218, 213, 0, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 128, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 194, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 0, 0, 0, 3, 0, 1, 0, 208, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 193, 251, 19, 88, 84, 20, 188, 149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 101, 114, 102, 97, 99, 101, 73, 100, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 101, 116, 104, 111, 100, 73, 100, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 115, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 59, 116, 150, 61, 34, 97, 14, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 101, 110, 100, 82, 101, 115, 117, 108, 116, 115, 84, 111, 0, 0, 0, 97, 108, 108, 111, 119, 84, 104, 105, 114, 100, 80, 97, 114, 116, 121, 84, 97, 105, 108, 67, 97, 108, 108, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_836a53ce789d4cd4 = b_836a53ce789d4cd4.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_836a53ce789d4cd4[] = { &s_95bc14545813fbc1, &s_9a0e61223d96743b, &s_dae8b0f61aab5f99, }; static const uint16_t m_836a53ce789d4cd4[] = {6, 2, 3, 4, 0, 5, 1}; static const uint16_t i_836a53ce789d4cd4[] = {0, 1, 2, 3, 4, 5, 6}; const ::capnp::_::RawSchema s_836a53ce789d4cd4 = { 0x836a53ce789d4cd4, b_836a53ce789d4cd4.words, 121, d_836a53ce789d4cd4, m_836a53ce789d4cd4, 3, 7, i_836a53ce789d4cd4, nullptr, nullptr, { &s_836a53ce789d4cd4, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<65> b_dae8b0f61aab5f99 = { { 0, 0, 0, 0, 5, 0, 6, 0, 153, 95, 171, 26, 246, 176, 232, 218, 21, 0, 0, 0, 1, 0, 3, 0, 212, 76, 157, 120, 206, 83, 106, 131, 3, 0, 7, 0, 1, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 67, 97, 108, 108, 46, 115, 101, 110, 100, 82, 101, 115, 117, 108, 116, 115, 84, 111, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 2, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 3, 0, 1, 0, 92, 0, 0, 0, 2, 0, 1, 0, 99, 97, 108, 108, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 111, 117, 114, 115, 101, 108, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 104, 105, 114, 100, 80, 97, 114, 116, 121, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_dae8b0f61aab5f99 = b_dae8b0f61aab5f99.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_dae8b0f61aab5f99[] = { &s_836a53ce789d4cd4, }; static const uint16_t m_dae8b0f61aab5f99[] = {0, 2, 1}; static const uint16_t i_dae8b0f61aab5f99[] = {0, 1, 2}; const ::capnp::_::RawSchema s_dae8b0f61aab5f99 = { 0xdae8b0f61aab5f99, b_dae8b0f61aab5f99.words, 65, d_dae8b0f61aab5f99, m_dae8b0f61aab5f99, 1, 3, i_dae8b0f61aab5f99, nullptr, nullptr, { &s_dae8b0f61aab5f99, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<148> b_9e19b28d3db3573a = { { 0, 0, 0, 0, 5, 0, 6, 0, 58, 87, 179, 61, 141, 178, 25, 158, 16, 0, 0, 0, 1, 0, 2, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 6, 0, 3, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 186, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 199, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 82, 101, 116, 117, 114, 110, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 32, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 209, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 0, 0, 0, 3, 0, 1, 0, 220, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 32, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 217, 0, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 220, 0, 0, 0, 3, 0, 1, 0, 232, 0, 0, 0, 2, 0, 1, 0, 2, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 224, 0, 0, 0, 3, 0, 1, 0, 236, 0, 0, 0, 2, 0, 1, 0, 3, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 233, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 0, 0, 0, 3, 0, 1, 0, 244, 0, 0, 0, 2, 0, 1, 0, 4, 0, 253, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 241, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 0, 0, 0, 3, 0, 1, 0, 252, 0, 0, 0, 2, 0, 1, 0, 5, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 0, 0, 0, 170, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252, 0, 0, 0, 3, 0, 1, 0, 8, 1, 0, 0, 2, 0, 1, 0, 6, 0, 251, 255, 2, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1, 0, 0, 3, 0, 1, 0, 20, 1, 0, 0, 2, 0, 1, 0, 7, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 1, 0, 0, 170, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 1, 0, 0, 3, 0, 1, 0, 32, 1, 0, 0, 2, 0, 1, 0, 97, 110, 115, 119, 101, 114, 73, 100, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 108, 101, 97, 115, 101, 80, 97, 114, 97, 109, 67, 97, 112, 115, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 115, 117, 108, 116, 115, 0, 16, 0, 0, 0, 0, 0, 0, 0, 59, 116, 150, 61, 34, 97, 14, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 120, 99, 101, 112, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 26, 105, 207, 58, 6, 183, 37, 214, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 110, 99, 101, 108, 101, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 115, 117, 108, 116, 115, 83, 101, 110, 116, 69, 108, 115, 101, 119, 104, 101, 114, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 107, 101, 70, 114, 111, 109, 79, 116, 104, 101, 114, 81, 117, 101, 115, 116, 105, 111, 110, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 99, 99, 101, 112, 116, 70, 114, 111, 109, 84, 104, 105, 114, 100, 80, 97, 114, 116, 121, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9e19b28d3db3573a = b_9e19b28d3db3573a.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9e19b28d3db3573a[] = { &s_9a0e61223d96743b, &s_d625b7063acf691a, }; static const uint16_t m_9e19b28d3db3573a[] = {7, 0, 4, 3, 1, 2, 5, 6}; static const uint16_t i_9e19b28d3db3573a[] = {2, 3, 4, 5, 6, 7, 0, 1}; const ::capnp::_::RawSchema s_9e19b28d3db3573a = { 0x9e19b28d3db3573a, b_9e19b28d3db3573a.words, 148, d_9e19b28d3db3573a, m_9e19b28d3db3573a, 2, 8, i_9e19b28d3db3573a, nullptr, nullptr, { &s_9e19b28d3db3573a, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<50> b_d37d2eb2c2f80e63 = { { 0, 0, 0, 0, 5, 0, 6, 0, 99, 14, 248, 194, 178, 46, 125, 211, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 186, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 70, 105, 110, 105, 115, 104, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 32, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 3, 0, 1, 0, 64, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 108, 101, 97, 115, 101, 82, 101, 115, 117, 108, 116, 67, 97, 112, 115, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d37d2eb2c2f80e63 = b_d37d2eb2c2f80e63.words; #if !CAPNP_LITE static const uint16_t m_d37d2eb2c2f80e63[] = {0, 1}; static const uint16_t i_d37d2eb2c2f80e63[] = {0, 1}; const ::capnp::_::RawSchema s_d37d2eb2c2f80e63 = { 0xd37d2eb2c2f80e63, b_d37d2eb2c2f80e63.words, 50, nullptr, m_d37d2eb2c2f80e63, 0, 2, i_d37d2eb2c2f80e63, nullptr, nullptr, { &s_d37d2eb2c2f80e63, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<64> b_bbc29655fa89086e = { { 0, 0, 0, 0, 5, 0, 6, 0, 110, 8, 137, 250, 85, 150, 194, 187, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 82, 101, 115, 111, 108, 118, 101, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 1, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 3, 0, 1, 0, 92, 0, 0, 0, 2, 0, 1, 0, 112, 114, 111, 109, 105, 115, 101, 73, 100, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 176, 184, 134, 11, 196, 221, 35, 133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 120, 99, 101, 112, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 26, 105, 207, 58, 6, 183, 37, 214, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_bbc29655fa89086e = b_bbc29655fa89086e.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_bbc29655fa89086e[] = { &s_8523ddc40b86b8b0, &s_d625b7063acf691a, }; static const uint16_t m_bbc29655fa89086e[] = {1, 2, 0}; static const uint16_t i_bbc29655fa89086e[] = {1, 2, 0}; const ::capnp::_::RawSchema s_bbc29655fa89086e = { 0xbbc29655fa89086e, b_bbc29655fa89086e.words, 64, d_bbc29655fa89086e, m_bbc29655fa89086e, 2, 3, i_bbc29655fa89086e, nullptr, nullptr, { &s_bbc29655fa89086e, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<48> b_ad1a6c0d7dd07497 = { { 0, 0, 0, 0, 5, 0, 6, 0, 151, 116, 208, 125, 13, 108, 26, 173, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 82, 101, 108, 101, 97, 115, 101, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 56, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 102, 101, 114, 101, 110, 99, 101, 67, 111, 117, 110, 116, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ad1a6c0d7dd07497 = b_ad1a6c0d7dd07497.words; #if !CAPNP_LITE static const uint16_t m_ad1a6c0d7dd07497[] = {0, 1}; static const uint16_t i_ad1a6c0d7dd07497[] = {0, 1}; const ::capnp::_::RawSchema s_ad1a6c0d7dd07497 = { 0xad1a6c0d7dd07497, b_ad1a6c0d7dd07497.words, 48, nullptr, m_ad1a6c0d7dd07497, 0, 2, i_ad1a6c0d7dd07497, nullptr, nullptr, { &s_ad1a6c0d7dd07497, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<41> b_f964368b0fbd3711 = { { 0, 0, 0, 0, 5, 0, 6, 0, 17, 55, 189, 15, 139, 54, 100, 249, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 218, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 68, 105, 115, 101, 109, 98, 97, 114, 103, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 77, 221, 91, 101, 223, 180, 98, 213, 45, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 193, 251, 19, 88, 84, 20, 188, 149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 111, 110, 116, 101, 120, 116, 0, } }; ::capnp::word const* const bp_f964368b0fbd3711 = b_f964368b0fbd3711.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_f964368b0fbd3711[] = { &s_95bc14545813fbc1, &s_d562b4df655bdd4d, }; static const uint16_t m_f964368b0fbd3711[] = {1, 0}; static const uint16_t i_f964368b0fbd3711[] = {0, 1}; const ::capnp::_::RawSchema s_f964368b0fbd3711 = { 0xf964368b0fbd3711, b_f964368b0fbd3711.words, 41, d_f964368b0fbd3711, m_f964368b0fbd3711, 2, 2, i_f964368b0fbd3711, nullptr, nullptr, { &s_f964368b0fbd3711, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<81> b_d562b4df655bdd4d = { { 0, 0, 0, 0, 5, 0, 6, 0, 77, 221, 91, 101, 223, 180, 98, 213, 27, 0, 0, 0, 1, 0, 1, 0, 17, 55, 189, 15, 139, 54, 100, 249, 1, 0, 7, 0, 1, 0, 4, 0, 2, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 68, 105, 115, 101, 109, 98, 97, 114, 103, 111, 46, 99, 111, 110, 116, 101, 120, 116, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 3, 0, 1, 0, 108, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 0, 0, 0, 3, 0, 1, 0, 120, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 3, 0, 1, 0, 124, 0, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 0, 0, 0, 3, 0, 1, 0, 128, 0, 0, 0, 2, 0, 1, 0, 115, 101, 110, 100, 101, 114, 76, 111, 111, 112, 98, 97, 99, 107, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 99, 101, 105, 118, 101, 114, 76, 111, 111, 112, 98, 97, 99, 107, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 99, 99, 101, 112, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 114, 111, 118, 105, 100, 101, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d562b4df655bdd4d = b_d562b4df655bdd4d.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d562b4df655bdd4d[] = { &s_f964368b0fbd3711, }; static const uint16_t m_d562b4df655bdd4d[] = {2, 3, 1, 0}; static const uint16_t i_d562b4df655bdd4d[] = {0, 1, 2, 3}; const ::capnp::_::RawSchema s_d562b4df655bdd4d = { 0xd562b4df655bdd4d, b_d562b4df655bdd4d.words, 81, d_d562b4df655bdd4d, m_d562b4df655bdd4d, 1, 4, i_d562b4df655bdd4d, nullptr, nullptr, { &s_d562b4df655bdd4d, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<64> b_9c6a046bfbc1ac5a = { { 0, 0, 0, 0, 5, 0, 6, 0, 90, 172, 193, 251, 107, 4, 106, 156, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 80, 114, 111, 118, 105, 100, 101, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 3, 0, 1, 0, 92, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 193, 251, 19, 88, 84, 20, 188, 149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 99, 105, 112, 105, 101, 110, 116, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9c6a046bfbc1ac5a = b_9c6a046bfbc1ac5a.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9c6a046bfbc1ac5a[] = { &s_95bc14545813fbc1, }; static const uint16_t m_9c6a046bfbc1ac5a[] = {0, 2, 1}; static const uint16_t i_9c6a046bfbc1ac5a[] = {0, 1, 2}; const ::capnp::_::RawSchema s_9c6a046bfbc1ac5a = { 0x9c6a046bfbc1ac5a, b_9c6a046bfbc1ac5a.words, 64, d_9c6a046bfbc1ac5a, m_9c6a046bfbc1ac5a, 1, 3, i_9c6a046bfbc1ac5a, nullptr, nullptr, { &s_9c6a046bfbc1ac5a, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<64> b_d4c9b56290554016 = { { 0, 0, 0, 0, 5, 0, 6, 0, 22, 64, 85, 144, 98, 181, 201, 212, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 186, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 65, 99, 99, 101, 112, 116, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 32, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 3, 0, 1, 0, 92, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 114, 111, 118, 105, 115, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 109, 98, 97, 114, 103, 111, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d4c9b56290554016 = b_d4c9b56290554016.words; #if !CAPNP_LITE static const uint16_t m_d4c9b56290554016[] = {2, 1, 0}; static const uint16_t i_d4c9b56290554016[] = {0, 1, 2}; const ::capnp::_::RawSchema s_d4c9b56290554016 = { 0xd4c9b56290554016, b_d4c9b56290554016.words, 64, nullptr, m_d4c9b56290554016, 0, 3, i_d4c9b56290554016, nullptr, nullptr, { &s_d4c9b56290554016, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<63> b_fbe1980490e001af = { { 0, 0, 0, 0, 5, 0, 6, 0, 175, 1, 224, 144, 4, 152, 225, 251, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 170, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 74, 111, 105, 110, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 193, 251, 19, 88, 84, 20, 188, 149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 107, 101, 121, 80, 97, 114, 116, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_fbe1980490e001af = b_fbe1980490e001af.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_fbe1980490e001af[] = { &s_95bc14545813fbc1, }; static const uint16_t m_fbe1980490e001af[] = {2, 0, 1}; static const uint16_t i_fbe1980490e001af[] = {0, 1, 2}; const ::capnp::_::RawSchema s_fbe1980490e001af = { 0xfbe1980490e001af, b_fbe1980490e001af.words, 63, d_fbe1980490e001af, m_fbe1980490e001af, 1, 3, i_fbe1980490e001af, nullptr, nullptr, { &s_fbe1980490e001af, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<50> b_95bc14545813fbc1 = { { 0, 0, 0, 0, 5, 0, 6, 0, 193, 251, 19, 88, 84, 20, 188, 149, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 77, 101, 115, 115, 97, 103, 101, 84, 97, 114, 103, 101, 116, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 3, 0, 1, 0, 60, 0, 0, 0, 2, 0, 1, 0, 105, 109, 112, 111, 114, 116, 101, 100, 67, 97, 112, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 114, 111, 109, 105, 115, 101, 100, 65, 110, 115, 119, 101, 114, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 160, 28, 111, 205, 214, 177, 0, 216, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_95bc14545813fbc1 = b_95bc14545813fbc1.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_95bc14545813fbc1[] = { &s_d800b1d6cd6f1ca0, }; static const uint16_t m_95bc14545813fbc1[] = {0, 1}; static const uint16_t i_95bc14545813fbc1[] = {0, 1}; const ::capnp::_::RawSchema s_95bc14545813fbc1 = { 0x95bc14545813fbc1, b_95bc14545813fbc1.words, 50, d_95bc14545813fbc1, m_95bc14545813fbc1, 1, 2, i_95bc14545813fbc1, nullptr, nullptr, { &s_95bc14545813fbc1, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<52> b_9a0e61223d96743b = { { 0, 0, 0, 0, 5, 0, 6, 0, 59, 116, 150, 61, 34, 97, 14, 154, 16, 0, 0, 0, 1, 0, 0, 0, 80, 162, 82, 37, 27, 152, 18, 179, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 80, 97, 121, 108, 111, 97, 100, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 72, 0, 0, 0, 2, 0, 1, 0, 99, 111, 110, 116, 101, 110, 116, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 84, 97, 98, 108, 101, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 176, 184, 134, 11, 196, 221, 35, 133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9a0e61223d96743b = b_9a0e61223d96743b.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9a0e61223d96743b[] = { &s_8523ddc40b86b8b0, }; static const uint16_t m_9a0e61223d96743b[] = {1, 0}; static const uint16_t i_9a0e61223d96743b[] = {0, 1}; const ::capnp::_::RawSchema s_9a0e61223d96743b = { 0x9a0e61223d96743b, b_9a0e61223d96743b.words, 52, d_9a0e61223d96743b, m_9a0e61223d96743b, 1, 2, i_9a0e61223d96743b, nullptr, nullptr, { &s_9a0e61223d96743b, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<130> b_8523ddc40b86b8b0 = { { 0, 0, 0, 0, 5, 0, 6, 0, 176, 184, 134, 11, 196, 221, 35, 133, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 143, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 67, 97, 112, 68, 101, 115, 99, 114, 105, 112, 116, 111, 114, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 28, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 0, 3, 0, 1, 0, 188, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 3, 0, 1, 0, 196, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 3, 0, 1, 0, 204, 0, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 201, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 3, 0, 1, 0, 212, 0, 0, 0, 2, 0, 1, 0, 4, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 209, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 0, 0, 0, 3, 0, 1, 0, 220, 0, 0, 0, 2, 0, 1, 0, 5, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 217, 0, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 220, 0, 0, 0, 3, 0, 1, 0, 232, 0, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 228, 0, 0, 0, 3, 0, 1, 0, 240, 0, 0, 0, 2, 0, 1, 0, 110, 111, 110, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 101, 110, 100, 101, 114, 72, 111, 115, 116, 101, 100, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 101, 110, 100, 101, 114, 80, 114, 111, 109, 105, 115, 101, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 99, 101, 105, 118, 101, 114, 72, 111, 115, 116, 101, 100, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 99, 101, 105, 118, 101, 114, 65, 110, 115, 119, 101, 114, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 160, 28, 111, 205, 214, 177, 0, 216, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 104, 105, 114, 100, 80, 97, 114, 116, 121, 72, 111, 115, 116, 101, 100, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 125, 2, 240, 225, 253, 7, 112, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 116, 116, 97, 99, 104, 101, 100, 70, 100, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_8523ddc40b86b8b0 = b_8523ddc40b86b8b0.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_8523ddc40b86b8b0[] = { &s_d37007fde1f0027d, &s_d800b1d6cd6f1ca0, }; static const uint16_t m_8523ddc40b86b8b0[] = {6, 0, 4, 3, 1, 2, 5}; static const uint16_t i_8523ddc40b86b8b0[] = {0, 1, 2, 3, 4, 5, 6}; const ::capnp::_::RawSchema s_8523ddc40b86b8b0 = { 0x8523ddc40b86b8b0, b_8523ddc40b86b8b0.words, 130, d_8523ddc40b86b8b0, m_8523ddc40b86b8b0, 2, 7, i_8523ddc40b86b8b0, nullptr, nullptr, { &s_8523ddc40b86b8b0, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<57> b_d800b1d6cd6f1ca0 = { { 0, 0, 0, 0, 5, 0, 6, 0, 160, 28, 111, 205, 214, 177, 0, 216, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 33, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 80, 114, 111, 109, 105, 115, 101, 100, 65, 110, 115, 119, 101, 114, 0, 0, 4, 0, 0, 0, 1, 0, 1, 0, 129, 144, 86, 21, 68, 148, 22, 243, 1, 0, 0, 0, 26, 0, 0, 0, 79, 112, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 113, 117, 101, 115, 116, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 114, 97, 110, 115, 102, 111, 114, 109, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 129, 144, 86, 21, 68, 148, 22, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d800b1d6cd6f1ca0 = b_d800b1d6cd6f1ca0.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d800b1d6cd6f1ca0[] = { &s_f316944415569081, }; static const uint16_t m_d800b1d6cd6f1ca0[] = {0, 1}; static const uint16_t i_d800b1d6cd6f1ca0[] = {0, 1}; const ::capnp::_::RawSchema s_d800b1d6cd6f1ca0 = { 0xd800b1d6cd6f1ca0, b_d800b1d6cd6f1ca0.words, 57, d_d800b1d6cd6f1ca0, m_d800b1d6cd6f1ca0, 1, 2, i_d800b1d6cd6f1ca0, nullptr, nullptr, { &s_d800b1d6cd6f1ca0, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<50> b_f316944415569081 = { { 0, 0, 0, 0, 5, 0, 6, 0, 129, 144, 86, 21, 68, 148, 22, 243, 31, 0, 0, 0, 1, 0, 1, 0, 160, 28, 111, 205, 214, 177, 0, 216, 0, 0, 7, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 18, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 80, 114, 111, 109, 105, 115, 101, 100, 65, 110, 115, 119, 101, 114, 46, 79, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 56, 0, 0, 0, 2, 0, 1, 0, 110, 111, 111, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 101, 116, 80, 111, 105, 110, 116, 101, 114, 70, 105, 101, 108, 100, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f316944415569081 = b_f316944415569081.words; #if !CAPNP_LITE static const uint16_t m_f316944415569081[] = {1, 0}; static const uint16_t i_f316944415569081[] = {0, 1}; const ::capnp::_::RawSchema s_f316944415569081 = { 0xf316944415569081, b_f316944415569081.words, 50, nullptr, m_f316944415569081, 0, 2, i_f316944415569081, nullptr, nullptr, { &s_f316944415569081, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<49> b_d37007fde1f0027d = { { 0, 0, 0, 0, 5, 0, 6, 0, 125, 2, 240, 225, 253, 7, 112, 211, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 66, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 84, 104, 105, 114, 100, 80, 97, 114, 116, 121, 67, 97, 112, 68, 101, 115, 99, 114, 105, 112, 116, 111, 114, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 105, 110, 101, 73, 100, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d37007fde1f0027d = b_d37007fde1f0027d.words; #if !CAPNP_LITE static const uint16_t m_d37007fde1f0027d[] = {0, 1}; static const uint16_t i_d37007fde1f0027d[] = {0, 1}; const ::capnp::_::RawSchema s_d37007fde1f0027d = { 0xd37007fde1f0027d, b_d37007fde1f0027d.words, 49, nullptr, m_d37007fde1f0027d, 0, 2, i_d37007fde1f0027d, nullptr, nullptr, { &s_d37007fde1f0027d, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<85> b_d625b7063acf691a = { { 0, 0, 0, 0, 5, 0, 6, 0, 26, 105, 207, 58, 6, 183, 37, 214, 16, 0, 0, 0, 1, 0, 1, 0, 80, 162, 82, 37, 27, 152, 18, 179, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 210, 0, 0, 0, 33, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 69, 120, 99, 101, 112, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 1, 0, 88, 189, 76, 63, 226, 150, 140, 178, 1, 0, 0, 0, 42, 0, 0, 0, 84, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 3, 0, 1, 0, 104, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 186, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 3, 0, 1, 0, 116, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 0, 0, 0, 3, 0, 1, 0, 128, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 125, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 3, 0, 1, 0, 132, 0, 0, 0, 2, 0, 1, 0, 114, 101, 97, 115, 111, 110, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 98, 115, 111, 108, 101, 116, 101, 73, 115, 67, 97, 108, 108, 101, 114, 115, 70, 97, 117, 108, 116, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 98, 115, 111, 108, 101, 116, 101, 68, 117, 114, 97, 98, 105, 108, 105, 116, 121, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 112, 101, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 88, 189, 76, 63, 226, 150, 140, 178, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d625b7063acf691a = b_d625b7063acf691a.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d625b7063acf691a[] = { &s_b28c96e23f4cbd58, }; static const uint16_t m_d625b7063acf691a[] = {2, 1, 0, 3}; static const uint16_t i_d625b7063acf691a[] = {0, 1, 2, 3}; const ::capnp::_::RawSchema s_d625b7063acf691a = { 0xd625b7063acf691a, b_d625b7063acf691a.words, 85, d_d625b7063acf691a, m_d625b7063acf691a, 1, 4, i_d625b7063acf691a, nullptr, nullptr, { &s_d625b7063acf691a, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<37> b_b28c96e23f4cbd58 = { { 0, 0, 0, 0, 5, 0, 6, 0, 88, 189, 76, 63, 226, 150, 140, 178, 26, 0, 0, 0, 2, 0, 0, 0, 26, 105, 207, 58, 6, 183, 37, 214, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 46, 99, 97, 112, 110, 112, 58, 69, 120, 99, 101, 112, 116, 105, 111, 110, 46, 84, 121, 112, 101, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 16, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 97, 105, 108, 101, 100, 0, 0, 111, 118, 101, 114, 108, 111, 97, 100, 101, 100, 0, 0, 0, 0, 0, 0, 100, 105, 115, 99, 111, 110, 110, 101, 99, 116, 101, 100, 0, 0, 0, 0, 117, 110, 105, 109, 112, 108, 101, 109, 101, 110, 116, 101, 100, 0, 0, 0, } }; ::capnp::word const* const bp_b28c96e23f4cbd58 = b_b28c96e23f4cbd58.words; #if !CAPNP_LITE static const uint16_t m_b28c96e23f4cbd58[] = {2, 0, 1, 3}; const ::capnp::_::RawSchema s_b28c96e23f4cbd58 = { 0xb28c96e23f4cbd58, b_b28c96e23f4cbd58.words, 37, nullptr, m_b28c96e23f4cbd58, 0, 4, nullptr, nullptr, nullptr, { &s_b28c96e23f4cbd58, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE CAPNP_DEFINE_ENUM(Type_b28c96e23f4cbd58, b28c96e23f4cbd58); } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { namespace rpc { // Message constexpr uint16_t Message::_capnpPrivate::dataWordSize; constexpr uint16_t Message::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Message::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Message::_capnpPrivate::schema; #endif // !CAPNP_LITE // Bootstrap constexpr uint16_t Bootstrap::_capnpPrivate::dataWordSize; constexpr uint16_t Bootstrap::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Bootstrap::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Bootstrap::_capnpPrivate::schema; #endif // !CAPNP_LITE // Call constexpr uint16_t Call::_capnpPrivate::dataWordSize; constexpr uint16_t Call::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Call::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Call::_capnpPrivate::schema; #endif // !CAPNP_LITE // Call::SendResultsTo constexpr uint16_t Call::SendResultsTo::_capnpPrivate::dataWordSize; constexpr uint16_t Call::SendResultsTo::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Call::SendResultsTo::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Call::SendResultsTo::_capnpPrivate::schema; #endif // !CAPNP_LITE // Return constexpr uint16_t Return::_capnpPrivate::dataWordSize; constexpr uint16_t Return::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Return::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Return::_capnpPrivate::schema; #endif // !CAPNP_LITE // Finish constexpr uint16_t Finish::_capnpPrivate::dataWordSize; constexpr uint16_t Finish::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Finish::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Finish::_capnpPrivate::schema; #endif // !CAPNP_LITE // Resolve constexpr uint16_t Resolve::_capnpPrivate::dataWordSize; constexpr uint16_t Resolve::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Resolve::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Resolve::_capnpPrivate::schema; #endif // !CAPNP_LITE // Release constexpr uint16_t Release::_capnpPrivate::dataWordSize; constexpr uint16_t Release::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Release::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Release::_capnpPrivate::schema; #endif // !CAPNP_LITE // Disembargo constexpr uint16_t Disembargo::_capnpPrivate::dataWordSize; constexpr uint16_t Disembargo::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Disembargo::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Disembargo::_capnpPrivate::schema; #endif // !CAPNP_LITE // Disembargo::Context constexpr uint16_t Disembargo::Context::_capnpPrivate::dataWordSize; constexpr uint16_t Disembargo::Context::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Disembargo::Context::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Disembargo::Context::_capnpPrivate::schema; #endif // !CAPNP_LITE // Provide constexpr uint16_t Provide::_capnpPrivate::dataWordSize; constexpr uint16_t Provide::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Provide::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Provide::_capnpPrivate::schema; #endif // !CAPNP_LITE // Accept constexpr uint16_t Accept::_capnpPrivate::dataWordSize; constexpr uint16_t Accept::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Accept::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Accept::_capnpPrivate::schema; #endif // !CAPNP_LITE // Join constexpr uint16_t Join::_capnpPrivate::dataWordSize; constexpr uint16_t Join::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Join::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Join::_capnpPrivate::schema; #endif // !CAPNP_LITE // MessageTarget constexpr uint16_t MessageTarget::_capnpPrivate::dataWordSize; constexpr uint16_t MessageTarget::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind MessageTarget::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* MessageTarget::_capnpPrivate::schema; #endif // !CAPNP_LITE // Payload constexpr uint16_t Payload::_capnpPrivate::dataWordSize; constexpr uint16_t Payload::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Payload::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Payload::_capnpPrivate::schema; #endif // !CAPNP_LITE // CapDescriptor constexpr uint16_t CapDescriptor::_capnpPrivate::dataWordSize; constexpr uint16_t CapDescriptor::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind CapDescriptor::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* CapDescriptor::_capnpPrivate::schema; #endif // !CAPNP_LITE // PromisedAnswer constexpr uint16_t PromisedAnswer::_capnpPrivate::dataWordSize; constexpr uint16_t PromisedAnswer::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind PromisedAnswer::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* PromisedAnswer::_capnpPrivate::schema; #endif // !CAPNP_LITE // PromisedAnswer::Op constexpr uint16_t PromisedAnswer::Op::_capnpPrivate::dataWordSize; constexpr uint16_t PromisedAnswer::Op::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind PromisedAnswer::Op::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* PromisedAnswer::Op::_capnpPrivate::schema; #endif // !CAPNP_LITE // ThirdPartyCapDescriptor constexpr uint16_t ThirdPartyCapDescriptor::_capnpPrivate::dataWordSize; constexpr uint16_t ThirdPartyCapDescriptor::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind ThirdPartyCapDescriptor::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* ThirdPartyCapDescriptor::_capnpPrivate::schema; #endif // !CAPNP_LITE // Exception constexpr uint16_t Exception::_capnpPrivate::dataWordSize; constexpr uint16_t Exception::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Exception::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Exception::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/rpc-twoparty.c++0000644000175000017500000003732013650101756021616 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "rpc-twoparty.h" #include "serialize-async.h" #include #include // Includes just for need SOL_SOCKET and SO_SNDBUF #if _WIN32 #define WIN32_LEAN_AND_MEAN // ::eyeroll:: #include #include #include #else #include #endif namespace capnp { TwoPartyVatNetwork::TwoPartyVatNetwork(kj::AsyncIoStream& stream, rpc::twoparty::Side side, ReaderOptions receiveOptions) : stream(&stream), maxFdsPerMessage(0), side(side), peerVatId(4), receiveOptions(receiveOptions), previousWrite(kj::READY_NOW) { peerVatId.initRoot().setSide( side == rpc::twoparty::Side::CLIENT ? rpc::twoparty::Side::SERVER : rpc::twoparty::Side::CLIENT); auto paf = kj::newPromiseAndFulfiller(); disconnectPromise = paf.promise.fork(); disconnectFulfiller.fulfiller = kj::mv(paf.fulfiller); } TwoPartyVatNetwork::TwoPartyVatNetwork(kj::AsyncCapabilityStream& stream, uint maxFdsPerMessage, rpc::twoparty::Side side, ReaderOptions receiveOptions) : TwoPartyVatNetwork(stream, side, receiveOptions) { this->stream = &stream; this->maxFdsPerMessage = maxFdsPerMessage; } void TwoPartyVatNetwork::FulfillerDisposer::disposeImpl(void* pointer) const { if (--refcount == 0) { fulfiller->fulfill(); } } kj::Own TwoPartyVatNetwork::asConnection() { ++disconnectFulfiller.refcount; return kj::Own(this, disconnectFulfiller); } kj::Maybe> TwoPartyVatNetwork::connect( rpc::twoparty::VatId::Reader ref) { if (ref.getSide() == side) { return nullptr; } else { return asConnection(); } } kj::Promise> TwoPartyVatNetwork::accept() { if (side == rpc::twoparty::Side::SERVER && !accepted) { accepted = true; return asConnection(); } else { // Create a promise that will never be fulfilled. auto paf = kj::newPromiseAndFulfiller>(); acceptFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } } class TwoPartyVatNetwork::OutgoingMessageImpl final : public OutgoingRpcMessage, public kj::Refcounted { public: OutgoingMessageImpl(TwoPartyVatNetwork& network, uint firstSegmentWordSize) : network(network), message(firstSegmentWordSize == 0 ? SUGGESTED_FIRST_SEGMENT_WORDS : firstSegmentWordSize) {} AnyPointer::Builder getBody() override { return message.getRoot(); } void setFds(kj::Array fds) override { if (network.stream.is()) { this->fds = kj::mv(fds); } } void send() override { size_t size = 0; for (auto& segment: message.getSegmentsForOutput()) { size += segment.size(); } KJ_REQUIRE(size < network.receiveOptions.traversalLimitInWords, size, "Trying to send Cap'n Proto message larger than our single-message size limit. The " "other side probably won't accept it (assuming its traversalLimitInWords matches " "ours) and would abort the connection, so I won't send it.") { return; } network.previousWrite = KJ_ASSERT_NONNULL(network.previousWrite, "already shut down") .then([&]() { // Note that if the write fails, all further writes will be skipped due to the exception. // We never actually handle this exception because we assume the read end will fail as well // and it's cleaner to handle the failure there. KJ_SWITCH_ONEOF(network.stream) { KJ_CASE_ONEOF(ioStream, kj::AsyncIoStream*) { return writeMessage(*ioStream, message); } KJ_CASE_ONEOF(capStream, kj::AsyncCapabilityStream*) { return writeMessage(*capStream, fds, message); } } KJ_UNREACHABLE; }).attach(kj::addRef(*this)) // Note that it's important that the eagerlyEvaluate() come *after* the attach() because // otherwise the message (and any capabilities in it) will not be released until a new // message is written! (Kenton once spent all afternoon tracking this down...) .eagerlyEvaluate(nullptr); } size_t sizeInWords() override { return message.sizeInWords(); } private: TwoPartyVatNetwork& network; MallocMessageBuilder message; kj::Array fds; }; class TwoPartyVatNetwork::IncomingMessageImpl final: public IncomingRpcMessage { public: IncomingMessageImpl(kj::Own message): message(kj::mv(message)) {} IncomingMessageImpl(MessageReaderAndFds init, kj::Array fdSpace) : message(kj::mv(init.reader)), fdSpace(kj::mv(fdSpace)), fds(init.fds) { KJ_DASSERT(this->fds.begin() == this->fdSpace.begin()); } AnyPointer::Reader getBody() override { return message->getRoot(); } kj::ArrayPtr getAttachedFds() override { return fds; } size_t sizeInWords() override { return message->sizeInWords(); } private: kj::Own message; kj::Array fdSpace; kj::ArrayPtr fds; }; kj::Own TwoPartyVatNetwork::newStream() { return RpcFlowController::newVariableWindowController(*this); } size_t TwoPartyVatNetwork::getWindow() { // The socket's send buffer size -- as returned by getsockopt(SO_SNDBUF) -- tells us how much // data the kernel itself is willing to buffer. The kernel will increase the send buffer size if // needed to fill the connection's congestion window. So we can cheat and use it as our stream // window, too, to make sure we saturate said congestion window. // // TODO(perf): Unfortunately, this hack breaks down in the presence of proxying. What we really // want is the window all the way to the endpoint, which could cross multiple connections. The // first-hop window could be either too big or too small: it's too big if the first hop has // much higher bandwidth than the full path (causing buffering at the bottleneck), and it's // too small if the first hop has much lower latency than the full path (causing not enough // data to be sent to saturate the connection). To handle this, we could either: // 1. Have proxies be aware of streaming, by flagging streaming calls in the RPC protocol. The // proxies would then handle backpressure at each hop. This seems simple to implement but // requires base RPC protocol changes and might require thinking carefully about e-ordering // implications. Also, it only fixes underutilization; it does not fix buffer bloat. // 2. Do our own BBR-like computation, where the client measures the end-to-end latency and // bandwidth based on the observed sends and returns, and then compute the window based on // that. This seems complicated, but avoids the need for any changes to the RPC protocol. // In theory it solves both underutilization and buffer bloat. Note that this approach would // require the RPC system to use a clock, which feels dirty and adds non-determinism. if (solSndbufUnimplemented) { return RpcFlowController::DEFAULT_WINDOW_SIZE; } else { // TODO(perf): It might be nice to have a tryGetsockopt() that doesn't require catching // exceptions? int bufSize = 0; KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { uint len = sizeof(int); KJ_SWITCH_ONEOF(stream) { KJ_CASE_ONEOF(s, kj::AsyncIoStream*) { s->getsockopt(SOL_SOCKET, SO_SNDBUF, &bufSize, &len); } KJ_CASE_ONEOF(s, kj::AsyncCapabilityStream*) { s->getsockopt(SOL_SOCKET, SO_SNDBUF, &bufSize, &len); } } KJ_ASSERT(len == sizeof(bufSize)) { break; } })) { if (exception->getType() != kj::Exception::Type::UNIMPLEMENTED) { // TODO(someday): Figure out why getting SO_SNDBUF sometimes throws EINVAL. I suspect it // happens when the remote side has closed their read end, meaning we no longer have // a send buffer, but I don't know what is the best way to verify that that was actually // the reason. I'd prefer not to ignore EINVAL errors in general. // kj::throwRecoverableException(kj::mv(*exception)); } solSndbufUnimplemented = true; bufSize = RpcFlowController::DEFAULT_WINDOW_SIZE; } return bufSize; } } rpc::twoparty::VatId::Reader TwoPartyVatNetwork::getPeerVatId() { return peerVatId.getRoot(); } kj::Own TwoPartyVatNetwork::newOutgoingMessage(uint firstSegmentWordSize) { return kj::refcounted(*this, firstSegmentWordSize); } kj::Promise>> TwoPartyVatNetwork::receiveIncomingMessage() { return kj::evalLater([this]() { KJ_SWITCH_ONEOF(stream) { KJ_CASE_ONEOF(ioStream, kj::AsyncIoStream*) { return tryReadMessage(*ioStream, receiveOptions) .then([](kj::Maybe>&& message) -> kj::Maybe> { KJ_IF_MAYBE(m, message) { return kj::Own(kj::heap(kj::mv(*m))); } else { return nullptr; } }); } KJ_CASE_ONEOF(capStream, kj::AsyncCapabilityStream*) { auto fdSpace = kj::heapArray(maxFdsPerMessage); auto promise = tryReadMessage(*capStream, fdSpace, receiveOptions); return promise.then([fdSpace = kj::mv(fdSpace)] (kj::Maybe&& messageAndFds) mutable -> kj::Maybe> { KJ_IF_MAYBE(m, messageAndFds) { if (m->fds.size() > 0) { return kj::Own( kj::heap(kj::mv(*m), kj::mv(fdSpace))); } else { return kj::Own(kj::heap(kj::mv(m->reader))); } } else { return nullptr; } }); } } KJ_UNREACHABLE; }); } kj::Promise TwoPartyVatNetwork::shutdown() { kj::Promise result = KJ_ASSERT_NONNULL(previousWrite, "already shut down").then([this]() { KJ_SWITCH_ONEOF(stream) { KJ_CASE_ONEOF(ioStream, kj::AsyncIoStream*) { ioStream->shutdownWrite(); } KJ_CASE_ONEOF(capStream, kj::AsyncCapabilityStream*) { capStream->shutdownWrite(); } } }); previousWrite = nullptr; return kj::mv(result); } // ======================================================================================= TwoPartyServer::TwoPartyServer(Capability::Client bootstrapInterface) : bootstrapInterface(kj::mv(bootstrapInterface)), tasks(*this) {} struct TwoPartyServer::AcceptedConnection { kj::Own connection; TwoPartyVatNetwork network; RpcSystem rpcSystem; explicit AcceptedConnection(Capability::Client bootstrapInterface, kj::Own&& connectionParam) : connection(kj::mv(connectionParam)), network(*connection, rpc::twoparty::Side::SERVER), rpcSystem(makeRpcServer(network, kj::mv(bootstrapInterface))) {} explicit AcceptedConnection(Capability::Client bootstrapInterface, kj::Own&& connectionParam, uint maxFdsPerMessage) : connection(kj::mv(connectionParam)), network(kj::downcast(*connection), maxFdsPerMessage, rpc::twoparty::Side::SERVER), rpcSystem(makeRpcServer(network, kj::mv(bootstrapInterface))) {} }; void TwoPartyServer::accept(kj::Own&& connection) { auto connectionState = kj::heap(bootstrapInterface, kj::mv(connection)); // Run the connection until disconnect. auto promise = connectionState->network.onDisconnect(); tasks.add(promise.attach(kj::mv(connectionState))); } void TwoPartyServer::accept( kj::Own&& connection, uint maxFdsPerMessage) { auto connectionState = kj::heap( bootstrapInterface, kj::mv(connection), maxFdsPerMessage); // Run the connection until disconnect. auto promise = connectionState->network.onDisconnect(); tasks.add(promise.attach(kj::mv(connectionState))); } kj::Promise TwoPartyServer::listen(kj::ConnectionReceiver& listener) { return listener.accept() .then([this,&listener](kj::Own&& connection) mutable { accept(kj::mv(connection)); return listen(listener); }); } kj::Promise TwoPartyServer::listenCapStreamReceiver( kj::ConnectionReceiver& listener, uint maxFdsPerMessage) { return listener.accept() .then([this,&listener,maxFdsPerMessage](kj::Own&& connection) mutable { accept(connection.downcast(), maxFdsPerMessage); return listenCapStreamReceiver(listener, maxFdsPerMessage); }); } void TwoPartyServer::taskFailed(kj::Exception&& exception) { KJ_LOG(ERROR, exception); } TwoPartyClient::TwoPartyClient(kj::AsyncIoStream& connection) : network(connection, rpc::twoparty::Side::CLIENT), rpcSystem(makeRpcClient(network)) {} TwoPartyClient::TwoPartyClient(kj::AsyncCapabilityStream& connection, uint maxFdsPerMessage) : network(connection, maxFdsPerMessage, rpc::twoparty::Side::CLIENT), rpcSystem(makeRpcClient(network)) {} TwoPartyClient::TwoPartyClient(kj::AsyncIoStream& connection, Capability::Client bootstrapInterface, rpc::twoparty::Side side) : network(connection, side), rpcSystem(network, bootstrapInterface) {} TwoPartyClient::TwoPartyClient(kj::AsyncCapabilityStream& connection, uint maxFdsPerMessage, Capability::Client bootstrapInterface, rpc::twoparty::Side side) : network(connection, maxFdsPerMessage, side), rpcSystem(network, bootstrapInterface) {} Capability::Client TwoPartyClient::bootstrap() { capnp::word scratch[4]; memset(&scratch, 0, sizeof(scratch)); capnp::MallocMessageBuilder message(scratch); auto vatId = message.getRoot(); vatId.setSide(network.getSide() == rpc::twoparty::Side::CLIENT ? rpc::twoparty::Side::SERVER : rpc::twoparty::Side::CLIENT); return rpcSystem.bootstrap(vatId); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/rpc-twoparty.capnp.c++0000644000175000017500000004147713340402540022715 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: rpc-twoparty.capnp #include "rpc-twoparty.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<26> b_9fd69ebc87b9719c = { { 0, 0, 0, 0, 5, 0, 6, 0, 156, 113, 185, 135, 188, 158, 214, 159, 25, 0, 0, 0, 2, 0, 0, 0, 161, 242, 218, 92, 136, 199, 132, 161, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 83, 105, 100, 101, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 101, 114, 118, 101, 114, 0, 0, 99, 108, 105, 101, 110, 116, 0, 0, } }; ::capnp::word const* const bp_9fd69ebc87b9719c = b_9fd69ebc87b9719c.words; #if !CAPNP_LITE static const uint16_t m_9fd69ebc87b9719c[] = {1, 0}; const ::capnp::_::RawSchema s_9fd69ebc87b9719c = { 0x9fd69ebc87b9719c, b_9fd69ebc87b9719c.words, 26, nullptr, m_9fd69ebc87b9719c, 0, 2, nullptr, nullptr, nullptr, { &s_9fd69ebc87b9719c, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE CAPNP_DEFINE_ENUM(Side_9fd69ebc87b9719c, 9fd69ebc87b9719c); static const ::capnp::_::AlignedData<33> b_d20b909fee733a8e = { { 0, 0, 0, 0, 5, 0, 6, 0, 142, 58, 115, 238, 159, 144, 11, 210, 25, 0, 0, 0, 1, 0, 1, 0, 161, 242, 218, 92, 136, 199, 132, 161, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 86, 97, 116, 73, 100, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 115, 105, 100, 101, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 156, 113, 185, 135, 188, 158, 214, 159, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d20b909fee733a8e = b_d20b909fee733a8e.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d20b909fee733a8e[] = { &s_9fd69ebc87b9719c, }; static const uint16_t m_d20b909fee733a8e[] = {0}; static const uint16_t i_d20b909fee733a8e[] = {0}; const ::capnp::_::RawSchema s_d20b909fee733a8e = { 0xd20b909fee733a8e, b_d20b909fee733a8e.words, 33, d_d20b909fee733a8e, m_d20b909fee733a8e, 1, 1, i_d20b909fee733a8e, nullptr, nullptr, { &s_d20b909fee733a8e, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<34> b_b88d09a9c5f39817 = { { 0, 0, 0, 0, 5, 0, 6, 0, 23, 152, 243, 197, 169, 9, 141, 184, 25, 0, 0, 0, 1, 0, 1, 0, 161, 242, 218, 92, 136, 199, 132, 161, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 42, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 80, 114, 111, 118, 105, 115, 105, 111, 110, 73, 100, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 106, 111, 105, 110, 73, 100, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b88d09a9c5f39817 = b_b88d09a9c5f39817.words; #if !CAPNP_LITE static const uint16_t m_b88d09a9c5f39817[] = {0}; static const uint16_t i_b88d09a9c5f39817[] = {0}; const ::capnp::_::RawSchema s_b88d09a9c5f39817 = { 0xb88d09a9c5f39817, b_b88d09a9c5f39817.words, 34, nullptr, m_b88d09a9c5f39817, 0, 1, i_b88d09a9c5f39817, nullptr, nullptr, { &s_b88d09a9c5f39817, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<18> b_89f389b6fd4082c1 = { { 0, 0, 0, 0, 5, 0, 6, 0, 193, 130, 64, 253, 182, 137, 243, 137, 25, 0, 0, 0, 1, 0, 0, 0, 161, 242, 218, 92, 136, 199, 132, 161, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 42, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 82, 101, 99, 105, 112, 105, 101, 110, 116, 73, 100, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, } }; ::capnp::word const* const bp_89f389b6fd4082c1 = b_89f389b6fd4082c1.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_89f389b6fd4082c1 = { 0x89f389b6fd4082c1, b_89f389b6fd4082c1.words, 18, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_89f389b6fd4082c1, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<19> b_b47f4979672cb59d = { { 0, 0, 0, 0, 5, 0, 6, 0, 157, 181, 44, 103, 121, 73, 127, 180, 25, 0, 0, 0, 1, 0, 0, 0, 161, 242, 218, 92, 136, 199, 132, 161, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 74, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 84, 104, 105, 114, 100, 80, 97, 114, 116, 121, 67, 97, 112, 73, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, } }; ::capnp::word const* const bp_b47f4979672cb59d = b_b47f4979672cb59d.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_b47f4979672cb59d = { 0xb47f4979672cb59d, b_b47f4979672cb59d.words, 19, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_b47f4979672cb59d, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<65> b_95b29059097fca83 = { { 0, 0, 0, 0, 5, 0, 6, 0, 131, 202, 127, 9, 89, 144, 178, 149, 25, 0, 0, 0, 1, 0, 1, 0, 161, 242, 218, 92, 136, 199, 132, 161, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 42, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 74, 111, 105, 110, 75, 101, 121, 80, 97, 114, 116, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 106, 111, 105, 110, 73, 100, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 116, 67, 111, 117, 110, 116, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 116, 78, 117, 109, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_95b29059097fca83 = b_95b29059097fca83.words; #if !CAPNP_LITE static const uint16_t m_95b29059097fca83[] = {0, 1, 2}; static const uint16_t i_95b29059097fca83[] = {0, 1, 2}; const ::capnp::_::RawSchema s_95b29059097fca83 = { 0x95b29059097fca83, b_95b29059097fca83.words, 65, nullptr, m_95b29059097fca83, 0, 3, i_95b29059097fca83, nullptr, nullptr, { &s_95b29059097fca83, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<65> b_9d263a3630b7ebee = { { 0, 0, 0, 0, 5, 0, 6, 0, 238, 235, 183, 48, 54, 58, 38, 157, 25, 0, 0, 0, 1, 0, 1, 0, 161, 242, 218, 92, 136, 199, 132, 161, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 34, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 114, 112, 99, 45, 116, 119, 111, 112, 97, 114, 116, 121, 46, 99, 97, 112, 110, 112, 58, 74, 111, 105, 110, 82, 101, 115, 117, 108, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 32, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 106, 111, 105, 110, 73, 100, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 117, 99, 99, 101, 101, 100, 101, 100, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9d263a3630b7ebee = b_9d263a3630b7ebee.words; #if !CAPNP_LITE static const uint16_t m_9d263a3630b7ebee[] = {2, 0, 1}; static const uint16_t i_9d263a3630b7ebee[] = {0, 1, 2}; const ::capnp::_::RawSchema s_9d263a3630b7ebee = { 0x9d263a3630b7ebee, b_9d263a3630b7ebee.words, 65, nullptr, m_9d263a3630b7ebee, 0, 3, i_9d263a3630b7ebee, nullptr, nullptr, { &s_9d263a3630b7ebee, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { namespace rpc { namespace twoparty { // VatId constexpr uint16_t VatId::_capnpPrivate::dataWordSize; constexpr uint16_t VatId::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind VatId::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* VatId::_capnpPrivate::schema; #endif // !CAPNP_LITE // ProvisionId constexpr uint16_t ProvisionId::_capnpPrivate::dataWordSize; constexpr uint16_t ProvisionId::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind ProvisionId::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* ProvisionId::_capnpPrivate::schema; #endif // !CAPNP_LITE // RecipientId constexpr uint16_t RecipientId::_capnpPrivate::dataWordSize; constexpr uint16_t RecipientId::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind RecipientId::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* RecipientId::_capnpPrivate::schema; #endif // !CAPNP_LITE // ThirdPartyCapId constexpr uint16_t ThirdPartyCapId::_capnpPrivate::dataWordSize; constexpr uint16_t ThirdPartyCapId::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind ThirdPartyCapId::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* ThirdPartyCapId::_capnpPrivate::schema; #endif // !CAPNP_LITE // JoinKeyPart constexpr uint16_t JoinKeyPart::_capnpPrivate::dataWordSize; constexpr uint16_t JoinKeyPart::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind JoinKeyPart::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* JoinKeyPart::_capnpPrivate::schema; #endif // !CAPNP_LITE // JoinResult constexpr uint16_t JoinResult::_capnpPrivate::dataWordSize; constexpr uint16_t JoinResult::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind JoinResult::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* JoinResult::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/persistent.capnp.c++0000644000175000017500000005756213340402540022444 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: persistent.capnp #include "persistent.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<54> b_c8cb212fcd9f5691 = { { 0, 0, 0, 0, 5, 0, 6, 0, 145, 86, 159, 205, 47, 33, 203, 200, 23, 0, 0, 0, 3, 0, 0, 0, 215, 238, 63, 152, 54, 8, 99, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 21, 0, 0, 0, 18, 1, 0, 0, 37, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 71, 0, 0, 0, 145, 0, 0, 0, 7, 0, 0, 0, 145, 0, 0, 0, 23, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 80, 101, 114, 115, 105, 115, 116, 101, 110, 116, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 165, 115, 48, 24, 89, 186, 111, 247, 9, 0, 0, 0, 90, 0, 0, 0, 191, 239, 64, 140, 193, 72, 104, 183, 9, 0, 0, 0, 98, 0, 0, 0, 83, 97, 118, 101, 80, 97, 114, 97, 109, 115, 0, 0, 0, 0, 0, 0, 83, 97, 118, 101, 82, 101, 115, 117, 108, 116, 115, 0, 0, 0, 0, 0, 4, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 115, 48, 24, 89, 186, 111, 247, 191, 239, 64, 140, 193, 72, 104, 183, 17, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 1, 0, 28, 0, 0, 0, 0, 0, 1, 0, 45, 0, 0, 0, 7, 0, 0, 0, 115, 97, 118, 101, 0, 0, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 82, 0, 0, 0, 9, 0, 0, 0, 50, 0, 0, 0, 83, 116, 117, 114, 100, 121, 82, 101, 102, 0, 0, 0, 0, 0, 0, 0, 79, 119, 110, 101, 114, 0, 0, 0, } }; ::capnp::word const* const bp_c8cb212fcd9f5691 = b_c8cb212fcd9f5691.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c8cb212fcd9f5691[] = { &s_b76848c18c40efbf, &s_f76fba59183073a5, }; static const uint16_t m_c8cb212fcd9f5691[] = {0}; KJ_CONSTEXPR(const) ::capnp::_::RawBrandedSchema::Dependency bd_c8cb212fcd9f5691[] = { { 33554432, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::SaveParams::_capnpPrivate::brand() }, { 50331648, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::SaveResults::_capnpPrivate::brand() }, }; const ::capnp::_::RawSchema s_c8cb212fcd9f5691 = { 0xc8cb212fcd9f5691, b_c8cb212fcd9f5691.words, 54, d_c8cb212fcd9f5691, m_c8cb212fcd9f5691, 2, 1, nullptr, nullptr, nullptr, { &s_c8cb212fcd9f5691, nullptr, bd_c8cb212fcd9f5691, 0, sizeof(bd_c8cb212fcd9f5691) / sizeof(bd_c8cb212fcd9f5691[0]), nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<35> b_f76fba59183073a5 = { { 0, 0, 0, 0, 5, 0, 6, 0, 165, 115, 48, 24, 89, 186, 111, 247, 34, 0, 0, 0, 1, 0, 0, 0, 145, 86, 159, 205, 47, 33, 203, 200, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 21, 0, 0, 0, 106, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 80, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 83, 97, 118, 101, 80, 97, 114, 97, 109, 115, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 115, 101, 97, 108, 70, 111, 114, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f76fba59183073a5 = b_f76fba59183073a5.words; #if !CAPNP_LITE static const uint16_t m_f76fba59183073a5[] = {0}; static const uint16_t i_f76fba59183073a5[] = {0}; const ::capnp::_::RawSchema s_f76fba59183073a5 = { 0xf76fba59183073a5, b_f76fba59183073a5.words, 35, nullptr, m_f76fba59183073a5, 0, 1, i_f76fba59183073a5, nullptr, nullptr, { &s_f76fba59183073a5, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<36> b_b76848c18c40efbf = { { 0, 0, 0, 0, 5, 0, 6, 0, 191, 239, 64, 140, 193, 72, 104, 183, 34, 0, 0, 0, 1, 0, 0, 0, 145, 86, 159, 205, 47, 33, 203, 200, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 21, 0, 0, 0, 114, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 80, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 83, 97, 118, 101, 82, 101, 115, 117, 108, 116, 115, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 24, 0, 0, 0, 2, 0, 1, 0, 115, 116, 117, 114, 100, 121, 82, 101, 102, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b76848c18c40efbf = b_b76848c18c40efbf.words; #if !CAPNP_LITE static const uint16_t m_b76848c18c40efbf[] = {0}; static const uint16_t i_b76848c18c40efbf[] = {0}; const ::capnp::_::RawSchema s_b76848c18c40efbf = { 0xb76848c18c40efbf, b_b76848c18c40efbf.words, 36, nullptr, m_b76848c18c40efbf, 0, 1, i_b76848c18c40efbf, nullptr, nullptr, { &s_b76848c18c40efbf, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<99> b_84ff286cd00a3ed4 = { { 0, 0, 0, 0, 5, 0, 6, 0, 212, 62, 10, 208, 108, 40, 255, 132, 23, 0, 0, 0, 3, 0, 0, 0, 215, 238, 63, 152, 54, 8, 99, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 21, 0, 0, 0, 34, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 135, 0, 0, 0, 41, 1, 0, 0, 7, 0, 0, 0, 41, 1, 0, 0, 39, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 82, 101, 97, 108, 109, 71, 97, 116, 101, 119, 97, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 87, 9, 57, 29, 204, 194, 240, 191, 239, 64, 140, 193, 72, 104, 183, 49, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 0, 0, 1, 0, 60, 0, 0, 0, 0, 0, 1, 0, 129, 0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 170, 163, 45, 72, 139, 161, 175, 236, 191, 239, 64, 140, 193, 72, 104, 183, 117, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 0, 0, 1, 0, 128, 0, 0, 0, 0, 0, 1, 0, 197, 0, 0, 0, 7, 0, 0, 0, 105, 109, 112, 111, 114, 116, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 212, 62, 10, 208, 108, 40, 255, 132, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 101, 120, 112, 111, 114, 116, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 212, 62, 10, 208, 108, 40, 255, 132, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 16, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 98, 0, 0, 0, 17, 0, 0, 0, 98, 0, 0, 0, 21, 0, 0, 0, 114, 0, 0, 0, 25, 0, 0, 0, 114, 0, 0, 0, 73, 110, 116, 101, 114, 110, 97, 108, 82, 101, 102, 0, 0, 0, 0, 0, 69, 120, 116, 101, 114, 110, 97, 108, 82, 101, 102, 0, 0, 0, 0, 0, 73, 110, 116, 101, 114, 110, 97, 108, 79, 119, 110, 101, 114, 0, 0, 0, 69, 120, 116, 101, 114, 110, 97, 108, 79, 119, 110, 101, 114, 0, 0, 0, } }; ::capnp::word const* const bp_84ff286cd00a3ed4 = b_84ff286cd00a3ed4.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_84ff286cd00a3ed4[] = { &s_b76848c18c40efbf, &s_ecafa18b482da3aa, &s_f0c2cc1d3909574d, }; static const uint16_t m_84ff286cd00a3ed4[] = {1, 0}; KJ_CONSTEXPR(const) ::capnp::_::RawBrandedSchema::Dependency bd_84ff286cd00a3ed4[] = { { 33554432, ::capnp::RealmGateway< ::capnp::AnyPointer, ::capnp::AnyPointer, ::capnp::AnyPointer, ::capnp::AnyPointer>::ImportParams::_capnpPrivate::brand() }, { 33554433, ::capnp::RealmGateway< ::capnp::AnyPointer, ::capnp::AnyPointer, ::capnp::AnyPointer, ::capnp::AnyPointer>::ExportParams::_capnpPrivate::brand() }, { 50331648, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::SaveResults::_capnpPrivate::brand() }, { 50331649, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::SaveResults::_capnpPrivate::brand() }, }; const ::capnp::_::RawSchema s_84ff286cd00a3ed4 = { 0x84ff286cd00a3ed4, b_84ff286cd00a3ed4.words, 99, d_84ff286cd00a3ed4, m_84ff286cd00a3ed4, 3, 2, nullptr, nullptr, nullptr, { &s_84ff286cd00a3ed4, nullptr, bd_84ff286cd00a3ed4, 0, sizeof(bd_84ff286cd00a3ed4) / sizeof(bd_84ff286cd00a3ed4[0]), nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<86> b_f0c2cc1d3909574d = { { 0, 0, 0, 0, 5, 0, 6, 0, 77, 87, 9, 57, 29, 204, 194, 240, 36, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 21, 0, 0, 0, 146, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 82, 101, 97, 108, 109, 71, 97, 116, 101, 119, 97, 121, 46, 105, 109, 112, 111, 114, 116, 36, 80, 97, 114, 97, 109, 115, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 120, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 3, 0, 1, 0, 196, 0, 0, 0, 2, 0, 1, 0, 99, 97, 112, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 115, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 165, 115, 48, 24, 89, 186, 111, 247, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f0c2cc1d3909574d = b_f0c2cc1d3909574d.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_f0c2cc1d3909574d[] = { &s_c8cb212fcd9f5691, &s_f76fba59183073a5, }; static const uint16_t m_f0c2cc1d3909574d[] = {0, 1}; static const uint16_t i_f0c2cc1d3909574d[] = {0, 1}; KJ_CONSTEXPR(const) ::capnp::_::RawBrandedSchema::Dependency bd_f0c2cc1d3909574d[] = { { 16777216, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::_capnpPrivate::brand() }, { 16777217, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::SaveParams::_capnpPrivate::brand() }, }; const ::capnp::_::RawSchema s_f0c2cc1d3909574d = { 0xf0c2cc1d3909574d, b_f0c2cc1d3909574d.words, 86, d_f0c2cc1d3909574d, m_f0c2cc1d3909574d, 2, 2, i_f0c2cc1d3909574d, nullptr, nullptr, { &s_f0c2cc1d3909574d, nullptr, bd_f0c2cc1d3909574d, 0, sizeof(bd_f0c2cc1d3909574d) / sizeof(bd_f0c2cc1d3909574d[0]), nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<86> b_ecafa18b482da3aa = { { 0, 0, 0, 0, 5, 0, 6, 0, 170, 163, 45, 72, 139, 161, 175, 236, 36, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 21, 0, 0, 0, 146, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 82, 101, 97, 108, 109, 71, 97, 116, 101, 119, 97, 121, 46, 101, 120, 112, 111, 114, 116, 36, 80, 97, 114, 97, 109, 115, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 120, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 3, 0, 1, 0, 196, 0, 0, 0, 2, 0, 1, 0, 99, 97, 112, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 115, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 165, 115, 48, 24, 89, 186, 111, 247, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 2, 0, 1, 0, 145, 86, 159, 205, 47, 33, 203, 200, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 212, 62, 10, 208, 108, 40, 255, 132, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ecafa18b482da3aa = b_ecafa18b482da3aa.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_ecafa18b482da3aa[] = { &s_c8cb212fcd9f5691, &s_f76fba59183073a5, }; static const uint16_t m_ecafa18b482da3aa[] = {0, 1}; static const uint16_t i_ecafa18b482da3aa[] = {0, 1}; KJ_CONSTEXPR(const) ::capnp::_::RawBrandedSchema::Dependency bd_ecafa18b482da3aa[] = { { 16777216, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::_capnpPrivate::brand() }, { 16777217, ::capnp::Persistent< ::capnp::AnyPointer, ::capnp::AnyPointer>::SaveParams::_capnpPrivate::brand() }, }; const ::capnp::_::RawSchema s_ecafa18b482da3aa = { 0xecafa18b482da3aa, b_ecafa18b482da3aa.words, 86, d_ecafa18b482da3aa, m_ecafa18b482da3aa, 2, 2, i_ecafa18b482da3aa, nullptr, nullptr, { &s_ecafa18b482da3aa, nullptr, bd_ecafa18b482da3aa, 0, sizeof(bd_ecafa18b482da3aa) / sizeof(bd_ecafa18b482da3aa[0]), nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<22> b_f622595091cafb67 = { { 0, 0, 0, 0, 5, 0, 6, 0, 103, 251, 202, 145, 80, 89, 34, 246, 23, 0, 0, 0, 5, 0, 32, 1, 215, 238, 63, 152, 54, 8, 99, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 18, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 46, 99, 97, 112, 110, 112, 58, 112, 101, 114, 115, 105, 115, 116, 101, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f622595091cafb67 = b_f622595091cafb67.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_f622595091cafb67 = { 0xf622595091cafb67, b_f622595091cafb67.words, 22, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_f622595091cafb67, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp capnproto-c++-0.8.0/src/capnp/ez-rpc.c++0000644000175000017500000003204713340402540020333 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "ez-rpc.h" #include "rpc-twoparty.h" #include #include #include #include #include namespace capnp { KJ_THREADLOCAL_PTR(EzRpcContext) threadEzContext = nullptr; class EzRpcContext: public kj::Refcounted { public: EzRpcContext(): ioContext(kj::setupAsyncIo()) { threadEzContext = this; } ~EzRpcContext() noexcept(false) { KJ_REQUIRE(threadEzContext == this, "EzRpcContext destroyed from different thread than it was created.") { return; } threadEzContext = nullptr; } kj::WaitScope& getWaitScope() { return ioContext.waitScope; } kj::AsyncIoProvider& getIoProvider() { return *ioContext.provider; } kj::LowLevelAsyncIoProvider& getLowLevelIoProvider() { return *ioContext.lowLevelProvider; } static kj::Own getThreadLocal() { EzRpcContext* existing = threadEzContext; if (existing != nullptr) { return kj::addRef(*existing); } else { return kj::refcounted(); } } private: kj::AsyncIoContext ioContext; }; // ======================================================================================= kj::Promise> connectAttach(kj::Own&& addr) { return addr->connect().attach(kj::mv(addr)); } struct EzRpcClient::Impl { kj::Own context; struct ClientContext { kj::Own stream; TwoPartyVatNetwork network; RpcSystem rpcSystem; ClientContext(kj::Own&& stream, ReaderOptions readerOpts) : stream(kj::mv(stream)), network(*this->stream, rpc::twoparty::Side::CLIENT, readerOpts), rpcSystem(makeRpcClient(network)) {} Capability::Client getMain() { word scratch[4]; memset(scratch, 0, sizeof(scratch)); MallocMessageBuilder message(scratch); auto hostId = message.getRoot(); hostId.setSide(rpc::twoparty::Side::SERVER); return rpcSystem.bootstrap(hostId); } Capability::Client restore(kj::StringPtr name) { word scratch[64]; memset(scratch, 0, sizeof(scratch)); MallocMessageBuilder message(scratch); auto hostIdOrphan = message.getOrphanage().newOrphan(); auto hostId = hostIdOrphan.get(); hostId.setSide(rpc::twoparty::Side::SERVER); auto objectId = message.getRoot(); objectId.setAs(name); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" return rpcSystem.restore(hostId, objectId); #pragma GCC diagnostic pop } }; kj::ForkedPromise setupPromise; kj::Maybe> clientContext; // Filled in before `setupPromise` resolves. Impl(kj::StringPtr serverAddress, uint defaultPort, ReaderOptions readerOpts) : context(EzRpcContext::getThreadLocal()), setupPromise(context->getIoProvider().getNetwork() .parseAddress(serverAddress, defaultPort) .then([](kj::Own&& addr) { return connectAttach(kj::mv(addr)); }).then([this, readerOpts](kj::Own&& stream) { clientContext = kj::heap(kj::mv(stream), readerOpts); }).fork()) {} Impl(const struct sockaddr* serverAddress, uint addrSize, ReaderOptions readerOpts) : context(EzRpcContext::getThreadLocal()), setupPromise( connectAttach(context->getIoProvider().getNetwork() .getSockaddr(serverAddress, addrSize)) .then([this, readerOpts](kj::Own&& stream) { clientContext = kj::heap(kj::mv(stream), readerOpts); }).fork()) {} Impl(int socketFd, ReaderOptions readerOpts) : context(EzRpcContext::getThreadLocal()), setupPromise(kj::Promise(kj::READY_NOW).fork()), clientContext(kj::heap( context->getLowLevelIoProvider().wrapSocketFd(socketFd), readerOpts)) {} }; EzRpcClient::EzRpcClient(kj::StringPtr serverAddress, uint defaultPort, ReaderOptions readerOpts) : impl(kj::heap(serverAddress, defaultPort, readerOpts)) {} EzRpcClient::EzRpcClient(const struct sockaddr* serverAddress, uint addrSize, ReaderOptions readerOpts) : impl(kj::heap(serverAddress, addrSize, readerOpts)) {} EzRpcClient::EzRpcClient(int socketFd, ReaderOptions readerOpts) : impl(kj::heap(socketFd, readerOpts)) {} EzRpcClient::~EzRpcClient() noexcept(false) {} Capability::Client EzRpcClient::getMain() { KJ_IF_MAYBE(client, impl->clientContext) { return client->get()->getMain(); } else { return impl->setupPromise.addBranch().then([this]() { return KJ_ASSERT_NONNULL(impl->clientContext)->getMain(); }); } } Capability::Client EzRpcClient::importCap(kj::StringPtr name) { KJ_IF_MAYBE(client, impl->clientContext) { return client->get()->restore(name); } else { return impl->setupPromise.addBranch().then(kj::mvCapture(kj::heapString(name), [this](kj::String&& name) { return KJ_ASSERT_NONNULL(impl->clientContext)->restore(name); })); } } kj::WaitScope& EzRpcClient::getWaitScope() { return impl->context->getWaitScope(); } kj::AsyncIoProvider& EzRpcClient::getIoProvider() { return impl->context->getIoProvider(); } kj::LowLevelAsyncIoProvider& EzRpcClient::getLowLevelIoProvider() { return impl->context->getLowLevelIoProvider(); } // ======================================================================================= namespace { class DummyFilter: public kj::LowLevelAsyncIoProvider::NetworkFilter { public: bool shouldAllow(const struct sockaddr* addr, uint addrlen) override { return true; } }; static DummyFilter DUMMY_FILTER; } // namespace struct EzRpcServer::Impl final: public SturdyRefRestorer, public kj::TaskSet::ErrorHandler { Capability::Client mainInterface; kj::Own context; struct ExportedCap { kj::String name; Capability::Client cap = nullptr; ExportedCap(kj::StringPtr name, Capability::Client cap) : name(kj::heapString(name)), cap(cap) {} ExportedCap() = default; ExportedCap(const ExportedCap&) = delete; ExportedCap(ExportedCap&&) = default; ExportedCap& operator=(const ExportedCap&) = delete; ExportedCap& operator=(ExportedCap&&) = default; // Make std::map happy... }; std::map exportMap; kj::ForkedPromise portPromise; kj::TaskSet tasks; struct ServerContext { kj::Own stream; TwoPartyVatNetwork network; RpcSystem rpcSystem; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" ServerContext(kj::Own&& stream, SturdyRefRestorer& restorer, ReaderOptions readerOpts) : stream(kj::mv(stream)), network(*this->stream, rpc::twoparty::Side::SERVER, readerOpts), rpcSystem(makeRpcServer(network, restorer)) {} #pragma GCC diagnostic pop }; Impl(Capability::Client mainInterface, kj::StringPtr bindAddress, uint defaultPort, ReaderOptions readerOpts) : mainInterface(kj::mv(mainInterface)), context(EzRpcContext::getThreadLocal()), portPromise(nullptr), tasks(*this) { auto paf = kj::newPromiseAndFulfiller(); portPromise = paf.promise.fork(); tasks.add(context->getIoProvider().getNetwork().parseAddress(bindAddress, defaultPort) .then(kj::mvCapture(paf.fulfiller, [this, readerOpts](kj::Own>&& portFulfiller, kj::Own&& addr) { auto listener = addr->listen(); portFulfiller->fulfill(listener->getPort()); acceptLoop(kj::mv(listener), readerOpts); }))); } Impl(Capability::Client mainInterface, struct sockaddr* bindAddress, uint addrSize, ReaderOptions readerOpts) : mainInterface(kj::mv(mainInterface)), context(EzRpcContext::getThreadLocal()), portPromise(nullptr), tasks(*this) { auto listener = context->getIoProvider().getNetwork() .getSockaddr(bindAddress, addrSize)->listen(); portPromise = kj::Promise(listener->getPort()).fork(); acceptLoop(kj::mv(listener), readerOpts); } Impl(Capability::Client mainInterface, int socketFd, uint port, ReaderOptions readerOpts) : mainInterface(kj::mv(mainInterface)), context(EzRpcContext::getThreadLocal()), portPromise(kj::Promise(port).fork()), tasks(*this) { acceptLoop(context->getLowLevelIoProvider().wrapListenSocketFd(socketFd, DUMMY_FILTER), readerOpts); } void acceptLoop(kj::Own&& listener, ReaderOptions readerOpts) { auto ptr = listener.get(); tasks.add(ptr->accept().then(kj::mvCapture(kj::mv(listener), [this, readerOpts](kj::Own&& listener, kj::Own&& connection) { acceptLoop(kj::mv(listener), readerOpts); auto server = kj::heap(kj::mv(connection), *this, readerOpts); // Arrange to destroy the server context when all references are gone, or when the // EzRpcServer is destroyed (which will destroy the TaskSet). tasks.add(server->network.onDisconnect().attach(kj::mv(server))); }))); } Capability::Client restore(AnyPointer::Reader objectId) override { if (objectId.isNull()) { return mainInterface; } else { auto name = objectId.getAs(); auto iter = exportMap.find(name); if (iter == exportMap.end()) { KJ_FAIL_REQUIRE("Server exports no such capability.", name) { break; } return nullptr; } else { return iter->second.cap; } } } void taskFailed(kj::Exception&& exception) override { kj::throwFatalException(kj::mv(exception)); } }; EzRpcServer::EzRpcServer(Capability::Client mainInterface, kj::StringPtr bindAddress, uint defaultPort, ReaderOptions readerOpts) : impl(kj::heap(kj::mv(mainInterface), bindAddress, defaultPort, readerOpts)) {} EzRpcServer::EzRpcServer(Capability::Client mainInterface, struct sockaddr* bindAddress, uint addrSize, ReaderOptions readerOpts) : impl(kj::heap(kj::mv(mainInterface), bindAddress, addrSize, readerOpts)) {} EzRpcServer::EzRpcServer(Capability::Client mainInterface, int socketFd, uint port, ReaderOptions readerOpts) : impl(kj::heap(kj::mv(mainInterface), socketFd, port, readerOpts)) {} EzRpcServer::EzRpcServer(kj::StringPtr bindAddress, uint defaultPort, ReaderOptions readerOpts) : EzRpcServer(nullptr, bindAddress, defaultPort, readerOpts) {} EzRpcServer::EzRpcServer(struct sockaddr* bindAddress, uint addrSize, ReaderOptions readerOpts) : EzRpcServer(nullptr, bindAddress, addrSize, readerOpts) {} EzRpcServer::EzRpcServer(int socketFd, uint port, ReaderOptions readerOpts) : EzRpcServer(nullptr, socketFd, port, readerOpts) {} EzRpcServer::~EzRpcServer() noexcept(false) {} void EzRpcServer::exportCap(kj::StringPtr name, Capability::Client cap) { Impl::ExportedCap entry(kj::heapString(name), cap); impl->exportMap[entry.name] = kj::mv(entry); } kj::Promise EzRpcServer::getPort() { return impl->portPromise.addBranch(); } kj::WaitScope& EzRpcServer::getWaitScope() { return impl->context->getWaitScope(); } kj::AsyncIoProvider& EzRpcServer::getIoProvider() { return impl->context->getIoProvider(); } kj::LowLevelAsyncIoProvider& EzRpcServer::getLowLevelIoProvider() { return impl->context->getLowLevelIoProvider(); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/c++.capnp.c++0000644000175000017500000000533613340402540020604 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: c++.capnp #include "c++.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<21> b_b9c6f99ebf805f2c = { { 0, 0, 0, 0, 5, 0, 6, 0, 44, 95, 128, 191, 158, 249, 198, 185, 16, 0, 0, 0, 5, 0, 1, 0, 129, 78, 48, 184, 123, 125, 248, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 210, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 43, 43, 46, 99, 97, 112, 110, 112, 58, 110, 97, 109, 101, 115, 112, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b9c6f99ebf805f2c = b_b9c6f99ebf805f2c.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_b9c6f99ebf805f2c = { 0xb9c6f99ebf805f2c, b_b9c6f99ebf805f2c.words, 21, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_b9c6f99ebf805f2c, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<20> b_f264a779fef191ce = { { 0, 0, 0, 0, 5, 0, 6, 0, 206, 145, 241, 254, 121, 167, 100, 242, 16, 0, 0, 0, 5, 0, 252, 7, 129, 78, 48, 184, 123, 125, 248, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 170, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 99, 43, 43, 46, 99, 97, 112, 110, 112, 58, 110, 97, 109, 101, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f264a779fef191ce = b_f264a779fef191ce.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_f264a779fef191ce = { 0xf264a779fef191ce, b_f264a779fef191ce.words, 20, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_f264a779fef191ce, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp capnproto-c++-0.8.0/src/capnp/blob.c++0000644000175000017500000000240613340402540020045 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "blob.h" namespace capnp { char Text::Builder::nulstr[1] = ""; } // namespace capnp capnproto-c++-0.8.0/src/capnp/arena.h0000644000175000017500000004672413650101756020120 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #ifndef CAPNP_PRIVATE #error "This header is only meant to be included by Cap'n Proto's own source code." #endif #include #include #include #include #include #include "common.h" #include "message.h" #include "layout.h" #include #if !CAPNP_LITE #include "capability.h" #endif // !CAPNP_LITE CAPNP_BEGIN_HEADER namespace capnp { #if !CAPNP_LITE class ClientHook; #endif // !CAPNP_LITE namespace _ { // private class SegmentReader; class SegmentBuilder; class Arena; class BuilderArena; class ReadLimiter; class Segment; typedef kj::Id SegmentId; class ReadLimiter { // Used to keep track of how much data has been processed from a message, and cut off further // processing if and when a particular limit is reached. This is primarily intended to guard // against maliciously-crafted messages which contain cycles or overlapping structures. Cycles // and overlapping are not permitted by the Cap'n Proto format because in many cases they could // be used to craft a deceptively small message which could consume excessive server resources to // process, perhaps even sending it into an infinite loop. Actually detecting overlaps would be // time-consuming, so instead we just keep track of how many words worth of data structures the // receiver has actually dereferenced and error out if this gets too high. // // This counting takes place as you call getters (for non-primitive values) on the message // readers. If you call the same getter twice, the data it returns may be double-counted. This // should not be a big deal in most cases -- just set the read limit high enough that it will // only trigger in unreasonable cases. // // This class is "safe" to use from multiple threads for its intended use case. Threads may // overwrite each others' changes to the counter, but this is OK because it only means that the // limit is enforced a bit less strictly -- it will still kick in eventually. public: inline explicit ReadLimiter(); // No limit. inline explicit ReadLimiter(WordCount64 limit); // Limit to the given number of words. inline void reset(WordCount64 limit); KJ_ALWAYS_INLINE(bool canRead(WordCount64 amount, Arena* arena)); void unread(WordCount64 amount); // Adds back some words to the limit. Useful when the caller knows they are double-reading // some data. private: volatile uint64_t limit; // Current limit, decremented each time catRead() is called. Volatile because multiple threads // could be trying to modify it at once. (This is not real thread-safety, but good enough for // the purpose of this class. See class comment.) KJ_DISALLOW_COPY(ReadLimiter); }; #if !CAPNP_LITE class BrokenCapFactory { // Callback for constructing broken caps. We use this so that we can avoid arena.c++ having a // link-time dependency on capability code that lives in libcapnp-rpc. public: virtual kj::Own newBrokenCap(kj::StringPtr description) = 0; virtual kj::Own newNullCap() = 0; }; #endif // !CAPNP_LITE class SegmentReader { public: inline SegmentReader(Arena* arena, SegmentId id, const word* ptr, SegmentWordCount size, ReadLimiter* readLimiter); KJ_ALWAYS_INLINE(const word* checkOffset(const word* from, ptrdiff_t offset)); // Adds the given offset to the given pointer, checks that it is still within the bounds of the // segment, then returns it. Note that the "end" pointer of the segment (which technically points // to the word after the last in the segment) is considered in-bounds for this purpose, so you // can't necessarily dereference it. You must call checkObject() next to check that the object // you want to read is entirely in-bounds. // // If `from + offset` is out-of-range, this returns a pointer to the end of the segment. Thus, // any non-zero-sized object will fail `checkObject()`. We do this instead of throwing to save // some code footprint. KJ_ALWAYS_INLINE(bool checkObject(const word* start, WordCountN<31> size)); // Assuming that `start` is in-bounds for this segment (probably checked using `checkOffset()`), // check that `start + size` is also in-bounds, and hence the whole area in-between is valid. KJ_ALWAYS_INLINE(bool amplifiedRead(WordCount virtualAmount)); // Indicates that the reader should pretend that `virtualAmount` additional data was read even // though no actual pointer was traversed. This is used e.g. when reading a struct list pointer // where the element sizes are zero -- the sender could set the list size arbitrarily high and // cause the receiver to iterate over this list even though the message itself is small, so we // need to defend against DoS attacks based on this. inline Arena* getArena(); inline SegmentId getSegmentId(); inline const word* getStartPtr(); inline SegmentWordCount getOffsetTo(const word* ptr); inline SegmentWordCount getSize(); inline kj::ArrayPtr getArray(); inline void unread(WordCount64 amount); // Add back some words to the ReadLimiter. private: Arena* arena; SegmentId id; kj::ArrayPtr ptr; // size guaranteed to fit in SEGMENT_WORD_COUNT_BITS bits ReadLimiter* readLimiter; KJ_DISALLOW_COPY(SegmentReader); friend class SegmentBuilder; static void abortCheckObjectFault(); // Called in debug mode in cases that would segfault in opt mode. (Should be impossible!) }; class SegmentBuilder: public SegmentReader { public: inline SegmentBuilder(BuilderArena* arena, SegmentId id, word* ptr, SegmentWordCount size, ReadLimiter* readLimiter, SegmentWordCount wordsUsed = ZERO * WORDS); inline SegmentBuilder(BuilderArena* arena, SegmentId id, const word* ptr, SegmentWordCount size, ReadLimiter* readLimiter); inline SegmentBuilder(BuilderArena* arena, SegmentId id, decltype(nullptr), ReadLimiter* readLimiter); KJ_ALWAYS_INLINE(word* allocate(SegmentWordCount amount)); KJ_ALWAYS_INLINE(void checkWritable()); // Throw an exception if the segment is read-only (meaning it is a reference to external data). KJ_ALWAYS_INLINE(word* getPtrUnchecked(SegmentWordCount offset)); // Get a writable pointer into the segment. Throws an exception if the segment is read-only (i.e. // a reference to external immutable data). inline BuilderArena* getArena(); inline kj::ArrayPtr currentlyAllocated(); inline void reset(); inline bool isWritable() { return !readOnly; } inline void tryTruncate(word* from, word* to); // If `from` points just past the current end of the segment, then move the end back to `to`. // Otherwise, do nothing. inline bool tryExtend(word* from, word* to); // If `from` points just past the current end of the segment, and `to` is within the segment // boundaries, then move the end up to `to` and return true. Otherwise, do nothing and return // false. private: word* pos; // Pointer to a pointer to the current end point of the segment, i.e. the location where the // next object should be allocated. bool readOnly; void throwNotWritable(); KJ_DISALLOW_COPY(SegmentBuilder); }; class Arena { public: virtual ~Arena() noexcept(false); virtual SegmentReader* tryGetSegment(SegmentId id) = 0; // Gets the segment with the given ID, or return nullptr if no such segment exists. virtual void reportReadLimitReached() = 0; // Called to report that the read limit has been reached. See ReadLimiter, below. This invokes // the VALIDATE_INPUT() macro which may throw an exception; if it returns normally, the caller // will need to continue with default values. }; class ReaderArena final: public Arena { public: explicit ReaderArena(MessageReader* message); ~ReaderArena() noexcept(false); KJ_DISALLOW_COPY(ReaderArena); size_t sizeInWords(); // implements Arena ------------------------------------------------ SegmentReader* tryGetSegment(SegmentId id) override; void reportReadLimitReached() override; private: MessageReader* message; ReadLimiter readLimiter; // Optimize for single-segment messages so that small messages are handled quickly. SegmentReader segment0; typedef kj::HashMap> SegmentMap; kj::MutexGuarded> moreSegments; // We need to mutex-guard the segment map because we lazily initialize segments when they are // first requested, but a Reader is allowed to be used concurrently in multiple threads. Luckily // this only applies to large messages. // // TODO(perf): Thread-local thing instead? Some kind of lockless map? Or do sharing of data // in a different way, where you have to construct a new MessageReader in each thread (but // possibly backed by the same data)? ReaderArena(MessageReader* message, kj::ArrayPtr firstSegment); ReaderArena(MessageReader* message, const word* firstSegment, SegmentWordCount firstSegmentSize); }; class BuilderArena final: public Arena { // A BuilderArena that does not allow the injection of capabilities. public: explicit BuilderArena(MessageBuilder* message); BuilderArena(MessageBuilder* message, kj::ArrayPtr segments); ~BuilderArena() noexcept(false); KJ_DISALLOW_COPY(BuilderArena); size_t sizeInWords(); inline SegmentBuilder* getRootSegment() { return &segment0; } kj::ArrayPtr> getSegmentsForOutput(); // Get an array of all the segments, suitable for writing out. This only returns the allocated // portion of each segment, whereas tryGetSegment() returns something that includes // not-yet-allocated space. inline CapTableBuilder* getLocalCapTable() { // Return a CapTableBuilder that merely implements local loopback. That is, you can set // capabilities, then read the same capabilities back, but there is no intent ever to transmit // these capabilities. A MessageBuilder that isn't imbued with some other CapTable uses this // by default. // // TODO(cleanup): It's sort of a hack that this exists. In theory, perhaps, unimbued // MessageBuilders should throw exceptions on any attempt to access capability fields, like // unimbued MessageReaders do. However, lots of code exists which uses MallocMessageBuilder // as a temporary holder for data to be copied in and out (without being serialized), and it // is expected that such data can include capabilities, which is admittedly reasonable. // Therefore, all MessageBuilders must have a cap table by default. Arguably we should // deprecate this usage and instead define a new helper type for this exact purpose. return &localCapTable; } kj::Own<_::CapTableBuilder> releaseLocalCapTable() { return kj::heap(kj::mv(localCapTable)); } SegmentBuilder* getSegment(SegmentId id); // Get the segment with the given id. Crashes or throws an exception if no such segment exists. struct AllocateResult { SegmentBuilder* segment; word* words; }; AllocateResult allocate(SegmentWordCount amount); // Find a segment with at least the given amount of space available and allocate the space. // Note that allocating directly from a particular segment is much faster, but allocating from // the arena is guaranteed to succeed. Therefore callers should try to allocate from a specific // segment first if there is one, then fall back to the arena. SegmentBuilder* addExternalSegment(kj::ArrayPtr content); // Add a new segment to the arena which points to some existing memory region. The segment is // assumed to be completley full; the arena will never allocate from it. In fact, the segment // is considered read-only. Any attempt to get a Builder pointing into this segment will throw // an exception. Readers are allowed, however. // // This can be used to inject some external data into a message without a copy, e.g. embedding a // large mmap'd file into a message as `Data` without forcing that data to actually be read in // from disk (until the message itself is written out). `Orphanage` provides the public API for // this feature. // implements Arena ------------------------------------------------ SegmentReader* tryGetSegment(SegmentId id) override; void reportReadLimitReached() override; private: MessageBuilder* message; ReadLimiter dummyLimiter; class LocalCapTable final: public CapTableBuilder { public: kj::Maybe> extractCap(uint index) override; uint injectCap(kj::Own&& cap) override; void dropCap(uint index) override; #if !CAPNP_LITE private: kj::Vector>> capTable; #endif // ! CAPNP_LITE }; LocalCapTable localCapTable; SegmentBuilder segment0; kj::ArrayPtr segment0ForOutput; struct MultiSegmentState { kj::Vector> builders; kj::Vector> forOutput; }; kj::Maybe> moreSegments; SegmentBuilder* segmentWithSpace = nullptr; // When allocating, look for space in this segment first before resorting to allocating a new // segment. This is not necessarily the last segment because addExternalSegment() may add a // segment that is already-full, in which case we don't update this pointer. template // Can be `word` or `const word`. SegmentBuilder* addSegmentInternal(kj::ArrayPtr content); }; // ======================================================================================= inline ReadLimiter::ReadLimiter() : limit(kj::maxValue) {} inline ReadLimiter::ReadLimiter(WordCount64 limit): limit(unbound(limit / WORDS)) {} inline void ReadLimiter::reset(WordCount64 limit) { this->limit = unbound(limit / WORDS); } inline bool ReadLimiter::canRead(WordCount64 amount, Arena* arena) { // Be careful not to store an underflowed value into `limit`, even if multiple threads are // decrementing it. uint64_t current = limit; if (KJ_UNLIKELY(unbound(amount / WORDS) > current)) { arena->reportReadLimitReached(); return false; } else { limit = current - unbound(amount / WORDS); return true; } } // ------------------------------------------------------------------- inline SegmentReader::SegmentReader(Arena* arena, SegmentId id, const word* ptr, SegmentWordCount size, ReadLimiter* readLimiter) : arena(arena), id(id), ptr(kj::arrayPtr(ptr, unbound(size / WORDS))), readLimiter(readLimiter) {} inline const word* SegmentReader::checkOffset(const word* from, ptrdiff_t offset) { ptrdiff_t min = ptr.begin() - from; ptrdiff_t max = ptr.end() - from; if (offset >= min && offset <= max) { return from + offset; } else { return ptr.end(); } } inline bool SegmentReader::checkObject(const word* start, WordCountN<31> size) { auto startOffset = intervalLength(ptr.begin(), start, MAX_SEGMENT_WORDS); #ifdef KJ_DEBUG if (startOffset > bounded(ptr.size()) * WORDS) { abortCheckObjectFault(); } #endif return startOffset + size <= bounded(ptr.size()) * WORDS && readLimiter->canRead(size, arena); } inline bool SegmentReader::amplifiedRead(WordCount virtualAmount) { return readLimiter->canRead(virtualAmount, arena); } inline Arena* SegmentReader::getArena() { return arena; } inline SegmentId SegmentReader::getSegmentId() { return id; } inline const word* SegmentReader::getStartPtr() { return ptr.begin(); } inline SegmentWordCount SegmentReader::getOffsetTo(const word* ptr) { KJ_IREQUIRE(this->ptr.begin() <= ptr && ptr <= this->ptr.end()); return intervalLength(this->ptr.begin(), ptr, MAX_SEGMENT_WORDS); } inline SegmentWordCount SegmentReader::getSize() { return assumeBits(ptr.size()) * WORDS; } inline kj::ArrayPtr SegmentReader::getArray() { return ptr; } inline void SegmentReader::unread(WordCount64 amount) { readLimiter->unread(amount); } // ------------------------------------------------------------------- inline SegmentBuilder::SegmentBuilder( BuilderArena* arena, SegmentId id, word* ptr, SegmentWordCount size, ReadLimiter* readLimiter, SegmentWordCount wordsUsed) : SegmentReader(arena, id, ptr, size, readLimiter), pos(ptr + wordsUsed), readOnly(false) {} inline SegmentBuilder::SegmentBuilder( BuilderArena* arena, SegmentId id, const word* ptr, SegmentWordCount size, ReadLimiter* readLimiter) : SegmentReader(arena, id, ptr, size, readLimiter), // const_cast is safe here because the member won't ever be dereferenced because it appears // to point to the end of the segment anyway. pos(const_cast(ptr + size)), readOnly(true) {} inline SegmentBuilder::SegmentBuilder(BuilderArena* arena, SegmentId id, decltype(nullptr), ReadLimiter* readLimiter) : SegmentReader(arena, id, nullptr, ZERO * WORDS, readLimiter), pos(nullptr), readOnly(false) {} inline word* SegmentBuilder::allocate(SegmentWordCount amount) { if (intervalLength(pos, ptr.end(), MAX_SEGMENT_WORDS) < amount) { // Not enough space in the segment for this allocation. return nullptr; } else { // Success. word* result = pos; pos = pos + amount; return result; } } inline void SegmentBuilder::checkWritable() { if (KJ_UNLIKELY(readOnly)) throwNotWritable(); } inline word* SegmentBuilder::getPtrUnchecked(SegmentWordCount offset) { return const_cast(ptr.begin() + offset); } inline BuilderArena* SegmentBuilder::getArena() { // Down-cast safe because SegmentBuilder's constructor always initializes its SegmentReader base // class with an Arena pointer that actually points to a BuilderArena. return static_cast(arena); } inline kj::ArrayPtr SegmentBuilder::currentlyAllocated() { return kj::arrayPtr(ptr.begin(), pos - ptr.begin()); } inline void SegmentBuilder::reset() { word* start = getPtrUnchecked(ZERO * WORDS); memset(start, 0, (pos - start) * sizeof(word)); pos = start; } inline void SegmentBuilder::tryTruncate(word* from, word* to) { if (pos == from) pos = to; } inline bool SegmentBuilder::tryExtend(word* from, word* to) { // Careful about overflow. if (pos == from && to <= ptr.end() && to >= from) { pos = to; return true; } else { return false; } } } // namespace _ (private) } // namespace capnp CAPNP_END_HEADER capnproto-c++-0.8.0/src/capnp/arena.c++0000644000175000017500000003415413650101756020233 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_PRIVATE #include "arena.h" #include "message.h" #include #include #include #include #include #include #if !CAPNP_LITE #include "capability.h" #endif // !CAPNP_LITE namespace capnp { namespace _ { // private Arena::~Arena() noexcept(false) {} void ReadLimiter::unread(WordCount64 amount) { // Be careful not to overflow here. Since ReadLimiter has no thread-safety, it's possible that // the limit value was not updated correctly for one or more reads, and therefore unread() could // overflow it even if it is only unreading bytes that were actually read. uint64_t oldValue = limit; uint64_t newValue = oldValue + unbound(amount / WORDS); if (newValue > oldValue) { limit = newValue; } } void SegmentReader::abortCheckObjectFault() { KJ_LOG(FATAL, "checkObject()'s parameter is not in-range; this would segfault in opt mode", "this is a serious bug in Cap'n Proto; please notify security@sandstorm.io"); abort(); } void SegmentBuilder::throwNotWritable() { KJ_FAIL_REQUIRE( "Tried to form a Builder to an external data segment referenced by the MessageBuilder. " "When you use Orphanage::reference*(), you are not allowed to obtain Builders to the " "referenced data, only Readers, because that data is const."); } // ======================================================================================= static SegmentWordCount verifySegmentSize(size_t size) { auto gsize = bounded(size) * WORDS; return assertMaxBits(gsize, [&]() { KJ_FAIL_REQUIRE("segment is too large", size); }); } static SegmentWordCount verifySegment(kj::ArrayPtr segment) { #if !CAPNP_ALLOW_UNALIGNED KJ_REQUIRE(reinterpret_cast(segment.begin()) % sizeof(void*) == 0, "Detected unaligned data in Cap'n Proto message. Messages must be aligned to the " "architecture's word size. Yes, even on x86: Unaligned access is undefined behavior " "under the C/C++ language standard, and compilers can and do assume alignment for the " "purpose of optimizations. Unaligned access may lead to crashes or subtle corruption. " "For example, GCC will use SIMD instructions in optimizations, and those instrsuctions " "require alignment. If you really insist on taking your changes with unaligned data, " "compile the Cap'n Proto library with -DCAPNP_ALLOW_UNALIGNED to remove this check.") { break; } #endif return verifySegmentSize(segment.size()); } inline ReaderArena::ReaderArena(MessageReader* message, const word* firstSegment, SegmentWordCount firstSegmentSize) : message(message), readLimiter(bounded(message->getOptions().traversalLimitInWords) * WORDS), segment0(this, SegmentId(0), firstSegment, firstSegmentSize, &readLimiter) {} inline ReaderArena::ReaderArena(MessageReader* message, kj::ArrayPtr firstSegment) : ReaderArena(message, firstSegment.begin(), verifySegment(firstSegment)) {} ReaderArena::ReaderArena(MessageReader* message) : ReaderArena(message, message->getSegment(0)) {} ReaderArena::~ReaderArena() noexcept(false) {} size_t ReaderArena::sizeInWords() { size_t total = segment0.getArray().size(); for (uint i = 0; ; i++) { SegmentReader* segment = tryGetSegment(SegmentId(i)); if (segment == nullptr) return total; total += unboundAs(segment->getSize() / WORDS); } } SegmentReader* ReaderArena::tryGetSegment(SegmentId id) { if (id == SegmentId(0)) { if (segment0.getArray() == nullptr) { return nullptr; } else { return &segment0; } } auto lock = moreSegments.lockExclusive(); SegmentMap* segments = nullptr; KJ_IF_MAYBE(s, *lock) { KJ_IF_MAYBE(segment, s->find(id.value)) { return *segment; } segments = s; } kj::ArrayPtr newSegment = message->getSegment(id.value); if (newSegment == nullptr) { return nullptr; } SegmentWordCount newSegmentSize = verifySegment(newSegment); if (*lock == nullptr) { // OK, the segment exists, so allocate the map. segments = &lock->emplace(); } auto segment = kj::heap( this, id, newSegment.begin(), newSegmentSize, &readLimiter); SegmentReader* result = segment; segments->insert(id.value, kj::mv(segment)); return result; } void ReaderArena::reportReadLimitReached() { KJ_FAIL_REQUIRE("Exceeded message traversal limit. See capnp::ReaderOptions.") { return; } } // ======================================================================================= BuilderArena::BuilderArena(MessageBuilder* message) : message(message), segment0(nullptr, SegmentId(0), nullptr, nullptr) {} BuilderArena::BuilderArena(MessageBuilder* message, kj::ArrayPtr segments) : message(message), segment0(this, SegmentId(0), segments[0].space.begin(), verifySegment(segments[0].space), &this->dummyLimiter, verifySegmentSize(segments[0].wordsUsed)) { if (segments.size() > 1) { kj::Vector> builders(segments.size() - 1); uint i = 1; for (auto& segment: segments.slice(1, segments.size())) { builders.add(kj::heap( this, SegmentId(i++), segment.space.begin(), verifySegment(segment.space), &this->dummyLimiter, verifySegmentSize(segment.wordsUsed))); } kj::Vector> forOutput; forOutput.resize(segments.size()); segmentWithSpace = builders.back(); this->moreSegments = kj::heap( MultiSegmentState { kj::mv(builders), kj::mv(forOutput) }); } else { segmentWithSpace = &segment0; } } BuilderArena::~BuilderArena() noexcept(false) {} size_t BuilderArena::sizeInWords() { KJ_IF_MAYBE(segmentState, moreSegments) { size_t total = segment0.currentlyAllocated().size(); for (auto& builder: segmentState->get()->builders) { total += builder->currentlyAllocated().size(); } return total; } else { if (segment0.getArena() == nullptr) { // We haven't actually allocated any segments yet. return 0; } else { // We have only one segment so far. return segment0.currentlyAllocated().size(); } } } SegmentBuilder* BuilderArena::getSegment(SegmentId id) { // This method is allowed to fail if the segment ID is not valid. if (id == SegmentId(0)) { return &segment0; } else { KJ_IF_MAYBE(s, moreSegments) { KJ_REQUIRE(id.value - 1 < s->get()->builders.size(), "invalid segment id", id.value); return const_cast(s->get()->builders[id.value - 1].get()); } else { KJ_FAIL_REQUIRE("invalid segment id", id.value); } } } BuilderArena::AllocateResult BuilderArena::allocate(SegmentWordCount amount) { if (segment0.getArena() == nullptr) { // We're allocating the first segment. kj::ArrayPtr ptr = message->allocateSegment(unbound(amount / WORDS)); auto actualSize = verifySegment(ptr); // Re-allocate segment0 in-place. This is a bit of a hack, but we have not returned any // pointers to this segment yet, so it should be fine. kj::dtor(segment0); kj::ctor(segment0, this, SegmentId(0), ptr.begin(), actualSize, &this->dummyLimiter); segmentWithSpace = &segment0; return AllocateResult { &segment0, segment0.allocate(amount) }; } else { if (segmentWithSpace != nullptr) { // Check if there is space in an existing segment. // TODO(perf): Check for available space in more than just the last segment. We don't // want this to be O(n), though, so we'll need to maintain some sort of table. Complicating // matters, we want SegmentBuilders::allocate() to be fast, so we can't update any such // table when allocation actually happens. Instead, we could have a priority queue based // on the last-known available size, and then re-check the size when we pop segments off it // and shove them to the back of the queue if they have become too small. word* attempt = segmentWithSpace->allocate(amount); if (attempt != nullptr) { return AllocateResult { segmentWithSpace, attempt }; } } // Need to allocate a new segment. SegmentBuilder* result = addSegmentInternal(message->allocateSegment(unbound(amount / WORDS))); // Check this new segment first the next time we need to allocate. segmentWithSpace = result; // Allocating from the new segment is guaranteed to succeed since we made it big enough. return AllocateResult { result, result->allocate(amount) }; } } SegmentBuilder* BuilderArena::addExternalSegment(kj::ArrayPtr content) { return addSegmentInternal(content); } template SegmentBuilder* BuilderArena::addSegmentInternal(kj::ArrayPtr content) { // This check should never fail in practice, since you can't get an Orphanage without allocating // the root segment. KJ_REQUIRE(segment0.getArena() != nullptr, "Can't allocate external segments before allocating the root segment."); auto contentSize = verifySegmentSize(content.size()); MultiSegmentState* segmentState; KJ_IF_MAYBE(s, moreSegments) { segmentState = *s; } else { auto newSegmentState = kj::heap(); segmentState = newSegmentState; moreSegments = kj::mv(newSegmentState); } kj::Own newBuilder = kj::heap( this, SegmentId(segmentState->builders.size() + 1), content.begin(), contentSize, &this->dummyLimiter); SegmentBuilder* result = newBuilder.get(); segmentState->builders.add(kj::mv(newBuilder)); // Keep forOutput the right size so that we don't have to re-allocate during // getSegmentsForOutput(), which callers might reasonably expect is a thread-safe method. segmentState->forOutput.resize(segmentState->builders.size() + 1); return result; } kj::ArrayPtr> BuilderArena::getSegmentsForOutput() { // Although this is a read-only method, we shouldn't need to lock a mutex here because if this // is called multiple times simultaneously, we should only be overwriting the array with the // exact same data. If the number or size of segments is actually changing due to an activity // in another thread, then the caller has a problem regardless of locking here. KJ_IF_MAYBE(segmentState, moreSegments) { KJ_DASSERT(segmentState->get()->forOutput.size() == segmentState->get()->builders.size() + 1, "segmentState->forOutput wasn't resized correctly when the last builder was added.", segmentState->get()->forOutput.size(), segmentState->get()->builders.size()); kj::ArrayPtr> result( &segmentState->get()->forOutput[0], segmentState->get()->forOutput.size()); uint i = 0; result[i++] = segment0.currentlyAllocated(); for (auto& builder: segmentState->get()->builders) { result[i++] = builder->currentlyAllocated(); } return result; } else { if (segment0.getArena() == nullptr) { // We haven't actually allocated any segments yet. return nullptr; } else { // We have only one segment so far. segment0ForOutput = segment0.currentlyAllocated(); return kj::arrayPtr(&segment0ForOutput, 1); } } } SegmentReader* BuilderArena::tryGetSegment(SegmentId id) { if (id == SegmentId(0)) { if (segment0.getArena() == nullptr) { // We haven't allocated any segments yet. return nullptr; } else { return &segment0; } } else { KJ_IF_MAYBE(segmentState, moreSegments) { if (id.value <= segmentState->get()->builders.size()) { // TODO(cleanup): Return a const SegmentReader and tediously constify all SegmentBuilder // pointers throughout the codebase. return const_cast(kj::implicitCast( segmentState->get()->builders[id.value - 1].get())); } } return nullptr; } } void BuilderArena::reportReadLimitReached() { KJ_FAIL_ASSERT("Read limit reached for BuilderArena, but it should have been unlimited.") { return; } } kj::Maybe> BuilderArena::LocalCapTable::extractCap(uint index) { #if CAPNP_LITE KJ_UNIMPLEMENTED("no cap tables in lite mode"); #else if (index < capTable.size()) { return capTable[index].map([](kj::Own& cap) { return cap->addRef(); }); } else { return nullptr; } #endif } uint BuilderArena::LocalCapTable::injectCap(kj::Own&& cap) { #if CAPNP_LITE KJ_UNIMPLEMENTED("no cap tables in lite mode"); #else uint result = capTable.size(); capTable.add(kj::mv(cap)); return result; #endif } void BuilderArena::LocalCapTable::dropCap(uint index) { #if CAPNP_LITE KJ_UNIMPLEMENTED("no cap tables in lite mode"); #else KJ_ASSERT(index < capTable.size(), "Invalid capability descriptor in message.") { return; } capTable[index] = nullptr; #endif } } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/layout.c++0000644000175000017500000045315613650101756020471 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_PRIVATE #include "layout.h" #include #include "arena.h" #include #include #if !CAPNP_LITE #include "capability.h" #endif // !CAPNP_LITE namespace capnp { namespace _ { // private #if !CAPNP_LITE static BrokenCapFactory* brokenCapFactory = nullptr; // Horrible hack: We need to be able to construct broken caps without any capability context, // but we can't have a link-time dependency on libcapnp-rpc. void setGlobalBrokenCapFactoryForLayoutCpp(BrokenCapFactory& factory) { // Called from capability.c++ when the capability API is used, to make sure that layout.c++ // is ready for it. May be called multiple times but always with the same value. #if __GNUC__ __atomic_store_n(&brokenCapFactory, &factory, __ATOMIC_RELAXED); #elif _MSC_VER *static_cast(&brokenCapFactory) = &factory; #else #error "Platform not supported" #endif } } // namespace _ (private) const uint ClientHook::NULL_CAPABILITY_BRAND = 0; const uint ClientHook::BROKEN_CAPABILITY_BRAND = 0; // Defined here rather than capability.c++ so that we can safely call isNull() in this file. namespace _ { // private #endif // !CAPNP_LITE #if CAPNP_DEBUG_TYPES #define G(n) bounded() #else #define G(n) n #endif // ======================================================================================= #if __GNUC__ >= 8 && !__clang__ // GCC 8 introduced a warning which complains whenever we try to memset() or memcpy() a // WirePointer, because we deleted the regular copy constructor / assignment operator. Weirdly, if // I remove those deletions, GCC *still* complains that WirePointer is non-trivial. I don't // understand why -- maybe because WireValue has private members? We don't want to make WireValue's // member public, but memset() and memcpy() on it are certainly valid and desirable, so we'll just // have to disable the warning I guess. #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif struct WirePointer { // A pointer, in exactly the format in which it appears on the wire. // Copying and moving is not allowed because the offset would become wrong. WirePointer(const WirePointer& other) = delete; WirePointer(WirePointer&& other) = delete; WirePointer& operator=(const WirePointer& other) = delete; WirePointer& operator=(WirePointer&& other) = delete; // ----------------------------------------------------------------- // Common part of all pointers: kind + offset // // Actually this is not terribly common. The "offset" could actually be different things // depending on the context: // - For a regular (e.g. struct/list) pointer, a signed word offset from the word immediately // following the pointer pointer. (The off-by-one means the offset is more often zero, saving // bytes on the wire when packed.) // - For an inline composite list tag (not really a pointer, but structured similarly), an // element count. // - For a FAR pointer, an unsigned offset into the target segment. // - For a FAR landing pad, zero indicates that the target value immediately follows the pad while // 1 indicates that the pad is followed by another FAR pointer that actually points at the // value. enum Kind { STRUCT = 0, // Reference points at / describes a struct. LIST = 1, // Reference points at / describes a list. FAR = 2, // Reference is a "far pointer", which points at data located in a different segment. The // eventual target is one of the other kinds. OTHER = 3 // Reference has type "other". If the next 30 bits are all zero (i.e. the lower 32 bits contain // only the kind OTHER) then the pointer is a capability. All other values are reserved. }; WireValue offsetAndKind; KJ_ALWAYS_INLINE(Kind kind() const) { return static_cast(offsetAndKind.get() & 3); } KJ_ALWAYS_INLINE(bool isPositional() const) { return (offsetAndKind.get() & 2) == 0; // match STRUCT and LIST but not FAR or OTHER } KJ_ALWAYS_INLINE(bool isCapability() const) { return offsetAndKind.get() == OTHER; } KJ_ALWAYS_INLINE(word* target()) { return reinterpret_cast(this) + 1 + (static_cast(offsetAndKind.get()) >> 2); } KJ_ALWAYS_INLINE(const word* target(SegmentReader* segment) const) { if (segment == nullptr) { return reinterpret_cast(this + 1) + (static_cast(offsetAndKind.get()) >> 2); } else { return segment->checkOffset(reinterpret_cast(this + 1), static_cast(offsetAndKind.get()) >> 2); } } KJ_ALWAYS_INLINE(void setKindAndTarget(Kind kind, word* target, SegmentBuilder* segment)) { // Check that the target is really in the same segment, otherwise subtracting pointers is // undefined behavior. As it turns out, it's undefined behavior that actually produces // unexpected results in a real-world situation that actually happened: At one time, // OrphanBuilder's "tag" (a WirePointer) was allowed to be initialized as if it lived in // a particular segment when in fact it does not. On 32-bit systems, where words might // only be 32-bit aligned, it's possible that the difference between `this` and `target` is // not a whole number of words. But clang optimizes: // (target - (word*)this - 1) << 2 // to: // (((ptrdiff_t)target - (ptrdiff_t)this - 8) >> 1) // So now when the pointers are not aligned the same, we can end up corrupting the bottom // two bits, where `kind` is stored. For example, this turns a struct into a far pointer. // Ouch! KJ_DREQUIRE(reinterpret_cast(this) >= reinterpret_cast(segment->getStartPtr())); KJ_DREQUIRE(reinterpret_cast(this) < reinterpret_cast(segment->getStartPtr() + segment->getSize())); KJ_DREQUIRE(reinterpret_cast(target) >= reinterpret_cast(segment->getStartPtr())); KJ_DREQUIRE(reinterpret_cast(target) <= reinterpret_cast(segment->getStartPtr() + segment->getSize())); offsetAndKind.set((static_cast(target - reinterpret_cast(this) - 1) << 2) | kind); } KJ_ALWAYS_INLINE(void setKindWithZeroOffset(Kind kind)) { offsetAndKind.set(kind); } KJ_ALWAYS_INLINE(void setKindAndTargetForEmptyStruct()) { // This pointer points at an empty struct. Assuming the WirePointer itself is in-bounds, we // can set the target to point either at the WirePointer itself or immediately after it. The // latter would cause the WirePointer to be "null" (since for an empty struct the upper 32 // bits are going to be zero). So we set an offset of -1, as if the struct were allocated // immediately before this pointer, to distinguish it from null. offsetAndKind.set(0xfffffffc); } KJ_ALWAYS_INLINE(void setKindForOrphan(Kind kind)) { // OrphanBuilder contains a WirePointer, but since it isn't located in a segment, it should // not have a valid offset (unless it is a FAR or OTHER pointer). We set its offset to -1 // because setting it to zero would mean a pointer to an empty struct would appear to be a null // pointer. KJ_DREQUIRE(isPositional()); offsetAndKind.set(kind | 0xfffffffc); } KJ_ALWAYS_INLINE(ListElementCount inlineCompositeListElementCount() const) { return ((bounded(offsetAndKind.get()) >> G(2)) & G(kj::maxValueForBits())) * ELEMENTS; } KJ_ALWAYS_INLINE(void setKindAndInlineCompositeListElementCount( Kind kind, ListElementCount elementCount)) { offsetAndKind.set(unboundAs((elementCount / ELEMENTS) << G(2)) | kind); } KJ_ALWAYS_INLINE(const word* farTarget(SegmentReader* segment) const) { KJ_DREQUIRE(kind() == FAR, "farTarget() should only be called on FAR pointers."); return segment->checkOffset(segment->getStartPtr(), offsetAndKind.get() >> 3); } KJ_ALWAYS_INLINE(word* farTarget(SegmentBuilder* segment) const) { KJ_DREQUIRE(kind() == FAR, "farTarget() should only be called on FAR pointers."); return segment->getPtrUnchecked((bounded(offsetAndKind.get()) >> G(3)) * WORDS); } KJ_ALWAYS_INLINE(bool isDoubleFar() const) { KJ_DREQUIRE(kind() == FAR, "isDoubleFar() should only be called on FAR pointers."); return (offsetAndKind.get() >> 2) & 1; } KJ_ALWAYS_INLINE(void setFar(bool isDoubleFar, WordCountN<29> pos)) { offsetAndKind.set(unboundAs((pos / WORDS) << G(3)) | (static_cast(isDoubleFar) << 2) | static_cast(Kind::FAR)); } KJ_ALWAYS_INLINE(void setCap(uint index)) { offsetAndKind.set(static_cast(Kind::OTHER)); capRef.index.set(index); } // ----------------------------------------------------------------- // Part of pointer that depends on the kind. // Note: Originally StructRef, ListRef, and FarRef were unnamed types, but this somehow // tickled a bug in GCC: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58192 struct StructRef { WireValue dataSize; WireValue ptrCount; inline WordCountN<17> wordSize() const { return upgradeBound(dataSize.get()) + ptrCount.get() * WORDS_PER_POINTER; } KJ_ALWAYS_INLINE(void set(WordCount16 ds, WirePointerCount16 rc)) { dataSize.set(ds); ptrCount.set(rc); } KJ_ALWAYS_INLINE(void set(StructSize size)) { dataSize.set(size.data); ptrCount.set(size.pointers); } }; struct ListRef { WireValue elementSizeAndCount; KJ_ALWAYS_INLINE(ElementSize elementSize() const) { return static_cast(elementSizeAndCount.get() & 7); } KJ_ALWAYS_INLINE(ElementCountN<29> elementCount() const) { return (bounded(elementSizeAndCount.get()) >> G(3)) * ELEMENTS; } KJ_ALWAYS_INLINE(WordCountN<29> inlineCompositeWordCount() const) { return elementCount() * (ONE * WORDS / ELEMENTS); } KJ_ALWAYS_INLINE(void set(ElementSize es, ElementCountN<29> ec)) { elementSizeAndCount.set(unboundAs((ec / ELEMENTS) << G(3)) | static_cast(es)); } KJ_ALWAYS_INLINE(void setInlineComposite(WordCountN<29> wc)) { elementSizeAndCount.set(unboundAs((wc / WORDS) << G(3)) | static_cast(ElementSize::INLINE_COMPOSITE)); } }; struct FarRef { WireValue segmentId; KJ_ALWAYS_INLINE(void set(SegmentId si)) { segmentId.set(si); } }; struct CapRef { WireValue index; // Index into the message's capability table. }; union { uint32_t upper32Bits; StructRef structRef; ListRef listRef; FarRef farRef; CapRef capRef; }; KJ_ALWAYS_INLINE(bool isNull() const) { // If the upper 32 bits are zero, this is a pointer to an empty struct. We consider that to be // our "null" value. return (offsetAndKind.get() == 0) & (upper32Bits == 0); } }; static_assert(sizeof(WirePointer) == sizeof(word), "capnp::WirePointer is not exactly one word. This will probably break everything."); static_assert(unboundAs(POINTERS * WORDS_PER_POINTER * BYTES_PER_WORD / BYTES) == sizeof(WirePointer), "WORDS_PER_POINTER is wrong."); static_assert(unboundAs(POINTERS * BYTES_PER_POINTER / BYTES) == sizeof(WirePointer), "BYTES_PER_POINTER is wrong."); static_assert(unboundAs(POINTERS * BITS_PER_POINTER / BITS_PER_BYTE / BYTES) == sizeof(WirePointer), "BITS_PER_POINTER is wrong."); namespace { static const union { AlignedData word; WirePointer pointer; } zero = {{{0}}}; } // namespace // ======================================================================================= namespace { template struct SegmentAnd { SegmentBuilder* segment; T value; }; } // namespace struct WireHelpers { #if CAPNP_DEBUG_TYPES template static KJ_ALWAYS_INLINE( kj::Quantity, word> roundBytesUpToWords( kj::Quantity, byte> bytes)) { static_assert(sizeof(word) == 8, "This code assumes 64-bit words."); return (bytes + G(7) * BYTES) / BYTES_PER_WORD; } template static KJ_ALWAYS_INLINE( kj::Quantity, byte> roundBitsUpToBytes( kj::Quantity, BitLabel> bits)) { return (bits + G(7) * BITS) / BITS_PER_BYTE; } template static KJ_ALWAYS_INLINE( kj::Quantity, word> roundBitsUpToWords( kj::Quantity, BitLabel> bits)) { static_assert(sizeof(word) == 8, "This code assumes 64-bit words."); return (bits + G(63) * BITS) / BITS_PER_WORD; } #else static KJ_ALWAYS_INLINE(WordCount roundBytesUpToWords(ByteCount bytes)) { static_assert(sizeof(word) == 8, "This code assumes 64-bit words."); return (bytes + G(7) * BYTES) / BYTES_PER_WORD; } static KJ_ALWAYS_INLINE(ByteCount roundBitsUpToBytes(BitCount bits)) { return (bits + G(7) * BITS) / BITS_PER_BYTE; } static KJ_ALWAYS_INLINE(WordCount64 roundBitsUpToWords(BitCount64 bits)) { static_assert(sizeof(word) == 8, "This code assumes 64-bit words."); return (bits + G(63) * BITS) / BITS_PER_WORD; } static KJ_ALWAYS_INLINE(ByteCount64 roundBitsUpToBytes(BitCount64 bits)) { return (bits + G(7) * BITS) / BITS_PER_BYTE; } #endif static KJ_ALWAYS_INLINE(void zeroMemory(byte* ptr, ByteCount32 count)) { if (count != ZERO * BYTES) memset(ptr, 0, unbound(count / BYTES)); } static KJ_ALWAYS_INLINE(void zeroMemory(word* ptr, WordCountN<29> count)) { if (count != ZERO * WORDS) memset(ptr, 0, unbound(count * BYTES_PER_WORD / BYTES)); } static KJ_ALWAYS_INLINE(void zeroMemory(WirePointer* ptr, WirePointerCountN<29> count)) { if (count != ZERO * POINTERS) memset(ptr, 0, unbound(count * BYTES_PER_POINTER / BYTES)); } static KJ_ALWAYS_INLINE(void zeroMemory(WirePointer* ptr)) { memset(ptr, 0, sizeof(*ptr)); } template static inline void zeroMemory(kj::ArrayPtr array) { if (array.size() != 0u) memset(array.begin(), 0, array.size() * sizeof(array[0])); } static KJ_ALWAYS_INLINE(void copyMemory(byte* to, const byte* from, ByteCount32 count)) { if (count != ZERO * BYTES) memcpy(to, from, unbound(count / BYTES)); } static KJ_ALWAYS_INLINE(void copyMemory(word* to, const word* from, WordCountN<29> count)) { if (count != ZERO * WORDS) memcpy(to, from, unbound(count * BYTES_PER_WORD / BYTES)); } static KJ_ALWAYS_INLINE(void copyMemory(WirePointer* to, const WirePointer* from, WirePointerCountN<29> count)) { if (count != ZERO * POINTERS) memcpy(to, from, unbound(count * BYTES_PER_POINTER / BYTES)); } template static inline void copyMemory(T* to, const T* from) { memcpy(to, from, sizeof(*from)); } // TODO(cleanup): Turn these into a .copyTo() method of ArrayPtr? template static inline void copyMemory(T* to, kj::ArrayPtr from) { if (from.size() != 0u) memcpy(to, from.begin(), from.size() * sizeof(from[0])); } template static inline void copyMemory(T* to, kj::ArrayPtr from) { if (from.size() != 0u) memcpy(to, from.begin(), from.size() * sizeof(from[0])); } static KJ_ALWAYS_INLINE(void copyMemory(char* to, kj::StringPtr from)) { if (from.size() != 0u) memcpy(to, from.begin(), from.size() * sizeof(from[0])); } static KJ_ALWAYS_INLINE(bool boundsCheck( SegmentReader* segment, const word* start, WordCountN<31> size)) { // If segment is null, this is an unchecked message, so we don't do bounds checks. return segment == nullptr || segment->checkObject(start, size); } static KJ_ALWAYS_INLINE(bool amplifiedRead(SegmentReader* segment, WordCount virtualAmount)) { // If segment is null, this is an unchecked message, so we don't do read limiter checks. return segment == nullptr || segment->amplifiedRead(virtualAmount); } static KJ_ALWAYS_INLINE(word* allocate( WirePointer*& ref, SegmentBuilder*& segment, CapTableBuilder* capTable, SegmentWordCount amount, WirePointer::Kind kind, BuilderArena* orphanArena)) { // Allocate space in the message for a new object, creating far pointers if necessary. // // * `ref` starts out being a reference to the pointer which shall be assigned to point at the // new object. On return, `ref` points to a pointer which needs to be initialized with // the object's type information. Normally this is the same pointer, but it can change if // a far pointer was allocated -- in this case, `ref` will end up pointing to the far // pointer's tag. Either way, `allocate()` takes care of making sure that the original // pointer ends up leading to the new object. On return, only the upper 32 bit of `*ref` // need to be filled in by the caller. // * `segment` starts out pointing to the segment containing `ref`. On return, it points to // the segment containing the allocated object, which is usually the same segment but could // be a different one if the original segment was out of space. // * `amount` is the number of words to allocate. // * `kind` is the kind of object to allocate. It is used to initialize the pointer. It // cannot be `FAR` -- far pointers are allocated automatically as needed. // * `orphanArena` is usually null. If it is non-null, then we're allocating an orphan object. // In this case, `segment` starts out null; the allocation takes place in an arbitrary // segment belonging to the arena. `ref` will be initialized as a non-far pointer, but its // target offset will be set to zero. if (orphanArena == nullptr) { if (!ref->isNull()) zeroObject(segment, capTable, ref); if (amount == ZERO * WORDS && kind == WirePointer::STRUCT) { // Note that the check for kind == WirePointer::STRUCT will hopefully cause this whole // branch to be optimized away from all the call sites that are allocating non-structs. ref->setKindAndTargetForEmptyStruct(); return reinterpret_cast(ref); } word* ptr = segment->allocate(amount); if (ptr == nullptr) { // Need to allocate in a new segment. We'll need to allocate an extra pointer worth of // space to act as the landing pad for a far pointer. WordCount amountPlusRef = amount + POINTER_SIZE_IN_WORDS; auto allocation = segment->getArena()->allocate( assertMaxBits(amountPlusRef, []() { KJ_FAIL_REQUIRE("requested object size exceeds maximum segment size"); })); segment = allocation.segment; ptr = allocation.words; // Set up the original pointer to be a far pointer to the new segment. ref->setFar(false, segment->getOffsetTo(ptr)); ref->farRef.set(segment->getSegmentId()); // Initialize the landing pad to indicate that the data immediately follows the pad. ref = reinterpret_cast(ptr); ref->setKindAndTarget(kind, ptr + POINTER_SIZE_IN_WORDS, segment); // Allocated space follows new pointer. return ptr + POINTER_SIZE_IN_WORDS; } else { ref->setKindAndTarget(kind, ptr, segment); return ptr; } } else { // orphanArena is non-null. Allocate an orphan. KJ_DASSERT(ref->isNull()); auto allocation = orphanArena->allocate(amount); segment = allocation.segment; ref->setKindForOrphan(kind); return allocation.words; } } static KJ_ALWAYS_INLINE(word* followFarsNoWritableCheck( WirePointer*& ref, word* refTarget, SegmentBuilder*& segment)) { // If `ref` is a far pointer, follow it. On return, `ref` will have been updated to point at // a WirePointer that contains the type information about the target object, and a pointer to // the object contents is returned. The caller must NOT use `ref->target()` as this may or may // not actually return a valid pointer. `segment` is also updated to point at the segment which // actually contains the object. // // If `ref` is not a far pointer, this simply returns `refTarget`. Usually, `refTarget` should // be the same as `ref->target()`, but may not be in cases where `ref` is only a tag. if (ref->kind() == WirePointer::FAR) { segment = segment->getArena()->getSegment(ref->farRef.segmentId.get()); WirePointer* pad = reinterpret_cast(ref->farTarget(segment)); if (!ref->isDoubleFar()) { ref = pad; return pad->target(); } // Landing pad is another far pointer. It is followed by a tag describing the pointed-to // object. ref = pad + 1; segment = segment->getArena()->getSegment(pad->farRef.segmentId.get()); return pad->farTarget(segment); } else { return refTarget; } } static KJ_ALWAYS_INLINE(word* followFars( WirePointer*& ref, word* refTarget, SegmentBuilder*& segment)) { auto result = followFarsNoWritableCheck(ref, refTarget, segment); segment->checkWritable(); return result; } static KJ_ALWAYS_INLINE(kj::Maybe followFars( const WirePointer*& ref, const word* refTarget, SegmentReader*& segment)) KJ_WARN_UNUSED_RESULT { // Like the other followFars() but operates on readers. // If the segment is null, this is an unchecked message, so there are no FAR pointers. if (segment != nullptr && ref->kind() == WirePointer::FAR) { // Look up the segment containing the landing pad. segment = segment->getArena()->tryGetSegment(ref->farRef.segmentId.get()); KJ_REQUIRE(segment != nullptr, "Message contains far pointer to unknown segment.") { return nullptr; } // Find the landing pad and check that it is within bounds. const word* ptr = ref->farTarget(segment); auto padWords = (ONE + bounded(ref->isDoubleFar())) * POINTER_SIZE_IN_WORDS; KJ_REQUIRE(boundsCheck(segment, ptr, padWords), "Message contains out-of-bounds far pointer.") { return nullptr; } const WirePointer* pad = reinterpret_cast(ptr); // If this is not a double-far then the landing pad is our final pointer. if (!ref->isDoubleFar()) { ref = pad; return pad->target(segment); } // Landing pad is another far pointer. It is followed by a tag describing the pointed-to // object. ref = pad + 1; SegmentReader* newSegment = segment->getArena()->tryGetSegment(pad->farRef.segmentId.get()); KJ_REQUIRE(newSegment != nullptr, "Message contains double-far pointer to unknown segment.") { return nullptr; } KJ_REQUIRE(pad->kind() == WirePointer::FAR, "Second word of double-far pad must be far pointer.") { return nullptr; } segment = newSegment; return pad->farTarget(segment); } else { KJ_DASSERT(refTarget != nullptr); return refTarget; } } // ----------------------------------------------------------------- static void zeroObject(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* ref) { // Zero out the pointed-to object. Use when the pointer is about to be overwritten making the // target object no longer reachable. // We shouldn't zero out external data linked into the message. if (!segment->isWritable()) return; switch (ref->kind()) { case WirePointer::STRUCT: case WirePointer::LIST: zeroObject(segment, capTable, ref, ref->target()); break; case WirePointer::FAR: { segment = segment->getArena()->getSegment(ref->farRef.segmentId.get()); if (segment->isWritable()) { // Don't zero external data. WirePointer* pad = reinterpret_cast(ref->farTarget(segment)); if (ref->isDoubleFar()) { segment = segment->getArena()->getSegment(pad->farRef.segmentId.get()); if (segment->isWritable()) { zeroObject(segment, capTable, pad + 1, pad->farTarget(segment)); } zeroMemory(pad, G(2) * POINTERS); } else { zeroObject(segment, capTable, pad); zeroMemory(pad); } } break; } case WirePointer::OTHER: if (ref->isCapability()) { #if CAPNP_LITE KJ_FAIL_ASSERT("Capability encountered in builder in lite mode?") { break; } #else // CAPNP_LINE capTable->dropCap(ref->capRef.index.get()); #endif // CAPNP_LITE, else } else { KJ_FAIL_REQUIRE("Unknown pointer type.") { break; } } break; } } static void zeroObject(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* tag, word* ptr) { // We shouldn't zero out external data linked into the message. if (!segment->isWritable()) return; switch (tag->kind()) { case WirePointer::STRUCT: { WirePointer* pointerSection = reinterpret_cast(ptr + tag->structRef.dataSize.get()); for (auto i: kj::zeroTo(tag->structRef.ptrCount.get())) { zeroObject(segment, capTable, pointerSection + i); } zeroMemory(ptr, tag->structRef.wordSize()); break; } case WirePointer::LIST: { switch (tag->listRef.elementSize()) { case ElementSize::VOID: // Nothing. break; case ElementSize::BIT: case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: { zeroMemory(ptr, roundBitsUpToWords( upgradeBound(tag->listRef.elementCount()) * dataBitsPerElement(tag->listRef.elementSize()))); break; } case ElementSize::POINTER: { WirePointer* typedPtr = reinterpret_cast(ptr); auto count = tag->listRef.elementCount() * (ONE * POINTERS / ELEMENTS); for (auto i: kj::zeroTo(count)) { zeroObject(segment, capTable, typedPtr + i); } zeroMemory(typedPtr, count); break; } case ElementSize::INLINE_COMPOSITE: { WirePointer* elementTag = reinterpret_cast(ptr); KJ_ASSERT(elementTag->kind() == WirePointer::STRUCT, "Don't know how to handle non-STRUCT inline composite."); WordCount dataSize = elementTag->structRef.dataSize.get(); WirePointerCount pointerCount = elementTag->structRef.ptrCount.get(); auto count = elementTag->inlineCompositeListElementCount(); if (pointerCount > ZERO * POINTERS) { word* pos = ptr + POINTER_SIZE_IN_WORDS; for (auto i KJ_UNUSED: kj::zeroTo(count)) { pos += dataSize; for (auto j KJ_UNUSED: kj::zeroTo(pointerCount)) { zeroObject(segment, capTable, reinterpret_cast(pos)); pos += POINTER_SIZE_IN_WORDS; } } } auto wordsPerElement = elementTag->structRef.wordSize() / ELEMENTS; zeroMemory(ptr, assertMaxBits(POINTER_SIZE_IN_WORDS + upgradeBound(count) * wordsPerElement, []() { KJ_FAIL_ASSERT("encountered list pointer in builder which is too large to " "possibly fit in a segment. Bug in builder code?"); })); break; } } break; } case WirePointer::FAR: KJ_FAIL_ASSERT("Unexpected FAR pointer.") { break; } break; case WirePointer::OTHER: KJ_FAIL_ASSERT("Unexpected OTHER pointer.") { break; } break; } } static KJ_ALWAYS_INLINE( void zeroPointerAndFars(SegmentBuilder* segment, WirePointer* ref)) { // Zero out the pointer itself and, if it is a far pointer, zero the landing pad as well, but // do not zero the object body. Used when upgrading. if (ref->kind() == WirePointer::FAR) { SegmentBuilder* padSegment = segment->getArena()->getSegment(ref->farRef.segmentId.get()); if (padSegment->isWritable()) { // Don't zero external data. WirePointer* pad = reinterpret_cast(ref->farTarget(padSegment)); if (ref->isDoubleFar()) { zeroMemory(pad, G(2) * POINTERS); } else { zeroMemory(pad); } } } zeroMemory(ref); } // ----------------------------------------------------------------- static MessageSizeCounts totalSize( SegmentReader* segment, const WirePointer* ref, int nestingLimit) { // Compute the total size of the object pointed to, not counting far pointer overhead. MessageSizeCounts result = { ZERO * WORDS, 0 }; if (ref->isNull()) { return result; } KJ_REQUIRE(nestingLimit > 0, "Message is too deeply-nested.") { return result; } --nestingLimit; const word* ptr; KJ_IF_MAYBE(p, followFars(ref, ref->target(segment), segment)) { ptr = p; } else { return result; } switch (ref->kind()) { case WirePointer::STRUCT: { KJ_REQUIRE(boundsCheck(segment, ptr, ref->structRef.wordSize()), "Message contained out-of-bounds struct pointer.") { return result; } result.addWords(ref->structRef.wordSize()); const WirePointer* pointerSection = reinterpret_cast(ptr + ref->structRef.dataSize.get()); for (auto i: kj::zeroTo(ref->structRef.ptrCount.get())) { result += totalSize(segment, pointerSection + i, nestingLimit); } break; } case WirePointer::LIST: { switch (ref->listRef.elementSize()) { case ElementSize::VOID: // Nothing. break; case ElementSize::BIT: case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: { auto totalWords = roundBitsUpToWords( upgradeBound(ref->listRef.elementCount()) * dataBitsPerElement(ref->listRef.elementSize())); KJ_REQUIRE(boundsCheck(segment, ptr, totalWords), "Message contained out-of-bounds list pointer.") { return result; } result.addWords(totalWords); break; } case ElementSize::POINTER: { auto count = ref->listRef.elementCount() * (POINTERS / ELEMENTS); KJ_REQUIRE(boundsCheck(segment, ptr, count * WORDS_PER_POINTER), "Message contained out-of-bounds list pointer.") { return result; } result.addWords(count * WORDS_PER_POINTER); for (auto i: kj::zeroTo(count)) { result += totalSize(segment, reinterpret_cast(ptr) + i, nestingLimit); } break; } case ElementSize::INLINE_COMPOSITE: { auto wordCount = ref->listRef.inlineCompositeWordCount(); KJ_REQUIRE(boundsCheck(segment, ptr, wordCount + POINTER_SIZE_IN_WORDS), "Message contained out-of-bounds list pointer.") { return result; } const WirePointer* elementTag = reinterpret_cast(ptr); auto count = elementTag->inlineCompositeListElementCount(); KJ_REQUIRE(elementTag->kind() == WirePointer::STRUCT, "Don't know how to handle non-STRUCT inline composite.") { return result; } auto actualSize = elementTag->structRef.wordSize() / ELEMENTS * upgradeBound(count); KJ_REQUIRE(actualSize <= wordCount, "Struct list pointer's elements overran size.") { return result; } // We count the actual size rather than the claimed word count because that's what // we'll end up with if we make a copy. result.addWords(actualSize + POINTER_SIZE_IN_WORDS); WordCount dataSize = elementTag->structRef.dataSize.get(); WirePointerCount pointerCount = elementTag->structRef.ptrCount.get(); if (pointerCount > ZERO * POINTERS) { const word* pos = ptr + POINTER_SIZE_IN_WORDS; for (auto i KJ_UNUSED: kj::zeroTo(count)) { pos += dataSize; for (auto j KJ_UNUSED: kj::zeroTo(pointerCount)) { result += totalSize(segment, reinterpret_cast(pos), nestingLimit); pos += POINTER_SIZE_IN_WORDS; } } } break; } } break; } case WirePointer::FAR: KJ_FAIL_REQUIRE("Unexpected FAR pointer.") { break; } break; case WirePointer::OTHER: if (ref->isCapability()) { result.capCount++; } else { KJ_FAIL_REQUIRE("Unknown pointer type.") { break; } } break; } return result; } // ----------------------------------------------------------------- // Copy from an unchecked message. static KJ_ALWAYS_INLINE( void copyStruct(SegmentBuilder* segment, CapTableBuilder* capTable, word* dst, const word* src, StructDataWordCount dataSize, StructPointerCount pointerCount)) { copyMemory(dst, src, dataSize); const WirePointer* srcRefs = reinterpret_cast(src + dataSize); WirePointer* dstRefs = reinterpret_cast(dst + dataSize); for (auto i: kj::zeroTo(pointerCount)) { SegmentBuilder* subSegment = segment; WirePointer* dstRef = dstRefs + i; copyMessage(subSegment, capTable, dstRef, srcRefs + i); } } static word* copyMessage( SegmentBuilder*& segment, CapTableBuilder* capTable, WirePointer*& dst, const WirePointer* src) { // Not always-inline because it's recursive. switch (src->kind()) { case WirePointer::STRUCT: { if (src->isNull()) { zeroMemory(dst); return nullptr; } else { const word* srcPtr = src->target(nullptr); word* dstPtr = allocate( dst, segment, capTable, src->structRef.wordSize(), WirePointer::STRUCT, nullptr); copyStruct(segment, capTable, dstPtr, srcPtr, src->structRef.dataSize.get(), src->structRef.ptrCount.get()); dst->structRef.set(src->structRef.dataSize.get(), src->structRef.ptrCount.get()); return dstPtr; } } case WirePointer::LIST: { switch (src->listRef.elementSize()) { case ElementSize::VOID: case ElementSize::BIT: case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: { auto wordCount = roundBitsUpToWords( upgradeBound(src->listRef.elementCount()) * dataBitsPerElement(src->listRef.elementSize())); const word* srcPtr = src->target(nullptr); word* dstPtr = allocate(dst, segment, capTable, wordCount, WirePointer::LIST, nullptr); copyMemory(dstPtr, srcPtr, wordCount); dst->listRef.set(src->listRef.elementSize(), src->listRef.elementCount()); return dstPtr; } case ElementSize::POINTER: { const WirePointer* srcRefs = reinterpret_cast(src->target(nullptr)); WirePointer* dstRefs = reinterpret_cast( allocate(dst, segment, capTable, src->listRef.elementCount() * (ONE * POINTERS / ELEMENTS) * WORDS_PER_POINTER, WirePointer::LIST, nullptr)); for (auto i: kj::zeroTo(src->listRef.elementCount() * (ONE * POINTERS / ELEMENTS))) { SegmentBuilder* subSegment = segment; WirePointer* dstRef = dstRefs + i; copyMessage(subSegment, capTable, dstRef, srcRefs + i); } dst->listRef.set(ElementSize::POINTER, src->listRef.elementCount()); return reinterpret_cast(dstRefs); } case ElementSize::INLINE_COMPOSITE: { const word* srcPtr = src->target(nullptr); word* dstPtr = allocate(dst, segment, capTable, assertMaxBits( src->listRef.inlineCompositeWordCount() + POINTER_SIZE_IN_WORDS, []() { KJ_FAIL_ASSERT("list too big to fit in a segment"); }), WirePointer::LIST, nullptr); dst->listRef.setInlineComposite(src->listRef.inlineCompositeWordCount()); const WirePointer* srcTag = reinterpret_cast(srcPtr); copyMemory(reinterpret_cast(dstPtr), srcTag); const word* srcElement = srcPtr + POINTER_SIZE_IN_WORDS; word* dstElement = dstPtr + POINTER_SIZE_IN_WORDS; KJ_ASSERT(srcTag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE of lists is not yet supported."); for (auto i KJ_UNUSED: kj::zeroTo(srcTag->inlineCompositeListElementCount())) { copyStruct(segment, capTable, dstElement, srcElement, srcTag->structRef.dataSize.get(), srcTag->structRef.ptrCount.get()); srcElement += srcTag->structRef.wordSize(); dstElement += srcTag->structRef.wordSize(); } return dstPtr; } } break; } case WirePointer::OTHER: KJ_FAIL_REQUIRE("Unchecked messages cannot contain OTHER pointers (e.g. capabilities)."); break; case WirePointer::FAR: KJ_FAIL_REQUIRE("Unchecked messages cannot contain far pointers."); break; } return nullptr; } static void transferPointer(SegmentBuilder* dstSegment, WirePointer* dst, SegmentBuilder* srcSegment, WirePointer* src) { // Make *dst point to the same object as *src. Both must reside in the same message, but can // be in different segments. Not always-inline because this is rarely used. // // Caller MUST zero out the source pointer after calling this, to make sure no later code // mistakenly thinks the source location still owns the object. transferPointer() doesn't do // this zeroing itself because many callers transfer several pointers in a loop then zero out // the whole section. KJ_DASSERT(dst->isNull()); // We expect the caller to ensure the target is already null so won't leak. if (src->isNull()) { zeroMemory(dst); } else if (src->isPositional()) { transferPointer(dstSegment, dst, srcSegment, src, src->target()); } else { // Far and other pointers are position-independent, so we can just copy. copyMemory(dst, src); } } static void transferPointer(SegmentBuilder* dstSegment, WirePointer* dst, SegmentBuilder* srcSegment, const WirePointer* srcTag, word* srcPtr) { // Like the other overload, but splits src into a tag and a target. Particularly useful for // OrphanBuilder. if (dstSegment == srcSegment) { // Same segment, so create a direct pointer. if (srcTag->kind() == WirePointer::STRUCT && srcTag->structRef.wordSize() == ZERO * WORDS) { dst->setKindAndTargetForEmptyStruct(); } else { dst->setKindAndTarget(srcTag->kind(), srcPtr, dstSegment); } // We can just copy the upper 32 bits. (Use memcpy() to comply with aliasing rules.) copyMemory(&dst->upper32Bits, &srcTag->upper32Bits); } else { // Need to create a far pointer. Try to allocate it in the same segment as the source, so // that it doesn't need to be a double-far. WirePointer* landingPad = reinterpret_cast(srcSegment->allocate(G(1) * WORDS)); if (landingPad == nullptr) { // Darn, need a double-far. auto allocation = srcSegment->getArena()->allocate(G(2) * WORDS); SegmentBuilder* farSegment = allocation.segment; landingPad = reinterpret_cast(allocation.words); landingPad[0].setFar(false, srcSegment->getOffsetTo(srcPtr)); landingPad[0].farRef.segmentId.set(srcSegment->getSegmentId()); landingPad[1].setKindWithZeroOffset(srcTag->kind()); copyMemory(&landingPad[1].upper32Bits, &srcTag->upper32Bits); dst->setFar(true, farSegment->getOffsetTo(reinterpret_cast(landingPad))); dst->farRef.set(farSegment->getSegmentId()); } else { // Simple landing pad is just a pointer. landingPad->setKindAndTarget(srcTag->kind(), srcPtr, srcSegment); copyMemory(&landingPad->upper32Bits, &srcTag->upper32Bits); dst->setFar(false, srcSegment->getOffsetTo(reinterpret_cast(landingPad))); dst->farRef.set(srcSegment->getSegmentId()); } } } // ----------------------------------------------------------------- static KJ_ALWAYS_INLINE(StructBuilder initStructPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, StructSize size, BuilderArena* orphanArena = nullptr)) { // Allocate space for the new struct. Newly-allocated space is automatically zeroed. word* ptr = allocate(ref, segment, capTable, size.total(), WirePointer::STRUCT, orphanArena); // Initialize the pointer. ref->structRef.set(size); // Build the StructBuilder. return StructBuilder(segment, capTable, ptr, reinterpret_cast(ptr + size.data), size.data * BITS_PER_WORD, size.pointers); } static KJ_ALWAYS_INLINE(StructBuilder getWritableStructPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, StructSize size, const word* defaultValue)) { return getWritableStructPointer(ref, ref->target(), segment, capTable, size, defaultValue); } static KJ_ALWAYS_INLINE(StructBuilder getWritableStructPointer( WirePointer* ref, word* refTarget, SegmentBuilder* segment, CapTableBuilder* capTable, StructSize size, const word* defaultValue, BuilderArena* orphanArena = nullptr)) { if (ref->isNull()) { useDefault: if (defaultValue == nullptr || reinterpret_cast(defaultValue)->isNull()) { return initStructPointer(ref, segment, capTable, size, orphanArena); } refTarget = copyMessage(segment, capTable, ref, reinterpret_cast(defaultValue)); defaultValue = nullptr; // If the default value is itself invalid, don't use it again. } WirePointer* oldRef = ref; SegmentBuilder* oldSegment = segment; word* oldPtr = followFars(oldRef, refTarget, oldSegment); KJ_REQUIRE(oldRef->kind() == WirePointer::STRUCT, "Message contains non-struct pointer where struct pointer was expected.") { goto useDefault; } auto oldDataSize = oldRef->structRef.dataSize.get(); auto oldPointerCount = oldRef->structRef.ptrCount.get(); WirePointer* oldPointerSection = reinterpret_cast(oldPtr + oldDataSize); if (oldDataSize < size.data || oldPointerCount < size.pointers) { // The space allocated for this struct is too small. Unlike with readers, we can't just // run with it and do bounds checks at access time, because how would we handle writes? // Instead, we have to copy the struct to a new space now. auto newDataSize = kj::max(oldDataSize, size.data); auto newPointerCount = kj::max(oldPointerCount, size.pointers); auto totalSize = newDataSize + newPointerCount * WORDS_PER_POINTER; // Don't let allocate() zero out the object just yet. zeroPointerAndFars(segment, ref); word* ptr = allocate(ref, segment, capTable, totalSize, WirePointer::STRUCT, orphanArena); ref->structRef.set(newDataSize, newPointerCount); // Copy data section. copyMemory(ptr, oldPtr, oldDataSize); // Copy pointer section. WirePointer* newPointerSection = reinterpret_cast(ptr + newDataSize); for (auto i: kj::zeroTo(oldPointerCount)) { transferPointer(segment, newPointerSection + i, oldSegment, oldPointerSection + i); } // Zero out old location. This has two purposes: // 1) We don't want to leak the original contents of the struct when the message is written // out as it may contain secrets that the caller intends to remove from the new copy. // 2) Zeros will be deflated by packing, making this dead memory almost-free if it ever // hits the wire. zeroMemory(oldPtr, oldDataSize + oldPointerCount * WORDS_PER_POINTER); return StructBuilder(segment, capTable, ptr, newPointerSection, newDataSize * BITS_PER_WORD, newPointerCount); } else { return StructBuilder(oldSegment, capTable, oldPtr, oldPointerSection, oldDataSize * BITS_PER_WORD, oldPointerCount); } } static KJ_ALWAYS_INLINE(ListBuilder initListPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, ElementCount elementCount, ElementSize elementSize, BuilderArena* orphanArena = nullptr)) { KJ_DREQUIRE(elementSize != ElementSize::INLINE_COMPOSITE, "Should have called initStructListPointer() instead."); auto checkedElementCount = assertMaxBits(elementCount, []() { KJ_FAIL_REQUIRE("tried to allocate list with too many elements"); }); auto dataSize = dataBitsPerElement(elementSize) * ELEMENTS; auto pointerCount = pointersPerElement(elementSize) * ELEMENTS; auto step = bitsPerElementIncludingPointers(elementSize); KJ_DASSERT(step * ELEMENTS == (dataSize + pointerCount * BITS_PER_POINTER)); // Calculate size of the list. auto wordCount = roundBitsUpToWords(upgradeBound(checkedElementCount) * step); // Allocate the list. word* ptr = allocate(ref, segment, capTable, wordCount, WirePointer::LIST, orphanArena); // Initialize the pointer. ref->listRef.set(elementSize, checkedElementCount); // Build the ListBuilder. return ListBuilder(segment, capTable, ptr, step, checkedElementCount, dataSize, pointerCount, elementSize); } static KJ_ALWAYS_INLINE(ListBuilder initStructListPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, ElementCount elementCount, StructSize elementSize, BuilderArena* orphanArena = nullptr)) { auto checkedElementCount = assertMaxBits(elementCount, []() { KJ_FAIL_REQUIRE("tried to allocate list with too many elements"); }); WordsPerElementN<17> wordsPerElement = elementSize.total() / ELEMENTS; // Allocate the list, prefixed by a single WirePointer. auto wordCount = assertMax() - 1>( upgradeBound(checkedElementCount) * wordsPerElement, []() { KJ_FAIL_REQUIRE("total size of struct list is larger than max segment size"); }); word* ptr = allocate(ref, segment, capTable, POINTER_SIZE_IN_WORDS + wordCount, WirePointer::LIST, orphanArena); // Initialize the pointer. // INLINE_COMPOSITE lists replace the element count with the word count. ref->listRef.setInlineComposite(wordCount); // Initialize the list tag. reinterpret_cast(ptr)->setKindAndInlineCompositeListElementCount( WirePointer::STRUCT, checkedElementCount); reinterpret_cast(ptr)->structRef.set(elementSize); ptr += POINTER_SIZE_IN_WORDS; // Build the ListBuilder. return ListBuilder(segment, capTable, ptr, wordsPerElement * BITS_PER_WORD, checkedElementCount, elementSize.data * BITS_PER_WORD, elementSize.pointers, ElementSize::INLINE_COMPOSITE); } static KJ_ALWAYS_INLINE(ListBuilder getWritableListPointer( WirePointer* origRef, SegmentBuilder* origSegment, CapTableBuilder* capTable, ElementSize elementSize, const word* defaultValue)) { return getWritableListPointer(origRef, origRef->target(), origSegment, capTable, elementSize, defaultValue); } static KJ_ALWAYS_INLINE(ListBuilder getWritableListPointer( WirePointer* origRef, word* origRefTarget, SegmentBuilder* origSegment, CapTableBuilder* capTable, ElementSize elementSize, const word* defaultValue, BuilderArena* orphanArena = nullptr)) { KJ_DREQUIRE(elementSize != ElementSize::INLINE_COMPOSITE, "Use getWritableStructListPointer() for struct lists."); if (origRef->isNull()) { useDefault: if (defaultValue == nullptr || reinterpret_cast(defaultValue)->isNull()) { return ListBuilder(elementSize); } origRefTarget = copyMessage( origSegment, capTable, origRef, reinterpret_cast(defaultValue)); defaultValue = nullptr; // If the default value is itself invalid, don't use it again. } // We must verify that the pointer has the right size. Unlike in // getWritableStructListPointer(), we never need to "upgrade" the data, because this // method is called only for non-struct lists, and there is no allowed upgrade path *to* // a non-struct list, only *from* them. WirePointer* ref = origRef; SegmentBuilder* segment = origSegment; word* ptr = followFars(ref, origRefTarget, segment); KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Called getWritableListPointer() but existing pointer is not a list.") { goto useDefault; } ElementSize oldSize = ref->listRef.elementSize(); if (oldSize == ElementSize::INLINE_COMPOSITE) { // The existing element size is INLINE_COMPOSITE, though we expected a list of primitives. // The existing data must have been written with a newer version of the protocol. We // therefore never need to upgrade the data in this case, but we do need to validate that it // is a valid upgrade from what we expected. // Read the tag to get the actual element count. WirePointer* tag = reinterpret_cast(ptr); KJ_REQUIRE(tag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE list with non-STRUCT elements not supported."); ptr += POINTER_SIZE_IN_WORDS; auto dataSize = tag->structRef.dataSize.get(); auto pointerCount = tag->structRef.ptrCount.get(); switch (elementSize) { case ElementSize::VOID: // Anything is a valid upgrade from Void. break; case ElementSize::BIT: KJ_FAIL_REQUIRE( "Found struct list where bit list was expected; upgrading boolean lists to structs " "is no longer supported.") { goto useDefault; } break; case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: KJ_REQUIRE(dataSize >= ONE * WORDS, "Existing list value is incompatible with expected type.") { goto useDefault; } break; case ElementSize::POINTER: KJ_REQUIRE(pointerCount >= ONE * POINTERS, "Existing list value is incompatible with expected type.") { goto useDefault; } // Adjust the pointer to point at the reference segment. ptr += dataSize; break; case ElementSize::INLINE_COMPOSITE: KJ_UNREACHABLE; } // OK, looks valid. return ListBuilder(segment, capTable, ptr, tag->structRef.wordSize() * BITS_PER_WORD / ELEMENTS, tag->inlineCompositeListElementCount(), dataSize * BITS_PER_WORD, pointerCount, ElementSize::INLINE_COMPOSITE); } else { auto dataSize = dataBitsPerElement(oldSize) * ELEMENTS; auto pointerCount = pointersPerElement(oldSize) * ELEMENTS; if (elementSize == ElementSize::BIT) { KJ_REQUIRE(oldSize == ElementSize::BIT, "Found non-bit list where bit list was expected.") { goto useDefault; } } else { KJ_REQUIRE(oldSize != ElementSize::BIT, "Found bit list where non-bit list was expected.") { goto useDefault; } KJ_REQUIRE(dataSize >= dataBitsPerElement(elementSize) * ELEMENTS, "Existing list value is incompatible with expected type.") { goto useDefault; } KJ_REQUIRE(pointerCount >= pointersPerElement(elementSize) * ELEMENTS, "Existing list value is incompatible with expected type.") { goto useDefault; } } auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS; return ListBuilder(segment, capTable, ptr, step, ref->listRef.elementCount(), dataSize, pointerCount, oldSize); } } static KJ_ALWAYS_INLINE(ListBuilder getWritableListPointerAnySize( WirePointer* origRef, SegmentBuilder* origSegment, CapTableBuilder* capTable, const word* defaultValue)) { return getWritableListPointerAnySize(origRef, origRef->target(), origSegment, capTable, defaultValue); } static KJ_ALWAYS_INLINE(ListBuilder getWritableListPointerAnySize( WirePointer* origRef, word* origRefTarget, SegmentBuilder* origSegment, CapTableBuilder* capTable, const word* defaultValue, BuilderArena* orphanArena = nullptr)) { if (origRef->isNull()) { useDefault: if (defaultValue == nullptr || reinterpret_cast(defaultValue)->isNull()) { return ListBuilder(ElementSize::VOID); } origRefTarget = copyMessage( origSegment, capTable, origRef, reinterpret_cast(defaultValue)); defaultValue = nullptr; // If the default value is itself invalid, don't use it again. } WirePointer* ref = origRef; SegmentBuilder* segment = origSegment; word* ptr = followFars(ref, origRefTarget, segment); KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Called getWritableListPointerAnySize() but existing pointer is not a list.") { goto useDefault; } ElementSize elementSize = ref->listRef.elementSize(); if (elementSize == ElementSize::INLINE_COMPOSITE) { // Read the tag to get the actual element count. WirePointer* tag = reinterpret_cast(ptr); KJ_REQUIRE(tag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE list with non-STRUCT elements not supported."); ptr += POINTER_SIZE_IN_WORDS; return ListBuilder(segment, capTable, ptr, tag->structRef.wordSize() * BITS_PER_WORD / ELEMENTS, tag->inlineCompositeListElementCount(), tag->structRef.dataSize.get() * BITS_PER_WORD, tag->structRef.ptrCount.get(), ElementSize::INLINE_COMPOSITE); } else { auto dataSize = dataBitsPerElement(elementSize) * ELEMENTS; auto pointerCount = pointersPerElement(elementSize) * ELEMENTS; auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS; return ListBuilder(segment, capTable, ptr, step, ref->listRef.elementCount(), dataSize, pointerCount, elementSize); } } static KJ_ALWAYS_INLINE(ListBuilder getWritableStructListPointer( WirePointer* origRef, SegmentBuilder* origSegment, CapTableBuilder* capTable, StructSize elementSize, const word* defaultValue)) { return getWritableStructListPointer(origRef, origRef->target(), origSegment, capTable, elementSize, defaultValue); } static KJ_ALWAYS_INLINE(ListBuilder getWritableStructListPointer( WirePointer* origRef, word* origRefTarget, SegmentBuilder* origSegment, CapTableBuilder* capTable, StructSize elementSize, const word* defaultValue, BuilderArena* orphanArena = nullptr)) { if (origRef->isNull()) { useDefault: if (defaultValue == nullptr || reinterpret_cast(defaultValue)->isNull()) { return ListBuilder(ElementSize::INLINE_COMPOSITE); } origRefTarget = copyMessage( origSegment, capTable, origRef, reinterpret_cast(defaultValue)); defaultValue = nullptr; // If the default value is itself invalid, don't use it again. } // We must verify that the pointer has the right size and potentially upgrade it if not. WirePointer* oldRef = origRef; SegmentBuilder* oldSegment = origSegment; word* oldPtr = followFars(oldRef, origRefTarget, oldSegment); KJ_REQUIRE(oldRef->kind() == WirePointer::LIST, "Called getList{Field,Element}() but existing pointer is not a list.") { goto useDefault; } ElementSize oldSize = oldRef->listRef.elementSize(); if (oldSize == ElementSize::INLINE_COMPOSITE) { // Existing list is INLINE_COMPOSITE, but we need to verify that the sizes match. WirePointer* oldTag = reinterpret_cast(oldPtr); oldPtr += POINTER_SIZE_IN_WORDS; KJ_REQUIRE(oldTag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE list with non-STRUCT elements not supported.") { goto useDefault; } auto oldDataSize = oldTag->structRef.dataSize.get(); auto oldPointerCount = oldTag->structRef.ptrCount.get(); auto oldStep = (oldDataSize + oldPointerCount * WORDS_PER_POINTER) / ELEMENTS; auto elementCount = oldTag->inlineCompositeListElementCount(); if (oldDataSize >= elementSize.data && oldPointerCount >= elementSize.pointers) { // Old size is at least as large as we need. Ship it. return ListBuilder(oldSegment, capTable, oldPtr, oldStep * BITS_PER_WORD, elementCount, oldDataSize * BITS_PER_WORD, oldPointerCount, ElementSize::INLINE_COMPOSITE); } // The structs in this list are smaller than expected, probably written using an older // version of the protocol. We need to make a copy and expand them. auto newDataSize = kj::max(oldDataSize, elementSize.data); auto newPointerCount = kj::max(oldPointerCount, elementSize.pointers); auto newStep = (newDataSize + newPointerCount * WORDS_PER_POINTER) / ELEMENTS; auto totalSize = assertMax() - 1>( newStep * upgradeBound(elementCount), []() { KJ_FAIL_REQUIRE("total size of struct list is larger than max segment size"); }); // Don't let allocate() zero out the object just yet. zeroPointerAndFars(origSegment, origRef); word* newPtr = allocate(origRef, origSegment, capTable, totalSize + POINTER_SIZE_IN_WORDS, WirePointer::LIST, orphanArena); origRef->listRef.setInlineComposite(totalSize); WirePointer* newTag = reinterpret_cast(newPtr); newTag->setKindAndInlineCompositeListElementCount(WirePointer::STRUCT, elementCount); newTag->structRef.set(newDataSize, newPointerCount); newPtr += POINTER_SIZE_IN_WORDS; word* src = oldPtr; word* dst = newPtr; for (auto i KJ_UNUSED: kj::zeroTo(elementCount)) { // Copy data section. copyMemory(dst, src, oldDataSize); // Copy pointer section. WirePointer* newPointerSection = reinterpret_cast(dst + newDataSize); WirePointer* oldPointerSection = reinterpret_cast(src + oldDataSize); for (auto j: kj::zeroTo(oldPointerCount)) { transferPointer(origSegment, newPointerSection + j, oldSegment, oldPointerSection + j); } dst += newStep * (ONE * ELEMENTS); src += oldStep * (ONE * ELEMENTS); } auto oldSize = assertMax() - 1>( oldStep * upgradeBound(elementCount), []() { KJ_FAIL_ASSERT("old size overflows but new size doesn't?"); }); // Zero out old location. See explanation in getWritableStructPointer(). // Make sure to include the tag word. zeroMemory(oldPtr - POINTER_SIZE_IN_WORDS, oldSize + POINTER_SIZE_IN_WORDS); return ListBuilder(origSegment, capTable, newPtr, newStep * BITS_PER_WORD, elementCount, newDataSize * BITS_PER_WORD, newPointerCount, ElementSize::INLINE_COMPOSITE); } else { // We're upgrading from a non-struct list. auto oldDataSize = dataBitsPerElement(oldSize) * ELEMENTS; auto oldPointerCount = pointersPerElement(oldSize) * ELEMENTS; auto oldStep = (oldDataSize + oldPointerCount * BITS_PER_POINTER) / ELEMENTS; auto elementCount = oldRef->listRef.elementCount(); if (oldSize == ElementSize::VOID) { // Nothing to copy, just allocate a new list. return initStructListPointer(origRef, origSegment, capTable, elementCount, elementSize); } else { // Upgrading to an inline composite list. KJ_REQUIRE(oldSize != ElementSize::BIT, "Found bit list where struct list was expected; upgrading boolean lists to structs " "is no longer supported.") { goto useDefault; } auto newDataSize = elementSize.data; auto newPointerCount = elementSize.pointers; if (oldSize == ElementSize::POINTER) { newPointerCount = kj::max(newPointerCount, ONE * POINTERS); } else { // Old list contains data elements, so we need at least 1 word of data. newDataSize = kj::max(newDataSize, ONE * WORDS); } auto newStep = (newDataSize + newPointerCount * WORDS_PER_POINTER) / ELEMENTS; auto totalWords = assertMax() - 1>( newStep * upgradeBound(elementCount), []() {KJ_FAIL_REQUIRE("total size of struct list is larger than max segment size");}); // Don't let allocate() zero out the object just yet. zeroPointerAndFars(origSegment, origRef); word* newPtr = allocate(origRef, origSegment, capTable, totalWords + POINTER_SIZE_IN_WORDS, WirePointer::LIST, orphanArena); origRef->listRef.setInlineComposite(totalWords); WirePointer* tag = reinterpret_cast(newPtr); tag->setKindAndInlineCompositeListElementCount(WirePointer::STRUCT, elementCount); tag->structRef.set(newDataSize, newPointerCount); newPtr += POINTER_SIZE_IN_WORDS; if (oldSize == ElementSize::POINTER) { WirePointer* dst = reinterpret_cast(newPtr + newDataSize); WirePointer* src = reinterpret_cast(oldPtr); for (auto i KJ_UNUSED: kj::zeroTo(elementCount)) { transferPointer(origSegment, dst, oldSegment, src); dst += newStep / WORDS_PER_POINTER * (ONE * ELEMENTS); ++src; } } else { byte* dst = reinterpret_cast(newPtr); byte* src = reinterpret_cast(oldPtr); auto newByteStep = newStep * (ONE * ELEMENTS) * BYTES_PER_WORD; auto oldByteStep = oldDataSize / BITS_PER_BYTE; for (auto i KJ_UNUSED: kj::zeroTo(elementCount)) { copyMemory(dst, src, oldByteStep); src += oldByteStep; dst += newByteStep; } } auto oldSize = assertMax() - 1>( roundBitsUpToWords(oldStep * upgradeBound(elementCount)), []() { KJ_FAIL_ASSERT("old size overflows but new size doesn't?"); }); // Zero out old location. See explanation in getWritableStructPointer(). zeroMemory(oldPtr, oldSize); return ListBuilder(origSegment, capTable, newPtr, newStep * BITS_PER_WORD, elementCount, newDataSize * BITS_PER_WORD, newPointerCount, ElementSize::INLINE_COMPOSITE); } } } static KJ_ALWAYS_INLINE(SegmentAnd initTextPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, TextSize size, BuilderArena* orphanArena = nullptr)) { // The byte list must include a NUL terminator. auto byteSize = size + ONE * BYTES; // Allocate the space. word* ptr = allocate( ref, segment, capTable, roundBytesUpToWords(byteSize), WirePointer::LIST, orphanArena); // Initialize the pointer. ref->listRef.set(ElementSize::BYTE, byteSize * (ONE * ELEMENTS / BYTES)); // Build the Text::Builder. This will initialize the NUL terminator. return { segment, Text::Builder(reinterpret_cast(ptr), unbound(size / BYTES)) }; } static KJ_ALWAYS_INLINE(SegmentAnd setTextPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, Text::Reader value, BuilderArena* orphanArena = nullptr)) { TextSize size = assertMax(bounded(value.size()), []() { KJ_FAIL_REQUIRE("text blob too big"); }) * BYTES; auto allocation = initTextPointer(ref, segment, capTable, size, orphanArena); copyMemory(allocation.value.begin(), value); return allocation; } static KJ_ALWAYS_INLINE(Text::Builder getWritableTextPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, const void* defaultValue, TextSize defaultSize)) { return getWritableTextPointer(ref, ref->target(), segment,capTable, defaultValue, defaultSize); } static KJ_ALWAYS_INLINE(Text::Builder getWritableTextPointer( WirePointer* ref, word* refTarget, SegmentBuilder* segment, CapTableBuilder* capTable, const void* defaultValue, TextSize defaultSize)) { if (ref->isNull()) { useDefault: if (defaultSize == ZERO * BYTES) { return nullptr; } else { Text::Builder builder = initTextPointer(ref, segment, capTable, defaultSize).value; copyMemory(builder.asBytes().begin(), reinterpret_cast(defaultValue), defaultSize); return builder; } } else { word* ptr = followFars(ref, refTarget, segment); byte* bptr = reinterpret_cast(ptr); KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Called getText{Field,Element}() but existing pointer is not a list.") { goto useDefault; } KJ_REQUIRE(ref->listRef.elementSize() == ElementSize::BYTE, "Called getText{Field,Element}() but existing list pointer is not byte-sized.") { goto useDefault; } auto maybeSize = trySubtract(ref->listRef.elementCount() * (ONE * BYTES / ELEMENTS), ONE * BYTES); KJ_IF_MAYBE(size, maybeSize) { KJ_REQUIRE(*(bptr + *size) == '\0', "Text blob missing NUL terminator.") { goto useDefault; } return Text::Builder(reinterpret_cast(bptr), unbound(*size / BYTES)); } else { KJ_FAIL_REQUIRE("zero-size blob can't be text (need NUL terminator)") { goto useDefault; }; } } } static KJ_ALWAYS_INLINE(SegmentAnd initDataPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, BlobSize size, BuilderArena* orphanArena = nullptr)) { // Allocate the space. word* ptr = allocate(ref, segment, capTable, roundBytesUpToWords(size), WirePointer::LIST, orphanArena); // Initialize the pointer. ref->listRef.set(ElementSize::BYTE, size * (ONE * ELEMENTS / BYTES)); // Build the Data::Builder. return { segment, Data::Builder(reinterpret_cast(ptr), unbound(size / BYTES)) }; } static KJ_ALWAYS_INLINE(SegmentAnd setDataPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, Data::Reader value, BuilderArena* orphanArena = nullptr)) { BlobSize size = assertMaxBits(bounded(value.size()), []() { KJ_FAIL_REQUIRE("text blob too big"); }) * BYTES; auto allocation = initDataPointer(ref, segment, capTable, size, orphanArena); copyMemory(allocation.value.begin(), value); return allocation; } static KJ_ALWAYS_INLINE(Data::Builder getWritableDataPointer( WirePointer* ref, SegmentBuilder* segment, CapTableBuilder* capTable, const void* defaultValue, BlobSize defaultSize)) { return getWritableDataPointer(ref, ref->target(), segment, capTable, defaultValue, defaultSize); } static KJ_ALWAYS_INLINE(Data::Builder getWritableDataPointer( WirePointer* ref, word* refTarget, SegmentBuilder* segment, CapTableBuilder* capTable, const void* defaultValue, BlobSize defaultSize)) { if (ref->isNull()) { useDefault: if (defaultSize == ZERO * BYTES) { return nullptr; } else { Data::Builder builder = initDataPointer(ref, segment, capTable, defaultSize).value; copyMemory(builder.begin(), reinterpret_cast(defaultValue), defaultSize); return builder; } } else { word* ptr = followFars(ref, refTarget, segment); KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Called getData{Field,Element}() but existing pointer is not a list.") { goto useDefault; } KJ_REQUIRE(ref->listRef.elementSize() == ElementSize::BYTE, "Called getData{Field,Element}() but existing list pointer is not byte-sized.") { goto useDefault; } return Data::Builder(reinterpret_cast(ptr), unbound(ref->listRef.elementCount() / ELEMENTS)); } } static SegmentAnd setStructPointer( SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* ref, StructReader value, BuilderArena* orphanArena = nullptr, bool canonical = false) { auto dataSize = roundBitsUpToBytes(value.dataSize); auto ptrCount = value.pointerCount; if (canonical) { // StructReaders should not have bitwidths other than 1, but let's be safe KJ_REQUIRE((value.dataSize == ONE * BITS) || (value.dataSize % BITS_PER_BYTE == ZERO * BITS)); if (value.dataSize == ONE * BITS) { // Handle the truncation case where it's a false in a 1-bit struct if (!value.getDataField(ZERO * ELEMENTS)) { dataSize = ZERO * BYTES; } } else { // Truncate the data section auto data = value.getDataSectionAsBlob(); auto end = data.end(); while (end > data.begin() && end[-1] == 0) --end; dataSize = intervalLength(data.begin(), end, MAX_STUCT_DATA_WORDS * BYTES_PER_WORD); } // Truncate pointer section const WirePointer* ptr = value.pointers + ptrCount; while (ptr > value.pointers && ptr[-1].isNull()) --ptr; ptrCount = intervalLength(value.pointers, ptr, MAX_STRUCT_POINTER_COUNT); } auto dataWords = roundBytesUpToWords(dataSize); auto totalSize = dataWords + ptrCount * WORDS_PER_POINTER; word* ptr = allocate(ref, segment, capTable, totalSize, WirePointer::STRUCT, orphanArena); ref->structRef.set(dataWords, ptrCount); if (value.dataSize == ONE * BITS) { // Data size could be made 0 by truncation if (dataSize != ZERO * BYTES) { *reinterpret_cast(ptr) = value.getDataField(ZERO * ELEMENTS); } } else { copyMemory(reinterpret_cast(ptr), reinterpret_cast(value.data), dataSize); } WirePointer* pointerSection = reinterpret_cast(ptr + dataWords); for (auto i: kj::zeroTo(ptrCount)) { copyPointer(segment, capTable, pointerSection + i, value.segment, value.capTable, value.pointers + i, value.nestingLimit, nullptr, canonical); } return { segment, ptr }; } #if !CAPNP_LITE static void setCapabilityPointer( SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* ref, kj::Own&& cap) { if (!ref->isNull()) { zeroObject(segment, capTable, ref); } if (cap->isNull()) { zeroMemory(ref); } else { ref->setCap(capTable->injectCap(kj::mv(cap))); } } #endif // !CAPNP_LITE static SegmentAnd setListPointer( SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* ref, ListReader value, BuilderArena* orphanArena = nullptr, bool canonical = false) { auto totalSize = assertMax() - 1>( roundBitsUpToWords(upgradeBound(value.elementCount) * value.step), []() { KJ_FAIL_ASSERT("encountered impossibly long struct list ListReader"); }); if (value.elementSize != ElementSize::INLINE_COMPOSITE) { // List of non-structs. word* ptr = allocate(ref, segment, capTable, totalSize, WirePointer::LIST, orphanArena); if (value.elementSize == ElementSize::POINTER) { // List of pointers. ref->listRef.set(ElementSize::POINTER, value.elementCount); for (auto i: kj::zeroTo(value.elementCount * (ONE * POINTERS / ELEMENTS))) { copyPointer(segment, capTable, reinterpret_cast(ptr) + i, value.segment, value.capTable, reinterpret_cast(value.ptr) + i, value.nestingLimit, nullptr, canonical); } } else { // List of data. ref->listRef.set(value.elementSize, value.elementCount); auto wholeByteSize = assertMax(MAX_SEGMENT_WORDS * BYTES_PER_WORD, upgradeBound(value.elementCount) * value.step / BITS_PER_BYTE, []() { KJ_FAIL_ASSERT("encountered impossibly long data ListReader"); }); copyMemory(reinterpret_cast(ptr), value.ptr, wholeByteSize); auto leftoverBits = (upgradeBound(value.elementCount) * value.step) % BITS_PER_BYTE; if (leftoverBits > ZERO * BITS) { // We need to copy a partial byte. uint8_t mask = (1 << unbound(leftoverBits / BITS)) - 1; *((reinterpret_cast(ptr)) + wholeByteSize) = mask & *(value.ptr + wholeByteSize); } } return { segment, ptr }; } else { // List of structs. StructDataWordCount declDataSize = value.structDataSize / BITS_PER_WORD; StructPointerCount declPointerCount = value.structPointerCount; StructDataWordCount dataSize = ZERO * WORDS; StructPointerCount ptrCount = ZERO * POINTERS; if (canonical) { for (auto i: kj::zeroTo(value.elementCount)) { auto element = value.getStructElement(i); // Truncate the data section auto data = element.getDataSectionAsBlob(); auto end = data.end(); while (end > data.begin() && end[-1] == 0) --end; dataSize = kj::max(dataSize, roundBytesUpToWords( intervalLength(data.begin(), end, MAX_STUCT_DATA_WORDS * BYTES_PER_WORD))); // Truncate pointer section const WirePointer* ptr = element.pointers + element.pointerCount; while (ptr > element.pointers && ptr[-1].isNull()) --ptr; ptrCount = kj::max(ptrCount, intervalLength(element.pointers, ptr, MAX_STRUCT_POINTER_COUNT)); } auto newTotalSize = (dataSize + upgradeBound(ptrCount) * WORDS_PER_POINTER) / ELEMENTS * value.elementCount; KJ_ASSERT(newTotalSize <= totalSize); // we've only removed data! totalSize = assumeMax() - 1>(newTotalSize); } else { dataSize = declDataSize; ptrCount = declPointerCount; } KJ_DASSERT(value.structDataSize % BITS_PER_WORD == ZERO * BITS); word* ptr = allocate(ref, segment, capTable, totalSize + POINTER_SIZE_IN_WORDS, WirePointer::LIST, orphanArena); ref->listRef.setInlineComposite(totalSize); WirePointer* tag = reinterpret_cast(ptr); tag->setKindAndInlineCompositeListElementCount(WirePointer::STRUCT, value.elementCount); tag->structRef.set(dataSize, ptrCount); word* dst = ptr + POINTER_SIZE_IN_WORDS; const word* src = reinterpret_cast(value.ptr); for (auto i KJ_UNUSED: kj::zeroTo(value.elementCount)) { copyMemory(dst, src, dataSize); dst += dataSize; src += declDataSize; for (auto j: kj::zeroTo(ptrCount)) { copyPointer(segment, capTable, reinterpret_cast(dst) + j, value.segment, value.capTable, reinterpret_cast(src) + j, value.nestingLimit, nullptr, canonical); } dst += ptrCount * WORDS_PER_POINTER; src += declPointerCount * WORDS_PER_POINTER; } return { segment, ptr }; } } static KJ_ALWAYS_INLINE(SegmentAnd copyPointer( SegmentBuilder* dstSegment, CapTableBuilder* dstCapTable, WirePointer* dst, SegmentReader* srcSegment, CapTableReader* srcCapTable, const WirePointer* src, int nestingLimit, BuilderArena* orphanArena = nullptr, bool canonical = false)) { return copyPointer(dstSegment, dstCapTable, dst, srcSegment, srcCapTable, src, src->target(srcSegment), nestingLimit, orphanArena, canonical); } static SegmentAnd copyPointer( SegmentBuilder* dstSegment, CapTableBuilder* dstCapTable, WirePointer* dst, SegmentReader* srcSegment, CapTableReader* srcCapTable, const WirePointer* src, const word* srcTarget, int nestingLimit, BuilderArena* orphanArena = nullptr, bool canonical = false) { // Deep-copy the object pointed to by src into dst. It turns out we can't reuse // readStructPointer(), etc. because they do type checking whereas here we want to accept any // valid pointer. if (src->isNull()) { useDefault: if (!dst->isNull()) { zeroObject(dstSegment, dstCapTable, dst); zeroMemory(dst); } return { dstSegment, nullptr }; } const word* ptr; KJ_IF_MAYBE(p, WireHelpers::followFars(src, srcTarget, srcSegment)) { ptr = p; } else { goto useDefault; } switch (src->kind()) { case WirePointer::STRUCT: KJ_REQUIRE(nestingLimit > 0, "Message is too deeply-nested or contains cycles. See capnp::ReaderOptions.") { goto useDefault; } KJ_REQUIRE(boundsCheck(srcSegment, ptr, src->structRef.wordSize()), "Message contained out-of-bounds struct pointer.") { goto useDefault; } return setStructPointer(dstSegment, dstCapTable, dst, StructReader(srcSegment, srcCapTable, ptr, reinterpret_cast(ptr + src->structRef.dataSize.get()), src->structRef.dataSize.get() * BITS_PER_WORD, src->structRef.ptrCount.get(), nestingLimit - 1), orphanArena, canonical); case WirePointer::LIST: { ElementSize elementSize = src->listRef.elementSize(); KJ_REQUIRE(nestingLimit > 0, "Message is too deeply-nested or contains cycles. See capnp::ReaderOptions.") { goto useDefault; } if (elementSize == ElementSize::INLINE_COMPOSITE) { auto wordCount = src->listRef.inlineCompositeWordCount(); const WirePointer* tag = reinterpret_cast(ptr); KJ_REQUIRE(boundsCheck(srcSegment, ptr, wordCount + POINTER_SIZE_IN_WORDS), "Message contains out-of-bounds list pointer.") { goto useDefault; } ptr += POINTER_SIZE_IN_WORDS; KJ_REQUIRE(tag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE lists of non-STRUCT type are not supported.") { goto useDefault; } auto elementCount = tag->inlineCompositeListElementCount(); auto wordsPerElement = tag->structRef.wordSize() / ELEMENTS; KJ_REQUIRE(wordsPerElement * upgradeBound(elementCount) <= wordCount, "INLINE_COMPOSITE list's elements overrun its word count.") { goto useDefault; } if (wordsPerElement * (ONE * ELEMENTS) == ZERO * WORDS) { // Watch out for lists of zero-sized structs, which can claim to be arbitrarily large // without having sent actual data. KJ_REQUIRE(amplifiedRead(srcSegment, elementCount * (ONE * WORDS / ELEMENTS)), "Message contains amplified list pointer.") { goto useDefault; } } return setListPointer(dstSegment, dstCapTable, dst, ListReader(srcSegment, srcCapTable, ptr, elementCount, wordsPerElement * BITS_PER_WORD, tag->structRef.dataSize.get() * BITS_PER_WORD, tag->structRef.ptrCount.get(), ElementSize::INLINE_COMPOSITE, nestingLimit - 1), orphanArena, canonical); } else { auto dataSize = dataBitsPerElement(elementSize) * ELEMENTS; auto pointerCount = pointersPerElement(elementSize) * ELEMENTS; auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS; auto elementCount = src->listRef.elementCount(); auto wordCount = roundBitsUpToWords(upgradeBound(elementCount) * step); KJ_REQUIRE(boundsCheck(srcSegment, ptr, wordCount), "Message contains out-of-bounds list pointer.") { goto useDefault; } if (elementSize == ElementSize::VOID) { // Watch out for lists of void, which can claim to be arbitrarily large without having // sent actual data. KJ_REQUIRE(amplifiedRead(srcSegment, elementCount * (ONE * WORDS / ELEMENTS)), "Message contains amplified list pointer.") { goto useDefault; } } return setListPointer(dstSegment, dstCapTable, dst, ListReader(srcSegment, srcCapTable, ptr, elementCount, step, dataSize, pointerCount, elementSize, nestingLimit - 1), orphanArena, canonical); } } case WirePointer::FAR: KJ_FAIL_REQUIRE("Unexpected FAR pointer.") { goto useDefault; } case WirePointer::OTHER: { KJ_REQUIRE(src->isCapability(), "Unknown pointer type.") { goto useDefault; } if (canonical) { KJ_FAIL_REQUIRE("Cannot create a canonical message with a capability") { break; } } #if !CAPNP_LITE KJ_IF_MAYBE(cap, srcCapTable->extractCap(src->capRef.index.get())) { setCapabilityPointer(dstSegment, dstCapTable, dst, kj::mv(*cap)); // Return dummy non-null pointer so OrphanBuilder doesn't end up null. return { dstSegment, reinterpret_cast(1) }; } else { #endif // !CAPNP_LITE KJ_FAIL_REQUIRE("Message contained invalid capability pointer.") { goto useDefault; } #if !CAPNP_LITE } #endif // !CAPNP_LITE } } KJ_UNREACHABLE; } static void adopt(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* ref, OrphanBuilder&& value) { KJ_REQUIRE(value.segment == nullptr || value.segment->getArena() == segment->getArena(), "Adopted object must live in the same message."); if (!ref->isNull()) { zeroObject(segment, capTable, ref); } if (value == nullptr) { // Set null. zeroMemory(ref); } else if (value.tagAsPtr()->isPositional()) { WireHelpers::transferPointer(segment, ref, value.segment, value.tagAsPtr(), value.location); } else { // FAR and OTHER pointers are position-independent, so we can just copy. copyMemory(ref, value.tagAsPtr()); } // Take ownership away from the OrphanBuilder. zeroMemory(value.tagAsPtr()); value.location = nullptr; value.segment = nullptr; } static OrphanBuilder disown(SegmentBuilder* segment, CapTableBuilder* capTable, WirePointer* ref) { word* location; if (ref->isNull()) { location = nullptr; } else if (ref->kind() == WirePointer::OTHER) { KJ_REQUIRE(ref->isCapability(), "Unknown pointer type.") { break; } location = reinterpret_cast(1); // dummy so that it is non-null } else { WirePointer* refCopy = ref; location = followFarsNoWritableCheck(refCopy, ref->target(), segment); } OrphanBuilder result(ref, segment, capTable, location); if (!ref->isNull() && ref->isPositional()) { result.tagAsPtr()->setKindForOrphan(ref->kind()); } // Zero out the pointer that was disowned. zeroMemory(ref); return result; } // ----------------------------------------------------------------- static KJ_ALWAYS_INLINE(StructReader readStructPointer( SegmentReader* segment, CapTableReader* capTable, const WirePointer* ref, const word* defaultValue, int nestingLimit)) { return readStructPointer(segment, capTable, ref, ref->target(segment), defaultValue, nestingLimit); } static KJ_ALWAYS_INLINE(StructReader readStructPointer( SegmentReader* segment, CapTableReader* capTable, const WirePointer* ref, const word* refTarget, const word* defaultValue, int nestingLimit)) { if (ref->isNull()) { useDefault: if (defaultValue == nullptr || reinterpret_cast(defaultValue)->isNull()) { return StructReader(); } segment = nullptr; ref = reinterpret_cast(defaultValue); refTarget = ref->target(segment); defaultValue = nullptr; // If the default value is itself invalid, don't use it again. } KJ_REQUIRE(nestingLimit > 0, "Message is too deeply-nested or contains cycles. See capnp::ReaderOptions.") { goto useDefault; } const word* ptr; KJ_IF_MAYBE(p, followFars(ref, refTarget, segment)) { ptr = p; } else { goto useDefault; } KJ_REQUIRE(ref->kind() == WirePointer::STRUCT, "Message contains non-struct pointer where struct pointer was expected.") { goto useDefault; } KJ_REQUIRE(boundsCheck(segment, ptr, ref->structRef.wordSize()), "Message contained out-of-bounds struct pointer.") { goto useDefault; } return StructReader( segment, capTable, ptr, reinterpret_cast(ptr + ref->structRef.dataSize.get()), ref->structRef.dataSize.get() * BITS_PER_WORD, ref->structRef.ptrCount.get(), nestingLimit - 1); } #if !CAPNP_LITE static KJ_ALWAYS_INLINE(kj::Own readCapabilityPointer( SegmentReader* segment, CapTableReader* capTable, const WirePointer* ref, int nestingLimit)) { kj::Maybe> maybeCap; KJ_REQUIRE(brokenCapFactory != nullptr, "Trying to read capabilities without ever having created a capability context. " "To read capabilities from a message, you must imbue it with CapReaderContext, or " "use the Cap'n Proto RPC system."); if (ref->isNull()) { return brokenCapFactory->newNullCap(); } else if (!ref->isCapability()) { KJ_FAIL_REQUIRE( "Message contains non-capability pointer where capability pointer was expected.") { break; } return brokenCapFactory->newBrokenCap( "Calling capability extracted from a non-capability pointer."); } else KJ_IF_MAYBE(cap, capTable->extractCap(ref->capRef.index.get())) { return kj::mv(*cap); } else { KJ_FAIL_REQUIRE("Message contains invalid capability pointer.") { break; } return brokenCapFactory->newBrokenCap("Calling invalid capability pointer."); } } #endif // !CAPNP_LITE static KJ_ALWAYS_INLINE(ListReader readListPointer( SegmentReader* segment, CapTableReader* capTable, const WirePointer* ref, const word* defaultValue, ElementSize expectedElementSize, int nestingLimit, bool checkElementSize = true)) { return readListPointer(segment, capTable, ref, ref->target(segment), defaultValue, expectedElementSize, nestingLimit, checkElementSize); } static KJ_ALWAYS_INLINE(ListReader readListPointer( SegmentReader* segment, CapTableReader* capTable, const WirePointer* ref, const word* refTarget, const word* defaultValue, ElementSize expectedElementSize, int nestingLimit, bool checkElementSize = true)) { if (ref->isNull()) { useDefault: if (defaultValue == nullptr || reinterpret_cast(defaultValue)->isNull()) { return ListReader(expectedElementSize); } segment = nullptr; ref = reinterpret_cast(defaultValue); refTarget = ref->target(segment); defaultValue = nullptr; // If the default value is itself invalid, don't use it again. } KJ_REQUIRE(nestingLimit > 0, "Message is too deeply-nested or contains cycles. See capnp::ReaderOptions.") { goto useDefault; } const word* ptr; KJ_IF_MAYBE(p, followFars(ref, refTarget, segment)) { ptr = p; } else { goto useDefault; } KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Message contains non-list pointer where list pointer was expected.") { goto useDefault; } ElementSize elementSize = ref->listRef.elementSize(); if (elementSize == ElementSize::INLINE_COMPOSITE) { auto wordCount = ref->listRef.inlineCompositeWordCount(); // An INLINE_COMPOSITE list points to a tag, which is formatted like a pointer. const WirePointer* tag = reinterpret_cast(ptr); KJ_REQUIRE(boundsCheck(segment, ptr, wordCount + POINTER_SIZE_IN_WORDS), "Message contains out-of-bounds list pointer.") { goto useDefault; } ptr += POINTER_SIZE_IN_WORDS; KJ_REQUIRE(tag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE lists of non-STRUCT type are not supported.") { goto useDefault; } auto size = tag->inlineCompositeListElementCount(); auto wordsPerElement = tag->structRef.wordSize() / ELEMENTS; KJ_REQUIRE(upgradeBound(size) * wordsPerElement <= wordCount, "INLINE_COMPOSITE list's elements overrun its word count.") { goto useDefault; } if (wordsPerElement * (ONE * ELEMENTS) == ZERO * WORDS) { // Watch out for lists of zero-sized structs, which can claim to be arbitrarily large // without having sent actual data. KJ_REQUIRE(amplifiedRead(segment, size * (ONE * WORDS / ELEMENTS)), "Message contains amplified list pointer.") { goto useDefault; } } if (checkElementSize) { // If a struct list was not expected, then presumably a non-struct list was upgraded to a // struct list. We need to manipulate the pointer to point at the first field of the // struct. Together with the `step` field, this will allow the struct list to be accessed // as if it were a primitive list without branching. // Check whether the size is compatible. switch (expectedElementSize) { case ElementSize::VOID: break; case ElementSize::BIT: KJ_FAIL_REQUIRE( "Found struct list where bit list was expected; upgrading boolean lists to structs " "is no longer supported.") { goto useDefault; } break; case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: KJ_REQUIRE(tag->structRef.dataSize.get() > ZERO * WORDS, "Expected a primitive list, but got a list of pointer-only structs.") { goto useDefault; } break; case ElementSize::POINTER: // We expected a list of pointers but got a list of structs. Assuming the first field // in the struct is the pointer we were looking for, we want to munge the pointer to // point at the first element's pointer section. ptr += tag->structRef.dataSize.get(); KJ_REQUIRE(tag->structRef.ptrCount.get() > ZERO * POINTERS, "Expected a pointer list, but got a list of data-only structs.") { goto useDefault; } break; case ElementSize::INLINE_COMPOSITE: break; } } return ListReader( segment, capTable, ptr, size, wordsPerElement * BITS_PER_WORD, tag->structRef.dataSize.get() * BITS_PER_WORD, tag->structRef.ptrCount.get(), ElementSize::INLINE_COMPOSITE, nestingLimit - 1); } else { // This is a primitive or pointer list, but all such lists can also be interpreted as struct // lists. We need to compute the data size and pointer count for such structs. auto dataSize = dataBitsPerElement(ref->listRef.elementSize()) * ELEMENTS; auto pointerCount = pointersPerElement(ref->listRef.elementSize()) * ELEMENTS; auto elementCount = ref->listRef.elementCount(); auto step = (dataSize + pointerCount * BITS_PER_POINTER) / ELEMENTS; auto wordCount = roundBitsUpToWords(upgradeBound(elementCount) * step); KJ_REQUIRE(boundsCheck(segment, ptr, wordCount), "Message contains out-of-bounds list pointer.") { goto useDefault; } if (elementSize == ElementSize::VOID) { // Watch out for lists of void, which can claim to be arbitrarily large without having sent // actual data. KJ_REQUIRE(amplifiedRead(segment, elementCount * (ONE * WORDS / ELEMENTS)), "Message contains amplified list pointer.") { goto useDefault; } } if (checkElementSize) { if (elementSize == ElementSize::BIT && expectedElementSize != ElementSize::BIT) { KJ_FAIL_REQUIRE( "Found bit list where struct list was expected; upgrading boolean lists to structs " "is no longer supported.") { goto useDefault; } } // Verify that the elements are at least as large as the expected type. Note that if we // expected INLINE_COMPOSITE, the expected sizes here will be zero, because bounds checking // will be performed at field access time. So this check here is for the case where we // expected a list of some primitive or pointer type. BitCount expectedDataBitsPerElement = dataBitsPerElement(expectedElementSize) * ELEMENTS; WirePointerCount expectedPointersPerElement = pointersPerElement(expectedElementSize) * ELEMENTS; KJ_REQUIRE(expectedDataBitsPerElement <= dataSize, "Message contained list with incompatible element type.") { goto useDefault; } KJ_REQUIRE(expectedPointersPerElement <= pointerCount, "Message contained list with incompatible element type.") { goto useDefault; } } return ListReader(segment, capTable, ptr, elementCount, step, dataSize, pointerCount, elementSize, nestingLimit - 1); } } static KJ_ALWAYS_INLINE(Text::Reader readTextPointer( SegmentReader* segment, const WirePointer* ref, const void* defaultValue, ByteCount defaultSize)) { return readTextPointer(segment, ref, ref->target(segment), defaultValue, defaultSize); } static KJ_ALWAYS_INLINE(Text::Reader readTextPointer( SegmentReader* segment, const WirePointer* ref, const word* refTarget, const void* defaultValue, ByteCount defaultSize)) { if (ref->isNull()) { useDefault: if (defaultValue == nullptr) defaultValue = ""; return Text::Reader(reinterpret_cast(defaultValue), unbound(defaultSize / BYTES)); } else { const word* ptr; KJ_IF_MAYBE(p, followFars(ref, refTarget, segment)) { ptr = p; } else { goto useDefault; } auto size = ref->listRef.elementCount() * (ONE * BYTES / ELEMENTS); KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Message contains non-list pointer where text was expected.") { goto useDefault; } KJ_REQUIRE(ref->listRef.elementSize() == ElementSize::BYTE, "Message contains list pointer of non-bytes where text was expected.") { goto useDefault; } KJ_REQUIRE(boundsCheck(segment, ptr, roundBytesUpToWords(size)), "Message contained out-of-bounds text pointer.") { goto useDefault; } KJ_REQUIRE(size > ZERO * BYTES, "Message contains text that is not NUL-terminated.") { goto useDefault; } const char* cptr = reinterpret_cast(ptr); uint unboundedSize = unbound(size / BYTES) - 1; KJ_REQUIRE(cptr[unboundedSize] == '\0', "Message contains text that is not NUL-terminated.") { goto useDefault; } return Text::Reader(cptr, unboundedSize); } } static KJ_ALWAYS_INLINE(Data::Reader readDataPointer( SegmentReader* segment, const WirePointer* ref, const void* defaultValue, BlobSize defaultSize)) { return readDataPointer(segment, ref, ref->target(segment), defaultValue, defaultSize); } static KJ_ALWAYS_INLINE(Data::Reader readDataPointer( SegmentReader* segment, const WirePointer* ref, const word* refTarget, const void* defaultValue, BlobSize defaultSize)) { if (ref->isNull()) { useDefault: return Data::Reader(reinterpret_cast(defaultValue), unbound(defaultSize / BYTES)); } else { const word* ptr; KJ_IF_MAYBE(p, followFars(ref, refTarget, segment)) { ptr = p; } else { goto useDefault; } if (KJ_UNLIKELY(ptr == nullptr)) { // Already reported error. goto useDefault; } auto size = ref->listRef.elementCount() * (ONE * BYTES / ELEMENTS); KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Message contains non-list pointer where data was expected.") { goto useDefault; } KJ_REQUIRE(ref->listRef.elementSize() == ElementSize::BYTE, "Message contains list pointer of non-bytes where data was expected.") { goto useDefault; } KJ_REQUIRE(boundsCheck(segment, ptr, roundBytesUpToWords(size)), "Message contained out-of-bounds data pointer.") { goto useDefault; } return Data::Reader(reinterpret_cast(ptr), unbound(size / BYTES)); } } }; // ======================================================================================= // PointerBuilder StructBuilder PointerBuilder::initStruct(StructSize size) { return WireHelpers::initStructPointer(pointer, segment, capTable, size); } StructBuilder PointerBuilder::getStruct(StructSize size, const word* defaultValue) { return WireHelpers::getWritableStructPointer(pointer, segment, capTable, size, defaultValue); } ListBuilder PointerBuilder::initList(ElementSize elementSize, ElementCount elementCount) { return WireHelpers::initListPointer(pointer, segment, capTable, elementCount, elementSize); } ListBuilder PointerBuilder::initStructList(ElementCount elementCount, StructSize elementSize) { return WireHelpers::initStructListPointer(pointer, segment, capTable, elementCount, elementSize); } ListBuilder PointerBuilder::getList(ElementSize elementSize, const word* defaultValue) { return WireHelpers::getWritableListPointer(pointer, segment, capTable, elementSize, defaultValue); } ListBuilder PointerBuilder::getStructList(StructSize elementSize, const word* defaultValue) { return WireHelpers::getWritableStructListPointer( pointer, segment, capTable, elementSize, defaultValue); } ListBuilder PointerBuilder::getListAnySize(const word* defaultValue) { return WireHelpers::getWritableListPointerAnySize(pointer, segment, capTable, defaultValue); } template <> Text::Builder PointerBuilder::initBlob(ByteCount size) { return WireHelpers::initTextPointer(pointer, segment, capTable, assertMax(size, ThrowOverflow())).value; } template <> void PointerBuilder::setBlob(Text::Reader value) { WireHelpers::setTextPointer(pointer, segment, capTable, value); } template <> Text::Builder PointerBuilder::getBlob(const void* defaultValue, ByteCount defaultSize) { return WireHelpers::getWritableTextPointer(pointer, segment, capTable, defaultValue, assertMax(defaultSize, ThrowOverflow())); } template <> Data::Builder PointerBuilder::initBlob(ByteCount size) { return WireHelpers::initDataPointer(pointer, segment, capTable, assertMaxBits(size, ThrowOverflow())).value; } template <> void PointerBuilder::setBlob(Data::Reader value) { WireHelpers::setDataPointer(pointer, segment, capTable, value); } template <> Data::Builder PointerBuilder::getBlob(const void* defaultValue, ByteCount defaultSize) { return WireHelpers::getWritableDataPointer(pointer, segment, capTable, defaultValue, assertMaxBits(defaultSize, ThrowOverflow())); } void PointerBuilder::setStruct(const StructReader& value, bool canonical) { WireHelpers::setStructPointer(segment, capTable, pointer, value, nullptr, canonical); } void PointerBuilder::setList(const ListReader& value, bool canonical) { WireHelpers::setListPointer(segment, capTable, pointer, value, nullptr, canonical); } #if !CAPNP_LITE kj::Own PointerBuilder::getCapability() { return WireHelpers::readCapabilityPointer( segment, capTable, pointer, kj::maxValue); } void PointerBuilder::setCapability(kj::Own&& cap) { WireHelpers::setCapabilityPointer(segment, capTable, pointer, kj::mv(cap)); } #endif // !CAPNP_LITE void PointerBuilder::adopt(OrphanBuilder&& value) { WireHelpers::adopt(segment, capTable, pointer, kj::mv(value)); } OrphanBuilder PointerBuilder::disown() { return WireHelpers::disown(segment, capTable, pointer); } void PointerBuilder::clear() { WireHelpers::zeroObject(segment, capTable, pointer); WireHelpers::zeroMemory(pointer); } PointerType PointerBuilder::getPointerType() const { if(pointer->isNull()) { return PointerType::NULL_; } else { WirePointer* ptr = pointer; SegmentBuilder* sgmt = segment; WireHelpers::followFars(ptr, ptr->target(), sgmt); switch(ptr->kind()) { case WirePointer::FAR: KJ_FAIL_ASSERT("far pointer not followed?"); case WirePointer::STRUCT: return PointerType::STRUCT; case WirePointer::LIST: return PointerType::LIST; case WirePointer::OTHER: KJ_REQUIRE(ptr->isCapability(), "unknown pointer type"); return PointerType::CAPABILITY; } KJ_UNREACHABLE; } } void PointerBuilder::transferFrom(PointerBuilder other) { if (!pointer->isNull()) { WireHelpers::zeroObject(segment, capTable, pointer); WireHelpers::zeroMemory(pointer); } WireHelpers::transferPointer(segment, pointer, other.segment, other.pointer); WireHelpers::zeroMemory(other.pointer); } void PointerBuilder::copyFrom(PointerReader other, bool canonical) { if (other.pointer == nullptr) { if (!pointer->isNull()) { WireHelpers::zeroObject(segment, capTable, pointer); WireHelpers::zeroMemory(pointer); } } else { WireHelpers::copyPointer(segment, capTable, pointer, other.segment, other.capTable, other.pointer, other.nestingLimit, nullptr, canonical); } } PointerReader PointerBuilder::asReader() const { return PointerReader(segment, capTable, pointer, kj::maxValue); } BuilderArena* PointerBuilder::getArena() const { return segment->getArena(); } CapTableBuilder* PointerBuilder::getCapTable() { return capTable; } PointerBuilder PointerBuilder::imbue(CapTableBuilder* capTable) { auto result = *this; result.capTable = capTable; return result; } // ======================================================================================= // PointerReader PointerReader PointerReader::getRoot(SegmentReader* segment, CapTableReader* capTable, const word* location, int nestingLimit) { KJ_REQUIRE(WireHelpers::boundsCheck(segment, location, POINTER_SIZE_IN_WORDS), "Root location out-of-bounds.") { location = nullptr; } return PointerReader(segment, capTable, reinterpret_cast(location), nestingLimit); } StructReader PointerReader::getStruct(const word* defaultValue) const { const WirePointer* ref = pointer == nullptr ? &zero.pointer : pointer; return WireHelpers::readStructPointer(segment, capTable, ref, defaultValue, nestingLimit); } ListReader PointerReader::getList(ElementSize expectedElementSize, const word* defaultValue) const { const WirePointer* ref = pointer == nullptr ? &zero.pointer : pointer; return WireHelpers::readListPointer( segment, capTable, ref, defaultValue, expectedElementSize, nestingLimit); } ListReader PointerReader::getListAnySize(const word* defaultValue) const { const WirePointer* ref = pointer == nullptr ? &zero.pointer : pointer; return WireHelpers::readListPointer( segment, capTable, ref, defaultValue, ElementSize::VOID /* dummy */, nestingLimit, false); } template <> Text::Reader PointerReader::getBlob(const void* defaultValue, ByteCount defaultSize) const { const WirePointer* ref = pointer == nullptr ? &zero.pointer : pointer; return WireHelpers::readTextPointer(segment, ref, defaultValue, defaultSize); } template <> Data::Reader PointerReader::getBlob(const void* defaultValue, ByteCount defaultSize) const { const WirePointer* ref = pointer == nullptr ? &zero.pointer : pointer; return WireHelpers::readDataPointer(segment, ref, defaultValue, assertMaxBits(defaultSize, ThrowOverflow())); } #if !CAPNP_LITE kj::Own PointerReader::getCapability() const { const WirePointer* ref = pointer == nullptr ? &zero.pointer : pointer; return WireHelpers::readCapabilityPointer(segment, capTable, ref, nestingLimit); } #endif // !CAPNP_LITE const word* PointerReader::getUnchecked() const { KJ_REQUIRE(segment == nullptr, "getUncheckedPointer() only allowed on unchecked messages."); return reinterpret_cast(pointer); } MessageSizeCounts PointerReader::targetSize() const { return pointer == nullptr ? MessageSizeCounts { ZERO * WORDS, 0 } : WireHelpers::totalSize(segment, pointer, nestingLimit); } PointerType PointerReader::getPointerType() const { if(pointer == nullptr || pointer->isNull()) { return PointerType::NULL_; } else { const WirePointer* ptr = pointer; const word* refTarget = ptr->target(segment); SegmentReader* sgmt = segment; if (WireHelpers::followFars(ptr, refTarget, sgmt) == nullptr) return PointerType::NULL_; switch(ptr->kind()) { case WirePointer::FAR: KJ_FAIL_ASSERT("far pointer not followed?") { return PointerType::NULL_; } case WirePointer::STRUCT: return PointerType::STRUCT; case WirePointer::LIST: return PointerType::LIST; case WirePointer::OTHER: KJ_REQUIRE(ptr->isCapability(), "unknown pointer type") { return PointerType::NULL_; } return PointerType::CAPABILITY; } KJ_UNREACHABLE; } } kj::Maybe PointerReader::getArena() const { return segment == nullptr ? nullptr : segment->getArena(); } CapTableReader* PointerReader::getCapTable() { return capTable; } PointerReader PointerReader::imbue(CapTableReader* capTable) const { auto result = *this; result.capTable = capTable; return result; } bool PointerReader::isCanonical(const word **readHead) { if (!this->pointer) { // The pointer is null, so we are canonical and do not read return true; } if (!this->pointer->isPositional()) { // The pointer is a FAR or OTHER pointer, and is non-canonical return false; } switch (this->getPointerType()) { case PointerType::NULL_: // The pointer is null, we are canonical and do not read return true; case PointerType::STRUCT: { bool dataTrunc = false, ptrTrunc = false; auto structReader = this->getStruct(nullptr); if (structReader.getDataSectionSize() == ZERO * BITS && structReader.getPointerSectionSize() == ZERO * POINTERS) { return reinterpret_cast(this->pointer) == structReader.getLocation(); } else { // Fun fact: Once this call to isCanonical() returns, Clang may re-order the evaluation of // the && operators. In theory this is wrong because && is short-circuiting, but Clang // apparently sees that there are no side effects to the right of &&, so decides it is // safe to skip short-circuiting. It turns out, though, this is observable under // valgrind: if we don't initialize `dataTrunc` when declaring it above, then valgrind // reports "Conditional jump or move depends on uninitialised value(s)". Specifically // this happens in cases where structReader.isCanonical() returns false -- it is allowed // to skip initializing `dataTrunc` in that case. The short-circuiting && should mean // that we don't read `dataTrunc` in that case, except Clang's optimizations. Ultimately // the uninitialized read is fine because eventually the whole expression evaluates false // either way. But, to make valgrind happy, we initialize the bools above... return structReader.isCanonical(readHead, readHead, &dataTrunc, &ptrTrunc) && dataTrunc && ptrTrunc; } } case PointerType::LIST: return this->getListAnySize(nullptr).isCanonical(readHead, pointer); case PointerType::CAPABILITY: KJ_FAIL_ASSERT("Capabilities are not positional"); } KJ_UNREACHABLE; } // ======================================================================================= // StructBuilder void StructBuilder::clearAll() { if (dataSize == ONE * BITS) { setDataField(ONE * ELEMENTS, false); } else { WireHelpers::zeroMemory(reinterpret_cast(data), dataSize / BITS_PER_BYTE); } for (auto i: kj::zeroTo(pointerCount)) { WireHelpers::zeroObject(segment, capTable, pointers + i); } WireHelpers::zeroMemory(pointers, pointerCount); } void StructBuilder::transferContentFrom(StructBuilder other) { // Determine the amount of data the builders have in common. auto sharedDataSize = kj::min(dataSize, other.dataSize); if (dataSize > sharedDataSize) { // Since the target is larger than the source, make sure to zero out the extra bits that the // source doesn't have. if (dataSize == ONE * BITS) { setDataField(ZERO * ELEMENTS, false); } else { byte* unshared = reinterpret_cast(data) + sharedDataSize / BITS_PER_BYTE; // Note: this subtraction can't fail due to the if() above WireHelpers::zeroMemory(unshared, subtractChecked(dataSize, sharedDataSize, []() {}) / BITS_PER_BYTE); } } // Copy over the shared part. if (sharedDataSize == ONE * BITS) { setDataField(ZERO * ELEMENTS, other.getDataField(ZERO * ELEMENTS)); } else { WireHelpers::copyMemory(reinterpret_cast(data), reinterpret_cast(other.data), sharedDataSize / BITS_PER_BYTE); } // Zero out all pointers in the target. for (auto i: kj::zeroTo(pointerCount)) { WireHelpers::zeroObject(segment, capTable, pointers + i); } WireHelpers::zeroMemory(pointers, pointerCount); // Transfer the pointers. auto sharedPointerCount = kj::min(pointerCount, other.pointerCount); for (auto i: kj::zeroTo(sharedPointerCount)) { WireHelpers::transferPointer(segment, pointers + i, other.segment, other.pointers + i); } // Zero out the pointers that were transferred in the source because it no longer has ownership. // If the source had any extra pointers that the destination didn't have space for, we // intentionally leave them be, so that they'll be cleaned up later. WireHelpers::zeroMemory(other.pointers, sharedPointerCount); } void StructBuilder::copyContentFrom(StructReader other) { // Determine the amount of data the builders have in common. auto sharedDataSize = kj::min(dataSize, other.dataSize); auto sharedPointerCount = kj::min(pointerCount, other.pointerCount); if ((sharedDataSize > ZERO * BITS && other.data == data) || (sharedPointerCount > ZERO * POINTERS && other.pointers == pointers)) { // At least one of the section pointers is pointing to ourself. Verify that the other is two // (but ignore empty sections). KJ_ASSERT((sharedDataSize == ZERO * BITS || other.data == data) && (sharedPointerCount == ZERO * POINTERS || other.pointers == pointers)); // So `other` appears to be a reader for this same struct. No coping is needed. return; } if (dataSize > sharedDataSize) { // Since the target is larger than the source, make sure to zero out the extra bits that the // source doesn't have. if (dataSize == ONE * BITS) { setDataField(ZERO * ELEMENTS, false); } else { byte* unshared = reinterpret_cast(data) + sharedDataSize / BITS_PER_BYTE; WireHelpers::zeroMemory(unshared, subtractChecked(dataSize, sharedDataSize, []() {}) / BITS_PER_BYTE); } } // Copy over the shared part. if (sharedDataSize == ONE * BITS) { setDataField(ZERO * ELEMENTS, other.getDataField(ZERO * ELEMENTS)); } else { WireHelpers::copyMemory(reinterpret_cast(data), reinterpret_cast(other.data), sharedDataSize / BITS_PER_BYTE); } // Zero out all pointers in the target. for (auto i: kj::zeroTo(pointerCount)) { WireHelpers::zeroObject(segment, capTable, pointers + i); } WireHelpers::zeroMemory(pointers, pointerCount); // Copy the pointers. for (auto i: kj::zeroTo(sharedPointerCount)) { WireHelpers::copyPointer(segment, capTable, pointers + i, other.segment, other.capTable, other.pointers + i, other.nestingLimit); } } StructReader StructBuilder::asReader() const { return StructReader(segment, capTable, data, pointers, dataSize, pointerCount, kj::maxValue); } BuilderArena* StructBuilder::getArena() { return segment->getArena(); } CapTableBuilder* StructBuilder::getCapTable() { return capTable; } StructBuilder StructBuilder::imbue(CapTableBuilder* capTable) { auto result = *this; result.capTable = capTable; return result; } // ======================================================================================= // StructReader MessageSizeCounts StructReader::totalSize() const { MessageSizeCounts result = { WireHelpers::roundBitsUpToWords(dataSize) + pointerCount * WORDS_PER_POINTER, 0 }; for (auto i: kj::zeroTo(pointerCount)) { result += WireHelpers::totalSize(segment, pointers + i, nestingLimit); } if (segment != nullptr) { // This traversal should not count against the read limit, because it's highly likely that // the caller is going to traverse the object again, e.g. to copy it. segment->unread(result.wordCount); } return result; } kj::Array StructReader::canonicalize() { auto size = totalSize().wordCount + POINTER_SIZE_IN_WORDS; kj::Array backing = kj::heapArray(unbound(size / WORDS)); WireHelpers::zeroMemory(backing.asPtr()); FlatMessageBuilder builder(backing); _::PointerHelpers::getInternalBuilder(builder.initRoot()).setStruct(*this, true); KJ_ASSERT(builder.isCanonical()); auto output = builder.getSegmentsForOutput()[0]; kj::Array trunc = kj::heapArray(output.size()); WireHelpers::copyMemory(trunc.begin(), output); return trunc; } CapTableReader* StructReader::getCapTable() { return capTable; } StructReader StructReader::imbue(CapTableReader* capTable) const { auto result = *this; result.capTable = capTable; return result; } bool StructReader::isCanonical(const word **readHead, const word **ptrHead, bool *dataTrunc, bool *ptrTrunc) { if (this->getLocation() != *readHead) { // Our target area is not at the readHead, preorder fails return false; } if (this->getDataSectionSize() % BITS_PER_WORD != ZERO * BITS) { // Using legacy non-word-size structs, reject return false; } auto dataSize = this->getDataSectionSize() / BITS_PER_WORD; // Mark whether the struct is properly truncated KJ_IF_MAYBE(diff, trySubtract(dataSize, ONE * WORDS)) { *dataTrunc = this->getDataField(*diff / WORDS * ELEMENTS) != 0; } else { // Data segment empty. *dataTrunc = true; } KJ_IF_MAYBE(diff, trySubtract(this->pointerCount, ONE * POINTERS)) { *ptrTrunc = !this->getPointerField(*diff).isNull(); } else { *ptrTrunc = true; } // Advance the read head *readHead += (dataSize + (this->pointerCount * WORDS_PER_POINTER)); // Check each pointer field for canonicity for (auto ptrIndex: kj::zeroTo(this->pointerCount)) { if (!this->getPointerField(ptrIndex).isCanonical(ptrHead)) { return false; } } return true; } // ======================================================================================= // ListBuilder Text::Builder ListBuilder::asText() { KJ_REQUIRE(structDataSize == G(8) * BITS && structPointerCount == ZERO * POINTERS, "Expected Text, got list of non-bytes.") { return Text::Builder(); } size_t size = unbound(elementCount / ELEMENTS); KJ_REQUIRE(size > 0, "Message contains text that is not NUL-terminated.") { return Text::Builder(); } char* cptr = reinterpret_cast(ptr); --size; // NUL terminator KJ_REQUIRE(cptr[size] == '\0', "Message contains text that is not NUL-terminated.") { return Text::Builder(); } return Text::Builder(cptr, size); } Data::Builder ListBuilder::asData() { KJ_REQUIRE(structDataSize == G(8) * BITS && structPointerCount == ZERO * POINTERS, "Expected Text, got list of non-bytes.") { return Data::Builder(); } return Data::Builder(reinterpret_cast(ptr), unbound(elementCount / ELEMENTS)); } StructBuilder ListBuilder::getStructElement(ElementCount index) { auto indexBit = upgradeBound(index) * step; byte* structData = ptr + indexBit / BITS_PER_BYTE; KJ_DASSERT(indexBit % BITS_PER_BYTE == ZERO * BITS); return StructBuilder(segment, capTable, structData, reinterpret_cast(structData + structDataSize / BITS_PER_BYTE), structDataSize, structPointerCount); } ListReader ListBuilder::asReader() const { return ListReader(segment, capTable, ptr, elementCount, step, structDataSize, structPointerCount, elementSize, kj::maxValue); } BuilderArena* ListBuilder::getArena() { return segment->getArena(); } CapTableBuilder* ListBuilder::getCapTable() { return capTable; } ListBuilder ListBuilder::imbue(CapTableBuilder* capTable) { auto result = *this; result.capTable = capTable; return result; } // ======================================================================================= // ListReader Text::Reader ListReader::asText() { KJ_REQUIRE(structDataSize == G(8) * BITS && structPointerCount == ZERO * POINTERS, "Expected Text, got list of non-bytes.") { return Text::Reader(); } size_t size = unbound(elementCount / ELEMENTS); KJ_REQUIRE(size > 0, "Message contains text that is not NUL-terminated.") { return Text::Reader(); } const char* cptr = reinterpret_cast(ptr); --size; // NUL terminator KJ_REQUIRE(cptr[size] == '\0', "Message contains text that is not NUL-terminated.") { return Text::Reader(); } return Text::Reader(cptr, size); } Data::Reader ListReader::asData() { KJ_REQUIRE(structDataSize == G(8) * BITS && structPointerCount == ZERO * POINTERS, "Expected Text, got list of non-bytes.") { return Data::Reader(); } return Data::Reader(reinterpret_cast(ptr), unbound(elementCount / ELEMENTS)); } kj::ArrayPtr ListReader::asRawBytes() const { KJ_REQUIRE(structPointerCount == ZERO * POINTERS, "Expected data only, got pointers.") { return kj::ArrayPtr(); } return arrayPtr(reinterpret_cast(ptr), WireHelpers::roundBitsUpToBytes( upgradeBound(elementCount) * (structDataSize / ELEMENTS))); } StructReader ListReader::getStructElement(ElementCount index) const { KJ_REQUIRE(nestingLimit > 0, "Message is too deeply-nested or contains cycles. See capnp::ReaderOptions.") { return StructReader(); } auto indexBit = upgradeBound(index) * step; const byte* structData = ptr + indexBit / BITS_PER_BYTE; const WirePointer* structPointers = reinterpret_cast(structData + structDataSize / BITS_PER_BYTE); KJ_DASSERT(indexBit % BITS_PER_BYTE == ZERO * BITS); return StructReader( segment, capTable, structData, structPointers, structDataSize, structPointerCount, nestingLimit - 1); } MessageSizeCounts ListReader::totalSize() const { // TODO(cleanup): This is kind of a lot of logic duplicated from WireHelpers::totalSize(), but // it's unclear how to share it effectively. MessageSizeCounts result = { ZERO * WORDS, 0 }; switch (elementSize) { case ElementSize::VOID: // Nothing. break; case ElementSize::BIT: case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: result.addWords(WireHelpers::roundBitsUpToWords( upgradeBound(elementCount) * dataBitsPerElement(elementSize))); break; case ElementSize::POINTER: { auto count = elementCount * (POINTERS / ELEMENTS); result.addWords(count * WORDS_PER_POINTER); for (auto i: kj::zeroTo(count)) { result += WireHelpers::totalSize(segment, reinterpret_cast(ptr) + i, nestingLimit); } break; } case ElementSize::INLINE_COMPOSITE: { // Don't forget to count the tag word. auto wordSize = upgradeBound(elementCount) * step / BITS_PER_WORD; result.addWords(wordSize + POINTER_SIZE_IN_WORDS); if (structPointerCount > ZERO * POINTERS) { const word* pos = reinterpret_cast(ptr); for (auto i KJ_UNUSED: kj::zeroTo(elementCount)) { pos += structDataSize / BITS_PER_WORD; for (auto j KJ_UNUSED: kj::zeroTo(structPointerCount)) { result += WireHelpers::totalSize(segment, reinterpret_cast(pos), nestingLimit); pos += POINTER_SIZE_IN_WORDS; } } } break; } } if (segment != nullptr) { // This traversal should not count against the read limit, because it's highly likely that // the caller is going to traverse the object again, e.g. to copy it. segment->unread(result.wordCount); } return result; } CapTableReader* ListReader::getCapTable() { return capTable; } ListReader ListReader::imbue(CapTableReader* capTable) const { auto result = *this; result.capTable = capTable; return result; } bool ListReader::isCanonical(const word **readHead, const WirePointer *ref) { switch (this->getElementSize()) { case ElementSize::INLINE_COMPOSITE: { *readHead += 1; if (reinterpret_cast(this->ptr) != *readHead) { // The next word to read is the tag word, but the pointer is in // front of it, so our check is slightly different return false; } if (this->structDataSize % BITS_PER_WORD != ZERO * BITS) { return false; } auto elementSize = StructSize(this->structDataSize / BITS_PER_WORD, this->structPointerCount).total() / ELEMENTS; auto totalSize = upgradeBound(this->elementCount) * elementSize; if (totalSize != ref->listRef.inlineCompositeWordCount()) { return false; } if (elementSize == ZERO * WORDS / ELEMENTS) { return true; } auto listEnd = *readHead + totalSize; auto pointerHead = listEnd; bool listDataTrunc = false; bool listPtrTrunc = false; for (auto ec: kj::zeroTo(this->elementCount)) { bool dataTrunc, ptrTrunc; if (!this->getStructElement(ec).isCanonical(readHead, &pointerHead, &dataTrunc, &ptrTrunc)) { return false; } listDataTrunc |= dataTrunc; listPtrTrunc |= ptrTrunc; } KJ_REQUIRE(*readHead == listEnd, *readHead, listEnd); *readHead = pointerHead; return listDataTrunc && listPtrTrunc; } case ElementSize::POINTER: { if (reinterpret_cast(this->ptr) != *readHead) { return false; } *readHead += this->elementCount * (POINTERS / ELEMENTS) * WORDS_PER_POINTER; for (auto ec: kj::zeroTo(this->elementCount)) { if (!this->getPointerElement(ec).isCanonical(readHead)) { return false; } } return true; } default: { if (reinterpret_cast(this->ptr) != *readHead) { return false; } auto bitSize = upgradeBound(this->elementCount) * dataBitsPerElement(this->elementSize); auto truncatedByteSize = bitSize / BITS_PER_BYTE; auto byteReadHead = reinterpret_cast(*readHead) + truncatedByteSize; auto readHeadEnd = *readHead + WireHelpers::roundBitsUpToWords(bitSize); auto leftoverBits = bitSize % BITS_PER_BYTE; if (leftoverBits > ZERO * BITS) { auto mask = ~((1 << unbound(leftoverBits / BITS)) - 1); if (mask & *byteReadHead) { return false; } byteReadHead += 1; } while (byteReadHead != reinterpret_cast(readHeadEnd)) { if (*byteReadHead != 0) { return false; } byteReadHead += 1; } *readHead = readHeadEnd; return true; } } KJ_UNREACHABLE; } // ======================================================================================= // OrphanBuilder OrphanBuilder OrphanBuilder::initStruct( BuilderArena* arena, CapTableBuilder* capTable, StructSize size) { OrphanBuilder result; StructBuilder builder = WireHelpers::initStructPointer( result.tagAsPtr(), nullptr, capTable, size, arena); result.segment = builder.segment; result.capTable = capTable; result.location = builder.getLocation(); return result; } OrphanBuilder OrphanBuilder::initList( BuilderArena* arena, CapTableBuilder* capTable, ElementCount elementCount, ElementSize elementSize) { OrphanBuilder result; ListBuilder builder = WireHelpers::initListPointer( result.tagAsPtr(), nullptr, capTable, elementCount, elementSize, arena); result.segment = builder.segment; result.capTable = capTable; result.location = builder.getLocation(); return result; } OrphanBuilder OrphanBuilder::initStructList( BuilderArena* arena, CapTableBuilder* capTable, ElementCount elementCount, StructSize elementSize) { OrphanBuilder result; ListBuilder builder = WireHelpers::initStructListPointer( result.tagAsPtr(), nullptr, capTable, elementCount, elementSize, arena); result.segment = builder.segment; result.capTable = capTable; result.location = builder.getLocation(); return result; } OrphanBuilder OrphanBuilder::initText( BuilderArena* arena, CapTableBuilder* capTable, ByteCount size) { OrphanBuilder result; auto allocation = WireHelpers::initTextPointer(result.tagAsPtr(), nullptr, capTable, assertMax(size, ThrowOverflow()), arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value.begin()); return result; } OrphanBuilder OrphanBuilder::initData( BuilderArena* arena, CapTableBuilder* capTable, ByteCount size) { OrphanBuilder result; auto allocation = WireHelpers::initDataPointer(result.tagAsPtr(), nullptr, capTable, assertMaxBits(size), arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value.begin()); return result; } OrphanBuilder OrphanBuilder::copy( BuilderArena* arena, CapTableBuilder* capTable, StructReader copyFrom) { OrphanBuilder result; auto allocation = WireHelpers::setStructPointer( nullptr, capTable, result.tagAsPtr(), copyFrom, arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value); return result; } OrphanBuilder OrphanBuilder::copy( BuilderArena* arena, CapTableBuilder* capTable, ListReader copyFrom) { OrphanBuilder result; auto allocation = WireHelpers::setListPointer( nullptr, capTable, result.tagAsPtr(), copyFrom, arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value); return result; } OrphanBuilder OrphanBuilder::copy( BuilderArena* arena, CapTableBuilder* capTable, PointerReader copyFrom) { OrphanBuilder result; auto allocation = WireHelpers::copyPointer( nullptr, capTable, result.tagAsPtr(), copyFrom.segment, copyFrom.capTable, copyFrom.pointer, copyFrom.nestingLimit, arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value); return result; } OrphanBuilder OrphanBuilder::copy( BuilderArena* arena, CapTableBuilder* capTable, Text::Reader copyFrom) { OrphanBuilder result; auto allocation = WireHelpers::setTextPointer( result.tagAsPtr(), nullptr, capTable, copyFrom, arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value.begin()); return result; } OrphanBuilder OrphanBuilder::copy( BuilderArena* arena, CapTableBuilder* capTable, Data::Reader copyFrom) { OrphanBuilder result; auto allocation = WireHelpers::setDataPointer( result.tagAsPtr(), nullptr, capTable, copyFrom, arena); result.segment = allocation.segment; result.capTable = capTable; result.location = reinterpret_cast(allocation.value.begin()); return result; } #if !CAPNP_LITE OrphanBuilder OrphanBuilder::copy( BuilderArena* arena, CapTableBuilder* capTable, kj::Own copyFrom) { OrphanBuilder result; WireHelpers::setCapabilityPointer(nullptr, capTable, result.tagAsPtr(), kj::mv(copyFrom)); result.segment = arena->getSegment(SegmentId(0)); result.capTable = capTable; result.location = &result.tag; // dummy to make location non-null return result; } #endif // !CAPNP_LITE OrphanBuilder OrphanBuilder::concat( BuilderArena* arena, CapTableBuilder* capTable, ElementSize elementSize, StructSize structSize, kj::ArrayPtr lists) { KJ_REQUIRE(lists.size() > 0, "Can't concat empty list "); // Find the overall element count and size. ListElementCount elementCount = ZERO * ELEMENTS; for (auto& list: lists) { elementCount = assertMaxBits(elementCount + list.elementCount, []() { KJ_FAIL_REQUIRE("concatenated list exceeds list size limit"); }); if (list.elementSize != elementSize) { // If element sizes don't all match, upgrade to struct list. KJ_REQUIRE(list.elementSize != ElementSize::BIT && elementSize != ElementSize::BIT, "can't upgrade bit lists to struct lists"); elementSize = ElementSize::INLINE_COMPOSITE; } structSize.data = kj::max(structSize.data, WireHelpers::roundBitsUpToWords(list.structDataSize)); structSize.pointers = kj::max(structSize.pointers, list.structPointerCount); } // Allocate the list. OrphanBuilder result; ListBuilder builder = (elementSize == ElementSize::INLINE_COMPOSITE) ? WireHelpers::initStructListPointer( result.tagAsPtr(), nullptr, capTable, elementCount, structSize, arena) : WireHelpers::initListPointer( result.tagAsPtr(), nullptr, capTable, elementCount, elementSize, arena); // Copy elements. switch (elementSize) { case ElementSize::INLINE_COMPOSITE: { ListElementCount pos = ZERO * ELEMENTS; for (auto& list: lists) { for (auto i: kj::zeroTo(list.size())) { builder.getStructElement(pos).copyContentFrom(list.getStructElement(i)); // assumeBits() safe because we checked total size earlier. pos = assumeBits(pos + ONE * ELEMENTS); } } break; } case ElementSize::POINTER: { ListElementCount pos = ZERO * ELEMENTS; for (auto& list: lists) { for (auto i: kj::zeroTo(list.size())) { builder.getPointerElement(pos).copyFrom(list.getPointerElement(i)); // assumeBits() safe because we checked total size earlier. pos = assumeBits(pos + ONE * ELEMENTS); } } break; } case ElementSize::BIT: { // It's difficult to memcpy() bits since a list could start or end mid-byte. For now we // do a slow, naive loop. Probably no one will ever care. ListElementCount pos = ZERO * ELEMENTS; for (auto& list: lists) { for (auto i: kj::zeroTo(list.size())) { builder.setDataElement(pos, list.getDataElement(i)); // assumeBits() safe because we checked total size earlier. pos = assumeBits(pos + ONE * ELEMENTS); } } break; } default: { // We know all the inputs are primitives with identical size because otherwise we would have // chosen INLINE_COMPOSITE. Therefore, we can safely use memcpy() here instead of copying // each element manually. byte* target = builder.ptr; auto step = builder.step / BITS_PER_BYTE; for (auto& list: lists) { auto count = step * upgradeBound(list.size()); WireHelpers::copyMemory(target, list.ptr, assumeBits(count)); target += count; } break; } } // Return orphan. result.segment = builder.segment; result.capTable = capTable; result.location = builder.getLocation(); return result; } OrphanBuilder OrphanBuilder::referenceExternalData(BuilderArena* arena, Data::Reader data) { KJ_REQUIRE(reinterpret_cast(data.begin()) % sizeof(void*) == 0, "Cannot referenceExternalData() that is not aligned."); auto checkedSize = assertMaxBits(bounded(data.size())); auto wordCount = WireHelpers::roundBytesUpToWords(checkedSize * BYTES); kj::ArrayPtr words(reinterpret_cast(data.begin()), unbound(wordCount / WORDS)); OrphanBuilder result; result.tagAsPtr()->setKindForOrphan(WirePointer::LIST); result.tagAsPtr()->listRef.set(ElementSize::BYTE, checkedSize * ELEMENTS); result.segment = arena->addExternalSegment(words); // External data cannot possibly contain capabilities. result.capTable = nullptr; // const_cast OK here because we will check whether the segment is writable when we try to get // a builder. result.location = const_cast(words.begin()); return result; } StructBuilder OrphanBuilder::asStruct(StructSize size) { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); StructBuilder result = WireHelpers::getWritableStructPointer( tagAsPtr(), location, segment, capTable, size, nullptr, segment->getArena()); // Watch out, the pointer could have been updated if the object had to be relocated. location = reinterpret_cast(result.data); return result; } ListBuilder OrphanBuilder::asList(ElementSize elementSize) { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); ListBuilder result = WireHelpers::getWritableListPointer( tagAsPtr(), location, segment, capTable, elementSize, nullptr, segment->getArena()); // Watch out, the pointer could have been updated if the object had to be relocated. // (Actually, currently this is not true for primitive lists, but let's not turn into a bug if // it changes!) location = result.getLocation(); return result; } ListBuilder OrphanBuilder::asStructList(StructSize elementSize) { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); ListBuilder result = WireHelpers::getWritableStructListPointer( tagAsPtr(), location, segment, capTable, elementSize, nullptr, segment->getArena()); // Watch out, the pointer could have been updated if the object had to be relocated. location = result.getLocation(); return result; } ListBuilder OrphanBuilder::asListAnySize() { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); ListBuilder result = WireHelpers::getWritableListPointerAnySize( tagAsPtr(), location, segment, capTable, nullptr, segment->getArena()); // Watch out, the pointer could have been updated if the object had to be relocated. location = result.getLocation(); return result; } Text::Builder OrphanBuilder::asText() { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); // Never relocates. return WireHelpers::getWritableTextPointer( tagAsPtr(), location, segment, capTable, nullptr, ZERO * BYTES); } Data::Builder OrphanBuilder::asData() { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); // Never relocates. return WireHelpers::getWritableDataPointer( tagAsPtr(), location, segment, capTable, nullptr, ZERO * BYTES); } StructReader OrphanBuilder::asStructReader(StructSize size) const { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); return WireHelpers::readStructPointer( segment, capTable, tagAsPtr(), location, nullptr, kj::maxValue); } ListReader OrphanBuilder::asListReader(ElementSize elementSize) const { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); return WireHelpers::readListPointer( segment, capTable, tagAsPtr(), location, nullptr, elementSize, kj::maxValue); } ListReader OrphanBuilder::asListReaderAnySize() const { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); return WireHelpers::readListPointer( segment, capTable, tagAsPtr(), location, nullptr, ElementSize::VOID /* dummy */, kj::maxValue); } #if !CAPNP_LITE kj::Own OrphanBuilder::asCapability() const { return WireHelpers::readCapabilityPointer(segment, capTable, tagAsPtr(), kj::maxValue); } #endif // !CAPNP_LITE Text::Reader OrphanBuilder::asTextReader() const { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); return WireHelpers::readTextPointer(segment, tagAsPtr(), location, nullptr, ZERO * BYTES); } Data::Reader OrphanBuilder::asDataReader() const { KJ_DASSERT(tagAsPtr()->isNull() == (location == nullptr)); return WireHelpers::readDataPointer(segment, tagAsPtr(), location, nullptr, ZERO * BYTES); } bool OrphanBuilder::truncate(ElementCount uncheckedSize, bool isText) { ListElementCount size = assertMaxBits(uncheckedSize, []() { KJ_FAIL_REQUIRE("requested list size is too large"); }); WirePointer* ref = tagAsPtr(); SegmentBuilder* segment = this->segment; word* target = WireHelpers::followFars(ref, location, segment); if (ref->isNull()) { // We don't know the right element size, so we can't resize this list. return size == ZERO * ELEMENTS; } KJ_REQUIRE(ref->kind() == WirePointer::LIST, "Can't truncate non-list.") { return false; } if (isText) { // Add space for the NUL terminator. size = assertMaxBits(size + ONE * ELEMENTS, []() { KJ_FAIL_REQUIRE("requested list size is too large"); }); } auto elementSize = ref->listRef.elementSize(); if (elementSize == ElementSize::INLINE_COMPOSITE) { auto oldWordCount = ref->listRef.inlineCompositeWordCount(); WirePointer* tag = reinterpret_cast(target); ++target; KJ_REQUIRE(tag->kind() == WirePointer::STRUCT, "INLINE_COMPOSITE lists of non-STRUCT type are not supported.") { return false; } StructSize structSize(tag->structRef.dataSize.get(), tag->structRef.ptrCount.get()); auto elementStep = structSize.total() / ELEMENTS; auto oldSize = tag->inlineCompositeListElementCount(); SegmentWordCount sizeWords = assertMaxBits( upgradeBound(size) * elementStep, []() { KJ_FAIL_ASSERT("requested list size too large to fit in message segment"); }); SegmentWordCount oldSizeWords = assertMaxBits( upgradeBound(oldSize) * elementStep, []() { KJ_FAIL_ASSERT("prior to truncate, list is larger than max segment size?"); }); word* newEndWord = target + sizeWords; word* oldEndWord = target + oldWordCount; if (size <= oldSize) { // Zero the trailing elements. for (auto i: kj::range(size, oldSize)) { // assumeBits() safe because we checked that both sizeWords and oldSizeWords are in-range // above. WireHelpers::zeroObject(segment, capTable, tag, target + assumeBits(upgradeBound(i) * elementStep)); } ref->listRef.setInlineComposite(sizeWords); tag->setKindAndInlineCompositeListElementCount(WirePointer::STRUCT, size); segment->tryTruncate(oldEndWord, newEndWord); } else if (newEndWord <= oldEndWord) { // Apparently the old list was over-allocated? The word count is more than needed to store // the elements. This is "valid" but shouldn't happen in practice unless someone is toying // with us. word* expectedEnd = target + oldSizeWords; KJ_ASSERT(newEndWord >= expectedEnd); WireHelpers::zeroMemory(expectedEnd, intervalLength(expectedEnd, newEndWord, MAX_SEGMENT_WORDS)); tag->setKindAndInlineCompositeListElementCount(WirePointer::STRUCT, size); } else { if (segment->tryExtend(oldEndWord, newEndWord)) { // Done in-place. Nothing else to do now; the new memory is already zero'd. ref->listRef.setInlineComposite(sizeWords); tag->setKindAndInlineCompositeListElementCount(WirePointer::STRUCT, size); } else { // Need to re-allocate and transfer. OrphanBuilder replacement = initStructList(segment->getArena(), capTable, size, structSize); ListBuilder newList = replacement.asStructList(structSize); for (auto i: kj::zeroTo(oldSize)) { // assumeBits() safe because we checked that both sizeWords and oldSizeWords are in-range // above. word* element = target + assumeBits(upgradeBound(i) * elementStep); newList.getStructElement(i).transferContentFrom( StructBuilder(segment, capTable, element, reinterpret_cast(element + structSize.data), structSize.data * BITS_PER_WORD, structSize.pointers)); } *this = kj::mv(replacement); } } } else if (elementSize == ElementSize::POINTER) { // TODO(cleanup): GCC won't let me declare this constexpr, claiming POINTERS is not constexpr, // but it is? const auto POINTERS_PER_ELEMENT = ONE * POINTERS / ELEMENTS; auto oldSize = ref->listRef.elementCount(); word* newEndWord = target + size * POINTERS_PER_ELEMENT * WORDS_PER_POINTER; word* oldEndWord = target + oldSize * POINTERS_PER_ELEMENT * WORDS_PER_POINTER; if (size <= oldSize) { // Zero the trailing elements. for (WirePointer* element = reinterpret_cast(newEndWord); element < reinterpret_cast(oldEndWord); ++element) { WireHelpers::zeroPointerAndFars(segment, element); } ref->listRef.set(ElementSize::POINTER, size); segment->tryTruncate(oldEndWord, newEndWord); } else { if (segment->tryExtend(oldEndWord, newEndWord)) { // Done in-place. Nothing else to do now; the new memory is already zero'd. ref->listRef.set(ElementSize::POINTER, size); } else { // Need to re-allocate and transfer. OrphanBuilder replacement = initList( segment->getArena(), capTable, size, ElementSize::POINTER); ListBuilder newList = replacement.asList(ElementSize::POINTER); WirePointer* oldPointers = reinterpret_cast(target); for (auto i: kj::zeroTo(oldSize)) { newList.getPointerElement(i).transferFrom( PointerBuilder(segment, capTable, oldPointers + i * POINTERS_PER_ELEMENT)); } *this = kj::mv(replacement); } } } else { auto oldSize = ref->listRef.elementCount(); auto step = dataBitsPerElement(elementSize); const auto MAX_STEP_BYTES = ONE * WORDS / ELEMENTS * BYTES_PER_WORD; word* newEndWord = target + WireHelpers::roundBitsUpToWords( upgradeBound(size) * step); word* oldEndWord = target + WireHelpers::roundBitsUpToWords( upgradeBound(oldSize) * step); if (size <= oldSize) { // When truncating text, we want to set the null terminator as well, so we'll do our zeroing // at the byte level. byte* begin = reinterpret_cast(target); byte* newEndByte = begin + WireHelpers::roundBitsUpToBytes( upgradeBound(size) * step) - isText; byte* oldEndByte = reinterpret_cast(oldEndWord); WireHelpers::zeroMemory(newEndByte, intervalLength(newEndByte, oldEndByte, MAX_LIST_ELEMENTS * MAX_STEP_BYTES)); ref->listRef.set(elementSize, size); segment->tryTruncate(oldEndWord, newEndWord); } else { // We're trying to extend, not truncate. if (segment->tryExtend(oldEndWord, newEndWord)) { // Done in-place. Nothing else to do now; the memory is already zero'd. ref->listRef.set(elementSize, size); } else { // Need to re-allocate and transfer. OrphanBuilder replacement = initList(segment->getArena(), capTable, size, elementSize); ListBuilder newList = replacement.asList(elementSize); auto words = WireHelpers::roundBitsUpToWords( dataBitsPerElement(elementSize) * upgradeBound(oldSize)); WireHelpers::copyMemory(reinterpret_cast(newList.ptr), target, words); *this = kj::mv(replacement); } } } return true; } void OrphanBuilder::truncate(ElementCount size, ElementSize elementSize) { if (!truncate(size, false)) { // assumeBits() safe since it's checked inside truncate() *this = initList(segment->getArena(), capTable, assumeBits(size), elementSize); } } void OrphanBuilder::truncate(ElementCount size, StructSize elementSize) { if (!truncate(size, false)) { // assumeBits() safe since it's checked inside truncate() *this = initStructList(segment->getArena(), capTable, assumeBits(size), elementSize); } } void OrphanBuilder::truncateText(ElementCount size) { if (!truncate(size, true)) { // assumeBits() safe since it's checked inside truncate() *this = initText(segment->getArena(), capTable, assumeBits(size) * (ONE * BYTES / ELEMENTS)); } } void OrphanBuilder::euthanize() { // Carefully catch any exceptions and rethrow them as recoverable exceptions since we may be in // a destructor. auto exception = kj::runCatchingExceptions([&]() { if (tagAsPtr()->isPositional()) { WireHelpers::zeroObject(segment, capTable, tagAsPtr(), location); } else { WireHelpers::zeroObject(segment, capTable, tagAsPtr()); } WireHelpers::zeroMemory(&tag, ONE * WORDS); segment = nullptr; location = nullptr; }); KJ_IF_MAYBE(e, exception) { kj::getExceptionCallback().onRecoverableException(kj::mv(*e)); } } } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/list.c++0000644000175000017500000000234113340402540020100 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "list.h" namespace capnp { } // namespace capnp capnproto-c++-0.8.0/src/capnp/any.c++0000644000175000017500000001731013340402540017716 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "any.h" #include #if !CAPNP_LITE #include "capability.h" #endif // !CAPNP_LITE namespace capnp { #if !CAPNP_LITE kj::Own PipelineHook::getPipelinedCap(kj::Array&& ops) { return getPipelinedCap(ops.asPtr()); } kj::Own AnyPointer::Reader::getPipelinedCap( kj::ArrayPtr ops) const { _::PointerReader pointer = reader; for (auto& op: ops) { switch (op.type) { case PipelineOp::Type::NOOP: break; case PipelineOp::Type::GET_POINTER_FIELD: pointer = pointer.getStruct(nullptr).getPointerField(bounded(op.pointerIndex) * POINTERS); break; } } return pointer.getCapability(); } AnyPointer::Pipeline AnyPointer::Pipeline::noop() { auto newOps = kj::heapArray(ops.size()); for (auto i: kj::indices(ops)) { newOps[i] = ops[i]; } return Pipeline(hook->addRef(), kj::mv(newOps)); } AnyPointer::Pipeline AnyPointer::Pipeline::getPointerField(uint16_t pointerIndex) { auto newOps = kj::heapArray(ops.size() + 1); for (auto i: kj::indices(ops)) { newOps[i] = ops[i]; } auto& newOp = newOps[ops.size()]; newOp.type = PipelineOp::GET_POINTER_FIELD; newOp.pointerIndex = pointerIndex; return Pipeline(hook->addRef(), kj::mv(newOps)); } kj::Own AnyPointer::Pipeline::asCap() { return hook->getPipelinedCap(ops); } #endif // !CAPNP_LITE Equality AnyStruct::Reader::equals(AnyStruct::Reader right) const { auto dataL = getDataSection(); size_t dataSizeL = dataL.size(); while(dataSizeL > 0 && dataL[dataSizeL - 1] == 0) { -- dataSizeL; } auto dataR = right.getDataSection(); size_t dataSizeR = dataR.size(); while(dataSizeR > 0 && dataR[dataSizeR - 1] == 0) { -- dataSizeR; } if(dataSizeL != dataSizeR) { return Equality::NOT_EQUAL; } if(0 != memcmp(dataL.begin(), dataR.begin(), dataSizeL)) { return Equality::NOT_EQUAL; } auto ptrsL = getPointerSection(); size_t ptrsSizeL = ptrsL.size(); while (ptrsSizeL > 0 && ptrsL[ptrsSizeL - 1].isNull()) { -- ptrsSizeL; } auto ptrsR = right.getPointerSection(); size_t ptrsSizeR = ptrsR.size(); while (ptrsSizeR > 0 && ptrsR[ptrsSizeR - 1].isNull()) { -- ptrsSizeR; } if(ptrsSizeL != ptrsSizeR) { return Equality::NOT_EQUAL; } size_t i = 0; auto eqResult = Equality::EQUAL; for (; i < ptrsSizeL; i++) { auto l = ptrsL[i]; auto r = ptrsR[i]; switch(l.equals(r)) { case Equality::EQUAL: break; case Equality::NOT_EQUAL: return Equality::NOT_EQUAL; case Equality::UNKNOWN_CONTAINS_CAPS: eqResult = Equality::UNKNOWN_CONTAINS_CAPS; break; default: KJ_UNREACHABLE; } } return eqResult; } kj::StringPtr KJ_STRINGIFY(Equality res) { switch(res) { case Equality::NOT_EQUAL: return "NOT_EQUAL"; case Equality::EQUAL: return "EQUAL"; case Equality::UNKNOWN_CONTAINS_CAPS: return "UNKNOWN_CONTAINS_CAPS"; } KJ_UNREACHABLE; } Equality AnyList::Reader::equals(AnyList::Reader right) const { if(size() != right.size()) { return Equality::NOT_EQUAL; } if (getElementSize() != right.getElementSize()) { return Equality::NOT_EQUAL; } auto eqResult = Equality::EQUAL; switch(getElementSize()) { case ElementSize::VOID: case ElementSize::BIT: case ElementSize::BYTE: case ElementSize::TWO_BYTES: case ElementSize::FOUR_BYTES: case ElementSize::EIGHT_BYTES: { size_t cmpSize = getRawBytes().size(); if (getElementSize() == ElementSize::BIT && size() % 8 != 0) { // The list does not end on a byte boundary. We need special handling for the final // byte because we only care about the bits that are actually elements of the list. uint8_t mask = (1 << (size() % 8)) - 1; // lowest size() bits set if ((getRawBytes()[cmpSize - 1] & mask) != (right.getRawBytes()[cmpSize - 1] & mask)) { return Equality::NOT_EQUAL; } cmpSize -= 1; } if (memcmp(getRawBytes().begin(), right.getRawBytes().begin(), cmpSize) == 0) { return Equality::EQUAL; } else { return Equality::NOT_EQUAL; } } case ElementSize::POINTER: case ElementSize::INLINE_COMPOSITE: { auto llist = as>(); auto rlist = right.as>(); for(size_t i = 0; i < size(); i++) { switch(llist[i].equals(rlist[i])) { case Equality::EQUAL: break; case Equality::NOT_EQUAL: return Equality::NOT_EQUAL; case Equality::UNKNOWN_CONTAINS_CAPS: eqResult = Equality::UNKNOWN_CONTAINS_CAPS; break; default: KJ_UNREACHABLE; } } return eqResult; } } KJ_UNREACHABLE; } Equality AnyPointer::Reader::equals(AnyPointer::Reader right) const { if(getPointerType() != right.getPointerType()) { return Equality::NOT_EQUAL; } switch(getPointerType()) { case PointerType::NULL_: return Equality::EQUAL; case PointerType::STRUCT: return getAs().equals(right.getAs()); case PointerType::LIST: return getAs().equals(right.getAs()); case PointerType::CAPABILITY: return Equality::UNKNOWN_CONTAINS_CAPS; } // There aren't currently any other types of pointers KJ_UNREACHABLE; } bool AnyPointer::Reader::operator==(AnyPointer::Reader right) const { switch(equals(right)) { case Equality::EQUAL: return true; case Equality::NOT_EQUAL: return false; case Equality::UNKNOWN_CONTAINS_CAPS: KJ_FAIL_REQUIRE( "operator== cannot determine equality of capabilities; use equals() instead if you need to handle this case"); } KJ_UNREACHABLE; } bool AnyStruct::Reader::operator==(AnyStruct::Reader right) const { switch(equals(right)) { case Equality::EQUAL: return true; case Equality::NOT_EQUAL: return false; case Equality::UNKNOWN_CONTAINS_CAPS: KJ_FAIL_REQUIRE( "operator== cannot determine equality of capabilities; use equals() instead if you need to handle this case"); } KJ_UNREACHABLE; } bool AnyList::Reader::operator==(AnyList::Reader right) const { switch(equals(right)) { case Equality::EQUAL: return true; case Equality::NOT_EQUAL: return false; case Equality::UNKNOWN_CONTAINS_CAPS: KJ_FAIL_REQUIRE( "operator== cannot determine equality of capabilities; use equals() instead if you need to handle this case"); } KJ_UNREACHABLE; } } // namespace capnp capnproto-c++-0.8.0/src/capnp/message.c++0000644000175000017500000002521413650101756020566 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_PRIVATE #include "message.h" #include #include "arena.h" #include "orphan.h" #include #include namespace capnp { namespace { class DummyCapTableReader: public _::CapTableReader { public: kj::Maybe> extractCap(uint index) override { #if CAPNP_LITE KJ_UNIMPLEMENTED("no cap tables in lite mode"); #else return nullptr; #endif } }; static KJ_CONSTEXPR(const) DummyCapTableReader dummyCapTableReader = DummyCapTableReader(); } // namespace MessageReader::MessageReader(ReaderOptions options): options(options), allocatedArena(false) {} MessageReader::~MessageReader() noexcept(false) { if (allocatedArena) { arena()->~ReaderArena(); } } bool MessageReader::isCanonical() { if (!allocatedArena) { static_assert(sizeof(_::ReaderArena) <= sizeof(arenaSpace), "arenaSpace is too small to hold a ReaderArena. Please increase it. This will break " "ABI compatibility."); kj::ctor(*arena(), this); allocatedArena = true; } _::SegmentReader *segment = arena()->tryGetSegment(_::SegmentId(0)); if (segment == NULL) { // The message has no segments return false; } if (arena()->tryGetSegment(_::SegmentId(1))) { // The message has more than one segment return false; } const word* readHead = segment->getStartPtr() + 1; bool rootIsCanonical = _::PointerReader::getRoot(segment, nullptr, segment->getStartPtr(), this->getOptions().nestingLimit) .isCanonical(&readHead); bool allWordsConsumed = segment->getOffsetTo(readHead) == segment->getSize(); return rootIsCanonical && allWordsConsumed; } size_t MessageReader::sizeInWords() { return arena()->sizeInWords(); } AnyPointer::Reader MessageReader::getRootInternal() { if (!allocatedArena) { static_assert(sizeof(_::ReaderArena) <= sizeof(arenaSpace), "arenaSpace is too small to hold a ReaderArena. Please increase it. This will break " "ABI compatibility."); kj::ctor(*arena(), this); allocatedArena = true; } _::SegmentReader* segment = arena()->tryGetSegment(_::SegmentId(0)); KJ_REQUIRE(segment != nullptr && segment->checkObject(segment->getStartPtr(), ONE * WORDS), "Message did not contain a root pointer.") { return AnyPointer::Reader(); } // const_cast here is safe because dummyCapTableReader has no state. return AnyPointer::Reader(_::PointerReader::getRoot( segment, const_cast(&dummyCapTableReader), segment->getStartPtr(), options.nestingLimit)); } // ------------------------------------------------------------------- MessageBuilder::MessageBuilder(): allocatedArena(false) {} MessageBuilder::~MessageBuilder() noexcept(false) { if (allocatedArena) { kj::dtor(*arena()); } } MessageBuilder::MessageBuilder(kj::ArrayPtr segments) : allocatedArena(false) { kj::ctor(*arena(), this, segments); allocatedArena = true; } _::SegmentBuilder* MessageBuilder::getRootSegment() { if (allocatedArena) { return arena()->getSegment(_::SegmentId(0)); } else { static_assert(sizeof(_::BuilderArena) <= sizeof(arenaSpace), "arenaSpace is too small to hold a BuilderArena. Please increase it."); kj::ctor(*arena(), this); allocatedArena = true; auto allocation = arena()->allocate(POINTER_SIZE_IN_WORDS); KJ_ASSERT(allocation.segment->getSegmentId() == _::SegmentId(0), "First allocated word of new arena was not in segment ID 0."); KJ_ASSERT(allocation.words == allocation.segment->getPtrUnchecked(ZERO * WORDS), "First allocated word of new arena was not the first word in its segment."); return allocation.segment; } } AnyPointer::Builder MessageBuilder::getRootInternal() { _::SegmentBuilder* rootSegment = getRootSegment(); return AnyPointer::Builder(_::PointerBuilder::getRoot( rootSegment, arena()->getLocalCapTable(), rootSegment->getPtrUnchecked(ZERO * WORDS))); } kj::ArrayPtr> MessageBuilder::getSegmentsForOutput() { if (allocatedArena) { return arena()->getSegmentsForOutput(); } else { return nullptr; } } Orphanage MessageBuilder::getOrphanage() { // We must ensure that the arena and root pointer have been allocated before the Orphanage // can be used. if (!allocatedArena) getRootSegment(); return Orphanage(arena(), arena()->getLocalCapTable()); } bool MessageBuilder::isCanonical() { _::SegmentReader *segment = getRootSegment(); if (segment == NULL) { // The message has no segments return false; } if (arena()->tryGetSegment(_::SegmentId(1))) { // The message has more than one segment return false; } const word* readHead = segment->getStartPtr() + 1; return _::PointerReader::getRoot(segment, nullptr, segment->getStartPtr(), kj::maxValue) .isCanonical(&readHead); } size_t MessageBuilder::sizeInWords() { return arena()->sizeInWords(); } kj::Own<_::CapTableBuilder> MessageBuilder::releaseBuiltinCapTable() { return arena()->releaseLocalCapTable(); } // ======================================================================================= SegmentArrayMessageReader::SegmentArrayMessageReader( kj::ArrayPtr> segments, ReaderOptions options) : MessageReader(options), segments(segments) {} SegmentArrayMessageReader::~SegmentArrayMessageReader() noexcept(false) {} kj::ArrayPtr SegmentArrayMessageReader::getSegment(uint id) { if (id < segments.size()) { return segments[id]; } else { return nullptr; } } // ------------------------------------------------------------------- MallocMessageBuilder::MallocMessageBuilder( uint firstSegmentWords, AllocationStrategy allocationStrategy) : nextSize(firstSegmentWords), allocationStrategy(allocationStrategy), ownFirstSegment(true), returnedFirstSegment(false), firstSegment(nullptr) {} MallocMessageBuilder::MallocMessageBuilder( kj::ArrayPtr firstSegment, AllocationStrategy allocationStrategy) : nextSize(firstSegment.size()), allocationStrategy(allocationStrategy), ownFirstSegment(false), returnedFirstSegment(false), firstSegment(firstSegment.begin()) { KJ_REQUIRE(firstSegment.size() > 0, "First segment size must be non-zero."); // Checking just the first word should catch most cases of failing to zero the segment. KJ_REQUIRE(*reinterpret_cast(firstSegment.begin()) == 0, "First segment must be zeroed."); } MallocMessageBuilder::~MallocMessageBuilder() noexcept(false) { if (returnedFirstSegment) { if (ownFirstSegment) { free(firstSegment); } else { // Must zero first segment. kj::ArrayPtr> segments = getSegmentsForOutput(); if (segments.size() > 0) { KJ_ASSERT(segments[0].begin() == firstSegment, "First segment in getSegmentsForOutput() is not the first segment allocated?"); memset(firstSegment, 0, segments[0].size() * sizeof(word)); } } for (void* ptr: moreSegments) { free(ptr); } } } kj::ArrayPtr MallocMessageBuilder::allocateSegment(uint minimumSize) { KJ_REQUIRE(bounded(minimumSize) * WORDS <= MAX_SEGMENT_WORDS, "MallocMessageBuilder asked to allocate segment above maximum serializable size."); KJ_ASSERT(bounded(nextSize) * WORDS <= MAX_SEGMENT_WORDS, "MallocMessageBuilder nextSize out of bounds."); if (!returnedFirstSegment && !ownFirstSegment) { kj::ArrayPtr result = kj::arrayPtr(reinterpret_cast(firstSegment), nextSize); if (result.size() >= minimumSize) { returnedFirstSegment = true; return result; } // If the provided first segment wasn't big enough, we discard it and proceed to allocate // our own. This never happens in practice since minimumSize is always 1 for the first // segment. ownFirstSegment = true; } uint size = kj::max(minimumSize, nextSize); void* result = calloc(size, sizeof(word)); if (result == nullptr) { KJ_FAIL_SYSCALL("calloc(size, sizeof(word))", ENOMEM, size); } if (!returnedFirstSegment) { firstSegment = result; returnedFirstSegment = true; // After the first segment, we want nextSize to equal the total size allocated so far. if (allocationStrategy == AllocationStrategy::GROW_HEURISTICALLY) nextSize = size; } else { moreSegments.add(result); if (allocationStrategy == AllocationStrategy::GROW_HEURISTICALLY) { // set nextSize = min(nextSize+size, MAX_SEGMENT_WORDS) // while protecting against possible overflow of (nextSize+size) nextSize = (size <= unbound(MAX_SEGMENT_WORDS / WORDS) - nextSize) ? nextSize + size : unbound(MAX_SEGMENT_WORDS / WORDS); } } return kj::arrayPtr(reinterpret_cast(result), size); } // ------------------------------------------------------------------- FlatMessageBuilder::FlatMessageBuilder(kj::ArrayPtr array): array(array), allocated(false) {} FlatMessageBuilder::~FlatMessageBuilder() noexcept(false) {} void FlatMessageBuilder::requireFilled() { KJ_REQUIRE(getSegmentsForOutput()[0].end() == array.end(), "FlatMessageBuilder's buffer was too large."); } kj::ArrayPtr FlatMessageBuilder::allocateSegment(uint minimumSize) { KJ_REQUIRE(!allocated, "FlatMessageBuilder's buffer was not large enough."); allocated = true; return array; } } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema.capnp.c++0000644000175000017500000051115413340402540021474 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: schema.capnp #include "schema.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<225> b_e682ab4cf923a417 = { { 0, 0, 0, 0, 5, 0, 6, 0, 23, 164, 35, 249, 76, 171, 130, 230, 19, 0, 0, 0, 1, 0, 5, 0, 217, 114, 76, 98, 9, 197, 63, 169, 6, 0, 7, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 23, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 0, 12, 0, 0, 0, 1, 0, 1, 0, 177, 163, 15, 241, 204, 27, 82, 185, 17, 0, 0, 0, 82, 0, 0, 0, 66, 194, 15, 250, 187, 85, 191, 222, 17, 0, 0, 0, 90, 0, 0, 0, 174, 87, 19, 4, 227, 29, 142, 243, 17, 0, 0, 0, 90, 0, 0, 0, 80, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 0, 0, 0, 0, 0, 78, 101, 115, 116, 101, 100, 78, 111, 100, 101, 0, 0, 0, 0, 0, 0, 83, 111, 117, 114, 99, 101, 73, 110, 102, 111, 0, 0, 0, 0, 0, 0, 56, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 1, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 1, 0, 0, 3, 0, 1, 0, 128, 1, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 125, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 1, 0, 0, 3, 0, 1, 0, 136, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 1, 0, 0, 194, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 1, 0, 0, 3, 0, 1, 0, 148, 1, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 1, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 1, 0, 0, 3, 0, 1, 0, 152, 1, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 1, 0, 0, 3, 0, 1, 0, 176, 1, 0, 0, 2, 0, 1, 0, 7, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 1, 0, 0, 3, 0, 1, 0, 200, 1, 0, 0, 2, 0, 1, 0, 8, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 1, 0, 0, 3, 0, 1, 0, 204, 1, 0, 0, 2, 0, 1, 0, 9, 0, 254, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 53, 68, 251, 55, 155, 177, 160, 158, 201, 1, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 253, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 152, 245, 51, 67, 54, 179, 74, 181, 177, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 252, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 143, 33, 194, 240, 207, 83, 39, 232, 153, 1, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 251, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 32, 148, 13, 122, 172, 165, 138, 177, 133, 1, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 250, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 144, 2, 10, 64, 212, 25, 22, 236, 109, 1, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 1, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 1, 0, 0, 3, 0, 1, 0, 116, 1, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 32, 1, 0, 0, 0, 0, 1, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 1, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 1, 0, 0, 3, 0, 1, 0, 124, 1, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 105, 115, 112, 108, 97, 121, 78, 97, 109, 101, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 105, 115, 112, 108, 97, 121, 78, 97, 109, 101, 80, 114, 101, 102, 105, 120, 76, 101, 110, 103, 116, 104, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 99, 111, 112, 101, 73, 100, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 101, 115, 116, 101, 100, 78, 111, 100, 101, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 66, 194, 15, 250, 187, 85, 191, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 66, 117, 37, 171, 13, 149, 200, 241, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 105, 108, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 117, 99, 116, 0, 0, 101, 110, 117, 109, 0, 0, 0, 0, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 99, 111, 110, 115, 116, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 101, 116, 101, 114, 115, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 177, 163, 15, 241, 204, 27, 82, 185, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 115, 71, 101, 110, 101, 114, 105, 99, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e682ab4cf923a417 = b_e682ab4cf923a417.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_e682ab4cf923a417[] = { &s_9ea0b19b37fb4435, &s_b18aa5ac7a0d9420, &s_b54ab3364333f598, &s_b9521bccf10fa3b1, &s_debf55bbfa0fc242, &s_e82753cff0c2218f, &s_ec1619d4400a0290, &s_f1c8950dab257542, }; static const uint16_t m_e682ab4cf923a417[] = {11, 5, 10, 1, 2, 8, 6, 0, 9, 13, 4, 12, 3, 7}; static const uint16_t i_e682ab4cf923a417[] = {6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12, 13}; const ::capnp::_::RawSchema s_e682ab4cf923a417 = { 0xe682ab4cf923a417, b_e682ab4cf923a417.words, 225, d_e682ab4cf923a417, m_e682ab4cf923a417, 8, 14, i_e682ab4cf923a417, nullptr, nullptr, { &s_e682ab4cf923a417, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<34> b_b9521bccf10fa3b1 = { { 0, 0, 0, 0, 5, 0, 6, 0, 177, 163, 15, 241, 204, 27, 82, 185, 24, 0, 0, 0, 1, 0, 0, 0, 23, 164, 35, 249, 76, 171, 130, 230, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 18, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 80, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b9521bccf10fa3b1 = b_b9521bccf10fa3b1.words; #if !CAPNP_LITE static const uint16_t m_b9521bccf10fa3b1[] = {0}; static const uint16_t i_b9521bccf10fa3b1[] = {0}; const ::capnp::_::RawSchema s_b9521bccf10fa3b1 = { 0xb9521bccf10fa3b1, b_b9521bccf10fa3b1.words, 34, nullptr, m_b9521bccf10fa3b1, 0, 1, i_b9521bccf10fa3b1, nullptr, nullptr, { &s_b9521bccf10fa3b1, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<49> b_debf55bbfa0fc242 = { { 0, 0, 0, 0, 5, 0, 6, 0, 66, 194, 15, 250, 187, 85, 191, 222, 24, 0, 0, 0, 1, 0, 1, 0, 23, 164, 35, 249, 76, 171, 130, 230, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 78, 101, 115, 116, 101, 100, 78, 111, 100, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_debf55bbfa0fc242 = b_debf55bbfa0fc242.words; #if !CAPNP_LITE static const uint16_t m_debf55bbfa0fc242[] = {1, 0}; static const uint16_t i_debf55bbfa0fc242[] = {0, 1}; const ::capnp::_::RawSchema s_debf55bbfa0fc242 = { 0xdebf55bbfa0fc242, b_debf55bbfa0fc242.words, 49, nullptr, m_debf55bbfa0fc242, 0, 2, i_debf55bbfa0fc242, nullptr, nullptr, { &s_debf55bbfa0fc242, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<72> b_f38e1de3041357ae = { { 0, 0, 0, 0, 5, 0, 6, 0, 174, 87, 19, 4, 227, 29, 142, 243, 24, 0, 0, 0, 1, 0, 1, 0, 23, 164, 35, 249, 76, 171, 130, 230, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 37, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 83, 111, 117, 114, 99, 101, 73, 110, 102, 111, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 1, 0, 162, 31, 142, 137, 56, 144, 186, 194, 1, 0, 0, 0, 58, 0, 0, 0, 77, 101, 109, 98, 101, 114, 0, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 104, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 111, 99, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 101, 109, 98, 101, 114, 115, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 162, 31, 142, 137, 56, 144, 186, 194, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f38e1de3041357ae = b_f38e1de3041357ae.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_f38e1de3041357ae[] = { &s_c2ba9038898e1fa2, }; static const uint16_t m_f38e1de3041357ae[] = {1, 0, 2}; static const uint16_t i_f38e1de3041357ae[] = {0, 1, 2}; const ::capnp::_::RawSchema s_f38e1de3041357ae = { 0xf38e1de3041357ae, b_f38e1de3041357ae.words, 72, d_f38e1de3041357ae, m_f38e1de3041357ae, 1, 3, i_f38e1de3041357ae, nullptr, nullptr, { &s_f38e1de3041357ae, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<36> b_c2ba9038898e1fa2 = { { 0, 0, 0, 0, 5, 0, 6, 0, 162, 31, 142, 137, 56, 144, 186, 194, 35, 0, 0, 0, 1, 0, 0, 0, 174, 87, 19, 4, 227, 29, 142, 243, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 82, 1, 0, 0, 41, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 83, 111, 117, 114, 99, 101, 73, 110, 102, 111, 46, 77, 101, 109, 98, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 24, 0, 0, 0, 2, 0, 1, 0, 100, 111, 99, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c2ba9038898e1fa2 = b_c2ba9038898e1fa2.words; #if !CAPNP_LITE static const uint16_t m_c2ba9038898e1fa2[] = {0}; static const uint16_t i_c2ba9038898e1fa2[] = {0}; const ::capnp::_::RawSchema s_c2ba9038898e1fa2 = { 0xc2ba9038898e1fa2, b_c2ba9038898e1fa2.words, 36, nullptr, m_c2ba9038898e1fa2, 0, 1, i_c2ba9038898e1fa2, nullptr, nullptr, { &s_c2ba9038898e1fa2, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<134> b_9ea0b19b37fb4435 = { { 0, 0, 0, 0, 5, 0, 6, 0, 53, 68, 251, 55, 155, 177, 160, 158, 24, 0, 0, 0, 1, 0, 5, 0, 23, 164, 35, 249, 76, 171, 130, 230, 6, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 143, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 115, 116, 114, 117, 99, 116, 0, 0, 28, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 0, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 180, 0, 0, 0, 3, 0, 1, 0, 192, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 12, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 188, 0, 0, 0, 3, 0, 1, 0, 200, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 13, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 0, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 3, 0, 1, 0, 212, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 224, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 209, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 0, 0, 0, 3, 0, 1, 0, 216, 0, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 15, 0, 0, 0, 0, 0, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 0, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 0, 0, 0, 3, 0, 1, 0, 228, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 8, 0, 0, 0, 0, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 225, 0, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 228, 0, 0, 0, 3, 0, 1, 0, 240, 0, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 0, 0, 0, 3, 0, 1, 0, 4, 1, 0, 0, 2, 0, 1, 0, 100, 97, 116, 97, 87, 111, 114, 100, 67, 111, 117, 110, 116, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 111, 105, 110, 116, 101, 114, 67, 111, 117, 110, 116, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 114, 101, 102, 101, 114, 114, 101, 100, 76, 105, 115, 116, 69, 110, 99, 111, 100, 105, 110, 103, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 38, 25, 82, 186, 125, 143, 149, 209, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 115, 71, 114, 111, 117, 112, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 105, 115, 99, 114, 105, 109, 105, 110, 97, 110, 116, 67, 111, 117, 110, 116, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 105, 115, 99, 114, 105, 109, 105, 110, 97, 110, 116, 79, 102, 102, 115, 101, 116, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 105, 101, 108, 100, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 95, 244, 74, 31, 164, 80, 173, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9ea0b19b37fb4435 = b_9ea0b19b37fb4435.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9ea0b19b37fb4435[] = { &s_9aad50a41f4af45f, &s_d1958f7dba521926, &s_e682ab4cf923a417, }; static const uint16_t m_9ea0b19b37fb4435[] = {0, 4, 5, 6, 3, 1, 2}; static const uint16_t i_9ea0b19b37fb4435[] = {0, 1, 2, 3, 4, 5, 6}; const ::capnp::_::RawSchema s_9ea0b19b37fb4435 = { 0x9ea0b19b37fb4435, b_9ea0b19b37fb4435.words, 134, d_9ea0b19b37fb4435, m_9ea0b19b37fb4435, 3, 7, i_9ea0b19b37fb4435, nullptr, nullptr, { &s_9ea0b19b37fb4435, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<37> b_b54ab3364333f598 = { { 0, 0, 0, 0, 5, 0, 6, 0, 152, 245, 51, 67, 54, 179, 74, 181, 24, 0, 0, 0, 1, 0, 5, 0, 23, 164, 35, 249, 76, 171, 130, 230, 6, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 234, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 101, 110, 117, 109, 0, 0, 0, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 40, 0, 0, 0, 2, 0, 1, 0, 101, 110, 117, 109, 101, 114, 97, 110, 116, 115, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 77, 154, 84, 220, 235, 124, 138, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b54ab3364333f598 = b_b54ab3364333f598.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_b54ab3364333f598[] = { &s_978a7cebdc549a4d, &s_e682ab4cf923a417, }; static const uint16_t m_b54ab3364333f598[] = {0}; static const uint16_t i_b54ab3364333f598[] = {0}; const ::capnp::_::RawSchema s_b54ab3364333f598 = { 0xb54ab3364333f598, b_b54ab3364333f598.words, 37, d_b54ab3364333f598, m_b54ab3364333f598, 2, 1, i_b54ab3364333f598, nullptr, nullptr, { &s_b54ab3364333f598, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<57> b_e82753cff0c2218f = { { 0, 0, 0, 0, 5, 0, 6, 0, 143, 33, 194, 240, 207, 83, 39, 232, 24, 0, 0, 0, 1, 0, 5, 0, 23, 164, 35, 249, 76, 171, 130, 230, 6, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 18, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 64, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 3, 0, 1, 0, 88, 0, 0, 0, 2, 0, 1, 0, 109, 101, 116, 104, 111, 100, 115, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 128, 77, 51, 59, 226, 204, 0, 149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 117, 112, 101, 114, 99, 108, 97, 115, 115, 101, 115, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 248, 215, 164, 208, 158, 42, 150, 169, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_e82753cff0c2218f = b_e82753cff0c2218f.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_e82753cff0c2218f[] = { &s_9500cce23b334d80, &s_a9962a9ed0a4d7f8, &s_e682ab4cf923a417, }; static const uint16_t m_e82753cff0c2218f[] = {0, 1}; static const uint16_t i_e82753cff0c2218f[] = {0, 1}; const ::capnp::_::RawSchema s_e82753cff0c2218f = { 0xe82753cff0c2218f, b_e82753cff0c2218f.words, 57, d_e82753cff0c2218f, m_e82753cff0c2218f, 3, 2, i_e82753cff0c2218f, nullptr, nullptr, { &s_e82753cff0c2218f, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<47> b_b18aa5ac7a0d9420 = { { 0, 0, 0, 0, 5, 0, 6, 0, 32, 148, 13, 122, 172, 165, 138, 177, 24, 0, 0, 0, 1, 0, 5, 0, 23, 164, 35, 249, 76, 171, 130, 230, 6, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 99, 111, 110, 115, 116, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 96, 204, 249, 225, 237, 120, 115, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 155, 12, 176, 215, 210, 220, 35, 206, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_b18aa5ac7a0d9420 = b_b18aa5ac7a0d9420.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_b18aa5ac7a0d9420[] = { &s_ce23dcd2d7b00c9b, &s_d07378ede1f9cc60, &s_e682ab4cf923a417, }; static const uint16_t m_b18aa5ac7a0d9420[] = {0, 1}; static const uint16_t i_b18aa5ac7a0d9420[] = {0, 1}; const ::capnp::_::RawSchema s_b18aa5ac7a0d9420 = { 0xb18aa5ac7a0d9420, b_b18aa5ac7a0d9420.words, 47, d_b18aa5ac7a0d9420, m_b18aa5ac7a0d9420, 3, 2, i_b18aa5ac7a0d9420, nullptr, nullptr, { &s_b18aa5ac7a0d9420, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<228> b_ec1619d4400a0290 = { { 0, 0, 0, 0, 5, 0, 6, 0, 144, 2, 10, 64, 212, 25, 22, 236, 24, 0, 0, 0, 1, 0, 5, 0, 23, 164, 35, 249, 76, 171, 130, 230, 6, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 223, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 78, 111, 100, 101, 46, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 1, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 1, 0, 0, 3, 0, 1, 0, 100, 1, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 112, 0, 0, 0, 0, 0, 1, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 1, 0, 0, 3, 0, 1, 0, 108, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 113, 0, 0, 0, 0, 0, 1, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 1, 0, 0, 3, 0, 1, 0, 116, 1, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 114, 0, 0, 0, 0, 0, 1, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 1, 0, 0, 3, 0, 1, 0, 124, 1, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 115, 0, 0, 0, 0, 0, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121, 1, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124, 1, 0, 0, 3, 0, 1, 0, 136, 1, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 116, 0, 0, 0, 0, 0, 1, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 133, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 1, 0, 0, 3, 0, 1, 0, 144, 1, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 117, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 1, 0, 0, 3, 0, 1, 0, 152, 1, 0, 0, 2, 0, 1, 0, 7, 0, 0, 0, 118, 0, 0, 0, 0, 0, 1, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 1, 0, 0, 3, 0, 1, 0, 160, 1, 0, 0, 2, 0, 1, 0, 8, 0, 0, 0, 119, 0, 0, 0, 0, 0, 1, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 157, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 156, 1, 0, 0, 3, 0, 1, 0, 168, 1, 0, 0, 2, 0, 1, 0, 9, 0, 0, 0, 120, 0, 0, 0, 0, 0, 1, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 1, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 1, 0, 0, 3, 0, 1, 0, 180, 1, 0, 0, 2, 0, 1, 0, 10, 0, 0, 0, 121, 0, 0, 0, 0, 0, 1, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177, 1, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 1, 0, 0, 3, 0, 1, 0, 188, 1, 0, 0, 2, 0, 1, 0, 11, 0, 0, 0, 122, 0, 0, 0, 0, 0, 1, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 1, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 1, 0, 0, 3, 0, 1, 0, 196, 1, 0, 0, 2, 0, 1, 0, 12, 0, 0, 0, 123, 0, 0, 0, 0, 0, 1, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 1, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 1, 0, 0, 3, 0, 1, 0, 208, 1, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 96, 204, 249, 225, 237, 120, 115, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 70, 105, 108, 101, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 67, 111, 110, 115, 116, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 69, 110, 117, 109, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 69, 110, 117, 109, 101, 114, 97, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 83, 116, 114, 117, 99, 116, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 70, 105, 101, 108, 100, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 85, 110, 105, 111, 110, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 71, 114, 111, 117, 112, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 73, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 77, 101, 116, 104, 111, 100, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 80, 97, 114, 97, 109, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 97, 114, 103, 101, 116, 115, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ec1619d4400a0290 = b_ec1619d4400a0290.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_ec1619d4400a0290[] = { &s_d07378ede1f9cc60, &s_e682ab4cf923a417, }; static const uint16_t m_ec1619d4400a0290[] = {12, 2, 3, 4, 6, 1, 8, 9, 10, 11, 5, 7, 0}; static const uint16_t i_ec1619d4400a0290[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; const ::capnp::_::RawSchema s_ec1619d4400a0290 = { 0xec1619d4400a0290, b_ec1619d4400a0290.words, 228, d_ec1619d4400a0290, m_ec1619d4400a0290, 2, 13, i_ec1619d4400a0290, nullptr, nullptr, { &s_ec1619d4400a0290, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<114> b_9aad50a41f4af45f = { { 0, 0, 0, 0, 5, 0, 6, 0, 95, 244, 74, 31, 164, 80, 173, 154, 19, 0, 0, 0, 1, 0, 3, 0, 217, 114, 76, 98, 9, 197, 63, 169, 4, 0, 7, 0, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 202, 0, 0, 0, 33, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 143, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 70, 105, 101, 108, 100, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 1, 0, 18, 199, 254, 124, 190, 76, 177, 151, 1, 0, 0, 0, 122, 0, 0, 0, 110, 111, 68, 105, 115, 99, 114, 105, 109, 105, 110, 97, 110, 116, 0, 0, 28, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 0, 3, 0, 1, 0, 188, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 3, 0, 1, 0, 196, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 0, 0, 0, 3, 0, 1, 0, 220, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 217, 0, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 220, 0, 0, 0, 3, 0, 1, 0, 232, 0, 0, 0, 2, 0, 1, 0, 4, 0, 255, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 111, 116, 180, 107, 71, 5, 35, 196, 229, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 254, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 17, 29, 219, 104, 219, 205, 252, 202, 205, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 230, 11, 135, 135, 194, 213, 144, 187, 181, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 111, 100, 101, 79, 114, 100, 101, 114, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 66, 117, 37, 171, 13, 149, 200, 241, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 105, 115, 99, 114, 105, 109, 105, 110, 97, 110, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 108, 111, 116, 0, 0, 0, 0, 103, 114, 111, 117, 112, 0, 0, 0, 111, 114, 100, 105, 110, 97, 108, 0, } }; ::capnp::word const* const bp_9aad50a41f4af45f = b_9aad50a41f4af45f.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9aad50a41f4af45f[] = { &s_bb90d5c287870be6, &s_c42305476bb4746f, &s_cafccddb68db1d11, &s_f1c8950dab257542, }; static const uint16_t m_9aad50a41f4af45f[] = {2, 1, 3, 5, 0, 6, 4}; static const uint16_t i_9aad50a41f4af45f[] = {4, 5, 0, 1, 2, 3, 6}; const ::capnp::_::RawSchema s_9aad50a41f4af45f = { 0x9aad50a41f4af45f, b_9aad50a41f4af45f.words, 114, d_9aad50a41f4af45f, m_9aad50a41f4af45f, 4, 7, i_9aad50a41f4af45f, nullptr, nullptr, { &s_9aad50a41f4af45f, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<25> b_97b14cbe7cfec712 = { { 0, 0, 0, 0, 5, 0, 6, 0, 18, 199, 254, 124, 190, 76, 177, 151, 25, 0, 0, 0, 4, 0, 0, 0, 95, 244, 74, 31, 164, 80, 173, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 66, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 3, 0, 1, 0, 44, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 70, 105, 101, 108, 100, 46, 110, 111, 68, 105, 115, 99, 114, 105, 109, 105, 110, 97, 110, 116, 0, 0, 0, 0, 0, 1, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_97b14cbe7cfec712 = b_97b14cbe7cfec712.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_97b14cbe7cfec712 = { 0x97b14cbe7cfec712, b_97b14cbe7cfec712.words, 25, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_97b14cbe7cfec712, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<80> b_c42305476bb4746f = { { 0, 0, 0, 0, 5, 0, 6, 0, 111, 116, 180, 107, 71, 5, 35, 196, 25, 0, 0, 0, 1, 0, 3, 0, 95, 244, 74, 31, 164, 80, 173, 154, 4, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 70, 105, 101, 108, 100, 46, 115, 108, 111, 116, 0, 0, 0, 16, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 3, 0, 1, 0, 104, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 3, 0, 1, 0, 108, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 3, 0, 1, 0, 116, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 128, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 0, 0, 0, 3, 0, 1, 0, 128, 0, 0, 0, 2, 0, 1, 0, 111, 102, 102, 115, 101, 116, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 96, 204, 249, 225, 237, 120, 115, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 101, 102, 97, 117, 108, 116, 86, 97, 108, 117, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 155, 12, 176, 215, 210, 220, 35, 206, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 97, 100, 69, 120, 112, 108, 105, 99, 105, 116, 68, 101, 102, 97, 117, 108, 116, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c42305476bb4746f = b_c42305476bb4746f.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c42305476bb4746f[] = { &s_9aad50a41f4af45f, &s_ce23dcd2d7b00c9b, &s_d07378ede1f9cc60, }; static const uint16_t m_c42305476bb4746f[] = {2, 3, 0, 1}; static const uint16_t i_c42305476bb4746f[] = {0, 1, 2, 3}; const ::capnp::_::RawSchema s_c42305476bb4746f = { 0xc42305476bb4746f, b_c42305476bb4746f.words, 80, d_c42305476bb4746f, m_c42305476bb4746f, 3, 4, i_c42305476bb4746f, nullptr, nullptr, { &s_c42305476bb4746f, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<32> b_cafccddb68db1d11 = { { 0, 0, 0, 0, 5, 0, 6, 0, 17, 29, 219, 104, 219, 205, 252, 202, 25, 0, 0, 0, 1, 0, 3, 0, 95, 244, 74, 31, 164, 80, 173, 154, 4, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 70, 105, 101, 108, 100, 46, 103, 114, 111, 117, 112, 0, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 20, 0, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 73, 100, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_cafccddb68db1d11 = b_cafccddb68db1d11.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_cafccddb68db1d11[] = { &s_9aad50a41f4af45f, }; static const uint16_t m_cafccddb68db1d11[] = {0}; static const uint16_t i_cafccddb68db1d11[] = {0}; const ::capnp::_::RawSchema s_cafccddb68db1d11 = { 0xcafccddb68db1d11, b_cafccddb68db1d11.words, 32, d_cafccddb68db1d11, m_cafccddb68db1d11, 1, 1, i_cafccddb68db1d11, nullptr, nullptr, { &s_cafccddb68db1d11, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<50> b_bb90d5c287870be6 = { { 0, 0, 0, 0, 5, 0, 6, 0, 230, 11, 135, 135, 194, 213, 144, 187, 25, 0, 0, 0, 1, 0, 3, 0, 95, 244, 74, 31, 164, 80, 173, 154, 4, 0, 7, 0, 1, 0, 2, 0, 5, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 70, 105, 101, 108, 100, 46, 111, 114, 100, 105, 110, 97, 108, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 6, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 3, 0, 1, 0, 60, 0, 0, 0, 2, 0, 1, 0, 105, 109, 112, 108, 105, 99, 105, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 120, 112, 108, 105, 99, 105, 116, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_bb90d5c287870be6 = b_bb90d5c287870be6.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_bb90d5c287870be6[] = { &s_9aad50a41f4af45f, }; static const uint16_t m_bb90d5c287870be6[] = {1, 0}; static const uint16_t i_bb90d5c287870be6[] = {0, 1}; const ::capnp::_::RawSchema s_bb90d5c287870be6 = { 0xbb90d5c287870be6, b_bb90d5c287870be6.words, 50, d_bb90d5c287870be6, m_bb90d5c287870be6, 1, 2, i_bb90d5c287870be6, nullptr, nullptr, { &s_bb90d5c287870be6, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<69> b_978a7cebdc549a4d = { { 0, 0, 0, 0, 5, 0, 6, 0, 77, 154, 84, 220, 235, 124, 138, 151, 19, 0, 0, 0, 1, 0, 1, 0, 217, 114, 76, 98, 9, 197, 63, 169, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 234, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 69, 110, 117, 109, 101, 114, 97, 110, 116, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 3, 0, 1, 0, 108, 0, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 111, 100, 101, 79, 114, 100, 101, 114, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 66, 117, 37, 171, 13, 149, 200, 241, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_978a7cebdc549a4d = b_978a7cebdc549a4d.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_978a7cebdc549a4d[] = { &s_f1c8950dab257542, }; static const uint16_t m_978a7cebdc549a4d[] = {2, 1, 0}; static const uint16_t i_978a7cebdc549a4d[] = {0, 1, 2}; const ::capnp::_::RawSchema s_978a7cebdc549a4d = { 0x978a7cebdc549a4d, b_978a7cebdc549a4d.words, 69, d_978a7cebdc549a4d, m_978a7cebdc549a4d, 1, 3, i_978a7cebdc549a4d, nullptr, nullptr, { &s_978a7cebdc549a4d, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<48> b_a9962a9ed0a4d7f8 = { { 0, 0, 0, 0, 5, 0, 6, 0, 248, 215, 164, 208, 158, 42, 150, 169, 19, 0, 0, 0, 1, 0, 1, 0, 217, 114, 76, 98, 9, 197, 63, 169, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 83, 117, 112, 101, 114, 99, 108, 97, 115, 115, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 114, 97, 110, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_a9962a9ed0a4d7f8 = b_a9962a9ed0a4d7f8.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_a9962a9ed0a4d7f8[] = { &s_903455f06065422b, }; static const uint16_t m_a9962a9ed0a4d7f8[] = {1, 0}; static const uint16_t i_a9962a9ed0a4d7f8[] = {0, 1}; const ::capnp::_::RawSchema s_a9962a9ed0a4d7f8 = { 0xa9962a9ed0a4d7f8, b_a9962a9ed0a4d7f8.words, 48, d_a9962a9ed0a4d7f8, m_a9962a9ed0a4d7f8, 1, 2, i_a9962a9ed0a4d7f8, nullptr, nullptr, { &s_a9962a9ed0a4d7f8, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<155> b_9500cce23b334d80 = { { 0, 0, 0, 0, 5, 0, 6, 0, 128, 77, 51, 59, 226, 204, 0, 149, 19, 0, 0, 0, 1, 0, 3, 0, 217, 114, 76, 98, 9, 197, 63, 169, 5, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 210, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 199, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 77, 101, 116, 104, 111, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 32, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 209, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 0, 0, 0, 3, 0, 1, 0, 216, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 212, 0, 0, 0, 3, 0, 1, 0, 224, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 221, 0, 0, 0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 220, 0, 0, 0, 3, 0, 1, 0, 232, 0, 0, 0, 2, 0, 1, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 0, 0, 0, 3, 0, 1, 0, 244, 0, 0, 0, 2, 0, 1, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 241, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 0, 0, 0, 3, 0, 1, 0, 12, 1, 0, 0, 2, 0, 1, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1, 0, 0, 3, 0, 1, 0, 20, 1, 0, 0, 2, 0, 1, 0, 6, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 1, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 1, 0, 0, 3, 0, 1, 0, 28, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 1, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 1, 0, 0, 3, 0, 1, 0, 56, 1, 0, 0, 2, 0, 1, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 111, 100, 101, 79, 114, 100, 101, 114, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 83, 116, 114, 117, 99, 116, 84, 121, 112, 101, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 115, 117, 108, 116, 83, 116, 114, 117, 99, 116, 84, 121, 112, 101, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 110, 111, 116, 97, 116, 105, 111, 110, 115, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 66, 117, 37, 171, 13, 149, 200, 241, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 66, 114, 97, 110, 100, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 115, 117, 108, 116, 66, 114, 97, 110, 100, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 109, 112, 108, 105, 99, 105, 116, 80, 97, 114, 97, 109, 101, 116, 101, 114, 115, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 177, 163, 15, 241, 204, 27, 82, 185, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9500cce23b334d80 = b_9500cce23b334d80.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9500cce23b334d80[] = { &s_903455f06065422b, &s_b9521bccf10fa3b1, &s_f1c8950dab257542, }; static const uint16_t m_9500cce23b334d80[] = {4, 1, 7, 0, 5, 2, 6, 3}; static const uint16_t i_9500cce23b334d80[] = {0, 1, 2, 3, 4, 5, 6, 7}; const ::capnp::_::RawSchema s_9500cce23b334d80 = { 0x9500cce23b334d80, b_9500cce23b334d80.words, 155, d_9500cce23b334d80, m_9500cce23b334d80, 3, 8, i_9500cce23b334d80, nullptr, nullptr, { &s_9500cce23b334d80, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<269> b_d07378ede1f9cc60 = { { 0, 0, 0, 0, 5, 0, 6, 0, 96, 204, 249, 225, 237, 120, 115, 208, 19, 0, 0, 0, 1, 0, 3, 0, 217, 114, 76, 98, 9, 197, 63, 169, 1, 0, 7, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 194, 0, 0, 0, 29, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 47, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 0, 0, 0, 0, 0, 1, 0, 1, 0, 76, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 1, 0, 12, 2, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 0, 0, 3, 0, 1, 0, 16, 2, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 2, 0, 0, 3, 0, 1, 0, 20, 2, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 2, 0, 0, 3, 0, 1, 0, 24, 2, 0, 0, 2, 0, 1, 0, 4, 0, 251, 255, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 2, 0, 0, 3, 0, 1, 0, 28, 2, 0, 0, 2, 0, 1, 0, 5, 0, 250, 255, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 2, 0, 0, 3, 0, 1, 0, 32, 2, 0, 0, 2, 0, 1, 0, 6, 0, 249, 255, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 2, 0, 0, 3, 0, 1, 0, 36, 2, 0, 0, 2, 0, 1, 0, 7, 0, 248, 255, 0, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 2, 0, 0, 3, 0, 1, 0, 40, 2, 0, 0, 2, 0, 1, 0, 8, 0, 247, 255, 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 2, 0, 0, 3, 0, 1, 0, 44, 2, 0, 0, 2, 0, 1, 0, 9, 0, 246, 255, 0, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 2, 0, 0, 3, 0, 1, 0, 48, 2, 0, 0, 2, 0, 1, 0, 10, 0, 245, 255, 0, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 2, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 2, 0, 0, 3, 0, 1, 0, 52, 2, 0, 0, 2, 0, 1, 0, 11, 0, 244, 255, 0, 0, 0, 0, 0, 0, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 2, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 2, 0, 0, 3, 0, 1, 0, 56, 2, 0, 0, 2, 0, 1, 0, 12, 0, 243, 255, 0, 0, 0, 0, 0, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 2, 0, 0, 3, 0, 1, 0, 60, 2, 0, 0, 2, 0, 1, 0, 13, 0, 242, 255, 0, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 2, 0, 0, 3, 0, 1, 0, 64, 2, 0, 0, 2, 0, 1, 0, 14, 0, 241, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 151, 234, 96, 10, 37, 57, 231, 135, 61, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 240, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 169, 135, 127, 26, 113, 120, 14, 158, 37, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 239, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 211, 198, 76, 239, 96, 111, 58, 172, 13, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 238, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 191, 12, 251, 247, 105, 202, 139, 237, 245, 1, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 237, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 241, 73, 62, 162, 232, 63, 87, 194, 225, 1, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 111, 105, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 111, 111, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 49, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 51, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 49, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 51, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 108, 111, 97, 116, 51, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 108, 111, 97, 116, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 101, 120, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 97, 116, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 105, 115, 116, 0, 0, 0, 0, 101, 110, 117, 109, 0, 0, 0, 0, 115, 116, 114, 117, 99, 116, 0, 0, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 97, 110, 121, 80, 111, 105, 110, 116, 101, 114, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d07378ede1f9cc60 = b_d07378ede1f9cc60.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_d07378ede1f9cc60[] = { &s_87e739250a60ea97, &s_9e0e78711a7f87a9, &s_ac3a6f60ef4cc6d3, &s_c2573fe8a23e49f1, &s_ed8bca69f7fb0cbf, }; static const uint16_t m_d07378ede1f9cc60[] = {18, 1, 13, 15, 10, 11, 3, 4, 5, 2, 17, 14, 16, 12, 7, 8, 9, 6, 0}; static const uint16_t i_d07378ede1f9cc60[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; const ::capnp::_::RawSchema s_d07378ede1f9cc60 = { 0xd07378ede1f9cc60, b_d07378ede1f9cc60.words, 269, d_d07378ede1f9cc60, m_d07378ede1f9cc60, 5, 19, i_d07378ede1f9cc60, nullptr, nullptr, { &s_d07378ede1f9cc60, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<33> b_87e739250a60ea97 = { { 0, 0, 0, 0, 5, 0, 6, 0, 151, 234, 96, 10, 37, 57, 231, 135, 24, 0, 0, 0, 1, 0, 3, 0, 96, 204, 249, 225, 237, 120, 115, 208, 1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 234, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 108, 105, 115, 116, 0, 0, 0, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 24, 0, 0, 0, 2, 0, 1, 0, 101, 108, 101, 109, 101, 110, 116, 84, 121, 112, 101, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 96, 204, 249, 225, 237, 120, 115, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_87e739250a60ea97 = b_87e739250a60ea97.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_87e739250a60ea97[] = { &s_d07378ede1f9cc60, }; static const uint16_t m_87e739250a60ea97[] = {0}; static const uint16_t i_87e739250a60ea97[] = {0}; const ::capnp::_::RawSchema s_87e739250a60ea97 = { 0x87e739250a60ea97, b_87e739250a60ea97.words, 33, d_87e739250a60ea97, m_87e739250a60ea97, 1, 1, i_87e739250a60ea97, nullptr, nullptr, { &s_87e739250a60ea97, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<47> b_9e0e78711a7f87a9 = { { 0, 0, 0, 0, 5, 0, 6, 0, 169, 135, 127, 26, 113, 120, 14, 158, 24, 0, 0, 0, 1, 0, 3, 0, 96, 204, 249, 225, 237, 120, 115, 208, 1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 234, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 101, 110, 117, 109, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 73, 100, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 114, 97, 110, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9e0e78711a7f87a9 = b_9e0e78711a7f87a9.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9e0e78711a7f87a9[] = { &s_903455f06065422b, &s_d07378ede1f9cc60, }; static const uint16_t m_9e0e78711a7f87a9[] = {1, 0}; static const uint16_t i_9e0e78711a7f87a9[] = {0, 1}; const ::capnp::_::RawSchema s_9e0e78711a7f87a9 = { 0x9e0e78711a7f87a9, b_9e0e78711a7f87a9.words, 47, d_9e0e78711a7f87a9, m_9e0e78711a7f87a9, 2, 2, i_9e0e78711a7f87a9, nullptr, nullptr, { &s_9e0e78711a7f87a9, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<47> b_ac3a6f60ef4cc6d3 = { { 0, 0, 0, 0, 5, 0, 6, 0, 211, 198, 76, 239, 96, 111, 58, 172, 24, 0, 0, 0, 1, 0, 3, 0, 96, 204, 249, 225, 237, 120, 115, 208, 1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 115, 116, 114, 117, 99, 116, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 73, 100, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 114, 97, 110, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ac3a6f60ef4cc6d3 = b_ac3a6f60ef4cc6d3.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_ac3a6f60ef4cc6d3[] = { &s_903455f06065422b, &s_d07378ede1f9cc60, }; static const uint16_t m_ac3a6f60ef4cc6d3[] = {1, 0}; static const uint16_t i_ac3a6f60ef4cc6d3[] = {0, 1}; const ::capnp::_::RawSchema s_ac3a6f60ef4cc6d3 = { 0xac3a6f60ef4cc6d3, b_ac3a6f60ef4cc6d3.words, 47, d_ac3a6f60ef4cc6d3, m_ac3a6f60ef4cc6d3, 2, 2, i_ac3a6f60ef4cc6d3, nullptr, nullptr, { &s_ac3a6f60ef4cc6d3, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<48> b_ed8bca69f7fb0cbf = { { 0, 0, 0, 0, 5, 0, 6, 0, 191, 12, 251, 247, 105, 202, 139, 237, 24, 0, 0, 0, 1, 0, 3, 0, 96, 204, 249, 225, 237, 120, 115, 208, 1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 18, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 116, 121, 112, 101, 73, 100, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 114, 97, 110, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ed8bca69f7fb0cbf = b_ed8bca69f7fb0cbf.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_ed8bca69f7fb0cbf[] = { &s_903455f06065422b, &s_d07378ede1f9cc60, }; static const uint16_t m_ed8bca69f7fb0cbf[] = {1, 0}; static const uint16_t i_ed8bca69f7fb0cbf[] = {0, 1}; const ::capnp::_::RawSchema s_ed8bca69f7fb0cbf = { 0xed8bca69f7fb0cbf, b_ed8bca69f7fb0cbf.words, 48, d_ed8bca69f7fb0cbf, m_ed8bca69f7fb0cbf, 2, 2, i_ed8bca69f7fb0cbf, nullptr, nullptr, { &s_ed8bca69f7fb0cbf, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<46> b_c2573fe8a23e49f1 = { { 0, 0, 0, 0, 5, 0, 6, 0, 241, 73, 62, 162, 232, 63, 87, 194, 24, 0, 0, 0, 1, 0, 3, 0, 96, 204, 249, 225, 237, 120, 115, 208, 1, 0, 7, 0, 1, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 26, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 97, 110, 121, 80, 111, 105, 110, 116, 101, 114, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 86, 54, 89, 254, 121, 95, 59, 142, 69, 0, 0, 0, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 254, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 133, 74, 97, 244, 36, 247, 209, 157, 49, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 253, 255, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 116, 226, 86, 12, 18, 201, 239, 186, 29, 0, 0, 0, 194, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 110, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 0, 0, 0, 112, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 0, 0, 0, 0, 0, 105, 109, 112, 108, 105, 99, 105, 116, 77, 101, 116, 104, 111, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 0, } }; ::capnp::word const* const bp_c2573fe8a23e49f1 = b_c2573fe8a23e49f1.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c2573fe8a23e49f1[] = { &s_8e3b5f79fe593656, &s_9dd1f724f4614a85, &s_baefc9120c56e274, &s_d07378ede1f9cc60, }; static const uint16_t m_c2573fe8a23e49f1[] = {2, 1, 0}; static const uint16_t i_c2573fe8a23e49f1[] = {0, 1, 2}; const ::capnp::_::RawSchema s_c2573fe8a23e49f1 = { 0xc2573fe8a23e49f1, b_c2573fe8a23e49f1.words, 46, d_c2573fe8a23e49f1, m_c2573fe8a23e49f1, 4, 3, i_c2573fe8a23e49f1, nullptr, nullptr, { &s_c2573fe8a23e49f1, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<81> b_8e3b5f79fe593656 = { { 0, 0, 0, 0, 5, 0, 6, 0, 86, 54, 89, 254, 121, 95, 59, 142, 35, 0, 0, 0, 1, 0, 3, 0, 241, 73, 62, 162, 232, 63, 87, 194, 1, 0, 7, 0, 1, 0, 4, 0, 5, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 138, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 97, 110, 121, 80, 111, 105, 110, 116, 101, 114, 46, 117, 110, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 3, 0, 1, 0, 104, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 3, 0, 1, 0, 108, 0, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 0, 0, 0, 0, 0, 0, 1, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 3, 0, 1, 0, 112, 0, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 0, 0, 0, 0, 0, 0, 1, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 0, 0, 0, 3, 0, 1, 0, 120, 0, 0, 0, 2, 0, 1, 0, 97, 110, 121, 75, 105, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 117, 99, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 105, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 97, 98, 105, 108, 105, 116, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_8e3b5f79fe593656 = b_8e3b5f79fe593656.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_8e3b5f79fe593656[] = { &s_c2573fe8a23e49f1, }; static const uint16_t m_8e3b5f79fe593656[] = {0, 3, 2, 1}; static const uint16_t i_8e3b5f79fe593656[] = {0, 1, 2, 3}; const ::capnp::_::RawSchema s_8e3b5f79fe593656 = { 0x8e3b5f79fe593656, b_8e3b5f79fe593656.words, 81, d_8e3b5f79fe593656, m_8e3b5f79fe593656, 1, 4, i_8e3b5f79fe593656, nullptr, nullptr, { &s_8e3b5f79fe593656, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<50> b_9dd1f724f4614a85 = { { 0, 0, 0, 0, 5, 0, 6, 0, 133, 74, 97, 244, 36, 247, 209, 157, 35, 0, 0, 0, 1, 0, 3, 0, 241, 73, 62, 162, 232, 63, 87, 194, 1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 97, 110, 121, 80, 111, 105, 110, 116, 101, 114, 46, 112, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 3, 0, 1, 0, 56, 0, 0, 0, 2, 0, 1, 0, 115, 99, 111, 112, 101, 73, 100, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 97, 114, 97, 109, 101, 116, 101, 114, 73, 110, 100, 101, 120, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_9dd1f724f4614a85 = b_9dd1f724f4614a85.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_9dd1f724f4614a85[] = { &s_c2573fe8a23e49f1, }; static const uint16_t m_9dd1f724f4614a85[] = {1, 0}; static const uint16_t i_9dd1f724f4614a85[] = {0, 1}; const ::capnp::_::RawSchema s_9dd1f724f4614a85 = { 0x9dd1f724f4614a85, b_9dd1f724f4614a85.words, 50, d_9dd1f724f4614a85, m_9dd1f724f4614a85, 1, 2, i_9dd1f724f4614a85, nullptr, nullptr, { &s_9dd1f724f4614a85, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<37> b_baefc9120c56e274 = { { 0, 0, 0, 0, 5, 0, 6, 0, 116, 226, 86, 12, 18, 201, 239, 186, 35, 0, 0, 0, 1, 0, 3, 0, 241, 73, 62, 162, 232, 63, 87, 194, 1, 0, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 218, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 84, 121, 112, 101, 46, 97, 110, 121, 80, 111, 105, 110, 116, 101, 114, 46, 105, 109, 112, 108, 105, 99, 105, 116, 77, 101, 116, 104, 111, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 3, 0, 1, 0, 24, 0, 0, 0, 2, 0, 1, 0, 112, 97, 114, 97, 109, 101, 116, 101, 114, 73, 110, 100, 101, 120, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_baefc9120c56e274 = b_baefc9120c56e274.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_baefc9120c56e274[] = { &s_c2573fe8a23e49f1, }; static const uint16_t m_baefc9120c56e274[] = {0}; static const uint16_t i_baefc9120c56e274[] = {0}; const ::capnp::_::RawSchema s_baefc9120c56e274 = { 0xbaefc9120c56e274, b_baefc9120c56e274.words, 37, d_baefc9120c56e274, m_baefc9120c56e274, 1, 1, i_baefc9120c56e274, nullptr, nullptr, { &s_baefc9120c56e274, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<43> b_903455f06065422b = { { 0, 0, 0, 0, 5, 0, 6, 0, 43, 66, 101, 96, 240, 85, 52, 144, 19, 0, 0, 0, 1, 0, 0, 0, 217, 114, 76, 98, 9, 197, 63, 169, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 202, 0, 0, 0, 33, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 66, 114, 97, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 1, 0, 1, 0, 201, 107, 99, 169, 133, 52, 215, 171, 9, 0, 0, 0, 50, 0, 0, 0, 252, 231, 158, 150, 22, 205, 99, 200, 5, 0, 0, 0, 66, 0, 0, 0, 83, 99, 111, 112, 101, 0, 0, 0, 66, 105, 110, 100, 105, 110, 103, 0, 4, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 3, 0, 1, 0, 36, 0, 0, 0, 2, 0, 1, 0, 115, 99, 111, 112, 101, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 201, 107, 99, 169, 133, 52, 215, 171, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_903455f06065422b = b_903455f06065422b.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_903455f06065422b[] = { &s_abd73485a9636bc9, }; static const uint16_t m_903455f06065422b[] = {0}; static const uint16_t i_903455f06065422b[] = {0}; const ::capnp::_::RawSchema s_903455f06065422b = { 0x903455f06065422b, b_903455f06065422b.words, 43, d_903455f06065422b, m_903455f06065422b, 1, 1, i_903455f06065422b, nullptr, nullptr, { &s_903455f06065422b, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<67> b_abd73485a9636bc9 = { { 0, 0, 0, 0, 5, 0, 6, 0, 201, 107, 99, 169, 133, 52, 215, 171, 25, 0, 0, 0, 1, 0, 2, 0, 43, 66, 101, 96, 240, 85, 52, 144, 1, 0, 7, 0, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 66, 114, 97, 110, 100, 46, 83, 99, 111, 112, 101, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 96, 0, 0, 0, 2, 0, 1, 0, 2, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 0, 0, 0, 3, 0, 1, 0, 100, 0, 0, 0, 2, 0, 1, 0, 115, 99, 111, 112, 101, 73, 100, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 105, 110, 100, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 252, 231, 158, 150, 22, 205, 99, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 104, 101, 114, 105, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_abd73485a9636bc9 = b_abd73485a9636bc9.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_abd73485a9636bc9[] = { &s_c863cd16969ee7fc, }; static const uint16_t m_abd73485a9636bc9[] = {1, 2, 0}; static const uint16_t i_abd73485a9636bc9[] = {1, 2, 0}; const ::capnp::_::RawSchema s_abd73485a9636bc9 = { 0xabd73485a9636bc9, b_abd73485a9636bc9.words, 67, d_abd73485a9636bc9, m_abd73485a9636bc9, 1, 3, i_abd73485a9636bc9, nullptr, nullptr, { &s_abd73485a9636bc9, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<49> b_c863cd16969ee7fc = { { 0, 0, 0, 0, 5, 0, 6, 0, 252, 231, 158, 150, 22, 205, 99, 200, 25, 0, 0, 0, 1, 0, 1, 0, 43, 66, 101, 96, 240, 85, 52, 144, 1, 0, 7, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 10, 1, 0, 0, 37, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 66, 114, 97, 110, 100, 46, 66, 105, 110, 100, 105, 110, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 117, 110, 98, 111, 117, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 121, 112, 101, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 96, 204, 249, 225, 237, 120, 115, 208, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_c863cd16969ee7fc = b_c863cd16969ee7fc.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_c863cd16969ee7fc[] = { &s_d07378ede1f9cc60, }; static const uint16_t m_c863cd16969ee7fc[] = {1, 0}; static const uint16_t i_c863cd16969ee7fc[] = {0, 1}; const ::capnp::_::RawSchema s_c863cd16969ee7fc = { 0xc863cd16969ee7fc, b_c863cd16969ee7fc.words, 49, d_c863cd16969ee7fc, m_c863cd16969ee7fc, 1, 2, i_c863cd16969ee7fc, nullptr, nullptr, { &s_c863cd16969ee7fc, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<305> b_ce23dcd2d7b00c9b = { { 0, 0, 0, 0, 5, 0, 6, 0, 155, 12, 176, 215, 210, 220, 35, 206, 19, 0, 0, 0, 1, 0, 2, 0, 217, 114, 76, 98, 9, 197, 63, 169, 1, 0, 7, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 202, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 47, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 86, 97, 108, 117, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 76, 0, 0, 0, 3, 0, 4, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 1, 0, 12, 2, 0, 0, 2, 0, 1, 0, 1, 0, 254, 255, 16, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 0, 0, 3, 0, 1, 0, 16, 2, 0, 0, 2, 0, 1, 0, 2, 0, 253, 255, 2, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 2, 0, 0, 3, 0, 1, 0, 20, 2, 0, 0, 2, 0, 1, 0, 3, 0, 252, 255, 1, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 2, 0, 0, 3, 0, 1, 0, 24, 2, 0, 0, 2, 0, 1, 0, 4, 0, 251, 255, 1, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 2, 0, 0, 3, 0, 1, 0, 28, 2, 0, 0, 2, 0, 1, 0, 5, 0, 250, 255, 1, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 2, 0, 0, 3, 0, 1, 0, 32, 2, 0, 0, 2, 0, 1, 0, 6, 0, 249, 255, 2, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 2, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 2, 0, 0, 3, 0, 1, 0, 36, 2, 0, 0, 2, 0, 1, 0, 7, 0, 248, 255, 1, 0, 0, 0, 0, 0, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 2, 0, 0, 3, 0, 1, 0, 40, 2, 0, 0, 2, 0, 1, 0, 8, 0, 247, 255, 1, 0, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 2, 0, 0, 3, 0, 1, 0, 44, 2, 0, 0, 2, 0, 1, 0, 9, 0, 246, 255, 1, 0, 0, 0, 0, 0, 1, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 2, 0, 0, 3, 0, 1, 0, 48, 2, 0, 0, 2, 0, 1, 0, 10, 0, 245, 255, 1, 0, 0, 0, 0, 0, 1, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 2, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 2, 0, 0, 3, 0, 1, 0, 52, 2, 0, 0, 2, 0, 1, 0, 11, 0, 244, 255, 1, 0, 0, 0, 0, 0, 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 2, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 2, 0, 0, 3, 0, 1, 0, 56, 2, 0, 0, 2, 0, 1, 0, 12, 0, 243, 255, 0, 0, 0, 0, 0, 0, 1, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 2, 0, 0, 3, 0, 1, 0, 60, 2, 0, 0, 2, 0, 1, 0, 13, 0, 242, 255, 0, 0, 0, 0, 0, 0, 1, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 2, 0, 0, 3, 0, 1, 0, 64, 2, 0, 0, 2, 0, 1, 0, 14, 0, 241, 255, 0, 0, 0, 0, 0, 0, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 2, 0, 0, 3, 0, 1, 0, 68, 2, 0, 0, 2, 0, 1, 0, 15, 0, 240, 255, 1, 0, 0, 0, 0, 0, 1, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 2, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 2, 0, 0, 3, 0, 1, 0, 72, 2, 0, 0, 2, 0, 1, 0, 16, 0, 239, 255, 0, 0, 0, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 2, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 2, 0, 0, 3, 0, 1, 0, 76, 2, 0, 0, 2, 0, 1, 0, 17, 0, 238, 255, 0, 0, 0, 0, 0, 0, 1, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 2, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 2, 0, 0, 3, 0, 1, 0, 84, 2, 0, 0, 2, 0, 1, 0, 18, 0, 237, 255, 0, 0, 0, 0, 0, 0, 1, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 2, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 2, 0, 0, 3, 0, 1, 0, 92, 2, 0, 0, 2, 0, 1, 0, 118, 111, 105, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 111, 111, 108, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 56, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 49, 54, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 51, 50, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 54, 52, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 56, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 49, 54, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 51, 50, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 105, 110, 116, 54, 52, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 108, 111, 97, 116, 51, 50, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 108, 111, 97, 116, 54, 52, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 101, 120, 116, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 97, 116, 97, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 105, 115, 116, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 110, 117, 109, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 117, 99, 116, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 110, 116, 101, 114, 102, 97, 99, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 110, 121, 80, 111, 105, 110, 116, 101, 114, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ce23dcd2d7b00c9b = b_ce23dcd2d7b00c9b.words; #if !CAPNP_LITE static const uint16_t m_ce23dcd2d7b00c9b[] = {18, 1, 13, 15, 10, 11, 3, 4, 5, 2, 17, 14, 16, 12, 7, 8, 9, 6, 0}; static const uint16_t i_ce23dcd2d7b00c9b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; const ::capnp::_::RawSchema s_ce23dcd2d7b00c9b = { 0xce23dcd2d7b00c9b, b_ce23dcd2d7b00c9b.words, 305, nullptr, m_ce23dcd2d7b00c9b, 0, 19, i_ce23dcd2d7b00c9b, nullptr, nullptr, { &s_ce23dcd2d7b00c9b, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<63> b_f1c8950dab257542 = { { 0, 0, 0, 0, 5, 0, 6, 0, 66, 117, 37, 171, 13, 149, 200, 241, 19, 0, 0, 0, 1, 0, 1, 0, 217, 114, 76, 98, 9, 197, 63, 169, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 242, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 65, 110, 110, 111, 116, 97, 116, 105, 111, 110, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 97, 108, 117, 101, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 155, 12, 176, 215, 210, 220, 35, 206, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 114, 97, 110, 100, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 43, 66, 101, 96, 240, 85, 52, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_f1c8950dab257542 = b_f1c8950dab257542.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_f1c8950dab257542[] = { &s_903455f06065422b, &s_ce23dcd2d7b00c9b, }; static const uint16_t m_f1c8950dab257542[] = {2, 0, 1}; static const uint16_t i_f1c8950dab257542[] = {0, 1, 2}; const ::capnp::_::RawSchema s_f1c8950dab257542 = { 0xf1c8950dab257542, b_f1c8950dab257542.words, 63, d_f1c8950dab257542, m_f1c8950dab257542, 2, 3, i_f1c8950dab257542, nullptr, nullptr, { &s_f1c8950dab257542, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<54> b_d1958f7dba521926 = { { 0, 0, 0, 0, 5, 0, 6, 0, 38, 25, 82, 186, 125, 143, 149, 209, 19, 0, 0, 0, 2, 0, 0, 0, 217, 114, 76, 98, 9, 197, 63, 169, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 250, 0, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 199, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 69, 108, 101, 109, 101, 110, 116, 83, 105, 122, 101, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 32, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 57, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 109, 112, 116, 121, 0, 0, 0, 98, 105, 116, 0, 0, 0, 0, 0, 98, 121, 116, 101, 0, 0, 0, 0, 116, 119, 111, 66, 121, 116, 101, 115, 0, 0, 0, 0, 0, 0, 0, 0, 102, 111, 117, 114, 66, 121, 116, 101, 115, 0, 0, 0, 0, 0, 0, 0, 101, 105, 103, 104, 116, 66, 121, 116, 101, 115, 0, 0, 0, 0, 0, 0, 112, 111, 105, 110, 116, 101, 114, 0, 105, 110, 108, 105, 110, 101, 67, 111, 109, 112, 111, 115, 105, 116, 101, 0, } }; ::capnp::word const* const bp_d1958f7dba521926 = b_d1958f7dba521926.words; #if !CAPNP_LITE static const uint16_t m_d1958f7dba521926[] = {1, 2, 5, 0, 4, 7, 6, 3}; const ::capnp::_::RawSchema s_d1958f7dba521926 = { 0xd1958f7dba521926, b_d1958f7dba521926.words, 54, nullptr, m_d1958f7dba521926, 0, 8, nullptr, nullptr, nullptr, { &s_d1958f7dba521926, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE CAPNP_DEFINE_ENUM(ElementSize_d1958f7dba521926, d1958f7dba521926); static const ::capnp::_::AlignedData<63> b_d85d305b7d839963 = { { 0, 0, 0, 0, 5, 0, 6, 0, 99, 153, 131, 125, 91, 48, 93, 216, 19, 0, 0, 0, 1, 0, 1, 0, 217, 114, 76, 98, 9, 197, 63, 169, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 2, 1, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 67, 97, 112, 110, 112, 86, 101, 114, 115, 105, 111, 110, 0, 0, 0, 0, 0, 1, 0, 1, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 3, 0, 1, 0, 80, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 109, 97, 106, 111, 114, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 105, 110, 111, 114, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 105, 99, 114, 111, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_d85d305b7d839963 = b_d85d305b7d839963.words; #if !CAPNP_LITE static const uint16_t m_d85d305b7d839963[] = {0, 2, 1}; static const uint16_t i_d85d305b7d839963[] = {0, 1, 2}; const ::capnp::_::RawSchema s_d85d305b7d839963 = { 0xd85d305b7d839963, b_d85d305b7d839963.words, 63, nullptr, m_d85d305b7d839963, 0, 3, i_d85d305b7d839963, nullptr, nullptr, { &s_d85d305b7d839963, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<98> b_bfc546f6210ad7ce = { { 0, 0, 0, 0, 5, 0, 6, 0, 206, 215, 10, 33, 246, 70, 197, 191, 19, 0, 0, 0, 1, 0, 0, 0, 217, 114, 76, 98, 9, 197, 63, 169, 4, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 66, 1, 0, 0, 37, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 67, 111, 100, 101, 71, 101, 110, 101, 114, 97, 116, 111, 114, 82, 101, 113, 117, 101, 115, 116, 0, 4, 0, 0, 0, 1, 0, 1, 0, 98, 0, 129, 46, 176, 14, 234, 207, 1, 0, 0, 0, 114, 0, 0, 0, 82, 101, 113, 117, 101, 115, 116, 101, 100, 70, 105, 108, 101, 0, 0, 0, 16, 0, 0, 0, 3, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 3, 0, 1, 0, 120, 0, 0, 0, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 117, 0, 0, 0, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116, 0, 0, 0, 3, 0, 1, 0, 144, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 0, 0, 0, 3, 0, 1, 0, 152, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 0, 0, 0, 3, 0, 1, 0, 176, 0, 0, 0, 2, 0, 1, 0, 110, 111, 100, 101, 115, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 23, 164, 35, 249, 76, 171, 130, 230, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114, 101, 113, 117, 101, 115, 116, 101, 100, 70, 105, 108, 101, 115, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 98, 0, 129, 46, 176, 14, 234, 207, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 86, 101, 114, 115, 105, 111, 110, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 99, 153, 131, 125, 91, 48, 93, 216, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 111, 117, 114, 99, 101, 73, 110, 102, 111, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 174, 87, 19, 4, 227, 29, 142, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_bfc546f6210ad7ce = b_bfc546f6210ad7ce.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_bfc546f6210ad7ce[] = { &s_cfea0eb02e810062, &s_d85d305b7d839963, &s_e682ab4cf923a417, &s_f38e1de3041357ae, }; static const uint16_t m_bfc546f6210ad7ce[] = {2, 0, 1, 3}; static const uint16_t i_bfc546f6210ad7ce[] = {0, 1, 2, 3}; const ::capnp::_::RawSchema s_bfc546f6210ad7ce = { 0xbfc546f6210ad7ce, b_bfc546f6210ad7ce.words, 98, d_bfc546f6210ad7ce, m_bfc546f6210ad7ce, 4, 4, i_bfc546f6210ad7ce, nullptr, nullptr, { &s_bfc546f6210ad7ce, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<74> b_cfea0eb02e810062 = { { 0, 0, 0, 0, 5, 0, 6, 0, 98, 0, 129, 46, 176, 14, 234, 207, 40, 0, 0, 0, 1, 0, 1, 0, 206, 215, 10, 33, 246, 70, 197, 191, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 178, 1, 0, 0, 45, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 67, 111, 100, 101, 71, 101, 110, 101, 114, 97, 116, 111, 114, 82, 101, 113, 117, 101, 115, 116, 46, 82, 101, 113, 117, 101, 115, 116, 101, 100, 70, 105, 108, 101, 0, 0, 0, 4, 0, 0, 0, 1, 0, 1, 0, 229, 87, 35, 18, 147, 65, 80, 174, 1, 0, 0, 0, 58, 0, 0, 0, 73, 109, 112, 111, 114, 116, 0, 0, 12, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 3, 0, 1, 0, 76, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 3, 0, 1, 0, 84, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 0, 0, 3, 0, 1, 0, 104, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 105, 108, 101, 110, 97, 109, 101, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105, 109, 112, 111, 114, 116, 115, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 16, 0, 0, 0, 0, 0, 0, 0, 229, 87, 35, 18, 147, 65, 80, 174, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_cfea0eb02e810062 = b_cfea0eb02e810062.words; #if !CAPNP_LITE static const ::capnp::_::RawSchema* const d_cfea0eb02e810062[] = { &s_ae504193122357e5, }; static const uint16_t m_cfea0eb02e810062[] = {1, 0, 2}; static const uint16_t i_cfea0eb02e810062[] = {0, 1, 2}; const ::capnp::_::RawSchema s_cfea0eb02e810062 = { 0xcfea0eb02e810062, b_cfea0eb02e810062.words, 74, d_cfea0eb02e810062, m_cfea0eb02e810062, 1, 3, i_cfea0eb02e810062, nullptr, nullptr, { &s_cfea0eb02e810062, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE static const ::capnp::_::AlignedData<52> b_ae504193122357e5 = { { 0, 0, 0, 0, 5, 0, 6, 0, 229, 87, 35, 18, 147, 65, 80, 174, 54, 0, 0, 0, 1, 0, 1, 0, 98, 0, 129, 46, 176, 14, 234, 207, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 234, 1, 0, 0, 49, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 99, 104, 101, 109, 97, 46, 99, 97, 112, 110, 112, 58, 67, 111, 100, 101, 71, 101, 110, 101, 114, 97, 116, 111, 114, 82, 101, 113, 117, 101, 115, 116, 46, 82, 101, 113, 117, 101, 115, 116, 101, 100, 70, 105, 108, 101, 46, 73, 109, 112, 111, 114, 116, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 3, 0, 1, 0, 48, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 3, 0, 1, 0, 52, 0, 0, 0, 2, 0, 1, 0, 105, 100, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 97, 109, 101, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } }; ::capnp::word const* const bp_ae504193122357e5 = b_ae504193122357e5.words; #if !CAPNP_LITE static const uint16_t m_ae504193122357e5[] = {0, 1}; static const uint16_t i_ae504193122357e5[] = {0, 1}; const ::capnp::_::RawSchema s_ae504193122357e5 = { 0xae504193122357e5, b_ae504193122357e5.words, 52, nullptr, m_ae504193122357e5, 0, 2, i_ae504193122357e5, nullptr, nullptr, { &s_ae504193122357e5, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { namespace schema { // Node constexpr uint16_t Node::_capnpPrivate::dataWordSize; constexpr uint16_t Node::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::Parameter constexpr uint16_t Node::Parameter::_capnpPrivate::dataWordSize; constexpr uint16_t Node::Parameter::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::Parameter::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::Parameter::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::NestedNode constexpr uint16_t Node::NestedNode::_capnpPrivate::dataWordSize; constexpr uint16_t Node::NestedNode::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::NestedNode::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::NestedNode::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::SourceInfo constexpr uint16_t Node::SourceInfo::_capnpPrivate::dataWordSize; constexpr uint16_t Node::SourceInfo::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::SourceInfo::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::SourceInfo::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::SourceInfo::Member constexpr uint16_t Node::SourceInfo::Member::_capnpPrivate::dataWordSize; constexpr uint16_t Node::SourceInfo::Member::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::SourceInfo::Member::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::SourceInfo::Member::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::Struct constexpr uint16_t Node::Struct::_capnpPrivate::dataWordSize; constexpr uint16_t Node::Struct::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::Struct::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::Struct::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::Enum constexpr uint16_t Node::Enum::_capnpPrivate::dataWordSize; constexpr uint16_t Node::Enum::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::Enum::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::Enum::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::Interface constexpr uint16_t Node::Interface::_capnpPrivate::dataWordSize; constexpr uint16_t Node::Interface::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::Interface::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::Interface::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::Const constexpr uint16_t Node::Const::_capnpPrivate::dataWordSize; constexpr uint16_t Node::Const::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::Const::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::Const::_capnpPrivate::schema; #endif // !CAPNP_LITE // Node::Annotation constexpr uint16_t Node::Annotation::_capnpPrivate::dataWordSize; constexpr uint16_t Node::Annotation::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Node::Annotation::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Node::Annotation::_capnpPrivate::schema; #endif // !CAPNP_LITE // Field constexpr uint16_t Field::_capnpPrivate::dataWordSize; constexpr uint16_t Field::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Field::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Field::_capnpPrivate::schema; #endif // !CAPNP_LITE #ifndef _MSC_VER constexpr ::uint16_t Field::NO_DISCRIMINANT; #endif // Field::Slot constexpr uint16_t Field::Slot::_capnpPrivate::dataWordSize; constexpr uint16_t Field::Slot::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Field::Slot::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Field::Slot::_capnpPrivate::schema; #endif // !CAPNP_LITE // Field::Group constexpr uint16_t Field::Group::_capnpPrivate::dataWordSize; constexpr uint16_t Field::Group::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Field::Group::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Field::Group::_capnpPrivate::schema; #endif // !CAPNP_LITE // Field::Ordinal constexpr uint16_t Field::Ordinal::_capnpPrivate::dataWordSize; constexpr uint16_t Field::Ordinal::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Field::Ordinal::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Field::Ordinal::_capnpPrivate::schema; #endif // !CAPNP_LITE // Enumerant constexpr uint16_t Enumerant::_capnpPrivate::dataWordSize; constexpr uint16_t Enumerant::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Enumerant::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Enumerant::_capnpPrivate::schema; #endif // !CAPNP_LITE // Superclass constexpr uint16_t Superclass::_capnpPrivate::dataWordSize; constexpr uint16_t Superclass::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Superclass::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Superclass::_capnpPrivate::schema; #endif // !CAPNP_LITE // Method constexpr uint16_t Method::_capnpPrivate::dataWordSize; constexpr uint16_t Method::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Method::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Method::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type constexpr uint16_t Type::_capnpPrivate::dataWordSize; constexpr uint16_t Type::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::List constexpr uint16_t Type::List::_capnpPrivate::dataWordSize; constexpr uint16_t Type::List::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::List::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::List::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::Enum constexpr uint16_t Type::Enum::_capnpPrivate::dataWordSize; constexpr uint16_t Type::Enum::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::Enum::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::Enum::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::Struct constexpr uint16_t Type::Struct::_capnpPrivate::dataWordSize; constexpr uint16_t Type::Struct::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::Struct::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::Struct::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::Interface constexpr uint16_t Type::Interface::_capnpPrivate::dataWordSize; constexpr uint16_t Type::Interface::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::Interface::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::Interface::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::AnyPointer constexpr uint16_t Type::AnyPointer::_capnpPrivate::dataWordSize; constexpr uint16_t Type::AnyPointer::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::AnyPointer::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::AnyPointer::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::AnyPointer::Unconstrained constexpr uint16_t Type::AnyPointer::Unconstrained::_capnpPrivate::dataWordSize; constexpr uint16_t Type::AnyPointer::Unconstrained::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::AnyPointer::Unconstrained::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::AnyPointer::Unconstrained::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::AnyPointer::Parameter constexpr uint16_t Type::AnyPointer::Parameter::_capnpPrivate::dataWordSize; constexpr uint16_t Type::AnyPointer::Parameter::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::AnyPointer::Parameter::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::AnyPointer::Parameter::_capnpPrivate::schema; #endif // !CAPNP_LITE // Type::AnyPointer::ImplicitMethodParameter constexpr uint16_t Type::AnyPointer::ImplicitMethodParameter::_capnpPrivate::dataWordSize; constexpr uint16_t Type::AnyPointer::ImplicitMethodParameter::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Type::AnyPointer::ImplicitMethodParameter::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Type::AnyPointer::ImplicitMethodParameter::_capnpPrivate::schema; #endif // !CAPNP_LITE // Brand constexpr uint16_t Brand::_capnpPrivate::dataWordSize; constexpr uint16_t Brand::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Brand::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Brand::_capnpPrivate::schema; #endif // !CAPNP_LITE // Brand::Scope constexpr uint16_t Brand::Scope::_capnpPrivate::dataWordSize; constexpr uint16_t Brand::Scope::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Brand::Scope::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Brand::Scope::_capnpPrivate::schema; #endif // !CAPNP_LITE // Brand::Binding constexpr uint16_t Brand::Binding::_capnpPrivate::dataWordSize; constexpr uint16_t Brand::Binding::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Brand::Binding::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Brand::Binding::_capnpPrivate::schema; #endif // !CAPNP_LITE // Value constexpr uint16_t Value::_capnpPrivate::dataWordSize; constexpr uint16_t Value::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Value::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Value::_capnpPrivate::schema; #endif // !CAPNP_LITE // Annotation constexpr uint16_t Annotation::_capnpPrivate::dataWordSize; constexpr uint16_t Annotation::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind Annotation::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* Annotation::_capnpPrivate::schema; #endif // !CAPNP_LITE // CapnpVersion constexpr uint16_t CapnpVersion::_capnpPrivate::dataWordSize; constexpr uint16_t CapnpVersion::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind CapnpVersion::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* CapnpVersion::_capnpPrivate::schema; #endif // !CAPNP_LITE // CodeGeneratorRequest constexpr uint16_t CodeGeneratorRequest::_capnpPrivate::dataWordSize; constexpr uint16_t CodeGeneratorRequest::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind CodeGeneratorRequest::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* CodeGeneratorRequest::_capnpPrivate::schema; #endif // !CAPNP_LITE // CodeGeneratorRequest::RequestedFile constexpr uint16_t CodeGeneratorRequest::RequestedFile::_capnpPrivate::dataWordSize; constexpr uint16_t CodeGeneratorRequest::RequestedFile::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind CodeGeneratorRequest::RequestedFile::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* CodeGeneratorRequest::RequestedFile::_capnpPrivate::schema; #endif // !CAPNP_LITE // CodeGeneratorRequest::RequestedFile::Import constexpr uint16_t CodeGeneratorRequest::RequestedFile::Import::_capnpPrivate::dataWordSize; constexpr uint16_t CodeGeneratorRequest::RequestedFile::Import::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind CodeGeneratorRequest::RequestedFile::Import::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* CodeGeneratorRequest::RequestedFile::Import::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace } // namespace capnproto-c++-0.8.0/src/capnp/stream.capnp.c++0000644000175000017500000000336213650101756021535 0ustar00kentonkenton00000000000000// Generated by Cap'n Proto compiler, DO NOT EDIT // source: stream.capnp #include "stream.capnp.h" namespace capnp { namespace schemas { static const ::capnp::_::AlignedData<17> b_995f9a3377c0b16e = { { 0, 0, 0, 0, 5, 0, 6, 0, 110, 177, 192, 119, 51, 154, 95, 153, 19, 0, 0, 0, 1, 0, 0, 0, 248, 243, 147, 19, 169, 102, 195, 134, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 2, 1, 0, 0, 33, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 97, 112, 110, 112, 47, 115, 116, 114, 101, 97, 109, 46, 99, 97, 112, 110, 112, 58, 83, 116, 114, 101, 97, 109, 82, 101, 115, 117, 108, 116, 0, 0, 0, 0, 0, 1, 0, 1, 0, } }; ::capnp::word const* const bp_995f9a3377c0b16e = b_995f9a3377c0b16e.words; #if !CAPNP_LITE const ::capnp::_::RawSchema s_995f9a3377c0b16e = { 0x995f9a3377c0b16e, b_995f9a3377c0b16e.words, 17, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &s_995f9a3377c0b16e, nullptr, nullptr, 0, 0, nullptr } }; #endif // !CAPNP_LITE } // namespace schemas } // namespace capnp // ======================================================================================= namespace capnp { // StreamResult constexpr uint16_t StreamResult::_capnpPrivate::dataWordSize; constexpr uint16_t StreamResult::_capnpPrivate::pointerCount; #if !CAPNP_LITE constexpr ::capnp::Kind StreamResult::_capnpPrivate::kind; constexpr ::capnp::_::RawSchema const* StreamResult::_capnpPrivate::schema; #endif // !CAPNP_LITE } // namespace capnproto-c++-0.8.0/src/capnp/serialize.c++0000644000175000017500000002502213650101756021126 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "serialize.h" #include "layout.h" #include #include namespace capnp { FlatArrayMessageReader::FlatArrayMessageReader( kj::ArrayPtr array, ReaderOptions options) : MessageReader(options), end(array.end()) { if (array.size() < 1) { // Assume empty message. return; } const _::WireValue* table = reinterpret_cast*>(array.begin()); uint segmentCount = table[0].get() + 1; size_t offset = segmentCount / 2u + 1u; KJ_REQUIRE(array.size() >= offset, "Message ends prematurely in segment table.") { return; } { uint segmentSize = table[1].get(); KJ_REQUIRE(array.size() >= offset + segmentSize, "Message ends prematurely in first segment.") { return; } segment0 = array.slice(offset, offset + segmentSize); offset += segmentSize; } if (segmentCount > 1) { moreSegments = kj::heapArray>(segmentCount - 1); for (uint i = 1; i < segmentCount; i++) { uint segmentSize = table[i + 1].get(); KJ_REQUIRE(array.size() >= offset + segmentSize, "Message ends prematurely.") { moreSegments = nullptr; return; } moreSegments[i - 1] = array.slice(offset, offset + segmentSize); offset += segmentSize; } } end = array.begin() + offset; } size_t expectedSizeInWordsFromPrefix(kj::ArrayPtr array) { if (array.size() < 1) { // All messages are at least one word. return 1; } const _::WireValue* table = reinterpret_cast*>(array.begin()); uint segmentCount = table[0].get() + 1; size_t offset = segmentCount / 2u + 1u; // If the array is too small to contain the full segment table, truncate segmentCount to just // what is available. segmentCount = kj::min(segmentCount, array.size() * 2 - 1u); size_t totalSize = offset; for (uint i = 0; i < segmentCount; i++) { totalSize += table[i + 1].get(); } return totalSize; } kj::ArrayPtr FlatArrayMessageReader::getSegment(uint id) { if (id == 0) { return segment0; } else if (id <= moreSegments.size()) { return moreSegments[id - 1]; } else { return nullptr; } } kj::ArrayPtr initMessageBuilderFromFlatArrayCopy( kj::ArrayPtr array, MessageBuilder& target, ReaderOptions options) { FlatArrayMessageReader reader(array, options); target.setRoot(reader.getRoot()); return kj::arrayPtr(reader.getEnd(), array.end()); } kj::Array messageToFlatArray(kj::ArrayPtr> segments) { kj::Array result = kj::heapArray(computeSerializedSizeInWords(segments)); _::WireValue* table = reinterpret_cast<_::WireValue*>(result.begin()); // We write the segment count - 1 because this makes the first word zero for single-segment // messages, improving compression. We don't bother doing this with segment sizes because // one-word segments are rare anyway. table[0].set(segments.size() - 1); for (uint i = 0; i < segments.size(); i++) { table[i + 1].set(segments[i].size()); } if (segments.size() % 2 == 0) { // Set padding byte. table[segments.size() + 1].set(0); } word* dst = result.begin() + segments.size() / 2 + 1; for (auto& segment: segments) { memcpy(dst, segment.begin(), segment.size() * sizeof(word)); dst += segment.size(); } KJ_DASSERT(dst == result.end(), "Buffer overrun/underrun bug in code above."); return kj::mv(result); } size_t computeSerializedSizeInWords(kj::ArrayPtr> segments) { KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message."); size_t totalSize = segments.size() / 2 + 1; for (auto& segment: segments) { totalSize += segment.size(); } return totalSize; } // ======================================================================================= InputStreamMessageReader::InputStreamMessageReader( kj::InputStream& inputStream, ReaderOptions options, kj::ArrayPtr scratchSpace) : MessageReader(options), inputStream(inputStream), readPos(nullptr) { _::WireValue firstWord[2]; inputStream.read(firstWord, sizeof(firstWord)); uint segmentCount = firstWord[0].get() + 1; uint segment0Size = segmentCount == 0 ? 0 : firstWord[1].get(); size_t totalWords = segment0Size; // Reject messages with too many segments for security reasons. KJ_REQUIRE(segmentCount < 512, "Message has too many segments.") { segmentCount = 1; segment0Size = 1; break; } // Read sizes for all segments except the first. Include padding if necessary. KJ_STACK_ARRAY(_::WireValue, moreSizes, segmentCount & ~1, 16, 64); if (segmentCount > 1) { inputStream.read(moreSizes.begin(), moreSizes.size() * sizeof(moreSizes[0])); for (uint i = 0; i < segmentCount - 1; i++) { totalWords += moreSizes[i].get(); } } // Don't accept a message which the receiver couldn't possibly traverse without hitting the // traversal limit. Without this check, a malicious client could transmit a very large segment // size to make the receiver allocate excessive space and possibly crash. KJ_REQUIRE(totalWords <= options.traversalLimitInWords, "Message is too large. To increase the limit on the receiving end, see " "capnp::ReaderOptions.") { segmentCount = 1; segment0Size = kj::min(segment0Size, options.traversalLimitInWords); totalWords = segment0Size; break; } if (scratchSpace.size() < totalWords) { // TODO(perf): Consider allocating each segment as a separate chunk to reduce memory // fragmentation. ownedSpace = kj::heapArray(totalWords); scratchSpace = ownedSpace; } segment0 = scratchSpace.slice(0, segment0Size); if (segmentCount > 1) { moreSegments = kj::heapArray>(segmentCount - 1); size_t offset = segment0Size; for (uint i = 0; i < segmentCount - 1; i++) { uint segmentSize = moreSizes[i].get(); moreSegments[i] = scratchSpace.slice(offset, offset + segmentSize); offset += segmentSize; } } if (segmentCount == 1) { inputStream.read(scratchSpace.begin(), totalWords * sizeof(word)); } else if (segmentCount > 1) { readPos = scratchSpace.asBytes().begin(); readPos += inputStream.read(readPos, segment0Size * sizeof(word), totalWords * sizeof(word)); } } InputStreamMessageReader::~InputStreamMessageReader() noexcept(false) { if (readPos != nullptr) { unwindDetector.catchExceptionsIfUnwinding([&]() { // Note that lazy reads only happen when we have multiple segments, so moreSegments.back() is // valid. const byte* allEnd = reinterpret_cast(moreSegments.back().end()); inputStream.skip(allEnd - readPos); }); } } kj::ArrayPtr InputStreamMessageReader::getSegment(uint id) { if (id > moreSegments.size()) { return nullptr; } kj::ArrayPtr segment = id == 0 ? segment0 : moreSegments[id - 1]; if (readPos != nullptr) { // May need to lazily read more data. const byte* segmentEnd = reinterpret_cast(segment.end()); if (readPos < segmentEnd) { // Note that lazy reads only happen when we have multiple segments, so moreSegments.back() is // valid. const byte* allEnd = reinterpret_cast(moreSegments.back().end()); readPos += inputStream.read(readPos, segmentEnd - readPos, allEnd - readPos); } } return segment; } void readMessageCopy(kj::InputStream& input, MessageBuilder& target, ReaderOptions options, kj::ArrayPtr scratchSpace) { InputStreamMessageReader message(input, options, scratchSpace); target.setRoot(message.getRoot()); } // ------------------------------------------------------------------- void writeMessage(kj::OutputStream& output, kj::ArrayPtr> segments) { KJ_REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message."); KJ_STACK_ARRAY(_::WireValue, table, (segments.size() + 2) & ~size_t(1), 16, 64); // We write the segment count - 1 because this makes the first word zero for single-segment // messages, improving compression. We don't bother doing this with segment sizes because // one-word segments are rare anyway. table[0].set(segments.size() - 1); for (uint i = 0; i < segments.size(); i++) { table[i + 1].set(segments[i].size()); } if (segments.size() % 2 == 0) { // Set padding byte. table[segments.size() + 1].set(0); } KJ_STACK_ARRAY(kj::ArrayPtr, pieces, segments.size() + 1, 4, 32); pieces[0] = table.asBytes(); for (uint i = 0; i < segments.size(); i++) { pieces[i + 1] = segments[i].asBytes(); } output.write(pieces); } // ======================================================================================= StreamFdMessageReader::~StreamFdMessageReader() noexcept(false) {} void writeMessageToFd(int fd, kj::ArrayPtr> segments) { kj::FdOutputStream stream(fd); writeMessage(stream, segments); } void readMessageCopyFromFd(int fd, MessageBuilder& target, ReaderOptions options, kj::ArrayPtr scratchSpace) { kj::FdInputStream stream(fd); readMessageCopy(stream, target, options, scratchSpace); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/serialize-packed.c++0000644000175000017500000003720513650101756022361 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "serialize-packed.h" #include #include "layout.h" #include namespace capnp { namespace _ { // private PackedInputStream::PackedInputStream(kj::BufferedInputStream& inner): inner(inner) {} PackedInputStream::~PackedInputStream() noexcept(false) {} size_t PackedInputStream::tryRead(void* dst, size_t minBytes, size_t maxBytes) { if (maxBytes == 0) { return 0; } KJ_DREQUIRE(minBytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned."); KJ_DREQUIRE(maxBytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned."); uint8_t* __restrict__ out = reinterpret_cast(dst); uint8_t* const outEnd = reinterpret_cast(dst) + maxBytes; uint8_t* const outMin = reinterpret_cast(dst) + minBytes; kj::ArrayPtr buffer = inner.tryGetReadBuffer(); if (buffer.size() == 0) { return 0; } const uint8_t* __restrict__ in = reinterpret_cast(buffer.begin()); #define REFRESH_BUFFER() \ inner.skip(buffer.size()); \ buffer = inner.getReadBuffer(); \ KJ_REQUIRE(buffer.size() > 0, "Premature end of packed input.") { \ return out - reinterpret_cast(dst); \ } \ in = reinterpret_cast(buffer.begin()) #define BUFFER_END (reinterpret_cast(buffer.end())) #define BUFFER_REMAINING ((size_t)(BUFFER_END - in)) for (;;) { uint8_t tag; KJ_DASSERT((out - reinterpret_cast(dst)) % sizeof(word) == 0, "Output pointer should always be aligned here."); if (BUFFER_REMAINING < 10) { if (out >= outMin) { // We read at least the minimum amount, so go ahead and return. inner.skip(in - reinterpret_cast(buffer.begin())); return out - reinterpret_cast(dst); } if (BUFFER_REMAINING == 0) { REFRESH_BUFFER(); continue; } // We have at least 1, but not 10, bytes available. We need to read slowly, doing a bounds // check on each byte. tag = *in++; for (uint i = 0; i < 8; i++) { if (tag & (1u << i)) { if (BUFFER_REMAINING == 0) { REFRESH_BUFFER(); } *out++ = *in++; } else { *out++ = 0; } } if (BUFFER_REMAINING == 0 && (tag == 0 || tag == 0xffu)) { REFRESH_BUFFER(); } } else { tag = *in++; #define HANDLE_BYTE(n) \ { \ bool isNonzero = (tag & (1u << n)) != 0; \ *out++ = *in & (-(int8_t)isNonzero); \ in += isNonzero; \ } HANDLE_BYTE(0); HANDLE_BYTE(1); HANDLE_BYTE(2); HANDLE_BYTE(3); HANDLE_BYTE(4); HANDLE_BYTE(5); HANDLE_BYTE(6); HANDLE_BYTE(7); #undef HANDLE_BYTE } if (tag == 0) { KJ_DASSERT(BUFFER_REMAINING > 0, "Should always have non-empty buffer here."); uint runLength = *in++ * sizeof(word); KJ_REQUIRE(runLength <= outEnd - out, "Packed input did not end cleanly on a segment boundary.") { return out - reinterpret_cast(dst); } memset(out, 0, runLength); out += runLength; } else if (tag == 0xffu) { KJ_DASSERT(BUFFER_REMAINING > 0, "Should always have non-empty buffer here."); uint runLength = *in++ * sizeof(word); KJ_REQUIRE(runLength <= outEnd - out, "Packed input did not end cleanly on a segment boundary.") { return out - reinterpret_cast(dst); } size_t inRemaining = BUFFER_REMAINING; if (inRemaining >= runLength) { // Fast path. memcpy(out, in, runLength); out += runLength; in += runLength; } else { // Copy over the first buffer, then do one big read for the rest. memcpy(out, in, inRemaining); out += inRemaining; runLength -= inRemaining; inner.skip(buffer.size()); inner.read(out, runLength); out += runLength; if (out == outEnd) { return maxBytes; } else { buffer = inner.getReadBuffer(); in = reinterpret_cast(buffer.begin()); // Skip the bounds check below since we just did the same check above. continue; } } } if (out == outEnd) { inner.skip(in - reinterpret_cast(buffer.begin())); return maxBytes; } } KJ_FAIL_ASSERT("Can't get here."); return 0; // GCC knows KJ_FAIL_ASSERT doesn't return, but Eclipse CDT still warns... #undef REFRESH_BUFFER } void PackedInputStream::skip(size_t bytes) { // We can't just read into buffers because buffers must end on block boundaries. if (bytes == 0) { return; } KJ_DREQUIRE(bytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned."); kj::ArrayPtr buffer = inner.getReadBuffer(); const uint8_t* __restrict__ in = reinterpret_cast(buffer.begin()); #define REFRESH_BUFFER() \ inner.skip(buffer.size()); \ buffer = inner.getReadBuffer(); \ KJ_REQUIRE(buffer.size() > 0, "Premature end of packed input.") { return; } \ in = reinterpret_cast(buffer.begin()) for (;;) { uint8_t tag; if (BUFFER_REMAINING < 10) { if (BUFFER_REMAINING == 0) { REFRESH_BUFFER(); continue; } // We have at least 1, but not 10, bytes available. We need to read slowly, doing a bounds // check on each byte. tag = *in++; for (uint i = 0; i < 8; i++) { if (tag & (1u << i)) { if (BUFFER_REMAINING == 0) { REFRESH_BUFFER(); } in++; } } bytes -= 8; if (BUFFER_REMAINING == 0 && (tag == 0 || tag == 0xffu)) { REFRESH_BUFFER(); } } else { tag = *in++; #define HANDLE_BYTE(n) \ in += (tag & (1u << n)) != 0 HANDLE_BYTE(0); HANDLE_BYTE(1); HANDLE_BYTE(2); HANDLE_BYTE(3); HANDLE_BYTE(4); HANDLE_BYTE(5); HANDLE_BYTE(6); HANDLE_BYTE(7); #undef HANDLE_BYTE bytes -= 8; } if (tag == 0) { KJ_DASSERT(BUFFER_REMAINING > 0, "Should always have non-empty buffer here."); uint runLength = *in++ * sizeof(word); KJ_REQUIRE(runLength <= bytes, "Packed input did not end cleanly on a segment boundary.") { return; } bytes -= runLength; } else if (tag == 0xffu) { KJ_DASSERT(BUFFER_REMAINING > 0, "Should always have non-empty buffer here."); uint runLength = *in++ * sizeof(word); KJ_REQUIRE(runLength <= bytes, "Packed input did not end cleanly on a segment boundary.") { return; } bytes -= runLength; size_t inRemaining = BUFFER_REMAINING; if (inRemaining > runLength) { // Fast path. in += runLength; } else { // Forward skip to the underlying stream. runLength -= inRemaining; inner.skip(buffer.size() + runLength); if (bytes == 0) { return; } else { buffer = inner.getReadBuffer(); in = reinterpret_cast(buffer.begin()); // Skip the bounds check below since we just did the same check above. continue; } } } if (bytes == 0) { inner.skip(in - reinterpret_cast(buffer.begin())); return; } } KJ_FAIL_ASSERT("Can't get here."); } // ------------------------------------------------------------------- PackedOutputStream::PackedOutputStream(kj::BufferedOutputStream& inner) : inner(inner) {} PackedOutputStream::~PackedOutputStream() noexcept(false) {} void PackedOutputStream::write(const void* src, size_t size) { kj::ArrayPtr buffer = inner.getWriteBuffer(); byte slowBuffer[20]; uint8_t* __restrict__ out = reinterpret_cast(buffer.begin()); const uint8_t* __restrict__ in = reinterpret_cast(src); const uint8_t* const inEnd = reinterpret_cast(src) + size; while (in < inEnd) { if (reinterpret_cast(buffer.end()) - out < 10) { // Oops, we're out of space. We need at least 10 bytes for the fast path, since we don't // bounds-check on every byte. // Write what we have so far. inner.write(buffer.begin(), out - reinterpret_cast(buffer.begin())); // Use a slow buffer into which we'll encode 10 to 20 bytes. This should get us past the // output stream's buffer boundary. buffer = kj::arrayPtr(slowBuffer, sizeof(slowBuffer)); out = reinterpret_cast(buffer.begin()); } uint8_t* tagPos = out++; #define HANDLE_BYTE(n) \ uint8_t bit##n = *in != 0; \ *out = *in; \ out += bit##n; /* out only advances if the byte was non-zero */ \ ++in HANDLE_BYTE(0); HANDLE_BYTE(1); HANDLE_BYTE(2); HANDLE_BYTE(3); HANDLE_BYTE(4); HANDLE_BYTE(5); HANDLE_BYTE(6); HANDLE_BYTE(7); #undef HANDLE_BYTE uint8_t tag = (bit0 << 0) | (bit1 << 1) | (bit2 << 2) | (bit3 << 3) | (bit4 << 4) | (bit5 << 5) | (bit6 << 6) | (bit7 << 7); *tagPos = tag; if (tag == 0) { // An all-zero word is followed by a count of consecutive zero words (not including the // first one). // We can check a whole word at a time. (Here is where we use the assumption that // `src` is word-aligned.) const uint64_t* inWord = reinterpret_cast(in); // The count must fit it 1 byte, so limit to 255 words. const uint64_t* limit = reinterpret_cast(inEnd); if (limit - inWord > 255) { limit = inWord + 255; } while (inWord < limit && *inWord == 0) { ++inWord; } // Write the count. *out++ = inWord - reinterpret_cast(in); // Advance input. in = reinterpret_cast(inWord); } else if (tag == 0xffu) { // An all-nonzero word is followed by a count of consecutive uncompressed words, followed // by the uncompressed words themselves. // Count the number of consecutive words in the input which have no more than a single // zero-byte. We look for at least two zeros because that's the point where our compression // scheme becomes a net win. // TODO(perf): Maybe look for three zeros? Compressing a two-zero word is a loss if the // following word has no zeros. const uint8_t* runStart = in; const uint8_t* limit = inEnd; if ((size_t)(limit - in) > 255 * sizeof(word)) { limit = in + 255 * sizeof(word); } while (in < limit) { // Check eight input bytes for zeros. uint c = *in++ == 0; c += *in++ == 0; c += *in++ == 0; c += *in++ == 0; c += *in++ == 0; c += *in++ == 0; c += *in++ == 0; c += *in++ == 0; if (c >= 2) { // Un-read the word with multiple zeros, since we'll want to compress that one. in -= 8; break; } } // Write the count. uint count = in - runStart; *out++ = count / sizeof(word); if (count <= reinterpret_cast(buffer.end()) - out) { // There's enough space to memcpy. memcpy(out, runStart, count); out += count; } else { // Input overruns the output buffer. We'll give it to the output stream in one chunk // and let it decide what to do. inner.write(buffer.begin(), reinterpret_cast(out) - buffer.begin()); inner.write(runStart, in - runStart); buffer = inner.getWriteBuffer(); out = reinterpret_cast(buffer.begin()); } } } // Write whatever is left. inner.write(buffer.begin(), reinterpret_cast(out) - buffer.begin()); } } // namespace _ (private) // ======================================================================================= PackedMessageReader::PackedMessageReader( kj::BufferedInputStream& inputStream, ReaderOptions options, kj::ArrayPtr scratchSpace) : PackedInputStream(inputStream), InputStreamMessageReader(static_cast(*this), options, scratchSpace) {} PackedMessageReader::~PackedMessageReader() noexcept(false) {} PackedFdMessageReader::PackedFdMessageReader( int fd, ReaderOptions options, kj::ArrayPtr scratchSpace) : FdInputStream(fd), BufferedInputStreamWrapper(static_cast(*this)), PackedMessageReader(static_cast(*this), options, scratchSpace) {} PackedFdMessageReader::PackedFdMessageReader( kj::AutoCloseFd fd, ReaderOptions options, kj::ArrayPtr scratchSpace) : FdInputStream(kj::mv(fd)), BufferedInputStreamWrapper(static_cast(*this)), PackedMessageReader(static_cast(*this), options, scratchSpace) {} PackedFdMessageReader::~PackedFdMessageReader() noexcept(false) {} void writePackedMessage(kj::BufferedOutputStream& output, kj::ArrayPtr> segments) { _::PackedOutputStream packedOutput(output); writeMessage(packedOutput, segments); } void writePackedMessage(kj::OutputStream& output, kj::ArrayPtr> segments) { KJ_IF_MAYBE(bufferedOutputPtr, kj::dynamicDowncastIfAvailable(output)) { writePackedMessage(*bufferedOutputPtr, segments); } else { byte buffer[8192]; kj::BufferedOutputStreamWrapper bufferedOutput(output, kj::arrayPtr(buffer, sizeof(buffer))); writePackedMessage(bufferedOutput, segments); } } void writePackedMessageToFd(int fd, kj::ArrayPtr> segments) { kj::FdOutputStream output(fd); writePackedMessage(output, segments); } size_t computeUnpackedSizeInWords(kj::ArrayPtr packedBytes) { const byte* ptr = packedBytes.begin(); const byte* end = packedBytes.end(); size_t total = 0; while (ptr < end) { uint tag = *ptr; size_t count = kj::popCount(tag); total += 1; KJ_REQUIRE(end - ptr >= count, "invalid packed data"); ptr += count + 1; if (tag == 0) { KJ_REQUIRE(ptr < end, "invalid packed data"); total += *ptr++; } else if (tag == 0xff) { KJ_REQUIRE(ptr < end, "invalid packed data"); size_t words = *ptr++; total += words; size_t bytes = words * sizeof(word); KJ_REQUIRE(end - ptr >= bytes, "invalid packed data"); ptr += bytes; } } return total; } } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema.c++0000644000175000017500000010030013650101756020370 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "schema.h" #include "message.h" #include #include namespace capnp { namespace schema { uint KJ_HASHCODE(Type::Which w) { return kj::hashCode(static_cast(w)); } // TODO(cleanup): Cap'n Proto does not declare stringifiers nor hashers for `Which` enums, unlike // all other enums. Fix that and remove this. } namespace _ { // private // Null schemas generated using the below schema file with: // // capnp eval -Isrc null-schemas.capnp node --flat | // hexdump -v -e '8/1 "0x%02x, "' -e '1/8 "\n"'; echo // // I totally don't understand hexdump format strings and came up with this command based on trial // and error. // // @0x879863d4b2cc4a1e; // // using Node = import "/capnp/schema.capnp".Node; // // const node :Node = ( // id = 0x0000000000000000, // displayName = "(null schema)"); // // const struct :Node = ( // id = 0x0000000000000001, // displayName = "(null struct schema)", // struct = ( // dataWordCount = 0, // pointerCount = 0, // preferredListEncoding = empty)); // // const enum :Node = ( // id = 0x0000000000000002, // displayName = "(null enum schema)", // enum = ()); // // const interface :Node = ( // id = 0x0000000000000003, // displayName = "(null interface schema)", // interface = ()); // // const const :Node = ( // id = 0x0000000000000004, // displayName = "(null const schema)", // const = (type = (void = void), value = (void = void))); static const AlignedData<13> NULL_SCHEMA_BYTES = {{ 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, // union discriminant intentionally mangled 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x29, 0x00, 0x00, 0x00, }}; const RawSchema NULL_SCHEMA = { 0x0000000000000000, NULL_SCHEMA_BYTES.words, 13, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &NULL_SCHEMA, nullptr, nullptr, 0, 0, nullptr } }; static const AlignedData<14> NULL_STRUCT_SCHEMA_BYTES = {{ 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x29, 0x00, 0x00, 0x00, 0x00, }}; const RawSchema NULL_STRUCT_SCHEMA = { 0x0000000000000001, NULL_STRUCT_SCHEMA_BYTES.words, 14, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &NULL_STRUCT_SCHEMA, nullptr, nullptr, 0, 0, nullptr } }; static const AlignedData<14> NULL_ENUM_SCHEMA_BYTES = {{ 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x65, 0x6e, 0x75, 0x6d, 0x20, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; const RawSchema NULL_ENUM_SCHEMA = { 0x0000000000000002, NULL_ENUM_SCHEMA_BYTES.words, 14, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &NULL_ENUM_SCHEMA, nullptr, nullptr, 0, 0, nullptr } }; static const AlignedData<14> NULL_INTERFACE_SCHEMA_BYTES = {{ 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x29, 0x00, }}; const RawSchema NULL_INTERFACE_SCHEMA = { 0x0000000000000003, NULL_INTERFACE_SCHEMA_BYTES.words, 14, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &NULL_INTERFACE_SCHEMA, nullptr, nullptr, 0, 0, nullptr } }; static const AlignedData<20> NULL_CONST_SCHEMA_BYTES = {{ 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x28, 0x6e, 0x75, 0x6c, 0x6c, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; const RawSchema NULL_CONST_SCHEMA = { 0x0000000000000004, NULL_CONST_SCHEMA_BYTES.words, 20, nullptr, nullptr, 0, 0, nullptr, nullptr, nullptr, { &NULL_CONST_SCHEMA, nullptr, nullptr, 0, 0, nullptr } }; } // namespace _ (private) // ======================================================================================= schema::Node::Reader Schema::getProto() const { return readMessageUnchecked(raw->generic->encodedNode); } kj::ArrayPtr Schema::asUncheckedMessage() const { return kj::arrayPtr(raw->generic->encodedNode, raw->generic->encodedSize); } Schema Schema::getDependency(uint64_t id, uint location) const { { // Binary search dependency list. uint lower = 0; uint upper = raw->dependencyCount; while (lower < upper) { uint mid = (lower + upper) / 2; auto candidate = raw->dependencies[mid]; if (candidate.location == location) { candidate.schema->ensureInitialized(); return Schema(candidate.schema); } else if (candidate.location < location) { lower = mid + 1; } else { upper = mid; } } } { uint lower = 0; uint upper = raw->generic->dependencyCount; while (lower < upper) { uint mid = (lower + upper) / 2; const _::RawSchema* candidate = raw->generic->dependencies[mid]; uint64_t candidateId = candidate->id; if (candidateId == id) { candidate->ensureInitialized(); return Schema(&candidate->defaultBrand); } else if (candidateId < id) { lower = mid + 1; } else { upper = mid; } } } KJ_FAIL_REQUIRE("Requested ID not found in dependency table.", kj::hex(id)) { return Schema(); } } Schema::BrandArgumentList Schema::getBrandArgumentsAtScope(uint64_t scopeId) const { KJ_REQUIRE(getProto().getIsGeneric(), "Not a generic type.", getProto().getDisplayName()); for (auto scope: kj::range(raw->scopes, raw->scopes + raw->scopeCount)) { if (scope->typeId == scopeId) { // OK, this scope matches the scope we're looking for. if (scope->isUnbound) { return BrandArgumentList(scopeId, true); } else { return BrandArgumentList(scopeId, scope->bindingCount, scope->bindings); } } } // This scope is not listed in the scopes list. return BrandArgumentList(scopeId, raw->isUnbound()); } StructSchema Schema::asStruct() const { KJ_REQUIRE(getProto().isStruct(), "Tried to use non-struct schema as a struct.", getProto().getDisplayName()) { return StructSchema(); } return StructSchema(*this); } EnumSchema Schema::asEnum() const { KJ_REQUIRE(getProto().isEnum(), "Tried to use non-enum schema as an enum.", getProto().getDisplayName()) { return EnumSchema(); } return EnumSchema(*this); } InterfaceSchema Schema::asInterface() const { KJ_REQUIRE(getProto().isInterface(), "Tried to use non-interface schema as an interface.", getProto().getDisplayName()) { return InterfaceSchema(); } return InterfaceSchema(*this); } ConstSchema Schema::asConst() const { KJ_REQUIRE(getProto().isConst(), "Tried to use non-constant schema as a constant.", getProto().getDisplayName()) { return ConstSchema(); } return ConstSchema(*this); } kj::StringPtr Schema::getShortDisplayName() const { auto proto = getProto(); return proto.getDisplayName().slice(proto.getDisplayNamePrefixLength()); } void Schema::requireUsableAs(const _::RawSchema* expected) const { KJ_REQUIRE(raw->generic == expected || (expected != nullptr && raw->generic->canCastTo == expected), "This schema is not compatible with the requested native type."); } uint32_t Schema::getSchemaOffset(const schema::Value::Reader& value) const { const word* ptr; switch (value.which()) { case schema::Value::TEXT: ptr = reinterpret_cast(value.getText().begin()); break; case schema::Value::DATA: ptr = reinterpret_cast(value.getData().begin()); break; case schema::Value::STRUCT: ptr = value.getStruct().getAs<_::UncheckedMessage>(); break; case schema::Value::LIST: ptr = value.getList().getAs<_::UncheckedMessage>(); break; case schema::Value::ANY_POINTER: ptr = value.getAnyPointer().getAs<_::UncheckedMessage>(); break; default: KJ_FAIL_ASSERT("getDefaultValueSchemaOffset() can only be called on struct, list, " "and any-pointer fields."); } return ptr - raw->generic->encodedNode; } Type Schema::getBrandBinding(uint64_t scopeId, uint index) const { return getBrandArgumentsAtScope(scopeId)[index]; } Type Schema::interpretType(schema::Type::Reader proto, uint location) const { switch (proto.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: return proto.which(); case schema::Type::STRUCT: { auto structType = proto.getStruct(); return getDependency(structType.getTypeId(), location).asStruct(); } case schema::Type::ENUM: { auto enumType = proto.getEnum(); return getDependency(enumType.getTypeId(), location).asEnum(); } case schema::Type::INTERFACE: { auto interfaceType = proto.getInterface(); return getDependency(interfaceType.getTypeId(), location).asInterface(); } case schema::Type::LIST: return ListSchema::of(interpretType(proto.getList().getElementType(), location)); case schema::Type::ANY_POINTER: { auto anyPointer = proto.getAnyPointer(); switch (anyPointer.which()) { case schema::Type::AnyPointer::UNCONSTRAINED: return anyPointer.getUnconstrained().which(); case schema::Type::AnyPointer::PARAMETER: { auto param = anyPointer.getParameter(); return getBrandBinding(param.getScopeId(), param.getParameterIndex()); } case schema::Type::AnyPointer::IMPLICIT_METHOD_PARAMETER: return Type(Type::ImplicitParameter { anyPointer.getImplicitMethodParameter().getParameterIndex() }); } KJ_UNREACHABLE; } } KJ_UNREACHABLE; } Type Schema::BrandArgumentList::operator[](uint index) const { if (isUnbound) { return Type::BrandParameter { scopeId, index }; } if (index >= size_) { // Binding index out-of-range. Treat as AnyPointer. This is important to allow new // type parameters to be added to existing types without breaking dependent // schemas. return schema::Type::ANY_POINTER; } auto& binding = bindings[index]; Type result; if (binding.which == (uint)schema::Type::ANY_POINTER) { if (binding.scopeId != 0) { result = Type::BrandParameter { binding.scopeId, binding.paramIndex }; } else if (binding.isImplicitParameter) { result = Type::ImplicitParameter { binding.paramIndex }; } else { result = static_cast(binding.paramIndex); } } else if (binding.schema == nullptr) { // Builtin / primitive type. result = static_cast(binding.which); } else { binding.schema->ensureInitialized(); result = Type(static_cast(binding.which), binding.schema); } return result.wrapInList(binding.listDepth); } kj::StringPtr KJ_STRINGIFY(const Schema& schema) { return schema.getProto().getDisplayName(); } // ======================================================================================= namespace { template auto findSchemaMemberByName(const _::RawSchema* raw, kj::StringPtr name, List&& list) -> kj::Maybe { uint lower = 0; uint upper = raw->memberCount; while (lower < upper) { uint mid = (lower + upper) / 2; uint16_t memberIndex = raw->membersByName[mid]; auto candidate = list[memberIndex]; kj::StringPtr candidateName = candidate.getProto().getName(); if (candidateName == name) { return candidate; } else if (candidateName < name) { lower = mid + 1; } else { upper = mid; } } return nullptr; } } // namespace StructSchema::FieldList StructSchema::getFields() const { return FieldList(*this, getProto().getStruct().getFields()); } StructSchema::FieldSubset StructSchema::getUnionFields() const { auto proto = getProto().getStruct(); return FieldSubset(*this, proto.getFields(), raw->generic->membersByDiscriminant, proto.getDiscriminantCount()); } StructSchema::FieldSubset StructSchema::getNonUnionFields() const { auto proto = getProto().getStruct(); auto fields = proto.getFields(); auto offset = proto.getDiscriminantCount(); auto size = fields.size() - offset; return FieldSubset(*this, fields, raw->generic->membersByDiscriminant + offset, size); } kj::Maybe StructSchema::findFieldByName(kj::StringPtr name) const { return findSchemaMemberByName(raw->generic, name, getFields()); } StructSchema::Field StructSchema::getFieldByName(kj::StringPtr name) const { KJ_IF_MAYBE(member, findFieldByName(name)) { return *member; } else { KJ_FAIL_REQUIRE("struct has no such member", name); } } kj::Maybe StructSchema::getFieldByDiscriminant(uint16_t discriminant) const { auto unionFields = getUnionFields(); if (discriminant >= unionFields.size()) { return nullptr; } else { return unionFields[discriminant]; } } bool StructSchema::isStreamResult() const { auto& streamRaw = _::rawSchema(); return raw->generic == &streamRaw || raw->generic->canCastTo == &streamRaw; } Type StructSchema::Field::getType() const { auto proto = getProto(); uint location = _::RawBrandedSchema::makeDepLocation(_::RawBrandedSchema::DepKind::FIELD, index); switch (proto.which()) { case schema::Field::SLOT: return parent.interpretType(proto.getSlot().getType(), location); case schema::Field::GROUP: return parent.getDependency(proto.getGroup().getTypeId(), location).asStruct(); } KJ_UNREACHABLE; } uint32_t StructSchema::Field::getDefaultValueSchemaOffset() const { return parent.getSchemaOffset(proto.getSlot().getDefaultValue()); } kj::StringPtr KJ_STRINGIFY(const StructSchema::Field& field) { return field.getProto().getName(); } // ------------------------------------------------------------------- EnumSchema::EnumerantList EnumSchema::getEnumerants() const { return EnumerantList(*this, getProto().getEnum().getEnumerants()); } kj::Maybe EnumSchema::findEnumerantByName(kj::StringPtr name) const { return findSchemaMemberByName(raw->generic, name, getEnumerants()); } EnumSchema::Enumerant EnumSchema::getEnumerantByName(kj::StringPtr name) const { KJ_IF_MAYBE(enumerant, findEnumerantByName(name)) { return *enumerant; } else { KJ_FAIL_REQUIRE("enum has no such enumerant", name); } } // ------------------------------------------------------------------- InterfaceSchema::MethodList InterfaceSchema::getMethods() const { return MethodList(*this, getProto().getInterface().getMethods()); } kj::Maybe InterfaceSchema::findMethodByName(kj::StringPtr name) const { uint counter = 0; return findMethodByName(name, counter); } static constexpr uint MAX_SUPERCLASSES = 64; kj::Maybe InterfaceSchema::findMethodByName( kj::StringPtr name, uint& counter) const { // Security: Don't let someone DOS us with a dynamic schema containing cyclic inheritance. KJ_REQUIRE(counter++ < MAX_SUPERCLASSES, "Cyclic or absurdly-large inheritance graph detected.") { return nullptr; } auto result = findSchemaMemberByName(raw->generic, name, getMethods()); if (result == nullptr) { // Search superclasses. // TODO(perf): This may be somewhat slow, and in the case of lots of diamond dependencies it // could get pathological. Arguably we should generate a flat list of transitive // superclasses to search and store it in the RawSchema. It's problematic, though, because // this means that a dynamically-loaded RawSchema cannot be correctly constructed until all // superclasses have been loaded, which imposes an ordering requirement on SchemaLoader or // requires updating subclasses whenever a new superclass is loaded. auto superclasses = getProto().getInterface().getSuperclasses(); for (auto i: kj::indices(superclasses)) { auto superclass = superclasses[i]; uint location = _::RawBrandedSchema::makeDepLocation( _::RawBrandedSchema::DepKind::SUPERCLASS, i); result = getDependency(superclass.getId(), location) .asInterface().findMethodByName(name, counter); if (result != nullptr) { break; } } } return result; } InterfaceSchema::Method InterfaceSchema::getMethodByName(kj::StringPtr name) const { KJ_IF_MAYBE(method, findMethodByName(name)) { return *method; } else { KJ_FAIL_REQUIRE("interface has no such method", name); } } InterfaceSchema::SuperclassList InterfaceSchema::getSuperclasses() const { return SuperclassList(*this, getProto().getInterface().getSuperclasses()); } bool InterfaceSchema::extends(InterfaceSchema other) const { if (other.raw->generic == &_::NULL_INTERFACE_SCHEMA) { // We consider all interfaces to extend the null schema. return true; } uint counter = 0; return extends(other, counter); } bool InterfaceSchema::extends(InterfaceSchema other, uint& counter) const { // Security: Don't let someone DOS us with a dynamic schema containing cyclic inheritance. KJ_REQUIRE(counter++ < MAX_SUPERCLASSES, "Cyclic or absurdly-large inheritance graph detected.") { return false; } if (other == *this) { return true; } // TODO(perf): This may be somewhat slow. See findMethodByName() for discussion. auto superclasses = getProto().getInterface().getSuperclasses(); for (auto i: kj::indices(superclasses)) { auto superclass = superclasses[i]; uint location = _::RawBrandedSchema::makeDepLocation( _::RawBrandedSchema::DepKind::SUPERCLASS, i); if (getDependency(superclass.getId(), location).asInterface().extends(other, counter)) { return true; } } return false; } kj::Maybe InterfaceSchema::findSuperclass(uint64_t typeId) const { if (typeId == _::NULL_INTERFACE_SCHEMA.id) { // We consider all interfaces to extend the null schema. return InterfaceSchema(); } uint counter = 0; return findSuperclass(typeId, counter); } kj::Maybe InterfaceSchema::findSuperclass(uint64_t typeId, uint& counter) const { // Security: Don't let someone DOS us with a dynamic schema containing cyclic inheritance. KJ_REQUIRE(counter++ < MAX_SUPERCLASSES, "Cyclic or absurdly-large inheritance graph detected.") { return nullptr; } if (typeId == raw->generic->id) { return *this; } // TODO(perf): This may be somewhat slow. See findMethodByName() for discussion. auto superclasses = getProto().getInterface().getSuperclasses(); for (auto i: kj::indices(superclasses)) { auto superclass = superclasses[i]; uint location = _::RawBrandedSchema::makeDepLocation( _::RawBrandedSchema::DepKind::SUPERCLASS, i); KJ_IF_MAYBE(result, getDependency(superclass.getId(), location).asInterface() .findSuperclass(typeId, counter)) { return *result; } } return nullptr; } StructSchema InterfaceSchema::Method::getParamType() const { auto proto = getProto(); uint location = _::RawBrandedSchema::makeDepLocation( _::RawBrandedSchema::DepKind::METHOD_PARAMS, ordinal); return parent.getDependency(proto.getParamStructType(), location).asStruct(); } StructSchema InterfaceSchema::Method::getResultType() const { auto proto = getProto(); uint location = _::RawBrandedSchema::makeDepLocation( _::RawBrandedSchema::DepKind::METHOD_RESULTS, ordinal); return parent.getDependency(proto.getResultStructType(), location).asStruct(); } InterfaceSchema InterfaceSchema::SuperclassList::operator[](uint index) const { auto superclass = list[index]; uint location = _::RawBrandedSchema::makeDepLocation( _::RawBrandedSchema::DepKind::SUPERCLASS, index); return parent.getDependency(superclass.getId(), location).asInterface(); } // ------------------------------------------------------------------- uint32_t ConstSchema::getValueSchemaOffset() const { return getSchemaOffset(getProto().getConst().getValue()); } Type ConstSchema::getType() const { return interpretType(getProto().getConst().getType(), _::RawBrandedSchema::makeDepLocation(_::RawBrandedSchema::DepKind::CONST_TYPE, 0)); } // ======================================================================================= ListSchema ListSchema::of(schema::Type::Which primitiveType) { switch (primitiveType) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: break; case schema::Type::STRUCT: case schema::Type::ENUM: case schema::Type::INTERFACE: case schema::Type::LIST: KJ_FAIL_REQUIRE("Must use one of the other ListSchema::of() overloads for complex types."); break; case schema::Type::ANY_POINTER: KJ_FAIL_REQUIRE("List(AnyPointer) not supported."); break; } return ListSchema(primitiveType); } ListSchema ListSchema::of(schema::Type::Reader elementType, Schema context) { // This method is deprecated because it can only be implemented in terms of other deprecated // methods. Temporarily disable warnings for those other deprecated methods. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" switch (elementType.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: return of(elementType.which()); case schema::Type::STRUCT: return of(context.getDependency(elementType.getStruct().getTypeId()).asStruct()); case schema::Type::ENUM: return of(context.getDependency(elementType.getEnum().getTypeId()).asEnum()); case schema::Type::INTERFACE: return of(context.getDependency(elementType.getInterface().getTypeId()).asInterface()); case schema::Type::LIST: return of(of(elementType.getList().getElementType(), context)); case schema::Type::ANY_POINTER: KJ_FAIL_REQUIRE("List(AnyPointer) not supported."); return ListSchema(); } // Unknown type is acceptable. return ListSchema(elementType.which()); #pragma GCC diagnostic pop } // ======================================================================================= StructSchema Type::asStruct() const { KJ_REQUIRE(isStruct(), "Tried to interpret a non-struct type as a struct.") { return StructSchema(); } KJ_ASSERT(schema != nullptr); return StructSchema(Schema(schema)); } EnumSchema Type::asEnum() const { KJ_REQUIRE(isEnum(), "Tried to interpret a non-enum type as an enum.") { return EnumSchema(); } KJ_ASSERT(schema != nullptr); return EnumSchema(Schema(schema)); } InterfaceSchema Type::asInterface() const { KJ_REQUIRE(isInterface(), "Tried to interpret a non-interface type as an interface.") { return InterfaceSchema(); } KJ_ASSERT(schema != nullptr); return InterfaceSchema(Schema(schema)); } ListSchema Type::asList() const { KJ_REQUIRE(isList(), "Type::asList(): Not a list.") { return ListSchema::of(schema::Type::VOID); } Type elementType = *this; --elementType.listDepth; return ListSchema::of(elementType); } kj::Maybe Type::getBrandParameter() const { KJ_REQUIRE(isAnyPointer(), "Type::getBrandParameter() can only be called on AnyPointer types."); if (scopeId == 0) { return nullptr; } else { return BrandParameter { scopeId, paramIndex }; } } kj::Maybe Type::getImplicitParameter() const { KJ_REQUIRE(isAnyPointer(), "Type::getImplicitParameter() can only be called on AnyPointer types."); if (isImplicitParam) { return ImplicitParameter { paramIndex }; } else { return nullptr; } } bool Type::operator==(const Type& other) const { if (baseType != other.baseType || listDepth != other.listDepth) { return false; } switch (baseType) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: return true; case schema::Type::STRUCT: case schema::Type::ENUM: case schema::Type::INTERFACE: return schema == other.schema; case schema::Type::LIST: KJ_UNREACHABLE; case schema::Type::ANY_POINTER: return scopeId == other.scopeId && isImplicitParam == other.isImplicitParam && // Trying to comply with strict aliasing rules. Hopefully the compiler realizes that // both branches compile to the same instructions and can optimize it away. (scopeId != 0 || isImplicitParam ? paramIndex == other.paramIndex : anyPointerKind == other.anyPointerKind); } KJ_UNREACHABLE; } uint Type::hashCode() const { switch (baseType) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: if (listDepth == 0) { // Make sure that hashCode(Type(baseType)) == hashCode(baseType), otherwise HashMap lookups // keyed by `Type` won't work when the caller passes `baseType` as the key. return kj::hashCode(baseType); } else { return kj::hashCode(baseType, listDepth); } case schema::Type::STRUCT: case schema::Type::ENUM: case schema::Type::INTERFACE: if (listDepth == 0) { // Make sure that hashCode(Type(schema)) == hashCode(schema), otherwise HashMap lookups // keyed by `Type` won't work when the caller passes `schema` as the key. return kj::hashCode(schema); } else { return kj::hashCode(schema, listDepth); } case schema::Type::LIST: KJ_UNREACHABLE; case schema::Type::ANY_POINTER: { // Trying to comply with strict aliasing rules. Hopefully the compiler realizes that // both branches compile to the same instructions and can optimize it away. uint16_t val = scopeId != 0 || isImplicitParam ? paramIndex : static_cast(anyPointerKind); return kj::hashCode(val, isImplicitParam, scopeId, listDepth); } } KJ_UNREACHABLE; } void Type::requireUsableAs(Type expected) const { KJ_REQUIRE(baseType == expected.baseType && listDepth == expected.listDepth, "This type is not compatible with the requested native type."); switch (baseType) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::ANY_POINTER: break; case schema::Type::STRUCT: case schema::Type::ENUM: case schema::Type::INTERFACE: Schema(schema).requireUsableAs(expected.schema->generic); break; case schema::Type::LIST: KJ_UNREACHABLE; } } } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema-loader.c++0000644000175000017500000022775513650101756021664 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_PRIVATE #include "schema-loader.h" #include "message.h" #include "arena.h" #include #include #include #include #include #include #include #if _MSC_VER #include #endif namespace capnp { namespace { struct SchemaBindingsPair { const _::RawSchema* schema; const _::RawBrandedSchema::Scope* scopeBindings; inline bool operator==(const SchemaBindingsPair& other) const { return schema == other.schema && scopeBindings == other.scopeBindings; } inline uint hashCode() const { return kj::hashCode(schema, scopeBindings); } }; } // namespace bool hasDiscriminantValue(const schema::Field::Reader& reader) { return reader.getDiscriminantValue() != schema::Field::NO_DISCRIMINANT; } class SchemaLoader::InitializerImpl: public _::RawSchema::Initializer { public: inline explicit InitializerImpl(const SchemaLoader& loader): loader(loader), callback(nullptr) {} inline InitializerImpl(const SchemaLoader& loader, const LazyLoadCallback& callback) : loader(loader), callback(callback) {} inline kj::Maybe getCallback() const { return callback; } void init(const _::RawSchema* schema) const override; inline bool operator==(decltype(nullptr)) const { return callback == nullptr; } private: const SchemaLoader& loader; kj::Maybe callback; }; class SchemaLoader::BrandedInitializerImpl: public _::RawBrandedSchema::Initializer { public: inline explicit BrandedInitializerImpl(const SchemaLoader& loader): loader(loader) {} void init(const _::RawBrandedSchema* schema) const override; private: const SchemaLoader& loader; }; class SchemaLoader::Impl { public: inline explicit Impl(const SchemaLoader& loader) : initializer(loader), brandedInitializer(loader) {} inline Impl(const SchemaLoader& loader, const LazyLoadCallback& callback) : initializer(loader, callback), brandedInitializer(loader) {} _::RawSchema* load(const schema::Node::Reader& reader, bool isPlaceholder); _::RawSchema* loadNative(const _::RawSchema* nativeSchema); _::RawSchema* loadEmpty(uint64_t id, kj::StringPtr name, schema::Node::Which kind, bool isPlaceholder); // Create a dummy empty schema of the given kind for the given id and load it. const _::RawBrandedSchema* makeBranded( const _::RawSchema* schema, schema::Brand::Reader proto, kj::Maybe> clientBrand); struct TryGetResult { _::RawSchema* schema; kj::Maybe callback; }; TryGetResult tryGet(uint64_t typeId) const; const _::RawBrandedSchema* getUnbound(const _::RawSchema* schema); kj::Array getAllLoaded() const; void requireStructSize(uint64_t id, uint dataWordCount, uint pointerCount); // Require any struct nodes loaded with this ID -- in the past and in the future -- to have at // least the given sizes. Struct nodes that don't comply will simply be rewritten to comply. // This is used to ensure that parents of group nodes have at least the size of the group node, // so that allocating a struct that contains a group then getting the group node and setting // its fields can't possibly write outside of the allocated space. kj::Arena arena; private: kj::HashSet> dedupTable; // Records raw segments of memory in the arena against which we my want to de-dupe later // additions. Specifically, RawBrandedSchema binding tables are de-duped. kj::HashMap schemas; kj::HashMap brands; kj::HashMap unboundBrands; struct RequiredSize { uint16_t dataWordCount; uint16_t pointerCount; }; kj::HashMap structSizeRequirements; InitializerImpl initializer; BrandedInitializerImpl brandedInitializer; kj::ArrayPtr makeUncheckedNode(schema::Node::Reader node); // Construct a copy of the given schema node, allocated as a single-segment ("unchecked") node // within the loader's arena. kj::ArrayPtr makeUncheckedNodeEnforcingSizeRequirements(schema::Node::Reader node); // Like makeUncheckedNode() but if structSizeRequirements has a requirement for this node which // is larger than the node claims to be, the size will be edited to comply. This should be rare. // If the incoming node is not a struct, any struct size requirements will be ignored, but if // such requirements exist, this indicates an inconsistency that could cause exceptions later on // (but at least can't cause memory corruption). kj::ArrayPtr rewriteStructNodeWithSizes( schema::Node::Reader node, uint dataWordCount, uint pointerCount); // Make a copy of the given node (which must be a struct node) and set its sizes to be the max // of what it said already and the given sizes. // If the encoded node does not meet the given struct size requirements, make a new copy that // does. void applyStructSizeRequirement(_::RawSchema* raw, uint dataWordCount, uint pointerCount); const _::RawBrandedSchema* makeBranded(const _::RawSchema* schema, kj::ArrayPtr scopes); kj::ArrayPtr makeBrandedDependencies( const _::RawSchema* schema, kj::Maybe> bindings); void makeDep(_::RawBrandedSchema::Binding& result, schema::Type::Reader type, kj::StringPtr scopeName, kj::Maybe> brandBindings); void makeDep(_::RawBrandedSchema::Binding& result, uint64_t typeId, schema::Type::Which whichType, schema::Node::Which expectedKind, schema::Brand::Reader brand, kj::StringPtr scopeName, kj::Maybe> brandBindings); // Looks up the schema and brand for a dependency, or creates lazily-evaluated placeholders if // they don't already exist, and fills in `result`. `scopeName` is a human-readable name of the // place where the type appeared. // // Note that we don't simply return a Binding because we need to be careful about initialization // to ensure that our byte-based de-duplification works. If we constructed a Binding on the stack // and returned it, padding bytes in that Binding could go uninitialized, causing it to appear // unique when it's not. It is expected that `result` has been zero'd via memset() before these // methods are called. const _::RawBrandedSchema* makeDepSchema( schema::Type::Reader type, kj::StringPtr scopeName, kj::Maybe> brandBindings); const _::RawBrandedSchema* makeDepSchema( uint64_t typeId, schema::Type::Which whichType, schema::Node::Which expectedKind, schema::Brand::Reader brand, kj::StringPtr scopeName, kj::Maybe> brandBindings); // Invoke makeDep() then return the result's schema, or nullptr if it's a primitive type. template kj::ArrayPtr copyDeduped(kj::ArrayPtr values); template kj::ArrayPtr copyDeduped(kj::ArrayPtr values); // Copy the given array into the arena and return the copy -- unless an identical array // was copied previously, in which case the existing copy is returned. friend class SchemaLoader::BrandedInitializerImpl; }; // ======================================================================================= inline static void verifyVoid(Void value) {} // Calls to this will break if the parameter type changes to non-void. We use this to detect // when the code needs updating. class SchemaLoader::Validator { public: Validator(SchemaLoader::Impl& loader): loader(loader) {} bool validate(const schema::Node::Reader& node) { isValid = true; nodeName = node.getDisplayName(); dependencies.clear(); KJ_CONTEXT("validating schema node", nodeName, (uint)node.which()); if (node.getParameters().size() > 0) { KJ_REQUIRE(node.getIsGeneric(), "if parameter list is non-empty, isGeneric must be true") { isValid = false; return false; } } switch (node.which()) { case schema::Node::FILE: verifyVoid(node.getFile()); break; case schema::Node::STRUCT: validate(node.getStruct(), node.getScopeId()); break; case schema::Node::ENUM: validate(node.getEnum()); break; case schema::Node::INTERFACE: validate(node.getInterface()); break; case schema::Node::CONST: validate(node.getConst()); break; case schema::Node::ANNOTATION: validate(node.getAnnotation()); break; } // We accept and pass through node types we don't recognize. return isValid; } const _::RawSchema** makeDependencyArray(uint32_t* count) { *count = dependencies.size(); kj::ArrayPtr result = loader.arena.allocateArray(*count); uint pos = 0; for (auto& dep: dependencies) { result[pos++] = dep.value; } KJ_DASSERT(pos == *count); return result.begin(); } const uint16_t* makeMemberInfoArray(uint32_t* count) { *count = members.size(); kj::ArrayPtr result = loader.arena.allocateArray(*count); uint pos = 0; for (auto& member: members) { result[pos++] = member.value; } KJ_DASSERT(pos == *count); return result.begin(); } const uint16_t* makeMembersByDiscriminantArray() { return membersByDiscriminant.begin(); } private: SchemaLoader::Impl& loader; Text::Reader nodeName; bool isValid; // Maps type IDs -> compiled schemas for each dependency. // Order is important because makeDependencyArray() compiles a sorted array. kj::TreeMap dependencies; // Maps name -> index for each member. // Order is important because makeMemberInfoArray() compiles a sorted array. kj::TreeMap members; kj::ArrayPtr membersByDiscriminant; #define VALIDATE_SCHEMA(condition, ...) \ KJ_REQUIRE(condition, ##__VA_ARGS__) { isValid = false; return; } #define FAIL_VALIDATE_SCHEMA(...) \ KJ_FAIL_REQUIRE(__VA_ARGS__) { isValid = false; return; } void validateMemberName(kj::StringPtr name, uint index) { members.upsert(name, index, [&](auto&, auto&&) { FAIL_VALIDATE_SCHEMA("duplicate name", name); }); } void validate(const schema::Node::Struct::Reader& structNode, uint64_t scopeId) { uint dataSizeInBits = structNode.getDataWordCount() * 64; uint pointerCount = structNode.getPointerCount(); auto fields = structNode.getFields(); KJ_STACK_ARRAY(bool, sawCodeOrder, fields.size(), 32, 256); memset(sawCodeOrder.begin(), 0, sawCodeOrder.size() * sizeof(sawCodeOrder[0])); KJ_STACK_ARRAY(bool, sawDiscriminantValue, structNode.getDiscriminantCount(), 32, 256); memset(sawDiscriminantValue.begin(), 0, sawDiscriminantValue.size() * sizeof(sawDiscriminantValue[0])); if (structNode.getDiscriminantCount() > 0) { VALIDATE_SCHEMA(structNode.getDiscriminantCount() != 1, "union must have at least two members"); VALIDATE_SCHEMA(structNode.getDiscriminantCount() <= fields.size(), "struct can't have more union fields than total fields"); VALIDATE_SCHEMA((structNode.getDiscriminantOffset() + 1) * 16 <= dataSizeInBits, "union discriminant is out-of-bounds"); } membersByDiscriminant = loader.arena.allocateArray(fields.size()); uint discriminantPos = 0; uint nonDiscriminantPos = structNode.getDiscriminantCount(); uint index = 0; uint nextOrdinal = 0; for (auto field: fields) { KJ_CONTEXT("validating struct field", field.getName()); validateMemberName(field.getName(), index); VALIDATE_SCHEMA(field.getCodeOrder() < sawCodeOrder.size() && !sawCodeOrder[field.getCodeOrder()], "invalid codeOrder"); sawCodeOrder[field.getCodeOrder()] = true; auto ordinal = field.getOrdinal(); if (ordinal.isExplicit()) { VALIDATE_SCHEMA(ordinal.getExplicit() >= nextOrdinal, "fields were not ordered by ordinal"); nextOrdinal = ordinal.getExplicit() + 1; } if (hasDiscriminantValue(field)) { VALIDATE_SCHEMA(field.getDiscriminantValue() < sawDiscriminantValue.size() && !sawDiscriminantValue[field.getDiscriminantValue()], "invalid discriminantValue"); sawDiscriminantValue[field.getDiscriminantValue()] = true; membersByDiscriminant[discriminantPos++] = index; } else { VALIDATE_SCHEMA(nonDiscriminantPos <= fields.size(), "discriminantCount did not match fields"); membersByDiscriminant[nonDiscriminantPos++] = index; } switch (field.which()) { case schema::Field::SLOT: { auto slot = field.getSlot(); uint fieldBits = 0; bool fieldIsPointer = false; validate(slot.getType(), slot.getDefaultValue(), &fieldBits, &fieldIsPointer); VALIDATE_SCHEMA(fieldBits * (slot.getOffset() + 1) <= dataSizeInBits && fieldIsPointer * (slot.getOffset() + 1) <= pointerCount, "field offset out-of-bounds", slot.getOffset(), dataSizeInBits, pointerCount); break; } case schema::Field::GROUP: // Require that the group is a struct node. validateTypeId(field.getGroup().getTypeId(), schema::Node::STRUCT); break; } ++index; } // If the above code is correct, these should pass. KJ_ASSERT(discriminantPos == structNode.getDiscriminantCount()); KJ_ASSERT(nonDiscriminantPos == fields.size()); if (structNode.getIsGroup()) { VALIDATE_SCHEMA(scopeId != 0, "group node missing scopeId"); // Require that the group's scope has at least the same size as the group, so that anyone // constructing an instance of the outer scope can safely read/write the group. loader.requireStructSize(scopeId, structNode.getDataWordCount(), structNode.getPointerCount()); // Require that the parent type is a struct. validateTypeId(scopeId, schema::Node::STRUCT); } } void validate(const schema::Node::Enum::Reader& enumNode) { auto enumerants = enumNode.getEnumerants(); KJ_STACK_ARRAY(bool, sawCodeOrder, enumerants.size(), 32, 256); memset(sawCodeOrder.begin(), 0, sawCodeOrder.size() * sizeof(sawCodeOrder[0])); uint index = 0; for (auto enumerant: enumerants) { validateMemberName(enumerant.getName(), index++); VALIDATE_SCHEMA(enumerant.getCodeOrder() < enumerants.size() && !sawCodeOrder[enumerant.getCodeOrder()], "invalid codeOrder", enumerant.getName()); sawCodeOrder[enumerant.getCodeOrder()] = true; } } void validate(const schema::Node::Interface::Reader& interfaceNode) { for (auto extend: interfaceNode.getSuperclasses()) { validateTypeId(extend.getId(), schema::Node::INTERFACE); validate(extend.getBrand()); } auto methods = interfaceNode.getMethods(); KJ_STACK_ARRAY(bool, sawCodeOrder, methods.size(), 32, 256); memset(sawCodeOrder.begin(), 0, sawCodeOrder.size() * sizeof(sawCodeOrder[0])); uint index = 0; for (auto method: methods) { KJ_CONTEXT("validating method", method.getName()); validateMemberName(method.getName(), index++); VALIDATE_SCHEMA(method.getCodeOrder() < methods.size() && !sawCodeOrder[method.getCodeOrder()], "invalid codeOrder"); sawCodeOrder[method.getCodeOrder()] = true; validateTypeId(method.getParamStructType(), schema::Node::STRUCT); validate(method.getParamBrand()); validateTypeId(method.getResultStructType(), schema::Node::STRUCT); validate(method.getResultBrand()); } } void validate(const schema::Node::Const::Reader& constNode) { uint dummy1; bool dummy2; validate(constNode.getType(), constNode.getValue(), &dummy1, &dummy2); } void validate(const schema::Node::Annotation::Reader& annotationNode) { validate(annotationNode.getType()); } void validate(const schema::Type::Reader& type, const schema::Value::Reader& value, uint* dataSizeInBits, bool* isPointer) { validate(type); schema::Value::Which expectedValueType = schema::Value::VOID; bool hadCase = false; switch (type.which()) { #define HANDLE_TYPE(name, bits, ptr) \ case schema::Type::name: \ expectedValueType = schema::Value::name; \ *dataSizeInBits = bits; *isPointer = ptr; \ hadCase = true; \ break; HANDLE_TYPE(VOID, 0, false) HANDLE_TYPE(BOOL, 1, false) HANDLE_TYPE(INT8, 8, false) HANDLE_TYPE(INT16, 16, false) HANDLE_TYPE(INT32, 32, false) HANDLE_TYPE(INT64, 64, false) HANDLE_TYPE(UINT8, 8, false) HANDLE_TYPE(UINT16, 16, false) HANDLE_TYPE(UINT32, 32, false) HANDLE_TYPE(UINT64, 64, false) HANDLE_TYPE(FLOAT32, 32, false) HANDLE_TYPE(FLOAT64, 64, false) HANDLE_TYPE(TEXT, 0, true) HANDLE_TYPE(DATA, 0, true) HANDLE_TYPE(LIST, 0, true) HANDLE_TYPE(ENUM, 16, false) HANDLE_TYPE(STRUCT, 0, true) HANDLE_TYPE(INTERFACE, 0, true) HANDLE_TYPE(ANY_POINTER, 0, true) #undef HANDLE_TYPE } if (hadCase) { VALIDATE_SCHEMA(value.which() == expectedValueType, "Value did not match type.", (uint)value.which(), (uint)expectedValueType); } } void validate(const schema::Type::Reader& type) { switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::ANY_POINTER: break; case schema::Type::STRUCT: { auto structType = type.getStruct(); validateTypeId(structType.getTypeId(), schema::Node::STRUCT); validate(structType.getBrand()); break; } case schema::Type::ENUM: { auto enumType = type.getEnum(); validateTypeId(enumType.getTypeId(), schema::Node::ENUM); validate(enumType.getBrand()); break; } case schema::Type::INTERFACE: { auto interfaceType = type.getInterface(); validateTypeId(interfaceType.getTypeId(), schema::Node::INTERFACE); validate(interfaceType.getBrand()); break; } case schema::Type::LIST: validate(type.getList().getElementType()); break; } // We intentionally allow unknown types. } void validate(const schema::Brand::Reader& brand) { for (auto scope: brand.getScopes()) { switch (scope.which()) { case schema::Brand::Scope::BIND: for (auto binding: scope.getBind()) { switch (binding.which()) { case schema::Brand::Binding::UNBOUND: break; case schema::Brand::Binding::TYPE: { auto type = binding.getType(); validate(type); bool isPointer = true; switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: isPointer = false; break; case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::ANY_POINTER: case schema::Type::STRUCT: case schema::Type::INTERFACE: case schema::Type::LIST: isPointer = true; break; } VALIDATE_SCHEMA(isPointer, "generic type parameter must be a pointer type", type); break; } } } break; case schema::Brand::Scope::INHERIT: break; } } } void validateTypeId(uint64_t id, schema::Node::Which expectedKind) { _::RawSchema* existing = loader.tryGet(id).schema; if (existing != nullptr) { auto node = readMessageUnchecked(existing->encodedNode); VALIDATE_SCHEMA(node.which() == expectedKind, "expected a different kind of node for this ID", id, (uint)expectedKind, (uint)node.which(), node.getDisplayName()); dependencies.upsert(id, existing, [](auto&,auto&&) { /* ignore dupe */ }); return; } dependencies.upsert(id, loader.loadEmpty( id, kj::str("(unknown type used by ", nodeName , ")"), expectedKind, true), [](auto&,auto&&) { /* ignore dupe */ }); } #undef VALIDATE_SCHEMA #undef FAIL_VALIDATE_SCHEMA }; // ======================================================================================= class SchemaLoader::CompatibilityChecker { public: CompatibilityChecker(SchemaLoader::Impl& loader): loader(loader) {} bool shouldReplace(const schema::Node::Reader& existingNode, const schema::Node::Reader& replacement, bool preferReplacementIfEquivalent) { this->existingNode = existingNode; this->replacementNode = replacement; KJ_CONTEXT("checking compatibility with previously-loaded node of the same id", existingNode.getDisplayName()); KJ_DREQUIRE(existingNode.getId() == replacement.getId()); nodeName = existingNode.getDisplayName(); compatibility = EQUIVALENT; checkCompatibility(existingNode, replacement); // Prefer the newer schema. return preferReplacementIfEquivalent ? compatibility != OLDER : compatibility == NEWER; } private: SchemaLoader::Impl& loader; Text::Reader nodeName; schema::Node::Reader existingNode; schema::Node::Reader replacementNode; enum Compatibility { EQUIVALENT, OLDER, NEWER, INCOMPATIBLE }; Compatibility compatibility; #define VALIDATE_SCHEMA(condition, ...) \ KJ_REQUIRE(condition, ##__VA_ARGS__) { compatibility = INCOMPATIBLE; return; } #define FAIL_VALIDATE_SCHEMA(...) \ KJ_FAIL_REQUIRE(__VA_ARGS__) { compatibility = INCOMPATIBLE; return; } void replacementIsNewer() { switch (compatibility) { case EQUIVALENT: compatibility = NEWER; break; case OLDER: FAIL_VALIDATE_SCHEMA("Schema node contains some changes that are upgrades and some " "that are downgrades. All changes must be in the same direction for compatibility."); break; case NEWER: break; case INCOMPATIBLE: break; } } void replacementIsOlder() { switch (compatibility) { case EQUIVALENT: compatibility = OLDER; break; case OLDER: break; case NEWER: FAIL_VALIDATE_SCHEMA("Schema node contains some changes that are upgrades and some " "that are downgrades. All changes must be in the same direction for compatibility."); break; case INCOMPATIBLE: break; } } void checkCompatibility(const schema::Node::Reader& node, const schema::Node::Reader& replacement) { // Returns whether `replacement` is equivalent, older than, newer than, or incompatible with // `node`. If exceptions are enabled, this will throw an exception on INCOMPATIBLE. VALIDATE_SCHEMA(node.which() == replacement.which(), "kind of declaration changed"); // No need to check compatibility of most of the non-body parts of the node: // - Arbitrary renaming and moving between scopes is allowed. // - Annotations are ignored for compatibility purposes. if (replacement.getParameters().size() > node.getParameters().size()) { replacementIsNewer(); } else if (replacement.getParameters().size() < node.getParameters().size()) { replacementIsOlder(); } switch (node.which()) { case schema::Node::FILE: verifyVoid(node.getFile()); break; case schema::Node::STRUCT: checkCompatibility(node.getStruct(), replacement.getStruct(), node.getScopeId(), replacement.getScopeId()); break; case schema::Node::ENUM: checkCompatibility(node.getEnum(), replacement.getEnum()); break; case schema::Node::INTERFACE: checkCompatibility(node.getInterface(), replacement.getInterface()); break; case schema::Node::CONST: checkCompatibility(node.getConst(), replacement.getConst()); break; case schema::Node::ANNOTATION: checkCompatibility(node.getAnnotation(), replacement.getAnnotation()); break; } } void checkCompatibility(const schema::Node::Struct::Reader& structNode, const schema::Node::Struct::Reader& replacement, uint64_t scopeId, uint64_t replacementScopeId) { if (replacement.getDataWordCount() > structNode.getDataWordCount()) { replacementIsNewer(); } else if (replacement.getDataWordCount() < structNode.getDataWordCount()) { replacementIsOlder(); } if (replacement.getPointerCount() > structNode.getPointerCount()) { replacementIsNewer(); } else if (replacement.getPointerCount() < structNode.getPointerCount()) { replacementIsOlder(); } if (replacement.getDiscriminantCount() > structNode.getDiscriminantCount()) { replacementIsNewer(); } else if (replacement.getDiscriminantCount() < structNode.getDiscriminantCount()) { replacementIsOlder(); } if (replacement.getDiscriminantCount() > 0 && structNode.getDiscriminantCount() > 0) { VALIDATE_SCHEMA(replacement.getDiscriminantOffset() == structNode.getDiscriminantOffset(), "union discriminant position changed"); } // The shared members should occupy corresponding positions in the member lists, since the // lists are sorted by ordinal. auto fields = structNode.getFields(); auto replacementFields = replacement.getFields(); uint count = std::min(fields.size(), replacementFields.size()); if (replacementFields.size() > fields.size()) { replacementIsNewer(); } else if (replacementFields.size() < fields.size()) { replacementIsOlder(); } for (uint i = 0; i < count; i++) { checkCompatibility(fields[i], replacementFields[i]); } // For the moment, we allow "upgrading" from non-group to group, mainly so that the // placeholders we generate for group parents (which in the absence of more info, we assume to // be non-groups) can be replaced with groups. // // TODO(cleanup): The placeholder approach is really breaking down. Maybe we need to maintain // a list of expectations for nodes we haven't loaded yet. if (structNode.getIsGroup()) { if (replacement.getIsGroup()) { VALIDATE_SCHEMA(replacementScopeId == scopeId, "group node's scope changed"); } else { replacementIsOlder(); } } else { if (replacement.getIsGroup()) { replacementIsNewer(); } } } void checkCompatibility(const schema::Field::Reader& field, const schema::Field::Reader& replacement) { KJ_CONTEXT("comparing struct field", field.getName()); // A field that is initially not in a union can be upgraded to be in one, as long as it has // discriminant 0. uint discriminant = hasDiscriminantValue(field) ? field.getDiscriminantValue() : 0; uint replacementDiscriminant = hasDiscriminantValue(replacement) ? replacement.getDiscriminantValue() : 0; VALIDATE_SCHEMA(discriminant == replacementDiscriminant, "Field discriminant changed."); switch (field.which()) { case schema::Field::SLOT: { auto slot = field.getSlot(); switch (replacement.which()) { case schema::Field::SLOT: { auto replacementSlot = replacement.getSlot(); checkCompatibility(slot.getType(), replacementSlot.getType(), NO_UPGRADE_TO_STRUCT); checkDefaultCompatibility(slot.getDefaultValue(), replacementSlot.getDefaultValue()); VALIDATE_SCHEMA(slot.getOffset() == replacementSlot.getOffset(), "field position changed"); break; } case schema::Field::GROUP: checkUpgradeToStruct(slot.getType(), replacement.getGroup().getTypeId(), existingNode, field); break; } break; } case schema::Field::GROUP: switch (replacement.which()) { case schema::Field::SLOT: checkUpgradeToStruct(replacement.getSlot().getType(), field.getGroup().getTypeId(), replacementNode, replacement); break; case schema::Field::GROUP: VALIDATE_SCHEMA(field.getGroup().getTypeId() == replacement.getGroup().getTypeId(), "group id changed"); break; } break; } } void checkCompatibility(const schema::Node::Enum::Reader& enumNode, const schema::Node::Enum::Reader& replacement) { uint size = enumNode.getEnumerants().size(); uint replacementSize = replacement.getEnumerants().size(); if (replacementSize > size) { replacementIsNewer(); } else if (replacementSize < size) { replacementIsOlder(); } } void checkCompatibility(const schema::Node::Interface::Reader& interfaceNode, const schema::Node::Interface::Reader& replacement) { { // Check superclasses. kj::Vector superclasses; kj::Vector replacementSuperclasses; for (auto superclass: interfaceNode.getSuperclasses()) { superclasses.add(superclass.getId()); } for (auto superclass: replacement.getSuperclasses()) { replacementSuperclasses.add(superclass.getId()); } std::sort(superclasses.begin(), superclasses.end()); std::sort(replacementSuperclasses.begin(), replacementSuperclasses.end()); auto iter = superclasses.begin(); auto replacementIter = replacementSuperclasses.begin(); while (iter != superclasses.end() || replacementIter != replacementSuperclasses.end()) { if (iter == superclasses.end()) { replacementIsNewer(); break; } else if (replacementIter == replacementSuperclasses.end()) { replacementIsOlder(); break; } else if (*iter < *replacementIter) { replacementIsOlder(); ++iter; } else if (*iter > *replacementIter) { replacementIsNewer(); ++replacementIter; } else { ++iter; ++replacementIter; } } } auto methods = interfaceNode.getMethods(); auto replacementMethods = replacement.getMethods(); if (replacementMethods.size() > methods.size()) { replacementIsNewer(); } else if (replacementMethods.size() < methods.size()) { replacementIsOlder(); } uint count = std::min(methods.size(), replacementMethods.size()); for (uint i = 0; i < count; i++) { checkCompatibility(methods[i], replacementMethods[i]); } } void checkCompatibility(const schema::Method::Reader& method, const schema::Method::Reader& replacement) { KJ_CONTEXT("comparing method", method.getName()); // TODO(someday): Allow named parameter list to be replaced by compatible struct type. VALIDATE_SCHEMA(method.getParamStructType() == replacement.getParamStructType(), "Updated method has different parameters."); VALIDATE_SCHEMA(method.getResultStructType() == replacement.getResultStructType(), "Updated method has different results."); } void checkCompatibility(const schema::Node::Const::Reader& constNode, const schema::Node::Const::Reader& replacement) { // Who cares? These don't appear on the wire. } void checkCompatibility(const schema::Node::Annotation::Reader& annotationNode, const schema::Node::Annotation::Reader& replacement) { // Who cares? These don't appear on the wire. } enum UpgradeToStructMode { ALLOW_UPGRADE_TO_STRUCT, NO_UPGRADE_TO_STRUCT }; void checkCompatibility(const schema::Type::Reader& type, const schema::Type::Reader& replacement, UpgradeToStructMode upgradeToStructMode) { if (replacement.which() != type.which()) { // Check for allowed "upgrade" to Data or AnyPointer. if (replacement.isData() && canUpgradeToData(type)) { replacementIsNewer(); return; } else if (type.isData() && canUpgradeToData(replacement)) { replacementIsOlder(); return; } else if (replacement.isAnyPointer() && canUpgradeToAnyPointer(type)) { replacementIsNewer(); return; } else if (type.isAnyPointer() && canUpgradeToAnyPointer(replacement)) { replacementIsOlder(); return; } if (upgradeToStructMode == ALLOW_UPGRADE_TO_STRUCT) { if (type.isStruct()) { checkUpgradeToStruct(replacement, type.getStruct().getTypeId()); return; } else if (replacement.isStruct()) { checkUpgradeToStruct(type, replacement.getStruct().getTypeId()); return; } } FAIL_VALIDATE_SCHEMA("a type was changed"); } switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::ANY_POINTER: return; case schema::Type::LIST: checkCompatibility(type.getList().getElementType(), replacement.getList().getElementType(), ALLOW_UPGRADE_TO_STRUCT); return; case schema::Type::ENUM: VALIDATE_SCHEMA(replacement.getEnum().getTypeId() == type.getEnum().getTypeId(), "type changed enum type"); return; case schema::Type::STRUCT: // TODO(someday): If the IDs don't match, we should compare the two structs for // compatibility. This is tricky, though, because the new type's target may not yet be // loaded. In that case we could take the old type, make a copy of it, assign the new // ID to the copy, and load() that. That forces any struct type loaded for that ID to // be compatible. However, that has another problem, which is that it could be that the // whole reason the type was replaced was to fork that type, and so an incompatibility // could be very much expected. This could be a rat hole... VALIDATE_SCHEMA(replacement.getStruct().getTypeId() == type.getStruct().getTypeId(), "type changed to incompatible struct type"); return; case schema::Type::INTERFACE: VALIDATE_SCHEMA(replacement.getInterface().getTypeId() == type.getInterface().getTypeId(), "type changed to incompatible interface type"); return; } // We assume unknown types (from newer versions of Cap'n Proto?) are equivalent. } void checkUpgradeToStruct(const schema::Type::Reader& type, uint64_t structTypeId, kj::Maybe matchSize = nullptr, kj::Maybe matchPosition = nullptr) { // We can't just look up the target struct and check it because it may not have been loaded // yet. Instead, we contrive a struct that looks like what we want and load() that, which // guarantees that any incompatibility will be caught either now or when the real version of // that struct is loaded. word scratch[32]; memset(scratch, 0, sizeof(scratch)); MallocMessageBuilder builder(scratch); auto node = builder.initRoot(); node.setId(structTypeId); node.setDisplayName(kj::str("(unknown type used in ", nodeName, ")")); auto structNode = node.initStruct(); switch (type.which()) { case schema::Type::VOID: structNode.setDataWordCount(0); structNode.setPointerCount(0); break; case schema::Type::BOOL: structNode.setDataWordCount(1); structNode.setPointerCount(0); break; case schema::Type::INT8: case schema::Type::UINT8: structNode.setDataWordCount(1); structNode.setPointerCount(0); break; case schema::Type::INT16: case schema::Type::UINT16: case schema::Type::ENUM: structNode.setDataWordCount(1); structNode.setPointerCount(0); break; case schema::Type::INT32: case schema::Type::UINT32: case schema::Type::FLOAT32: structNode.setDataWordCount(1); structNode.setPointerCount(0); break; case schema::Type::INT64: case schema::Type::UINT64: case schema::Type::FLOAT64: structNode.setDataWordCount(1); structNode.setPointerCount(0); break; 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: structNode.setDataWordCount(0); structNode.setPointerCount(1); break; } KJ_IF_MAYBE(s, matchSize) { auto match = s->getStruct(); structNode.setDataWordCount(match.getDataWordCount()); structNode.setPointerCount(match.getPointerCount()); } auto field = structNode.initFields(1)[0]; field.setName("member0"); field.setCodeOrder(0); auto slot = field.initSlot(); slot.setType(type); KJ_IF_MAYBE(p, matchPosition) { if (p->getOrdinal().isExplicit()) { field.getOrdinal().setExplicit(p->getOrdinal().getExplicit()); } else { field.getOrdinal().setImplicit(); } auto matchSlot = p->getSlot(); slot.setOffset(matchSlot.getOffset()); slot.setDefaultValue(matchSlot.getDefaultValue()); } else { field.getOrdinal().setExplicit(0); slot.setOffset(0); schema::Value::Builder value = slot.initDefaultValue(); switch (type.which()) { case schema::Type::VOID: value.setVoid(); break; case schema::Type::BOOL: value.setBool(false); break; case schema::Type::INT8: value.setInt8(0); break; case schema::Type::INT16: value.setInt16(0); break; case schema::Type::INT32: value.setInt32(0); break; case schema::Type::INT64: value.setInt64(0); break; case schema::Type::UINT8: value.setUint8(0); break; case schema::Type::UINT16: value.setUint16(0); break; case schema::Type::UINT32: value.setUint32(0); break; case schema::Type::UINT64: value.setUint64(0); break; case schema::Type::FLOAT32: value.setFloat32(0); break; case schema::Type::FLOAT64: value.setFloat64(0); break; case schema::Type::ENUM: value.setEnum(0); break; case schema::Type::TEXT: value.adoptText(Orphan()); break; case schema::Type::DATA: value.adoptData(Orphan()); break; case schema::Type::LIST: value.initList(); break; case schema::Type::STRUCT: value.initStruct(); break; case schema::Type::INTERFACE: value.setInterface(); break; case schema::Type::ANY_POINTER: value.initAnyPointer(); break; } } loader.load(node, true); } bool canUpgradeToData(const schema::Type::Reader& type) { if (type.isText()) { return true; } else if (type.isList()) { switch (type.getList().getElementType().which()) { case schema::Type::INT8: case schema::Type::UINT8: return true; default: return false; } } else { return false; } } bool canUpgradeToAnyPointer(const schema::Type::Reader& type) { switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: return false; 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: return true; } // Be lenient with unknown types. return true; } void checkDefaultCompatibility(const schema::Value::Reader& value, const schema::Value::Reader& replacement) { // Note that we test default compatibility only after testing type compatibility, and default // values have already been validated as matching their types, so this should pass. KJ_ASSERT(value.which() == replacement.which()) { compatibility = INCOMPATIBLE; return; } switch (value.which()) { #define HANDLE_TYPE(discrim, name) \ case schema::Value::discrim: \ VALIDATE_SCHEMA(value.get##name() == replacement.get##name(), "default value changed"); \ break; HANDLE_TYPE(VOID, Void); HANDLE_TYPE(BOOL, Bool); HANDLE_TYPE(INT8, Int8); HANDLE_TYPE(INT16, Int16); HANDLE_TYPE(INT32, Int32); HANDLE_TYPE(INT64, Int64); HANDLE_TYPE(UINT8, Uint8); HANDLE_TYPE(UINT16, Uint16); HANDLE_TYPE(UINT32, Uint32); HANDLE_TYPE(UINT64, Uint64); HANDLE_TYPE(FLOAT32, Float32); HANDLE_TYPE(FLOAT64, Float64); HANDLE_TYPE(ENUM, Enum); #undef HANDLE_TYPE case schema::Value::TEXT: case schema::Value::DATA: case schema::Value::LIST: case schema::Value::STRUCT: case schema::Value::INTERFACE: case schema::Value::ANY_POINTER: // It's not a big deal if default values for pointers change, and it would be difficult for // us to compare these defaults here, so just let it slide. break; } } }; // ======================================================================================= _::RawSchema* SchemaLoader::Impl::load(const schema::Node::Reader& reader, bool isPlaceholder) { // Make a copy of the node which can be used unchecked. kj::ArrayPtr validated = makeUncheckedNodeEnforcingSizeRequirements(reader); // Validate the copy. Validator validator(*this); auto validatedReader = readMessageUnchecked(validated.begin()); if (!validator.validate(validatedReader)) { // Not valid. Construct an empty schema of the same type and return that. return loadEmpty(validatedReader.getId(), validatedReader.getDisplayName(), validatedReader.which(), false); } // Check if we already have a schema for this ID. _::RawSchema* schema; bool shouldReplace; bool shouldClearInitializer; KJ_IF_MAYBE(match, schemas.find(validatedReader.getId())) { // Yes, check if it is compatible and figure out which schema is newer. schema = *match; // If the existing schema is a placeholder, but we're upgrading it to a non-placeholder, we // need to clear the initializer later. shouldClearInitializer = schema->lazyInitializer != nullptr && !isPlaceholder; auto existing = readMessageUnchecked(schema->encodedNode); CompatibilityChecker checker(*this); // Prefer to replace the existing schema if the existing schema is a placeholder. Otherwise, // prefer to keep the existing schema. shouldReplace = checker.shouldReplace( existing, validatedReader, schema->lazyInitializer != nullptr); } else { // Nope, allocate a new RawSchema. schema = &arena.allocate<_::RawSchema>(); memset(&schema->defaultBrand, 0, sizeof(schema->defaultBrand)); schema->id = validatedReader.getId(); schema->canCastTo = nullptr; schema->defaultBrand.generic = schema; schema->lazyInitializer = isPlaceholder ? &initializer : nullptr; schema->defaultBrand.lazyInitializer = isPlaceholder ? &brandedInitializer : nullptr; shouldReplace = true; shouldClearInitializer = false; schemas.insert(validatedReader.getId(), schema); } if (shouldReplace) { // Initialize the RawSchema. schema->encodedNode = validated.begin(); schema->encodedSize = validated.size(); schema->dependencies = validator.makeDependencyArray(&schema->dependencyCount); schema->membersByName = validator.makeMemberInfoArray(&schema->memberCount); schema->membersByDiscriminant = validator.makeMembersByDiscriminantArray(); // Even though this schema isn't itself branded, it may have dependencies that are. So, we // need to set up the "dependencies" map under defaultBrand. auto deps = makeBrandedDependencies(schema, kj::ArrayPtr()); schema->defaultBrand.dependencies = deps.begin(); schema->defaultBrand.dependencyCount = deps.size(); } if (shouldClearInitializer) { // If this schema is not newly-allocated, it may already be in the wild, specifically in the // dependency list of other schemas. Once the initializer is null, it is live, so we must do // a release-store here. #if __GNUC__ __atomic_store_n(&schema->lazyInitializer, nullptr, __ATOMIC_RELEASE); __atomic_store_n(&schema->defaultBrand.lazyInitializer, nullptr, __ATOMIC_RELEASE); #elif _MSC_VER std::atomic_thread_fence(std::memory_order_release); *static_cast<_::RawSchema::Initializer const* volatile*>(&schema->lazyInitializer) = nullptr; *static_cast<_::RawBrandedSchema::Initializer const* volatile*>( &schema->defaultBrand.lazyInitializer) = nullptr; #else #error "Platform not supported" #endif } return schema; } _::RawSchema* SchemaLoader::Impl::loadNative(const _::RawSchema* nativeSchema) { _::RawSchema* schema; bool shouldReplace; bool shouldClearInitializer; KJ_IF_MAYBE(match, schemas.find(nativeSchema->id)) { schema = *match; if (schema->canCastTo != nullptr) { // Already loaded natively, or we're currently in the process of loading natively and there // was a dependency cycle. KJ_REQUIRE(schema->canCastTo == nativeSchema, "two different compiled-in type have the same type ID", nativeSchema->id, readMessageUnchecked(nativeSchema->encodedNode).getDisplayName(), readMessageUnchecked(schema->canCastTo->encodedNode).getDisplayName()); return schema; } else { auto existing = readMessageUnchecked(schema->encodedNode); auto native = readMessageUnchecked(nativeSchema->encodedNode); CompatibilityChecker checker(*this); shouldReplace = checker.shouldReplace(existing, native, true); shouldClearInitializer = schema->lazyInitializer != nullptr; } } else { schema = &arena.allocate<_::RawSchema>(); memset(&schema->defaultBrand, 0, sizeof(schema->defaultBrand)); schema->defaultBrand.generic = schema; schema->lazyInitializer = nullptr; schema->defaultBrand.lazyInitializer = nullptr; shouldReplace = true; shouldClearInitializer = false; // already cleared above schemas.insert(nativeSchema->id, schema); } if (shouldReplace) { // Set the schema to a copy of the native schema, but make sure not to null out lazyInitializer // yet. _::RawSchema temp = *nativeSchema; temp.lazyInitializer = schema->lazyInitializer; *schema = temp; schema->defaultBrand.generic = schema; // Indicate that casting is safe. Note that it's important to set this before recursively // loading dependencies, so that cycles don't cause infinite loops! schema->canCastTo = nativeSchema; // We need to set the dependency list to point at other loader-owned RawSchemas. kj::ArrayPtr dependencies = arena.allocateArray(schema->dependencyCount); for (uint i = 0; i < nativeSchema->dependencyCount; i++) { dependencies[i] = loadNative(nativeSchema->dependencies[i]); } schema->dependencies = dependencies.begin(); // Also need to re-do the branded dependencies. auto deps = makeBrandedDependencies(schema, kj::ArrayPtr()); schema->defaultBrand.dependencies = deps.begin(); schema->defaultBrand.dependencyCount = deps.size(); // If there is a struct size requirement, we need to make sure that it is satisfied. KJ_IF_MAYBE(sizeReq, structSizeRequirements.find(nativeSchema->id)) { applyStructSizeRequirement(schema, sizeReq->dataWordCount, sizeReq->pointerCount); } } else { // The existing schema is newer. // Indicate that casting is safe. Note that it's important to set this before recursively // loading dependencies, so that cycles don't cause infinite loops! schema->canCastTo = nativeSchema; // Make sure the dependencies are loaded and compatible. for (uint i = 0; i < nativeSchema->dependencyCount; i++) { loadNative(nativeSchema->dependencies[i]); } } if (shouldClearInitializer) { // If this schema is not newly-allocated, it may already be in the wild, specifically in the // dependency list of other schemas. Once the initializer is null, it is live, so we must do // a release-store here. #if __GNUC__ __atomic_store_n(&schema->lazyInitializer, nullptr, __ATOMIC_RELEASE); __atomic_store_n(&schema->defaultBrand.lazyInitializer, nullptr, __ATOMIC_RELEASE); #elif _MSC_VER std::atomic_thread_fence(std::memory_order_release); *static_cast<_::RawSchema::Initializer const* volatile*>(&schema->lazyInitializer) = nullptr; *static_cast<_::RawBrandedSchema::Initializer const* volatile*>( &schema->defaultBrand.lazyInitializer) = nullptr; #else #error "Platform not supported" #endif } return schema; } _::RawSchema* SchemaLoader::Impl::loadEmpty( uint64_t id, kj::StringPtr name, schema::Node::Which kind, bool isPlaceholder) { word scratch[32]; memset(scratch, 0, sizeof(scratch)); MallocMessageBuilder builder(scratch); auto node = builder.initRoot(); node.setId(id); node.setDisplayName(name); switch (kind) { case schema::Node::STRUCT: node.initStruct(); break; case schema::Node::ENUM: node.initEnum(); break; case schema::Node::INTERFACE: node.initInterface(); break; case schema::Node::FILE: case schema::Node::CONST: case schema::Node::ANNOTATION: KJ_FAIL_REQUIRE("Not a type."); break; } return load(node, isPlaceholder); } const _::RawBrandedSchema* SchemaLoader::Impl::makeBranded( const _::RawSchema* schema, schema::Brand::Reader proto, kj::Maybe> clientBrand) { kj::StringPtr scopeName = readMessageUnchecked(schema->encodedNode).getDisplayName(); auto srcScopes = proto.getScopes(); KJ_STACK_ARRAY(_::RawBrandedSchema::Scope, dstScopes, srcScopes.size(), 16, 32); memset(dstScopes.begin(), 0, dstScopes.size() * sizeof(dstScopes[0])); uint dstScopeCount = 0; for (auto srcScope: srcScopes) { switch (srcScope.which()) { case schema::Brand::Scope::BIND: { auto srcBindings = srcScope.getBind(); KJ_STACK_ARRAY(_::RawBrandedSchema::Binding, dstBindings, srcBindings.size(), 16, 32); memset(dstBindings.begin(), 0, dstBindings.size() * sizeof(dstBindings[0])); for (auto j: kj::indices(srcBindings)) { auto srcBinding = srcBindings[j]; auto& dstBinding = dstBindings[j]; memset(&dstBinding, 0, sizeof(dstBinding)); dstBinding.which = schema::Type::ANY_POINTER; switch (srcBinding.which()) { case schema::Brand::Binding::UNBOUND: break; case schema::Brand::Binding::TYPE: { makeDep(dstBinding, srcBinding.getType(), scopeName, clientBrand); break; } } } auto& dstScope = dstScopes[dstScopeCount++]; dstScope.typeId = srcScope.getScopeId(); dstScope.bindingCount = dstBindings.size(); dstScope.bindings = copyDeduped(dstBindings).begin(); break; } case schema::Brand::Scope::INHERIT: { // Inherit the whole scope from the client -- or if the client doesn't have it, at least // include an empty dstScope in the list just to show that this scope was specified as // inherited, as opposed to being unspecified (which would be treated as all AnyPointer). auto& dstScope = dstScopes[dstScopeCount++]; dstScope.typeId = srcScope.getScopeId(); KJ_IF_MAYBE(b, clientBrand) { for (auto& clientScope: *b) { if (clientScope.typeId == dstScope.typeId) { // Overwrite the whole thing. dstScope = clientScope; break; } } } else { dstScope.isUnbound = true; } break; } } } dstScopes = dstScopes.slice(0, dstScopeCount); std::sort(dstScopes.begin(), dstScopes.end(), [](const _::RawBrandedSchema::Scope& a, const _::RawBrandedSchema::Scope& b) { return a.typeId < b.typeId; }); return makeBranded(schema, copyDeduped(dstScopes)); } const _::RawBrandedSchema* SchemaLoader::Impl::makeBranded( const _::RawSchema* schema, kj::ArrayPtr bindings) { if (bindings.size() == 0) { // `defaultBrand` is the version where all type parameters are bound to `AnyPointer`. return &schema->defaultBrand; } SchemaBindingsPair key { schema, bindings.begin() }; KJ_IF_MAYBE(existing, brands.find(key)) { return *existing; } else { auto& brand = arena.allocate<_::RawBrandedSchema>(); memset(&brand, 0, sizeof(brand)); brands.insert(key, &brand); brand.generic = schema; brand.scopes = bindings.begin(); brand.scopeCount = bindings.size(); brand.lazyInitializer = &brandedInitializer; return &brand; } } kj::ArrayPtr SchemaLoader::Impl::makeBrandedDependencies( const _::RawSchema* schema, kj::Maybe> bindings) { kj::StringPtr scopeName = readMessageUnchecked(schema->encodedNode).getDisplayName(); kj::Vector<_::RawBrandedSchema::Dependency> deps; schema::Node::Reader node = readMessageUnchecked(schema->encodedNode); #define ADD_ENTRY(kind, index, make) \ if (const _::RawBrandedSchema* dep = make) { \ auto& slot = deps.add(); \ memset(&slot, 0, sizeof(slot)); \ slot.location = _::RawBrandedSchema::makeDepLocation( \ _::RawBrandedSchema::DepKind::kind, index); \ slot.schema = dep; \ } switch (node.which()) { case schema::Node::FILE: case schema::Node::ENUM: case schema::Node::ANNOTATION: break; case schema::Node::CONST: ADD_ENTRY(CONST_TYPE, 0, makeDepSchema( node.getConst().getType(), scopeName, bindings)); break; case schema::Node::STRUCT: { auto fields = node.getStruct().getFields(); for (auto i: kj::indices(fields)) { auto field = fields[i]; switch (field.which()) { case schema::Field::SLOT: ADD_ENTRY(FIELD, i, makeDepSchema( field.getSlot().getType(), scopeName, bindings)) break; case schema::Field::GROUP: { const _::RawSchema* group = loadEmpty( field.getGroup().getTypeId(), "(unknown group type)", schema::Node::STRUCT, true); KJ_IF_MAYBE(b, bindings) { ADD_ENTRY(FIELD, i, makeBranded(group, *b)); } else { ADD_ENTRY(FIELD, i, getUnbound(group)); } break; } } } break; } case schema::Node::INTERFACE: { auto interface = node.getInterface(); { auto superclasses = interface.getSuperclasses(); for (auto i: kj::indices(superclasses)) { auto superclass = superclasses[i]; ADD_ENTRY(SUPERCLASS, i, makeDepSchema( superclass.getId(), schema::Type::INTERFACE, schema::Node::INTERFACE, superclass.getBrand(), scopeName, bindings)) } } { auto methods = interface.getMethods(); for (auto i: kj::indices(methods)) { auto method = methods[i]; ADD_ENTRY(METHOD_PARAMS, i, makeDepSchema( method.getParamStructType(), schema::Type::STRUCT, schema::Node::STRUCT, method.getParamBrand(), scopeName, bindings)) ADD_ENTRY(METHOD_RESULTS, i, makeDepSchema( method.getResultStructType(), schema::Type::STRUCT, schema::Node::STRUCT, method.getResultBrand(), scopeName, bindings)) } } break; } } #undef ADD_ENTRY std::sort(deps.begin(), deps.end(), [](const _::RawBrandedSchema::Dependency& a, const _::RawBrandedSchema::Dependency& b) { return a.location < b.location; }); return copyDeduped(deps.asPtr()); } void SchemaLoader::Impl::makeDep(_::RawBrandedSchema::Binding& result, schema::Type::Reader type, kj::StringPtr scopeName, kj::Maybe> brandBindings) { switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: result.which = static_cast(type.which()); return; case schema::Type::STRUCT: { auto structType = type.getStruct(); makeDep(result, structType.getTypeId(), schema::Type::STRUCT, schema::Node::STRUCT, structType.getBrand(), scopeName, brandBindings); return; } case schema::Type::ENUM: { auto enumType = type.getEnum(); makeDep(result, enumType.getTypeId(), schema::Type::ENUM, schema::Node::ENUM, enumType.getBrand(), scopeName, brandBindings); return; } case schema::Type::INTERFACE: { auto interfaceType = type.getInterface(); makeDep(result, interfaceType.getTypeId(), schema::Type::INTERFACE, schema::Node::INTERFACE, interfaceType.getBrand(), scopeName, brandBindings); return; } case schema::Type::LIST: { makeDep(result, type.getList().getElementType(), scopeName, brandBindings); ++result.listDepth; return; } case schema::Type::ANY_POINTER: { result.which = static_cast(schema::Type::ANY_POINTER); auto anyPointer = type.getAnyPointer(); switch (anyPointer.which()) { case schema::Type::AnyPointer::UNCONSTRAINED: return; case schema::Type::AnyPointer::PARAMETER: { auto param = anyPointer.getParameter(); uint64_t id = param.getScopeId(); uint16_t index = param.getParameterIndex(); KJ_IF_MAYBE(b, brandBindings) { // TODO(perf): We could binary search here, but... bleh. for (auto& scope: *b) { if (scope.typeId == id) { if (scope.isUnbound) { // Unbound brand parameter. result.scopeId = id; result.paramIndex = index; return; } else if (index >= scope.bindingCount) { // Binding index out-of-range. Treat as AnyPointer. This is important to allow // new type parameters to be added to existing types without breaking dependent // schemas. return; } else { result = scope.bindings[index]; return; } } } return; } else { // Unbound brand parameter. result.scopeId = id; result.paramIndex = index; return; } } case schema::Type::AnyPointer::IMPLICIT_METHOD_PARAMETER: result.isImplicitParameter = true; result.paramIndex = anyPointer.getImplicitMethodParameter().getParameterIndex(); return; } KJ_UNREACHABLE; } } KJ_UNREACHABLE; } void SchemaLoader::Impl::makeDep(_::RawBrandedSchema::Binding& result, uint64_t typeId, schema::Type::Which whichType, schema::Node::Which expectedKind, schema::Brand::Reader brand, kj::StringPtr scopeName, kj::Maybe> brandBindings) { const _::RawSchema* schema; if (typeId == capnp::typeId()) { // StreamResult is a very special type that is used to mark when a method is declared as // streaming ("foo @0 () -> stream;"). We like to auto-load it if we see it as someone's // dependency. schema = loadNative(&_::rawSchema()); } else { schema = loadEmpty(typeId, kj::str("(unknown type; seen as dependency of ", scopeName, ")"), expectedKind, true); } result.which = static_cast(whichType); result.schema = makeBranded(schema, brand, brandBindings); } const _::RawBrandedSchema* SchemaLoader::Impl::makeDepSchema( schema::Type::Reader type, kj::StringPtr scopeName, kj::Maybe> brandBindings) { _::RawBrandedSchema::Binding binding; memset(&binding, 0, sizeof(binding)); makeDep(binding, type, scopeName, brandBindings); return binding.schema; } const _::RawBrandedSchema* SchemaLoader::Impl::makeDepSchema( uint64_t typeId, schema::Type::Which whichType, schema::Node::Which expectedKind, schema::Brand::Reader brand, kj::StringPtr scopeName, kj::Maybe> brandBindings) { _::RawBrandedSchema::Binding binding; memset(&binding, 0, sizeof(binding)); makeDep(binding, typeId, whichType, expectedKind, brand, scopeName, brandBindings); return binding.schema; } template kj::ArrayPtr SchemaLoader::Impl::copyDeduped(kj::ArrayPtr values) { if (values.size() == 0) { return kj::arrayPtr(kj::implicitCast(nullptr), 0); } auto bytes = values.asBytes(); KJ_IF_MAYBE(dupe, dedupTable.find(bytes)) { return kj::arrayPtr(reinterpret_cast(dupe->begin()), values.size()); } // Need to make a new copy. auto copy = arena.allocateArray(values.size()); memcpy(copy.begin(), values.begin(), values.size() * sizeof(T)); dedupTable.insert(copy.asBytes()); return copy; } template kj::ArrayPtr SchemaLoader::Impl::copyDeduped(kj::ArrayPtr values) { return copyDeduped(kj::ArrayPtr(values)); } SchemaLoader::Impl::TryGetResult SchemaLoader::Impl::tryGet(uint64_t typeId) const { KJ_IF_MAYBE(schema, schemas.find(typeId)) { return {*schema, initializer.getCallback()}; } else { return {nullptr, initializer.getCallback()}; } } const _::RawBrandedSchema* SchemaLoader::Impl::getUnbound(const _::RawSchema* schema) { if (!readMessageUnchecked(schema->encodedNode).getIsGeneric()) { // Not a generic type, so just return the default brand. return &schema->defaultBrand; } KJ_IF_MAYBE(existing, unboundBrands.find(schema)) { return *existing; } else { auto slot = &arena.allocate<_::RawBrandedSchema>(); memset(slot, 0, sizeof(*slot)); slot->generic = schema; auto deps = makeBrandedDependencies(schema, nullptr); slot->dependencies = deps.begin(); slot->dependencyCount = deps.size(); unboundBrands.insert(schema, slot); return slot; } } kj::Array SchemaLoader::Impl::getAllLoaded() const { size_t count = 0; for (auto& schema: schemas) { if (schema.value->lazyInitializer == nullptr) ++count; } kj::Array result = kj::heapArray(count); size_t i = 0; for (auto& schema: schemas) { if (schema.value->lazyInitializer == nullptr) { result[i++] = Schema(&schema.value->defaultBrand); } } return result; } void SchemaLoader::Impl::requireStructSize(uint64_t id, uint dataWordCount, uint pointerCount) { structSizeRequirements.upsert(id, { uint16_t(dataWordCount), uint16_t(pointerCount) }, [&](RequiredSize& existingValue, RequiredSize&& newValue) { existingValue.dataWordCount = kj::max(existingValue.dataWordCount, newValue.dataWordCount); existingValue.pointerCount = kj::max(existingValue.pointerCount, newValue.pointerCount); }); KJ_IF_MAYBE(schema, schemas.find(id)) { applyStructSizeRequirement(*schema, dataWordCount, pointerCount); } } kj::ArrayPtr SchemaLoader::Impl::makeUncheckedNode(schema::Node::Reader node) { size_t size = node.totalSize().wordCount + 1; kj::ArrayPtr result = arena.allocateArray(size); memset(result.begin(), 0, size * sizeof(word)); copyToUnchecked(node, result); return result; } kj::ArrayPtr SchemaLoader::Impl::makeUncheckedNodeEnforcingSizeRequirements( schema::Node::Reader node) { if (node.isStruct()) { KJ_IF_MAYBE(requirement, structSizeRequirements.find(node.getId())) { auto structNode = node.getStruct(); if (structNode.getDataWordCount() < requirement->dataWordCount || structNode.getPointerCount() < requirement->pointerCount) { return rewriteStructNodeWithSizes(node, requirement->dataWordCount, requirement->pointerCount); } } } return makeUncheckedNode(node); } kj::ArrayPtr SchemaLoader::Impl::rewriteStructNodeWithSizes( schema::Node::Reader node, uint dataWordCount, uint pointerCount) { MallocMessageBuilder builder; builder.setRoot(node); auto root = builder.getRoot(); auto newStruct = root.getStruct(); newStruct.setDataWordCount(kj::max(newStruct.getDataWordCount(), dataWordCount)); newStruct.setPointerCount(kj::max(newStruct.getPointerCount(), pointerCount)); return makeUncheckedNode(root); } void SchemaLoader::Impl::applyStructSizeRequirement( _::RawSchema* raw, uint dataWordCount, uint pointerCount) { auto node = readMessageUnchecked(raw->encodedNode); auto structNode = node.getStruct(); if (structNode.getDataWordCount() < dataWordCount || structNode.getPointerCount() < pointerCount) { // Sizes need to be increased. Must rewrite. kj::ArrayPtr words = rewriteStructNodeWithSizes(node, dataWordCount, pointerCount); // We don't need to re-validate the node because we know this change could not possibly have // invalidated it. Just remake the unchecked message. raw->encodedNode = words.begin(); raw->encodedSize = words.size(); } } void SchemaLoader::InitializerImpl::init(const _::RawSchema* schema) const { KJ_IF_MAYBE(c, callback) { c->load(loader, schema->id); } if (schema->lazyInitializer != nullptr) { // The callback declined to load a schema. We need to disable the initializer so that it // doesn't get invoked again later, as we can no longer modify this schema once it is in use. // Lock the loader for read to make sure no one is concurrently loading a replacement for this // schema node. auto lock = loader.impl.lockShared(); // Get the mutable version of the schema. _::RawSchema* mutableSchema = lock->get()->tryGet(schema->id).schema; KJ_ASSERT(mutableSchema == schema, "A schema not belonging to this loader used its initializer."); // Disable the initializer. #if __GNUC__ __atomic_store_n(&mutableSchema->lazyInitializer, nullptr, __ATOMIC_RELEASE); __atomic_store_n(&mutableSchema->defaultBrand.lazyInitializer, nullptr, __ATOMIC_RELEASE); #elif _MSC_VER std::atomic_thread_fence(std::memory_order_release); *static_cast<_::RawSchema::Initializer const* volatile*>( &mutableSchema->lazyInitializer) = nullptr; *static_cast<_::RawBrandedSchema::Initializer const* volatile*>( &mutableSchema->defaultBrand.lazyInitializer) = nullptr; #else #error "Platform not supported" #endif } } void SchemaLoader::BrandedInitializerImpl::init(const _::RawBrandedSchema* schema) const { schema->generic->ensureInitialized(); auto lock = loader.impl.lockExclusive(); if (schema->lazyInitializer == nullptr) { // Never mind, someone beat us to it. return; } // Get the mutable version. _::RawBrandedSchema* mutableSchema = KJ_ASSERT_NONNULL( lock->get()->brands.find(SchemaBindingsPair { schema->generic, schema->scopes })); KJ_ASSERT(mutableSchema == schema); // Construct its dependency map. auto deps = lock->get()->makeBrandedDependencies(mutableSchema->generic, kj::arrayPtr(mutableSchema->scopes, mutableSchema->scopeCount)); mutableSchema->dependencies = deps.begin(); mutableSchema->dependencyCount = deps.size(); // It's initialized now, so disable the initializer. #if __GNUC__ __atomic_store_n(&mutableSchema->lazyInitializer, nullptr, __ATOMIC_RELEASE); #elif _MSC_VER std::atomic_thread_fence(std::memory_order_release); *static_cast<_::RawBrandedSchema::Initializer const* volatile*>( &mutableSchema->lazyInitializer) = nullptr; #else #error "Platform not supported" #endif } // ======================================================================================= SchemaLoader::SchemaLoader(): impl(kj::heap(*this)) {} SchemaLoader::SchemaLoader(const LazyLoadCallback& callback) : impl(kj::heap(*this, callback)) {} SchemaLoader::~SchemaLoader() noexcept(false) {} Schema SchemaLoader::get(uint64_t id, schema::Brand::Reader brand, Schema scope) const { KJ_IF_MAYBE(result, tryGet(id, brand, scope)) { return *result; } else { KJ_FAIL_REQUIRE("no schema node loaded for id", kj::hex(id)); } } kj::Maybe SchemaLoader::tryGet( uint64_t id, schema::Brand::Reader brand, Schema scope) const { auto getResult = impl.lockShared()->get()->tryGet(id); if (getResult.schema == nullptr || getResult.schema->lazyInitializer != nullptr) { // This schema couldn't be found or has yet to be lazily loaded. If we have a lazy loader // callback, invoke it now to try to get it to load this schema. KJ_IF_MAYBE(c, getResult.callback) { c->load(*this, id); } getResult = impl.lockShared()->get()->tryGet(id); } if (getResult.schema != nullptr && getResult.schema->lazyInitializer == nullptr) { if (brand.getScopes().size() > 0) { auto brandedSchema = impl.lockExclusive()->get()->makeBranded( getResult.schema, brand, scope.raw->isUnbound() ? kj::Maybe>(nullptr) : kj::arrayPtr(scope.raw->scopes, scope.raw->scopeCount)); brandedSchema->ensureInitialized(); return Schema(brandedSchema); } else { return Schema(&getResult.schema->defaultBrand); } } else { return nullptr; } } Schema SchemaLoader::getUnbound(uint64_t id) const { auto schema = get(id); return Schema(impl.lockExclusive()->get()->getUnbound(schema.raw->generic)); } Type SchemaLoader::getType(schema::Type::Reader proto, Schema scope) const { switch (proto.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::TEXT: case schema::Type::DATA: return proto.which(); case schema::Type::STRUCT: { auto structType = proto.getStruct(); return get(structType.getTypeId(), structType.getBrand(), scope).asStruct(); } case schema::Type::ENUM: { auto enumType = proto.getEnum(); return get(enumType.getTypeId(), enumType.getBrand(), scope).asEnum(); } case schema::Type::INTERFACE: { auto interfaceType = proto.getInterface(); return get(interfaceType.getTypeId(), interfaceType.getBrand(), scope) .asInterface(); } case schema::Type::LIST: return ListSchema::of(getType(proto.getList().getElementType(), scope)); case schema::Type::ANY_POINTER: { auto anyPointer = proto.getAnyPointer(); switch (anyPointer.which()) { case schema::Type::AnyPointer::UNCONSTRAINED: return schema::Type::ANY_POINTER; case schema::Type::AnyPointer::PARAMETER: { auto param = anyPointer.getParameter(); return scope.getBrandBinding(param.getScopeId(), param.getParameterIndex()); } case schema::Type::AnyPointer::IMPLICIT_METHOD_PARAMETER: // We don't support binding implicit method params here. return schema::Type::ANY_POINTER; } KJ_UNREACHABLE; } } KJ_UNREACHABLE; } Schema SchemaLoader::load(const schema::Node::Reader& reader) { return Schema(&impl.lockExclusive()->get()->load(reader, false)->defaultBrand); } Schema SchemaLoader::loadOnce(const schema::Node::Reader& reader) const { auto locked = impl.lockExclusive(); auto getResult = locked->get()->tryGet(reader.getId()); if (getResult.schema == nullptr || getResult.schema->lazyInitializer != nullptr) { // Doesn't exist yet, or the existing schema is a placeholder and therefore has not yet been // seen publicly. Go ahead and load the incoming reader. return Schema(&locked->get()->load(reader, false)->defaultBrand); } else { return Schema(&getResult.schema->defaultBrand); } } kj::Array SchemaLoader::getAllLoaded() const { return impl.lockShared()->get()->getAllLoaded(); } void SchemaLoader::loadNative(const _::RawSchema* nativeSchema) { impl.lockExclusive()->get()->loadNative(nativeSchema); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/dynamic.c++0000644000175000017500000023026613650101756020573 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "dynamic.h" #include namespace capnp { namespace { bool hasDiscriminantValue(const schema::Field::Reader& reader) { return reader.getDiscriminantValue() != schema::Field::NO_DISCRIMINANT; } template KJ_ALWAYS_INLINE(T bitCast(U value)); template inline T bitCast(U value) { static_assert(sizeof(T) == sizeof(U), "Size must match."); return value; } template <> inline float bitCast(uint32_t value) KJ_UNUSED; template <> inline float bitCast(uint32_t value) { float result; memcpy(&result, &value, sizeof(value)); return result; } template <> inline double bitCast(uint64_t value) KJ_UNUSED; template <> inline double bitCast(uint64_t value) { double result; memcpy(&result, &value, sizeof(value)); return result; } template <> inline uint32_t bitCast(float value) { uint32_t result; memcpy(&result, &value, sizeof(value)); return result; } template <> inline uint64_t bitCast(double value) { uint64_t result; memcpy(&result, &value, sizeof(value)); return result; } ElementSize elementSizeFor(schema::Type::Which elementType) { switch (elementType) { case schema::Type::VOID: return ElementSize::VOID; case schema::Type::BOOL: return ElementSize::BIT; case schema::Type::INT8: return ElementSize::BYTE; case schema::Type::INT16: return ElementSize::TWO_BYTES; case schema::Type::INT32: return ElementSize::FOUR_BYTES; case schema::Type::INT64: return ElementSize::EIGHT_BYTES; case schema::Type::UINT8: return ElementSize::BYTE; case schema::Type::UINT16: return ElementSize::TWO_BYTES; case schema::Type::UINT32: return ElementSize::FOUR_BYTES; case schema::Type::UINT64: return ElementSize::EIGHT_BYTES; case schema::Type::FLOAT32: return ElementSize::FOUR_BYTES; case schema::Type::FLOAT64: return ElementSize::EIGHT_BYTES; case schema::Type::TEXT: return ElementSize::POINTER; case schema::Type::DATA: return ElementSize::POINTER; case schema::Type::LIST: return ElementSize::POINTER; case schema::Type::ENUM: return ElementSize::TWO_BYTES; case schema::Type::STRUCT: return ElementSize::INLINE_COMPOSITE; case schema::Type::INTERFACE: return ElementSize::POINTER; case schema::Type::ANY_POINTER: KJ_FAIL_ASSERT("List(AnyPointer) not supported."); break; } // Unknown type. Treat it as zero-size. return ElementSize::VOID; } inline _::StructSize structSizeFromSchema(StructSchema schema) { auto node = schema.getProto().getStruct(); return _::StructSize( bounded(node.getDataWordCount()) * WORDS, bounded(node.getPointerCount()) * POINTERS); } } // namespace // ======================================================================================= kj::Maybe DynamicEnum::getEnumerant() const { auto enumerants = schema.getEnumerants(); if (value < enumerants.size()) { return enumerants[value]; } else { return nullptr; } } uint16_t DynamicEnum::asImpl(uint64_t requestedTypeId) const { KJ_REQUIRE(requestedTypeId == schema.getProto().getId(), "Type mismatch in DynamicEnum.as().") { // use it anyway break; } return value; } // ======================================================================================= bool DynamicStruct::Reader::isSetInUnion(StructSchema::Field field) const { auto proto = field.getProto(); if (hasDiscriminantValue(proto)) { uint16_t discrim = reader.getDataField( assumeDataOffset(schema.getProto().getStruct().getDiscriminantOffset())); return discrim == proto.getDiscriminantValue(); } else { return true; } } void DynamicStruct::Reader::verifySetInUnion(StructSchema::Field field) const { KJ_REQUIRE(isSetInUnion(field), "Tried to get() a union member which is not currently initialized.", field.getProto().getName(), schema.getProto().getDisplayName()); } bool DynamicStruct::Builder::isSetInUnion(StructSchema::Field field) { auto proto = field.getProto(); if (hasDiscriminantValue(proto)) { uint16_t discrim = builder.getDataField( assumeDataOffset(schema.getProto().getStruct().getDiscriminantOffset())); return discrim == proto.getDiscriminantValue(); } else { return true; } } void DynamicStruct::Builder::verifySetInUnion(StructSchema::Field field) { KJ_REQUIRE(isSetInUnion(field), "Tried to get() a union member which is not currently initialized.", field.getProto().getName(), schema.getProto().getDisplayName()); } void DynamicStruct::Builder::setInUnion(StructSchema::Field field) { // If a union member, set the discriminant to match. auto proto = field.getProto(); if (hasDiscriminantValue(proto)) { builder.setDataField( assumeDataOffset(schema.getProto().getStruct().getDiscriminantOffset()), proto.getDiscriminantValue()); } } DynamicValue::Reader DynamicStruct::Reader::get(StructSchema::Field field) const { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); verifySetInUnion(field); auto type = field.getType(); auto proto = field.getProto(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); // Note that the default value might be "anyPointer" even if the type is some poniter type // *other than* anyPointer. This happens with generics -- the field is actually a generic // parameter that has been bound, but the default value was of course compiled without any // binding available. auto dval = slot.getDefaultValue(); switch (type.which()) { case schema::Type::VOID: return reader.getDataField(assumeDataOffset(slot.getOffset())); #define HANDLE_TYPE(discrim, titleCase, type) \ case schema::Type::discrim: \ return reader.getDataField( \ assumeDataOffset(slot.getOffset()), \ bitCast<_::Mask>(dval.get##titleCase())); HANDLE_TYPE(BOOL, Bool, bool) HANDLE_TYPE(INT8, Int8, int8_t) HANDLE_TYPE(INT16, Int16, int16_t) HANDLE_TYPE(INT32, Int32, int32_t) HANDLE_TYPE(INT64, Int64, int64_t) HANDLE_TYPE(UINT8, Uint8, uint8_t) HANDLE_TYPE(UINT16, Uint16, uint16_t) HANDLE_TYPE(UINT32, Uint32, uint32_t) HANDLE_TYPE(UINT64, Uint64, uint64_t) HANDLE_TYPE(FLOAT32, Float32, float) HANDLE_TYPE(FLOAT64, Float64, double) #undef HANDLE_TYPE case schema::Type::ENUM: { uint16_t typedDval = dval.getEnum(); return DynamicEnum(type.asEnum(), reader.getDataField(assumeDataOffset(slot.getOffset()), typedDval)); } case schema::Type::TEXT: { Text::Reader typedDval = dval.isAnyPointer() ? Text::Reader() : dval.getText(); return reader.getPointerField(assumePointerOffset(slot.getOffset())) .getBlob(typedDval.begin(), assumeMax(typedDval.size()) * BYTES); } case schema::Type::DATA: { Data::Reader typedDval = dval.isAnyPointer() ? Data::Reader() : dval.getData(); return reader.getPointerField(assumePointerOffset(slot.getOffset())) .getBlob(typedDval.begin(), assumeBits(typedDval.size()) * BYTES); } case schema::Type::LIST: { auto elementType = type.asList().getElementType(); return DynamicList::Reader(type.asList(), reader.getPointerField(assumePointerOffset(slot.getOffset())) .getList(elementSizeFor(elementType.which()), dval.isAnyPointer() ? nullptr : dval.getList().getAs<_::UncheckedMessage>())); } case schema::Type::STRUCT: return DynamicStruct::Reader(type.asStruct(), reader.getPointerField(assumePointerOffset(slot.getOffset())) .getStruct(dval.isAnyPointer() ? nullptr : dval.getStruct().getAs<_::UncheckedMessage>())); case schema::Type::ANY_POINTER: return AnyPointer::Reader(reader.getPointerField(assumePointerOffset(slot.getOffset()))); case schema::Type::INTERFACE: return DynamicCapability::Client(type.asInterface(), reader.getPointerField(assumePointerOffset(slot.getOffset())).getCapability()); } KJ_UNREACHABLE; } case schema::Field::GROUP: return DynamicStruct::Reader(type.asStruct(), reader); } KJ_UNREACHABLE; } DynamicValue::Builder DynamicStruct::Builder::get(StructSchema::Field field) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); verifySetInUnion(field); auto proto = field.getProto(); auto type = field.getType(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); // Note that the default value might be "anyPointer" even if the type is some poniter type // *other than* anyPointer. This happens with generics -- the field is actually a generic // parameter that has been bound, but the default value was of course compiled without any // binding available. auto dval = slot.getDefaultValue(); switch (type.which()) { case schema::Type::VOID: return builder.getDataField(assumeDataOffset(slot.getOffset())); #define HANDLE_TYPE(discrim, titleCase, type) \ case schema::Type::discrim: \ return builder.getDataField( \ assumeDataOffset(slot.getOffset()), \ bitCast<_::Mask>(dval.get##titleCase())); HANDLE_TYPE(BOOL, Bool, bool) HANDLE_TYPE(INT8, Int8, int8_t) HANDLE_TYPE(INT16, Int16, int16_t) HANDLE_TYPE(INT32, Int32, int32_t) HANDLE_TYPE(INT64, Int64, int64_t) HANDLE_TYPE(UINT8, Uint8, uint8_t) HANDLE_TYPE(UINT16, Uint16, uint16_t) HANDLE_TYPE(UINT32, Uint32, uint32_t) HANDLE_TYPE(UINT64, Uint64, uint64_t) HANDLE_TYPE(FLOAT32, Float32, float) HANDLE_TYPE(FLOAT64, Float64, double) #undef HANDLE_TYPE case schema::Type::ENUM: { uint16_t typedDval = dval.getEnum(); return DynamicEnum(type.asEnum(), builder.getDataField(assumeDataOffset(slot.getOffset()), typedDval)); } case schema::Type::TEXT: { Text::Reader typedDval = dval.isAnyPointer() ? Text::Reader() : dval.getText(); return builder.getPointerField(assumePointerOffset(slot.getOffset())) .getBlob(typedDval.begin(), assumeMax(typedDval.size()) * BYTES); } case schema::Type::DATA: { Data::Reader typedDval = dval.isAnyPointer() ? Data::Reader() : dval.getData(); return builder.getPointerField(assumePointerOffset(slot.getOffset())) .getBlob(typedDval.begin(), assumeBits(typedDval.size()) * BYTES); } case schema::Type::LIST: { ListSchema listType = type.asList(); if (listType.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(listType, builder.getPointerField(assumePointerOffset(slot.getOffset())) .getStructList(structSizeFromSchema(listType.getStructElementType()), dval.isAnyPointer() ? nullptr : dval.getList().getAs<_::UncheckedMessage>())); } else { return DynamicList::Builder(listType, builder.getPointerField(assumePointerOffset(slot.getOffset())) .getList(elementSizeFor(listType.whichElementType()), dval.isAnyPointer() ? nullptr : dval.getList().getAs<_::UncheckedMessage>())); } } case schema::Type::STRUCT: { auto structSchema = type.asStruct(); return DynamicStruct::Builder(structSchema, builder.getPointerField(assumePointerOffset(slot.getOffset())) .getStruct(structSizeFromSchema(structSchema), dval.isAnyPointer() ? nullptr : dval.getStruct().getAs<_::UncheckedMessage>())); } case schema::Type::ANY_POINTER: return AnyPointer::Builder( builder.getPointerField(assumePointerOffset(slot.getOffset()))); case schema::Type::INTERFACE: return DynamicCapability::Client(type.asInterface(), builder.getPointerField(assumePointerOffset(slot.getOffset())).getCapability()); } KJ_UNREACHABLE; } case schema::Field::GROUP: return DynamicStruct::Builder(type.asStruct(), builder); } KJ_UNREACHABLE; } DynamicValue::Pipeline DynamicStruct::Pipeline::get(StructSchema::Field field) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); auto proto = field.getProto(); KJ_REQUIRE(!hasDiscriminantValue(proto), "Can't pipeline on union members."); auto type = field.getType(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); switch (type.which()) { case schema::Type::STRUCT: return DynamicStruct::Pipeline(type.asStruct(), typeless.getPointerField(slot.getOffset())); case schema::Type::INTERFACE: return DynamicCapability::Client(type.asInterface(), typeless.getPointerField(slot.getOffset()).asCap()); case schema::Type::ANY_POINTER: switch (type.whichAnyPointerKind()) { case schema::Type::AnyPointer::Unconstrained::STRUCT: return DynamicStruct::Pipeline(StructSchema(), typeless.getPointerField(slot.getOffset())); case schema::Type::AnyPointer::Unconstrained::CAPABILITY: return DynamicCapability::Client(Capability::Client( typeless.getPointerField(slot.getOffset()).asCap())); default: KJ_FAIL_REQUIRE("Can only pipeline on struct and interface fields."); } default: KJ_FAIL_REQUIRE("Can only pipeline on struct and interface fields."); } KJ_UNREACHABLE; } case schema::Field::GROUP: return DynamicStruct::Pipeline(type.asStruct(), typeless.noop()); } KJ_UNREACHABLE; } bool DynamicStruct::Reader::has(StructSchema::Field field, HasMode mode) const { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); auto proto = field.getProto(); if (hasDiscriminantValue(proto)) { uint16_t discrim = reader.getDataField( assumeDataOffset(schema.getProto().getStruct().getDiscriminantOffset())); if (discrim != proto.getDiscriminantValue()) { // Field is not active in the union. return false; } } switch (proto.which()) { case schema::Field::SLOT: // Continue to below. break; case schema::Field::GROUP: return true; } auto slot = proto.getSlot(); auto type = field.getType(); switch (type.which()) { case schema::Type::VOID: // Void is always equal to the default. return mode == HasMode::NON_NULL; case schema::Type::BOOL: return mode == HasMode::NON_NULL || reader.getDataField(assumeDataOffset(slot.getOffset()), 0) != 0; case schema::Type::INT8: case schema::Type::UINT8: return mode == HasMode::NON_NULL || reader.getDataField(assumeDataOffset(slot.getOffset()), 0) != 0; case schema::Type::INT16: case schema::Type::UINT16: case schema::Type::ENUM: return mode == HasMode::NON_NULL || reader.getDataField(assumeDataOffset(slot.getOffset()), 0) != 0; case schema::Type::INT32: case schema::Type::UINT32: case schema::Type::FLOAT32: return mode == HasMode::NON_NULL || reader.getDataField(assumeDataOffset(slot.getOffset()), 0) != 0; case schema::Type::INT64: case schema::Type::UINT64: case schema::Type::FLOAT64: return mode == HasMode::NON_NULL || reader.getDataField(assumeDataOffset(slot.getOffset()), 0) != 0; case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::LIST: case schema::Type::STRUCT: case schema::Type::ANY_POINTER: case schema::Type::INTERFACE: return !reader.getPointerField(assumePointerOffset(slot.getOffset())).isNull(); } // Unknown type. As far as we know, it isn't set. return false; } kj::Maybe DynamicStruct::Reader::which() const { auto structProto = schema.getProto().getStruct(); if (structProto.getDiscriminantCount() == 0) { return nullptr; } uint16_t discrim = reader.getDataField( assumeDataOffset(structProto.getDiscriminantOffset())); return schema.getFieldByDiscriminant(discrim); } kj::Maybe DynamicStruct::Builder::which() { auto structProto = schema.getProto().getStruct(); if (structProto.getDiscriminantCount() == 0) { return nullptr; } uint16_t discrim = builder.getDataField( assumeDataOffset(structProto.getDiscriminantOffset())); return schema.getFieldByDiscriminant(discrim); } void DynamicStruct::Builder::set(StructSchema::Field field, const DynamicValue::Reader& value) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); setInUnion(field); auto proto = field.getProto(); auto type = field.getType(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); auto dval = slot.getDefaultValue(); switch (type.which()) { case schema::Type::VOID: builder.setDataField(assumeDataOffset(slot.getOffset()), value.as()); return; #define HANDLE_TYPE(discrim, titleCase, type) \ case schema::Type::discrim: \ builder.setDataField( \ assumeDataOffset(slot.getOffset()), value.as(), \ bitCast<_::Mask >(dval.get##titleCase())); \ return; HANDLE_TYPE(BOOL, Bool, bool) HANDLE_TYPE(INT8, Int8, int8_t) HANDLE_TYPE(INT16, Int16, int16_t) HANDLE_TYPE(INT32, Int32, int32_t) HANDLE_TYPE(INT64, Int64, int64_t) HANDLE_TYPE(UINT8, Uint8, uint8_t) HANDLE_TYPE(UINT16, Uint16, uint16_t) HANDLE_TYPE(UINT32, Uint32, uint32_t) HANDLE_TYPE(UINT64, Uint64, uint64_t) HANDLE_TYPE(FLOAT32, Float32, float) HANDLE_TYPE(FLOAT64, Float64, double) #undef HANDLE_TYPE case schema::Type::ENUM: { uint16_t rawValue; auto enumSchema = type.asEnum(); if (value.getType() == DynamicValue::TEXT) { // Convert from text. rawValue = enumSchema.getEnumerantByName(value.as()).getOrdinal(); } else if (value.getType() == DynamicValue::INT || value.getType() == DynamicValue::UINT) { rawValue = value.as(); } else { DynamicEnum enumValue = value.as(); KJ_REQUIRE(enumValue.getSchema() == enumSchema, "Value type mismatch.") { return; } rawValue = enumValue.getRaw(); } builder.setDataField(assumeDataOffset(slot.getOffset()), rawValue, dval.getEnum()); return; } case schema::Type::TEXT: builder.getPointerField(assumePointerOffset(slot.getOffset())) .setBlob(value.as()); return; case schema::Type::DATA: builder.getPointerField(assumePointerOffset(slot.getOffset())) .setBlob(value.as()); return; case schema::Type::LIST: { ListSchema listType = type.asList(); auto listValue = value.as(); KJ_REQUIRE(listValue.getSchema() == listType, "Value type mismatch.") { return; } builder.getPointerField(assumePointerOffset(slot.getOffset())) .setList(listValue.reader); return; } case schema::Type::STRUCT: { auto structType = type.asStruct(); auto structValue = value.as(); KJ_REQUIRE(structValue.getSchema() == structType, "Value type mismatch.") { return; } builder.getPointerField(assumePointerOffset(slot.getOffset())) .setStruct(structValue.reader); return; } case schema::Type::ANY_POINTER: { auto target = AnyPointer::Builder( builder.getPointerField(assumePointerOffset(slot.getOffset()))); switch (value.getType()) { case DynamicValue::Type::TEXT: target.setAs(value.as()); return; case DynamicValue::Type::DATA: target.setAs(value.as()); return; case DynamicValue::Type::LIST: target.setAs(value.as()); return; case DynamicValue::Type::STRUCT: target.setAs(value.as()); return; case DynamicValue::Type::CAPABILITY: target.setAs(value.as()); return; case DynamicValue::Type::ANY_POINTER: target.set(value.as()); return; case DynamicValue::Type::UNKNOWN: case DynamicValue::Type::VOID: case DynamicValue::Type::BOOL: case DynamicValue::Type::INT: case DynamicValue::Type::UINT: case DynamicValue::Type::FLOAT: case DynamicValue::Type::ENUM: KJ_FAIL_ASSERT("Value type mismatch; expected AnyPointer"); } KJ_UNREACHABLE; } case schema::Type::INTERFACE: { auto interfaceType = type.asInterface(); auto capability = value.as(); KJ_REQUIRE(capability.getSchema().extends(interfaceType), "Value type mismatch.") { return; } builder.getPointerField(assumePointerOffset(slot.getOffset())) .setCapability(kj::mv(capability.hook)); return; } } KJ_UNREACHABLE; } case schema::Field::GROUP: { auto src = value.as(); auto dst = init(field).as(); KJ_IF_MAYBE(unionField, src.which()) { dst.set(*unionField, src.get(*unionField)); } for (auto field: src.schema.getNonUnionFields()) { if (src.has(field)) { dst.set(field, src.get(field)); } } } } KJ_UNREACHABLE; } DynamicValue::Builder DynamicStruct::Builder::init(StructSchema::Field field) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); setInUnion(field); auto proto = field.getProto(); auto type = field.getType(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); switch (type.which()) { case schema::Type::STRUCT: { auto subSchema = type.asStruct(); return DynamicStruct::Builder(subSchema, builder.getPointerField(assumePointerOffset(slot.getOffset())) .initStruct(structSizeFromSchema(subSchema))); } case schema::Type::ANY_POINTER: { auto pointer = builder.getPointerField(assumePointerOffset(slot.getOffset())); pointer.clear(); return AnyPointer::Builder(pointer); } default: KJ_FAIL_REQUIRE("init() without a size is only valid for struct and object fields."); } } case schema::Field::GROUP: { clear(field); return DynamicStruct::Builder(type.asStruct(), builder); } } KJ_UNREACHABLE; } DynamicValue::Builder DynamicStruct::Builder::init(StructSchema::Field field, uint size) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); setInUnion(field); auto proto = field.getProto(); auto type = field.getType(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); switch (type.which()) { case schema::Type::LIST: { auto listType = type.asList(); if (listType.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(listType, builder.getPointerField(assumePointerOffset(slot.getOffset())) .initStructList(bounded(size) * ELEMENTS, structSizeFromSchema(listType.getStructElementType()))); } else { return DynamicList::Builder(listType, builder.getPointerField(assumePointerOffset(slot.getOffset())) .initList(elementSizeFor(listType.whichElementType()), bounded(size) * ELEMENTS)); } } case schema::Type::TEXT: return builder.getPointerField(assumePointerOffset(slot.getOffset())) .initBlob(bounded(size) * BYTES); case schema::Type::DATA: return builder.getPointerField(assumePointerOffset(slot.getOffset())) .initBlob(bounded(size) * BYTES); default: KJ_FAIL_REQUIRE( "init() with size is only valid for list, text, or data fields.", (uint)type.which()); break; } KJ_UNREACHABLE; } case schema::Field::GROUP: KJ_FAIL_REQUIRE("init() with size is only valid for list, text, or data fields."); } KJ_UNREACHABLE; } void DynamicStruct::Builder::adopt(StructSchema::Field field, Orphan&& orphan) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); setInUnion(field); auto proto = field.getProto(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); auto type = field.getType(); switch (type.which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: set(field, orphan.getReader()); return; case schema::Type::TEXT: KJ_REQUIRE(orphan.getType() == DynamicValue::TEXT, "Value type mismatch."); break; case schema::Type::DATA: KJ_REQUIRE(orphan.getType() == DynamicValue::DATA, "Value type mismatch."); break; case schema::Type::LIST: { ListSchema listType = type.asList(); KJ_REQUIRE(orphan.getType() == DynamicValue::LIST && orphan.listSchema == listType, "Value type mismatch.") { return; } break; } case schema::Type::STRUCT: { auto structType = type.asStruct(); KJ_REQUIRE(orphan.getType() == DynamicValue::STRUCT && orphan.structSchema == structType, "Value type mismatch.") { return; } break; } case schema::Type::ANY_POINTER: KJ_REQUIRE(orphan.getType() == DynamicValue::STRUCT || orphan.getType() == DynamicValue::LIST || orphan.getType() == DynamicValue::TEXT || orphan.getType() == DynamicValue::DATA || orphan.getType() == DynamicValue::CAPABILITY || orphan.getType() == DynamicValue::ANY_POINTER, "Value type mismatch.") { return; } break; case schema::Type::INTERFACE: { auto interfaceType = type.asInterface(); KJ_REQUIRE(orphan.getType() == DynamicValue::CAPABILITY && orphan.interfaceSchema.extends(interfaceType), "Value type mismatch.") { return; } break; } } builder.getPointerField(assumePointerOffset(slot.getOffset())).adopt(kj::mv(orphan.builder)); return; } case schema::Field::GROUP: // Have to transfer fields. auto src = orphan.get().as(); auto dst = init(field).as(); KJ_REQUIRE(orphan.getType() == DynamicValue::STRUCT && orphan.structSchema == dst.getSchema(), "Value type mismatch."); KJ_IF_MAYBE(unionField, src.which()) { dst.adopt(*unionField, src.disown(*unionField)); } for (auto field: src.schema.getNonUnionFields()) { if (src.has(field)) { dst.adopt(field, src.disown(field)); } } return; } KJ_UNREACHABLE; } Orphan DynamicStruct::Builder::disown(StructSchema::Field field) { // We end up calling get(field) below, so we don't need to validate `field` here. auto proto = field.getProto(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); switch (field.getType().which()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: { auto result = Orphan(get(field), _::OrphanBuilder()); clear(field); return kj::mv(result); } case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::LIST: case schema::Type::STRUCT: case schema::Type::ANY_POINTER: case schema::Type::INTERFACE: { auto value = get(field); return Orphan( value, builder.getPointerField(assumePointerOffset(slot.getOffset())).disown()); } } KJ_UNREACHABLE; } case schema::Field::GROUP: { // We have to allocate new space for the group, unfortunately. auto src = get(field).as(); Orphan result = Orphanage::getForMessageContaining(*this).newOrphan(src.getSchema()); auto dst = result.get(); KJ_IF_MAYBE(unionField, src.which()) { dst.adopt(*unionField, src.disown(*unionField)); } // We need to explicitly reset the union to its default field. KJ_IF_MAYBE(unionField, src.schema.getFieldByDiscriminant(0)) { src.clear(*unionField); } for (auto field: src.schema.getNonUnionFields()) { if (src.has(field)) { dst.adopt(field, src.disown(field)); } } return kj::mv(result); } } KJ_UNREACHABLE; } void DynamicStruct::Builder::clear(StructSchema::Field field) { KJ_REQUIRE(field.getContainingStruct() == schema, "`field` is not a field of this struct."); setInUnion(field); auto proto = field.getProto(); auto type = field.getType(); switch (proto.which()) { case schema::Field::SLOT: { auto slot = proto.getSlot(); switch (type.which()) { case schema::Type::VOID: builder.setDataField(assumeDataOffset(slot.getOffset()), VOID); return; #define HANDLE_TYPE(discrim, type) \ case schema::Type::discrim: \ builder.setDataField(assumeDataOffset(slot.getOffset()), 0); \ return; HANDLE_TYPE(BOOL, bool) HANDLE_TYPE(INT8, uint8_t) HANDLE_TYPE(INT16, uint16_t) HANDLE_TYPE(INT32, uint32_t) HANDLE_TYPE(INT64, uint64_t) HANDLE_TYPE(UINT8, uint8_t) HANDLE_TYPE(UINT16, uint16_t) HANDLE_TYPE(UINT32, uint32_t) HANDLE_TYPE(UINT64, uint64_t) HANDLE_TYPE(FLOAT32, uint32_t) HANDLE_TYPE(FLOAT64, uint64_t) HANDLE_TYPE(ENUM, uint16_t) #undef HANDLE_TYPE case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::LIST: case schema::Type::STRUCT: case schema::Type::ANY_POINTER: case schema::Type::INTERFACE: builder.getPointerField(assumePointerOffset(slot.getOffset())).clear(); return; } KJ_UNREACHABLE; } case schema::Field::GROUP: { DynamicStruct::Builder group(type.asStruct(), builder); // We clear the union field with discriminant 0 rather than the one that is set because // we want the union to end up with its default field active. KJ_IF_MAYBE(unionField, group.schema.getFieldByDiscriminant(0)) { group.clear(*unionField); } for (auto subField: group.schema.getNonUnionFields()) { group.clear(subField); } return; } } KJ_UNREACHABLE; } DynamicValue::Reader DynamicStruct::Reader::get(kj::StringPtr name) const { return get(schema.getFieldByName(name)); } DynamicValue::Builder DynamicStruct::Builder::get(kj::StringPtr name) { return get(schema.getFieldByName(name)); } DynamicValue::Pipeline DynamicStruct::Pipeline::get(kj::StringPtr name) { return get(schema.getFieldByName(name)); } bool DynamicStruct::Reader::has(kj::StringPtr name, HasMode mode) const { return has(schema.getFieldByName(name), mode); } bool DynamicStruct::Builder::has(kj::StringPtr name, HasMode mode) { return has(schema.getFieldByName(name), mode); } void DynamicStruct::Builder::set(kj::StringPtr name, const DynamicValue::Reader& value) { set(schema.getFieldByName(name), value); } void DynamicStruct::Builder::set(kj::StringPtr name, std::initializer_list value) { auto list = init(name, value.size()).as(); uint i = 0; for (auto element: value) { list.set(i++, element); } } DynamicValue::Builder DynamicStruct::Builder::init(kj::StringPtr name) { return init(schema.getFieldByName(name)); } DynamicValue::Builder DynamicStruct::Builder::init(kj::StringPtr name, uint size) { return init(schema.getFieldByName(name), size); } void DynamicStruct::Builder::adopt(kj::StringPtr name, Orphan&& orphan) { adopt(schema.getFieldByName(name), kj::mv(orphan)); } Orphan DynamicStruct::Builder::disown(kj::StringPtr name) { return disown(schema.getFieldByName(name)); } void DynamicStruct::Builder::clear(kj::StringPtr name) { clear(schema.getFieldByName(name)); } // ======================================================================================= DynamicValue::Reader DynamicList::Reader::operator[](uint index) const { KJ_REQUIRE(index < size(), "List index out-of-bounds."); switch (schema.whichElementType()) { #define HANDLE_TYPE(name, discrim, typeName) \ case schema::Type::discrim: \ return reader.getDataElement(bounded(index) * ELEMENTS); HANDLE_TYPE(void, VOID, Void) HANDLE_TYPE(bool, BOOL, bool) HANDLE_TYPE(int8, INT8, int8_t) HANDLE_TYPE(int16, INT16, int16_t) HANDLE_TYPE(int32, INT32, int32_t) HANDLE_TYPE(int64, INT64, int64_t) HANDLE_TYPE(uint8, UINT8, uint8_t) HANDLE_TYPE(uint16, UINT16, uint16_t) HANDLE_TYPE(uint32, UINT32, uint32_t) HANDLE_TYPE(uint64, UINT64, uint64_t) HANDLE_TYPE(float32, FLOAT32, float) HANDLE_TYPE(float64, FLOAT64, double) #undef HANDLE_TYPE case schema::Type::TEXT: return reader.getPointerElement(bounded(index) * ELEMENTS) .getBlob(nullptr, ZERO * BYTES); case schema::Type::DATA: return reader.getPointerElement(bounded(index) * ELEMENTS) .getBlob(nullptr, ZERO * BYTES); case schema::Type::LIST: { auto elementType = schema.getListElementType(); return DynamicList::Reader(elementType, reader.getPointerElement(bounded(index) * ELEMENTS) .getList(elementSizeFor(elementType.whichElementType()), nullptr)); } case schema::Type::STRUCT: return DynamicStruct::Reader(schema.getStructElementType(), reader.getStructElement(bounded(index) * ELEMENTS)); case schema::Type::ENUM: return DynamicEnum(schema.getEnumElementType(), reader.getDataElement(bounded(index) * ELEMENTS)); case schema::Type::ANY_POINTER: return AnyPointer::Reader(reader.getPointerElement(bounded(index) * ELEMENTS)); case schema::Type::INTERFACE: return DynamicCapability::Client(schema.getInterfaceElementType(), reader.getPointerElement(bounded(index) * ELEMENTS) .getCapability()); } return nullptr; } DynamicValue::Builder DynamicList::Builder::operator[](uint index) { KJ_REQUIRE(index < size(), "List index out-of-bounds."); switch (schema.whichElementType()) { #define HANDLE_TYPE(name, discrim, typeName) \ case schema::Type::discrim: \ return builder.getDataElement(bounded(index) * ELEMENTS); HANDLE_TYPE(void, VOID, Void) HANDLE_TYPE(bool, BOOL, bool) HANDLE_TYPE(int8, INT8, int8_t) HANDLE_TYPE(int16, INT16, int16_t) HANDLE_TYPE(int32, INT32, int32_t) HANDLE_TYPE(int64, INT64, int64_t) HANDLE_TYPE(uint8, UINT8, uint8_t) HANDLE_TYPE(uint16, UINT16, uint16_t) HANDLE_TYPE(uint32, UINT32, uint32_t) HANDLE_TYPE(uint64, UINT64, uint64_t) HANDLE_TYPE(float32, FLOAT32, float) HANDLE_TYPE(float64, FLOAT64, double) #undef HANDLE_TYPE case schema::Type::TEXT: return builder.getPointerElement(bounded(index) * ELEMENTS) .getBlob(nullptr, ZERO * BYTES); case schema::Type::DATA: return builder.getPointerElement(bounded(index) * ELEMENTS) .getBlob(nullptr, ZERO * BYTES); case schema::Type::LIST: { ListSchema elementType = schema.getListElementType(); if (elementType.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(elementType, builder.getPointerElement(bounded(index) * ELEMENTS) .getStructList(structSizeFromSchema(elementType.getStructElementType()), nullptr)); } else { return DynamicList::Builder(elementType, builder.getPointerElement(bounded(index) * ELEMENTS) .getList(elementSizeFor(elementType.whichElementType()), nullptr)); } } case schema::Type::STRUCT: return DynamicStruct::Builder(schema.getStructElementType(), builder.getStructElement(bounded(index) * ELEMENTS)); case schema::Type::ENUM: return DynamicEnum(schema.getEnumElementType(), builder.getDataElement(bounded(index) * ELEMENTS)); case schema::Type::ANY_POINTER: KJ_FAIL_ASSERT("List(AnyPointer) not supported."); return nullptr; case schema::Type::INTERFACE: return DynamicCapability::Client(schema.getInterfaceElementType(), builder.getPointerElement(bounded(index) * ELEMENTS) .getCapability()); } return nullptr; } void DynamicList::Builder::set(uint index, const DynamicValue::Reader& value) { KJ_REQUIRE(index < size(), "List index out-of-bounds.") { return; } switch (schema.whichElementType()) { #define HANDLE_TYPE(name, discrim, typeName) \ case schema::Type::discrim: \ builder.setDataElement(bounded(index) * ELEMENTS, value.as()); \ return; HANDLE_TYPE(void, VOID, Void) HANDLE_TYPE(bool, BOOL, bool) HANDLE_TYPE(int8, INT8, int8_t) HANDLE_TYPE(int16, INT16, int16_t) HANDLE_TYPE(int32, INT32, int32_t) HANDLE_TYPE(int64, INT64, int64_t) HANDLE_TYPE(uint8, UINT8, uint8_t) HANDLE_TYPE(uint16, UINT16, uint16_t) HANDLE_TYPE(uint32, UINT32, uint32_t) HANDLE_TYPE(uint64, UINT64, uint64_t) HANDLE_TYPE(float32, FLOAT32, float) HANDLE_TYPE(float64, FLOAT64, double) #undef HANDLE_TYPE case schema::Type::TEXT: builder.getPointerElement(bounded(index) * ELEMENTS).setBlob(value.as()); return; case schema::Type::DATA: builder.getPointerElement(bounded(index) * ELEMENTS).setBlob(value.as()); return; case schema::Type::LIST: { auto listValue = value.as(); KJ_REQUIRE(listValue.getSchema() == schema.getListElementType(), "Value type mismatch.") { return; } builder.getPointerElement(bounded(index) * ELEMENTS).setList(listValue.reader); return; } case schema::Type::STRUCT: { auto structValue = value.as(); KJ_REQUIRE(structValue.getSchema() == schema.getStructElementType(), "Value type mismatch.") { return; } builder.getStructElement(bounded(index) * ELEMENTS).copyContentFrom(structValue.reader); return; } case schema::Type::ENUM: { uint16_t rawValue; if (value.getType() == DynamicValue::TEXT) { // Convert from text. rawValue = schema.getEnumElementType().getEnumerantByName(value.as()).getOrdinal(); } else { DynamicEnum enumValue = value.as(); KJ_REQUIRE(schema.getEnumElementType() == enumValue.getSchema(), "Type mismatch when using DynamicList::Builder::set().") { return; } rawValue = enumValue.getRaw(); } builder.setDataElement(bounded(index) * ELEMENTS, rawValue); return; } case schema::Type::ANY_POINTER: KJ_FAIL_ASSERT("List(AnyPointer) not supported.") { return; } case schema::Type::INTERFACE: { auto capValue = value.as(); KJ_REQUIRE(capValue.getSchema().extends(schema.getInterfaceElementType()), "Value type mismatch.") { return; } builder.getPointerElement(bounded(index) * ELEMENTS).setCapability(kj::mv(capValue.hook)); return; } } KJ_FAIL_REQUIRE("can't set element of unknown type", (uint)schema.whichElementType()) { return; } } DynamicValue::Builder DynamicList::Builder::init(uint index, uint size) { KJ_REQUIRE(index < this->size(), "List index out-of-bounds."); switch (schema.whichElementType()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: case schema::Type::STRUCT: case schema::Type::INTERFACE: KJ_FAIL_REQUIRE("Expected a list or blob."); return nullptr; case schema::Type::TEXT: return builder.getPointerElement(bounded(index) * ELEMENTS) .initBlob(bounded(size) * BYTES); case schema::Type::DATA: return builder.getPointerElement(bounded(index) * ELEMENTS) .initBlob(bounded(size) * BYTES); case schema::Type::LIST: { auto elementType = schema.getListElementType(); if (elementType.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(elementType, builder.getPointerElement(bounded(index) * ELEMENTS) .initStructList(bounded(size) * ELEMENTS, structSizeFromSchema(elementType.getStructElementType()))); } else { return DynamicList::Builder(elementType, builder.getPointerElement(bounded(index) * ELEMENTS) .initList(elementSizeFor(elementType.whichElementType()), bounded(size) * ELEMENTS)); } } case schema::Type::ANY_POINTER: { KJ_FAIL_ASSERT("List(AnyPointer) not supported."); return nullptr; } } return nullptr; } void DynamicList::Builder::adopt(uint index, Orphan&& orphan) { switch (schema.whichElementType()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: set(index, orphan.getReader()); return; case schema::Type::TEXT: KJ_REQUIRE(orphan.getType() == DynamicValue::TEXT, "Value type mismatch."); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(orphan.builder)); return; case schema::Type::DATA: KJ_REQUIRE(orphan.getType() == DynamicValue::DATA, "Value type mismatch."); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(orphan.builder)); return; case schema::Type::LIST: { ListSchema elementType = schema.getListElementType(); KJ_REQUIRE(orphan.getType() == DynamicValue::LIST && orphan.listSchema == elementType, "Value type mismatch."); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(orphan.builder)); return; } case schema::Type::STRUCT: { auto elementType = schema.getStructElementType(); KJ_REQUIRE(orphan.getType() == DynamicValue::STRUCT && orphan.structSchema == elementType, "Value type mismatch."); builder.getStructElement(bounded(index) * ELEMENTS).transferContentFrom( orphan.builder.asStruct(structSizeFromSchema(elementType))); return; } case schema::Type::ANY_POINTER: KJ_FAIL_ASSERT("List(AnyPointer) not supported."); case schema::Type::INTERFACE: { auto elementType = schema.getInterfaceElementType(); KJ_REQUIRE(orphan.getType() == DynamicValue::CAPABILITY && orphan.interfaceSchema.extends(elementType), "Value type mismatch."); builder.getPointerElement(bounded(index) * ELEMENTS).adopt(kj::mv(orphan.builder)); return; } } KJ_UNREACHABLE; } Orphan DynamicList::Builder::disown(uint index) { switch (schema.whichElementType()) { case schema::Type::VOID: case schema::Type::BOOL: case schema::Type::INT8: case schema::Type::INT16: case schema::Type::INT32: case schema::Type::INT64: case schema::Type::UINT8: case schema::Type::UINT16: case schema::Type::UINT32: case schema::Type::UINT64: case schema::Type::FLOAT32: case schema::Type::FLOAT64: case schema::Type::ENUM: { auto result = Orphan(operator[](index), _::OrphanBuilder()); switch (elementSizeFor(schema.whichElementType())) { case ElementSize::VOID: break; case ElementSize::BIT: builder.setDataElement(bounded(index) * ELEMENTS, false); break; case ElementSize::BYTE: builder.setDataElement(bounded(index) * ELEMENTS, 0); break; case ElementSize::TWO_BYTES: builder.setDataElement(bounded(index) * ELEMENTS, 0); break; case ElementSize::FOUR_BYTES: builder.setDataElement(bounded(index) * ELEMENTS, 0); break; case ElementSize::EIGHT_BYTES: builder.setDataElement(bounded(index) * ELEMENTS, 0);break; case ElementSize::POINTER: case ElementSize::INLINE_COMPOSITE: KJ_UNREACHABLE; } return kj::mv(result); } case schema::Type::TEXT: case schema::Type::DATA: case schema::Type::LIST: case schema::Type::ANY_POINTER: case schema::Type::INTERFACE: { auto value = operator[](index); return Orphan(value, builder.getPointerElement(bounded(index) * ELEMENTS).disown()); } case schema::Type::STRUCT: { // We have to make a copy. Orphan result = Orphanage::getForMessageContaining(*this).newOrphan(schema.getStructElementType()); auto element = builder.getStructElement(bounded(index) * ELEMENTS); result.get().builder.transferContentFrom(element); element.clearAll(); return kj::mv(result); } } KJ_UNREACHABLE; } void DynamicList::Builder::copyFrom(std::initializer_list value) { KJ_REQUIRE(value.size() == size(), "DynamicList::copyFrom() argument had different size."); uint i = 0; for (auto element: value) { set(i++, element); } } DynamicList::Reader DynamicList::Builder::asReader() const { return DynamicList::Reader(schema, builder.asReader()); } // ======================================================================================= DynamicValue::Reader::Reader(ConstSchema constant): type(VOID) { auto type = constant.getType(); auto value = constant.getProto().getConst().getValue(); switch (type.which()) { case schema::Type::VOID: *this = capnp::VOID; break; case schema::Type::BOOL: *this = value.getBool(); break; case schema::Type::INT8: *this = value.getInt8(); break; case schema::Type::INT16: *this = value.getInt16(); break; case schema::Type::INT32: *this = value.getInt32(); break; case schema::Type::INT64: *this = value.getInt64(); break; case schema::Type::UINT8: *this = value.getUint8(); break; case schema::Type::UINT16: *this = value.getUint16(); break; case schema::Type::UINT32: *this = value.getUint32(); break; case schema::Type::UINT64: *this = value.getUint64(); break; case schema::Type::FLOAT32: *this = value.getFloat32(); break; case schema::Type::FLOAT64: *this = value.getFloat64(); break; case schema::Type::TEXT: *this = value.getText(); break; case schema::Type::DATA: *this = value.getData(); break; case schema::Type::ENUM: *this = DynamicEnum(type.asEnum(), value.getEnum()); break; case schema::Type::STRUCT: *this = value.getStruct().getAs(type.asStruct()); break; case schema::Type::LIST: *this = value.getList().getAs(type.asList()); break; case schema::Type::ANY_POINTER: *this = value.getAnyPointer(); break; case schema::Type::INTERFACE: KJ_FAIL_ASSERT("Constants can't have interface type."); } } #if __GNUC__ && !__clang__ && __GNUC__ >= 9 // In the copy constructors below, we use memcpy() to copy only after verifying that it is safe. // But GCC 9 doesn't know we've checked, and whines. I suppose GCC is probably right: our checks // probably don't technically make memcpy safe according to the standard. But it works in practice, // and if it ever stops working, the tests will catch it. #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif DynamicValue::Reader::Reader(const Reader& other) { switch (other.type) { case UNKNOWN: case VOID: case BOOL: case INT: case UINT: case FLOAT: case TEXT: case DATA: case LIST: case ENUM: case STRUCT: case ANY_POINTER: KJ_ASSERT_CAN_MEMCPY(Text::Reader); KJ_ASSERT_CAN_MEMCPY(Data::Reader); KJ_ASSERT_CAN_MEMCPY(DynamicList::Reader); KJ_ASSERT_CAN_MEMCPY(DynamicEnum); KJ_ASSERT_CAN_MEMCPY(DynamicStruct::Reader); KJ_ASSERT_CAN_MEMCPY(AnyPointer::Reader); break; case CAPABILITY: type = CAPABILITY; kj::ctor(capabilityValue, other.capabilityValue); return; } memcpy(this, &other, sizeof(*this)); } DynamicValue::Reader::Reader(Reader&& other) noexcept { switch (other.type) { case UNKNOWN: case VOID: case BOOL: case INT: case UINT: case FLOAT: case TEXT: case DATA: case LIST: case ENUM: case STRUCT: case ANY_POINTER: KJ_ASSERT_CAN_MEMCPY(Text::Reader); KJ_ASSERT_CAN_MEMCPY(Data::Reader); KJ_ASSERT_CAN_MEMCPY(DynamicList::Reader); KJ_ASSERT_CAN_MEMCPY(DynamicEnum); KJ_ASSERT_CAN_MEMCPY(DynamicStruct::Reader); KJ_ASSERT_CAN_MEMCPY(AnyPointer::Reader); break; case CAPABILITY: type = CAPABILITY; kj::ctor(capabilityValue, kj::mv(other.capabilityValue)); return; } memcpy(this, &other, sizeof(*this)); } DynamicValue::Reader::~Reader() noexcept(false) { if (type == CAPABILITY) { kj::dtor(capabilityValue); } } DynamicValue::Reader& DynamicValue::Reader::operator=(const Reader& other) { if (type == CAPABILITY) { kj::dtor(capabilityValue); } kj::ctor(*this, other); return *this; } DynamicValue::Reader& DynamicValue::Reader::operator=(Reader&& other) { if (type == CAPABILITY) { kj::dtor(capabilityValue); } kj::ctor(*this, kj::mv(other)); return *this; } DynamicValue::Builder::Builder(Builder& other) { switch (other.type) { case UNKNOWN: case VOID: case BOOL: case INT: case UINT: case FLOAT: case TEXT: case DATA: case LIST: case ENUM: case STRUCT: case ANY_POINTER: // Unfortunately canMemcpy() doesn't work on these types due to the use of // DisallowConstCopy, but __has_trivial_destructor should detect if any of these types // become non-trivial. static_assert(__has_trivial_destructor(Text::Builder) && __has_trivial_destructor(Data::Builder) && __has_trivial_destructor(DynamicList::Builder) && __has_trivial_destructor(DynamicEnum) && __has_trivial_destructor(DynamicStruct::Builder) && __has_trivial_destructor(AnyPointer::Builder), "Assumptions here don't hold."); break; case CAPABILITY: type = CAPABILITY; kj::ctor(capabilityValue, other.capabilityValue); return; } memcpy(this, &other, sizeof(*this)); } DynamicValue::Builder::Builder(Builder&& other) noexcept { switch (other.type) { case UNKNOWN: case VOID: case BOOL: case INT: case UINT: case FLOAT: case TEXT: case DATA: case LIST: case ENUM: case STRUCT: case ANY_POINTER: // Unfortunately __has_trivial_copy doesn't work on these types due to the use of // DisallowConstCopy, but __has_trivial_destructor should detect if any of these types // become non-trivial. static_assert(__has_trivial_destructor(Text::Builder) && __has_trivial_destructor(Data::Builder) && __has_trivial_destructor(DynamicList::Builder) && __has_trivial_destructor(DynamicEnum) && __has_trivial_destructor(DynamicStruct::Builder) && __has_trivial_destructor(AnyPointer::Builder), "Assumptions here don't hold."); break; case CAPABILITY: type = CAPABILITY; kj::ctor(capabilityValue, kj::mv(other.capabilityValue)); return; } memcpy(this, &other, sizeof(*this)); } DynamicValue::Builder::~Builder() noexcept(false) { if (type == CAPABILITY) { kj::dtor(capabilityValue); } } DynamicValue::Builder& DynamicValue::Builder::operator=(Builder& other) { if (type == CAPABILITY) { kj::dtor(capabilityValue); } kj::ctor(*this, other); return *this; } DynamicValue::Builder& DynamicValue::Builder::operator=(Builder&& other) { if (type == CAPABILITY) { kj::dtor(capabilityValue); } kj::ctor(*this, kj::mv(other)); return *this; } DynamicValue::Reader DynamicValue::Builder::asReader() const { switch (type) { case UNKNOWN: return Reader(); case VOID: return Reader(voidValue); case BOOL: return Reader(boolValue); case INT: return Reader(intValue); case UINT: return Reader(uintValue); case FLOAT: return Reader(floatValue); case TEXT: return Reader(textValue.asReader()); case DATA: return Reader(dataValue.asReader()); case LIST: return Reader(listValue.asReader()); case ENUM: return Reader(enumValue); case STRUCT: return Reader(structValue.asReader()); case CAPABILITY: return Reader(capabilityValue); case ANY_POINTER: return Reader(anyPointerValue.asReader()); } KJ_FAIL_ASSERT("Missing switch case."); return Reader(); } DynamicValue::Pipeline::Pipeline(Pipeline&& other) noexcept: type(other.type) { switch (type) { case UNKNOWN: break; case STRUCT: kj::ctor(structValue, kj::mv(other.structValue)); break; case CAPABILITY: kj::ctor(capabilityValue, kj::mv(other.capabilityValue)); break; default: KJ_LOG(ERROR, "Unexpected pipeline type.", (uint)type); type = UNKNOWN; break; } } DynamicValue::Pipeline& DynamicValue::Pipeline::operator=(Pipeline&& other) { kj::dtor(*this); kj::ctor(*this, kj::mv(other)); return *this; } DynamicValue::Pipeline::~Pipeline() noexcept(false) { switch (type) { case UNKNOWN: break; case STRUCT: kj::dtor(structValue); break; case CAPABILITY: kj::dtor(capabilityValue); break; default: KJ_FAIL_ASSERT("Unexpected pipeline type.", (uint)type) { type = UNKNOWN; break; } break; } } namespace { template T signedToUnsigned(long long value) { KJ_REQUIRE(value >= 0 && T(value) == value, "Value out-of-range for requested type.", value) { // Use it anyway. break; } return value; } template <> uint64_t signedToUnsigned(long long value) { KJ_REQUIRE(value >= 0, "Value out-of-range for requested type.", value) { // Use it anyway. break; } return value; } template T unsignedToSigned(unsigned long long value) { KJ_REQUIRE(T(value) >= 0 && (unsigned long long)T(value) == value, "Value out-of-range for requested type.", value) { // Use it anyway. break; } return value; } template <> int64_t unsignedToSigned(unsigned long long value) { KJ_REQUIRE(int64_t(value) >= 0, "Value out-of-range for requested type.", value) { // Use it anyway. break; } return value; } template T checkRoundTrip(U value) { #if __aarch64__ // Work around an apparently broken compiler optimization on Clang / arm64. It appears that // for T = int8_t, U = double, and value = 128, the compiler incorrectly believes that the // round-trip does not change the value, where in fact it should change to -128. Similar problems // exist for various other types and inputs -- json-test seems to exercise several problem cases. // The problem only exists when compiling with optimization. In any case, declaring the variable // `volatile` kills the optimization. volatile #endif T result = value; KJ_REQUIRE(U(result) == value, "Value out-of-range for requested type.", value) { // Use it anyway. break; } return result; } } // namespace #define HANDLE_NUMERIC_TYPE(typeName, ifInt, ifUint, ifFloat) \ typeName DynamicValue::Reader::AsImpl::apply(const Reader& reader) { \ switch (reader.type) { \ case INT: \ return ifInt(reader.intValue); \ case UINT: \ return ifUint(reader.uintValue); \ case FLOAT: \ return ifFloat(reader.floatValue); \ default: \ KJ_FAIL_REQUIRE("Value type mismatch.") { \ return 0; \ } \ } \ } \ typeName DynamicValue::Builder::AsImpl::apply(Builder& builder) { \ switch (builder.type) { \ case INT: \ return ifInt(builder.intValue); \ case UINT: \ return ifUint(builder.uintValue); \ case FLOAT: \ return ifFloat(builder.floatValue); \ default: \ KJ_FAIL_REQUIRE("Value type mismatch.") { \ return 0; \ } \ } \ } HANDLE_NUMERIC_TYPE(int8_t, checkRoundTrip, unsignedToSigned, checkRoundTrip) HANDLE_NUMERIC_TYPE(int16_t, checkRoundTrip, unsignedToSigned, checkRoundTrip) HANDLE_NUMERIC_TYPE(int32_t, checkRoundTrip, unsignedToSigned, checkRoundTrip) HANDLE_NUMERIC_TYPE(int64_t, kj::implicitCast, unsignedToSigned, checkRoundTrip) HANDLE_NUMERIC_TYPE(uint8_t, signedToUnsigned, checkRoundTrip, checkRoundTrip) HANDLE_NUMERIC_TYPE(uint16_t, signedToUnsigned, checkRoundTrip, checkRoundTrip) HANDLE_NUMERIC_TYPE(uint32_t, signedToUnsigned, checkRoundTrip, checkRoundTrip) HANDLE_NUMERIC_TYPE(uint64_t, signedToUnsigned, kj::implicitCast, checkRoundTrip) HANDLE_NUMERIC_TYPE(float, kj::implicitCast, kj::implicitCast, kj::implicitCast) HANDLE_NUMERIC_TYPE(double, kj::implicitCast, kj::implicitCast, kj::implicitCast) #undef HANDLE_NUMERIC_TYPE #define HANDLE_TYPE(name, discrim, typeName) \ ReaderFor DynamicValue::Reader::AsImpl::apply(const Reader& reader) { \ KJ_REQUIRE(reader.type == discrim, "Value type mismatch.") { \ return ReaderFor(); \ } \ return reader.name##Value; \ } \ BuilderFor DynamicValue::Builder::AsImpl::apply(Builder& builder) { \ KJ_REQUIRE(builder.type == discrim, "Value type mismatch."); \ return builder.name##Value; \ } //HANDLE_TYPE(void, VOID, Void) HANDLE_TYPE(bool, BOOL, bool) HANDLE_TYPE(text, TEXT, Text) HANDLE_TYPE(list, LIST, DynamicList) HANDLE_TYPE(struct, STRUCT, DynamicStruct) HANDLE_TYPE(enum, ENUM, DynamicEnum) HANDLE_TYPE(anyPointer, ANY_POINTER, AnyPointer) #undef HANDLE_TYPE PipelineFor DynamicValue::Pipeline::AsImpl::apply( Pipeline& pipeline) { KJ_REQUIRE(pipeline.type == STRUCT, "Pipeline type mismatch."); return kj::mv(pipeline.structValue); } ReaderFor DynamicValue::Reader::AsImpl::apply( const Reader& reader) { KJ_REQUIRE(reader.type == CAPABILITY, "Value type mismatch.") { return DynamicCapability::Client(); } return reader.capabilityValue; } BuilderFor DynamicValue::Builder::AsImpl::apply( Builder& builder) { KJ_REQUIRE(builder.type == CAPABILITY, "Value type mismatch.") { return DynamicCapability::Client(); } return builder.capabilityValue; } PipelineFor DynamicValue::Pipeline::AsImpl::apply( Pipeline& pipeline) { KJ_REQUIRE(pipeline.type == CAPABILITY, "Pipeline type mismatch.") { return DynamicCapability::Client(); } return kj::mv(pipeline.capabilityValue); } Data::Reader DynamicValue::Reader::AsImpl::apply(const Reader& reader) { if (reader.type == TEXT) { // Coerce text to data. return reader.textValue.asBytes(); } KJ_REQUIRE(reader.type == DATA, "Value type mismatch.") { return Data::Reader(); } return reader.dataValue; } Data::Builder DynamicValue::Builder::AsImpl::apply(Builder& builder) { if (builder.type == TEXT) { // Coerce text to data. return builder.textValue.asBytes(); } KJ_REQUIRE(builder.type == DATA, "Value type mismatch.") { return BuilderFor(); } return builder.dataValue; } // As in the header, HANDLE_TYPE(void, VOID, Void) crashes GCC 4.7. Void DynamicValue::Reader::AsImpl::apply(const Reader& reader) { KJ_REQUIRE(reader.type == VOID, "Value type mismatch.") { return Void(); } return reader.voidValue; } Void DynamicValue::Builder::AsImpl::apply(Builder& builder) { KJ_REQUIRE(builder.type == VOID, "Value type mismatch.") { return Void(); } return builder.voidValue; } // ======================================================================================= namespace _ { // private DynamicStruct::Reader PointerHelpers::getDynamic( PointerReader reader, StructSchema schema) { KJ_REQUIRE(!schema.getProto().getStruct().getIsGroup(), "Cannot form pointer to group type."); return DynamicStruct::Reader(schema, reader.getStruct(nullptr)); } DynamicStruct::Builder PointerHelpers::getDynamic( PointerBuilder builder, StructSchema schema) { KJ_REQUIRE(!schema.getProto().getStruct().getIsGroup(), "Cannot form pointer to group type."); return DynamicStruct::Builder(schema, builder.getStruct( structSizeFromSchema(schema), nullptr)); } void PointerHelpers::set( PointerBuilder builder, const DynamicStruct::Reader& value) { KJ_REQUIRE(!value.schema.getProto().getStruct().getIsGroup(), "Cannot form pointer to group type."); builder.setStruct(value.reader); } DynamicStruct::Builder PointerHelpers::init( PointerBuilder builder, StructSchema schema) { KJ_REQUIRE(!schema.getProto().getStruct().getIsGroup(), "Cannot form pointer to group type."); return DynamicStruct::Builder(schema, builder.initStruct(structSizeFromSchema(schema))); } DynamicList::Reader PointerHelpers::getDynamic( PointerReader reader, ListSchema schema) { return DynamicList::Reader(schema, reader.getList(elementSizeFor(schema.whichElementType()), nullptr)); } DynamicList::Builder PointerHelpers::getDynamic( PointerBuilder builder, ListSchema schema) { if (schema.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(schema, builder.getStructList( structSizeFromSchema(schema.getStructElementType()), nullptr)); } else { return DynamicList::Builder(schema, builder.getList(elementSizeFor(schema.whichElementType()), nullptr)); } } void PointerHelpers::set( PointerBuilder builder, const DynamicList::Reader& value) { builder.setList(value.reader); } DynamicList::Builder PointerHelpers::init( PointerBuilder builder, ListSchema schema, uint size) { if (schema.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(schema, builder.initStructList(bounded(size) * ELEMENTS, structSizeFromSchema(schema.getStructElementType()))); } else { return DynamicList::Builder(schema, builder.initList(elementSizeFor(schema.whichElementType()), bounded(size) * ELEMENTS)); } } DynamicCapability::Client PointerHelpers::getDynamic( PointerReader reader, InterfaceSchema schema) { return DynamicCapability::Client(schema, reader.getCapability()); } DynamicCapability::Client PointerHelpers::getDynamic( PointerBuilder builder, InterfaceSchema schema) { return DynamicCapability::Client(schema, builder.getCapability()); } void PointerHelpers::set( PointerBuilder builder, DynamicCapability::Client& value) { builder.setCapability(value.hook->addRef()); } void PointerHelpers::set( PointerBuilder builder, DynamicCapability::Client&& value) { builder.setCapability(kj::mv(value.hook)); } } // namespace _ (private) template <> void AnyPointer::Builder::adopt(Orphan&& orphan) { switch (orphan.getType()) { case DynamicValue::UNKNOWN: case DynamicValue::VOID: case DynamicValue::BOOL: case DynamicValue::INT: case DynamicValue::UINT: case DynamicValue::FLOAT: case DynamicValue::ENUM: KJ_FAIL_REQUIRE("AnyPointer cannot adopt primitive (non-object) value."); case DynamicValue::STRUCT: case DynamicValue::LIST: case DynamicValue::TEXT: case DynamicValue::DATA: case DynamicValue::CAPABILITY: case DynamicValue::ANY_POINTER: builder.adopt(kj::mv(orphan.builder)); break; } } DynamicStruct::Reader::Reader(StructSchema schema, const _::OrphanBuilder& orphan) : schema(schema), reader(orphan.asStructReader(structSizeFromSchema(schema))) {} DynamicStruct::Builder::Builder(StructSchema schema, _::OrphanBuilder& orphan) : schema(schema), builder(orphan.asStruct(structSizeFromSchema(schema))) {} DynamicList::Reader::Reader(ListSchema schema, const _::OrphanBuilder& orphan) : schema(schema), reader(orphan.asListReader(elementSizeFor(schema.whichElementType()))) {} DynamicList::Builder::Builder(ListSchema schema, _::OrphanBuilder& orphan) : schema(schema), builder(schema.whichElementType() == schema::Type::STRUCT ? orphan.asStructList(structSizeFromSchema(schema.getStructElementType())) : orphan.asList(elementSizeFor(schema.whichElementType()))) {} // ------------------------------------------------------------------- Orphan Orphanage::newOrphan(StructSchema schema) const { return Orphan( schema, _::OrphanBuilder::initStruct(arena, capTable, structSizeFromSchema(schema))); } Orphan Orphanage::newOrphan(ListSchema schema, uint size) const { if (schema.whichElementType() == schema::Type::STRUCT) { return Orphan(schema, _::OrphanBuilder::initStructList( arena, capTable, bounded(size) * ELEMENTS, structSizeFromSchema(schema.getStructElementType()))); } else { return Orphan(schema, _::OrphanBuilder::initList( arena, capTable, bounded(size) * ELEMENTS, elementSizeFor(schema.whichElementType()))); } } DynamicStruct::Builder Orphan::get() { return DynamicStruct::Builder(schema, builder.asStruct(structSizeFromSchema(schema))); } DynamicStruct::Reader Orphan::getReader() const { return DynamicStruct::Reader(schema, builder.asStructReader(structSizeFromSchema(schema))); } DynamicList::Builder Orphan::get() { if (schema.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder( schema, builder.asStructList(structSizeFromSchema(schema.getStructElementType()))); } else { return DynamicList::Builder( schema, builder.asList(elementSizeFor(schema.whichElementType()))); } } DynamicList::Reader Orphan::getReader() const { return DynamicList::Reader( schema, builder.asListReader(elementSizeFor(schema.whichElementType()))); } DynamicCapability::Client Orphan::get() { return DynamicCapability::Client(schema, builder.asCapability()); } DynamicCapability::Client Orphan::getReader() const { return DynamicCapability::Client(schema, builder.asCapability()); } Orphan::Orphan(DynamicValue::Builder value, _::OrphanBuilder&& builder) : type(value.getType()), builder(kj::mv(builder)) { switch (type) { case DynamicValue::UNKNOWN: break; case DynamicValue::VOID: voidValue = value.voidValue; break; case DynamicValue::BOOL: boolValue = value.boolValue; break; case DynamicValue::INT: intValue = value.intValue; break; case DynamicValue::UINT: uintValue = value.uintValue; break; case DynamicValue::FLOAT: floatValue = value.floatValue; break; case DynamicValue::ENUM: enumValue = value.enumValue; break; case DynamicValue::TEXT: break; case DynamicValue::DATA: break; case DynamicValue::LIST: listSchema = value.listValue.getSchema(); break; case DynamicValue::STRUCT: structSchema = value.structValue.getSchema(); break; case DynamicValue::CAPABILITY: interfaceSchema = value.capabilityValue.getSchema(); break; case DynamicValue::ANY_POINTER: break; } } DynamicValue::Builder Orphan::get() { switch (type) { case DynamicValue::UNKNOWN: return nullptr; case DynamicValue::VOID: return voidValue; case DynamicValue::BOOL: return boolValue; case DynamicValue::INT: return intValue; case DynamicValue::UINT: return uintValue; case DynamicValue::FLOAT: return floatValue; case DynamicValue::ENUM: return enumValue; case DynamicValue::TEXT: return builder.asText(); case DynamicValue::DATA: return builder.asData(); case DynamicValue::LIST: if (listSchema.whichElementType() == schema::Type::STRUCT) { return DynamicList::Builder(listSchema, builder.asStructList(structSizeFromSchema(listSchema.getStructElementType()))); } else { return DynamicList::Builder(listSchema, builder.asList(elementSizeFor(listSchema.whichElementType()))); } case DynamicValue::STRUCT: return DynamicStruct::Builder(structSchema, builder.asStruct(structSizeFromSchema(structSchema))); case DynamicValue::CAPABILITY: return DynamicCapability::Client(interfaceSchema, builder.asCapability()); case DynamicValue::ANY_POINTER: KJ_FAIL_REQUIRE("Can't get() an AnyPointer orphan; there is no underlying pointer to " "wrap in an AnyPointer::Builder."); } KJ_UNREACHABLE; } DynamicValue::Reader Orphan::getReader() const { switch (type) { case DynamicValue::UNKNOWN: return nullptr; case DynamicValue::VOID: return voidValue; case DynamicValue::BOOL: return boolValue; case DynamicValue::INT: return intValue; case DynamicValue::UINT: return uintValue; case DynamicValue::FLOAT: return floatValue; case DynamicValue::ENUM: return enumValue; case DynamicValue::TEXT: return builder.asTextReader(); case DynamicValue::DATA: return builder.asDataReader(); case DynamicValue::LIST: return DynamicList::Reader(listSchema, builder.asListReader(elementSizeFor(listSchema.whichElementType()))); case DynamicValue::STRUCT: return DynamicStruct::Reader(structSchema, builder.asStructReader(structSizeFromSchema(structSchema))); case DynamicValue::CAPABILITY: return DynamicCapability::Client(interfaceSchema, builder.asCapability()); case DynamicValue::ANY_POINTER: KJ_FAIL_ASSERT("Can't get() an AnyPointer orphan; there is no underlying pointer to " "wrap in an AnyPointer::Builder."); } KJ_UNREACHABLE; } template <> Orphan Orphan::releaseAs() { KJ_REQUIRE(type == DynamicValue::ANY_POINTER, "Value type mismatch."); type = DynamicValue::UNKNOWN; return Orphan(kj::mv(builder)); } template <> Orphan Orphan::releaseAs() { KJ_REQUIRE(type == DynamicValue::STRUCT, "Value type mismatch."); type = DynamicValue::UNKNOWN; return Orphan(structSchema, kj::mv(builder)); } template <> Orphan Orphan::releaseAs() { KJ_REQUIRE(type == DynamicValue::LIST, "Value type mismatch."); type = DynamicValue::UNKNOWN; return Orphan(listSchema, kj::mv(builder)); } template <> Orphan Orphanage::newOrphanCopy( DynamicValue::Reader copyFrom) const { switch (copyFrom.getType()) { case DynamicValue::UNKNOWN: return nullptr; case DynamicValue::VOID: return copyFrom.voidValue; case DynamicValue::BOOL: return copyFrom.boolValue; case DynamicValue::INT: return copyFrom.intValue; case DynamicValue::UINT: return copyFrom.uintValue; case DynamicValue::FLOAT: return copyFrom.floatValue; case DynamicValue::ENUM: return copyFrom.enumValue; case DynamicValue::TEXT: return newOrphanCopy(copyFrom.textValue); case DynamicValue::DATA: return newOrphanCopy(copyFrom.dataValue); case DynamicValue::LIST: return newOrphanCopy(copyFrom.listValue); case DynamicValue::STRUCT: return newOrphanCopy(copyFrom.structValue); case DynamicValue::CAPABILITY: return newOrphanCopy(copyFrom.capabilityValue); case DynamicValue::ANY_POINTER: return newOrphanCopy(copyFrom.anyPointerValue); } KJ_UNREACHABLE; } } // namespace capnp capnproto-c++-0.8.0/src/capnp/stringify.c++0000644000175000017500000002260713340402540021152 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "dynamic.h" #include #include #include namespace capnp { namespace { enum PrintMode { BARE, // The value is planned to be printed on its own line, unless it is very short and contains // no inner newlines. PREFIXED, // The value is planned to be printed with a prefix, like "memberName = " (a struct field). PARENTHESIZED // The value is printed in parenthesized (a union value). }; enum class PrintKind { LIST, RECORD }; class Indent { public: explicit Indent(bool enable): amount(enable ? 1 : 0) {} Indent next() { return Indent(amount == 0 ? 0 : amount + 1); } kj::StringTree delimit(kj::Array items, PrintMode mode, PrintKind kind) { if (amount == 0 || canPrintAllInline(items, kind)) { return kj::StringTree(kj::mv(items), ", "); } else { KJ_STACK_ARRAY(char, delimArrayPtr, amount * 2 + 3, 32, 256); auto delim = delimArrayPtr.begin(); delim[0] = ','; delim[1] = '\n'; memset(delim + 2, ' ', amount * 2); delim[amount * 2 + 2] = '\0'; // If the outer value isn't being printed on its own line, we need to add a newline/indent // before the first item, otherwise we only add a space on the assumption that it is preceded // by an open bracket or parenthesis. return kj::strTree(mode == BARE ? " " : delim + 1, kj::StringTree(kj::mv(items), kj::StringPtr(delim, amount * 2 + 2)), ' '); } } private: uint amount; explicit Indent(uint amount): amount(amount) {} static constexpr size_t maxInlineValueSize = 24; static constexpr size_t maxInlineRecordSize = 64; static bool canPrintInline(const kj::StringTree& text) { if (text.size() > maxInlineValueSize) { return false; } char flat[maxInlineValueSize + 1]; text.flattenTo(flat); flat[text.size()] = '\0'; if (strchr(flat, '\n') != nullptr) { return false; } return true; } static bool canPrintAllInline(const kj::Array& items, PrintKind kind) { size_t totalSize = 0; for (auto& item: items) { if (!canPrintInline(item)) return false; if (kind == PrintKind::RECORD) { totalSize += item.size(); if (totalSize > maxInlineRecordSize) return false; } } return true; } }; static schema::Type::Which whichFieldType(const StructSchema::Field& field) { auto proto = field.getProto(); switch (proto.which()) { case schema::Field::SLOT: return proto.getSlot().getType().which(); case schema::Field::GROUP: return schema::Type::STRUCT; } KJ_UNREACHABLE; } static kj::StringTree print(const DynamicValue::Reader& value, schema::Type::Which which, Indent indent, PrintMode mode) { switch (value.getType()) { case DynamicValue::UNKNOWN: return kj::strTree("?"); case DynamicValue::VOID: return kj::strTree("void"); case DynamicValue::BOOL: return kj::strTree(value.as() ? "true" : "false"); case DynamicValue::INT: return kj::strTree(value.as()); case DynamicValue::UINT: return kj::strTree(value.as()); case DynamicValue::FLOAT: if (which == schema::Type::FLOAT32) { return kj::strTree(value.as()); } else { return kj::strTree(value.as()); } case DynamicValue::TEXT: case DynamicValue::DATA: { // TODO(someday): Maybe data should be printed as binary literal. kj::ArrayPtr chars; if (value.getType() == DynamicValue::DATA) { chars = value.as().asChars(); } else { chars = value.as(); } return kj::strTree('"', kj::encodeCEscape(chars), '"'); } case DynamicValue::LIST: { auto listValue = value.as(); auto which = listValue.getSchema().whichElementType(); kj::Array elements = KJ_MAP(element, listValue) { return print(element, which, indent.next(), BARE); }; return kj::strTree('[', indent.delimit(kj::mv(elements), mode, PrintKind::LIST), ']'); } case DynamicValue::ENUM: { auto enumValue = value.as(); KJ_IF_MAYBE(enumerant, enumValue.getEnumerant()) { return kj::strTree(enumerant->getProto().getName()); } else { // Unknown enum value; output raw number. return kj::strTree('(', enumValue.getRaw(), ')'); } break; } case DynamicValue::STRUCT: { auto structValue = value.as(); auto unionFields = structValue.getSchema().getUnionFields(); auto nonUnionFields = structValue.getSchema().getNonUnionFields(); kj::Vector printedFields(nonUnionFields.size() + (unionFields.size() != 0)); // We try to write the union field, if any, in proper order with the rest. auto which = structValue.which(); kj::StringTree unionValue; KJ_IF_MAYBE(field, which) { // Even if the union field has its default value, if it is not the default field of the // union then we have to print it anyway. auto fieldProto = field->getProto(); if (fieldProto.getDiscriminantValue() != 0 || structValue.has(*field)) { unionValue = kj::strTree( fieldProto.getName(), " = ", print(structValue.get(*field), whichFieldType(*field), indent.next(), PREFIXED)); } else { which = nullptr; } } for (auto field: nonUnionFields) { KJ_IF_MAYBE(unionField, which) { if (unionField->getIndex() < field.getIndex()) { printedFields.add(kj::mv(unionValue)); which = nullptr; } } if (structValue.has(field)) { printedFields.add(kj::strTree( field.getProto().getName(), " = ", print(structValue.get(field), whichFieldType(field), indent.next(), PREFIXED))); } } if (which != nullptr) { // Union value is last. printedFields.add(kj::mv(unionValue)); } if (mode == PARENTHESIZED) { return indent.delimit(printedFields.releaseAsArray(), mode, PrintKind::RECORD); } else { return kj::strTree( '(', indent.delimit(printedFields.releaseAsArray(), mode, PrintKind::RECORD), ')'); } } case DynamicValue::CAPABILITY: return kj::strTree(""); case DynamicValue::ANY_POINTER: return kj::strTree(""); } KJ_UNREACHABLE; } kj::StringTree stringify(DynamicValue::Reader value) { return print(value, schema::Type::STRUCT, Indent(false), BARE); } } // namespace kj::StringTree prettyPrint(DynamicStruct::Reader value) { return print(value, schema::Type::STRUCT, Indent(true), BARE); } kj::StringTree prettyPrint(DynamicList::Reader value) { return print(value, schema::Type::LIST, Indent(true), BARE); } kj::StringTree prettyPrint(DynamicStruct::Builder value) { return prettyPrint(value.asReader()); } kj::StringTree prettyPrint(DynamicList::Builder value) { return prettyPrint(value.asReader()); } kj::StringTree KJ_STRINGIFY(const DynamicValue::Reader& value) { return stringify(value); } kj::StringTree KJ_STRINGIFY(const DynamicValue::Builder& value) { return stringify(value.asReader()); } kj::StringTree KJ_STRINGIFY(DynamicEnum value) { return stringify(value); } kj::StringTree KJ_STRINGIFY(const DynamicStruct::Reader& value) { return stringify(value); } kj::StringTree KJ_STRINGIFY(const DynamicStruct::Builder& value) { return stringify(value.asReader()); } kj::StringTree KJ_STRINGIFY(const DynamicList::Reader& value) { return stringify(value); } kj::StringTree KJ_STRINGIFY(const DynamicList::Builder& value) { return stringify(value.asReader()); } namespace _ { // private kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema) { return stringify(DynamicStruct::Reader(Schema(&schema).asStruct(), reader)); } kj::String enumString(uint16_t value, const RawBrandedSchema& schema) { auto enumerants = Schema(&schema).asEnum().getEnumerants(); if (value < enumerants.size()) { return kj::heapString(enumerants[value].getProto().getName()); } else { return kj::str(value); } } } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema-parser.c++0000644000175000017500000003504513340402540021666 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "schema-parser.h" #include "message.h" #include #include #include #include #include #include #include #include #include #include #include namespace capnp { namespace { template size_t findLargestElementBefore(const kj::Vector& vec, const T& key) { KJ_REQUIRE(vec.size() > 0 && vec[0] <= key); size_t lower = 0; size_t upper = vec.size(); while (upper - lower > 1) { size_t mid = (lower + upper) / 2; if (vec[mid] > key) { upper = mid; } else { lower = mid; } } return lower; } } // namespace // ======================================================================================= class SchemaParser::ModuleImpl final: public compiler::Module { public: ModuleImpl(const SchemaParser& parser, kj::Own&& file) : parser(parser), file(kj::mv(file)) {} kj::StringPtr getSourceName() override { return file->getDisplayName(); } Orphan loadContent(Orphanage orphanage) override { kj::Array content = file->readContent(); lineBreaks.get([&](kj::SpaceFor>& space) { auto vec = space.construct(content.size() / 40); vec->add(0); for (const char* pos = content.begin(); pos < content.end(); ++pos) { if (*pos == '\n') { vec->add(pos + 1 - content.begin()); } } return vec; }); MallocMessageBuilder lexedBuilder; auto statements = lexedBuilder.initRoot(); compiler::lex(content, statements, *this); auto parsed = orphanage.newOrphan(); compiler::parseFile(statements.getStatements(), parsed.get(), *this); return parsed; } kj::Maybe importRelative(kj::StringPtr importPath) override { KJ_IF_MAYBE(importedFile, file->import(importPath)) { return parser.getModuleImpl(kj::mv(*importedFile)); } else { return nullptr; } } kj::Maybe> embedRelative(kj::StringPtr embedPath) override { KJ_IF_MAYBE(importedFile, file->import(embedPath)) { return importedFile->get()->readContent().releaseAsBytes(); } else { return nullptr; } } void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) override { auto& lines = lineBreaks.get( [](kj::SpaceFor>& space) { KJ_FAIL_REQUIRE("Can't report errors until loadContent() is called."); return space.construct(); }); // TODO(someday): This counts tabs as single characters. Do we care? uint startLine = findLargestElementBefore(lines, startByte); uint startCol = startByte - lines[startLine]; uint endLine = findLargestElementBefore(lines, endByte); uint endCol = endByte - lines[endLine]; file->reportError( SchemaFile::SourcePos { startByte, startLine, startCol }, SchemaFile::SourcePos { endByte, endLine, endCol }, message); // We intentionally only set hadErrors true if reportError() didn't throw. parser.hadErrors = true; } bool hadErrors() override { return parser.hadErrors; } private: const SchemaParser& parser; kj::Own file; kj::Lazy> lineBreaks; // Byte offsets of the first byte in each source line. The first element is always zero. // Initialized the first time the module is loaded. }; // ======================================================================================= namespace { struct SchemaFileHash { inline bool operator()(const SchemaFile* f) const { return f->hashCode(); } }; struct SchemaFileEq { inline bool operator()(const SchemaFile* a, const SchemaFile* b) const { return *a == *b; } }; } // namespace struct SchemaParser::DiskFileCompat { // Stuff we only create if parseDiskFile() is ever called, in order to translate that call into // KJ filesystem API calls. kj::Own ownFs; kj::Filesystem& fs; struct ImportDir { kj::String pathStr; kj::Path path; kj::Own dir; }; std::map cachedImportDirs; std::map, kj::Array> cachedImportPaths; DiskFileCompat(): ownFs(kj::newDiskFilesystem()), fs(*ownFs) {} DiskFileCompat(kj::Filesystem& fs): fs(fs) {} }; struct SchemaParser::Impl { typedef std::unordered_map< const SchemaFile*, kj::Own, SchemaFileHash, SchemaFileEq> FileMap; kj::MutexGuarded fileMap; compiler::Compiler compiler; kj::MutexGuarded> compat; }; SchemaParser::SchemaParser(): impl(kj::heap()) {} SchemaParser::~SchemaParser() noexcept(false) {} ParsedSchema SchemaParser::parseFromDirectory( const kj::ReadableDirectory& baseDir, kj::Path path, kj::ArrayPtr importPath) const { return parseFile(SchemaFile::newFromDirectory(baseDir, kj::mv(path), importPath)); } ParsedSchema SchemaParser::parseDiskFile( kj::StringPtr displayName, kj::StringPtr diskPath, kj::ArrayPtr importPath) const { auto lock = impl->compat.lockExclusive(); DiskFileCompat* compat; KJ_IF_MAYBE(c, *lock) { compat = c; } else { compat = &lock->emplace(); } auto& root = compat->fs.getRoot(); auto cwd = compat->fs.getCurrentPath(); const kj::ReadableDirectory* baseDir = &root; kj::Path path = cwd.evalNative(diskPath); kj::ArrayPtr translatedImportPath = nullptr; if (importPath.size() > 0) { auto importPathKey = std::make_pair(importPath.begin(), importPath.size()); auto& slot = compat->cachedImportPaths[importPathKey]; if (slot == nullptr) { slot = KJ_MAP(path, importPath) -> const kj::ReadableDirectory* { auto iter = compat->cachedImportDirs.find(path); if (iter != compat->cachedImportDirs.end()) { return iter->second.dir; } auto parsed = cwd.evalNative(path); kj::Own dir; KJ_IF_MAYBE(d, root.tryOpenSubdir(parsed)) { dir = kj::mv(*d); } else { // Ignore paths that don't exist. dir = kj::newInMemoryDirectory(kj::nullClock()); } const kj::ReadableDirectory* result = dir; kj::StringPtr pathRef = path; KJ_ASSERT(compat->cachedImportDirs.insert(std::make_pair(pathRef, DiskFileCompat::ImportDir { kj::str(path), kj::mv(parsed), kj::mv(dir) })).second); return result; }; } translatedImportPath = slot; // Check if `path` appears to be inside any of the import path directories. If so, adjust // to be relative to that directory rather than absolute. kj::Maybe matchedImportDir; size_t bestMatchLength = 0; for (auto importDir: importPath) { auto iter = compat->cachedImportDirs.find(importDir); KJ_ASSERT(iter != compat->cachedImportDirs.end()); if (path.startsWith(iter->second.path)) { // Looks like we're trying to load a file from inside this import path. Treat the import // path as the base directory. if (iter->second.path.size() > bestMatchLength) { bestMatchLength = iter->second.path.size(); matchedImportDir = iter->second; } } } KJ_IF_MAYBE(match, matchedImportDir) { baseDir = match->dir; path = path.slice(match->path.size(), path.size()).clone(); } } return parseFile(SchemaFile::newFromDirectory( *baseDir, kj::mv(path), translatedImportPath, kj::str(displayName))); } void SchemaParser::setDiskFilesystem(kj::Filesystem& fs) { auto lock = impl->compat.lockExclusive(); KJ_REQUIRE(*lock == nullptr, "already called parseDiskFile() or setDiskFilesystem()"); lock->emplace(fs); } ParsedSchema SchemaParser::parseFile(kj::Own&& file) const { KJ_DEFER(impl->compiler.clearWorkspace()); uint64_t id = impl->compiler.add(getModuleImpl(kj::mv(file))); impl->compiler.eagerlyCompile(id, compiler::Compiler::NODE | compiler::Compiler::CHILDREN | compiler::Compiler::DEPENDENCIES | compiler::Compiler::DEPENDENCY_DEPENDENCIES); return ParsedSchema(impl->compiler.getLoader().get(id), *this); } kj::Maybe SchemaParser::getSourceInfo(Schema schema) const { return impl->compiler.getSourceInfo(schema.getProto().getId()); } SchemaParser::ModuleImpl& SchemaParser::getModuleImpl(kj::Own&& file) const { auto lock = impl->fileMap.lockExclusive(); auto insertResult = lock->insert(std::make_pair(file.get(), kj::Own())); if (insertResult.second) { // This is a newly-inserted entry. Construct the ModuleImpl. insertResult.first->second = kj::heap(*this, kj::mv(file)); } return *insertResult.first->second; } SchemaLoader& SchemaParser::getLoader() { return impl->compiler.getLoader(); } kj::Maybe ParsedSchema::findNested(kj::StringPtr name) const { return parser->impl->compiler.lookup(getProto().getId(), name).map( [this](uint64_t childId) { return ParsedSchema(parser->impl->compiler.getLoader().get(childId), *parser); }); } ParsedSchema ParsedSchema::getNested(kj::StringPtr nestedName) const { KJ_IF_MAYBE(nested, findNested(nestedName)) { return *nested; } else { KJ_FAIL_REQUIRE("no such nested declaration", getProto().getDisplayName(), nestedName); } } schema::Node::SourceInfo::Reader ParsedSchema::getSourceInfo() const { return KJ_ASSERT_NONNULL(parser->getSourceInfo(*this)); } // ------------------------------------------------------------------- class SchemaFile::DiskSchemaFile final: public SchemaFile { public: DiskSchemaFile(const kj::ReadableDirectory& baseDir, kj::Path pathParam, kj::ArrayPtr importPath, kj::Own file, kj::Maybe displayNameOverride) : baseDir(baseDir), path(kj::mv(pathParam)), importPath(importPath), file(kj::mv(file)) { KJ_IF_MAYBE(dn, displayNameOverride) { displayName = kj::mv(*dn); displayNameOverridden = true; } else { displayName = path.toString(); displayNameOverridden = false; } } kj::StringPtr getDisplayName() const override { return displayName; } kj::Array readContent() const override { return file->mmap(0, file->stat().size).releaseAsChars(); } kj::Maybe> import(kj::StringPtr target) const override { if (target.startsWith("/")) { auto parsed = kj::Path::parse(target.slice(1)); for (auto candidate: importPath) { KJ_IF_MAYBE(newFile, candidate->tryOpenFile(parsed)) { return kj::implicitCast>(kj::heap( *candidate, kj::mv(parsed), importPath, kj::mv(*newFile), nullptr)); } } return nullptr; } else { auto parsed = path.parent().eval(target); kj::Maybe displayNameOverride; if (displayNameOverridden) { // Try to create a consistent display name override for the imported file. This is for // backwards-compatibility only -- display names are only overridden when using the // deprecated parseDiskFile() interface. kj::runCatchingExceptions([&]() { displayNameOverride = kj::Path::parse(displayName).parent().eval(target).toString(); }); } KJ_IF_MAYBE(newFile, baseDir.tryOpenFile(parsed)) { return kj::implicitCast>(kj::heap( baseDir, kj::mv(parsed), importPath, kj::mv(*newFile), kj::mv(displayNameOverride))); } else { return nullptr; } } } bool operator==(const SchemaFile& other) const override { auto& other2 = kj::downcast(other); return &baseDir == &other2.baseDir && path == other2.path; } bool operator!=(const SchemaFile& other) const override { return !operator==(other); } size_t hashCode() const override { // djb hash with xor // TODO(someday): Add hashing library to KJ. size_t result = reinterpret_cast(&baseDir); for (auto& part: path) { for (char c: part) { result = (result * 33) ^ c; } result = (result * 33) ^ '/'; } return result; } void reportError(SourcePos start, SourcePos end, kj::StringPtr message) const override { kj::getExceptionCallback().onRecoverableException(kj::Exception( kj::Exception::Type::FAILED, path.toString(), start.line, kj::heapString(message))); } private: const kj::ReadableDirectory& baseDir; kj::Path path; kj::ArrayPtr importPath; kj::Own file; kj::String displayName; bool displayNameOverridden; }; kj::Own SchemaFile::newFromDirectory( const kj::ReadableDirectory& baseDir, kj::Path path, kj::ArrayPtr importPath, kj::Maybe displayNameOverride) { return kj::heap(baseDir, kj::mv(path), importPath, baseDir.openFile(path), kj::mv(displayNameOverride)); } } // namespace capnp capnproto-c++-0.8.0/src/capnp/serialize-text.c++0000644000175000017500000001216513340402540022103 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Philip Quinn. // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "serialize-text.h" #include #include "pretty-print.h" #include "compiler/lexer.capnp.h" #include "compiler/lexer.h" #include "compiler/node-translator.h" #include "compiler/parser.h" namespace { class ThrowingErrorReporter final: public capnp::compiler::ErrorReporter { // Throws all errors as assertion failures. public: void addError(uint32_t startByte, uint32_t endByte, kj::StringPtr message) override { KJ_FAIL_REQUIRE(kj::str(message, " (", startByte, ":", endByte, ").")); } bool hadErrors() override { return false; } }; class ExternalResolver final: public capnp::compiler::ValueTranslator::Resolver { // Throws all external resolution requests as assertion failures. public: kj::Maybe resolveConstant(capnp::compiler::Expression::Reader name) override { KJ_FAIL_REQUIRE("External constants not allowed."); } kj::Maybe> readEmbed(capnp::compiler::LocatedText::Reader filename) override { KJ_FAIL_REQUIRE("External embeds not allowed."); } }; template void lexAndParseExpression(kj::StringPtr input, Function f) { // Parses a single expression from the input and calls `f(expression)`. ThrowingErrorReporter errorReporter; capnp::MallocMessageBuilder tokenArena; auto lexedTokens = tokenArena.initRoot(); capnp::compiler::lex(input, lexedTokens, errorReporter); capnp::compiler::CapnpParser parser(tokenArena.getOrphanage(), errorReporter); auto tokens = lexedTokens.asReader().getTokens(); capnp::compiler::CapnpParser::ParserInput parserInput(tokens.begin(), tokens.end()); if (parserInput.getPosition() != tokens.end()) { KJ_IF_MAYBE(expression, parser.getParsers().expression(parserInput)) { // The input is expected to contain a *single* message. KJ_REQUIRE(parserInput.getPosition() == tokens.end(), "Extra tokens in input."); f(expression->getReader()); } else { auto best = parserInput.getBest(); if (best == tokens.end()) { KJ_FAIL_REQUIRE("Premature end of input."); } else { errorReporter.addErrorOn(*best, "Parse error"); } } } else { KJ_FAIL_REQUIRE("Failed to read input."); } } } // namespace namespace capnp { TextCodec::TextCodec() : prettyPrint(false) {} TextCodec::~TextCodec() noexcept(true) {} void TextCodec::setPrettyPrint(bool enabled) { prettyPrint = enabled; } kj::String TextCodec::encode(DynamicValue::Reader value) const { if (!prettyPrint) { return kj::str(value); } else { if (value.getType() == DynamicValue::Type::STRUCT) { return capnp::prettyPrint(value.as()).flatten(); } else if (value.getType() == DynamicValue::Type::LIST) { return capnp::prettyPrint(value.as()).flatten(); } else { return kj::str(value); } } } void TextCodec::decode(kj::StringPtr input, DynamicStruct::Builder output) const { lexAndParseExpression(input, [&output](compiler::Expression::Reader expression) { KJ_REQUIRE(expression.isTuple(), "Input does not contain a struct."); ThrowingErrorReporter errorReporter; ExternalResolver nullResolver; Orphanage orphanage = Orphanage::getForMessageContaining(output); compiler::ValueTranslator translator(nullResolver, errorReporter, orphanage); translator.fillStructValue(output, expression.getTuple()); }); } Orphan TextCodec::decode(kj::StringPtr input, Type type, Orphanage orphanage) const { Orphan output; lexAndParseExpression(input, [&type, &orphanage, &output](compiler::Expression::Reader expression) { ThrowingErrorReporter errorReporter; ExternalResolver nullResolver; compiler::ValueTranslator translator(nullResolver, errorReporter, orphanage); KJ_IF_MAYBE(value, translator.compileValue(expression, type)) { output = *kj::mv(value); } else { // An error should have already been given to the errorReporter. } }); return output; } } // namespace capnp capnproto-c++-0.8.0/src/capnp/afl-testcase.c++0000644000175000017500000001131613340402540021502 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "test-util.h" #include #include "serialize.h" #include #include namespace capnp { namespace _ { namespace { class AflTestMain { public: explicit AflTestMain(kj::ProcessContext& context) : context(context) {} kj::MainFunc getMain() { return kj::MainBuilder(context, "(unknown version)", "American Fuzzy Lop test case. Pass input on stdin. Expects a binary " "message of type TestAllTypes.") .addOption({"lists"}, KJ_BIND_METHOD(*this, runLists), "Expect a message of type TestLists instead of TestAllTypes.") .addOption({"canonicalize"}, KJ_BIND_METHOD(*this, canonicalize), "Test canonicalization code.") .callAfterParsing(KJ_BIND_METHOD(*this, run)) .build(); } kj::MainBuilder::Validity run() { capnp::StreamFdMessageReader reader(STDIN_FILENO); KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { checkTestMessage(reader.getRoot()); })) { KJ_LOG(ERROR, "threw"); } KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { checkDynamicTestMessage(reader.getRoot(Schema::from())); })) { KJ_LOG(ERROR, "dynamic threw"); } KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { kj::str(reader.getRoot()); })) { KJ_LOG(ERROR, "str threw"); } return true; } kj::MainBuilder::Validity runLists() { capnp::StreamFdMessageReader reader(STDIN_FILENO); KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { kj::str(reader.getRoot()); })) { KJ_LOG(ERROR, "threw"); } return true; } kj::MainBuilder::Validity canonicalize() { // (Test case contributed by David Renshaw.) kj::Array canonical; bool equal = false; KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { capnp::ReaderOptions options; // The default traversal limit of 8 * 1024 * 1024 causes // AFL to think that it has found "hang" bugs. options.traversalLimitInWords = 8 * 1024; capnp::StreamFdMessageReader message(0, options); // read from stdin TestAllTypes::Reader myStruct = message.getRoot(); canonical = capnp::canonicalize(myStruct); kj::ArrayPtr segments[1] = {canonical.asPtr()}; capnp::SegmentArrayMessageReader reader(kj::arrayPtr(segments, 1)); auto originalAny = message.getRoot(); // Discard cases where the original message is null. KJ_ASSERT(!originalAny.isNull()); equal = originalAny == reader.getRoot(); })) { // Probably some kind of decoding exception. KJ_LOG(ERROR, "threw"); context.exit(); } KJ_ASSERT(equal); kj::ArrayPtr segments[1] = {canonical.asPtr()}; capnp::SegmentArrayMessageReader reader(kj::arrayPtr(segments, 1)); KJ_ASSERT(reader.isCanonical()); kj::Array canonical2; { capnp::ReaderOptions options; options.traversalLimitInWords = 8 * 1024; TestAllTypes::Reader myStruct = reader.getRoot(); canonical2 = capnp::canonicalize(myStruct); } KJ_ASSERT(canonical.size() == canonical2.size()); auto b1 = canonical.asBytes(); auto b2 = canonical2.asBytes(); for (int idx = 0; idx < b1.size(); ++idx) { KJ_ASSERT(b1[idx] == b2[idx], idx, b1.size()); } return true; } private: kj::ProcessContext& context; }; } // namespace } // namespace _ } // namespace capnp KJ_MAIN(capnp::_::AflTestMain); capnproto-c++-0.8.0/src/capnp/common-test.c++0000644000175000017500000000627313340402540021402 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "common.h" #include #include #include #include #if HAVE_CONFIG_H #include "config.h" #endif namespace capnp { namespace { TEST(Common, Version) { #ifdef VERSION auto expectedVersion = kj::str(CAPNP_VERSION_MAJOR, '.', CAPNP_VERSION_MINOR, '.', CAPNP_VERSION_MICRO); auto devVersion = kj::str(CAPNP_VERSION_MAJOR, '.', CAPNP_VERSION_MINOR, "-dev"); kj::StringPtr actualVersion = VERSION; KJ_ASSERT(actualVersion == expectedVersion || actualVersion.startsWith(kj::str(expectedVersion, '-')) || actualVersion.startsWith(kj::str(expectedVersion, '.')) || (actualVersion == devVersion && CAPNP_VERSION_MICRO == 0), expectedVersion, actualVersion); #endif } struct ExampleStruct { struct _capnpPrivate { struct IsStruct; }; }; struct ExampleInterface { struct _capnpPrivate { struct IsInterface; }; }; static_assert(_::Kind_::kind == Kind::STRUCT, "Kind SFINAE failed."); static_assert(_::Kind_::kind == Kind::INTERFACE, "Kind SFINAE failed."); // Test FromAnay<> template struct EqualTypes_ { static constexpr bool value = false; }; template struct EqualTypes_ { static constexpr bool value = true; }; template inline constexpr bool equalTypes() { return EqualTypes_::value; } using capnproto_test::capnp::test::TestAllTypes; using capnproto_test::capnp::test::TestInterface; static_assert(equalTypes, int>(), ""); static_assert(equalTypes, TestAllTypes>(), ""); static_assert(equalTypes, TestAllTypes>(), ""); #if !CAPNP_LITE static_assert(equalTypes, TestAllTypes>(), ""); static_assert(equalTypes, TestInterface>(), ""); static_assert(equalTypes>, TestInterface>(), ""); #endif } // namespace } // namespace capnp capnproto-c++-0.8.0/src/capnp/blob-test.c++0000644000175000017500000001074313340402540021025 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "blob.h" #include #include #include #include "test-util.h" // TODO(test): This test is outdated -- it predates the retrofit of Text and Data on top of // kj::ArrayPtr/kj::StringPtr. Clean it up. namespace capnp { namespace { TEST(Blob, Text) { std::string str = "foo"; Text::Reader text = str.c_str(); EXPECT_EQ("foo", text); EXPECT_STREQ("foo", text.cStr()); EXPECT_STREQ("foo", text.begin()); EXPECT_EQ(3u, text.size()); Text::Reader text2 = "bar"; EXPECT_EQ("bar", text2); char c[4] = "baz"; Text::Reader text3(c); EXPECT_EQ("baz", text3); Text::Builder builder(c, 3); EXPECT_EQ("baz", builder); EXPECT_EQ(kj::arrayPtr("az", 2), builder.slice(1, 3)); } Data::Reader dataLit(const char* str) { return Data::Reader(reinterpret_cast(str), strlen(str)); } TEST(Blob, Data) { Data::Reader data = dataLit("foo"); EXPECT_EQ(dataLit("foo"), data); EXPECT_EQ(3u, data.size()); Data::Reader data2 = dataLit("bar"); EXPECT_EQ(dataLit("bar"), data2); byte c[4] = "baz"; Data::Reader data3(c, 3); EXPECT_EQ(dataLit("baz"), data3); Data::Builder builder(c, 3); EXPECT_EQ(dataLit("baz"), builder); EXPECT_EQ(dataLit("az"), builder.slice(1, 3)); } TEST(Blob, Compare) { EXPECT_TRUE (Text::Reader("foo") == Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foo") != Text::Reader("foo")); EXPECT_TRUE (Text::Reader("foo") <= Text::Reader("foo")); EXPECT_TRUE (Text::Reader("foo") >= Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foo") < Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foo") > Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foo") == Text::Reader("bar")); EXPECT_TRUE (Text::Reader("foo") != Text::Reader("bar")); EXPECT_FALSE(Text::Reader("foo") <= Text::Reader("bar")); EXPECT_TRUE (Text::Reader("foo") >= Text::Reader("bar")); EXPECT_FALSE(Text::Reader("foo") < Text::Reader("bar")); EXPECT_TRUE (Text::Reader("foo") > Text::Reader("bar")); EXPECT_FALSE(Text::Reader("bar") == Text::Reader("foo")); EXPECT_TRUE (Text::Reader("bar") != Text::Reader("foo")); EXPECT_TRUE (Text::Reader("bar") <= Text::Reader("foo")); EXPECT_FALSE(Text::Reader("bar") >= Text::Reader("foo")); EXPECT_TRUE (Text::Reader("bar") < Text::Reader("foo")); EXPECT_FALSE(Text::Reader("bar") > Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foobar") == Text::Reader("foo")); EXPECT_TRUE (Text::Reader("foobar") != Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foobar") <= Text::Reader("foo")); EXPECT_TRUE (Text::Reader("foobar") >= Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foobar") < Text::Reader("foo")); EXPECT_TRUE (Text::Reader("foobar") > Text::Reader("foo")); EXPECT_FALSE(Text::Reader("foo") == Text::Reader("foobar")); EXPECT_TRUE (Text::Reader("foo") != Text::Reader("foobar")); EXPECT_TRUE (Text::Reader("foo") <= Text::Reader("foobar")); EXPECT_FALSE(Text::Reader("foo") >= Text::Reader("foobar")); EXPECT_TRUE (Text::Reader("foo") < Text::Reader("foobar")); EXPECT_FALSE(Text::Reader("foo") > Text::Reader("foobar")); } #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP TEST(Blob, StlInterop) { std::string foo = "foo"; Text::Reader reader = foo; EXPECT_EQ("foo", reader); std::string bar = reader; EXPECT_EQ("foo", bar); } #endif } // namespace } // namespace capnp capnproto-c++-0.8.0/src/capnp/endian-test.c++0000644000175000017500000000604213340402540021342 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "endian.h" #include namespace capnp { namespace _ { // private namespace { #if CAPNP_DISABLE_ENDIAN_DETECTION #define Endian EndianUnoptimized #endif TEST(Endian, Byte) { byte bytes[] = {123, 45, 67, 89}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(123, vals[0].get()); EXPECT_EQ(45, vals[1].get()); EXPECT_EQ(67, vals[2].get()); EXPECT_EQ(89, vals[3].get()); vals[0].set(21); vals[1].set(43); vals[2].set(65); vals[3].set(87); EXPECT_EQ(21, bytes[0]); EXPECT_EQ(43, bytes[1]); EXPECT_EQ(65, bytes[2]); EXPECT_EQ(87, bytes[3]); } TEST(Endian, TwoBytes) { byte bytes[] = {0x12, 0x34, 0x56, 0x78}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(0x3412, vals[0].get()); EXPECT_EQ(0x7856, vals[1].get()); vals[0].set(0x2345); vals[1].set(0x6789); EXPECT_EQ(0x45, bytes[0]); EXPECT_EQ(0x23, bytes[1]); EXPECT_EQ(0x89, bytes[2]); EXPECT_EQ(0x67, bytes[3]); } TEST(Endian, FourBytes) { byte bytes[] = {0x12, 0x34, 0x56, 0x78}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(0x78563412u, vals[0].get()); vals[0].set(0x23456789); EXPECT_EQ(0x89, bytes[0]); EXPECT_EQ(0x67, bytes[1]); EXPECT_EQ(0x45, bytes[2]); EXPECT_EQ(0x23, bytes[3]); } TEST(Endian, EightBytes) { byte bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(0xf0debc9a78563412, vals[0].get()); vals[0].set(0x23456789abcdef01); EXPECT_EQ(0x01, bytes[0]); EXPECT_EQ(0xef, bytes[1]); EXPECT_EQ(0xcd, bytes[2]); EXPECT_EQ(0xab, bytes[3]); EXPECT_EQ(0x89, bytes[4]); EXPECT_EQ(0x67, bytes[5]); EXPECT_EQ(0x45, bytes[6]); EXPECT_EQ(0x23, bytes[7]); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/endian-fallback-test.c++0000644000175000017500000000245113340402540023077 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // Test that the shift-based implementation of WireValue works. #define CAPNP_DISABLE_ENDIAN_DETECTION 1 #include "endian-test.c++" capnproto-c++-0.8.0/src/capnp/endian-reverse-test.c++0000644000175000017500000000651213340402540023015 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if !_MSC_VER // (MSVC targets only little-endian platforms and so we haven't implemented any byte swapping // intrinsics for it. So, this test would fail there.) // Test that the code for the opposite endianness of our CPU works. E.g. on x86 this will test // the bswap-based code. #define CAPNP_REVERSE_ENDIAN 1 #include "endian.h" #include namespace capnp { namespace _ { // private namespace { TEST(EndianReverse, Byte) { byte bytes[] = {123, 45, 67, 89}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(123, vals[0].get()); EXPECT_EQ(45, vals[1].get()); EXPECT_EQ(67, vals[2].get()); EXPECT_EQ(89, vals[3].get()); vals[0].set(21); vals[1].set(43); vals[2].set(65); vals[3].set(87); EXPECT_EQ(21, bytes[0]); EXPECT_EQ(43, bytes[1]); EXPECT_EQ(65, bytes[2]); EXPECT_EQ(87, bytes[3]); } TEST(EndianReverse, TwoBytes) { byte bytes[] = {0x12, 0x34, 0x56, 0x78}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(0x1234, vals[0].get()); EXPECT_EQ(0x5678, vals[1].get()); vals[0].set(0x2345); vals[1].set(0x6789); EXPECT_EQ(0x23, bytes[0]); EXPECT_EQ(0x45, bytes[1]); EXPECT_EQ(0x67, bytes[2]); EXPECT_EQ(0x89, bytes[3]); } TEST(EndianReverse, FourBytes) { byte bytes[] = {0x12, 0x34, 0x56, 0x78}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(0x12345678u, vals[0].get()); vals[0].set(0x23456789); EXPECT_EQ(0x23, bytes[0]); EXPECT_EQ(0x45, bytes[1]); EXPECT_EQ(0x67, bytes[2]); EXPECT_EQ(0x89, bytes[3]); } TEST(EndianReverse, EightBytes) { byte bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}; WireValue* vals = reinterpret_cast*>(bytes); EXPECT_EQ(0x123456789abcdef0ull, vals[0].get()); vals[0].set(0x23456789abcdef01ull); EXPECT_EQ(0x23, bytes[0]); EXPECT_EQ(0x45, bytes[1]); EXPECT_EQ(0x67, bytes[2]); EXPECT_EQ(0x89, bytes[3]); EXPECT_EQ(0xab, bytes[4]); EXPECT_EQ(0xcd, bytes[5]); EXPECT_EQ(0xef, bytes[6]); EXPECT_EQ(0x01, bytes[7]); } } // namespace } // namespace _ (private) } // namespace capnp #endif // !_MSC_VER capnproto-c++-0.8.0/src/capnp/layout-test.c++0000644000175000017500000004300313340402540021417 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_PRIVATE #include "layout.h" #include "message.h" #include "arena.h" #include #if CAPNP_DEBUG_TYPES namespace kj { template String KJ_STRINGIFY(kj::Quantity value) { return kj::str(unboundAs(value / kj::unit>())); } // Hack: Allow direct comparisons and multiplications so that we don't have to rewrite the code // below. template inline constexpr Bounded<65535, T> operator*(uint a, Bounded b) { return assumeBits<16>(a * unbound(b)); } template inline constexpr Bounded<65535, uint> operator*(uint a, BoundedConst) { return assumeBits<16>(a * b); } } #endif namespace capnp { namespace _ { // private namespace { TEST(WireFormat, SimpleRawDataStruct) { AlignedData<2> data = {{ // Struct ref, offset = 1, dataSize = 1, pointerCount = 0 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // Content for the data section. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }}; StructReader reader = PointerReader::getRootUnchecked(data.words).getStruct(nullptr); EXPECT_EQ(0xefcdab8967452301ull, reader.getDataField(0 * ELEMENTS)); EXPECT_EQ(0u, reader.getDataField(1 * ELEMENTS)); EXPECT_EQ(0x67452301u, reader.getDataField(0 * ELEMENTS)); EXPECT_EQ(0xefcdab89u, reader.getDataField(1 * ELEMENTS)); EXPECT_EQ(0u, reader.getDataField(2 * ELEMENTS)); EXPECT_EQ(0x2301u, reader.getDataField(0 * ELEMENTS)); EXPECT_EQ(0x6745u, reader.getDataField(1 * ELEMENTS)); EXPECT_EQ(0xab89u, reader.getDataField(2 * ELEMENTS)); EXPECT_EQ(0xefcdu, reader.getDataField(3 * ELEMENTS)); EXPECT_EQ(0u, reader.getDataField(4 * ELEMENTS)); EXPECT_EQ(321u ^ 0xefcdab8967452301ull, reader.getDataField(0 * ELEMENTS, 321u)); EXPECT_EQ(321u ^ 0x67452301u, reader.getDataField(0 * ELEMENTS, 321u)); EXPECT_EQ(321u ^ 0x2301u, reader.getDataField(0 * ELEMENTS, 321u)); EXPECT_EQ(321u, reader.getDataField(1 * ELEMENTS, 321u)); EXPECT_EQ(321u, reader.getDataField(2 * ELEMENTS, 321u)); EXPECT_EQ(321u, reader.getDataField(4 * ELEMENTS, 321u)); // Bits EXPECT_TRUE (reader.getDataField(0 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(1 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(2 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(3 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(4 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(5 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(6 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(7 * ELEMENTS)); EXPECT_TRUE (reader.getDataField( 8 * ELEMENTS)); EXPECT_TRUE (reader.getDataField( 9 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(10 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(11 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(12 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(13 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(14 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(15 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(63 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(64 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(0 * ELEMENTS, false)); EXPECT_FALSE(reader.getDataField(1 * ELEMENTS, false)); EXPECT_TRUE (reader.getDataField(63 * ELEMENTS, false)); EXPECT_FALSE(reader.getDataField(64 * ELEMENTS, false)); EXPECT_FALSE(reader.getDataField(0 * ELEMENTS, true)); EXPECT_TRUE (reader.getDataField(1 * ELEMENTS, true)); EXPECT_FALSE(reader.getDataField(63 * ELEMENTS, true)); EXPECT_TRUE (reader.getDataField(64 * ELEMENTS, true)); } static const AlignedData<2> SUBSTRUCT_DEFAULT = {{0,0,0,0,1,0,0,0, 0,0,0,0,0,0,0,0}}; static const AlignedData<2> STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT = {{0,0,0,0,1,0,0,0, 0,0,0,0,0,0,0,0}}; static constexpr StructSize STRUCTLIST_ELEMENT_SIZE(1 * WORDS, 1 * POINTERS); static void setupStruct(StructBuilder builder) { builder.setDataField(0 * ELEMENTS, 0x1011121314151617ull); builder.setDataField(2 * ELEMENTS, 0x20212223u); builder.setDataField(6 * ELEMENTS, 0x3031u); builder.setDataField(14 * ELEMENTS, 0x40u); builder.setDataField(120 * ELEMENTS, false); builder.setDataField(121 * ELEMENTS, false); builder.setDataField(122 * ELEMENTS, true); builder.setDataField(123 * ELEMENTS, false); builder.setDataField(124 * ELEMENTS, true); builder.setDataField(125 * ELEMENTS, true); builder.setDataField(126 * ELEMENTS, true); builder.setDataField(127 * ELEMENTS, false); { StructBuilder subStruct = builder.getPointerField(0 * POINTERS).initStruct( StructSize(1 * WORDS, 0 * POINTERS)); subStruct.setDataField(0 * ELEMENTS, 123); } { ListBuilder list = builder.getPointerField(1 * POINTERS) .initList(ElementSize::FOUR_BYTES, 3 * ELEMENTS); EXPECT_EQ(3 * ELEMENTS, list.size()); list.setDataElement(0 * ELEMENTS, 200); list.setDataElement(1 * ELEMENTS, 201); list.setDataElement(2 * ELEMENTS, 202); } { ListBuilder list = builder.getPointerField(2 * POINTERS).initStructList( 4 * ELEMENTS, STRUCTLIST_ELEMENT_SIZE); EXPECT_EQ(4 * ELEMENTS, list.size()); for (int i = 0; i < 4; i++) { StructBuilder element = list.getStructElement(i * ELEMENTS); element.setDataField(0 * ELEMENTS, 300 + i); element.getPointerField(0 * POINTERS) .initStruct(StructSize(1 * WORDS, 0 * POINTERS)) .setDataField(0 * ELEMENTS, 400 + i); } } { ListBuilder list = builder.getPointerField(3 * POINTERS) .initList(ElementSize::POINTER, 5 * ELEMENTS); EXPECT_EQ(5 * ELEMENTS, list.size()); for (uint i = 0; i < 5; i++) { ListBuilder element = list.getPointerElement(i * ELEMENTS) .initList(ElementSize::TWO_BYTES, (i + 1) * ELEMENTS); EXPECT_EQ((i + 1) * ELEMENTS, element.size()); for (uint j = 0; j <= i; j++) { element.setDataElement(j * ELEMENTS, 500 + j); } } } } static void checkStruct(StructBuilder builder) { EXPECT_EQ(0x1011121314151617ull, builder.getDataField(0 * ELEMENTS)); EXPECT_EQ(0x20212223u, builder.getDataField(2 * ELEMENTS)); EXPECT_EQ(0x3031u, builder.getDataField(6 * ELEMENTS)); EXPECT_EQ(0x40u, builder.getDataField(14 * ELEMENTS)); EXPECT_FALSE(builder.getDataField(120 * ELEMENTS)); EXPECT_FALSE(builder.getDataField(121 * ELEMENTS)); EXPECT_TRUE (builder.getDataField(122 * ELEMENTS)); EXPECT_FALSE(builder.getDataField(123 * ELEMENTS)); EXPECT_TRUE (builder.getDataField(124 * ELEMENTS)); EXPECT_TRUE (builder.getDataField(125 * ELEMENTS)); EXPECT_TRUE (builder.getDataField(126 * ELEMENTS)); EXPECT_FALSE(builder.getDataField(127 * ELEMENTS)); { StructBuilder subStruct = builder.getPointerField(0 * POINTERS).getStruct( StructSize(1 * WORDS, 0 * POINTERS), SUBSTRUCT_DEFAULT.words); EXPECT_EQ(123u, subStruct.getDataField(0 * ELEMENTS)); } { ListBuilder list = builder.getPointerField(1 * POINTERS) .getList(ElementSize::FOUR_BYTES, nullptr); ASSERT_EQ(3 * ELEMENTS, list.size()); EXPECT_EQ(200, list.getDataElement(0 * ELEMENTS)); EXPECT_EQ(201, list.getDataElement(1 * ELEMENTS)); EXPECT_EQ(202, list.getDataElement(2 * ELEMENTS)); } { ListBuilder list = builder.getPointerField(2 * POINTERS) .getStructList(STRUCTLIST_ELEMENT_SIZE, nullptr); ASSERT_EQ(4 * ELEMENTS, list.size()); for (int i = 0; i < 4; i++) { StructBuilder element = list.getStructElement(i * ELEMENTS); EXPECT_EQ(300 + i, element.getDataField(0 * ELEMENTS)); EXPECT_EQ(400 + i, element.getPointerField(0 * POINTERS) .getStruct(StructSize(1 * WORDS, 0 * POINTERS), STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT.words) .getDataField(0 * ELEMENTS)); } } { ListBuilder list = builder.getPointerField(3 * POINTERS).getList(ElementSize::POINTER, nullptr); ASSERT_EQ(5 * ELEMENTS, list.size()); for (uint i = 0; i < 5; i++) { ListBuilder element = list.getPointerElement(i * ELEMENTS) .getList(ElementSize::TWO_BYTES, nullptr); ASSERT_EQ((i + 1) * ELEMENTS, element.size()); for (uint j = 0; j <= i; j++) { EXPECT_EQ(500u + j, element.getDataElement(j * ELEMENTS)); } } } } static void checkStruct(StructReader reader) { EXPECT_EQ(0x1011121314151617ull, reader.getDataField(0 * ELEMENTS)); EXPECT_EQ(0x20212223u, reader.getDataField(2 * ELEMENTS)); EXPECT_EQ(0x3031u, reader.getDataField(6 * ELEMENTS)); EXPECT_EQ(0x40u, reader.getDataField(14 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(120 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(121 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(122 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(123 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(124 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(125 * ELEMENTS)); EXPECT_TRUE (reader.getDataField(126 * ELEMENTS)); EXPECT_FALSE(reader.getDataField(127 * ELEMENTS)); { StructReader subStruct = reader.getPointerField(0 * POINTERS) .getStruct(SUBSTRUCT_DEFAULT.words); EXPECT_EQ(123u, subStruct.getDataField(0 * ELEMENTS)); } { ListReader list = reader.getPointerField(1 * POINTERS).getList(ElementSize::FOUR_BYTES, nullptr); ASSERT_EQ(3 * ELEMENTS, list.size()); EXPECT_EQ(200, list.getDataElement(0 * ELEMENTS)); EXPECT_EQ(201, list.getDataElement(1 * ELEMENTS)); EXPECT_EQ(202, list.getDataElement(2 * ELEMENTS)); } { ListReader list = reader.getPointerField(2 * POINTERS) .getList(ElementSize::INLINE_COMPOSITE, nullptr); ASSERT_EQ(4 * ELEMENTS, list.size()); for (int i = 0; i < 4; i++) { StructReader element = list.getStructElement(i * ELEMENTS); EXPECT_EQ(300 + i, element.getDataField(0 * ELEMENTS)); EXPECT_EQ(400 + i, element.getPointerField(0 * POINTERS) .getStruct(STRUCTLIST_ELEMENT_SUBSTRUCT_DEFAULT.words) .getDataField(0 * ELEMENTS)); } } { ListReader list = reader.getPointerField(3 * POINTERS).getList(ElementSize::POINTER, nullptr); ASSERT_EQ(5 * ELEMENTS, list.size()); for (uint i = 0; i < 5; i++) { ListReader element = list.getPointerElement(i * ELEMENTS) .getList(ElementSize::TWO_BYTES, nullptr); ASSERT_EQ((i + 1) * ELEMENTS, element.size()); for (uint j = 0; j <= i; j++) { EXPECT_EQ(500u + j, element.getDataElement(j * ELEMENTS)); } } } } TEST(WireFormat, StructRoundTrip_OneSegment) { MallocMessageBuilder message; BuilderArena arena(&message); auto allocation = arena.allocate(1 * WORDS); SegmentBuilder* segment = allocation.segment; word* rootLocation = allocation.words; StructBuilder builder = PointerBuilder::getRoot(segment, nullptr, rootLocation) .initStruct(StructSize(2 * WORDS, 4 * POINTERS)); setupStruct(builder); // word count: // 1 root pointer // 6 root struct // 1 sub message // 2 3-element int32 list // 13 struct list // 1 tag // 12 4x struct // 1 data section // 1 pointer section // 1 sub-struct // 11 list list // 5 pointers to sub-lists // 6 sub-lists (4x 1 word, 1x 2 words) // ----- // 34 kj::ArrayPtr> segments = arena.getSegmentsForOutput(); ASSERT_EQ(1u, segments.size()); EXPECT_EQ(34u, segments[0].size()); checkStruct(builder); checkStruct(builder.asReader()); checkStruct(PointerReader::getRootUnchecked(segment->getStartPtr()).getStruct(nullptr)); checkStruct(PointerReader::getRoot(segment, nullptr, segment->getStartPtr(), 4) .getStruct(nullptr)); } TEST(WireFormat, StructRoundTrip_OneSegmentPerAllocation) { MallocMessageBuilder message(0, AllocationStrategy::FIXED_SIZE); BuilderArena arena(&message); auto allocation = arena.allocate(1 * WORDS); SegmentBuilder* segment = allocation.segment; word* rootLocation = allocation.words; StructBuilder builder = PointerBuilder::getRoot(segment, nullptr, rootLocation) .initStruct(StructSize(2 * WORDS, 4 * POINTERS)); setupStruct(builder); // Verify that we made 15 segments. kj::ArrayPtr> segments = arena.getSegmentsForOutput(); ASSERT_EQ(15u, segments.size()); // Check that each segment has the expected size. Recall that the first word of each segment will // actually be a pointer to the first thing allocated within that segment. EXPECT_EQ( 1u, segments[ 0].size()); // root ref EXPECT_EQ( 7u, segments[ 1].size()); // root struct EXPECT_EQ( 2u, segments[ 2].size()); // sub-struct EXPECT_EQ( 3u, segments[ 3].size()); // 3-element int32 list EXPECT_EQ(10u, segments[ 4].size()); // struct list EXPECT_EQ( 2u, segments[ 5].size()); // struct list substruct 1 EXPECT_EQ( 2u, segments[ 6].size()); // struct list substruct 2 EXPECT_EQ( 2u, segments[ 7].size()); // struct list substruct 3 EXPECT_EQ( 2u, segments[ 8].size()); // struct list substruct 4 EXPECT_EQ( 6u, segments[ 9].size()); // list list EXPECT_EQ( 2u, segments[10].size()); // list list sublist 1 EXPECT_EQ( 2u, segments[11].size()); // list list sublist 2 EXPECT_EQ( 2u, segments[12].size()); // list list sublist 3 EXPECT_EQ( 2u, segments[13].size()); // list list sublist 4 EXPECT_EQ( 3u, segments[14].size()); // list list sublist 5 checkStruct(builder); checkStruct(builder.asReader()); checkStruct(PointerReader::getRoot(segment, nullptr, segment->getStartPtr(), 4) .getStruct(nullptr)); } TEST(WireFormat, StructRoundTrip_MultipleSegmentsWithMultipleAllocations) { MallocMessageBuilder message(8, AllocationStrategy::FIXED_SIZE); BuilderArena arena(&message); auto allocation = arena.allocate(1 * WORDS); SegmentBuilder* segment = allocation.segment; word* rootLocation = allocation.words; StructBuilder builder = PointerBuilder::getRoot(segment, nullptr, rootLocation) .initStruct(StructSize(2 * WORDS, 4 * POINTERS)); setupStruct(builder); // Verify that we made 6 segments. kj::ArrayPtr> segments = arena.getSegmentsForOutput(); ASSERT_EQ(6u, segments.size()); // Check that each segment has the expected size. Recall that each object will be prefixed by an // extra word if its parent is in a different segment. EXPECT_EQ( 8u, segments[0].size()); // root ref + struct + sub EXPECT_EQ( 3u, segments[1].size()); // 3-element int32 list EXPECT_EQ(10u, segments[2].size()); // struct list EXPECT_EQ( 8u, segments[3].size()); // struct list substructs EXPECT_EQ( 8u, segments[4].size()); // list list + sublist 1,2 EXPECT_EQ( 7u, segments[5].size()); // list list sublist 3,4,5 checkStruct(builder); checkStruct(builder.asReader()); checkStruct(PointerReader::getRoot(segment, nullptr, segment->getStartPtr(), 4) .getStruct(nullptr)); } inline bool isNan(float f) { return f != f; } inline bool isNan(double f) { return f != f; } TEST(WireFormat, NanPatching) { EXPECT_EQ(0x7fc00000u, mask(kj::nan(), 0)); EXPECT_TRUE(isNan(unmask(0x7fc00000u, 0))); EXPECT_TRUE(isNan(unmask(0x7fc00001u, 0))); EXPECT_TRUE(isNan(unmask(0x7fc00005u, 0))); EXPECT_EQ(0x7fc00000u, mask(unmask(0x7fc00000u, 0), 0)); EXPECT_EQ(0x7ff8000000000000ull, mask((double)kj::nan(), 0)); EXPECT_TRUE(isNan(unmask(0x7ff8000000000000ull, 0))); EXPECT_TRUE(isNan(unmask(0x7ff8000000000001ull, 0))); EXPECT_TRUE(isNan(unmask(0x7ff8000000000005ull, 0))); EXPECT_EQ(0x7ff8000000000000ull, mask(unmask(0x7ff8000000000000ull, 0), 0)); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/any-test.c++0000644000175000017500000003657613340402540020712 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "any.h" #include "message.h" #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { TEST(Any, AnyPointer) { MallocMessageBuilder builder; auto root = builder.getRoot(); initTestMessage(root.getAnyPointerField().initAs()); checkTestMessage(root.getAnyPointerField().getAs()); checkTestMessage(root.asReader().getAnyPointerField().getAs()); root.getAnyPointerField().setAs("foo"); EXPECT_EQ("foo", root.getAnyPointerField().getAs()); EXPECT_EQ("foo", root.asReader().getAnyPointerField().getAs()); root.getAnyPointerField().setAs(data("foo")); EXPECT_EQ(data("foo"), root.getAnyPointerField().getAs()); EXPECT_EQ(data("foo"), root.asReader().getAnyPointerField().getAs()); { root.getAnyPointerField().setAs>({123, 456, 789}); { List::Builder list = root.getAnyPointerField().getAs>(); ASSERT_EQ(3u, list.size()); EXPECT_EQ(123u, list[0]); EXPECT_EQ(456u, list[1]); EXPECT_EQ(789u, list[2]); } { List::Reader list = root.asReader().getAnyPointerField().getAs>(); ASSERT_EQ(3u, list.size()); EXPECT_EQ(123u, list[0]); EXPECT_EQ(456u, list[1]); EXPECT_EQ(789u, list[2]); } } { root.getAnyPointerField().setAs>({"foo", "bar"}); { List::Builder list = root.getAnyPointerField().getAs>(); ASSERT_EQ(2u, list.size()); EXPECT_EQ("foo", list[0]); EXPECT_EQ("bar", list[1]); } { List::Reader list = root.asReader().getAnyPointerField().getAs>(); ASSERT_EQ(2u, list.size()); EXPECT_EQ("foo", list[0]); EXPECT_EQ("bar", list[1]); } } { { List::Builder list = root.getAnyPointerField().initAs>(2); ASSERT_EQ(2u, list.size()); initTestMessage(list[0]); } { List::Builder list = root.getAnyPointerField().getAs>(); ASSERT_EQ(2u, list.size()); checkTestMessage(list[0]); checkTestMessageAllZero(list[1]); } { List::Reader list = root.asReader().getAnyPointerField().getAs>(); ASSERT_EQ(2u, list.size()); checkTestMessage(list[0]); checkTestMessageAllZero(list[1]); } } } TEST(Any, AnyStruct) { MallocMessageBuilder builder; auto root = builder.getRoot(); initTestMessage(root.getAnyPointerField().initAs()); checkTestMessage(root.getAnyPointerField().getAs()); checkTestMessage(root.asReader().getAnyPointerField().getAs()); auto allTypes = root.getAnyPointerField().getAs().as(); auto allTypesReader = root.getAnyPointerField().getAs().asReader().as(); allTypes.setInt32Field(100); EXPECT_EQ(100, allTypes.getInt32Field()); EXPECT_EQ(100, allTypesReader.getInt32Field()); EXPECT_EQ(48, root.getAnyPointerField().getAs().getDataSection().size()); EXPECT_EQ(20, root.getAnyPointerField().getAs().getPointerSection().size()); EXPECT_EQ(48, root.getAnyPointerField().asReader().getAs().getDataSection().size()); EXPECT_EQ(20, root.getAnyPointerField().asReader().getAs().getPointerSection().size()); auto b = toAny(root.getAnyPointerField().getAs()); EXPECT_EQ(48, b.getDataSection().size()); EXPECT_EQ(20, b.getPointerSection().size()); #if !_MSC_VER // TODO(msvc): ICE on the necessary constructor; see any.h. b = root.getAnyPointerField().getAs(); EXPECT_EQ(48, b.getDataSection().size()); EXPECT_EQ(20, b.getPointerSection().size()); #endif auto r = toAny(root.getAnyPointerField().getAs().asReader()); EXPECT_EQ(48, r.getDataSection().size()); EXPECT_EQ(20, r.getPointerSection().size()); r = toAny(root.getAnyPointerField().getAs()).asReader(); EXPECT_EQ(48, r.getDataSection().size()); EXPECT_EQ(20, r.getPointerSection().size()); #if !_MSC_VER // TODO(msvc): ICE on the necessary constructor; see any.h. r = root.getAnyPointerField().getAs().asReader(); EXPECT_EQ(48, r.getDataSection().size()); EXPECT_EQ(20, r.getPointerSection().size()); #endif { MallocMessageBuilder b2; auto root2 = b2.getRoot(); auto sb = root2.getAnyPointerField().initAsAnyStruct( r.getDataSection().size() / 8, r.getPointerSection().size()); EXPECT_EQ(48, sb.getDataSection().size()); EXPECT_EQ(20, sb.getPointerSection().size()); // TODO: is there a higher-level API for this? memcpy(sb.getDataSection().begin(), r.getDataSection().begin(), r.getDataSection().size()); } { auto ptrs = r.getPointerSection(); EXPECT_EQ("foo", ptrs[0].getAs()); EXPECT_EQ("bar", kj::heapString(ptrs[1].getAs().asChars())); EXPECT_EQ("xyzzy", ptrs[15].getAs>()[1]); } { auto ptrs = b.getPointerSection(); EXPECT_EQ("foo", ptrs[0].getAs()); EXPECT_EQ("bar", kj::heapString(ptrs[1].getAs().asChars())); EXPECT_EQ("xyzzy", ptrs[15].getAs>()[1]); } } TEST(Any, AnyList) { MallocMessageBuilder builder; auto root = builder.getRoot(); List::Builder b = root.getAnyPointerField().initAs>(2); initTestMessage(b[0]); auto ptr = root.getAnyPointerField().getAs(); EXPECT_EQ(2, ptr.size()); EXPECT_EQ(48, ptr.as>()[0].getDataSection().size()); EXPECT_EQ(20, ptr.as>()[0].getPointerSection().size()); auto readPtr = root.getAnyPointerField().asReader().getAs(); EXPECT_EQ(2, readPtr.size()); EXPECT_EQ(48, readPtr.as>()[0].getDataSection().size()); EXPECT_EQ(20, readPtr.as>()[0].getPointerSection().size()); auto alb = toAny(root.getAnyPointerField().getAs>()); EXPECT_EQ(2, alb.size()); EXPECT_EQ(48, alb.as>()[0].getDataSection().size()); EXPECT_EQ(20, alb.as>()[0].getPointerSection().size()); #if !_MSC_VER // TODO(msvc): ICE on the necessary constructor; see any.h. alb = root.getAnyPointerField().getAs>(); EXPECT_EQ(2, alb.size()); EXPECT_EQ(48, alb.as>()[0].getDataSection().size()); EXPECT_EQ(20, alb.as>()[0].getPointerSection().size()); #endif auto alr = toAny(root.getAnyPointerField().getAs>().asReader()); EXPECT_EQ(2, alr.size()); EXPECT_EQ(48, alr.as>()[0].getDataSection().size()); EXPECT_EQ(20, alr.as>()[0].getPointerSection().size()); alr = toAny(root.getAnyPointerField().getAs>()).asReader(); EXPECT_EQ(2, alr.size()); EXPECT_EQ(48, alr.as>()[0].getDataSection().size()); EXPECT_EQ(20, alr.as>()[0].getPointerSection().size()); #if !_MSC_VER // TODO(msvc): ICE on the necessary constructor; see any.h. alr = root.getAnyPointerField().getAs>().asReader(); EXPECT_EQ(2, alr.size()); EXPECT_EQ(48, alr.as>()[0].getDataSection().size()); EXPECT_EQ(20, alr.as>()[0].getPointerSection().size()); #endif } TEST(Any, AnyStructListCapInSchema) { MallocMessageBuilder builder; auto root = builder.getRoot(); { initTestMessage(root.initAnyStructFieldAs()); AnyStruct::Builder anyStruct = root.getAnyStructField(); checkTestMessage(anyStruct.as()); checkTestMessage(anyStruct.asReader().as()); EXPECT_TRUE(root.hasAnyStructField()); auto orphan = root.disownAnyStructField(); checkTestMessage(orphan.getReader().as()); EXPECT_FALSE(root.hasAnyStructField()); root.adoptAnyStructField(kj::mv(orphan)); EXPECT_TRUE(root.hasAnyStructField()); checkTestMessage(root.getAnyStructField().as()); } { List::Builder list = root.initAnyListFieldAs>(3); list.set(0, 123); list.set(1, 456); list.set(2, 789); AnyList::Builder anyList = root.getAnyListField(); checkList(anyList.as>(), {123, 456, 789}); EXPECT_TRUE(root.hasAnyListField()); auto orphan = root.disownAnyListField(); checkList(orphan.getReader().as>(), {123, 456, 789}); EXPECT_FALSE(root.hasAnyListField()); root.adoptAnyListField(kj::mv(orphan)); EXPECT_TRUE(root.hasAnyListField()); checkList(root.getAnyListField().as>(), {123, 456, 789}); } #if !CAPNP_LITE // This portion of the test relies on a Client, not present in lite-mode. { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; root.setCapabilityField(kj::heap(callCount)); Capability::Client client = root.getCapabilityField(); auto req = client.castAs().fooRequest(); req.setI(123); req.setJ(true); req.send().wait(waitScope); EXPECT_EQ(1, callCount); } #endif } KJ_TEST("Builder::isStruct() does not corrupt segment pointer") { MallocMessageBuilder builder(1); // small first segment auto root = builder.getRoot(); // Do a lot of allocations so that there is likely a segment with a decent // amount of free space. initTestMessage(root.initAs()); // This will probably get allocated in a segment that still has room for the // Data allocation below. root.initAs(); // At one point, this caused root.builder.segment to point to the segment // where the struct is allocated, rather than segment where the root pointer // lives, i.e. segment zero. EXPECT_TRUE(root.isStruct()); // If root.builder.segment points to the wrong segment and that segment has free // space, then this triggers a DREQUIRE failure in WirePointer::setKindAndTarget(). root.initAs(1); } TEST(Any, Equals) { MallocMessageBuilder builderA; auto rootA = builderA.getRoot(); auto anyA = builderA.getRoot(); initTestMessage(rootA); MallocMessageBuilder builderB; auto rootB = builderB.getRoot(); auto anyB = builderB.getRoot(); initTestMessage(rootB); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); rootA.setBoolField(false); EXPECT_EQ(Equality::NOT_EQUAL, anyA.equals(anyB)); rootB.setBoolField(false); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); rootB.setEnumField(test::TestEnum::GARPLY); EXPECT_EQ(Equality::NOT_EQUAL, anyA.equals(anyB)); rootA.setEnumField(test::TestEnum::GARPLY); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); rootA.getStructField().setTextField("buzz"); EXPECT_EQ(Equality::NOT_EQUAL, anyA.equals(anyB)); rootB.getStructField().setTextField("buzz"); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); rootA.initVoidList(3); EXPECT_EQ(Equality::NOT_EQUAL, anyA.equals(anyB)); rootB.initVoidList(3); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); rootA.getBoolList().set(2, true); EXPECT_EQ(Equality::NOT_EQUAL, anyA.equals(anyB)); rootB.getBoolList().set(2, true); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); rootB.getStructList()[1].setTextField("my NEW structlist 2"); EXPECT_EQ(Equality::NOT_EQUAL, anyA.equals(anyB)); rootA.getStructList()[1].setTextField("my NEW structlist 2"); EXPECT_EQ(Equality::EQUAL, anyA.equals(anyB)); } KJ_TEST("Bit list with nonzero pad bits") { AlignedData<2> segment1 = {{ 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, // eleven bit-sized elements 0xee, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // twelfth bit is set! }}; kj::ArrayPtr segments1[1] = { kj::arrayPtr(segment1.words, 2) }; SegmentArrayMessageReader message1(kj::arrayPtr(segments1, 1)); AlignedData<2> segment2 = {{ 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, // eleven bit-sized elements 0xee, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // twelfth bit is not set }}; kj::ArrayPtr segments2[1] = { kj::arrayPtr(segment2.words, 2) }; SegmentArrayMessageReader message2(kj::arrayPtr(segments2, 1)); // Should be equal, despite nonzero padding. KJ_ASSERT(message1.getRoot() == message2.getRoot()); } KJ_TEST("Pointer list unequal to struct list") { AlignedData<1> segment1 = {{ // list with zero pointer-sized elements 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments1[1] = { kj::arrayPtr(segment1.words, 1) }; SegmentArrayMessageReader message1(kj::arrayPtr(segments1, 1)); AlignedData<2> segment2 = {{ // struct list of length zero 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, // struct list tag, zero elements 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments2[1] = { kj::arrayPtr(segment2.words, 2) }; SegmentArrayMessageReader message2(kj::arrayPtr(segments2, 1)); EXPECT_EQ(Equality::NOT_EQUAL, message1.getRoot().equals(message2.getRoot())); } KJ_TEST("Truncating non-null pointer fields does not preserve equality") { AlignedData<3> segment1 = {{ // list with one data word and one pointer field 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, // data word 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, // non-null pointer to zero-sized struct 0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments1[1] = { kj::arrayPtr(segment1.words, 3) }; SegmentArrayMessageReader message1(kj::arrayPtr(segments1, 1)); AlignedData<2> segment2 = {{ // list with one data word and zero pointers 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // data word 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, }}; kj::ArrayPtr segments2[1] = { kj::arrayPtr(segment2.words, 2) }; SegmentArrayMessageReader message2(kj::arrayPtr(segments2, 1)); EXPECT_EQ(Equality::NOT_EQUAL, message1.getRoot().equals(message2.getRoot())); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/message-test.c++0000644000175000017500000001600513650101756021541 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "message.h" #include "test-util.h" #include #include #include #include namespace capnp { namespace _ { // private namespace { TEST(Message, MallocBuilderWithFirstSegment) { word scratch[16]; memset(scratch, 0, sizeof(scratch)); MallocMessageBuilder builder(kj::arrayPtr(scratch, 16), AllocationStrategy::FIXED_SIZE); kj::ArrayPtr segment = builder.allocateSegment(1); EXPECT_EQ(scratch, segment.begin()); EXPECT_EQ(16u, segment.size()); segment = builder.allocateSegment(1); EXPECT_NE(scratch, segment.begin()); EXPECT_EQ(16u, segment.size()); segment = builder.allocateSegment(1); EXPECT_NE(scratch, segment.begin()); EXPECT_EQ(16u, segment.size()); } class TestInitMessageBuilder: public MessageBuilder { public: TestInitMessageBuilder(kj::ArrayPtr segments): MessageBuilder(segments) {} kj::ArrayPtr allocateSegment(uint minimumSize) override { auto array = kj::heapArray(minimumSize); memset(array.begin(), 0, array.asBytes().size()); allocations.add(kj::mv(array)); return allocations.back(); } kj::Vector> allocations; }; TEST(Message, MessageBuilderInit) { MallocMessageBuilder builder(2048); initTestMessage(builder.getRoot()); // Pull the segments out and make a segment init table out of them. // // We const_cast for simplicity of implementing the test, but you shouldn't do that at home. :) auto segs = builder.getSegmentsForOutput(); ASSERT_EQ(1, segs.size()); auto segInits = KJ_MAP(seg, segs) -> MessageBuilder::SegmentInit { return { kj::arrayPtr(const_cast(seg.begin()), seg.size()), seg.size() }; }; // Init a new builder from the old segments. TestInitMessageBuilder builder2(segInits); checkTestMessage(builder2.getRoot()); // Verify that they're really using the same underlying memory. builder2.getRoot().setInt64Field(123321); EXPECT_EQ(123321, builder.getRoot().getInt64Field()); // Force builder2 to allocate new space. EXPECT_EQ(0, builder2.allocations.size()); builder2.getRoot().setTextField("foobarbaz"); EXPECT_EQ(1, builder2.allocations.size()); } TEST(Message, MessageBuilderInitMultiSegment) { // Same as previous test, but with a message containing many segments. MallocMessageBuilder builder(1, AllocationStrategy::FIXED_SIZE); initTestMessage(builder.getRoot()); // Pull the segments out and make a segment init table out of them. // // We const_cast for simplicity of implementing the test, but you shouldn't do that at home. :) auto segs = builder.getSegmentsForOutput(); ASSERT_NE(1, segs.size()); auto segInits = KJ_MAP(seg, segs) -> MessageBuilder::SegmentInit { return { kj::arrayPtr(const_cast(seg.begin()), seg.size()), seg.size() }; }; // Init a new builder from the old segments. TestInitMessageBuilder builder2(segInits); checkTestMessage(builder2.getRoot()); // Verify that they're really using the same underlying memory. builder2.getRoot().setInt64Field(123321); EXPECT_EQ(123321, builder.getRoot().getInt64Field()); // Force builder2 to allocate new space. EXPECT_EQ(0, builder2.allocations.size()); builder2.getRoot().setTextField("foobarbaz"); EXPECT_EQ(1, builder2.allocations.size()); } TEST(Message, MessageBuilderInitSpaceAvailable) { word buffer[2048]; memset(buffer, 0, sizeof(buffer)); MallocMessageBuilder builder(buffer); initTestMessage(builder.getRoot()); // Find out how much space in `buffer` was used in order to use in initializing the new message. auto segs = builder.getSegmentsForOutput(); ASSERT_EQ(1, segs.size()); KJ_ASSERT(segs[0].begin() == buffer); MessageBuilder::SegmentInit init = { kj::ArrayPtr(buffer), segs[0].size() }; // Init a new builder from the old segments. TestInitMessageBuilder builder2(kj::arrayPtr(&init, 1)); checkTestMessage(builder2.getRoot()); // Verify that they're really using the same underlying memory. builder2.getRoot().setInt64Field(123321); EXPECT_EQ(123321, builder.getRoot().getInt64Field()); // Ask builder2 to allocate new space. It should go into the free space at the end of the // segment. EXPECT_EQ(0, builder2.allocations.size()); builder2.getRoot().setTextField("foobarbaz"); EXPECT_EQ(0, builder2.allocations.size()); EXPECT_EQ(kj::implicitCast(buffer + segs[0].size()), kj::implicitCast(builder2.getRoot().getTextField().begin())); } TEST(Message, ReadWriteDataStruct) { MallocMessageBuilder builder; auto root = builder.getRoot(); root.setUInt32Field(123); root.setFloat64Field(1.5); root.setTextField("foo"); auto copy = readDataStruct(writeDataStruct(root)); EXPECT_EQ(123, copy.getUInt32Field()); EXPECT_EQ(1.5, copy.getFloat64Field()); EXPECT_FALSE(copy.hasTextField()); checkTestMessageAllZero(readDataStruct(nullptr)); checkTestMessageAllZero(defaultValue()); } KJ_TEST("clone()") { MallocMessageBuilder builder(2048); initTestMessage(builder.getRoot()); auto copy = clone(builder.getRoot().asReader()); checkTestMessage(*copy); } #if !CAPNP_ALLOW_UNALIGNED KJ_TEST("disallow unaligned") { union { char buffer[16]; word align; }; memset(buffer, 0, sizeof(buffer)); auto unaligned = kj::arrayPtr(reinterpret_cast(buffer + 1), 1); kj::ArrayPtr segments[1] = {unaligned}; SegmentArrayMessageReader message(segments); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("unaligned", message.getRoot()); } #endif // TODO(test): More tests. } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/encoding-test.c++0000644000175000017500000021710013340402540021671 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include #include #include "message.h" #include #include #include "test-util.h" #include "schema-lite.h" #include "serialize-packed.h" namespace capnp { namespace _ { // private namespace { TEST(Encoding, AllTypes) { MallocMessageBuilder builder; initTestMessage(builder.initRoot()); checkTestMessage(builder.getRoot()); checkTestMessage(builder.getRoot().asReader()); SegmentArrayMessageReader reader(builder.getSegmentsForOutput()); checkTestMessage(reader.getRoot()); ASSERT_EQ(1u, builder.getSegmentsForOutput().size()); checkTestMessage(readMessageUnchecked(builder.getSegmentsForOutput()[0].begin())); EXPECT_EQ(builder.getSegmentsForOutput()[0].size() - 1, // -1 for root pointer reader.getRoot().totalSize().wordCount); } TEST(Encoding, AllTypesMultiSegment) { MallocMessageBuilder builder(0, AllocationStrategy::FIXED_SIZE); initTestMessage(builder.initRoot()); checkTestMessage(builder.getRoot()); checkTestMessage(builder.getRoot().asReader()); SegmentArrayMessageReader reader(builder.getSegmentsForOutput()); checkTestMessage(reader.getRoot()); } TEST(Encoding, Defaults) { AlignedData<1> nullRoot = {{0, 0, 0, 0, 0, 0, 0, 0}}; kj::ArrayPtr segments[1] = {kj::arrayPtr(nullRoot.words, 1)}; SegmentArrayMessageReader reader(kj::arrayPtr(segments, 1)); checkTestMessage(reader.getRoot()); checkTestMessage(readMessageUnchecked(nullRoot.words)); checkTestMessage(TestDefaults::Reader()); } TEST(Encoding, DefaultInitialization) { MallocMessageBuilder builder; checkTestMessage(builder.getRoot()); // first pass initializes to defaults checkTestMessage(builder.getRoot().asReader()); checkTestMessage(builder.getRoot()); // second pass just reads the initialized structure checkTestMessage(builder.getRoot().asReader()); SegmentArrayMessageReader reader(builder.getSegmentsForOutput()); checkTestMessage(reader.getRoot()); } TEST(Encoding, DefaultInitializationMultiSegment) { MallocMessageBuilder builder(0, AllocationStrategy::FIXED_SIZE); // first pass initializes to defaults checkTestMessage(builder.getRoot()); checkTestMessage(builder.getRoot().asReader()); // second pass just reads the initialized structure checkTestMessage(builder.getRoot()); checkTestMessage(builder.getRoot().asReader()); SegmentArrayMessageReader reader(builder.getSegmentsForOutput()); checkTestMessage(reader.getRoot()); } TEST(Encoding, DefaultsFromEmptyMessage) { AlignedData<1> emptyMessage = {{0, 0, 0, 0, 0, 0, 0, 0}}; kj::ArrayPtr segments[1] = {kj::arrayPtr(emptyMessage.words, 1)}; SegmentArrayMessageReader reader(kj::arrayPtr(segments, 1)); checkTestMessage(reader.getRoot()); checkTestMessage(readMessageUnchecked(emptyMessage.words)); } TEST(Encoding, Unions) { MallocMessageBuilder builder; TestUnion::Builder root = builder.getRoot(); EXPECT_EQ(TestUnion::Union0::U0F0S0, root.getUnion0().which()); EXPECT_EQ(VOID, root.getUnion0().getU0f0s0()); EXPECT_DEBUG_ANY_THROW(root.getUnion0().getU0f0s1()); root.getUnion0().setU0f0s1(true); EXPECT_EQ(TestUnion::Union0::U0F0S1, root.getUnion0().which()); EXPECT_TRUE(root.getUnion0().getU0f0s1()); EXPECT_DEBUG_ANY_THROW(root.getUnion0().getU0f0s0()); root.getUnion0().setU0f0s8(123); EXPECT_EQ(TestUnion::Union0::U0F0S8, root.getUnion0().which()); EXPECT_EQ(123, root.getUnion0().getU0f0s8()); EXPECT_DEBUG_ANY_THROW(root.getUnion0().getU0f0s1()); } struct UnionState { uint discriminants[4]; int dataOffset; UnionState(std::initializer_list discriminants, int dataOffset) : dataOffset(dataOffset) { memcpy(this->discriminants, discriminants.begin(), sizeof(this->discriminants)); } bool operator==(const UnionState& other) const { for (uint i = 0; i < 4; i++) { if (discriminants[i] != other.discriminants[i]) { return false; } } return dataOffset == other.dataOffset; } }; kj::String KJ_STRINGIFY(const UnionState& us) { return kj::str("UnionState({", kj::strArray(us.discriminants, ", "), "}, ", us.dataOffset, ")"); } template UnionState initUnion(Func&& initializer) { // Use the given setter to initialize the given union field and then return a struct indicating // the location of the data that was written as well as the values of the four union // discriminants. MallocMessageBuilder builder; initializer(builder.getRoot()); kj::ArrayPtr segment = builder.getSegmentsForOutput()[0]; KJ_ASSERT(segment.size() > 2, segment.size()); // Find the offset of the first set bit after the union discriminants. int offset = 0; for (const uint8_t* p = reinterpret_cast(segment.begin() + 2); p < reinterpret_cast(segment.end()); p++) { if (*p != 0) { uint8_t bits = *p; while ((bits & 1) == 0) { ++offset; bits >>= 1; } goto found; } offset += 8; } offset = -1; found: const uint8_t* discriminants = reinterpret_cast(segment.begin() + 1); return UnionState({discriminants[0], discriminants[2], discriminants[4], discriminants[6]}, offset); } TEST(Encoding, UnionLayout) { #define INIT_UNION(setter) \ initUnion([](TestUnion::Builder b) {b.setter;}) EXPECT_EQ(UnionState({ 0,0,0,0}, -1), INIT_UNION(getUnion0().setU0f0s0(VOID))); EXPECT_EQ(UnionState({ 1,0,0,0}, 0), INIT_UNION(getUnion0().setU0f0s1(1))); EXPECT_EQ(UnionState({ 2,0,0,0}, 0), INIT_UNION(getUnion0().setU0f0s8(1))); EXPECT_EQ(UnionState({ 3,0,0,0}, 0), INIT_UNION(getUnion0().setU0f0s16(1))); EXPECT_EQ(UnionState({ 4,0,0,0}, 0), INIT_UNION(getUnion0().setU0f0s32(1))); EXPECT_EQ(UnionState({ 5,0,0,0}, 0), INIT_UNION(getUnion0().setU0f0s64(1))); EXPECT_EQ(UnionState({ 6,0,0,0}, 448), INIT_UNION(getUnion0().setU0f0sp("1"))); EXPECT_EQ(UnionState({ 7,0,0,0}, -1), INIT_UNION(getUnion0().setU0f1s0(VOID))); EXPECT_EQ(UnionState({ 8,0,0,0}, 0), INIT_UNION(getUnion0().setU0f1s1(1))); EXPECT_EQ(UnionState({ 9,0,0,0}, 0), INIT_UNION(getUnion0().setU0f1s8(1))); EXPECT_EQ(UnionState({10,0,0,0}, 0), INIT_UNION(getUnion0().setU0f1s16(1))); EXPECT_EQ(UnionState({11,0,0,0}, 0), INIT_UNION(getUnion0().setU0f1s32(1))); EXPECT_EQ(UnionState({12,0,0,0}, 0), INIT_UNION(getUnion0().setU0f1s64(1))); EXPECT_EQ(UnionState({13,0,0,0}, 448), INIT_UNION(getUnion0().setU0f1sp("1"))); EXPECT_EQ(UnionState({0, 0,0,0}, -1), INIT_UNION(getUnion1().setU1f0s0(VOID))); EXPECT_EQ(UnionState({0, 1,0,0}, 65), INIT_UNION(getUnion1().setU1f0s1(1))); EXPECT_EQ(UnionState({0, 2,0,0}, 65), INIT_UNION(getUnion1().setU1f1s1(1))); EXPECT_EQ(UnionState({0, 3,0,0}, 72), INIT_UNION(getUnion1().setU1f0s8(1))); EXPECT_EQ(UnionState({0, 4,0,0}, 72), INIT_UNION(getUnion1().setU1f1s8(1))); EXPECT_EQ(UnionState({0, 5,0,0}, 80), INIT_UNION(getUnion1().setU1f0s16(1))); EXPECT_EQ(UnionState({0, 6,0,0}, 80), INIT_UNION(getUnion1().setU1f1s16(1))); EXPECT_EQ(UnionState({0, 7,0,0}, 96), INIT_UNION(getUnion1().setU1f0s32(1))); EXPECT_EQ(UnionState({0, 8,0,0}, 96), INIT_UNION(getUnion1().setU1f1s32(1))); EXPECT_EQ(UnionState({0, 9,0,0}, 128), INIT_UNION(getUnion1().setU1f0s64(1))); EXPECT_EQ(UnionState({0,10,0,0}, 128), INIT_UNION(getUnion1().setU1f1s64(1))); EXPECT_EQ(UnionState({0,11,0,0}, 512), INIT_UNION(getUnion1().setU1f0sp("1"))); EXPECT_EQ(UnionState({0,12,0,0}, 512), INIT_UNION(getUnion1().setU1f1sp("1"))); EXPECT_EQ(UnionState({0,13,0,0}, -1), INIT_UNION(getUnion1().setU1f2s0(VOID))); EXPECT_EQ(UnionState({0,14,0,0}, 65), INIT_UNION(getUnion1().setU1f2s1(1))); EXPECT_EQ(UnionState({0,15,0,0}, 72), INIT_UNION(getUnion1().setU1f2s8(1))); EXPECT_EQ(UnionState({0,16,0,0}, 80), INIT_UNION(getUnion1().setU1f2s16(1))); EXPECT_EQ(UnionState({0,17,0,0}, 96), INIT_UNION(getUnion1().setU1f2s32(1))); EXPECT_EQ(UnionState({0,18,0,0}, 128), INIT_UNION(getUnion1().setU1f2s64(1))); EXPECT_EQ(UnionState({0,19,0,0}, 512), INIT_UNION(getUnion1().setU1f2sp("1"))); EXPECT_EQ(UnionState({0,0,0,0}, 192), INIT_UNION(getUnion2().setU2f0s1(1))); EXPECT_EQ(UnionState({0,0,0,0}, 193), INIT_UNION(getUnion3().setU3f0s1(1))); EXPECT_EQ(UnionState({0,0,1,0}, 200), INIT_UNION(getUnion2().setU2f0s8(1))); EXPECT_EQ(UnionState({0,0,0,1}, 208), INIT_UNION(getUnion3().setU3f0s8(1))); EXPECT_EQ(UnionState({0,0,2,0}, 224), INIT_UNION(getUnion2().setU2f0s16(1))); EXPECT_EQ(UnionState({0,0,0,2}, 240), INIT_UNION(getUnion3().setU3f0s16(1))); EXPECT_EQ(UnionState({0,0,3,0}, 256), INIT_UNION(getUnion2().setU2f0s32(1))); EXPECT_EQ(UnionState({0,0,0,3}, 288), INIT_UNION(getUnion3().setU3f0s32(1))); EXPECT_EQ(UnionState({0,0,4,0}, 320), INIT_UNION(getUnion2().setU2f0s64(1))); EXPECT_EQ(UnionState({0,0,0,4}, 384), INIT_UNION(getUnion3().setU3f0s64(1))); #undef INIT_UNION } TEST(Encoding, UnnamedUnion) { MallocMessageBuilder builder; auto root = builder.initRoot(); EXPECT_EQ(test::TestUnnamedUnion::FOO, root.which()); root.setBar(321); EXPECT_EQ(test::TestUnnamedUnion::BAR, root.which()); EXPECT_EQ(test::TestUnnamedUnion::BAR, root.asReader().which()); EXPECT_EQ(321u, root.getBar()); EXPECT_EQ(321u, root.asReader().getBar()); EXPECT_DEBUG_ANY_THROW(root.getFoo()); EXPECT_DEBUG_ANY_THROW(root.asReader().getFoo()); root.setFoo(123); EXPECT_EQ(test::TestUnnamedUnion::FOO, root.which()); EXPECT_EQ(test::TestUnnamedUnion::FOO, root.asReader().which()); EXPECT_EQ(123u, root.getFoo()); EXPECT_EQ(123u, root.asReader().getFoo()); EXPECT_DEBUG_ANY_THROW(root.getBar()); EXPECT_DEBUG_ANY_THROW(root.asReader().getBar()); #if !CAPNP_LITE StructSchema schema = Schema::from(); // The discriminant is allocated just before allocating "bar". EXPECT_EQ(2u, schema.getProto().getStruct().getDiscriminantOffset()); EXPECT_EQ(0u, schema.getFieldByName("foo").getProto().getSlot().getOffset()); EXPECT_EQ(2u, schema.getFieldByName("bar").getProto().getSlot().getOffset()); #endif // !CAPNP_LITE } TEST(Encoding, Groups) { MallocMessageBuilder builder; auto root = builder.initRoot(); { auto foo = root.getGroups().initFoo(); foo.setCorge(12345678); foo.setGrault(123456789012345ll); foo.setGarply("foobar"); EXPECT_EQ(12345678, foo.getCorge()); EXPECT_EQ(123456789012345ll, foo.getGrault()); EXPECT_EQ("foobar", foo.getGarply()); } { auto bar = root.getGroups().initBar(); bar.setCorge(23456789); bar.setGrault("barbaz"); bar.setGarply(234567890123456ll); EXPECT_EQ(23456789, bar.getCorge()); EXPECT_EQ("barbaz", bar.getGrault()); EXPECT_EQ(234567890123456ll, bar.getGarply()); } { auto baz = root.getGroups().initBaz(); baz.setCorge(34567890); baz.setGrault("bazqux"); baz.setGarply("quxquux"); EXPECT_EQ(34567890, baz.getCorge()); EXPECT_EQ("bazqux", baz.getGrault()); EXPECT_EQ("quxquux", baz.getGarply()); } } TEST(Encoding, InterleavedGroups) { MallocMessageBuilder builder; auto root = builder.initRoot(); // Init both groups to different values. { auto group = root.getGroup1(); group.setFoo(12345678u); group.setBar(123456789012345llu); auto corge = group.initCorge(); corge.setGrault(987654321098765llu); corge.setGarply(12345u); corge.setPlugh("plugh"); corge.setXyzzy("xyzzy"); group.setWaldo("waldo"); } { auto group = root.getGroup2(); group.setFoo(23456789u); group.setBar(234567890123456llu); auto corge = group.initCorge(); corge.setGrault(876543210987654llu); corge.setGarply(23456u); corge.setPlugh("hgulp"); corge.setXyzzy("yzzyx"); group.setWaldo("odlaw"); } // Check group1 is still set correctly. { auto group = root.asReader().getGroup1(); EXPECT_EQ(12345678u, group.getFoo()); EXPECT_EQ(123456789012345llu, group.getBar()); auto corge = group.getCorge(); EXPECT_EQ(987654321098765llu, corge.getGrault()); EXPECT_EQ(12345u, corge.getGarply()); EXPECT_EQ("plugh", corge.getPlugh()); EXPECT_EQ("xyzzy", corge.getXyzzy()); EXPECT_EQ("waldo", group.getWaldo()); } // Zero out group 1 and see if it is zero'd. { auto group = root.initGroup1().asReader(); EXPECT_EQ(0u, group.getFoo()); EXPECT_EQ(0u, group.getBar()); EXPECT_EQ(test::TestInterleavedGroups::Group1::QUX, group.which()); EXPECT_EQ(0u, group.getQux()); EXPECT_FALSE(group.hasWaldo()); } // Group 2 should not have been touched. { auto group = root.asReader().getGroup2(); EXPECT_EQ(23456789u, group.getFoo()); EXPECT_EQ(234567890123456llu, group.getBar()); auto corge = group.getCorge(); EXPECT_EQ(876543210987654llu, corge.getGrault()); EXPECT_EQ(23456u, corge.getGarply()); EXPECT_EQ("hgulp", corge.getPlugh()); EXPECT_EQ("yzzyx", corge.getXyzzy()); EXPECT_EQ("odlaw", group.getWaldo()); } } TEST(Encoding, UnionDefault) { MallocMessageBuilder builder; TestUnionDefaults::Reader reader = builder.getRoot().asReader(); { auto field = reader.getS16s8s64s8Set(); EXPECT_EQ(TestUnion::Union0::U0F0S16, field.getUnion0().which()); EXPECT_EQ(TestUnion::Union1::U1F0S8 , field.getUnion1().which()); EXPECT_EQ(TestUnion::Union2::U2F0S64, field.getUnion2().which()); EXPECT_EQ(TestUnion::Union3::U3F0S8 , field.getUnion3().which()); EXPECT_EQ(321, field.getUnion0().getU0f0s16()); EXPECT_EQ(123, field.getUnion1().getU1f0s8()); EXPECT_EQ(12345678901234567ll, field.getUnion2().getU2f0s64()); EXPECT_EQ(55, field.getUnion3().getU3f0s8()); } { auto field = reader.getS0sps1s32Set(); EXPECT_EQ(TestUnion::Union0::U0F1S0 , field.getUnion0().which()); EXPECT_EQ(TestUnion::Union1::U1F0SP , field.getUnion1().which()); EXPECT_EQ(TestUnion::Union2::U2F0S1 , field.getUnion2().which()); EXPECT_EQ(TestUnion::Union3::U3F0S32, field.getUnion3().which()); EXPECT_EQ(VOID, field.getUnion0().getU0f1s0()); EXPECT_EQ("foo", field.getUnion1().getU1f0sp()); EXPECT_EQ(true, field.getUnion2().getU2f0s1()); EXPECT_EQ(12345678, field.getUnion3().getU3f0s32()); } { auto field = reader.getUnnamed1(); EXPECT_EQ(test::TestUnnamedUnion::FOO, field.which()); EXPECT_EQ(123u, field.getFoo()); EXPECT_FALSE(field.hasBefore()); EXPECT_FALSE(field.hasAfter()); } { auto field = reader.getUnnamed2(); EXPECT_EQ(test::TestUnnamedUnion::BAR, field.which()); EXPECT_EQ(321u, field.getBar()); EXPECT_EQ("foo", field.getBefore()); EXPECT_EQ("bar", field.getAfter()); } } // ======================================================================================= TEST(Encoding, ListDefaults) { MallocMessageBuilder builder; TestListDefaults::Builder root = builder.getRoot(); checkTestMessage(root.asReader()); checkTestMessage(root); checkTestMessage(root.asReader()); } TEST(Encoding, BuildListDefaults) { MallocMessageBuilder builder; TestListDefaults::Builder root = builder.getRoot(); initTestMessage(root); checkTestMessage(root.asReader()); checkTestMessage(root); checkTestMessage(root.asReader()); } TEST(Encoding, SmallStructLists) { // In this test, we will manually initialize TestListDefaults.lists to match the default // value and verify that we end up with the same encoding that the compiler produces. MallocMessageBuilder builder; auto root = builder.getRoot(); auto sl = root.initLists(); // Verify that all the lists are actually empty. EXPECT_EQ(0u, sl.getList0 ().size()); EXPECT_EQ(0u, sl.getList1 ().size()); EXPECT_EQ(0u, sl.getList8 ().size()); EXPECT_EQ(0u, sl.getList16().size()); EXPECT_EQ(0u, sl.getList32().size()); EXPECT_EQ(0u, sl.getList64().size()); EXPECT_EQ(0u, sl.getListP ().size()); EXPECT_EQ(0u, sl.getInt32ListList().size()); EXPECT_EQ(0u, sl.getTextListList().size()); EXPECT_EQ(0u, sl.getStructListList().size()); { auto l = sl.initList0 (2); l[0].setF(VOID); l[1].setF(VOID); } { auto l = sl.initList1 (4); l[0].setF(true); l[1].setF(false); l[2].setF(true); l[3].setF(true); } { auto l = sl.initList8 (2); l[0].setF(123u); l[1].setF(45u); } { auto l = sl.initList16(2); l[0].setF(12345u); l[1].setF(6789u); } { auto l = sl.initList32(2); l[0].setF(123456789u); l[1].setF(234567890u); } { auto l = sl.initList64(2); l[0].setF(1234567890123456u); l[1].setF(2345678901234567u); } { auto l = sl.initListP (2); l[0].setF("foo"); l[1].setF("bar"); } { auto l = sl.initInt32ListList(3); l.set(0, {1, 2, 3}); l.set(1, {4, 5}); l.set(2, {12341234}); } { auto l = sl.initTextListList(3); l.set(0, {"foo", "bar"}); l.set(1, {"baz"}); l.set(2, {"qux", "corge"}); } { auto l = sl.initStructListList(2); l.init(0, 2); l.init(1, 1); l[0][0].setInt32Field(123); l[0][1].setInt32Field(456); l[1][0].setInt32Field(789); } kj::ArrayPtr segment = builder.getSegmentsForOutput()[0]; // Initialize another message such that it copies the default value for that field. MallocMessageBuilder defaultBuilder; defaultBuilder.getRoot().getLists(); kj::ArrayPtr defaultSegment = defaultBuilder.getSegmentsForOutput()[0]; // Should match... EXPECT_EQ(defaultSegment.size(), segment.size()); for (size_t i = 0; i < kj::min(segment.size(), defaultSegment.size()); i++) { EXPECT_EQ(reinterpret_cast(defaultSegment.begin())[i], reinterpret_cast(segment.begin())[i]); } } TEST(Encoding, SetListToEmpty) { // Test initializing list fields from various ways of constructing zero-sized lists. // At one point this would often fail because the lists would have ElementSize::VOID which is // incompatible with other list sizes. #define ALL_LIST_TYPES(MACRO) \ MACRO(Void, Void) \ MACRO(Bool, bool) \ MACRO(UInt8, uint8_t) \ MACRO(UInt16, uint16_t) \ MACRO(UInt32, uint32_t) \ MACRO(UInt64, uint64_t) \ MACRO(Int8, int8_t) \ MACRO(Int16, int16_t) \ MACRO(Int32, int32_t) \ MACRO(Int64, int64_t) \ MACRO(Float32, float) \ MACRO(Float64, double) \ MACRO(Text, Text) \ MACRO(Data, Data) \ MACRO(Struct, TestAllTypes) #define SET_FROM_READER_ACCESSOR(name, type) \ root.set##name##List(reader.get##name##List()); #define SET_FROM_BUILDER_ACCESSOR(name, type) \ root.set##name##List(root.get##name##List()); #define SET_FROM_READER_CONSTRUCTOR(name, type) \ root.set##name##List(List::Reader()); #define SET_FROM_BUILDER_CONSTRUCTOR(name, type) \ root.set##name##List(List::Builder()); #define CHECK_EMPTY_NONNULL(name, type) \ EXPECT_TRUE(root.has##name##List()); \ EXPECT_EQ(0, root.get##name##List().size()); { MallocMessageBuilder builder; auto root = builder.initRoot(); auto reader = root.asReader(); ALL_LIST_TYPES(SET_FROM_READER_ACCESSOR) ALL_LIST_TYPES(CHECK_EMPTY_NONNULL) } { MallocMessageBuilder builder; auto root = builder.initRoot(); ALL_LIST_TYPES(SET_FROM_BUILDER_ACCESSOR) ALL_LIST_TYPES(CHECK_EMPTY_NONNULL) } { MallocMessageBuilder builder; auto root = builder.initRoot(); ALL_LIST_TYPES(SET_FROM_READER_CONSTRUCTOR) ALL_LIST_TYPES(CHECK_EMPTY_NONNULL) } { MallocMessageBuilder builder; auto root = builder.initRoot(); ALL_LIST_TYPES(SET_FROM_BUILDER_CONSTRUCTOR) ALL_LIST_TYPES(CHECK_EMPTY_NONNULL) } #undef SET_FROM_READER_ACCESSOR #undef SET_FROM_BUILDER_ACCESSOR #undef SET_FROM_READER_CONSTRUCTOR #undef SET_FROM_BUILDER_CONSTRUCTOR #undef CHECK_EMPTY_NONNULL } #if CAPNP_EXPENSIVE_TESTS TEST(Encoding, LongList) { // This test allocates 512MB of contiguous memory and takes several seconds, so we usually don't // run it. It is run before release, though. MallocMessageBuilder builder; auto root = builder.initRoot(); uint length = 1 << 27; auto list = root.initUInt64List(length); for (uint ii = 0; ii < length; ++ii) { list.set(ii, ii); } for (uint ii = 0; ii < length; ++ii) { ASSERT_EQ(list[ii], ii); } } #endif // ======================================================================================= TEST(Encoding, ListUpgrade) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getAnyPointerField().setAs>({12, 34, 56}); checkList(root.getAnyPointerField().getAs>(), {12, 34, 56}); { auto l = root.getAnyPointerField().getAs>(); ASSERT_EQ(3u, l.size()); EXPECT_EQ(12u, l[0].getF()); EXPECT_EQ(34u, l[1].getF()); EXPECT_EQ(56u, l[2].getF()); } checkList(root.getAnyPointerField().getAs>(), {12, 34, 56}); auto reader = root.asReader(); checkList(reader.getAnyPointerField().getAs>(), {12, 34, 56}); { auto l = reader.getAnyPointerField().getAs>(); ASSERT_EQ(3u, l.size()); EXPECT_EQ(12u, l[0].getF()); EXPECT_EQ(34u, l[1].getF()); EXPECT_EQ(56u, l[2].getF()); } root.getAnyPointerField().setAs>({12, 34, 56}); { kj::Maybe e = kj::runCatchingExceptions([&]() { reader.getAnyPointerField().getAs>(); #if !KJ_NO_EXCEPTIONS ADD_FAILURE() << "Should have thrown an exception."; #endif }); KJ_EXPECT(e != nullptr, "Should have thrown an exception."); } { auto l = reader.getAnyPointerField().getAs>(); ASSERT_EQ(3u, l.size()); // These should return default values because the structs aren't big enough. EXPECT_EQ(0u, l[0].getF()); EXPECT_EQ(0u, l[1].getF()); EXPECT_EQ(0u, l[2].getF()); } checkList(reader.getAnyPointerField().getAs>(), {12, 34, 56}); } TEST(Encoding, BitListDowngrade) { // NO LONGER SUPPORTED -- We check for exceptions thrown. MallocMessageBuilder builder; auto root = builder.initRoot(); root.getAnyPointerField().setAs>({0x1201u, 0x3400u, 0x5601u, 0x7801u}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); { auto l = root.getAnyPointerField().getAs>(); ASSERT_EQ(4u, l.size()); EXPECT_TRUE(l[0].getF()); EXPECT_FALSE(l[1].getF()); EXPECT_TRUE(l[2].getF()); EXPECT_TRUE(l[3].getF()); } checkList(root.getAnyPointerField().getAs>(), {0x1201u, 0x3400u, 0x5601u, 0x7801u}); auto reader = root.asReader(); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); { auto l = reader.getAnyPointerField().getAs>(); ASSERT_EQ(4u, l.size()); EXPECT_TRUE(l[0].getF()); EXPECT_FALSE(l[1].getF()); EXPECT_TRUE(l[2].getF()); EXPECT_TRUE(l[3].getF()); } checkList(reader.getAnyPointerField().getAs>(), {0x1201u, 0x3400u, 0x5601u, 0x7801u}); } TEST(Encoding, BitListDowngradeFromStruct) { MallocMessageBuilder builder; auto root = builder.initRoot(); { auto list = root.getAnyPointerField().initAs>(4); list[0].setF(true); list[1].setF(false); list[2].setF(true); list[3].setF(true); } EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); { auto l = root.getAnyPointerField().getAs>(); ASSERT_EQ(4u, l.size()); EXPECT_TRUE(l[0].getF()); EXPECT_FALSE(l[1].getF()); EXPECT_TRUE(l[2].getF()); EXPECT_TRUE(l[3].getF()); } auto reader = root.asReader(); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); { auto l = reader.getAnyPointerField().getAs>(); ASSERT_EQ(4u, l.size()); EXPECT_TRUE(l[0].getF()); EXPECT_FALSE(l[1].getF()); EXPECT_TRUE(l[2].getF()); EXPECT_TRUE(l[3].getF()); } } TEST(Encoding, BitListUpgrade) { // No longer supported! MallocMessageBuilder builder; auto root = builder.initRoot(); root.getAnyPointerField().setAs>({true, false, true, true}); { kj::Maybe e = kj::runCatchingExceptions([&]() { root.getAnyPointerField().getAs>(); #if !KJ_NO_EXCEPTIONS ADD_FAILURE() << "Should have thrown an exception."; #endif }); KJ_EXPECT(e != nullptr, "Should have thrown an exception."); } auto reader = root.asReader(); { kj::Maybe e = kj::runCatchingExceptions([&]() { reader.getAnyPointerField().getAs>(); #if !KJ_NO_EXCEPTIONS ADD_FAILURE() << "Should have thrown an exception."; #endif }); KJ_EXPECT(e != nullptr, "Should have thrown an exception."); } } TEST(Encoding, UpgradeStructInBuilder) { MallocMessageBuilder builder; auto root = builder.initRoot(); test::TestOldVersion::Reader oldReader; { auto oldVersion = root.getAnyPointerField().initAs(); oldVersion.setOld1(123); oldVersion.setOld2("foo"); auto sub = oldVersion.initOld3(); sub.setOld1(456); sub.setOld2("bar"); oldReader = oldVersion; } size_t size = builder.getSegmentsForOutput()[0].size(); size_t size2; { auto newVersion = root.getAnyPointerField().getAs(); // The old instance should have been zero'd. EXPECT_EQ(0, oldReader.getOld1()); EXPECT_EQ("", oldReader.getOld2()); EXPECT_EQ(0, oldReader.getOld3().getOld1()); EXPECT_EQ("", oldReader.getOld3().getOld2()); // Size should have increased due to re-allocating the struct. size_t size1 = builder.getSegmentsForOutput()[0].size(); EXPECT_GT(size1, size); auto sub = newVersion.getOld3(); // Size should have increased due to re-allocating the sub-struct. size2 = builder.getSegmentsForOutput()[0].size(); EXPECT_GT(size2, size1); // Check contents. EXPECT_EQ(123, newVersion.getOld1()); EXPECT_EQ("foo", newVersion.getOld2()); EXPECT_EQ(987, newVersion.getNew1()); EXPECT_EQ("baz", newVersion.getNew2()); EXPECT_EQ(456, sub.getOld1()); EXPECT_EQ("bar", sub.getOld2()); EXPECT_EQ(987, sub.getNew1()); EXPECT_EQ("baz", sub.getNew2()); newVersion.setOld1(234); newVersion.setOld2("qux"); newVersion.setNew1(321); newVersion.setNew2("quux"); sub.setOld1(567); sub.setOld2("corge"); sub.setNew1(654); sub.setNew2("grault"); } // We set four small text fields and implicitly initialized two to defaults, so the size should // have raised by six words. size_t size3 = builder.getSegmentsForOutput()[0].size(); EXPECT_EQ(size2 + 6, size3); { // Go back to old version. It should have the values set on the new version. auto oldVersion = root.getAnyPointerField().getAs(); EXPECT_EQ(234, oldVersion.getOld1()); EXPECT_EQ("qux", oldVersion.getOld2()); auto sub = oldVersion.getOld3(); EXPECT_EQ(567, sub.getOld1()); EXPECT_EQ("corge", sub.getOld2()); // Overwrite the old fields. The new fields should remain intact. oldVersion.setOld1(345); oldVersion.setOld2("garply"); sub.setOld1(678); sub.setOld2("waldo"); } // We set two small text fields, so the size should have raised by two words. size_t size4 = builder.getSegmentsForOutput()[0].size(); EXPECT_EQ(size3 + 2, size4); { // Back to the new version again. auto newVersion = root.getAnyPointerField().getAs(); EXPECT_EQ(345, newVersion.getOld1()); EXPECT_EQ("garply", newVersion.getOld2()); EXPECT_EQ(321, newVersion.getNew1()); EXPECT_EQ("quux", newVersion.getNew2()); auto sub = newVersion.getOld3(); EXPECT_EQ(678, sub.getOld1()); EXPECT_EQ("waldo", sub.getOld2()); EXPECT_EQ(654, sub.getNew1()); EXPECT_EQ("grault", sub.getNew2()); } // Size should not have changed because we didn't write anything and the structs were already // the right size. EXPECT_EQ(size4, builder.getSegmentsForOutput()[0].size()); } TEST(Encoding, UpgradeStructInBuilderMultiSegment) { // Exactly like the previous test, except that we force multiple segments. Since we force a // separate segment for every object, every pointer is a far pointer, and far pointers are easily // transferred, so this is actually not such a complicated case. MallocMessageBuilder builder(0, AllocationStrategy::FIXED_SIZE); auto root = builder.initRoot(); // Start with a 1-word first segment and the root object in the second segment. size_t size = builder.getSegmentsForOutput().size(); EXPECT_EQ(2u, size); { auto oldVersion = root.getAnyPointerField().initAs(); oldVersion.setOld1(123); oldVersion.setOld2("foo"); auto sub = oldVersion.initOld3(); sub.setOld1(456); sub.setOld2("bar"); } // Allocated two structs and two strings. size_t size2 = builder.getSegmentsForOutput().size(); EXPECT_EQ(size + 4, size2); size_t size4; { auto newVersion = root.getAnyPointerField().getAs(); // Allocated a new struct. size_t size3 = builder.getSegmentsForOutput().size(); EXPECT_EQ(size2 + 1, size3); auto sub = newVersion.getOld3(); // Allocated another new struct for its string field. size4 = builder.getSegmentsForOutput().size(); EXPECT_EQ(size3 + 1, size4); // Check contents. EXPECT_EQ(123, newVersion.getOld1()); EXPECT_EQ("foo", newVersion.getOld2()); EXPECT_EQ(987, newVersion.getNew1()); EXPECT_EQ("baz", newVersion.getNew2()); EXPECT_EQ(456, sub.getOld1()); EXPECT_EQ("bar", sub.getOld2()); EXPECT_EQ(987, sub.getNew1()); EXPECT_EQ("baz", sub.getNew2()); newVersion.setOld1(234); newVersion.setOld2("qux"); newVersion.setNew1(321); newVersion.setNew2("quux"); sub.setOld1(567); sub.setOld2("corge"); sub.setNew1(654); sub.setNew2("grault"); } // Set four strings and implicitly initialized two. size_t size5 = builder.getSegmentsForOutput().size(); EXPECT_EQ(size4 + 6, size5); { // Go back to old version. It should have the values set on the new version. auto oldVersion = root.getAnyPointerField().getAs(); EXPECT_EQ(234, oldVersion.getOld1()); EXPECT_EQ("qux", oldVersion.getOld2()); auto sub = oldVersion.getOld3(); EXPECT_EQ(567, sub.getOld1()); EXPECT_EQ("corge", sub.getOld2()); // Overwrite the old fields. The new fields should remain intact. oldVersion.setOld1(345); oldVersion.setOld2("garply"); sub.setOld1(678); sub.setOld2("waldo"); } // Set two new strings. size_t size6 = builder.getSegmentsForOutput().size(); EXPECT_EQ(size5 + 2, size6); { // Back to the new version again. auto newVersion = root.getAnyPointerField().getAs(); EXPECT_EQ(345, newVersion.getOld1()); EXPECT_EQ("garply", newVersion.getOld2()); EXPECT_EQ(321, newVersion.getNew1()); EXPECT_EQ("quux", newVersion.getNew2()); auto sub = newVersion.getOld3(); EXPECT_EQ(678, sub.getOld1()); EXPECT_EQ("waldo", sub.getOld2()); EXPECT_EQ(654, sub.getNew1()); EXPECT_EQ("grault", sub.getNew2()); } // Size should not have changed because we didn't write anything and the structs were already // the right size. EXPECT_EQ(size6, builder.getSegmentsForOutput().size()); } TEST(Encoding, UpgradeStructInBuilderFarPointers) { // Force allocation of a Far pointer. MallocMessageBuilder builder(7, AllocationStrategy::FIXED_SIZE); auto root = builder.initRoot(); root.getAnyPointerField().initAs().setOld2("foo"); // We should have allocated all but one word of the first segment. EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); EXPECT_EQ(6u, builder.getSegmentsForOutput()[0].size()); // Now if we upgrade... EXPECT_EQ("foo", root.getAnyPointerField().getAs().getOld2()); // We should have allocated the new struct in a new segment, but allocated the far pointer // landing pad back in the first segment. ASSERT_EQ(2u, builder.getSegmentsForOutput().size()); EXPECT_EQ(7u, builder.getSegmentsForOutput()[0].size()); EXPECT_EQ(6u, builder.getSegmentsForOutput()[1].size()); } TEST(Encoding, UpgradeStructInBuilderDoubleFarPointers) { // Force allocation of a double-Far pointer. MallocMessageBuilder builder(6, AllocationStrategy::FIXED_SIZE); auto root = builder.initRoot(); root.getAnyPointerField().initAs().setOld2("foo"); // We should have allocated all of the first segment. EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); EXPECT_EQ(6u, builder.getSegmentsForOutput()[0].size()); // Now if we upgrade... EXPECT_EQ("foo", root.getAnyPointerField().getAs().getOld2()); // We should have allocated the new struct in a new segment, and also allocated the far pointer // landing pad in yet another segment. ASSERT_EQ(3u, builder.getSegmentsForOutput().size()); EXPECT_EQ(6u, builder.getSegmentsForOutput()[0].size()); EXPECT_EQ(6u, builder.getSegmentsForOutput()[1].size()); EXPECT_EQ(2u, builder.getSegmentsForOutput()[2].size()); } void checkUpgradedList(test::TestAnyPointer::Builder root, std::initializer_list expectedData, std::initializer_list expectedPointers) { { auto builder = root.getAnyPointerField().getAs>(); ASSERT_EQ(expectedData.size(), builder.size()); for (uint i = 0; i < expectedData.size(); i++) { EXPECT_EQ(expectedData.begin()[i], builder[i].getOld1()); EXPECT_EQ(expectedPointers.begin()[i], builder[i].getOld2()); // Other fields shouldn't be set. EXPECT_EQ(0, builder[i].asReader().getOld3().getOld1()); EXPECT_EQ("", builder[i].asReader().getOld3().getOld2()); EXPECT_EQ(987, builder[i].getNew1()); EXPECT_EQ("baz", builder[i].getNew2()); // Write some new data. builder[i].setOld1(i * 123); builder[i].setOld2(kj::str("qux", i, '\0').begin()); builder[i].setNew1(i * 456); builder[i].setNew2(kj::str("corge", i, '\0').begin()); } } // Read the newly-written data as TestOldVersion to ensure it was updated. { auto builder = root.getAnyPointerField().getAs>(); ASSERT_EQ(expectedData.size(), builder.size()); for (uint i = 0; i < expectedData.size(); i++) { EXPECT_EQ(i * 123, builder[i].getOld1()); EXPECT_EQ(Text::Reader(kj::str("qux", i, "\0").begin()), builder[i].getOld2()); } } // Also read back as TestNewVersion again. { auto builder = root.getAnyPointerField().getAs>(); ASSERT_EQ(expectedData.size(), builder.size()); for (uint i = 0; i < expectedData.size(); i++) { EXPECT_EQ(i * 123, builder[i].getOld1()); EXPECT_EQ(Text::Reader(kj::str("qux", i, '\0').begin()), builder[i].getOld2()); EXPECT_EQ(i * 456, builder[i].getNew1()); EXPECT_EQ(Text::Reader(kj::str("corge", i, '\0').begin()), builder[i].getNew2()); } } } TEST(Encoding, UpgradeListInBuilder) { // Test every damned list upgrade. MallocMessageBuilder builder; auto root = builder.initRoot(); // ----------------------------------------------------------------- root.getAnyPointerField().setAs>({VOID, VOID, VOID, VOID}); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID, VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkUpgradedList(root, {0, 0, 0, 0}, {"", "", "", ""}); // ----------------------------------------------------------------- { root.getAnyPointerField().setAs>({true, false, true, true}); auto orig = root.asReader().getAnyPointerField().getAs>(); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {true, false, true, true}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(orig, {true, false, true, true}); // Can't upgrade bit lists. (This used to be supported.) EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); } // ----------------------------------------------------------------- { root.getAnyPointerField().setAs>({0x12, 0x23, 0x33, 0x44}); auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID, VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {0x12, 0x23, 0x33, 0x44}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(orig, {0x12, 0x23, 0x33, 0x44}); checkUpgradedList(root, {0x12, 0x23, 0x33, 0x44}, {"", "", "", ""}); checkList(orig, {0, 0, 0, 0}); // old location zero'd during upgrade } // ----------------------------------------------------------------- { root.getAnyPointerField().setAs>({0x5612, 0x7823, 0xab33, 0xcd44}); auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID, VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {0x12, 0x23, 0x33, 0x44}); checkList(root.getAnyPointerField().getAs>(), {0x5612, 0x7823, 0xab33, 0xcd44}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(orig, {0x5612, 0x7823, 0xab33, 0xcd44}); checkUpgradedList(root, {0x5612, 0x7823, 0xab33, 0xcd44}, {"", "", "", ""}); checkList(orig, {0, 0, 0, 0}); // old location zero'd during upgrade } // ----------------------------------------------------------------- { root.getAnyPointerField().setAs>({0x17595612, 0x29347823, 0x5923ab32, 0x1a39cd45}); auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID, VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {0x12, 0x23, 0x32, 0x45}); checkList(root.getAnyPointerField().getAs>(), {0x5612, 0x7823, 0xab32, 0xcd45}); checkList(root.getAnyPointerField().getAs>(), {0x17595612u, 0x29347823u, 0x5923ab32u, 0x1a39cd45u}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(orig, {0x17595612u, 0x29347823u, 0x5923ab32u, 0x1a39cd45u}); checkUpgradedList(root, {0x17595612, 0x29347823, 0x5923ab32, 0x1a39cd45}, {"", "", "", ""}); checkList(orig, {0u, 0u, 0u, 0u}); // old location zero'd during upgrade } // ----------------------------------------------------------------- { root.getAnyPointerField().setAs>({0x1234abcd8735fe21, 0x7173bc0e1923af36}); auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {0x21, 0x36}); checkList(root.getAnyPointerField().getAs>(), {0xfe21, 0xaf36}); checkList(root.getAnyPointerField().getAs>(), {0x8735fe21u, 0x1923af36u}); checkList(root.getAnyPointerField().getAs>(), {0x1234abcd8735fe21ull, 0x7173bc0e1923af36ull}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(orig, {0x1234abcd8735fe21ull, 0x7173bc0e1923af36ull}); checkUpgradedList(root, {0x1234abcd8735fe21ull, 0x7173bc0e1923af36ull}, {"", ""}); checkList(orig, {0u, 0u}); // old location zero'd during upgrade } // ----------------------------------------------------------------- { root.getAnyPointerField().setAs>({"foo", "bar", "baz"}); auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {"foo", "bar", "baz"}); checkList(orig, {"foo", "bar", "baz"}); checkUpgradedList(root, {0, 0, 0}, {"foo", "bar", "baz"}); checkList(orig, {"", "", ""}); // old location zero'd during upgrade } // ----------------------------------------------------------------- { { auto l = root.getAnyPointerField().initAs>(3); l[0].setOld1(0x1234567890abcdef); l[1].setOld1(0x234567890abcdef1); l[2].setOld1(0x34567890abcdef12); l[0].setOld2("foo"); l[1].setOld2("bar"); l[2].setOld2("baz"); } auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(root.getAnyPointerField().getAs>(), {VOID, VOID, VOID}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {0xefu, 0xf1u, 0x12u}); checkList(root.getAnyPointerField().getAs>(), {0xcdefu, 0xdef1u, 0xef12u}); checkList(root.getAnyPointerField().getAs>(), {0x90abcdefu, 0x0abcdef1u, 0xabcdef12u}); checkList(root.getAnyPointerField().getAs>(), {0x1234567890abcdefull, 0x234567890abcdef1ull, 0x34567890abcdef12ull}); checkList(root.getAnyPointerField().getAs>(), {"foo", "bar", "baz"}); checkList(orig, {0x1234567890abcdefull, 0x234567890abcdef1ull, 0x34567890abcdef12ull}, {"foo", "bar", "baz"}); checkUpgradedList(root, {0x1234567890abcdefull, 0x234567890abcdef1ull, 0x34567890abcdef12ull}, {"foo", "bar", "baz"}); checkList(orig, {0u, 0u, 0u}, {"", "", ""}); // old location zero'd during upgrade } // ----------------------------------------------------------------- // OK, now we've tested upgrading every primitive list to every primitive list, every primitive // list to a multi-word struct, and a multi-word struct to every primitive list. But we haven't // tried upgrading primitive lists to sub-word structs. // Upgrade from multi-byte, sub-word data. root.getAnyPointerField().setAs>({12u, 34u, 56u, 78u}); { auto orig = root.asReader().getAnyPointerField().getAs>(); checkList(orig, {12u, 34u, 56u, 78u}); auto l = root.getAnyPointerField().getAs>(); checkList(orig, {0u, 0u, 0u, 0u}); // old location zero'd during upgrade ASSERT_EQ(4u, l.size()); EXPECT_EQ(12u, l[0].getF()); EXPECT_EQ(34u, l[1].getF()); EXPECT_EQ(56u, l[2].getF()); EXPECT_EQ(78u, l[3].getF()); l[0].setF(0x65ac1235u); l[1].setF(0x13f12879u); l[2].setF(0x33423082u); l[3].setF(0x12988948u); } EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {0x35u, 0x79u, 0x82u, 0x48u}); checkList(root.getAnyPointerField().getAs>(), {0x1235u, 0x2879u, 0x3082u, 0x8948u}); checkList(root.getAnyPointerField().getAs>(), {0x65ac1235u, 0x13f12879u, 0x33423082u, 0x12988948u}); checkList(root.getAnyPointerField().getAs>(), {0x65ac1235u, 0x13f12879u, 0x33423082u, 0x12988948u}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); // Upgrade from void -> data struct root.getAnyPointerField().setAs>({VOID, VOID, VOID, VOID}); { auto l = root.getAnyPointerField().getAs>(); ASSERT_EQ(4u, l.size()); EXPECT_EQ(0u, l[0].getF()); EXPECT_EQ(0u, l[1].getF()); EXPECT_EQ(0u, l[2].getF()); EXPECT_EQ(0u, l[3].getF()); l[0].setF(12573); l[1].setF(3251); l[2].setF(9238); l[3].setF(5832); } EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {12573u, 3251u, 9238u, 5832u}); checkList(root.getAnyPointerField().getAs>(), {12573u, 3251u, 9238u, 5832u}); checkList(root.getAnyPointerField().getAs>(), {12573u, 3251u, 9238u, 5832u}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); // Upgrade from void -> pointer struct root.getAnyPointerField().setAs>({VOID, VOID, VOID, VOID}); { auto l = root.getAnyPointerField().getAs>(); ASSERT_EQ(4u, l.size()); EXPECT_EQ("", l[0].getF()); EXPECT_EQ("", l[1].getF()); EXPECT_EQ("", l[2].getF()); EXPECT_EQ("", l[3].getF()); l[0].setF("foo"); l[1].setF("bar"); l[2].setF("baz"); l[3].setF("qux"); } EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); checkList(root.getAnyPointerField().getAs>(), {"foo", "bar", "baz", "qux"}); // Verify that we cannot "side-grade" a pointer list to a data list, or a data list to // a pointer struct list. root.getAnyPointerField().setAs>({"foo", "bar", "baz", "qux"}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); root.getAnyPointerField().setAs>({12, 34, 56, 78}); EXPECT_NONFATAL_FAILURE(root.getAnyPointerField().getAs>()); } TEST(Encoding, UpgradeUnion) { // This tests for a specific case that was broken originally. MallocMessageBuilder builder; { auto root = builder.getRoot(); root.setB(123); } { auto root = builder.getRoot(); ASSERT_TRUE(root.isB()) EXPECT_EQ(123, root.getB()); } } // ======================================================================================= // Tests of generated code, not really of the encoding. // TODO(cleanup): Move to a different test? TEST(Encoding, NestedTypes) { // This is more of a test of the generated code than the encoding. MallocMessageBuilder builder; TestNestedTypes::Reader reader = builder.getRoot().asReader(); EXPECT_EQ(TestNestedTypes::NestedEnum::BAR, reader.getOuterNestedEnum()); EXPECT_EQ(TestNestedTypes::NestedStruct::NestedEnum::QUUX, reader.getInnerNestedEnum()); TestNestedTypes::NestedStruct::Reader nested = reader.getNestedStruct(); EXPECT_EQ(TestNestedTypes::NestedEnum::BAR, nested.getOuterNestedEnum()); EXPECT_EQ(TestNestedTypes::NestedStruct::NestedEnum::QUUX, nested.getInnerNestedEnum()); } TEST(Encoding, Imports) { // Also just testing the generated code. { MallocMessageBuilder builder; TestImport::Builder root = builder.getRoot(); initTestMessage(root.initField()); checkTestMessage(root.asReader().getField()); } { MallocMessageBuilder builder; TestImport2::Builder root = builder.getRoot(); initTestMessage(root.initFoo()); checkTestMessage(root.asReader().getFoo()); root.setBar(schemaProto()); initTestMessage(root.initBaz().initField()); checkTestMessage(root.asReader().getBaz().getField()); } } TEST(Encoding, Using) { MallocMessageBuilder builder; TestUsing::Reader reader = builder.getRoot().asReader(); EXPECT_EQ(TestNestedTypes::NestedEnum::BAR, reader.getOuterNestedEnum()); EXPECT_EQ(TestNestedTypes::NestedStruct::NestedEnum::QUUX, reader.getInnerNestedEnum()); } TEST(Encoding, StructSetters) { MallocMessageBuilder builder; auto root = builder.getRoot(); initTestMessage(root); { MallocMessageBuilder builder2; builder2.setRoot(root.asReader()); checkTestMessage(builder2.getRoot()); } { MallocMessageBuilder builder2; auto root2 = builder2.getRoot(); root2.setStructField(root); checkTestMessage(root2.getStructField()); } { MallocMessageBuilder builder2; auto root2 = builder2.getRoot(); root2.getAnyPointerField().setAs(root); checkTestMessage(root2.getAnyPointerField().getAs()); } } TEST(Encoding, OneBitStructSetters) { // Test case of setting a 1-bit struct. MallocMessageBuilder builder; auto root = builder.getRoot(); auto list = root.initList1(8); list[0].setF(true); list[1].setF(true); list[2].setF(false); list[3].setF(true); list[4].setF(true); list[5].setF(false); list[6].setF(true); list[7].setF(false); MallocMessageBuilder builder2; builder2.setRoot(list.asReader()[2]); EXPECT_FALSE(builder2.getRoot().getF()); builder2.setRoot(list.asReader()[6]); EXPECT_TRUE(builder2.getRoot().getF()); } TEST(Encoding, ListSetters) { MallocMessageBuilder builder; auto root = builder.getRoot(); initTestMessage(root); { MallocMessageBuilder builder2; auto root2 = builder2.getRoot(); root2.getLists().setList0(root.getLists().getList0()); root2.getLists().setList1(root.getLists().getList1()); root2.getLists().setList8(root.getLists().getList8()); root2.getLists().setList16(root.getLists().getList16()); root2.getLists().setList32(root.getLists().getList32()); root2.getLists().setList64(root.getLists().getList64()); root2.getLists().setListP(root.getLists().getListP()); { auto dst = root2.getLists().initInt32ListList(3); auto src = root.getLists().getInt32ListList(); dst.set(0, src[0]); dst.set(1, src[1]); dst.set(2, src[2]); } { auto dst = root2.getLists().initTextListList(3); auto src = root.getLists().getTextListList(); dst.set(0, src[0]); dst.set(1, src[1]); dst.set(2, src[2]); } { auto dst = root2.getLists().initStructListList(2); auto src = root.getLists().getStructListList(); dst.set(0, src[0]); dst.set(1, src[1]); } } } TEST(Encoding, ZeroOldObject) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root); auto oldRoot = root.asReader(); checkTestMessage(oldRoot); auto oldSub = oldRoot.getStructField(); auto oldSub2 = oldRoot.getStructList()[0]; root = builder.initRoot(); checkTestMessageAllZero(oldRoot); checkTestMessageAllZero(oldSub); checkTestMessageAllZero(oldSub2); } TEST(Encoding, Has) { MallocMessageBuilder builder; auto root = builder.initRoot(); EXPECT_FALSE(root.hasTextField()); EXPECT_FALSE(root.hasDataField()); EXPECT_FALSE(root.hasStructField()); EXPECT_FALSE(root.hasInt32List()); EXPECT_FALSE(root.asReader().hasTextField()); EXPECT_FALSE(root.asReader().hasDataField()); EXPECT_FALSE(root.asReader().hasStructField()); EXPECT_FALSE(root.asReader().hasInt32List()); initTestMessage(root); EXPECT_TRUE(root.hasTextField()); EXPECT_TRUE(root.hasDataField()); EXPECT_TRUE(root.hasStructField()); EXPECT_TRUE(root.hasInt32List()); EXPECT_TRUE(root.asReader().hasTextField()); EXPECT_TRUE(root.asReader().hasDataField()); EXPECT_TRUE(root.asReader().hasStructField()); EXPECT_TRUE(root.asReader().hasInt32List()); } TEST(Encoding, VoidListAmplification) { MallocMessageBuilder builder; builder.initRoot().getAnyPointerField().initAs>(1u << 28); auto segments = builder.getSegmentsForOutput(); EXPECT_EQ(1, segments.size()); EXPECT_LT(segments[0].size(), 16); // quite small for such a big list! SegmentArrayMessageReader reader(builder.getSegmentsForOutput()); auto root = reader.getRoot().getAnyPointerField(); EXPECT_NONFATAL_FAILURE(root.getAs>()); MallocMessageBuilder copy; EXPECT_NONFATAL_FAILURE(copy.setRoot(reader.getRoot())); } TEST(Encoding, EmptyStructListAmplification) { MallocMessageBuilder builder(1024); auto listList = builder.initRoot().getAnyPointerField() .initAs>>(500); for (uint i = 0; i < listList.size(); i++) { listList.init(i, 1u << 28); } auto segments = builder.getSegmentsForOutput(); ASSERT_EQ(1, segments.size()); SegmentArrayMessageReader reader(builder.getSegmentsForOutput()); auto root = reader.getRoot(); auto listListReader = root.getAnyPointerField().getAs>>(); EXPECT_NONFATAL_FAILURE(listListReader[0]); EXPECT_NONFATAL_FAILURE(listListReader[10]); EXPECT_EQ(segments[0].size() - 1, root.totalSize().wordCount); } TEST(Encoding, Constants) { EXPECT_EQ(VOID, test::TestConstants::VOID_CONST); EXPECT_EQ(true, test::TestConstants::BOOL_CONST); EXPECT_EQ(-123, test::TestConstants::INT8_CONST); EXPECT_EQ(-12345, test::TestConstants::INT16_CONST); EXPECT_EQ(-12345678, test::TestConstants::INT32_CONST); EXPECT_EQ(-123456789012345ll, test::TestConstants::INT64_CONST); EXPECT_EQ(234u, test::TestConstants::UINT8_CONST); EXPECT_EQ(45678u, test::TestConstants::UINT16_CONST); EXPECT_EQ(3456789012u, test::TestConstants::UINT32_CONST); EXPECT_EQ(12345678901234567890ull, test::TestConstants::UINT64_CONST); EXPECT_FLOAT_EQ(1234.5f, test::TestConstants::FLOAT32_CONST); EXPECT_DOUBLE_EQ(-123e45, test::TestConstants::FLOAT64_CONST); EXPECT_EQ("foo", *test::TestConstants::TEXT_CONST); EXPECT_EQ(data("bar"), test::TestConstants::DATA_CONST); { TestAllTypes::Reader subReader = test::TestConstants::STRUCT_CONST; EXPECT_EQ(VOID, subReader.getVoidField()); EXPECT_EQ(true, subReader.getBoolField()); EXPECT_EQ(-12, subReader.getInt8Field()); EXPECT_EQ(3456, subReader.getInt16Field()); EXPECT_EQ(-78901234, subReader.getInt32Field()); EXPECT_EQ(56789012345678ll, subReader.getInt64Field()); EXPECT_EQ(90u, subReader.getUInt8Field()); EXPECT_EQ(1234u, subReader.getUInt16Field()); EXPECT_EQ(56789012u, subReader.getUInt32Field()); EXPECT_EQ(345678901234567890ull, subReader.getUInt64Field()); EXPECT_FLOAT_EQ(-1.25e-10f, subReader.getFloat32Field()); EXPECT_DOUBLE_EQ(345, subReader.getFloat64Field()); EXPECT_EQ("baz", subReader.getTextField()); EXPECT_EQ(data("qux"), subReader.getDataField()); { auto subSubReader = subReader.getStructField(); EXPECT_EQ("nested", subSubReader.getTextField()); EXPECT_EQ("really nested", subSubReader.getStructField().getTextField()); } EXPECT_EQ(TestEnum::BAZ, subReader.getEnumField()); checkList(subReader.getVoidList(), {VOID, VOID, VOID}); checkList(subReader.getBoolList(), {false, true, false, true, true}); checkList(subReader.getInt8List(), {12, -34, -0x80, 0x7f}); checkList(subReader.getInt16List(), {1234, -5678, -0x8000, 0x7fff}); // gcc warns on -0x800... and the only work-around I could find was to do -0x7ff...-1. checkList(subReader.getInt32List(), {12345678, -90123456, -0x7fffffff - 1, 0x7fffffff}); checkList(subReader.getInt64List(), {123456789012345ll, -678901234567890ll, -0x7fffffffffffffffll-1, 0x7fffffffffffffffll}); checkList(subReader.getUInt8List(), {12u, 34u, 0u, 0xffu}); checkList(subReader.getUInt16List(), {1234u, 5678u, 0u, 0xffffu}); checkList(subReader.getUInt32List(), {12345678u, 90123456u, 0u, 0xffffffffu}); checkList(subReader.getUInt64List(), {123456789012345ull, 678901234567890ull, 0ull, 0xffffffffffffffffull}); checkList(subReader.getFloat32List(), {0.0f, 1234567.0f, 1e37f, -1e37f, 1e-37f, -1e-37f}); checkList(subReader.getFloat64List(), {0.0, 123456789012345.0, 1e306, -1e306, 1e-306, -1e-306}); checkList(subReader.getTextList(), {"quux", "corge", "grault"}); checkList(subReader.getDataList(), {data("garply"), data("waldo"), data("fred")}); { auto listReader = subReader.getStructList(); ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("x structlist 1", listReader[0].getTextField()); EXPECT_EQ("x structlist 2", listReader[1].getTextField()); EXPECT_EQ("x structlist 3", listReader[2].getTextField()); } checkList(subReader.getEnumList(), {TestEnum::QUX, TestEnum::BAR, TestEnum::GRAULT}); } EXPECT_EQ(TestEnum::CORGE, test::TestConstants::ENUM_CONST); EXPECT_EQ(6u, test::TestConstants::VOID_LIST_CONST->size()); checkList(*test::TestConstants::BOOL_LIST_CONST, {true, false, false, true}); checkList(*test::TestConstants::INT8_LIST_CONST, {111, -111}); checkList(*test::TestConstants::INT16_LIST_CONST, {11111, -11111}); checkList(*test::TestConstants::INT32_LIST_CONST, {111111111, -111111111}); checkList(*test::TestConstants::INT64_LIST_CONST, {1111111111111111111ll, -1111111111111111111ll}); checkList(*test::TestConstants::UINT8_LIST_CONST, {111u, 222u}); checkList(*test::TestConstants::UINT16_LIST_CONST, {33333u, 44444u}); checkList(*test::TestConstants::UINT32_LIST_CONST, {3333333333u}); checkList(*test::TestConstants::UINT64_LIST_CONST, {11111111111111111111ull}); { List::Reader listReader = test::TestConstants::FLOAT32_LIST_CONST; ASSERT_EQ(4u, listReader.size()); EXPECT_EQ(5555.5f, listReader[0]); EXPECT_EQ(kj::inf(), listReader[1]); EXPECT_EQ(-kj::inf(), listReader[2]); EXPECT_TRUE(listReader[3] != listReader[3]); } { List::Reader listReader = test::TestConstants::FLOAT64_LIST_CONST; ASSERT_EQ(4u, listReader.size()); EXPECT_EQ(7777.75, listReader[0]); EXPECT_EQ(kj::inf(), listReader[1]); EXPECT_EQ(-kj::inf(), listReader[2]); EXPECT_TRUE(listReader[3] != listReader[3]); } checkList(*test::TestConstants::TEXT_LIST_CONST, {"plugh", "xyzzy", "thud"}); checkList(*test::TestConstants::DATA_LIST_CONST, {data("oops"), data("exhausted"), data("rfc3092")}); { List::Reader listReader = test::TestConstants::STRUCT_LIST_CONST; ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("structlist 1", listReader[0].getTextField()); EXPECT_EQ("structlist 2", listReader[1].getTextField()); EXPECT_EQ("structlist 3", listReader[2].getTextField()); } checkList(*test::TestConstants::ENUM_LIST_CONST, {TestEnum::FOO, TestEnum::GARPLY}); } TEST(Encoding, AnyPointerConstants) { auto reader = test::ANY_POINTER_CONSTANTS.get(); EXPECT_EQ("baz", reader.getAnyKindAsStruct().getAs().getTextField()); EXPECT_EQ("baz", reader.getAnyStructAsStruct().as().getTextField()); EXPECT_EQ(111111111, reader.getAnyKindAsList().getAs>()[0]); EXPECT_EQ(111111111, reader.getAnyListAsList().as>()[0]); } TEST(Encoding, GlobalConstants) { EXPECT_EQ(12345u, test::GLOBAL_INT); EXPECT_EQ("foobar", test::GLOBAL_TEXT.get()); EXPECT_EQ(54321, test::GLOBAL_STRUCT->getInt32Field()); TestAllTypes::Reader reader = test::DERIVED_CONSTANT; EXPECT_EQ(12345, reader.getUInt32Field()); EXPECT_EQ("foo", reader.getTextField()); checkList(reader.getStructField().getTextList(), {"quux", "corge", "grault"}); checkList(reader.getInt16List(), {11111, -11111}); { List::Reader listReader = reader.getStructList(); ASSERT_EQ(3u, listReader.size()); EXPECT_EQ("structlist 1", listReader[0].getTextField()); EXPECT_EQ("structlist 2", listReader[1].getTextField()); EXPECT_EQ("structlist 3", listReader[2].getTextField()); } } TEST(Encoding, Embeds) { { kj::ArrayInputStream input(test::EMBEDDED_DATA); PackedMessageReader reader(input); checkTestMessage(reader.getRoot()); } #if !CAPNP_LITE { MallocMessageBuilder builder; auto root = builder.getRoot(); initTestMessage(root); kj::StringPtr text = test::EMBEDDED_TEXT; EXPECT_EQ(kj::str(root, text.endsWith("\r\n") ? "\r\n" : "\n"), text); } #endif // CAPNP_LITE { checkTestMessage(test::EMBEDDED_STRUCT); } } TEST(Encoding, HasEmptyStruct) { MallocMessageBuilder message; auto root = message.initRoot(); EXPECT_EQ(1, root.totalSize().wordCount); EXPECT_FALSE(root.asReader().hasAnyPointerField()); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().initAs(); EXPECT_TRUE(root.asReader().hasAnyPointerField()); EXPECT_TRUE(root.hasAnyPointerField()); EXPECT_EQ(1, root.totalSize().wordCount); } TEST(Encoding, HasEmptyList) { MallocMessageBuilder message; auto root = message.initRoot(); EXPECT_EQ(1, root.totalSize().wordCount); EXPECT_FALSE(root.asReader().hasAnyPointerField()); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().initAs>(0); EXPECT_TRUE(root.asReader().hasAnyPointerField()); EXPECT_TRUE(root.hasAnyPointerField()); EXPECT_EQ(1, root.totalSize().wordCount); } TEST(Encoding, HasEmptyStructList) { MallocMessageBuilder message; auto root = message.initRoot(); EXPECT_EQ(1, root.totalSize().wordCount); EXPECT_FALSE(root.asReader().hasAnyPointerField()); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().initAs>(0); EXPECT_TRUE(root.asReader().hasAnyPointerField()); EXPECT_TRUE(root.hasAnyPointerField()); EXPECT_EQ(2, root.totalSize().wordCount); } TEST(Encoding, NameAnnotation) { EXPECT_EQ(2, static_cast(test::RenamedStruct::RenamedEnum::QUX)); EXPECT_EQ(2, static_cast(test::RenamedStruct::RenamedNestedStruct::RenamedDeeplyNestedEnum::GARPLY)); MallocMessageBuilder message; auto root = message.initRoot(); root.setGoodFieldName(true); EXPECT_EQ(true, root.getGoodFieldName()); EXPECT_TRUE(root.isGoodFieldName()); root.setBar(0xff); EXPECT_FALSE(root.isGoodFieldName()); root.setAnotherGoodFieldName(test::RenamedStruct::RenamedEnum::QUX); EXPECT_EQ(test::RenamedStruct::RenamedEnum::QUX, root.getAnotherGoodFieldName()); EXPECT_FALSE(root.getRenamedUnion().isQux()); auto quxBuilder = root.getRenamedUnion().initQux(); EXPECT_TRUE(root.getRenamedUnion().isQux()); EXPECT_FALSE(root.getRenamedUnion().getQux().hasAnotherGoodNestedFieldName()); quxBuilder.setGoodNestedFieldName(true); EXPECT_EQ(true, quxBuilder.getGoodNestedFieldName()); EXPECT_FALSE(quxBuilder.hasAnotherGoodNestedFieldName()); auto nestedFieldBuilder = quxBuilder.initAnotherGoodNestedFieldName(); EXPECT_TRUE(quxBuilder.hasAnotherGoodNestedFieldName()); nestedFieldBuilder.setGoodNestedFieldName(true); EXPECT_EQ(true, nestedFieldBuilder.getGoodNestedFieldName()); EXPECT_FALSE(nestedFieldBuilder.hasAnotherGoodNestedFieldName()); EXPECT_FALSE(root.getRenamedUnion().isRenamedGroup()); auto renamedGroupBuilder KJ_UNUSED = root.getRenamedUnion().initRenamedGroup(); EXPECT_TRUE(root.getRenamedUnion().isRenamedGroup()); test::RenamedInterface::RenamedMethodParams::Reader renamedInterfaceParams; renamedInterfaceParams.getRenamedParam(); } TEST(Encoding, DefaultFloatPlusNan) { MallocMessageBuilder message; auto root = message.initRoot(); root.setFloat32Field(kj::nan()); root.setFloat64Field(kj::nan()); float f = root.getFloat32Field(); EXPECT_TRUE(f != f); double d = root.getFloat64Field(); EXPECT_TRUE(d != d); } TEST(Encoding, WholeFloatDefault) { MallocMessageBuilder message; auto root = message.initRoot(); EXPECT_EQ(123.0f, root.getField()); EXPECT_EQ(2e30f, root.getBigField()); EXPECT_EQ(456.0f, test::TestWholeFloatDefault::CONSTANT); EXPECT_EQ(4e30f, test::TestWholeFloatDefault::BIG_CONSTANT); } TEST(Encoding, Generics) { MallocMessageBuilder message; auto root = message.initRoot(); auto reader = root.asReader(); initTestMessage(root.initBasic().initFoo()); checkTestMessage(reader.getBasic().getFoo()); { auto typed = root.getBasic(); test::TestGenerics<>::Reader generic = typed.asGeneric<>(); checkTestMessage(generic.getFoo().getAs()); test::TestGenerics::Reader halfGeneric = typed.asGeneric(); checkTestMessage(halfGeneric.getFoo()); } { auto typed = root.getBasic().asReader(); test::TestGenerics<>::Reader generic = typed.asGeneric<>(); checkTestMessage(generic.getFoo().getAs()); test::TestGenerics::Reader halfGeneric = typed.asGeneric(); checkTestMessage(halfGeneric.getFoo()); } initTestMessage(root.initInner().initFoo()); checkTestMessage(reader.getInner().getFoo()); { auto typed = root.getInner(); test::TestGenerics<>::Inner::Reader generic = typed.asTestGenericsGeneric<>(); checkTestMessage(generic.getFoo().getAs()); test::TestGenerics::Inner::Reader halfGeneric = typed.asTestGenericsGeneric(); checkTestMessage(halfGeneric.getFoo()); } { auto typed = root.getInner().asReader(); test::TestGenerics<>::Inner::Reader generic = typed.asTestGenericsGeneric<>(); checkTestMessage(generic.getFoo().getAs()); test::TestGenerics::Inner::Reader halfGeneric = typed.asTestGenericsGeneric(); checkTestMessage(halfGeneric.getFoo()); } root.initInner2().setBaz("foo"); EXPECT_EQ("foo", reader.getInner2().getBaz()); initTestMessage(root.getInner2().initInnerBound().initFoo()); checkTestMessage(reader.getInner2().getInnerBound().getFoo()); initTestMessage(root.getInner2().initInnerUnbound().getFoo().initAs()); checkTestMessage(reader.getInner2().getInnerUnbound().getFoo().getAs()); initTestMessage(root.initUnspecified().getFoo().initAs()); checkTestMessage(reader.getUnspecified().getFoo().getAs()); initTestMessage(root.initWrapper().initValue().initFoo()); checkTestMessage(reader.getWrapper().getValue().getFoo()); } TEST(Encoding, GenericDefaults) { test::TestUseGenerics::Reader reader; EXPECT_EQ(123, reader.getDefault().getFoo().getInt16Field()); EXPECT_EQ(123, reader.getDefaultInner().getFoo().getInt16Field()); EXPECT_EQ("text", reader.getDefaultInner().getBar()); EXPECT_EQ(123, reader.getDefaultUser().getBasic().getFoo().getInt16Field()); EXPECT_EQ("text", reader.getDefaultWrapper().getValue().getFoo()); EXPECT_EQ(321, reader.getDefaultWrapper().getValue().getRev().getFoo().getInt16Field()); EXPECT_EQ("text", reader.getDefaultWrapper2().getValue().getValue().getFoo()); EXPECT_EQ(321, reader.getDefaultWrapper2().getValue() .getValue().getRev().getFoo().getInt16Field()); } TEST(Encoding, UnionInGenerics) { MallocMessageBuilder message; auto builder = message.initRoot>(); auto reader = builder.asReader(); //just call the methods to verify that generated code compiles reader.which(); builder.which(); reader.isUv(); builder.isUv(); reader.getUv(); builder.getUv(); builder.setUv(); builder.initUg(); reader.isUg(); builder.isUg(); reader.getUg(); builder.getUg(); builder.initUg(); } TEST(Encoding, DefaultListBuilder) { // At one point, this wouldn't compile. List::Builder(nullptr); List::Builder(nullptr); List>::Builder(nullptr); List::Builder(nullptr); } TEST(Encoding, ListSize) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root); auto lists = root.asReader().getLists(); auto listSizes = lists.getList0().totalSize() + lists.getList1().totalSize() + lists.getList8().totalSize() + lists.getList16().totalSize() + lists.getList32().totalSize() + lists.getList64().totalSize() + lists.getListP().totalSize() + lists.getInt32ListList().totalSize() + lists.getTextListList().totalSize() + lists.getStructListList().totalSize(); auto structSize = lists.totalSize(); auto shallowSize = unbound(capnp::_::structSize().total() / WORDS); EXPECT_EQ(structSize.wordCount - shallowSize, listSizes.wordCount); } KJ_TEST("list.setWithCaveats(i, list[i]) doesn't corrupt contents") { MallocMessageBuilder builder; auto root = builder.initRoot(); auto list = root.initStructList(2); initTestMessage(list[0]); list.setWithCaveats(0, list[0]); checkTestMessage(list[0]); checkTestMessageAllZero(list[1]); list.setWithCaveats(1, list[0]); checkTestMessage(list[0]); checkTestMessage(list[1]); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/orphan-test.c++0000644000175000017500000015061213340402540021376 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "message.h" #include #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { TEST(Orphans, Structs) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root.initStructField()); EXPECT_TRUE(root.hasStructField()); Orphan orphan = root.disownStructField(); EXPECT_FALSE(orphan == nullptr); checkTestMessage(orphan.getReader()); checkTestMessage(orphan.get()); EXPECT_FALSE(root.hasStructField()); root.adoptStructField(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasStructField()); checkTestMessage(root.asReader().getStructField()); } TEST(Orphans, EmptyStruct) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto anyPointer = root.getAnyPointerField(); EXPECT_TRUE(anyPointer.isNull()); auto orphan = builder.getOrphanage().newOrphan(); anyPointer.adopt(kj::mv(orphan)); EXPECT_EQ(0, anyPointer.targetSize().wordCount); EXPECT_FALSE(anyPointer.isNull()); } TEST(Orphans, EmptyStructOverwrite) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto anyPointer = root.getAnyPointerField(); EXPECT_TRUE(anyPointer.isNull()); anyPointer.initAs(); auto orphan = builder.getOrphanage().newOrphan(); anyPointer.adopt(kj::mv(orphan)); EXPECT_EQ(0, anyPointer.targetSize().wordCount); EXPECT_FALSE(anyPointer.isNull()); } TEST(Orphans, AdoptNullStruct) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto anyPointer = root.getAnyPointerField(); EXPECT_TRUE(anyPointer.isNull()); anyPointer.initAs(); anyPointer.adopt(Orphan()); EXPECT_EQ(0, anyPointer.targetSize().wordCount); EXPECT_TRUE(anyPointer.isNull()); } TEST(Orphans, Lists) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.setUInt32List({12, 34, 56}); EXPECT_TRUE(root.hasUInt32List()); Orphan> orphan = root.disownUInt32List(); EXPECT_FALSE(orphan == nullptr); checkList(orphan.getReader(), {12u, 34u, 56u}); checkList(orphan.get(), {12u, 34u, 56u}); EXPECT_FALSE(root.hasUInt32List()); root.adoptUInt32List(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasUInt32List()); checkList(root.asReader().getUInt32List(), {12u, 34u, 56u}); } TEST(Orphans, StructLists) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto list = root.initStructList(2); list[0].setTextField("foo"); list[1].setTextField("bar"); EXPECT_TRUE(root.hasStructList()); Orphan> orphan = root.disownStructList(); EXPECT_FALSE(orphan == nullptr); ASSERT_EQ(2u, orphan.getReader().size()); EXPECT_EQ("foo", orphan.getReader()[0].getTextField()); EXPECT_EQ("bar", orphan.getReader()[1].getTextField()); ASSERT_EQ(2u, orphan.get().size()); EXPECT_EQ("foo", orphan.get()[0].getTextField()); EXPECT_EQ("bar", orphan.get()[1].getTextField()); EXPECT_FALSE(root.hasStructList()); root.adoptStructList(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasStructList()); ASSERT_EQ(2u, root.asReader().getStructList().size()); EXPECT_EQ("foo", root.asReader().getStructList()[0].getTextField()); EXPECT_EQ("bar", root.asReader().getStructList()[1].getTextField()); } TEST(Orphans, Text) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.setTextField("foo"); EXPECT_TRUE(root.hasTextField()); Orphan orphan = root.disownTextField(); EXPECT_FALSE(orphan == nullptr); EXPECT_EQ("foo", orphan.getReader()); EXPECT_EQ("foo", orphan.get()); EXPECT_FALSE(root.hasTextField()); root.adoptTextField(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasTextField()); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, Data) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.setDataField(data("foo")); EXPECT_TRUE(root.hasDataField()); Orphan orphan = root.disownDataField(); EXPECT_FALSE(orphan == nullptr); EXPECT_EQ(data("foo"), orphan.getReader()); EXPECT_EQ(data("foo"), orphan.get()); EXPECT_FALSE(root.hasDataField()); root.adoptDataField(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasDataField()); EXPECT_EQ(data("foo"), root.getDataField()); } TEST(Orphans, NoCrossMessageTransfers) { MallocMessageBuilder builder1; MallocMessageBuilder builder2; auto root1 = builder1.initRoot(); auto root2 = builder2.initRoot(); initTestMessage(root1.initStructField()); EXPECT_ANY_THROW(root2.adoptStructField(root1.disownStructField())); } TEST(Orphans, OrphanageStruct) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphan(); initTestMessage(orphan.get()); checkTestMessage(orphan.getReader()); auto root = builder.initRoot(); root.adoptStructField(kj::mv(orphan)); } TEST(Orphans, OrphanageList) { MallocMessageBuilder builder; Orphan> orphan = builder.getOrphanage().newOrphan>(2); orphan.get().set(0, 123); orphan.get().set(1, 456); List::Reader reader = orphan.getReader(); ASSERT_EQ(2u, reader.size()); EXPECT_EQ(123u, reader[0]); EXPECT_EQ(456u, reader[1]); auto root = builder.initRoot(); root.adoptUInt32List(kj::mv(orphan)); } TEST(Orphans, OrphanageText) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphan(8); ASSERT_EQ(8u, orphan.get().size()); memcpy(orphan.get().begin(), "12345678", 8); auto root = builder.initRoot(); root.adoptTextField(kj::mv(orphan)); EXPECT_EQ("12345678", root.getTextField()); } TEST(Orphans, OrphanageData) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphan(2); ASSERT_EQ(2u, orphan.get().size()); orphan.get()[0] = 123; orphan.get()[1] = 45; auto root = builder.initRoot(); root.adoptDataField(kj::mv(orphan)); ASSERT_EQ(2u, root.getDataField().size()); EXPECT_EQ(123u, root.getDataField()[0]); EXPECT_EQ(45u, root.getDataField()[1]); } TEST(Orphans, OrphanageStructCopy) { MallocMessageBuilder builder1; MallocMessageBuilder builder2; auto root1 = builder1.initRoot(); initTestMessage(root1); Orphan orphan = builder2.getOrphanage().newOrphanCopy(root1.asReader()); checkTestMessage(orphan.getReader()); auto root2 = builder2.initRoot(); root2.adoptStructField(kj::mv(orphan)); } TEST(Orphans, OrphanageListCopy) { MallocMessageBuilder builder1; MallocMessageBuilder builder2; auto root1 = builder1.initRoot(); root1.setUInt32List({12, 34, 56}); Orphan> orphan = builder2.getOrphanage().newOrphanCopy( root1.asReader().getUInt32List()); checkList(orphan.getReader(), {12u, 34u, 56u}); auto root2 = builder2.initRoot(); root2.adoptUInt32List(kj::mv(orphan)); } TEST(Orphans, OrphanageTextCopy) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphanCopy(Text::Reader("foobarba")); EXPECT_EQ("foobarba", orphan.getReader()); auto root = builder.initRoot(); root.adoptTextField(kj::mv(orphan)); } TEST(Orphans, OrphanageDataCopy) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphanCopy(data("foo")); EXPECT_EQ(data("foo"), orphan.getReader()); auto root = builder.initRoot(); root.adoptDataField(kj::mv(orphan)); } TEST(Orphans, ZeroOut) { MallocMessageBuilder builder; TestAllTypes::Reader orphanReader; { Orphan orphan = builder.getOrphanage().newOrphan(); orphanReader = orphan.getReader(); initTestMessage(orphan.get()); checkTestMessage(orphan.getReader()); } // Once the Orphan destructor is called, the message should be zero'd out. checkTestMessageAllZero(orphanReader); } TEST(Orphans, StructAnyPointer) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root.getAnyPointerField().initAs()); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disownAs(); EXPECT_FALSE(orphan == nullptr); checkTestMessage(orphan.getReader()); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().adopt(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasAnyPointerField()); checkTestMessage(root.asReader().getAnyPointerField().getAs()); } TEST(Orphans, ListAnyPointer) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getAnyPointerField().setAs>({12, 34, 56}); EXPECT_TRUE(root.hasAnyPointerField()); Orphan> orphan = root.getAnyPointerField().disownAs>(); EXPECT_FALSE(orphan == nullptr); checkList(orphan.getReader(), {12u, 34u, 56u}); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().adopt(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasAnyPointerField()); checkList(root.asReader().getAnyPointerField().getAs>(), {12u, 34u, 56u}); } #if !CAPNP_LITE TEST(Orphans, DynamicStruct) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root.getAnyPointerField().initAs()); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disownAs(Schema::from()); EXPECT_FALSE(orphan == nullptr); EXPECT_TRUE(orphan.get().getSchema() == Schema::from()); checkDynamicTestMessage(orphan.getReader()); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().adopt(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasAnyPointerField()); checkTestMessage(root.asReader().getAnyPointerField().getAs()); Orphan orphan2 = root.getAnyPointerField().disownAs(); EXPECT_FALSE(orphan2 == nullptr); EXPECT_TRUE(orphan2.get().getSchema() == Schema::from()); checkDynamicTestMessage(orphan2.getReader()); } TEST(Orphans, DynamicList) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getAnyPointerField().setAs>({12, 34, 56}); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disownAs(Schema::from>()); EXPECT_FALSE(orphan == nullptr); checkList(orphan.getReader(), {12, 34, 56}); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().adopt(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasAnyPointerField()); checkList(root.asReader().getAnyPointerField().getAs>(), {12u, 34u, 56u}); Orphan orphan2 = root.getAnyPointerField().disownAs>(); EXPECT_FALSE(orphan2 == nullptr); checkList(orphan2.getReader(), {12, 34, 56}); } TEST(Orphans, DynamicStructList) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto list = root.getAnyPointerField().initAs>(2); list[0].setTextField("foo"); list[1].setTextField("bar"); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disownAs(Schema::from>()); EXPECT_FALSE(orphan == nullptr); ASSERT_EQ(2u, orphan.get().size()); EXPECT_EQ("foo", orphan.get()[0].as().getTextField()); EXPECT_EQ("bar", orphan.get()[1].as().getTextField()); EXPECT_FALSE(root.hasAnyPointerField()); root.getAnyPointerField().adopt(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); EXPECT_TRUE(root.hasAnyPointerField()); ASSERT_EQ(2u, root.asReader().getAnyPointerField().getAs>().size()); EXPECT_EQ("foo", root.asReader().getAnyPointerField() .getAs>()[0].getTextField()); EXPECT_EQ("bar", root.asReader().getAnyPointerField() .getAs>()[1].getTextField()); } TEST(Orphans, OrphanageDynamicStruct) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphan(Schema::from()); initDynamicTestMessage(orphan.get()); checkDynamicTestMessage(orphan.getReader()); auto root = builder.initRoot(); root.getAnyPointerField().adopt(kj::mv(orphan)); checkTestMessage(root.asReader().getAnyPointerField().getAs()); } TEST(Orphans, OrphanageDynamicList) { MallocMessageBuilder builder; Orphan orphan = builder.getOrphanage().newOrphan(Schema::from>(), 2); orphan.get().set(0, 123); orphan.get().set(1, 456); checkList(orphan.getReader(), {123, 456}); auto root = builder.initRoot(); root.getAnyPointerField().adopt(kj::mv(orphan)); checkList(root.getAnyPointerField().getAs>(), {123u, 456u}); } TEST(Orphans, OrphanageDynamicStructCopy) { MallocMessageBuilder builder1; MallocMessageBuilder builder2; auto root1 = builder1.initRoot(); initTestMessage(root1.getAnyPointerField().initAs()); Orphan orphan = builder2.getOrphanage().newOrphanCopy( root1.asReader().getAnyPointerField().getAs(Schema::from())); checkDynamicTestMessage(orphan.getReader()); auto root2 = builder2.initRoot(); root2.getAnyPointerField().adopt(kj::mv(orphan)); checkTestMessage(root2.asReader().getAnyPointerField().getAs()); } TEST(Orphans, OrphanageDynamicListCopy) { MallocMessageBuilder builder1; MallocMessageBuilder builder2; auto root1 = builder1.initRoot(); root1.getAnyPointerField().setAs>({12, 34, 56}); Orphan orphan = builder2.getOrphanage().newOrphanCopy( root1.asReader().getAnyPointerField().getAs(Schema::from>())); checkList(orphan.getReader(), {12, 34, 56}); auto root2 = builder2.initRoot(); root2.getAnyPointerField().adopt(kj::mv(orphan)); checkList(root2.getAnyPointerField().getAs>(), {12u, 34u, 56u}); } TEST(Orphans, DynamicStructAs) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root.getAnyPointerField().initAs()); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disownAs(Schema::from()); EXPECT_EQ(DynamicValue::STRUCT, orphan.getType()); checkTestMessage(orphan.getReader().as()); checkTestMessage(orphan.get().as()); { Orphan structOrphan = orphan.releaseAs(); EXPECT_EQ(DynamicValue::UNKNOWN, orphan.getType()); EXPECT_FALSE(structOrphan == nullptr); checkDynamicTestMessage(structOrphan.getReader()); checkDynamicTestMessage(structOrphan.get()); checkTestMessage(structOrphan.getReader().as()); checkTestMessage(structOrphan.get().as()); { Orphan typedOrphan = structOrphan.releaseAs(); EXPECT_TRUE(structOrphan == nullptr); EXPECT_FALSE(typedOrphan == nullptr); checkTestMessage(typedOrphan.getReader()); checkTestMessage(typedOrphan.get()); orphan = kj::mv(typedOrphan); EXPECT_EQ(DynamicValue::STRUCT, orphan.getType()); EXPECT_TRUE(typedOrphan == nullptr); } } { Orphan typedOrphan = orphan.releaseAs(); checkTestMessage(typedOrphan.getReader()); checkTestMessage(typedOrphan.get()); } } TEST(Orphans, DynamicListAs) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getAnyPointerField().setAs>({12, 34, 56}); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disownAs(Schema::from>()); EXPECT_EQ(DynamicValue::LIST, orphan.getType()); checkList(orphan.getReader().as>(), {12, 34, 56}); checkList(orphan.get().as>(), {12, 34, 56}); { Orphan listOrphan = orphan.releaseAs(); EXPECT_EQ(DynamicValue::UNKNOWN, orphan.getType()); EXPECT_FALSE(listOrphan == nullptr); checkList(listOrphan.getReader(), {12, 34, 56}); checkList(listOrphan.get(), {12, 34, 56}); checkList(listOrphan.getReader().as>(), {12, 34, 56}); checkList(listOrphan.get().as>(), {12, 34, 56}); { Orphan> typedOrphan = listOrphan.releaseAs>(); EXPECT_TRUE(listOrphan == nullptr); EXPECT_FALSE(typedOrphan == nullptr); checkList(typedOrphan.getReader(), {12, 34, 56}); checkList(typedOrphan.get(), {12, 34, 56}); orphan = kj::mv(typedOrphan); EXPECT_EQ(DynamicValue::LIST, orphan.getType()); EXPECT_TRUE(typedOrphan == nullptr); } } { Orphan> typedOrphan = orphan.releaseAs>(); checkList(typedOrphan.getReader(), {12, 34, 56}); checkList(typedOrphan.get(), {12, 34, 56}); } } TEST(Orphans, DynamicAnyPointer) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root.getAnyPointerField().initAs()); EXPECT_TRUE(root.hasAnyPointerField()); Orphan orphan = root.getAnyPointerField().disown(); EXPECT_EQ(DynamicValue::ANY_POINTER, orphan.getType()); Orphan objectOrphan = orphan.releaseAs(); checkTestMessage(objectOrphan.getAs()); checkDynamicTestMessage(objectOrphan.getAs(Schema::from())); } TEST(Orphans, DynamicDisown) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root); Orphan dstOrphan = Orphanage::getForMessageContaining(root).newOrphan(); auto dst = dstOrphan.get(); DynamicStruct::Builder dynamic = root; DynamicStruct::Builder dynamicDst = dst; for (auto field: dynamic.getSchema().getFields()) { dynamicDst.adopt(field, dynamic.disown(field)); } checkTestMessageAllZero(root.asReader()); checkTestMessage(dst.asReader()); for (auto field: dynamic.getSchema().getFields()) { dynamicDst.adopt(field, dynamic.disown(field)); } checkTestMessageAllZero(root.asReader()); checkTestMessageAllZero(dst.asReader()); } TEST(Orphans, DynamicDisownGroup) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto bar = root.initGroups().initBar(); bar.setCorge(123); bar.setGrault("foo"); bar.setGarply(9876543210987ll); Orphan dstOrphan = Orphanage::getForMessageContaining(root).newOrphan(); auto dst = dstOrphan.get(); toDynamic(dst).adopt("groups", toDynamic(root).disown("groups")); EXPECT_EQ(test::TestGroups::Groups::FOO, root.getGroups().which()); EXPECT_EQ(test::TestGroups::Groups::BAR, dst.getGroups().which()); auto newBar = dst.getGroups().getBar(); EXPECT_EQ(123, newBar.getCorge()); EXPECT_EQ("foo", newBar.getGrault()); EXPECT_EQ(9876543210987ll, newBar.getGarply()); } #endif // !CAPNP_LITE TEST(Orphans, OrphanageFromBuilder) { MallocMessageBuilder builder; auto root = builder.initRoot(); { Orphanage orphanage = Orphanage::getForMessageContaining(root); Orphan orphan = orphanage.newOrphan(); initTestMessage(orphan.get()); root.adoptStructField(kj::mv(orphan)); checkTestMessage(root.asReader().getStructField()); } { Orphanage orphanage = Orphanage::getForMessageContaining(root.initBoolList(3)); Orphan orphan = orphanage.newOrphan(); initTestMessage(orphan.get()); root.adoptStructField(kj::mv(orphan)); checkTestMessage(root.asReader().getStructField()); } #if !CAPNP_LITE { Orphanage orphanage = Orphanage::getForMessageContaining(toDynamic(root)); Orphan orphan = orphanage.newOrphan(); initTestMessage(orphan.get()); root.adoptStructField(kj::mv(orphan)); checkTestMessage(root.asReader().getStructField()); } { Orphanage orphanage = Orphanage::getForMessageContaining(toDynamic(root.initBoolList(3))); Orphan orphan = orphanage.newOrphan(); initTestMessage(orphan.get()); root.adoptStructField(kj::mv(orphan)); checkTestMessage(root.asReader().getStructField()); } #endif // !CAPNP_LITE } static bool allZero(const word* begin, const word* end) { for (const byte* pos = reinterpret_cast(begin); pos < reinterpret_cast(end); ++pos) { if (*pos != 0) return false; } return true; } TEST(Orphans, StructsZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); initTestMessage(root.initStructField()); const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setTextField("foo"); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownStructField(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, ListsZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); root.setUInt32List({12, 34, 56}); const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setTextField("foo"); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownUInt32List(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, EmptyListsZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); root.initUInt32List(0); const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setTextField("foo"); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownUInt32List(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, StructListsZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); { auto list = root.initStructList(2); initTestMessage(list[0]); initTestMessage(list[1]); } const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setTextField("foo"); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownStructList(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, EmptyStructListsZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); root.initStructList(0); const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setTextField("foo"); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownStructList(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, TextZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); root.setTextField("abcd123"); const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setDataField(data("foo")); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownTextField(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ(data("foo"), root.getDataField()); } TEST(Orphans, DataZerodAfterUse) { MallocMessageBuilder builder; auto root = builder.initRoot(); const word* zerosStart = builder.getSegmentsForOutput()[0].end(); root.setDataField(data("abcd123")); const word* zerosEnd = builder.getSegmentsForOutput()[0].end(); root.setTextField("foo"); // guard against overruns EXPECT_EQ(1u, builder.getSegmentsForOutput().size()); // otherwise test is invalid root.disownDataField(); EXPECT_TRUE(allZero(zerosStart, zerosEnd)); EXPECT_EQ("foo", root.getTextField()); } TEST(Orphans, FarPointer) { MallocMessageBuilder builder(0, AllocationStrategy::FIXED_SIZE); auto root = builder.initRoot(); auto child = root.initStructField(); initTestMessage(child); auto orphan = root.disownStructField(); EXPECT_FALSE(root.hasStructField()); EXPECT_TRUE(orphan != nullptr); EXPECT_FALSE(orphan == nullptr); checkTestMessage(orphan.getReader()); checkTestMessage(orphan.get()); } TEST(Orphans, UpgradeStruct) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto old = root.getAnyPointerField().initAs(); old.setOld1(1234); old.setOld2("foo"); auto orphan = root.getAnyPointerField().disownAs(); // Relocation has not occurred yet. old.setOld1(12345); EXPECT_EQ(12345, orphan.getReader().getOld1()); EXPECT_EQ("foo", old.getOld2()); // This will relocate the struct. auto newVersion = orphan.get(); EXPECT_EQ(0, old.getOld1()); EXPECT_EQ("", old.getOld2()); EXPECT_EQ(12345, newVersion.getOld1()); EXPECT_EQ("foo", newVersion.getOld2()); } TEST(Orphans, UpgradeStructList) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto old = root.getAnyPointerField().initAs>(2); old[0].setOld1(1234); old[0].setOld2("foo"); old[1].setOld1(4321); old[1].setOld2("bar"); auto orphan = root.getAnyPointerField().disownAs>(); // Relocation has not occurred yet. old[0].setOld1(12345); EXPECT_EQ(12345, orphan.getReader()[0].getOld1()); EXPECT_EQ("foo", old[0].getOld2()); // This will relocate the struct. auto newVersion = orphan.get(); EXPECT_EQ(0, old[0].getOld1()); EXPECT_EQ("", old[0].getOld2()); EXPECT_EQ(12345, newVersion[0].getOld1()); EXPECT_EQ("foo", newVersion[0].getOld2()); EXPECT_EQ(4321, newVersion[1].getOld1()); EXPECT_EQ("bar", newVersion[1].getOld2()); } TEST(Orphans, DisownNull) { MallocMessageBuilder builder; auto root = builder.initRoot(); { Orphan orphan = root.disownStructField(); EXPECT_TRUE(orphan == nullptr); checkTestMessageAllZero(orphan.getReader()); EXPECT_TRUE(orphan == nullptr); // get()ing the orphan allocates an object, for security reasons. checkTestMessageAllZero(orphan.get()); EXPECT_FALSE(orphan == nullptr); } { Orphan> orphan = root.disownInt32List(); EXPECT_TRUE(orphan == nullptr); EXPECT_EQ(0, orphan.getReader().size()); EXPECT_TRUE(orphan == nullptr); EXPECT_EQ(0, orphan.get().size()); EXPECT_TRUE(orphan == nullptr); } { Orphan> orphan = root.disownStructList(); EXPECT_TRUE(orphan == nullptr); EXPECT_EQ(0, orphan.getReader().size()); EXPECT_TRUE(orphan == nullptr); EXPECT_EQ(0, orphan.get().size()); EXPECT_TRUE(orphan == nullptr); } } TEST(Orphans, ReferenceExternalData) { MallocMessageBuilder builder; union { word align; byte data[50]; }; memset(data, 0x55, sizeof(data)); auto orphan = builder.getOrphanage().referenceExternalData(Data::Builder(data, sizeof(data))); // Data was added as a new segment. { auto segments = builder.getSegmentsForOutput(); ASSERT_EQ(2, segments.size()); EXPECT_EQ(data, segments[1].asBytes().begin()); EXPECT_EQ((sizeof(data) + 7) / 8, segments[1].size()); } // Can't get builder because it's read-only. EXPECT_ANY_THROW(orphan.get()); // Can get reader. { auto reader = orphan.getReader(); EXPECT_EQ(data, reader.begin()); EXPECT_EQ(sizeof(data), reader.size()); } // Adopt into message tree. auto root = builder.getRoot(); root.adoptDataField(kj::mv(orphan)); // Can't get child builder. EXPECT_ANY_THROW(root.getDataField()); // Can get child reader. { auto reader = root.asReader().getDataField(); EXPECT_EQ(data, reader.begin()); EXPECT_EQ(sizeof(data), reader.size()); } // Back to orphan. orphan = root.disownDataField(); // Now the orphan may be pointing to a far pointer landing pad, so check that it still does the // right things. // Can't get builder because it's read-only. EXPECT_ANY_THROW(orphan.get()); // Can get reader. { auto reader = orphan.getReader(); EXPECT_EQ(data, reader.begin()); EXPECT_EQ(sizeof(data), reader.size()); } // Finally, let's abandon the orphan and check that this doesn't zero out the data. orphan = Orphan(); for (byte b: data) { EXPECT_EQ(0x55, b); } } TEST(Orphans, ReferenceExternalData_NoZeroOnSet) { // Verify that an external blob is not zeroed by setFoo(). union { word align; byte data[50]; }; memset(data, 0x55, sizeof(data)); MallocMessageBuilder builder; auto root = builder.getRoot(); root.adoptDataField(builder.getOrphanage().referenceExternalData( Data::Builder(data, sizeof(data)))); root.setDataField(Data::Builder()); for (byte b: data) { EXPECT_EQ(0x55, b); } } TEST(Orphans, ReferenceExternalData_NoZeroImmediateAbandon) { // Verify that an external blob is not zeroed when abandoned immediately, without ever being // adopted. union { word align; byte data[50]; }; memset(data, 0x55, sizeof(data)); MallocMessageBuilder builder; builder.getOrphanage().referenceExternalData(Data::Builder(data, sizeof(data))); for (byte b: data) { EXPECT_EQ(0x55, b); } } TEST(Orphans, TruncateData) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan(17); auto builder = orphan.get(); memset(builder.begin(), 123, builder.size()); EXPECT_EQ(4, message.getSegmentsForOutput()[0].size()); orphan.truncate(2); EXPECT_EQ(2, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(2, reader.size()); EXPECT_EQ(builder.begin(), reader.begin()); EXPECT_EQ(123, builder[0]); EXPECT_EQ(123, builder[1]); EXPECT_EQ(0, builder[2]); EXPECT_EQ(0, builder[3]); EXPECT_EQ(0, builder[16]); } TEST(Orphans, ExtendData) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan(17); auto builder = orphan.get(); memset(builder.begin(), 123, builder.size()); EXPECT_EQ(4, message.getSegmentsForOutput()[0].size()); orphan.truncate(27); EXPECT_EQ(5, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(27, reader.size()); EXPECT_EQ(builder.begin(), reader.begin()); for (uint i = 0; i < 17; i++) { EXPECT_EQ(123, reader[i]); } for (uint i = 17; i < 27; i++) { EXPECT_EQ(0, reader[i]); } } TEST(Orphans, ExtendDataCopy) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan(17); auto builder = orphan.get(); memset(builder.begin(), 123, builder.size()); auto orphan2 = message.getOrphanage().newOrphan(1); orphan2.get()[0] = 32; orphan.truncate(27); auto reader = orphan.getReader(); EXPECT_EQ(27, reader.size()); EXPECT_NE(builder.begin(), reader.begin()); for (uint i = 0; i < 17; i++) { EXPECT_EQ(123, reader[i]); EXPECT_EQ(0, builder[i]); } for (uint i = 17; i < 27; i++) { EXPECT_EQ(0, reader[i]); } EXPECT_EQ(32, orphan2.getReader()[0]); } TEST(Orphans, ExtendDataFromEmpty) { MallocMessageBuilder message; auto orphan = message.initRoot().disownDataField(); orphan.truncate(3); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ(0, reader[i]); } } TEST(Orphans, TruncateText) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan(17); auto builder = orphan.get(); memset(builder.begin(), 'a', builder.size()); EXPECT_EQ(4, message.getSegmentsForOutput()[0].size()); orphan.truncate(2); EXPECT_EQ(2, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(2, reader.size()); EXPECT_EQ(builder.begin(), reader.begin()); EXPECT_EQ('a', builder[0]); EXPECT_EQ('a', builder[1]); EXPECT_EQ('\0', builder[2]); EXPECT_EQ('\0', builder[3]); EXPECT_EQ('\0', builder[16]); } TEST(Orphans, ExtendText) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan(17); auto builder = orphan.get(); memset(builder.begin(), 'a', builder.size()); EXPECT_EQ(4, message.getSegmentsForOutput()[0].size()); orphan.truncate(27); EXPECT_EQ(5, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(27, reader.size()); EXPECT_EQ(builder.begin(), reader.begin()); for (uint i = 0; i < 17; i++) { EXPECT_EQ('a', reader[i]); } for (uint i = 17; i < 27; i++) { EXPECT_EQ('\0', reader[i]); } } TEST(Orphans, ExtendTextCopy) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan(17); auto builder = orphan.get(); memset(builder.begin(), 'a', builder.size()); auto orphan2 = message.getOrphanage().newOrphan(1); orphan2.get()[0] = 32; orphan.truncate(27); auto reader = orphan.getReader(); EXPECT_EQ(27, reader.size()); EXPECT_NE(builder.begin(), reader.begin()); for (uint i = 0; i < 17; i++) { EXPECT_EQ('a', reader[i]); EXPECT_EQ('\0', builder[i]); } for (uint i = 17; i < 27; i++) { EXPECT_EQ('\0', reader[i]); } EXPECT_EQ(32, orphan2.getReader()[0]); } TEST(Orphans, ExtendTextFromEmpty) { MallocMessageBuilder message; auto orphan = message.initRoot().disownTextField(); orphan.truncate(3); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ('\0', reader[i]); } } TEST(Orphans, TruncatePrimitiveList) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.set(i, 123456789 + i); } EXPECT_EQ(5, message.getSegmentsForOutput()[0].size()); orphan.truncate(3); EXPECT_EQ(3, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ(123456789 + i, builder[i]); EXPECT_EQ(123456789 + i, reader[i]); } for (uint i = 3; i < 7; i++) { EXPECT_EQ(0, builder[i]); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder.set(0, 321); EXPECT_EQ(321, reader[0]); } TEST(Orphans, ExtendPrimitiveList) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.set(i, 123456789 + i); } EXPECT_EQ(5, message.getSegmentsForOutput()[0].size()); orphan.truncate(11); EXPECT_EQ(7, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(11, reader.size()); for (uint i = 0; i < 7; i++) { EXPECT_EQ(123456789 + i, reader[i]); EXPECT_EQ(123456789 + i, builder[i]); } for (uint i = 7; i < 11; i++) { EXPECT_EQ(0, reader[i]); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder.set(0, 321); EXPECT_EQ(321, reader[0]); } TEST(Orphans, ExtendPrimitiveListCopy) { MallocMessageBuilder message; auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.set(i, 123456789 + i); } auto orphan2 = message.getOrphanage().newOrphan(1); orphan2.get()[0] = 32; orphan.truncate(11); auto reader = orphan.getReader(); EXPECT_EQ(11, reader.size()); for (uint i = 0; i < 7; i++) { EXPECT_EQ(123456789 + i, reader[i]); EXPECT_EQ(0, builder[i]); } for (uint i = 7; i < 11; i++) { EXPECT_EQ(0, reader[i]); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder.set(0, 321); EXPECT_EQ(123456789, reader[0]); EXPECT_EQ(32, orphan2.getReader()[0]); } TEST(Orphans, ExtendPointerListFromEmpty) { MallocMessageBuilder message; auto orphan = message.initRoot().disownUInt32List(); orphan.truncate(3); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ(0, reader[i]); } } TEST(Orphans, TruncatePointerList) { MallocMessageBuilder message; // Allocate data in advance so that the list itself is at the end of the segment. kj::Vector> pointers(7); for (uint i = 0; i < 7; i++) { pointers.add(message.getOrphanage().newOrphanCopy(Text::Reader(kj::str("foo", i)))); } size_t sizeBeforeList = message.getSegmentsForOutput()[0].size(); auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.adopt(i, kj::mv(pointers[i])); } EXPECT_EQ(sizeBeforeList + 7, message.getSegmentsForOutput()[0].size()); orphan.truncate(3); EXPECT_EQ(sizeBeforeList + 3, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ(kj::str("foo", i), builder[i]); EXPECT_EQ(kj::str("foo", i), reader[i]); } for (uint i = 3; i < 7; i++) { EXPECT_TRUE(builder[i] == nullptr); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder.set(0, "bar"); EXPECT_EQ("bar", reader[0]); } TEST(Orphans, ExtendPointerList) { MallocMessageBuilder message; // Allocate data in advance so that the list itself is at the end of the segment. kj::Vector> pointers(7); for (uint i = 0; i < 7; i++) { pointers.add(message.getOrphanage().newOrphanCopy(Text::Reader(kj::str("foo", i)))); } size_t sizeBeforeList = message.getSegmentsForOutput()[0].size(); auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.adopt(i, kj::mv(pointers[i])); } EXPECT_EQ(sizeBeforeList + 7, message.getSegmentsForOutput()[0].size()); orphan.truncate(11); EXPECT_EQ(sizeBeforeList + 11, message.getSegmentsForOutput()[0].size()); auto reader = orphan.getReader(); EXPECT_EQ(11, reader.size()); for (uint i = 0; i < 7; i++) { EXPECT_EQ(kj::str("foo", i), reader[i]); EXPECT_EQ(kj::str("foo", i), builder[i]); } for (uint i = 7; i < 11; i++) { EXPECT_TRUE(reader[i] == nullptr); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder.set(0, "bar"); EXPECT_EQ("bar", reader[0]); } TEST(Orphans, ExtendPointerListCopy) { MallocMessageBuilder message; // Allocate data in advance so that the list itself is at the end of the segment. kj::Vector> pointers(7); for (uint i = 0; i < 7; i++) { pointers.add(message.getOrphanage().newOrphanCopy(Text::Reader(kj::str("foo", i)))); } auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.adopt(i, kj::mv(pointers[i])); } auto orphan2 = message.getOrphanage().newOrphan(1); orphan2.get()[0] = 32; orphan.truncate(11); auto reader = orphan.getReader(); EXPECT_EQ(11, reader.size()); for (uint i = 0; i < 7; i++) { EXPECT_EQ(kj::str("foo", i), reader[i]); EXPECT_TRUE(builder[i] == nullptr); } for (uint i = 7; i < 11; i++) { EXPECT_TRUE(reader[i] == nullptr); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder.set(0, "bar"); EXPECT_EQ("foo0", reader[0]); EXPECT_EQ(32, orphan2.getReader()[0]); } TEST(Orphans, ExtendPointerListFromEmpty) { MallocMessageBuilder message; auto orphan = message.initRoot().disownTextList(); orphan.truncate(3); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ("", reader[i]); } } TEST(Orphans, TruncateStructList) { MallocMessageBuilder message; // Allocate data in advance so that the list itself is at the end of the segment. kj::Vector> pointers(7); for (uint i = 0; i < 7; i++) { auto o = message.getOrphanage().newOrphan(); auto b = o.get(); b.setUInt32Field(123456789 + i); b.setTextField(kj::str("foo", i)); b.setUInt8List({123, 45}); pointers.add(kj::mv(o)); } auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.adoptWithCaveats(i, kj::mv(pointers[i])); } size_t sizeBeforeTruncate = message.getSegmentsForOutput()[0].size(); orphan.truncate(3); EXPECT_LT(message.getSegmentsForOutput()[0].size(), sizeBeforeTruncate); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { EXPECT_EQ(123456789 + i, reader[i].getUInt32Field()); EXPECT_EQ(kj::str("foo", i), reader[i].getTextField()); checkList(reader[i].getUInt8List(), {123, 45}); EXPECT_EQ(123456789 + i, builder[i].getUInt32Field()); EXPECT_EQ(kj::str("foo", i), builder[i].getTextField()); checkList(builder[i].getUInt8List(), {123, 45}); } for (uint i = 3; i < 7; i++) { checkTestMessageAllZero(builder[i]); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder[0].setUInt32Field(321); EXPECT_EQ(321, reader[0].getUInt32Field()); } TEST(Orphans, ExtendStructList) { MallocMessageBuilder message; // Allocate data in advance so that the list itself is at the end of the segment. kj::Vector> pointers(7); for (uint i = 0; i < 7; i++) { auto o = message.getOrphanage().newOrphan(); auto b = o.get(); b.setUInt32Field(123456789 + i); b.setTextField(kj::str("foo", i)); b.setUInt8List({123, 45}); pointers.add(kj::mv(o)); } auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.adoptWithCaveats(i, kj::mv(pointers[i])); } orphan.truncate(11); auto reader = orphan.getReader(); EXPECT_EQ(11, reader.size()); for (uint i = 0; i < 7; i++) { EXPECT_EQ(123456789 + i, reader[i].getUInt32Field()); EXPECT_EQ(kj::str("foo", i), reader[i].getTextField()); checkList(reader[i].getUInt8List(), {123, 45}); EXPECT_EQ(123456789 + i, builder[i].getUInt32Field()); EXPECT_EQ(kj::str("foo", i), builder[i].getTextField()); checkList(builder[i].getUInt8List(), {123, 45}); } for (uint i = 7; i < 11; i++) { checkTestMessageAllZero(reader[i]); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder[0].setUInt32Field(321); EXPECT_EQ(321, reader[0].getUInt32Field()); } TEST(Orphans, ExtendStructListCopy) { MallocMessageBuilder message; // Allocate data in advance so that the list itself is at the end of the segment. kj::Vector> pointers(7); for (uint i = 0; i < 7; i++) { auto o = message.getOrphanage().newOrphan(); auto b = o.get(); b.setUInt32Field(123456789 + i); b.setTextField(kj::str("foo", i)); b.setUInt8List({123, 45}); pointers.add(kj::mv(o)); } auto orphan = message.getOrphanage().newOrphan>(7); auto builder = orphan.get(); for (uint i = 0; i < 7; i++) { builder.adoptWithCaveats(i, kj::mv(pointers[i])); } auto orphan2 = message.getOrphanage().newOrphan(1); orphan2.get()[0] = 32; orphan.truncate(11); auto reader = orphan.getReader(); EXPECT_EQ(11, reader.size()); for (uint i = 0; i < 7; i++) { EXPECT_EQ(123456789 + i, reader[i].getUInt32Field()); EXPECT_EQ(kj::str("foo", i), reader[i].getTextField()); checkList(reader[i].getUInt8List(), {123, 45}); checkTestMessageAllZero(builder[i]); } for (uint i = 7; i < 11; i++) { checkTestMessageAllZero(reader[i]); } // Can't compare pointers directly, but we can check if builder modifications are visible using // the reader. builder[0].setUInt32Field(321); EXPECT_EQ(123456789, reader[0].getUInt32Field()); EXPECT_EQ(32, orphan2.getReader()[0]); } TEST(Orphans, ExtendStructListFromEmpty) { MallocMessageBuilder message; auto orphan = message.initRoot().disownStructList(); orphan.truncate(3); auto reader = orphan.getReader(); EXPECT_EQ(3, reader.size()); for (uint i = 0; i < 3; i++) { checkTestMessageAllZero(reader[i]); } } template void initList(ListBuilder builder, std::initializer_list>>> values) { KJ_ASSERT(builder.size() == values.size()); size_t i = 0; for (auto& value: values) { builder.set(i++, value); } } TEST(Orphans, ConcatenatePrimitiveLists) { MallocMessageBuilder message; auto orphanage = message.getOrphanage(); auto list1 = orphanage.newOrphan>(3); initList(list1.get(), {12, 34, 56}); auto list2 = orphanage.newOrphan>(2); initList(list2.get(), {78, 90}); auto list3 = orphanage.newOrphan>(4); initList(list3.get(), {23, 45, 67, 89}); List::Reader lists[] = { list1.getReader(), list2.getReader(), list3.getReader() }; kj::ArrayPtr::Reader> array = lists; auto cat = message.getOrphanage().newOrphanConcat(array); checkList(cat.getReader(), {12, 34, 56, 78, 90, 23, 45, 67, 89}); } TEST(Orphans, ConcatenateBitLists) { MallocMessageBuilder message; auto orphanage = message.getOrphanage(); auto list1 = orphanage.newOrphan>(3); initList(list1.get(), {true, true, false}); auto list2 = orphanage.newOrphan>(2); initList(list2.get(), {false, true}); auto list3 = orphanage.newOrphan>(4); initList(list3.get(), {false, false, true, false}); List::Reader lists[] = { list1.getReader(), list2.getReader(), list3.getReader() }; kj::ArrayPtr::Reader> array = lists; auto cat = message.getOrphanage().newOrphanConcat(array); checkList(cat.getReader(), {true, true, false, false, true, false, false, true, false}); } TEST(Orphans, ConcatenatePointerLists) { MallocMessageBuilder message; auto orphanage = message.getOrphanage(); auto list1 = orphanage.newOrphan>(3); initList(list1.get(), {"foo", "bar", "baz"}); auto list2 = orphanage.newOrphan>(2); initList(list2.get(), {"qux", "corge"}); auto list3 = orphanage.newOrphan>(4); initList(list3.get(), {"grault", "garply", "waldo", "fred"}); List::Reader lists[] = { list1.getReader(), list2.getReader(), list3.getReader() }; kj::ArrayPtr::Reader> array = lists; auto cat = message.getOrphanage().newOrphanConcat(array); checkList(cat.getReader(), { "foo", "bar", "baz", "qux", "corge", "grault", "garply", "waldo", "fred"}); } TEST(Orphans, ConcatenateStructLists) { // In this test, we not only concatenate two struct lists, but we concatenate in a list that // contains a newer-than-expected version of the struct with extra fields, in order to verify // that the new fields aren't lost. MallocMessageBuilder message; auto orphanage = message.getOrphanage(); auto orphan1 = orphanage.newOrphan>(2); auto list1 = orphan1.get(); list1[0].setOld1(12); list1[0].setOld2("foo"); list1[1].setOld1(34); list1[1].setOld2("bar"); auto orphan2 = orphanage.newOrphan(); auto list2 = orphan2.get().getAnyPointerField().initAs>(2); list2[0].setOld1(56); list2[0].setOld2("baz"); list2[0].setNew1(123); list2[0].setNew2("corge"); list2[1].setOld1(78); list2[1].setOld2("qux"); list2[1].setNew1(456); list2[1].setNew2("grault"); List::Reader lists[] = { orphan1.getReader(), orphan2.getReader().getAnyPointerField().getAs>() }; kj::ArrayPtr::Reader> array = lists; auto orphan3 = orphanage.newOrphan(); orphan3.get().getAnyPointerField().adopt(message.getOrphanage().newOrphanConcat(array)); auto cat = orphan3.getReader().getAnyPointerField().getAs>(); ASSERT_EQ(4, cat.size()); EXPECT_EQ(12, cat[0].getOld1()); EXPECT_EQ("foo", cat[0].getOld2()); EXPECT_EQ(987, cat[0].getNew1()); EXPECT_FALSE(cat[0].hasNew2()); EXPECT_EQ(34, cat[1].getOld1()); EXPECT_EQ("bar", cat[1].getOld2()); EXPECT_EQ(987, cat[1].getNew1()); EXPECT_FALSE(cat[1].hasNew2()); EXPECT_EQ(56, cat[2].getOld1()); EXPECT_EQ("baz", cat[2].getOld2()); EXPECT_EQ(123, cat[2].getNew1()); EXPECT_EQ("corge", cat[2].getNew2()); EXPECT_EQ(78, cat[3].getOld1()); EXPECT_EQ("qux", cat[3].getOld2()); EXPECT_EQ(456, cat[3].getNew1()); EXPECT_EQ("grault", cat[3].getNew2()); } TEST(Orphans, ConcatenateStructListsUpgradeFromPrimitive) { // Like above, but we're "upgrading" a primitive list to a struct list. MallocMessageBuilder message; auto orphanage = message.getOrphanage(); auto orphan1 = orphanage.newOrphan>(2); auto list1 = orphan1.get(); list1[0].setOld1(12); list1[0].setOld2("foo"); list1[1].setOld1(34); list1[1].setOld2("bar"); auto orphan2 = orphanage.newOrphan(); auto list2 = orphan2.get().getAnyPointerField().initAs>(2); initList(list2, {12345, 67890}); List::Reader lists[] = { orphan1.getReader(), orphan2.getReader().getAnyPointerField().getAs>() }; kj::ArrayPtr::Reader> array = lists; auto orphan3 = message.getOrphanage().newOrphanConcat(array); auto cat = orphan3.getReader(); ASSERT_EQ(4, cat.size()); EXPECT_EQ(12, cat[0].getOld1()); EXPECT_EQ("foo", cat[0].getOld2()); EXPECT_EQ(34, cat[1].getOld1()); EXPECT_EQ("bar", cat[1].getOld2()); EXPECT_EQ(12345, cat[2].getOld1()); EXPECT_FALSE(cat[2].hasOld2()); EXPECT_EQ(67890, cat[3].getOld1()); EXPECT_FALSE(cat[3].hasOld2()); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/serialize-test.c++0000644000175000017500000003541513650101756022112 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "serialize.h" #include #include #include #include #include #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { class TestMessageBuilder: public MallocMessageBuilder { // A MessageBuilder that tries to allocate an exact number of total segments, by allocating // minimum-size segments until it reaches the number, then allocating one large segment to // finish. public: explicit TestMessageBuilder(uint desiredSegmentCount) : MallocMessageBuilder(0, AllocationStrategy::FIXED_SIZE), desiredSegmentCount(desiredSegmentCount) {} ~TestMessageBuilder() { EXPECT_EQ(0u, desiredSegmentCount); } kj::ArrayPtr allocateSegment(uint minimumSize) override { if (desiredSegmentCount <= 1) { if (desiredSegmentCount < 1) { ADD_FAILURE() << "Allocated more segments than desired."; } else { --desiredSegmentCount; } return MallocMessageBuilder::allocateSegment(SUGGESTED_FIRST_SEGMENT_WORDS); } else { --desiredSegmentCount; return MallocMessageBuilder::allocateSegment(minimumSize); } } private: uint desiredSegmentCount; }; kj::Array copyWords(kj::ArrayPtr input) { auto result = kj::heapArray(input.size()); memcpy(result.asBytes().begin(), input.asBytes().begin(), input.asBytes().size()); return result; } TEST(Serialize, FlatArray) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); { FlatArrayMessageReader reader(serialized.asPtr()); checkTestMessage(reader.getRoot()); EXPECT_EQ(serialized.end(), reader.getEnd()); } { MallocMessageBuilder builder2; auto remaining = initMessageBuilderFromFlatArrayCopy(serialized, builder2); checkTestMessage(builder2.getRoot()); EXPECT_EQ(serialized.end(), remaining.begin()); EXPECT_EQ(serialized.end(), remaining.end()); } kj::Array serializedWithSuffix = kj::heapArray(serialized.size() + 5); memcpy(serializedWithSuffix.begin(), serialized.begin(), serialized.size() * sizeof(word)); { FlatArrayMessageReader reader(serializedWithSuffix.asPtr()); checkTestMessage(reader.getRoot()); EXPECT_EQ(serializedWithSuffix.end() - 5, reader.getEnd()); } { MallocMessageBuilder builder2; auto remaining = initMessageBuilderFromFlatArrayCopy(serializedWithSuffix, builder2); checkTestMessage(builder2.getRoot()); EXPECT_EQ(serializedWithSuffix.end() - 5, remaining.begin()); EXPECT_EQ(serializedWithSuffix.end(), remaining.end()); } { // Test expectedSizeInWordsFromPrefix(). We pass in a copy of the slice so that valgrind can // detect out-of-bounds access. EXPECT_EQ(1, expectedSizeInWordsFromPrefix(copyWords(serialized.slice(0, 0)))); for (uint i = 1; i <= serialized.size(); i++) { EXPECT_EQ(serialized.size(), expectedSizeInWordsFromPrefix(copyWords(serialized.slice(0, i)))); } } } TEST(Serialize, FlatArrayOddSegmentCount) { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); { FlatArrayMessageReader reader(serialized.asPtr()); checkTestMessage(reader.getRoot()); EXPECT_EQ(serialized.end(), reader.getEnd()); } kj::Array serializedWithSuffix = kj::heapArray(serialized.size() + 5); memcpy(serializedWithSuffix.begin(), serialized.begin(), serialized.size() * sizeof(word)); { FlatArrayMessageReader reader(serializedWithSuffix.asPtr()); checkTestMessage(reader.getRoot()); EXPECT_EQ(serializedWithSuffix.end() - 5, reader.getEnd()); } { // Test expectedSizeInWordsFromPrefix(). We pass in a copy of the slice so that valgrind can // detect out-of-bounds access. // Segment table is 4 words, so with fewer words we'll have incomplete information. for (uint i = 0; i < 4; i++) { size_t expectedSize = expectedSizeInWordsFromPrefix(copyWords(serialized.slice(0, i))); EXPECT_LT(expectedSize, serialized.size()); EXPECT_GT(expectedSize, i); } // After that, we get the exact length. for (uint i = 4; i <= serialized.size(); i++) { EXPECT_EQ(serialized.size(), expectedSizeInWordsFromPrefix(copyWords(serialized.slice(0, i)))); } } } TEST(Serialize, FlatArrayEvenSegmentCount) { TestMessageBuilder builder(10); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); { FlatArrayMessageReader reader(serialized.asPtr()); checkTestMessage(reader.getRoot()); EXPECT_EQ(serialized.end(), reader.getEnd()); } kj::Array serializedWithSuffix = kj::heapArray(serialized.size() + 5); memcpy(serializedWithSuffix.begin(), serialized.begin(), serialized.size() * sizeof(word)); { FlatArrayMessageReader reader(serializedWithSuffix.asPtr()); checkTestMessage(reader.getRoot()); EXPECT_EQ(serializedWithSuffix.end() - 5, reader.getEnd()); } { // Test expectedSizeInWordsFromPrefix(). We pass in a copy of the slice so that valgrind can // detect out-of-bounds access. // Segment table is 6 words, so with fewer words we'll have incomplete information. for (uint i = 0; i < 6; i++) { size_t expectedSize = expectedSizeInWordsFromPrefix(copyWords(serialized.slice(0, i))); EXPECT_LT(expectedSize, serialized.size()); EXPECT_GT(expectedSize, i); } // After that, we get the exact length. for (uint i = 6; i <= serialized.size(); i++) { EXPECT_EQ(serialized.size(), expectedSizeInWordsFromPrefix(copyWords(serialized.slice(0, i)))); } } } class TestInputStream: public kj::InputStream { public: TestInputStream(kj::ArrayPtr data, bool lazy) : pos(data.asChars().begin()), end(data.asChars().end()), lazy(lazy) {} ~TestInputStream() {} size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { KJ_ASSERT(maxBytes <= size_t(end - pos), "Overran end of stream."); size_t amount = lazy ? minBytes : maxBytes; memcpy(buffer, pos, amount); pos += amount; return amount; } private: const char* pos; const char* end; bool lazy; }; TEST(Serialize, InputStream) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), false); InputStreamMessageReader reader(stream, ReaderOptions()); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamScratchSpace) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); word scratch[4096]; TestInputStream stream(serialized.asPtr(), false); InputStreamMessageReader reader(stream, ReaderOptions(), kj::ArrayPtr(scratch, 4096)); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamLazy) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), true); InputStreamMessageReader reader(stream, ReaderOptions()); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamOddSegmentCount) { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), false); InputStreamMessageReader reader(stream, ReaderOptions()); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamOddSegmentCountLazy) { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), true); InputStreamMessageReader reader(stream, ReaderOptions()); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamEvenSegmentCount) { TestMessageBuilder builder(10); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), false); InputStreamMessageReader reader(stream, ReaderOptions()); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamEvenSegmentCountLazy) { TestMessageBuilder builder(10); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), true); InputStreamMessageReader reader(stream, ReaderOptions()); checkTestMessage(reader.getRoot()); } TEST(Serialize, InputStreamToBuilder) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestInputStream stream(serialized.asPtr(), false); MallocMessageBuilder builder2; readMessageCopy(stream, builder2); checkTestMessage(builder2.getRoot()); } class TestOutputStream: public kj::OutputStream { public: TestOutputStream() {} ~TestOutputStream() {} void write(const void* buffer, size_t size) override { data.append(reinterpret_cast(buffer), size); } bool dataEquals(kj::ArrayPtr other) { return data == std::string(other.asChars().begin(), other.asChars().size()); } private: std::string data; }; TEST(Serialize, WriteMessage) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestOutputStream output; writeMessage(output, builder); EXPECT_TRUE(output.dataEquals(serialized.asPtr())); } TEST(Serialize, WriteMessageOddSegmentCount) { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestOutputStream output; writeMessage(output, builder); EXPECT_TRUE(output.dataEquals(serialized.asPtr())); } TEST(Serialize, WriteMessageEvenSegmentCount) { TestMessageBuilder builder(10); initTestMessage(builder.initRoot()); kj::Array serialized = messageToFlatArray(builder); TestOutputStream output; writeMessage(output, builder); EXPECT_TRUE(output.dataEquals(serialized.asPtr())); } #if _WIN32 int mkstemp(char *tpl) { char* end = tpl + strlen(tpl); while (end > tpl && *(end-1) == 'X') --end; for (;;) { KJ_ASSERT(_mktemp(tpl) == tpl); int fd = open(tpl, O_RDWR | O_CREAT | O_EXCL | O_TEMPORARY | O_BINARY, 0700); if (fd >= 0) { return fd; } int error = errno; if (error != EEXIST && error != EINTR) { KJ_FAIL_SYSCALL("open(mktemp())", error, tpl); } memset(end, 'X', strlen(end)); } } #endif TEST(Serialize, FileDescriptors) { #if _WIN32 || __ANDROID__ // TODO(cleanup): Find the Windows temp directory? Seems overly difficult. char filename[] = "capnproto-serialize-test-XXXXXX"; #else char filename[] = "/tmp/capnproto-serialize-test-XXXXXX"; #endif kj::AutoCloseFd tmpfile(mkstemp(filename)); ASSERT_GE(tmpfile.get(), 0); #if !_WIN32 // Unlink the file so that it will be deleted on close. // (For win32, we already handled this is mkstemp().) EXPECT_EQ(0, unlink(filename)); #endif { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); writeMessageToFd(tmpfile.get(), builder); } { TestMessageBuilder builder(1); builder.initRoot().setTextField("second message in file"); writeMessageToFd(tmpfile.get(), builder); } lseek(tmpfile, 0, SEEK_SET); { StreamFdMessageReader reader(tmpfile.get()); checkTestMessage(reader.getRoot()); } { StreamFdMessageReader reader(tmpfile.get()); EXPECT_EQ("second message in file", reader.getRoot().getTextField()); } } TEST(Serialize, RejectTooManySegments) { kj::Array data = kj::heapArray(8192); WireValue* table = reinterpret_cast*>(data.begin()); table[0].set(1024); for (uint i = 0; i < 1024; i++) { table[i+1].set(1); } TestInputStream input(data.asPtr(), false); kj::Maybe e = kj::runCatchingExceptions([&]() { InputStreamMessageReader reader(input); #if !KJ_NO_EXCEPTIONS ADD_FAILURE() << "Should have thrown an exception."; #endif }); KJ_EXPECT(e != nullptr, "Should have thrown an exception."); } #if !__MINGW32__ // Inexplicably crashes when exception is thrown from constructor. TEST(Serialize, RejectHugeMessage) { // A message whose root struct contains two words of data! AlignedData<4> data = {{0,0,0,0,3,0,0,0, 0,0,0,0,2,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0}}; TestInputStream input(kj::arrayPtr(data.words, 4), false); // We'll set the traversal limit to 2 words so our 3-word message is too big. ReaderOptions options; options.traversalLimitInWords = 2; kj::Maybe e = kj::runCatchingExceptions([&]() { InputStreamMessageReader reader(input, options); #if !KJ_NO_EXCEPTIONS ADD_FAILURE() << "Should have thrown an exception."; #endif }); KJ_EXPECT(e != nullptr, "Should have thrown an exception."); } #endif // !__MINGW32__ // TODO(test): Test error cases. } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/serialize-packed-test.c++0000644000175000017500000004435213340402540023326 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "serialize-packed.h" #include #include #include #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { class TestPipe: public kj::BufferedInputStream, public kj::OutputStream { public: TestPipe() : preferredReadSize(kj::maxValue), readPos(0) {} explicit TestPipe(size_t preferredReadSize) : preferredReadSize(preferredReadSize), readPos(0) {} ~TestPipe() {} const std::string& getData() { return data; } kj::ArrayPtr getArray() { return kj::arrayPtr(reinterpret_cast(data.data()), data.size()); } void resetRead(size_t preferredReadSize = kj::maxValue) { readPos = 0; this->preferredReadSize = preferredReadSize; } bool allRead() { return readPos == data.size(); } void clear(size_t preferredReadSize = kj::maxValue) { resetRead(preferredReadSize); data.clear(); } void write(const void* buffer, size_t size) override { data.append(reinterpret_cast(buffer), size); } size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { KJ_ASSERT(maxBytes <= data.size() - readPos, "Overran end of stream."); size_t amount = kj::min(maxBytes, kj::max(minBytes, preferredReadSize)); memcpy(buffer, data.data() + readPos, amount); readPos += amount; return amount; } void skip(size_t bytes) override { KJ_ASSERT(bytes <= data.size() - readPos, "Overran end of stream."); readPos += bytes; } kj::ArrayPtr tryGetReadBuffer() override { size_t amount = kj::min(data.size() - readPos, preferredReadSize); return kj::arrayPtr(reinterpret_cast(data.data() + readPos), amount); } private: size_t preferredReadSize; std::string data; std::string::size_type readPos; }; void expectPacksTo(kj::ArrayPtr unpackedUnaligned, kj::ArrayPtr packed) { TestPipe pipe; auto unpackedSizeInWords = computeUnpackedSizeInWords(packed); EXPECT_EQ(unpackedUnaligned.size(), unpackedSizeInWords * sizeof(word)); // Make a guaranteed-to-be-aligned copy of the unpacked buffer. kj::Array unpackedWords = kj::heapArray(unpackedSizeInWords); if (unpackedUnaligned.size() != 0u) { memcpy(unpackedWords.begin(), unpackedUnaligned.begin(), unpackedUnaligned.size()); } kj::ArrayPtr unpacked = unpackedWords.asBytes(); // ----------------------------------------------------------------- // write { kj::BufferedOutputStreamWrapper bufferedOut(pipe); PackedOutputStream packedOut(bufferedOut); packedOut.write(unpacked.begin(), unpacked.size()); } if (pipe.getData() != std::string(packed.asChars().begin(), packed.asChars().size())) { KJ_FAIL_ASSERT("Tried to pack `unpacked`, expected `packed`, got `pipe.getData()`", unpacked, packed, pipe.getData()); return; } // ----------------------------------------------------------------- // read kj::Array roundTrip = kj::heapArray(unpacked.size()); { PackedInputStream packedIn(pipe); packedIn.InputStream::read(roundTrip.begin(), roundTrip.size()); EXPECT_TRUE(pipe.allRead()); } if (memcmp(roundTrip.begin(), unpacked.begin(), unpacked.size()) != 0) { KJ_FAIL_ASSERT("Tried to unpack `packed`, expected `unpacked`, got `roundTrip`", packed, unpacked, roundTrip); return; } for (uint blockSize = 1; blockSize < packed.size(); blockSize <<= 1) { pipe.resetRead(blockSize); { PackedInputStream packedIn(pipe); packedIn.InputStream::read(roundTrip.begin(), roundTrip.size()); EXPECT_TRUE(pipe.allRead()); } if (memcmp(roundTrip.begin(), unpacked.begin(), unpacked.size()) != 0) { KJ_FAIL_ASSERT("Tried to unpack `packed`, expected `unpacked`, got `roundTrip`", packed, blockSize, unpacked, roundTrip); } } // ----------------------------------------------------------------- // skip pipe.resetRead(); { PackedInputStream packedIn(pipe); packedIn.skip(unpacked.size()); EXPECT_TRUE(pipe.allRead()); } for (uint blockSize = 1; blockSize < packed.size(); blockSize <<= 1) { pipe.resetRead(blockSize); { PackedInputStream packedIn(pipe); packedIn.skip(unpacked.size()); EXPECT_TRUE(pipe.allRead()); } } pipe.clear(); // ----------------------------------------------------------------- // write / read multiple { kj::BufferedOutputStreamWrapper bufferedOut(pipe); PackedOutputStream packedOut(bufferedOut); for (uint i = 0; i < 5; i++) { packedOut.write(unpacked.begin(), unpacked.size()); } } for (uint i = 0; i < 5; i++) { PackedInputStream packedIn(pipe); packedIn.InputStream::read(&*roundTrip.begin(), roundTrip.size()); if (memcmp(roundTrip.begin(), unpacked.begin(), unpacked.size()) != 0) { KJ_FAIL_ASSERT("Tried to unpack `packed`, expected `unpacked`, got `roundTrip`", packed, i, unpacked, roundTrip); } } EXPECT_TRUE(pipe.allRead()); } #ifdef __CDT_PARSER__ // CDT doesn't seem to understand these initializer lists. #define expectPacksTo(...) #endif TEST(Packed, SimplePacking) { expectPacksTo({}, {}); expectPacksTo({0,0,0,0,0,0,0,0}, {0,0}); expectPacksTo({0,0,12,0,0,34,0,0}, {0x24,12,34}); expectPacksTo({1,3,2,4,5,7,6,8}, {0xff,1,3,2,4,5,7,6,8,0}); expectPacksTo({0,0,0,0,0,0,0,0,1,3,2,4,5,7,6,8}, {0,0,0xff,1,3,2,4,5,7,6,8,0}); expectPacksTo({0,0,12,0,0,34,0,0,1,3,2,4,5,7,6,8}, {0x24,12,34,0xff,1,3,2,4,5,7,6,8,0}); expectPacksTo({1,3,2,4,5,7,6,8,8,6,7,4,5,2,3,1}, {0xff,1,3,2,4,5,7,6,8,1,8,6,7,4,5,2,3,1}); expectPacksTo( {1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 0,2,4,0,9,0,5,1}, {0xff,1,2,3,4,5,6,7,8, 3, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 0xd6,2,4,9,5,1}); expectPacksTo( {1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 6,2,4,3,9,0,5,1, 1,2,3,4,5,6,7,8, 0,2,4,0,9,0,5,1}, {0xff,1,2,3,4,5,6,7,8, 3, 1,2,3,4,5,6,7,8, 6,2,4,3,9,0,5,1, 1,2,3,4,5,6,7,8, 0xd6,2,4,9,5,1}); expectPacksTo( {8,0,100,6,0,1,1,2, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,1,0,2,0,3,1}, {0xed,8,100,6,1,1,2, 0,2, 0xd4,1,2,3,1}); } // ======================================================================================= class TestMessageBuilder: public MallocMessageBuilder { // A MessageBuilder that tries to allocate an exact number of total segments, by allocating // minimum-size segments until it reaches the number, then allocating one large segment to // finish. public: explicit TestMessageBuilder(uint desiredSegmentCount) : MallocMessageBuilder(0, AllocationStrategy::FIXED_SIZE), desiredSegmentCount(desiredSegmentCount) {} ~TestMessageBuilder() { EXPECT_EQ(0u, desiredSegmentCount); } kj::ArrayPtr allocateSegment(uint minimumSize) override { if (desiredSegmentCount <= 1) { if (desiredSegmentCount < 1) { ADD_FAILURE() << "Allocated more segments than desired."; } else { --desiredSegmentCount; } return MallocMessageBuilder::allocateSegment(SUGGESTED_FIRST_SEGMENT_WORDS); } else { --desiredSegmentCount; return MallocMessageBuilder::allocateSegment(minimumSize); } } private: uint desiredSegmentCount; }; TEST(Packed, RoundTrip) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripScratchSpace) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); word scratch[1024]; PackedMessageReader reader(pipe, ReaderOptions(), kj::ArrayPtr(scratch, 1024)); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripLazy) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripOddSegmentCount) { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripOddSegmentCountLazy) { TestMessageBuilder builder(7); initTestMessage(builder.initRoot()); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripEvenSegmentCount) { TestMessageBuilder builder(10); initTestMessage(builder.initRoot()); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripEvenSegmentCountLazy) { TestMessageBuilder builder(10); initTestMessage(builder.initRoot()); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } TEST(Packed, RoundTripTwoMessages) { TestMessageBuilder builder(1); initTestMessage(builder.initRoot()); TestMessageBuilder builder2(1); builder2.initRoot().setTextField("Second message."); TestPipe pipe; writePackedMessage(pipe, builder); writePackedMessage(pipe, builder2); EXPECT_EQ(computeSerializedSizeInWords(builder) + computeSerializedSizeInWords(builder2), computeUnpackedSizeInWords(pipe.getArray())); { PackedMessageReader reader(pipe); checkTestMessage(reader.getRoot()); } { PackedMessageReader reader(pipe); EXPECT_EQ("Second message.", reader.getRoot().getTextField()); } } // ======================================================================================= TEST(Packed, RoundTripAllZero) { TestMessageBuilder builder(1); builder.initRoot(); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessageAllZero(reader.getRoot()); // Segment table packs to 2 bytes. // Root pointer packs to 3 bytes. // Content packs to 2 bytes (zero span). EXPECT_LE(pipe.getData().size(), 7u); } TEST(Packed, RoundTripAllZeroScratchSpace) { TestMessageBuilder builder(1); builder.initRoot(); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); word scratch[1024]; PackedMessageReader reader(pipe, ReaderOptions(), kj::ArrayPtr(scratch, 1024)); checkTestMessageAllZero(reader.getRoot()); } TEST(Packed, RoundTripAllZeroLazy) { TestMessageBuilder builder(1); builder.initRoot(); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessageAllZero(reader.getRoot()); } TEST(Packed, RoundTripAllZeroOddSegmentCount) { TestMessageBuilder builder(3); builder.initRoot().initStructField().initStructField(); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessageAllZero(reader.getRoot()); } TEST(Packed, RoundTripAllZeroOddSegmentCountLazy) { TestMessageBuilder builder(3); builder.initRoot().initStructField().initStructField(); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessageAllZero(reader.getRoot()); } TEST(Packed, RoundTripAllZeroEvenSegmentCount) { TestMessageBuilder builder(2); builder.initRoot().initStructField().initStructField(); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessageAllZero(reader.getRoot()); } TEST(Packed, RoundTripAllZeroEvenSegmentCountLazy) { TestMessageBuilder builder(2); builder.initRoot().initStructField().initStructField(); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); checkTestMessageAllZero(reader.getRoot()); } // ======================================================================================= TEST(Packed, RoundTripHugeString) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(1); builder.initRoot().setTextField(huge); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } TEST(Packed, RoundTripHugeStringScratchSpace) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(1); builder.initRoot().setTextField(huge); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); word scratch[1024]; PackedMessageReader reader(pipe, ReaderOptions(), kj::ArrayPtr(scratch, 1024)); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } TEST(Packed, RoundTripHugeStringLazy) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(1); builder.initRoot().setTextField(huge); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } TEST(Packed, RoundTripHugeStringOddSegmentCount) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(3); builder.initRoot().setTextField(huge); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } TEST(Packed, RoundTripHugeStringOddSegmentCountLazy) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(3); builder.initRoot().setTextField(huge); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } TEST(Packed, RoundTripHugeStringEvenSegmentCount) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(2); builder.initRoot().setTextField(huge); TestPipe pipe; writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } TEST(Packed, RoundTripHugeStringEvenSegmentCountLazy) { kj::String huge = kj::heapString(5023); memset(huge.begin(), 'x', 5023); TestMessageBuilder builder(2); builder.initRoot().setTextField(huge); TestPipe pipe(1); writePackedMessage(pipe, builder); EXPECT_EQ(computeSerializedSizeInWords(builder), computeUnpackedSizeInWords(pipe.getArray())); PackedMessageReader reader(pipe); EXPECT_TRUE(reader.getRoot().getTextField() == huge); } // TODO(test): Test error cases. } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/fuzz-test.c++0000644000175000017500000002206613340402540021106 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include "message.h" #include "serialize.h" #include #include #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { bool skipFuzzTest() { if (getenv("CAPNP_SKIP_FUZZ_TEST") != nullptr) { KJ_LOG(WARNING, "Skipping test because CAPNP_SKIP_FUZZ_TEST is set in environment."); return true; } else { return false; } } class DisableStackTraces: public kj::ExceptionCallback { // This test generates a lot of exceptions. Performing a backtrace on each one can be slow, // especially on Windows (where it is very, very slow). So, disable them. public: StackTraceMode stackTraceMode() override { return StackTraceMode::NONE; } }; uint64_t traverse(AnyPointer::Reader reader); uint64_t traverse(AnyStruct::Reader reader); uint64_t traverse(AnyList::Reader reader); template uint64_t traverseList(ListType list) { // Traverse in reverse order in order to trigger segfaults before exceptions if we go // out-of-bounds. uint64_t result = 0; for (size_t i = list.size(); i != 0; i--) { result += traverse(list[i-1]); } return result; } uint64_t traverse(AnyStruct::Reader reader) { uint64_t result = 0; for (byte b: reader.getDataSection()) { result += b; } result += traverseList(reader.getPointerSection()); return result; } uint64_t traverse(AnyList::Reader reader) { uint64_t result = 0; switch (reader.getElementSize()) { case ElementSize::VOID: break; case ElementSize::BIT: for (auto e: reader.as>()) result += e; break; case ElementSize::BYTE: for (auto e: reader.as>()) result += e; break; case ElementSize::TWO_BYTES: for (auto e: reader.as>()) result += e; break; case ElementSize::FOUR_BYTES: for (auto e: reader.as>()) result += e; break; case ElementSize::EIGHT_BYTES: for (auto e: reader.as>()) result += e; break; case ElementSize::POINTER: traverseList(reader.as>()); break; case ElementSize::INLINE_COMPOSITE: traverseList(reader.as>()); break; } return result; } uint64_t traverse(AnyPointer::Reader reader) { if (reader.isStruct()) { return traverse(reader.getAs()); } else if (reader.isList()) { return traverse(reader.getAs()); } else { return 0; } } template void traverseCatchingExceptions(kj::ArrayPtr data) { // Try traversing through Checker. kj::runCatchingExceptions([&]() { FlatArrayMessageReader reader(data); KJ_ASSERT(Checker::check(reader) != 0) { break; } }); // Try traversing through AnyPointer. kj::runCatchingExceptions([&]() { FlatArrayMessageReader reader(data); KJ_ASSERT(traverse(reader.getRoot()) != 0) { break; } }); // Try counting the size.. kj::runCatchingExceptions([&]() { FlatArrayMessageReader reader(data); KJ_ASSERT(reader.getRoot().targetSize().wordCount != 0) { break; } }); // Try copying into a builder, and if that works, traversing it with Checker. static word buffer[8192]; kj::runCatchingExceptions([&]() { FlatArrayMessageReader reader(data); MallocMessageBuilder copyBuilder(buffer); copyBuilder.setRoot(reader.getRoot()); KJ_ASSERT(Checker::check(copyBuilder) != 0) { break; } }); } template void fuzz(kj::ArrayPtr data, uint flipCount, uint startAt, uint endAt) { if (flipCount == 0) { traverseCatchingExceptions(data); } else { for (uint i = startAt; i < endAt; i++) { byte bit = 1u << (i % 8); byte old = data.asBytes()[i / 8]; data.asBytes()[i / 8] |= bit; fuzz(data, flipCount - 1, i + 1, endAt); data.asBytes()[i / 8] &= ~bit; fuzz(data, flipCount - 1, i + 1, endAt); data.asBytes()[i / 8] = bit; fuzz(data, flipCount - 1, i + 1, endAt); data.asBytes()[i / 8] = old; } } } struct StructChecker { template static uint check(ReaderOrBuilder& message) { uint result = 0; for (auto c: message.template getRoot().getTextField()) { result += c; } return result; } }; KJ_TEST("fuzz-test struct pointer") { if (skipFuzzTest()) return; DisableStackTraces disableStackTraces; MallocMessageBuilder builder; builder.getRoot().setTextField("foo"); KJ_ASSERT(builder.getSegmentsForOutput().size() == 1); fuzz(messageToFlatArray(builder), 2, 64, 192); } struct ListChecker { template static uint check(ReaderOrBuilder& message) { uint result = 0; for (auto e: message.template getRoot>()) { result += e; } return result; } }; KJ_TEST("fuzz-test list pointer") { if (skipFuzzTest()) return; DisableStackTraces disableStackTraces; MallocMessageBuilder builder; auto list = builder.getRoot().initAs>(2); list.set(0, 12345); list.set(1, 67890); fuzz(messageToFlatArray(builder), 2, 64, 192); } struct StructListChecker { template static uint check(ReaderOrBuilder& message) { uint result = 0; auto l = message.template getRoot>(); for (size_t i = l.size(); i > 0; i--) { for (auto c: l[i-1].getTextField()) { result += c; } } return result; } }; KJ_TEST("fuzz-test struct list pointer") { if (skipFuzzTest()) return; DisableStackTraces disableStackTraces; MallocMessageBuilder builder; auto list = builder.getRoot().initAs>(2); list[0].setTextField("foo"); list[1].setTextField("bar"); KJ_ASSERT(builder.getSegmentsForOutput().size() == 1); fuzz(messageToFlatArray(builder), 2, 64, 192); } struct TextChecker { template static uint check(ReaderOrBuilder& message) { uint result = 0; for (auto c: message.template getRoot()) { result += c; } return result; } }; KJ_TEST("fuzz-test text pointer") { if (skipFuzzTest()) return; DisableStackTraces disableStackTraces; MallocMessageBuilder builder; builder.template getRoot().setAs("foo"); fuzz(messageToFlatArray(builder), 2, 64, 192); } KJ_TEST("fuzz-test far pointer") { if (skipFuzzTest()) return; DisableStackTraces disableStackTraces; MallocMessageBuilder builder(1, AllocationStrategy::FIXED_SIZE); initTestMessage(builder.getRoot()); uint segmentCount = builder.getSegmentsForOutput().size(); uint tableSize = segmentCount / 2 + 1; // Fuzz the root far pointer plus its landing pad, which should be in the next word. fuzz(messageToFlatArray(builder), 2, tableSize * 64, tableSize * 64 + 128); } KJ_TEST("fuzz-test double-far pointer") { if (skipFuzzTest()) return; DisableStackTraces disableStackTraces; MallocMessageBuilder builder(1, AllocationStrategy::FIXED_SIZE); // Carefully arrange for a double-far pointer to be created. auto root = builder.getRoot(); root.adopt(builder.getOrphanage().newOrphanCopy(Text::Reader("foo"))); // Verify that did what we expected. KJ_ASSERT(builder.getSegmentsForOutput().size() == 3); KJ_ASSERT(builder.getSegmentsForOutput()[0].size() == 1); // root pointer KJ_ASSERT(builder.getSegmentsForOutput()[1].size() == 1); // "foo" KJ_ASSERT(builder.getSegmentsForOutput()[2].size() == 2); // double-far landing pad // Fuzz the root far pointer. fuzz(messageToFlatArray(builder), 2, 64, 128); // Fuzz the landing pad. fuzz(messageToFlatArray(builder), 2, 192, 320); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/canonicalize-test.c++0000644000175000017500000003423013340402540022543 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "message.h" #include "any.h" #include #include #include "test-util.h" namespace capnp { namespace _ { // private using test::TestLists; namespace { KJ_TEST("canonicalize yields canonical message") { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root); auto canonicalWords = canonicalize(root.asReader()); // Throws an exception on canonicalization failure. kj::ArrayPtr canonicalSegments[1] = {canonicalWords.asPtr()}; capnp::SegmentArrayMessageReader canonicalReader(kj::arrayPtr(canonicalSegments, 1)); KJ_ASSERT(AnyStruct::Reader(root.asReader()) == AnyStruct::Reader(canonicalReader.getRoot())); } KJ_TEST("canonicalize succeeds on empty struct") { MallocMessageBuilder builder; auto root = builder.initRoot(); canonicalize(root.asReader()); // Throws an exception on canoncalization failure. } KJ_TEST("data word with only its most significant bit set does not get truncated") { AlignedData<3> segment = {{ // Struct pointer, body immediately follows, two data words 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // First data word 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, // Second data word, all zero except most significant bit 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 3)}; SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1)); KJ_ASSERT(messageReader.isCanonical()); auto canonicalWords = canonicalize(messageReader.getRoot()); // At one point this failed because an off-by-one bug in canonicalization // caused the second word of the data section to be truncated. ASSERT_EQ(canonicalWords.asBytes(), kj::arrayPtr(segment.bytes, 3 * 8)); } KJ_TEST("INLINE_COMPOSITE data word with only its most significant bit set does not get truncated") { AlignedData<5> segment = {{ // Struct pointer, body immediately follows, one pointer 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // List pointer, no offset, inline composite, two words long 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, // Tag word, list has one element with two data words and no pointers 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // First data word 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, // Second data word, all zero except most significant bit 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 5)}; SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1)); KJ_ASSERT(messageReader.isCanonical()); auto canonicalWords = canonicalize(messageReader.getRoot()); // At one point this failed because an off-by-one bug in canonicalization // caused the second word of the data section to be truncated. ASSERT_EQ(canonicalWords.asBytes(), kj::arrayPtr(segment.bytes, 5 * 8)); } KJ_TEST("canonical non-null empty struct field") { AlignedData<4> nonNullEmptyStruct = {{ // Struct pointer, body immediately follows, two pointer fields, no data. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, // First pointer field, struct, offset of 1, data size 1, no pointers. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // Non-null pointer to empty struct. 0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // Body of struct filled with non-zero data. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(nonNullEmptyStruct.words, 4)}; SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1)); KJ_ASSERT(messageReader.isCanonical()); } KJ_TEST("for pointers to empty structs, preorder is not canonical") { AlignedData<4> nonNullEmptyStruct = {{ // Struct pointer, body immediately follows, two pointer fields, no data. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, // First pointer field, struct, offset of 1, data size 1, no pointers. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // Non-null pointer to empty struct. Offset puts it in "preorder". Would need to have // an offset of -1 to be canonical. 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Body of struct filled with non-zero data. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(nonNullEmptyStruct.words, 4)}; SegmentArrayMessageReader messageReader(kj::arrayPtr(segments, 1)); KJ_ASSERT(!messageReader.isCanonical()); } KJ_TEST("isCanonical requires pointer preorder") { AlignedData<5> misorderedSegment = {{ //Struct pointer, data immediately follows, two pointer fields, no data 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, //Pointer field 1, pointing to the last entry, data size 1, no pointer 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //Pointer field 2, pointing to the next entry, data size 2, no pointer 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //Data for field 2 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, //Data for field 1 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(misorderedSegment.words, 5)}; SegmentArrayMessageReader outOfOrder(kj::arrayPtr(segments, 1)); KJ_ASSERT(!outOfOrder.isCanonical()); } KJ_TEST("isCanonical requires dense packing") { AlignedData<3> gapSegment = {{ //Struct pointer, data after a gap 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //The gap 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Data for field 1 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(gapSegment.words, 3)}; SegmentArrayMessageReader gap(kj::arrayPtr(segments, 1)); KJ_ASSERT(!gap.isCanonical()); } KJ_TEST("isCanonical rejects multi-segment messages") { AlignedData<1> farPtr = {{ //Far pointer to next segment 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, }}; AlignedData<2> farTarget = {{ //Struct pointer (needed to make the far pointer legal) 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //Dummy data 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, }}; kj::ArrayPtr segments[2] = { kj::arrayPtr(farPtr.words, 1), kj::arrayPtr(farTarget.words, 2) }; SegmentArrayMessageReader multiSegmentMessage(kj::arrayPtr(segments, 2)); KJ_ASSERT(!multiSegmentMessage.isCanonical()); } KJ_TEST("isCanonical rejects zero segment messages") { SegmentArrayMessageReader zero(kj::arrayPtr((kj::ArrayPtr*)NULL, 0)); KJ_ASSERT(!zero.isCanonical()); } KJ_TEST("isCanonical requires truncation of 0-valued struct fields") { AlignedData<2> nonTruncatedSegment = {{ //Struct pointer, data immediately follows 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, //Default data value, should have been truncated 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments[1] = { kj::arrayPtr(nonTruncatedSegment.words, 3) }; SegmentArrayMessageReader nonTruncated(kj::arrayPtr(segments, 1)); KJ_ASSERT(!nonTruncated.isCanonical()); } KJ_TEST("isCanonical rejects unused trailing words") { AlignedData<3> segment = {{ // Struct pointer, data in next word 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // Data section of struct 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // Trailing zero word 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 3)}; SegmentArrayMessageReader message(kj::arrayPtr(segments, 1)); KJ_ASSERT(!message.isCanonical()); } KJ_TEST("isCanonical accepts empty inline composite list of zero-sized structs") { AlignedData<3> segment = {{ // Struct pointer, pointer in next word 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // List pointer, inline composite, zero words long 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, // Tag word 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 3)}; SegmentArrayMessageReader message(kj::arrayPtr(segments, 1)); KJ_ASSERT(message.isCanonical()); } KJ_TEST("isCanonical rejects inline composite list with inaccurate word-length") { AlignedData<6> segment = {{ // Struct pointer, no offset, pointer section has two entries 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, // List pointer, offset of one, inline composite, two words long // (The list only needs to be one word long to hold its actual elements; // therefore this message is not canonical.) 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, // Struct pointer, offset two, data section has one word 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // Tag word, struct, one element, one word data section 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // Data section of struct element of list 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // Data section of struct field in top-level struct 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 6)}; SegmentArrayMessageReader message(kj::arrayPtr(segments, 1)); KJ_ASSERT(!message.isCanonical()); } KJ_TEST("upgraded lists can be canonicalized") { AlignedData<7> upgradedList = {{ //Struct pointer, data immediately follows, 4 pointer fields, no data 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, //Three words of default pointers to get to the int16 list 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //List pointer, 3 int16s. 0x01, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, //First two elements 0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x05, 0x06, //Last element 0x07, 0x08, 0x09, 0x10, 0x00, 0x00, 0x00, 0x00 }}; kj::ArrayPtr segments[1] = { kj::arrayPtr(upgradedList.words, 7) }; SegmentArrayMessageReader upgraded(kj::arrayPtr(segments, 1)); auto root = upgraded.getRoot(); canonicalize(root); } KJ_TEST("isCanonical requires truncation of 0-valued struct fields in all list members") { AlignedData<6> nonTruncatedList = {{ //List pointer, composite, 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, //Struct tag word, 2 structs, 2 data words per struct 0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, //Data word non-null 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, //Null trailing word 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //Data word non-null 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, //Null trailing word 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}; kj::ArrayPtr segments[1] = { kj::arrayPtr(nonTruncatedList.words, 6) }; SegmentArrayMessageReader nonTruncated(kj::arrayPtr(segments, 1)); KJ_ASSERT(!nonTruncated.isCanonical()); } KJ_TEST("primitive list with nonzero padding") { AlignedData<3> segment = {{ // Struct, one pointer field. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // List of three byte-sized elements. 0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, // Fourth byte is non-zero! 0x01, 0x02, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 3)}; SegmentArrayMessageReader message(kj::arrayPtr(segments, 1)); KJ_ASSERT(!message.isCanonical()); auto canonicalWords = canonicalize(message.getRoot()); AlignedData<3> canonicalSegment = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, }}; ASSERT_EQ(canonicalWords.asBytes(), kj::arrayPtr(canonicalSegment.bytes, 3 * 8)); } KJ_TEST("bit list with nonzero padding") { AlignedData<3> segment = {{ // Struct, one pointer field. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // List of eleven bit-sized elements. 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, // Twelfth bit is non-zero! 0xee, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; kj::ArrayPtr segments[1] = {kj::arrayPtr(segment.words, 3)}; SegmentArrayMessageReader message(kj::arrayPtr(segments, 1)); KJ_ASSERT(!message.isCanonical()); auto canonicalWords = canonicalize(message.getRoot()); AlignedData<3> canonicalSegment = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0xee, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }}; ASSERT_EQ(canonicalWords.asBytes(), kj::arrayPtr(canonicalSegment.bytes, 3 * 8)); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/capability-test.c++0000644000175000017500000011664413650317313022246 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "schema.capnp.h" #ifdef CAPNP_CAPABILITY_H_INCLUDED #error "schema.capnp should not depend on capability.h, because it contains no interfaces." #endif #include #ifndef CAPNP_CAPABILITY_H_INCLUDED #error "test.capnp did not include capability.h." #endif #include "capability.h" #include "test-util.h" #include #include namespace capnp { namespace _ { namespace { TEST(Capability, Basic) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; test::TestInterface::Client client(kj::heap(callCount)); auto request1 = client.fooRequest(); request1.setI(123); request1.setJ(true); auto promise1 = request1.send(); auto request2 = client.bazRequest(); initTestMessage(request2.initS()); auto promise2 = request2.send(); bool barFailed = false; auto request3 = client.barRequest(); auto promise3 = request3.send().then( [](Response&& response) { ADD_FAILURE() << "Expected bar() call to fail."; }, [&](kj::Exception&& e) { EXPECT_EQ(kj::Exception::Type::UNIMPLEMENTED, e.getType()); barFailed = true; }); EXPECT_EQ(0, callCount); auto response1 = promise1.wait(waitScope); EXPECT_EQ("foo", response1.getX()); auto response2 = promise2.wait(waitScope); promise3.wait(waitScope); EXPECT_EQ(2, callCount); EXPECT_TRUE(barFailed); } TEST(Capability, Inheritance) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; test::TestExtends::Client client1(kj::heap(callCount)); test::TestInterface::Client client2 = client1; auto client = client2.castAs(); auto request1 = client.fooRequest(); request1.setI(321); auto promise1 = request1.send(); auto request2 = client.graultRequest(); auto promise2 = request2.send(); EXPECT_EQ(0, callCount); auto response2 = promise2.wait(waitScope); checkTestMessage(response2); auto response1 = promise1.wait(waitScope); EXPECT_EQ("bar", response1.getX()); EXPECT_EQ(2, callCount); } TEST(Capability, Pipelining) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; int chainedCallCount = 0; test::TestPipeline::Client client(kj::heap(callCount)); auto request = client.getCapRequest(); request.setN(234); request.setInCap(test::TestInterface::Client(kj::heap(chainedCallCount))); auto promise = request.send(); auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); pipelineRequest.setI(321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = promise.getOutBox().getCap().castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); promise = nullptr; // Just to be annoying, drop the original promise. EXPECT_EQ(0, callCount); EXPECT_EQ(0, chainedCallCount); auto response = pipelinePromise.wait(waitScope); EXPECT_EQ("bar", response.getX()); auto response2 = pipelinePromise2.wait(waitScope); checkTestMessage(response2); EXPECT_EQ(3, callCount); EXPECT_EQ(1, chainedCallCount); } TEST(Capability, TailCall) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int calleeCallCount = 0; int callerCallCount = 0; test::TestTailCallee::Client callee(kj::heap(calleeCallCount)); test::TestTailCaller::Client caller(kj::heap(callerCallCount)); auto request = caller.fooRequest(); request.setI(456); request.setCallee(callee); auto promise = request.send(); auto dependentCall0 = promise.getC().getCallSequenceRequest().send(); auto response = promise.wait(waitScope); EXPECT_EQ(456, response.getI()); EXPECT_EQ(456, response.getI()); auto dependentCall1 = promise.getC().getCallSequenceRequest().send(); auto dependentCall2 = response.getC().getCallSequenceRequest().send(); EXPECT_EQ(0, dependentCall0.wait(waitScope).getN()); EXPECT_EQ(1, dependentCall1.wait(waitScope).getN()); EXPECT_EQ(2, dependentCall2.wait(waitScope).getN()); EXPECT_EQ(1, calleeCallCount); EXPECT_EQ(1, callerCallCount); } TEST(Capability, AsyncCancelation) { // Tests allowCancellation(). kj::EventLoop loop; kj::WaitScope waitScope(loop); auto paf = kj::newPromiseAndFulfiller(); bool destroyed = false; auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); int callCount = 0; int handleCount = 0; test::TestMoreStuff::Client client(kj::heap(callCount, handleCount)); kj::Promise promise = nullptr; bool returned = false; { auto request = client.expectCancelRequest(); request.setCap(test::TestInterface::Client(kj::heap(kj::mv(paf.fulfiller)))); promise = request.send().then( [&](Response&& response) { returned = true; }).eagerlyEvaluate(nullptr); } kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); // We can detect that the method was canceled because it will drop the cap. EXPECT_FALSE(destroyed); EXPECT_FALSE(returned); promise = nullptr; // request cancellation destructionPromise.wait(waitScope); EXPECT_TRUE(destroyed); EXPECT_FALSE(returned); } // ======================================================================================= TEST(Capability, DynamicClient) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; DynamicCapability::Client client = test::TestInterface::Client(kj::heap(callCount)); auto request1 = client.newRequest("foo"); request1.set("i", 123); request1.set("j", true); auto promise1 = request1.send(); auto request2 = client.newRequest("baz"); initDynamicTestMessage(request2.init("s").as()); auto promise2 = request2.send(); bool barFailed = false; auto request3 = client.newRequest("bar"); auto promise3 = request3.send().then( [](Response&& response) { ADD_FAILURE() << "Expected bar() call to fail."; }, [&](kj::Exception&& e) { EXPECT_EQ(kj::Exception::Type::UNIMPLEMENTED, e.getType()); barFailed = true; }); EXPECT_EQ(0, callCount); auto response1 = promise1.wait(waitScope); EXPECT_EQ("foo", response1.get("x").as()); auto response2 = promise2.wait(waitScope); promise3.wait(waitScope); EXPECT_EQ(2, callCount); EXPECT_TRUE(barFailed); } TEST(Capability, DynamicClientInheritance) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; DynamicCapability::Client client1 = test::TestExtends::Client(kj::heap(callCount)); EXPECT_EQ(Schema::from(), client1.getSchema()); EXPECT_NE(Schema::from(), client1.getSchema()); DynamicCapability::Client client2 = client1.upcast(Schema::from()); EXPECT_EQ(Schema::from(), client2.getSchema()); EXPECT_ANY_THROW(client2.upcast(Schema::from())); auto client = client2.castAs(Schema::from()); auto request1 = client.newRequest("foo"); request1.set("i", 321); auto promise1 = request1.send(); auto request2 = client.newRequest("grault"); auto promise2 = request2.send(); EXPECT_EQ(0, callCount); auto response2 = promise2.wait(waitScope); checkDynamicTestMessage(response2.as()); auto response1 = promise1.wait(waitScope); EXPECT_EQ("bar", response1.get("x").as()); EXPECT_EQ(2, callCount); } TEST(Capability, DynamicClientPipelining) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; int chainedCallCount = 0; DynamicCapability::Client client = test::TestPipeline::Client(kj::heap(callCount)); auto request = client.newRequest("getCap"); request.set("n", 234); request.set("inCap", test::TestInterface::Client(kj::heap(chainedCallCount))); auto promise = request.send(); auto outCap = promise.get("outBox").releaseAs() .get("cap").releaseAs(); auto pipelineRequest = outCap.newRequest("foo"); pipelineRequest.set("i", 321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = outCap.castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); promise = nullptr; // Just to be annoying, drop the original promise. EXPECT_EQ(0, callCount); EXPECT_EQ(0, chainedCallCount); auto response = pipelinePromise.wait(waitScope); EXPECT_EQ("bar", response.get("x").as()); auto response2 = pipelinePromise2.wait(waitScope); checkTestMessage(response2); EXPECT_EQ(3, callCount); EXPECT_EQ(1, chainedCallCount); } TEST(Capability, DynamicClientPipelineAnyCap) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; int chainedCallCount = 0; DynamicCapability::Client client = test::TestPipeline::Client(kj::heap(callCount)); auto request = client.newRequest("getAnyCap"); request.set("n", 234); request.set("inCap", test::TestInterface::Client(kj::heap(chainedCallCount))); auto promise = request.send(); auto outAnyCap = promise.get("outBox").releaseAs() .get("cap").releaseAs(); EXPECT_EQ(Schema::from(), outAnyCap.getSchema()); auto outCap = outAnyCap.castAs(Schema::from()); auto pipelineRequest = outCap.newRequest("foo"); pipelineRequest.set("i", 321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = outCap.castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); promise = nullptr; // Just to be annoying, drop the original promise. EXPECT_EQ(0, callCount); EXPECT_EQ(0, chainedCallCount); auto response = pipelinePromise.wait(waitScope); EXPECT_EQ("bar", response.get("x").as()); auto response2 = pipelinePromise2.wait(waitScope); checkTestMessage(response2); EXPECT_EQ(3, callCount); EXPECT_EQ(1, chainedCallCount); } // ======================================================================================= class TestInterfaceDynamicImpl final: public DynamicCapability::Server { public: TestInterfaceDynamicImpl(int& callCount) : DynamicCapability::Server(Schema::from()), callCount(callCount) {} int& callCount; kj::Promise call(InterfaceSchema::Method method, CallContext context) { auto methodName = method.getProto().getName(); if (methodName == "foo") { ++callCount; auto params = context.getParams(); EXPECT_EQ(123, params.get("i").as()); EXPECT_TRUE(params.get("j").as()); context.getResults().set("x", "foo"); return kj::READY_NOW; } else if (methodName == "baz") { ++callCount; auto params = context.getParams(); checkDynamicTestMessage(params.get("s").as()); context.releaseParams(); EXPECT_ANY_THROW(context.getParams()); return kj::READY_NOW; } else { KJ_UNIMPLEMENTED("Method not implemented", methodName) { break; } return kj::READY_NOW; } } }; TEST(Capability, DynamicServer) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; test::TestInterface::Client client = DynamicCapability::Client(kj::heap(callCount)) .castAs(); auto request1 = client.fooRequest(); request1.setI(123); request1.setJ(true); auto promise1 = request1.send(); auto request2 = client.bazRequest(); initTestMessage(request2.initS()); auto promise2 = request2.send(); bool barFailed = false; auto request3 = client.barRequest(); auto promise3 = request3.send().then( [](Response&& response) { ADD_FAILURE() << "Expected bar() call to fail."; }, [&](kj::Exception&& e) { EXPECT_EQ(kj::Exception::Type::UNIMPLEMENTED, e.getType()); barFailed = true; }); EXPECT_EQ(0, callCount); auto response1 = promise1.wait(waitScope); EXPECT_EQ("foo", response1.getX()); auto response2 = promise2.wait(waitScope); promise3.wait(waitScope); EXPECT_EQ(2, callCount); EXPECT_TRUE(barFailed); } class TestExtendsDynamicImpl final: public DynamicCapability::Server { public: TestExtendsDynamicImpl(int& callCount) : DynamicCapability::Server(Schema::from()), callCount(callCount) {} int& callCount; kj::Promise call(InterfaceSchema::Method method, CallContext context) { auto methodName = method.getProto().getName(); if (methodName == "foo") { ++callCount; auto params = context.getParams(); EXPECT_EQ(321, params.get("i").as()); EXPECT_FALSE(params.get("j").as()); context.getResults().set("x", "bar"); return kj::READY_NOW; } else if (methodName == "grault") { ++callCount; context.releaseParams(); initDynamicTestMessage(context.getResults()); return kj::READY_NOW; } else { KJ_FAIL_ASSERT("Method not implemented", methodName); } } }; TEST(Capability, DynamicServerInheritance) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; test::TestExtends::Client client1 = DynamicCapability::Client(kj::heap(callCount)) .castAs(); test::TestInterface::Client client2 = client1; auto client = client2.castAs(); auto request1 = client.fooRequest(); request1.setI(321); auto promise1 = request1.send(); auto request2 = client.graultRequest(); auto promise2 = request2.send(); EXPECT_EQ(0, callCount); auto response2 = promise2.wait(waitScope); checkTestMessage(response2); auto response1 = promise1.wait(waitScope); EXPECT_EQ("bar", response1.getX()); EXPECT_EQ(2, callCount); } class TestPipelineDynamicImpl final: public DynamicCapability::Server { public: TestPipelineDynamicImpl(int& callCount) : DynamicCapability::Server(Schema::from()), callCount(callCount) {} int& callCount; kj::Promise call(InterfaceSchema::Method method, CallContext context) { auto methodName = method.getProto().getName(); if (methodName == "getCap") { ++callCount; auto params = context.getParams(); EXPECT_EQ(234, params.get("n").as()); auto cap = params.get("inCap").as(); context.releaseParams(); auto request = cap.newRequest("foo"); request.set("i", 123); request.set("j", true); return request.send().then( [this,KJ_CPCAP(context)](capnp::Response&& response) mutable { EXPECT_EQ("foo", response.get("x").as()); auto result = context.getResults(); result.set("s", "bar"); auto box = result.init("outBox").as(); // Too lazy to write a whole separate test for each of these cases... so just make // sure they both compile here, and only actually test the latter. box.set("cap", kj::heap(callCount)); #if __GNUG__ && !__clang__ && __GNUG__ == 4 && __GNUC_MINOR__ == 9 // The last line in this block tickles a bug in Debian G++ 4.9.2 that is not present // in 4.8.x nor in 4.9.4: // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=781060 // // Unfortunately 4.9.2 is present on many Debian Jessie systems.. // // For the moment, we can get away with skipping the last line as the previous line // will set things up in a way that allows the test to complete successfully. return; #endif box.set("cap", kj::heap(callCount)); }); } else { KJ_FAIL_ASSERT("Method not implemented", methodName); } } }; TEST(Capability, DynamicServerPipelining) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; int chainedCallCount = 0; test::TestPipeline::Client client = DynamicCapability::Client(kj::heap(callCount)) .castAs(); auto request = client.getCapRequest(); request.setN(234); request.setInCap(test::TestInterface::Client(kj::heap(chainedCallCount))); auto promise = request.send(); auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); pipelineRequest.setI(321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = promise.getOutBox().getCap().castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); promise = nullptr; // Just to be annoying, drop the original promise. EXPECT_EQ(0, callCount); EXPECT_EQ(0, chainedCallCount); auto response = pipelinePromise.wait(waitScope); EXPECT_EQ("bar", response.getX()); auto response2 = pipelinePromise2.wait(waitScope); checkTestMessage(response2); EXPECT_EQ(3, callCount); EXPECT_EQ(1, chainedCallCount); } class TestTailCallerDynamicImpl final: public DynamicCapability::Server { public: TestTailCallerDynamicImpl(int& callCount) : DynamicCapability::Server(Schema::from()), callCount(callCount) {} int& callCount; kj::Promise call(InterfaceSchema::Method method, CallContext context) { auto methodName = method.getProto().getName(); if (methodName == "foo") { ++callCount; auto params = context.getParams(); auto tailRequest = params.get("callee").as().newRequest("foo"); tailRequest.set("i", params.get("i")); tailRequest.set("t", "from TestTailCaller"); return context.tailCall(kj::mv(tailRequest)); } else { KJ_FAIL_ASSERT("Method not implemented", methodName); } } }; TEST(Capability, DynamicServerTailCall) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int calleeCallCount = 0; int callerCallCount = 0; test::TestTailCallee::Client callee(kj::heap(calleeCallCount)); test::TestTailCaller::Client caller = DynamicCapability::Client(kj::heap(callerCallCount)) .castAs(); auto request = caller.fooRequest(); request.setI(456); request.setCallee(callee); auto promise = request.send(); auto dependentCall0 = promise.getC().getCallSequenceRequest().send(); auto response = promise.wait(waitScope); EXPECT_EQ(456, response.getI()); EXPECT_EQ(456, response.getI()); auto dependentCall1 = promise.getC().getCallSequenceRequest().send(); auto dependentCall2 = response.getC().getCallSequenceRequest().send(); EXPECT_EQ(0, dependentCall0.wait(waitScope).getN()); EXPECT_EQ(1, dependentCall1.wait(waitScope).getN()); EXPECT_EQ(2, dependentCall2.wait(waitScope).getN()); EXPECT_EQ(1, calleeCallCount); EXPECT_EQ(1, callerCallCount); } // ======================================================================================= void verifyClient(test::TestInterface::Client client, const int& callCount, kj::WaitScope& waitScope) { int origCount = callCount; auto request = client.fooRequest(); request.setI(123); request.setJ(true); auto response = request.send().wait(waitScope); EXPECT_EQ("foo", response.getX()); EXPECT_EQ(origCount + 1, callCount); } void verifyClient(DynamicCapability::Client client, const int& callCount, kj::WaitScope& waitScope) { int origCount = callCount; auto request = client.newRequest("foo"); request.set("i", 123); request.set("j", true); auto response = request.send().wait(waitScope); EXPECT_EQ("foo", response.get("x").as()); EXPECT_EQ(origCount + 1, callCount); } TEST(Capability, AnyPointersAndOrphans) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount1 = 0; int callCount2 = 0; // We use a TestPipeline instance here merely as a way to conveniently obtain an imbued message // instance. test::TestPipeline::Client baseClient(nullptr); test::TestInterface::Client client1(kj::heap(callCount1)); test::TestInterface::Client client2(kj::heap(callCount2)); auto request = baseClient.testPointersRequest(); request.setCap(client1); EXPECT_TRUE(request.hasCap()); Orphan orphan = request.disownCap(); EXPECT_FALSE(orphan == nullptr); EXPECT_FALSE(request.hasCap()); verifyClient(orphan.get(), callCount1, waitScope); verifyClient(orphan.getReader(), callCount1, waitScope); request.getObj().adopt(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); verifyClient(request.getObj().getAs(), callCount1, waitScope); verifyClient(request.asReader().getObj().getAs(), callCount1, waitScope); verifyClient(request.getObj().getAs( Schema::from()), callCount1, waitScope); verifyClient(request.asReader().getObj().getAs( Schema::from()), callCount1, waitScope); request.getObj().clear(); EXPECT_FALSE(request.hasObj()); request.getObj().setAs(client2); verifyClient(request.getObj().getAs(), callCount2, waitScope); Orphan dynamicOrphan = request.getObj().disownAs( Schema::from()); verifyClient(dynamicOrphan.get(), callCount2, waitScope); verifyClient(dynamicOrphan.getReader(), callCount2, waitScope); Orphan dynamicValueOrphan = kj::mv(dynamicOrphan); verifyClient(dynamicValueOrphan.get().as(), callCount2, waitScope); orphan = dynamicValueOrphan.releaseAs(); EXPECT_FALSE(orphan == nullptr); verifyClient(orphan.get(), callCount2, waitScope); request.adoptCap(kj::mv(orphan)); EXPECT_TRUE(orphan == nullptr); verifyClient(request.getCap(), callCount2, waitScope); Orphan dynamicOrphan2 = request.disownCap(); verifyClient(dynamicOrphan2.get(), callCount2, waitScope); verifyClient(dynamicOrphan2.getReader(), callCount2, waitScope); } TEST(Capability, Lists) { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount1 = 0; int callCount2 = 0; int callCount3 = 0; test::TestPipeline::Client baseClient(kj::heap(callCount1)); test::TestInterface::Client client1(kj::heap(callCount1)); test::TestInterface::Client client2(kj::heap(callCount2)); test::TestInterface::Client client3(kj::heap(callCount3)); auto request = baseClient.testPointersRequest(); auto list = request.initList(3); list.set(0, client1); list.set(1, client2); list.set(2, client3); verifyClient(list[0], callCount1, waitScope); verifyClient(list[1], callCount2, waitScope); verifyClient(list[2], callCount3, waitScope); auto listReader = request.asReader().getList(); verifyClient(listReader[0], callCount1, waitScope); verifyClient(listReader[1], callCount2, waitScope); verifyClient(listReader[2], callCount3, waitScope); auto dynamicList = toDynamic(list); verifyClient(dynamicList[0].as(), callCount1, waitScope); verifyClient(dynamicList[1].as(), callCount2, waitScope); verifyClient(dynamicList[2].as(), callCount3, waitScope); auto dynamicListReader = toDynamic(listReader); verifyClient(dynamicListReader[0].as(), callCount1, waitScope); verifyClient(dynamicListReader[1].as(), callCount2, waitScope); verifyClient(dynamicListReader[2].as(), callCount3, waitScope); } TEST(Capability, KeywordMethods) { // Verify that keywords are only munged where necessary. kj::EventLoop loop; kj::WaitScope waitScope(loop); bool called = false; class TestKeywordMethodsImpl final: public test::TestKeywordMethods::Server { public: TestKeywordMethodsImpl(bool& called): called(called) {} kj::Promise delete_(DeleteContext context) override { called = true; return kj::READY_NOW; } private: bool& called; }; test::TestKeywordMethods::Client client = kj::heap(called); client.deleteRequest().send().wait(waitScope); EXPECT_TRUE(called); } TEST(Capability, Generics) { kj::EventLoop loop; kj::WaitScope waitScope(loop); typedef test::TestGenerics::Interface> Interface; Interface::Client client = nullptr; auto request = client.callRequest(); request.setBaz("hello"); initTestMessage(request.initInnerBound().initFoo()); initTestMessage(request.initInnerUnbound().getFoo().initAs()); auto promise = request.send().then([](capnp::Response&& response) { // This doesn't actually execute; we're just checking that it compiles. List::Reader qux = response.getQux(); qux.size(); checkTestMessage(response.getGen().getFoo()); }, [](kj::Exception&& e) { // Ignore exception (which we'll always get because we're calling a null capability). }); promise.wait(waitScope); // Check that asGeneric<>() compiles. test::TestGenerics::Interface<>::Client castClient = client.asGeneric<>(); test::TestGenerics::Interface::Client castClient2 = client.asGeneric(); test::TestGenerics<>::Interface>::Client castClient3 = client.asTestGenericsGeneric<>(); } TEST(Capability, Generics2) { MallocMessageBuilder builder; auto root = builder.getRoot(); root.initCap().setFoo(test::TestInterface::Client(nullptr)); } TEST(Capability, ImplicitParams) { kj::EventLoop loop; kj::WaitScope waitScope(loop); typedef test::TestImplicitMethodParams Interface; Interface::Client client = nullptr; capnp::Request, test::TestGenerics> request = client.callRequest(); request.setFoo("hello"); initTestMessage(request.initBar()); auto promise = request.send() .then([](capnp::Response>&& response) { // This doesn't actually execute; we're just checking that it compiles. Text::Reader text = response.getFoo(); text.size(); checkTestMessage(response.getRev().getFoo()); }, [](kj::Exception&& e) {}); promise.wait(waitScope); } TEST(Capability, CapabilityServerSet) { kj::EventLoop loop; kj::WaitScope waitScope(loop); CapabilityServerSet set1, set2; int callCount = 0; test::TestInterface::Client clientStandalone(kj::heap(callCount)); test::TestInterface::Client clientNull = nullptr; auto ownServer1 = kj::heap(callCount); auto& server1 = *ownServer1; test::TestInterface::Client client1 = set1.add(kj::mv(ownServer1)); auto ownServer2 = kj::heap(callCount); auto& server2 = *ownServer2; test::TestInterface::Client client2 = set2.add(kj::mv(ownServer2)); // Getting the local server using the correct set works. EXPECT_EQ(&server1, &KJ_ASSERT_NONNULL(set1.getLocalServer(client1).wait(waitScope))); EXPECT_EQ(&server2, &KJ_ASSERT_NONNULL(set2.getLocalServer(client2).wait(waitScope))); // Getting the local server using the wrong set doesn't work. EXPECT_TRUE(set1.getLocalServer(client2).wait(waitScope) == nullptr); EXPECT_TRUE(set2.getLocalServer(client1).wait(waitScope) == nullptr); EXPECT_TRUE(set1.getLocalServer(clientStandalone).wait(waitScope) == nullptr); EXPECT_TRUE(set1.getLocalServer(clientNull).wait(waitScope) == nullptr); // A promise client waits to be resolved. auto paf = kj::newPromiseAndFulfiller(); test::TestInterface::Client clientPromise = kj::mv(paf.promise); auto errorPaf = kj::newPromiseAndFulfiller(); test::TestInterface::Client errorPromise = kj::mv(errorPaf.promise); bool resolved1 = false, resolved2 = false, resolved3 = false; auto promise1 = set1.getLocalServer(clientPromise) .then([&](kj::Maybe server) { resolved1 = true; EXPECT_EQ(&server1, &KJ_ASSERT_NONNULL(server)); }); auto promise2 = set2.getLocalServer(clientPromise) .then([&](kj::Maybe server) { resolved2 = true; EXPECT_TRUE(server == nullptr); }); auto promise3 = set1.getLocalServer(errorPromise) .then([&](kj::Maybe server) { KJ_FAIL_EXPECT("getLocalServer() on error promise should have thrown"); }, [&](kj::Exception&& e) { resolved3 = true; KJ_EXPECT(e.getDescription().endsWith("foo"), e.getDescription()); }); kj::evalLater([](){}).wait(waitScope); kj::evalLater([](){}).wait(waitScope); kj::evalLater([](){}).wait(waitScope); kj::evalLater([](){}).wait(waitScope); EXPECT_FALSE(resolved1); EXPECT_FALSE(resolved2); EXPECT_FALSE(resolved3); paf.fulfiller->fulfill(kj::cp(client1)); errorPaf.fulfiller->reject(KJ_EXCEPTION(FAILED, "foo")); promise1.wait(waitScope); promise2.wait(waitScope); promise3.wait(waitScope); EXPECT_TRUE(resolved1); EXPECT_TRUE(resolved2); EXPECT_TRUE(resolved3); } class TestThisCap final: public test::TestInterface::Server { public: TestThisCap(int& callCount): callCount(callCount) {} ~TestThisCap() noexcept(false) { callCount = -1; } test::TestInterface::Client getSelf() { return thisCap(); } protected: kj::Promise bar(BarContext context) { ++callCount; return kj::READY_NOW; } private: int& callCount; }; TEST(Capability, ThisCap) { int callCount = 0; kj::EventLoop loop; kj::WaitScope waitScope(loop); auto server = kj::heap(callCount); TestThisCap* serverPtr = server; test::TestInterface::Client client = kj::mv(server); client.barRequest().send().wait(waitScope); EXPECT_EQ(1, callCount); test::TestInterface::Client client2 = serverPtr->getSelf(); EXPECT_EQ(1, callCount); client2.barRequest().send().wait(waitScope); EXPECT_EQ(2, callCount); client = nullptr; EXPECT_EQ(2, callCount); client2.barRequest().send().wait(waitScope); EXPECT_EQ(3, callCount); client2 = nullptr; EXPECT_EQ(-1, callCount); } TEST(Capability, TransferCap) { kj::EventLoop loop; kj::WaitScope waitScope(loop); MallocMessageBuilder message; auto root = message.initRoot(); auto orphan = message.getOrphanage().newOrphan(); auto e = orphan.get(); e.setText("foo"); e.setCap(KJ_EXCEPTION(FAILED, "whatever")); root.initList(1).adoptWithCaveats(0, kj::mv(orphan)); // This line used to throw due to capability pointers being incorrectly transferred. auto cap = root.getList()[0].getCap(); cap.whenResolved().then([]() { KJ_FAIL_EXPECT("didn't throw?"); }, [](kj::Exception&&) { // success }).wait(waitScope); } KJ_TEST("Promise> automatically reduces to RemotePromise") { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; test::TestInterface::Client client(kj::heap(callCount)); RemotePromise promise = kj::evalLater([&]() { auto request = client.fooRequest(); request.setI(123); request.setJ(true); return request.send(); }); EXPECT_EQ(0, callCount); auto response = promise.wait(waitScope); EXPECT_EQ("foo", response.getX()); EXPECT_EQ(1, callCount); } KJ_TEST("Promise> automatically reduces to RemotePromise with pipelining") { kj::EventLoop loop; kj::WaitScope waitScope(loop); int callCount = 0; int chainedCallCount = 0; test::TestPipeline::Client client(kj::heap(callCount)); auto promise = kj::evalLater([&]() { auto request = client.getCapRequest(); request.setN(234); request.setInCap(test::TestInterface::Client(kj::heap(chainedCallCount))); return request.send(); }); auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); pipelineRequest.setI(321); auto pipelinePromise = pipelineRequest.send(); EXPECT_EQ(0, callCount); EXPECT_EQ(0, chainedCallCount); auto response = pipelinePromise.wait(waitScope); EXPECT_EQ("bar", response.getX()); EXPECT_EQ(2, callCount); EXPECT_EQ(1, chainedCallCount); } KJ_TEST("clone() with caps") { int dummy = 0; MallocMessageBuilder builder(2048); auto root = builder.getRoot().initAs>(3); root.set(0, kj::heap(dummy)); root.set(1, kj::heap(dummy)); root.set(2, kj::heap(dummy)); auto copyPtr = clone(root.asReader()); auto& copy = *copyPtr; KJ_ASSERT(copy.size() == 3); KJ_EXPECT(ClientHook::from(copy[0]).get() == ClientHook::from(root[0]).get()); KJ_EXPECT(ClientHook::from(copy[1]).get() == ClientHook::from(root[1]).get()); KJ_EXPECT(ClientHook::from(copy[2]).get() == ClientHook::from(root[2]).get()); KJ_EXPECT(ClientHook::from(copy[0]).get() != ClientHook::from(root[1]).get()); KJ_EXPECT(ClientHook::from(copy[1]).get() != ClientHook::from(root[2]).get()); KJ_EXPECT(ClientHook::from(copy[2]).get() != ClientHook::from(root[0]).get()); } KJ_TEST("Streaming calls block subsequent calls") { kj::EventLoop loop; kj::WaitScope waitScope(loop); auto ownServer = kj::heap(); auto& server = *ownServer; test::TestStreaming::Client cap = kj::mv(ownServer); kj::Promise promise1 = nullptr, promise2 = nullptr, promise3 = nullptr; { auto req = cap.doStreamIRequest(); req.setI(123); promise1 = req.send(); } { auto req = cap.doStreamJRequest(); req.setJ(321); promise2 = req.send(); } { auto req = cap.doStreamIRequest(); req.setI(456); promise3 = req.send(); } auto promise4 = cap.finishStreamRequest().send(); KJ_EXPECT(server.iSum == 0); KJ_EXPECT(server.jSum == 0); KJ_EXPECT(!promise1.poll(waitScope)); KJ_EXPECT(!promise2.poll(waitScope)); KJ_EXPECT(!promise3.poll(waitScope)); KJ_EXPECT(!promise4.poll(waitScope)); KJ_EXPECT(server.iSum == 123); KJ_EXPECT(server.jSum == 0); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(promise1.poll(waitScope)); KJ_EXPECT(!promise2.poll(waitScope)); KJ_EXPECT(!promise3.poll(waitScope)); KJ_EXPECT(!promise4.poll(waitScope)); KJ_EXPECT(server.iSum == 123); KJ_EXPECT(server.jSum == 321); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(promise1.poll(waitScope)); KJ_EXPECT(promise2.poll(waitScope)); KJ_EXPECT(!promise3.poll(waitScope)); KJ_EXPECT(!promise4.poll(waitScope)); KJ_EXPECT(server.iSum == 579); KJ_EXPECT(server.jSum == 321); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(promise1.poll(waitScope)); KJ_EXPECT(promise2.poll(waitScope)); KJ_EXPECT(promise3.poll(waitScope)); KJ_EXPECT(promise4.poll(waitScope)); auto result = promise4.wait(waitScope); KJ_EXPECT(result.getTotalI() == 579); KJ_EXPECT(result.getTotalJ() == 321); } KJ_TEST("Streaming calls can be canceled") { kj::EventLoop loop; kj::WaitScope waitScope(loop); auto ownServer = kj::heap(); auto& server = *ownServer; test::TestStreaming::Client cap = kj::mv(ownServer); kj::Promise promise1 = nullptr, promise2 = nullptr, promise3 = nullptr; { auto req = cap.doStreamIRequest(); req.setI(123); promise1 = req.send(); } { auto req = cap.doStreamJRequest(); req.setJ(321); promise2 = req.send(); } { auto req = cap.doStreamIRequest(); req.setI(456); promise3 = req.send(); } auto promise4 = cap.finishStreamRequest().send(); // Cancel the streaming calls. promise1 = nullptr; promise2 = nullptr; promise3 = nullptr; KJ_EXPECT(server.iSum == 0); KJ_EXPECT(server.jSum == 0); KJ_EXPECT(!promise4.poll(waitScope)); KJ_EXPECT(server.iSum == 123); KJ_EXPECT(server.jSum == 0); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(!promise4.poll(waitScope)); // The call to doStreamJ() opted into cancellation so the next call to doStreamI() happens // immediately. KJ_EXPECT(server.iSum == 579); KJ_EXPECT(server.jSum == 321); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(promise4.poll(waitScope)); auto result = promise4.wait(waitScope); KJ_EXPECT(result.getTotalI() == 579); KJ_EXPECT(result.getTotalJ() == 321); } KJ_TEST("Streaming call throwing cascades to following calls") { kj::EventLoop loop; kj::WaitScope waitScope(loop); auto ownServer = kj::heap(); auto& server = *ownServer; test::TestStreaming::Client cap = kj::mv(ownServer); server.jShouldThrow = true; kj::Promise promise1 = nullptr, promise2 = nullptr, promise3 = nullptr; { auto req = cap.doStreamIRequest(); req.setI(123); promise1 = req.send(); } { auto req = cap.doStreamJRequest(); req.setJ(321); promise2 = req.send(); } { auto req = cap.doStreamIRequest(); req.setI(456); promise3 = req.send(); } auto promise4 = cap.finishStreamRequest().send(); KJ_EXPECT(server.iSum == 0); KJ_EXPECT(server.jSum == 0); KJ_EXPECT(!promise1.poll(waitScope)); KJ_EXPECT(!promise2.poll(waitScope)); KJ_EXPECT(!promise3.poll(waitScope)); KJ_EXPECT(!promise4.poll(waitScope)); KJ_EXPECT(server.iSum == 123); KJ_EXPECT(server.jSum == 0); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(promise1.poll(waitScope)); KJ_EXPECT(promise2.poll(waitScope)); KJ_EXPECT(promise3.poll(waitScope)); KJ_EXPECT(promise4.poll(waitScope)); KJ_EXPECT(server.iSum == 123); KJ_EXPECT(server.jSum == 321); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("throw requested", promise2.wait(waitScope)); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("throw requested", promise3.wait(waitScope)); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("throw requested", promise4.ignoreResult().wait(waitScope)); } } // namespace } // namespace _ } // namespace capnp capnproto-c++-0.8.0/src/capnp/membrane-test.c++0000644000175000017500000003253713650101756021713 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "membrane.h" #include #include "test-util.h" #include #include #include "rpc-twoparty.h" namespace capnp { namespace _ { namespace { using Thing = test::TestMembrane::Thing; class ThingImpl final: public Thing::Server { public: ThingImpl(kj::StringPtr text): text(text) {} protected: kj::Promise passThrough(PassThroughContext context) override { context.getResults().setText(text); return kj::READY_NOW; } kj::Promise intercept(InterceptContext context) override { context.getResults().setText(text); return kj::READY_NOW; } private: kj::StringPtr text; }; class TestMembraneImpl final: public test::TestMembrane::Server { protected: kj::Promise makeThing(MakeThingContext context) override { context.getResults().setThing(kj::heap("inside")); return kj::READY_NOW; } kj::Promise callPassThrough(CallPassThroughContext context) override { auto params = context.getParams(); auto req = params.getThing().passThroughRequest(); if (params.getTailCall()) { return context.tailCall(kj::mv(req)); } else { return req.send().then( [KJ_CPCAP(context)](Response&& result) mutable { context.setResults(result); }); } } kj::Promise callIntercept(CallInterceptContext context) override { auto params = context.getParams(); auto req = params.getThing().interceptRequest(); if (params.getTailCall()) { return context.tailCall(kj::mv(req)); } else { return req.send().then( [KJ_CPCAP(context)](Response&& result) mutable { context.setResults(result); }); } } kj::Promise loopback(LoopbackContext context) override { context.getResults().setThing(context.getParams().getThing()); return kj::READY_NOW; } kj::Promise waitForever(WaitForeverContext context) override { context.allowCancellation(); return kj::NEVER_DONE; } }; class MembranePolicyImpl: public MembranePolicy, public kj::Refcounted { public: MembranePolicyImpl() = default; MembranePolicyImpl(kj::Maybe> revokePromise) : revokePromise(revokePromise.map([](kj::Promise& p) { return p.fork(); })) {} kj::Maybe inboundCall(uint64_t interfaceId, uint16_t methodId, Capability::Client target) override { if (interfaceId == capnp::typeId() && methodId == 1) { return Capability::Client(kj::heap("inbound")); } else { return nullptr; } } kj::Maybe outboundCall(uint64_t interfaceId, uint16_t methodId, Capability::Client target) override { if (interfaceId == capnp::typeId() && methodId == 1) { return Capability::Client(kj::heap("outbound")); } else { return nullptr; } } kj::Own addRef() override { return kj::addRef(*this); } kj::Maybe> onRevoked() override { return revokePromise.map([](kj::ForkedPromise& fork) { return fork.addBranch(); }); } private: kj::Maybe> revokePromise; }; void testThingImpl(kj::WaitScope& waitScope, test::TestMembrane::Client membraned, kj::Function makeThing, kj::StringPtr localPassThrough, kj::StringPtr localIntercept, kj::StringPtr remotePassThrough, kj::StringPtr remoteIntercept) { KJ_EXPECT(makeThing().passThroughRequest().send().wait(waitScope).getText() == localPassThrough); KJ_EXPECT(makeThing().interceptRequest().send().wait(waitScope).getText() == localIntercept); { auto req = membraned.callPassThroughRequest(); req.setThing(makeThing()); req.setTailCall(false); KJ_EXPECT(req.send().wait(waitScope).getText() == remotePassThrough); } { auto req = membraned.callInterceptRequest(); req.setThing(makeThing()); req.setTailCall(false); KJ_EXPECT(req.send().wait(waitScope).getText() == remoteIntercept); } { auto req = membraned.callPassThroughRequest(); req.setThing(makeThing()); req.setTailCall(true); KJ_EXPECT(req.send().wait(waitScope).getText() == remotePassThrough); } { auto req = membraned.callInterceptRequest(); req.setThing(makeThing()); req.setTailCall(true); KJ_EXPECT(req.send().wait(waitScope).getText() == remoteIntercept); } } struct TestEnv { kj::EventLoop loop; kj::WaitScope waitScope; kj::Own policy; test::TestMembrane::Client membraned; TestEnv() : waitScope(loop), policy(kj::refcounted()), membraned(membrane(kj::heap(), policy->addRef())) {} void testThing(kj::Function makeThing, kj::StringPtr localPassThrough, kj::StringPtr localIntercept, kj::StringPtr remotePassThrough, kj::StringPtr remoteIntercept) { testThingImpl(waitScope, membraned, kj::mv(makeThing), localPassThrough, localIntercept, remotePassThrough, remoteIntercept); } }; KJ_TEST("call local object inside membrane") { TestEnv env; env.testThing([&]() { return env.membraned.makeThingRequest().send().wait(env.waitScope).getThing(); }, "inside", "inbound", "inside", "inside"); } KJ_TEST("call local promise inside membrane") { TestEnv env; env.testThing([&]() { return env.membraned.makeThingRequest().send().getThing(); }, "inside", "inbound", "inside", "inside"); } KJ_TEST("call local resolved promise inside membrane") { TestEnv env; env.testThing([&]() { auto thing = env.membraned.makeThingRequest().send().getThing(); thing.whenResolved().wait(env.waitScope); return thing; }, "inside", "inbound", "inside", "inside"); } KJ_TEST("call local object outside membrane") { TestEnv env; env.testThing([&]() { return kj::heap("outside"); }, "outside", "outside", "outside", "outbound"); } KJ_TEST("call local capability that has passed into and back out of membrane") { TestEnv env; env.testThing([&]() { auto req = env.membraned.loopbackRequest(); req.setThing(kj::heap("outside")); return req.send().wait(env.waitScope).getThing(); }, "outside", "outside", "outside", "outbound"); } KJ_TEST("call local promise pointing into membrane that eventually resolves to outside") { TestEnv env; env.testThing([&]() { auto req = env.membraned.loopbackRequest(); req.setThing(kj::heap("outside")); return req.send().getThing(); }, "outside", "outside", "outside", "outbound"); } KJ_TEST("apply membrane using copyOutOfMembrane() on struct") { TestEnv env; env.testThing([&]() { MallocMessageBuilder outsideBuilder; auto root = outsideBuilder.initRoot(); root.setCap(kj::heap("inside")); MallocMessageBuilder insideBuilder; insideBuilder.adoptRoot(copyOutOfMembrane( root.asReader(), insideBuilder.getOrphanage(), env.policy->addRef())); return insideBuilder.getRoot().getCap(); }, "inside", "inbound", "inside", "inside"); } KJ_TEST("apply membrane using copyOutOfMembrane() on list") { TestEnv env; env.testThing([&]() { MallocMessageBuilder outsideBuilder; auto list = outsideBuilder.initRoot().initList(1); list.set(0, kj::heap("inside")); MallocMessageBuilder insideBuilder; insideBuilder.initRoot().adoptList(copyOutOfMembrane( list.asReader(), insideBuilder.getOrphanage(), env.policy->addRef())); return insideBuilder.getRoot().getList()[0]; }, "inside", "inbound", "inside", "inside"); } KJ_TEST("apply membrane using copyOutOfMembrane() on AnyPointer") { TestEnv env; env.testThing([&]() { MallocMessageBuilder outsideBuilder; auto ptr = outsideBuilder.initRoot().getAnyPointerField(); ptr.setAs(kj::heap("inside")); MallocMessageBuilder insideBuilder; insideBuilder.initRoot().getAnyPointerField().adopt(copyOutOfMembrane( ptr.asReader(), insideBuilder.getOrphanage(), env.policy->addRef())); return insideBuilder.getRoot().getAnyPointerField() .getAs(); }, "inside", "inbound", "inside", "inside"); } struct TestRpcEnv { kj::EventLoop loop; kj::WaitScope waitScope; kj::TwoWayPipe pipe; TwoPartyClient client; TwoPartyClient server; test::TestMembrane::Client membraned; TestRpcEnv(kj::Maybe> revokePromise = nullptr) : waitScope(loop), pipe(kj::newTwoWayPipe()), client(*pipe.ends[0]), server(*pipe.ends[1], membrane(kj::heap(), kj::refcounted(kj::mv(revokePromise))), rpc::twoparty::Side::SERVER), membraned(client.bootstrap().castAs()) {} void testThing(kj::Function makeThing, kj::StringPtr localPassThrough, kj::StringPtr localIntercept, kj::StringPtr remotePassThrough, kj::StringPtr remoteIntercept) { testThingImpl(waitScope, membraned, kj::mv(makeThing), localPassThrough, localIntercept, remotePassThrough, remoteIntercept); } }; KJ_TEST("call remote object inside membrane") { TestRpcEnv env; env.testThing([&]() { return env.membraned.makeThingRequest().send().wait(env.waitScope).getThing(); }, "inside", "inbound", "inside", "inside"); } KJ_TEST("call remote promise inside membrane") { TestRpcEnv env; env.testThing([&]() { return env.membraned.makeThingRequest().send().getThing(); }, "inside", "inbound", "inside", "inside"); } KJ_TEST("call remote resolved promise inside membrane") { TestEnv env; env.testThing([&]() { auto thing = env.membraned.makeThingRequest().send().getThing(); thing.whenResolved().wait(env.waitScope); return thing; }, "inside", "inbound", "inside", "inside"); } KJ_TEST("call remote object outside membrane") { TestRpcEnv env; env.testThing([&]() { return kj::heap("outside"); }, "outside", "outside", "outside", "outbound"); } KJ_TEST("call remote capability that has passed into and back out of membrane") { TestRpcEnv env; env.testThing([&]() { auto req = env.membraned.loopbackRequest(); req.setThing(kj::heap("outside")); return req.send().wait(env.waitScope).getThing(); }, "outside", "outside", "outside", "outbound"); } KJ_TEST("call remote promise pointing into membrane that eventually resolves to outside") { TestRpcEnv env; env.testThing([&]() { auto req = env.membraned.loopbackRequest(); req.setThing(kj::heap("outside")); return req.send().getThing(); }, "outside", "outside", "outside", "outbound"); } KJ_TEST("revoke membrane") { auto paf = kj::newPromiseAndFulfiller(); TestRpcEnv env(kj::mv(paf.promise)); auto thing = env.membraned.makeThingRequest().send().wait(env.waitScope).getThing(); auto callPromise = env.membraned.waitForeverRequest().send(); KJ_EXPECT(!callPromise.poll(env.waitScope)); paf.fulfiller->reject(KJ_EXCEPTION(DISCONNECTED, "foobar")); // TRICKY: We need to use .ignoreResult().wait() below because when compiling with // -fno-exceptions, void waits throw recoverable exceptions while non-void waits necessarily // throw fatal exceptions... but testing for fatal exceptions when exceptions are disabled // involves fork()ing the process to run the code so if it has side effects on file descriptors // then we'll get in a bad state... KJ_ASSERT(callPromise.poll(env.waitScope)); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", callPromise.ignoreResult().wait(env.waitScope)); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", env.membraned.makeThingRequest().send().ignoreResult().wait(env.waitScope)); KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", thing.passThroughRequest().send().ignoreResult().wait(env.waitScope)); } } // namespace } // namespace _ } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema-test.c++0000644000175000017500000004060713340402540021351 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_TESTING_CAPNP 1 #include "schema.h" #include #include "test-util.h" // TODO(cleanup): Auto-generate stringification functions for union discriminants. namespace capnp { namespace schema { inline kj::String KJ_STRINGIFY(Type::Which which) { return kj::str(static_cast(which)); } } // namespace schema } // namespace capnp namespace capnp { namespace _ { // private namespace { TEST(Schema, Structs) { StructSchema schema = Schema::from(); EXPECT_EQ(typeId(), schema.getProto().getId()); EXPECT_TRUE(schema.getDependency(typeId()) == Schema::from()); EXPECT_TRUE(schema.getDependency(typeId()) != schema); EXPECT_TRUE(schema.getDependency(typeId()) == Schema::from()); EXPECT_TRUE(schema.getDependency(typeId()) == schema); EXPECT_NONFATAL_FAILURE(schema.getDependency(typeId())); EXPECT_TRUE(schema.asStruct() == schema); EXPECT_NONFATAL_FAILURE(schema.asEnum()); EXPECT_NONFATAL_FAILURE(schema.asInterface()); ASSERT_EQ(schema.getFields().size(), schema.getProto().getStruct().getFields().size()); StructSchema::Field field = schema.getFields()[0]; EXPECT_EQ("voidField", field.getProto().getName()); EXPECT_TRUE(field.getContainingStruct() == schema); StructSchema::Field lookup = schema.getFieldByName("voidField"); EXPECT_TRUE(lookup == field); EXPECT_TRUE(lookup != schema.getFields()[1]); EXPECT_FALSE(lookup.getProto().getSlot().getHadExplicitDefault()); EXPECT_FALSE(schema.getFieldByName("int32Field").getProto().getSlot().getHadExplicitDefault()); EXPECT_TRUE(Schema::from().getFieldByName("int32Field") .getProto().getSlot().getHadExplicitDefault()); EXPECT_TRUE(schema.findFieldByName("noSuchField") == nullptr); EXPECT_TRUE(schema.findFieldByName("int32Field") != nullptr); EXPECT_TRUE(schema.findFieldByName("float32List") != nullptr); EXPECT_TRUE(schema.findFieldByName("dataList") != nullptr); EXPECT_TRUE(schema.findFieldByName("textField") != nullptr); EXPECT_TRUE(schema.findFieldByName("structField") != nullptr); } TEST(Schema, FieldLookupOutOfOrder) { // Tests that name lookup works correctly when the fields are defined out-of-order in the schema // file. auto schema = Schema::from().asStruct(); EXPECT_EQ("qux", schema.getFields()[0].getProto().getName()); EXPECT_EQ("grault", schema.getFields()[1].getProto().getName()); EXPECT_EQ("bar", schema.getFields()[2].getProto().getName()); EXPECT_EQ("foo", schema.getFields()[3].getProto().getName()); EXPECT_EQ("corge", schema.getFields()[4].getProto().getName()); EXPECT_EQ("waldo", schema.getFields()[5].getProto().getName()); EXPECT_EQ("quux", schema.getFields()[6].getProto().getName()); EXPECT_EQ("garply", schema.getFields()[7].getProto().getName()); EXPECT_EQ("baz", schema.getFields()[8].getProto().getName()); EXPECT_EQ(3, schema.getFieldByName("foo").getProto().getOrdinal().getExplicit()); EXPECT_EQ(2, schema.getFieldByName("bar").getProto().getOrdinal().getExplicit()); EXPECT_EQ(8, schema.getFieldByName("baz").getProto().getOrdinal().getExplicit()); EXPECT_EQ(0, schema.getFieldByName("qux").getProto().getOrdinal().getExplicit()); EXPECT_EQ(6, schema.getFieldByName("quux").getProto().getOrdinal().getExplicit()); EXPECT_EQ(4, schema.getFieldByName("corge").getProto().getOrdinal().getExplicit()); EXPECT_EQ(1, schema.getFieldByName("grault").getProto().getOrdinal().getExplicit()); EXPECT_EQ(7, schema.getFieldByName("garply").getProto().getOrdinal().getExplicit()); EXPECT_EQ(5, schema.getFieldByName("waldo").getProto().getOrdinal().getExplicit()); } TEST(Schema, Unions) { auto schema = Schema::from().asStruct(); EXPECT_TRUE(schema.findFieldByName("bit0") != nullptr); EXPECT_TRUE(schema.findFieldByName("u1f0s8") == nullptr); auto union1 = schema.getFieldByName("union1"); auto union1g = schema.getDependency(union1.getProto().getGroup().getTypeId()).asStruct(); EXPECT_EQ(schema, union1g.getDependency(union1g.getProto().getScopeId())); EXPECT_TRUE(union1g.findFieldByName("bin0") == nullptr); auto u1f0s8 = union1g.getFieldByName("u1f0s8"); EXPECT_EQ("u1f0s8", u1f0s8.getProto().getName()); EXPECT_TRUE(u1f0s8.getContainingStruct() == union1g); EXPECT_TRUE(union1g.findFieldByName("u1f1s8") != nullptr); EXPECT_TRUE(union1g.findFieldByName("u1f0s32") != nullptr); EXPECT_TRUE(union1g.findFieldByName("u1f0sp") != nullptr); EXPECT_TRUE(union1g.findFieldByName("u1f1s1") != nullptr); EXPECT_TRUE(union1g.findFieldByName("u0f0s1") == nullptr); EXPECT_TRUE(union1g.findFieldByName("u2f0s8") == nullptr); EXPECT_TRUE(union1g.findFieldByName("noSuchField") == nullptr); } TEST(Schema, Enums) { EnumSchema schema = Schema::from(); EXPECT_EQ(typeId(), schema.getProto().getId()); EXPECT_NONFATAL_FAILURE(schema.getDependency(typeId())); EXPECT_NONFATAL_FAILURE(schema.getDependency(typeId())); EXPECT_NONFATAL_FAILURE(schema.asStruct()); EXPECT_NONFATAL_FAILURE(schema.asInterface()); EXPECT_TRUE(schema.asEnum() == schema); ASSERT_EQ(schema.getEnumerants().size(), schema.getProto().getEnum().getEnumerants().size()); EnumSchema::Enumerant enumerant = schema.getEnumerants()[0]; EXPECT_EQ("foo", enumerant.getProto().getName()); EXPECT_TRUE(enumerant.getContainingEnum() == schema); EnumSchema::Enumerant lookup = schema.getEnumerantByName("foo"); EXPECT_TRUE(lookup == enumerant); EXPECT_TRUE(lookup != schema.getEnumerants()[1]); EXPECT_TRUE(schema.findEnumerantByName("noSuchEnumerant") == nullptr); EXPECT_TRUE(schema.findEnumerantByName("bar") != nullptr); EXPECT_TRUE(schema.findEnumerantByName("qux") != nullptr); EXPECT_TRUE(schema.findEnumerantByName("corge") != nullptr); EXPECT_TRUE(schema.findEnumerantByName("grault") != nullptr); } // TODO(someday): Test interface schemas when interfaces are implemented. TEST(Schema, Lists) { EXPECT_EQ(schema::Type::VOID, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::BOOL, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::INT8, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::INT16, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::INT32, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::INT64, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::UINT8, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::UINT16, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::UINT32, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::UINT64, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::FLOAT32, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::FLOAT64, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::TEXT, Schema::from>().whichElementType()); EXPECT_EQ(schema::Type::DATA, Schema::from>().whichElementType()); EXPECT_NONFATAL_FAILURE(Schema::from>().getStructElementType()); EXPECT_NONFATAL_FAILURE(Schema::from>().getEnumElementType()); EXPECT_NONFATAL_FAILURE(Schema::from>().getInterfaceElementType()); EXPECT_NONFATAL_FAILURE(Schema::from>().getListElementType()); { ListSchema schema = Schema::from>(); EXPECT_EQ(schema::Type::STRUCT, schema.whichElementType()); EXPECT_TRUE(schema.getStructElementType() == Schema::from()); EXPECT_NONFATAL_FAILURE(schema.getEnumElementType()); EXPECT_NONFATAL_FAILURE(schema.getInterfaceElementType()); EXPECT_NONFATAL_FAILURE(schema.getListElementType()); } { ListSchema schema = Schema::from>(); EXPECT_EQ(schema::Type::ENUM, schema.whichElementType()); EXPECT_TRUE(schema.getEnumElementType() == Schema::from()); EXPECT_NONFATAL_FAILURE(schema.getStructElementType()); EXPECT_NONFATAL_FAILURE(schema.getInterfaceElementType()); EXPECT_NONFATAL_FAILURE(schema.getListElementType()); } // TODO(someday): Test interfaces. { ListSchema schema = Schema::from>>(); EXPECT_EQ(schema::Type::LIST, schema.whichElementType()); EXPECT_NONFATAL_FAILURE(schema.getStructElementType()); EXPECT_NONFATAL_FAILURE(schema.getEnumElementType()); EXPECT_NONFATAL_FAILURE(schema.getInterfaceElementType()); ListSchema inner = schema.getListElementType(); EXPECT_EQ(schema::Type::INT32, inner.whichElementType()); } { ListSchema schema = Schema::from>>(); EXPECT_EQ(schema::Type::LIST, schema.whichElementType()); EXPECT_NONFATAL_FAILURE(schema.getStructElementType()); EXPECT_NONFATAL_FAILURE(schema.getEnumElementType()); EXPECT_NONFATAL_FAILURE(schema.getInterfaceElementType()); ListSchema inner = schema.getListElementType(); EXPECT_EQ(schema::Type::STRUCT, inner.whichElementType()); EXPECT_TRUE(inner.getStructElementType() == Schema::from()); } { ListSchema schema = Schema::from>>(); EXPECT_EQ(schema::Type::LIST, schema.whichElementType()); EXPECT_NONFATAL_FAILURE(schema.getStructElementType()); EXPECT_NONFATAL_FAILURE(schema.getEnumElementType()); EXPECT_NONFATAL_FAILURE(schema.getInterfaceElementType()); ListSchema inner = schema.getListElementType(); EXPECT_EQ(schema::Type::ENUM, inner.whichElementType()); EXPECT_TRUE(inner.getEnumElementType() == Schema::from()); } { auto context = Schema::from(); auto type = context.getFieldByName("enumList").getProto().getSlot().getType(); ListSchema schema = ListSchema::of(type.getList().getElementType(), context); EXPECT_EQ(schema::Type::ENUM, schema.whichElementType()); EXPECT_TRUE(schema.getEnumElementType() == Schema::from()); EXPECT_NONFATAL_FAILURE(schema.getStructElementType()); EXPECT_NONFATAL_FAILURE(schema.getInterfaceElementType()); EXPECT_NONFATAL_FAILURE(schema.getListElementType()); } } TEST(Schema, UnnamedUnion) { StructSchema schema = Schema::from(); EXPECT_TRUE(schema.findFieldByName("") == nullptr); EXPECT_TRUE(schema.findFieldByName("foo") != nullptr); EXPECT_TRUE(schema.findFieldByName("bar") != nullptr); EXPECT_TRUE(schema.findFieldByName("before") != nullptr); EXPECT_TRUE(schema.findFieldByName("after") != nullptr); } TEST(Schema, NullSchemas) { EXPECT_EQ(0xff, (uint)Schema().getProto().which()); EXPECT_TRUE(StructSchema().getProto().isStruct()); EXPECT_TRUE(EnumSchema().getProto().isEnum()); EXPECT_TRUE(InterfaceSchema().getProto().isInterface()); EXPECT_TRUE(ConstSchema().getProto().isConst()); EXPECT_EQ("(null schema)", Schema().getProto().getDisplayName()); EXPECT_EQ("(null struct schema)", StructSchema().getProto().getDisplayName()); EXPECT_EQ("(null enum schema)", EnumSchema().getProto().getDisplayName()); EXPECT_EQ("(null interface schema)", InterfaceSchema().getProto().getDisplayName()); EXPECT_EQ("(null const schema)", ConstSchema().getProto().getDisplayName()); EXPECT_TRUE(Schema::from() == InterfaceSchema()); EXPECT_EQ(InterfaceSchema().getProto().getId(), typeId()); } TEST(Schema, Interfaces) { InterfaceSchema schema = Schema::from(); EXPECT_EQ(typeId(), schema.getProto().getId()); EXPECT_TRUE(schema.getDependency(typeId()) == Schema::from()); EXPECT_TRUE(schema.getDependency(typeId()) != schema); EXPECT_NONFATAL_FAILURE(schema.getDependency(typeId())); EXPECT_TRUE(schema.asInterface() == schema); EXPECT_NONFATAL_FAILURE(schema.asStruct()); EXPECT_NONFATAL_FAILURE(schema.asEnum()); ASSERT_EQ(schema.getMethods().size(), schema.getProto().getInterface().getMethods().size()); InterfaceSchema::Method method = schema.getMethods()[0]; EXPECT_EQ("callFoo", method.getProto().getName()); EXPECT_TRUE(method.getContainingInterface() == schema); InterfaceSchema::Method lookup = schema.getMethodByName("callFoo"); EXPECT_TRUE(lookup == method); EXPECT_TRUE(lookup != schema.getMethods()[1]); EXPECT_TRUE(Schema::from().getFieldByName("int32Field") .getProto().getSlot().getHadExplicitDefault()); EXPECT_TRUE(schema.findMethodByName("noSuchMethod") == nullptr); EXPECT_TRUE(schema.findMethodByName("callFooWhenResolved") != nullptr); EXPECT_TRUE(schema.findMethodByName("neverReturn") != nullptr); EXPECT_TRUE(schema.findMethodByName("hold") != nullptr); EXPECT_TRUE(schema.findMethodByName("callHeld") != nullptr); EXPECT_TRUE(schema.findMethodByName("getHeld") != nullptr); auto params = schema.getDependency(schema.getMethodByName("methodWithDefaults") .getProto().getParamStructType()).asStruct(); EXPECT_FALSE(params.getFieldByName("a").getProto().getSlot().getHadExplicitDefault()); EXPECT_TRUE(params.getFieldByName("b").getProto().getSlot().getHadExplicitDefault()); EXPECT_TRUE(params.getFieldByName("c").getProto().getSlot().getHadExplicitDefault()); } TEST(Schema, Generics) { StructSchema allTypes = Schema::from(); StructSchema tap = Schema::from(); StructSchema schema = Schema::from(); StructSchema branded; { StructSchema::Field basic = schema.getFieldByName("basic"); branded = basic.getType().asStruct(); StructSchema::Field foo = branded.getFieldByName("foo"); EXPECT_TRUE(foo.getType().asStruct() == allTypes); EXPECT_TRUE(foo.getType().asStruct() != tap); StructSchema instance2 = branded.getFieldByName("rev").getType().asStruct(); StructSchema::Field foo2 = instance2.getFieldByName("foo"); EXPECT_TRUE(foo2.getType().asStruct() == tap); EXPECT_TRUE(foo2.getType().asStruct() != allTypes); } { StructSchema inner2 = schema.getFieldByName("inner2").getType().asStruct(); StructSchema bound = inner2.getFieldByName("innerBound").getType().asStruct(); Type boundFoo = bound.getFieldByName("foo").getType(); EXPECT_FALSE(boundFoo.isAnyPointer()); EXPECT_TRUE(boundFoo.asStruct() == allTypes); StructSchema unbound = inner2.getFieldByName("innerUnbound").getType().asStruct(); Type unboundFoo = unbound.getFieldByName("foo").getType(); EXPECT_TRUE(unboundFoo.isAnyPointer()); } { InterfaceSchema cap = schema.getFieldByName("genericCap").getType().asInterface(); InterfaceSchema::Method method = cap.getMethodByName("call"); StructSchema inner2 = method.getParamType(); StructSchema bound = inner2.getFieldByName("innerBound").getType().asStruct(); Type boundFoo = bound.getFieldByName("foo").getType(); EXPECT_FALSE(boundFoo.isAnyPointer()); EXPECT_TRUE(boundFoo.asStruct() == allTypes); EXPECT_TRUE(inner2.getFieldByName("baz").getType().isText()); StructSchema results = method.getResultType(); EXPECT_TRUE(results.getFieldByName("qux").getType().isData()); EXPECT_TRUE(results.getFieldByName("gen").getType().asStruct() == branded); } } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema-loader-test.c++0000644000175000017500000003401313650101756022620 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_TESTING_CAPNP 1 #include "schema-loader.h" #include #include "test-util.h" #include namespace capnp { namespace _ { // private namespace { TEST(SchemaLoader, Load) { SchemaLoader loader; Schema struct32Schema = loader.load(Schema::from().getProto()); auto nativeSchema = Schema::from(); Schema testListsSchema = loader.load(nativeSchema.getProto()); Schema struct8Schema = loader.load(Schema::from().getProto()); Schema structPSchema = loader.load(Schema::from().getProto()); EXPECT_EQ(kj::str(nativeSchema.getProto()), kj::str(testListsSchema.getProto())); EXPECT_FALSE(testListsSchema == nativeSchema); EXPECT_FALSE(struct32Schema == Schema::from()); EXPECT_FALSE(struct8Schema == Schema::from()); EXPECT_FALSE(structPSchema == Schema::from()); EXPECT_TRUE(testListsSchema.getDependency(typeId()) == struct32Schema); EXPECT_TRUE(testListsSchema.getDependency(typeId()) == struct8Schema); EXPECT_TRUE(testListsSchema.getDependency(typeId()) == structPSchema); auto struct16Schema = testListsSchema.getDependency(typeId()); EXPECT_EQ(0u, struct16Schema.getProto().getStruct().getFields().size()); } TEST(SchemaLoader, LoadLateUnion) { SchemaLoader loader; StructSchema schema = loader.load(Schema::from().getProto()).asStruct(); loader.load(Schema::from().getProto()).asStruct(); loader.load(Schema::from().getProto()).asStruct(); EXPECT_EQ(6, schema.getDependency(schema.getFieldByName("theUnion").getProto().getGroup().getTypeId()) .asStruct().getFieldByName("grault").getProto().getOrdinal().getExplicit()); EXPECT_EQ(9, schema.getDependency(schema.getFieldByName("anotherUnion").getProto().getGroup().getTypeId()) .asStruct().getFieldByName("corge").getProto().getOrdinal().getExplicit()); EXPECT_TRUE(schema.findFieldByName("corge") == nullptr); EXPECT_TRUE(schema.findFieldByName("grault") == nullptr); } TEST(SchemaLoader, LoadUnnamedUnion) { SchemaLoader loader; StructSchema schema = loader.load(Schema::from().getProto()).asStruct(); EXPECT_TRUE(schema.findFieldByName("") == nullptr); EXPECT_TRUE(schema.findFieldByName("foo") != nullptr); EXPECT_TRUE(schema.findFieldByName("bar") != nullptr); EXPECT_TRUE(schema.findFieldByName("before") != nullptr); EXPECT_TRUE(schema.findFieldByName("after") != nullptr); } TEST(SchemaLoader, Use) { SchemaLoader loader; StructSchema schema = loader.load(Schema::from().getProto()).asStruct(); // Also have to load TestEnum. loader.load(Schema::from().getProto()); { MallocMessageBuilder builder; auto root = builder.getRoot(schema); initDynamicTestMessage(root); checkDynamicTestMessage(root.asReader()); // Can't convert to TestAllTypes because we didn't use loadCompiledTypeAndDependencies(). EXPECT_ANY_THROW(root.as()); // But if we reinterpret the raw bytes, it works. checkTestMessage(builder.getRoot()); } loader.loadCompiledTypeAndDependencies(); { MallocMessageBuilder builder; auto root = builder.getRoot(schema); initDynamicTestMessage(root); // Now we can actually cast. checkTestMessage(root.as()); } // Let's also test TestListDefaults, but as we do so, let's load the compiled types first, to // make sure the opposite order works. loader.loadCompiledTypeAndDependencies(); StructSchema testListsSchema = loader.get(typeId()).asStruct(); EXPECT_TRUE(testListsSchema != Schema::from()); { MallocMessageBuilder builder; auto root = builder.getRoot(testListsSchema); initDynamicTestLists(root); checkDynamicTestLists(root.asReader()); checkTestMessage(root.as()); } EXPECT_TRUE(loader.load(Schema::from().getProto()) == testListsSchema); { MallocMessageBuilder builder; auto root = builder.getRoot(testListsSchema); initDynamicTestLists(root); checkTestMessage(root.as()); } // Finally, let's test some unions. StructSchema unionSchema = loader.load(Schema::from().getProto()).asStruct(); loader.load(Schema::from().getProto()); loader.load(Schema::from().getProto()); { MallocMessageBuilder builder; auto root = builder.getRoot(unionSchema); root.get("union0").as().set("u0f1s16", 123); root.get("union1").as().set("u1f0sp", "hello"); auto reader = builder.getRoot().asReader(); EXPECT_EQ(123, reader.getUnion0().getU0f1s16()); EXPECT_EQ("hello", reader.getUnion1().getU1f0sp()); } } template Schema loadUnderAlternateTypeId(SchemaLoader& loader, uint64_t id) { MallocMessageBuilder schemaBuilder; schemaBuilder.setRoot(Schema::from().getProto()); auto root = schemaBuilder.getRoot(); root.setId(id); if (root.isStruct()) { // If the struct contains any self-referential members, change their type IDs as well. auto fields = root.getStruct().getFields(); for (auto field: fields) { if (field.isSlot()) { auto type = field.getSlot().getType(); if (type.isStruct() && type.getStruct().getTypeId() == typeId()) { type.getStruct().setTypeId(id); } } } } return loader.load(root); } TEST(SchemaLoader, Upgrade) { SchemaLoader loader; loader.loadCompiledTypeAndDependencies(); StructSchema schema = loader.get(typeId()).asStruct(); EXPECT_EQ(kj::str(Schema::from().getProto()), kj::str(schema.getProto())); loadUnderAlternateTypeId(loader, typeId()); // The new version replaced the old. EXPECT_EQ(Schema::from().getProto().getDisplayName(), schema.getProto().getDisplayName()); // But it is still usable as the old version. schema.requireUsableAs(); } TEST(SchemaLoader, Downgrade) { SchemaLoader loader; loader.loadCompiledTypeAndDependencies(); StructSchema schema = loader.get(typeId()).asStruct(); EXPECT_EQ(kj::str(Schema::from().getProto()), kj::str(schema.getProto())); loadUnderAlternateTypeId(loader, typeId()); // We kept the new version, because the replacement was older. EXPECT_EQ(Schema::from().getProto().getDisplayName(), schema.getProto().getDisplayName()); schema.requireUsableAs(); } TEST(SchemaLoader, Incompatible) { SchemaLoader loader; loader.loadCompiledTypeAndDependencies(); EXPECT_NONFATAL_FAILURE( loadUnderAlternateTypeId(loader, typeId())); } TEST(SchemaLoader, Enumerate) { SchemaLoader loader; loader.loadCompiledTypeAndDependencies(); auto list = loader.getAllLoaded(); ASSERT_EQ(2u, list.size()); if (list[0] == loader.get(typeId())) { EXPECT_TRUE(list[1] == loader.get(typeId())); } else { EXPECT_TRUE(list[0] == loader.get(typeId())); EXPECT_TRUE(list[1] == loader.get(typeId())); } } TEST(SchemaLoader, EnumerateNoPlaceholders) { SchemaLoader loader; Schema schema = loader.load(Schema::from().getProto()); { auto list = loader.getAllLoaded(); ASSERT_EQ(1u, list.size()); EXPECT_TRUE(list[0] == schema); } Schema dep = schema.getDependency(typeId()); { auto list = loader.getAllLoaded(); ASSERT_EQ(2u, list.size()); if (list[0] == schema) { EXPECT_TRUE(list[1] == dep); } else { EXPECT_TRUE(list[0] == dep); EXPECT_TRUE(list[1] == schema); } } } class FakeLoaderCallback: public SchemaLoader::LazyLoadCallback { public: FakeLoaderCallback(const schema::Node::Reader node): node(node), loaded(false) {} bool isLoaded() { return loaded; } void load(const SchemaLoader& loader, uint64_t id) const override { if (id == 1234) { // Magic "not found" ID. return; } EXPECT_EQ(node.getId(), id); EXPECT_FALSE(loaded); loaded = true; loader.loadOnce(node); } private: const schema::Node::Reader node; mutable bool loaded = false; }; TEST(SchemaLoader, LazyLoad) { FakeLoaderCallback callback(Schema::from().getProto()); SchemaLoader loader(callback); EXPECT_TRUE(loader.tryGet(1234) == nullptr); EXPECT_FALSE(callback.isLoaded()); Schema schema = loader.get(typeId()); EXPECT_TRUE(callback.isLoaded()); EXPECT_EQ(schema.getProto().getDisplayName(), Schema::from().getProto().getDisplayName()); EXPECT_EQ(schema, schema.getDependency(typeId())); EXPECT_EQ(schema, loader.get(typeId())); } TEST(SchemaLoader, LazyLoadGetDependency) { FakeLoaderCallback callback(Schema::from().getProto()); SchemaLoader loader(callback); Schema schema = loader.load(Schema::from().getProto()); EXPECT_FALSE(callback.isLoaded()); Schema dep = schema.getDependency(typeId()); EXPECT_TRUE(callback.isLoaded()); EXPECT_EQ(dep.getProto().getDisplayName(), Schema::from().getProto().getDisplayName()); EXPECT_EQ(dep, schema.getDependency(typeId())); EXPECT_EQ(dep, loader.get(typeId())); } TEST(SchemaLoader, Generics) { SchemaLoader loader; StructSchema allTypes = loader.load(Schema::from().getProto()).asStruct(); StructSchema tap = loader.load(Schema::from().getProto()).asStruct(); loader.load(Schema::from::Inner>().getProto()); loader.load(Schema::from::Inner2<>>().getProto()); loader.load(Schema::from::Interface<>>().getProto()); loader.load(Schema::from::Interface<>::CallResults>().getProto()); loader.load(Schema::from>().getProto()); StructSchema schema = loader.load(Schema::from().getProto()).asStruct(); StructSchema branded; { StructSchema::Field basic = schema.getFieldByName("basic"); branded = basic.getType().asStruct(); StructSchema::Field foo = branded.getFieldByName("foo"); EXPECT_TRUE(foo.getType().asStruct() == allTypes); EXPECT_TRUE(foo.getType().asStruct() != tap); StructSchema instance2 = branded.getFieldByName("rev").getType().asStruct(); StructSchema::Field foo2 = instance2.getFieldByName("foo"); EXPECT_TRUE(foo2.getType().asStruct() == tap); EXPECT_TRUE(foo2.getType().asStruct() != allTypes); } { StructSchema inner2 = schema.getFieldByName("inner2").getType().asStruct(); StructSchema bound = inner2.getFieldByName("innerBound").getType().asStruct(); Type boundFoo = bound.getFieldByName("foo").getType(); EXPECT_FALSE(boundFoo.isAnyPointer()); EXPECT_TRUE(boundFoo.asStruct() == allTypes); StructSchema unbound = inner2.getFieldByName("innerUnbound").getType().asStruct(); Type unboundFoo = unbound.getFieldByName("foo").getType(); EXPECT_TRUE(unboundFoo.isAnyPointer()); } { InterfaceSchema cap = schema.getFieldByName("genericCap").getType().asInterface(); InterfaceSchema::Method method = cap.getMethodByName("call"); StructSchema inner2 = method.getParamType(); StructSchema bound = inner2.getFieldByName("innerBound").getType().asStruct(); Type boundFoo = bound.getFieldByName("foo").getType(); EXPECT_FALSE(boundFoo.isAnyPointer()); EXPECT_TRUE(boundFoo.asStruct() == allTypes); EXPECT_TRUE(inner2.getFieldByName("baz").getType().isText()); StructSchema results = method.getResultType(); EXPECT_TRUE(results.getFieldByName("qux").getType().isData()); EXPECT_TRUE(results.getFieldByName("gen").getType().asStruct() == branded); } } TEST(SchemaLoader, LoadStreaming) { SchemaLoader loader; InterfaceSchema schema = loader.load(Schema::from().getProto()).asInterface(); auto results = schema.getMethodByName("doStreamI").getResultType(); KJ_EXPECT(results.isStreamResult()); KJ_EXPECT(results.getShortDisplayName() == "StreamResult", results.getShortDisplayName()); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/schema-parser-test.c++0000644000175000017500000002421413340402540022637 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_TESTING_CAPNP 1 #include "schema-parser.h" #include #include "test-util.h" #include #include namespace capnp { namespace { #if _WIN32 #define ABS(x) "C:\\" x #else #define ABS(x) "/" x #endif class FakeFileReader final: public kj::Filesystem { public: void add(kj::StringPtr name, kj::StringPtr content) { root->openFile(cwd.evalNative(name), kj::WriteMode::CREATE | kj::WriteMode::CREATE_PARENT) ->writeAll(content); } const kj::Directory& getRoot() const override { return *root; } const kj::Directory& getCurrent() const override { return *current; } kj::PathPtr getCurrentPath() const override { return cwd; } private: kj::Own root = kj::newInMemoryDirectory(kj::nullClock()); kj::Path cwd = kj::Path({}).evalNative(ABS("path/to/current/dir")); kj::Own current = root->openSubdir(cwd, kj::WriteMode::CREATE | kj::WriteMode::CREATE_PARENT); }; static uint64_t getFieldTypeFileId(StructSchema::Field field) { return field.getContainingStruct() .getDependency(field.getProto().getSlot().getType().getStruct().getTypeId()) .getProto().getScopeId(); } TEST(SchemaParser, Basic) { FakeFileReader reader; SchemaParser parser; parser.setDiskFilesystem(reader); reader.add("src/foo/bar.capnp", "@0x8123456789abcdef;\n" "struct Bar {\n" " baz @0: import \"baz.capnp\".Baz;\n" " corge @1: import \"../qux/corge.capnp\".Corge;\n" " grault @2: import \"/grault.capnp\".Grault;\n" " garply @3: import \"/garply.capnp\".Garply;\n" "}\n"); reader.add("src/foo/baz.capnp", "@0x823456789abcdef1;\n" "struct Baz {}\n"); reader.add("src/qux/corge.capnp", "@0x83456789abcdef12;\n" "struct Corge {}\n"); reader.add(ABS("usr/include/grault.capnp"), "@0x8456789abcdef123;\n" "struct Grault {}\n"); reader.add(ABS("opt/include/grault.capnp"), "@0x8000000000000001;\n" "struct WrongGrault {}\n"); reader.add(ABS("usr/local/include/garply.capnp"), "@0x856789abcdef1234;\n" "struct Garply {}\n"); kj::StringPtr importPath[] = { ABS("usr/include"), ABS("usr/local/include"), ABS("opt/include") }; ParsedSchema barSchema = parser.parseDiskFile( "foo2/bar2.capnp", "src/foo/bar.capnp", importPath); auto barProto = barSchema.getProto(); EXPECT_EQ(0x8123456789abcdefull, barProto.getId()); EXPECT_EQ("foo2/bar2.capnp", barProto.getDisplayName()); auto barStruct = barSchema.getNested("Bar"); auto barFields = barStruct.asStruct().getFields(); ASSERT_EQ(4u, barFields.size()); EXPECT_EQ("baz", barFields[0].getProto().getName()); EXPECT_EQ(0x823456789abcdef1ull, getFieldTypeFileId(barFields[0])); EXPECT_EQ("corge", barFields[1].getProto().getName()); EXPECT_EQ(0x83456789abcdef12ull, getFieldTypeFileId(barFields[1])); EXPECT_EQ("grault", barFields[2].getProto().getName()); EXPECT_EQ(0x8456789abcdef123ull, getFieldTypeFileId(barFields[2])); EXPECT_EQ("garply", barFields[3].getProto().getName()); EXPECT_EQ(0x856789abcdef1234ull, getFieldTypeFileId(barFields[3])); auto bazSchema = parser.parseDiskFile( "not/used/because/already/loaded", "src/foo/baz.capnp", importPath); EXPECT_EQ(0x823456789abcdef1ull, bazSchema.getProto().getId()); EXPECT_EQ("foo2/baz.capnp", bazSchema.getProto().getDisplayName()); auto bazStruct = bazSchema.getNested("Baz").asStruct(); EXPECT_EQ(bazStruct, barStruct.getDependency(bazStruct.getProto().getId())); auto corgeSchema = parser.parseDiskFile( "not/used/because/already/loaded", "src/qux/corge.capnp", importPath); EXPECT_EQ(0x83456789abcdef12ull, corgeSchema.getProto().getId()); EXPECT_EQ("qux/corge.capnp", corgeSchema.getProto().getDisplayName()); auto corgeStruct = corgeSchema.getNested("Corge").asStruct(); EXPECT_EQ(corgeStruct, barStruct.getDependency(corgeStruct.getProto().getId())); auto graultSchema = parser.parseDiskFile( "not/used/because/already/loaded", ABS("usr/include/grault.capnp"), importPath); EXPECT_EQ(0x8456789abcdef123ull, graultSchema.getProto().getId()); EXPECT_EQ("grault.capnp", graultSchema.getProto().getDisplayName()); auto graultStruct = graultSchema.getNested("Grault").asStruct(); EXPECT_EQ(graultStruct, barStruct.getDependency(graultStruct.getProto().getId())); // Try importing the other grault.capnp directly. It'll get the display name we specify since // it wasn't imported before. auto wrongGraultSchema = parser.parseDiskFile( "weird/display/name.capnp", ABS("opt/include/grault.capnp"), importPath); EXPECT_EQ(0x8000000000000001ull, wrongGraultSchema.getProto().getId()); EXPECT_EQ("weird/display/name.capnp", wrongGraultSchema.getProto().getDisplayName()); } TEST(SchemaParser, Constants) { // This is actually a test of the full dynamic API stack for constants, because the schemas for // constants are not actually accessible from the generated code API, so the only way to ever // get a ConstSchema is by parsing it. FakeFileReader reader; SchemaParser parser; parser.setDiskFilesystem(reader); reader.add("const.capnp", "@0x8123456789abcdef;\n" "const uint32Const :UInt32 = 1234;\n" "const listConst :List(Float32) = [1.25, 2.5, 3e4];\n" "const structConst :Foo = (bar = 123, baz = \"qux\");\n" "struct Foo {\n" " bar @0 :Int16;\n" " baz @1 :Text;\n" "}\n" "const genericConst :TestGeneric(Text) = (value = \"text\");\n" "struct TestGeneric(T) {\n" " value @0 :T;\n" "}\n"); ParsedSchema fileSchema = parser.parseDiskFile( "const.capnp", "const.capnp", nullptr); EXPECT_EQ(1234, fileSchema.getNested("uint32Const").asConst().as()); auto list = fileSchema.getNested("listConst").asConst().as(); ASSERT_EQ(3u, list.size()); EXPECT_EQ(1.25, list[0].as()); EXPECT_EQ(2.5, list[1].as()); EXPECT_EQ(3e4f, list[2].as()); auto structConst = fileSchema.getNested("structConst").asConst().as(); EXPECT_EQ(123, structConst.get("bar").as()); EXPECT_EQ("qux", structConst.get("baz").as()); auto genericConst = fileSchema.getNested("genericConst").asConst().as(); EXPECT_EQ("text", genericConst.get("value").as()); } void expectSourceInfo(schema::Node::SourceInfo::Reader sourceInfo, uint64_t expectedId, kj::StringPtr expectedComment, std::initializer_list expectedMembers) { KJ_EXPECT(sourceInfo.getId() == expectedId, sourceInfo, expectedId); KJ_EXPECT(sourceInfo.getDocComment() == expectedComment, sourceInfo, expectedComment); auto members = sourceInfo.getMembers(); KJ_ASSERT(members.size() == expectedMembers.size()); for (auto i: kj::indices(expectedMembers)) { KJ_EXPECT(members[i].getDocComment() == expectedMembers.begin()[i], members[i], expectedMembers.begin()[i]); } } TEST(SchemaParser, SourceInfo) { FakeFileReader reader; SchemaParser parser; parser.setDiskFilesystem(reader); reader.add("foo.capnp", "@0x84a2c6051e1061ed;\n" "# file doc comment\n" "\n" "struct Foo @0xc6527d0a670dc4c3 {\n" " # struct doc comment\n" " # second line\n" "\n" " bar @0 :UInt32;\n" " # field doc comment\n" " baz :group {\n" " # group doc comment\n" " qux @1 :Text;\n" " # group field doc comment\n" " }\n" "}\n" "\n" "enum Corge @0xae08878f1a016f14 {\n" " # enum doc comment\n" " grault @0;\n" " # enumerant doc comment\n" " garply @1;\n" "}\n" "\n" "interface Waldo @0xc0f1b0aff62b761e {\n" " # interface doc comment\n" " fred @0 (plugh :Int32) -> (xyzzy :Text);\n" " # method doc comment\n" "}\n" "\n" "struct Thud @0xcca9972702b730b4 {}\n" "# post-comment\n"); ParsedSchema file = parser.parseDiskFile( "foo.capnp", "foo.capnp", nullptr); ParsedSchema foo = file.getNested("Foo"); expectSourceInfo(file.getSourceInfo(), 0x84a2c6051e1061edull, "file doc comment\n", {}); expectSourceInfo(foo.getSourceInfo(), 0xc6527d0a670dc4c3ull, "struct doc comment\nsecond line\n", { "field doc comment\n", "group doc comment\n" }); auto group = foo.asStruct().getFieldByName("baz").getType().asStruct(); expectSourceInfo(KJ_ASSERT_NONNULL(parser.getSourceInfo(group)), group.getProto().getId(), "group doc comment\n", { "group field doc comment\n" }); ParsedSchema corge = file.getNested("Corge"); expectSourceInfo(corge.getSourceInfo(), 0xae08878f1a016f14, "enum doc comment\n", { "enumerant doc comment\n", "" }); ParsedSchema waldo = file.getNested("Waldo"); expectSourceInfo(waldo.getSourceInfo(), 0xc0f1b0aff62b761e, "interface doc comment\n", { "method doc comment\n" }); ParsedSchema thud = file.getNested("Thud"); expectSourceInfo(thud.getSourceInfo(), 0xcca9972702b730b4, "post-comment\n", {}); } } // namespace } // namespace capnp capnproto-c++-0.8.0/src/capnp/dynamic-test.c++0000644000175000017500000004273013340402540021534 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "dynamic.h" #include "message.h" #include #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { template void checkList(T reader, std::initializer_list> expected) { auto list = reader.template as(); ASSERT_EQ(expected.size(), list.size()); for (uint i = 0; i < expected.size(); i++) { EXPECT_EQ(expected.begin()[i], list[i].template as()); } auto typed = reader.template as>(); ASSERT_EQ(expected.size(), typed.size()); for (uint i = 0; i < expected.size(); i++) { EXPECT_EQ(expected.begin()[i], typed[i]); } } TEST(DynamicApi, Build) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); initDynamicTestMessage(root); checkTestMessage(root.asReader().as()); checkDynamicTestMessage(root.asReader()); checkDynamicTestMessage(root); } TEST(DynamicApi, Read) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root); checkDynamicTestMessage(toDynamic(root.asReader())); checkDynamicTestMessage(toDynamic(root).asReader()); checkDynamicTestMessage(toDynamic(root)); } TEST(DynamicApi, Defaults) { AlignedData<1> nullRoot = {{0, 0, 0, 0, 0, 0, 0, 0}}; kj::ArrayPtr segments[1] = {kj::arrayPtr(nullRoot.words, 1)}; SegmentArrayMessageReader reader(kj::arrayPtr(segments, 1)); auto root = reader.getRoot(Schema::from()); checkDynamicTestMessage(root); } TEST(DynamicApi, DefaultsBuilder) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); checkTestMessage(root.asReader().as()); checkDynamicTestMessage(root.asReader()); // This will initialize the whole message, replacing null pointers with copies of defaults. checkDynamicTestMessage(root); // Check again now that the message is initialized. checkTestMessage(root.asReader().as()); checkDynamicTestMessage(root.asReader()); checkDynamicTestMessage(root); } TEST(DynamicApi, Zero) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); checkDynamicTestMessageAllZero(root.asReader()); checkTestMessageAllZero(root.asReader().as()); checkDynamicTestMessageAllZero(root); checkTestMessageAllZero(root.asReader().as()); } TEST(DynamicApi, ListListsBuild) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); initDynamicTestLists(root); checkTestMessage(root.asReader().as()); checkDynamicTestLists(root.asReader()); checkDynamicTestLists(root); } TEST(DynamicApi, ListListsRead) { MallocMessageBuilder builder; auto root = builder.initRoot(); initTestMessage(root); checkDynamicTestLists(toDynamic(root.asReader())); checkDynamicTestLists(toDynamic(root).asReader()); checkDynamicTestLists(toDynamic(root)); } TEST(DynamicApi, AnyPointers) { MallocMessageBuilder builder; auto root = builder.getRoot(); initDynamicTestMessage( root.getAnyPointerField().initAs(Schema::from())); checkTestMessage(root.asReader().getAnyPointerField().getAs()); checkDynamicTestMessage( root.asReader().getAnyPointerField().getAs(Schema::from())); checkDynamicTestMessage( root.getAnyPointerField().getAs(Schema::from())); { { auto list = root.getAnyPointerField().initAs(Schema::from>(), 4); list.set(0, 123); list.set(1, 456); list.set(2, 789); list.set(3, 123456789); } { auto list = root.asReader().getAnyPointerField().getAs>(); ASSERT_EQ(4u, list.size()); EXPECT_EQ(123u, list[0]); EXPECT_EQ(456u, list[1]); EXPECT_EQ(789u, list[2]); EXPECT_EQ(123456789u, list[3]); } checkList(root.asReader().getAnyPointerField().getAs( Schema::from>()), {123u, 456u, 789u, 123456789u}); checkList(root.getAnyPointerField().getAs( Schema::from>()), {123u, 456u, 789u, 123456789u}); } // Setting an AnyPointer to various types should work. toDynamic(root).set("anyPointerField", capnp::Text::Reader("foo")); EXPECT_EQ("foo", root.getAnyPointerField().getAs()); { auto orphan = builder.getOrphanage().newOrphan(); initTestMessage(orphan.get()); toDynamic(root).set("anyPointerField", orphan.getReader()); checkTestMessage(root.getAnyPointerField().getAs()); toDynamic(root).adopt("anyPointerField", kj::mv(orphan)); checkTestMessage(root.getAnyPointerField().getAs()); } { auto lorphan = builder.getOrphanage().newOrphan>(3); lorphan.get().set(0, 12); lorphan.get().set(1, 34); lorphan.get().set(2, 56); toDynamic(root).set("anyPointerField", lorphan.getReader()); auto l = root.getAnyPointerField().getAs>(); ASSERT_EQ(3, l.size()); EXPECT_EQ(12, l[0]); EXPECT_EQ(34, l[1]); EXPECT_EQ(56, l[2]); } // Just compile this one. toDynamic(root).set("anyPointerField", Capability::Client(nullptr)); root.getAnyPointerField().getAs(); } TEST(DynamicApi, DynamicAnyPointers) { MallocMessageBuilder builder; auto root = builder.getRoot(Schema::from()); initDynamicTestMessage( root.get("anyPointerField").as() .initAs(Schema::from())); checkTestMessage( root.asReader().as().getAnyPointerField().getAs()); checkDynamicTestMessage( root.asReader().get("anyPointerField").as() .getAs(Schema::from())); checkDynamicTestMessage( root.asReader().get("anyPointerField").as() .getAs(Schema::from())); checkDynamicTestMessage( root.get("anyPointerField").as().asReader() .getAs(Schema::from())); checkDynamicTestMessage( root.get("anyPointerField").as() .getAs(Schema::from())); { { auto list = root.init("anyPointerField").as() .initAs(Schema::from>(), 4); list.set(0, 123); list.set(1, 456); list.set(2, 789); list.set(3, 123456789); } { auto list = root.asReader().as() .getAnyPointerField().getAs>(); ASSERT_EQ(4u, list.size()); EXPECT_EQ(123u, list[0]); EXPECT_EQ(456u, list[1]); EXPECT_EQ(789u, list[2]); EXPECT_EQ(123456789u, list[3]); } checkList( root.asReader().get("anyPointerField").as() .getAs(Schema::from>()), {123u, 456u, 789u, 123456789u}); checkList( root.asReader().get("anyPointerField").as() .getAs(Schema::from>()), {123u, 456u, 789u, 123456789u}); checkList( root.get("anyPointerField").as().asReader() .getAs(Schema::from>()), {123u, 456u, 789u, 123456789u}); checkList( root.get("anyPointerField").as() .getAs(Schema::from>()), {123u, 456u, 789u, 123456789u}); } } TEST(DynamicApi, DynamicAnyStructs) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); root.as().as().setInt8Field(123); EXPECT_EQ(root.get("int8Field").as(), 123); EXPECT_EQ(root.asReader().as().as().getInt8Field(), 123); } #define EXPECT_MAYBE_EQ(name, exp, expected, actual) \ KJ_IF_MAYBE(name, exp) { \ EXPECT_EQ(expected, actual); \ } else { \ KJ_FAIL_EXPECT("Maybe was empty."); \ } TEST(DynamicApi, UnionsRead) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getUnion0().setU0f1s32(1234567); root.getUnion1().setU1f1sp("foo"); root.getUnion2().setU2f0s1(true); root.getUnion3().setU3f0s64(1234567890123456789ll); { auto dynamic = toDynamic(root.asReader()); { auto u = dynamic.get("union0").as(); EXPECT_MAYBE_EQ(w, u.which(), "u0f1s32", w->getProto().getName()); EXPECT_EQ(1234567, u.get("u0f1s32").as()); } { auto u = dynamic.get("union1").as(); EXPECT_MAYBE_EQ(w, u.which(), "u1f1sp", w->getProto().getName()); EXPECT_EQ("foo", u.get("u1f1sp").as()); } { auto u = dynamic.get("union2").as(); EXPECT_MAYBE_EQ(w, u.which(), "u2f0s1", w->getProto().getName()); EXPECT_TRUE(u.get("u2f0s1").as()); } { auto u = dynamic.get("union3").as(); EXPECT_MAYBE_EQ(w, u.which(), "u3f0s64", w->getProto().getName()); EXPECT_EQ(1234567890123456789ll, u.get("u3f0s64").as()); } } { // Again as a builder. auto dynamic = toDynamic(root); { auto u = dynamic.get("union0").as(); EXPECT_MAYBE_EQ(w, u.which(), "u0f1s32", w->getProto().getName()); EXPECT_EQ(1234567, u.get("u0f1s32").as()); } { auto u = dynamic.get("union1").as(); EXPECT_MAYBE_EQ(w, u.which(), "u1f1sp", w->getProto().getName()); EXPECT_EQ("foo", u.get("u1f1sp").as()); } { auto u = dynamic.get("union2").as(); EXPECT_MAYBE_EQ(w, u.which(), "u2f0s1", w->getProto().getName()); EXPECT_TRUE(u.get("u2f0s1").as()); } { auto u = dynamic.get("union3").as(); EXPECT_MAYBE_EQ(w, u.which(), "u3f0s64", w->getProto().getName()); EXPECT_EQ(1234567890123456789ll, u.get("u3f0s64").as()); } } } TEST(DynamicApi, UnionsWrite) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); root.get("union0").as().set("u0f1s32", 1234567); root.get("union1").as().set("u1f1sp", "foo"); root.get("union2").as().set("u2f0s1", true); root.get("union3").as().set("u3f0s64", 1234567890123456789ll); auto reader = root.asReader().as(); ASSERT_EQ(TestUnion::Union0::U0F1S32, reader.getUnion0().which()); EXPECT_EQ(1234567, reader.getUnion0().getU0f1s32()); ASSERT_EQ(TestUnion::Union1::U1F1SP, reader.getUnion1().which()); EXPECT_EQ("foo", reader.getUnion1().getU1f1sp()); ASSERT_EQ(TestUnion::Union2::U2F0S1, reader.getUnion2().which()); EXPECT_TRUE(reader.getUnion2().getU2f0s1()); ASSERT_EQ(TestUnion::Union3::U3F0S64, reader.getUnion3().which()); EXPECT_EQ(1234567890123456789ll, reader.getUnion3().getU3f0s64()); // Can't access union members by name from the root. EXPECT_ANY_THROW(root.get("u0f1s32")); EXPECT_ANY_THROW(root.set("u0f1s32", 1234567)); } TEST(DynamicApi, UnnamedUnion) { MallocMessageBuilder builder; StructSchema schema = Schema::from(); auto root = builder.initRoot(schema); EXPECT_EQ(schema.getFieldByName("foo"), KJ_ASSERT_NONNULL(root.which())); root.set("bar", 321); EXPECT_EQ(schema.getFieldByName("bar"), KJ_ASSERT_NONNULL(root.which())); EXPECT_EQ(321u, root.get("bar").as()); EXPECT_EQ(321u, root.asReader().get("bar").as()); EXPECT_ANY_THROW(root.get("foo")); EXPECT_ANY_THROW(root.asReader().get("foo")); root.set("foo", 123); EXPECT_EQ(schema.getFieldByName("foo"), KJ_ASSERT_NONNULL(root.which())); EXPECT_EQ(123u, root.get("foo").as()); EXPECT_EQ(123u, root.asReader().get("foo").as()); EXPECT_ANY_THROW(root.get("bar")); EXPECT_ANY_THROW(root.asReader().get("bar")); root.set("bar", 321); EXPECT_EQ(schema.getFieldByName("bar"), KJ_ASSERT_NONNULL(root.which())); EXPECT_EQ(321u, root.get("bar").as()); EXPECT_EQ(321u, root.asReader().get("bar").as()); EXPECT_ANY_THROW(root.get("foo")); EXPECT_ANY_THROW(root.asReader().get("foo")); root.set("foo", 123); EXPECT_EQ(schema.getFieldByName("foo"), KJ_ASSERT_NONNULL(root.which())); EXPECT_EQ(123u, root.get("foo").as()); EXPECT_EQ(123u, root.asReader().get("foo").as()); EXPECT_ANY_THROW(root.get("bar")); EXPECT_ANY_THROW(root.asReader().get("bar")); } TEST(DynamicApi, ConversionFailures) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); root.set("int8Field", 123); EXPECT_NONFATAL_FAILURE(root.set("int8Field", 1234)); root.set("uInt32Field", 1); EXPECT_NONFATAL_FAILURE(root.set("uInt32Field", -1)); root.set("int16Field", 5); EXPECT_NONFATAL_FAILURE(root.set("int16Field", 0.5)); root.set("boolField", true); EXPECT_NONFATAL_FAILURE(root.set("boolField", 1)); } TEST(DynamicApi, LateUnion) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); root.get("theUnion").as().set("qux", "hello"); EXPECT_EQ("hello", root.as().getTheUnion().getQux()); } TEST(DynamicApi, Has) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); // Primitive fields are always present even if set to default. EXPECT_TRUE(root.has("int32Field")); EXPECT_FALSE(root.has("int32Field", HasMode::NON_DEFAULT)); root.set("int32Field", 123); EXPECT_TRUE(root.has("int32Field")); EXPECT_TRUE(root.has("int32Field", HasMode::NON_DEFAULT)); root.set("int32Field", -12345678); EXPECT_TRUE(root.has("int32Field")); EXPECT_FALSE(root.has("int32Field", HasMode::NON_DEFAULT)); // Pointers are absent until initialized. EXPECT_FALSE(root.has("structField")); EXPECT_FALSE(root.has("structField", HasMode::NON_DEFAULT)); root.init("structField"); EXPECT_TRUE(root.has("structField")); EXPECT_TRUE(root.has("structField", HasMode::NON_DEFAULT)); } TEST(DynamicApi, HasWhenEmpty) { AlignedData<1> nullRoot = {{0, 0, 0, 0, 0, 0, 0, 0}}; kj::ArrayPtr segments[1] = {kj::arrayPtr(nullRoot.words, 1)}; SegmentArrayMessageReader reader(kj::arrayPtr(segments, 1)); auto root = reader.getRoot(Schema::from()); EXPECT_TRUE(root.has("voidField")); EXPECT_TRUE(root.has("int32Field")); EXPECT_FALSE(root.has("structField")); EXPECT_FALSE(root.has("int32List")); EXPECT_FALSE(root.has("voidField", HasMode::NON_DEFAULT)); EXPECT_FALSE(root.has("int32Field", HasMode::NON_DEFAULT)); EXPECT_FALSE(root.has("structField", HasMode::NON_DEFAULT)); EXPECT_FALSE(root.has("int32List", HasMode::NON_DEFAULT)); } TEST(DynamicApi, SetEnumFromNative) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); root.set("enumField", TestEnum::BAZ); root.set("enumList", {TestEnum::BAR, TestEnum::FOO}); EXPECT_EQ(TestEnum::BAZ, root.get("enumField").as()); checkList(root.get("enumList"), {TestEnum::BAR, TestEnum::FOO}); } TEST(DynamicApi, SetDataFromText) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); root.set("dataField", "foo"); EXPECT_EQ(data("foo"), root.get("dataField").as()); } TEST(DynamicApi, BuilderAssign) { MallocMessageBuilder builder; auto root = builder.initRoot(Schema::from()); // Declare upfront, assign later. // Note that the Python implementation requires defaulted constructors. Do not delete them! DynamicValue::Builder value; DynamicStruct::Builder structValue; DynamicList::Builder listValue; value = root.get("structField"); structValue = value.as(); structValue.set("int32Field", 123); value = root.init("int32List", 1); listValue = value.as(); listValue.set(0, 123); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/stringify-test.c++0000644000175000017500000005773413340402540022140 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "message.h" #include "dynamic.h" #include "pretty-print.h" #include #include #include "test-util.h" namespace capnp { namespace _ { // private namespace { TEST(Stringify, KjStringification) { MallocMessageBuilder builder; auto root = builder.initRoot(); // This test got ugly after printing was changed to always print primitives even when they have // default values... EXPECT_EQ("(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "enumField = foo, " "interfaceField = void)", kj::str(root)); initTestMessage(root); EXPECT_EQ("(" "voidField = void, " "boolField = true, " "int8Field = -123, " "int16Field = -12345, " "int32Field = -12345678, " "int64Field = -123456789012345, " "uInt8Field = 234, " "uInt16Field = 45678, " "uInt32Field = 3456789012, " "uInt64Field = 12345678901234567890, " "float32Field = 1234.5, " "float64Field = -1.23e47, " "textField = \"foo\", " "dataField = \"bar\", " "structField = (" "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 = (" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"nested\", " "structField = (" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"really nested\", " "enumField = foo, " "interfaceField = void), " "enumField = foo, " "interfaceField = void), " "enumField = baz, " "interfaceField = void, " "voidList = [void, void, void], " "boolList = [false, true, false, true, true], " "int8List = [12, -34, -128, 127], " "int16List = [1234, -5678, -32768, 32767], " "int32List = [12345678, -90123456, -2147483648, 2147483647], " "int64List = [123456789012345, -678901234567890, " "-9223372036854775808, 9223372036854775807], " "uInt8List = [12, 34, 0, 255], " "uInt16List = [1234, 5678, 0, 65535], " "uInt32List = [12345678, 90123456, 0, 4294967295], " "uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615], " "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 = [" "(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"x structlist 1\", " "enumField = foo, " "interfaceField = void), " "(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"x structlist 2\", " "enumField = foo, " "interfaceField = void), " "(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"x structlist 3\", " "enumField = foo, " "interfaceField = void)], " "enumList = [qux, bar, grault]), " "enumField = corge, " "interfaceField = void, " "voidList = [void, void, void, void, void, void], " "boolList = [true, false, false, true], " "int8List = [111, -111], " "int16List = [11111, -11111], " "int32List = [111111111, -111111111], " "int64List = [1111111111111111111, -1111111111111111111], " "uInt8List = [111, 222], " "uInt16List = [33333, 44444], " "uInt32List = [3333333333], " "uInt64List = [11111111111111111111], " "float32List = [5555.5, inf, -inf, nan], " "float64List = [7777.75, inf, -inf, nan], " "textList = [\"plugh\", \"xyzzy\", \"thud\"], " "dataList = [\"oops\", \"exhausted\", \"rfc3092\"], " "structList = [" "(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"structlist 1\", " "enumField = foo, " "interfaceField = void), " "(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"structlist 2\", " "enumField = foo, " "interfaceField = void), " "(" "voidField = void, " "boolField = false, " "int8Field = 0, " "int16Field = 0, " "int32Field = 0, " "int64Field = 0, " "uInt8Field = 0, " "uInt16Field = 0, " "uInt32Field = 0, " "uInt64Field = 0, " "float32Field = 0, " "float64Field = 0, " "textField = \"structlist 3\", " "enumField = foo, " "interfaceField = void)], " "enumList = [foo, garply])", kj::str(root)); } TEST(Stringify, PrettyPrint) { MallocMessageBuilder builder; auto root = builder.initRoot(); EXPECT_EQ( "( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " enumField = foo,\n" " interfaceField = void )", prettyPrint(root).flatten()); initTestMessage(root); EXPECT_EQ( "( voidField = void,\n" " boolField = true,\n" " int8Field = -123,\n" " int16Field = -12345,\n" " int32Field = -12345678,\n" " int64Field = -123456789012345,\n" " uInt8Field = 234,\n" " uInt16Field = 45678,\n" " uInt32Field = 3456789012,\n" " uInt64Field = 12345678901234567890,\n" " float32Field = 1234.5,\n" " float64Field = -1.23e47,\n" " textField = \"foo\",\n" " dataField = \"bar\",\n" " structField = (\n" " voidField = void,\n" " boolField = true,\n" " int8Field = -12,\n" " int16Field = 3456,\n" " int32Field = -78901234,\n" " int64Field = 56789012345678,\n" " uInt8Field = 90,\n" " uInt16Field = 1234,\n" " uInt32Field = 56789012,\n" " uInt64Field = 345678901234567890,\n" " float32Field = -1.25e-10,\n" " float64Field = 345,\n" " textField = \"baz\",\n" " dataField = \"qux\",\n" " structField = (\n" " voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"nested\",\n" " structField = (\n" " voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"really nested\",\n" " enumField = foo,\n" " interfaceField = void ),\n" " enumField = foo,\n" " interfaceField = void ),\n" " enumField = baz,\n" " interfaceField = void,\n" " voidList = [void, void, void],\n" " boolList = [false, true, false, true, true],\n" " int8List = [12, -34, -128, 127],\n" " int16List = [1234, -5678, -32768, 32767],\n" " int32List = [12345678, -90123456, -2147483648, 2147483647],\n" " int64List = [123456789012345, -678901234567890, " "-9223372036854775808, 9223372036854775807],\n" " uInt8List = [12, 34, 0, 255],\n" " uInt16List = [1234, 5678, 0, 65535],\n" " uInt32List = [12345678, 90123456, 0, 4294967295],\n" " uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615],\n" " float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37],\n" " float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306],\n" " textList = [\"quux\", \"corge\", \"grault\"],\n" " dataList = [\"garply\", \"waldo\", \"fred\"],\n" " structList = [\n" " ( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"x structlist 1\",\n" " enumField = foo,\n" " interfaceField = void ),\n" " ( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"x structlist 2\",\n" " enumField = foo,\n" " interfaceField = void ),\n" " ( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"x structlist 3\",\n" " enumField = foo,\n" " interfaceField = void ) ],\n" " enumList = [qux, bar, grault] ),\n" " enumField = corge,\n" " interfaceField = void,\n" " voidList = [void, void, void, void, void, void],\n" " boolList = [true, false, false, true],\n" " int8List = [111, -111],\n" " int16List = [11111, -11111],\n" " int32List = [111111111, -111111111],\n" " int64List = [1111111111111111111, -1111111111111111111],\n" " uInt8List = [111, 222],\n" " uInt16List = [33333, 44444],\n" " uInt32List = [3333333333],\n" " uInt64List = [11111111111111111111],\n" " float32List = [5555.5, inf, -inf, nan],\n" " float64List = [7777.75, inf, -inf, nan],\n" " textList = [\"plugh\", \"xyzzy\", \"thud\"],\n" " dataList = [\"oops\", \"exhausted\", \"rfc3092\"],\n" " structList = [\n" " ( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"structlist 1\",\n" " enumField = foo,\n" " interfaceField = void ),\n" " ( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"structlist 2\",\n" " enumField = foo,\n" " interfaceField = void ),\n" " ( voidField = void,\n" " boolField = false,\n" " int8Field = 0,\n" " int16Field = 0,\n" " int32Field = 0,\n" " int64Field = 0,\n" " uInt8Field = 0,\n" " uInt16Field = 0,\n" " uInt32Field = 0,\n" " uInt64Field = 0,\n" " float32Field = 0,\n" " float64Field = 0,\n" " textField = \"structlist 3\",\n" " enumField = foo,\n" " interfaceField = void ) ],\n" " enumList = [foo, garply] )", prettyPrint(root).flatten()); } TEST(Stringify, PrettyPrintAdvanced) { MallocMessageBuilder builder; { auto root = builder.initRoot(); auto list = root.initStructList(3); list[0].setInt32Field(123); list[0].setTextField("foo"); list[1].setInt32Field(456); list[1].setTextField("bar"); list[2].setInt32Field(789); list[2].setTextField("baz"); EXPECT_EQ( "( structList = [\n" " (int32Field = 123, textField = \"foo\"),\n" " (int32Field = 456, textField = \"bar\"),\n" " (int32Field = 789, textField = \"baz\") ] )", prettyPrint(root).flatten()); root.setSomeText("foo"); EXPECT_EQ( "( someText = \"foo\",\n" " structList = [\n" " (int32Field = 123, textField = \"foo\"),\n" " (int32Field = 456, textField = \"bar\"),\n" " (int32Field = 789, textField = \"baz\") ] )", prettyPrint(root).flatten()); } { auto root = builder.initRoot(); auto ll = root.initInt32ListList(3); ll.set(0, {123, 456, 789, 1234567890}); ll.set(1, {234, 567, 891, 1234567890}); ll.set(2, {345, 678, 912, 1234567890}); EXPECT_EQ( "[ [123, 456, 789, 1234567890],\n" " [234, 567, 891, 1234567890],\n" " [345, 678, 912, 1234567890] ]", prettyPrint(ll).flatten()); EXPECT_EQ( "( int32ListList = [\n" " [123, 456, 789, 1234567890],\n" " [234, 567, 891, 1234567890],\n" " [345, 678, 912, 1234567890] ] )", prettyPrint(root).flatten()); root.initList8(0); EXPECT_EQ( "( list8 = [],\n" " int32ListList = [\n" " [123, 456, 789, 1234567890],\n" " [234, 567, 891, 1234567890],\n" " [345, 678, 912, 1234567890] ] )", prettyPrint(root).flatten()); auto l8 = root.initList8(1); l8[0].setF(12); EXPECT_EQ( "( list8 = [(f = 12)],\n" " int32ListList = [\n" " [123, 456, 789, 1234567890],\n" " [234, 567, 891, 1234567890],\n" " [345, 678, 912, 1234567890] ] )", prettyPrint(root).flatten()); l8 = root.initList8(2); l8[0].setF(12); l8[1].setF(34); EXPECT_EQ( "( list8 = [(f = 12), (f = 34)],\n" " int32ListList = [\n" " [123, 456, 789, 1234567890],\n" " [234, 567, 891, 1234567890],\n" " [345, 678, 912, 1234567890] ] )", prettyPrint(root).flatten()); } { auto root = builder.initRoot(); auto s = root.getUn().initStruct(); EXPECT_EQ( "(un = (struct = ()))", prettyPrint(root).flatten()); s.setSomeText("foo"); EXPECT_EQ( "( un = (\n" " struct = (someText = \"foo\") ) )", prettyPrint(root).flatten()); s.setMoreText("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar"); EXPECT_EQ( "( un = (\n" " struct = (\n" " someText = \"foo\",\n" " moreText = \"baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\" ) ) )", prettyPrint(root).flatten()); } } TEST(Stringify, Unions) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getUnion0().setU0f0s16(123); root.getUnion1().setU1f0sp("foo"); root.getUnion2().setU2f0s1(true); root.getUnion3().setU3f0s64(123456789012345678ll); EXPECT_EQ("(" "union0 = (u0f0s16 = 123), " "union1 = (u1f0sp = \"foo\"), " "union2 = (u2f0s1 = true), " "union3 = (u3f0s64 = 123456789012345678), " "bit0 = false, bit2 = false, bit3 = false, bit4 = false, bit5 = false, " "bit6 = false, bit7 = false, byte0 = 0)", kj::str(root)); EXPECT_EQ("(u0f0s16 = 123)", kj::str(root.getUnion0())); EXPECT_EQ("(u1f0sp = \"foo\")", kj::str(root.getUnion1())); EXPECT_EQ("(u2f0s1 = true)", kj::str(root.getUnion2())); EXPECT_EQ("(u3f0s64 = 123456789012345678)", kj::str(root.getUnion3())); } TEST(Stringify, UnionDefaults) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.getUnion0().setU0f0s16(0); // Non-default field has default value. root.getUnion1().setU1f0sp("foo"); // Non-default field has non-default value. root.getUnion2().setU2f0s1(false); // Default field has default value. root.getUnion3().setU3f0s1(true); // Default field has non-default value. EXPECT_EQ("(" "union0 = (u0f0s16 = 0), " "union1 = (u1f0sp = \"foo\"), " "union2 = (u2f0s1 = false), " "union3 = (u3f0s1 = true), " "bit0 = false, bit2 = false, bit3 = false, bit4 = false, bit5 = false, " "bit6 = false, bit7 = false, byte0 = 0)", kj::str(root)); EXPECT_EQ("(u0f0s16 = 0)", kj::str(root.getUnion0())); EXPECT_EQ("(u1f0sp = \"foo\")", kj::str(root.getUnion1())); EXPECT_EQ("(u2f0s1 = false)", kj::str(root.getUnion2())); EXPECT_EQ("(u3f0s1 = true)", kj::str(root.getUnion3())); } TEST(Stringify, UnnamedUnions) { MallocMessageBuilder builder; auto root = builder.initRoot(); root.setBar(123); EXPECT_EQ("(middle = 0, bar = 123)", kj::str(root)); EXPECT_EQ("(middle = 0, bar = 123)", prettyPrint(root).flatten()); root.setAfter("foooooooooooooooooooooooooooooooo"); EXPECT_EQ("(middle = 0, bar = 123, after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root)); EXPECT_EQ( "( middle = 0,\n" " bar = 123,\n" " after = \"foooooooooooooooooooooooooooooooo\" )", prettyPrint(root).flatten()); root.setBefore("before"); EXPECT_EQ("(before = \"before\", middle = 0, bar = 123, " "after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root)); EXPECT_EQ( "( before = \"before\",\n" " middle = 0,\n" " bar = 123,\n" " after = \"foooooooooooooooooooooooooooooooo\" )", prettyPrint(root).flatten()); root.setFoo(0); EXPECT_EQ( "(before = \"before\", foo = 0, middle = 0, after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root)); EXPECT_EQ( "( before = \"before\",\n" " foo = 0,\n" " middle = 0,\n" " after = \"foooooooooooooooooooooooooooooooo\" )", prettyPrint(root).flatten()); } TEST(Stringify, StructUnions) { MallocMessageBuilder builder; auto root = builder.initRoot(); auto s = root.getUn().initStruct(); s.setSomeText("foo"); s.setMoreText("bar"); EXPECT_EQ("(un = (struct = (someText = \"foo\", moreText = \"bar\")))", kj::str(root)); } TEST(Stringify, MoreValues) { EXPECT_EQ("123", kj::str(DynamicValue::Reader(123))); EXPECT_EQ("1.23e47", kj::str(DynamicValue::Reader(123e45))); EXPECT_EQ("\"foo\"", kj::str(DynamicValue::Reader("foo"))); EXPECT_EQ("\"\\a\\b\\n\\t\\\"\"", kj::str(DynamicValue::Reader("\a\b\n\t\""))); EXPECT_EQ("foo", kj::str(DynamicValue::Reader(TestEnum::FOO))); EXPECT_EQ("(123)", kj::str(DynamicValue::Reader(static_cast(123)))); } TEST(Stringify, Generics) { MallocMessageBuilder builder; auto root = builder.initRoot>::Inner>(); root.setFoo("abcd"); auto l = root.initBar(2); l.set(0, 123); l.set(1, 456); EXPECT_EQ("(foo = \"abcd\", bar = [123, 456])", kj::str(root)); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/serialize-async-test.c++0000644000175000017500000002407013340402540023207 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "serialize-async.h" #include "serialize.h" #include #include #include #include #include "test-util.h" #include #if _WIN32 #define WIN32_LEAN_AND_MEAN #include #include namespace kj { namespace _ { int win32Socketpair(SOCKET socks[2]); } } #else #include #endif namespace capnp { namespace _ { // private namespace { #if _WIN32 inline void delay() { Sleep(5); } #else inline void delay() { usleep(5000); } #endif class FragmentingOutputStream: public kj::OutputStream { public: FragmentingOutputStream(kj::OutputStream& inner): inner(inner) {} void write(const void* buffer, size_t size) override { while (size > 0) { delay(); size_t n = rand() % size + 1; inner.write(buffer, n); buffer = reinterpret_cast(buffer) + n; size -= n; } } private: kj::OutputStream& inner; }; class TestMessageBuilder: public MallocMessageBuilder { // A MessageBuilder that tries to allocate an exact number of total segments, by allocating // minimum-size segments until it reaches the number, then allocating one large segment to // finish. public: explicit TestMessageBuilder(uint desiredSegmentCount) : MallocMessageBuilder(0, AllocationStrategy::FIXED_SIZE), desiredSegmentCount(desiredSegmentCount) {} ~TestMessageBuilder() { EXPECT_EQ(0u, desiredSegmentCount); } kj::ArrayPtr allocateSegment(uint minimumSize) override { if (desiredSegmentCount <= 1) { if (desiredSegmentCount < 1) { ADD_FAILURE() << "Allocated more segments than desired."; } else { --desiredSegmentCount; } return MallocMessageBuilder::allocateSegment(8192); } else { --desiredSegmentCount; return MallocMessageBuilder::allocateSegment(minimumSize); } } private: uint desiredSegmentCount; }; class PipeWithSmallBuffer { public: #ifdef _WIN32 #define KJ_SOCKCALL KJ_WINSOCK #ifndef SHUT_WR #define SHUT_WR SD_SEND #endif #define socketpair(family, type, flags, fds) kj::_::win32Socketpair(fds) #else #define KJ_SOCKCALL KJ_SYSCALL #endif PipeWithSmallBuffer() { // Use a socketpair rather than a pipe so that we can set the buffer size extremely small. KJ_SOCKCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); KJ_SOCKCALL(shutdown(fds[0], SHUT_WR)); // Note: OSX reports ENOTCONN if we also try to shutdown(fds[1], SHUT_RD). // Request that the buffer size be as small as possible, to force the event loop to kick in. // FUN STUFF: // - On Linux, the kernel rounds up to the smallest size it permits, so we can ask for a size of // zero. // - On OSX, the kernel reports EINVAL on zero, but will dutifully use a 1-byte buffer if we // set the size to 1. This tends to cause stack overflows due to ridiculously long promise // chains. // - Cygwin will apparently actually use a buffer size of 0 and therefore block forever waiting // for buffer space. // - GNU HURD throws ENOPROTOOPT for SO_RCVBUF. Apparently, technically, a Unix domain socket // has only one buffer, and it's controlled via SO_SNDBUF on the other end. OK, we'll ignore // errors on SO_RCVBUF, then. // // Anyway, we now use 127 to avoid these issues (but also to screw around with non-word-boundary // writes). uint small = 127; setsockopt(fds[0], SOL_SOCKET, SO_RCVBUF, (const char*)&small, sizeof(small)); KJ_SOCKCALL(setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, (const char*)&small, sizeof(small))); } ~PipeWithSmallBuffer() { #if _WIN32 closesocket(fds[0]); closesocket(fds[1]); #else close(fds[0]); close(fds[1]); #endif } inline int operator[](uint index) { return fds[index]; } private: #ifdef _WIN32 SOCKET fds[2]; #else int fds[2]; #endif }; #if _WIN32 // Sockets on win32 are not file descriptors. Ugh. // // TODO(cleanup): Maybe put these somewhere reusable? kj/io.h is inappropriate since we don't // really want to link against winsock. class SocketOutputStream: public kj::OutputStream { public: explicit SocketOutputStream(SOCKET fd): fd(fd) {} void write(const void* buffer, size_t size) override { const char* ptr = reinterpret_cast(buffer); while (size > 0) { kj::miniposix::ssize_t n; KJ_SOCKCALL(n = send(fd, ptr, size, 0)); size -= n; ptr += n; } } private: SOCKET fd; }; class SocketInputStream: public kj::InputStream { public: explicit SocketInputStream(SOCKET fd): fd(fd) {} size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { char* ptr = reinterpret_cast(buffer); size_t total = 0; while (total < minBytes) { kj::miniposix::ssize_t n; KJ_SOCKCALL(n = recv(fd, ptr, maxBytes, 0)); total += n; maxBytes -= n; ptr += n; } return total; } private: SOCKET fd; }; #else // _WIN32 typedef kj::FdOutputStream SocketOutputStream; typedef kj::FdInputStream SocketInputStream; #endif // _WIN32, else TEST(SerializeAsyncTest, ParseAsync) { PipeWithSmallBuffer fds; auto ioContext = kj::setupAsyncIo(); auto input = ioContext.lowLevelProvider->wrapInputFd(fds[0]); SocketOutputStream rawOutput(fds[1]); FragmentingOutputStream output(rawOutput); TestMessageBuilder message(1); initTestMessage(message.getRoot()); kj::Thread thread([&]() { writeMessage(output, message); }); auto received = readMessage(*input).wait(ioContext.waitScope); checkTestMessage(received->getRoot()); } TEST(SerializeAsyncTest, ParseAsyncOddSegmentCount) { PipeWithSmallBuffer fds; auto ioContext = kj::setupAsyncIo(); auto input = ioContext.lowLevelProvider->wrapInputFd(fds[0]); SocketOutputStream rawOutput(fds[1]); FragmentingOutputStream output(rawOutput); TestMessageBuilder message(7); initTestMessage(message.getRoot()); kj::Thread thread([&]() { writeMessage(output, message); }); auto received = readMessage(*input).wait(ioContext.waitScope); checkTestMessage(received->getRoot()); } TEST(SerializeAsyncTest, ParseAsyncEvenSegmentCount) { PipeWithSmallBuffer fds; auto ioContext = kj::setupAsyncIo(); auto input = ioContext.lowLevelProvider->wrapInputFd(fds[0]); SocketOutputStream rawOutput(fds[1]); FragmentingOutputStream output(rawOutput); TestMessageBuilder message(10); initTestMessage(message.getRoot()); kj::Thread thread([&]() { writeMessage(output, message); }); auto received = readMessage(*input).wait(ioContext.waitScope); checkTestMessage(received->getRoot()); } TEST(SerializeAsyncTest, WriteAsync) { PipeWithSmallBuffer fds; auto ioContext = kj::setupAsyncIo(); auto output = ioContext.lowLevelProvider->wrapOutputFd(fds[1]); TestMessageBuilder message(1); auto root = message.getRoot(); auto list = root.initStructList(16); for (auto element: list) { initTestMessage(element); } kj::Thread thread([&]() { SocketInputStream input(fds[0]); InputStreamMessageReader reader(input); auto listReader = reader.getRoot().getStructList(); EXPECT_EQ(list.size(), listReader.size()); for (auto element: listReader) { checkTestMessage(element); } }); writeMessage(*output, message).wait(ioContext.waitScope); } TEST(SerializeAsyncTest, WriteAsyncOddSegmentCount) { PipeWithSmallBuffer fds; auto ioContext = kj::setupAsyncIo(); auto output = ioContext.lowLevelProvider->wrapOutputFd(fds[1]); TestMessageBuilder message(7); auto root = message.getRoot(); auto list = root.initStructList(16); for (auto element: list) { initTestMessage(element); } kj::Thread thread([&]() { SocketInputStream input(fds[0]); InputStreamMessageReader reader(input); auto listReader = reader.getRoot().getStructList(); EXPECT_EQ(list.size(), listReader.size()); for (auto element: listReader) { checkTestMessage(element); } }); writeMessage(*output, message).wait(ioContext.waitScope); } TEST(SerializeAsyncTest, WriteAsyncEvenSegmentCount) { PipeWithSmallBuffer fds; auto ioContext = kj::setupAsyncIo(); auto output = ioContext.lowLevelProvider->wrapOutputFd(fds[1]); TestMessageBuilder message(10); auto root = message.getRoot(); auto list = root.initStructList(16); for (auto element: list) { initTestMessage(element); } kj::Thread thread([&]() { SocketInputStream input(fds[0]); InputStreamMessageReader reader(input); auto listReader = reader.getRoot().getStructList(); EXPECT_EQ(list.size(), listReader.size()); for (auto element: listReader) { checkTestMessage(element); } }); writeMessage(*output, message).wait(ioContext.waitScope); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/serialize-text-test.c++0000644000175000017500000001027413340402540023057 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Philip Quinn. // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "serialize-text.h" #include #include #include #include #include "test-util.h" #include namespace capnp { namespace _ { // private namespace { KJ_TEST("TestAllTypes") { MallocMessageBuilder builder; initTestMessage(builder.initRoot()); { // Plain output TextCodec codec; codec.setPrettyPrint(false); auto text = codec.encode(builder.getRoot()); auto stringify = kj::str(builder.getRoot()); KJ_EXPECT(text == stringify); MallocMessageBuilder reader; auto orphan = codec.decode(text, reader.getOrphanage()); auto structReader = orphan.getReader(); checkTestMessage(structReader); } { // Pretty output TextCodec codec; codec.setPrettyPrint(true); auto text = codec.encode(builder.getRoot()); auto stringify = prettyPrint(builder.getRoot()).flatten(); KJ_EXPECT(text == stringify); MallocMessageBuilder reader; auto orphan = codec.decode(text, reader.getOrphanage()); auto structReader = orphan.getReader(); checkTestMessage(structReader); } } KJ_TEST("TestDefaults") { MallocMessageBuilder builder; initTestMessage(builder.initRoot()); TextCodec codec; auto text = codec.encode(builder.getRoot()); MallocMessageBuilder reader; auto orphan = codec.decode(text, reader.getOrphanage()); auto structReader = orphan.getReader(); checkTestMessage(structReader); } KJ_TEST("TestListDefaults") { MallocMessageBuilder builder; initTestMessage(builder.initRoot()); TextCodec codec; auto text = codec.encode(builder.getRoot()); MallocMessageBuilder reader; auto orphan = codec.decode(text, reader.getOrphanage()); auto structReader = orphan.getReader(); checkTestMessage(structReader); } KJ_TEST("raw text") { using TestType = capnproto_test::capnp::test::TestLateUnion; kj::String message = kj::str(R"(( foo = -123, bar = "bar", baz = 456, # Test Comment theUnion = ( qux = "qux" ), anotherUnion = ( corge = [ 7, 8, 9 ] ), ))"); MallocMessageBuilder builder; auto testType = builder.initRoot(); TextCodec codec; codec.decode(message, testType); auto reader = testType.asReader(); KJ_EXPECT(reader.getFoo() == -123); KJ_EXPECT(reader.getBar() == "bar"); KJ_EXPECT(reader.getBaz() == 456); KJ_EXPECT(reader.getTheUnion().isQux()); KJ_EXPECT(reader.getTheUnion().hasQux()); KJ_EXPECT(reader.getTheUnion().getQux() == "qux"); KJ_EXPECT(reader.getAnotherUnion().isCorge()); KJ_EXPECT(reader.getAnotherUnion().hasCorge()); KJ_EXPECT(reader.getAnotherUnion().getCorge().size() == 3); KJ_EXPECT(reader.getAnotherUnion().getCorge()[0] == 7); KJ_EXPECT(reader.getAnotherUnion().getCorge()[1] == 8); KJ_EXPECT(reader.getAnotherUnion().getCorge()[2] == 9); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/rpc-test.c++0000644000175000017500000013307713650101756020712 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_TESTING_CAPNP 1 #include "rpc.h" #include "test-util.h" #include "schema.h" #include "serialize.h" #include #include #include #include #include #include // TODO(cleanup): Auto-generate stringification functions for union discriminants. namespace capnp { namespace rpc { inline kj::String KJ_STRINGIFY(Message::Which which) { return kj::str(static_cast(which)); } } // namespace rpc } // namespace capnp namespace capnp { namespace _ { // private namespace { class RpcDumper { // Class which stringifies RPC messages for debugging purposes, including decoding params and // results based on the call's interface and method IDs and extracting cap descriptors. // // TODO(cleanup): Clean this code up and move it to someplace reusable, so it can be used as // a packet inspector / debugging tool for Cap'n Proto network traffic. public: void addSchema(InterfaceSchema schema) { schemas[schema.getProto().getId()] = schema; } enum Sender { CLIENT, SERVER }; kj::String dump(rpc::Message::Reader message, Sender sender) { const char* senderName = sender == CLIENT ? "client" : "server"; switch (message.which()) { case rpc::Message::CALL: { auto call = message.getCall(); auto iter = schemas.find(call.getInterfaceId()); if (iter == schemas.end()) { break; } InterfaceSchema schema = iter->second; auto methods = schema.getMethods(); if (call.getMethodId() >= methods.size()) { break; } InterfaceSchema::Method method = methods[call.getMethodId()]; auto schemaProto = schema.getProto(); auto interfaceName = schemaProto.getDisplayName().slice(schemaProto.getDisplayNamePrefixLength()); auto methodProto = method.getProto(); auto paramType = method.getParamType(); auto resultType = method.getResultType(); if (call.getSendResultsTo().isCaller()) { returnTypes[std::make_pair(sender, call.getQuestionId())] = resultType; } auto payload = call.getParams(); auto params = kj::str(payload.getContent().getAs(paramType)); auto sendResultsTo = call.getSendResultsTo(); return kj::str(senderName, "(", call.getQuestionId(), "): call ", call.getTarget(), " <- ", interfaceName, ".", methodProto.getName(), params, " caps:[", kj::strArray(payload.getCapTable(), ", "), "]", sendResultsTo.isCaller() ? kj::str() : kj::str(" sendResultsTo:", sendResultsTo)); } case rpc::Message::RETURN: { auto ret = message.getReturn(); auto iter = returnTypes.find( std::make_pair(sender == CLIENT ? SERVER : CLIENT, ret.getAnswerId())); if (iter == returnTypes.end()) { break; } auto schema = iter->second; returnTypes.erase(iter); if (ret.which() != rpc::Return::RESULTS) { // Oops, no results returned. We don't check this earlier because we want to make sure // returnTypes.erase() gets a chance to happen. break; } auto payload = ret.getResults(); if (schema.getProto().isStruct()) { auto results = kj::str(payload.getContent().getAs(schema.asStruct())); return kj::str(senderName, "(", ret.getAnswerId(), "): return ", results, " caps:[", kj::strArray(payload.getCapTable(), ", "), "]"); } else if (schema.getProto().isInterface()) { payload.getContent().getAs(schema.asInterface()); return kj::str(senderName, "(", ret.getAnswerId(), "): return cap ", kj::strArray(payload.getCapTable(), ", ")); } else { break; } } case rpc::Message::BOOTSTRAP: { auto restore = message.getBootstrap(); returnTypes[std::make_pair(sender, restore.getQuestionId())] = InterfaceSchema(); return kj::str(senderName, "(", restore.getQuestionId(), "): bootstrap ", restore.getDeprecatedObjectId().getAs()); } default: break; } return kj::str(senderName, ": ", message); } private: std::map schemas; std::map, Schema> returnTypes; }; // ======================================================================================= class TestNetworkAdapter; class TestNetwork { public: TestNetwork() { dumper.addSchema(Schema::from()); dumper.addSchema(Schema::from()); dumper.addSchema(Schema::from()); dumper.addSchema(Schema::from()); dumper.addSchema(Schema::from()); dumper.addSchema(Schema::from()); dumper.addSchema(Schema::from()); } ~TestNetwork() noexcept(false); TestNetworkAdapter& add(kj::StringPtr name); kj::Maybe find(kj::StringPtr name) { auto iter = map.find(name); if (iter == map.end()) { return nullptr; } else { return *iter->second; } } RpcDumper dumper; private: std::map> map; }; typedef VatNetwork< test::TestSturdyRefHostId, test::TestProvisionId, test::TestRecipientId, test::TestThirdPartyCapId, test::TestJoinResult> TestNetworkAdapterBase; class TestNetworkAdapter final: public TestNetworkAdapterBase { public: TestNetworkAdapter(TestNetwork& network, kj::StringPtr self): network(network), self(self) {} ~TestNetworkAdapter() { kj::Exception exception = KJ_EXCEPTION(FAILED, "Network was destroyed."); for (auto& entry: connections) { entry.second->disconnect(kj::cp(exception)); } } uint getSentCount() { return sent; } uint getReceivedCount() { return received; } typedef TestNetworkAdapterBase::Connection Connection; class ConnectionImpl final : public Connection, public kj::Refcounted, public kj::TaskSet::ErrorHandler { public: ConnectionImpl(TestNetworkAdapter& network, RpcDumper::Sender sender) : network(network), sender(sender), tasks(kj::heap(*this)) {} void attach(ConnectionImpl& other) { KJ_REQUIRE(partner == nullptr); KJ_REQUIRE(other.partner == nullptr); partner = other; other.partner = *this; } void disconnect(kj::Exception&& exception) { while (!fulfillers.empty()) { fulfillers.front()->reject(kj::cp(exception)); fulfillers.pop(); } networkException = kj::mv(exception); tasks = nullptr; } class IncomingRpcMessageImpl final: public IncomingRpcMessage, public kj::Refcounted { public: IncomingRpcMessageImpl(kj::Array data) : data(kj::mv(data)), message(this->data) {} AnyPointer::Reader getBody() override { return message.getRoot(); } size_t sizeInWords() override { return data.size(); } kj::Array data; FlatArrayMessageReader message; }; class OutgoingRpcMessageImpl final: public OutgoingRpcMessage { public: OutgoingRpcMessageImpl(ConnectionImpl& connection, uint firstSegmentWordSize) : connection(connection), message(firstSegmentWordSize == 0 ? SUGGESTED_FIRST_SEGMENT_WORDS : firstSegmentWordSize) {} AnyPointer::Builder getBody() override { return message.getRoot(); } void send() override { if (connection.networkException != nullptr) { return; } ++connection.network.sent; // Uncomment to get a debug dump. // kj::String msg = connection.network.network.dumper.dump( // message.getRoot(), connection.sender); // KJ_ DBG(msg); auto incomingMessage = kj::heap(messageToFlatArray(message)); auto connectionPtr = &connection; connection.tasks->add(kj::evalLater(kj::mvCapture(incomingMessage, [connectionPtr](kj::Own&& message) { KJ_IF_MAYBE(p, connectionPtr->partner) { if (p->fulfillers.empty()) { p->messages.push(kj::mv(message)); } else { ++p->network.received; p->fulfillers.front()->fulfill( kj::Own(kj::mv(message))); p->fulfillers.pop(); } } }))); } size_t sizeInWords() override { return message.sizeInWords(); } private: ConnectionImpl& connection; MallocMessageBuilder message; }; test::TestSturdyRefHostId::Reader getPeerVatId() override { // Not actually implemented for the purpose of this test. return test::TestSturdyRefHostId::Reader(); } kj::Own newOutgoingMessage(uint firstSegmentWordSize) override { return kj::heap(*this, firstSegmentWordSize); } kj::Promise>> receiveIncomingMessage() override { KJ_IF_MAYBE(e, networkException) { return kj::cp(*e); } if (messages.empty()) { KJ_IF_MAYBE(f, fulfillOnEnd) { f->get()->fulfill(); return kj::Maybe>(nullptr); } else { auto paf = kj::newPromiseAndFulfiller>>(); fulfillers.push(kj::mv(paf.fulfiller)); return kj::mv(paf.promise); } } else { ++network.received; auto result = kj::mv(messages.front()); messages.pop(); return kj::Maybe>(kj::mv(result)); } } kj::Promise shutdown() override { KJ_IF_MAYBE(p, partner) { auto paf = kj::newPromiseAndFulfiller(); p->fulfillOnEnd = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } else { return kj::READY_NOW; } } void taskFailed(kj::Exception&& exception) override { ADD_FAILURE() << kj::str(exception).cStr(); } private: TestNetworkAdapter& network; RpcDumper::Sender sender KJ_UNUSED_MEMBER; kj::Maybe partner; kj::Maybe networkException; std::queue>>>> fulfillers; std::queue> messages; kj::Maybe>> fulfillOnEnd; kj::Own tasks; }; kj::Maybe> connect(test::TestSturdyRefHostId::Reader hostId) override { if (hostId.getHost() == self) { return nullptr; } TestNetworkAdapter& dst = KJ_REQUIRE_NONNULL(network.find(hostId.getHost())); auto iter = connections.find(&dst); if (iter == connections.end()) { auto local = kj::refcounted(*this, RpcDumper::CLIENT); auto remote = kj::refcounted(dst, RpcDumper::SERVER); local->attach(*remote); connections[&dst] = kj::addRef(*local); dst.connections[this] = kj::addRef(*remote); if (dst.fulfillerQueue.empty()) { dst.connectionQueue.push(kj::mv(remote)); } else { dst.fulfillerQueue.front()->fulfill(kj::mv(remote)); dst.fulfillerQueue.pop(); } return kj::Own(kj::mv(local)); } else { return kj::Own(kj::addRef(*iter->second)); } } kj::Promise> accept() override { if (connectionQueue.empty()) { auto paf = kj::newPromiseAndFulfiller>(); fulfillerQueue.push(kj::mv(paf.fulfiller)); return kj::mv(paf.promise); } else { auto result = kj::mv(connectionQueue.front()); connectionQueue.pop(); return kj::mv(result); } } private: TestNetwork& network; kj::StringPtr self; uint sent = 0; uint received = 0; std::map> connections; std::queue>>> fulfillerQueue; std::queue> connectionQueue; }; TestNetwork::~TestNetwork() noexcept(false) {} TestNetworkAdapter& TestNetwork::add(kj::StringPtr name) { return *(map[name] = kj::heap(*this, name)); } // ======================================================================================= class TestRestorer final: public SturdyRefRestorer { public: int callCount = 0; int handleCount = 0; Capability::Client restore(test::TestSturdyRefObjectId::Reader objectId) override { switch (objectId.getTag()) { case test::TestSturdyRefObjectId::Tag::TEST_INTERFACE: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_EXTENDS: return Capability::Client(newBrokenCap("No TestExtends implemented.")); case test::TestSturdyRefObjectId::Tag::TEST_PIPELINE: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLEE: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLER: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF: return kj::heap(callCount, handleCount); } KJ_UNREACHABLE; } }; struct TestContext { kj::EventLoop loop; kj::WaitScope waitScope; TestNetwork network; TestRestorer restorer; TestNetworkAdapter& clientNetwork; TestNetworkAdapter& serverNetwork; RpcSystem rpcClient; RpcSystem rpcServer; TestContext() : waitScope(loop), clientNetwork(network.add("client")), serverNetwork(network.add("server")), rpcClient(makeRpcClient(clientNetwork)), rpcServer(makeRpcServer(serverNetwork, restorer)) {} TestContext(Capability::Client bootstrap) : waitScope(loop), clientNetwork(network.add("client")), serverNetwork(network.add("server")), rpcClient(makeRpcClient(clientNetwork)), rpcServer(makeRpcServer(serverNetwork, bootstrap)) {} TestContext(Capability::Client bootstrap, RealmGateway::Client gateway) : waitScope(loop), clientNetwork(network.add("client")), serverNetwork(network.add("server")), rpcClient(makeRpcClient(clientNetwork, gateway)), rpcServer(makeRpcServer(serverNetwork, bootstrap)) {} TestContext(Capability::Client bootstrap, RealmGateway::Client gateway, bool) : waitScope(loop), clientNetwork(network.add("client")), serverNetwork(network.add("server")), rpcClient(makeRpcClient(clientNetwork)), rpcServer(makeRpcServer(serverNetwork, bootstrap, gateway)) {} Capability::Client connect(test::TestSturdyRefObjectId::Tag tag) { MallocMessageBuilder refMessage(128); auto ref = refMessage.initRoot(); auto hostId = ref.initHostId(); hostId.setHost("server"); ref.getObjectId().initAs().setTag(tag); return rpcClient.restore(hostId, ref.getObjectId()); } }; TEST(Rpc, Basic) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_INTERFACE) .castAs(); auto request1 = client.fooRequest(); request1.setI(123); request1.setJ(true); auto promise1 = request1.send(); // We used to call bar() after baz(), hence the numbering, but this masked the case where the // RPC system actually disconnected on bar() (thus returning an exception, which we decided // was expected). bool barFailed = false; auto request3 = client.barRequest(); auto promise3 = request3.send().then( [](Response&& response) { ADD_FAILURE() << "Expected bar() call to fail."; }, [&](kj::Exception&& e) { barFailed = true; }); auto request2 = client.bazRequest(); initTestMessage(request2.initS()); auto promise2 = request2.send(); EXPECT_EQ(0, context.restorer.callCount); auto response1 = promise1.wait(context.waitScope); EXPECT_EQ("foo", response1.getX()); auto response2 = promise2.wait(context.waitScope); promise3.wait(context.waitScope); EXPECT_EQ(2, context.restorer.callCount); EXPECT_TRUE(barFailed); } TEST(Rpc, Pipelining) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_PIPELINE) .castAs(); int chainedCallCount = 0; auto request = client.getCapRequest(); request.setN(234); request.setInCap(kj::heap(chainedCallCount)); auto promise = request.send(); auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); pipelineRequest.setI(321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = promise.getOutBox().getCap().castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); promise = nullptr; // Just to be annoying, drop the original promise. EXPECT_EQ(0, context.restorer.callCount); EXPECT_EQ(0, chainedCallCount); auto response = pipelinePromise.wait(context.waitScope); EXPECT_EQ("bar", response.getX()); auto response2 = pipelinePromise2.wait(context.waitScope); checkTestMessage(response2); EXPECT_EQ(3, context.restorer.callCount); EXPECT_EQ(1, chainedCallCount); } TEST(Rpc, Release) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto handle1 = client.getHandleRequest().send().wait(context.waitScope).getHandle(); auto promise = client.getHandleRequest().send(); auto handle2 = promise.wait(context.waitScope).getHandle(); EXPECT_EQ(2, context.restorer.handleCount); handle1 = nullptr; for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); EXPECT_EQ(1, context.restorer.handleCount); handle2 = nullptr; for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); EXPECT_EQ(1, context.restorer.handleCount); promise = nullptr; for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); EXPECT_EQ(0, context.restorer.handleCount); } TEST(Rpc, ReleaseOnCancel) { // At one time, there was a bug where if a Return contained capabilities, but the client had // canceled the request and already send a Finish (which presumably didn't reach the server before // the Return), then we'd leak those caps. Test for that. TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); client.whenResolved().wait(context.waitScope); { auto promise = client.getHandleRequest().send(); // If the server receives cancellation too early, it won't even return a capability in the // results, it will just return "canceled". We want to emulate the case where the return message // and the cancel (finish) message cross paths. It turns out that exactly two evalLater()s get // us there. // // TODO(cleanup): This is fragile, but I'm not sure how else to write it without a ton // of scaffolding. kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); } for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); EXPECT_EQ(0, context.restorer.handleCount); } TEST(Rpc, TailCall) { TestContext context; auto caller = context.connect(test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLER) .castAs(); int calleeCallCount = 0; test::TestTailCallee::Client callee(kj::heap(calleeCallCount)); auto request = caller.fooRequest(); request.setI(456); request.setCallee(callee); auto promise = request.send(); auto dependentCall0 = promise.getC().getCallSequenceRequest().send(); auto response = promise.wait(context.waitScope); EXPECT_EQ(456, response.getI()); EXPECT_EQ("from TestTailCaller", response.getT()); auto dependentCall1 = promise.getC().getCallSequenceRequest().send(); EXPECT_EQ(0, dependentCall0.wait(context.waitScope).getN()); EXPECT_EQ(1, dependentCall1.wait(context.waitScope).getN()); // TODO(someday): We used to initiate dependentCall2 here before waiting on the first two calls, // and the ordering was still "correct". But this was apparently by accident. Calling getC() on // the final response returns a different capability from calling getC() on the promise. There // are no guarantees on the ordering of calls on the response capability vs. the earlier // promise. When ordering matters, applications should take the original promise capability and // keep using that. In theory the RPC system could create continuity here, but it would be // annoying: for each capability that had been fetched on the promise, it would need to // traverse to the same capability in the final response and swap it out in-place for the // pipelined cap returned earlier. Maybe we'll determine later that that's really needed but // for now I'm not gonna do it. auto dependentCall2 = response.getC().getCallSequenceRequest().send(); EXPECT_EQ(2, dependentCall2.wait(context.waitScope).getN()); EXPECT_EQ(1, calleeCallCount); EXPECT_EQ(1, context.restorer.callCount); } TEST(Rpc, Cancelation) { // Tests allowCancellation(). TestContext context; auto paf = kj::newPromiseAndFulfiller(); bool destroyed = false; auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); kj::Promise promise = nullptr; bool returned = false; { auto request = client.expectCancelRequest(); request.setCap(kj::heap(kj::mv(paf.fulfiller))); promise = request.send().then( [&](Response&& response) { returned = true; }).eagerlyEvaluate(nullptr); } kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); // We can detect that the method was canceled because it will drop the cap. EXPECT_FALSE(destroyed); EXPECT_FALSE(returned); promise = nullptr; // request cancellation destructionPromise.wait(context.waitScope); EXPECT_TRUE(destroyed); EXPECT_FALSE(returned); } TEST(Rpc, PromiseResolve) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); int chainedCallCount = 0; auto request = client.callFooRequest(); auto request2 = client.callFooWhenResolvedRequest(); auto paf = kj::newPromiseAndFulfiller(); { auto fork = paf.promise.fork(); request.setCap(fork.addBranch()); request2.setCap(fork.addBranch()); } auto promise = request.send(); auto promise2 = request2.send(); // Make sure getCap() has been called on the server side by sending another call and waiting // for it. EXPECT_EQ(2, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); EXPECT_EQ(3, context.restorer.callCount); // OK, now fulfill the local promise. paf.fulfiller->fulfill(kj::heap(chainedCallCount)); // We should now be able to wait for getCap() to finish. EXPECT_EQ("bar", promise.wait(context.waitScope).getS()); EXPECT_EQ("bar", promise2.wait(context.waitScope).getS()); EXPECT_EQ(3, context.restorer.callCount); EXPECT_EQ(2, chainedCallCount); } TEST(Rpc, RetainAndRelease) { TestContext context; auto paf = kj::newPromiseAndFulfiller(); bool destroyed = false; auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); { auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); { auto request = client.holdRequest(); request.setCap(kj::heap(kj::mv(paf.fulfiller))); request.send().wait(context.waitScope); } // Do some other call to add a round trip. EXPECT_EQ(1, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); // Shouldn't be destroyed because it's being held by the server. EXPECT_FALSE(destroyed); // We can ask it to call the held capability. EXPECT_EQ("bar", client.callHeldRequest().send().wait(context.waitScope).getS()); { // We can get the cap back from it. auto capCopy = client.getHeldRequest().send().wait(context.waitScope).getCap(); { // And call it, without any network communications. uint oldSentCount = context.clientNetwork.getSentCount(); auto request = capCopy.fooRequest(); request.setI(123); request.setJ(true); EXPECT_EQ("foo", request.send().wait(context.waitScope).getX()); EXPECT_EQ(oldSentCount, context.clientNetwork.getSentCount()); } { // We can send another copy of the same cap to another method, and it works. auto request = client.callFooRequest(); request.setCap(capCopy); EXPECT_EQ("bar", request.send().wait(context.waitScope).getS()); } } // Give some time to settle. EXPECT_EQ(5, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); EXPECT_EQ(6, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); EXPECT_EQ(7, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); // Can't be destroyed, we haven't released it. EXPECT_FALSE(destroyed); } // We released our client, which should cause the server to be released, which in turn will // release the cap pointing back to us. destructionPromise.wait(context.waitScope); EXPECT_TRUE(destroyed); } TEST(Rpc, Cancel) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto paf = kj::newPromiseAndFulfiller(); bool destroyed = false; auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); { auto request = client.neverReturnRequest(); request.setCap(kj::heap(kj::mv(paf.fulfiller))); { auto responsePromise = request.send(); // Allow some time to settle. EXPECT_EQ(1, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); EXPECT_EQ(2, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); // The cap shouldn't have been destroyed yet because the call never returned. EXPECT_FALSE(destroyed); } } // Now the cap should be released. destructionPromise.wait(context.waitScope); EXPECT_TRUE(destroyed); } TEST(Rpc, SendTwice) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto paf = kj::newPromiseAndFulfiller(); bool destroyed = false; auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); auto cap = test::TestInterface::Client(kj::heap(kj::mv(paf.fulfiller))); { auto request = client.callFooRequest(); request.setCap(cap); EXPECT_EQ("bar", request.send().wait(context.waitScope).getS()); } // Allow some time for the server to release `cap`. EXPECT_EQ(1, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); { // More requests with the same cap. auto request = client.callFooRequest(); auto request2 = client.callFooRequest(); request.setCap(cap); request2.setCap(kj::mv(cap)); auto promise = request.send(); auto promise2 = request2.send(); EXPECT_EQ("bar", promise.wait(context.waitScope).getS()); EXPECT_EQ("bar", promise2.wait(context.waitScope).getS()); } // Now the cap should be released. destructionPromise.wait(context.waitScope); EXPECT_TRUE(destroyed); } RemotePromise getCallSequence( test::TestCallOrder::Client& client, uint expected) { auto req = client.getCallSequenceRequest(); req.setExpected(expected); return req.send(); } TEST(Rpc, Embargo) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto cap = test::TestCallOrder::Client(kj::heap()); auto earlyCall = client.getCallSequenceRequest().send(); auto echoRequest = client.echoRequest(); echoRequest.setCap(cap); auto echo = echoRequest.send(); auto pipeline = echo.getCap(); auto call0 = getCallSequence(pipeline, 0); auto call1 = getCallSequence(pipeline, 1); earlyCall.wait(context.waitScope); auto call2 = getCallSequence(pipeline, 2); auto resolved = echo.wait(context.waitScope).getCap(); auto call3 = getCallSequence(pipeline, 3); auto call4 = getCallSequence(pipeline, 4); auto call5 = getCallSequence(pipeline, 5); EXPECT_EQ(0, call0.wait(context.waitScope).getN()); EXPECT_EQ(1, call1.wait(context.waitScope).getN()); EXPECT_EQ(2, call2.wait(context.waitScope).getN()); EXPECT_EQ(3, call3.wait(context.waitScope).getN()); EXPECT_EQ(4, call4.wait(context.waitScope).getN()); EXPECT_EQ(5, call5.wait(context.waitScope).getN()); } TEST(Rpc, EmbargoUnwrap) { // Test that embargos properly block unwraping a capability using CapabilityServerSet. TestContext context; capnp::CapabilityServerSet capSet; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto cap = capSet.add(kj::heap()); auto earlyCall = client.getCallSequenceRequest().send(); auto echoRequest = client.echoRequest(); echoRequest.setCap(cap); auto echo = echoRequest.send(); auto pipeline = echo.getCap(); auto unwrap = capSet.getLocalServer(pipeline) .then([](kj::Maybe unwrapped) { return kj::downcast(KJ_ASSERT_NONNULL(unwrapped)).getCount(); }).eagerlyEvaluate(nullptr); auto call0 = getCallSequence(pipeline, 0); auto call1 = getCallSequence(pipeline, 1); earlyCall.wait(context.waitScope); auto call2 = getCallSequence(pipeline, 2); auto resolved = echo.wait(context.waitScope).getCap(); auto call3 = getCallSequence(pipeline, 4); auto call4 = getCallSequence(pipeline, 4); auto call5 = getCallSequence(pipeline, 5); EXPECT_EQ(0, call0.wait(context.waitScope).getN()); EXPECT_EQ(1, call1.wait(context.waitScope).getN()); EXPECT_EQ(2, call2.wait(context.waitScope).getN()); EXPECT_EQ(3, call3.wait(context.waitScope).getN()); EXPECT_EQ(4, call4.wait(context.waitScope).getN()); EXPECT_EQ(5, call5.wait(context.waitScope).getN()); uint unwrappedAt = unwrap.wait(context.waitScope); KJ_EXPECT(unwrappedAt >= 3, unwrappedAt); } template void expectPromiseThrows(kj::Promise&& promise, kj::WaitScope& waitScope) { EXPECT_TRUE(promise.then([](T&&) { return false; }, [](kj::Exception&&) { return true; }) .wait(waitScope)); } template <> void expectPromiseThrows(kj::Promise&& promise, kj::WaitScope& waitScope) { EXPECT_TRUE(promise.then([]() { return false; }, [](kj::Exception&&) { return true; }) .wait(waitScope)); } TEST(Rpc, EmbargoError) { TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto paf = kj::newPromiseAndFulfiller(); auto cap = test::TestCallOrder::Client(kj::mv(paf.promise)); auto earlyCall = client.getCallSequenceRequest().send(); auto echoRequest = client.echoRequest(); echoRequest.setCap(cap); auto echo = echoRequest.send(); auto pipeline = echo.getCap(); auto call0 = getCallSequence(pipeline, 0); auto call1 = getCallSequence(pipeline, 1); earlyCall.wait(context.waitScope); auto call2 = getCallSequence(pipeline, 2); auto resolved = echo.wait(context.waitScope).getCap(); auto call3 = getCallSequence(pipeline, 3); auto call4 = getCallSequence(pipeline, 4); auto call5 = getCallSequence(pipeline, 5); paf.fulfiller->rejectIfThrows([]() { KJ_FAIL_ASSERT("foo") { break; } }); expectPromiseThrows(kj::mv(call0), context.waitScope); expectPromiseThrows(kj::mv(call1), context.waitScope); expectPromiseThrows(kj::mv(call2), context.waitScope); expectPromiseThrows(kj::mv(call3), context.waitScope); expectPromiseThrows(kj::mv(call4), context.waitScope); expectPromiseThrows(kj::mv(call5), context.waitScope); // Verify that we're still connected (there were no protocol errors). getCallSequence(client, 1).wait(context.waitScope); } TEST(Rpc, EmbargoNull) { // Set up a situation where we pipeline on a capability that ends up coming back null. This // should NOT cause a Disembargo to be sent, but due to a bug in earlier versions of Cap'n Proto, // a Disembargo was indeed sent to the null capability, which caused the server to disconnect // due to protocol error. TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto promise = client.getNullRequest().send(); auto cap = promise.getNullCap(); auto call0 = cap.getCallSequenceRequest().send(); promise.wait(context.waitScope); auto call1 = cap.getCallSequenceRequest().send(); expectPromiseThrows(kj::mv(call0), context.waitScope); expectPromiseThrows(kj::mv(call1), context.waitScope); // Verify that we're still connected (there were no protocol errors). getCallSequence(client, 0).wait(context.waitScope); } TEST(Rpc, CallBrokenPromise) { // Tell the server to call back to a promise client, then resolve the promise to an error. TestContext context; auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) .castAs(); auto paf = kj::newPromiseAndFulfiller(); { auto req = client.holdRequest(); req.setCap(kj::mv(paf.promise)); req.send().wait(context.waitScope); } bool returned = false; auto req = client.callHeldRequest().send() .then([&](capnp::Response&&) { returned = true; }, [&](kj::Exception&& e) { returned = true; kj::throwRecoverableException(kj::mv(e)); }).eagerlyEvaluate(nullptr); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); EXPECT_FALSE(returned); paf.fulfiller->rejectIfThrows([]() { KJ_FAIL_ASSERT("foo") { break; } }); expectPromiseThrows(kj::mv(req), context.waitScope); EXPECT_TRUE(returned); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); kj::evalLater([]() {}).wait(context.waitScope); // Verify that we're still connected (there were no protocol errors). getCallSequence(client, 1).wait(context.waitScope); } TEST(Rpc, Abort) { // Verify that aborts are received. TestContext context; MallocMessageBuilder refMessage(128); auto hostId = refMessage.initRoot(); hostId.setHost("server"); auto conn = KJ_ASSERT_NONNULL(context.clientNetwork.connect(hostId)); { // Send an invalid message (Return to non-existent question). auto msg = conn->newOutgoingMessage(128); auto body = msg->getBody().initAs().initReturn(); body.setAnswerId(1234); body.setCanceled(); msg->send(); } auto reply = KJ_ASSERT_NONNULL(conn->receiveIncomingMessage().wait(context.waitScope)); EXPECT_EQ(rpc::Message::ABORT, reply->getBody().getAs().which()); EXPECT_TRUE(conn->receiveIncomingMessage().wait(context.waitScope) == nullptr); } // ======================================================================================= typedef RealmGateway TestRealmGateway; class TestGateway final: public TestRealmGateway::Server { public: kj::Promise import(ImportContext context) override { auto cap = context.getParams().getCap(); context.releaseParams(); return cap.saveRequest().send() .then([KJ_CPCAP(context)](Response::SaveResults> response) mutable { context.getResults().initSturdyRef().getObjectId().setAs( kj::str("imported-", response.getSturdyRef())); }); } kj::Promise export_(ExportContext context) override { auto cap = context.getParams().getCap(); context.releaseParams(); return cap.saveRequest().send() .then([KJ_CPCAP(context)] (Response::SaveResults> response) mutable { context.getResults().setSturdyRef(kj::str("exported-", response.getSturdyRef().getObjectId().getAs())); }); } }; class TestPersistent final: public Persistent::Server { public: TestPersistent(kj::StringPtr name): name(name) {} kj::Promise save(SaveContext context) override { context.initResults().initSturdyRef().getObjectId().setAs(name); return kj::READY_NOW; } private: kj::StringPtr name; }; class TestPersistentText final: public Persistent::Server { public: TestPersistentText(kj::StringPtr name): name(name) {} kj::Promise save(SaveContext context) override { context.initResults().setSturdyRef(name); return kj::READY_NOW; } private: kj::StringPtr name; }; TEST(Rpc, RealmGatewayImport) { TestRealmGateway::Client gateway = kj::heap(); Persistent::Client bootstrap = kj::heap("foo"); MallocMessageBuilder hostIdBuilder; auto hostId = hostIdBuilder.getRoot(); hostId.setHost("server"); TestContext context(bootstrap, gateway); auto client = context.rpcClient.bootstrap(hostId).castAs>(); auto response = client.saveRequest().send().wait(context.waitScope); EXPECT_EQ("imported-foo", response.getSturdyRef().getObjectId().getAs()); } TEST(Rpc, RealmGatewayExport) { TestRealmGateway::Client gateway = kj::heap(); Persistent::Client bootstrap = kj::heap("foo"); MallocMessageBuilder hostIdBuilder; auto hostId = hostIdBuilder.getRoot(); hostId.setHost("server"); TestContext context(bootstrap, gateway, true); auto client = context.rpcClient.bootstrap(hostId).castAs>(); auto response = client.saveRequest().send().wait(context.waitScope); EXPECT_EQ("exported-foo", response.getSturdyRef()); } TEST(Rpc, RealmGatewayImportExport) { // Test that a save request which leaves the realm, bounces through a promise capability, and // then comes back into the realm, does not actually get translated both ways. TestRealmGateway::Client gateway = kj::heap(); Persistent::Client bootstrap = kj::heap("foo"); MallocMessageBuilder serverHostIdBuilder; auto serverHostId = serverHostIdBuilder.getRoot(); serverHostId.setHost("server"); MallocMessageBuilder clientHostIdBuilder; auto clientHostId = clientHostIdBuilder.getRoot(); clientHostId.setHost("client"); kj::EventLoop loop; kj::WaitScope waitScope(loop); TestNetwork network; TestNetworkAdapter& clientNetwork = network.add("client"); TestNetworkAdapter& serverNetwork = network.add("server"); RpcSystem rpcClient = makeRpcServer(clientNetwork, bootstrap, gateway); auto paf = kj::newPromiseAndFulfiller(); RpcSystem rpcServer = makeRpcServer(serverNetwork, kj::mv(paf.promise)); auto client = rpcClient.bootstrap(serverHostId).castAs>(); bool responseReady = false; auto responsePromise = client.saveRequest().send() .then([&](Response::SaveResults>&& response) { responseReady = true; return kj::mv(response); }).eagerlyEvaluate(nullptr); // Crank the event loop to give the message time to reach the server and block on the promise // resolution. kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); EXPECT_FALSE(responseReady); paf.fulfiller->fulfill(rpcServer.bootstrap(clientHostId)); auto response = responsePromise.wait(waitScope); // Should have the original value. If it went through export and re-import, though, then this // will be "imported-exported-foo", which is wrong. EXPECT_EQ("foo", response.getSturdyRef().getObjectId().getAs()); } TEST(Rpc, RealmGatewayImportExport) { // Test that a save request which enters the realm, bounces through a promise capability, and // then goes back out of the realm, does not actually get translated both ways. TestRealmGateway::Client gateway = kj::heap(); Persistent::Client bootstrap = kj::heap("foo"); MallocMessageBuilder serverHostIdBuilder; auto serverHostId = serverHostIdBuilder.getRoot(); serverHostId.setHost("server"); MallocMessageBuilder clientHostIdBuilder; auto clientHostId = clientHostIdBuilder.getRoot(); clientHostId.setHost("client"); kj::EventLoop loop; kj::WaitScope waitScope(loop); TestNetwork network; TestNetworkAdapter& clientNetwork = network.add("client"); TestNetworkAdapter& serverNetwork = network.add("server"); RpcSystem rpcClient = makeRpcServer(clientNetwork, bootstrap); auto paf = kj::newPromiseAndFulfiller(); RpcSystem rpcServer = makeRpcServer(serverNetwork, kj::mv(paf.promise), gateway); auto client = rpcClient.bootstrap(serverHostId).castAs>(); bool responseReady = false; auto responsePromise = client.saveRequest().send() .then([&](Response::SaveResults>&& response) { responseReady = true; return kj::mv(response); }).eagerlyEvaluate(nullptr); // Crank the event loop to give the message time to reach the server and block on the promise // resolution. kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); kj::evalLater([]() {}).wait(waitScope); EXPECT_FALSE(responseReady); paf.fulfiller->fulfill(rpcServer.bootstrap(clientHostId)); auto response = responsePromise.wait(waitScope); // Should have the original value. If it went through import and re-export, though, then this // will be "exported-imported-foo", which is wrong. EXPECT_EQ("foo", response.getSturdyRef()); } KJ_TEST("loopback bootstrap()") { int callCount = 0; test::TestInterface::Client bootstrap = kj::heap(callCount); MallocMessageBuilder hostIdBuilder; auto hostId = hostIdBuilder.getRoot(); hostId.setHost("server"); TestContext context(bootstrap); auto client = context.rpcServer.bootstrap(hostId).castAs(); auto request = client.fooRequest(); request.setI(123); request.setJ(true); auto response = request.send().wait(context.waitScope); KJ_EXPECT(response.getX() == "foo"); KJ_EXPECT(callCount == 1); } } // namespace } // namespace _ (private) } // namespace capnp capnproto-c++-0.8.0/src/capnp/rpc-twoparty-test.c++0000644000175000017500000006253313650101756022577 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_TESTING_CAPNP 1 #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "rpc-twoparty.h" #include "test-util.h" #include #include #include #include #include // Includes just for need SOL_SOCKET and SO_SNDBUF #if _WIN32 #define WIN32_LEAN_AND_MEAN // ::eyeroll:: #include #include #include #else #include #endif // TODO(cleanup): Auto-generate stringification functions for union discriminants. namespace capnp { namespace rpc { inline kj::String KJ_STRINGIFY(Message::Which which) { return kj::str(static_cast(which)); } } // namespace rpc } // namespace capnp namespace capnp { namespace _ { namespace { class TestRestorer final: public SturdyRefRestorer { public: TestRestorer(int& callCount, int& handleCount) : callCount(callCount), handleCount(handleCount) {} Capability::Client restore(test::TestSturdyRefObjectId::Reader objectId) override { switch (objectId.getTag()) { case test::TestSturdyRefObjectId::Tag::TEST_INTERFACE: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_EXTENDS: return Capability::Client(newBrokenCap("No TestExtends implemented.")); case test::TestSturdyRefObjectId::Tag::TEST_PIPELINE: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLEE: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLER: return kj::heap(callCount); case test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF: return kj::heap(callCount, handleCount); } KJ_UNREACHABLE; } private: int& callCount; int& handleCount; }; kj::AsyncIoProvider::PipeThread runServer(kj::AsyncIoProvider& ioProvider, int& callCount, int& handleCount) { return ioProvider.newPipeThread( [&callCount, &handleCount]( kj::AsyncIoProvider& ioProvider, kj::AsyncIoStream& stream, kj::WaitScope& waitScope) { TwoPartyVatNetwork network(stream, rpc::twoparty::Side::SERVER); TestRestorer restorer(callCount, handleCount); auto server = makeRpcServer(network, restorer); network.onDisconnect().wait(waitScope); }); } Capability::Client getPersistentCap(RpcSystem& client, rpc::twoparty::Side side, test::TestSturdyRefObjectId::Tag tag) { // Create the VatId. MallocMessageBuilder hostIdMessage(8); auto hostId = hostIdMessage.initRoot(); hostId.setSide(side); // Create the SturdyRefObjectId. MallocMessageBuilder objectIdMessage(8); objectIdMessage.initRoot().setTag(tag); // Connect to the remote capability. return client.restore(hostId, objectIdMessage.getRoot()); } TEST(TwoPartyNetwork, Basic) { auto ioContext = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; auto serverThread = runServer(*ioContext.provider, callCount, handleCount); TwoPartyVatNetwork network(*serverThread.pipe, rpc::twoparty::Side::CLIENT); auto rpcClient = makeRpcClient(network); // Request the particular capability from the server. auto client = getPersistentCap(rpcClient, rpc::twoparty::Side::SERVER, test::TestSturdyRefObjectId::Tag::TEST_INTERFACE).castAs(); // Use the capability. auto request1 = client.fooRequest(); request1.setI(123); request1.setJ(true); auto promise1 = request1.send(); auto request2 = client.bazRequest(); initTestMessage(request2.initS()); auto promise2 = request2.send(); bool barFailed = false; auto request3 = client.barRequest(); auto promise3 = request3.send().then( [](Response&& response) { ADD_FAILURE() << "Expected bar() call to fail."; }, [&](kj::Exception&& e) { barFailed = true; }); EXPECT_EQ(0, callCount); auto response1 = promise1.wait(ioContext.waitScope); EXPECT_EQ("foo", response1.getX()); auto response2 = promise2.wait(ioContext.waitScope); promise3.wait(ioContext.waitScope); EXPECT_EQ(2, callCount); EXPECT_TRUE(barFailed); } TEST(TwoPartyNetwork, Pipelining) { auto ioContext = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; int reverseCallCount = 0; // Calls back from server to client. auto serverThread = runServer(*ioContext.provider, callCount, handleCount); TwoPartyVatNetwork network(*serverThread.pipe, rpc::twoparty::Side::CLIENT); auto rpcClient = makeRpcClient(network); bool disconnected = false; kj::Promise disconnectPromise = network.onDisconnect().then([&]() { disconnected = true; }); { // Request the particular capability from the server. auto client = getPersistentCap(rpcClient, rpc::twoparty::Side::SERVER, test::TestSturdyRefObjectId::Tag::TEST_PIPELINE).castAs(); { // Use the capability. auto request = client.getCapRequest(); request.setN(234); request.setInCap(kj::heap(reverseCallCount)); auto promise = request.send(); auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); pipelineRequest.setI(321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = promise.getOutBox().getCap() .castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); promise = nullptr; // Just to be annoying, drop the original promise. EXPECT_EQ(0, callCount); EXPECT_EQ(0, reverseCallCount); auto response = pipelinePromise.wait(ioContext.waitScope); EXPECT_EQ("bar", response.getX()); auto response2 = pipelinePromise2.wait(ioContext.waitScope); checkTestMessage(response2); EXPECT_EQ(3, callCount); EXPECT_EQ(1, reverseCallCount); } EXPECT_FALSE(disconnected); // What if we disconnect? serverThread.pipe->shutdownWrite(); // The other side should also disconnect. disconnectPromise.wait(ioContext.waitScope); { // Use the now-broken capability. auto request = client.getCapRequest(); request.setN(234); request.setInCap(kj::heap(reverseCallCount)); auto promise = request.send(); auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); pipelineRequest.setI(321); auto pipelinePromise = pipelineRequest.send(); auto pipelineRequest2 = promise.getOutBox().getCap() .castAs().graultRequest(); auto pipelinePromise2 = pipelineRequest2.send(); EXPECT_ANY_THROW(pipelinePromise.wait(ioContext.waitScope)); EXPECT_ANY_THROW(pipelinePromise2.wait(ioContext.waitScope)); EXPECT_EQ(3, callCount); EXPECT_EQ(1, reverseCallCount); } } } TEST(TwoPartyNetwork, Release) { auto ioContext = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; auto serverThread = runServer(*ioContext.provider, callCount, handleCount); TwoPartyVatNetwork network(*serverThread.pipe, rpc::twoparty::Side::CLIENT); auto rpcClient = makeRpcClient(network); // Request the particular capability from the server. auto client = getPersistentCap(rpcClient, rpc::twoparty::Side::SERVER, test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF).castAs(); auto handle1 = client.getHandleRequest().send().wait(ioContext.waitScope).getHandle(); auto promise = client.getHandleRequest().send(); auto handle2 = promise.wait(ioContext.waitScope).getHandle(); EXPECT_EQ(2, handleCount); handle1 = nullptr; // There once was a bug where the last outgoing message (and any capabilities attached) would // not get cleaned up (until a new message was sent). This appeared to be a bug in Release, // because if a client received a message and then released a capability from it but then did // not make any further calls, then the capability would not be released because the message // introducing it remained the last server -> client message (because a "Release" message has // no reply). Here we are explicitly trying to catch this bug. This proves tricky, because when // we drop a reference on the client side, there's no particular way to wait for the release // message to reach the server except to make a subsequent call and wait for the return -- but // that would mask the bug. So, we wait spin waiting for handleCount to change. uint maxSpins = 1000; while (handleCount > 1) { ioContext.provider->getTimer().afterDelay(10 * kj::MILLISECONDS).wait(ioContext.waitScope); KJ_ASSERT(--maxSpins > 0); } EXPECT_EQ(1, handleCount); handle2 = nullptr; ioContext.provider->getTimer().afterDelay(10 * kj::MILLISECONDS).wait(ioContext.waitScope); EXPECT_EQ(1, handleCount); promise = nullptr; while (handleCount > 0) { ioContext.provider->getTimer().afterDelay(10 * kj::MILLISECONDS).wait(ioContext.waitScope); KJ_ASSERT(--maxSpins > 0); } EXPECT_EQ(0, handleCount); } TEST(TwoPartyNetwork, Abort) { // Verify that aborts are received. auto ioContext = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; auto serverThread = runServer(*ioContext.provider, callCount, handleCount); TwoPartyVatNetwork network(*serverThread.pipe, rpc::twoparty::Side::CLIENT); MallocMessageBuilder refMessage(128); auto hostId = refMessage.initRoot(); hostId.setSide(rpc::twoparty::Side::SERVER); auto conn = KJ_ASSERT_NONNULL(network.connect(hostId)); { // Send an invalid message (Return to non-existent question). auto msg = conn->newOutgoingMessage(128); auto body = msg->getBody().initAs().initReturn(); body.setAnswerId(1234); body.setCanceled(); msg->send(); } auto reply = KJ_ASSERT_NONNULL(conn->receiveIncomingMessage().wait(ioContext.waitScope)); EXPECT_EQ(rpc::Message::ABORT, reply->getBody().getAs().which()); EXPECT_TRUE(conn->receiveIncomingMessage().wait(ioContext.waitScope) == nullptr); } TEST(TwoPartyNetwork, ConvenienceClasses) { auto ioContext = kj::setupAsyncIo(); int callCount = 0; TwoPartyServer server(kj::heap(callCount)); auto address = ioContext.provider->getNetwork() .parseAddress("127.0.0.1").wait(ioContext.waitScope); auto listener = address->listen(); auto listenPromise = server.listen(*listener); address = ioContext.provider->getNetwork() .parseAddress("127.0.0.1", listener->getPort()).wait(ioContext.waitScope); auto connection = address->connect().wait(ioContext.waitScope); TwoPartyClient client(*connection); auto cap = client.bootstrap().castAs(); auto request = cap.fooRequest(); request.setI(123); request.setJ(true); EXPECT_EQ(0, callCount); auto response = request.send().wait(ioContext.waitScope); EXPECT_EQ("foo", response.getX()); EXPECT_EQ(1, callCount); } TEST(TwoPartyNetwork, HugeMessage) { auto ioContext = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; auto serverThread = runServer(*ioContext.provider, callCount, handleCount); TwoPartyVatNetwork network(*serverThread.pipe, rpc::twoparty::Side::CLIENT); auto rpcClient = makeRpcClient(network); auto client = getPersistentCap(rpcClient, rpc::twoparty::Side::SERVER, test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF).castAs(); // Oversized request fails. { auto req = client.methodWithDefaultsRequest(); req.initA(100000000); // 100 MB KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("larger than our single-message size limit", req.send().ignoreResult().wait(ioContext.waitScope)); } // Oversized response fails. KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("larger than our single-message size limit", client.getEnormousStringRequest().send().ignoreResult().wait(ioContext.waitScope)); // Connection is still up. { auto req = client.getCallSequenceRequest(); req.setExpected(0); KJ_EXPECT(req.send().wait(ioContext.waitScope).getN() == 0); } } class TestAuthenticatedBootstrapImpl final : public test::TestAuthenticatedBootstrap::Server { public: TestAuthenticatedBootstrapImpl(rpc::twoparty::VatId::Reader clientId) { this->clientId.setRoot(clientId); } protected: kj::Promise getCallerId(GetCallerIdContext context) override { context.getResults().setCaller(clientId.getRoot()); return kj::READY_NOW; } private: MallocMessageBuilder clientId; }; class TestBootstrapFactory: public BootstrapFactory { public: Capability::Client createFor(rpc::twoparty::VatId::Reader clientId) { called = true; EXPECT_EQ(rpc::twoparty::Side::CLIENT, clientId.getSide()); return kj::heap(clientId); } bool called = false; }; kj::AsyncIoProvider::PipeThread runAuthenticatingServer( kj::AsyncIoProvider& ioProvider, BootstrapFactory& bootstrapFactory) { return ioProvider.newPipeThread([&bootstrapFactory]( kj::AsyncIoProvider& ioProvider, kj::AsyncIoStream& stream, kj::WaitScope& waitScope) { TwoPartyVatNetwork network(stream, rpc::twoparty::Side::SERVER); auto server = makeRpcServer(network, bootstrapFactory); network.onDisconnect().wait(waitScope); }); } TEST(TwoPartyNetwork, BootstrapFactory) { auto ioContext = kj::setupAsyncIo(); TestBootstrapFactory bootstrapFactory; auto serverThread = runAuthenticatingServer(*ioContext.provider, bootstrapFactory); TwoPartyClient client(*serverThread.pipe); auto resp = client.bootstrap().castAs>() .getCallerIdRequest().send().wait(ioContext.waitScope); EXPECT_EQ(rpc::twoparty::Side::CLIENT, resp.getCaller().getSide()); EXPECT_TRUE(bootstrapFactory.called); } // ======================================================================================= #if !_WIN32 && !__CYGWIN__ // Windows and Cygwin don't support SCM_RIGHTS. KJ_TEST("send FD over RPC") { auto io = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; TwoPartyServer server(kj::heap(callCount, handleCount)); auto pipe = io.provider->newCapabilityPipe(); server.accept(kj::mv(pipe.ends[0]), 2); TwoPartyClient client(*pipe.ends[1], 2); auto cap = client.bootstrap().castAs(); int pipeFds[2]; KJ_SYSCALL(kj::miniposix::pipe(pipeFds)); kj::AutoCloseFd in1(pipeFds[0]); kj::AutoCloseFd out1(pipeFds[1]); KJ_SYSCALL(kj::miniposix::pipe(pipeFds)); kj::AutoCloseFd in2(pipeFds[0]); kj::AutoCloseFd out2(pipeFds[1]); capnp::RemotePromise promise = nullptr; { auto req = cap.writeToFdRequest(); // Order reversal intentional, just trying to mix things up. req.setFdCap1(kj::heap(kj::mv(out2))); req.setFdCap2(kj::heap(kj::mv(out1))); promise = req.send(); } int in3 = KJ_ASSERT_NONNULL(promise.getFdCap3().getFd().wait(io.waitScope)); KJ_EXPECT(io.lowLevelProvider->wrapInputFd(kj::mv(in3))->readAllText().wait(io.waitScope) == "baz"); { auto promise2 = kj::mv(promise); // make sure the PipelineHook also goes out of scope auto response = promise2.wait(io.waitScope); KJ_EXPECT(response.getSecondFdPresent()); } KJ_EXPECT(io.lowLevelProvider->wrapInputFd(kj::mv(in1))->readAllText().wait(io.waitScope) == "bar"); KJ_EXPECT(io.lowLevelProvider->wrapInputFd(kj::mv(in2))->readAllText().wait(io.waitScope) == "foo"); } KJ_TEST("FD per message limit") { auto io = kj::setupAsyncIo(); int callCount = 0; int handleCount = 0; TwoPartyServer server(kj::heap(callCount, handleCount)); auto pipe = io.provider->newCapabilityPipe(); server.accept(kj::mv(pipe.ends[0]), 1); TwoPartyClient client(*pipe.ends[1], 1); auto cap = client.bootstrap().castAs(); int pipeFds[2]; KJ_SYSCALL(kj::miniposix::pipe(pipeFds)); kj::AutoCloseFd in1(pipeFds[0]); kj::AutoCloseFd out1(pipeFds[1]); KJ_SYSCALL(kj::miniposix::pipe(pipeFds)); kj::AutoCloseFd in2(pipeFds[0]); kj::AutoCloseFd out2(pipeFds[1]); capnp::RemotePromise promise = nullptr; { auto req = cap.writeToFdRequest(); // Order reversal intentional, just trying to mix things up. req.setFdCap1(kj::heap(kj::mv(out2))); req.setFdCap2(kj::heap(kj::mv(out1))); promise = req.send(); } int in3 = KJ_ASSERT_NONNULL(promise.getFdCap3().getFd().wait(io.waitScope)); KJ_EXPECT(io.lowLevelProvider->wrapInputFd(kj::mv(in3))->readAllText().wait(io.waitScope) == "baz"); { auto promise2 = kj::mv(promise); // make sure the PipelineHook also goes out of scope auto response = promise2.wait(io.waitScope); KJ_EXPECT(!response.getSecondFdPresent()); } KJ_EXPECT(io.lowLevelProvider->wrapInputFd(kj::mv(in1))->readAllText().wait(io.waitScope) == ""); KJ_EXPECT(io.lowLevelProvider->wrapInputFd(kj::mv(in2))->readAllText().wait(io.waitScope) == "foo"); } #endif // !_WIN32 && !__CYGWIN__ // ======================================================================================= class MockSndbufStream final: public kj::AsyncIoStream { public: MockSndbufStream(kj::Own inner, size_t& window, size_t& written) : inner(kj::mv(inner)), window(window), written(written) {} kj::Promise read(void* buffer, size_t minBytes, size_t maxBytes) override { return inner->read(buffer, minBytes, maxBytes); } kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return inner->tryRead(buffer, minBytes, maxBytes); } kj::Maybe tryGetLength() override { return inner->tryGetLength(); } kj::Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return inner->pumpTo(output, amount); } kj::Promise write(const void* buffer, size_t size) override { written += size; return inner->write(buffer, size); } kj::Promise write(kj::ArrayPtr> pieces) override { for (auto& piece: pieces) written += piece.size(); return inner->write(pieces); } kj::Maybe> tryPumpFrom( kj::AsyncInputStream& input, uint64_t amount) override { return inner->tryPumpFrom(input, amount); } kj::Promise whenWriteDisconnected() override { return inner->whenWriteDisconnected(); } void shutdownWrite() override { return inner->shutdownWrite(); } void abortRead() override { return inner->abortRead(); } void getsockopt(int level, int option, void* value, uint* length) override { if (level == SOL_SOCKET && option == SO_SNDBUF) { KJ_ASSERT(*length == sizeof(int)); *reinterpret_cast(value) = window; } else { KJ_UNIMPLEMENTED("not implemented for test", level, option); } } private: kj::Own inner; size_t& window; size_t& written; }; KJ_TEST("Streaming over RPC") { kj::EventLoop loop; kj::WaitScope waitScope(loop); auto pipe = kj::newTwoWayPipe(); size_t window = 1024; size_t clientWritten = 0; size_t serverWritten = 0; pipe.ends[0] = kj::heap(kj::mv(pipe.ends[0]), window, clientWritten); pipe.ends[1] = kj::heap(kj::mv(pipe.ends[1]), window, serverWritten); auto ownServer = kj::heap(); auto& server = *ownServer; test::TestStreaming::Client serverCap(kj::mv(ownServer)); TwoPartyClient tpClient(*pipe.ends[0]); TwoPartyClient tpServer(*pipe.ends[1], serverCap, rpc::twoparty::Side::SERVER); auto cap = tpClient.bootstrap().castAs(); // Send stream requests until we can't anymore. kj::Promise promise = kj::READY_NOW; uint count = 0; while (promise.poll(waitScope)) { promise.wait(waitScope); auto req = cap.doStreamIRequest(); req.setI(++count); promise = req.send(); } // We should have sent... several. KJ_EXPECT(count > 5); // Now, cause calls to finish server-side one-at-a-time and check that this causes the client // side to be willing to send more. uint countReceived = 0; for (uint i = 0; i < 50; i++) { KJ_EXPECT(server.iSum == ++countReceived); server.iSum = 0; KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_ASSERT(promise.poll(waitScope)); promise.wait(waitScope); auto req = cap.doStreamIRequest(); req.setI(++count); promise = req.send(); if (promise.poll(waitScope)) { // We'll see a couple of instances where completing one request frees up space to make two // more. This is because the first few requests we made are a little bit larger than the // rest due to being pipelined on the bootstrap. Once the bootstrap resolves, the request // size gets smaller. promise.wait(waitScope); req = cap.doStreamIRequest(); req.setI(++count); promise = req.send(); // We definitely shouldn't have freed up stream space for more than two additional requests! KJ_ASSERT(!promise.poll(waitScope)); } } } KJ_TEST("Streaming over RPC then unwrap with CapabilitySet") { kj::EventLoop loop; kj::WaitScope waitScope(loop); auto pipe = kj::newTwoWayPipe(); CapabilityServerSet capSet; auto ownServer = kj::heap(); auto& server = *ownServer; auto serverCap = capSet.add(kj::mv(ownServer)); auto paf = kj::newPromiseAndFulfiller(); TwoPartyClient tpClient(*pipe.ends[0], serverCap); TwoPartyClient tpServer(*pipe.ends[1], kj::mv(paf.promise), rpc::twoparty::Side::SERVER); auto clientCap = tpClient.bootstrap().castAs(); // Send stream requests until we can't anymore. kj::Promise promise = kj::READY_NOW; uint count = 0; while (promise.poll(waitScope)) { promise.wait(waitScope); auto req = clientCap.doStreamIRequest(); req.setI(++count); promise = req.send(); } // We should have sent... several. KJ_EXPECT(count > 10); // Now try to unwrap. auto unwrapPromise = capSet.getLocalServer(clientCap); // It won't work yet, obviously, because we haven't resolved the promise. KJ_EXPECT(!unwrapPromise.poll(waitScope)); // So do that. paf.fulfiller->fulfill(tpServer.bootstrap().castAs()); clientCap.whenResolved().wait(waitScope); // But the unwrap still doesn't resolve because streaming requests are queued up. KJ_EXPECT(!unwrapPromise.poll(waitScope)); // OK, let's resolve a streaming request. KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); // All of our call promises have now completed from the client's perspective. promise.wait(waitScope); // But we still can't unwrap, because calls are queued server-side. KJ_EXPECT(!unwrapPromise.poll(waitScope)); // Let's even make one more call now. But this is actually a local call since the promise // resolved. { auto req = clientCap.doStreamIRequest(); req.setI(++count); promise = req.send(); } // Because it's a local call, it doesn't resolve early. The window is no longer in effect. KJ_EXPECT(!promise.poll(waitScope)); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(!promise.poll(waitScope)); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(!promise.poll(waitScope)); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(!promise.poll(waitScope)); KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); KJ_EXPECT(!promise.poll(waitScope)); // Our unwrap promise is also still not resolved. KJ_EXPECT(!unwrapPromise.poll(waitScope)); // Close out stream calls until it does resolve! while (!unwrapPromise.poll(waitScope)) { KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); } // Now we can unwrap! KJ_EXPECT(&KJ_ASSERT_NONNULL(unwrapPromise.wait(waitScope)) == &server); // But our last stream call still isn't done. KJ_EXPECT(!promise.poll(waitScope)); // Finish it. KJ_ASSERT_NONNULL(server.fulfiller)->fulfill(); promise.wait(waitScope); } } // namespace } // namespace _ } // namespace capnp capnproto-c++-0.8.0/src/capnp/ez-rpc-test.c++0000644000175000017500000000531613340402540021307 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define CAPNP_TESTING_CAPNP 1 #include "ez-rpc.h" #include "test-util.h" #include namespace capnp { namespace _ { namespace { TEST(EzRpc, Basic) { int callCount = 0; EzRpcServer server(kj::heap(callCount), "localhost"); EzRpcClient client("localhost", server.getPort().wait(server.getWaitScope())); auto cap = client.getMain(); auto request = cap.fooRequest(); request.setI(123); request.setJ(true); EXPECT_EQ(0, callCount); auto response = request.send().wait(server.getWaitScope()); EXPECT_EQ("foo", response.getX()); EXPECT_EQ(1, callCount); } TEST(EzRpc, DeprecatedNames) { EzRpcServer server("localhost"); int callCount = 0; server.exportCap("cap1", kj::heap(callCount)); server.exportCap("cap2", kj::heap()); EzRpcClient client("localhost", server.getPort().wait(server.getWaitScope())); auto cap = client.importCap("cap1"); auto request = cap.fooRequest(); request.setI(123); request.setJ(true); EXPECT_EQ(0, callCount); auto response = request.send().wait(server.getWaitScope()); EXPECT_EQ("foo", response.getX()); EXPECT_EQ(1, callCount); EXPECT_EQ(0, client.importCap("cap2").castAs() .getCallSequenceRequest().send().wait(server.getWaitScope()).getN()); EXPECT_EQ(1, client.importCap("cap2").castAs() .getCallSequenceRequest().send().wait(server.getWaitScope()).getN()); } } // namespace } // namespace _ } // namespace capnp capnproto-c++-0.8.0/src/capnp/test.capnp0000644000175000017500000007023513650101756020655 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xd508eebdc2dc42b8; using Cxx = import "c++.capnp"; # Use a namespace likely to cause trouble if the generated code doesn't use fully-qualified # names for stuff in the capnproto namespace. $Cxx.namespace("capnproto_test::capnp::test"); enum TestEnum { foo @0; bar @1; baz @2; qux @3; quux @4; corge @5; grault @6; garply @7; } 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; enumField @15 : TestEnum; 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); enumList @32 : List(TestEnum); interfaceList @33 : List(Void); # TODO } 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"; dataField @13 : Data = 0x"62 61 72"; # "bar" 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")), 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 = [ (textField = "x " "structlist" " 1"), (textField = "x structlist 2"), (textField = "x structlist 3")], enumList = [qux, bar, grault] # interfaceList can't have a default ); enumField @15 : TestEnum = corge; 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]; float32List @27 : List(Float32) = [5555.5, inf, -inf, nan]; float64List @28 : List(Float64) = [7777.75, inf, -inf, nan]; 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")]; enumList @32 : List(TestEnum) = [foo, garply]; interfaceList @33 : List(Void); # TODO } struct TestAnyPointer { anyPointerField @0 :AnyPointer; # Do not add any other fields here! Some tests rely on anyPointerField being the last pointer # in the struct. } struct TestAnyOthers { anyStructField @0 :AnyStruct; anyListField @1 :AnyList; capabilityField @2 :Capability; } 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; } struct TestUnion { union0 @0! :union { # 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; # 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; } # Pack one bit in order to make pathological situation for union1. bit0 @18: Bool; union1 @1! :union { # 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; # Pack more stuff into union1 -- each should go into the same space as corresponding u1f0s*. u1f2s0 @32: Void; u1f2s1 @33: Bool; u1f2s8 @34: Int8; u1f2s16 @35: Int16; u1f2s32 @36: Int32; u1f2s64 @37: Int64; u1f2sp @38: Text; } # 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. union2 @2! :union { u2f0s64 @54: Int64; u2f0s32 @52: Int32; u2f0s16 @50: Int16; u2f0s8 @47: Int8; u2f0s1 @45: Bool; } union3 @3! :union { u3f0s64 @55: Int64; u3f0s32 @53: Int32; u3f0s16 @51: Int16; u3f0s8 @48: Int8; u3f0s1 @46: Bool; } byte0 @49: UInt8; } struct TestUnnamedUnion { before @0 :Text; union { foo @1 :UInt16; bar @3 :UInt32; } middle @2 :UInt16; after @4 :Text; } struct TestUnionInUnion { # There is no reason to ever do this. outer :union { inner :union { foo @0 :Int32; bar @1 :Int32; } baz @2 :Int32; } } struct TestGroups { groups :union { foo :group { corge @0 :Int32; grault @2 :Int64; garply @8 :Text; } bar :group { corge @3 :Int32; grault @4 :Text; garply @5 :Int64; } baz :group { corge @1 :Int32; grault @6 :Text; garply @7 :Text; } } } 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; } } struct TestUnionDefaults { s16s8s64s8Set @0 :TestUnion = (union0 = (u0f0s16 = 321), union1 = (u1f0s8 = 123), union2 = (u2f0s64 = 12345678901234567), union3 = (u3f0s8 = 55)); s0sps1s32Set @1 :TestUnion = (union0 = (u0f1s0 = void), union1 = (u1f0sp = "foo"), union2 = (u2f0s1 = true), union3 = (u3f0s32 = 12345678)); unnamed1 @2 :TestUnnamedUnion = (foo = 123); unnamed2 @3 :TestUnnamedUnion = (bar = 321, before = "foo", after = "bar"); } 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; } struct TestUsing { using OuterNestedEnum = TestNestedTypes.NestedEnum; using TestNestedTypes.NestedStruct.NestedEnum; outerNestedEnum @1 :OuterNestedEnum = bar; innerNestedEnum @0 :NestedEnum = quux; } struct TestLists { # Small structs, when encoded as list, will be encoded as primitive lists rather than struct # lists, to save space. 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; } # 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; } 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); int32ListList @7 :List(List(Int32)); textListList @8 :List(List(Text)); structListList @9 :List(List(TestAllTypes)); } struct TestFieldZeroIsBit { bit @0 :Bool; secondBit @1 :Bool = true; thirdField @2 :UInt8 = 123; } struct TestListDefaults { lists @0 :TestLists = ( list0 = [(f = void), (f = void)], list1 = [(f = true), (f = false), (f = true), (f = true)], list8 = [(f = 123), (f = 45)], list16 = [(f = 12345), (f = 6789)], list32 = [(f = 123456789), (f = 234567890)], list64 = [(f = 1234567890123456), (f = 2345678901234567)], listP = [(f = "foo"), (f = "bar")], int32ListList = [[1, 2, 3], [4, 5], [12341234]], textListList = [["foo", "bar"], ["baz"], ["qux", "corge"]], structListList = [[(int32Field = 123), (int32Field = 456)], [(int32Field = 789)]]); } struct TestLateUnion { # Test what happens if the unions are not the first ordinals in the struct. At one point this # was broken for the dynamic API. foo @0 :Int32; bar @1 :Text; baz @2 :Int16; theUnion @3! :union { qux @4 :Text; corge @5 :List(Int32); grault @6 :Float32; } anotherUnion @7! :union { qux @8 :Text; corge @9 :List(Int32); grault @10 :Float32; } } struct TestOldVersion { # A subset of TestNewVersion. old1 @0 :Int64; old2 @1 :Text; old3 @2 :TestOldVersion; } struct TestNewVersion { # A superset of TestOldVersion. old1 @0 :Int64; old2 @1 :Text; old3 @2 :TestNewVersion; new1 @3 :Int64 = 987; new2 @4 :Text = "baz"; } struct TestOldUnionVersion { union { a @0 :Void; b @1 :UInt64; } } struct TestNewUnionVersion { union { a :union { a0 @0 :Void; a1 @2 :UInt64; } b @1 :UInt64; } } struct TestStructUnion { un @0! :union { struct @1 :SomeStruct; object @2 :TestAnyPointer; } 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; } } 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; bigField @1 :Float32 = 2e30; const constant :Float32 = 456; const bigConstant :Float32 = 4e30; } struct TestGenerics(Foo, Bar) { foo @0 :Foo; rev @1 :TestGenerics(Bar, Foo); union { uv @2:Void; ug :group { ugfoo @3:Int32; } } list @4 :List(Inner); # At one time this failed to compile with MSVC due to poor expression SFINAE support. struct Inner { foo @0 :Foo; bar @1 :Bar; } struct Inner2(Baz) { bar @0 :Bar; baz @1 :Baz; innerBound @2 :Inner; innerUnbound @3 :TestGenerics.Inner; struct DeepNest(Qux) { foo @0 :Foo; bar @1 :Bar; baz @2 :Baz; qux @3 :Qux; interface DeepNestInterface(Quux) { # At one time this failed to compile. call @0 () -> (); } } } interface Interface(Qux) { call @0 Inner2(Text) -> (qux :Qux, gen :TestGenerics(TestAllTypes, TestAnyPointer)); } annotation ann(struct) :Foo; 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; } } struct TestGenericsWrapper(Foo, Bar) { value @0 :TestGenerics(Foo, Bar); } struct TestGenericsWrapper2 { value @0 :TestGenericsWrapper(Text, TestAllTypes); } interface TestImplicitMethodParams { call @0 [T, U] (foo :T, bar :U) -> TestGenerics(T, U); } interface TestImplicitMethodParamsInGeneric(V) { call @0 [T, U] (foo :T, bar :U) -> TestGenerics(T, U); } struct TestGenericsUnion(Foo, Bar) { # At one point this failed to compile. union { foo @0 :Foo; bar @1 :Bar; } } 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); unspecified @3 :TestGenerics; unspecifiedInner @4 :TestGenerics.Inner2(Text); wrapper @8 :TestGenericsWrapper(TestAllTypes, TestAnyPointer); cap @18 :TestGenerics(TestInterface, Text); genericCap @19 :TestGenerics(TestAllTypes, List(UInt32)).Interface(Data); 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))))); 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]); } struct TestEmptyStruct {} 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 = [ (textField = "x " "structlist" " 1"), (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); const globalPrintableStruct :TestPrintInlineStructs = (someText = "foo"); const derivedConstant :TestAllTypes = ( uInt32Field = .globalInt, textField = TestConstants.textConst, structField = TestConstants.structConst, int16List = TestConstants.int16ListConst, structList = TestConstants.structListConst); const genericConstant :TestGenerics(TestAllTypes, Text) = (foo = (int16Field = 123), rev = (foo = "text", rev = (foo = (int16Field = 321)))); const embeddedData :Data = embed "testdata/packed"; const embeddedText :Text = embed "testdata/short.txt"; const embeddedStruct :TestAllTypes = embed "testdata/binary"; const nonAsciiText :Text = "♫ é ✓"; 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, ); interface TestInterface { foo @0 (i :UInt32, j :Bool) -> (x :Text); bar @1 () -> (); baz @2 (s: TestAllTypes); } interface TestExtends extends(TestInterface) { qux @0 (); corge @1 TestAllTypes -> (); grault @2 () -> TestAllTypes; } interface TestExtends2 extends(TestExtends) {} interface TestPipeline { getCap @0 (n: UInt32, inCap :TestInterface) -> (s: Text, outBox :Box); testPointers @1 (cap :TestInterface, obj :AnyPointer, list :List(TestInterface)) -> (); getAnyCap @2 (n: UInt32, inCap :Capability) -> (s: Text, outBox :AnyBox); struct Box { cap @0 :TestInterface; } struct AnyBox { cap @0 :Capability; } } interface TestCallOrder { getCallSequence @0 (expected: UInt32) -> (n: UInt32); # First call returns 0, next returns 1, ... # # The input `expected` is ignored but useful for disambiguating debug logs. } 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; } interface TestStreaming { doStreamI @0 (i :UInt32) -> stream; doStreamJ @1 (j :UInt32) -> stream; finishStream @2 () -> (totalI :UInt32, totalJ :UInt32); # Test streaming. finishStream() returns the totals of the values streamed to the other calls. } interface TestHandle {} 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. 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). echo @6 (cap :TestCallOrder) -> (cap :TestCallOrder); # Just returns the input cap. expectCancel @7 (cap :TestInterface) -> (); # evalLater()-loops forever, holding `cap`. Must be canceled. methodWithDefaults @8 (a :Text, b :UInt32 = 123, c :Text = "foo") -> (d :Text, e :Text = "bar"); methodWithNullDefault @12 (a :Text, b :TestInterface = null); 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. getNull @10 () -> (nullCap :TestMoreStuff); # Always returns a null capability. getEnormousString @11 () -> (str :Text); # Attempts to return an 100MB string. Should always fail. writeToFd @13 (fdCap1 :TestInterface, fdCap2 :TestInterface) -> (fdCap3 :TestInterface, secondFdPresent :Bool); # Expects fdCap1 and fdCap2 wrap socket file descriptors. Writes "foo" to the first and "bar" to # the second. Also creates a socketpair, writes "baz" to one end, and returns the other end. } 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); waitForever @4 (); interface Thing { passThrough @0 () -> Result; intercept @1 () -> Result; } struct Result { text @0 :Text; } } struct TestContainMembrane { cap @0 :TestMembrane.Thing; list @1 :List(TestMembrane.Thing); } struct TestTransferCap { list @0 :List(Element); struct Element { text @0 :Text; cap @1 :TestInterface; } } interface TestKeywordMethods { delete @0 (); class @1 (); void @2 (); return @3 (); } interface TestAuthenticatedBootstrap(VatId) { getCallerId @0 () -> (caller :VatId); } struct TestSturdyRef { hostId @0 :TestSturdyRefHostId; objectId @1 :AnyPointer; } struct TestSturdyRefHostId { host @0 :Text; } struct TestSturdyRefObjectId { tag @0 :Tag; enum Tag { testInterface @0; testExtends @1; testPipeline @2; testTailCallee @3; testTailCaller @4; testMoreStuff @5; } } struct TestProvisionId {} struct TestRecipientId {} struct TestThirdPartyCapId {} struct TestJoinResult {} 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"); struct NestedStruct $Cxx.name("RenamedNestedStruct") { badNestedFieldName @0 :Bool $Cxx.name("goodNestedFieldName"); anotherBadNestedFieldName @1 :NestedStruct $Cxx.name("anotherGoodNestedFieldName"); enum DeeplyNestedEnum $Cxx.name("RenamedDeeplyNestedEnum") { quux @0; corge @1; grault @2 $Cxx.name("garply"); } } badlyNamedUnion :union $Cxx.name("renamedUnion") { badlyNamedGroup :group $Cxx.name("renamedGroup") { foo @3 :Void; bar @4 :Void; } baz @5 :NestedStruct $Cxx.name("qux"); } } interface TestNameAnnotationInterface $Cxx.name("RenamedInterface") { badlyNamedMethod @0 (badlyNamedParam :UInt8 $Cxx.name("renamedParam")) $Cxx.name("renamedMethod"); } capnproto-c++-0.8.0/src/capnp/test-import.capnp0000644000175000017500000000237713340402540022156 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xf36d7b330303c66e; using Test = import "test.capnp"; struct TestImport { field @0 :Test.TestAllTypes; } capnproto-c++-0.8.0/src/capnp/test-import2.capnp0000644000175000017500000000262413340402540022233 0ustar00kentonkenton00000000000000# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors # Licensed under the MIT License: # # 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: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # 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. @0xc64a3bf0338a124a; using Import1 = import "/capnp/schema.capnp"; using Import2 = import "test-import.capnp"; using Import3 = import "test.capnp"; struct TestImport2 { foo @0 :Import3.TestAllTypes; bar @1 :Import1.Node; baz @2 :Import2.TestImport; } capnproto-c++-0.8.0/src/capnp/CMakeLists.txt0000644000175000017500000002075513650101756021415 0ustar00kentonkenton00000000000000 # capnp ======================================================================== set(capnp_sources_lite c++.capnp.c++ blob.c++ arena.c++ layout.c++ list.c++ any.c++ message.c++ schema.capnp.c++ stream.capnp.c++ serialize.c++ serialize-packed.c++ ) set(capnp_sources_heavy schema.c++ schema-loader.c++ dynamic.c++ stringify.c++ ) if(NOT CAPNP_LITE) set(capnp_sources ${capnp_sources_lite} ${capnp_sources_heavy}) else() set(capnp_sources ${capnp_sources_lite}) endif() set(capnp_headers c++.capnp.h common.h compat/std-iterator.h blob.h endian.h layout.h orphan.h list.h any.h message.h capability.h membrane.h dynamic.h schema.h schema.capnp.h stream.capnp.h schema-lite.h schema-loader.h schema-parser.h pretty-print.h serialize.h serialize-async.h serialize-packed.h serialize-text.h pointer-helpers.h generated-header-support.h raw-schema.h ) set(capnp_schemas c++.capnp schema.capnp stream.capnp ) add_library(capnp ${capnp_sources}) add_library(CapnProto::capnp ALIAS capnp) target_link_libraries(capnp PUBLIC kj) #make sure external consumers don't need to manually set the include dirs target_include_directories(capnp INTERFACE $ $ ) # Ensure the library has a version set to match autotools build set_target_properties(capnp PROPERTIES VERSION ${VERSION}) install(TARGETS capnp ${INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${capnp_headers} ${capnp_schemas} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp") set(capnp-rpc_sources serialize-async.c++ capability.c++ membrane.c++ dynamic-capability.c++ rpc.c++ rpc.capnp.c++ rpc-twoparty.c++ rpc-twoparty.capnp.c++ persistent.capnp.c++ ez-rpc.c++ ) set(capnp-rpc_headers rpc-prelude.h rpc.h rpc-twoparty.h rpc.capnp.h rpc-twoparty.capnp.h persistent.capnp.h ez-rpc.h ) set(capnp-rpc_schemas rpc.capnp rpc-twoparty.capnp persistent.capnp ) if(NOT CAPNP_LITE) add_library(capnp-rpc ${capnp-rpc_sources}) add_library(CapnProto::capnp-rpc ALIAS capnp-rpc) target_link_libraries(capnp-rpc PUBLIC capnp kj-async kj) # Ensure the library has a version set to match autotools build set_target_properties(capnp-rpc PROPERTIES VERSION ${VERSION}) install(TARGETS capnp-rpc ${INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${capnp-rpc_headers} ${capnp-rpc_schemas} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp") endif() # capnp-json ======================================================================== set(capnp-json_sources compat/json.c++ compat/json.capnp.c++ ) set(capnp-json_headers compat/json.h compat/json.capnp.h ) set(capnp-json_schemas compat/json.capnp ) if(NOT CAPNP_LITE) add_library(capnp-json ${capnp-json_sources}) add_library(CapnProto::capnp-json ALIAS capnp-json) target_link_libraries(capnp-json PUBLIC capnp kj-async kj) # Ensure the library has a version set to match autotools build set_target_properties(capnp-json PROPERTIES VERSION ${VERSION}) install(TARGETS capnp-json ${INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${capnp-json_headers} ${capnp-json_schemas} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp/compat") endif() # Tools/Compilers ============================================================== set(capnpc_sources compiler/type-id.c++ compiler/error-reporter.c++ compiler/lexer.capnp.c++ compiler/lexer.c++ compiler/grammar.capnp.c++ compiler/parser.c++ compiler/node-translator.c++ compiler/compiler.c++ schema-parser.c++ serialize-text.c++ ) if(NOT CAPNP_LITE) add_library(capnpc ${capnpc_sources}) target_link_libraries(capnpc PUBLIC capnp kj) # Ensure the library has a version set to match autotools build set_target_properties(capnpc PROPERTIES VERSION ${VERSION}) install(TARGETS capnpc ${INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${capnpc_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/capnp") endif() if(NOT CAPNP_LITE) add_executable(capnp_tool compiler/module-loader.c++ compiler/capnp.c++ ) target_link_libraries(capnp_tool capnpc capnp-json capnp kj) set_target_properties(capnp_tool PROPERTIES OUTPUT_NAME capnp) set_target_properties(capnp_tool PROPERTIES CAPNP_INCLUDE_DIRECTORY $,$> ) add_executable(capnpc_cpp compiler/capnpc-c++.c++ ) target_link_libraries(capnpc_cpp capnp kj) set_target_properties(capnpc_cpp PROPERTIES OUTPUT_NAME capnpc-c++) #Capnp tool needs capnpc_cpp location. But cmake deprecated LOCATION property. #So we use custom property to pass location set_target_properties(capnpc_cpp PROPERTIES CAPNPC_CXX_EXECUTABLE $ ) add_executable(capnpc_capnp compiler/capnpc-capnp.c++ ) target_link_libraries(capnpc_capnp capnp kj) set_target_properties(capnpc_capnp PROPERTIES OUTPUT_NAME capnpc-capnp) install(TARGETS capnp_tool capnpc_cpp capnpc_capnp ${INSTALL_TARGETS_DEFAULT_ARGS}) # Symlink capnpc -> capnp install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" -E create_symlink capnp${CMAKE_EXECUTABLE_SUFFIX} \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/capnpc${CMAKE_EXECUTABLE_SUFFIX}\")") endif() # NOT CAPNP_LITE # Tests ======================================================================== if(BUILD_TESTING) set(test_capnp_files test.capnp test-import.capnp test-import2.capnp compat/json-test.capnp ) set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/test_capnp") include_directories("${CAPNPC_OUTPUT_DIR}") file(MAKE_DIRECTORY "${CAPNPC_OUTPUT_DIR}") # Tell capnp_generate_cpp to set --src-prefix to our parent directory. This allows us to pass our # .capnp files relative to this directory, but have their canonical name end up as # capnp/test.capnp, capnp/test-import.capnp, etc. get_filename_component(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}" DIRECTORY) capnp_generate_cpp(test_capnp_cpp_files test_capnp_h_files ${test_capnp_files}) # TODO(cleanup): capnp-tests and capnp-heavy-tests both depend on the test.capnp output files. In # a parallel Makefile-based build (maybe others?), they can race and cause the custom capnp # command in capnp_generate_cpp() to run twice. To get around this I'm using a custom target to # force CMake to generate race-free Makefiles. Remove this garbage when we move to a # target-based capnp_generate() command, as that will make CMake do the right thing by default. add_custom_target(test_capnp DEPENDS ${test_capnp_cpp_files} ${test_capnp_h_files}) if(CAPNP_LITE) set(test_libraries capnp kj-test kj) else() set(test_libraries capnp-json capnp-rpc capnp capnpc kj-async kj-test kj) endif() add_executable(capnp-tests common-test.c++ blob-test.c++ endian-test.c++ endian-fallback-test.c++ layout-test.c++ any-test.c++ message-test.c++ encoding-test.c++ orphan-test.c++ serialize-test.c++ serialize-packed-test.c++ canonicalize-test.c++ fuzz-test.c++ test-util.c++ ${test_capnp_cpp_files} ${test_capnp_h_files} ) target_link_libraries(capnp-tests ${test_libraries}) add_dependencies(capnp-tests test_capnp) add_dependencies(check capnp-tests) add_test(NAME capnp-tests-run COMMAND capnp-tests) if(NOT CAPNP_LITE) add_executable(capnp-heavy-tests endian-reverse-test.c++ capability-test.c++ membrane-test.c++ schema-test.c++ schema-loader-test.c++ schema-parser-test.c++ dynamic-test.c++ stringify-test.c++ serialize-async-test.c++ serialize-text-test.c++ rpc-test.c++ rpc-twoparty-test.c++ ez-rpc-test.c++ compiler/lexer-test.c++ compiler/type-id-test.c++ test-util.c++ compat/json-test.c++ ${test_capnp_cpp_files} ${test_capnp_h_files} ) target_link_libraries(capnp-heavy-tests ${test_libraries}) if(NOT MSVC) set_target_properties(capnp-heavy-tests PROPERTIES COMPILE_FLAGS "-Wno-deprecated-declarations" ) endif() add_dependencies(capnp-heavy-tests test_capnp) add_dependencies(check capnp-heavy-tests) add_test(NAME capnp-heavy-tests-run COMMAND capnp-heavy-tests) add_executable(capnp-evolution-tests compiler/evolution-test.c++) target_link_libraries(capnp-evolution-tests capnpc capnp kj) add_dependencies(check capnp-evolution-tests) add_test(NAME capnp-evolution-tests-run COMMAND capnp-evolution-tests) endif() # NOT CAPNP_LITE endif() # BUILD_TESTING capnproto-c++-0.8.0/src/kj/0000755000175000017500000000000013650320027016141 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/kj/compat/0000755000175000017500000000000013650320027017424 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/kj/compat/gtest.h0000644000175000017500000001075113340402540020724 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // This file defines compatibility macros converting Google Test tests into KJ tests. // // This is only intended to cover the most common functionality. Many tests will likely need // additional tweaks. For instance: // - Using operator<< to print information on failure is not supported. Instead, switch to // KJ_ASSERT/KJ_EXPECT and pass in stuff to print as additional parameters. // - Test fixtures are not supported. Allocate your "test fixture" on the stack instead. Do setup // in the constructor, teardown in the destructor. #include "../test.h" #include // work-around macro conflict with `ERROR` namespace kj { namespace _ { // private template T abs(T value) { return value < 0 ? -value : value; } inline bool floatAlmostEqual(float a, float b) { return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-5; } inline bool doubleAlmostEqual(double a, double b) { return a == b || abs(a - b) < (abs(a) + abs(b)) * 1e-12; } } // namespace _ (private) #define EXPECT_FALSE(x) KJ_EXPECT(!(x)) #define EXPECT_TRUE(x) KJ_EXPECT(x) #define EXPECT_EQ(x, y) KJ_EXPECT((x) == (y), x, y) #define EXPECT_NE(x, y) KJ_EXPECT((x) != (y), x, y) #define EXPECT_LE(x, y) KJ_EXPECT((x) <= (y), x, y) #define EXPECT_GE(x, y) KJ_EXPECT((x) >= (y), x, y) #define EXPECT_LT(x, y) KJ_EXPECT((x) < (y), x, y) #define EXPECT_GT(x, y) KJ_EXPECT((x) > (y), x, y) #define EXPECT_STREQ(x, y) KJ_EXPECT(::strcmp(x, y) == 0, x, y) #define EXPECT_FLOAT_EQ(x, y) KJ_EXPECT(::kj::_::floatAlmostEqual(y, x), y, x); #define EXPECT_DOUBLE_EQ(x, y) KJ_EXPECT(::kj::_::doubleAlmostEqual(y, x), y, x); #define ASSERT_FALSE(x) KJ_ASSERT(!(x)) #define ASSERT_TRUE(x) KJ_ASSERT(x) #define ASSERT_EQ(x, y) KJ_ASSERT((x) == (y), x, y) #define ASSERT_NE(x, y) KJ_ASSERT((x) != (y), x, y) #define ASSERT_LE(x, y) KJ_ASSERT((x) <= (y), x, y) #define ASSERT_GE(x, y) KJ_ASSERT((x) >= (y), x, y) #define ASSERT_LT(x, y) KJ_ASSERT((x) < (y), x, y) #define ASSERT_GT(x, y) KJ_ASSERT((x) > (y), x, y) #define ASSERT_STREQ(x, y) KJ_ASSERT(::strcmp(x, y) == 0, x, y) #define ASSERT_FLOAT_EQ(x, y) KJ_ASSERT(::kj::_::floatAlmostEqual(y, x), y, x); #define ASSERT_DOUBLE_EQ(x, y) KJ_ASSERT(::kj::_::doubleAlmostEqual(y, x), y, x); class AddFailureAdapter { public: AddFailureAdapter(const char* file, int line): file(file), line(line) {} ~AddFailureAdapter() { if (!handled) { _::Debug::log(file, line, LogSeverity::ERROR, "expectation failed"); } } template void operator<<(T&& info) { handled = true; _::Debug::log(file, line, LogSeverity::ERROR, "\"expectation failed\", info", "expectation failed", kj::fwd(info)); } private: bool handled = false; const char* file; int line; }; #define ADD_FAILURE() ::kj::AddFailureAdapter(__FILE__, __LINE__) #if KJ_NO_EXCEPTIONS #define EXPECT_ANY_THROW(code) \ KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, nullptr, [&]() { code; })) #else #define EXPECT_ANY_THROW(code) \ KJ_EXPECT(::kj::runCatchingExceptions([&]() { code; }) != nullptr) #endif #define EXPECT_NONFATAL_FAILURE(code) \ EXPECT_TRUE(kj::runCatchingExceptions([&]() { code; }) != nullptr); #ifdef KJ_DEBUG #define EXPECT_DEBUG_ANY_THROW EXPECT_ANY_THROW #else #define EXPECT_DEBUG_ANY_THROW(EXP) #endif #define TEST(x, y) KJ_TEST("legacy test: " #x "/" #y) } // namespace kj capnproto-c++-0.8.0/src/kj/compat/url.h0000644000175000017500000001255213650101756020412 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include namespace kj { struct UrlOptions { // A bag of options that you can pass to Url::parse()/tryParse() to customize the parser's // behavior. // // A copy of this options struct will be stored in the parsed Url object, at which point it // controls the behavior of the serializer in Url::toString(). bool percentDecode = true; // True if URL components should be automatically percent-decoded during parsing, and // percent-encoded during serialization. bool allowEmpty = false; // Whether or not to allow empty path and query components when parsing; otherwise, they are // silently removed. In other words, setting this false causes consecutive slashes in the path or // consecutive ampersands in the query to be collapsed into one, whereas if true then they // produce empty components. }; struct Url { // Represents a URL (or, more accurately, a URI, but whatever). // // Can be parsed from a string and composed back into a string. String scheme; // E.g. "http", "https". struct UserInfo { String username; Maybe password; }; Maybe userInfo; // Username / password. String host; // Hostname, including port if specified. We choose not to parse out the port because KJ's // network address parsing functions already accept addresses containing port numbers, and // because most web standards don't actually want to separate host and port. Vector path; bool hasTrailingSlash = false; // Path, split on '/' characters. Note that the individual components of `path` could contain // '/' characters if they were percent-encoded in the original URL. // // No component of the path is allowed to be "", ".", nor ".."; if such components are present, // toString() will throw. Note that parse() and parseRelative() automatically resolve such // components. struct QueryParam { String name; String value; }; Vector query; // Query, e.g. from "?key=value&key2=value2". If a component of the query contains no '=' sign, // it will be parsed as a key with a null value, and later serialized with no '=' sign if you call // Url::toString(). // // To distinguish between null-valued and empty-valued query parameters, we test whether // QueryParam::value is an allocated or unallocated string. For example: // // QueryParam { kj::str("name"), nullptr } // Null-valued; will not have an '=' sign. // QueryParam { kj::str("name"), kj::str("") } // Empty-valued; WILL have an '=' sign. Maybe fragment; // The stuff after the '#' character (not including the '#' character itself), if present. using Options = UrlOptions; Options options; // --------------------------------------------------------------------------- Url() = default; Url(Url&&) = default; ~Url() noexcept(false); Url& operator=(Url&&) = default; Url clone() const; enum Context { REMOTE_HREF, // A link to a remote resource. Requires an authority (hostname) section, hence this will // reject things like "mailto:" and "data:". This is the default context. HTTP_PROXY_REQUEST, // The URL to place in the first line of an HTTP proxy request. This includes scheme, host, // path, and query, but omits userInfo (which should be used to construct the Authorization // header) and fragment (which should not be transmitted). HTTP_REQUEST // The path to place in the first line of a regular HTTP request. This includes only the path // and query. Scheme, user, host, and fragment are omitted. // TODO(someday): Add context(s) that supports things like "mailto:", "data:", "blob:". These // don't have an authority section. }; kj::String toString(Context context = REMOTE_HREF) const; // Convert the URL to a string. static Url parse(StringPtr text, Context context = REMOTE_HREF, Options options = {}); static Maybe tryParse(StringPtr text, Context context = REMOTE_HREF, Options options = {}); // Parse an absolute URL. Url parseRelative(StringPtr relative) const; Maybe tryParseRelative(StringPtr relative) const; // Parse a relative URL string with this URL as the base. }; } // namespace kj capnproto-c++-0.8.0/src/kj/compat/http.h0000644000175000017500000012512613650317313020567 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // The KJ HTTP client/server library. // // This is a simple library which can be used to implement an HTTP client or server. Properties // of this library include: // - Uses KJ async framework. // - Agnostic to transport layer -- you can provide your own. // - Header parsing is zero-copy -- it results in strings that point directly into the buffer // received off the wire. // - Application code which reads and writes headers refers to headers by symbolic names, not by // string literals, with lookups being array-index-based, not map-based. To make this possible, // the application announces what headers it cares about in advance, in order to assign numeric // values to them. // - Methods are identified by an enum. #include #include #include #include #include namespace kj { #define KJ_HTTP_FOR_EACH_METHOD(MACRO) \ MACRO(GET) \ MACRO(HEAD) \ MACRO(POST) \ MACRO(PUT) \ MACRO(DELETE) \ MACRO(PATCH) \ MACRO(PURGE) \ MACRO(OPTIONS) \ MACRO(TRACE) \ /* standard methods */ \ /* */ \ /* (CONNECT is intentionally omitted since it should be handled specially in HttpServer) */ \ \ MACRO(COPY) \ MACRO(LOCK) \ MACRO(MKCOL) \ MACRO(MOVE) \ MACRO(PROPFIND) \ MACRO(PROPPATCH) \ MACRO(SEARCH) \ MACRO(UNLOCK) \ MACRO(ACL) \ /* WebDAV */ \ \ MACRO(REPORT) \ MACRO(MKACTIVITY) \ MACRO(CHECKOUT) \ MACRO(MERGE) \ /* Subversion */ \ \ MACRO(MSEARCH) \ MACRO(NOTIFY) \ MACRO(SUBSCRIBE) \ MACRO(UNSUBSCRIBE) /* UPnP */ enum class HttpMethod { // Enum of known HTTP methods. // // We use an enum rather than a string to allow for faster parsing and switching and to reduce // ambiguity. #define DECLARE_METHOD(id) id, KJ_HTTP_FOR_EACH_METHOD(DECLARE_METHOD) #undef DECLARE_METHOD }; kj::StringPtr KJ_STRINGIFY(HttpMethod method); kj::Maybe tryParseHttpMethod(kj::StringPtr name); class HttpHeaderTable; class HttpHeaderId { // Identifies an HTTP header by numeric ID that indexes into an HttpHeaderTable. // // The KJ HTTP API prefers that headers be identified by these IDs for a few reasons: // - Integer lookups are much more efficient than string lookups. // - Case-insensitivity is awkward to deal with when const strings are being passed to the lookup // method. // - Writing out strings less often means fewer typos. // // See HttpHeaderTable for usage hints. public: HttpHeaderId() = default; inline bool operator==(const HttpHeaderId& other) const { return id == other.id; } inline bool operator!=(const HttpHeaderId& other) const { return id != other.id; } inline bool operator< (const HttpHeaderId& other) const { return id < other.id; } inline bool operator> (const HttpHeaderId& other) const { return id > other.id; } inline bool operator<=(const HttpHeaderId& other) const { return id <= other.id; } inline bool operator>=(const HttpHeaderId& other) const { return id >= other.id; } inline size_t hashCode() const { return id; } // Returned value is guaranteed to be small and never collide with other headers on the same // table. kj::StringPtr toString() const; void requireFrom(const HttpHeaderTable& table) const; // In debug mode, throws an exception if the HttpHeaderId is not from the given table. // // In opt mode, no-op. #define KJ_HTTP_FOR_EACH_BUILTIN_HEADER(MACRO) \ /* Headers that are always read-only. */ \ MACRO(CONNECTION, "Connection") \ MACRO(KEEP_ALIVE, "Keep-Alive") \ MACRO(TE, "TE") \ MACRO(TRAILER, "Trailer") \ MACRO(UPGRADE, "Upgrade") \ \ /* Headers that are read-only except in the case of a response to a HEAD request. */ \ MACRO(CONTENT_LENGTH, "Content-Length") \ MACRO(TRANSFER_ENCODING, "Transfer-Encoding") \ \ /* Headers that are read-only for WebSocket handshakes. */ \ MACRO(SEC_WEBSOCKET_KEY, "Sec-WebSocket-Key") \ MACRO(SEC_WEBSOCKET_VERSION, "Sec-WebSocket-Version") \ MACRO(SEC_WEBSOCKET_ACCEPT, "Sec-WebSocket-Accept") \ MACRO(SEC_WEBSOCKET_EXTENSIONS, "Sec-WebSocket-Extensions") \ \ /* Headers that you can write. */ \ MACRO(HOST, "Host") \ MACRO(DATE, "Date") \ MACRO(LOCATION, "Location") \ MACRO(CONTENT_TYPE, "Content-Type") // For convenience, these headers are valid for all HttpHeaderTables. You can refer to them like: // // HttpHeaderId::HOST // // TODO(someday): Fill this out with more common headers. #define DECLARE_HEADER(id, name) \ static const HttpHeaderId id; // Declare a constant for each builtin header, e.g.: HttpHeaderId::CONNECTION KJ_HTTP_FOR_EACH_BUILTIN_HEADER(DECLARE_HEADER); #undef DECLARE_HEADER private: const HttpHeaderTable* table; uint id; inline explicit constexpr HttpHeaderId(const HttpHeaderTable* table, uint id) : table(table), id(id) {} friend class HttpHeaderTable; friend class HttpHeaders; }; class HttpHeaderTable { // Construct an HttpHeaderTable to declare which headers you'll be interested in later on, and // to manufacture IDs for them. // // Example: // // // Build a header table with the headers we are interested in. // kj::HttpHeaderTable::Builder builder; // const HttpHeaderId accept = builder.add("Accept"); // const HttpHeaderId contentType = builder.add("Content-Type"); // kj::HttpHeaderTable table(kj::mv(builder)); // // // Create an HTTP client. // auto client = kj::newHttpClient(table, network); // // // Get http://example.com. // HttpHeaders headers(table); // headers.set(accept, "text/html"); // auto response = client->send(kj::HttpMethod::GET, "http://example.com", headers) // .wait(waitScope); // auto msg = kj::str("Response content type: ", response.headers.get(contentType)); struct IdsByNameMap; public: HttpHeaderTable(); // Constructs a table that only contains the builtin headers. class Builder { public: Builder(); HttpHeaderId add(kj::StringPtr name); Own build(); HttpHeaderTable& getFutureTable(); // Get the still-unbuilt header table. You cannot actually use it until build() has been // called. // // This method exists to help when building a shared header table -- the Builder may be passed // to several components, each of which will register the headers they need and get a reference // to the future table. private: kj::Own table; }; KJ_DISALLOW_COPY(HttpHeaderTable); // Can't copy because HttpHeaderId points to the table. ~HttpHeaderTable() noexcept(false); uint idCount() const; // Return the number of IDs in the table. kj::Maybe stringToId(kj::StringPtr name) const; // Try to find an ID for the given name. The matching is case-insensitive, per the HTTP spec. // // Note: if `name` contains characters that aren't allowed in HTTP header names, this may return // a bogus value rather than null, due to optimizations used in case-insensitive matching. kj::StringPtr idToString(HttpHeaderId id) const; // Get the canonical string name for the given ID. private: kj::Vector namesById; kj::Own idsByName; }; class HttpHeaders { // Represents a set of HTTP headers. // // This class guards against basic HTTP header injection attacks: Trying to set a header name or // value containing a newline, carriage return, or other invalid character will throw an // exception. public: explicit HttpHeaders(const HttpHeaderTable& table); KJ_DISALLOW_COPY(HttpHeaders); HttpHeaders(HttpHeaders&&) = default; HttpHeaders& operator=(HttpHeaders&&) = default; size_t size() const; // Returns the number of headers that forEach() would iterate over. void clear(); // Clears all contents, as if the object was freshly-allocated. However, calling this rather // than actually re-allocating the object may avoid re-allocation of internal objects. HttpHeaders clone() const; // Creates a deep clone of the HttpHeaders. The returned object owns all strings it references. HttpHeaders cloneShallow() const; // Creates a shallow clone of the HttpHeaders. The returned object references the same strings // as the original, owning none of them. bool isWebSocket() const; // Convenience method that checks for the presence of the header `Upgrade: websocket`. // // Note that this does not actually validate that the request is a complete WebSocket handshake // with the correct version number -- such validation will occur if and when you call // acceptWebSocket(). kj::Maybe get(HttpHeaderId id) const; // Read a header. template void forEach(Func&& func) const; // Calls `func(name, value)` for each header in the set -- including headers that aren't mapped // to IDs in the header table. Both inputs are of type kj::StringPtr. template void forEach(Func1&& func1, Func2&& func2) const; // Calls `func1(id, value)` for each header in the set that has a registered HttpHeaderId, and // `func2(name, value)` for each header that does not. All calls to func1() precede all calls to // func2(). void set(HttpHeaderId id, kj::StringPtr value); void set(HttpHeaderId id, kj::String&& value); // Sets a header value, overwriting the existing value. // // The String&& version is equivalent to calling the other version followed by takeOwnership(). // // WARNING: It is the caller's responsibility to ensure that `value` remains valid until the // HttpHeaders object is destroyed. This allows string literals to be passed without making a // copy, but complicates the use of dynamic values. Hint: Consider using `takeOwnership()`. void add(kj::StringPtr name, kj::StringPtr value); void add(kj::StringPtr name, kj::String&& value); void add(kj::String&& name, kj::String&& value); // Append a header. `name` will be looked up in the header table, but if it's not mapped, the // header will be added to the list of unmapped headers. // // The String&& versions are equivalent to calling the other version followed by takeOwnership(). // // WARNING: It is the caller's responsibility to ensure that `name` and `value` remain valid // until the HttpHeaders object is destroyed. This allows string literals to be passed without // making a copy, but complicates the use of dynamic values. Hint: Consider using // `takeOwnership()`. void unset(HttpHeaderId id); // Removes a header. // // It's not possible to remove a header by string name because non-indexed headers would take // O(n) time to remove. Instead, construct a new HttpHeaders object and copy contents. void takeOwnership(kj::String&& string); void takeOwnership(kj::Array&& chars); void takeOwnership(HttpHeaders&& otherHeaders); // Takes overship of a string so that it lives until the HttpHeaders object is destroyed. Useful // when you've passed a dynamic value to set() or add() or parse*(). struct Request { HttpMethod method; kj::StringPtr url; }; struct Response { uint statusCode; kj::StringPtr statusText; }; struct ProtocolError { // Represents a protocol error, such as a bad request method or invalid headers. Debugging such // errors is difficult without a copy of the data which we tried to parse, but this data is // sensitive, so we can't just lump it into the error description directly. ProtocolError // provides this sensitive data separate from the error description. // // TODO(cleanup): Should maybe not live in HttpHeaders? HttpServerErrorHandler::ProtocolError? // Or HttpProtocolError? Or maybe we need a more general way of attaching sensitive context to // kj::Exceptions? uint statusCode; // Suggested HTTP status code that should be used when returning an error to the client. // // Most errors are 400. An unrecognized method will be 501. kj::StringPtr statusMessage; // HTTP status message to go with `statusCode`, e.g. "Bad Request". kj::StringPtr description; // An error description safe for all the world to see. kj::ArrayPtr rawContent; // Unredacted data which led to the error condition. This may contain anything transported over // HTTP, to include sensitive PII, so you must take care to sanitize this before using it in any // error report that may leak to unprivileged eyes. // // This ArrayPtr is merely a copy of the `content` parameter passed to `tryParseRequest()` / // `tryParseResponse()`, thus it remains valid for as long as a successfully-parsed HttpHeaders // object would remain valid. }; using RequestOrProtocolError = kj::OneOf; using ResponseOrProtocolError = kj::OneOf; RequestOrProtocolError tryParseRequest(kj::ArrayPtr content); ResponseOrProtocolError tryParseResponse(kj::ArrayPtr content); // Parse an HTTP header blob and add all the headers to this object. // // `content` should be all text from the start of the request to the first occurrance of two // newlines in a row -- including the first of these two newlines, but excluding the second. // // The parse is performed with zero copies: The callee clobbers `content` with '\0' characters // to split it into a bunch of shorter strings. The caller must keep `content` valid until the // `HttpHeaders` is destroyed, or pass it to `takeOwnership()`. bool tryParse(kj::ArrayPtr content); // Like tryParseRequest()/tryParseResponse(), but don't expect any request/response line. kj::String serializeRequest(HttpMethod method, kj::StringPtr url, kj::ArrayPtr connectionHeaders = nullptr) const; kj::String serializeResponse(uint statusCode, kj::StringPtr statusText, kj::ArrayPtr connectionHeaders = nullptr) const; // **Most applications will not use these methods; they are called by the HTTP client and server // implementations.** // // Serialize the headers as a complete request or response blob. The blob uses '\r\n' newlines // and includes the double-newline to indicate the end of the headers. // // `connectionHeaders`, if provided, contains connection-level headers supplied by the HTTP // implementation, in the order specified by the KJ_HTTP_FOR_EACH_BUILTIN_HEADER macro. These // headers values override any corresponding header value in the HttpHeaders object. The // CONNECTION_HEADERS_COUNT constants below can help you construct this `connectionHeaders` array. enum class BuiltinIndicesEnum { #define HEADER_ID(id, name) id, KJ_HTTP_FOR_EACH_BUILTIN_HEADER(HEADER_ID) #undef HEADER_ID }; struct BuiltinIndices { #define HEADER_ID(id, name) static constexpr uint id = static_cast(BuiltinIndicesEnum::id); KJ_HTTP_FOR_EACH_BUILTIN_HEADER(HEADER_ID) #undef HEADER_ID }; static constexpr uint HEAD_RESPONSE_CONNECTION_HEADERS_COUNT = BuiltinIndices::CONTENT_LENGTH; static constexpr uint CONNECTION_HEADERS_COUNT = BuiltinIndices::SEC_WEBSOCKET_KEY; static constexpr uint WEBSOCKET_CONNECTION_HEADERS_COUNT = BuiltinIndices::HOST; // Constants for use with HttpHeaders::serialize*(). kj::String toString() const; private: const HttpHeaderTable* table; kj::Array indexedHeaders; // Size is always table->idCount(). struct Header { kj::StringPtr name; kj::StringPtr value; }; kj::Vector
unindexedHeaders; kj::Vector> ownedStrings; void addNoCheck(kj::StringPtr name, kj::StringPtr value); kj::StringPtr cloneToOwn(kj::StringPtr str); kj::String serialize(kj::ArrayPtr word1, kj::ArrayPtr word2, kj::ArrayPtr word3, kj::ArrayPtr connectionHeaders) const; bool parseHeaders(char* ptr, char* end); // TODO(perf): Arguably we should store a map, but header sets are never very long // TODO(perf): We could optimize for common headers by storing them directly as fields. We could // also add direct accessors for those headers. }; class HttpInputStream { // Low-level interface to receive HTTP-formatted messages (headers followed by body) from an // input stream, without a paired output stream. // // Most applications will not use this. Regular HTTP clients and servers don't need this. This // is mainly useful for apps implementing various protocols that look like HTTP but aren't // really. public: struct Request { HttpMethod method; kj::StringPtr url; const HttpHeaders& headers; kj::Own body; }; virtual kj::Promise readRequest() = 0; // Reads one HTTP request from the input stream. // // The returned struct contains pointers directly into a buffer that is invalidated on the next // message read. struct Response { uint statusCode; kj::StringPtr statusText; const HttpHeaders& headers; kj::Own body; }; virtual kj::Promise readResponse(HttpMethod requestMethod) = 0; // Reads one HTTP response from the input stream. // // You must provide the request method because responses to HEAD requests require special // treatment. // // The returned struct contains pointers directly into a buffer that is invalidated on the next // message read. struct Message { const HttpHeaders& headers; kj::Own body; }; virtual kj::Promise readMessage() = 0; // Reads an HTTP header set followed by a body, with no request or response line. This is not // useful for HTTP but may be useful for other protocols that make the unfortunate choice to // mimic HTTP message format, such as Visual Studio Code's JSON-RPC transport. // // The returned struct contains pointers directly into a buffer that is invalidated on the next // message read. virtual kj::Promise awaitNextMessage() = 0; // Waits until more data is available, but doesn't consume it. Returns false on EOF. }; class EntropySource { // Interface for an object that generates entropy. Typically, cryptographically-random entropy // is expected. // // TODO(cleanup): Put this somewhere more general. public: virtual void generate(kj::ArrayPtr buffer) = 0; }; class WebSocket { // Interface representincg an open WebSocket session. // // Each side can send and receive data and "close" messages. // // Ping/Pong and message fragmentation are not exposed through this interface. These features of // the underlying WebSocket protocol are not exposed by the browser-level JavaScript API either, // and thus applications typically need to implement these features at the application protocol // level instead. The implementation is, however, expected to reply to Ping messages it receives. public: virtual kj::Promise send(kj::ArrayPtr message) = 0; virtual kj::Promise send(kj::ArrayPtr message) = 0; // Send a message (binary or text). The underlying buffer must remain valid, and you must not // call send() again, until the returned promise resolves. virtual kj::Promise close(uint16_t code, kj::StringPtr reason) = 0; // Send a Close message. // // Note that the returned Promise resolves once the message has been sent -- it does NOT wait // for the other end to send a Close reply. The application should await a reply before dropping // the WebSocket object. virtual kj::Promise disconnect() = 0; // Sends EOF on the underlying connection without sending a "close" message. This is NOT a clean // shutdown, but is sometimes useful when you want the other end to trigger whatever behavior // it normally triggers when a connection is dropped. virtual void abort() = 0; // Forcefully close this WebSocket, such that the remote end should get a DISCONNECTED error if // it continues to write. This differs from disconnect(), which only closes the sending // direction, but still allows receives. virtual kj::Promise whenAborted() = 0; // Resolves when the remote side aborts the connection such that send() would throw DISCONNECTED, // if this can be detected without actually writing a message. (If not, this promise never // resolves, but send() or receive() will throw DISCONNECTED when appropriate. See also // kj::AsyncOutputStream::whenWriteDisconnected().) struct Close { uint16_t code; kj::String reason; }; typedef kj::OneOf, Close> Message; virtual kj::Promise receive() = 0; // Read one message from the WebSocket and return it. Can only call once at a time. Do not call // again after Close is received. virtual kj::Promise pumpTo(WebSocket& other); // Continuously receives messages from this WebSocket and send them to `other`. // // On EOF, calls other.disconnect(), then resolves. // // On other read errors, calls other.close() with the error, then resolves. // // On write error, rejects with the error. virtual kj::Maybe> tryPumpFrom(WebSocket& other); // Either returns null, or performs the equivalent of other.pumpTo(*this). Only returns non-null // if this WebSocket implementation is able to perform the pump in an optimized way, better than // the default implementation of pumpTo(). The default implementation of pumpTo() always tries // calling this first, and the default implementation of tryPumpFrom() always returns null. }; class HttpClient { // Interface to the client end of an HTTP connection. // // There are two kinds of clients: // * Host clients are used when talking to a specific host. The `url` specified in a request // is actually just a path. (A `Host` header is still required in all requests.) // * Proxy clients are used when the target could be any arbitrary host on the internet. // The `url` specified in a request is a full URL including protocol and hostname. public: struct Response { uint statusCode; kj::StringPtr statusText; const HttpHeaders* headers; kj::Own body; // `statusText` and `headers` remain valid until `body` is dropped or read from. }; struct Request { kj::Own body; // Write the request entity body to this stream, then drop it when done. // // May be null for GET and HEAD requests (which have no body) and requests that have // Content-Length: 0. kj::Promise response; // Promise for the eventual respnose. }; virtual Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) = 0; // Perform an HTTP request. // // `url` may be a full URL (with protocol and host) or it may be only the path part of the URL, // depending on whether the client is a proxy client or a host client. // // `url` and `headers` need only remain valid until `request()` returns (they can be // stack-allocated). // // `expectedBodySize`, if provided, must be exactly the number of bytes that will be written to // the body. This will trigger use of the `Content-Length` connection header. Otherwise, // `Transfer-Encoding: chunked` will be used. struct WebSocketResponse { uint statusCode; kj::StringPtr statusText; const HttpHeaders* headers; kj::OneOf, kj::Own> webSocketOrBody; // `statusText` and `headers` remain valid until `webSocketOrBody` is dropped or read from. }; virtual kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers); // Tries to open a WebSocket. Default implementation calls send() and never returns a WebSocket. // // `url` and `headers` need only remain valid until `openWebSocket()` returns (they can be // stack-allocated). virtual kj::Promise> connect(kj::StringPtr host); // Handles CONNECT requests. Only relevant for proxy clients. Default implementation throws // UNIMPLEMENTED. }; class HttpService { // Interface which HTTP services should implement. // // This interface is functionally equivalent to HttpClient, but is intended for applications to // implement rather than call. The ergonomics and performance of the method signatures are // optimized for the serving end. // // As with clients, there are two kinds of services: // * Host services are used when talking to a specific host. The `url` specified in a request // is actually just a path. (A `Host` header is still required in all requests, and the service // may in fact serve multiple origins via this header.) // * Proxy services are used when the target could be any arbitrary host on the internet, i.e. to // implement an HTTP proxy. The `url` specified in a request is a full URL including protocol // and hostname. public: class Response { public: virtual kj::Own send( uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) = 0; // Begin the response. // // `statusText` and `headers` need only remain valid until send() returns (they can be // stack-allocated). virtual kj::Own acceptWebSocket(const HttpHeaders& headers) = 0; // If headers.isWebSocket() is true then you can call acceptWebSocket() instead of send(). kj::Promise sendError(uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers); kj::Promise sendError(uint statusCode, kj::StringPtr statusText, const HttpHeaderTable& headerTable); // Convenience wrapper around send() which sends a basic error. A generic error page specifying // the error code is sent as the body. // // You must provide headers or a header table because downstream service wrappers may be // expecting response headers built with a particular table so that they can insert additional // headers. }; virtual kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) = 0; // Perform an HTTP request. // // `url` may be a full URL (with protocol and host) or it may be only the path part of the URL, // depending on whether the service is a proxy service or a host service. // // `url` and `headers` are invalidated on the first read from `requestBody` or when the returned // promise resolves, whichever comes first. // // Request processing can be canceled by dropping the returned promise. HttpServer may do so if // the client disconnects prematurely. virtual kj::Promise> connect(kj::StringPtr host); // Handles CONNECT requests. Only relevant for proxy services. Default implementation throws // UNIMPLEMENTED. }; struct HttpClientSettings { kj::Duration idleTimeout = 5 * kj::SECONDS; // For clients which automatically create new connections, any connection idle for at least this // long will be closed. Set this to 0 to prevent connection reuse entirely. kj::Maybe entropySource = nullptr; // Must be provided in order to use `openWebSocket`. If you don't need WebSockets, this can be // omitted. The WebSocket protocol uses random values to avoid triggering flaws (including // security flaws) in certain HTTP proxy software. Specifically, entropy is used to generate the // `Sec-WebSocket-Key` header and to generate frame masks. If you know that there are no broken // or vulnerable proxies between you and the server, you can provide a dummy entropy source that // doesn't generate real entropy (e.g. returning the same value every time). Otherwise, you must // provide a cryptographically-random entropy source. }; kj::Own newHttpClient(kj::Timer& timer, HttpHeaderTable& responseHeaderTable, kj::Network& network, kj::Maybe tlsNetwork, HttpClientSettings settings = HttpClientSettings()); // Creates a proxy HttpClient that connects to hosts over the given network. The URL must always // be an absolute URL; the host is parsed from the URL. This implementation will automatically // add an appropriate Host header (and convert the URL to just a path) once it has connected. // // Note that if you wish to route traffic through an HTTP proxy server rather than connect to // remote hosts directly, you should use the form of newHttpClient() that takes a NetworkAddress, // and supply the proxy's address. // // `responseHeaderTable` is used when parsing HTTP responses. Requests can use any header table. // // `tlsNetwork` is required to support HTTPS destination URLs. If null, only HTTP URLs can be // fetched. kj::Own newHttpClient(kj::Timer& timer, HttpHeaderTable& responseHeaderTable, kj::NetworkAddress& addr, HttpClientSettings settings = HttpClientSettings()); // Creates an HttpClient that always connects to the given address no matter what URL is requested. // The client will open and close connections as needed. It will attempt to reuse connections for // multiple requests but will not send a new request before the previous response on the same // connection has completed, as doing so can result in head-of-line blocking issues. The client may // be used as a proxy client or a host client depending on whether the peer is operating as // a proxy. (Hint: This is the best kind of client to use when routing traffic through an HTTP // proxy. `addr` should be the address of the proxy, and the proxy itself will resolve remote hosts // based on the URLs passed to it.) // // `responseHeaderTable` is used when parsing HTTP responses. Requests can use any header table. kj::Own newHttpClient(HttpHeaderTable& responseHeaderTable, kj::AsyncIoStream& stream, HttpClientSettings settings = HttpClientSettings()); // Creates an HttpClient that speaks over the given pre-established connection. The client may // be used as a proxy client or a host client depending on whether the peer is operating as // a proxy. // // Note that since this client has only one stream to work with, it will try to pipeline all // requests on this stream. If one request or response has an I/O failure, all subsequent requests // fail as well. If the destination server chooses to close the connection after a response, // subsequent requests will fail. If a response takes a long time, it blocks subsequent responses. // If a WebSocket is opened successfully, all subsequent requests fail. kj::Own newConcurrencyLimitingHttpClient( HttpClient& inner, uint maxConcurrentRequests, kj::Function countChangedCallback); // Creates an HttpClient that is limited to a maximum number of concurrent requests. Additional // requests are queued, to be opened only after an open request completes. `countChangedCallback` // is called when a new connection is opened or enqueued and when an open connection is closed, // passing the number of open and pending connections. kj::Own newHttpClient(HttpService& service); kj::Own newHttpService(HttpClient& client); // Adapts an HttpClient to an HttpService and vice versa. kj::Own newHttpInputStream( kj::AsyncInputStream& input, HttpHeaderTable& headerTable); // Create an HttpInputStream on top of the given stream. Normally applications would not call this // directly, but it can be useful for implementing protocols that aren't quite HTTP but use similar // message delimiting. // // The HttpInputStream implementation does read-ahead buffering on `input`. Therefore, when the // HttpInputStream is destroyed, some data read from `input` may be lost, so it's not possible to // continue reading from `input` in a reliable way. kj::Own newWebSocket(kj::Own stream, kj::Maybe maskEntropySource); // Create a new WebSocket on top of the given stream. It is assumed that the HTTP -> WebSocket // upgrade handshake has already occurred (or is not needed), and messages can immediately be // sent and received on the stream. Normally applications would not call this directly. // // `maskEntropySource` is used to generate cryptographically-random frame masks. If null, outgoing // frames will not be masked. Servers are required NOT to mask their outgoing frames, but clients // ARE required to do so. So, on the client side, you MUST specify an entropy source. The mask // must be crytographically random if the data being sent on the WebSocket may be malicious. The // purpose of the mask is to prevent badly-written HTTP proxies from interpreting "things that look // like HTTP requests" in a message as being actual HTTP requests, which could result in cache // poisoning. See RFC6455 section 10.3. struct WebSocketPipe { kj::Own ends[2]; }; WebSocketPipe newWebSocketPipe(); // Create a WebSocket pipe. Messages written to one end of the pipe will be readable from the other // end. No buffering occurs -- a message send does not complete until a corresponding receive // accepts the message. class HttpServerErrorHandler; struct HttpServerSettings { kj::Duration headerTimeout = 15 * kj::SECONDS; // After initial connection open, or after receiving the first byte of a pipelined request, // the client must send the complete request within this time. kj::Duration pipelineTimeout = 5 * kj::SECONDS; // After one request/response completes, we'll wait up to this long for a pipelined request to // arrive. kj::Duration canceledUploadGracePeriod = 1 * kj::SECONDS; size_t canceledUploadGraceBytes = 65536; // If the HttpService sends a response and returns without having read the entire request body, // then we have to decide whether to close the connection or wait for the client to finish the // request so that it can pipeline the next one. We'll give them a grace period defined by the // above two values -- if they hit either one, we'll close the socket, but if the request // completes, we'll let the connection stay open to handle more requests. kj::Maybe errorHandler = nullptr; // Customize how client protocol errors and service application exceptions are handled by the // HttpServer. If null, HttpServerErrorHandler's default implementation will be used. }; class HttpServerErrorHandler { public: virtual kj::Promise handleClientProtocolError( HttpHeaders::ProtocolError protocolError, kj::HttpService::Response& response); virtual kj::Promise handleApplicationError( kj::Exception exception, kj::Maybe response); virtual kj::Promise handleNoResponse(kj::HttpService::Response& response); // Override these functions to customize error handling during the request/response cycle. // // Client protocol errors arise when the server receives an HTTP message that fails to parse. As // such, HttpService::request() will not have been called yet, and the handler is always // guaranteed an opportunity to send a response. The default implementation of // handleClientProtocolError() replies with a 400 Bad Request response. // // Application errors arise when HttpService::request() throws an exception. The default // implementation of handleApplicationError() maps the following exception types to HTTP statuses, // and generates bodies from the stringified exceptions: // // - OVERLOADED: 503 Service Unavailable // - UNIMPLEMENTED: 501 Not Implemented // - DISCONNECTED: (no response) // - FAILED: 500 Internal Server Error // // No-response errors occur when HttpService::request() allows its promise to settle before // sending a response. The default implementation of handleNoResponse() replies with a 500 // Internal Server Error response. // // Unlike `HttpService::request()`, when calling `response.send()` in the context of one of these // functions, a "Connection: close" header will be added, and the connection will be closed. // // Also unlike `HttpService::request()`, it is okay to return kj::READY_NOW without calling // `response.send()`. In this case, no response will be sent, and the connection will be closed. }; class HttpServer final: private kj::TaskSet::ErrorHandler { // Class which listens for requests on ports or connections and sends them to an HttpService. public: typedef HttpServerSettings Settings; typedef kj::Function(kj::AsyncIoStream&)> HttpServiceFactory; HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, HttpService& service, Settings settings = Settings()); // Set up an HttpServer that directs incoming connections to the given service. The service // may be a host service or a proxy service depending on whether you are intending to implement // an HTTP server or an HTTP proxy. HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, HttpServiceFactory serviceFactory, Settings settings = Settings()); // Like the other constructor, but allows a new HttpService object to be used for each // connection, based on the connection object. This is particularly useful for capturing the // client's IP address and injecting it as a header. kj::Promise drain(); // Stop accepting new connections or new requests on existing connections. Finish any requests // that are already executing, then close the connections. Returns once no more requests are // in-flight. kj::Promise listenHttp(kj::ConnectionReceiver& port); // Accepts HTTP connections on the given port and directs them to the handler. // // The returned promise never completes normally. It may throw if port.accept() throws. Dropping // the returned promise will cause the server to stop listening on the port, but already-open // connections will continue to be served. Destroy the whole HttpServer to cancel all I/O. kj::Promise listenHttp(kj::Own connection); // Reads HTTP requests from the given connection and directs them to the handler. A successful // completion of the promise indicates that all requests received on the connection resulted in // a complete response, and the client closed the connection gracefully or drain() was called. // The promise throws if an unparseable request is received or if some I/O error occurs. Dropping // the returned promise will cancel all I/O on the connection and cancel any in-flight requests. kj::Promise listenHttpCleanDrain(kj::AsyncIoStream& connection); // Like listenHttp(), but allows you to potentially drain the server without closing connections. // The returned promise resolves to `true` if the connection has been left in a state where a // new HttpServer could potentially accept further requests from it. If `false`, then the // connection is either in an inconsistent state or already completed a closing handshake; the // caller should close it without any further reads/writes. Note this only ever returns `true` // if you called `drain()` -- otherwise this server would keep handling the connection. private: class Connection; kj::Timer& timer; HttpHeaderTable& requestHeaderTable; kj::OneOf service; Settings settings; bool draining = false; kj::ForkedPromise onDrain; kj::Own> drainFulfiller; uint connectionCount = 0; kj::Maybe>> zeroConnectionsFulfiller; kj::TaskSet tasks; HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, kj::OneOf service, Settings settings, kj::PromiseFulfillerPair paf); kj::Promise listenLoop(kj::ConnectionReceiver& port); void taskFailed(kj::Exception&& exception) override; }; // ======================================================================================= // inline implementation inline void HttpHeaderId::requireFrom(const HttpHeaderTable& table) const { KJ_IREQUIRE(this->table == nullptr || this->table == &table, "the provided HttpHeaderId is from the wrong HttpHeaderTable"); } inline kj::Own HttpHeaderTable::Builder::build() { return kj::mv(table); } inline HttpHeaderTable& HttpHeaderTable::Builder::getFutureTable() { return *table; } inline uint HttpHeaderTable::idCount() const { return namesById.size(); } inline kj::StringPtr HttpHeaderTable::idToString(HttpHeaderId id) const { id.requireFrom(*this); return namesById[id.id]; } inline kj::Maybe HttpHeaders::get(HttpHeaderId id) const { id.requireFrom(*table); auto result = indexedHeaders[id.id]; return result == nullptr ? kj::Maybe(nullptr) : result; } inline void HttpHeaders::unset(HttpHeaderId id) { id.requireFrom(*table); indexedHeaders[id.id] = nullptr; } template inline void HttpHeaders::forEach(Func&& func) const { for (auto i: kj::indices(indexedHeaders)) { if (indexedHeaders[i] != nullptr) { func(table->idToString(HttpHeaderId(table, i)), indexedHeaders[i]); } } for (auto& header: unindexedHeaders) { func(header.name, header.value); } } template inline void HttpHeaders::forEach(Func1&& func1, Func2&& func2) const { for (auto i: kj::indices(indexedHeaders)) { if (indexedHeaders[i] != nullptr) { func1(HttpHeaderId(table, i), indexedHeaders[i]); } } for (auto& header: unindexedHeaders) { func2(header.name, header.value); } } } // namespace kj capnproto-c++-0.8.0/src/kj/compat/gzip.h0000644000175000017500000001016313650101756020555 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include #include namespace kj { namespace _ { // private class GzipOutputContext final { public: GzipOutputContext(kj::Maybe compressionLevel); ~GzipOutputContext() noexcept(false); KJ_DISALLOW_COPY(GzipOutputContext); void setInput(const void* in, size_t size); kj::Tuple> pumpOnce(int flush); private: bool compressing; z_stream ctx = {}; byte buffer[4096]; void fail(int result); }; } // namespace _ (private) class GzipInputStream final: public InputStream { public: GzipInputStream(InputStream& inner); ~GzipInputStream() noexcept(false); KJ_DISALLOW_COPY(GzipInputStream); size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; private: InputStream& inner; z_stream ctx = {}; bool atValidEndpoint = false; byte buffer[4096]; size_t readImpl(byte* buffer, size_t minBytes, size_t maxBytes, size_t alreadyRead); }; class GzipOutputStream final: public OutputStream { public: enum { DECOMPRESS }; GzipOutputStream(OutputStream& inner, int compressionLevel = Z_DEFAULT_COMPRESSION); GzipOutputStream(OutputStream& inner, decltype(DECOMPRESS)); ~GzipOutputStream() noexcept(false); KJ_DISALLOW_COPY(GzipOutputStream); void write(const void* buffer, size_t size) override; using OutputStream::write; inline void flush() { pump(Z_SYNC_FLUSH); } private: OutputStream& inner; _::GzipOutputContext ctx; void pump(int flush); }; class GzipAsyncInputStream final: public AsyncInputStream { public: GzipAsyncInputStream(AsyncInputStream& inner); ~GzipAsyncInputStream() noexcept(false); KJ_DISALLOW_COPY(GzipAsyncInputStream); Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; private: AsyncInputStream& inner; z_stream ctx = {}; bool atValidEndpoint = false; byte buffer[4096]; Promise readImpl(byte* buffer, size_t minBytes, size_t maxBytes, size_t alreadyRead); }; class GzipAsyncOutputStream final: public AsyncOutputStream { public: enum { DECOMPRESS }; GzipAsyncOutputStream(AsyncOutputStream& inner, int compressionLevel = Z_DEFAULT_COMPRESSION); GzipAsyncOutputStream(AsyncOutputStream& inner, decltype(DECOMPRESS)); KJ_DISALLOW_COPY(GzipAsyncOutputStream); Promise write(const void* buffer, size_t size) override; Promise write(ArrayPtr> pieces) override; Promise whenWriteDisconnected() override { return inner.whenWriteDisconnected(); } inline Promise flush() { return pump(Z_SYNC_FLUSH); } // Call if you need to flush a stream at an arbitrary data point. Promise end() { return pump(Z_FINISH); } // Must call to flush and finish the stream, since some data may be buffered. // // TODO(cleanup): This should be a virtual method on AsyncOutputStream. private: AsyncOutputStream& inner; _::GzipOutputContext ctx; kj::Promise pump(int flush); }; } // namespace kj capnproto-c++-0.8.0/src/kj/compat/readiness-io.h0000644000175000017500000000642413650101756022173 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include namespace kj { class ReadyInputStreamWrapper { // Provides readiness-based Async I/O as a wrapper around KJ's standard completion-based API, for // compatibility with libraries that use readiness-based abstractions (e.g. OpenSSL). // // Unfortunately this requires buffering, so is not very efficient. public: ReadyInputStreamWrapper(AsyncInputStream& input); ~ReadyInputStreamWrapper() noexcept(false); KJ_DISALLOW_COPY(ReadyInputStreamWrapper); kj::Maybe read(kj::ArrayPtr dst); // Reads bytes into `dst`, returning the number of bytes read. Returns zero only at EOF. Returns // nullptr if not ready. kj::Promise whenReady(); // Returns a promise that resolves when read() will return non-null. private: AsyncInputStream& input; kj::ForkedPromise pumpTask = nullptr; bool isPumping = false; bool eof = false; kj::ArrayPtr content = nullptr; // Points to currently-valid part of `buffer`. byte buffer[8192]; }; class ReadyOutputStreamWrapper { // Provides readiness-based Async I/O as a wrapper around KJ's standard completion-based API, for // compatibility with libraries that use readiness-based abstractions (e.g. OpenSSL). // // Unfortunately this requires buffering, so is not very efficient. public: ReadyOutputStreamWrapper(AsyncOutputStream& output); ~ReadyOutputStreamWrapper() noexcept(false); KJ_DISALLOW_COPY(ReadyOutputStreamWrapper); kj::Maybe write(kj::ArrayPtr src); // Writes bytes from `src`, returning the number of bytes written. Never returns zero. Returns // nullptr if not ready. kj::Promise whenReady(); // Returns a promise that resolves when write() will return non-null. private: AsyncOutputStream& output; ArrayPtr segments[2]; kj::ForkedPromise pumpTask = nullptr; bool isPumping = false; uint start = 0; // index of first byte uint filled = 0; // number of bytes currently in buffer byte buffer[8192]; kj::Promise pump(); // Asynchronously push the buffer out to the underlying stream. }; } // namespace kj capnproto-c++-0.8.0/src/kj/compat/tls.h0000644000175000017500000002276713340402540020412 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // This file implements TLS (aka SSL) encrypted networking. It is actually a wrapper, currently // around OpenSSL / BoringSSL / LibreSSL, but the interface is intended to remain // implementation-agnostic. // // Unlike OpenSSL's API, the API defined in this file is intended to be hard to use wrong. Good // ciphers and settings are used by default. Certificates validation is performed automatically // and cannot be bypassed. #include namespace kj { class TlsPrivateKey; class TlsCertificate; struct TlsKeypair; class TlsSniCallback; enum class TlsVersion { SSL_3, // avoid; cryptographically broken TLS_1_0, TLS_1_1, TLS_1_2 }; class TlsContext { // TLS system. Allocate one of these, configure it with the proper keys and certificates (or // use the defaults), and then use it to wrap the standard KJ network interfaces in // implementations that transparently use TLS. public: struct Options { Options(); // Initializes all values to reasonable defaults. bool useSystemTrustStore; // Whether or not to trust the system's default trust store. Default: true. bool verifyClients; // If true, when acting as a server, require the client to present a certificate. The // certificate must be signed by one of the trusted CAs, otherwise the client will be rejected. // (Typically you should set `useSystemTrustStore` false when using this flag, and specify // your specific trusted CAs in `trustedCertificates`.) // Default: false kj::ArrayPtr trustedCertificates; // Additional certificates which should be trusted. Default: none. TlsVersion minVersion; // Minimum version. Defaults to minimum version that hasn't been cryptographically broken. // If you override this, consider doing: // // options.minVersion = kj::max(myVersion, options.minVersion); kj::StringPtr cipherList; // OpenSSL cipher list string. The default is a curated list designed to be compatible with // almost all software in curent use (specifically, based on Mozilla's "intermediate" // recommendations). The defaults will change in future versions of this library to account // for the latest cryptanalysis. // // Generally you should only specify your own `cipherList` if: // - You have extreme backwards-compatibility needs and wish to enable obsolete and/or broken // algorithms. // - You need quickly to disable an algorithm recently discovered to be broken. kj::Maybe defaultKeypair; // Default keypair to use for all connections. Required for servers; optional for clients. kj::Maybe sniCallback; // Callback that can be used to choose a different key/certificate based on the specific // hostname requested by the client. }; TlsContext(Options options = Options()); ~TlsContext() noexcept(false); KJ_DISALLOW_COPY(TlsContext); kj::Promise> wrapServer(kj::Own stream); // Upgrade a regular network stream to TLS and begin the initial handshake as the server. The // returned promise resolves when the handshake has completed successfully. kj::Promise> wrapClient( kj::Own stream, kj::StringPtr expectedServerHostname); // Upgrade a regular network stream to TLS and begin the initial handshake as a client. The // returned promise resolves when the handshake has completed successfully, including validating // the server's certificate. // // You must specify the server's hostname. This is used for two purposes: // 1. It is sent to the server in the initial handshake via the TLS SNI extension, so that a // server serving multiple hosts knows which certificate to use. // 2. The server's certificate is validated against this hostname. If validation fails, the // promise returned by wrapClient() will be broken; you'll never get a stream. kj::Own wrapPort(kj::Own port); // Upgrade a ConnectionReceiver to one that automatically upgrades all accepted connections to // TLS (acting as the server). kj::Own wrapNetwork(kj::Network& network); // Upgrade a Network to one that automatically upgrades all connections to TLS. The network will // only accept addresses of the form "hostname" and "hostname:port" (it does not accept raw IP // addresses). It will automatically use SNI and verify certificates based on these hostnames. private: void* ctx; // actually type SSL_CTX, but we don't want to #include the OpenSSL headers here struct SniCallback; }; class TlsPrivateKey { // A private key suitable for use in a TLS server. public: TlsPrivateKey(kj::ArrayPtr asn1); // Parse a single binary (ASN1) private key. Supports PKCS8 keys as well as "traditional format" // RSA and DSA keys. Does not accept encrypted keys; it is the caller's responsibility to // decrypt. TlsPrivateKey(kj::StringPtr pem, kj::Maybe password = nullptr); // Parse a single PEM-encoded private key. Supports PKCS8 keys as well as "traditional format" // RSA and DSA keys. A password may optionally be provided and will be used if the key is // encrypted. ~TlsPrivateKey() noexcept(false); TlsPrivateKey(const TlsPrivateKey& other); TlsPrivateKey& operator=(const TlsPrivateKey& other); // Copy-by-refcount. inline TlsPrivateKey(TlsPrivateKey&& other): pkey(other.pkey) { other.pkey = nullptr; } inline TlsPrivateKey& operator=(TlsPrivateKey&& other) { pkey = other.pkey; other.pkey = nullptr; return *this; } private: void* pkey; // actually type EVP_PKEY* friend class TlsContext; static int passwordCallback(char* buf, int size, int rwflag, void* u); }; class TlsCertificate { // A TLS certificate, possibly with chained intermediate certificates. public: TlsCertificate(kj::ArrayPtr asn1); // Parse a single binary (ASN1) X509 certificate. TlsCertificate(kj::ArrayPtr> asn1); // Parse a chain of binary (ASN1) X509 certificates. TlsCertificate(kj::StringPtr pem); // Parse a PEM-encode X509 certificate or certificate chain. A chain can be constructed by // concatenating multiple PEM-encoded certificates, starting with the leaf certificate. ~TlsCertificate() noexcept(false); TlsCertificate(const TlsCertificate& other); TlsCertificate& operator=(const TlsCertificate& other); // Copy-by-refcount. inline TlsCertificate(TlsCertificate&& other) { memcpy(chain, other.chain, sizeof(chain)); memset(other.chain, 0, sizeof(chain)); } inline TlsCertificate& operator=(TlsCertificate&& other) { memcpy(chain, other.chain, sizeof(chain)); memset(other.chain, 0, sizeof(chain)); return *this; } private: void* chain[10]; // Actually type X509*[10]. // // Note that OpenSSL has a default maximum cert chain length of 10. Although configurable at // runtime, you'd actually have to convince the _peer_ to reconfigure, which is unlikely except // in specific use cases. So to avoid excess allocations we just assume a max of 10 certs. // // If this proves to be a problem, we should maybe use STACK_OF(X509) here, but stacks are not // refcounted -- the X509_chain_up_ref() function actually allocates a new stack and uprefs all // the certs. friend class TlsContext; }; struct TlsKeypair { // A pair of a private key and a certificate, for use by a server. TlsPrivateKey privateKey; TlsCertificate certificate; }; class TlsSniCallback { // Callback object to implement Server Name Indication, in which the server is able to decide // what key and certificate to use based on the hostname that the client is requesting. // // TODO(someday): Currently this callback is synchronous, because the OpenSSL API seems to be // synchronous. Other people (e.g. Node) have figured out how to do it asynchronously, but // it's unclear to me if and how this is possible while using the OpenSSL APIs. It looks like // Node may be manually parsing the ClientHello message rather than relying on OpenSSL. We // could do that but it's too much work for today. public: virtual kj::Maybe getKey(kj::StringPtr hostname) = 0; // Get the key to use for `hostname`. Null return means use the default from // TlsContext::Options::defaultKeypair. }; } // namespace kj capnproto-c++-0.8.0/src/kj/compat/gzip.c++0000644000175000017500000002057513340402540020675 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if KJ_HAS_ZLIB #include "gzip.h" #include namespace kj { namespace _ { // private GzipOutputContext::GzipOutputContext(kj::Maybe compressionLevel) { int initResult; KJ_IF_MAYBE(level, compressionLevel) { compressing = true; initResult = deflateInit2(&ctx, *level, Z_DEFLATED, 15 + 16, // windowBits = 15 (maximum) + magic value 16 to ask for gzip. 8, // memLevel = 8 (the default) Z_DEFAULT_STRATEGY); } else { compressing = false; initResult = inflateInit2(&ctx, 15 + 16); } if (initResult != Z_OK) { fail(initResult); } } GzipOutputContext::~GzipOutputContext() noexcept(false) { compressing ? deflateEnd(&ctx) : inflateEnd(&ctx); } void GzipOutputContext::setInput(const void* in, size_t size) { ctx.next_in = const_cast(reinterpret_cast(in)); ctx.avail_in = size; } kj::Tuple> GzipOutputContext::pumpOnce(int flush) { ctx.next_out = buffer; ctx.avail_out = sizeof(buffer); auto result = compressing ? deflate(&ctx, flush) : inflate(&ctx, flush); if (result != Z_OK && result != Z_BUF_ERROR && result != Z_STREAM_END) { fail(result); } // - Z_STREAM_END means we have finished the stream successfully. // - Z_BUF_ERROR means we didn't have any more input to process // (but still have to make a call to write to potentially flush data). return kj::tuple(result == Z_OK, kj::arrayPtr(buffer, sizeof(buffer) - ctx.avail_out)); } void GzipOutputContext::fail(int result) { auto header = compressing ? "gzip compression failed" : "gzip decompression failed"; if (ctx.msg == nullptr) { KJ_FAIL_REQUIRE(header, result); } else { KJ_FAIL_REQUIRE(header, ctx.msg); } } } // namespace _ (private) GzipInputStream::GzipInputStream(InputStream& inner) : inner(inner) { // windowBits = 15 (maximum) + magic value 16 to ask for gzip. KJ_ASSERT(inflateInit2(&ctx, 15 + 16) == Z_OK); } GzipInputStream::~GzipInputStream() noexcept(false) { inflateEnd(&ctx); } size_t GzipInputStream::tryRead(void* out, size_t minBytes, size_t maxBytes) { if (maxBytes == 0) return size_t(0); return readImpl(reinterpret_cast(out), minBytes, maxBytes, 0); } size_t GzipInputStream::readImpl( byte* out, size_t minBytes, size_t maxBytes, size_t alreadyRead) { if (ctx.avail_in == 0) { size_t amount = inner.tryRead(buffer, 1, sizeof(buffer)); if (amount == 0) { if (!atValidEndpoint) { KJ_FAIL_REQUIRE("gzip compressed stream ended prematurely"); } return alreadyRead; } else { ctx.next_in = buffer; ctx.avail_in = amount; } } ctx.next_out = reinterpret_cast(out); ctx.avail_out = maxBytes; auto inflateResult = inflate(&ctx, Z_NO_FLUSH); atValidEndpoint = inflateResult == Z_STREAM_END; if (inflateResult == Z_OK || inflateResult == Z_STREAM_END) { if (atValidEndpoint && ctx.avail_in > 0) { // There's more data available. Assume start of new content. KJ_ASSERT(inflateReset(&ctx) == Z_OK); } size_t n = maxBytes - ctx.avail_out; if (n >= minBytes) { return n + alreadyRead; } else { return readImpl(out + n, minBytes - n, maxBytes - n, alreadyRead + n); } } else { if (ctx.msg == nullptr) { KJ_FAIL_REQUIRE("gzip decompression failed", inflateResult); } else { KJ_FAIL_REQUIRE("gzip decompression failed", ctx.msg); } } } // ======================================================================================= GzipOutputStream::GzipOutputStream(OutputStream& inner, int compressionLevel) : inner(inner), ctx(compressionLevel) {} GzipOutputStream::GzipOutputStream(OutputStream& inner, decltype(DECOMPRESS)) : inner(inner), ctx(nullptr) {} GzipOutputStream::~GzipOutputStream() noexcept(false) { pump(Z_FINISH); } void GzipOutputStream::write(const void* in, size_t size) { ctx.setInput(in, size); pump(Z_NO_FLUSH); } void GzipOutputStream::pump(int flush) { bool ok; do { auto result = ctx.pumpOnce(flush); ok = get<0>(result); auto chunk = get<1>(result); inner.write(chunk.begin(), chunk.size()); } while (ok); } // ======================================================================================= GzipAsyncInputStream::GzipAsyncInputStream(AsyncInputStream& inner) : inner(inner) { // windowBits = 15 (maximum) + magic value 16 to ask for gzip. KJ_ASSERT(inflateInit2(&ctx, 15 + 16) == Z_OK); } GzipAsyncInputStream::~GzipAsyncInputStream() noexcept(false) { inflateEnd(&ctx); } Promise GzipAsyncInputStream::tryRead(void* out, size_t minBytes, size_t maxBytes) { if (maxBytes == 0) return size_t(0); return readImpl(reinterpret_cast(out), minBytes, maxBytes, 0); } Promise GzipAsyncInputStream::readImpl( byte* out, size_t minBytes, size_t maxBytes, size_t alreadyRead) { if (ctx.avail_in == 0) { return inner.tryRead(buffer, 1, sizeof(buffer)) .then([this,out,minBytes,maxBytes,alreadyRead](size_t amount) -> Promise { if (amount == 0) { if (!atValidEndpoint) { return KJ_EXCEPTION(DISCONNECTED, "gzip compressed stream ended prematurely"); } return alreadyRead; } else { ctx.next_in = buffer; ctx.avail_in = amount; return readImpl(out, minBytes, maxBytes, alreadyRead); } }); } ctx.next_out = reinterpret_cast(out); ctx.avail_out = maxBytes; auto inflateResult = inflate(&ctx, Z_NO_FLUSH); atValidEndpoint = inflateResult == Z_STREAM_END; if (inflateResult == Z_OK || inflateResult == Z_STREAM_END) { if (atValidEndpoint && ctx.avail_in > 0) { // There's more data available. Assume start of new content. KJ_ASSERT(inflateReset(&ctx) == Z_OK); } size_t n = maxBytes - ctx.avail_out; if (n >= minBytes) { return n + alreadyRead; } else { return readImpl(out + n, minBytes - n, maxBytes - n, alreadyRead + n); } } else { if (ctx.msg == nullptr) { KJ_FAIL_REQUIRE("gzip decompression failed", inflateResult); } else { KJ_FAIL_REQUIRE("gzip decompression failed", ctx.msg); } } } // ======================================================================================= GzipAsyncOutputStream::GzipAsyncOutputStream(AsyncOutputStream& inner, int compressionLevel) : inner(inner), ctx(compressionLevel) {} GzipAsyncOutputStream::GzipAsyncOutputStream(AsyncOutputStream& inner, decltype(DECOMPRESS)) : inner(inner), ctx(nullptr) {} Promise GzipAsyncOutputStream::write(const void* in, size_t size) { ctx.setInput(in, size); return pump(Z_NO_FLUSH); } Promise GzipAsyncOutputStream::write(ArrayPtr> pieces) { if (pieces.size() == 0) return kj::READY_NOW; return write(pieces[0].begin(), pieces[0].size()) .then([this,pieces]() { return write(pieces.slice(1, pieces.size())); }); } kj::Promise GzipAsyncOutputStream::pump(int flush) { auto result = ctx.pumpOnce(flush); auto ok = get<0>(result); auto chunk = get<1>(result); auto promise = inner.write(chunk.begin(), chunk.size()); if (ok) { promise = promise.then([this, flush]() { return pump(flush); }); } return promise; } } // namespace kj #endif // KJ_HAS_ZLIB capnproto-c++-0.8.0/src/kj/compat/url.c++0000644000175000017500000003702713650101756020537 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "url.h" #include #include #include #include namespace kj { namespace { constexpr auto ALPHAS = parse::charRange('a', 'z').orRange('A', 'Z'); constexpr auto DIGITS = parse::charRange('0', '9'); constexpr auto END_AUTHORITY = parse::anyOfChars("/?#"); // Authority, path, and query components can typically be terminated by the start of a fragment. // However, fragments are disallowed in HTTP_REQUEST and HTTP_PROXY_REQUEST contexts. As a quirk, we // allow the fragment start character ('#') to live unescaped in path and query components. We do // not currently allow it in the authority component, because our parser would reject it as a host // character anyway. const parse::CharGroup_& getEndPathPart(Url::Context context) { static constexpr auto END_PATH_PART_HREF = parse::anyOfChars("/?#"); static constexpr auto END_PATH_PART_REQUEST = parse::anyOfChars("/?"); switch (context) { case Url::REMOTE_HREF: return END_PATH_PART_HREF; case Url::HTTP_PROXY_REQUEST: return END_PATH_PART_REQUEST; case Url::HTTP_REQUEST: return END_PATH_PART_REQUEST; } KJ_UNREACHABLE; } const parse::CharGroup_& getEndQueryPart(Url::Context context) { static constexpr auto END_QUERY_PART_HREF = parse::anyOfChars("&#"); static constexpr auto END_QUERY_PART_REQUEST = parse::anyOfChars("&"); switch (context) { case Url::REMOTE_HREF: return END_QUERY_PART_HREF; case Url::HTTP_PROXY_REQUEST: return END_QUERY_PART_REQUEST; case Url::HTTP_REQUEST: return END_QUERY_PART_REQUEST; } KJ_UNREACHABLE; } constexpr auto SCHEME_CHARS = ALPHAS.orGroup(DIGITS).orAny("+-."); constexpr auto NOT_SCHEME_CHARS = SCHEME_CHARS.invert(); constexpr auto HOST_CHARS = ALPHAS.orGroup(DIGITS).orAny(".-:[]_"); // [] is for ipv6 literals. // _ is not allowed in domain names, but the WHATWG URL spec allows it in hostnames, so we do, too. // TODO(someday): The URL spec actually allows a lot more than just '_', and requires nameprepping // to Punycode. We'll have to decide how we want to deal with all that. void toLower(String& text) { for (char& c: text) { if ('A' <= c && c <= 'Z') { c += 'a' - 'A'; } } } Maybe> trySplit(StringPtr& text, char c) { KJ_IF_MAYBE(pos, text.findFirst(c)) { ArrayPtr result = text.slice(0, *pos); text = text.slice(*pos + 1); return result; } else { return nullptr; } } Maybe> trySplit(ArrayPtr& text, char c) { for (auto i: kj::indices(text)) { if (text[i] == c) { ArrayPtr result = text.slice(0, i); text = text.slice(i + 1, text.size()); return result; } } return nullptr; } ArrayPtr split(StringPtr& text, const parse::CharGroup_& chars) { for (auto i: kj::indices(text)) { if (chars.contains(text[i])) { ArrayPtr result = text.slice(0, i); text = text.slice(i); return result; } } auto result = text.asArray(); text = ""; return result; } String percentDecode(ArrayPtr text, bool& hadErrors, const Url::Options& options) { if (options.percentDecode) { auto result = decodeUriComponent(text); if (result.hadErrors) hadErrors = true; return kj::mv(result); } return kj::str(text); } String percentDecodeQuery(ArrayPtr text, bool& hadErrors, const Url::Options& options) { if (options.percentDecode) { auto result = decodeWwwForm(text); if (result.hadErrors) hadErrors = true; return kj::mv(result); } return kj::str(text); } } // namespace Url::~Url() noexcept(false) {} Url Url::clone() const { return { kj::str(scheme), userInfo.map([](const UserInfo& ui) -> UserInfo { return { kj::str(ui.username), ui.password.map([](const String& s) { return kj::str(s); }) }; }), kj::str(host), KJ_MAP(part, path) { return kj::str(part); }, hasTrailingSlash, KJ_MAP(param, query) -> QueryParam { // Preserve the "allocated-ness" of `param.value` with this careful copy. return { kj::str(param.name), param.value.begin() == nullptr ? kj::String() : kj::str(param.value) }; }, fragment.map([](const String& s) { return kj::str(s); }), options }; } Url Url::parse(StringPtr url, Context context, Options options) { return KJ_REQUIRE_NONNULL(tryParse(url, context, options), "invalid URL", url); } Maybe Url::tryParse(StringPtr text, Context context, Options options) { Url result; result.options = options; bool err = false; // tracks percent-decoding errors auto& END_PATH_PART = getEndPathPart(context); auto& END_QUERY_PART = getEndQueryPart(context); if (context == HTTP_REQUEST) { if (!text.startsWith("/")) { return nullptr; } } else { KJ_IF_MAYBE(scheme, trySplit(text, ':')) { result.scheme = kj::str(*scheme); } else { // missing scheme return nullptr; } toLower(result.scheme); if (result.scheme.size() == 0 || !ALPHAS.contains(result.scheme[0]) || !SCHEME_CHARS.containsAll(result.scheme.slice(1))) { // bad scheme return nullptr; } if (!text.startsWith("//")) { // We require an authority (hostname) part. return nullptr; } text = text.slice(2); { auto authority = split(text, END_AUTHORITY); KJ_IF_MAYBE(userpass, trySplit(authority, '@')) { if (context != REMOTE_HREF) { // No user/pass allowed here. return nullptr; } KJ_IF_MAYBE(username, trySplit(*userpass, ':')) { result.userInfo = UserInfo { percentDecode(*username, err, options), percentDecode(*userpass, err, options) }; } else { result.userInfo = UserInfo { percentDecode(*userpass, err, options), nullptr }; } } result.host = percentDecode(authority, err, options); if (!HOST_CHARS.containsAll(result.host)) return nullptr; toLower(result.host); } } while (text.startsWith("/")) { text = text.slice(1); auto part = split(text, END_PATH_PART); if (part.size() == 2 && part[0] == '.' && part[1] == '.') { if (result.path.size() != 0) { result.path.removeLast(); } result.hasTrailingSlash = true; } else if ((part.size() == 0 && (!options.allowEmpty || text.size() == 0)) || (part.size() == 1 && part[0] == '.')) { // Collapse consecutive slashes and "/./". result.hasTrailingSlash = true; } else { result.path.add(percentDecode(part, err, options)); result.hasTrailingSlash = false; } } if (text.startsWith("?")) { do { text = text.slice(1); auto part = split(text, END_QUERY_PART); if (part.size() > 0 || options.allowEmpty) { KJ_IF_MAYBE(key, trySplit(part, '=')) { result.query.add(QueryParam { percentDecodeQuery(*key, err, options), percentDecodeQuery(part, err, options) }); } else { result.query.add(QueryParam { percentDecodeQuery(part, err, options), nullptr }); } } } while (text.startsWith("&")); } if (text.startsWith("#")) { if (context != REMOTE_HREF) { // No fragment allowed here. return nullptr; } result.fragment = percentDecode(text.slice(1), err, options); } else { // We should have consumed everything. KJ_ASSERT(text.size() == 0); } if (err) return nullptr; return kj::mv(result); } Url Url::parseRelative(StringPtr url) const { return KJ_REQUIRE_NONNULL(tryParseRelative(url), "invalid relative URL", url); } Maybe Url::tryParseRelative(StringPtr text) const { if (text.size() == 0) return clone(); Url result; result.options = options; bool err = false; // tracks percent-decoding errors auto& END_PATH_PART = getEndPathPart(Url::REMOTE_HREF); auto& END_QUERY_PART = getEndQueryPart(Url::REMOTE_HREF); // scheme { bool gotScheme = false; for (auto i: kj::indices(text)) { if (text[i] == ':') { // found valid scheme result.scheme = kj::str(text.slice(0, i)); text = text.slice(i + 1); gotScheme = true; break; } else if (NOT_SCHEME_CHARS.contains(text[i])) { // no scheme break; } } if (!gotScheme) { // copy scheme result.scheme = kj::str(this->scheme); } } // authority bool hadNewAuthority = text.startsWith("//"); if (hadNewAuthority) { text = text.slice(2); auto authority = split(text, END_AUTHORITY); KJ_IF_MAYBE(userpass, trySplit(authority, '@')) { KJ_IF_MAYBE(username, trySplit(*userpass, ':')) { result.userInfo = UserInfo { percentDecode(*username, err, options), percentDecode(*userpass, err, options) }; } else { result.userInfo = UserInfo { percentDecode(*userpass, err, options), nullptr }; } } result.host = percentDecode(authority, err, options); if (!HOST_CHARS.containsAll(result.host)) return nullptr; toLower(result.host); } else { // copy authority result.host = kj::str(this->host); result.userInfo = this->userInfo.map([](const UserInfo& userInfo) { return UserInfo { kj::str(userInfo.username), userInfo.password.map([](const String& password) { return kj::str(password); }), }; }); } // path bool hadNewPath = text.size() > 0 && text[0] != '?' && text[0] != '#'; if (hadNewPath) { // There's a new path. if (text[0] == '/') { // New path is absolute, so don't copy the old path. text = text.slice(1); result.hasTrailingSlash = true; } else if (this->path.size() > 0) { // New path is relative, so start from the old path, dropping everything after the last // slash. auto slice = this->path.slice(0, this->path.size() - (this->hasTrailingSlash ? 0 : 1)); result.path = KJ_MAP(part, slice) { return kj::str(part); }; result.hasTrailingSlash = true; } for (;;) { auto part = split(text, END_PATH_PART); if (part.size() == 2 && part[0] == '.' && part[1] == '.') { if (result.path.size() != 0) { result.path.removeLast(); } result.hasTrailingSlash = true; } else if (part.size() == 0 || (part.size() == 1 && part[0] == '.')) { // Collapse consecutive slashes and "/./". result.hasTrailingSlash = true; } else { result.path.add(percentDecode(part, err, options)); result.hasTrailingSlash = false; } if (!text.startsWith("/")) break; text = text.slice(1); } } else if (!hadNewAuthority) { // copy path result.path = KJ_MAP(part, this->path) { return kj::str(part); }; result.hasTrailingSlash = this->hasTrailingSlash; } if (text.startsWith("?")) { do { text = text.slice(1); auto part = split(text, END_QUERY_PART); if (part.size() > 0) { KJ_IF_MAYBE(key, trySplit(part, '=')) { result.query.add(QueryParam { percentDecodeQuery(*key, err, options), percentDecodeQuery(part, err, options) }); } else { result.query.add(QueryParam { percentDecodeQuery(part, err, options), nullptr }); } } } while (text.startsWith("&")); } else if (!hadNewAuthority && !hadNewPath) { // copy query result.query = KJ_MAP(param, this->query) -> QueryParam { // Preserve the "allocated-ness" of `param.value` with this careful copy. return { kj::str(param.name), param.value.begin() == nullptr ? kj::String() : kj::str(param.value) }; }; } if (text.startsWith("#")) { result.fragment = percentDecode(text.slice(1), err, options); } else { // We should have consumed everything. KJ_ASSERT(text.size() == 0); } if (err) return nullptr; return kj::mv(result); } String Url::toString(Context context) const { Vector chars(128); if (context != HTTP_REQUEST) { chars.addAll(scheme); chars.addAll(StringPtr("://")); if (context == REMOTE_HREF) { KJ_IF_MAYBE(user, userInfo) { chars.addAll(options.percentDecode ? encodeUriUserInfo(user->username) : kj::str(user->username)); KJ_IF_MAYBE(pass, user->password) { chars.add(':'); chars.addAll(options.percentDecode ? encodeUriUserInfo(*pass) : kj::str(*pass)); } chars.add('@'); } } // RFC3986 specifies that hosts can contain percent-encoding escapes while suggesting that // they should only be used for UTF-8 sequences. However, the DNS standard specifies a // different way to encode Unicode into domain names and doesn't permit any characters which // would need to be escaped. Meanwhile, encodeUriComponent() here would incorrectly try to // escape colons and brackets (e.g. around ipv6 literal addresses). So, instead, we throw if // the host is invalid. if (HOST_CHARS.containsAll(host)) { chars.addAll(host); } else { KJ_FAIL_REQUIRE("invalid hostname when stringifying URL", host) { chars.addAll(StringPtr("invalid-host")); break; } } } for (auto& pathPart: path) { // Protect against path injection. KJ_REQUIRE((pathPart != "" || options.allowEmpty) && pathPart != "." && pathPart != "..", "invalid name in URL path", path) { continue; } chars.add('/'); chars.addAll(options.percentDecode ? encodeUriPath(pathPart) : kj::str(pathPart)); } if (hasTrailingSlash || (path.size() == 0 && context == HTTP_REQUEST)) { chars.add('/'); } bool first = true; for (auto& param: query) { chars.add(first ? '?' : '&'); first = false; chars.addAll(options.percentDecode ? encodeWwwForm(param.name) : kj::str(param.name)); if (param.value.begin() != nullptr) { chars.add('='); chars.addAll(options.percentDecode ? encodeWwwForm(param.value) : kj::str(param.value)); } } if (context == REMOTE_HREF) { KJ_IF_MAYBE(f, fragment) { chars.add('#'); chars.addAll(options.percentDecode ? encodeUriFragment(*f) : kj::str(*f)); } } chars.add('\0'); return String(chars.releaseAsArray()); } } // namespace kj capnproto-c++-0.8.0/src/kj/compat/http.c++0000644000175000017500000054065213650317313020715 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "http.h" #include "url.h" #include #include #include #include #include #include #include #include namespace kj { // ======================================================================================= // SHA-1 implementation from https://github.com/clibs/sha1 // // The WebSocket standard depends on SHA-1. ARRRGGGHHHHH. // // Any old checksum would have served the purpose, or hell, even just returning the header // verbatim. But NO, they decided to throw a whole complicated hash algorithm in there, AND // THEY CHOSE A BROKEN ONE THAT WE OTHERWISE WOULDN'T NEED ANYMORE. // // TODO(cleanup): Move this to a shared hashing library. Maybe. Or maybe don't, because no one // should be using SHA-1 anymore. // // THIS USAGE IS NOT SECURITY SENSITIVE. IF YOU REPORT A SECURITY ISSUE BECAUSE YOU SAW SHA1 IN THE // SOURCE CODE I WILL MAKE FUN OF YOU. /* SHA-1 in C By Steve Reid 100% Public Domain Test Vectors (from FIPS PUB 180-1) "abc" A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 A million repetitions of "a" 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */ /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */ /* #define SHA1HANDSOFF * Copies data before messing with it. */ #define SHA1HANDSOFF typedef struct { uint32_t state[5]; uint32_t count[2]; unsigned char buffer[64]; } SHA1_CTX; #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) /* blk0() and blk() perform the initial expand. */ /* I got the idea of expanding during the round function from SSLeay */ #if BYTE_ORDER == LITTLE_ENDIAN #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |(rol(block->l[i],8)&0x00FF00FF)) #elif BYTE_ORDER == BIG_ENDIAN #define blk0(i) block->l[i] #else #error "Endianness not defined!" #endif #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ ^block->l[(i+2)&15]^block->l[i&15],1)) /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); /* Hash a single 512-bit block. This is the core of the algorithm. */ void SHA1Transform( uint32_t state[5], const unsigned char buffer[64] ) { uint32_t a, b, c, d, e; typedef union { unsigned char c[64]; uint32_t l[16]; } CHAR64LONG16; #ifdef SHA1HANDSOFF CHAR64LONG16 block[1]; /* use array to appear as a pointer */ memcpy(block, buffer, 64); #else /* The following had better never be used because it causes the * pointer-to-const buffer to be cast into a pointer to non-const. * And the result is written through. I threw a "const" in, hoping * this will cause a diagnostic. */ CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer; #endif /* Copy context->state[] to working vars */ a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; /* 4 rounds of 20 operations each. Loop unrolled. */ R0(a, b, c, d, e, 0); R0(e, a, b, c, d, 1); R0(d, e, a, b, c, 2); R0(c, d, e, a, b, 3); R0(b, c, d, e, a, 4); R0(a, b, c, d, e, 5); R0(e, a, b, c, d, 6); R0(d, e, a, b, c, 7); R0(c, d, e, a, b, 8); R0(b, c, d, e, a, 9); R0(a, b, c, d, e, 10); R0(e, a, b, c, d, 11); R0(d, e, a, b, c, 12); R0(c, d, e, a, b, 13); R0(b, c, d, e, a, 14); R0(a, b, c, d, e, 15); R1(e, a, b, c, d, 16); R1(d, e, a, b, c, 17); R1(c, d, e, a, b, 18); R1(b, c, d, e, a, 19); R2(a, b, c, d, e, 20); R2(e, a, b, c, d, 21); R2(d, e, a, b, c, 22); R2(c, d, e, a, b, 23); R2(b, c, d, e, a, 24); R2(a, b, c, d, e, 25); R2(e, a, b, c, d, 26); R2(d, e, a, b, c, 27); R2(c, d, e, a, b, 28); R2(b, c, d, e, a, 29); R2(a, b, c, d, e, 30); R2(e, a, b, c, d, 31); R2(d, e, a, b, c, 32); R2(c, d, e, a, b, 33); R2(b, c, d, e, a, 34); R2(a, b, c, d, e, 35); R2(e, a, b, c, d, 36); R2(d, e, a, b, c, 37); R2(c, d, e, a, b, 38); R2(b, c, d, e, a, 39); R3(a, b, c, d, e, 40); R3(e, a, b, c, d, 41); R3(d, e, a, b, c, 42); R3(c, d, e, a, b, 43); R3(b, c, d, e, a, 44); R3(a, b, c, d, e, 45); R3(e, a, b, c, d, 46); R3(d, e, a, b, c, 47); R3(c, d, e, a, b, 48); R3(b, c, d, e, a, 49); R3(a, b, c, d, e, 50); R3(e, a, b, c, d, 51); R3(d, e, a, b, c, 52); R3(c, d, e, a, b, 53); R3(b, c, d, e, a, 54); R3(a, b, c, d, e, 55); R3(e, a, b, c, d, 56); R3(d, e, a, b, c, 57); R3(c, d, e, a, b, 58); R3(b, c, d, e, a, 59); R4(a, b, c, d, e, 60); R4(e, a, b, c, d, 61); R4(d, e, a, b, c, 62); R4(c, d, e, a, b, 63); R4(b, c, d, e, a, 64); R4(a, b, c, d, e, 65); R4(e, a, b, c, d, 66); R4(d, e, a, b, c, 67); R4(c, d, e, a, b, 68); R4(b, c, d, e, a, 69); R4(a, b, c, d, e, 70); R4(e, a, b, c, d, 71); R4(d, e, a, b, c, 72); R4(c, d, e, a, b, 73); R4(b, c, d, e, a, 74); R4(a, b, c, d, e, 75); R4(e, a, b, c, d, 76); R4(d, e, a, b, c, 77); R4(c, d, e, a, b, 78); R4(b, c, d, e, a, 79); /* Add the working vars back into context.state[] */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; /* Wipe variables */ a = b = c = d = e = 0; #ifdef SHA1HANDSOFF memset(block, '\0', sizeof(block)); #endif } /* SHA1Init - Initialize new context */ void SHA1Init( SHA1_CTX * context ) { /* SHA1 initialization constants */ context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; context->state[2] = 0x98BADCFE; context->state[3] = 0x10325476; context->state[4] = 0xC3D2E1F0; context->count[0] = context->count[1] = 0; } /* Run your data through this. */ void SHA1Update( SHA1_CTX * context, const unsigned char *data, uint32_t len ) { uint32_t i; uint32_t j; j = context->count[0]; if ((context->count[0] += len << 3) < j) context->count[1]++; context->count[1] += (len >> 29); j = (j >> 3) & 63; if ((j + len) > 63) { memcpy(&context->buffer[j], data, (i = 64 - j)); SHA1Transform(context->state, context->buffer); for (; i + 63 < len; i += 64) { SHA1Transform(context->state, &data[i]); } j = 0; } else i = 0; memcpy(&context->buffer[j], &data[i], len - i); } /* Add padding and return the message digest. */ void SHA1Final( unsigned char digest[20], SHA1_CTX * context ) { unsigned i; unsigned char finalcount[8]; unsigned char c; #if 0 /* untested "improvement" by DHR */ /* Convert context->count to a sequence of bytes * in finalcount. Second element first, but * big-endian order within element. * But we do it all backwards. */ unsigned char *fcp = &finalcount[8]; for (i = 0; i < 2; i++) { uint32_t t = context->count[i]; int j; for (j = 0; j < 4; t >>= 8, j++) *--fcp = (unsigned char) t} #else for (i = 0; i < 8; i++) { finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ } #endif c = 0200; SHA1Update(context, &c, 1); while ((context->count[0] & 504) != 448) { c = 0000; SHA1Update(context, &c, 1); } SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ for (i = 0; i < 20; i++) { digest[i] = (unsigned char) ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); } /* Wipe variables */ memset(context, '\0', sizeof(*context)); memset(&finalcount, '\0', sizeof(finalcount)); } // End SHA-1 implementation. // ======================================================================================= static const char* METHOD_NAMES[] = { #define METHOD_NAME(id) #id, KJ_HTTP_FOR_EACH_METHOD(METHOD_NAME) #undef METHOD_NAME }; kj::StringPtr KJ_STRINGIFY(HttpMethod method) { return METHOD_NAMES[static_cast(method)]; } static kj::Maybe consumeHttpMethod(char*& ptr) { char* p = ptr; #define EXPECT_REST(prefix, suffix) \ if (strncmp(p, #suffix, sizeof(#suffix)-1) == 0) { \ ptr = p + (sizeof(#suffix)-1); \ return HttpMethod::prefix##suffix; \ } else { \ return nullptr; \ } switch (*p++) { case 'A': EXPECT_REST(A,CL) case 'C': switch (*p++) { case 'H': EXPECT_REST(CH,ECKOUT) case 'O': EXPECT_REST(CO,PY) default: return nullptr; } case 'D': EXPECT_REST(D,ELETE) case 'G': EXPECT_REST(G,ET) case 'H': EXPECT_REST(H,EAD) case 'L': EXPECT_REST(L,OCK) case 'M': switch (*p++) { case 'E': EXPECT_REST(ME,RGE) case 'K': switch (*p++) { case 'A': EXPECT_REST(MKA,CTIVITY) case 'C': EXPECT_REST(MKC,OL) default: return nullptr; } case 'O': EXPECT_REST(MO,VE) case 'S': EXPECT_REST(MS,EARCH) default: return nullptr; } case 'N': EXPECT_REST(N,OTIFY) case 'O': EXPECT_REST(O,PTIONS) case 'P': switch (*p++) { case 'A': EXPECT_REST(PA,TCH) case 'O': EXPECT_REST(PO,ST) case 'R': if (*p++ != 'O' || *p++ != 'P') return nullptr; switch (*p++) { case 'F': EXPECT_REST(PROPF,IND) case 'P': EXPECT_REST(PROPP,ATCH) default: return nullptr; } case 'U': switch (*p++) { case 'R': EXPECT_REST(PUR,GE) case 'T': EXPECT_REST(PUT,) default: return nullptr; } default: return nullptr; } case 'R': EXPECT_REST(R,EPORT) case 'S': switch (*p++) { case 'E': EXPECT_REST(SE,ARCH) case 'U': EXPECT_REST(SU,BSCRIBE) default: return nullptr; } case 'T': EXPECT_REST(T,RACE) case 'U': if (*p++ != 'N') return nullptr; switch (*p++) { case 'L': EXPECT_REST(UNL,OCK) case 'S': EXPECT_REST(UNS,UBSCRIBE) default: return nullptr; } default: return nullptr; } #undef EXPECT_REST } kj::Maybe tryParseHttpMethod(kj::StringPtr name) { // const_cast OK because we don't actually access it. consumeHttpMethod() is also called by some // code later than explicitly needs to use a non-const pointer. char* ptr = const_cast(name.begin()); auto result = consumeHttpMethod(ptr); if (*ptr == '\0') { return result; } else { return nullptr; } } // ======================================================================================= namespace { constexpr char WEBSOCKET_GUID[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; // From RFC6455. static kj::String generateWebSocketAccept(kj::StringPtr key) { // WebSocket demands we do a SHA-1 here. ARRGHH WHY SHA-1 WHYYYYYY? SHA1_CTX ctx; byte digest[20]; SHA1Init(&ctx); SHA1Update(&ctx, key.asBytes().begin(), key.size()); SHA1Update(&ctx, reinterpret_cast(WEBSOCKET_GUID), strlen(WEBSOCKET_GUID)); SHA1Final(digest, &ctx); return kj::encodeBase64(digest); } constexpr auto HTTP_SEPARATOR_CHARS = kj::parse::anyOfChars("()<>@,;:\\\"/[]?={} \t"); // RFC2616 section 2.2: https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2 constexpr auto HTTP_TOKEN_CHARS = kj::parse::controlChar.orChar('\x7f') .orGroup(kj::parse::whitespaceChar) .orGroup(HTTP_SEPARATOR_CHARS) .invert(); // RFC2616 section 2.2: https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2 constexpr auto HTTP_HEADER_NAME_CHARS = HTTP_TOKEN_CHARS; // RFC2616 section 4.2: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 static void requireValidHeaderName(kj::StringPtr name) { for (char c: name) { KJ_REQUIRE(HTTP_HEADER_NAME_CHARS.contains(c), "invalid header name", name); } } static void requireValidHeaderValue(kj::StringPtr value) { for (char c: value) { // While the HTTP spec suggests that only printable ASCII characters are allowed in header // values, reality has a different opinion. See: https://github.com/httpwg/http11bis/issues/19 // We follow the browsers' lead. KJ_REQUIRE(c != '\0' && c != '\r' && c != '\n', "invalid header value", kj::encodeCEscape(value)); } } static const char* BUILTIN_HEADER_NAMES[] = { // Indexed by header ID, which includes connection headers, so we include those names too. #define HEADER_NAME(id, name) name, KJ_HTTP_FOR_EACH_BUILTIN_HEADER(HEADER_NAME) #undef HEADER_NAME }; } // namespace #define HEADER_ID(id, name) constexpr uint HttpHeaders::BuiltinIndices::id; KJ_HTTP_FOR_EACH_BUILTIN_HEADER(HEADER_ID) #undef HEADER_ID #define DEFINE_HEADER(id, name) \ const HttpHeaderId HttpHeaderId::id(nullptr, HttpHeaders::BuiltinIndices::id); KJ_HTTP_FOR_EACH_BUILTIN_HEADER(DEFINE_HEADER) #undef DEFINE_HEADER kj::StringPtr HttpHeaderId::toString() const { if (table == nullptr) { KJ_ASSERT(id < kj::size(BUILTIN_HEADER_NAMES)); return BUILTIN_HEADER_NAMES[id]; } else { return table->idToString(*this); } } namespace { struct HeaderNameHash { size_t operator()(kj::StringPtr s) const { size_t result = 5381; for (byte b: s.asBytes()) { // Masking bit 0x20 makes our hash case-insensitive while conveniently avoiding any // collisions that would matter for header names. result = ((result << 5) + result) ^ (b & ~0x20); } return result; } bool operator()(kj::StringPtr a, kj::StringPtr b) const { // TODO(perf): I wonder if we can beat strcasecmp() by masking bit 0x20 from each byte. We'd // need to prohibit one of the technically-legal characters '^' or '~' from header names // since they'd otherwise be ambiguous, but otherwise there is no ambiguity. #if _MSC_VER return _stricmp(a.cStr(), b.cStr()) == 0; #else return strcasecmp(a.cStr(), b.cStr()) == 0; #endif } }; } // namespace struct HttpHeaderTable::IdsByNameMap { // TODO(perf): If we were cool we could maybe use a perfect hash here, since our hashtable is // static once built. std::unordered_map map; }; HttpHeaderTable::Builder::Builder() : table(kj::heap()) {} HttpHeaderId HttpHeaderTable::Builder::add(kj::StringPtr name) { requireValidHeaderName(name); auto insertResult = table->idsByName->map.insert(std::make_pair(name, table->namesById.size())); if (insertResult.second) { table->namesById.add(name); } return HttpHeaderId(table, insertResult.first->second); } HttpHeaderTable::HttpHeaderTable() : idsByName(kj::heap()) { #define ADD_HEADER(id, name) \ namesById.add(name); \ idsByName->map.insert(std::make_pair(name, HttpHeaders::BuiltinIndices::id)); KJ_HTTP_FOR_EACH_BUILTIN_HEADER(ADD_HEADER); #undef ADD_HEADER } HttpHeaderTable::~HttpHeaderTable() noexcept(false) {} kj::Maybe HttpHeaderTable::stringToId(kj::StringPtr name) const { auto iter = idsByName->map.find(name); if (iter == idsByName->map.end()) { return nullptr; } else { return HttpHeaderId(this, iter->second); } } // ======================================================================================= HttpHeaders::HttpHeaders(const HttpHeaderTable& table) : table(&table), indexedHeaders(kj::heapArray(table.idCount())) {} void HttpHeaders::clear() { for (auto& header: indexedHeaders) { header = nullptr; } unindexedHeaders.clear(); } size_t HttpHeaders::size() const { size_t result = unindexedHeaders.size(); for (auto i: kj::indices(indexedHeaders)) { if (indexedHeaders[i] != nullptr) { ++result; } } return result; } HttpHeaders HttpHeaders::clone() const { HttpHeaders result(*table); for (auto i: kj::indices(indexedHeaders)) { if (indexedHeaders[i] != nullptr) { result.indexedHeaders[i] = result.cloneToOwn(indexedHeaders[i]); } } result.unindexedHeaders.resize(unindexedHeaders.size()); for (auto i: kj::indices(unindexedHeaders)) { result.unindexedHeaders[i].name = result.cloneToOwn(unindexedHeaders[i].name); result.unindexedHeaders[i].value = result.cloneToOwn(unindexedHeaders[i].value); } return result; } HttpHeaders HttpHeaders::cloneShallow() const { HttpHeaders result(*table); for (auto i: kj::indices(indexedHeaders)) { if (indexedHeaders[i] != nullptr) { result.indexedHeaders[i] = indexedHeaders[i]; } } result.unindexedHeaders.resize(unindexedHeaders.size()); for (auto i: kj::indices(unindexedHeaders)) { result.unindexedHeaders[i] = unindexedHeaders[i]; } return result; } kj::StringPtr HttpHeaders::cloneToOwn(kj::StringPtr str) { auto copy = kj::heapString(str); kj::StringPtr result = copy; ownedStrings.add(copy.releaseArray()); return result; } namespace { template constexpr bool fastCaseCmp(const char* actual); } // namespace bool HttpHeaders::isWebSocket() const { return fastCaseCmp<'w', 'e', 'b', 's', 'o', 'c', 'k', 'e', 't'>( get(HttpHeaderId::UPGRADE).orDefault(nullptr).cStr()); } void HttpHeaders::set(HttpHeaderId id, kj::StringPtr value) { id.requireFrom(*table); requireValidHeaderValue(value); indexedHeaders[id.id] = value; } void HttpHeaders::set(HttpHeaderId id, kj::String&& value) { set(id, kj::StringPtr(value)); takeOwnership(kj::mv(value)); } void HttpHeaders::add(kj::StringPtr name, kj::StringPtr value) { requireValidHeaderName(name); requireValidHeaderValue(value); addNoCheck(name, value); } void HttpHeaders::add(kj::StringPtr name, kj::String&& value) { add(name, kj::StringPtr(value)); takeOwnership(kj::mv(value)); } void HttpHeaders::add(kj::String&& name, kj::String&& value) { add(kj::StringPtr(name), kj::StringPtr(value)); takeOwnership(kj::mv(name)); takeOwnership(kj::mv(value)); } void HttpHeaders::addNoCheck(kj::StringPtr name, kj::StringPtr value) { KJ_IF_MAYBE(id, table->stringToId(name)) { if (indexedHeaders[id->id] == nullptr) { indexedHeaders[id->id] = value; } else { // Duplicate HTTP headers are equivalent to the values being separated by a comma. #if _MSC_VER if (_stricmp(name.cStr(), "set-cookie") == 0) { #else if (strcasecmp(name.cStr(), "set-cookie") == 0) { #endif // Uh-oh, Set-Cookie will be corrupted if we try to concatenate it. We'll make it an // unindexed header, which is weird, but the alternative is guaranteed corruption, so... // TODO(cleanup): Maybe HttpHeaders should just special-case set-cookie in general? unindexedHeaders.add(Header {name, value}); } else { auto concat = kj::str(indexedHeaders[id->id], ", ", value); indexedHeaders[id->id] = concat; ownedStrings.add(concat.releaseArray()); } } } else { unindexedHeaders.add(Header {name, value}); } } void HttpHeaders::takeOwnership(kj::String&& string) { ownedStrings.add(string.releaseArray()); } void HttpHeaders::takeOwnership(kj::Array&& chars) { ownedStrings.add(kj::mv(chars)); } void HttpHeaders::takeOwnership(HttpHeaders&& otherHeaders) { for (auto& str: otherHeaders.ownedStrings) { ownedStrings.add(kj::mv(str)); } otherHeaders.ownedStrings.clear(); } // ----------------------------------------------------------------------------- static inline char* skipSpace(char* p) { for (;;) { switch (*p) { case '\t': case ' ': ++p; break; default: return p; } } } static kj::Maybe consumeWord(char*& ptr) { char* start = skipSpace(ptr); char* p = start; for (;;) { switch (*p) { case '\0': ptr = p; return kj::StringPtr(start, p); case '\t': case ' ': { char* end = p++; ptr = p; *end = '\0'; return kj::StringPtr(start, end); } case '\n': case '\r': // Not expecting EOL! return nullptr; default: ++p; break; } } } static kj::Maybe consumeNumber(char*& ptr) { char* start = skipSpace(ptr); char* p = start; uint result = 0; for (;;) { char c = *p; if ('0' <= c && c <= '9') { result = result * 10 + (c - '0'); ++p; } else { if (p == start) return nullptr; ptr = p; return result; } } } static kj::StringPtr consumeLine(char*& ptr) { char* start = skipSpace(ptr); char* p = start; for (;;) { switch (*p) { case '\0': ptr = p; return kj::StringPtr(start, p); case '\r': { char* end = p++; if (*p == '\n') ++p; if (*p == ' ' || *p == '\t') { // Whoa, continuation line. These are deprecated, but historically a line starting with // a space was treated as a continuation of the previous line. The behavior should be // the same as if the \r\n were replaced with spaces, so let's do that here to prevent // confusion later. *end = ' '; p[-1] = ' '; break; } ptr = p; *end = '\0'; return kj::StringPtr(start, end); } case '\n': { char* end = p++; if (*p == ' ' || *p == '\t') { // Whoa, continuation line. These are deprecated, but historically a line starting with // a space was treated as a continuation of the previous line. The behavior should be // the same as if the \n were replaced with spaces, so let's do that here to prevent // confusion later. *end = ' '; break; } ptr = p; *end = '\0'; return kj::StringPtr(start, end); } default: ++p; break; } } } static kj::Maybe consumeHeaderName(char*& ptr) { // Do NOT skip spaces before the header name. Leading spaces indicate a continuation line; they // should have been handled in consumeLine(). char* p = ptr; char* start = p; while (HTTP_HEADER_NAME_CHARS.contains(*p)) ++p; char* end = p; p = skipSpace(p); if (end == start || *p != ':') return nullptr; ++p; p = skipSpace(p); *end = '\0'; ptr = p; return kj::StringPtr(start, end); } static char* trimHeaderEnding(kj::ArrayPtr content) { // Trim off the trailing \r\n from a header blob. if (content.size() < 2) return nullptr; // Remove trailing \r\n\r\n and replace with \0 sentinel char. char* end = content.end(); if (end[-1] != '\n') return nullptr; --end; if (end[-1] == '\r') --end; *end = '\0'; return end; } HttpHeaders::RequestOrProtocolError HttpHeaders::tryParseRequest(kj::ArrayPtr content) { char* end = trimHeaderEnding(content); if (end == nullptr) { return ProtocolError { 400, "Bad Request", "ERROR: Request headers have no terminal newline.", content }; } char* ptr = content.begin(); HttpHeaders::Request request; KJ_IF_MAYBE(method, consumeHttpMethod(ptr)) { request.method = *method; if (*ptr != ' ' && *ptr != '\t') { return ProtocolError { 501, "Not Implemented", "ERROR: Unrecognized request method.", content }; } ++ptr; } else { return ProtocolError { 501, "Not Implemented", "ERROR: Unrecognized request method.", content }; } KJ_IF_MAYBE(path, consumeWord(ptr)) { request.url = *path; } else { return ProtocolError { 400, "Bad Request", "ERROR: Invalid request line.", content }; } // Ignore rest of line. Don't care about "HTTP/1.1" or whatever. consumeLine(ptr); if (!parseHeaders(ptr, end)) { return ProtocolError { 400, "Bad Request", "ERROR: The headers sent by your client are not valid.", content }; } return request; } HttpHeaders::ResponseOrProtocolError HttpHeaders::tryParseResponse(kj::ArrayPtr content) { char* end = trimHeaderEnding(content); if (end == nullptr) { return ProtocolError { 502, "Bad Gateway", "ERROR: Response headers have no terminal newline.", content }; } char* ptr = content.begin(); HttpHeaders::Response response; KJ_IF_MAYBE(version, consumeWord(ptr)) { if (!version->startsWith("HTTP/")) { return ProtocolError { 502, "Bad Gateway", "ERROR: Invalid response status line (invalid protocol).", content }; } } else { return ProtocolError { 502, "Bad Gateway", "ERROR: Invalid response status line (no spaces).", content }; } KJ_IF_MAYBE(code, consumeNumber(ptr)) { response.statusCode = *code; } else { return ProtocolError { 502, "Bad Gateway", "ERROR: Invalid response status line (invalid status code).", content }; } response.statusText = consumeLine(ptr); if (!parseHeaders(ptr, end)) { return ProtocolError { 502, "Bad Gateway", "ERROR: The headers sent by the server are not valid.", content }; } return response; } bool HttpHeaders::tryParse(kj::ArrayPtr content) { char* end = trimHeaderEnding(content); if (end == nullptr) return false; char* ptr = content.begin(); return parseHeaders(ptr, end); } bool HttpHeaders::parseHeaders(char* ptr, char* end) { while (*ptr != '\0') { KJ_IF_MAYBE(name, consumeHeaderName(ptr)) { kj::StringPtr line = consumeLine(ptr); addNoCheck(*name, line); } else { return false; } } return ptr == end; } // ----------------------------------------------------------------------------- kj::String HttpHeaders::serializeRequest( HttpMethod method, kj::StringPtr url, kj::ArrayPtr connectionHeaders) const { return serialize(kj::toCharSequence(method), url, kj::StringPtr("HTTP/1.1"), connectionHeaders); } kj::String HttpHeaders::serializeResponse( uint statusCode, kj::StringPtr statusText, kj::ArrayPtr connectionHeaders) const { auto statusCodeStr = kj::toCharSequence(statusCode); return serialize(kj::StringPtr("HTTP/1.1"), statusCodeStr, statusText, connectionHeaders); } kj::String HttpHeaders::serialize(kj::ArrayPtr word1, kj::ArrayPtr word2, kj::ArrayPtr word3, kj::ArrayPtr connectionHeaders) const { const kj::StringPtr space = " "; const kj::StringPtr newline = "\r\n"; const kj::StringPtr colon = ": "; size_t size = 2; // final \r\n if (word1 != nullptr) { size += word1.size() + word2.size() + word3.size() + 4; } KJ_ASSERT(connectionHeaders.size() <= indexedHeaders.size()); for (auto i: kj::indices(indexedHeaders)) { kj::StringPtr value = i < connectionHeaders.size() ? connectionHeaders[i] : indexedHeaders[i]; if (value != nullptr) { size += table->idToString(HttpHeaderId(table, i)).size() + value.size() + 4; } } for (auto& header: unindexedHeaders) { size += header.name.size() + header.value.size() + 4; } String result = heapString(size); char* ptr = result.begin(); if (word1 != nullptr) { ptr = kj::_::fill(ptr, word1, space, word2, space, word3, newline); } for (auto i: kj::indices(indexedHeaders)) { kj::StringPtr value = i < connectionHeaders.size() ? connectionHeaders[i] : indexedHeaders[i]; if (value != nullptr) { ptr = kj::_::fill(ptr, table->idToString(HttpHeaderId(table, i)), colon, value, newline); } } for (auto& header: unindexedHeaders) { ptr = kj::_::fill(ptr, header.name, colon, header.value, newline); } ptr = kj::_::fill(ptr, newline); KJ_ASSERT(ptr == result.end()); return result; } kj::String HttpHeaders::toString() const { return serialize(nullptr, nullptr, nullptr, nullptr); } // ======================================================================================= namespace { static constexpr size_t MIN_BUFFER = 4096; static constexpr size_t MAX_BUFFER = 65536; static constexpr size_t MAX_CHUNK_HEADER_SIZE = 32; class HttpInputStreamImpl final: public HttpInputStream { public: explicit HttpInputStreamImpl(AsyncInputStream& inner, HttpHeaderTable& table) : inner(inner), headerBuffer(kj::heapArray(MIN_BUFFER)), headers(table) { } bool canReuse() { return !broken && pendingMessageCount == 0; } // --------------------------------------------------------------------------- // public interface kj::Promise readRequest() override { return readRequestHeaders() .then([this](HttpHeaders::RequestOrProtocolError&& requestOrProtocolError) -> HttpInputStream::Request { auto request = KJ_REQUIRE_NONNULL( requestOrProtocolError.tryGet(), "bad request"); auto body = getEntityBody(HttpInputStreamImpl::REQUEST, request.method, 0, headers); return { request.method, request.url, headers, kj::mv(body) }; }); } kj::Promise readResponse(HttpMethod requestMethod) override { return readResponseHeaders() .then([this,requestMethod](HttpHeaders::ResponseOrProtocolError&& responseOrProtocolError) -> HttpInputStream::Response { auto response = KJ_REQUIRE_NONNULL( responseOrProtocolError.tryGet(), "bad response"); auto body = getEntityBody(HttpInputStreamImpl::RESPONSE, requestMethod, 0, headers); return { response.statusCode, response.statusText, headers, kj::mv(body) }; }); } kj::Promise readMessage() override { return readMessageHeaders() .then([this](kj::ArrayPtr text) -> HttpInputStream::Message { headers.clear(); KJ_REQUIRE(headers.tryParse(text), "bad message"); auto body = getEntityBody(HttpInputStreamImpl::RESPONSE, HttpMethod::GET, 0, headers); return { headers, kj::mv(body) }; }); } // --------------------------------------------------------------------------- // Stream locking: While an entity-body is being read, the body stream "locks" the underlying // HTTP stream. Once the entity-body is complete, we can read the next pipelined message. void finishRead() { // Called when entire request has been read. KJ_REQUIRE_NONNULL(onMessageDone)->fulfill(); onMessageDone = nullptr; --pendingMessageCount; } void abortRead() { // Called when a body input stream was destroyed without reading to the end. KJ_REQUIRE_NONNULL(onMessageDone)->reject(KJ_EXCEPTION(FAILED, "application did not finish reading previous HTTP response body", "can't read next pipelined request/response")); onMessageDone = nullptr; broken = true; } // --------------------------------------------------------------------------- kj::Promise awaitNextMessage() override { // Waits until more data is available, but doesn't consume it. Returns false on EOF. // // Used on the server after a request is handled, to check for pipelined requests. // // Used on the client to detect when idle connections are closed from the server end. (In this // case, the promise always returns false or is canceled.) if (onMessageDone != nullptr) { // We're still working on reading the previous body. auto fork = messageReadQueue.fork(); messageReadQueue = fork.addBranch(); return fork.addBranch().then([this]() { return awaitNextMessage(); }); } snarfBufferedLineBreak(); if (!lineBreakBeforeNextHeader && leftover != nullptr) { return true; } return inner.tryRead(headerBuffer.begin(), 1, headerBuffer.size()) .then([this](size_t amount) -> kj::Promise { if (amount > 0) { leftover = headerBuffer.slice(0, amount); return awaitNextMessage(); } else { return false; } }); } bool isCleanDrain() { // Returns whether we can cleanly drain the stream at this point. if (onMessageDone != nullptr) return false; snarfBufferedLineBreak(); return !lineBreakBeforeNextHeader && leftover == nullptr; } kj::Promise> readMessageHeaders() { ++pendingMessageCount; auto paf = kj::newPromiseAndFulfiller(); auto promise = messageReadQueue .then(kj::mvCapture(paf.fulfiller, [this](kj::Own> fulfiller) { onMessageDone = kj::mv(fulfiller); return readHeader(HeaderType::MESSAGE, 0, 0); })); messageReadQueue = kj::mv(paf.promise); return promise; } kj::Promise readChunkHeader() { KJ_REQUIRE(onMessageDone != nullptr); // We use the portion of the header after the end of message headers. return readHeader(HeaderType::CHUNK, messageHeaderEnd, messageHeaderEnd) .then([](kj::ArrayPtr text) -> uint64_t { KJ_REQUIRE(text.size() > 0) { break; } uint64_t value = 0; for (char c: text) { if ('0' <= c && c <= '9') { value = value * 16 + (c - '0'); } else if ('a' <= c && c <= 'f') { value = value * 16 + (c - 'a' + 10); } else if ('A' <= c && c <= 'F') { value = value * 16 + (c - 'A' + 10); } else { KJ_FAIL_REQUIRE("invalid HTTP chunk size", text, text.asBytes()) { break; } return value; } } return value; }); } inline kj::Promise readRequestHeaders() { return readMessageHeaders().then([this](kj::ArrayPtr text) { headers.clear(); return headers.tryParseRequest(text); }); } inline kj::Promise readResponseHeaders() { // Note: readResponseHeaders() could be called multiple times concurrently when pipelining // requests. readMessageHeaders() will serialize these, but it's important not to mess with // state (like calling headers.clear()) before said serialization has taken place. return readMessageHeaders().then([this](kj::ArrayPtr text) { headers.clear(); return headers.tryParseResponse(text); }); } inline const HttpHeaders& getHeaders() const { return headers; } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) { // Read message body data. KJ_REQUIRE(onMessageDone != nullptr); if (leftover == nullptr) { // No leftovers. Forward directly to inner stream. return inner.tryRead(buffer, minBytes, maxBytes); } else if (leftover.size() >= maxBytes) { // Didn't even read the entire leftover buffer. memcpy(buffer, leftover.begin(), maxBytes); leftover = leftover.slice(maxBytes, leftover.size()); return maxBytes; } else { // Read the entire leftover buffer, plus some. memcpy(buffer, leftover.begin(), leftover.size()); size_t copied = leftover.size(); leftover = nullptr; if (copied >= minBytes) { // Got enough to stop here. return copied; } else { // Read the rest from the underlying stream. return inner.tryRead(reinterpret_cast(buffer) + copied, minBytes - copied, maxBytes - copied) .then([copied](size_t n) { return n + copied; }); } } } enum RequestOrResponse { REQUEST, RESPONSE }; kj::Own getEntityBody( RequestOrResponse type, HttpMethod method, uint statusCode, const kj::HttpHeaders& headers); struct ReleasedBuffer { kj::Array buffer; kj::ArrayPtr leftover; }; ReleasedBuffer releaseBuffer() { return { headerBuffer.releaseAsBytes(), leftover.asBytes() }; } private: AsyncInputStream& inner; kj::Array headerBuffer; size_t messageHeaderEnd = 0; // Position in headerBuffer where the message headers end -- further buffer space can // be used for chunk headers. kj::ArrayPtr leftover; // Data in headerBuffer that comes immediately after the header content, if any. HttpHeaders headers; // Parsed headers, after a call to parseAwaited*(). bool lineBreakBeforeNextHeader = false; // If true, the next await should expect to start with a spurious '\n' or '\r\n'. This happens // as a side-effect of HTTP chunked encoding, where such a newline is added to the end of each // chunk, for no good reason. bool broken = false; // Becomes true if the caller failed to read the whole entity-body before closing the stream. uint pendingMessageCount = 0; // Number of reads we have queued up. kj::Promise messageReadQueue = kj::READY_NOW; kj::Maybe>> onMessageDone; // Fulfill once the current message has been completely read. Unblocks reading of the next // message headers. enum class HeaderType { MESSAGE, CHUNK }; kj::Promise> readHeader( HeaderType type, size_t bufferStart, size_t bufferEnd) { // Reads the HTTP message header or a chunk header (as in transfer-encoding chunked) and // returns the buffer slice containing it. // // The main source of complication here is that we want to end up with one continuous buffer // containing the result, and that the input is delimited by newlines rather than by an upfront // length. kj::Promise readPromise = nullptr; // Figure out where we're reading from. if (leftover != nullptr) { // Some data is still left over from the previous message, so start with that. // This can only happen if this is the initial call to readHeader() (not recursive). KJ_ASSERT(bufferStart == bufferEnd); // OK, set bufferStart and bufferEnd to both point to the start of the leftover, and then // fake a read promise as if we read the bytes from the leftover. bufferStart = leftover.begin() - headerBuffer.begin(); bufferEnd = bufferStart; readPromise = leftover.size(); leftover = nullptr; } else { // Need to read more data from the underlying stream. if (bufferEnd == headerBuffer.size()) { // Out of buffer space. // Maybe we can move bufferStart backwards to make more space at the end? size_t minStart = type == HeaderType::MESSAGE ? 0 : messageHeaderEnd; if (bufferStart > minStart) { // Move to make space. memmove(headerBuffer.begin() + minStart, headerBuffer.begin() + bufferStart, bufferEnd - bufferStart); bufferEnd = bufferEnd - bufferStart + minStart; bufferStart = minStart; } else { // Really out of buffer space. Grow the buffer. if (type != HeaderType::MESSAGE) { // Can't grow because we'd invalidate the HTTP headers. return KJ_EXCEPTION(FAILED, "invalid HTTP chunk size"); } KJ_REQUIRE(headerBuffer.size() < MAX_BUFFER, "request headers too large"); auto newBuffer = kj::heapArray(headerBuffer.size() * 2); memcpy(newBuffer.begin(), headerBuffer.begin(), headerBuffer.size()); headerBuffer = kj::mv(newBuffer); } } // How many bytes will we read? size_t maxBytes = headerBuffer.size() - bufferEnd; if (type == HeaderType::CHUNK) { // Roughly limit the amount of data we read to MAX_CHUNK_HEADER_SIZE. // TODO(perf): This is mainly to avoid copying a lot of body data into our buffer just to // copy it again when it is read. But maybe the copy would be cheaper than overhead of // extra event loop turns? KJ_REQUIRE(bufferEnd - bufferStart <= MAX_CHUNK_HEADER_SIZE, "invalid HTTP chunk size"); maxBytes = kj::min(maxBytes, MAX_CHUNK_HEADER_SIZE); } readPromise = inner.read(headerBuffer.begin() + bufferEnd, 1, maxBytes); } return readPromise.then([this,type,bufferStart,bufferEnd](size_t amount) mutable -> kj::Promise> { if (lineBreakBeforeNextHeader) { // Hackily deal with expected leading line break. if (bufferEnd == bufferStart && headerBuffer[bufferEnd] == '\r') { ++bufferEnd; --amount; } if (amount > 0 && headerBuffer[bufferEnd] == '\n') { lineBreakBeforeNextHeader = false; ++bufferEnd; --amount; // Cut the leading line break out of the buffer entirely. bufferStart = bufferEnd; } if (amount == 0) { return readHeader(type, bufferStart, bufferEnd); } } size_t pos = bufferEnd; size_t newEnd = pos + amount; for (;;) { // Search for next newline. char* nl = reinterpret_cast( memchr(headerBuffer.begin() + pos, '\n', newEnd - pos)); if (nl == nullptr) { // No newline found. Wait for more data. return readHeader(type, bufferStart, newEnd); } // Is this newline which we found the last of the header? For a chunk header, always. For // a message header, we search for two newlines in a row. We accept either "\r\n" or just // "\n" as a newline sequence (though the standard requires "\r\n"). if (type == HeaderType::CHUNK || (nl - headerBuffer.begin() >= 4 && ((nl[-1] == '\r' && nl[-2] == '\n') || (nl[-1] == '\n')))) { // OK, we've got all the data! size_t endIndex = nl + 1 - headerBuffer.begin(); size_t leftoverStart = endIndex; // Strip off the last newline from end. endIndex -= 1 + (nl[-1] == '\r'); if (type == HeaderType::MESSAGE) { if (headerBuffer.size() - newEnd < MAX_CHUNK_HEADER_SIZE) { // Ugh, there's not enough space for the secondary await buffer. Grow once more. auto newBuffer = kj::heapArray(headerBuffer.size() * 2); memcpy(newBuffer.begin(), headerBuffer.begin(), headerBuffer.size()); headerBuffer = kj::mv(newBuffer); } messageHeaderEnd = endIndex; } else { // For some reason, HTTP specifies that there will be a line break after each chunk. lineBreakBeforeNextHeader = true; } auto result = headerBuffer.slice(bufferStart, endIndex); leftover = headerBuffer.slice(leftoverStart, newEnd); return result; } else { pos = nl - headerBuffer.begin() + 1; } } }); } void snarfBufferedLineBreak() { // Slightly-crappy code to snarf the expected line break. This will actually eat the leading // regex /\r*\n?/. while (lineBreakBeforeNextHeader && leftover.size() > 0) { if (leftover[0] == '\r') { leftover = leftover.slice(1, leftover.size()); } else if (leftover[0] == '\n') { leftover = leftover.slice(1, leftover.size()); lineBreakBeforeNextHeader = false; } else { // Err, missing line break, whatever. lineBreakBeforeNextHeader = false; } } } }; // ----------------------------------------------------------------------------- class HttpEntityBodyReader: public kj::AsyncInputStream { public: HttpEntityBodyReader(HttpInputStreamImpl& inner): inner(inner) {} ~HttpEntityBodyReader() noexcept(false) { if (!finished) { inner.abortRead(); } } protected: HttpInputStreamImpl& inner; void doneReading() { KJ_REQUIRE(!finished); finished = true; inner.finishRead(); } inline bool alreadyDone() { return finished; } private: bool finished = false; }; class HttpNullEntityReader final: public HttpEntityBodyReader { // Stream for an entity-body which is not present. Always returns EOF on read, but tryGetLength() // may indicate non-zero in the special case of a response to a HEAD request. public: HttpNullEntityReader(HttpInputStreamImpl& inner, kj::Maybe length) : HttpEntityBodyReader(inner), length(length) { // `length` is what to return from tryGetLength(). For a response to a HEAD request, this may // be non-zero. doneReading(); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return size_t(0); } Maybe tryGetLength() override { return length; } private: kj::Maybe length; }; class HttpConnectionCloseEntityReader final: public HttpEntityBodyReader { // Stream which reads until EOF. public: HttpConnectionCloseEntityReader(HttpInputStreamImpl& inner) : HttpEntityBodyReader(inner) {} Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { if (alreadyDone()) return size_t(0); return inner.tryRead(buffer, minBytes, maxBytes) .then([=](size_t amount) { if (amount < minBytes) { doneReading(); } return amount; }); } }; class HttpFixedLengthEntityReader final: public HttpEntityBodyReader { // Stream which reads only up to a fixed length from the underlying stream, then emulates EOF. public: HttpFixedLengthEntityReader(HttpInputStreamImpl& inner, size_t length) : HttpEntityBodyReader(inner), length(length) { if (length == 0) doneReading(); } Maybe tryGetLength() override { return length; } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { if (length == 0) return size_t(0); return inner.tryRead(buffer, kj::min(minBytes, length), kj::min(maxBytes, length)) .then([=](size_t amount) { length -= amount; if (length > 0 && amount < minBytes) { kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "premature EOF in HTTP entity body; did not reach Content-Length")); } else if (length == 0) { doneReading(); } return amount; }); } private: size_t length; }; class HttpChunkedEntityReader final: public HttpEntityBodyReader { // Stream which reads a Transfer-Encoding: Chunked stream. public: HttpChunkedEntityReader(HttpInputStreamImpl& inner) : HttpEntityBodyReader(inner) {} Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return tryReadInternal(buffer, minBytes, maxBytes, 0); } private: size_t chunkSize = 0; Promise tryReadInternal(void* buffer, size_t minBytes, size_t maxBytes, size_t alreadyRead) { if (alreadyDone()) { return alreadyRead; } else if (chunkSize == 0) { // Read next chunk header. return inner.readChunkHeader().then([=](uint64_t nextChunkSize) { if (nextChunkSize == 0) { doneReading(); } chunkSize = nextChunkSize; return tryReadInternal(buffer, minBytes, maxBytes, alreadyRead); }); } else if (chunkSize < minBytes) { // Read entire current chunk and continue to next chunk. return inner.tryRead(buffer, chunkSize, chunkSize) .then([=](size_t amount) -> kj::Promise { chunkSize -= amount; if (chunkSize > 0) { return KJ_EXCEPTION(DISCONNECTED, "premature EOF in HTTP chunk"); } return tryReadInternal(reinterpret_cast(buffer) + amount, minBytes - amount, maxBytes - amount, alreadyRead + amount); }); } else { // Read only part of the current chunk. return inner.tryRead(buffer, minBytes, kj::min(maxBytes, chunkSize)) .then([=](size_t amount) -> size_t { chunkSize -= amount; if (amount < minBytes) { kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "premature EOF in HTTP chunk")); } return alreadyRead + amount; }); } } }; template struct FastCaseCmp; template struct FastCaseCmp { static constexpr bool apply(const char* actual) { return 'a' <= first && first <= 'z' ? (*actual | 0x20) == first && FastCaseCmp::apply(actual + 1) : 'A' <= first && first <= 'Z' ? (*actual & ~0x20) == first && FastCaseCmp::apply(actual + 1) : *actual == first && FastCaseCmp::apply(actual + 1); } }; template <> struct FastCaseCmp<> { static constexpr bool apply(const char* actual) { return *actual == '\0'; } }; template constexpr bool fastCaseCmp(const char* actual) { return FastCaseCmp::apply(actual); } // Tests static_assert(fastCaseCmp<'f','O','o','B','1'>("FooB1"), ""); static_assert(!fastCaseCmp<'f','O','o','B','2'>("FooB1"), ""); static_assert(!fastCaseCmp<'n','O','o','B','1'>("FooB1"), ""); static_assert(!fastCaseCmp<'f','O','o','B'>("FooB1"), ""); static_assert(!fastCaseCmp<'f','O','o','B','1','a'>("FooB1"), ""); kj::Own HttpInputStreamImpl::getEntityBody( RequestOrResponse type, HttpMethod method, uint statusCode, const kj::HttpHeaders& headers) { // Rules to determine how HTTP entity-body is delimited: // https://tools.ietf.org/html/rfc7230#section-3.3.3 // #1 if (type == RESPONSE) { if (method == HttpMethod::HEAD) { // Body elided. kj::Maybe length; KJ_IF_MAYBE(cl, headers.get(HttpHeaderId::CONTENT_LENGTH)) { length = strtoull(cl->cStr(), nullptr, 10); } else if (headers.get(HttpHeaderId::TRANSFER_ENCODING) == nullptr) { // HACK: Neither Content-Length nor Transfer-Encoding header in response to HEAD request. // Propagate this fact with a 0 expected body length. length = uint64_t(0); } return kj::heap(*this, length); } else if (statusCode == 204 || statusCode == 205 || statusCode == 304) { // No body. return kj::heap(*this, uint64_t(0)); } } // #2 deals with the CONNECT method which is handled separately. // #3 KJ_IF_MAYBE(te, headers.get(HttpHeaderId::TRANSFER_ENCODING)) { // TODO(someday): Support plugable transfer encodings? Or at least gzip? // TODO(someday): Support stacked transfer encodings, e.g. "gzip, chunked". // NOTE: #3¶3 is ambiguous about what should happen if Transfer-Encoding and Content-Length are // both present. It says that Transfer-Encoding takes precedence, but also that the request // "ought to be handled as an error", and that proxies "MUST" drop the Content-Length before // forwarding. We ignore the vague "ought to" part and implement the other two. (The // dropping of Content-Length will happen naturally if/when the message is sent back out to // the network.) if (fastCaseCmp<'c','h','u','n','k','e','d'>(te->cStr())) { // #3¶1 return kj::heap(*this); } else if (fastCaseCmp<'i','d','e','n','t','i','t','y'>(te->cStr())) { // #3¶2 KJ_REQUIRE(type != REQUEST, "request body cannot have Transfer-Encoding other than chunked"); return kj::heap(*this); } else { KJ_FAIL_REQUIRE("unknown transfer encoding", *te) { break; } } } // #4 and #5 KJ_IF_MAYBE(cl, headers.get(HttpHeaderId::CONTENT_LENGTH)) { // NOTE: By spec, multiple Content-Length values are allowed as long as they are the same, e.g. // "Content-Length: 5, 5, 5". Hopefully no one actually does that... char* end; uint64_t length = strtoull(cl->cStr(), &end, 10); if (end > cl->begin() && *end == '\0') { // #5 return kj::heap(*this, length); } else { // #4 (bad content-length) KJ_FAIL_REQUIRE("invalid Content-Length header value", *cl); } } // #6 if (type == REQUEST) { // Lack of a Content-Length or Transfer-Encoding means no body for requests. return kj::heap(*this, uint64_t(0)); } // RFC 2616 permitted "multipart/byteranges" responses to be self-delimiting, but this was // mercifully removed in RFC 7230, and new exceptions of this type are disallowed: // https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4 // https://tools.ietf.org/html/rfc7230#page-81 // To be extra-safe, we'll reject a multipart/byteranges response that lacks transfer-encoding // and content-length. KJ_IF_MAYBE(type, headers.get(HttpHeaderId::CONTENT_TYPE)) { if (type->startsWith("multipart/byteranges")) { KJ_FAIL_REQUIRE( "refusing to handle multipart/byteranges response without transfer-encoding nor " "content-length due to ambiguity between RFC 2616 vs RFC 7230."); } } // #7 return kj::heap(*this); } } // namespace kj::Own newHttpInputStream(kj::AsyncInputStream& input, HttpHeaderTable& table) { return kj::heap(input, table); } // ======================================================================================= namespace { class HttpOutputStream { public: HttpOutputStream(AsyncOutputStream& inner): inner(inner) {} bool isInBody() { return inBody; } bool canReuse() { return !inBody && !broken && !writeInProgress; } bool canWriteBodyData() { return !writeInProgress && inBody; } bool isBroken() { return broken; } void writeHeaders(String content) { // Writes some header content and begins a new entity body. KJ_REQUIRE(!writeInProgress, "concurrent write()s not allowed") { return; } KJ_REQUIRE(!inBody, "previous HTTP message body incomplete; can't write more messages"); inBody = true; queueWrite(kj::mv(content)); } void writeBodyData(kj::String content) { KJ_REQUIRE(!writeInProgress, "concurrent write()s not allowed") { return; } KJ_REQUIRE(inBody) { return; } queueWrite(kj::mv(content)); } kj::Promise writeBodyData(const void* buffer, size_t size) { KJ_REQUIRE(!writeInProgress, "concurrent write()s not allowed") { return kj::READY_NOW; } KJ_REQUIRE(inBody) { return kj::READY_NOW; } writeInProgress = true; auto fork = writeQueue.fork(); writeQueue = fork.addBranch(); return fork.addBranch().then([this,buffer,size]() { return inner.write(buffer, size); }).then([this]() { writeInProgress = false; }); } kj::Promise writeBodyData(kj::ArrayPtr> pieces) { KJ_REQUIRE(!writeInProgress, "concurrent write()s not allowed") { return kj::READY_NOW; } KJ_REQUIRE(inBody) { return kj::READY_NOW; } writeInProgress = true; auto fork = writeQueue.fork(); writeQueue = fork.addBranch(); return fork.addBranch().then([this,pieces]() { return inner.write(pieces); }).then([this]() { writeInProgress = false; }); } Promise pumpBodyFrom(AsyncInputStream& input, uint64_t amount) { KJ_REQUIRE(!writeInProgress, "concurrent write()s not allowed") { return uint64_t(0); } KJ_REQUIRE(inBody) { return uint64_t(0); } writeInProgress = true; auto fork = writeQueue.fork(); writeQueue = fork.addBranch(); return fork.addBranch().then([this,&input,amount]() { return input.pumpTo(inner, amount); }).then([this](uint64_t actual) { writeInProgress = false; return actual; }); } void finishBody() { // Called when entire body was written. KJ_REQUIRE(inBody) { return; } inBody = false; if (writeInProgress) { // It looks like the last write never completed -- possibly because it was canceled or threw // an exception. We must treat this equivalent to abortBody(). broken = true; // Cancel any writes that are still queued. writeQueue = KJ_EXCEPTION(FAILED, "previous HTTP message body incomplete; can't write more messages"); } } void abortBody() { // Called if the application failed to write all expected body bytes. KJ_REQUIRE(inBody) { return; } inBody = false; broken = true; // Cancel any writes that are still queued. writeQueue = KJ_EXCEPTION(FAILED, "previous HTTP message body incomplete; can't write more messages"); } kj::Promise flush() { auto fork = writeQueue.fork(); writeQueue = fork.addBranch(); return fork.addBranch(); } Promise whenWriteDisconnected() { return inner.whenWriteDisconnected(); } bool isWriteInProgress() { return writeInProgress; } private: AsyncOutputStream& inner; kj::Promise writeQueue = kj::READY_NOW; bool inBody = false; bool broken = false; bool writeInProgress = false; // True if a write method has been called and has not completed successfully. In the case that // a write throws an exception or is canceled, this remains true forever. In these cases, the // underlying stream is in an inconsistent state and cannot be reused. void queueWrite(kj::String content) { // We only use queueWrite() in cases where we can take ownership of the write buffer, and where // it is convenient if we can return `void` rather than a promise. In particular, this is used // to write headers and chunk boundaries. Writes of application data do not go into // `writeQueue` because this would prevent cancellation. Instead, they wait until `writeQueue` // is empty, then they make the write directly, using `writeInProgress` to detect and block // concurrent writes. writeQueue = writeQueue.then(kj::mvCapture(content, [this](kj::String&& content) { auto promise = inner.write(content.begin(), content.size()); return promise.attach(kj::mv(content)); })); } }; class HttpNullEntityWriter final: public kj::AsyncOutputStream { public: Promise write(const void* buffer, size_t size) override { return KJ_EXCEPTION(FAILED, "HTTP message has no entity-body; can't write()"); } Promise write(ArrayPtr> pieces) override { return KJ_EXCEPTION(FAILED, "HTTP message has no entity-body; can't write()"); } Promise whenWriteDisconnected() override { return kj::NEVER_DONE; } }; class HttpDiscardingEntityWriter final: public kj::AsyncOutputStream { public: Promise write(const void* buffer, size_t size) override { return kj::READY_NOW; } Promise write(ArrayPtr> pieces) override { return kj::READY_NOW; } Promise whenWriteDisconnected() override { return kj::NEVER_DONE; } }; class HttpFixedLengthEntityWriter final: public kj::AsyncOutputStream { public: HttpFixedLengthEntityWriter(HttpOutputStream& inner, uint64_t length) : inner(inner), length(length) { if (length == 0) inner.finishBody(); } ~HttpFixedLengthEntityWriter() noexcept(false) { if (length > 0 || inner.isWriteInProgress()) { inner.abortBody(); } } Promise write(const void* buffer, size_t size) override { if (size == 0) return kj::READY_NOW; KJ_REQUIRE(size <= length, "overwrote Content-Length"); length -= size; return maybeFinishAfter(inner.writeBodyData(buffer, size)); } Promise write(ArrayPtr> pieces) override { uint64_t size = 0; for (auto& piece: pieces) size += piece.size(); if (size == 0) return kj::READY_NOW; KJ_REQUIRE(size <= length, "overwrote Content-Length"); length -= size; return maybeFinishAfter(inner.writeBodyData(pieces)); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { if (amount == 0) return Promise(uint64_t(0)); bool overshot = amount > length; if (overshot) { // Hmm, the requested amount was too large, but it's common to specify kj::max as the amount // to pump, in which case we pump to EOF. Let's try to verify whether EOF is where we // expect it to be. KJ_IF_MAYBE(available, input.tryGetLength()) { // Great, the stream knows how large it is. If it's indeed larger than the space available // then let's abort. KJ_REQUIRE(*available <= length, "overwrote Content-Length"); } else { // OK, we have no idea how large the input is, so we'll have to check later. } } amount = kj::min(amount, length); length -= amount; auto promise = amount == 0 ? kj::Promise(amount) : inner.pumpBodyFrom(input, amount).then([this,amount](uint64_t actual) { // Adjust for bytes not written. length += amount - actual; if (length == 0) inner.finishBody(); return actual; }); if (overshot) { promise = promise.then([amount,&input](uint64_t actual) -> kj::Promise { if (actual == amount) { // We read exactly the amount expected. In order to detect an overshoot, we have to // try reading one more byte. Ugh. static byte junk; return input.tryRead(&junk, 1, 1).then([actual](size_t extra) { KJ_REQUIRE(extra == 0, "overwrote Content-Length"); return actual; }); } else { // We actually read less data than requested so we couldn't have overshot. In fact, we // undershot. return actual; } }); } return kj::mv(promise); } Promise whenWriteDisconnected() override { return inner.whenWriteDisconnected(); } private: HttpOutputStream& inner; uint64_t length; kj::Promise maybeFinishAfter(kj::Promise promise) { if (length == 0) { return promise.then([this]() { inner.finishBody(); }); } else { return kj::mv(promise); } } }; class HttpChunkedEntityWriter final: public kj::AsyncOutputStream { public: HttpChunkedEntityWriter(HttpOutputStream& inner) : inner(inner) {} ~HttpChunkedEntityWriter() noexcept(false) { if (inner.canWriteBodyData()) { inner.writeBodyData(kj::str("0\r\n\r\n")); inner.finishBody(); } else { inner.abortBody(); } } Promise write(const void* buffer, size_t size) override { if (size == 0) return kj::READY_NOW; // can't encode zero-size chunk since it indicates EOF. auto header = kj::str(kj::hex(size), "\r\n"); auto parts = kj::heapArray>(3); parts[0] = header.asBytes(); parts[1] = kj::arrayPtr(reinterpret_cast(buffer), size); parts[2] = kj::StringPtr("\r\n").asBytes(); auto promise = inner.writeBodyData(parts.asPtr()); return promise.attach(kj::mv(header), kj::mv(parts)); } Promise write(ArrayPtr> pieces) override { uint64_t size = 0; for (auto& piece: pieces) size += piece.size(); if (size == 0) return kj::READY_NOW; // can't encode zero-size chunk since it indicates EOF. auto header = kj::str(kj::hex(size), "\r\n"); auto partsBuilder = kj::heapArrayBuilder>(pieces.size() + 2); partsBuilder.add(header.asBytes()); for (auto& piece: pieces) { partsBuilder.add(piece); } partsBuilder.add(kj::StringPtr("\r\n").asBytes()); auto parts = partsBuilder.finish(); auto promise = inner.writeBodyData(parts.asPtr()); return promise.attach(kj::mv(header), kj::mv(parts)); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { KJ_IF_MAYBE(l, input.tryGetLength()) { // Hey, we know exactly how large the input is, so we can write just one chunk. uint64_t length = kj::min(amount, *l); inner.writeBodyData(kj::str(kj::hex(length), "\r\n")); return inner.pumpBodyFrom(input, length) .then([this,length](uint64_t actual) { if (actual < length) { inner.abortBody(); KJ_FAIL_REQUIRE( "value returned by input.tryGetLength() was greater than actual bytes transferred") { break; } } inner.writeBodyData(kj::str("\r\n")); return actual; }); } else { // Need to use naive read/write loop. return nullptr; } } Promise whenWriteDisconnected() override { return inner.whenWriteDisconnected(); } private: HttpOutputStream& inner; }; // ======================================================================================= class WebSocketImpl final: public WebSocket { public: WebSocketImpl(kj::Own stream, kj::Maybe maskKeyGenerator, kj::Array buffer = kj::heapArray(4096), kj::ArrayPtr leftover = nullptr, kj::Maybe> waitBeforeSend = nullptr) : stream(kj::mv(stream)), maskKeyGenerator(maskKeyGenerator), sendingPong(kj::mv(waitBeforeSend)), recvBuffer(kj::mv(buffer)), recvData(leftover) {} kj::Promise send(kj::ArrayPtr message) override { return sendImpl(OPCODE_BINARY, message); } kj::Promise send(kj::ArrayPtr message) override { return sendImpl(OPCODE_TEXT, message.asBytes()); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { kj::Array payload; if (code == 1005) { KJ_REQUIRE(reason.size() == 0, "WebSocket close code 1005 cannot have a reason"); // code 1005 -- leave payload empty } else { payload = heapArray(reason.size() + 2); payload[0] = code >> 8; payload[1] = code; memcpy(payload.begin() + 2, reason.begin(), reason.size()); } auto promise = sendImpl(OPCODE_CLOSE, payload); return promise.attach(kj::mv(payload)); } kj::Promise disconnect() override { KJ_REQUIRE(!currentlySending, "another message send is already in progress"); KJ_IF_MAYBE(p, sendingPong) { // We recently sent a pong, make sure it's finished before proceeding. currentlySending = true; auto promise = p->then([this]() { currentlySending = false; return disconnect(); }); sendingPong = nullptr; return promise; } disconnected = true; stream->shutdownWrite(); return kj::READY_NOW; } void abort() override { queuedPong = nullptr; sendingPong = nullptr; disconnected = true; stream->abortRead(); stream->shutdownWrite(); } kj::Promise whenAborted() override { return stream->whenWriteDisconnected(); } kj::Promise receive() override { size_t headerSize = Header::headerSize(recvData.begin(), recvData.size()); if (headerSize > recvData.size()) { if (recvData.begin() != recvBuffer.begin()) { // Move existing data to front of buffer. if (recvData.size() > 0) { memmove(recvBuffer.begin(), recvData.begin(), recvData.size()); } recvData = recvBuffer.slice(0, recvData.size()); } return stream->tryRead(recvData.end(), 1, recvBuffer.end() - recvData.end()) .then([this](size_t actual) -> kj::Promise { if (actual == 0) { if (recvData.size() > 0) { return KJ_EXCEPTION(DISCONNECTED, "WebSocket EOF in frame header"); } else { // It's incorrect for the WebSocket to disconnect without sending `Close`. return KJ_EXCEPTION(DISCONNECTED, "WebSocket disconnected between frames without sending `Close`."); } } recvData = recvBuffer.slice(0, recvData.size() + actual); return receive(); }); } auto& recvHeader = *reinterpret_cast(recvData.begin()); recvData = recvData.slice(headerSize, recvData.size()); size_t payloadLen = recvHeader.getPayloadLen(); auto opcode = recvHeader.getOpcode(); bool isData = opcode < OPCODE_FIRST_CONTROL; if (opcode == OPCODE_CONTINUATION) { KJ_REQUIRE(!fragments.empty(), "unexpected continuation frame in WebSocket"); opcode = fragmentOpcode; } else if (isData) { KJ_REQUIRE(fragments.empty(), "expected continuation frame in WebSocket"); } bool isFin = recvHeader.isFin(); kj::Array message; // space to allocate byte* payloadTarget; // location into which to read payload (size is payloadLen) if (isFin) { // Add space for NUL terminator when allocating text message. size_t amountToAllocate = payloadLen + (opcode == OPCODE_TEXT && isFin); if (isData && !fragments.empty()) { // Final frame of a fragmented message. Gather the fragments. size_t offset = 0; for (auto& fragment: fragments) offset += fragment.size(); message = kj::heapArray(offset + amountToAllocate); offset = 0; for (auto& fragment: fragments) { memcpy(message.begin() + offset, fragment.begin(), fragment.size()); offset += fragment.size(); } payloadTarget = message.begin() + offset; fragments.clear(); fragmentOpcode = 0; } else { // Single-frame message. message = kj::heapArray(amountToAllocate); payloadTarget = message.begin(); } } else { // Fragmented message, and this isn't the final fragment. KJ_REQUIRE(isData, "WebSocket control frame cannot be fragmented"); message = kj::heapArray(payloadLen); payloadTarget = message.begin(); if (fragments.empty()) { // This is the first fragment, so set the opcode. fragmentOpcode = opcode; } } Mask mask = recvHeader.getMask(); auto handleMessage = kj::mvCapture(message, [this,opcode,payloadTarget,payloadLen,mask,isFin] (kj::Array&& message) -> kj::Promise { if (!mask.isZero()) { mask.apply(kj::arrayPtr(payloadTarget, payloadLen)); } if (!isFin) { // Add fragment to the list and loop. fragments.add(kj::mv(message)); return receive(); } switch (opcode) { case OPCODE_CONTINUATION: // Shouldn't get here; handled above. KJ_UNREACHABLE; case OPCODE_TEXT: message.back() = '\0'; return Message(kj::String(message.releaseAsChars())); case OPCODE_BINARY: return Message(message.releaseAsBytes()); case OPCODE_CLOSE: if (message.size() < 2) { return Message(Close { 1005, nullptr }); } else { uint16_t status = (static_cast(message[0]) << 8) | (static_cast(message[1]) ); return Message(Close { status, kj::heapString(message.slice(2, message.size()).asChars()) }); } case OPCODE_PING: // Send back a pong. queuePong(kj::mv(message)); return receive(); case OPCODE_PONG: // Unsolicited pong. Ignore. return receive(); default: KJ_FAIL_REQUIRE("unknown WebSocket opcode", opcode); } }); if (payloadLen <= recvData.size()) { // All data already received. memcpy(payloadTarget, recvData.begin(), payloadLen); recvData = recvData.slice(payloadLen, recvData.size()); return handleMessage(); } else { // Need to read more data. memcpy(payloadTarget, recvData.begin(), recvData.size()); size_t remaining = payloadLen - recvData.size(); auto promise = stream->tryRead(payloadTarget + recvData.size(), remaining, remaining) .then([remaining](size_t amount) { if (amount < remaining) { kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "WebSocket EOF in message")); } }); recvData = nullptr; return promise.then(kj::mv(handleMessage)); } } private: class Mask { public: Mask(): maskBytes { 0, 0, 0, 0 } {} Mask(const byte* ptr) { memcpy(maskBytes, ptr, 4); } Mask(kj::Maybe generator) { KJ_IF_MAYBE(g, generator) { g->generate(maskBytes); } else { memset(maskBytes, 0, 4); } } void apply(kj::ArrayPtr bytes) const { apply(bytes.begin(), bytes.size()); } void copyTo(byte* output) const { memcpy(output, maskBytes, 4); } bool isZero() const { return (maskBytes[0] | maskBytes[1] | maskBytes[2] | maskBytes[3]) == 0; } private: byte maskBytes[4]; void apply(byte* __restrict__ bytes, size_t size) const { for (size_t i = 0; i < size; i++) { bytes[i] ^= maskBytes[i % 4]; } } }; class Header { public: kj::ArrayPtr compose(bool fin, byte opcode, uint64_t payloadLen, Mask mask) { bytes[0] = (fin ? FIN_MASK : 0) | opcode; bool hasMask = !mask.isZero(); size_t fill; if (payloadLen < 126) { bytes[1] = (hasMask ? USE_MASK_MASK : 0) | payloadLen; if (hasMask) { mask.copyTo(bytes + 2); fill = 6; } else { fill = 2; } } else if (payloadLen < 65536) { bytes[1] = (hasMask ? USE_MASK_MASK : 0) | 126; bytes[2] = static_cast(payloadLen >> 8); bytes[3] = static_cast(payloadLen ); if (hasMask) { mask.copyTo(bytes + 4); fill = 8; } else { fill = 4; } } else { bytes[1] = (hasMask ? USE_MASK_MASK : 0) | 127; bytes[2] = static_cast(payloadLen >> 56); bytes[3] = static_cast(payloadLen >> 48); bytes[4] = static_cast(payloadLen >> 40); bytes[5] = static_cast(payloadLen >> 42); bytes[6] = static_cast(payloadLen >> 24); bytes[7] = static_cast(payloadLen >> 16); bytes[8] = static_cast(payloadLen >> 8); bytes[9] = static_cast(payloadLen ); if (hasMask) { mask.copyTo(bytes + 10); fill = 14; } else { fill = 10; } } return arrayPtr(bytes, fill); } bool isFin() const { return bytes[0] & FIN_MASK; } bool hasRsv() const { return bytes[0] & RSV_MASK; } byte getOpcode() const { return bytes[0] & OPCODE_MASK; } uint64_t getPayloadLen() const { byte payloadLen = bytes[1] & PAYLOAD_LEN_MASK; if (payloadLen == 127) { return (static_cast(bytes[2]) << 56) | (static_cast(bytes[3]) << 48) | (static_cast(bytes[4]) << 40) | (static_cast(bytes[5]) << 32) | (static_cast(bytes[6]) << 24) | (static_cast(bytes[7]) << 16) | (static_cast(bytes[8]) << 8) | (static_cast(bytes[9]) ); } else if (payloadLen == 126) { return (static_cast(bytes[2]) << 8) | (static_cast(bytes[3]) ); } else { return payloadLen; } } Mask getMask() const { if (bytes[1] & USE_MASK_MASK) { byte payloadLen = bytes[1] & PAYLOAD_LEN_MASK; if (payloadLen == 127) { return Mask(bytes + 10); } else if (payloadLen == 126) { return Mask(bytes + 4); } else { return Mask(bytes + 2); } } else { return Mask(); } } static size_t headerSize(byte const* bytes, size_t sizeSoFar) { if (sizeSoFar < 2) return 2; size_t required = 2; if (bytes[1] & USE_MASK_MASK) { required += 4; } byte payloadLen = bytes[1] & PAYLOAD_LEN_MASK; if (payloadLen == 127) { required += 8; } else if (payloadLen == 126) { required += 2; } return required; } private: byte bytes[14]; static constexpr byte FIN_MASK = 0x80; static constexpr byte RSV_MASK = 0x70; static constexpr byte OPCODE_MASK = 0x0f; static constexpr byte USE_MASK_MASK = 0x80; static constexpr byte PAYLOAD_LEN_MASK = 0x7f; }; static constexpr byte OPCODE_CONTINUATION = 0; static constexpr byte OPCODE_TEXT = 1; static constexpr byte OPCODE_BINARY = 2; static constexpr byte OPCODE_CLOSE = 8; static constexpr byte OPCODE_PING = 9; static constexpr byte OPCODE_PONG = 10; static constexpr byte OPCODE_FIRST_CONTROL = 8; // --------------------------------------------------------------------------- kj::Own stream; kj::Maybe maskKeyGenerator; bool hasSentClose = false; bool disconnected = false; bool currentlySending = false; Header sendHeader; kj::ArrayPtr sendParts[2]; kj::Maybe> queuedPong; // If a Ping is received while currentlySending is true, then queuedPong is set to the body of // a pong message that should be sent once the current send is complete. kj::Maybe> sendingPong; // If a Pong is being sent asynchronously in response to a Ping, this is a promise for the // completion of that send. // // Additionally, this member is used if we need to block our first send on WebSocket startup, // e.g. because we need to wait for HTTP handshake writes to flush before we can start sending // WebSocket data. `sendingPong` was overloaded for this use case because the logic is the same. // Perhaps it should be renamed to `blockSend` or `writeQueue`. uint fragmentOpcode = 0; kj::Vector> fragments; // If `fragments` is non-empty, we've already received some fragments of a message. // `fragmentOpcode` is the original opcode. kj::Array recvBuffer; kj::ArrayPtr recvData; kj::Promise sendImpl(byte opcode, kj::ArrayPtr message) { KJ_REQUIRE(!disconnected, "WebSocket can't send after disconnect()"); KJ_REQUIRE(!currentlySending, "another message send is already in progress"); currentlySending = true; KJ_IF_MAYBE(p, sendingPong) { // We recently sent a pong, make sure it's finished before proceeding. auto promise = p->then([this, opcode, message]() { currentlySending = false; return sendImpl(opcode, message); }); sendingPong = nullptr; return promise; } // We don't stop the application from sending further messages after close() -- this is the // application's error to make. But, we do want to make sure we don't send any PONGs after a // close, since that would be our error. So we stack whether we closed for that reason. hasSentClose = hasSentClose || opcode == OPCODE_CLOSE; Mask mask(maskKeyGenerator); kj::Array ownMessage; if (!mask.isZero()) { // Sadness, we have to make a copy to apply the mask. ownMessage = kj::heapArray(message); mask.apply(ownMessage); message = ownMessage; } sendParts[0] = sendHeader.compose(true, opcode, message.size(), mask); sendParts[1] = message; auto promise = stream->write(sendParts); if (!mask.isZero()) { promise = promise.attach(kj::mv(ownMessage)); } return promise.then([this]() { currentlySending = false; // Send queued pong if needed. KJ_IF_MAYBE(q, queuedPong) { kj::Array payload = kj::mv(*q); queuedPong = nullptr; queuePong(kj::mv(payload)); } }); } void queuePong(kj::Array payload) { if (currentlySending) { // There is a message-send in progress, so we cannot write to the stream now. // // Note: According to spec, if the server receives a second ping before responding to the // previous one, it can opt to respond only to the last ping. So we don't have to check if // queuedPong is already non-null. queuedPong = kj::mv(payload); } else KJ_IF_MAYBE(promise, sendingPong) { // We're still sending a previous pong. Wait for it to finish before sending ours. sendingPong = promise->then(kj::mvCapture(payload, [this](kj::Array payload) mutable { return sendPong(kj::mv(payload)); })); } else { // We're not sending any pong currently. sendingPong = sendPong(kj::mv(payload)); } } kj::Promise sendPong(kj::Array payload) { if (hasSentClose || disconnected) { return kj::READY_NOW; } sendParts[0] = sendHeader.compose(true, OPCODE_PONG, payload.size(), Mask(maskKeyGenerator)); sendParts[1] = payload; return stream->write(sendParts).attach(kj::mv(payload)); } }; kj::Own upgradeToWebSocket( kj::Own stream, HttpInputStreamImpl& httpInput, HttpOutputStream& httpOutput, kj::Maybe maskKeyGenerator) { // Create a WebSocket upgraded from an HTTP stream. auto releasedBuffer = httpInput.releaseBuffer(); return kj::heap(kj::mv(stream), maskKeyGenerator, kj::mv(releasedBuffer.buffer), releasedBuffer.leftover, httpOutput.flush()); } } // namespace kj::Own newWebSocket(kj::Own stream, kj::Maybe maskKeyGenerator) { return kj::heap(kj::mv(stream), maskKeyGenerator); } static kj::Promise pumpWebSocketLoop(WebSocket& from, WebSocket& to) { return from.receive().then([&from,&to](WebSocket::Message&& message) { KJ_SWITCH_ONEOF(message) { KJ_CASE_ONEOF(text, kj::String) { return to.send(text) .attach(kj::mv(text)) .then([&from,&to]() { return pumpWebSocketLoop(from, to); }); } KJ_CASE_ONEOF(data, kj::Array) { return to.send(data) .attach(kj::mv(data)) .then([&from,&to]() { return pumpWebSocketLoop(from, to); }); } KJ_CASE_ONEOF(close, WebSocket::Close) { // Once a close has passed through, the pump is complete. return to.close(close.code, close.reason) .attach(kj::mv(close)); } } KJ_UNREACHABLE; }, [&to](kj::Exception&& e) { if (e.getType() == kj::Exception::Type::DISCONNECTED) { return to.disconnect(); } else { return to.close(1002, e.getDescription()); } }); } kj::Promise WebSocket::pumpTo(WebSocket& other) { KJ_IF_MAYBE(p, other.tryPumpFrom(*this)) { // Yay, optimized pump! return kj::mv(*p); } else { // Fall back to default implementation. return kj::evalNow([&]() { auto cancelPromise = other.whenAborted().then([this]() -> kj::Promise { this->abort(); return KJ_EXCEPTION(DISCONNECTED, "destination of WebSocket pump disconnected prematurely"); }); return pumpWebSocketLoop(*this, other).exclusiveJoin(kj::mv(cancelPromise)); }); } } kj::Maybe> WebSocket::tryPumpFrom(WebSocket& other) { return nullptr; } namespace { class WebSocketPipeImpl final: public WebSocket, public kj::Refcounted { // Represents one direction of a WebSocket pipe. // // This class behaves as a "loopback" WebSocket: a message sent using send() is received using // receive(), on the same object. This is *not* how WebSocket implementations usually behave. // But, this object is actually used to implement only one direction of a bidirectional pipe. At // another layer above this, the pipe is actually composed of two WebSocketPipeEnd instances, // which layer on top of two WebSocketPipeImpl instances representing the two directions. So, // send() calls on a WebSocketPipeImpl instance always come from one of the two WebSocketPipeEnds // while receive() calls come from the other end. public: ~WebSocketPipeImpl() noexcept(false) { KJ_REQUIRE(state == nullptr || ownState.get() != nullptr, "destroying WebSocketPipe with operation still in-progress; probably going to segfault") { // Don't std::terminate(). break; } } void abort() override { KJ_IF_MAYBE(s, state) { s->abort(); } else { ownState = heap(); state = *ownState; aborted = true; KJ_IF_MAYBE(f, abortedFulfiller) { f->get()->fulfill(); abortedFulfiller = nullptr; } } } kj::Promise send(kj::ArrayPtr message) override { KJ_IF_MAYBE(s, state) { return s->send(message); } else { return newAdaptedPromise(*this, MessagePtr(message)); } } kj::Promise send(kj::ArrayPtr message) override { KJ_IF_MAYBE(s, state) { return s->send(message); } else { return newAdaptedPromise(*this, MessagePtr(message)); } } kj::Promise close(uint16_t code, kj::StringPtr reason) override { KJ_IF_MAYBE(s, state) { return s->close(code, reason); } else { return newAdaptedPromise(*this, MessagePtr(ClosePtr { code, reason })); } } kj::Promise disconnect() override { KJ_IF_MAYBE(s, state) { return s->disconnect(); } else { ownState = heap(); state = *ownState; return kj::READY_NOW; } } kj::Promise whenAborted() override { if (aborted) { return kj::READY_NOW; } else KJ_IF_MAYBE(p, abortedPromise) { return p->addBranch(); } else { auto paf = newPromiseAndFulfiller(); abortedFulfiller = kj::mv(paf.fulfiller); auto fork = paf.promise.fork(); auto result = fork.addBranch(); abortedPromise = kj::mv(fork); return result; } } kj::Maybe> tryPumpFrom(WebSocket& other) override { KJ_IF_MAYBE(s, state) { return s->tryPumpFrom(other); } else { return newAdaptedPromise(*this, other); } } kj::Promise receive() override { KJ_IF_MAYBE(s, state) { return s->receive(); } else { return newAdaptedPromise(*this); } } kj::Promise pumpTo(WebSocket& other) override { KJ_IF_MAYBE(s, state) { return s->pumpTo(other); } else { return newAdaptedPromise(*this, other); } } private: kj::Maybe state; // Object-oriented state! If any method call is blocked waiting on activity from the other end, // then `state` is non-null and method calls should be forwarded to it. If no calls are // outstanding, `state` is null. kj::Own ownState; bool aborted = false; Maybe>> abortedFulfiller = nullptr; Maybe> abortedPromise = nullptr; void endState(WebSocket& obj) { KJ_IF_MAYBE(s, state) { if (s == &obj) { state = nullptr; } } } struct ClosePtr { uint16_t code; kj::StringPtr reason; }; typedef kj::OneOf, kj::ArrayPtr, ClosePtr> MessagePtr; class BlockedSend final: public WebSocket { public: BlockedSend(kj::PromiseFulfiller& fulfiller, WebSocketPipeImpl& pipe, MessagePtr message) : fulfiller(fulfiller), pipe(pipe), message(kj::mv(message)) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedSend() noexcept(false) { pipe.endState(*this); } void abort() override { canceler.cancel("other end of WebSocketPipe was destroyed"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed")); pipe.endState(*this); pipe.abort(); } kj::Promise whenAborted() override { KJ_FAIL_ASSERT("can't get here -- implemented by WebSocketPipeImpl"); } kj::Promise send(kj::ArrayPtr message) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise send(kj::ArrayPtr message) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise disconnect() override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Maybe> tryPumpFrom(WebSocket& other) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise receive() override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); fulfiller.fulfill(); pipe.endState(*this); KJ_SWITCH_ONEOF(message) { KJ_CASE_ONEOF(arr, kj::ArrayPtr) { return Message(kj::str(arr)); } KJ_CASE_ONEOF(arr, kj::ArrayPtr) { auto copy = kj::heapArray(arr.size()); memcpy(copy.begin(), arr.begin(), arr.size()); return Message(kj::mv(copy)); } KJ_CASE_ONEOF(close, ClosePtr) { return Message(Close { close.code, kj::str(close.reason) }); } } KJ_UNREACHABLE; } kj::Promise pumpTo(WebSocket& other) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); kj::Promise promise = nullptr; KJ_SWITCH_ONEOF(message) { KJ_CASE_ONEOF(arr, kj::ArrayPtr) { promise = other.send(arr); } KJ_CASE_ONEOF(arr, kj::ArrayPtr) { promise = other.send(arr); } KJ_CASE_ONEOF(close, ClosePtr) { promise = other.close(close.code, close.reason); } } return canceler.wrap(promise.then([this,&other]() { canceler.release(); fulfiller.fulfill(); pipe.endState(*this); return pipe.pumpTo(other); }, [this](kj::Exception&& e) -> kj::Promise { canceler.release(); fulfiller.reject(kj::cp(e)); pipe.endState(*this); return kj::mv(e); })); } private: kj::PromiseFulfiller& fulfiller; WebSocketPipeImpl& pipe; MessagePtr message; Canceler canceler; }; class BlockedPumpFrom final: public WebSocket { public: BlockedPumpFrom(kj::PromiseFulfiller& fulfiller, WebSocketPipeImpl& pipe, WebSocket& input) : fulfiller(fulfiller), pipe(pipe), input(input) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedPumpFrom() noexcept(false) { pipe.endState(*this); } void abort() override { canceler.cancel("other end of WebSocketPipe was destroyed"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed")); pipe.endState(*this); pipe.abort(); } kj::Promise whenAborted() override { KJ_FAIL_ASSERT("can't get here -- implemented by WebSocketPipeImpl"); } kj::Promise send(kj::ArrayPtr message) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise send(kj::ArrayPtr message) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise disconnect() override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Maybe> tryPumpFrom(WebSocket& other) override { KJ_FAIL_ASSERT("another message send is already in progress"); } kj::Promise receive() override { KJ_REQUIRE(canceler.isEmpty(), "another message receive is already in progress"); return canceler.wrap(input.receive() .then([this](Message message) { if (message.is()) { canceler.release(); fulfiller.fulfill(); pipe.endState(*this); } return kj::mv(message); }, [this](kj::Exception&& e) -> Message { canceler.release(); fulfiller.reject(kj::cp(e)); pipe.endState(*this); kj::throwRecoverableException(kj::mv(e)); return Message(kj::String()); })); } kj::Promise pumpTo(WebSocket& other) override { KJ_REQUIRE(canceler.isEmpty(), "another message receive is already in progress"); return canceler.wrap(input.pumpTo(other) .then([this]() { canceler.release(); fulfiller.fulfill(); pipe.endState(*this); }, [this](kj::Exception&& e) { canceler.release(); fulfiller.reject(kj::cp(e)); pipe.endState(*this); kj::throwRecoverableException(kj::mv(e)); })); } private: kj::PromiseFulfiller& fulfiller; WebSocketPipeImpl& pipe; WebSocket& input; Canceler canceler; }; class BlockedReceive final: public WebSocket { public: BlockedReceive(kj::PromiseFulfiller& fulfiller, WebSocketPipeImpl& pipe) : fulfiller(fulfiller), pipe(pipe) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedReceive() noexcept(false) { pipe.endState(*this); } void abort() override { canceler.cancel("other end of WebSocketPipe was destroyed"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed")); pipe.endState(*this); pipe.abort(); } kj::Promise whenAborted() override { KJ_FAIL_ASSERT("can't get here -- implemented by WebSocketPipeImpl"); } kj::Promise send(kj::ArrayPtr message) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto copy = kj::heapArray(message.size()); memcpy(copy.begin(), message.begin(), message.size()); fulfiller.fulfill(Message(kj::mv(copy))); pipe.endState(*this); return kj::READY_NOW; } kj::Promise send(kj::ArrayPtr message) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); fulfiller.fulfill(Message(kj::str(message))); pipe.endState(*this); return kj::READY_NOW; } kj::Promise close(uint16_t code, kj::StringPtr reason) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); fulfiller.fulfill(Message(Close { code, kj::str(reason) })); pipe.endState(*this); return kj::READY_NOW; } kj::Promise disconnect() override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "WebSocket disconnected")); pipe.endState(*this); return pipe.disconnect(); } kj::Maybe> tryPumpFrom(WebSocket& other) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); return canceler.wrap(other.receive().then([this,&other](Message message) { canceler.release(); fulfiller.fulfill(kj::mv(message)); pipe.endState(*this); return other.pumpTo(pipe); }, [this](kj::Exception&& e) -> kj::Promise { canceler.release(); fulfiller.reject(kj::cp(e)); pipe.endState(*this); return kj::mv(e); })); } kj::Promise receive() override { KJ_FAIL_ASSERT("another message receive is already in progress"); } kj::Promise pumpTo(WebSocket& other) override { KJ_FAIL_ASSERT("another message receive is already in progress"); } private: kj::PromiseFulfiller& fulfiller; WebSocketPipeImpl& pipe; Canceler canceler; }; class BlockedPumpTo final: public WebSocket { public: BlockedPumpTo(kj::PromiseFulfiller& fulfiller, WebSocketPipeImpl& pipe, WebSocket& output) : fulfiller(fulfiller), pipe(pipe), output(output) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedPumpTo() noexcept(false) { pipe.endState(*this); } void abort() override { canceler.cancel("other end of WebSocketPipe was destroyed"); // abort() is called when the pipe end is dropped. This should be treated as disconnecting, // so pumpTo() should complete normally. fulfiller.fulfill(); pipe.endState(*this); pipe.abort(); } kj::Promise whenAborted() override { KJ_FAIL_ASSERT("can't get here -- implemented by WebSocketPipeImpl"); } kj::Promise send(kj::ArrayPtr message) override { KJ_REQUIRE(canceler.isEmpty(), "another message send is already in progress"); return canceler.wrap(output.send(message)); } kj::Promise send(kj::ArrayPtr message) override { KJ_REQUIRE(canceler.isEmpty(), "another message send is already in progress"); return canceler.wrap(output.send(message)); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { KJ_REQUIRE(canceler.isEmpty(), "another message send is already in progress"); return canceler.wrap(output.close(code, reason).then([this]() { // A pump is expected to end upon seeing a Close message. canceler.release(); pipe.endState(*this); fulfiller.fulfill(); })); } kj::Promise disconnect() override { KJ_REQUIRE(canceler.isEmpty(), "another message send is already in progress"); return canceler.wrap(output.disconnect().then([this]() { canceler.release(); pipe.endState(*this); fulfiller.fulfill(); return pipe.disconnect(); })); } kj::Maybe> tryPumpFrom(WebSocket& other) override { KJ_REQUIRE(canceler.isEmpty(), "another message send is already in progress"); return canceler.wrap(other.pumpTo(output).then([this]() { canceler.release(); pipe.endState(*this); fulfiller.fulfill(); })); } kj::Promise receive() override { KJ_FAIL_ASSERT("another message receive is already in progress"); } kj::Promise pumpTo(WebSocket& other) override { KJ_FAIL_ASSERT("another message receive is already in progress"); } private: kj::PromiseFulfiller& fulfiller; WebSocketPipeImpl& pipe; WebSocket& output; Canceler canceler; }; class Disconnected final: public WebSocket { public: void abort() override { // can ignore } kj::Promise whenAborted() override { KJ_FAIL_ASSERT("can't get here -- implemented by WebSocketPipeImpl"); } kj::Promise send(kj::ArrayPtr message) override { KJ_FAIL_REQUIRE("can't send() after disconnect()"); } kj::Promise send(kj::ArrayPtr message) override { KJ_FAIL_REQUIRE("can't send() after disconnect()"); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { KJ_FAIL_REQUIRE("can't close() after disconnect()"); } kj::Promise disconnect() override { return kj::READY_NOW; } kj::Maybe> tryPumpFrom(WebSocket& other) override { KJ_FAIL_REQUIRE("can't tryPumpFrom() after disconnect()"); } kj::Promise receive() override { return KJ_EXCEPTION(DISCONNECTED, "WebSocket disconnected"); } kj::Promise pumpTo(WebSocket& other) override { return kj::READY_NOW; } }; class Aborted final: public WebSocket { public: void abort() override { // can ignore } kj::Promise whenAborted() override { KJ_FAIL_ASSERT("can't get here -- implemented by WebSocketPipeImpl"); } kj::Promise send(kj::ArrayPtr message) override { return KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed"); } kj::Promise send(kj::ArrayPtr message) override { return KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed"); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { return KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed"); } kj::Promise disconnect() override { return KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed"); } kj::Maybe> tryPumpFrom(WebSocket& other) override { return kj::Promise(KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed")); } kj::Promise receive() override { return KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed"); } kj::Promise pumpTo(WebSocket& other) override { return KJ_EXCEPTION(DISCONNECTED, "other end of WebSocketPipe was destroyed"); } }; }; class WebSocketPipeEnd final: public WebSocket { public: WebSocketPipeEnd(kj::Own in, kj::Own out) : in(kj::mv(in)), out(kj::mv(out)) {} ~WebSocketPipeEnd() noexcept(false) { in->abort(); out->abort(); } kj::Promise send(kj::ArrayPtr message) override { return out->send(message); } kj::Promise send(kj::ArrayPtr message) override { return out->send(message); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { return out->close(code, reason); } kj::Promise disconnect() override { return out->disconnect(); } void abort() override { in->abort(); out->abort(); } kj::Promise whenAborted() override { return out->whenAborted(); } kj::Maybe> tryPumpFrom(WebSocket& other) override { return out->tryPumpFrom(other); } kj::Promise receive() override { return in->receive(); } kj::Promise pumpTo(WebSocket& other) override { return in->pumpTo(other); } private: kj::Own in; kj::Own out; }; } // namespace WebSocketPipe newWebSocketPipe() { auto pipe1 = kj::refcounted(); auto pipe2 = kj::refcounted(); auto end1 = kj::heap(kj::addRef(*pipe1), kj::addRef(*pipe2)); auto end2 = kj::heap(kj::mv(pipe2), kj::mv(pipe1)); return { { kj::mv(end1), kj::mv(end2) } }; } // ======================================================================================= namespace { class HttpClientImpl final: public HttpClient { public: HttpClientImpl(HttpHeaderTable& responseHeaderTable, kj::Own rawStream, HttpClientSettings settings) : httpInput(*rawStream, responseHeaderTable), httpOutput(*rawStream), ownStream(kj::mv(rawStream)), settings(kj::mv(settings)) {} bool canReuse() { // Returns true if we can immediately reuse this HttpClient for another message (so all // previous messages have been fully read). return !upgraded && !closed && httpInput.canReuse() && httpOutput.canReuse(); } Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { KJ_REQUIRE(!upgraded, "can't make further requests on this HttpClient because it has been or is in the process " "of being upgraded"); KJ_REQUIRE(!closed, "this HttpClient's connection has been closed by the server or due to an error"); KJ_REQUIRE(httpOutput.canReuse(), "can't start new request until previous request body has been fully written"); closeWatcherTask = nullptr; kj::StringPtr connectionHeaders[HttpHeaders::CONNECTION_HEADERS_COUNT]; kj::String lengthStr; bool isGet = method == HttpMethod::GET || method == HttpMethod::HEAD; bool hasBody; KJ_IF_MAYBE(s, expectedBodySize) { if (isGet && *s == 0) { // GET with empty body; don't send any Content-Length. hasBody = false; } else { lengthStr = kj::str(*s); connectionHeaders[HttpHeaders::BuiltinIndices::CONTENT_LENGTH] = lengthStr; hasBody = true; } } else { if (isGet && headers.get(HttpHeaderId::TRANSFER_ENCODING) == nullptr) { // GET with empty body; don't send any Transfer-Encoding. hasBody = false; } else { // HACK: Normally GET requests shouldn't have bodies. But, if the caller set a // Transfer-Encoding header on a GET, we use this as a special signal that it might // actually want to send a body. This allows pass-through of a GET request with a chunked // body to "just work". We strongly discourage writing any new code that sends // full-bodied GETs. connectionHeaders[HttpHeaders::BuiltinIndices::TRANSFER_ENCODING] = "chunked"; hasBody = true; } } httpOutput.writeHeaders(headers.serializeRequest(method, url, connectionHeaders)); kj::Own bodyStream; if (!hasBody) { // No entity-body. httpOutput.finishBody(); bodyStream = heap(); } else KJ_IF_MAYBE(s, expectedBodySize) { bodyStream = heap(httpOutput, *s); } else { bodyStream = heap(httpOutput); } auto id = ++counter; auto responsePromise = httpInput.readResponseHeaders().then( [this,method,id](HttpHeaders::ResponseOrProtocolError&& responseOrProtocolError) -> HttpClient::Response { KJ_SWITCH_ONEOF(responseOrProtocolError) { KJ_CASE_ONEOF(response, HttpHeaders::Response) { auto& responseHeaders = httpInput.getHeaders(); HttpClient::Response result { response.statusCode, response.statusText, &responseHeaders, httpInput.getEntityBody( HttpInputStreamImpl::RESPONSE, method, response.statusCode, responseHeaders) }; if (fastCaseCmp<'c', 'l', 'o', 's', 'e'>( responseHeaders.get(HttpHeaderId::CONNECTION).orDefault(nullptr).cStr())) { closed = true; } else if (counter == id) { watchForClose(); } else { // Another request was already queued after this one, so we don't want to watch for // stream closure because we're fully expecting another response. } return result; } KJ_CASE_ONEOF(protocolError, HttpHeaders::ProtocolError) { closed = true; // TODO(someday): Do something with ProtocolError::rawContent. Exceptions feel like the // most idiomatic way to report errors when using HttpClient::request(), but we don't // have a good way of attaching the raw content to the exception. KJ_FAIL_REQUIRE(protocolError.description) { break; } return HttpClient::Response(); } } KJ_UNREACHABLE; }); return { kj::mv(bodyStream), kj::mv(responsePromise) }; } kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers) override { KJ_REQUIRE(!upgraded, "can't make further requests on this HttpClient because it has been or is in the process " "of being upgraded"); KJ_REQUIRE(!closed, "this HttpClient's connection has been closed by the server or due to an error"); closeWatcherTask = nullptr; // Mark upgraded for now, even though the upgrade could fail, because we can't allow pipelined // requests in the meantime. upgraded = true; byte keyBytes[16]; KJ_ASSERT_NONNULL(settings.entropySource, "can't use openWebSocket() because no EntropySource was provided when creating the " "HttpClient").generate(keyBytes); auto keyBase64 = kj::encodeBase64(keyBytes); kj::StringPtr connectionHeaders[HttpHeaders::WEBSOCKET_CONNECTION_HEADERS_COUNT]; connectionHeaders[HttpHeaders::BuiltinIndices::CONNECTION] = "Upgrade"; connectionHeaders[HttpHeaders::BuiltinIndices::UPGRADE] = "websocket"; connectionHeaders[HttpHeaders::BuiltinIndices::SEC_WEBSOCKET_VERSION] = "13"; connectionHeaders[HttpHeaders::BuiltinIndices::SEC_WEBSOCKET_KEY] = keyBase64; httpOutput.writeHeaders(headers.serializeRequest(HttpMethod::GET, url, connectionHeaders)); // No entity-body. httpOutput.finishBody(); auto id = ++counter; return httpInput.readResponseHeaders() .then([this,id,keyBase64 = kj::mv(keyBase64)]( HttpHeaders::ResponseOrProtocolError&& responseOrProtocolError) -> HttpClient::WebSocketResponse { KJ_SWITCH_ONEOF(responseOrProtocolError) { KJ_CASE_ONEOF(response, HttpHeaders::Response) { auto& responseHeaders = httpInput.getHeaders(); if (response.statusCode == 101) { if (!fastCaseCmp<'w', 'e', 'b', 's', 'o', 'c', 'k', 'e', 't'>( responseHeaders.get(HttpHeaderId::UPGRADE).orDefault(nullptr).cStr())) { KJ_FAIL_REQUIRE("server returned incorrect Upgrade header; should be 'websocket'", responseHeaders.get(HttpHeaderId::UPGRADE).orDefault("(null)")) { break; } return HttpClient::WebSocketResponse(); } auto expectedAccept = generateWebSocketAccept(keyBase64); if (responseHeaders.get(HttpHeaderId::SEC_WEBSOCKET_ACCEPT).orDefault(nullptr) != expectedAccept) { KJ_FAIL_REQUIRE("server returned incorrect Sec-WebSocket-Accept header", responseHeaders.get(HttpHeaderId::SEC_WEBSOCKET_ACCEPT).orDefault("(null)"), expectedAccept) { break; } return HttpClient::WebSocketResponse(); } return { response.statusCode, response.statusText, &httpInput.getHeaders(), upgradeToWebSocket(kj::mv(ownStream), httpInput, httpOutput, settings.entropySource), }; } else { upgraded = false; HttpClient::WebSocketResponse result { response.statusCode, response.statusText, &responseHeaders, httpInput.getEntityBody(HttpInputStreamImpl::RESPONSE, HttpMethod::GET, response.statusCode, responseHeaders) }; if (fastCaseCmp<'c', 'l', 'o', 's', 'e'>( responseHeaders.get(HttpHeaderId::CONNECTION).orDefault(nullptr).cStr())) { closed = true; } else if (counter == id) { watchForClose(); } else { // Another request was already queued after this one, so we don't want to watch for // stream closure because we're fully expecting another response. } return result; } } KJ_CASE_ONEOF(protocolError, HttpHeaders::ProtocolError) { // TODO(someday): Do something with ProtocolError::rawContent. Exceptions feel like the // most idiomatic way to report errors when using HttpClient::request(), but we don't // have a good way of attaching the raw content to the exception. KJ_FAIL_REQUIRE(protocolError.description) { break; } return HttpClient::WebSocketResponse(); } } KJ_UNREACHABLE; }); } private: HttpInputStreamImpl httpInput; HttpOutputStream httpOutput; kj::Own ownStream; HttpClientSettings settings; kj::Maybe> closeWatcherTask; bool upgraded = false; bool closed = false; uint counter = 0; // Counts requests for the sole purpose of detecting if more requests have been made after some // point in history. void watchForClose() { closeWatcherTask = httpInput.awaitNextMessage() .then([this](bool hasData) -> kj::Promise { if (hasData) { // Uhh... The server sent some data before we asked for anything. Perhaps due to properties // of this application, the server somehow already knows what the next request will be, and // it is trying to optimize. Or maybe this is some sort of test and the server is just // replaying a script. In any case, we will humor it -- leave the data in the buffer and // let it become the response to the next request. return kj::READY_NOW; } else { // EOF -- server disconnected. closed = true; if (httpOutput.isInBody()) { // Huh, the application is still sending a request. We should let it finish. We do not // need to proactively free the socket in this case because we know that we're not // sitting in a reusable connection pool, because we know the application is still // actively using the connection. return kj::READY_NOW; } else { return httpOutput.flush().then([this]() { // We might be sitting in NetworkAddressHttpClient's `availableClients` pool. We don't // have a way to notify it to remove this client from the pool; instead, when it tries // to pull this client from the pool later, it will notice the client is dead and will // discard it then. But, we would like to avoid holding on to a socket forever. So, // destroy the socket now. // TODO(cleanup): Maybe we should arrange to proactively remove ourselves? Seems // like the code will be awkward. ownStream = nullptr; }); } } }).eagerlyEvaluate(nullptr); } }; } // namespace kj::Promise HttpClient::openWebSocket( kj::StringPtr url, const HttpHeaders& headers) { return request(HttpMethod::GET, url, headers, nullptr) .response.then([](HttpClient::Response&& response) -> WebSocketResponse { kj::OneOf, kj::Own> body; body.init>(kj::mv(response.body)); return { response.statusCode, response.statusText, response.headers, kj::mv(body) }; }); } kj::Promise> HttpClient::connect(kj::StringPtr host) { KJ_UNIMPLEMENTED("CONNECT is not implemented by this HttpClient"); } kj::Own newHttpClient( HttpHeaderTable& responseHeaderTable, kj::AsyncIoStream& stream, HttpClientSettings settings) { return kj::heap(responseHeaderTable, kj::Own(&stream, kj::NullDisposer::instance), kj::mv(settings)); } // ======================================================================================= namespace { class NetworkAddressHttpClient final: public HttpClient { public: NetworkAddressHttpClient(kj::Timer& timer, HttpHeaderTable& responseHeaderTable, kj::Own address, HttpClientSettings settings) : timer(timer), responseHeaderTable(responseHeaderTable), address(kj::mv(address)), settings(kj::mv(settings)) {} bool isDrained() { // Returns true if there are no open connections. return activeConnectionCount == 0 && availableClients.empty(); } kj::Promise onDrained() { // Returns a promise which resolves the next time isDrained() transitions from false to true. auto paf = kj::newPromiseAndFulfiller(); drainedFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { auto refcounted = getClient(); auto result = refcounted->client->request(method, url, headers, expectedBodySize); result.body = result.body.attach(kj::addRef(*refcounted)); result.response = result.response.then(kj::mvCapture(refcounted, [](kj::Own&& refcounted, Response&& response) { response.body = response.body.attach(kj::mv(refcounted)); return kj::mv(response); })); return result; } kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers) override { auto refcounted = getClient(); auto result = refcounted->client->openWebSocket(url, headers); return result.then(kj::mvCapture(refcounted, [](kj::Own&& refcounted, WebSocketResponse&& response) { KJ_SWITCH_ONEOF(response.webSocketOrBody) { KJ_CASE_ONEOF(body, kj::Own) { response.webSocketOrBody = body.attach(kj::mv(refcounted)); } KJ_CASE_ONEOF(ws, kj::Own) { // The only reason we need to attach the client to the WebSocket is because otherwise // the response headers will be deleted prematurely. Otherwise, the WebSocket has taken // ownership of the connection. // // TODO(perf): Maybe we could transfer ownership of the response headers specifically? response.webSocketOrBody = ws.attach(kj::mv(refcounted)); } } return kj::mv(response); })); } private: kj::Timer& timer; HttpHeaderTable& responseHeaderTable; kj::Own address; HttpClientSettings settings; kj::Maybe>> drainedFulfiller; uint activeConnectionCount = 0; bool timeoutsScheduled = false; kj::Promise timeoutTask = nullptr; struct AvailableClient { kj::Own client; kj::TimePoint expires; }; std::deque availableClients; struct RefcountedClient final: public kj::Refcounted { RefcountedClient(NetworkAddressHttpClient& parent, kj::Own client) : parent(parent), client(kj::mv(client)) { ++parent.activeConnectionCount; } ~RefcountedClient() noexcept(false) { --parent.activeConnectionCount; KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { parent.returnClientToAvailable(kj::mv(client)); })) { KJ_LOG(ERROR, *exception); } } NetworkAddressHttpClient& parent; kj::Own client; }; kj::Own getClient() { for (;;) { if (availableClients.empty()) { auto stream = newPromisedStream(address->connect()); return kj::refcounted(*this, kj::heap(responseHeaderTable, kj::mv(stream), settings)); } else { auto client = kj::mv(availableClients.back().client); availableClients.pop_back(); if (client->canReuse()) { return kj::refcounted(*this, kj::mv(client)); } // Whoops, this client's connection was closed by the server at some point. Discard. } } } void returnClientToAvailable(kj::Own client) { // Only return the connection to the pool if it is reusable and if our settings indicate we // should reuse connections. if (client->canReuse() && settings.idleTimeout > 0 * kj::SECONDS) { availableClients.push_back(AvailableClient { kj::mv(client), timer.now() + settings.idleTimeout }); } // Call this either way because it also signals onDrained(). if (!timeoutsScheduled) { timeoutsScheduled = true; timeoutTask = applyTimeouts(); } } kj::Promise applyTimeouts() { if (availableClients.empty()) { timeoutsScheduled = false; if (activeConnectionCount == 0) { KJ_IF_MAYBE(f, drainedFulfiller) { f->get()->fulfill(); drainedFulfiller = nullptr; } } return kj::READY_NOW; } else { auto time = availableClients.front().expires; return timer.atTime(time).then([this,time]() { while (!availableClients.empty() && availableClients.front().expires <= time) { availableClients.pop_front(); } return applyTimeouts(); }); } } }; class PromiseNetworkAddressHttpClient final: public HttpClient { // An HttpClient which waits for a promise to resolve then forwards all calls to the promised // client. public: PromiseNetworkAddressHttpClient(kj::Promise> promise) : promise(promise.then([this](kj::Own&& client) { this->client = kj::mv(client); }).fork()) {} bool isDrained() { KJ_IF_MAYBE(c, client) { return c->get()->isDrained(); } else { return failed; } } kj::Promise onDrained() { KJ_IF_MAYBE(c, client) { return c->get()->onDrained(); } else { return promise.addBranch().then([this]() { return KJ_ASSERT_NONNULL(client)->onDrained(); }, [this](kj::Exception&& e) { // Connecting failed. Treat as immediately drained. failed = true; return kj::READY_NOW; }); } } Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { KJ_IF_MAYBE(c, client) { return c->get()->request(method, url, headers, expectedBodySize); } else { // This gets complicated since request() returns a pair of a stream and a promise. auto urlCopy = kj::str(url); auto headersCopy = headers.clone(); auto combined = promise.addBranch().then(kj::mvCapture(urlCopy, kj::mvCapture(headersCopy, [this,method,expectedBodySize](HttpHeaders&& headers, kj::String&& url) -> kj::Tuple, kj::Promise> { auto req = KJ_ASSERT_NONNULL(client)->request(method, url, headers, expectedBodySize); return kj::tuple(kj::mv(req.body), kj::mv(req.response)); }))); auto split = combined.split(); return { newPromisedStream(kj::mv(kj::get<0>(split))), kj::mv(kj::get<1>(split)) }; } } kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers) override { KJ_IF_MAYBE(c, client) { return c->get()->openWebSocket(url, headers); } else { auto urlCopy = kj::str(url); auto headersCopy = headers.clone(); return promise.addBranch().then(kj::mvCapture(urlCopy, kj::mvCapture(headersCopy, [this](HttpHeaders&& headers, kj::String&& url) { return KJ_ASSERT_NONNULL(client)->openWebSocket(url, headers); }))); } } private: kj::ForkedPromise promise; kj::Maybe> client; bool failed = false; }; class NetworkHttpClient final: public HttpClient, private kj::TaskSet::ErrorHandler { public: NetworkHttpClient(kj::Timer& timer, HttpHeaderTable& responseHeaderTable, kj::Network& network, kj::Maybe tlsNetwork, HttpClientSettings settings) : timer(timer), responseHeaderTable(responseHeaderTable), network(network), tlsNetwork(tlsNetwork), settings(kj::mv(settings)), tasks(*this) {} Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { // We need to parse the proxy-style URL to convert it to host-style. // Use URL parsing options that avoid unnecessary rewrites. Url::Options urlOptions; urlOptions.allowEmpty = true; urlOptions.percentDecode = false; auto parsed = Url::parse(url, Url::HTTP_PROXY_REQUEST, urlOptions); auto path = parsed.toString(Url::HTTP_REQUEST); auto headersCopy = headers.clone(); headersCopy.set(HttpHeaderId::HOST, parsed.host); return getClient(parsed).request(method, path, headersCopy, expectedBodySize); } kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers) override { // We need to parse the proxy-style URL to convert it to host-style. // Use URL parsing options that avoid unnecessary rewrites. Url::Options urlOptions; urlOptions.allowEmpty = true; urlOptions.percentDecode = false; auto parsed = Url::parse(url, Url::HTTP_PROXY_REQUEST, urlOptions); auto path = parsed.toString(Url::HTTP_REQUEST); auto headersCopy = headers.clone(); headersCopy.set(HttpHeaderId::HOST, parsed.host); return getClient(parsed).openWebSocket(path, headersCopy); } private: kj::Timer& timer; HttpHeaderTable& responseHeaderTable; kj::Network& network; kj::Maybe tlsNetwork; HttpClientSettings settings; struct Host { kj::String name; // including port, if non-default kj::Own client; }; std::map httpHosts; std::map httpsHosts; struct RequestInfo { HttpMethod method; kj::String hostname; kj::String path; HttpHeaders headers; kj::Maybe expectedBodySize; }; kj::TaskSet tasks; HttpClient& getClient(kj::Url& parsed) { bool isHttps = parsed.scheme == "https"; bool isHttp = parsed.scheme == "http"; KJ_REQUIRE(isHttp || isHttps); auto& hosts = isHttps ? httpsHosts : httpHosts; // Look for a cached client for this host. // TODO(perf): It would be nice to recognize when different hosts have the same address and // reuse the same connection pool, but: // - We'd need a reliable way to compare NetworkAddresses, e.g. .equals() and .hashCode(). // It's very Java... ick. // - Correctly handling TLS would be tricky: we'd need to verify that the new hostname is // on the certificate. When SNI is in use we might have to request an additional // certificate (is that possible?). auto iter = hosts.find(parsed.host); if (iter == hosts.end()) { // Need to open a new connection. kj::Network* networkToUse = &network; if (isHttps) { networkToUse = &KJ_REQUIRE_NONNULL(tlsNetwork, "this HttpClient doesn't support HTTPS"); } auto promise = networkToUse->parseAddress(parsed.host, isHttps ? 443 : 80) .then([this](kj::Own addr) { return kj::heap( timer, responseHeaderTable, kj::mv(addr), settings); }); Host host { kj::mv(parsed.host), kj::heap(kj::mv(promise)) }; kj::StringPtr nameRef = host.name; auto insertResult = hosts.insert(std::make_pair(nameRef, kj::mv(host))); KJ_ASSERT(insertResult.second); iter = insertResult.first; tasks.add(handleCleanup(hosts, iter)); } return *iter->second.client; } kj::Promise handleCleanup(std::map& hosts, std::map::iterator iter) { return iter->second.client->onDrained() .then([this,&hosts,iter]() -> kj::Promise { // Double-check that it's really drained to avoid race conditions. if (iter->second.client->isDrained()) { hosts.erase(iter); return kj::READY_NOW; } else { return handleCleanup(hosts, iter); } }); } void taskFailed(kj::Exception&& exception) override { KJ_LOG(ERROR, exception); } }; } // namespace kj::Own newHttpClient(kj::Timer& timer, HttpHeaderTable& responseHeaderTable, kj::NetworkAddress& addr, HttpClientSettings settings) { return kj::heap(timer, responseHeaderTable, kj::Own(&addr, kj::NullDisposer::instance), kj::mv(settings)); } kj::Own newHttpClient(kj::Timer& timer, HttpHeaderTable& responseHeaderTable, kj::Network& network, kj::Maybe tlsNetwork, HttpClientSettings settings) { return kj::heap( timer, responseHeaderTable, network, tlsNetwork, kj::mv(settings)); } // ======================================================================================= namespace { class ConcurrencyLimitingHttpClient final: public HttpClient { public: ConcurrencyLimitingHttpClient( kj::HttpClient& inner, uint maxConcurrentRequests, kj::Function countChangedCallback) : inner(inner), maxConcurrentRequests(maxConcurrentRequests), countChangedCallback(kj::mv(countChangedCallback)) {} Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { if (concurrentRequests < maxConcurrentRequests) { auto counter = ConnectionCounter(*this); auto request = inner.request(method, url, headers, expectedBodySize); fireCountChanged(); auto promise = attachCounter(kj::mv(request.response), kj::mv(counter)); return { kj::mv(request.body), kj::mv(promise) }; } auto paf = kj::newPromiseAndFulfiller(); auto urlCopy = kj::str(url); auto headersCopy = headers.clone(); auto combined = paf.promise .then([this, method, urlCopy = kj::mv(urlCopy), headersCopy = kj::mv(headersCopy), expectedBodySize](ConnectionCounter&& counter) mutable { auto req = inner.request(method, urlCopy, headersCopy, expectedBodySize); return kj::tuple(kj::mv(req.body), attachCounter(kj::mv(req.response), kj::mv(counter))); }); auto split = combined.split(); pendingRequests.push(kj::mv(paf.fulfiller)); fireCountChanged(); return { newPromisedStream(kj::mv(kj::get<0>(split))), kj::mv(kj::get<1>(split)) }; } kj::Promise openWebSocket( kj::StringPtr url, const kj::HttpHeaders& headers) override { if (concurrentRequests < maxConcurrentRequests) { auto counter = ConnectionCounter(*this); auto response = inner.openWebSocket(url, headers); fireCountChanged(); return attachCounter(kj::mv(response), kj::mv(counter)); } auto paf = kj::newPromiseAndFulfiller(); auto urlCopy = kj::str(url); auto headersCopy = headers.clone(); auto promise = paf.promise .then([this, urlCopy = kj::mv(urlCopy), headersCopy = kj::mv(headersCopy)](ConnectionCounter&& counter) mutable { return attachCounter(inner.openWebSocket(urlCopy, headersCopy), kj::mv(counter)); }); pendingRequests.push(kj::mv(paf.fulfiller)); fireCountChanged(); return kj::mv(promise); } private: struct ConnectionCounter; kj::HttpClient& inner; uint maxConcurrentRequests; uint concurrentRequests = 0; kj::Function countChangedCallback; std::queue>> pendingRequests; // TODO(someday): want maximum cap on queue size? struct ConnectionCounter final { ConnectionCounter(ConcurrencyLimitingHttpClient& client) : parent(&client) { ++parent->concurrentRequests; } KJ_DISALLOW_COPY(ConnectionCounter); ~ConnectionCounter() noexcept(false) { if (parent != nullptr) { --parent->concurrentRequests; parent->serviceQueue(); parent->fireCountChanged(); } } ConnectionCounter(ConnectionCounter&& other) : parent(other.parent) { other.parent = nullptr; } ConnectionCounter& operator=(ConnectionCounter&& other) { if (this != &other) { this->parent = other.parent; other.parent = nullptr; } return *this; } ConcurrencyLimitingHttpClient* parent; }; void serviceQueue() { if (concurrentRequests >= maxConcurrentRequests) { return; } if (pendingRequests.empty()) { return; } auto fulfiller = kj::mv(pendingRequests.front()); pendingRequests.pop(); fulfiller->fulfill(ConnectionCounter(*this)); } void fireCountChanged() { countChangedCallback(concurrentRequests, pendingRequests.size()); } using WebSocketOrBody = kj::OneOf, kj::Own>; static WebSocketOrBody attachCounter(WebSocketOrBody&& webSocketOrBody, ConnectionCounter&& counter) { KJ_SWITCH_ONEOF(webSocketOrBody) { KJ_CASE_ONEOF(ws, kj::Own) { return ws.attach(kj::mv(counter)); } KJ_CASE_ONEOF(body, kj::Own) { return body.attach(kj::mv(counter)); } } KJ_UNREACHABLE; } static kj::Promise attachCounter(kj::Promise&& promise, ConnectionCounter&& counter) { return promise.then([counter = kj::mv(counter)](WebSocketResponse&& response) mutable { return WebSocketResponse { response.statusCode, response.statusText, response.headers, attachCounter(kj::mv(response.webSocketOrBody), kj::mv(counter)) }; }); } static kj::Promise attachCounter(kj::Promise&& promise, ConnectionCounter&& counter) { return promise.then([counter = kj::mv(counter)](Response&& response) mutable { return Response { response.statusCode, response.statusText, response.headers, response.body.attach(kj::mv(counter)) }; }); } }; } kj::Own newConcurrencyLimitingHttpClient( HttpClient& inner, uint maxConcurrentRequests, kj::Function countChangedCallback) { return kj::heap(inner, maxConcurrentRequests, kj::mv(countChangedCallback)); } // ======================================================================================= namespace { class NullInputStream final: public kj::AsyncInputStream { public: NullInputStream(kj::Maybe expectedLength = size_t(0)) : expectedLength(expectedLength) {} kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return size_t(0); } kj::Maybe tryGetLength() override { return expectedLength; } kj::Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return uint64_t(0); } private: kj::Maybe expectedLength; }; class NullOutputStream final: public kj::AsyncOutputStream { public: Promise write(const void* buffer, size_t size) override { return kj::READY_NOW; } Promise write(ArrayPtr> pieces) override { return kj::READY_NOW; } Promise whenWriteDisconnected() override { return kj::NEVER_DONE; } // We can't really optimize tryPumpFrom() unless AsyncInputStream grows a skip() method. }; class HttpClientAdapter final: public HttpClient { public: HttpClientAdapter(HttpService& service): service(service) {} Request request(HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { // We have to clone the URL and headers because HttpService implementation are allowed to // assume that they remain valid until the service handler completes whereas HttpClient callers // are allowed to destroy them immediately after the call. auto urlCopy = kj::str(url); auto headersCopy = kj::heap(headers.clone()); auto pipe = newOneWayPipe(expectedBodySize); // TODO(cleanup): The ownership relationships here are a mess. Can we do something better // involving a PromiseAdapter, maybe? auto paf = kj::newPromiseAndFulfiller(); auto responder = kj::refcounted(method, kj::mv(paf.fulfiller)); auto requestPaf = kj::newPromiseAndFulfiller>(); responder->setPromise(kj::mv(requestPaf.promise)); auto promise = service.request(method, urlCopy, *headersCopy, *pipe.in, *responder) .attach(kj::mv(pipe.in), kj::mv(urlCopy), kj::mv(headersCopy)); requestPaf.fulfiller->fulfill(kj::mv(promise)); return { kj::mv(pipe.out), paf.promise.attach(kj::mv(responder)) }; } kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers) override { // We have to clone the URL and headers because HttpService implementation are allowed to // assume that they remain valid until the service handler completes whereas HttpClient callers // are allowed to destroy them immediately after the call. Also we need to add // `Upgrade: websocket` so that headers.isWebSocket() returns true on the service side. auto urlCopy = kj::str(url); auto headersCopy = kj::heap(headers.clone()); headersCopy->set(HttpHeaderId::UPGRADE, "websocket"); KJ_DASSERT(headersCopy->isWebSocket()); auto paf = kj::newPromiseAndFulfiller(); auto responder = kj::refcounted(kj::mv(paf.fulfiller)); auto requestPaf = kj::newPromiseAndFulfiller>(); responder->setPromise(kj::mv(requestPaf.promise)); auto in = kj::heap(); auto promise = service.request(HttpMethod::GET, urlCopy, *headersCopy, *in, *responder) .attach(kj::mv(in), kj::mv(urlCopy), kj::mv(headersCopy)); requestPaf.fulfiller->fulfill(kj::mv(promise)); return paf.promise.attach(kj::mv(responder)); } kj::Promise> connect(kj::StringPtr host) override { return service.connect(kj::mv(host)); } private: HttpService& service; class DelayedEofInputStream final: public kj::AsyncInputStream { // An AsyncInputStream wrapper that, when it reaches EOF, delays the final read until some // promise completes. public: DelayedEofInputStream(kj::Own inner, kj::Promise completionTask) : inner(kj::mv(inner)), completionTask(kj::mv(completionTask)) {} kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return wrap(minBytes, inner->tryRead(buffer, minBytes, maxBytes)); } kj::Maybe tryGetLength() override { return inner->tryGetLength(); } kj::Promise pumpTo(kj::AsyncOutputStream& output, uint64_t amount) override { return wrap(amount, inner->pumpTo(output, amount)); } private: kj::Own inner; kj::Maybe> completionTask; template kj::Promise wrap(T requested, kj::Promise innerPromise) { return innerPromise.then([this,requested](T actual) -> kj::Promise { if (actual < requested) { // Must have reached EOF. KJ_IF_MAYBE(t, completionTask) { // Delay until completion. auto result = t->then([actual]() { return actual; }); completionTask = nullptr; return result; } else { // Must have called tryRead() again after we already signaled EOF. Fine. return actual; } } else { return actual; } }, [this](kj::Exception&& e) -> kj::Promise { // The stream threw an exception, but this exception is almost certainly just complaining // that the other end of the stream was dropped. In all likelihood, the HttpService // request() call itself will throw a much more interesting error -- we'd rather propagate // that one, if so. KJ_IF_MAYBE(t, completionTask) { auto result = t->then([e = kj::mv(e)]() mutable -> kj::Promise { // Looks like the service didn't throw. I guess we should propagate the stream error // after all. return kj::mv(e); }); completionTask = nullptr; return result; } else { // Must have called tryRead() again after we already signaled EOF or threw. Fine. return kj::mv(e); } }); } }; class ResponseImpl final: public HttpService::Response, public kj::Refcounted { public: ResponseImpl(kj::HttpMethod method, kj::Own> fulfiller) : method(method), fulfiller(kj::mv(fulfiller)) {} void setPromise(kj::Promise promise) { task = promise.eagerlyEvaluate([this](kj::Exception&& exception) { if (fulfiller->isWaiting()) { fulfiller->reject(kj::mv(exception)); } else { // We need to cause the response stream's read() to throw this, so we should propagate it. kj::throwRecoverableException(kj::mv(exception)); } }); } kj::Own send( uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { // The caller of HttpClient is allowed to assume that the statusText and headers remain // valid until the body stream is dropped, but the HttpService implementation is allowed to // send values that are only valid until send() returns, so we have to copy. auto statusTextCopy = kj::str(statusText); auto headersCopy = kj::heap(headers.clone()); if (method == kj::HttpMethod::HEAD || expectedBodySize.orDefault(1) == 0) { // We're not expecting any body. We need to delay reporting completion to the client until // the server side has actually returned from the service method, otherwise we may // prematurely cancel it. task = task.then([this,statusCode,statusTextCopy=kj::mv(statusTextCopy), headersCopy=kj::mv(headersCopy),expectedBodySize]() mutable { fulfiller->fulfill({ statusCode, statusTextCopy, headersCopy.get(), kj::heap(expectedBodySize) .attach(kj::mv(statusTextCopy), kj::mv(headersCopy)) }); }).eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); return kj::heap(); } else { auto pipe = newOneWayPipe(expectedBodySize); // Wrap the stream in a wrapper that delays the last read (the one that signals EOF) until // the service's request promise has finished. auto wrapper = kj::heap( kj::mv(pipe.in), task.attach(kj::addRef(*this))); fulfiller->fulfill({ statusCode, statusTextCopy, headersCopy.get(), wrapper.attach(kj::mv(statusTextCopy), kj::mv(headersCopy)) }); return kj::mv(pipe.out); } } kj::Own acceptWebSocket(const HttpHeaders& headers) override { KJ_FAIL_REQUIRE("a WebSocket was not requested"); } private: kj::HttpMethod method; kj::Own> fulfiller; kj::Promise task = nullptr; }; class DelayedCloseWebSocket final: public WebSocket { // A WebSocket wrapper that, when it reaches Close (in both directions), delays the final close // operation until some promise completes. public: DelayedCloseWebSocket(kj::Own inner, kj::Promise completionTask) : inner(kj::mv(inner)), completionTask(kj::mv(completionTask)) {} kj::Promise send(kj::ArrayPtr message) override { return inner->send(message); } kj::Promise send(kj::ArrayPtr message) override { return inner->send(message); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { return inner->close(code, reason) .then([this]() { return afterSendClosed(); }); } kj::Promise disconnect() override { return inner->disconnect(); } void abort() override { // Don't need to worry about completion task in this case -- cancelling it is reasonable. inner->abort(); } kj::Promise whenAborted() override { return inner->whenAborted(); } kj::Promise receive() override { return inner->receive().then([this](Message&& message) -> kj::Promise { if (message.is()) { return afterReceiveClosed() .then([message = kj::mv(message)]() mutable { return kj::mv(message); }); } return kj::mv(message); }); } kj::Promise pumpTo(WebSocket& other) override { return inner->pumpTo(other).then([this]() { return afterReceiveClosed(); }); } kj::Maybe> tryPumpFrom(WebSocket& other) override { return other.pumpTo(*inner).then([this]() { return afterSendClosed(); }); } private: kj::Own inner; kj::Maybe> completionTask; bool sentClose = false; bool receivedClose = false; kj::Promise afterSendClosed() { sentClose = true; if (receivedClose) { KJ_IF_MAYBE(t, completionTask) { auto result = kj::mv(*t); completionTask = nullptr; return result; } } return kj::READY_NOW; } kj::Promise afterReceiveClosed() { receivedClose = true; if (sentClose) { KJ_IF_MAYBE(t, completionTask) { auto result = kj::mv(*t); completionTask = nullptr; return result; } } return kj::READY_NOW; } }; class WebSocketResponseImpl final: public HttpService::Response, public kj::Refcounted { public: WebSocketResponseImpl(kj::Own> fulfiller) : fulfiller(kj::mv(fulfiller)) {} void setPromise(kj::Promise promise) { task = promise.eagerlyEvaluate([this](kj::Exception&& exception) { if (fulfiller->isWaiting()) { fulfiller->reject(kj::mv(exception)); } else { // We need to cause the client-side WebSocket to throw on close, so propagate the // exception. kj::throwRecoverableException(kj::mv(exception)); } }); } kj::Own send( uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { // The caller of HttpClient is allowed to assume that the statusText and headers remain // valid until the body stream is dropped, but the HttpService implementation is allowed to // send values that are only valid until send() returns, so we have to copy. auto statusTextCopy = kj::str(statusText); auto headersCopy = kj::heap(headers.clone()); if (expectedBodySize.orDefault(1) == 0) { // We're not expecting any body. We need to delay reporting completion to the client until // the server side has actually returned from the service method, otherwise we may // prematurely cancel it. task = task.then([this,statusCode,statusTextCopy=kj::mv(statusTextCopy), headersCopy=kj::mv(headersCopy),expectedBodySize]() mutable { fulfiller->fulfill({ statusCode, statusTextCopy, headersCopy.get(), kj::Own(kj::heap(expectedBodySize) .attach(kj::mv(statusTextCopy), kj::mv(headersCopy))) }); }).eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); return kj::heap(); } else { auto pipe = newOneWayPipe(expectedBodySize); // Wrap the stream in a wrapper that delays the last read (the one that signals EOF) until // the service's request promise has finished. kj::Own wrapper = kj::heap(kj::mv(pipe.in), task.attach(kj::addRef(*this))); fulfiller->fulfill({ statusCode, statusTextCopy, headersCopy.get(), wrapper.attach(kj::mv(statusTextCopy), kj::mv(headersCopy)) }); return kj::mv(pipe.out); } } kj::Own acceptWebSocket(const HttpHeaders& headers) override { // The caller of HttpClient is allowed to assume that the headers remain valid until the body // stream is dropped, but the HttpService implementation is allowed to send headers that are // only valid until acceptWebSocket() returns, so we have to copy. auto headersCopy = kj::heap(headers.clone()); auto pipe = newWebSocketPipe(); // Wrap the client-side WebSocket in a wrapper that delays clean close of the WebSocket until // the service's request promise has finished. kj::Own wrapper = kj::heap(kj::mv(pipe.ends[0]), task.attach(kj::addRef(*this))); fulfiller->fulfill({ 101, "Switching Protocols", headersCopy.get(), wrapper.attach(kj::mv(headersCopy)) }); return kj::mv(pipe.ends[1]); } private: kj::Own> fulfiller; kj::Promise task = nullptr; }; }; } // namespace kj::Own newHttpClient(HttpService& service) { return kj::heap(service); } // ======================================================================================= namespace { class HttpServiceAdapter final: public HttpService { public: HttpServiceAdapter(HttpClient& client): client(client) {} kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { if (!headers.isWebSocket()) { auto innerReq = client.request(method, url, headers, requestBody.tryGetLength()); auto promises = kj::heapArrayBuilder>(2); promises.add(requestBody.pumpTo(*innerReq.body).ignoreResult() .attach(kj::mv(innerReq.body)).eagerlyEvaluate(nullptr)); promises.add(innerReq.response .then([&response](HttpClient::Response&& innerResponse) { auto out = response.send( innerResponse.statusCode, innerResponse.statusText, *innerResponse.headers, innerResponse.body->tryGetLength()); auto promise = innerResponse.body->pumpTo(*out); return promise.ignoreResult().attach(kj::mv(out), kj::mv(innerResponse.body)); })); return kj::joinPromises(promises.finish()); } else { return client.openWebSocket(url, headers) .then([&response](HttpClient::WebSocketResponse&& innerResponse) -> kj::Promise { KJ_SWITCH_ONEOF(innerResponse.webSocketOrBody) { KJ_CASE_ONEOF(ws, kj::Own) { auto ws2 = response.acceptWebSocket(*innerResponse.headers); auto promises = kj::heapArrayBuilder>(2); promises.add(ws->pumpTo(*ws2)); promises.add(ws2->pumpTo(*ws)); return kj::joinPromises(promises.finish()).attach(kj::mv(ws), kj::mv(ws2)); } KJ_CASE_ONEOF(body, kj::Own) { auto out = response.send( innerResponse.statusCode, innerResponse.statusText, *innerResponse.headers, body->tryGetLength()); auto promise = body->pumpTo(*out); return promise.ignoreResult().attach(kj::mv(out), kj::mv(body)); } } KJ_UNREACHABLE; }); } } kj::Promise> connect(kj::StringPtr host) override { return client.connect(kj::mv(host)); } private: HttpClient& client; }; } // namespace kj::Own newHttpService(HttpClient& client) { return kj::heap(client); } // ======================================================================================= kj::Promise HttpService::Response::sendError( uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers) { auto stream = send(statusCode, statusText, headers, statusText.size()); auto promise = stream->write(statusText.begin(), statusText.size()); return promise.attach(kj::mv(stream)); } kj::Promise HttpService::Response::sendError( uint statusCode, kj::StringPtr statusText, const HttpHeaderTable& headerTable) { return sendError(statusCode, statusText, HttpHeaders(headerTable)); } kj::Promise> HttpService::connect(kj::StringPtr host) { KJ_UNIMPLEMENTED("CONNECT is not implemented by this HttpService"); } class HttpServer::Connection final: private HttpService::Response, private HttpServerErrorHandler { public: Connection(HttpServer& server, kj::AsyncIoStream& stream, HttpService& service) : server(server), stream(stream), service(service), httpInput(stream, server.requestHeaderTable), httpOutput(stream) { ++server.connectionCount; } ~Connection() noexcept(false) { if (--server.connectionCount == 0) { KJ_IF_MAYBE(f, server.zeroConnectionsFulfiller) { f->get()->fulfill(); } } } kj::Promise loop(bool firstRequest) { if (!firstRequest && server.draining && httpInput.isCleanDrain()) { // Don't call awaitNextMessage() in this case because that will initiate a read() which will // immediately be canceled, losing data. return true; } auto firstByte = httpInput.awaitNextMessage(); if (!firstRequest) { // For requests after the first, require that the first byte arrive before the pipeline // timeout, otherwise treat it like the connection was simply closed. auto timeoutPromise = server.timer.afterDelay(server.settings.pipelineTimeout); if (httpInput.isCleanDrain()) { // If we haven't buffered any data, then we can safely drain here, so allow the wait to // be canceled by the onDrain promise. timeoutPromise = timeoutPromise.exclusiveJoin(server.onDrain.addBranch()); } firstByte = firstByte.exclusiveJoin(timeoutPromise.then([this]() -> bool { timedOut = true; return false; })); } auto receivedHeaders = firstByte .then([this,firstRequest](bool hasData) -> kj::Promise { if (hasData) { auto readHeaders = httpInput.readRequestHeaders(); if (!firstRequest) { // On requests other than the first, the header timeout starts ticking when we receive // the first byte of a pipeline response. readHeaders = readHeaders.exclusiveJoin( server.timer.afterDelay(server.settings.headerTimeout) .then([this]() -> HttpHeaders::RequestOrProtocolError { timedOut = true; return HttpHeaders::ProtocolError { 408, "Request Timeout", "ERROR: Timed out waiting for next request headers.", nullptr }; })); } return kj::mv(readHeaders); } else { // Client closed connection or pipeline timed out with no bytes received. This is not an // error, so don't report one. this->closed = true; return HttpHeaders::RequestOrProtocolError(HttpHeaders::ProtocolError { 408, "Request Timeout", "ERROR: Client closed connection or connection timeout " "while waiting for request headers.", nullptr }); } }); if (firstRequest) { // On the first request, the header timeout starts ticking immediately upon request opening. auto timeoutPromise = server.timer.afterDelay(server.settings.headerTimeout) .exclusiveJoin(server.onDrain.addBranch()) .then([this]() -> HttpHeaders::RequestOrProtocolError { timedOut = true; return HttpHeaders::ProtocolError { 408, "Request Timeout", "ERROR: Timed out waiting for initial request headers.", nullptr }; }); receivedHeaders = receivedHeaders.exclusiveJoin(kj::mv(timeoutPromise)); } return receivedHeaders .then([this](HttpHeaders::RequestOrProtocolError&& requestOrProtocolError) -> kj::Promise { if (timedOut) { // Client took too long to send anything, so we're going to close the connection. In // theory, we should send back an HTTP 408 error -- it is designed exactly for this // purpose. Alas, in practice, Google Chrome does not have any special handling for 408 // errors -- it will assume the error is a response to the next request it tries to send, // and will happily serve the error to the user. OTOH, if we simply close the connection, // Chrome does the "right thing", apparently. (Though I'm not sure what happens if a // request is in-flight when we close... if it's a GET, the browser should retry. But if // it's a POST, retrying may be dangerous. This is why 408 exists -- it unambiguously // tells the client that it should retry.) // // Also note that if we ever decide to send 408 again, we might want to send some other // error in the case that the server is draining, which also sets timedOut = true; see // above. return httpOutput.flush().then([this]() { return server.draining && httpInput.isCleanDrain(); }); } if (closed) { // Client closed connection. Close our end too. return httpOutput.flush().then([]() { return false; }); } KJ_SWITCH_ONEOF(requestOrProtocolError) { KJ_CASE_ONEOF(request, HttpHeaders::Request) { auto& headers = httpInput.getHeaders(); currentMethod = request.method; auto body = httpInput.getEntityBody( HttpInputStreamImpl::REQUEST, request.method, 0, headers); // TODO(perf): If the client disconnects, should we cancel the response? Probably, to // prevent permanent deadlock. It's slightly weird in that arguably the client should // be able to shutdown the upstream but still wait on the downstream, but I believe many // other HTTP servers do similar things. auto promise = service.request( request.method, request.url, headers, *body, *this); return promise.then([this, body = kj::mv(body)]() mutable -> kj::Promise { // Response done. Await next request. KJ_IF_MAYBE(p, webSocketError) { // sendWebSocketError() was called. Finish sending and close the connection. auto promise = kj::mv(*p); webSocketError = nullptr; return kj::mv(promise); } if (upgraded) { // We've upgraded to WebSocket, and by now we should have closed the WebSocket. if (!webSocketClosed) { // This is gonna segfault later so abort now instead. KJ_LOG(FATAL, "Accepted WebSocket object must be destroyed before HttpService " "request handler completes."); abort(); } // Once we start a WebSocket there's no going back to HTTP. return false; } if (currentMethod != nullptr) { return sendError(); } if (httpOutput.isBroken()) { // We started a response but didn't finish it. But HttpService returns success? // Perhaps it decided that it doesn't want to finish this response. We'll have to // disconnect here. If the response body is not complete (e.g. Content-Length not // reached), the client should notice. We don't want to log an error because this // condition might be intentional on the service's part. return false; } return httpOutput.flush().then( [this, body = kj::mv(body)]() mutable -> kj::Promise { if (httpInput.canReuse()) { // Things look clean. Go ahead and accept the next request. // Note that we don't have to handle server.draining here because we'll take care of // it the next time around the loop. return loop(false); } else { // Apparently, the application did not read the request body. Maybe this is a bug, // or maybe not: maybe the client tried to upload too much data and the application // legitimately wants to cancel the upload without reading all it it. // // We have a problem, though: We did send a response, and we didn't send // `Connection: close`, so the client may expect that it can send another request. // Perhaps the client has even finished sending the previous request's body, in // which case the moment it finishes receiving the response, it could be completely // within its rights to start a new request. If we close the socket now, we might // interrupt that new request. // // There's no way we can get out of this perfectly cleanly. HTTP just isn't good // enough at connection management. The best we can do is give the client some grace // period and then abort the connection. auto dummy = kj::heap(); auto lengthGrace = body->pumpTo(*dummy, server.settings.canceledUploadGraceBytes) .then([this](size_t amount) { if (httpInput.canReuse()) { // Success, we can continue. return true; } else { // Still more data. Give up. return false; } }); lengthGrace = lengthGrace.attach(kj::mv(dummy), kj::mv(body)); auto timeGrace = server.timer.afterDelay(server.settings.canceledUploadGracePeriod) .then([]() { return false; }); return lengthGrace.exclusiveJoin(kj::mv(timeGrace)) .then([this](bool clean) -> kj::Promise { if (clean) { // We recovered. Continue loop. return loop(false); } else { // Client still not done. Return broken. return false; } }); } }); }); } KJ_CASE_ONEOF(protocolError, HttpHeaders::ProtocolError) { // Bad request. // sendError() uses Response::send(), which requires that we have a currentMethod, but we // never read one. GET seems like the correct choice here. currentMethod = HttpMethod::GET; return sendError(kj::mv(protocolError)); } } KJ_UNREACHABLE; }).catch_([this](kj::Exception&& e) -> kj::Promise { // Exception; report 5xx. KJ_IF_MAYBE(p, webSocketError) { // sendWebSocketError() was called. Finish sending and close the connection. Don't log // the exception because it's probably a side-effect of this. auto promise = kj::mv(*p); webSocketError = nullptr; return kj::mv(promise); } return sendError(kj::mv(e)); }); } private: HttpServer& server; kj::AsyncIoStream& stream; HttpService& service; HttpInputStreamImpl httpInput; HttpOutputStream httpOutput; kj::Maybe currentMethod; bool timedOut = false; bool closed = false; bool upgraded = false; bool webSocketClosed = false; bool closeAfterSend = false; // True if send() should set Connection: close. kj::Maybe> webSocketError; kj::Own send( uint statusCode, kj::StringPtr statusText, const HttpHeaders& headers, kj::Maybe expectedBodySize) override { auto method = KJ_REQUIRE_NONNULL(currentMethod, "already called send()"); currentMethod = nullptr; kj::StringPtr connectionHeaders[HttpHeaders::CONNECTION_HEADERS_COUNT]; kj::String lengthStr; if (closeAfterSend) { connectionHeaders[HttpHeaders::BuiltinIndices::CONNECTION] = "close"; } if (statusCode == 204 || statusCode == 205 || statusCode == 304) { // No entity-body. } else KJ_IF_MAYBE(s, expectedBodySize) { // HACK: We interpret a zero-length expected body length on responses to HEAD requests to mean // "don't set a Content-Length header at all." This provides a way to omit a body header on // HEAD responses with non-null-body status codes. This is a hack that *only* makes sense // for HEAD responses. if (method != HttpMethod::HEAD || *s > 0) { lengthStr = kj::str(*s); connectionHeaders[HttpHeaders::BuiltinIndices::CONTENT_LENGTH] = lengthStr; } } else { connectionHeaders[HttpHeaders::BuiltinIndices::TRANSFER_ENCODING] = "chunked"; } // For HEAD requests, if the application specified a Content-Length or Transfer-Encoding // header, use that instead of whatever we decided above. kj::ArrayPtr connectionHeadersArray = connectionHeaders; if (method == HttpMethod::HEAD) { if (headers.get(HttpHeaderId::CONTENT_LENGTH) != nullptr || headers.get(HttpHeaderId::TRANSFER_ENCODING) != nullptr) { connectionHeadersArray = connectionHeadersArray .slice(0, HttpHeaders::HEAD_RESPONSE_CONNECTION_HEADERS_COUNT); } } httpOutput.writeHeaders(headers.serializeResponse( statusCode, statusText, connectionHeadersArray)); kj::Own bodyStream; if (method == HttpMethod::HEAD) { // Ignore entity-body. httpOutput.finishBody(); return heap(); } else if (statusCode == 204 || statusCode == 205 || statusCode == 304) { // No entity-body. httpOutput.finishBody(); return heap(); } else KJ_IF_MAYBE(s, expectedBodySize) { return heap(httpOutput, *s); } else { return heap(httpOutput); } } kj::Own acceptWebSocket(const HttpHeaders& headers) override { auto& requestHeaders = httpInput.getHeaders(); KJ_REQUIRE(requestHeaders.isWebSocket(), "can't call acceptWebSocket() if the request headers didn't have Upgrade: WebSocket"); auto method = KJ_REQUIRE_NONNULL(currentMethod, "already called send()"); // Unlike send(), we neither need nor want to null out currentMethod. The error cases below // depend on it being non-null to allow error responses to be sent, and the happy path expects // it to be GET. if (method != HttpMethod::GET) { return sendWebSocketError("ERROR: WebSocket must be initiated with a GET request."); } if (requestHeaders.get(HttpHeaderId::SEC_WEBSOCKET_VERSION).orDefault(nullptr) != "13") { return sendWebSocketError("ERROR: The requested WebSocket version is not supported."); } kj::String key; KJ_IF_MAYBE(k, requestHeaders.get(HttpHeaderId::SEC_WEBSOCKET_KEY)) { key = kj::str(*k); } else { return sendWebSocketError("ERROR: Missing Sec-WebSocket-Key"); } auto websocketAccept = generateWebSocketAccept(key); kj::StringPtr connectionHeaders[HttpHeaders::WEBSOCKET_CONNECTION_HEADERS_COUNT]; connectionHeaders[HttpHeaders::BuiltinIndices::SEC_WEBSOCKET_ACCEPT] = websocketAccept; connectionHeaders[HttpHeaders::BuiltinIndices::UPGRADE] = "websocket"; connectionHeaders[HttpHeaders::BuiltinIndices::CONNECTION] = "Upgrade"; httpOutput.writeHeaders(headers.serializeResponse( 101, "Switching Protocols", connectionHeaders)); upgraded = true; // We need to give the WebSocket an Own, but we only have a reference. This is // safe because the application is expected to drop the WebSocket object before returning // from the request handler. For some extra safety, we check that webSocketClosed has been // set true when the handler returns. auto deferNoteClosed = kj::defer([this]() { webSocketClosed = true; }); kj::Own ownStream(&stream, kj::NullDisposer::instance); return upgradeToWebSocket(ownStream.attach(kj::mv(deferNoteClosed)), httpInput, httpOutput, nullptr); } kj::Promise sendError(HttpHeaders::ProtocolError protocolError) { closeAfterSend = true; // Client protocol errors always happen on request headers parsing, before we call into the // HttpService, meaning no response has been sent and we can provide a Response object. auto promise = server.settings.errorHandler.orDefault(*this).handleClientProtocolError( kj::mv(protocolError), *this); return promise.then([this]() { return httpOutput.flush(); }) .then([]() { return false; }); // loop ends after flush } kj::Promise sendError(kj::Exception&& exception) { closeAfterSend = true; // We only provide the Response object if we know we haven't already sent a response. auto promise = server.settings.errorHandler.orDefault(*this).handleApplicationError( kj::mv(exception), currentMethod.map([this](auto&&) -> Response& { return *this; })); return promise.then([this]() { return httpOutput.flush(); }) .then([]() { return false; }); // loop ends after flush } kj::Promise sendError() { closeAfterSend = true; // We can provide a Response object, since none has already been sent. auto promise = server.settings.errorHandler.orDefault(*this).handleNoResponse(*this); return promise.then([this]() { return httpOutput.flush(); }) .then([]() { return false; }); // loop ends after flush } kj::Own sendWebSocketError(StringPtr errorMessage) { kj::Exception exception = KJ_EXCEPTION(FAILED, "received bad WebSocket handshake", errorMessage); webSocketError = sendError( HttpHeaders::ProtocolError { 400, "Bad Request", errorMessage, nullptr }); kj::throwRecoverableException(kj::mv(exception)); // Fallback path when exceptions are disabled. class BrokenWebSocket final: public WebSocket { public: BrokenWebSocket(kj::Exception exception): exception(kj::mv(exception)) {} kj::Promise send(kj::ArrayPtr message) override { return kj::cp(exception); } kj::Promise send(kj::ArrayPtr message) override { return kj::cp(exception); } kj::Promise close(uint16_t code, kj::StringPtr reason) override { return kj::cp(exception); } kj::Promise disconnect() override { return kj::cp(exception); } void abort() override { kj::throwRecoverableException(kj::cp(exception)); } kj::Promise whenAborted() override { return kj::cp(exception); } kj::Promise receive() override { return kj::cp(exception); } private: kj::Exception exception; }; return kj::heap(KJ_EXCEPTION(FAILED, "received bad WebSocket handshake", errorMessage)); } }; HttpServer::HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, HttpService& service, Settings settings) : HttpServer(timer, requestHeaderTable, &service, settings, kj::newPromiseAndFulfiller()) {} HttpServer::HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, HttpServiceFactory serviceFactory, Settings settings) : HttpServer(timer, requestHeaderTable, kj::mv(serviceFactory), settings, kj::newPromiseAndFulfiller()) {} HttpServer::HttpServer(kj::Timer& timer, HttpHeaderTable& requestHeaderTable, kj::OneOf service, Settings settings, kj::PromiseFulfillerPair paf) : timer(timer), requestHeaderTable(requestHeaderTable), service(kj::mv(service)), settings(settings), onDrain(paf.promise.fork()), drainFulfiller(kj::mv(paf.fulfiller)), tasks(*this) {} kj::Promise HttpServer::drain() { KJ_REQUIRE(!draining, "you can only call drain() once"); draining = true; drainFulfiller->fulfill(); if (connectionCount == 0) { return kj::READY_NOW; } else { auto paf = kj::newPromiseAndFulfiller(); zeroConnectionsFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } } kj::Promise HttpServer::listenHttp(kj::ConnectionReceiver& port) { return listenLoop(port).exclusiveJoin(onDrain.addBranch()); } kj::Promise HttpServer::listenLoop(kj::ConnectionReceiver& port) { return port.accept() .then([this,&port](kj::Own&& connection) -> kj::Promise { if (draining) { // Can get here if we *just* started draining. return kj::READY_NOW; } tasks.add(listenHttp(kj::mv(connection))); return listenLoop(port); }); } kj::Promise HttpServer::listenHttp(kj::Own connection) { auto promise = listenHttpCleanDrain(*connection).ignoreResult(); // eagerlyEvaluate() to maintain historical guarantee that this method eagerly closes the // connection when done. return promise.attach(kj::mv(connection)).eagerlyEvaluate(nullptr); } kj::Promise HttpServer::listenHttpCleanDrain(kj::AsyncIoStream& connection) { kj::Own obj; KJ_SWITCH_ONEOF(service) { KJ_CASE_ONEOF(ptr, HttpService*) { obj = heap(*this, connection, *ptr); } KJ_CASE_ONEOF(func, HttpServiceFactory) { auto srv = func(connection); obj = heap(*this, connection, *srv); obj = obj.attach(kj::mv(srv)); } } // Start reading requests and responding to them, but immediately cancel processing if the client // disconnects. auto promise = obj->loop(true) .exclusiveJoin(connection.whenWriteDisconnected().then([]() {return false;})); // Eagerly evaluate so that we drop the connection when the promise resolves, even if the caller // doesn't eagerly evaluate. return promise.attach(kj::mv(obj)).eagerlyEvaluate(nullptr); } void HttpServer::taskFailed(kj::Exception&& exception) { KJ_LOG(ERROR, "unhandled exception in HTTP server", exception); } kj::Promise HttpServerErrorHandler::handleClientProtocolError( HttpHeaders::ProtocolError protocolError, kj::HttpService::Response& response) { // Default error handler implementation. HttpHeaderTable headerTable {}; HttpHeaders headers(headerTable); headers.set(HttpHeaderId::CONTENT_TYPE, "text/plain"); auto errorMessage = kj::str(protocolError.description); auto body = response.send(protocolError.statusCode, protocolError.statusMessage, headers, errorMessage.size()); return body->write(errorMessage.begin(), errorMessage.size()) .attach(kj::mv(errorMessage), kj::mv(body)); } kj::Promise HttpServerErrorHandler::handleApplicationError( kj::Exception exception, kj::Maybe response) { // Default error handler implementation. if (exception.getType() == kj::Exception::Type::DISCONNECTED) { // How do we tell an HTTP client that there was a transient network error, and it should // try again immediately? There's no HTTP status code for this (503 is meant for "try // again later, not now"). Here's an idea: Don't send any response; just close the // connection, so that it looks like the connection between the HTTP client and server // was dropped. A good client should treat this exactly the way we want. // // We also bail here to avoid logging the disconnection, which isn't very interesting. return kj::READY_NOW; } KJ_IF_MAYBE(r, response) { HttpHeaderTable headerTable {}; HttpHeaders headers(headerTable); headers.set(HttpHeaderId::CONTENT_TYPE, "text/plain"); kj::String errorMessage; kj::Own body; if (exception.getType() == kj::Exception::Type::OVERLOADED) { errorMessage = kj::str( "ERROR: The server is temporarily unable to handle your request. Details:\n\n", exception); body = r->send(503, "Service Unavailable", headers, errorMessage.size()); } else if (exception.getType() == kj::Exception::Type::UNIMPLEMENTED) { errorMessage = kj::str( "ERROR: The server does not implement this operation. Details:\n\n", exception); body = r->send(501, "Not Implemented", headers, errorMessage.size()); } else { errorMessage = kj::str( "ERROR: The server threw an exception. Details:\n\n", exception); body = r->send(500, "Internal Server Error", headers, errorMessage.size()); } return body->write(errorMessage.begin(), errorMessage.size()) .attach(kj::mv(errorMessage), kj::mv(body)); } KJ_LOG(ERROR, "HttpService threw exception after generating a partial response", "too late to report error to client", exception); return kj::READY_NOW; } kj::Promise HttpServerErrorHandler::handleNoResponse(kj::HttpService::Response& response) { HttpHeaderTable headerTable {}; HttpHeaders headers(headerTable); headers.set(HttpHeaderId::CONTENT_TYPE, "text/plain"); constexpr auto errorMessage = "ERROR: The HttpService did not generate a response."_kj; auto body = response.send(500, "Internal Server Error", headers, errorMessage.size()); return body->write(errorMessage.begin(), errorMessage.size()).attach(kj::mv(body)); } } // namespace kj capnproto-c++-0.8.0/src/kj/compat/readiness-io.c++0000644000175000017500000001056113340402540022300 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "readiness-io.h" namespace kj { static size_t copyInto(kj::ArrayPtr dst, kj::ArrayPtr& src) { size_t n = kj::min(dst.size(), src.size()); memcpy(dst.begin(), src.begin(), n); src = src.slice(n, src.size()); return n; } // ======================================================================================= ReadyInputStreamWrapper::ReadyInputStreamWrapper(AsyncInputStream& input): input(input) {} ReadyInputStreamWrapper::~ReadyInputStreamWrapper() noexcept(false) {} kj::Maybe ReadyInputStreamWrapper::read(kj::ArrayPtr dst) { if (eof || dst.size() == 0) return size_t(0); if (content.size() == 0) { // No data available. Try to read more. if (!isPumping) { isPumping = true; pumpTask = kj::evalNow([&]() { return input.tryRead(buffer, 1, sizeof(buffer)).then([this](size_t n) { if (n == 0) { eof = true; } else { content = kj::arrayPtr(buffer, n); } isPumping = false; }); }).fork(); } return nullptr; } return copyInto(dst, content); } kj::Promise ReadyInputStreamWrapper::whenReady() { return pumpTask.addBranch(); } // ======================================================================================= ReadyOutputStreamWrapper::ReadyOutputStreamWrapper(AsyncOutputStream& output): output(output) {} ReadyOutputStreamWrapper::~ReadyOutputStreamWrapper() noexcept(false) {} kj::Maybe ReadyOutputStreamWrapper::write(kj::ArrayPtr data) { if (data.size() == 0) return size_t(0); if (filled == sizeof(buffer)) { // No space. return nullptr; } uint end = start + filled; size_t result = 0; if (end < sizeof(buffer)) { // The filled part of the buffer is somewhere in the middle. // Copy into space after filled space. result += copyInto(kj::arrayPtr(buffer + end, buffer + sizeof(buffer)), data); // Copy into space before filled space. result += copyInto(kj::arrayPtr(buffer, buffer + start), data); } else { // Fill currently loops, so we only have one segment of empty space to copy into. // Copy into the space between the fill's end and the fill's start. result += copyInto(kj::arrayPtr(buffer + end % sizeof(buffer), buffer + start), data); } filled += result; if (!isPumping) { isPumping = true; pumpTask = kj::evalNow([&]() { return pump(); }).fork(); } return result; } kj::Promise ReadyOutputStreamWrapper::whenReady() { return pumpTask.addBranch(); } kj::Promise ReadyOutputStreamWrapper::pump() { uint oldFilled = filled; uint end = start + filled; kj::Promise promise = nullptr; if (end <= sizeof(buffer)) { promise = output.write(buffer + start, filled); } else { end = end % sizeof(buffer); segments[0] = kj::arrayPtr(buffer + start, buffer + sizeof(buffer)); segments[1] = kj::arrayPtr(buffer, buffer + end); promise = output.write(segments); } return promise.then([this,oldFilled,end]() -> kj::Promise { filled -= oldFilled; start = end; if (filled > 0) { return pump(); } else { isPumping = false; return kj::READY_NOW; } }); } } // namespace kj capnproto-c++-0.8.0/src/kj/compat/tls.c++0000644000175000017500000006636213650101756020543 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if KJ_HAS_OPENSSL #include "tls.h" #include "readiness-io.h" #include #include #include #include #include #include #include #include #include #include #include #if OPENSSL_VERSION_NUMBER < 0x10100000L #define BIO_set_init(x,v) (x->init=v) #define BIO_get_data(x) (x->ptr) #define BIO_set_data(x,v) (x->ptr=v) #endif namespace kj { namespace { // ======================================================================================= // misc helpers KJ_NORETURN(void throwOpensslError()); void throwOpensslError() { // Call when an OpenSSL function returns an error code to convert that into an exception and // throw it. kj::Vector lines; while (unsigned long long error = ERR_get_error()) { char message[1024]; ERR_error_string_n(error, message, sizeof(message)); lines.add(kj::heapString(message)); } kj::String message = kj::strArray(lines, "\n"); KJ_FAIL_ASSERT("OpenSSL error", message); } #if OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(OPENSSL_IS_BORINGSSL) // Older versions of OpenSSL don't define _up_ref() functions. void EVP_PKEY_up_ref(EVP_PKEY* pkey) { CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); } void X509_up_ref(X509* x509) { CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509); } #endif #if OPENSSL_VERSION_NUMBER < 0x10100000L class OpenSslInit { // Initializes the OpenSSL library. public: OpenSslInit() { SSL_library_init(); SSL_load_error_strings(); OPENSSL_config(nullptr); } }; void ensureOpenSslInitialized() { // Initializes the OpenSSL library the first time it is called. static OpenSslInit init; } #else inline void ensureOpenSslInitialized() { // As of 1.1.0, no initialization is needed. } #endif // ======================================================================================= // Implementation of kj::AsyncIoStream that applies TLS on top of some other AsyncIoStream. // // TODO(perf): OpenSSL's I/O abstraction layer, "BIO", is readiness-based, but AsyncIoStream is // completion-based. This forces us to use an intermediate buffer which wastes memory and incurs // redundant copies. We could improve the situation by creating a way to detect if the underlying // AsyncIoStream is simply wrapping a file descriptor (or other readiness-based stream?) and use // that directly if so. class TlsConnection final: public kj::AsyncIoStream { public: TlsConnection(kj::Own stream, SSL_CTX* ctx) : TlsConnection(*stream, ctx) { ownInner = kj::mv(stream); } TlsConnection(kj::AsyncIoStream& stream, SSL_CTX* ctx) : inner(stream), readBuffer(stream), writeBuffer(stream) { ssl = SSL_new(ctx); if (ssl == nullptr) { throwOpensslError(); } BIO* bio = BIO_new(const_cast(getBioVtable())); if (bio == nullptr) { SSL_free(ssl); throwOpensslError(); } BIO_set_data(bio, this); BIO_set_init(bio, 1); SSL_set_bio(ssl, bio, bio); } kj::Promise connect(kj::StringPtr expectedServerHostname) { if (!SSL_set_tlsext_host_name(ssl, expectedServerHostname.cStr())) { throwOpensslError(); } X509_VERIFY_PARAM* verify = SSL_get0_param(ssl); if (verify == nullptr) { throwOpensslError(); } if (X509_VERIFY_PARAM_set1_host( verify, expectedServerHostname.cStr(), expectedServerHostname.size()) <= 0) { throwOpensslError(); } return sslCall([this]() { return SSL_connect(ssl); }).then([this](size_t) { X509* cert = SSL_get_peer_certificate(ssl); KJ_REQUIRE(cert != nullptr, "TLS peer provided no certificate"); X509_free(cert); auto result = SSL_get_verify_result(ssl); if (result != X509_V_OK) { const char* reason = X509_verify_cert_error_string(result); KJ_FAIL_REQUIRE("TLS peer's certificate is not trusted", reason); } }); } kj::Promise accept() { // We are the server. Set SSL options to prefer server's cipher choice. SSL_set_options(ssl, SSL_OP_CIPHER_SERVER_PREFERENCE); return sslCall([this]() { return SSL_accept(ssl); }).ignoreResult(); } ~TlsConnection() noexcept(false) { SSL_free(ssl); } kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return tryReadInternal(buffer, minBytes, maxBytes, 0); } Promise write(const void* buffer, size_t size) override { return writeInternal(kj::arrayPtr(reinterpret_cast(buffer), size), nullptr); } Promise write(ArrayPtr> pieces) override { return writeInternal(pieces[0], pieces.slice(1, pieces.size())); } Promise whenWriteDisconnected() override { return inner.whenWriteDisconnected(); } void shutdownWrite() override { KJ_REQUIRE(shutdownTask == nullptr, "already called shutdownWrite()"); // TODO(0.9): shutdownWrite() is problematic because it doesn't return a promise. It was // designed to assume that it would only be called after all writes are finished and that // there was no reason to block at that point, but SSL sessions don't fit this since they // actually have to send a shutdown message. shutdownTask = sslCall([this]() { // The first SSL_shutdown() call is expected to return 0 and may flag a misleading error. int result = SSL_shutdown(ssl); return result == 0 ? 1 : result; }).ignoreResult().eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); } void abortRead() override { inner.abortRead(); } void getsockopt(int level, int option, void* value, uint* length) override { inner.getsockopt(level, option, value, length); } void setsockopt(int level, int option, const void* value, uint length) override { inner.setsockopt(level, option, value, length); } void getsockname(struct sockaddr* addr, uint* length) override { inner.getsockname(addr, length); } void getpeername(struct sockaddr* addr, uint* length) override { inner.getpeername(addr, length); } private: SSL* ssl; kj::AsyncIoStream& inner; kj::Own ownInner; bool disconnected = false; kj::Maybe> shutdownTask; ReadyInputStreamWrapper readBuffer; ReadyOutputStreamWrapper writeBuffer; kj::Promise tryReadInternal( void* buffer, size_t minBytes, size_t maxBytes, size_t alreadyDone) { if (disconnected) return alreadyDone; return sslCall([this,buffer,maxBytes]() { return SSL_read(ssl, buffer, maxBytes); }) .then([this,buffer,minBytes,maxBytes,alreadyDone](size_t n) -> kj::Promise { if (n >= minBytes || n == 0) { return alreadyDone + n; } else { return tryReadInternal(reinterpret_cast(buffer) + n, minBytes - n, maxBytes - n, alreadyDone + n); } }); } Promise writeInternal(kj::ArrayPtr first, kj::ArrayPtr> rest) { KJ_REQUIRE(shutdownTask == nullptr, "already called shutdownWrite()"); return sslCall([this,first]() { return SSL_write(ssl, first.begin(), first.size()); }) .then([this,first,rest](size_t n) -> kj::Promise { if (n == 0) { return KJ_EXCEPTION(DISCONNECTED, "ssl connection ended during write"); } else if (n < first.size()) { return writeInternal(first.slice(n, first.size()), rest); } else if (rest.size() > 0) { return writeInternal(rest[0], rest.slice(1, rest.size())); } else { return kj::READY_NOW; } }); } template kj::Promise sslCall(Func&& func) { if (disconnected) return size_t(0); ssize_t result = func(); if (result > 0) { return result; } else { int error = SSL_get_error(ssl, result); switch (error) { case SSL_ERROR_ZERO_RETURN: disconnected = true; return size_t(0); case SSL_ERROR_WANT_READ: return readBuffer.whenReady().then(kj::mvCapture(func, [this](Func&& func) mutable { return sslCall(kj::fwd(func)); })); case SSL_ERROR_WANT_WRITE: return writeBuffer.whenReady().then(kj::mvCapture(func, [this](Func&& func) mutable { return sslCall(kj::fwd(func)); })); case SSL_ERROR_SSL: throwOpensslError(); case SSL_ERROR_SYSCALL: if (result == 0) { disconnected = true; return size_t(0); } else { // According to documentation we shouldn't get here, because our BIO never returns an // "error". But in practice we do get here sometimes when the peer disconnects // prematurely. KJ_FAIL_ASSERT("TLS protocol error"); } default: KJ_FAIL_ASSERT("unexpected SSL error code", error); } } } static int bioRead(BIO* b, char* out, int outl) { BIO_clear_retry_flags(b); KJ_IF_MAYBE(n, reinterpret_cast(BIO_get_data(b))->readBuffer .read(kj::arrayPtr(out, outl).asBytes())) { return *n; } else { BIO_set_retry_read(b); return -1; } } static int bioWrite(BIO* b, const char* in, int inl) { BIO_clear_retry_flags(b); KJ_IF_MAYBE(n, reinterpret_cast(BIO_get_data(b))->writeBuffer .write(kj::arrayPtr(in, inl).asBytes())) { return *n; } else { BIO_set_retry_write(b); return -1; } } static long bioCtrl(BIO* b, int cmd, long num, void* ptr) { switch (cmd) { case BIO_CTRL_FLUSH: return 1; case BIO_CTRL_PUSH: case BIO_CTRL_POP: // Informational? return 0; default: KJ_LOG(WARNING, "unimplemented bio_ctrl", cmd); return 0; } } static int bioCreate(BIO* b) { BIO_set_data(b, nullptr); return 1; } static int bioDestroy(BIO* b) { // The BIO does NOT own the TlsConnection. return 1; } #if OPENSSL_VERSION_NUMBER < 0x10100000L static const BIO_METHOD* getBioVtable() { static const BIO_METHOD VTABLE { BIO_TYPE_SOURCE_SINK, "KJ stream", TlsConnection::bioWrite, TlsConnection::bioRead, nullptr, // puts nullptr, // gets TlsConnection::bioCtrl, TlsConnection::bioCreate, TlsConnection::bioDestroy, nullptr }; return &VTABLE; } #else static const BIO_METHOD* getBioVtable() { static const BIO_METHOD* const vtable = makeBioVtable(); return vtable; } static const BIO_METHOD* makeBioVtable() { BIO_METHOD* vtable = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "KJ stream"); BIO_meth_set_write(vtable, TlsConnection::bioWrite); BIO_meth_set_read(vtable, TlsConnection::bioRead); BIO_meth_set_ctrl(vtable, TlsConnection::bioCtrl); BIO_meth_set_create(vtable, TlsConnection::bioCreate); BIO_meth_set_destroy(vtable, TlsConnection::bioDestroy); return vtable; } #endif }; // ======================================================================================= // Implementations of ConnectionReceiver, NetworkAddress, and Network as wrappers adding TLS. class TlsConnectionReceiver final: public kj::ConnectionReceiver { public: TlsConnectionReceiver(TlsContext& tls, kj::Own inner) : tls(tls), inner(kj::mv(inner)) {} Promise> accept() override { return inner->accept().then([this](kj::Own stream) { return tls.wrapServer(kj::mv(stream)); }); } uint getPort() override { return inner->getPort(); } void getsockopt(int level, int option, void* value, uint* length) override { return inner->getsockopt(level, option, value, length); } void setsockopt(int level, int option, const void* value, uint length) override { return inner->setsockopt(level, option, value, length); } private: TlsContext& tls; kj::Own inner; }; class TlsNetworkAddress final: public kj::NetworkAddress { public: TlsNetworkAddress(TlsContext& tls, kj::String hostname, kj::Own&& inner) : tls(tls), hostname(kj::mv(hostname)), inner(kj::mv(inner)) {} Promise> connect() override { // Note: It's unfortunately pretty common for people to assume they can drop the NetworkAddress // as soon as connect() returns, and this works with the native network implementation. // So, we make some copies here. auto& tlsRef = tls; auto hostnameCopy = kj::str(hostname); return inner->connect().then(kj::mvCapture(hostnameCopy, [&tlsRef](kj::String&& hostname, Own&& stream) { return tlsRef.wrapClient(kj::mv(stream), hostname); })); } Own listen() override { return tls.wrapPort(inner->listen()); } Own clone() override { return kj::heap(tls, kj::str(hostname), inner->clone()); } String toString() override { return kj::str("tls:", inner->toString()); } private: TlsContext& tls; kj::String hostname; kj::Own inner; }; class TlsNetwork final: public kj::Network { public: TlsNetwork(TlsContext& tls, kj::Network& inner): tls(tls), inner(inner) {} TlsNetwork(TlsContext& tls, kj::Own inner) : tls(tls), inner(*inner), ownInner(kj::mv(inner)) {} Promise> parseAddress(StringPtr addr, uint portHint) override { kj::String hostname; KJ_IF_MAYBE(pos, addr.findFirst(':')) { hostname = kj::heapString(addr.slice(0, *pos)); } else { hostname = kj::heapString(addr); } return inner.parseAddress(addr, portHint) .then(kj::mvCapture(hostname, [this](kj::String&& hostname, kj::Own&& addr) -> kj::Own { return kj::heap(tls, kj::mv(hostname), kj::mv(addr)); })); } Own getSockaddr(const void* sockaddr, uint len) override { KJ_UNIMPLEMENTED("TLS does not implement getSockaddr() because it needs to know hostnames"); } Own restrictPeers( kj::ArrayPtr allow, kj::ArrayPtr deny = nullptr) override { // TODO(someday): Maybe we could implement the ability to specify CA or hostname restrictions? // Or is it better to let people do that via the TlsContext? A neat thing about // restrictPeers() is that it's easy to make user-configurable. return kj::heap(tls, inner.restrictPeers(allow, deny)); } private: TlsContext& tls; kj::Network& inner; kj::Own ownInner; }; } // namespace // ======================================================================================= // class TlsContext TlsContext::Options::Options() : useSystemTrustStore(true), verifyClients(false), minVersion(TlsVersion::TLS_1_0), cipherList("ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS") {} // Cipher list is Mozilla's "intermediate" list, except with classic DH removed since we don't // currently support setting dhparams. See: // https://mozilla.github.io/server-side-tls/ssl-config-generator/ // // Classic DH is arguably obsolete and will only become more so as time passes, so perhaps we'll // never bother. struct TlsContext::SniCallback { // struct SniCallback exists only so that callback() can be declared in the .c++ file, since it // references OpenSSL types. static int callback(SSL* ssl, int* ad, void* arg); }; TlsContext::TlsContext(Options options) { ensureOpenSslInitialized(); #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) SSL_CTX* ctx = SSL_CTX_new(TLS_method()); #else SSL_CTX* ctx = SSL_CTX_new(SSLv23_method()); #endif if (ctx == nullptr) { throwOpensslError(); } KJ_ON_SCOPE_FAILURE(SSL_CTX_free(ctx)); // honor options.useSystemTrustStore if (options.useSystemTrustStore) { if (!SSL_CTX_set_default_verify_paths(ctx)) { throwOpensslError(); } } // honor options.trustedCertificates if (options.trustedCertificates.size() > 0) { X509_STORE* store = SSL_CTX_get_cert_store(ctx); if (store == nullptr) { throwOpensslError(); } for (auto& cert: options.trustedCertificates) { if (!X509_STORE_add_cert(store, reinterpret_cast(cert.chain[0]))) { throwOpensslError(); } } } if (options.verifyClients) { SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); } // honor options.minVersion long optionFlags = 0; if (options.minVersion > TlsVersion::SSL_3) { optionFlags |= SSL_OP_NO_SSLv3; } if (options.minVersion > TlsVersion::TLS_1_0) { optionFlags |= SSL_OP_NO_TLSv1; } if (options.minVersion > TlsVersion::TLS_1_1) { optionFlags |= SSL_OP_NO_TLSv1_1; } if (options.minVersion > TlsVersion::TLS_1_2) { optionFlags |= SSL_OP_NO_TLSv1_2; } SSL_CTX_set_options(ctx, optionFlags); // note: never fails; returns new options bitmask // honor options.cipherList if (!SSL_CTX_set_cipher_list(ctx, options.cipherList.cStr())) { throwOpensslError(); } // honor options.defaultKeypair KJ_IF_MAYBE(kp, options.defaultKeypair) { if (!SSL_CTX_use_PrivateKey(ctx, reinterpret_cast(kp->privateKey.pkey))) { throwOpensslError(); } if (!SSL_CTX_use_certificate(ctx, reinterpret_cast(kp->certificate.chain[0]))) { throwOpensslError(); } for (size_t i = 1; i < kj::size(kp->certificate.chain); i++) { X509* x509 = reinterpret_cast(kp->certificate.chain[i]); if (x509 == nullptr) break; // end of chain if (!SSL_CTX_add_extra_chain_cert(ctx, x509)) { throwOpensslError(); } // SSL_CTX_add_extra_chain_cert() does NOT up the refcount itself. X509_up_ref(x509); } } // honor options.sniCallback KJ_IF_MAYBE(sni, options.sniCallback) { SSL_CTX_set_tlsext_servername_callback(ctx, &SniCallback::callback); SSL_CTX_set_tlsext_servername_arg(ctx, sni); } this->ctx = ctx; } int TlsContext::SniCallback::callback(SSL* ssl, int* ad, void* arg) { // The third parameter is actually type TlsSniCallback*. KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { TlsSniCallback& sni = *reinterpret_cast(arg); const char* name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); if (name != nullptr) { KJ_IF_MAYBE(kp, sni.getKey(name)) { if (!SSL_use_PrivateKey(ssl, reinterpret_cast(kp->privateKey.pkey))) { throwOpensslError(); } if (!SSL_use_certificate(ssl, reinterpret_cast(kp->certificate.chain[0]))) { throwOpensslError(); } if (!SSL_clear_chain_certs(ssl)) { throwOpensslError(); } for (size_t i = 1; i < kj::size(kp->certificate.chain); i++) { X509* x509 = reinterpret_cast(kp->certificate.chain[i]); if (x509 == nullptr) break; // end of chain if (!SSL_add0_chain_cert(ssl, x509)) { throwOpensslError(); } // SSL_add0_chain_cert() does NOT up the refcount itself. X509_up_ref(x509); } } } })) { KJ_LOG(ERROR, "exception when invoking SNI callback", *exception); *ad = SSL_AD_INTERNAL_ERROR; return SSL_TLSEXT_ERR_ALERT_FATAL; } return SSL_TLSEXT_ERR_OK; } TlsContext::~TlsContext() noexcept(false) { SSL_CTX_free(reinterpret_cast(ctx)); } kj::Promise> TlsContext::wrapClient( kj::Own stream, kj::StringPtr expectedServerHostname) { auto conn = kj::heap(kj::mv(stream), reinterpret_cast(ctx)); auto promise = conn->connect(expectedServerHostname); return promise.then(kj::mvCapture(conn, [](kj::Own conn) -> kj::Own { return kj::mv(conn); })); } kj::Promise> TlsContext::wrapServer(kj::Own stream) { auto conn = kj::heap(kj::mv(stream), reinterpret_cast(ctx)); auto promise = conn->accept(); return promise.then(kj::mvCapture(conn, [](kj::Own conn) -> kj::Own { return kj::mv(conn); })); } kj::Own TlsContext::wrapPort(kj::Own port) { return kj::heap(*this, kj::mv(port)); } kj::Own TlsContext::wrapNetwork(kj::Network& network) { return kj::heap(*this, network); } // ======================================================================================= // class TlsPrivateKey TlsPrivateKey::TlsPrivateKey(kj::ArrayPtr asn1) { ensureOpenSslInitialized(); const byte* ptr = asn1.begin(); pkey = d2i_AutoPrivateKey(nullptr, &ptr, asn1.size()); if (pkey == nullptr) { throwOpensslError(); } } TlsPrivateKey::TlsPrivateKey(kj::StringPtr pem, kj::Maybe password) { ensureOpenSslInitialized(); // const_cast apparently needed for older versions of OpenSSL. BIO* bio = BIO_new_mem_buf(const_cast(pem.begin()), pem.size()); KJ_DEFER(BIO_free(bio)); pkey = PEM_read_bio_PrivateKey(bio, nullptr, &passwordCallback, &password); if (pkey == nullptr) { throwOpensslError(); } } TlsPrivateKey::TlsPrivateKey(const TlsPrivateKey& other) : pkey(other.pkey) { if (pkey != nullptr) EVP_PKEY_up_ref(reinterpret_cast(pkey)); } TlsPrivateKey& TlsPrivateKey::operator=(const TlsPrivateKey& other) { if (pkey != other.pkey) { EVP_PKEY_free(reinterpret_cast(pkey)); pkey = other.pkey; if (pkey != nullptr) EVP_PKEY_up_ref(reinterpret_cast(pkey)); } return *this; } TlsPrivateKey::~TlsPrivateKey() noexcept(false) { EVP_PKEY_free(reinterpret_cast(pkey)); } int TlsPrivateKey::passwordCallback(char* buf, int size, int rwflag, void* u) { auto& password = *reinterpret_cast*>(u); KJ_IF_MAYBE(p, password) { int result = kj::min(p->size(), size); memcpy(buf, p->begin(), result); return result; } else { return 0; } } // ======================================================================================= // class TlsCertificate TlsCertificate::TlsCertificate(kj::ArrayPtr> asn1) { ensureOpenSslInitialized(); KJ_REQUIRE(asn1.size() > 0, "must provide at least one certificate in chain"); KJ_REQUIRE(asn1.size() <= kj::size(chain), "exceeded maximum certificate chain length of 10"); memset(chain, 0, sizeof(chain)); for (auto i: kj::indices(asn1)) { auto p = asn1[i].begin(); // "_AUX" apparently refers to some auxilliary information that can be appended to the // certificate, but should only be trusted for your own certificate, not the whole chain?? // I don't really know, I'm just cargo-culting. chain[i] = i == 0 ? d2i_X509_AUX(nullptr, &p, asn1[i].size()) : d2i_X509(nullptr, &p, asn1[i].size()); if (chain[i] == nullptr) { for (size_t j = 0; j < i; j++) { X509_free(reinterpret_cast(chain[j])); } throwOpensslError(); } } } TlsCertificate::TlsCertificate(kj::ArrayPtr asn1) : TlsCertificate(kj::arrayPtr(&asn1, 1)) {} TlsCertificate::TlsCertificate(kj::StringPtr pem) { ensureOpenSslInitialized(); memset(chain, 0, sizeof(chain)); // const_cast apparently needed for older versions of OpenSSL. BIO* bio = BIO_new_mem_buf(const_cast(pem.begin()), pem.size()); KJ_DEFER(BIO_free(bio)); for (auto i: kj::indices(chain)) { // "_AUX" apparently refers to some auxilliary information that can be appended to the // certificate, but should only be trusted for your own certificate, not the whole chain?? // I don't really know, I'm just cargo-culting. chain[i] = i == 0 ? PEM_read_bio_X509_AUX(bio, nullptr, nullptr, nullptr) : PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); if (chain[i] == nullptr) { auto error = ERR_peek_last_error(); if (i > 0 && ERR_GET_LIB(error) == ERR_LIB_PEM && ERR_GET_REASON(error) == PEM_R_NO_START_LINE) { // EOF; we're done. ERR_clear_error(); return; } else { for (size_t j = 0; j < i; j++) { X509_free(reinterpret_cast(chain[j])); } throwOpensslError(); } } } // We reached the chain length limit. Try to read one more to verify that the chain ends here. X509* dummy = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); if (dummy != nullptr) { X509_free(dummy); for (auto i: kj::indices(chain)) { X509_free(reinterpret_cast(chain[i])); } KJ_FAIL_REQUIRE("exceeded maximum certificate chain length of 10"); } } TlsCertificate::TlsCertificate(const TlsCertificate& other) { memcpy(chain, other.chain, sizeof(chain)); for (void* p: chain) { if (p == nullptr) break; // end of chain; quit early X509_up_ref(reinterpret_cast(p)); } } TlsCertificate& TlsCertificate::operator=(const TlsCertificate& other) { for (auto i: kj::indices(chain)) { if (chain[i] != other.chain[i]) { EVP_PKEY_free(reinterpret_cast(chain[i])); chain[i] = other.chain[i]; if (chain[i] != nullptr) X509_up_ref(reinterpret_cast(chain[i])); } else if (chain[i] == nullptr) { // end of both chains; quit early break; } } return *this; } TlsCertificate::~TlsCertificate() noexcept(false) { for (void* p: chain) { if (p == nullptr) break; // end of chain; quit early X509_free(reinterpret_cast(p)); } } } // namespace kj #endif // KJ_HAS_OPENSSL capnproto-c++-0.8.0/src/kj/compat/url-test.c++0000644000175000017500000005230513650101756021510 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "url.h" #include #include namespace kj { namespace { Url parseAndCheck(kj::StringPtr originalText, kj::StringPtr expectedRestringified = nullptr, Url::Options options = {}) { if (expectedRestringified == nullptr) expectedRestringified = originalText; auto url = Url::parse(originalText, Url::REMOTE_HREF, options); KJ_EXPECT(kj::str(url) == expectedRestringified, url, originalText, expectedRestringified); // Make sure clones also restringify to the expected string. auto clone = url.clone(); KJ_EXPECT(kj::str(clone) == expectedRestringified, clone, originalText, expectedRestringified); return url; } static constexpr Url::Options NO_DECODE { false, // percentDecode false, // allowEmpty }; static constexpr Url::Options ALLOW_EMPTY { true, // percentDecode true, // allowEmpty }; KJ_TEST("parse / stringify URL") { { auto url = parseAndCheck("https://capnproto.org"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://capnproto.org:80"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org:80"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://capnproto.org/"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://capnproto.org/foo/bar"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://capnproto.org/foo/bar/"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=qux&corge#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 2); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == "qux"); KJ_EXPECT(url.query[1].name == "corge"); KJ_EXPECT(url.query[1].value == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=qux&corge=#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 2); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == "qux"); KJ_EXPECT(url.query[1].name == "corge"); KJ_EXPECT(url.query[1].value == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=&corge=grault#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 2); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == ""); KJ_EXPECT(url.query[1].name == "corge"); KJ_EXPECT(url.query[1].value == "grault"); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/foo/bar/?baz=qux&corge=grault#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 2); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == "qux"); KJ_EXPECT(url.query[1].name == "corge"); KJ_EXPECT(url.query[1].value == "grault"); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/foo/bar?baz=qux#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 1); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == "qux"); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/foo?bar%20baz=qux+quux", "https://capnproto.org/foo?bar+baz=qux+quux"); KJ_ASSERT(url.query.size() == 1); KJ_EXPECT(url.query[0].name == "bar baz"); KJ_EXPECT(url.query[0].value == "qux quux"); } { auto url = parseAndCheck("https://capnproto.org/foo/bar#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/foo/bar/#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://capnproto.org/#garply"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(url.fragment) == "garply"); } { auto url = parseAndCheck("https://foo@capnproto.org"); KJ_EXPECT(url.scheme == "https"); auto& user = KJ_ASSERT_NONNULL(url.userInfo); KJ_EXPECT(user.username == "foo"); KJ_EXPECT(user.password == nullptr); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://$foo&:12+,34@capnproto.org"); KJ_EXPECT(url.scheme == "https"); auto& user = KJ_ASSERT_NONNULL(url.userInfo); KJ_EXPECT(user.username == "$foo&"); KJ_EXPECT(KJ_ASSERT_NONNULL(user.password) == "12+,34"); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://[2001:db8::1234]:80/foo"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.userInfo == nullptr); KJ_EXPECT(url.host == "[2001:db8::1234]:80"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { auto url = parseAndCheck("https://capnproto.org/foo%2Fbar/baz"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo/bar", "baz"})); } parseAndCheck("https://capnproto.org/foo/bar?", "https://capnproto.org/foo/bar"); parseAndCheck("https://capnproto.org/foo/bar?#", "https://capnproto.org/foo/bar#"); parseAndCheck("https://capnproto.org/foo/bar#"); // Scheme and host are forced to lower-case. parseAndCheck("hTtP://capNprotO.org/fOo/bAr", "http://capnproto.org/fOo/bAr"); // URLs with underscores in their hostnames are allowed, but you probably shouldn't use them. They // are not valid domain names. parseAndCheck("https://bad_domain.capnproto.org/"); // Make sure URLs with %-encoded '%' signs in their userinfo, path, query, and fragment components // get correctly re-encoded. parseAndCheck("https://foo%25bar:baz%25qux@capnproto.org/"); parseAndCheck("https://capnproto.org/foo%25bar"); parseAndCheck("https://capnproto.org/?foo%25bar=baz%25qux"); parseAndCheck("https://capnproto.org/#foo%25bar"); // Make sure redundant /'s and &'s aren't collapsed when options.removeEmpty is false. parseAndCheck("https://capnproto.org/foo//bar///test//?foo=bar&&baz=qux&", nullptr, ALLOW_EMPTY); // "." and ".." are still processed, though. parseAndCheck("https://capnproto.org/foo//../bar/.", "https://capnproto.org/foo/bar/", ALLOW_EMPTY); { auto url = parseAndCheck("https://foo/", nullptr, ALLOW_EMPTY); KJ_EXPECT(url.path.size() == 0); KJ_EXPECT(url.hasTrailingSlash); } { auto url = parseAndCheck("https://foo/bar/", nullptr, ALLOW_EMPTY); KJ_EXPECT(url.path.size() == 1); KJ_EXPECT(url.hasTrailingSlash); } } KJ_TEST("URL percent encoding") { parseAndCheck( "https://b%6fb:%61bcd@capnpr%6fto.org/f%6fo?b%61r=b%61z#q%75x", "https://bob:abcd@capnproto.org/foo?bar=baz#qux"); parseAndCheck( "https://b\001b:\001bcd@capnproto.org/f\001o?b\001r=b\001z#q\001x", "https://b%01b:%01bcd@capnproto.org/f%01o?b%01r=b%01z#q%01x"); parseAndCheck( "https://b b: bcd@capnproto.org/f o?b r=b z#q x", "https://b%20b:%20bcd@capnproto.org/f%20o?b+r=b+z#q%20x"); parseAndCheck( "https://capnproto.org/foo?bar=baz#@?#^[\\]{|}", "https://capnproto.org/foo?bar=baz#@?#^[\\]{|}"); // All permissible non-alphanumeric, non-separator path characters. parseAndCheck( "https://capnproto.org/!$&'()*+,-.:;=@[]^_|~", "https://capnproto.org/!$&'()*+,-.:;=@[]^_|~"); } KJ_TEST("parse / stringify URL w/o decoding") { { auto url = parseAndCheck("https://capnproto.org/foo%2Fbar/baz", nullptr, NO_DECODE); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo%2Fbar", "baz"})); } { // This case would throw an exception without NO_DECODE. Url url = parseAndCheck("https://capnproto.org/R%20%26%20S?%foo=%QQ", nullptr, NO_DECODE); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"R%20%26%20S"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 1); KJ_EXPECT(url.query[0].name == "%foo"); KJ_EXPECT(url.query[0].value == "%QQ"); } } KJ_TEST("URL relative paths") { parseAndCheck( "https://capnproto.org/foo//bar", "https://capnproto.org/foo/bar"); parseAndCheck( "https://capnproto.org/foo/./bar", "https://capnproto.org/foo/bar"); parseAndCheck( "https://capnproto.org/foo/bar//", "https://capnproto.org/foo/bar/"); parseAndCheck( "https://capnproto.org/foo/bar/.", "https://capnproto.org/foo/bar/"); parseAndCheck( "https://capnproto.org/foo/baz/../bar", "https://capnproto.org/foo/bar"); parseAndCheck( "https://capnproto.org/foo/bar/baz/..", "https://capnproto.org/foo/bar/"); parseAndCheck( "https://capnproto.org/..", "https://capnproto.org/"); parseAndCheck( "https://capnproto.org/foo/../..", "https://capnproto.org/"); } KJ_TEST("URL for HTTP request") { { Url url = Url::parse("https://bob:1234@capnproto.org/foo/bar?baz=qux#corge"); KJ_EXPECT(url.toString(Url::REMOTE_HREF) == "https://bob:1234@capnproto.org/foo/bar?baz=qux#corge"); KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org/foo/bar?baz=qux"); KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/foo/bar?baz=qux"); } { Url url = Url::parse("https://capnproto.org"); KJ_EXPECT(url.toString(Url::REMOTE_HREF) == "https://capnproto.org"); KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org"); KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/"); } { Url url = Url::parse("/foo/bar?baz=qux&corge", Url::HTTP_REQUEST); KJ_EXPECT(url.scheme == nullptr); KJ_EXPECT(url.host == nullptr); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 2); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == "qux"); KJ_EXPECT(url.query[1].name == "corge"); KJ_EXPECT(url.query[1].value == nullptr); } { Url url = Url::parse("https://capnproto.org/foo/bar?baz=qux&corge", Url::HTTP_PROXY_REQUEST); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr({"foo", "bar"})); KJ_EXPECT(!url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 2); KJ_EXPECT(url.query[0].name == "baz"); KJ_EXPECT(url.query[0].value == "qux"); KJ_EXPECT(url.query[1].name == "corge"); KJ_EXPECT(url.query[1].value == nullptr); } { // '#' is allowed in path components in the HTTP_REQUEST context. Url url = Url::parse("/foo#bar", Url::HTTP_REQUEST); KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/foo%23bar"); KJ_EXPECT(url.scheme == nullptr); KJ_EXPECT(url.host == nullptr); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr{"foo#bar"}); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { // '#' is allowed in path components in the HTTP_PROXY_REQUEST context. Url url = Url::parse("https://capnproto.org/foo#bar", Url::HTTP_PROXY_REQUEST); KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org/foo%23bar"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path.asPtr() == kj::ArrayPtr{"foo#bar"}); KJ_EXPECT(!url.hasTrailingSlash); KJ_EXPECT(url.query == nullptr); KJ_EXPECT(url.fragment == nullptr); } { // '#' is allowed in query components in the HTTP_REQUEST context. Url url = Url::parse("/?foo=bar#123", Url::HTTP_REQUEST); KJ_EXPECT(url.toString(Url::HTTP_REQUEST) == "/?foo=bar%23123"); KJ_EXPECT(url.scheme == nullptr); KJ_EXPECT(url.host == nullptr); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 1); KJ_EXPECT(url.query[0].name == "foo"); KJ_EXPECT(url.query[0].value == "bar#123"); KJ_EXPECT(url.fragment == nullptr); } { // '#' is allowed in query components in the HTTP_PROXY_REQUEST context. Url url = Url::parse("https://capnproto.org/?foo=bar#123", Url::HTTP_PROXY_REQUEST); KJ_EXPECT(url.toString(Url::HTTP_PROXY_REQUEST) == "https://capnproto.org/?foo=bar%23123"); KJ_EXPECT(url.scheme == "https"); KJ_EXPECT(url.host == "capnproto.org"); KJ_EXPECT(url.path == nullptr); KJ_EXPECT(url.hasTrailingSlash); KJ_ASSERT(url.query.size() == 1); KJ_EXPECT(url.query[0].name == "foo"); KJ_EXPECT(url.query[0].value == "bar#123"); KJ_EXPECT(url.fragment == nullptr); } } KJ_TEST("parse URL failure") { KJ_EXPECT(Url::tryParse("ht/tps://capnproto.org") == nullptr); KJ_EXPECT(Url::tryParse("capnproto.org") == nullptr); KJ_EXPECT(Url::tryParse("https:foo") == nullptr); // percent-decode errors KJ_EXPECT(Url::tryParse("https://capnproto.org/f%nno") == nullptr); KJ_EXPECT(Url::tryParse("https://capnproto.org/foo?b%nnr=baz") == nullptr); // components not valid in context KJ_EXPECT(Url::tryParse("https://capnproto.org/foo", Url::HTTP_REQUEST) == nullptr); KJ_EXPECT(Url::tryParse("https://bob:123@capnproto.org/foo", Url::HTTP_PROXY_REQUEST) == nullptr); KJ_EXPECT(Url::tryParse("https://capnproto.org#/foo", Url::HTTP_PROXY_REQUEST) == nullptr); } void parseAndCheckRelative(kj::StringPtr base, kj::StringPtr relative, kj::StringPtr expected, Url::Options options = {}) { auto parsed = Url::parse(base, Url::REMOTE_HREF, options).parseRelative(relative); KJ_EXPECT(kj::str(parsed) == expected, parsed, expected); auto clone = parsed.clone(); KJ_EXPECT(kj::str(clone) == expected, clone, expected); } KJ_TEST("parse relative URL") { parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "#grault", "https://capnproto.org/foo/bar?baz=qux#grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz#corge", "#grault", "https://capnproto.org/foo/bar?baz#grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=#corge", "#grault", "https://capnproto.org/foo/bar?baz=#grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "?grault", "https://capnproto.org/foo/bar?grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "?grault=", "https://capnproto.org/foo/bar?grault="); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "?grault+garply=waldo", "https://capnproto.org/foo/bar?grault+garply=waldo"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "grault", "https://capnproto.org/foo/grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "/grault", "https://capnproto.org/grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "//grault", "https://grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "//grault/garply", "https://grault/garply"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "http:/grault", "http://capnproto.org/grault"); parseAndCheckRelative("https://capnproto.org/foo/bar?baz=qux#corge", "/http:/grault", "https://capnproto.org/http:/grault"); parseAndCheckRelative("https://capnproto.org/", "/foo/../bar", "https://capnproto.org/bar"); } KJ_TEST("parse relative URL w/o decoding") { // This case would throw an exception without NO_DECODE. parseAndCheckRelative("https://capnproto.org/R%20%26%20S?%foo=%QQ", "%ANOTH%ERBAD%URL", "https://capnproto.org/%ANOTH%ERBAD%URL", NO_DECODE); } KJ_TEST("parse relative URL failure") { auto base = Url::parse("https://example.com/"); KJ_EXPECT(base.tryParseRelative("https://[not a host]") == nullptr); } } // namespace } // namespace kj capnproto-c++-0.8.0/src/kj/compat/http-test.c++0000644000175000017500000037062713650101756021677 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #define KJ_TESTING_KJ 1 #include "http.h" #include #include #include #include #if KJ_HTTP_TEST_USE_OS_PIPE // Run the test using OS-level socketpairs. (See http-socketpair-test.c++.) #define KJ_HTTP_TEST_SETUP_IO \ auto io = kj::setupAsyncIo(); \ auto& waitScope KJ_UNUSED = io.waitScope #define KJ_HTTP_TEST_SETUP_LOOPBACK_LISTENER_AND_ADDR \ auto listener = io.provider->getNetwork().parseAddress("localhost", 0) \ .wait(waitScope)->listen(); \ auto addr = io.provider->getNetwork().parseAddress("localhost", listener->getPort()) \ .wait(waitScope) #define KJ_HTTP_TEST_CREATE_2PIPE \ io.provider->newTwoWayPipe() #else // Run the test using in-process two-way pipes. #define KJ_HTTP_TEST_SETUP_IO \ kj::EventLoop eventLoop; \ kj::WaitScope waitScope(eventLoop) #define KJ_HTTP_TEST_SETUP_LOOPBACK_LISTENER_AND_ADDR \ auto capPipe = newCapabilityPipe(); \ auto listener = kj::heap(*capPipe.ends[0]); \ auto addr = kj::heap(nullptr, *capPipe.ends[1]) #define KJ_HTTP_TEST_CREATE_2PIPE \ kj::newTwoWayPipe() #endif namespace kj { namespace { KJ_TEST("HttpMethod parse / stringify") { #define TRY(name) \ KJ_EXPECT(kj::str(HttpMethod::name) == #name); \ KJ_IF_MAYBE(parsed, tryParseHttpMethod(#name)) { \ KJ_EXPECT(*parsed == HttpMethod::name); \ } else { \ KJ_FAIL_EXPECT("couldn't parse \"" #name "\" as HttpMethod"); \ } KJ_HTTP_FOR_EACH_METHOD(TRY) #undef TRY KJ_EXPECT(tryParseHttpMethod("FOO") == nullptr); KJ_EXPECT(tryParseHttpMethod("") == nullptr); KJ_EXPECT(tryParseHttpMethod("G") == nullptr); KJ_EXPECT(tryParseHttpMethod("GE") == nullptr); KJ_EXPECT(tryParseHttpMethod("GET ") == nullptr); KJ_EXPECT(tryParseHttpMethod("get") == nullptr); } KJ_TEST("HttpHeaderTable") { HttpHeaderTable::Builder builder; auto host = builder.add("Host"); auto host2 = builder.add("hOsT"); auto fooBar = builder.add("Foo-Bar"); auto bazQux = builder.add("baz-qux"); auto bazQux2 = builder.add("Baz-Qux"); auto table = builder.build(); uint builtinHeaderCount = 0; #define INCREMENT(id, name) ++builtinHeaderCount; KJ_HTTP_FOR_EACH_BUILTIN_HEADER(INCREMENT) #undef INCREMENT KJ_EXPECT(table->idCount() == builtinHeaderCount + 2); KJ_EXPECT(host == HttpHeaderId::HOST); KJ_EXPECT(host != HttpHeaderId::DATE); KJ_EXPECT(host2 == host); KJ_EXPECT(host != fooBar); KJ_EXPECT(host != bazQux); KJ_EXPECT(fooBar != bazQux); KJ_EXPECT(bazQux == bazQux2); KJ_EXPECT(kj::str(host) == "Host"); KJ_EXPECT(kj::str(host2) == "Host"); KJ_EXPECT(kj::str(fooBar) == "Foo-Bar"); KJ_EXPECT(kj::str(bazQux) == "baz-qux"); KJ_EXPECT(kj::str(HttpHeaderId::HOST) == "Host"); KJ_EXPECT(table->idToString(HttpHeaderId::DATE) == "Date"); KJ_EXPECT(table->idToString(fooBar) == "Foo-Bar"); KJ_EXPECT(KJ_ASSERT_NONNULL(table->stringToId("Date")) == HttpHeaderId::DATE); KJ_EXPECT(KJ_ASSERT_NONNULL(table->stringToId("dATE")) == HttpHeaderId::DATE); KJ_EXPECT(KJ_ASSERT_NONNULL(table->stringToId("Foo-Bar")) == fooBar); KJ_EXPECT(KJ_ASSERT_NONNULL(table->stringToId("foo-BAR")) == fooBar); KJ_EXPECT(table->stringToId("foobar") == nullptr); KJ_EXPECT(table->stringToId("barfoo") == nullptr); } KJ_TEST("HttpHeaders::parseRequest") { HttpHeaderTable::Builder builder; auto fooBar = builder.add("Foo-Bar"); auto bazQux = builder.add("baz-qux"); auto table = builder.build(); HttpHeaders headers(*table); auto text = kj::heapString( "POST /some/path \t HTTP/1.1\r\n" "Foo-BaR: Baz\r\n" "Host: example.com\r\n" "Content-Length: 123\r\n" "DATE: early\r\n" "other-Header: yep\r\n" "with.dots: sure\r\n" "\r\n"); auto result = headers.tryParseRequest(text.asArray()).get(); KJ_EXPECT(result.method == HttpMethod::POST); KJ_EXPECT(result.url == "/some/path"); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(HttpHeaderId::HOST)) == "example.com"); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(HttpHeaderId::DATE)) == "early"); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(fooBar)) == "Baz"); KJ_EXPECT(headers.get(bazQux) == nullptr); KJ_EXPECT(headers.get(HttpHeaderId::CONTENT_TYPE) == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(HttpHeaderId::CONTENT_LENGTH)) == "123"); KJ_EXPECT(headers.get(HttpHeaderId::TRANSFER_ENCODING) == nullptr); std::map unpackedHeaders; headers.forEach([&](kj::StringPtr name, kj::StringPtr value) { KJ_EXPECT(unpackedHeaders.insert(std::make_pair(name, value)).second); }); KJ_EXPECT(unpackedHeaders.size() == 6); KJ_EXPECT(unpackedHeaders["Content-Length"] == "123"); KJ_EXPECT(unpackedHeaders["Host"] == "example.com"); KJ_EXPECT(unpackedHeaders["Date"] == "early"); KJ_EXPECT(unpackedHeaders["Foo-Bar"] == "Baz"); KJ_EXPECT(unpackedHeaders["other-Header"] == "yep"); KJ_EXPECT(unpackedHeaders["with.dots"] == "sure"); KJ_EXPECT(headers.serializeRequest(result.method, result.url) == "POST /some/path HTTP/1.1\r\n" "Content-Length: 123\r\n" "Host: example.com\r\n" "Date: early\r\n" "Foo-Bar: Baz\r\n" "other-Header: yep\r\n" "with.dots: sure\r\n" "\r\n"); } KJ_TEST("HttpHeaders::parseResponse") { HttpHeaderTable::Builder builder; auto fooBar = builder.add("Foo-Bar"); auto bazQux = builder.add("baz-qux"); auto table = builder.build(); HttpHeaders headers(*table); auto text = kj::heapString( "HTTP/1.1\t\t 418\t I'm a teapot\r\n" "Foo-BaR: Baz\r\n" "Host: example.com\r\n" "Content-Length: 123\r\n" "DATE: early\r\n" "other-Header: yep\r\n" "\r\n"); auto result = headers.tryParseResponse(text.asArray()).get(); KJ_EXPECT(result.statusCode == 418); KJ_EXPECT(result.statusText == "I'm a teapot"); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(HttpHeaderId::HOST)) == "example.com"); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(HttpHeaderId::DATE)) == "early"); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(fooBar)) == "Baz"); KJ_EXPECT(headers.get(bazQux) == nullptr); KJ_EXPECT(headers.get(HttpHeaderId::CONTENT_TYPE) == nullptr); KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(HttpHeaderId::CONTENT_LENGTH)) == "123"); KJ_EXPECT(headers.get(HttpHeaderId::TRANSFER_ENCODING) == nullptr); std::map unpackedHeaders; headers.forEach([&](kj::StringPtr name, kj::StringPtr value) { KJ_EXPECT(unpackedHeaders.insert(std::make_pair(name, value)).second); }); KJ_EXPECT(unpackedHeaders.size() == 5); KJ_EXPECT(unpackedHeaders["Content-Length"] == "123"); KJ_EXPECT(unpackedHeaders["Host"] == "example.com"); KJ_EXPECT(unpackedHeaders["Date"] == "early"); KJ_EXPECT(unpackedHeaders["Foo-Bar"] == "Baz"); KJ_EXPECT(unpackedHeaders["other-Header"] == "yep"); KJ_EXPECT(headers.serializeResponse( result.statusCode, result.statusText) == "HTTP/1.1 418 I'm a teapot\r\n" "Content-Length: 123\r\n" "Host: example.com\r\n" "Date: early\r\n" "Foo-Bar: Baz\r\n" "other-Header: yep\r\n" "\r\n"); } KJ_TEST("HttpHeaders parse invalid") { auto table = HttpHeaderTable::Builder().build(); HttpHeaders headers(*table); // NUL byte in request. { auto input = kj::heapString( "POST \0 /some/path \t HTTP/1.1\r\n" "Foo-BaR: Baz\r\n" "Host: example.com\r\n" "DATE: early\r\n" "other-Header: yep\r\n" "\r\n"); auto protocolError = headers.tryParseRequest(input).get(); KJ_EXPECT(protocolError.description == "ERROR: Request headers have no terminal newline.", protocolError.description); KJ_EXPECT(protocolError.rawContent.asChars() == input); } // Control character in header name. { auto input = kj::heapString( "POST /some/path \t HTTP/1.1\r\n" "Foo-BaR: Baz\r\n" "Cont\001ent-Length: 123\r\n" "DATE: early\r\n" "other-Header: yep\r\n" "\r\n"); auto protocolError = headers.tryParseRequest(input).get(); KJ_EXPECT(protocolError.description == "ERROR: The headers sent by your client are not valid.", protocolError.description); KJ_EXPECT(protocolError.rawContent.asChars() == input); } // Separator character in header name. { auto input = kj::heapString( "POST /some/path \t HTTP/1.1\r\n" "Foo-BaR: Baz\r\n" "Host: example.com\r\n" "DATE/: early\r\n" "other-Header: yep\r\n" "\r\n"); auto protocolError = headers.tryParseRequest(input).get(); KJ_EXPECT(protocolError.description == "ERROR: The headers sent by your client are not valid.", protocolError.description); KJ_EXPECT(protocolError.rawContent.asChars() == input); } // Response status code not numeric. { auto input = kj::heapString( "HTTP/1.1\t\t abc\t I'm a teapot\r\n" "Foo-BaR: Baz\r\n" "Host: example.com\r\n" "DATE: early\r\n" "other-Header: yep\r\n" "\r\n"); auto protocolError = headers.tryParseRequest(input).get(); KJ_EXPECT(protocolError.description == "ERROR: Unrecognized request method.", protocolError.description); KJ_EXPECT(protocolError.rawContent.asChars() == input); } } KJ_TEST("HttpHeaders validation") { auto table = HttpHeaderTable::Builder().build(); HttpHeaders headers(*table); headers.add("Valid-Name", "valid value"); // The HTTP RFC prohibits control characters, but browsers only prohibit \0, \r, and \n. KJ goes // with the browsers for compatibility. headers.add("Valid-Name", "valid\x01value"); // The HTTP RFC does not permit non-ASCII values. // KJ chooses to interpret them as UTF-8, to avoid the need for any expensive conversion. // Browsers apparently interpret them as LATIN-1. Applications can reinterpet these strings as // LATIN-1 easily enough if they really need to. headers.add("Valid-Name", u8"valid€value"); KJ_EXPECT_THROW_MESSAGE("invalid header name", headers.add("Invalid Name", "value")); KJ_EXPECT_THROW_MESSAGE("invalid header name", headers.add("Invalid@Name", "value")); KJ_EXPECT_THROW_MESSAGE("invalid header value", headers.set(HttpHeaderId::HOST, "in\nvalid")); KJ_EXPECT_THROW_MESSAGE("invalid header value", headers.add("Valid-Name", "in\nvalid")); } KJ_TEST("HttpHeaders Set-Cookie handling") { HttpHeaderTable::Builder builder; auto hCookie = builder.add("Cookie"); auto hSetCookie = builder.add("Set-Cookie"); auto table = builder.build(); HttpHeaders headers(*table); headers.set(hCookie, "Foo"); headers.add("Cookie", "Bar"); headers.add("Cookie", "Baz"); headers.set(hSetCookie, "Foo"); headers.add("Set-Cookie", "Bar"); headers.add("Set-Cookie", "Baz"); auto text = headers.toString(); KJ_EXPECT(text == "Cookie: Foo, Bar, Baz\r\n" "Set-Cookie: Foo\r\n" "Set-Cookie: Bar\r\n" "Set-Cookie: Baz\r\n" "\r\n", text); } // ======================================================================================= class ReadFragmenter final: public kj::AsyncIoStream { public: ReadFragmenter(AsyncIoStream& inner, size_t limit): inner(inner), limit(limit) {} Promise read(void* buffer, size_t minBytes, size_t maxBytes) override { return inner.read(buffer, minBytes, kj::max(minBytes, kj::min(limit, maxBytes))); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return inner.tryRead(buffer, minBytes, kj::max(minBytes, kj::min(limit, maxBytes))); } Maybe tryGetLength() override { return inner.tryGetLength(); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return inner.pumpTo(output, amount); } Promise write(const void* buffer, size_t size) override { return inner.write(buffer, size); } Promise write(ArrayPtr> pieces) override { return inner.write(pieces); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { return inner.tryPumpFrom(input, amount); } Promise whenWriteDisconnected() override { return inner.whenWriteDisconnected(); } void shutdownWrite() override { return inner.shutdownWrite(); } void abortRead() override { return inner.abortRead(); } void getsockopt(int level, int option, void* value, uint* length) override { return inner.getsockopt(level, option, value, length); } void setsockopt(int level, int option, const void* value, uint length) override { return inner.setsockopt(level, option, value, length); } void getsockname(struct sockaddr* addr, uint* length) override { return inner.getsockname(addr, length); } void getpeername(struct sockaddr* addr, uint* length) override { return inner.getsockname(addr, length); } private: kj::AsyncIoStream& inner; size_t limit; }; template class InitializeableArray: public Array { public: InitializeableArray(std::initializer_list init) : Array(kj::heapArray(init)) {} }; enum Side { BOTH, CLIENT_ONLY, SERVER_ONLY }; struct HeaderTestCase { HttpHeaderId id; kj::StringPtr value; }; struct HttpRequestTestCase { kj::StringPtr raw; HttpMethod method; kj::StringPtr path; InitializeableArray requestHeaders; kj::Maybe requestBodySize; InitializeableArray requestBodyParts; Side side = BOTH; // TODO(cleanup): Delete this constructor if/when we move to C++14. HttpRequestTestCase(kj::StringPtr raw, HttpMethod method, kj::StringPtr path, InitializeableArray requestHeaders, kj::Maybe requestBodySize, InitializeableArray requestBodyParts, Side side = BOTH) : raw(raw), method(method), path(path), requestHeaders(kj::mv(requestHeaders)), requestBodySize(requestBodySize), requestBodyParts(kj::mv(requestBodyParts)), side(side) {} }; struct HttpResponseTestCase { kj::StringPtr raw; uint64_t statusCode; kj::StringPtr statusText; InitializeableArray responseHeaders; kj::Maybe responseBodySize; InitializeableArray responseBodyParts; HttpMethod method = HttpMethod::GET; Side side = BOTH; // TODO(cleanup): Delete this constructor if/when we move to C++14. HttpResponseTestCase(kj::StringPtr raw, uint64_t statusCode, kj::StringPtr statusText, InitializeableArray responseHeaders, kj::Maybe responseBodySize, InitializeableArray responseBodyParts, HttpMethod method = HttpMethod::GET, Side side = BOTH) : raw(raw), statusCode(statusCode), statusText(statusText), responseHeaders(kj::mv(responseHeaders)), responseBodySize(responseBodySize), responseBodyParts(kj::mv(responseBodyParts)), method(method), side(side) {} }; struct HttpTestCase { HttpRequestTestCase request; HttpResponseTestCase response; }; kj::Promise writeEach(kj::AsyncOutputStream& out, kj::ArrayPtr parts) { if (parts.size() == 0) return kj::READY_NOW; return out.write(parts[0].begin(), parts[0].size()) .then([&out,parts]() { return writeEach(out, parts.slice(1, parts.size())); }); } kj::Promise expectRead(kj::AsyncInputStream& in, kj::StringPtr expected) { if (expected.size() == 0) return kj::READY_NOW; auto buffer = kj::heapArray(expected.size()); auto promise = in.tryRead(buffer.begin(), 1, buffer.size()); return promise.then(kj::mvCapture(buffer, [&in,expected](kj::Array buffer, size_t amount) { if (amount == 0) { KJ_FAIL_ASSERT("expected data never sent", expected); } auto actual = buffer.slice(0, amount); if (memcmp(actual.begin(), expected.begin(), actual.size()) != 0) { KJ_FAIL_ASSERT("data from stream doesn't match expected", expected, actual); } return expectRead(in, expected.slice(amount)); })); } kj::Promise expectRead(kj::AsyncInputStream& in, kj::ArrayPtr expected) { if (expected.size() == 0) return kj::READY_NOW; auto buffer = kj::heapArray(expected.size()); auto promise = in.tryRead(buffer.begin(), 1, buffer.size()); return promise.then(kj::mvCapture(buffer, [&in,expected](kj::Array buffer, size_t amount) { if (amount == 0) { KJ_FAIL_ASSERT("expected data never sent", expected); } auto actual = buffer.slice(0, amount); if (memcmp(actual.begin(), expected.begin(), actual.size()) != 0) { KJ_FAIL_ASSERT("data from stream doesn't match expected", expected, actual); } return expectRead(in, expected.slice(amount, expected.size())); })); } void testHttpClientRequest(kj::WaitScope& waitScope, const HttpRequestTestCase& testCase, kj::TwoWayPipe pipe) { auto serverTask = expectRead(*pipe.ends[1], testCase.raw).then([&]() { static const char SIMPLE_RESPONSE[] = "HTTP/1.1 200 OK\r\n" "Content-Length: 0\r\n" "\r\n"; return pipe.ends[1]->write(SIMPLE_RESPONSE, strlen(SIMPLE_RESPONSE)); }).then([&]() -> kj::Promise { return kj::NEVER_DONE; }); HttpHeaderTable table; auto client = newHttpClient(table, *pipe.ends[0]); HttpHeaders headers(table); for (auto& header: testCase.requestHeaders) { headers.set(header.id, header.value); } auto request = client->request(testCase.method, testCase.path, headers, testCase.requestBodySize); if (testCase.requestBodyParts.size() > 0) { writeEach(*request.body, testCase.requestBodyParts).wait(waitScope); } request.body = nullptr; auto clientTask = request.response .then([&](HttpClient::Response&& response) { auto promise = response.body->readAllText(); return promise.attach(kj::mv(response.body)); }).ignoreResult(); serverTask.exclusiveJoin(kj::mv(clientTask)).wait(waitScope); // Verify no more data written by client. client = nullptr; pipe.ends[0]->shutdownWrite(); KJ_EXPECT(pipe.ends[1]->readAllText().wait(waitScope) == ""); } void testHttpClientResponse(kj::WaitScope& waitScope, const HttpResponseTestCase& testCase, size_t readFragmentSize, kj::TwoWayPipe pipe) { ReadFragmenter fragmenter(*pipe.ends[0], readFragmentSize); auto expectedReqText = testCase.method == HttpMethod::GET || testCase.method == HttpMethod::HEAD ? kj::str(testCase.method, " / HTTP/1.1\r\n\r\n") : kj::str(testCase.method, " / HTTP/1.1\r\nContent-Length: 0\r\n"); auto serverTask = expectRead(*pipe.ends[1], expectedReqText).then([&]() { return pipe.ends[1]->write(testCase.raw.begin(), testCase.raw.size()); }).then([&]() -> kj::Promise { pipe.ends[1]->shutdownWrite(); return kj::NEVER_DONE; }); HttpHeaderTable table; auto client = newHttpClient(table, fragmenter); HttpHeaders headers(table); auto request = client->request(testCase.method, "/", headers, uint64_t(0)); request.body = nullptr; auto clientTask = request.response .then([&](HttpClient::Response&& response) { KJ_EXPECT(response.statusCode == testCase.statusCode); KJ_EXPECT(response.statusText == testCase.statusText); for (auto& header: testCase.responseHeaders) { KJ_EXPECT(KJ_ASSERT_NONNULL(response.headers->get(header.id)) == header.value); } auto promise = response.body->readAllText(); return promise.attach(kj::mv(response.body)); }).then([&](kj::String body) { KJ_EXPECT(body == kj::strArray(testCase.responseBodyParts, ""), body); }); serverTask.exclusiveJoin(kj::mv(clientTask)).wait(waitScope); // Verify no more data written by client. client = nullptr; pipe.ends[0]->shutdownWrite(); KJ_EXPECT(pipe.ends[1]->readAllText().wait(waitScope) == ""); } void testHttpClient(kj::WaitScope& waitScope, HttpHeaderTable& table, HttpClient& client, const HttpTestCase& testCase) { KJ_CONTEXT(testCase.request.raw, testCase.response.raw); HttpHeaders headers(table); for (auto& header: testCase.request.requestHeaders) { headers.set(header.id, header.value); } auto request = client.request( testCase.request.method, testCase.request.path, headers, testCase.request.requestBodySize); for (auto& part: testCase.request.requestBodyParts) { request.body->write(part.begin(), part.size()).wait(waitScope); } request.body = nullptr; auto response = request.response.wait(waitScope); KJ_EXPECT(response.statusCode == testCase.response.statusCode); auto body = response.body->readAllText().wait(waitScope); if (testCase.request.method == HttpMethod::HEAD) { KJ_EXPECT(body == ""); } else { KJ_EXPECT(body == kj::strArray(testCase.response.responseBodyParts, ""), body); } } class TestHttpService final: public HttpService { public: TestHttpService(const HttpRequestTestCase& expectedRequest, const HttpResponseTestCase& response, HttpHeaderTable& table) : singleExpectedRequest(&expectedRequest), singleResponse(&response), responseHeaders(table) {} TestHttpService(kj::ArrayPtr testCases, HttpHeaderTable& table) : singleExpectedRequest(nullptr), singleResponse(nullptr), testCases(testCases), responseHeaders(table) {} uint getRequestCount() { return requestCount; } kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& responseSender) override { auto& expectedRequest = testCases == nullptr ? *singleExpectedRequest : testCases[requestCount % testCases.size()].request; auto& response = testCases == nullptr ? *singleResponse : testCases[requestCount % testCases.size()].response; ++requestCount; KJ_EXPECT(method == expectedRequest.method, method); KJ_EXPECT(url == expectedRequest.path, url); for (auto& header: expectedRequest.requestHeaders) { KJ_EXPECT(KJ_ASSERT_NONNULL(headers.get(header.id)) == header.value); } auto size = requestBody.tryGetLength(); KJ_IF_MAYBE(expectedSize, expectedRequest.requestBodySize) { KJ_IF_MAYBE(s, size) { KJ_EXPECT(*s == *expectedSize, *s); } else { KJ_FAIL_EXPECT("tryGetLength() returned nullptr; expected known size"); } } else { KJ_EXPECT(size == nullptr); } return requestBody.readAllText() .then([this,&expectedRequest,&response,&responseSender](kj::String text) { KJ_EXPECT(text == kj::strArray(expectedRequest.requestBodyParts, ""), text); responseHeaders.clear(); for (auto& header: response.responseHeaders) { responseHeaders.set(header.id, header.value); } auto stream = responseSender.send(response.statusCode, response.statusText, responseHeaders, response.responseBodySize); auto promise = writeEach(*stream, response.responseBodyParts); return promise.attach(kj::mv(stream)); }); } private: const HttpRequestTestCase* singleExpectedRequest; const HttpResponseTestCase* singleResponse; kj::ArrayPtr testCases; HttpHeaders responseHeaders; uint requestCount = 0; }; void testHttpServerRequest(kj::WaitScope& waitScope, kj::Timer& timer, const HttpRequestTestCase& requestCase, const HttpResponseTestCase& responseCase, kj::TwoWayPipe pipe) { HttpHeaderTable table; TestHttpService service(requestCase, responseCase, table); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); pipe.ends[1]->write(requestCase.raw.begin(), requestCase.raw.size()).wait(waitScope); pipe.ends[1]->shutdownWrite(); expectRead(*pipe.ends[1], responseCase.raw).wait(waitScope); listenTask.wait(waitScope); KJ_EXPECT(service.getRequestCount() == 1); } kj::ArrayPtr requestTestCases() { static const auto HUGE_STRING = kj::strArray(kj::repeat("abcdefgh", 4096), ""); static const auto HUGE_REQUEST = kj::str( "GET / HTTP/1.1\r\n" "Host: ", HUGE_STRING, "\r\n" "\r\n"); static const HttpRequestTestCase REQUEST_TEST_CASES[] { { "GET /foo/bar HTTP/1.1\r\n" "Host: example.com\r\n" "\r\n", HttpMethod::GET, "/foo/bar", {{HttpHeaderId::HOST, "example.com"}}, uint64_t(0), {}, }, { "HEAD /foo/bar HTTP/1.1\r\n" "Host: example.com\r\n" "\r\n", HttpMethod::HEAD, "/foo/bar", {{HttpHeaderId::HOST, "example.com"}}, uint64_t(0), {}, }, { "POST / HTTP/1.1\r\n" "Content-Length: 9\r\n" "Host: example.com\r\n" "Content-Type: text/plain\r\n" "\r\n" "foobarbaz", HttpMethod::POST, "/", { {HttpHeaderId::HOST, "example.com"}, {HttpHeaderId::CONTENT_TYPE, "text/plain"}, }, 9, { "foo", "bar", "baz" }, }, { "POST / HTTP/1.1\r\n" "Transfer-Encoding: chunked\r\n" "Host: example.com\r\n" "Content-Type: text/plain\r\n" "\r\n" "3\r\n" "foo\r\n" "6\r\n" "barbaz\r\n" "0\r\n" "\r\n", HttpMethod::POST, "/", { {HttpHeaderId::HOST, "example.com"}, {HttpHeaderId::CONTENT_TYPE, "text/plain"}, }, nullptr, { "foo", "barbaz" }, }, { "POST / HTTP/1.1\r\n" "Transfer-Encoding: chunked\r\n" "Host: example.com\r\n" "Content-Type: text/plain\r\n" "\r\n" "1d\r\n" "0123456789abcdef0123456789abc\r\n" "0\r\n" "\r\n", HttpMethod::POST, "/", { {HttpHeaderId::HOST, "example.com"}, {HttpHeaderId::CONTENT_TYPE, "text/plain"}, }, nullptr, { "0123456789abcdef0123456789abc" }, }, { HUGE_REQUEST, HttpMethod::GET, "/", {{HttpHeaderId::HOST, HUGE_STRING}}, uint64_t(0), {} }, { "GET /foo/bar HTTP/1.1\r\n" "Content-Length: 6\r\n" "Host: example.com\r\n" "\r\n" "foobar", HttpMethod::GET, "/foo/bar", {{HttpHeaderId::HOST, "example.com"}}, uint64_t(6), { "foobar" }, }, { "GET /foo/bar HTTP/1.1\r\n" "Transfer-Encoding: chunked\r\n" "Host: example.com\r\n" "\r\n" "3\r\n" "foo\r\n" "3\r\n" "bar\r\n" "0\r\n" "\r\n", HttpMethod::GET, "/foo/bar", {{HttpHeaderId::HOST, "example.com"}, {HttpHeaderId::TRANSFER_ENCODING, "chunked"}}, nullptr, { "foo", "bar" }, } }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents REQUEST_TEST_CASES from implicitly // casting to our return type. return kj::arrayPtr(REQUEST_TEST_CASES, kj::size(REQUEST_TEST_CASES)); } kj::ArrayPtr responseTestCases() { static const HttpResponseTestCase RESPONSE_TEST_CASES[] { { "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Connection: close\r\n" "\r\n" "baz qux", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}}, nullptr, {"baz qux"}, HttpMethod::GET, CLIENT_ONLY, // Server never sends connection: close }, { "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Transfer-Encoding: identity\r\n" "Content-Length: foobar\r\n" // intentionally wrong "\r\n" "baz qux", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}}, nullptr, {"baz qux"}, HttpMethod::GET, CLIENT_ONLY, // Server never sends transfer-encoding: identity }, { "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "\r\n" "baz qux", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}}, nullptr, {"baz qux"}, HttpMethod::GET, CLIENT_ONLY, // Server never sends non-delimited message }, { "HTTP/1.1 200 OK\r\n" "Content-Length: 123\r\n" "Content-Type: text/plain\r\n" "\r\n", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}}, 123, {}, HttpMethod::HEAD, }, { "HTTP/1.1 200 OK\r\n" "Content-Length: foobar\r\n" "Content-Type: text/plain\r\n" "\r\n", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}, {HttpHeaderId::CONTENT_LENGTH, "foobar"}}, 123, {}, HttpMethod::HEAD, }, // Zero-length expected size response to HEAD request has no Content-Length header. { "HTTP/1.1 200 OK\r\n" "\r\n", 200, "OK", {}, uint64_t(0), {}, HttpMethod::HEAD, }, { "HTTP/1.1 200 OK\r\n" "Content-Length: 8\r\n" "Content-Type: text/plain\r\n" "\r\n" "quxcorge", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}}, 8, { "qux", "corge" } }, { "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "Content-Type: text/plain\r\n" "\r\n" "3\r\n" "qux\r\n" "5\r\n" "corge\r\n" "0\r\n" "\r\n", 200, "OK", {{HttpHeaderId::CONTENT_TYPE, "text/plain"}}, nullptr, { "qux", "corge" } }, }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents RESPONSE_TEST_CASES from implicitly // casting to our return type. return kj::arrayPtr(RESPONSE_TEST_CASES, kj::size(RESPONSE_TEST_CASES)); } KJ_TEST("HttpClient requests") { KJ_HTTP_TEST_SETUP_IO; for (auto& testCase: requestTestCases()) { if (testCase.side == SERVER_ONLY) continue; KJ_CONTEXT(testCase.raw); testHttpClientRequest(waitScope, testCase, KJ_HTTP_TEST_CREATE_2PIPE); } } KJ_TEST("HttpClient responses") { KJ_HTTP_TEST_SETUP_IO; size_t FRAGMENT_SIZES[] = { 1, 2, 3, 4, 5, 6, 7, 8, 16, 31, kj::maxValue }; for (auto& testCase: responseTestCases()) { if (testCase.side == SERVER_ONLY) continue; for (size_t fragmentSize: FRAGMENT_SIZES) { KJ_CONTEXT(testCase.raw, fragmentSize); testHttpClientResponse(waitScope, testCase, fragmentSize, KJ_HTTP_TEST_CREATE_2PIPE); } } } KJ_TEST("HttpClient canceled write") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto serverPromise = pipe.ends[1]->readAllText(); { HttpHeaderTable table; auto client = newHttpClient(table, *pipe.ends[0]); auto body = kj::heapArray(4096); memset(body.begin(), 0xcf, body.size()); auto req = client->request(HttpMethod::POST, "/", HttpHeaders(table), uint64_t(4096)); // Note: This poll() forces the server to receive what was written so far. Otherwise, // cancelling the write below may in fact cancel the previous write as well. KJ_EXPECT(!serverPromise.poll(waitScope)); // Start a write and immediately cancel it. { auto ignore KJ_UNUSED = req.body->write(body.begin(), body.size()); } KJ_EXPECT_THROW_MESSAGE("overwrote", req.body->write("foo", 3).wait(waitScope)); req.body = nullptr; KJ_EXPECT(!serverPromise.poll(waitScope)); KJ_EXPECT_THROW_MESSAGE("can't start new request until previous request body", client->request(HttpMethod::GET, "/", HttpHeaders(table)).response.wait(waitScope)); } pipe.ends[0]->shutdownWrite(); auto text = serverPromise.wait(waitScope); KJ_EXPECT(text == "POST / HTTP/1.1\r\nContent-Length: 4096\r\n\r\n", text); } KJ_TEST("HttpClient chunked body gather-write") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto serverPromise = pipe.ends[1]->readAllText(); { HttpHeaderTable table; auto client = newHttpClient(table, *pipe.ends[0]); auto req = client->request(HttpMethod::POST, "/", HttpHeaders(table)); kj::ArrayPtr bodyParts[] = { "foo"_kj.asBytes(), " "_kj.asBytes(), "bar"_kj.asBytes(), " "_kj.asBytes(), "baz"_kj.asBytes() }; req.body->write(kj::arrayPtr(bodyParts, kj::size(bodyParts))).wait(waitScope); req.body = nullptr; // Wait for a response so the client has a chance to end the request body with a 0-chunk. kj::StringPtr responseText = "HTTP/1.1 204 No Content\r\n\r\n"; pipe.ends[1]->write(responseText.begin(), responseText.size()).wait(waitScope); auto response = req.response.wait(waitScope); } pipe.ends[0]->shutdownWrite(); auto text = serverPromise.wait(waitScope); KJ_EXPECT(text == "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n" "b\r\nfoo bar baz\r\n0\r\n\r\n", text); } KJ_TEST("HttpClient chunked body pump from fixed length stream") { class FixedBodyStream final: public kj::AsyncInputStream { Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { auto n = kj::min(body.size(), maxBytes); n = kj::max(n, minBytes); n = kj::min(n, body.size()); memcpy(buffer, body.begin(), n); body = body.slice(n); return n; } Maybe tryGetLength() override { return body.size(); } kj::StringPtr body = "foo bar baz"; }; KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto serverPromise = pipe.ends[1]->readAllText(); { HttpHeaderTable table; auto client = newHttpClient(table, *pipe.ends[0]); auto req = client->request(HttpMethod::POST, "/", HttpHeaders(table)); FixedBodyStream bodyStream; bodyStream.pumpTo(*req.body).wait(waitScope); req.body = nullptr; // Wait for a response so the client has a chance to end the request body with a 0-chunk. kj::StringPtr responseText = "HTTP/1.1 204 No Content\r\n\r\n"; pipe.ends[1]->write(responseText.begin(), responseText.size()).wait(waitScope); auto response = req.response.wait(waitScope); } pipe.ends[0]->shutdownWrite(); auto text = serverPromise.wait(waitScope); KJ_EXPECT(text == "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n" "b\r\nfoo bar baz\r\n0\r\n\r\n", text); } KJ_TEST("HttpServer requests") { HttpResponseTestCase RESPONSE = { "HTTP/1.1 200 OK\r\n" "Content-Length: 3\r\n" "\r\n" "foo", 200, "OK", {}, 3, {"foo"} }; HttpResponseTestCase HEAD_RESPONSE = { "HTTP/1.1 200 OK\r\n" "Content-Length: 3\r\n" "\r\n", 200, "OK", {}, 3, {"foo"} }; KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); for (auto& testCase: requestTestCases()) { if (testCase.side == CLIENT_ONLY) continue; KJ_CONTEXT(testCase.raw); testHttpServerRequest(waitScope, timer, testCase, testCase.method == HttpMethod::HEAD ? HEAD_RESPONSE : RESPONSE, KJ_HTTP_TEST_CREATE_2PIPE); } } KJ_TEST("HttpServer responses") { HttpRequestTestCase REQUEST = { "GET / HTTP/1.1\r\n" "\r\n", HttpMethod::GET, "/", {}, uint64_t(0), {}, }; HttpRequestTestCase HEAD_REQUEST = { "HEAD / HTTP/1.1\r\n" "\r\n", HttpMethod::HEAD, "/", {}, uint64_t(0), {}, }; KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); for (auto& testCase: responseTestCases()) { if (testCase.side == CLIENT_ONLY) continue; KJ_CONTEXT(testCase.raw); testHttpServerRequest(waitScope, timer, testCase.method == HttpMethod::HEAD ? HEAD_REQUEST : REQUEST, testCase, KJ_HTTP_TEST_CREATE_2PIPE); } } // ----------------------------------------------------------------------------- kj::ArrayPtr pipelineTestCases() { static const HttpTestCase PIPELINE_TESTS[] = { { { "GET / HTTP/1.1\r\n" "\r\n", HttpMethod::GET, "/", {}, uint64_t(0), {}, }, { "HTTP/1.1 200 OK\r\n" "Content-Length: 7\r\n" "\r\n" "foo bar", 200, "OK", {}, 7, { "foo bar" } }, }, { { "POST /foo HTTP/1.1\r\n" "Content-Length: 6\r\n" "\r\n" "grault", HttpMethod::POST, "/foo", {}, 6, { "grault" }, }, { "HTTP/1.1 404 Not Found\r\n" "Content-Length: 13\r\n" "\r\n" "baz qux corge", 404, "Not Found", {}, 13, { "baz qux corge" } }, }, // Throw a zero-size request/response into the pipeline to check for a bug that existed with // them previously. { { "POST /foo HTTP/1.1\r\n" "Content-Length: 0\r\n" "\r\n", HttpMethod::POST, "/foo", {}, uint64_t(0), {}, }, { "HTTP/1.1 200 OK\r\n" "Content-Length: 0\r\n" "\r\n", 200, "OK", {}, uint64_t(0), {} }, }, // Also a zero-size chunked request/response. { { "POST /foo HTTP/1.1\r\n" "Transfer-Encoding: chunked\r\n" "\r\n" "0\r\n" "\r\n", HttpMethod::POST, "/foo", {}, nullptr, {}, }, { "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "\r\n" "0\r\n" "\r\n", 200, "OK", {}, nullptr, {} }, }, { { "POST /bar HTTP/1.1\r\n" "Transfer-Encoding: chunked\r\n" "\r\n" "6\r\n" "garply\r\n" "5\r\n" "waldo\r\n" "0\r\n" "\r\n", HttpMethod::POST, "/bar", {}, nullptr, { "garply", "waldo" }, }, { "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "\r\n" "4\r\n" "fred\r\n" "5\r\n" "plugh\r\n" "0\r\n" "\r\n", 200, "OK", {}, nullptr, { "fred", "plugh" } }, }, { { "HEAD / HTTP/1.1\r\n" "\r\n", HttpMethod::HEAD, "/", {}, uint64_t(0), {}, }, { "HTTP/1.1 200 OK\r\n" "Content-Length: 7\r\n" "\r\n", 200, "OK", {}, 7, { "foo bar" } }, }, // Zero-length expected size response to HEAD request has no Content-Length header. { { "HEAD / HTTP/1.1\r\n" "\r\n", HttpMethod::HEAD, "/", {}, uint64_t(0), {}, }, { "HTTP/1.1 200 OK\r\n" "\r\n", 200, "OK", {}, uint64_t(0), {}, HttpMethod::HEAD, }, }, }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents RESPONSE_TEST_CASES from implicitly // casting to our return type. return kj::arrayPtr(PIPELINE_TESTS, kj::size(PIPELINE_TESTS)); } KJ_TEST("HttpClient pipeline") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; kj::Promise writeResponsesPromise = kj::READY_NOW; for (auto& testCase: PIPELINE_TESTS) { writeResponsesPromise = writeResponsesPromise .then([&]() { return expectRead(*pipe.ends[1], testCase.request.raw); }).then([&]() { return pipe.ends[1]->write(testCase.response.raw.begin(), testCase.response.raw.size()); }); } HttpHeaderTable table; auto client = newHttpClient(table, *pipe.ends[0]); for (auto& testCase: PIPELINE_TESTS) { testHttpClient(waitScope, table, *client, testCase); } client = nullptr; pipe.ends[0]->shutdownWrite(); writeResponsesPromise.wait(waitScope); } KJ_TEST("HttpClient parallel pipeline") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; kj::Promise readRequestsPromise = kj::READY_NOW; kj::Promise writeResponsesPromise = kj::READY_NOW; for (auto& testCase: PIPELINE_TESTS) { auto forked = readRequestsPromise .then([&]() { return expectRead(*pipe.ends[1], testCase.request.raw); }).fork(); readRequestsPromise = forked.addBranch(); // Don't write each response until the corresponding request is received. auto promises = kj::heapArrayBuilder>(2); promises.add(forked.addBranch()); promises.add(kj::mv(writeResponsesPromise)); writeResponsesPromise = kj::joinPromises(promises.finish()).then([&]() { return pipe.ends[1]->write(testCase.response.raw.begin(), testCase.response.raw.size()); }); } HttpHeaderTable table; auto client = newHttpClient(table, *pipe.ends[0]); auto responsePromises = KJ_MAP(testCase, PIPELINE_TESTS) { KJ_CONTEXT(testCase.request.raw, testCase.response.raw); HttpHeaders headers(table); for (auto& header: testCase.request.requestHeaders) { headers.set(header.id, header.value); } auto request = client->request( testCase.request.method, testCase.request.path, headers, testCase.request.requestBodySize); for (auto& part: testCase.request.requestBodyParts) { request.body->write(part.begin(), part.size()).wait(waitScope); } return kj::mv(request.response); }; for (auto i: kj::indices(PIPELINE_TESTS)) { auto& testCase = PIPELINE_TESTS[i]; auto response = responsePromises[i].wait(waitScope); KJ_EXPECT(response.statusCode == testCase.response.statusCode); auto body = response.body->readAllText().wait(waitScope); if (testCase.request.method == HttpMethod::HEAD) { KJ_EXPECT(body == ""); } else { KJ_EXPECT(body == kj::strArray(testCase.response.responseBodyParts, ""), body); } } client = nullptr; pipe.ends[0]->shutdownWrite(); writeResponsesPromise.wait(waitScope); } KJ_TEST("HttpServer pipeline") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; TestHttpService service(PIPELINE_TESTS, table); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); for (auto& testCase: PIPELINE_TESTS) { KJ_CONTEXT(testCase.request.raw, testCase.response.raw); pipe.ends[1]->write(testCase.request.raw.begin(), testCase.request.raw.size()) .wait(waitScope); expectRead(*pipe.ends[1], testCase.response.raw).wait(waitScope); } pipe.ends[1]->shutdownWrite(); listenTask.wait(waitScope); KJ_EXPECT(service.getRequestCount() == kj::size(PIPELINE_TESTS)); } KJ_TEST("HttpServer parallel pipeline") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto allRequestText = kj::strArray(KJ_MAP(testCase, PIPELINE_TESTS) { return testCase.request.raw; }, ""); auto allResponseText = kj::strArray(KJ_MAP(testCase, PIPELINE_TESTS) { return testCase.response.raw; }, ""); HttpHeaderTable table; TestHttpService service(PIPELINE_TESTS, table); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); pipe.ends[1]->write(allRequestText.begin(), allRequestText.size()).wait(waitScope); pipe.ends[1]->shutdownWrite(); auto rawResponse = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(rawResponse == allResponseText, rawResponse); listenTask.wait(waitScope); KJ_EXPECT(service.getRequestCount() == kj::size(PIPELINE_TESTS)); } KJ_TEST("HttpClient <-> HttpServer") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; TestHttpService service(PIPELINE_TESTS, table); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[1])); auto client = newHttpClient(table, *pipe.ends[0]); for (auto& testCase: PIPELINE_TESTS) { testHttpClient(waitScope, table, *client, testCase); } client = nullptr; pipe.ends[0]->shutdownWrite(); listenTask.wait(waitScope); KJ_EXPECT(service.getRequestCount() == kj::size(PIPELINE_TESTS)); } // ----------------------------------------------------------------------------- KJ_TEST("HttpInputStream requests") { KJ_HTTP_TEST_SETUP_IO; kj::HttpHeaderTable table; auto pipe = kj::newOneWayPipe(); auto input = newHttpInputStream(*pipe.in, table); kj::Promise writeQueue = kj::READY_NOW; for (auto& testCase: requestTestCases()) { writeQueue = writeQueue.then([&]() { return pipe.out->write(testCase.raw.begin(), testCase.raw.size()); }); } writeQueue = writeQueue.then([&]() { pipe.out = nullptr; }); for (auto& testCase: requestTestCases()) { KJ_CONTEXT(testCase.raw); KJ_ASSERT(input->awaitNextMessage().wait(waitScope)); auto req = input->readRequest().wait(waitScope); KJ_EXPECT(req.method == testCase.method); KJ_EXPECT(req.url == testCase.path); for (auto& header: testCase.requestHeaders) { KJ_EXPECT(KJ_ASSERT_NONNULL(req.headers.get(header.id)) == header.value); } auto body = req.body->readAllText().wait(waitScope); KJ_EXPECT(body == kj::strArray(testCase.requestBodyParts, "")); } writeQueue.wait(waitScope); KJ_EXPECT(!input->awaitNextMessage().wait(waitScope)); } KJ_TEST("HttpInputStream responses") { KJ_HTTP_TEST_SETUP_IO; kj::HttpHeaderTable table; auto pipe = kj::newOneWayPipe(); auto input = newHttpInputStream(*pipe.in, table); kj::Promise writeQueue = kj::READY_NOW; for (auto& testCase: responseTestCases()) { if (testCase.side == CLIENT_ONLY) continue; // skip Connection: close case. writeQueue = writeQueue.then([&]() { return pipe.out->write(testCase.raw.begin(), testCase.raw.size()); }); } writeQueue = writeQueue.then([&]() { pipe.out = nullptr; }); for (auto& testCase: responseTestCases()) { if (testCase.side == CLIENT_ONLY) continue; // skip Connection: close case. KJ_CONTEXT(testCase.raw); KJ_ASSERT(input->awaitNextMessage().wait(waitScope)); auto resp = input->readResponse(testCase.method).wait(waitScope); KJ_EXPECT(resp.statusCode == testCase.statusCode); KJ_EXPECT(resp.statusText == testCase.statusText); for (auto& header: testCase.responseHeaders) { KJ_EXPECT(KJ_ASSERT_NONNULL(resp.headers.get(header.id)) == header.value); } auto body = resp.body->readAllText().wait(waitScope); KJ_EXPECT(body == kj::strArray(testCase.responseBodyParts, "")); } writeQueue.wait(waitScope); KJ_EXPECT(!input->awaitNextMessage().wait(waitScope)); } KJ_TEST("HttpInputStream bare messages") { KJ_HTTP_TEST_SETUP_IO; kj::HttpHeaderTable table; auto pipe = kj::newOneWayPipe(); auto input = newHttpInputStream(*pipe.in, table); kj::StringPtr messages = "Content-Length: 6\r\n" "\r\n" "foobar" "Content-Length: 11\r\n" "Content-Type: some/type\r\n" "\r\n" "bazquxcorge" "Transfer-Encoding: chunked\r\n" "\r\n" "6\r\n" "grault\r\n" "b\r\n" "garplywaldo\r\n" "0\r\n" "\r\n"_kj; kj::Promise writeTask = pipe.out->write(messages.begin(), messages.size()) .then([&]() { pipe.out = nullptr; }); { KJ_ASSERT(input->awaitNextMessage().wait(waitScope)); auto message = input->readMessage().wait(waitScope); KJ_EXPECT(KJ_ASSERT_NONNULL(message.headers.get(HttpHeaderId::CONTENT_LENGTH)) == "6"); KJ_EXPECT(message.body->readAllText().wait(waitScope) == "foobar"); } { KJ_ASSERT(input->awaitNextMessage().wait(waitScope)); auto message = input->readMessage().wait(waitScope); KJ_EXPECT(KJ_ASSERT_NONNULL(message.headers.get(HttpHeaderId::CONTENT_LENGTH)) == "11"); KJ_EXPECT(KJ_ASSERT_NONNULL(message.headers.get(HttpHeaderId::CONTENT_TYPE)) == "some/type"); KJ_EXPECT(message.body->readAllText().wait(waitScope) == "bazquxcorge"); } { KJ_ASSERT(input->awaitNextMessage().wait(waitScope)); auto message = input->readMessage().wait(waitScope); KJ_EXPECT(KJ_ASSERT_NONNULL(message.headers.get(HttpHeaderId::TRANSFER_ENCODING)) == "chunked"); KJ_EXPECT(message.body->readAllText().wait(waitScope) == "graultgarplywaldo"); } writeTask.wait(waitScope); KJ_EXPECT(!input->awaitNextMessage().wait(waitScope)); } // ----------------------------------------------------------------------------- KJ_TEST("WebSocket core protocol") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto client = newWebSocket(kj::mv(pipe.ends[0]), nullptr); auto server = newWebSocket(kj::mv(pipe.ends[1]), nullptr); auto mediumString = kj::strArray(kj::repeat(kj::StringPtr("123456789"), 30), ""); auto bigString = kj::strArray(kj::repeat(kj::StringPtr("123456789"), 10000), ""); auto clientTask = client->send(kj::StringPtr("hello")) .then([&]() { return client->send(mediumString); }) .then([&]() { return client->send(bigString); }) .then([&]() { return client->send(kj::StringPtr("world").asBytes()); }) .then([&]() { return client->close(1234, "bored"); }); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "hello"); } { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == mediumString); } { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == bigString); } { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is>()); KJ_EXPECT(kj::str(message.get>().asChars()) == "world"); } { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get().code == 1234); KJ_EXPECT(message.get().reason == "bored"); } auto serverTask = server->close(4321, "whatever"); { auto message = client->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get().code == 4321); KJ_EXPECT(message.get().reason == "whatever"); } clientTask.wait(waitScope); serverTask.wait(waitScope); } KJ_TEST("WebSocket fragmented") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto client = kj::mv(pipe.ends[0]); auto server = newWebSocket(kj::mv(pipe.ends[1]), nullptr); byte DATA[] = { 0x01, 0x06, 'h', 'e', 'l', 'l', 'o', ' ', 0x00, 0x03, 'w', 'o', 'r', 0x80, 0x02, 'l', 'd', }; auto clientTask = client->write(DATA, sizeof(DATA)); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "hello world"); } clientTask.wait(waitScope); } class FakeEntropySource final: public EntropySource { public: void generate(kj::ArrayPtr buffer) override { static constexpr byte DUMMY[4] = { 12, 34, 56, 78 }; for (auto i: kj::indices(buffer)) { buffer[i] = DUMMY[i % sizeof(DUMMY)]; } } }; KJ_TEST("WebSocket masked") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; FakeEntropySource maskGenerator; auto client = kj::mv(pipe.ends[0]); auto server = newWebSocket(kj::mv(pipe.ends[1]), maskGenerator); byte DATA[] = { 0x81, 0x86, 12, 34, 56, 78, 'h' ^ 12, 'e' ^ 34, 'l' ^ 56, 'l' ^ 78, 'o' ^ 12, ' ' ^ 34, }; auto clientTask = client->write(DATA, sizeof(DATA)); auto serverTask = server->send(kj::StringPtr("hello ")); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "hello "); } expectRead(*client, DATA).wait(waitScope); clientTask.wait(waitScope); serverTask.wait(waitScope); } KJ_TEST("WebSocket unsolicited pong") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto client = kj::mv(pipe.ends[0]); auto server = newWebSocket(kj::mv(pipe.ends[1]), nullptr); byte DATA[] = { 0x01, 0x06, 'h', 'e', 'l', 'l', 'o', ' ', 0x8A, 0x03, 'f', 'o', 'o', 0x80, 0x05, 'w', 'o', 'r', 'l', 'd', }; auto clientTask = client->write(DATA, sizeof(DATA)); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "hello world"); } clientTask.wait(waitScope); } KJ_TEST("WebSocket ping") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto client = kj::mv(pipe.ends[0]); auto server = newWebSocket(kj::mv(pipe.ends[1]), nullptr); // Be extra-annoying by having the ping arrive between fragments. byte DATA[] = { 0x01, 0x06, 'h', 'e', 'l', 'l', 'o', ' ', 0x89, 0x03, 'f', 'o', 'o', 0x80, 0x05, 'w', 'o', 'r', 'l', 'd', }; auto clientTask = client->write(DATA, sizeof(DATA)); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "hello world"); } auto serverTask = server->send(kj::StringPtr("bar")); byte EXPECTED[] = { 0x8A, 0x03, 'f', 'o', 'o', // pong 0x81, 0x03, 'b', 'a', 'r', // message }; expectRead(*client, EXPECTED).wait(waitScope); clientTask.wait(waitScope); serverTask.wait(waitScope); } KJ_TEST("WebSocket ping mid-send") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto client = kj::mv(pipe.ends[0]); auto server = newWebSocket(kj::mv(pipe.ends[1]), nullptr); auto bigString = kj::strArray(kj::repeat(kj::StringPtr("12345678"), 65536), ""); auto serverTask = server->send(bigString).eagerlyEvaluate(nullptr); byte DATA[] = { 0x89, 0x03, 'f', 'o', 'o', // ping 0x81, 0x03, 'b', 'a', 'r', // some other message }; auto clientTask = client->write(DATA, sizeof(DATA)); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "bar"); } byte EXPECTED1[] = { 0x81, 0x7f, 0, 0, 0, 0, 0, 8, 0, 0 }; expectRead(*client, EXPECTED1).wait(waitScope); expectRead(*client, bigString).wait(waitScope); byte EXPECTED2[] = { 0x8A, 0x03, 'f', 'o', 'o' }; expectRead(*client, EXPECTED2).wait(waitScope); clientTask.wait(waitScope); serverTask.wait(waitScope); } class InputOutputPair final: public kj::AsyncIoStream { // Creates an AsyncIoStream out of an AsyncInputStream and an AsyncOutputStream. public: InputOutputPair(kj::Own in, kj::Own out) : in(kj::mv(in)), out(kj::mv(out)) {} kj::Promise read(void* buffer, size_t minBytes, size_t maxBytes) override { return in->read(buffer, minBytes, maxBytes); } kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return in->tryRead(buffer, minBytes, maxBytes); } Maybe tryGetLength() override { return in->tryGetLength(); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount = kj::maxValue) override { return in->pumpTo(output, amount); } kj::Promise write(const void* buffer, size_t size) override { return out->write(buffer, size); } kj::Promise write(kj::ArrayPtr> pieces) override { return out->write(pieces); } kj::Maybe> tryPumpFrom( kj::AsyncInputStream& input, uint64_t amount = kj::maxValue) override { return out->tryPumpFrom(input, amount); } Promise whenWriteDisconnected() override { return out->whenWriteDisconnected(); } void shutdownWrite() override { out = nullptr; } private: kj::Own in; kj::Own out; }; KJ_TEST("WebSocket double-ping mid-send") { KJ_HTTP_TEST_SETUP_IO; auto upPipe = newOneWayPipe(); auto downPipe = newOneWayPipe(); InputOutputPair client(kj::mv(downPipe.in), kj::mv(upPipe.out)); auto server = newWebSocket(kj::heap(kj::mv(upPipe.in), kj::mv(downPipe.out)), nullptr); auto bigString = kj::strArray(kj::repeat(kj::StringPtr("12345678"), 65536), ""); auto serverTask = server->send(bigString).eagerlyEvaluate(nullptr); byte DATA[] = { 0x89, 0x03, 'f', 'o', 'o', // ping 0x89, 0x03, 'q', 'u', 'x', // ping2 0x81, 0x03, 'b', 'a', 'r', // some other message }; auto clientTask = client.write(DATA, sizeof(DATA)); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "bar"); } byte EXPECTED1[] = { 0x81, 0x7f, 0, 0, 0, 0, 0, 8, 0, 0 }; expectRead(client, EXPECTED1).wait(waitScope); expectRead(client, bigString).wait(waitScope); byte EXPECTED2[] = { 0x8A, 0x03, 'q', 'u', 'x' }; expectRead(client, EXPECTED2).wait(waitScope); clientTask.wait(waitScope); serverTask.wait(waitScope); } KJ_TEST("WebSocket ping received during pong send") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto client = kj::mv(pipe.ends[0]); auto server = newWebSocket(kj::mv(pipe.ends[1]), nullptr); // Send a very large ping so that sending the pong takes a while. Then send a second ping // immediately after. byte PREFIX[] = { 0x89, 0x7f, 0, 0, 0, 0, 0, 8, 0, 0 }; auto bigString = kj::strArray(kj::repeat(kj::StringPtr("12345678"), 65536), ""); byte POSTFIX[] = { 0x89, 0x03, 'f', 'o', 'o', 0x81, 0x03, 'b', 'a', 'r', }; kj::ArrayPtr parts[] = {PREFIX, bigString.asBytes(), POSTFIX}; auto clientTask = client->write(parts); { auto message = server->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "bar"); } byte EXPECTED1[] = { 0x8A, 0x7f, 0, 0, 0, 0, 0, 8, 0, 0 }; expectRead(*client, EXPECTED1).wait(waitScope); expectRead(*client, bigString).wait(waitScope); byte EXPECTED2[] = { 0x8A, 0x03, 'f', 'o', 'o' }; expectRead(*client, EXPECTED2).wait(waitScope); clientTask.wait(waitScope); } KJ_TEST("WebSocket pump disconnect on send") { KJ_HTTP_TEST_SETUP_IO; auto pipe1 = KJ_HTTP_TEST_CREATE_2PIPE; auto pipe2 = KJ_HTTP_TEST_CREATE_2PIPE; auto client1 = newWebSocket(kj::mv(pipe1.ends[0]), nullptr); auto server1 = newWebSocket(kj::mv(pipe1.ends[1]), nullptr); auto client2 = newWebSocket(kj::mv(pipe2.ends[0]), nullptr); auto pumpTask = server1->pumpTo(*client2); auto sendTask = client1->send("hello"_kj); // Endpoint reads three bytes and then disconnects. char buffer[3]; pipe2.ends[1]->read(buffer, 3).wait(waitScope); pipe2.ends[1] = nullptr; // Pump throws disconnected. KJ_EXPECT_THROW_RECOVERABLE(DISCONNECTED, pumpTask.wait(waitScope)); // client1 managed to send its whole message into the pump, though. sendTask.wait(waitScope); } KJ_TEST("WebSocket pump disconnect on receive") { KJ_HTTP_TEST_SETUP_IO; auto pipe1 = KJ_HTTP_TEST_CREATE_2PIPE; auto pipe2 = KJ_HTTP_TEST_CREATE_2PIPE; auto server1 = newWebSocket(kj::mv(pipe1.ends[1]), nullptr); auto client2 = newWebSocket(kj::mv(pipe2.ends[0]), nullptr); auto server2 = newWebSocket(kj::mv(pipe2.ends[1]), nullptr); auto pumpTask = server1->pumpTo(*client2); auto receiveTask = server2->receive(); // Client sends three bytes of a valid message then disconnects. const char DATA[] = {0x01, 0x06, 'h'}; pipe1.ends[0]->write(DATA, 3).wait(waitScope); pipe1.ends[0] = nullptr; // The pump completes successfully, forwarding the disconnect. pumpTask.wait(waitScope); // The eventual receiver gets a disconnect execption. KJ_EXPECT_THROW(DISCONNECTED, receiveTask.wait(waitScope)); } class TestWebSocketService final: public HttpService, private kj::TaskSet::ErrorHandler { public: TestWebSocketService(HttpHeaderTable& headerTable, HttpHeaderId hMyHeader) : headerTable(headerTable), hMyHeader(hMyHeader), tasks(*this) {} kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { KJ_ASSERT(headers.isWebSocket()); HttpHeaders responseHeaders(headerTable); KJ_IF_MAYBE(h, headers.get(hMyHeader)) { responseHeaders.set(hMyHeader, kj::str("respond-", *h)); } if (url == "/return-error") { response.send(404, "Not Found", responseHeaders, uint64_t(0)); return kj::READY_NOW; } else if (url == "/websocket") { auto ws = response.acceptWebSocket(responseHeaders); return doWebSocket(*ws, "start-inline").attach(kj::mv(ws)); } else { KJ_FAIL_ASSERT("unexpected path", url); } } private: HttpHeaderTable& headerTable; HttpHeaderId hMyHeader; kj::TaskSet tasks; void taskFailed(kj::Exception&& exception) override { KJ_LOG(ERROR, exception); } static kj::Promise doWebSocket(WebSocket& ws, kj::StringPtr message) { auto copy = kj::str(message); return ws.send(copy).attach(kj::mv(copy)) .then([&ws]() { return ws.receive(); }).then([&ws](WebSocket::Message&& message) { KJ_SWITCH_ONEOF(message) { KJ_CASE_ONEOF(str, kj::String) { return doWebSocket(ws, kj::str("reply:", str)); } KJ_CASE_ONEOF(data, kj::Array) { return doWebSocket(ws, kj::str("reply:", data)); } KJ_CASE_ONEOF(close, WebSocket::Close) { auto reason = kj::str("close-reply:", close.reason); return ws.close(close.code + 1, reason).attach(kj::mv(reason)); } } KJ_UNREACHABLE; }); } }; const char WEBSOCKET_REQUEST_HANDSHAKE[] = " HTTP/1.1\r\n" "Connection: Upgrade\r\n" "Upgrade: websocket\r\n" "Sec-WebSocket-Key: DCI4TgwiOE4MIjhODCI4Tg==\r\n" "Sec-WebSocket-Version: 13\r\n" "My-Header: foo\r\n" "\r\n"; const char WEBSOCKET_RESPONSE_HANDSHAKE[] = "HTTP/1.1 101 Switching Protocols\r\n" "Connection: Upgrade\r\n" "Upgrade: websocket\r\n" "Sec-WebSocket-Accept: pShtIFKT0s8RYZvnWY/CrjQD8CM=\r\n" "My-Header: respond-foo\r\n" "\r\n"; const char WEBSOCKET_RESPONSE_HANDSHAKE_ERROR[] = "HTTP/1.1 404 Not Found\r\n" "Content-Length: 0\r\n" "My-Header: respond-foo\r\n" "\r\n"; const byte WEBSOCKET_FIRST_MESSAGE_INLINE[] = { 0x81, 0x0c, 's','t','a','r','t','-','i','n','l','i','n','e' }; const byte WEBSOCKET_SEND_MESSAGE[] = { 0x81, 0x83, 12, 34, 56, 78, 'b'^12, 'a'^34, 'r'^56 }; const byte WEBSOCKET_REPLY_MESSAGE[] = { 0x81, 0x09, 'r','e','p','l','y',':','b','a','r' }; const byte WEBSOCKET_SEND_CLOSE[] = { 0x88, 0x85, 12, 34, 56, 78, 0x12^12, 0x34^34, 'q'^56, 'u'^78, 'x'^12 }; const byte WEBSOCKET_REPLY_CLOSE[] = { 0x88, 0x11, 0x12, 0x35, 'c','l','o','s','e','-','r','e','p','l','y',':','q','u','x' }; template kj::ArrayPtr asBytes(const char (&chars)[s]) { return kj::ArrayPtr(chars, s - 1).asBytes(); } void testWebSocketClient(kj::WaitScope& waitScope, HttpHeaderTable& headerTable, kj::HttpHeaderId hMyHeader, HttpClient& client) { kj::HttpHeaders headers(headerTable); headers.set(hMyHeader, "foo"); auto response = client.openWebSocket("/websocket", headers).wait(waitScope); KJ_EXPECT(response.statusCode == 101); KJ_EXPECT(response.statusText == "Switching Protocols", response.statusText); KJ_EXPECT(KJ_ASSERT_NONNULL(response.headers->get(hMyHeader)) == "respond-foo"); KJ_ASSERT(response.webSocketOrBody.is>()); auto ws = kj::mv(response.webSocketOrBody.get>()); { auto message = ws->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "start-inline"); } ws->send(kj::StringPtr("bar")).wait(waitScope); { auto message = ws->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get() == "reply:bar"); } ws->close(0x1234, "qux").wait(waitScope); { auto message = ws->receive().wait(waitScope); KJ_ASSERT(message.is()); KJ_EXPECT(message.get().code == 0x1235); KJ_EXPECT(message.get().reason == "close-reply:qux"); } } inline kj::Promise writeA(kj::AsyncOutputStream& out, kj::ArrayPtr data) { return out.write(data.begin(), data.size()); } KJ_TEST("HttpClient WebSocket handshake") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto request = kj::str("GET /websocket", WEBSOCKET_REQUEST_HANDSHAKE); auto serverTask = expectRead(*pipe.ends[1], request) .then([&]() { return writeA(*pipe.ends[1], asBytes(WEBSOCKET_RESPONSE_HANDSHAKE)); }) .then([&]() { return writeA(*pipe.ends[1], WEBSOCKET_FIRST_MESSAGE_INLINE); }) .then([&]() { return expectRead(*pipe.ends[1], WEBSOCKET_SEND_MESSAGE); }) .then([&]() { return writeA(*pipe.ends[1], WEBSOCKET_REPLY_MESSAGE); }) .then([&]() { return expectRead(*pipe.ends[1], WEBSOCKET_SEND_CLOSE); }) .then([&]() { return writeA(*pipe.ends[1], WEBSOCKET_REPLY_CLOSE); }) .eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); HttpHeaderTable::Builder tableBuilder; HttpHeaderId hMyHeader = tableBuilder.add("My-Header"); auto headerTable = tableBuilder.build(); FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; auto client = newHttpClient(*headerTable, *pipe.ends[0], clientSettings); testWebSocketClient(waitScope, *headerTable, hMyHeader, *client); serverTask.wait(waitScope); } KJ_TEST("HttpClient WebSocket error") { KJ_HTTP_TEST_SETUP_IO; auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; auto request = kj::str("GET /websocket", WEBSOCKET_REQUEST_HANDSHAKE); auto serverTask = expectRead(*pipe.ends[1], request) .then([&]() { return writeA(*pipe.ends[1], asBytes(WEBSOCKET_RESPONSE_HANDSHAKE_ERROR)); }) .then([&]() { return expectRead(*pipe.ends[1], request); }) .then([&]() { return writeA(*pipe.ends[1], asBytes(WEBSOCKET_RESPONSE_HANDSHAKE_ERROR)); }) .eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); HttpHeaderTable::Builder tableBuilder; HttpHeaderId hMyHeader = tableBuilder.add("My-Header"); auto headerTable = tableBuilder.build(); FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; auto client = newHttpClient(*headerTable, *pipe.ends[0], clientSettings); kj::HttpHeaders headers(*headerTable); headers.set(hMyHeader, "foo"); { auto response = client->openWebSocket("/websocket", headers).wait(waitScope); KJ_EXPECT(response.statusCode == 404); KJ_EXPECT(response.statusText == "Not Found", response.statusText); KJ_EXPECT(KJ_ASSERT_NONNULL(response.headers->get(hMyHeader)) == "respond-foo"); KJ_ASSERT(response.webSocketOrBody.is>()); } { auto response = client->openWebSocket("/websocket", headers).wait(waitScope); KJ_EXPECT(response.statusCode == 404); KJ_EXPECT(response.statusText == "Not Found", response.statusText); KJ_EXPECT(KJ_ASSERT_NONNULL(response.headers->get(hMyHeader)) == "respond-foo"); KJ_ASSERT(response.webSocketOrBody.is>()); } serverTask.wait(waitScope); } KJ_TEST("HttpServer WebSocket handshake") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable::Builder tableBuilder; HttpHeaderId hMyHeader = tableBuilder.add("My-Header"); auto headerTable = tableBuilder.build(); TestWebSocketService service(*headerTable, hMyHeader); HttpServer server(timer, *headerTable, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); auto request = kj::str("GET /websocket", WEBSOCKET_REQUEST_HANDSHAKE); writeA(*pipe.ends[1], request.asBytes()).wait(waitScope); expectRead(*pipe.ends[1], WEBSOCKET_RESPONSE_HANDSHAKE).wait(waitScope); expectRead(*pipe.ends[1], WEBSOCKET_FIRST_MESSAGE_INLINE).wait(waitScope); writeA(*pipe.ends[1], WEBSOCKET_SEND_MESSAGE).wait(waitScope); expectRead(*pipe.ends[1], WEBSOCKET_REPLY_MESSAGE).wait(waitScope); writeA(*pipe.ends[1], WEBSOCKET_SEND_CLOSE).wait(waitScope); expectRead(*pipe.ends[1], WEBSOCKET_REPLY_CLOSE).wait(waitScope); listenTask.wait(waitScope); } KJ_TEST("HttpServer WebSocket handshake error") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable::Builder tableBuilder; HttpHeaderId hMyHeader = tableBuilder.add("My-Header"); auto headerTable = tableBuilder.build(); TestWebSocketService service(*headerTable, hMyHeader); HttpServer server(timer, *headerTable, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); auto request = kj::str("GET /return-error", WEBSOCKET_REQUEST_HANDSHAKE); writeA(*pipe.ends[1], request.asBytes()).wait(waitScope); expectRead(*pipe.ends[1], WEBSOCKET_RESPONSE_HANDSHAKE_ERROR).wait(waitScope); // Can send more requests! writeA(*pipe.ends[1], request.asBytes()).wait(waitScope); expectRead(*pipe.ends[1], WEBSOCKET_RESPONSE_HANDSHAKE_ERROR).wait(waitScope); pipe.ends[1]->shutdownWrite(); listenTask.wait(waitScope); } // ----------------------------------------------------------------------------- KJ_TEST("HttpServer request timeout") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; TestHttpService service(PIPELINE_TESTS, table); HttpServerSettings settings; settings.headerTimeout = 1 * kj::MILLISECONDS; HttpServer server(timer, table, service, settings); // Shouldn't hang! Should time out. auto promise = server.listenHttp(kj::mv(pipe.ends[0])); KJ_EXPECT(!promise.poll(waitScope)); timer.advanceTo(timer.now() + settings.headerTimeout / 2); KJ_EXPECT(!promise.poll(waitScope)); timer.advanceTo(timer.now() + settings.headerTimeout); promise.wait(waitScope); // Closes the connection without sending anything. KJ_EXPECT(pipe.ends[1]->readAllText().wait(waitScope) == ""); } KJ_TEST("HttpServer pipeline timeout") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; TestHttpService service(PIPELINE_TESTS, table); HttpServerSettings settings; settings.pipelineTimeout = 1 * kj::MILLISECONDS; HttpServer server(timer, table, service, settings); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); expectRead(*pipe.ends[1], PIPELINE_TESTS[0].response.raw).wait(waitScope); // Listen task should time out even though we didn't shutdown the socket. KJ_EXPECT(!listenTask.poll(waitScope)); timer.advanceTo(timer.now() + settings.pipelineTimeout / 2); KJ_EXPECT(!listenTask.poll(waitScope)); timer.advanceTo(timer.now() + settings.pipelineTimeout); listenTask.wait(waitScope); // In this case, no data is sent back. KJ_EXPECT(pipe.ends[1]->readAllText().wait(waitScope) == ""); } class BrokenHttpService final: public HttpService { // HttpService that doesn't send a response. public: BrokenHttpService() = default; explicit BrokenHttpService(kj::Exception&& exception): exception(kj::mv(exception)) {} kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& responseSender) override { return requestBody.readAllBytes().then([this](kj::Array&&) -> kj::Promise { KJ_IF_MAYBE(e, exception) { return kj::cp(*e); } else { return kj::READY_NOW; } }); } private: kj::Maybe exception; }; KJ_TEST("HttpServer no response") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "HTTP/1.1 500 Internal Server Error\r\n" "Connection: close\r\n" "Content-Length: 51\r\n" "Content-Type: text/plain\r\n" "\r\n" "ERROR: The HttpService did not generate a response.", text); } KJ_TEST("HttpServer disconnected") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service(KJ_EXCEPTION(DISCONNECTED, "disconnected")); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "", text); } KJ_TEST("HttpServer overloaded") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service(KJ_EXCEPTION(OVERLOADED, "overloaded")); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text.startsWith("HTTP/1.1 503 Service Unavailable"), text); } KJ_TEST("HttpServer unimplemented") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service(KJ_EXCEPTION(UNIMPLEMENTED, "unimplemented")); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text.startsWith("HTTP/1.1 501 Not Implemented"), text); } KJ_TEST("HttpServer threw exception") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service(KJ_EXCEPTION(FAILED, "failed")); HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text.startsWith("HTTP/1.1 500 Internal Server Error"), text); } KJ_TEST("HttpServer bad request") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); static constexpr auto request = "GET / HTTP/1.1\r\nbad request\r\n\r\n"_kj; auto writePromise = pipe.ends[1]->write(request.begin(), request.size()); auto response = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(writePromise.poll(waitScope)); writePromise.wait(waitScope); static constexpr auto expectedResponse = "HTTP/1.1 400 Bad Request\r\n" "Connection: close\r\n" "Content-Length: 53\r\n" "Content-Type: text/plain\r\n" "\r\n" "ERROR: The headers sent by your client are not valid."_kj; KJ_EXPECT(expectedResponse == response, expectedResponse, response); } KJ_TEST("HttpServer invalid method") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; BrokenHttpService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); static constexpr auto request = "bad request\r\n\r\n"_kj; auto writePromise = pipe.ends[1]->write(request.begin(), request.size()); auto response = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(writePromise.poll(waitScope)); writePromise.wait(waitScope); static constexpr auto expectedResponse = "HTTP/1.1 501 Not Implemented\r\n" "Connection: close\r\n" "Content-Length: 35\r\n" "Content-Type: text/plain\r\n" "\r\n" "ERROR: Unrecognized request method."_kj; KJ_EXPECT(expectedResponse == response, expectedResponse, response); } // Ensure that HttpServerSettings can continue to be constexpr. KJ_UNUSED static constexpr HttpServerSettings STATIC_CONSTEXPR_SETTINGS {}; class TestErrorHandler: public HttpServerErrorHandler { public: kj::Promise handleClientProtocolError( HttpHeaders::ProtocolError protocolError, kj::HttpService::Response& response) override { // In a real error handler, you should redact `protocolError.rawContent`. auto message = kj::str("Saw protocol error: ", protocolError.description, "; rawContent = ", encodeCEscape(protocolError.rawContent)); return sendError(400, "Bad Request", kj::mv(message), response); } kj::Promise handleApplicationError( kj::Exception exception, kj::Maybe response) override { return sendError(500, "Internal Server Error", kj::str("Saw application error: ", exception.getDescription()), response); } kj::Promise handleNoResponse(kj::HttpService::Response& response) override { return sendError(500, "Internal Server Error", kj::str("Saw no response."), response); } static TestErrorHandler instance; private: kj::Promise sendError(uint statusCode, kj::StringPtr statusText, String message, Maybe response) { KJ_IF_MAYBE(r, response) { HttpHeaderTable headerTable; HttpHeaders headers(headerTable); auto body = r->send(statusCode, statusText, headers, message.size()); return body->write(message.begin(), message.size()).attach(kj::mv(body), kj::mv(message)); } else { KJ_LOG(ERROR, "Saw an error but too late to report to client."); return kj::READY_NOW; } } }; TestErrorHandler TestErrorHandler::instance {}; KJ_TEST("HttpServer no response, custom error handler") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpServerSettings settings {}; settings.errorHandler = TestErrorHandler::instance; HttpHeaderTable table; BrokenHttpService service; HttpServer server(timer, table, service, settings); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "HTTP/1.1 500 Internal Server Error\r\n" "Connection: close\r\n" "Content-Length: 16\r\n" "\r\n" "Saw no response.", text); } KJ_TEST("HttpServer threw exception, custom error handler") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpServerSettings settings {}; settings.errorHandler = TestErrorHandler::instance; HttpHeaderTable table; BrokenHttpService service(KJ_EXCEPTION(FAILED, "failed")); HttpServer server(timer, table, service, settings); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "HTTP/1.1 500 Internal Server Error\r\n" "Connection: close\r\n" "Content-Length: 29\r\n" "\r\n" "Saw application error: failed", text); } KJ_TEST("HttpServer bad request, custom error handler") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpServerSettings settings {}; settings.errorHandler = TestErrorHandler::instance; HttpHeaderTable table; BrokenHttpService service; HttpServer server(timer, table, service, settings); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); static constexpr auto request = "bad request\r\n\r\n"_kj; auto writePromise = pipe.ends[1]->write(request.begin(), request.size()); auto response = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(writePromise.poll(waitScope)); writePromise.wait(waitScope); static constexpr auto expectedResponse = "HTTP/1.1 400 Bad Request\r\n" "Connection: close\r\n" "Content-Length: 87\r\n" "\r\n" "Saw protocol error: ERROR: Unrecognized request method.; " "rawContent = bad request\\000\\n"_kj; KJ_EXPECT(expectedResponse == response, expectedResponse, response); } class PartialResponseService final: public HttpService { // HttpService that sends a partial response then throws. public: kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { return requestBody.readAllBytes() .then([this,&response](kj::Array&&) -> kj::Promise { HttpHeaders headers(table); auto body = response.send(200, "OK", headers, 32); auto promise = body->write("foo", 3); return promise.attach(kj::mv(body)).then([]() -> kj::Promise { return KJ_EXCEPTION(FAILED, "failed"); }); }); } private: kj::Maybe exception; HttpHeaderTable table; }; KJ_TEST("HttpServer threw exception after starting response") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; PartialResponseService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); KJ_EXPECT_LOG(ERROR, "HttpService threw exception after generating a partial response"); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "HTTP/1.1 200 OK\r\n" "Content-Length: 32\r\n" "\r\n" "foo", text); } class PartialResponseNoThrowService final: public HttpService { // HttpService that sends a partial response then returns without throwing. public: kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { return requestBody.readAllBytes() .then([this,&response](kj::Array&&) -> kj::Promise { HttpHeaders headers(table); auto body = response.send(200, "OK", headers, 32); auto promise = body->write("foo", 3); return promise.attach(kj::mv(body)); }); } private: kj::Maybe exception; HttpHeaderTable table; }; KJ_TEST("HttpServer failed to write complete response but didn't throw") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; PartialResponseNoThrowService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "HTTP/1.1 200 OK\r\n" "Content-Length: 32\r\n" "\r\n" "foo", text); } class SimpleInputStream final: public kj::AsyncInputStream { // An InputStream that returns bytes out of a static string. public: SimpleInputStream(kj::StringPtr text) : unread(text.asBytes()) {} kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { size_t amount = kj::min(maxBytes, unread.size()); memcpy(buffer, unread.begin(), amount); unread = unread.slice(amount, unread.size()); return amount; } private: kj::ArrayPtr unread; }; class PumpResponseService final: public HttpService { // HttpService that uses pumpTo() to write a response, without carefully specifying how much to // pump, but the stream happens to be the right size. public: kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { return requestBody.readAllBytes() .then([this,&response](kj::Array&&) -> kj::Promise { HttpHeaders headers(table); kj::StringPtr text = "Hello, World!"; auto body = response.send(200, "OK", headers, text.size()); auto stream = kj::heap(text); auto promise = stream->pumpTo(*body); return promise.attach(kj::mv(body), kj::mv(stream)) .then([text](uint64_t amount) { KJ_EXPECT(amount == text.size()); }); }); } private: kj::Maybe exception; HttpHeaderTable table; }; KJ_TEST("HttpFixedLengthEntityWriter correctly implements tryPumpFrom") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; PumpResponseService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); // Do one request. pipe.ends[1]->write(PIPELINE_TESTS[0].request.raw.begin(), PIPELINE_TESTS[0].request.raw.size()) .wait(waitScope); pipe.ends[1]->shutdownWrite(); auto text = pipe.ends[1]->readAllText().wait(waitScope); KJ_EXPECT(text == "HTTP/1.1 200 OK\r\n" "Content-Length: 13\r\n" "\r\n" "Hello, World!", text); } class HangingHttpService final: public HttpService { // HttpService that hangs forever. public: kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& responseSender) override { kj::Promise result = kj::NEVER_DONE; ++inFlight; return result.attach(kj::defer([this]() { if (--inFlight == 0) { KJ_IF_MAYBE(f, onCancelFulfiller) { f->get()->fulfill(); } } })); } kj::Promise onCancel() { auto paf = kj::newPromiseAndFulfiller(); onCancelFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } uint inFlight = 0; private: kj::Maybe exception; kj::Maybe>> onCancelFulfiller; }; KJ_TEST("HttpServer cancels request when client disconnects") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable table; HangingHttpService service; HttpServer server(timer, table, service); auto listenTask = server.listenHttp(kj::mv(pipe.ends[0])); KJ_EXPECT(service.inFlight == 0); static constexpr auto request = "GET / HTTP/1.1\r\n\r\n"_kj; pipe.ends[1]->write(request.begin(), request.size()).wait(waitScope); auto cancelPromise = service.onCancel(); KJ_EXPECT(!cancelPromise.poll(waitScope)); KJ_EXPECT(service.inFlight == 1); // Disconnect client and verify server cancels. pipe.ends[1] = nullptr; KJ_ASSERT(cancelPromise.poll(waitScope)); KJ_EXPECT(service.inFlight == 0); cancelPromise.wait(waitScope); } // ----------------------------------------------------------------------------- KJ_TEST("newHttpService from HttpClient") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto frontPipe = KJ_HTTP_TEST_CREATE_2PIPE; auto backPipe = KJ_HTTP_TEST_CREATE_2PIPE; kj::Promise writeResponsesPromise = kj::READY_NOW; for (auto& testCase: PIPELINE_TESTS) { writeResponsesPromise = writeResponsesPromise .then([&]() { return expectRead(*backPipe.ends[1], testCase.request.raw); }).then([&]() { return backPipe.ends[1]->write(testCase.response.raw.begin(), testCase.response.raw.size()); }); } { HttpHeaderTable table; auto backClient = newHttpClient(table, *backPipe.ends[0]); auto frontService = newHttpService(*backClient); HttpServer frontServer(timer, table, *frontService); auto listenTask = frontServer.listenHttp(kj::mv(frontPipe.ends[1])); for (auto& testCase: PIPELINE_TESTS) { KJ_CONTEXT(testCase.request.raw, testCase.response.raw); frontPipe.ends[0]->write(testCase.request.raw.begin(), testCase.request.raw.size()) .wait(waitScope); expectRead(*frontPipe.ends[0], testCase.response.raw).wait(waitScope); } frontPipe.ends[0]->shutdownWrite(); listenTask.wait(waitScope); } backPipe.ends[0]->shutdownWrite(); writeResponsesPromise.wait(waitScope); } KJ_TEST("newHttpService from HttpClient WebSockets") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto frontPipe = KJ_HTTP_TEST_CREATE_2PIPE; auto backPipe = KJ_HTTP_TEST_CREATE_2PIPE; auto request = kj::str("GET /websocket", WEBSOCKET_REQUEST_HANDSHAKE); auto writeResponsesPromise = expectRead(*backPipe.ends[1], request) .then([&]() { return writeA(*backPipe.ends[1], asBytes(WEBSOCKET_RESPONSE_HANDSHAKE)); }) .then([&]() { return writeA(*backPipe.ends[1], WEBSOCKET_FIRST_MESSAGE_INLINE); }) .then([&]() { return expectRead(*backPipe.ends[1], WEBSOCKET_SEND_MESSAGE); }) .then([&]() { return writeA(*backPipe.ends[1], WEBSOCKET_REPLY_MESSAGE); }) .then([&]() { return expectRead(*backPipe.ends[1], WEBSOCKET_SEND_CLOSE); }) .then([&]() { return writeA(*backPipe.ends[1], WEBSOCKET_REPLY_CLOSE); }) .eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); { HttpHeaderTable table; FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; auto backClient = newHttpClient(table, *backPipe.ends[0], clientSettings); auto frontService = newHttpService(*backClient); HttpServer frontServer(timer, table, *frontService); auto listenTask = frontServer.listenHttp(kj::mv(frontPipe.ends[1])); writeA(*frontPipe.ends[0], request.asBytes()).wait(waitScope); expectRead(*frontPipe.ends[0], WEBSOCKET_RESPONSE_HANDSHAKE).wait(waitScope); expectRead(*frontPipe.ends[0], WEBSOCKET_FIRST_MESSAGE_INLINE).wait(waitScope); writeA(*frontPipe.ends[0], WEBSOCKET_SEND_MESSAGE).wait(waitScope); expectRead(*frontPipe.ends[0], WEBSOCKET_REPLY_MESSAGE).wait(waitScope); writeA(*frontPipe.ends[0], WEBSOCKET_SEND_CLOSE).wait(waitScope); expectRead(*frontPipe.ends[0], WEBSOCKET_REPLY_CLOSE).wait(waitScope); frontPipe.ends[0]->shutdownWrite(); listenTask.wait(waitScope); } writeResponsesPromise.wait(waitScope); } KJ_TEST("newHttpService from HttpClient WebSockets disconnect") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto frontPipe = KJ_HTTP_TEST_CREATE_2PIPE; auto backPipe = KJ_HTTP_TEST_CREATE_2PIPE; auto request = kj::str("GET /websocket", WEBSOCKET_REQUEST_HANDSHAKE); auto writeResponsesPromise = expectRead(*backPipe.ends[1], request) .then([&]() { return writeA(*backPipe.ends[1], asBytes(WEBSOCKET_RESPONSE_HANDSHAKE)); }) .then([&]() { return writeA(*backPipe.ends[1], WEBSOCKET_FIRST_MESSAGE_INLINE); }) .then([&]() { return expectRead(*backPipe.ends[1], WEBSOCKET_SEND_MESSAGE); }) .then([&]() { backPipe.ends[1]->shutdownWrite(); }) .eagerlyEvaluate([](kj::Exception&& e) { KJ_LOG(ERROR, e); }); { HttpHeaderTable table; FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; auto backClient = newHttpClient(table, *backPipe.ends[0], clientSettings); auto frontService = newHttpService(*backClient); HttpServer frontServer(timer, table, *frontService); auto listenTask = frontServer.listenHttp(kj::mv(frontPipe.ends[1])); writeA(*frontPipe.ends[0], request.asBytes()).wait(waitScope); expectRead(*frontPipe.ends[0], WEBSOCKET_RESPONSE_HANDSHAKE).wait(waitScope); expectRead(*frontPipe.ends[0], WEBSOCKET_FIRST_MESSAGE_INLINE).wait(waitScope); writeA(*frontPipe.ends[0], WEBSOCKET_SEND_MESSAGE).wait(waitScope); KJ_EXPECT(frontPipe.ends[0]->readAllText().wait(waitScope) == ""); frontPipe.ends[0]->shutdownWrite(); listenTask.wait(waitScope); } writeResponsesPromise.wait(waitScope); } // ----------------------------------------------------------------------------- KJ_TEST("newHttpClient from HttpService") { auto PIPELINE_TESTS = pipelineTestCases(); KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); HttpHeaderTable table; TestHttpService service(PIPELINE_TESTS, table); auto client = newHttpClient(service); for (auto& testCase: PIPELINE_TESTS) { testHttpClient(waitScope, table, *client, testCase); } } KJ_TEST("newHttpClient from HttpService WebSockets") { KJ_HTTP_TEST_SETUP_IO; kj::TimerImpl timer(kj::origin()); auto pipe = KJ_HTTP_TEST_CREATE_2PIPE; HttpHeaderTable::Builder tableBuilder; HttpHeaderId hMyHeader = tableBuilder.add("My-Header"); auto headerTable = tableBuilder.build(); TestWebSocketService service(*headerTable, hMyHeader); auto client = newHttpClient(service); testWebSocketClient(waitScope, *headerTable, hMyHeader, *client); } KJ_TEST("adapted client/server propagates request exceptions like non-adapted client") { KJ_HTTP_TEST_SETUP_IO; HttpHeaderTable table; HttpHeaders headers(table); class FailingHttpClient final: public HttpClient { public: Request request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::Maybe expectedBodySize = nullptr) override { KJ_FAIL_ASSERT("request_fail"); } kj::Promise openWebSocket( kj::StringPtr url, const HttpHeaders& headers) override { KJ_FAIL_ASSERT("websocket_fail"); } }; auto rawClient = kj::heap(); auto innerClient = kj::heap(); auto adaptedService = kj::newHttpService(*innerClient).attach(kj::mv(innerClient)); auto adaptedClient = kj::newHttpClient(*adaptedService).attach(kj::mv(adaptedService)); KJ_EXPECT_THROW_MESSAGE("request_fail", rawClient->request(HttpMethod::POST, "/"_kj, headers)); KJ_EXPECT_THROW_MESSAGE("request_fail", adaptedClient->request(HttpMethod::POST, "/"_kj, headers)); KJ_EXPECT_THROW_MESSAGE("websocket_fail", rawClient->openWebSocket("/"_kj, headers)); KJ_EXPECT_THROW_MESSAGE("websocket_fail", adaptedClient->openWebSocket("/"_kj, headers)); } class DelayedCompletionHttpService final: public HttpService { public: DelayedCompletionHttpService(HttpHeaderTable& table, kj::Maybe expectedLength) : table(table), expectedLength(expectedLength) {} kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { auto stream = response.send(200, "OK", HttpHeaders(table), expectedLength); auto promise = stream->write("foo", 3); return promise.attach(kj::mv(stream)).then([this]() { return kj::mv(paf.promise); }); } kj::PromiseFulfiller& getFulfiller() { return *paf.fulfiller; } private: HttpHeaderTable& table; kj::Maybe expectedLength; kj::PromiseFulfillerPair paf = kj::newPromiseAndFulfiller(); }; void doDelayedCompletionTest(bool exception, kj::Maybe expectedLength) noexcept { KJ_HTTP_TEST_SETUP_IO; HttpHeaderTable table; DelayedCompletionHttpService service(table, expectedLength); auto client = newHttpClient(service); auto resp = client->request(HttpMethod::GET, "/", HttpHeaders(table), uint64_t(0)) .response.wait(waitScope); KJ_EXPECT(resp.statusCode == 200); // Read "foo" from the response body: works char buffer[16]; KJ_ASSERT(resp.body->tryRead(buffer, 1, sizeof(buffer)).wait(waitScope) == 3); buffer[3] = '\0'; KJ_EXPECT(buffer == "foo"_kj); // But reading any more hangs. auto promise = resp.body->tryRead(buffer, 1, sizeof(buffer)); KJ_EXPECT(!promise.poll(waitScope)); // Until we cause the service to return. if (exception) { service.getFulfiller().reject(KJ_EXCEPTION(FAILED, "service-side failure")); } else { service.getFulfiller().fulfill(); } KJ_ASSERT(promise.poll(waitScope)); if (exception) { KJ_EXPECT_THROW_MESSAGE("service-side failure", promise.wait(waitScope)); } else { promise.wait(waitScope); } }; KJ_TEST("adapted client waits for service to complete before returning EOF on response stream") { doDelayedCompletionTest(false, uint64_t(3)); } KJ_TEST("adapted client waits for service to complete before returning EOF on chunked response") { doDelayedCompletionTest(false, nullptr); } KJ_TEST("adapted client propagates throw from service after complete response body sent") { doDelayedCompletionTest(true, uint64_t(3)); } KJ_TEST("adapted client propagates throw from service after incomplete response body sent") { doDelayedCompletionTest(true, uint64_t(6)); } KJ_TEST("adapted client propagates throw from service after chunked response body sent") { doDelayedCompletionTest(true, nullptr); } class DelayedCompletionWebSocketHttpService final: public HttpService { public: DelayedCompletionWebSocketHttpService(HttpHeaderTable& table, bool closeUpstreamFirst) : table(table), closeUpstreamFirst(closeUpstreamFirst) {} kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { KJ_ASSERT(headers.isWebSocket()); auto ws = response.acceptWebSocket(HttpHeaders(table)); kj::Promise promise = kj::READY_NOW; if (closeUpstreamFirst) { // Wait for a close message from the client before starting. promise = promise.then([&ws = *ws]() { return ws.receive(); }).ignoreResult(); } promise = promise .then([&ws = *ws]() { return ws.send("foo"_kj); }) .then([&ws = *ws]() { return ws.close(1234, "closed"_kj); }); if (!closeUpstreamFirst) { // Wait for a close message from the client at the end. promise = promise.then([&ws = *ws]() { return ws.receive(); }).ignoreResult(); } return promise.attach(kj::mv(ws)).then([this]() { return kj::mv(paf.promise); }); } kj::PromiseFulfiller& getFulfiller() { return *paf.fulfiller; } private: HttpHeaderTable& table; bool closeUpstreamFirst; kj::PromiseFulfillerPair paf = kj::newPromiseAndFulfiller(); }; void doDelayedCompletionWebSocketTest(bool exception, bool closeUpstreamFirst) noexcept { KJ_HTTP_TEST_SETUP_IO; HttpHeaderTable table; DelayedCompletionWebSocketHttpService service(table, closeUpstreamFirst); auto client = newHttpClient(service); auto resp = client->openWebSocket("/", HttpHeaders(table)).wait(waitScope); auto ws = kj::mv(KJ_ASSERT_NONNULL(resp.webSocketOrBody.tryGet>())); if (closeUpstreamFirst) { // Send "close" immediately. ws->close(1234, "whatever"_kj).wait(waitScope); } // Read "foo" from the WebSocket: works { auto msg = ws->receive().wait(waitScope); KJ_ASSERT(msg.is()); KJ_ASSERT(msg.get() == "foo"); } kj::Promise promise = nullptr; if (closeUpstreamFirst) { // Receiving the close hangs. promise = ws->receive() .then([](WebSocket::Message&& msg) { KJ_EXPECT(msg.is()); }); } else { auto msg = ws->receive().wait(waitScope); KJ_ASSERT(msg.is()); // Sending a close hangs. promise = ws->close(1234, "whatever"_kj); } KJ_EXPECT(!promise.poll(waitScope)); // Until we cause the service to return. if (exception) { service.getFulfiller().reject(KJ_EXCEPTION(FAILED, "service-side failure")); } else { service.getFulfiller().fulfill(); } KJ_ASSERT(promise.poll(waitScope)); if (exception) { KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("service-side failure", promise.wait(waitScope)); } else { promise.wait(waitScope); } }; KJ_TEST("adapted client waits for service to complete before completing upstream close on WebSocket") { doDelayedCompletionWebSocketTest(false, false); } KJ_TEST("adapted client waits for service to complete before returning downstream close on WebSocket") { doDelayedCompletionWebSocketTest(false, true); } KJ_TEST("adapted client propagates throw from service after WebSocket upstream close sent") { doDelayedCompletionWebSocketTest(true, false); } KJ_TEST("adapted client propagates throw from service after WebSocket downstream close sent") { doDelayedCompletionWebSocketTest(true, true); } // ----------------------------------------------------------------------------- class CountingIoStream final: public kj::AsyncIoStream { // An AsyncIoStream wrapper which decrements a counter when destroyed (allowing us to count how // many connections are open). public: CountingIoStream(kj::Own inner, uint& count) : inner(kj::mv(inner)), count(count) {} ~CountingIoStream() noexcept(false) { --count; } kj::Promise read(void* buffer, size_t minBytes, size_t maxBytes) override { return inner->read(buffer, minBytes, maxBytes); } kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return inner->tryRead(buffer, minBytes, maxBytes); } kj::Maybe tryGetLength() override { return inner->tryGetLength();; } kj::Promise pumpTo(kj::AsyncOutputStream& output, uint64_t amount) override { return inner->pumpTo(output, amount); } kj::Promise write(const void* buffer, size_t size) override { return inner->write(buffer, size); } kj::Promise write(kj::ArrayPtr> pieces) override { return inner->write(pieces); } kj::Maybe> tryPumpFrom( kj::AsyncInputStream& input, uint64_t amount = kj::maxValue) override { return inner->tryPumpFrom(input, amount); } Promise whenWriteDisconnected() override { return inner->whenWriteDisconnected(); } void shutdownWrite() override { return inner->shutdownWrite(); } void abortRead() override { return inner->abortRead(); } public: kj::Own inner; uint& count; }; class CountingNetworkAddress final: public kj::NetworkAddress { public: CountingNetworkAddress(kj::NetworkAddress& inner, uint& count, uint& cumulative) : inner(inner), count(count), addrCount(ownAddrCount), cumulative(cumulative) {} CountingNetworkAddress(kj::Own inner, uint& count, uint& addrCount) : inner(*inner), ownInner(kj::mv(inner)), count(count), addrCount(addrCount), cumulative(ownCumulative) {} ~CountingNetworkAddress() noexcept(false) { --addrCount; } kj::Promise> connect() override { ++count; ++cumulative; return inner.connect() .then([this](kj::Own stream) -> kj::Own { return kj::heap(kj::mv(stream), count); }); } kj::Own listen() override { KJ_UNIMPLEMENTED("test"); } kj::Own clone() override { KJ_UNIMPLEMENTED("test"); } kj::String toString() override { KJ_UNIMPLEMENTED("test"); } private: kj::NetworkAddress& inner; kj::Own ownInner; uint& count; uint ownAddrCount = 1; uint& addrCount; uint ownCumulative = 0; uint& cumulative; }; class ConnectionCountingNetwork final: public kj::Network { public: ConnectionCountingNetwork(kj::Network& inner, uint& count, uint& addrCount) : inner(inner), count(count), addrCount(addrCount) {} Promise> parseAddress(StringPtr addr, uint portHint = 0) override { ++addrCount; return inner.parseAddress(addr, portHint) .then([this](Own&& addr) -> Own { return kj::heap(kj::mv(addr), count, addrCount); }); } Own getSockaddr(const void* sockaddr, uint len) override { KJ_UNIMPLEMENTED("test"); } Own restrictPeers( kj::ArrayPtr allow, kj::ArrayPtr deny = nullptr) override { KJ_UNIMPLEMENTED("test"); } private: kj::Network& inner; uint& count; uint& addrCount; }; class DummyService final: public HttpService { public: DummyService(HttpHeaderTable& headerTable): headerTable(headerTable) {} kj::Promise request( HttpMethod method, kj::StringPtr url, const HttpHeaders& headers, kj::AsyncInputStream& requestBody, Response& response) override { if (!headers.isWebSocket()) { if (url == "/throw") { return KJ_EXCEPTION(FAILED, "client requested failure"); } auto body = kj::str(headers.get(HttpHeaderId::HOST).orDefault("null"), ":", url); auto stream = response.send(200, "OK", HttpHeaders(headerTable), body.size()); auto promises = kj::heapArrayBuilder>(2); promises.add(stream->write(body.begin(), body.size())); promises.add(requestBody.readAllBytes().ignoreResult()); return kj::joinPromises(promises.finish()).attach(kj::mv(stream), kj::mv(body)); } else { auto ws = response.acceptWebSocket(HttpHeaders(headerTable)); auto body = kj::str(headers.get(HttpHeaderId::HOST).orDefault("null"), ":", url); auto sendPromise = ws->send(body); auto promises = kj::heapArrayBuilder>(2); promises.add(sendPromise.attach(kj::mv(body))); promises.add(ws->receive().ignoreResult()); return kj::joinPromises(promises.finish()).attach(kj::mv(ws)); } } private: HttpHeaderTable& headerTable; }; KJ_TEST("HttpClient connection management") { KJ_HTTP_TEST_SETUP_IO; KJ_HTTP_TEST_SETUP_LOOPBACK_LISTENER_AND_ADDR; kj::TimerImpl serverTimer(kj::origin()); kj::TimerImpl clientTimer(kj::origin()); HttpHeaderTable headerTable; DummyService service(headerTable); HttpServerSettings serverSettings; HttpServer server(serverTimer, headerTable, service, serverSettings); auto listenTask = server.listenHttp(*listener); uint count = 0; uint cumulative = 0; CountingNetworkAddress countingAddr(*addr, count, cumulative); FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; auto client = newHttpClient(clientTimer, headerTable, countingAddr, clientSettings); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 0); uint i = 0; auto doRequest = [&]() { uint n = i++; return client->request(HttpMethod::GET, kj::str("/", n), HttpHeaders(headerTable)).response .then([](HttpClient::Response&& response) { auto promise = response.body->readAllText(); return promise.attach(kj::mv(response.body)); }).then([n](kj::String body) { KJ_EXPECT(body == kj::str("null:/", n)); }); }; // We can do several requests in a row and only have one connection. doRequest().wait(waitScope); doRequest().wait(waitScope); doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 1); // But if we do two in parallel, we'll end up with two connections. auto req1 = doRequest(); auto req2 = doRequest(); req1.wait(waitScope); req2.wait(waitScope); KJ_EXPECT(count == 2); KJ_EXPECT(cumulative == 2); // We can reuse after a POST, provided we write the whole POST body properly. { auto req = client->request( HttpMethod::POST, kj::str("/foo"), HttpHeaders(headerTable), size_t(6)); req.body->write("foobar", 6).wait(waitScope); req.response.wait(waitScope).body->readAllBytes().wait(waitScope); } KJ_EXPECT(count == 2); KJ_EXPECT(cumulative == 2); doRequest().wait(waitScope); KJ_EXPECT(count == 2); KJ_EXPECT(cumulative == 2); // Advance time for half the timeout, then exercise one of the connections. clientTimer.advanceTo(clientTimer.now() + clientSettings.idleTimeout / 2); doRequest().wait(waitScope); doRequest().wait(waitScope); waitScope.poll(); KJ_EXPECT(count == 2); KJ_EXPECT(cumulative == 2); // Advance time past when the other connection should time out. It should be dropped. clientTimer.advanceTo(clientTimer.now() + clientSettings.idleTimeout * 3 / 4); waitScope.poll(); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 2); // Wait for the other to drop. clientTimer.advanceTo(clientTimer.now() + clientSettings.idleTimeout / 2); waitScope.poll(); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 2); // New request creates a new connection again. doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 3); // WebSocket connections are not reused. client->openWebSocket(kj::str("/websocket"), HttpHeaders(headerTable)) .wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 3); // Errored connections are not reused. doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 4); client->request(HttpMethod::GET, kj::str("/throw"), HttpHeaders(headerTable)).response .wait(waitScope).body->readAllBytes().wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 4); // Connections where we failed to read the full response body are not reused. doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 5); client->request(HttpMethod::GET, kj::str("/foo"), HttpHeaders(headerTable)).response .wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 5); // Connections where we didn't even wait for the response headers are not reused. doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 6); client->request(HttpMethod::GET, kj::str("/foo"), HttpHeaders(headerTable)); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 6); // Connections where we failed to write the full request body are not reused. doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 7); client->request(HttpMethod::POST, kj::str("/foo"), HttpHeaders(headerTable), size_t(6)).response .wait(waitScope).body->readAllBytes().wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 7); // If the server times out the connection, we figure it out on the client. doRequest().wait(waitScope); // TODO(someday): Figure out why the following poll is necessary for the test to pass on Windows // and Mac. Without it, it seems that the request's connection never starts, so the // subsequent advanceTo() does not actually time out the connection. waitScope.poll(); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 8); serverTimer.advanceTo(serverTimer.now() + serverSettings.pipelineTimeout * 2); waitScope.poll(); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 8); // Can still make requests. doRequest().wait(waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 9); } KJ_TEST("HttpClient disable connection reuse") { KJ_HTTP_TEST_SETUP_IO; KJ_HTTP_TEST_SETUP_LOOPBACK_LISTENER_AND_ADDR; kj::TimerImpl serverTimer(kj::origin()); kj::TimerImpl clientTimer(kj::origin()); HttpHeaderTable headerTable; DummyService service(headerTable); HttpServerSettings serverSettings; HttpServer server(serverTimer, headerTable, service, serverSettings); auto listenTask = server.listenHttp(*listener); uint count = 0; uint cumulative = 0; CountingNetworkAddress countingAddr(*addr, count, cumulative); FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; clientSettings.idleTimeout = 0 * kj::SECONDS; auto client = newHttpClient(clientTimer, headerTable, countingAddr, clientSettings); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 0); uint i = 0; auto doRequest = [&]() { uint n = i++; return client->request(HttpMethod::GET, kj::str("/", n), HttpHeaders(headerTable)).response .then([](HttpClient::Response&& response) { auto promise = response.body->readAllText(); return promise.attach(kj::mv(response.body)); }).then([n](kj::String body) { KJ_EXPECT(body == kj::str("null:/", n)); }); }; // Each serial request gets its own connection. doRequest().wait(waitScope); doRequest().wait(waitScope); doRequest().wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 3); // Each parallel request gets its own connection. auto req1 = doRequest(); auto req2 = doRequest(); req1.wait(waitScope); req2.wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 5); } KJ_TEST("HttpClient concurrency limiting") { #if KJ_HTTP_TEST_USE_OS_PIPE && !__linux__ // On Windows and Mac, OS event delivery is not always immediate, and that seems to make this // test flakey. On Linux, events are always immediately delivered. For now, we compile the test // but we don't run it outside of Linux. We do run the in-memory-pipes version on all OSs since // that mode shouldn't depend on kernel behavior at all. return; #endif KJ_HTTP_TEST_SETUP_IO; KJ_HTTP_TEST_SETUP_LOOPBACK_LISTENER_AND_ADDR; kj::TimerImpl serverTimer(kj::origin()); kj::TimerImpl clientTimer(kj::origin()); HttpHeaderTable headerTable; DummyService service(headerTable); HttpServerSettings serverSettings; HttpServer server(serverTimer, headerTable, service, serverSettings); auto listenTask = server.listenHttp(*listener); uint count = 0; uint cumulative = 0; CountingNetworkAddress countingAddr(*addr, count, cumulative); FakeEntropySource entropySource; HttpClientSettings clientSettings; clientSettings.entropySource = entropySource; clientSettings.idleTimeout = 0 * kj::SECONDS; auto innerClient = newHttpClient(clientTimer, headerTable, countingAddr, clientSettings); struct CallbackEvent { uint runningCount; uint pendingCount; bool operator==(const CallbackEvent& other) const { return runningCount == other.runningCount && pendingCount == other.pendingCount; } bool operator!=(const CallbackEvent& other) const { return !(*this == other); } // TODO(someday): Can use default spaceship operator in C++20: //auto operator<=>(const CallbackEvent&) const = default; }; kj::Vector callbackEvents; auto callback = [&](uint runningCount, uint pendingCount) { callbackEvents.add(CallbackEvent{runningCount, pendingCount}); }; auto client = newConcurrencyLimitingHttpClient(*innerClient, 1, kj::mv(callback)); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 0); uint i = 0; auto doRequest = [&]() { uint n = i++; return client->request(HttpMethod::GET, kj::str("/", n), HttpHeaders(headerTable)).response .then([](HttpClient::Response&& response) { auto promise = response.body->readAllText(); return promise.attach(kj::mv(response.body)); }).then([n](kj::String body) { KJ_EXPECT(body == kj::str("null:/", n)); }); }; // Second connection blocked by first. auto req1 = doRequest(); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {1, 0} })); callbackEvents.clear(); auto req2 = doRequest(); // TODO(someday): Figure out why this poll() is necessary on Windows and macOS. waitScope.poll(); KJ_EXPECT(req1.poll(waitScope)); KJ_EXPECT(!req2.poll(waitScope)); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 1); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {1, 1} })); callbackEvents.clear(); // Releasing first connection allows second to start. req1.wait(waitScope); KJ_EXPECT(req2.poll(waitScope)); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 2); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {1, 0} })); callbackEvents.clear(); req2.wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 2); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {0, 0} })); callbackEvents.clear(); // Using body stream after releasing blocked response promise throws no exception auto req3 = doRequest(); { kj::Own req4Body; { auto req4 = client->request(HttpMethod::GET, kj::str("/", ++i), HttpHeaders(headerTable)); waitScope.poll(); req4Body = kj::mv(req4.body); } auto writePromise = req4Body->write("a", 1); KJ_EXPECT(!writePromise.poll(waitScope)); } req3.wait(waitScope); KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 3); // Similar connection limiting for web sockets // TODO(someday): Figure out why the sequencing of websockets events does // not work correctly on Windows (and maybe macOS?). The solution is not as // simple as inserting poll()s as above, since doing so puts the websocket in // a state that trips a "previous HTTP message body incomplete" assertion, // while trying to write 500 network response. callbackEvents.clear(); auto ws1 = kj::heap(client->openWebSocket(kj::str("/websocket"), HttpHeaders(headerTable))); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {1, 0} })); callbackEvents.clear(); auto ws2 = kj::heap(client->openWebSocket(kj::str("/websocket"), HttpHeaders(headerTable))); KJ_EXPECT(ws1->poll(waitScope)); KJ_EXPECT(!ws2->poll(waitScope)); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 4); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {1, 1} })); callbackEvents.clear(); { auto response1 = ws1->wait(waitScope); KJ_EXPECT(!ws2->poll(waitScope)); KJ_EXPECT(callbackEvents == kj::ArrayPtr({})); } KJ_EXPECT(ws2->poll(waitScope)); KJ_EXPECT(count == 1); KJ_EXPECT(cumulative == 5); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {1, 0} })); callbackEvents.clear(); { auto response2 = ws2->wait(waitScope); KJ_EXPECT(callbackEvents == kj::ArrayPtr({})); } KJ_EXPECT(count == 0); KJ_EXPECT(cumulative == 5); KJ_EXPECT(callbackEvents == kj::ArrayPtr({ {0, 0} })); } #if KJ_HTTP_TEST_USE_OS_PIPE // TODO(someday): Implement mock kj::Network for userspace version of this test? KJ_TEST("HttpClient multi host") { auto io = kj::setupAsyncIo(); kj::TimerImpl serverTimer(kj::origin()); kj::TimerImpl clientTimer(kj::origin()); HttpHeaderTable headerTable; auto listener1 = io.provider->getNetwork().parseAddress("localhost", 0) .wait(io.waitScope)->listen(); auto listener2 = io.provider->getNetwork().parseAddress("localhost", 0) .wait(io.waitScope)->listen(); DummyService service(headerTable); HttpServer server(serverTimer, headerTable, service); auto listenTask1 = server.listenHttp(*listener1); auto listenTask2 = server.listenHttp(*listener2); uint count = 0, addrCount = 0; uint tlsCount = 0, tlsAddrCount = 0; ConnectionCountingNetwork countingNetwork(io.provider->getNetwork(), count, addrCount); ConnectionCountingNetwork countingTlsNetwork(io.provider->getNetwork(), tlsCount, tlsAddrCount); HttpClientSettings clientSettings; auto client = newHttpClient(clientTimer, headerTable, countingNetwork, countingTlsNetwork, clientSettings); KJ_EXPECT(count == 0); uint i = 0; auto doRequest = [&](bool tls, uint port) { uint n = i++; // We stick a double-slash in the URL to test that it doesn't get coalesced into one slash, // which was a bug in the past. return client->request(HttpMethod::GET, kj::str((tls ? "https://localhost:" : "http://localhost:"), port, "//", n), HttpHeaders(headerTable)).response .then([](HttpClient::Response&& response) { auto promise = response.body->readAllText(); return promise.attach(kj::mv(response.body)); }).then([n, port](kj::String body) { KJ_EXPECT(body == kj::str("localhost:", port, "://", n), body, port, n); }); }; uint port1 = listener1->getPort(); uint port2 = listener2->getPort(); // We can do several requests in a row to the same host and only have one connection. doRequest(false, port1).wait(io.waitScope); doRequest(false, port1).wait(io.waitScope); doRequest(false, port1).wait(io.waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(tlsCount == 0); KJ_EXPECT(addrCount == 1); KJ_EXPECT(tlsAddrCount == 0); // Request a different host, and now we have two connections. doRequest(false, port2).wait(io.waitScope); KJ_EXPECT(count == 2); KJ_EXPECT(tlsCount == 0); KJ_EXPECT(addrCount == 2); KJ_EXPECT(tlsAddrCount == 0); // Try TLS. doRequest(true, port1).wait(io.waitScope); KJ_EXPECT(count == 2); KJ_EXPECT(tlsCount == 1); KJ_EXPECT(addrCount == 2); KJ_EXPECT(tlsAddrCount == 1); // Try first host again, no change in connection count. doRequest(false, port1).wait(io.waitScope); KJ_EXPECT(count == 2); KJ_EXPECT(tlsCount == 1); KJ_EXPECT(addrCount == 2); KJ_EXPECT(tlsAddrCount == 1); // Multiple requests in parallel forces more connections to that host. auto promise1 = doRequest(false, port1); auto promise2 = doRequest(false, port1); promise1.wait(io.waitScope); promise2.wait(io.waitScope); KJ_EXPECT(count == 3); KJ_EXPECT(tlsCount == 1); KJ_EXPECT(addrCount == 2); KJ_EXPECT(tlsAddrCount == 1); // Let everything expire. clientTimer.advanceTo(clientTimer.now() + clientSettings.idleTimeout * 2); io.waitScope.poll(); KJ_EXPECT(count == 0); KJ_EXPECT(tlsCount == 0); KJ_EXPECT(addrCount == 0); KJ_EXPECT(tlsAddrCount == 0); // We can still request those hosts again. doRequest(false, port1).wait(io.waitScope); KJ_EXPECT(count == 1); KJ_EXPECT(tlsCount == 0); KJ_EXPECT(addrCount == 1); KJ_EXPECT(tlsAddrCount == 0); } #endif // ----------------------------------------------------------------------------- #if KJ_HTTP_TEST_USE_OS_PIPE // This test only makes sense using the real network. KJ_TEST("HttpClient to capnproto.org") { auto io = kj::setupAsyncIo(); auto maybeConn = io.provider->getNetwork().parseAddress("capnproto.org", 80) .then([](kj::Own addr) { auto promise = addr->connect(); return promise.attach(kj::mv(addr)); }).then([](kj::Own&& connection) -> kj::Maybe> { return kj::mv(connection); }, [](kj::Exception&& e) -> kj::Maybe> { KJ_LOG(WARNING, "skipping test because couldn't connect to capnproto.org"); return nullptr; }).wait(io.waitScope); KJ_IF_MAYBE(conn, maybeConn) { // Successfully connected to capnproto.org. Try doing GET /. We expect to get a redirect to // HTTPS, because what kind of horrible web site would serve in plaintext, really? HttpHeaderTable table; auto client = newHttpClient(table, **conn); HttpHeaders headers(table); headers.set(HttpHeaderId::HOST, "capnproto.org"); auto response = client->request(HttpMethod::GET, "/", headers).response.wait(io.waitScope); KJ_EXPECT(response.statusCode / 100 == 3); auto location = KJ_ASSERT_NONNULL(response.headers->get(HttpHeaderId::LOCATION)); KJ_EXPECT(location == "https://capnproto.org/"); auto body = response.body->readAllText().wait(io.waitScope); } } #endif } // namespace } // namespace kj capnproto-c++-0.8.0/src/kj/compat/gzip-test.c++0000644000175000017500000002413613650101756021660 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if KJ_HAS_ZLIB #include "gzip.h" #include #include #include namespace kj { namespace { static const byte FOOBAR_GZIP[] = { 0x1F, 0x8B, 0x08, 0x00, 0xF9, 0x05, 0xB7, 0x59, 0x00, 0x03, 0x4B, 0xCB, 0xCF, 0x4F, 0x4A, 0x2C, 0x02, 0x00, 0x95, 0x1F, 0xF6, 0x9E, 0x06, 0x00, 0x00, 0x00, }; class MockInputStream: public InputStream { public: MockInputStream(kj::ArrayPtr bytes, size_t blockSize) : bytes(bytes), blockSize(blockSize) {} size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { // Clamp max read to blockSize. size_t n = kj::min(blockSize, maxBytes); // Unless that's less than minBytes -- in which case, use minBytes. n = kj::max(n, minBytes); // But also don't read more data than we have. n = kj::min(n, bytes.size()); memcpy(buffer, bytes.begin(), n); bytes = bytes.slice(n, bytes.size()); return n; } private: kj::ArrayPtr bytes; size_t blockSize; }; class MockAsyncInputStream: public AsyncInputStream { public: MockAsyncInputStream(kj::ArrayPtr bytes, size_t blockSize) : bytes(bytes), blockSize(blockSize) {} Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { // Clamp max read to blockSize. size_t n = kj::min(blockSize, maxBytes); // Unless that's less than minBytes -- in which case, use minBytes. n = kj::max(n, minBytes); // But also don't read more data than we have. n = kj::min(n, bytes.size()); memcpy(buffer, bytes.begin(), n); bytes = bytes.slice(n, bytes.size()); return n; } private: kj::ArrayPtr bytes; size_t blockSize; }; class MockOutputStream: public OutputStream { public: kj::Vector bytes; kj::String decompress() { MockInputStream rawInput(bytes, kj::maxValue); GzipInputStream gzip(rawInput); return gzip.readAllText(); } void write(const void* buffer, size_t size) override { bytes.addAll(arrayPtr(reinterpret_cast(buffer), size)); } void write(ArrayPtr> pieces) override { for (auto& piece: pieces) { bytes.addAll(piece); } } }; class MockAsyncOutputStream: public AsyncOutputStream { public: kj::Vector bytes; kj::String decompress(WaitScope& ws) { MockAsyncInputStream rawInput(bytes, kj::maxValue); GzipAsyncInputStream gzip(rawInput); return gzip.readAllText().wait(ws); } Promise write(const void* buffer, size_t size) override { bytes.addAll(arrayPtr(reinterpret_cast(buffer), size)); return kj::READY_NOW; } Promise write(ArrayPtr> pieces) override { for (auto& piece: pieces) { bytes.addAll(piece); } return kj::READY_NOW; } Promise whenWriteDisconnected() override { KJ_UNIMPLEMENTED("not used"); } }; KJ_TEST("gzip decompression") { // Normal read. { MockInputStream rawInput(FOOBAR_GZIP, kj::maxValue); GzipInputStream gzip(rawInput); KJ_EXPECT(gzip.readAllText() == "foobar"); } // Force read one byte at a time. { MockInputStream rawInput(FOOBAR_GZIP, 1); GzipInputStream gzip(rawInput); KJ_EXPECT(gzip.readAllText() == "foobar"); } // Read truncated input. { MockInputStream rawInput(kj::arrayPtr(FOOBAR_GZIP, sizeof(FOOBAR_GZIP) / 2), kj::maxValue); GzipInputStream gzip(rawInput); char text[16]; size_t n = gzip.tryRead(text, 1, sizeof(text)); text[n] = '\0'; KJ_EXPECT(StringPtr(text, n) == "fo"); KJ_EXPECT_THROW_MESSAGE("gzip compressed stream ended prematurely", gzip.tryRead(text, 1, sizeof(text))); } // Read concatenated input. { Vector bytes; bytes.addAll(ArrayPtr(FOOBAR_GZIP)); bytes.addAll(ArrayPtr(FOOBAR_GZIP)); MockInputStream rawInput(bytes, kj::maxValue); GzipInputStream gzip(rawInput); KJ_EXPECT(gzip.readAllText() == "foobarfoobar"); } } KJ_TEST("async gzip decompression") { auto io = setupAsyncIo(); // Normal read. { MockAsyncInputStream rawInput(FOOBAR_GZIP, kj::maxValue); GzipAsyncInputStream gzip(rawInput); KJ_EXPECT(gzip.readAllText().wait(io.waitScope) == "foobar"); } // Force read one byte at a time. { MockAsyncInputStream rawInput(FOOBAR_GZIP, 1); GzipAsyncInputStream gzip(rawInput); KJ_EXPECT(gzip.readAllText().wait(io.waitScope) == "foobar"); } // Read truncated input. { MockAsyncInputStream rawInput(kj::arrayPtr(FOOBAR_GZIP, sizeof(FOOBAR_GZIP) / 2), kj::maxValue); GzipAsyncInputStream gzip(rawInput); char text[16]; size_t n = gzip.tryRead(text, 1, sizeof(text)).wait(io.waitScope); text[n] = '\0'; KJ_EXPECT(StringPtr(text, n) == "fo"); KJ_EXPECT_THROW_MESSAGE("gzip compressed stream ended prematurely", gzip.tryRead(text, 1, sizeof(text)).wait(io.waitScope)); } // Read concatenated input. { Vector bytes; bytes.addAll(ArrayPtr(FOOBAR_GZIP)); bytes.addAll(ArrayPtr(FOOBAR_GZIP)); MockAsyncInputStream rawInput(bytes, kj::maxValue); GzipAsyncInputStream gzip(rawInput); KJ_EXPECT(gzip.readAllText().wait(io.waitScope) == "foobarfoobar"); } // Decompress using an output stream. { MockAsyncOutputStream rawOutput; GzipAsyncOutputStream gzip(rawOutput, GzipAsyncOutputStream::DECOMPRESS); auto mid = sizeof(FOOBAR_GZIP) / 2; gzip.write(FOOBAR_GZIP, mid).wait(io.waitScope); auto str1 = kj::heapString(rawOutput.bytes.asPtr().asChars()); KJ_EXPECT(str1 == "fo", str1); gzip.write(FOOBAR_GZIP + mid, sizeof(FOOBAR_GZIP) - mid).wait(io.waitScope); auto str2 = kj::heapString(rawOutput.bytes.asPtr().asChars()); KJ_EXPECT(str2 == "foobar", str2); gzip.end().wait(io.waitScope); } } KJ_TEST("gzip compression") { // Normal write. { MockOutputStream rawOutput; { GzipOutputStream gzip(rawOutput); gzip.write("foobar", 6); } KJ_EXPECT(rawOutput.decompress() == "foobar"); } // Multi-part write. { MockOutputStream rawOutput; { GzipOutputStream gzip(rawOutput); gzip.write("foo", 3); gzip.write("bar", 3); } KJ_EXPECT(rawOutput.decompress() == "foobar"); } // Array-of-arrays write. { MockOutputStream rawOutput; { GzipOutputStream gzip(rawOutput); ArrayPtr pieces[] = { kj::StringPtr("foo").asBytes(), kj::StringPtr("bar").asBytes(), }; gzip.write(pieces); } KJ_EXPECT(rawOutput.decompress() == "foobar"); } } KJ_TEST("gzip huge round trip") { auto bytes = heapArray(65536); for (auto& b: bytes) { b = rand(); } MockOutputStream rawOutput; { GzipOutputStream gzipOut(rawOutput); gzipOut.write(bytes.begin(), bytes.size()); } MockInputStream rawInput(rawOutput.bytes, kj::maxValue); GzipInputStream gzipIn(rawInput); auto decompressed = gzipIn.readAllBytes(); KJ_ASSERT(decompressed.size() == bytes.size()); KJ_ASSERT(memcmp(bytes.begin(), decompressed.begin(), bytes.size()) == 0); } KJ_TEST("async gzip compression") { auto io = setupAsyncIo(); // Normal write. { MockAsyncOutputStream rawOutput; GzipAsyncOutputStream gzip(rawOutput); gzip.write("foobar", 6).wait(io.waitScope); gzip.end().wait(io.waitScope); KJ_EXPECT(rawOutput.decompress(io.waitScope) == "foobar"); } // Multi-part write. { MockAsyncOutputStream rawOutput; GzipAsyncOutputStream gzip(rawOutput); gzip.write("foo", 3).wait(io.waitScope); auto prevSize = rawOutput.bytes.size(); gzip.write("bar", 3).wait(io.waitScope); auto curSize = rawOutput.bytes.size(); KJ_EXPECT(prevSize == curSize, prevSize, curSize); gzip.flush().wait(io.waitScope); curSize = rawOutput.bytes.size(); KJ_EXPECT(prevSize < curSize, prevSize, curSize); gzip.end().wait(io.waitScope); KJ_EXPECT(rawOutput.decompress(io.waitScope) == "foobar"); } // Array-of-arrays write. { MockAsyncOutputStream rawOutput; GzipAsyncOutputStream gzip(rawOutput); ArrayPtr pieces[] = { kj::StringPtr("foo").asBytes(), kj::StringPtr("bar").asBytes(), }; gzip.write(pieces).wait(io.waitScope); gzip.end().wait(io.waitScope); KJ_EXPECT(rawOutput.decompress(io.waitScope) == "foobar"); } } KJ_TEST("async gzip huge round trip") { auto io = setupAsyncIo(); auto bytes = heapArray(65536); for (auto& b: bytes) { b = rand(); } MockAsyncOutputStream rawOutput; GzipAsyncOutputStream gzipOut(rawOutput); gzipOut.write(bytes.begin(), bytes.size()).wait(io.waitScope); gzipOut.end().wait(io.waitScope); MockAsyncInputStream rawInput(rawOutput.bytes, kj::maxValue); GzipAsyncInputStream gzipIn(rawInput); auto decompressed = gzipIn.readAllBytes().wait(io.waitScope); KJ_ASSERT(decompressed.size() == bytes.size()); KJ_ASSERT(memcmp(bytes.begin(), decompressed.begin(), bytes.size()) == 0); } } // namespace } // namespace kj #endif // KJ_HAS_ZLIB capnproto-c++-0.8.0/src/kj/compat/readiness-io-test.c++0000644000175000017500000001323713340402540023260 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "readiness-io.h" #include #include namespace kj { namespace { KJ_TEST("readiness IO: write small") { auto io = setupAsyncIo(); auto pipe = io.provider->newOneWayPipe(); char buf[4]; auto readPromise = pipe.in->read(buf, 3, 4); ReadyOutputStreamWrapper out(*pipe.out); KJ_ASSERT(KJ_ASSERT_NONNULL(out.write(kj::StringPtr("foo").asBytes())) == 3); KJ_ASSERT(readPromise.wait(io.waitScope) == 3); buf[3] = '\0'; KJ_ASSERT(kj::StringPtr(buf) == "foo"); } KJ_TEST("readiness IO: write many odd") { auto io = setupAsyncIo(); auto pipe = io.provider->newOneWayPipe(); ReadyOutputStreamWrapper out(*pipe.out); size_t totalWritten = 0; for (;;) { KJ_IF_MAYBE(n, out.write(kj::StringPtr("bar").asBytes())) { totalWritten += *n; if (*n < 3) { break; } } else { KJ_FAIL_ASSERT("pipe buffer is divisible by 3? really?"); } } auto buf = kj::heapArray(totalWritten + 1); size_t n = pipe.in->read(buf.begin(), totalWritten, buf.size()).wait(io.waitScope); KJ_ASSERT(n == totalWritten); for (size_t i = 0; i < totalWritten; i++) { KJ_ASSERT(buf[i] == "bar"[i%3]); } } KJ_TEST("readiness IO: write even") { auto io = setupAsyncIo(); auto pipe = io.provider->newOneWayPipe(); ReadyOutputStreamWrapper out(*pipe.out); size_t totalWritten = 0; for (;;) { KJ_IF_MAYBE(n, out.write(kj::StringPtr("ba").asBytes())) { totalWritten += *n; if (*n < 2) { KJ_FAIL_ASSERT("pipe buffer is not divisible by 2? really?"); } } else { break; } } auto buf = kj::heapArray(totalWritten + 1); size_t n = pipe.in->read(buf.begin(), totalWritten, buf.size()).wait(io.waitScope); KJ_ASSERT(n == totalWritten); for (size_t i = 0; i < totalWritten; i++) { KJ_ASSERT(buf[i] == "ba"[i%2]); } } KJ_TEST("readiness IO: read small") { auto io = setupAsyncIo(); auto pipe = io.provider->newOneWayPipe(); ReadyInputStreamWrapper in(*pipe.in); char buf[4]; KJ_ASSERT(in.read(kj::ArrayPtr(buf).asBytes()) == nullptr); pipe.out->write("foo", 3).wait(io.waitScope); in.whenReady().wait(io.waitScope); KJ_ASSERT(KJ_ASSERT_NONNULL(in.read(kj::ArrayPtr(buf).asBytes())) == 3); buf[3] = '\0'; KJ_ASSERT(kj::StringPtr(buf) == "foo"); pipe.out = nullptr; kj::Maybe finalRead; for (;;) { finalRead = in.read(kj::ArrayPtr(buf).asBytes()); KJ_IF_MAYBE(n, finalRead) { KJ_ASSERT(*n == 0); break; } else { in.whenReady().wait(io.waitScope); } } } KJ_TEST("readiness IO: read many odd") { auto io = setupAsyncIo(); auto pipe = io.provider->newOneWayPipe(); char dummy[8192]; for (auto i: kj::indices(dummy)) { dummy[i] = "bar"[i%3]; } auto writeTask = pipe.out->write(dummy, sizeof(dummy)).then([&]() { // shutdown pipe.out = nullptr; }).eagerlyEvaluate(nullptr); ReadyInputStreamWrapper in(*pipe.in); char buf[3]; for (;;) { auto result = in.read(kj::ArrayPtr(buf).asBytes()); KJ_IF_MAYBE(n, result) { for (size_t i = 0; i < *n; i++) { KJ_ASSERT(buf[i] == "bar"[i]); } KJ_ASSERT(*n != 0, "ended at wrong spot"); if (*n < 3) { break; } } else { in.whenReady().wait(io.waitScope); } } kj::Maybe finalRead; for (;;) { finalRead = in.read(kj::ArrayPtr(buf).asBytes()); KJ_IF_MAYBE(n, finalRead) { KJ_ASSERT(*n == 0); break; } else { in.whenReady().wait(io.waitScope); } } } KJ_TEST("readiness IO: read many even") { auto io = setupAsyncIo(); auto pipe = io.provider->newOneWayPipe(); char dummy[8192]; for (auto i: kj::indices(dummy)) { dummy[i] = "ba"[i%2]; } auto writeTask = pipe.out->write(dummy, sizeof(dummy)).then([&]() { // shutdown pipe.out = nullptr; }).eagerlyEvaluate(nullptr); ReadyInputStreamWrapper in(*pipe.in); char buf[2]; for (;;) { auto result = in.read(kj::ArrayPtr(buf).asBytes()); KJ_IF_MAYBE(n, result) { for (size_t i = 0; i < *n; i++) { KJ_ASSERT(buf[i] == "ba"[i]); } if (*n == 0) { break; } KJ_ASSERT(*n == 2, "ended at wrong spot"); } else { in.whenReady().wait(io.waitScope); } } kj::Maybe finalRead; for (;;) { finalRead = in.read(kj::ArrayPtr(buf).asBytes()); KJ_IF_MAYBE(n, finalRead) { KJ_ASSERT(*n == 0); break; } else { in.whenReady().wait(io.waitScope); } } } } // namespace } // namespace kj capnproto-c++-0.8.0/src/kj/compat/tls-test.c++0000644000175000017500000007053713650101756021517 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if KJ_HAS_OPENSSL #include "tls.h" #include #include #include #include namespace kj { namespace { // ======================================================================================= // test data // // made with make-test-certs.sh static constexpr char CA_CERT[] = "-----BEGIN CERTIFICATE-----\n" "MIIGJTCCBA2gAwIBAgIJALIY6TRIbD8CMA0GCSqGSIb3DQEBCwUAMIGnMQswCQYD\n" "VQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UEBwwJUGFsbyBBbHRv\n" "MRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZBgNVBAsMElRlc3RpbmcgRGVwYXJ0\n" "bWVudDEXMBUGA1UEAwwOY2EuZXhhbXBsZS5jb20xIjAgBgkqhkiG9w0BCQEWE2dh\n" "cnBseUBzYW5kc3Rvcm0uaW8wIBcNMTYwNTMxMDQyODQ0WhgPMjExNjA1MDcwNDI4\n" "NDRaMIGnMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UE\n" "BwwJUGFsbyBBbHRvMRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZBgNVBAsMElRl\n" "c3RpbmcgRGVwYXJ0bWVudDEXMBUGA1UEAwwOY2EuZXhhbXBsZS5jb20xIjAgBgkq\n" "hkiG9w0BCQEWE2dhcnBseUBzYW5kc3Rvcm0uaW8wggIiMA0GCSqGSIb3DQEBAQUA\n" "A4ICDwAwggIKAoICAQCzVgWV0irYeGCd0bxf5bMHUTpfgWgXOrnT1kno8N+v8JAN\n" "n9BdU0GhqFqVC6rZ+XH+C6funzGgVzpXUsFToWOJ2nKqcc2gGufB/WHaoG2qiY+1\n" "6RekttqJzSvuUMnyPZnA7VDnZ9Pr3YIkIZQQR1FMXFSOVqrdVh4SV7emnZzFgQY/\n" "iXbKhqaCJI8dSmeIIjTc4PLKoP/yLbmaW+/mRbYv6QEm2poTaS0NtUFqN/B+oaWr\n" "dAsRdb7Vv5ME+FDx6XOC6vvdmE1UdgQHgAvU4Hhylqln+J0h6wxVAZ3mx3xqiQXS\n" "rZuI2qEi8wOVNnZDIxaFPqLgCQVCANmK6XmlvQXG9mzaprtwtH4eF586pvDo7RuC\n" "oqKVTKi6YoWj1ne8FAd0i3QLBiuwivSFl42UbDV75oFSnXIY/t3v9niIzftb17TB\n" "PfGAJXQ9z2ggWENI3TLojnay8pMISPZnVxAbLfiESp+GGuDmdmZC9EGp1dzeW1Em\n" "n53iFVYmyOhPy6SdqUm3Bg2gdMB9upUWXjeRczBmI5A8aZRoimTENff1BT0RFJpJ\n" "4W+6FUpisNZ1MaIA+2MWf5C9fnFQ1zn1lXty3+104ReXE0PfmjRjU1qVjSWW10/Q\n" "vzz0vnkInxqkw8yVrzx9vX0irHxGVl7Q9RRNkejFcRXpymrooah4mtlrPmTFKwID\n" "AQABo1AwTjAdBgNVHQ4EFgQU0hyt1HIvkxyWekOFhm4GdnZfsvEwHwYDVR0jBBgw\n" "FoAU0hyt1HIvkxyWekOFhm4GdnZfsvEwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0B\n" "AQsFAAOCAgEAKfB8JfYko8QjUeGl9goSJp7VgLM7CVqhPOfHkyglruQ9KAF4v2vz\n" "QUjCUl3bps12oHPKkJhPLD5Nhvw76o2xqJoRLdfzq0h6MXIoKgMFZlAPYZdxZCDy\n" "CbF4aFerXlkuG8cq8rjdtKYWYe8uka2eVWeRr4gQcK0IW7d6bsQW094xFiLOY8Zx\n" "Z+nlKDVDDPRfPHoK1wl1CZkv3e6PpwrYpei7rgT5XUmUcdX6LDXOfn94JTk1nw8M\n" "K0bfr5LXNjNOfIo1cIf12Rn7v3vIJXnC4k9WQYe+Iq++G7B3bOTEP2SKbExTimn6\n" "0R9EFvbvCQXJQSN4ylZKXohqH6cSEfbrnVil+cVe0WtmQU6tihBZIVy06UZp32cQ\n" "SN4Gn84sQmDoJAK+0+X3VhtFlAySqOdpt5CY2UMXCx+DDsEtQHZG+kRVdVsJTuWb\n" "D7QPB2BUylD6NBLp/3JAFkRek1Wd38HrIMXWkENP1oNoHQdO9kKyzTtmmybD+qom\n" "/ZnXxSjJzh2F40Ph8LJgd1JWVjPaazQbVwUElVSSoLKTmbZcVAqwBAQcX7gfaPve\n" "8GTHufekQbwVDnWThML8Y/ofIk+Zl3g8MUhi0Q8X60IN24WUflM3S9cHnINVGmuN\n" "Wz3Z8Z8gQnpBaMv5/6Wt9KQ7Low8iJmMQ5mspVP6Y/BbhXUg9dZjZt4=\n" "-----END CERTIFICATE-----\n"; static constexpr char INTERMEDIATE_CERT[] = "-----BEGIN CERTIFICATE-----\n" "MIIGDjCCA/agAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgacxCzAJBgNVBAYTAlVT\n" "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRIwEAYDVQQHDAlQYWxvIEFsdG8xFTATBgNV\n" "BAoMDFNhbmRzdG9ybS5pbzEbMBkGA1UECwwSVGVzdGluZyBEZXBhcnRtZW50MRcw\n" "FQYDVQQDDA5jYS5leGFtcGxlLmNvbTEiMCAGCSqGSIb3DQEJARYTZ2FycGx5QHNh\n" "bmRzdG9ybS5pbzAgFw0xNjA1MzEwNDI4NDZaGA8yMTE2MDUwNzA0Mjg0NlowgZcx\n" "CzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRUwEwYDVQQKDAxTYW5k\n" "c3Rvcm0uaW8xGzAZBgNVBAsMElRlc3RpbmcgRGVwYXJ0bWVudDEbMBkGA1UEAwwS\n" "aW50LWNhLmV4YW1wbGUuY29tMSIwIAYJKoZIhvcNAQkBFhNnYXJwbHlAc2FuZHN0\n" "b3JtLmlvMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtqWYNn5PO7cG\n" "gyFzFX3q4QUQuKX+mzHjf560nOiF0Hon4SnZHnJqfGTSPBiEtqsaYLLf4dmO2SkR\n" "w65MRKy8A6/441NhmCv8OamepOJWB71jlGVuLIoUwYgvT0R529r+Kzdq2xcnSgOo\n" "O54SB4d8sSALAfOMKky+EOqcQq7VC+JZtel8LEJUpfvicHZtwS64D4IetsaGuAPz\n" "KCZ3QTWMZr+FPzvmTJnxmrVwHU/whT8ma8rFljZ3oQiawIzeb/hQ4OjGdjt/H3TZ\n" "A5rWL5s3t6RbR/MhLkajH4Fpw1qQUt5aHm54czLnHBQ9yz4dP/oWxZ8GBWT9PV63\n" "CW34Irb6Zb5bmfYpaDtd0XsGACYCmLwDxBH2G5UZ6Z5nazjOHAQcYb6f4kjYVuoG\n" "PvD5pjsZ7pcZr//gb4+SsVgMQDC1jRNoJPS9G3Wz42msR9IAqFaSUuay6viTPQqf\n" "mQGOMPzJFbxnje0U483YotL+DhqtV5zFxx1l1nNJL7/5AdSPBRCvriLzIxblemBq\n" "5JdlkKcqWq/QJtvrzHnlixVCKA1mHaWUB7zv7Np0KzeHWZ6sIcBlR7No0boW4Xmm\n" "S4F9sTIfh+4f6qhXnjq3G4yIXhggHIWOutFAgupMQv5oNZYDjqzBhDIfWXK/8V9P\n" "/3ubm34tOsNcWwpxxiEc5dkQAvLmFycCAwEAAaNQME4wHQYDVR0OBBYEFMvpWUv7\n" "zweivFl4A6ZykYOe9hogMB8GA1UdIwQYMBaAFNIcrdRyL5MclnpDhYZuBnZ2X7Lx\n" "MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggIBAFyOD1Zti+4awHLMYDwn\n" "s6YMSKDNnG1GjpNVjkOPZ2BOAbg18fcsNmy69R4S4rfRQMIEZPNSXh092v4BKUmc\n" "Afa4ahEZmDET4plRWWpWUC8j8/ESIv2372QG2Q00TNWmhPiUKd8VMwV4HFcHml/+\n" "99BrTNn68s8zMB5BNiJgWTxFIRPLy12FWjr0OriVp1EaPOstkYNxfl4RdBgTXCJ8\n" "A9XRBNc25JHPNGPVqiRmCpewsSzEmP2sdz1MGspX7xiaE0UWnCyMPOmX90+dZlSI\n" "GHoBI6iNL894pzKiGvl4dZ2cwKhMncXqvbx9v+IDGSXrC6bsZNl42wee8D7axsKE\n" "xGNQKcGpU8EQqt6YC2wFV+wAHIlYuH90yk1LAZrehKy0E7N/ToKBz7MgGtoQLbn6\n" "UuHZto/LthCY6A8ppgqrq1JEbuLoqskRuOCIF6qXjPTsNhsBXCU/xnkae74QO+2r\n" "adrOubDvIjNtaHBuDgAeVl6PIP4mcJUMF3KhdVg/EDXWVq34YSEF+G/hR/Cj8GKl\n" "DIGGtcQ2re+uPAoPnQ+II9iJcDeY+1wpo9HK4VL54ERzXd53Vl357mNuA/69Fjh3\n" "HRzI5ELdyzWOEFCteCWyPtlOCZJ3Uuj9umS9K/B7KM1XmRzTOYvvj7aTaT4DhoO6\n" "VfvEtl+RmGSm/YmfTJ8551Zq\n" "-----END CERTIFICATE-----\n"; static constexpr char HOST_KEY[] = "-----BEGIN RSA PRIVATE KEY-----\n" "MIIJKQIBAAKCAgEA2wdK2q0OK/6HPd66cO5Otxj/bTuyAIRt05tykA28tXONJzrs\n" "H1p6Yypr/JPwGYeSBOhyTUNKQQ46YSey+zOTO3VH21/rpdeEAmCVgy/cjtJaoyYA\n" "VuSf6+5NcHph/oPX9wRqVAb3BjpjQByLr4EvKdhK2S+r0AJcN1bePWJSIpQum0Eg\n" "mxz1z2OWQRX0+3yV8tZQtk1iJKvmPI1JNFIqQsgQMxQgBQSysRgfFqwyKgDgEHoU\n" "YqEvU2zZeOQ1onzRNLxf3mseZz9JGsaTaqYjs4vsOSKYQT5Uca+t7q07Z2bFf97k\n" "KTJdVV4NtXyybhWXxvm1p+MSoSSpTg47oN4AVGH75PusmjnagjWlO7rTAZh17QiA\n" "ORXGgGlYRqVCYKPMdahehEj7cupNn55mxH4YaomFrwjPayXa/1TsXzP17b5aTSMN\n" "ye0q6NwOMIUGOAyn8wTEUSSDDErC4nihRHxGb3kFToq1dqCI9Sc9L1HY57Sp8tpK\n" "5CQD1G3ppzhwZ2/Xn84ipQriogsV5ALbxukC7WGZjTgBahsMZBqHWtWIuOUjAKr/\n" "X/jpoNgBpkT6VLqpx9L7Tj1zjvFwLsE0YAQq37lx2Sam/+CSzA3BHDsx6GNSptRK\n" "KYvPSIYDFxJUvf/KHg8mI/gnqa0EgHJvTPBOS7go1G4OhP9IUgjWooXMhPMCAwEA\n" "AQKCAgEArGxpOQzTAz80KDiWfSCdRvae3dcIoe+epd7RqSWnURDOJfv0thn8DuTu\n" "bb/oW7Cl+scidEBszBnvS1x9QdOwLDZ/gutYDw5CFb0C9mtPLf/a6mSYD8+bNZg7\n" "zjgJvNr9wK/xJIT3IigEyguuy1LfVgm3opIsp2u0PLxd5+Tm0+HjbsUube22dLTp\n" "LAOk//Vr9edRUrJIeKX6ceCnqFCmhDwKxKsrKcgxA8kBcE/OjdJykYYJVjudjgc6\n" "jDjbIDcyWlmQ/v9Ex/LCEhoRIvv3Tvjv1WqugW4X/AdY3XPyN8xn3eoRo3zKjNGl\n" "6SFpNdA506Hwp2HS4JiDz7bUqicaCd63/+h6/Lxf1R9xpM6T6A+C7Ausy0yTpQQb\n" "YzbVgNL7HWelf93zkNOXKAASzJmYq01lBF+LnKL+Yy3q85tr5YXtoNBj21oR+fRa\n" "gBr1gaeqtRF5wj0uiPSg8M4UuDacHkm8sU2V+citkx/vLh+ofl/xVLPxDfIbIT5V\n" "cwQvyoI743zR4/6ZzM9x5a89a2Rvb74YP8dx5nkf7x+Tk9UnGIPXgMuPN7PRMyWY\n" "1wxnCNEdIoZ90qOhrAt8IzJuWOIVY6UdFut9sl8TbKRuzUmX2TQjYBVXULyyjsDQ\n" "pVlOwwncBuHe+BHDn2pqK9BIgI7oK3iB8Fa4YrgGcYYAyuIs6IECggEBAPRIYoTs\n" "KHpb8ugPyYpjIrOYvyca05jP8ctCSi1B4OmUnYuv2KxNap7HIrgysBna5nOBY6LG\n" "0B1Kut7yKd5XtE2vNKVQRd/gR6wBBgm3xIYv9wZHcMFcRZl9xofGSmCk3iOh//Bv\n" "IS+Mpw6TcKlUHlWLaGQBTf8I3/9nrSJ4zvhoJeyojj21+QZ4xI+W+g2tA4WEbc/p\n" "jckVZwGNXwSVvfqnmaSC34cFgDMh1mKmD8g60Mje9ynMgG0cHZ+RxuICfQKgFEI2\n" "iqaagO4kTl+sAwj8XRlcAm+P4/EKjYYOH6DRDQLwybDz+o/RXKkx6lWi3kc7pXmn\n" "3aVI6UREW5UjuqkCggEBAOWIza+AtMxA+9XyJ6222qEq2MduQPYAbvshqQQ+n/93\n" "3z5er2fd854X4HX3/w2Ov+C+OOCnpaOkH+EgWUR4yUolw0YGBaFU2evzV2P6HSkf\n" "Hvw7HxKasquBLppUiOr7YCkm1bd+oDxFuVPq+Zza24KwPfszd2dtGqbXywmv6TLE\n" "6dH4AEBsUNjLs1oqWX2jic60eSk0zzSY/LP57VLR9u6H7GOS1Z4bqt8rA1gJnWOd\n" "/J5fsuL2unRoy3jM0X/tr7ZMukA7wIvBN29YR7j2Qvnkf4PLoHD7ftgPaU4XZf/d\n" "ogc3pTh++0zyPti9UkrCALXP4i3gl8nlKZbepeDcgDsCggEBAKHmU307MzydMilB\n" "RVa1i2syYgYdzn1p3BvVbGoATnsgpyXMPrM7f92Jp2YjGfmYzcFh0NIyJ/4x6BYY\n" "s00MHZCa/S5PPHA7KeVCrGjGZbZ1laeQs5dDe1FWPb0A24yf2CYPmRwV2w2zj4im\n" "iTWAbbZOdbpJ7xKHJEYWxXWiUbHq/K+TquoVb90tL0DnVAS6VSopccopRXIvABzU\n" "QFQ+ljHI4JhasKDBMY0x8O9ilfUjnfpzY6ZNRhSKXMvEBucFtSqHQ8X6dfwjTC4I\n" "2/SmgUB0WZOUGn0sBWtcjh15wNaJlrELOvFPUhH9NQdh8KgfEGhvjKVLbye7YfZ/\n" "w57dljkCggEAacV4wv8UUWtAoX5NOoegh9QuwPfVh4b7nU4NjJ8vK5IZlawcOEjX\n" "Emr+TF5TcfPuB6qgmyWl9pqS9jLp79uZJknwijwMLCPlqA0ioDeJaIGmzaSQ1Qnk\n" "e5Oz3fpGfcIIte3nXf9D54JZvInzLIzNypNcfH1i8I4eUfPu5C/jzjlfZhpaQ1Wm\n" "i8CSjWImivbpcg9IJezn7tzw1h69dgS7PX/1No1bUth9DQnNKKyFknojBvgifuQj\n" "V7FS0f/QKptk9SS2TxM5zyziVrTfmCQjCPR6rkkPTgEWmom/hPTTU+zV1W2W/UnG\n" "k9atj0LuwPRVT3LUTz/HsomfeJ5w4gW6MQKCAQA09gsSFV3yonIXN8lb6v1NbJ0+\n" "6UVyQkP3IWrYtxhmGjOvVTDNgc7CFjKNoWjzA2eeiQOh4BEgAPKS7FEhsPlCd+4U\n" "cI/U/msRtwyYsGkWzlcoGnI6BzuqVQlzWUJnNMv8vK4bZLQud3C7SPge+FZRoZCj\n" "YKUJmBUV/0flipX4g+hAjbE0H3zweLbzkB4TStgxrTAHSLvk4o8UxBn0Dm/eerg3\n" "rzgAlo553rmN82uJdckIaKqJGwGAJwZCWpjtnj4mcYrXIJ6R70NViE9D7c64LLZ7\n" "jvyFqcN8xfd2RYujuYFD9p0UPtTY2C0sUp5nvDQFkB9kq91yTKHrLg4CWa+R\n" "-----END RSA PRIVATE KEY-----\n"; static constexpr char VALID_CERT[] = "-----BEGIN CERTIFICATE-----\n" "MIIF9zCCA9+gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZcxCzAJBgNVBAYTAlVT\n" "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZ\n" "BgNVBAsMElRlc3RpbmcgRGVwYXJ0bWVudDEbMBkGA1UEAwwSaW50LWNhLmV4YW1w\n" "bGUuY29tMSIwIAYJKoZIhvcNAQkBFhNnYXJwbHlAc2FuZHN0b3JtLmlvMCAXDTE2\n" "MDUzMTA0Mjg0N1oYDzIxMTYwNTMxMDQyODQ3WjCBkDELMAkGA1UEBhMCVVMxEzAR\n" "BgNVBAgMCkNhbGlmb3JuaWExFTATBgNVBAoMDFNhbmRzdG9ybS5pbzEbMBkGA1UE\n" "CwwSVGVzdGluZyBEZXBhcnRtZW50MRQwEgYDVQQDDAtleGFtcGxlLmNvbTEiMCAG\n" "CSqGSIb3DQEJARYTZ2FycGx5QHNhbmRzdG9ybS5pbzCCAiIwDQYJKoZIhvcNAQEB\n" "BQADggIPADCCAgoCggIBANsHStqtDiv+hz3eunDuTrcY/207sgCEbdObcpANvLVz\n" "jSc67B9aemMqa/yT8BmHkgTock1DSkEOOmEnsvszkzt1R9tf66XXhAJglYMv3I7S\n" "WqMmAFbkn+vuTXB6Yf6D1/cEalQG9wY6Y0Aci6+BLynYStkvq9ACXDdW3j1iUiKU\n" "LptBIJsc9c9jlkEV9Pt8lfLWULZNYiSr5jyNSTRSKkLIEDMUIAUEsrEYHxasMioA\n" "4BB6FGKhL1Ns2XjkNaJ80TS8X95rHmc/SRrGk2qmI7OL7DkimEE+VHGvre6tO2dm\n" "xX/e5CkyXVVeDbV8sm4Vl8b5tafjEqEkqU4OO6DeAFRh++T7rJo52oI1pTu60wGY\n" "de0IgDkVxoBpWEalQmCjzHWoXoRI+3LqTZ+eZsR+GGqJha8Iz2sl2v9U7F8z9e2+\n" "Wk0jDcntKujcDjCFBjgMp/MExFEkgwxKwuJ4oUR8Rm95BU6KtXagiPUnPS9R2Oe0\n" "qfLaSuQkA9Rt6ac4cGdv15/OIqUK4qILFeQC28bpAu1hmY04AWobDGQah1rViLjl\n" "IwCq/1/46aDYAaZE+lS6qcfS+049c47xcC7BNGAEKt+5cdkmpv/gkswNwRw7Mehj\n" "UqbUSimLz0iGAxcSVL3/yh4PJiP4J6mtBIByb0zwTku4KNRuDoT/SFII1qKFzITz\n" "AgMBAAGjUDBOMB0GA1UdDgQWBBT2qSSBGCUEYfohpsiHlq9yECjAqDAfBgNVHSME\n" "GDAWgBTL6VlL+88HorxZeAOmcpGDnvYaIDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3\n" "DQEBCwUAA4ICAQCb34A5Hz6iI80U+mSnkvOnVtaqyxnsVcBbfMcsRyGG/GSVBNJD\n" "zcCxnxrPc0NXcsK3wIR7KU1oQmusNCtI2XNd1lceBytQD6TDzcuuNCpjF7Uh+pdi\n" "AL2HzDoy9q4Mxxk1wTGDuyy+4opZQG12fe9pr4wo93/BXbE4kDrSzp/2iTQp9/zh\n" "JRrRISwyFH6HKX/MoVbpJfAqiMHXHeHylH6h4lUVVfYFSSB8PWL3lxAwCM0ECGmd\n" "ZMGyh089ViW0mBoF5lacwHkAnw17S0JrUM9+66oPRLIo2rsgnNj4sMo/9dzSJ9/T\n" "OneewTK4O8t/mZNp1auYFC1+m8wWRh0G5Y5CwZ1CJqy8mHMd/33FbMO+MyyEeFkG\n" "DHNzCYEupp1ymqvFZK8TyIX1m/QOy0W6NT6INFY1dy3CoJWnMAJRKvxeFQGJ28Up\n" "wYPZPj7xxGb6TdgVC0c7kMCorIu3tsLLRtwtAbN/D8ogS78QwgyLqJ41friFMD+9\n" "AS1sjfqiiC4hpr11z+xdCJLb4vkBsigHuofjx3uyiueyKXQTFQCXjF2w1FbJTKSZ\n" "kQ4CP2eG8UN5BN46kih89NjBUCduXq/x4SJCEjtG57QFBogmcG/OTX1pCOwjnK4L\n" "z6Kz4+2VTNYHvIXSIoicO+jZeISYpllg6ISNBeqoOp2zp6Rf6rwR6/YZog==\n" "-----END CERTIFICATE-----\n"; static constexpr char EXPIRED_CERT[] = "-----BEGIN CERTIFICATE-----\n" "MIIF9TCCA92gAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZcxCzAJBgNVBAYTAlVT\n" "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZ\n" "BgNVBAsMElRlc3RpbmcgRGVwYXJ0bWVudDEbMBkGA1UEAwwSaW50LWNhLmV4YW1w\n" "bGUuY29tMSIwIAYJKoZIhvcNAQkBFhNnYXJwbHlAc2FuZHN0b3JtLmlvMB4XDTE2\n" "MDEwMTAwMDAwMFoXDTE2MDEwMTAwMDAwMFowgZAxCzAJBgNVBAYTAlVTMRMwEQYD\n" "VQQIDApDYWxpZm9ybmlhMRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZBgNVBAsM\n" "ElRlc3RpbmcgRGVwYXJ0bWVudDEUMBIGA1UEAwwLZXhhbXBsZS5jb20xIjAgBgkq\n" "hkiG9w0BCQEWE2dhcnBseUBzYW5kc3Rvcm0uaW8wggIiMA0GCSqGSIb3DQEBAQUA\n" "A4ICDwAwggIKAoICAQDbB0rarQ4r/oc93rpw7k63GP9tO7IAhG3Tm3KQDby1c40n\n" "OuwfWnpjKmv8k/AZh5IE6HJNQ0pBDjphJ7L7M5M7dUfbX+ul14QCYJWDL9yO0lqj\n" "JgBW5J/r7k1wemH+g9f3BGpUBvcGOmNAHIuvgS8p2ErZL6vQAlw3Vt49YlIilC6b\n" "QSCbHPXPY5ZBFfT7fJXy1lC2TWIkq+Y8jUk0UipCyBAzFCAFBLKxGB8WrDIqAOAQ\n" "ehRioS9TbNl45DWifNE0vF/eax5nP0kaxpNqpiOzi+w5IphBPlRxr63urTtnZsV/\n" "3uQpMl1VXg21fLJuFZfG+bWn4xKhJKlODjug3gBUYfvk+6yaOdqCNaU7utMBmHXt\n" "CIA5FcaAaVhGpUJgo8x1qF6ESPty6k2fnmbEfhhqiYWvCM9rJdr/VOxfM/XtvlpN\n" "Iw3J7Sro3A4whQY4DKfzBMRRJIMMSsLieKFEfEZveQVOirV2oIj1Jz0vUdjntKny\n" "2krkJAPUbemnOHBnb9efziKlCuKiCxXkAtvG6QLtYZmNOAFqGwxkGoda1Yi45SMA\n" "qv9f+Omg2AGmRPpUuqnH0vtOPXOO8XAuwTRgBCrfuXHZJqb/4JLMDcEcOzHoY1Km\n" "1Eopi89IhgMXElS9/8oeDyYj+CeprQSAcm9M8E5LuCjUbg6E/0hSCNaihcyE8wID\n" "AQABo1AwTjAdBgNVHQ4EFgQU9qkkgRglBGH6IabIh5avchAowKgwHwYDVR0jBBgw\n" "FoAUy+lZS/vPB6K8WXgDpnKRg572GiAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0B\n" "AQsFAAOCAgEAtIjwUBNVrc62XK36bHHFLawYJaJUNj4zwdBUmVc5BL0tPHfDIwhP\n" "GBbxZCrAbPlHUkWig6l52S2lG0Aq2rtBwZPrjSJMio4ln/3y9qZjdqCjKtiQy7lc\n" "jQu9wXzlcTomARt2NcgTMedOrC4+eXMExUZ+n7xHKsj9u6sYeH0LXtMfg0Xzloc7\n" "ojF+ISM3bZbZQzGNkG+Fz1OCGgJ6W/wHxAadIHMmOMl1YN9ZRbO7T93AlMSGpehi\n" "LYh/n8B769yyxlfMWIM/aGECZdLyE5NUeN2r0jfjSkgp9l9dXr5fJL7bA54YcQC4\n" "dWjuMf8tpPpI1580fU5xD0ta6NGeLkQ5lEUexIaH0vSmUDs0N9HpTooGFcONl75R\n" "sEaB7/xH/ZGjtRXTc/QAPyfzFvbQNgRSoiif7DHRt7Wv13fKt/xL9uZlTGZUfojt\n" "eIFe82dNIffpoXkkHGFlpxxgnpwMb62N7BFqi2uJrSasrNEYZuEKTQMb1p76qojp\n" "6zAUPaut+FHgM5zVaKkvdXvJEReHG7un2a/DfZelIa8VIWO4BGQJBhSaKbRiGXiu\n" "2BbV3/qp9R0msirfyesBa/NV1syw2PYoouYukSdMfROK4r2FGPN7cw0AYrY3NbGG\n" "5jFKu2Vr1krHysnmFyXhkoyVSy4dYjrrqa1ItZW9SF0f83IN54C/P7E=\n" "-----END CERTIFICATE-----\n"; static constexpr char SELF_SIGNED_CERT[] = "-----BEGIN CERTIFICATE-----\n" "MIIGHzCCBAegAwIBAgIJAN7Q46+wlXJHMA0GCSqGSIb3DQEBCwUAMIGkMQswCQYD\n" "VQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UEBwwJUGFsbyBBbHRv\n" "MRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZBgNVBAsMElRlc3RpbmcgRGVwYXJ0\n" "bWVudDEUMBIGA1UEAwwLZXhhbXBsZS5jb20xIjAgBgkqhkiG9w0BCQEWE2dhcnBs\n" "eUBzYW5kc3Rvcm0uaW8wIBcNMTYwNTMxMDQyODQ3WhgPMjExNjA1MzEwNDI4NDda\n" "MIGkMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UEBwwJ\n" "UGFsbyBBbHRvMRUwEwYDVQQKDAxTYW5kc3Rvcm0uaW8xGzAZBgNVBAsMElRlc3Rp\n" "bmcgRGVwYXJ0bWVudDEUMBIGA1UEAwwLZXhhbXBsZS5jb20xIjAgBgkqhkiG9w0B\n" "CQEWE2dhcnBseUBzYW5kc3Rvcm0uaW8wggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw\n" "ggIKAoICAQDbB0rarQ4r/oc93rpw7k63GP9tO7IAhG3Tm3KQDby1c40nOuwfWnpj\n" "Kmv8k/AZh5IE6HJNQ0pBDjphJ7L7M5M7dUfbX+ul14QCYJWDL9yO0lqjJgBW5J/r\n" "7k1wemH+g9f3BGpUBvcGOmNAHIuvgS8p2ErZL6vQAlw3Vt49YlIilC6bQSCbHPXP\n" "Y5ZBFfT7fJXy1lC2TWIkq+Y8jUk0UipCyBAzFCAFBLKxGB8WrDIqAOAQehRioS9T\n" "bNl45DWifNE0vF/eax5nP0kaxpNqpiOzi+w5IphBPlRxr63urTtnZsV/3uQpMl1V\n" "Xg21fLJuFZfG+bWn4xKhJKlODjug3gBUYfvk+6yaOdqCNaU7utMBmHXtCIA5FcaA\n" "aVhGpUJgo8x1qF6ESPty6k2fnmbEfhhqiYWvCM9rJdr/VOxfM/XtvlpNIw3J7Sro\n" "3A4whQY4DKfzBMRRJIMMSsLieKFEfEZveQVOirV2oIj1Jz0vUdjntKny2krkJAPU\n" "bemnOHBnb9efziKlCuKiCxXkAtvG6QLtYZmNOAFqGwxkGoda1Yi45SMAqv9f+Omg\n" "2AGmRPpUuqnH0vtOPXOO8XAuwTRgBCrfuXHZJqb/4JLMDcEcOzHoY1Km1Eopi89I\n" "hgMXElS9/8oeDyYj+CeprQSAcm9M8E5LuCjUbg6E/0hSCNaihcyE8wIDAQABo1Aw\n" "TjAdBgNVHQ4EFgQU9qkkgRglBGH6IabIh5avchAowKgwHwYDVR0jBBgwFoAU9qkk\n" "gRglBGH6IabIh5avchAowKgwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n" "AgEA0dYsDTtt75fqQ35+ZHZGFCzJ4eUM4m/IdXfMlVw5Eb+2XA7pAVQgHhUD2qU0\n" "6Xk5EqINr7X8TvvR3OGVzlAw7BD4ZVKXkNy82BwRoydSUN9GM+Q1yqHES8+dBH5D\n" "jdrBq5XdtG3cQEjxxHPb5iWc5MLvVM0UtuuBtk3rp9IoY3+ReczGbm8CXep7jyIv\n" "W0wTi8gjMPWqn/f4ikkprMh0hXCwRTojfWi+Gl1sKyzviG+FF3hJj/fNf7IKYG8d\n" "lO/Ay9wqY+j7oMIz6RUUOht31tLnTaSSDDknuHF+r0GJoNWjsncf/NseGDLBiTkZ\n" "bBGMJhB3Pd6FNcLuREtE1WT5EwJr9WZeSh1mQPssQvwgYjdCI558sw1WEBdON73M\n" "0/aZzc/gDx9G1zxUxzn85/pxpBeQgb+J8iaAz1Iy9Gn64A6rVbQWAb5/xZ4y1LSe\n" "xTFVcK9spSf3k2FX24DPOov3oLu7vGmgye76bD4I0WtcVFFK5vXsfXGHq+P8EXv5\n" "F2KmlitfAh6N+uSeWzROrwU7roYsg81epvXVTYR/MyXNEB46iRMNdOqWThpdteEk\n" "qts6SreMmr0+aX7oqJ52Ohtw437JvxMqd5PNHuU3qQQS2o7cJzlZ+dDQMoZdxNaV\n" "bWPnPRk8plkBJLuQZA7KcTGA3b6qHl0hMTDJUE8bscNmMs4=\n" "-----END CERTIFICATE-----\n"; // ======================================================================================= class ErrorNexus { // Helper class that wraps various promises such that if one throws an exception, they all do. public: ErrorNexus(): ErrorNexus(kj::newPromiseAndFulfiller()) {} template kj::Promise wrap(kj::Promise&& promise) { return promise.catch_([this](kj::Exception&& e) -> kj::Promise { fulfiller->reject(kj::cp(e)); return kj::mv(e); }).exclusiveJoin(failurePromise.addBranch().then([]() -> T { KJ_UNREACHABLE; })); } private: kj::ForkedPromise failurePromise; kj::Own> fulfiller; ErrorNexus(kj::PromiseFulfillerPair paf) : failurePromise(kj::mv(paf.promise).fork()), fulfiller(kj::mv(paf.fulfiller)) {} }; struct TlsTest { kj::AsyncIoContext io = setupAsyncIo(); TlsContext tlsClient; TlsContext tlsServer; TlsTest(TlsContext::Options clientOpts = defaultClient(), TlsContext::Options serverOpts = defaultServer()) : tlsClient(kj::mv(clientOpts)), tlsServer(kj::mv(serverOpts)) {} static TlsContext::Options defaultServer() { static TlsKeypair keypair = { TlsPrivateKey(HOST_KEY), TlsCertificate(kj::str(VALID_CERT, INTERMEDIATE_CERT)) }; TlsContext::Options options; options.defaultKeypair = keypair; return options; } static TlsContext::Options defaultClient() { static TlsCertificate caCert(CA_CERT); TlsContext::Options options; options.useSystemTrustStore = false; options.trustedCertificates = kj::arrayPtr(&caCert, 1); return options; } }; KJ_TEST("TLS basics") { TlsTest test; ErrorNexus e; auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = e.wrap(test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com")); auto serverPromise = e.wrap(test.tlsServer.wrapServer(kj::mv(pipe.ends[1]))); auto client = clientPromise.wait(test.io.waitScope); auto server = serverPromise.wait(test.io.waitScope); auto writePromise = client->write("foo", 3); char buf[4]; server->read(&buf, 3).wait(test.io.waitScope); buf[3] = '\0'; KJ_ASSERT(kj::StringPtr(buf) == "foo"); } KJ_TEST("TLS multiple messages") { TlsTest test; ErrorNexus e; auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = e.wrap(test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com")); auto serverPromise = e.wrap(test.tlsServer.wrapServer(kj::mv(pipe.ends[1]))); auto client = clientPromise.wait(test.io.waitScope); auto server = serverPromise.wait(test.io.waitScope); auto writePromise = client->write("foo", 3) .then([&]() { return client->write("bar", 3); }); char buf[4]; buf[3] = '\0'; server->read(&buf, 3).wait(test.io.waitScope); KJ_ASSERT(kj::StringPtr(buf) == "foo"); writePromise = writePromise .then([&]() { return client->write("baz", 3); }); server->read(&buf, 3).wait(test.io.waitScope); KJ_ASSERT(kj::StringPtr(buf) == "bar"); server->read(&buf, 3).wait(test.io.waitScope); KJ_ASSERT(kj::StringPtr(buf) == "baz"); auto readPromise = server->read(&buf, 3); KJ_EXPECT(!readPromise.poll(test.io.waitScope)); writePromise = writePromise .then([&]() { return client->write("qux", 3); }); readPromise.wait(test.io.waitScope); KJ_ASSERT(kj::StringPtr(buf) == "qux"); } kj::Promise writeN(kj::AsyncIoStream& stream, kj::StringPtr text, size_t count) { if (count == 0) return kj::READY_NOW; --count; return stream.write(text.begin(), text.size()) .then([&stream, text, count]() { return writeN(stream, text, count); }); } kj::Promise readN(kj::AsyncIoStream& stream, kj::StringPtr text, size_t count) { if (count == 0) return kj::READY_NOW; --count; auto buf = kj::heapString(text.size()); auto promise = stream.read(buf.begin(), buf.size()); return promise.then(kj::mvCapture(buf, [&stream, text, count](kj::String buf) { KJ_ASSERT(buf == text, buf, text, count); return readN(stream, text, count); })); } KJ_TEST("TLS full duplex") { TlsTest test; ErrorNexus e; auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = e.wrap(test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com")); auto serverPromise = e.wrap(test.tlsServer.wrapServer(kj::mv(pipe.ends[1]))); auto client = clientPromise.wait(test.io.waitScope); auto server = serverPromise.wait(test.io.waitScope); auto writeUp = writeN(*client, "foo", 10000); auto readDown = readN(*client, "bar", 10000); KJ_EXPECT(!writeUp.poll(test.io.waitScope)); KJ_EXPECT(!readDown.poll(test.io.waitScope)); auto writeDown = writeN(*server, "bar", 10000); auto readUp = readN(*server, "foo", 10000); readUp.wait(test.io.waitScope); readDown.wait(test.io.waitScope); writeUp.wait(test.io.waitScope); writeDown.wait(test.io.waitScope); } class TestSniCallback: public TlsSniCallback { public: kj::Maybe getKey(kj::StringPtr hostname) override { ++callCount; KJ_ASSERT(hostname == "example.com"); return TlsKeypair { TlsPrivateKey(HOST_KEY), TlsCertificate(kj::str(VALID_CERT, INTERMEDIATE_CERT)) }; } uint callCount = 0; }; KJ_TEST("TLS SNI") { TlsContext::Options serverOptions; TestSniCallback callback; serverOptions.sniCallback = callback; TlsTest test(TlsTest::defaultClient(), serverOptions); ErrorNexus e; auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = e.wrap(test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com")); auto serverPromise = e.wrap(test.tlsServer.wrapServer(kj::mv(pipe.ends[1]))); auto client = clientPromise.wait(test.io.waitScope); auto server = serverPromise.wait(test.io.waitScope); auto writePromise = client->write("foo", 3); char buf[4]; server->read(&buf, 3).wait(test.io.waitScope); buf[3] = '\0'; KJ_ASSERT(kj::StringPtr(buf) == "foo"); KJ_ASSERT(callback.callCount == 1); } void expectInvalidCert(kj::StringPtr hostname, TlsCertificate cert, kj::StringPtr message) { TlsKeypair keypair = { TlsPrivateKey(HOST_KEY), kj::mv(cert) }; TlsContext::Options serverOpts; serverOpts.defaultKeypair = keypair; TlsTest test(TlsTest::defaultClient(), kj::mv(serverOpts)); ErrorNexus e; auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = e.wrap(test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), hostname)); auto serverPromise = e.wrap(test.tlsServer.wrapServer(kj::mv(pipe.ends[1]))); KJ_EXPECT_THROW_MESSAGE(message, clientPromise.wait(test.io.waitScope)); } KJ_TEST("TLS certificate validation") { expectInvalidCert("wrong.com", TlsCertificate(kj::str(VALID_CERT, INTERMEDIATE_CERT)), "Hostname mismatch"); expectInvalidCert("example.com", TlsCertificate(VALID_CERT), "unable to get local issuer certificate"); expectInvalidCert("example.com", TlsCertificate(kj::str(EXPIRED_CERT, INTERMEDIATE_CERT)), "certificate has expired"); expectInvalidCert("example.com", TlsCertificate(SELF_SIGNED_CERT), "self signed certificate"); } // BoringSSL seems to print error messages differently. #ifdef OPENSSL_IS_BORINGSSL #define SSL_MESSAGE(interesting, boring) boring #else #define SSL_MESSAGE(interesting, boring) interesting #endif KJ_TEST("TLS client certificate verification") { TlsContext::Options serverOptions = TlsTest::defaultServer(); TlsContext::Options clientOptions = TlsTest::defaultClient(); serverOptions.verifyClients = true; serverOptions.trustedCertificates = clientOptions.trustedCertificates; // No certificate loaded in the client: fail { TlsTest test(clientOptions, serverOptions); auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com") .then([](kj::Own stream) { auto promise = stream->readAllBytes(); return promise.attach(kj::mv(stream)); }); auto serverPromise = test.tlsServer.wrapServer(kj::mv(pipe.ends[1])); KJ_EXPECT_THROW_MESSAGE( SSL_MESSAGE("peer did not return a certificate", "PEER_DID_NOT_RETURN_A_CERTIFICATE"), serverPromise.wait(test.io.waitScope)); #if !KJ_NO_EXCEPTIONS // if exceptions are disabled, we're now in a bad state because // KJ_EXPECT_THROW_MESSAGE() runs in a forked child process. KJ_EXPECT_THROW_MESSAGE( SSL_MESSAGE("alert", // "alert handshake failure" or "alert certificate required" "TLSV1_CERTIFICATE_REQUIRED"), clientPromise.wait(test.io.waitScope)); #endif } // Self-signed certificate loaded in the client: fail { TlsKeypair selfSignedKeypair = { TlsPrivateKey(HOST_KEY), TlsCertificate(SELF_SIGNED_CERT) }; clientOptions.defaultKeypair = selfSignedKeypair; TlsTest test(clientOptions, serverOptions); auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com") .then([](kj::Own stream) { auto promise = stream->readAllBytes(); return promise.attach(kj::mv(stream)); }); auto serverPromise = test.tlsServer.wrapServer(kj::mv(pipe.ends[1])); KJ_EXPECT_THROW_MESSAGE( SSL_MESSAGE("certificate verify failed", "CERTIFICATE_VERIFY_FAILED"), serverPromise.wait(test.io.waitScope)); #if !KJ_NO_EXCEPTIONS // if exceptions are disabled, we're now in a bad state because // KJ_EXPECT_THROW_MESSAGE() runs in a forked child process. KJ_EXPECT_THROW_MESSAGE( SSL_MESSAGE("alert unknown ca", "TLSV1_ALERT_UNKNOWN_CA"), clientPromise.wait(test.io.waitScope)); #endif } // Trusted certificate loaded in the client: success. { clientOptions.defaultKeypair = serverOptions.defaultKeypair; TlsTest test(clientOptions, serverOptions); ErrorNexus e; auto pipe = test.io.provider->newTwoWayPipe(); auto clientPromise = e.wrap(test.tlsClient.wrapClient(kj::mv(pipe.ends[0]), "example.com")); auto serverPromise = e.wrap(test.tlsServer.wrapServer(kj::mv(pipe.ends[1]))); auto client = clientPromise.wait(test.io.waitScope); auto server = serverPromise.wait(test.io.waitScope); auto writePromise = client->write("foo", 3); char buf[4]; server->read(&buf, 3).wait(test.io.waitScope); buf[3] = '\0'; KJ_ASSERT(kj::StringPtr(buf) == "foo"); } } #ifdef KJ_EXTERNAL_TESTS KJ_TEST("TLS to capnproto.org") { kj::AsyncIoContext io = setupAsyncIo(); TlsContext tls; auto network = tls.wrapNetwork(io.provider->getNetwork()); auto addr = network->parseAddress("capnproto.org", 443).wait(io.waitScope); auto stream = addr->connect().wait(io.waitScope); kj::StringPtr request = "HEAD / HTTP/1.1\r\n" "Host: capnproto.org\r\n" "Connection: close\r\n" "User-Agent: capnp-test/0.6\r\n" "\r\n"; stream->write(request.begin(), request.size()).wait(io.waitScope); char buffer[4096]; size_t n = stream->tryRead(buffer, sizeof(buffer) - 1, sizeof(buffer) - 1).wait(io.waitScope); buffer[n] = '\0'; kj::StringPtr response(buffer, n); KJ_ASSERT(response.startsWith("HTTP/1.1 200 OK\r\n")); } #endif } // namespace } // namespace kj #endif // KJ_HAS_OPENSSL capnproto-c++-0.8.0/src/kj/parse/0000755000175000017500000000000013650320027017253 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/kj/parse/common.h0000644000175000017500000006360713650101756020736 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // Parser combinator framework! // // This file declares several functions which construct parsers, usually taking other parsers as // input, thus making them parser combinators. // // A valid parser is any functor which takes a reference to an input cursor (defined below) as its // input and returns a Maybe. The parser returns null on parse failure, or returns the parsed // result on success. // // An "input cursor" is any type which implements the same interface as IteratorInput, below. Such // a type acts as a pointer to the current input location. When a parser returns successfully, it // will have updated the input cursor to point to the position just past the end of what was parsed. // On failure, the cursor position is unspecified. #pragma once #include "../common.h" #include "../memory.h" #include "../array.h" #include "../tuple.h" #include "../vector.h" #if _MSC_VER && !__clang__ #include // result_of_t #endif KJ_BEGIN_HEADER namespace kj { namespace parse { template class IteratorInput { // A parser input implementation based on an iterator range. public: IteratorInput(Iterator begin, Iterator end) : parent(nullptr), pos(begin), end(end), best(begin) {} explicit IteratorInput(IteratorInput& parent) : parent(&parent), pos(parent.pos), end(parent.end), best(parent.pos) {} ~IteratorInput() { if (parent != nullptr) { parent->best = kj::max(kj::max(pos, best), parent->best); } } KJ_DISALLOW_COPY(IteratorInput); void advanceParent() { parent->pos = pos; } void forgetParent() { parent = nullptr; } bool atEnd() { return pos == end; } auto current() -> decltype(*instance()) { KJ_IREQUIRE(!atEnd()); return *pos; } auto consume() -> decltype(*instance()) { KJ_IREQUIRE(!atEnd()); return *pos++; } void next() { KJ_IREQUIRE(!atEnd()); ++pos; } Iterator getBest() { return kj::max(pos, best); } Iterator getPosition() { return pos; } private: IteratorInput* parent; Iterator pos; Iterator end; Iterator best; // furthest we got with any sub-input }; template struct OutputType_; template struct OutputType_> { typedef T Type; }; template using OutputType = typename OutputType_< #if _MSC_VER && !__clang__ std::result_of_t // The instance() based version below results in: // C2064: term does not evaluate to a function taking 1 arguments #else decltype(instance()(instance())) #endif >::Type; // Synonym for the output type of a parser, given the parser type and the input type. // ======================================================================================= template class ParserRef { // Acts as a reference to some other parser, with simplified type. The referenced parser // is polymorphic by virtual call rather than templates. For grammars of non-trivial size, // it is important to inject refs into the grammar here and there to prevent the parser types // from becoming ridiculous. Using too many of them can hurt performance, though. public: ParserRef(): parser(nullptr), wrapper(nullptr) {} ParserRef(const ParserRef&) = default; ParserRef(ParserRef&&) = default; ParserRef& operator=(const ParserRef& other) = default; ParserRef& operator=(ParserRef&& other) = default; template constexpr ParserRef(Other&& other) : parser(&other), wrapper(&WrapperImplInstance>::instance) { static_assert(kj::isReference(), "ParserRef should not be assigned to a temporary."); } template inline ParserRef& operator=(Other&& other) { static_assert(kj::isReference(), "ParserRef should not be assigned to a temporary."); parser = &other; wrapper = &WrapperImplInstance>::instance; return *this; } KJ_ALWAYS_INLINE(Maybe operator()(Input& input) const) { // Always inline in the hopes that this allows branch prediction to kick in so the virtual call // doesn't hurt so much. return wrapper->parse(parser, input); } private: struct Wrapper { virtual Maybe parse(const void* parser, Input& input) const = 0; }; template struct WrapperImpl: public Wrapper { Maybe parse(const void* parser, Input& input) const override { return (*reinterpret_cast(parser))(input); } }; template struct WrapperImplInstance { #if _MSC_VER && !__clang__ // TODO(msvc): MSVC currently fails to initialize vtable pointers for constexpr values so // we have to make this just const instead. static const WrapperImpl instance; #else static constexpr WrapperImpl instance = WrapperImpl(); #endif }; const void* parser; const Wrapper* wrapper; }; template template #if _MSC_VER && !__clang__ const typename ParserRef::template WrapperImpl ParserRef::WrapperImplInstance::instance = WrapperImpl(); #else constexpr typename ParserRef::template WrapperImpl ParserRef::WrapperImplInstance::instance; #endif template constexpr ParserRef> ref(ParserImpl& impl) { // Constructs a ParserRef. You must specify the input type explicitly, e.g. // `ref(myParser)`. return ParserRef>(impl); } // ------------------------------------------------------------------- // any // Output = one token class Any_ { public: template Maybe().consume())>> operator()(Input& input) const { if (input.atEnd()) { return nullptr; } else { return input.consume(); } } }; constexpr Any_ any = Any_(); // A parser which matches any token and simply returns it. // ------------------------------------------------------------------- // exactly() // Output = Tuple<> template class Exactly_ { public: explicit constexpr Exactly_(T&& expected): expected(expected) {} template Maybe> operator()(Input& input) const { if (input.atEnd() || input.current() != expected) { return nullptr; } else { input.next(); return Tuple<>(); } } private: T expected; }; template constexpr Exactly_ exactly(T&& expected) { // Constructs a parser which succeeds when the input is exactly the token specified. The // result is always the empty tuple. return Exactly_(kj::fwd(expected)); } // ------------------------------------------------------------------- // exactlyConst() // Output = Tuple<> template class ExactlyConst_ { public: explicit constexpr ExactlyConst_() {} template Maybe> operator()(Input& input) const { if (input.atEnd() || input.current() != expected) { return nullptr; } else { input.next(); return Tuple<>(); } } }; template constexpr ExactlyConst_ exactlyConst() { // Constructs a parser which succeeds when the input is exactly the token specified. The // result is always the empty tuple. This parser is templated on the token value which may cause // it to perform better -- or worse. Be sure to measure. return ExactlyConst_(); } // ------------------------------------------------------------------- // constResult() template class ConstResult_ { public: explicit constexpr ConstResult_(SubParser&& subParser, Result&& result) : subParser(kj::fwd(subParser)), result(kj::fwd(result)) {} template Maybe operator()(Input& input) const { if (subParser(input) == nullptr) { return nullptr; } else { return result; } } private: SubParser subParser; Result result; }; template constexpr ConstResult_ constResult(SubParser&& subParser, Result&& result) { // Constructs a parser which returns exactly `result` if `subParser` is successful. return ConstResult_(kj::fwd(subParser), kj::fwd(result)); } template constexpr ConstResult_> discard(SubParser&& subParser) { // Constructs a parser which wraps `subParser` but discards the result. return constResult(kj::fwd(subParser), Tuple<>()); } // ------------------------------------------------------------------- // sequence() // Output = Flattened Tuple of outputs of sub-parsers. template class Sequence_; template class Sequence_ { public: template explicit constexpr Sequence_(T&& firstSubParser, U&&... rest) : first(kj::fwd(firstSubParser)), rest(kj::fwd(rest)...) {} // TODO(msvc): The trailing return types on `operator()` and `parseNext()` expose at least two // bugs in MSVC: // // 1. An ICE. // 2. 'error C2672: 'operator __surrogate_func': no matching overloaded function found)', // which crops up in numerous places when trying to build the capnp command line tools. // // The only workaround I found for both bugs is to omit the trailing return types and instead // rely on C++14's return type deduction. template auto operator()(Input& input) const #if !_MSC_VER || __clang__ -> Maybe>(), instance>()...))> #endif { return parseNext(input); } template auto parseNext(Input& input, InitialParams&&... initialParams) const #if !_MSC_VER || __clang__ -> Maybe(initialParams)..., instance>(), instance>()...))> #endif { KJ_IF_MAYBE(firstResult, first(input)) { return rest.parseNext(input, kj::fwd(initialParams)..., kj::mv(*firstResult)); } else { // TODO(msvc): MSVC depends on return type deduction to compile this function, so we need to // help it deduce the right type on this code path. return Maybe(initialParams)..., instance>(), instance>()...))>{nullptr}; } } private: FirstSubParser first; Sequence_ rest; }; template <> class Sequence_<> { public: template Maybe> operator()(Input& input) const { return parseNext(input); } template auto parseNext(Input& input, Params&&... params) const -> Maybe(params)...))> { return tuple(kj::fwd(params)...); } }; template constexpr Sequence_ sequence(SubParsers&&... subParsers) { // Constructs a parser that executes each of the parameter parsers in sequence and returns a // tuple of their results. return Sequence_(kj::fwd(subParsers)...); } // ------------------------------------------------------------------- // many() // Output = Array of output of sub-parser, or just a uint count if the sub-parser returns Tuple<>. template class Many_ { template > struct Impl; public: explicit constexpr Many_(SubParser&& subParser) : subParser(kj::fwd(subParser)) {} template auto operator()(Input& input) const -> decltype(Impl::apply(instance(), input)); private: SubParser subParser; }; template template struct Many_::Impl { static Maybe> apply(const SubParser& subParser, Input& input) { typedef Vector> Results; Results results; while (!input.atEnd()) { Input subInput(input); KJ_IF_MAYBE(subResult, subParser(subInput)) { subInput.advanceParent(); results.add(kj::mv(*subResult)); } else { break; } } if (atLeastOne && results.empty()) { return nullptr; } return results.releaseAsArray(); } }; template template struct Many_::Impl> { // If the sub-parser output is Tuple<>, just return a count. static Maybe apply(const SubParser& subParser, Input& input) { uint count = 0; while (!input.atEnd()) { Input subInput(input); KJ_IF_MAYBE(subResult, subParser(subInput)) { subInput.advanceParent(); ++count; } else { break; } } if (atLeastOne && count == 0) { return nullptr; } return count; } }; template template auto Many_::operator()(Input& input) const -> decltype(Impl::apply(instance(), input)) { return Impl>::apply(subParser, input); } template constexpr Many_ many(SubParser&& subParser) { // Constructs a parser that repeatedly executes the given parser until it fails, returning an // Array of the results (or a uint count if `subParser` returns an empty tuple). return Many_(kj::fwd(subParser)); } template constexpr Many_ oneOrMore(SubParser&& subParser) { // Like `many()` but the parser must parse at least one item to be successful. return Many_(kj::fwd(subParser)); } // ------------------------------------------------------------------- // times() // Output = Array of output of sub-parser, or Tuple<> if sub-parser returns Tuple<>. template class Times_ { template > struct Impl; public: explicit constexpr Times_(SubParser&& subParser, uint count) : subParser(kj::fwd(subParser)), count(count) {} template auto operator()(Input& input) const -> decltype(Impl::apply(instance(), instance(), input)); private: SubParser subParser; uint count; }; template template struct Times_::Impl { static Maybe> apply(const SubParser& subParser, uint count, Input& input) { auto results = heapArrayBuilder>(count); while (results.size() < count) { if (input.atEnd()) { return nullptr; } else KJ_IF_MAYBE(subResult, subParser(input)) { results.add(kj::mv(*subResult)); } else { return nullptr; } } return results.finish(); } }; template template struct Times_::Impl> { // If the sub-parser output is Tuple<>, just return a count. static Maybe> apply(const SubParser& subParser, uint count, Input& input) { uint actualCount = 0; while (actualCount < count) { if (input.atEnd()) { return nullptr; } else KJ_IF_MAYBE(subResult, subParser(input)) { ++actualCount; } else { return nullptr; } } return tuple(); } }; template template auto Times_::operator()(Input& input) const -> decltype(Impl::apply(instance(), instance(), input)) { return Impl>::apply(subParser, count, input); } template constexpr Times_ times(SubParser&& subParser, uint count) { // Constructs a parser that repeats the subParser exactly `count` times. return Times_(kj::fwd(subParser), count); } // ------------------------------------------------------------------- // optional() // Output = Maybe template class Optional_ { public: explicit constexpr Optional_(SubParser&& subParser) : subParser(kj::fwd(subParser)) {} template Maybe>> operator()(Input& input) const { typedef Maybe> Result; Input subInput(input); KJ_IF_MAYBE(subResult, subParser(subInput)) { subInput.advanceParent(); return Result(kj::mv(*subResult)); } else { return Result(nullptr); } } private: SubParser subParser; }; template constexpr Optional_ optional(SubParser&& subParser) { // Constructs a parser that accepts zero or one of the given sub-parser, returning a Maybe // of the sub-parser's result. return Optional_(kj::fwd(subParser)); } // ------------------------------------------------------------------- // oneOf() // All SubParsers must have same output type, which becomes the output type of the // OneOfParser. template class OneOf_; template class OneOf_ { public: explicit constexpr OneOf_(FirstSubParser&& firstSubParser, SubParsers&&... rest) : first(kj::fwd(firstSubParser)), rest(kj::fwd(rest)...) {} template Maybe> operator()(Input& input) const { { Input subInput(input); Maybe> firstResult = first(subInput); if (firstResult != nullptr) { subInput.advanceParent(); return kj::mv(firstResult); } } // Hoping for some tail recursion here... return rest(input); } private: FirstSubParser first; OneOf_ rest; }; template <> class OneOf_<> { public: template decltype(nullptr) operator()(Input& input) const { return nullptr; } }; template constexpr OneOf_ oneOf(SubParsers&&... parsers) { // Constructs a parser that accepts one of a set of options. The parser behaves as the first // sub-parser in the list which returns successfully. All of the sub-parsers must return the // same type. return OneOf_(kj::fwd(parsers)...); } // ------------------------------------------------------------------- // transform() // Output = Result of applying transform functor to input value. If input is a tuple, it is // unpacked to form the transformation parameters. template struct Span { public: inline const Position& begin() const { return begin_; } inline const Position& end() const { return end_; } Span() = default; inline constexpr Span(Position&& begin, Position&& end): begin_(mv(begin)), end_(mv(end)) {} private: Position begin_; Position end_; }; template constexpr Span> span(Position&& start, Position&& end) { return Span>(kj::fwd(start), kj::fwd(end)); } template class Transform_ { public: explicit constexpr Transform_(SubParser&& subParser, TransformFunc&& transform) : subParser(kj::fwd(subParser)), transform(kj::fwd(transform)) {} template Maybe(), instance&&>()))> operator()(Input& input) const { KJ_IF_MAYBE(subResult, subParser(input)) { return kj::apply(transform, kj::mv(*subResult)); } else { return nullptr; } } private: SubParser subParser; TransformFunc transform; }; template class TransformOrReject_ { public: explicit constexpr TransformOrReject_(SubParser&& subParser, TransformFunc&& transform) : subParser(kj::fwd(subParser)), transform(kj::fwd(transform)) {} template decltype(kj::apply(instance(), instance&&>())) operator()(Input& input) const { KJ_IF_MAYBE(subResult, subParser(input)) { return kj::apply(transform, kj::mv(*subResult)); } else { return nullptr; } } private: SubParser subParser; TransformFunc transform; }; template class TransformWithLocation_ { public: explicit constexpr TransformWithLocation_(SubParser&& subParser, TransformFunc&& transform) : subParser(kj::fwd(subParser)), transform(kj::fwd(transform)) {} template Maybe(), instance().getPosition())>>>(), instance&&>()))> operator()(Input& input) const { auto start = input.getPosition(); KJ_IF_MAYBE(subResult, subParser(input)) { return kj::apply(transform, Span(kj::mv(start), input.getPosition()), kj::mv(*subResult)); } else { return nullptr; } } private: SubParser subParser; TransformFunc transform; }; template constexpr Transform_ transform( SubParser&& subParser, TransformFunc&& functor) { // Constructs a parser which executes some other parser and then transforms the result by invoking // `functor` on it. Typically `functor` is a lambda. It is invoked using `kj::apply`, // meaning tuples will be unpacked as arguments. return Transform_( kj::fwd(subParser), kj::fwd(functor)); } template constexpr TransformOrReject_ transformOrReject( SubParser&& subParser, TransformFunc&& functor) { // Like `transform()` except that `functor` returns a `Maybe`. If it returns null, parsing fails, // otherwise the parser's result is the content of the `Maybe`. return TransformOrReject_( kj::fwd(subParser), kj::fwd(functor)); } template constexpr TransformWithLocation_ transformWithLocation( SubParser&& subParser, TransformFunc&& functor) { // Like `transform` except that `functor` also takes a `Span` as its first parameter specifying // the location of the parsed content. The span's position type is whatever the parser input's // getPosition() returns. return TransformWithLocation_( kj::fwd(subParser), kj::fwd(functor)); } // ------------------------------------------------------------------- // notLookingAt() // Fails if the given parser succeeds at the current location. template class NotLookingAt_ { public: explicit constexpr NotLookingAt_(SubParser&& subParser) : subParser(kj::fwd(subParser)) {} template Maybe> operator()(Input& input) const { Input subInput(input); subInput.forgetParent(); if (subParser(subInput) == nullptr) { return Tuple<>(); } else { return nullptr; } } private: SubParser subParser; }; template constexpr NotLookingAt_ notLookingAt(SubParser&& subParser) { // Constructs a parser which fails at any position where the given parser succeeds. Otherwise, // it succeeds without consuming any input and returns an empty tuple. return NotLookingAt_(kj::fwd(subParser)); } // ------------------------------------------------------------------- // endOfInput() // Output = Tuple<>, only succeeds if at end-of-input class EndOfInput_ { public: template Maybe> operator()(Input& input) const { if (input.atEnd()) { return Tuple<>(); } else { return nullptr; } } }; constexpr EndOfInput_ endOfInput = EndOfInput_(); // A parser that succeeds only if it is called with no input. } // namespace parse } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/parse/char.h0000644000175000017500000002767513650101756020370 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains parsers useful for character stream inputs, including parsers to parse // common kinds of tokens like identifiers, numbers, and quoted strings. #pragma once #include "common.h" #include "../string.h" #include KJ_BEGIN_HEADER namespace kj { namespace parse { // ======================================================================================= // Exact char/string. class ExactString_ { public: constexpr inline ExactString_(const char* str): str(str) {} template Maybe> operator()(Input& input) const { const char* ptr = str; while (*ptr != '\0') { if (input.atEnd() || input.current() != *ptr) return nullptr; input.next(); ++ptr; } return Tuple<>(); } private: const char* str; }; constexpr inline ExactString_ exactString(const char* str) { return ExactString_(str); } template constexpr ExactlyConst_ exactChar() { // Returns a parser that matches exactly the character given by the template argument (returning // no result). return ExactlyConst_(); } // ======================================================================================= // Char ranges / sets class CharGroup_ { public: constexpr inline CharGroup_(): bits{0, 0, 0, 0} {} constexpr inline CharGroup_ orRange(unsigned char first, unsigned char last) const { return CharGroup_(bits[0] | (oneBits(last + 1) & ~oneBits(first )), bits[1] | (oneBits(last - 63) & ~oneBits(first - 64)), bits[2] | (oneBits(last - 127) & ~oneBits(first - 128)), bits[3] | (oneBits(last - 191) & ~oneBits(first - 192))); } constexpr inline CharGroup_ orAny(const char* chars) const { return *chars == 0 ? *this : orChar(*chars).orAny(chars + 1); } constexpr inline CharGroup_ orChar(unsigned char c) const { return CharGroup_(bits[0] | bit(c), bits[1] | bit(c - 64), bits[2] | bit(c - 128), bits[3] | bit(c - 256)); } constexpr inline CharGroup_ orGroup(CharGroup_ other) const { return CharGroup_(bits[0] | other.bits[0], bits[1] | other.bits[1], bits[2] | other.bits[2], bits[3] | other.bits[3]); } constexpr inline CharGroup_ invert() const { return CharGroup_(~bits[0], ~bits[1], ~bits[2], ~bits[3]); } constexpr inline bool contains(unsigned char c) const { return (bits[c / 64] & (1ll << (c % 64))) != 0; } inline bool containsAll(ArrayPtr text) const { for (char c: text) { if (!contains(c)) return false; } return true; } template Maybe operator()(Input& input) const { if (input.atEnd()) return nullptr; unsigned char c = input.current(); if (contains(c)) { input.next(); return c; } else { return nullptr; } } private: typedef unsigned long long Bits64; constexpr inline CharGroup_(Bits64 a, Bits64 b, Bits64 c, Bits64 d): bits{a, b, c, d} {} Bits64 bits[4]; static constexpr inline Bits64 oneBits(int count) { return count <= 0 ? 0ll : count >= 64 ? -1ll : ((1ll << count) - 1); } static constexpr inline Bits64 bit(int index) { return index < 0 ? 0 : index >= 64 ? 0 : (1ll << index); } }; constexpr inline CharGroup_ charRange(char first, char last) { // Create a parser which accepts any character in the range from `first` to `last`, inclusive. // For example: `charRange('a', 'z')` matches all lower-case letters. The parser's result is the // character matched. // // The returned object has methods which can be used to match more characters. The following // produces a parser which accepts any letter as well as '_', '+', '-', and '.'. // // charRange('a', 'z').orRange('A', 'Z').orChar('_').orAny("+-.") // // You can also use `.invert()` to match the opposite set of characters. return CharGroup_().orRange(first, last); } #if _MSC_VER #define anyOfChars(chars) CharGroup_().orAny(chars) // TODO(msvc): MSVC ICEs on the proper definition of `anyOfChars()`, which in turn prevents us from // building the compiler or schema parser. We don't know why this happens, but Harris found that // this horrible, horrible hack makes things work. This is awful, but it's better than nothing. // Hopefully, MSVC will get fixed soon and we'll be able to remove this. #else constexpr inline CharGroup_ anyOfChars(const char* chars) { // Returns a parser that accepts any of the characters in the given string (which should usually // be a literal). The returned parser is of the same type as returned by `charRange()` -- see // that function for more info. return CharGroup_().orAny(chars); } #endif // ======================================================================================= namespace _ { // private struct ArrayToString { inline String operator()(const Array& arr) const { return heapString(arr); } }; } // namespace _ (private) template constexpr inline auto charsToString(SubParser&& subParser) -> decltype(transform(kj::fwd(subParser), _::ArrayToString())) { // Wraps a parser that returns Array such that it returns String instead. return parse::transform(kj::fwd(subParser), _::ArrayToString()); } // ======================================================================================= // Basic character classes. constexpr auto alpha = charRange('a', 'z').orRange('A', 'Z'); constexpr auto digit = charRange('0', '9'); constexpr auto alphaNumeric = alpha.orGroup(digit); constexpr auto nameStart = alpha.orChar('_'); constexpr auto nameChar = alphaNumeric.orChar('_'); constexpr auto hexDigit = charRange('0', '9').orRange('a', 'f').orRange('A', 'F'); constexpr auto octDigit = charRange('0', '7'); constexpr auto whitespaceChar = anyOfChars(" \f\n\r\t\v"); constexpr auto controlChar = charRange(0, 0x1f).invert().orGroup(whitespaceChar).invert(); constexpr auto whitespace = many(anyOfChars(" \f\n\r\t\v")); constexpr auto discardWhitespace = discard(many(discard(anyOfChars(" \f\n\r\t\v")))); // Like discard(whitespace) but avoids some memory allocation. // ======================================================================================= // Identifiers namespace _ { // private struct IdentifierToString { inline String operator()(char first, const Array& rest) const { if (rest.size() == 0) return heapString(&first, 1); String result = heapString(rest.size() + 1); result[0] = first; memcpy(result.begin() + 1, rest.begin(), rest.size()); return result; } }; } // namespace _ (private) constexpr auto identifier = transform(sequence(nameStart, many(nameChar)), _::IdentifierToString()); // Parses an identifier (e.g. a C variable name). // ======================================================================================= // Integers namespace _ { // private inline char parseDigit(char c) { if (c < 'A') return c - '0'; if (c < 'a') return c - 'A' + 10; return c - 'a' + 10; } template struct ParseInteger { inline uint64_t operator()(const Array& digits) const { return operator()('0', digits); } uint64_t operator()(char first, const Array& digits) const { uint64_t result = parseDigit(first); for (char digit: digits) { result = result * base + parseDigit(digit); } return result; } }; } // namespace _ (private) constexpr auto integer = sequence( oneOf( transform(sequence(exactChar<'0'>(), exactChar<'x'>(), oneOrMore(hexDigit)), _::ParseInteger<16>()), transform(sequence(exactChar<'0'>(), many(octDigit)), _::ParseInteger<8>()), transform(sequence(charRange('1', '9'), many(digit)), _::ParseInteger<10>())), notLookingAt(alpha.orAny("_."))); // ======================================================================================= // Numbers (i.e. floats) namespace _ { // private struct ParseFloat { double operator()(const Array& digits, const Maybe>& fraction, const Maybe, Array>>& exponent) const; }; } // namespace _ (private) constexpr auto number = transform( sequence( oneOrMore(digit), optional(sequence(exactChar<'.'>(), many(digit))), optional(sequence(discard(anyOfChars("eE")), optional(anyOfChars("+-")), many(digit))), notLookingAt(alpha.orAny("_."))), _::ParseFloat()); // ======================================================================================= // Quoted strings namespace _ { // private struct InterpretEscape { char operator()(char c) const { switch (c) { case 'a': return '\a'; case 'b': return '\b'; case 'f': return '\f'; case 'n': return '\n'; case 'r': return '\r'; case 't': return '\t'; case 'v': return '\v'; default: return c; } } }; struct ParseHexEscape { inline char operator()(char first, char second) const { return (parseDigit(first) << 4) | parseDigit(second); } }; struct ParseHexByte { inline byte operator()(char first, char second) const { return (parseDigit(first) << 4) | parseDigit(second); } }; struct ParseOctEscape { inline char operator()(char first, Maybe second, Maybe third) const { char result = first - '0'; KJ_IF_MAYBE(digit1, second) { result = (result << 3) | (*digit1 - '0'); KJ_IF_MAYBE(digit2, third) { result = (result << 3) | (*digit2 - '0'); } } return result; } }; } // namespace _ (private) constexpr auto escapeSequence = sequence(exactChar<'\\'>(), oneOf( transform(anyOfChars("abfnrtv'\"\\\?"), _::InterpretEscape()), transform(sequence(exactChar<'x'>(), hexDigit, hexDigit), _::ParseHexEscape()), transform(sequence(octDigit, optional(octDigit), optional(octDigit)), _::ParseOctEscape()))); // A parser that parses a C-string-style escape sequence (starting with a backslash). Returns // a char. constexpr auto doubleQuotedString = charsToString(sequence( exactChar<'\"'>(), many(oneOf(anyOfChars("\\\n\"").invert(), escapeSequence)), exactChar<'\"'>())); // Parses a C-style double-quoted string. constexpr auto singleQuotedString = charsToString(sequence( exactChar<'\''>(), many(oneOf(anyOfChars("\\\n\'").invert(), escapeSequence)), exactChar<'\''>())); // Parses a C-style single-quoted string. constexpr auto doubleQuotedHexBinary = sequence( exactChar<'0'>(), exactChar<'x'>(), exactChar<'\"'>(), oneOrMore(transform(sequence(discardWhitespace, hexDigit, hexDigit), _::ParseHexByte())), discardWhitespace, exactChar<'\"'>()); // Parses a double-quoted hex binary literal. Returns Array. } // namespace parse } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/parse/char.c++0000644000175000017500000000445613340402540020470 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "char.h" #include "../debug.h" #include namespace kj { namespace parse { namespace _ { // private double ParseFloat::operator()(const Array& digits, const Maybe>& fraction, const Maybe, Array>>& exponent) const { size_t bufSize = digits.size(); KJ_IF_MAYBE(f, fraction) { bufSize += 1 + f->size(); } KJ_IF_MAYBE(e, exponent) { bufSize += 1 + (get<0>(*e) != nullptr) + get<1>(*e).size(); } KJ_STACK_ARRAY(char, buf, bufSize + 1, 128, 128); char* pos = buf.begin(); memcpy(pos, digits.begin(), digits.size()); pos += digits.size(); KJ_IF_MAYBE(f, fraction) { *pos++ = '.'; memcpy(pos, f->begin(), f->size()); pos += f->size(); } KJ_IF_MAYBE(e, exponent) { *pos++ = 'e'; KJ_IF_MAYBE(sign, get<0>(*e)) { *pos++ = *sign; } memcpy(pos, get<1>(*e).begin(), get<1>(*e).size()); pos += get<1>(*e).size(); } *pos++ = '\0'; KJ_DASSERT(pos == buf.end()); return strtod(buf.begin(), nullptr); } } // namespace _ (private) } // namespace parse } // namespace kj capnproto-c++-0.8.0/src/kj/parse/common-test.c++0000644000175000017500000003272413340402540022017 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "common.h" #include "../string.h" #include namespace kj { namespace parse { namespace { typedef IteratorInput Input; TEST(CommonParsers, AnyParser) { StringPtr text = "foo"; Input input(text.begin(), text.end()); constexpr auto parser = any; Maybe result = parser(input); KJ_IF_MAYBE(c, result) { EXPECT_EQ('f', *c); } else { ADD_FAILURE() << "Expected 'c', got null."; } EXPECT_FALSE(input.atEnd()); result = parser(input); KJ_IF_MAYBE(c, result) { EXPECT_EQ('o', *c); } else { ADD_FAILURE() << "Expected 'o', got null."; } EXPECT_FALSE(input.atEnd()); result = parser(input); KJ_IF_MAYBE(c, result) { EXPECT_EQ('o', *c); } else { ADD_FAILURE() << "Expected 'o', got null."; } EXPECT_TRUE(input.atEnd()); result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_TRUE(input.atEnd()); } TEST(CommonParsers, ExactElementParser) { StringPtr text = "foo"; Input input(text.begin(), text.end()); Maybe> result = exactly('f')(input); EXPECT_TRUE(result != nullptr); EXPECT_FALSE(input.atEnd()); result = exactly('o')(input); EXPECT_TRUE(result != nullptr); EXPECT_FALSE(input.atEnd()); result = exactly('x')(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); auto parser = exactly('o'); ParserRef> wrapped = ref(parser); result = wrapped(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } TEST(CommonParsers, ExactlyConstParser) { StringPtr text = "foo"; Input input(text.begin(), text.end()); Maybe> result = exactlyConst()(input); EXPECT_TRUE(result != nullptr); EXPECT_FALSE(input.atEnd()); result = exactlyConst()(input); EXPECT_TRUE(result != nullptr); EXPECT_FALSE(input.atEnd()); result = exactlyConst()(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); auto parser = exactlyConst(); ParserRef> wrapped = ref(parser); result = wrapped(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } TEST(CommonParsers, ConstResultParser) { auto parser = constResult(exactly('o'), 123); StringPtr text = "o"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(123, *i); } else { ADD_FAILURE() << "Expected 123, got null."; } EXPECT_TRUE(input.atEnd()); } TEST(CommonParsers, DiscardParser) { auto parser = discard(any); StringPtr text = "o"; Input input(text.begin(), text.end()); Maybe> result = parser(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } TEST(CommonParsers, SequenceParser) { StringPtr text = "foo"; { Input input(text.begin(), text.end()); Maybe> result = sequence(exactly('f'), exactly('o'), exactly('o'))(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe> result = sequence(exactly('f'), exactly('o'))(input); EXPECT_TRUE(result != nullptr); EXPECT_FALSE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe> result = sequence(exactly('x'), exactly('o'), exactly('o'))(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe> result = sequence(sequence(exactly('f'), exactly('o')), exactly('o'))(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe> result = sequence(sequence(exactly('f')), exactly('o'), exactly('o'))(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe result = sequence(transform(exactly('f'), [](){return 123;}), exactly('o'), exactly('o'))(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(123, *i); } else { ADD_FAILURE() << "Expected 123, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CommonParsers, ManyParserCountOnly) { StringPtr text = "foooob"; auto parser = sequence(exactly('f'), many(exactly('o'))); { Input input(text.begin(), text.begin() + 3); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(2, *i); } else { ADD_FAILURE() << "Expected 2, got null."; } EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.begin() + 5); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(4, *i); } else { ADD_FAILURE() << "Expected 4, got null."; } EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(4, *i); } else { ADD_FAILURE() << "Expected 4, got null."; } EXPECT_FALSE(input.atEnd()); } } TEST(CommonParsers, TimesParser) { StringPtr text = "foobar"; auto parser = sequence(exactly('f'), times(any, 4)); { Input input(text.begin(), text.begin() + 4); Maybe> result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.begin() + 5); Maybe> result = parser(input); KJ_IF_MAYBE(s, result) { EXPECT_EQ("ooba", heapString(*s)); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe> result = parser(input); KJ_IF_MAYBE(s, result) { EXPECT_EQ("ooba", heapString(*s)); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_FALSE(input.atEnd()); } } TEST(CommonParsers, TimesParserCountOnly) { StringPtr text = "foooob"; auto parser = sequence(exactly('f'), times(exactly('o'), 4)); { Input input(text.begin(), text.begin() + 4); Maybe> result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.begin() + 5); Maybe> result = parser(input); EXPECT_TRUE(result != nullptr); EXPECT_TRUE(input.atEnd()); } { Input input(text.begin(), text.end()); Maybe> result = parser(input); EXPECT_TRUE(result != nullptr); EXPECT_FALSE(input.atEnd()); } text = "fooob"; { Input input(text.begin(), text.end()); Maybe> result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } } TEST(CommonParsers, ManyParserSubResult) { StringPtr text = "foooob"; auto parser = many(any); { Input input(text.begin(), text.end()); Maybe> result = parser(input); KJ_IF_MAYBE(chars, result) { EXPECT_EQ(text, heapString(*chars)); } else { ADD_FAILURE() << "Expected char array, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CommonParsers, OptionalParser) { auto parser = sequence( transform(exactly('b'), []() -> uint { return 123; }), optional(transform(exactly('a'), []() -> uint { return 456; })), transform(exactly('r'), []() -> uint { return 789; })); { StringPtr text = "bar"; Input input(text.begin(), text.end()); Maybe, uint>> result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(123u, get<0>(*value)); KJ_IF_MAYBE(value2, get<1>(*value)) { EXPECT_EQ(456u, *value2); } else { ADD_FAILURE() << "Expected 456, got null."; } EXPECT_EQ(789u, get<2>(*value)); } else { ADD_FAILURE() << "Expected result tuple, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "br"; Input input(text.begin(), text.end()); Maybe, uint>> result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(123u, get<0>(*value)); EXPECT_TRUE(get<1>(*value) == nullptr); EXPECT_EQ(789u, get<2>(*value)); } else { ADD_FAILURE() << "Expected result tuple, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "bzr"; Input input(text.begin(), text.end()); Maybe, uint>> result = parser(input); EXPECT_TRUE(result == nullptr); } } TEST(CommonParsers, OneOfParser) { auto parser = oneOf( transform(sequence(exactly('f'), exactly('o'), exactly('o')), []() -> StringPtr { return "foo"; }), transform(sequence(exactly('b'), exactly('a'), exactly('r')), []() -> StringPtr { return "bar"; })); { StringPtr text = "foo"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(s, result) { EXPECT_EQ("foo", *s); } else { ADD_FAILURE() << "Expected 'foo', got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "bar"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(s, result) { EXPECT_EQ("bar", *s); } else { ADD_FAILURE() << "Expected 'bar', got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CommonParsers, TransformParser) { StringPtr text = "foo"; auto parser = transformWithLocation( sequence(exactly('f'), exactly('o'), exactly('o')), [](Span location) -> int { EXPECT_EQ("foo", StringPtr(location.begin(), location.end())); return 123; }); { Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(123, *i); } else { ADD_FAILURE() << "Expected 123, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CommonParsers, TransformOrRejectParser) { auto parser = transformOrReject(many(any), [](Array chars) -> Maybe { if (heapString(chars) == "foo") { return 123; } else { return nullptr; } }); { StringPtr text = "foo"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(123, *i); } else { ADD_FAILURE() << "Expected 123, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "bar"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_TRUE(input.atEnd()); } } TEST(CommonParsers, References) { struct TransformFunc { int value; TransformFunc(int value): value(value) {} int operator()() const { return value; } }; // Don't use auto for the parsers here in order to verify that the templates are properly choosing // whether to use references or copies. Transform_, TransformFunc> parser1 = transform(exactly('f'), TransformFunc(12)); auto otherParser = exactly('o'); Transform_&, TransformFunc> parser2 = transform(otherParser, TransformFunc(34)); auto otherParser2 = exactly('b'); Transform_, TransformFunc> parser3 = transform(mv(otherParser2), TransformFunc(56)); StringPtr text = "foob"; auto parser = transform( sequence(parser1, parser2, exactly('o'), parser3), [](int i, int j, int k) { return i + j + k; }); { Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(i, result) { EXPECT_EQ(12 + 34 + 56, *i); } else { ADD_FAILURE() << "Expected 12 + 34 + 56, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CommonParsers, NotLookingAt) { auto parser = notLookingAt(exactly('a')); { StringPtr text = "a"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) == nullptr); EXPECT_FALSE(input.atEnd()); } { StringPtr text = "b"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) != nullptr); EXPECT_FALSE(input.atEnd()); } } TEST(CommonParsers, EndOfInput) { auto parser = endOfInput; { StringPtr text = "a"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) == nullptr); EXPECT_TRUE(parser(input) == nullptr); input.next(); EXPECT_FALSE(parser(input) == nullptr); } } } // namespace } // namespace parse } // namespace kj capnproto-c++-0.8.0/src/kj/parse/char-test.c++0000644000175000017500000002516213340402540021442 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "char.h" #include "../string.h" #include namespace kj { namespace parse { namespace { typedef IteratorInput Input; typedef Span TestLocation; TEST(CharParsers, ExactChar) { constexpr auto parser = exactChar<'a'>(); { StringPtr text = "a"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) != nullptr); EXPECT_TRUE(input.atEnd()); } { StringPtr text = "b"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) == nullptr); EXPECT_FALSE(input.atEnd()); } } TEST(CharParsers, ExactString) { constexpr auto parser = exactString("foo"); { StringPtr text = "foobar"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) != nullptr); ASSERT_FALSE(input.atEnd()); EXPECT_EQ('b', input.current()); } { StringPtr text = "bar"; Input input(text.begin(), text.end()); EXPECT_TRUE(parser(input) == nullptr); EXPECT_FALSE(input.atEnd()); EXPECT_EQ('b', input.current()); } } TEST(CharParsers, CharRange) { constexpr auto parser = charRange('a', 'z'); { StringPtr text = "a"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ('a', *value); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "n"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ('n', *value); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "z"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ('z', *value); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "`"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } { StringPtr text = "{"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } { StringPtr text = "A"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } } TEST(CharParsers, AnyOfChars) { constexpr auto parser = anyOfChars("axn2B"); { StringPtr text = "a"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ('a', *value); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "n"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ('n', *value); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "B"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ('B', *value); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "b"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } { StringPtr text = "j"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } { StringPtr text = "A"; Input input(text.begin(), text.end()); Maybe result = parser(input); EXPECT_TRUE(result == nullptr); EXPECT_FALSE(input.atEnd()); } } TEST(CharParsers, CharGroupCombo) { constexpr auto parser = many(charRange('0', '9').orRange('a', 'z').orRange('A', 'Z').orAny("-_")); { StringPtr text = "foo1-bar2_baz3@qux"; Input input(text.begin(), text.end()); Maybe> result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("foo1-bar2_baz3", str(*value)); } else { ADD_FAILURE() << "Expected parse result, got null."; } EXPECT_FALSE(input.atEnd()); } } TEST(CharParsers, Identifier) { constexpr auto parser = identifier; { StringPtr text = "helloWorld123 "; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("helloWorld123", *value); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_FALSE(input.atEnd()); } } TEST(CharParsers, Integer) { constexpr auto parser = integer; { StringPtr text = "12349"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(12349u, *value); } else { ADD_FAILURE() << "Expected integer, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "0x1aF0"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(0x1aF0u, *value); } else { ADD_FAILURE() << "Expected integer, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "064270"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(064270u, *value); } else { ADD_FAILURE() << "Expected integer, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CharParsers, Number) { constexpr auto parser = number; { StringPtr text = "12345"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(12345, *value); } else { ADD_FAILURE() << "Expected number, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "123.25"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(123.25, *value); } else { ADD_FAILURE() << "Expected number, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "123e10"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(123e10, *value); } else { ADD_FAILURE() << "Expected number, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "123.25E+10"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(123.25E+10, *value); } else { ADD_FAILURE() << "Expected number, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "25e-2"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ(25e-2, *value); } else { ADD_FAILURE() << "Expected number, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CharParsers, DoubleQuotedString) { constexpr auto parser = doubleQuotedString; { StringPtr text = "\"hello\""; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("hello", *value); } else { ADD_FAILURE() << "Expected \"hello\", got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "\"test\\a\\b\\f\\n\\r\\t\\v\\\'\\\"\\\?\\x01\\x20\\2\\34\\156\""; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("test\a\b\f\n\r\t\v\'\"\?\x01\x20\2\34\156", *value); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "\"foo'bar\""; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("foo'bar", *value); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_TRUE(input.atEnd()); } } TEST(CharParsers, SingleQuotedString) { constexpr auto parser = singleQuotedString; { StringPtr text = "\'hello\'"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("hello", *value); } else { ADD_FAILURE() << "Expected \"hello\", got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "\'test\\a\\b\\f\\n\\r\\t\\v\\\'\\\"\\\?\x01\2\34\156\'"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("test\a\b\f\n\r\t\v\'\"\?\x01\2\34\156", *value); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_TRUE(input.atEnd()); } { StringPtr text = "\'foo\"bar\'"; Input input(text.begin(), text.end()); Maybe result = parser(input); KJ_IF_MAYBE(value, result) { EXPECT_EQ("foo\"bar", *value); } else { ADD_FAILURE() << "Expected string, got null."; } EXPECT_TRUE(input.atEnd()); } } } // namespace } // namespace parse } // namespace kj capnproto-c++-0.8.0/src/kj/std/0000755000175000017500000000000013650320027016733 5ustar00kentonkenton00000000000000capnproto-c++-0.8.0/src/kj/std/iostream.h0000644000175000017500000000500613650101756020736 0ustar00kentonkenton00000000000000// Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. /* * Compatibility layer for stdlib iostream */ #pragma once #include "../io.h" #include KJ_BEGIN_HEADER namespace kj { namespace std { class StdOutputStream: public kj::OutputStream { public: explicit StdOutputStream(::std::ostream& stream) : stream_(stream) {} ~StdOutputStream() noexcept(false) {} virtual void write(const void* src, size_t size) override { // Always writes the full size. stream_.write((char*)src, size); } virtual void write(ArrayPtr> pieces) override { // Equivalent to write()ing each byte array in sequence, which is what the // default implementation does. Override if you can do something better, // e.g. use writev() to do the write in a single syscall. for (auto piece : pieces) { write(piece.begin(), piece.size()); } } private: ::std::ostream& stream_; }; class StdInputStream: public kj::InputStream { public: explicit StdInputStream(::std::istream& stream) : stream_(stream) {} ~StdInputStream() noexcept(false) {} virtual size_t tryRead( void* buffer, size_t minBytes, size_t maxBytes) override { // Like read(), but may return fewer than minBytes on EOF. stream_.read((char*)buffer, maxBytes); return stream_.gcount(); } private: ::std::istream& stream_; }; } // namespace std } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/std/iostream-test.c++0000644000175000017500000000545613340402540022034 0ustar00kentonkenton00000000000000// Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "iostream.h" #include #include namespace kj { namespace std { namespace { TEST(StdIoStream, WriteVec) { // Check that writing an array of arrays works even when some of the arrays // are empty. (This used to not work in some cases.) ::std::stringstream ss; StdInputStream in(ss); StdOutputStream out(ss); ArrayPtr pieces[5] = { arrayPtr(implicitCast(nullptr), 0), arrayPtr(reinterpret_cast("foo"), 3), arrayPtr(implicitCast(nullptr), 0), arrayPtr(reinterpret_cast("bar"), 3), arrayPtr(implicitCast(nullptr), 0) }; out.write(pieces); char buf[7]; in.read(buf, 6); buf[6] = '\0'; EXPECT_STREQ("foobar", buf); } TEST(StdIoStream, TryReadToEndOfFile) { // Check that tryRead works when eof is reached before minBytes. ::std::stringstream ss; StdInputStream in(ss); StdOutputStream out(ss); const void* bytes = "foobar"; out.write(bytes, 6); char buf[9]; in.tryRead(buf, 8, 8); buf[6] = '\0'; EXPECT_STREQ("foobar", buf); } TEST(StdIoStream, ReadToEndOfFile) { // Check that read throws an exception when eof is reached before specified // bytes. ::std::stringstream ss; StdInputStream in(ss); StdOutputStream out(ss); const void* bytes = "foobar"; out.write(bytes, 6); char buf[9]; Maybe e = kj::runCatchingExceptions([&]() { in.read(buf, 8, 8); }); buf[6] = '\0'; ASSERT_FALSE(e == nullptr); // Ensure that the value is still read up to the EOF. EXPECT_STREQ("foobar", buf); } } // namespace } // namespace std } // namespace kj capnproto-c++-0.8.0/src/kj/common.h0000644000175000017500000016374413650317313017625 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // Header that should be #included by everyone. // // This defines very simple utilities that are widely applicable. #pragma once #ifdef __GNUC__ #define KJ_BEGIN_SYSTEM_HEADER _Pragma("GCC system_header") #elif defined(_MSC_VER) #define KJ_BEGIN_SYSTEM_HEADER __pragma(warning(push, 0)) #define KJ_END_SYSTEM_HEADER __pragma(warning(pop)) #endif #ifndef KJ_BEGIN_SYSTEM_HEADER #define KJ_BEGIN_SYSTEM_HEADER #endif #ifndef KJ_END_SYSTEM_HEADER #define KJ_END_SYSTEM_HEADER #endif #if !defined(KJ_HEADER_WARNINGS) || !KJ_HEADER_WARNINGS #define KJ_BEGIN_HEADER KJ_BEGIN_SYSTEM_HEADER #define KJ_END_HEADER KJ_END_SYSTEM_HEADER #else #define KJ_BEGIN_HEADER #define KJ_END_HEADER #endif KJ_BEGIN_HEADER #ifndef KJ_NO_COMPILER_CHECK // Technically, __cplusplus should be 201402L for C++14, but GCC 4.9 -- which is supported -- still // had it defined to 201300L even with -std=c++14. #if __cplusplus < 201300L && !__CDT_PARSER__ && !_MSC_VER #error "This code requires C++14. Either your compiler does not support it or it is not enabled." #ifdef __GNUC__ // Compiler claims compatibility with GCC, so presumably supports -std. #error "Pass -std=c++14 on the compiler command line to enable C++14." #endif #endif #ifdef __GNUC__ #if __clang__ #if __clang_major__ < 5 #warning "This library requires at least Clang 5.0." #elif __cplusplus >= 201402L && !__has_include() #warning "Your compiler supports C++14 but your C++ standard library does not. If your "\ "system has libc++ installed (as should be the case on e.g. Mac OSX), try adding "\ "-stdlib=libc++ to your CXXFLAGS." #endif #else #if __GNUC__ < 5 #warning "This library requires at least GCC 5.0." #endif #endif #elif defined(_MSC_VER) #if _MSC_VER < 1910 #error "You need Visual Studio 2017 or better to compile this code." #endif #else #warning "I don't recognize your compiler. As of this writing, Clang, GCC, and Visual Studio "\ "are the only known compilers with enough C++14 support for this library. "\ "#define KJ_NO_COMPILER_CHECK to make this warning go away." #endif #endif #include #include #if __linux__ && __cplusplus > 201200L // Hack around stdlib bug with C++14 that exists on some Linux systems. // Apparently in this mode the C library decides not to define gets() but the C++ library still // tries to import it into the std namespace. This bug has been fixed at the source but is still // widely present in the wild e.g. on Ubuntu 14.04. #undef _GLIBCXX_HAVE_GETS #endif #if defined(_MSC_VER) #ifndef NOMINMAX #define NOMINMAX 1 #endif #include // __popcnt #endif // ======================================================================================= namespace kj { typedef unsigned int uint; typedef unsigned char byte; // ======================================================================================= // Common macros, especially for common yet compiler-specific features. // Detect whether RTTI and exceptions are enabled, assuming they are unless we have specific // evidence to the contrary. Clients can always define KJ_NO_RTTI or KJ_NO_EXCEPTIONS explicitly // to override these checks. #ifdef __GNUC__ #if !defined(KJ_NO_RTTI) && !__GXX_RTTI #define KJ_NO_RTTI 1 #endif #if !defined(KJ_NO_EXCEPTIONS) && !__EXCEPTIONS #define KJ_NO_EXCEPTIONS 1 #endif #elif defined(_MSC_VER) #if !defined(KJ_NO_RTTI) && !defined(_CPPRTTI) #define KJ_NO_RTTI 1 #endif #if !defined(KJ_NO_EXCEPTIONS) && !defined(_CPPUNWIND) #define KJ_NO_EXCEPTIONS 1 #endif #endif #if !defined(KJ_DEBUG) && !defined(KJ_NDEBUG) // Heuristically decide whether to enable debug mode. If DEBUG or NDEBUG is defined, use that. // Otherwise, fall back to checking whether optimization is enabled. #if defined(DEBUG) || defined(_DEBUG) #define KJ_DEBUG #elif defined(NDEBUG) #define KJ_NDEBUG #elif __OPTIMIZE__ #define KJ_NDEBUG #else #define KJ_DEBUG #endif #endif #define KJ_DISALLOW_COPY(classname) \ classname(const classname&) = delete; \ classname& operator=(const classname&) = delete // Deletes the implicit copy constructor and assignment operator. #ifdef __GNUC__ #define KJ_LIKELY(condition) __builtin_expect(condition, true) #define KJ_UNLIKELY(condition) __builtin_expect(condition, false) // Branch prediction macros. Evaluates to the condition given, but also tells the compiler that we // expect the condition to be true/false enough of the time that it's worth hard-coding branch // prediction. #else #define KJ_LIKELY(condition) (condition) #define KJ_UNLIKELY(condition) (condition) #endif #if defined(KJ_DEBUG) || __NO_INLINE__ #define KJ_ALWAYS_INLINE(...) inline __VA_ARGS__ // Don't force inline in debug mode. #else #if defined(_MSC_VER) #define KJ_ALWAYS_INLINE(...) __forceinline __VA_ARGS__ #else #define KJ_ALWAYS_INLINE(...) inline __VA_ARGS__ __attribute__((always_inline)) #endif // Force a function to always be inlined. Apply only to the prototype, not to the definition. #endif #if defined(_MSC_VER) #define KJ_NOINLINE __declspec(noinline) #else #define KJ_NOINLINE __attribute__((noinline)) #endif #if defined(_MSC_VER) && !__clang__ #define KJ_NORETURN(prototype) __declspec(noreturn) prototype #define KJ_UNUSED #define KJ_WARN_UNUSED_RESULT // TODO(msvc): KJ_WARN_UNUSED_RESULT can use _Check_return_ on MSVC, but it's a prefix, so // wrapping the whole prototype is needed. http://msdn.microsoft.com/en-us/library/jj159529.aspx // Similarly, KJ_UNUSED could use __pragma(warning(suppress:...)), but again that's a prefix. #else #define KJ_NORETURN(prototype) prototype __attribute__((noreturn)) #define KJ_UNUSED __attribute__((unused)) #define KJ_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) #endif #if __clang__ #define KJ_UNUSED_MEMBER __attribute__((unused)) // Inhibits "unused" warning for member variables. Only Clang produces such a warning, while GCC // complains if the attribute is set on members. #else #define KJ_UNUSED_MEMBER #endif #if __clang__ #define KJ_DEPRECATED(reason) \ __attribute__((deprecated(reason))) #define KJ_UNAVAILABLE(reason) \ __attribute__((unavailable(reason))) #elif __GNUC__ #define KJ_DEPRECATED(reason) \ __attribute__((deprecated)) #define KJ_UNAVAILABLE(reason) #else #define KJ_DEPRECATED(reason) #define KJ_UNAVAILABLE(reason) // TODO(msvc): Again, here, MSVC prefers a prefix, __declspec(deprecated). #endif #if KJ_TESTING_KJ // defined in KJ's own unit tests; others should not define this #undef KJ_DEPRECATED #define KJ_DEPRECATED(reason) #endif namespace _ { // private KJ_NORETURN(void inlineRequireFailure( const char* file, int line, const char* expectation, const char* macroArgs, const char* message = nullptr)); KJ_NORETURN(void unreachable()); } // namespace _ (private) #ifdef KJ_DEBUG #if _MSC_VER #define KJ_IREQUIRE(condition, ...) \ if (KJ_LIKELY(condition)); else ::kj::_::inlineRequireFailure( \ __FILE__, __LINE__, #condition, "" #__VA_ARGS__, __VA_ARGS__) // Version of KJ_DREQUIRE() which is safe to use in headers that are #included by users. Used to // check preconditions inside inline methods. KJ_IREQUIRE is particularly useful in that // it will be enabled depending on whether the application is compiled in debug mode rather than // whether libkj is. #else #define KJ_IREQUIRE(condition, ...) \ if (KJ_LIKELY(condition)); else ::kj::_::inlineRequireFailure( \ __FILE__, __LINE__, #condition, #__VA_ARGS__, ##__VA_ARGS__) // Version of KJ_DREQUIRE() which is safe to use in headers that are #included by users. Used to // check preconditions inside inline methods. KJ_IREQUIRE is particularly useful in that // it will be enabled depending on whether the application is compiled in debug mode rather than // whether libkj is. #endif #else #define KJ_IREQUIRE(condition, ...) #endif #define KJ_IASSERT KJ_IREQUIRE #define KJ_UNREACHABLE ::kj::_::unreachable(); // Put this on code paths that cannot be reached to suppress compiler warnings about missing // returns. #if __clang__ #define KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT #else #define KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT KJ_UNREACHABLE #endif // #define KJ_STACK_ARRAY(type, name, size, minStack, maxStack) // // Allocate an array, preferably on the stack, unless it is too big. On GCC this will use // variable-sized arrays. For other compilers we could just use a fixed-size array. `minStack` // is the stack array size to use if variable-width arrays are not supported. `maxStack` is the // maximum stack array size if variable-width arrays *are* supported. #if __GNUC__ && !__clang__ #define KJ_STACK_ARRAY(type, name, size, minStack, maxStack) \ size_t name##_size = (size); \ bool name##_isOnStack = name##_size <= (maxStack); \ type name##_stack[kj::max(1, name##_isOnStack ? name##_size : 0)]; \ ::kj::Array name##_heap = name##_isOnStack ? \ nullptr : kj::heapArray(name##_size); \ ::kj::ArrayPtr name = name##_isOnStack ? \ kj::arrayPtr(name##_stack, name##_size) : name##_heap #else #define KJ_STACK_ARRAY(type, name, size, minStack, maxStack) \ size_t name##_size = (size); \ bool name##_isOnStack = name##_size <= (minStack); \ type name##_stack[minStack]; \ ::kj::Array name##_heap = name##_isOnStack ? \ nullptr : kj::heapArray(name##_size); \ ::kj::ArrayPtr name = name##_isOnStack ? \ kj::arrayPtr(name##_stack, name##_size) : name##_heap #endif #define KJ_CONCAT_(x, y) x##y #define KJ_CONCAT(x, y) KJ_CONCAT_(x, y) #define KJ_UNIQUE_NAME(prefix) KJ_CONCAT(prefix, __LINE__) // Create a unique identifier name. We use concatenate __LINE__ rather than __COUNTER__ so that // the name can be used multiple times in the same macro. #if _MSC_VER #define KJ_CONSTEXPR(...) __VA_ARGS__ // Use in cases where MSVC barfs on constexpr. A replacement keyword (e.g. "const") can be // provided, or just leave blank to remove the keyword entirely. // // TODO(msvc): Remove this hack once MSVC fully supports constexpr. #ifndef __restrict__ #define __restrict__ __restrict // TODO(msvc): Would it be better to define a KJ_RESTRICT macro? #endif #pragma warning(disable: 4521 4522) // This warning complains when there are two copy constructors, one for a const reference and // one for a non-const reference. It is often quite necessary to do this in wrapper templates, // therefore this warning is dumb and we disable it. #pragma warning(disable: 4458) // Warns when a parameter name shadows a class member. Unfortunately my code does this a lot, // since I don't use a special name format for members. #else // _MSC_VER #define KJ_CONSTEXPR(...) constexpr #endif #if defined(_MSC_VER) && _MSC_VER < 1910 // TODO(msvc): Visual Studio 2015 mishandles declaring the no-arg constructor `= default` for // certain template types -- it fails to call member constructors. #define KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY {} #else #define KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY = default; #endif // ======================================================================================= // Template metaprogramming helpers. template struct NoInfer_ { typedef T Type; }; template using NoInfer = typename NoInfer_::Type; // Use NoInfer::Type in place of T for a template function parameter to prevent inference of // the type based on the parameter value. template struct RemoveConst_ { typedef T Type; }; template struct RemoveConst_ { typedef T Type; }; template using RemoveConst = typename RemoveConst_::Type; template struct IsLvalueReference_ { static constexpr bool value = false; }; template struct IsLvalueReference_ { static constexpr bool value = true; }; template inline constexpr bool isLvalueReference() { return IsLvalueReference_::value; } template struct Decay_ { typedef T Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template struct Decay_ { typedef typename Decay_::Type Type; }; template using Decay = typename Decay_::Type; template struct EnableIf_; template <> struct EnableIf_ { typedef void Type; }; template using EnableIf = typename EnableIf_::Type; // Use like: // // template ()> // void func(T&& t); template struct VoidSfinae_ { using Type = void; }; template using VoidSfinae = typename VoidSfinae_::Type; // Note: VoidSfinae is std::void_t from C++17. template T instance() noexcept; // Like std::declval, but doesn't transform T into an rvalue reference. If you want that, specify // instance(). struct DisallowConstCopy { // Inherit from this, or declare a member variable of this type, to prevent the class from being // copyable from a const reference -- instead, it will only be copyable from non-const references. // This is useful for enforcing transitive constness of contained pointers. // // For example, say you have a type T which contains a pointer. T has non-const methods which // modify the value at that pointer, but T's const methods are designed to allow reading only. // Unfortunately, if T has a regular copy constructor, someone can simply make a copy of T and // then use it to modify the pointed-to value. However, if T inherits DisallowConstCopy, then // callers will only be able to copy non-const instances of T. Ideally, there is some // parallel type ImmutableT which is like a version of T that only has const methods, and can // be copied from a const T. // // Note that due to C++ rules about implicit copy constructors and assignment operators, any // type that contains or inherits from a type that disallows const copies will also automatically // disallow const copies. Hey, cool, that's exactly what we want. #if CAPNP_DEBUG_TYPES // Alas! Declaring a defaulted non-const copy constructor tickles a bug which causes GCC and // Clang to disagree on ABI, using different calling conventions to pass this type, leading to // immediate segfaults. See: // https://bugs.llvm.org/show_bug.cgi?id=23764 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58074 // // Because of this, we can't use this technique. We guard it by CAPNP_DEBUG_TYPES so that it // still applies to the Cap'n Proto developers during internal testing. DisallowConstCopy() = default; DisallowConstCopy(DisallowConstCopy&) = default; DisallowConstCopy(DisallowConstCopy&&) = default; DisallowConstCopy& operator=(DisallowConstCopy&) = default; DisallowConstCopy& operator=(DisallowConstCopy&&) = default; #endif }; #if _MSC_VER #define KJ_CPCAP(obj) obj=::kj::cp(obj) // TODO(msvc): MSVC refuses to invoke non-const versions of copy constructors in by-value lambda // captures. Wrap your captured object in this macro to force the compiler to perform a copy. // Example: // // struct Foo: DisallowConstCopy {}; // Foo foo; // auto lambda = [KJ_CPCAP(foo)] {}; #else #define KJ_CPCAP(obj) obj // Clang and gcc both already perform copy capturing correctly with non-const copy constructors. #endif template struct DisallowConstCopyIfNotConst: public DisallowConstCopy { // Inherit from this when implementing a template that contains a pointer to T and which should // enforce transitive constness. If T is a const type, this has no effect. Otherwise, it is // an alias for DisallowConstCopy. }; template struct DisallowConstCopyIfNotConst {}; template struct IsConst_ { static constexpr bool value = false; }; template struct IsConst_ { static constexpr bool value = true; }; template constexpr bool isConst() { return IsConst_::value; } template struct EnableIfNotConst_ { typedef T Type; }; template struct EnableIfNotConst_; template using EnableIfNotConst = typename EnableIfNotConst_::Type; template struct EnableIfConst_; template struct EnableIfConst_ { typedef T Type; }; template using EnableIfConst = typename EnableIfConst_::Type; template struct RemoveConstOrDisable_ { struct Type; }; template struct RemoveConstOrDisable_ { typedef T Type; }; template using RemoveConstOrDisable = typename RemoveConstOrDisable_::Type; template struct IsReference_ { static constexpr bool value = false; }; template struct IsReference_ { static constexpr bool value = true; }; template constexpr bool isReference() { return IsReference_::value; } template struct PropagateConst_ { typedef To Type; }; template struct PropagateConst_ { typedef const To Type; }; template using PropagateConst = typename PropagateConst_::Type; namespace _ { // private template T refIfLvalue(T&&); } // namespace _ (private) #define KJ_DECLTYPE_REF(exp) decltype(::kj::_::refIfLvalue(exp)) // Like decltype(exp), but if exp is an lvalue, produces a reference type. // // int i; // decltype(i) i1(i); // i1 has type int. // KJ_DECLTYPE_REF(i + 1) i2(i + 1); // i2 has type int. // KJ_DECLTYPE_REF(i) i3(i); // i3 has type int&. // KJ_DECLTYPE_REF(kj::mv(i)) i4(kj::mv(i)); // i4 has type int. template struct IsSameType_ { static constexpr bool value = false; }; template struct IsSameType_ { static constexpr bool value = true; }; template constexpr bool isSameType() { return IsSameType_::value; } template struct CanConvert_ { static int sfinae(T); static bool sfinae(...); }; template constexpr bool canConvert() { return sizeof(CanConvert_::sfinae(instance())) == sizeof(int); } #if __GNUC__ && !__clang__ && __GNUC__ < 5 template constexpr bool canMemcpy() { // Returns true if T can be copied using memcpy instead of using the copy constructor or // assignment operator. // GCC 4 does not have __is_trivially_constructible and friends, and there doesn't seem to be // any reliable alternative. __has_trivial_copy() and __has_trivial_assign() return the right // thing at one point but later on they changed such that a deleted copy constructor was // considered "trivial" (apparently technically correct, though useless). So, on GCC 4 we give up // and assume we can't memcpy() at all, and must explicitly copy-construct everything. return false; } #define KJ_ASSERT_CAN_MEMCPY(T) #else template constexpr bool canMemcpy() { // Returns true if T can be copied using memcpy instead of using the copy constructor or // assignment operator. return __is_trivially_constructible(T, const T&) && __is_trivially_assignable(T, const T&); } #define KJ_ASSERT_CAN_MEMCPY(T) \ static_assert(kj::canMemcpy(), "this code expects this type to be memcpy()-able"); #endif template class Badge { // A pattern for marking individual methods such that they can only be called from a specific // caller class: Make the method public but give it a parameter of type `Badge`. Only // `Caller` can construct one, so only `Caller` can call the method. // // // We only allow calls from the class `Bar`. // void foo(Badge) // // The call site looks like: // // foo({}); // // This pattern also works well for declaring private constructors, but still being able to use // them with `kj::heap()`, etc. // // Idea from: https://awesomekling.github.io/Serenity-C++-patterns-The-Badge/ // // Note that some forms of this idea make the copy constructor private as well, in order to // prohibit `Badge(*(Badge*)nullptr)`. However, that would prevent badges from // being passed through forwarding functions like `kj::heap()`, which would ruin one of the main // use cases for this pattern in KJ. In any case, dereferencing a null pointer is UB; there are // plenty of other ways to get access to private members if you're willing to go UB. For one-off // debugging purposes, you might as well use `#define private public` at the top of the file. private: Badge() {} friend T; }; // ======================================================================================= // Equivalents to std::move() and std::forward(), since these are very commonly needed and the // std header pulls in lots of other stuff. // // We use abbreviated names mv and fwd because these helpers (especially mv) are so commonly used // that the cost of typing more letters outweighs the cost of being slightly harder to understand // when first encountered. template constexpr T&& mv(T& t) noexcept { return static_cast(t); } template constexpr T&& fwd(NoInfer& t) noexcept { return static_cast(t); } template constexpr T cp(T& t) noexcept { return t; } template constexpr T cp(const T& t) noexcept { return t; } // Useful to force a copy, particularly to pass into a function that expects T&&. template struct ChooseType_; template struct ChooseType_ { typedef T Type; }; template struct ChooseType_ { typedef T Type; }; template struct ChooseType_ { typedef U Type; }; template using WiderType = typename ChooseType_= sizeof(U)>::Type; template inline constexpr auto min(T&& a, U&& b) -> WiderType, Decay> { return a < b ? WiderType, Decay>(a) : WiderType, Decay>(b); } template inline constexpr auto max(T&& a, U&& b) -> WiderType, Decay> { return a > b ? WiderType, Decay>(a) : WiderType, Decay>(b); } template inline constexpr size_t size(T (&arr)[s]) { return s; } template inline constexpr size_t size(T&& arr) { return arr.size(); } // Returns the size of the parameter, whether the parameter is a regular C array or a container // with a `.size()` method. class MaxValue_ { private: template inline constexpr T maxSigned() const { return (1ull << (sizeof(T) * 8 - 1)) - 1; } template inline constexpr T maxUnsigned() const { return ~static_cast(0u); } public: #define _kJ_HANDLE_TYPE(T) \ inline constexpr operator signed T() const { return MaxValue_::maxSigned < signed T>(); } \ inline constexpr operator unsigned T() const { return MaxValue_::maxUnsigned(); } _kJ_HANDLE_TYPE(char) _kJ_HANDLE_TYPE(short) _kJ_HANDLE_TYPE(int) _kJ_HANDLE_TYPE(long) _kJ_HANDLE_TYPE(long long) #undef _kJ_HANDLE_TYPE inline constexpr operator char() const { // `char` is different from both `signed char` and `unsigned char`, and may be signed or // unsigned on different platforms. Ugh. return char(-1) < 0 ? MaxValue_::maxSigned() : MaxValue_::maxUnsigned(); } }; class MinValue_ { private: template inline constexpr T minSigned() const { return 1ull << (sizeof(T) * 8 - 1); } template inline constexpr T minUnsigned() const { return 0u; } public: #define _kJ_HANDLE_TYPE(T) \ inline constexpr operator signed T() const { return MinValue_::minSigned < signed T>(); } \ inline constexpr operator unsigned T() const { return MinValue_::minUnsigned(); } _kJ_HANDLE_TYPE(char) _kJ_HANDLE_TYPE(short) _kJ_HANDLE_TYPE(int) _kJ_HANDLE_TYPE(long) _kJ_HANDLE_TYPE(long long) #undef _kJ_HANDLE_TYPE inline constexpr operator char() const { // `char` is different from both `signed char` and `unsigned char`, and may be signed or // unsigned on different platforms. Ugh. return char(-1) < 0 ? MinValue_::minSigned() : MinValue_::minUnsigned(); } }; static KJ_CONSTEXPR(const) MaxValue_ maxValue = MaxValue_(); // A special constant which, when cast to an integer type, takes on the maximum possible value of // that type. This is useful to use as e.g. a parameter to a function because it will be robust // in the face of changes to the parameter's type. // // `char` is not supported, but `signed char` and `unsigned char` are. static KJ_CONSTEXPR(const) MinValue_ minValue = MinValue_(); // A special constant which, when cast to an integer type, takes on the minimum possible value // of that type. This is useful to use as e.g. a parameter to a function because it will be robust // in the face of changes to the parameter's type. // // `char` is not supported, but `signed char` and `unsigned char` are. template inline bool operator==(T t, MaxValue_) { return t == Decay(maxValue); } template inline bool operator==(T t, MinValue_) { return t == Decay(minValue); } template inline constexpr unsigned long long maxValueForBits() { // Get the maximum integer representable in the given number of bits. // 1ull << 64 is unfortunately undefined. return (bits == 64 ? 0 : (1ull << bits)) - 1; } struct ThrowOverflow { // Functor which throws an exception complaining about integer overflow. Usually this is used // with the interfaces in units.h, but is defined here because Cap'n Proto wants to avoid // including units.h when not using CAPNP_DEBUG_TYPES. void operator()() const; }; #if __GNUC__ || __clang__ inline constexpr float inf() { return __builtin_huge_valf(); } inline constexpr float nan() { return __builtin_nanf(""); } #elif _MSC_VER // Do what MSVC math.h does #pragma warning(push) #pragma warning(disable: 4756) // "overflow in constant arithmetic" inline constexpr float inf() { return (float)(1e300 * 1e300); } #pragma warning(pop) float nan(); // Unfortunately, inf() * 0.0f produces a NaN with the sign bit set, whereas our preferred // canonical NaN should not have the sign bit set. std::numeric_limits::quiet_NaN() // returns the correct NaN, but we don't want to #include that here. So, we give up and make // this out-of-line on MSVC. // // TODO(msvc): Can we do better? #else #error "Not sure how to support your compiler." #endif inline constexpr bool isNaN(float f) { return f != f; } inline constexpr bool isNaN(double f) { return f != f; } inline int popCount(unsigned int x) { #if defined(_MSC_VER) return __popcnt(x); // Note: __popcnt returns unsigned int, but the value is clearly guaranteed to fit into an int #else return __builtin_popcount(x); #endif } // ======================================================================================= // Useful fake containers template class Range { public: inline constexpr Range(const T& begin, const T& end): begin_(begin), end_(end) {} inline explicit constexpr Range(const T& end): begin_(0), end_(end) {} class Iterator { public: Iterator() = default; inline Iterator(const T& value): value(value) {} inline const T& operator* () const { return value; } inline const T& operator[](size_t index) const { return value + index; } inline Iterator& operator++() { ++value; return *this; } inline Iterator operator++(int) { return Iterator(value++); } inline Iterator& operator--() { --value; return *this; } inline Iterator operator--(int) { return Iterator(value--); } inline Iterator& operator+=(ptrdiff_t amount) { value += amount; return *this; } inline Iterator& operator-=(ptrdiff_t amount) { value -= amount; return *this; } inline Iterator operator+ (ptrdiff_t amount) const { return Iterator(value + amount); } inline Iterator operator- (ptrdiff_t amount) const { return Iterator(value - amount); } inline ptrdiff_t operator- (const Iterator& other) const { return value - other.value; } inline bool operator==(const Iterator& other) const { return value == other.value; } inline bool operator!=(const Iterator& other) const { return value != other.value; } inline bool operator<=(const Iterator& other) const { return value <= other.value; } inline bool operator>=(const Iterator& other) const { return value >= other.value; } inline bool operator< (const Iterator& other) const { return value < other.value; } inline bool operator> (const Iterator& other) const { return value > other.value; } private: T value; }; inline Iterator begin() const { return Iterator(begin_); } inline Iterator end() const { return Iterator(end_); } inline auto size() const -> decltype(instance() - instance()) { return end_ - begin_; } private: T begin_; T end_; }; template inline constexpr Range, Decay>> range(T begin, U end) { return Range, Decay>>(begin, end); } template inline constexpr Range> range(T begin, T end) { return Range>(begin, end); } // Returns a fake iterable container containing all values of T from `begin` (inclusive) to `end` // (exclusive). Example: // // // Prints 1, 2, 3, 4, 5, 6, 7, 8, 9. // for (int i: kj::range(1, 10)) { print(i); } template inline constexpr Range> zeroTo(T end) { return Range>(end); } // Returns a fake iterable container containing all values of T from zero (inclusive) to `end` // (exclusive). Example: // // // Prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. // for (int i: kj::zeroTo(10)) { print(i); } template inline constexpr Range indices(T&& container) { // Shortcut for iterating over the indices of a container: // // for (size_t i: kj::indices(myArray)) { handle(myArray[i]); } return range(0, kj::size(container)); } template class Repeat { public: inline constexpr Repeat(const T& value, size_t count): value(value), count(count) {} class Iterator { public: Iterator() = default; inline Iterator(const T& value, size_t index): value(value), index(index) {} inline const T& operator* () const { return value; } inline const T& operator[](ptrdiff_t index) const { return value; } inline Iterator& operator++() { ++index; return *this; } inline Iterator operator++(int) { return Iterator(value, index++); } inline Iterator& operator--() { --index; return *this; } inline Iterator operator--(int) { return Iterator(value, index--); } inline Iterator& operator+=(ptrdiff_t amount) { index += amount; return *this; } inline Iterator& operator-=(ptrdiff_t amount) { index -= amount; return *this; } inline Iterator operator+ (ptrdiff_t amount) const { return Iterator(value, index + amount); } inline Iterator operator- (ptrdiff_t amount) const { return Iterator(value, index - amount); } inline ptrdiff_t operator- (const Iterator& other) const { return index - other.index; } inline bool operator==(const Iterator& other) const { return index == other.index; } inline bool operator!=(const Iterator& other) const { return index != other.index; } inline bool operator<=(const Iterator& other) const { return index <= other.index; } inline bool operator>=(const Iterator& other) const { return index >= other.index; } inline bool operator< (const Iterator& other) const { return index < other.index; } inline bool operator> (const Iterator& other) const { return index > other.index; } private: T value; size_t index; }; inline Iterator begin() const { return Iterator(value, 0); } inline Iterator end() const { return Iterator(value, count); } inline size_t size() const { return count; } inline const T& operator[](ptrdiff_t) const { return value; } private: T value; size_t count; }; template inline constexpr Repeat> repeat(T&& value, size_t count) { // Returns a fake iterable which contains `count` repeats of `value`. Useful for e.g. creating // a bunch of spaces: `kj::repeat(' ', indent * 2)` return Repeat>(value, count); } // ======================================================================================= // Manually invoking constructors and destructors // // ctor(x, ...) and dtor(x) invoke x's constructor or destructor, respectively. // We want placement new, but we don't want to #include . operator new cannot be defined in // a namespace, and defining it globally conflicts with the definition in . So we have to // define a dummy type and an operator new that uses it. namespace _ { // private struct PlacementNew {}; } // namespace _ (private) } // namespace kj inline void* operator new(size_t, kj::_::PlacementNew, void* __p) noexcept { return __p; } inline void operator delete(void*, kj::_::PlacementNew, void* __p) noexcept {} namespace kj { template inline void ctor(T& location, Params&&... params) { new (_::PlacementNew(), &location) T(kj::fwd(params)...); } template inline void dtor(T& location) { location.~T(); } // ======================================================================================= // Maybe // // Use in cases where you want to indicate that a value may be null. Using Maybe instead of T* // forces the caller to handle the null case in order to satisfy the compiler, thus reliably // preventing null pointer dereferences at runtime. // // Maybe can be implicitly constructed from T and from nullptr. Additionally, it can be // implicitly constructed from T*, in which case the pointer is checked for nullness at runtime. // To read the value of a Maybe, do: // // KJ_IF_MAYBE(value, someFuncReturningMaybe()) { // doSomething(*value); // } else { // maybeWasNull(); // } // // KJ_IF_MAYBE's first parameter is a variable name which will be defined within the following // block. The variable will behave like a (guaranteed non-null) pointer to the Maybe's value, // though it may or may not actually be a pointer. // // Note that Maybe actually just wraps a pointer, whereas Maybe wraps a T and a boolean // indicating nullness. template class Maybe; namespace _ { // private template class NullableValue { // Class whose interface behaves much like T*, but actually contains an instance of T and a // boolean flag indicating nullness. public: inline NullableValue(NullableValue&& other) : isSet(other.isSet) { if (isSet) { ctor(value, kj::mv(other.value)); } } inline NullableValue(const NullableValue& other) : isSet(other.isSet) { if (isSet) { ctor(value, other.value); } } inline NullableValue(NullableValue& other) : isSet(other.isSet) { if (isSet) { ctor(value, other.value); } } inline ~NullableValue() #if _MSC_VER // TODO(msvc): MSVC has a hard time with noexcept specifier expressions that are more complex // than `true` or `false`. We had a workaround for VS2015, but VS2017 regressed. noexcept(false) #else noexcept(noexcept(instance().~T())) #endif { if (isSet) { dtor(value); } } inline T& operator*() & { return value; } inline const T& operator*() const & { return value; } inline T&& operator*() && { return kj::mv(value); } inline const T&& operator*() const && { return kj::mv(value); } inline T* operator->() { return &value; } inline const T* operator->() const { return &value; } inline operator T*() { return isSet ? &value : nullptr; } inline operator const T*() const { return isSet ? &value : nullptr; } template inline T& emplace(Params&&... params) { if (isSet) { isSet = false; dtor(value); } ctor(value, kj::fwd(params)...); isSet = true; return value; } inline NullableValue(): isSet(false) {} inline NullableValue(T&& t) : isSet(true) { ctor(value, kj::mv(t)); } inline NullableValue(T& t) : isSet(true) { ctor(value, t); } inline NullableValue(const T& t) : isSet(true) { ctor(value, t); } template inline NullableValue(NullableValue&& other) : isSet(other.isSet) { if (isSet) { ctor(value, kj::mv(other.value)); } } template inline NullableValue(const NullableValue& other) : isSet(other.isSet) { if (isSet) { ctor(value, other.value); } } template inline NullableValue(const NullableValue& other) : isSet(other.isSet) { if (isSet) { ctor(value, *other.ptr); } } inline NullableValue(decltype(nullptr)): isSet(false) {} inline NullableValue& operator=(NullableValue&& other) { if (&other != this) { // Careful about throwing destructors/constructors here. if (isSet) { isSet = false; dtor(value); } if (other.isSet) { ctor(value, kj::mv(other.value)); isSet = true; } } return *this; } inline NullableValue& operator=(NullableValue& other) { if (&other != this) { // Careful about throwing destructors/constructors here. if (isSet) { isSet = false; dtor(value); } if (other.isSet) { ctor(value, other.value); isSet = true; } } return *this; } inline NullableValue& operator=(const NullableValue& other) { if (&other != this) { // Careful about throwing destructors/constructors here. if (isSet) { isSet = false; dtor(value); } if (other.isSet) { ctor(value, other.value); isSet = true; } } return *this; } inline NullableValue& operator=(T&& other) { emplace(kj::mv(other)); return *this; } inline NullableValue& operator=(T& other) { emplace(other); return *this; } inline NullableValue& operator=(const T& other) { emplace(other); return *this; } template inline NullableValue& operator=(NullableValue&& other) { if (other.isSet) { emplace(kj::mv(other.value)); } else { *this = nullptr; } return *this; } template inline NullableValue& operator=(const NullableValue& other) { if (other.isSet) { emplace(other.value); } else { *this = nullptr; } return *this; } template inline NullableValue& operator=(const NullableValue& other) { if (other.isSet) { emplace(other.value); } else { *this = nullptr; } return *this; } inline NullableValue& operator=(decltype(nullptr)) { if (isSet) { isSet = false; dtor(value); } return *this; } inline bool operator==(decltype(nullptr)) const { return !isSet; } inline bool operator!=(decltype(nullptr)) const { return isSet; } NullableValue(const T* t) = delete; NullableValue& operator=(const T* other) = delete; // We used to permit assigning a Maybe directly from a T*, and the assignment would check for // nullness. This turned out never to be useful, and sometimes to be dangerous. private: bool isSet; #if _MSC_VER #pragma warning(push) #pragma warning(disable: 4624) // Warns that the anonymous union has a deleted destructor when T is non-trivial. This warning // seems broken. #endif union { T value; }; #if _MSC_VER #pragma warning(pop) #endif friend class kj::Maybe; template friend NullableValue&& readMaybe(Maybe&& maybe); }; template inline NullableValue&& readMaybe(Maybe&& maybe) { return kj::mv(maybe.ptr); } template inline T* readMaybe(Maybe& maybe) { return maybe.ptr; } template inline const T* readMaybe(const Maybe& maybe) { return maybe.ptr; } template inline T* readMaybe(Maybe&& maybe) { return maybe.ptr; } template inline T* readMaybe(const Maybe& maybe) { return maybe.ptr; } template inline T* readMaybe(T* ptr) { return ptr; } // Allow KJ_IF_MAYBE to work on regular pointers. } // namespace _ (private) #define KJ_IF_MAYBE(name, exp) if (auto name = ::kj::_::readMaybe(exp)) template class Maybe { // A T, or nullptr. // IF YOU CHANGE THIS CLASS: Note that there is a specialization of it in memory.h. public: Maybe(): ptr(nullptr) {} Maybe(T&& t): ptr(kj::mv(t)) {} Maybe(T& t): ptr(t) {} Maybe(const T& t): ptr(t) {} Maybe(Maybe&& other): ptr(kj::mv(other.ptr)) {} Maybe(const Maybe& other): ptr(other.ptr) {} Maybe(Maybe& other): ptr(other.ptr) {} template Maybe(Maybe&& other) { KJ_IF_MAYBE(val, kj::mv(other)) { ptr.emplace(kj::mv(*val)); } } template Maybe(Maybe&& other) { KJ_IF_MAYBE(val, other) { ptr.emplace(*val); } } template Maybe(const Maybe& other) { KJ_IF_MAYBE(val, other) { ptr.emplace(*val); } } Maybe(decltype(nullptr)): ptr(nullptr) {} template inline T& emplace(Params&&... params) { // Replace this Maybe's content with a new value constructed by passing the given parametrs to // T's constructor. This can be used to initialize a Maybe without copying or even moving a T. // Returns a reference to the newly-constructed value. return ptr.emplace(kj::fwd(params)...); } inline Maybe& operator=(T&& other) { ptr = kj::mv(other); return *this; } inline Maybe& operator=(T& other) { ptr = other; return *this; } inline Maybe& operator=(const T& other) { ptr = other; return *this; } inline Maybe& operator=(Maybe&& other) { ptr = kj::mv(other.ptr); return *this; } inline Maybe& operator=(Maybe& other) { ptr = other.ptr; return *this; } inline Maybe& operator=(const Maybe& other) { ptr = other.ptr; return *this; } template Maybe& operator=(Maybe&& other) { KJ_IF_MAYBE(val, kj::mv(other)) { ptr.emplace(kj::mv(*val)); } else { ptr = nullptr; } return *this; } template Maybe& operator=(const Maybe& other) { KJ_IF_MAYBE(val, other) { ptr.emplace(*val); } else { ptr = nullptr; } return *this; } inline Maybe& operator=(decltype(nullptr)) { ptr = nullptr; return *this; } inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; } inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; } Maybe(const T* t) = delete; Maybe& operator=(const T* other) = delete; // We used to permit assigning a Maybe directly from a T*, and the assignment would check for // nullness. This turned out never to be useful, and sometimes to be dangerous. T& orDefault(T& defaultValue) & { if (ptr == nullptr) { return defaultValue; } else { return *ptr; } } const T& orDefault(const T& defaultValue) const & { if (ptr == nullptr) { return defaultValue; } else { return *ptr; } } T&& orDefault(T&& defaultValue) && { if (ptr == nullptr) { return kj::mv(defaultValue); } else { return kj::mv(*ptr); } } const T&& orDefault(const T&& defaultValue) const && { if (ptr == nullptr) { return kj::mv(defaultValue); } else { return kj::mv(*ptr); } } template auto map(Func&& f) & -> Maybe()))> { if (ptr == nullptr) { return nullptr; } else { return f(*ptr); } } template auto map(Func&& f) const & -> Maybe()))> { if (ptr == nullptr) { return nullptr; } else { return f(*ptr); } } template auto map(Func&& f) && -> Maybe()))> { if (ptr == nullptr) { return nullptr; } else { return f(kj::mv(*ptr)); } } template auto map(Func&& f) const && -> Maybe()))> { if (ptr == nullptr) { return nullptr; } else { return f(kj::mv(*ptr)); } } private: _::NullableValue ptr; template friend class Maybe; template friend _::NullableValue&& _::readMaybe(Maybe&& maybe); template friend U* _::readMaybe(Maybe& maybe); template friend const U* _::readMaybe(const Maybe& maybe); }; template class Maybe: public DisallowConstCopyIfNotConst { public: constexpr Maybe(): ptr(nullptr) {} constexpr Maybe(T& t): ptr(&t) {} constexpr Maybe(T* t): ptr(t) {} template inline constexpr Maybe(Maybe& other): ptr(other.ptr) {} template inline constexpr Maybe(const Maybe& other): ptr(const_cast(other.ptr)) {} inline constexpr Maybe(decltype(nullptr)): ptr(nullptr) {} inline Maybe& operator=(T& other) { ptr = &other; return *this; } inline Maybe& operator=(T* other) { ptr = other; return *this; } template inline Maybe& operator=(Maybe& other) { ptr = other.ptr; return *this; } template inline Maybe& operator=(const Maybe& other) { ptr = other.ptr; return *this; } inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; } inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; } T& orDefault(T& defaultValue) { if (ptr == nullptr) { return defaultValue; } else { return *ptr; } } const T& orDefault(const T& defaultValue) const { if (ptr == nullptr) { return defaultValue; } else { return *ptr; } } template auto map(Func&& f) -> Maybe()))> { if (ptr == nullptr) { return nullptr; } else { return f(*ptr); } } template auto map(Func&& f) const -> Maybe()))> { if (ptr == nullptr) { return nullptr; } else { const T& ref = *ptr; return f(ref); } } private: T* ptr; template friend class Maybe; template friend U* _::readMaybe(Maybe&& maybe); template friend U* _::readMaybe(const Maybe& maybe); }; // ======================================================================================= // ArrayPtr // // So common that we put it in common.h rather than array.h. template class Array; template class ArrayPtr: public DisallowConstCopyIfNotConst { // A pointer to an array. Includes a size. Like any pointer, it doesn't own the target data, // and passing by value only copies the pointer, not the target. public: inline constexpr ArrayPtr(): ptr(nullptr), size_(0) {} inline constexpr ArrayPtr(decltype(nullptr)): ptr(nullptr), size_(0) {} inline constexpr ArrayPtr(T* ptr, size_t size): ptr(ptr), size_(size) {} inline constexpr ArrayPtr(T* begin, T* end): ptr(begin), size_(end - begin) {} #if __GNUC__ && !__clang__ && __GNUC__ >= 9 // GCC 9 added a warning when we take an initializer_list as a constructor parameter and save a // pointer to its content in a class member. GCC apparently imagines we're going to do something // dumb like this: // ArrayPtr ptr = { 1, 2, 3 }; // foo(ptr[1]); // undefined behavior! // Any KJ programmer should be able to recognize that this is UB, because an ArrayPtr does not own // its content. That's not what this constructor is for, tohugh. This constructor is meant to allow // code like this: // int foo(ArrayPtr p); // // ... later ... // foo({1, 2, 3}); // In this case, the initializer_list's backing array, like any temporary, lives until the end of // the statement `foo({1, 2, 3});`. Therefore, it lives at least until the call to foo() has // returned, which is exactly what we care about. This usage is fine! GCC is wrong to warn. // // Amusingly, Clang's implementation has a similar type that they call ArrayRef which apparently // triggers this same GCC warning. My guess is that Clang will not introduce a similar warning // given that it triggers on their own, legitimate code. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Winit-list-lifetime" #endif inline KJ_CONSTEXPR() ArrayPtr(::std::initializer_list> init) : ptr(init.begin()), size_(init.size()) {} #if __GNUC__ && !__clang__ && __GNUC__ >= 9 #pragma GCC diagnostic pop #endif template inline constexpr ArrayPtr(T (&native)[size]): ptr(native), size_(size) { // Construct an ArrayPtr from a native C-style array. // // We disable this constructor for const char arrays because otherwise you would be able to // implicitly convert a character literal to ArrayPtr, which sounds really great, // except that the NUL terminator would be included, which probably isn't what you intended. // // TODO(someday): Maybe we should support character literals but explicitly chop off the NUL // terminator. This could do the wrong thing if someone tries to construct an // ArrayPtr from a non-NUL-terminated char array, but evidence suggests that all // real use cases are in fact intending to remove the NUL terminator. It's convenient to be // able to specify ArrayPtr as a parameter type and be able to accept strings // as input in addition to arrays. Currently, you'll need overloading to support string // literals in this case, but if you overload StringPtr, then you'll find that several // conversions (e.g. from String and from a literal char array) become ambiguous! You end up // having to overload for literal char arrays specifically which is cumbersome. static_assert(!isSameType(), "Can't implicitly convert literal char array to ArrayPtr because we don't know if " "you meant to include the NUL terminator. We may change this in the future to " "automatically drop the NUL terminator. For now, try explicitly converting to StringPtr, " "which can in turn implicitly convert to ArrayPtr."); static_assert(!isSameType(), "see above"); static_assert(!isSameType(), "see above"); } inline operator ArrayPtr() const { return ArrayPtr(ptr, size_); } inline ArrayPtr asConst() const { return ArrayPtr(ptr, size_); } inline constexpr size_t size() const { return size_; } inline const T& operator[](size_t index) const { KJ_IREQUIRE(index < size_, "Out-of-bounds ArrayPtr access."); return ptr[index]; } inline T& operator[](size_t index) { KJ_IREQUIRE(index < size_, "Out-of-bounds ArrayPtr access."); return ptr[index]; } inline T* begin() { return ptr; } inline T* end() { return ptr + size_; } inline T& front() { return *ptr; } inline T& back() { return *(ptr + size_ - 1); } inline constexpr const T* begin() const { return ptr; } inline constexpr const T* end() const { return ptr + size_; } inline const T& front() const { return *ptr; } inline const T& back() const { return *(ptr + size_ - 1); } inline ArrayPtr slice(size_t start, size_t end) const { KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds ArrayPtr::slice()."); return ArrayPtr(ptr + start, end - start); } inline ArrayPtr slice(size_t start, size_t end) { KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds ArrayPtr::slice()."); return ArrayPtr(ptr + start, end - start); } inline ArrayPtr> asBytes() const { // Reinterpret the array as a byte array. This is explicitly legal under C++ aliasing // rules. return { reinterpret_cast*>(ptr), size_ * sizeof(T) }; } inline ArrayPtr> asChars() const { // Reinterpret the array as a char array. This is explicitly legal under C++ aliasing // rules. return { reinterpret_cast*>(ptr), size_ * sizeof(T) }; } inline bool operator==(decltype(nullptr)) const { return size_ == 0; } inline bool operator!=(decltype(nullptr)) const { return size_ != 0; } inline bool operator==(const ArrayPtr& other) const { if (size_ != other.size_) return false; for (size_t i = 0; i < size_; i++) { if (ptr[i] != other[i]) return false; } return true; } inline bool operator!=(const ArrayPtr& other) const { return !(*this == other); } template inline bool operator==(const ArrayPtr& other) const { if (size_ != other.size()) return false; for (size_t i = 0; i < size_; i++) { if (ptr[i] != other[i]) return false; } return true; } template inline bool operator!=(const ArrayPtr& other) const { return !(*this == other); } template Array attach(Attachments&&... attachments) const KJ_WARN_UNUSED_RESULT; // Like Array::attach(), but also promotes an ArrayPtr to an Array. Generally the attachment // should be an object that actually owns the array that the ArrayPtr is pointing at. // // You must include kj/array.h to call this. private: T* ptr; size_t size_; }; template inline constexpr ArrayPtr arrayPtr(T* ptr, size_t size) { // Use this function to construct ArrayPtrs without writing out the type name. return ArrayPtr(ptr, size); } template inline constexpr ArrayPtr arrayPtr(T* begin, T* end) { // Use this function to construct ArrayPtrs without writing out the type name. return ArrayPtr(begin, end); } // ======================================================================================= // Casts template To implicitCast(From&& from) { // `implicitCast(value)` casts `value` to type `T` only if the conversion is implicit. Useful // for e.g. resolving ambiguous overloads without sacrificing type-safety. return kj::fwd(from); } template Maybe dynamicDowncastIfAvailable(From& from) { // If RTTI is disabled, always returns nullptr. Otherwise, works like dynamic_cast. Useful // in situations where dynamic_cast could allow an optimization, but isn't strictly necessary // for correctness. It is highly recommended that you try to arrange all your dynamic_casts // this way, as a dynamic_cast that is necessary for correctness implies a flaw in the interface // design. // Force a compile error if To is not a subtype of From. Cross-casting is rare; if it is needed // we should have a separate cast function like dynamicCrosscastIfAvailable(). if (false) { kj::implicitCast(kj::implicitCast(nullptr)); } #if KJ_NO_RTTI return nullptr; #else return dynamic_cast(&from); #endif } template To& downcast(From& from) { // Down-cast a value to a sub-type, asserting that the cast is valid. In opt mode this is a // static_cast, but in debug mode (when RTTI is enabled) a dynamic_cast will be used to verify // that the value really has the requested type. // Force a compile error if To is not a subtype of From. if (false) { kj::implicitCast(kj::implicitCast(nullptr)); } #if !KJ_NO_RTTI KJ_IREQUIRE(dynamic_cast(&from) != nullptr, "Value cannot be downcast() to requested type."); #endif return static_cast(from); } // ======================================================================================= // Defer namespace _ { // private template class Deferred { public: inline Deferred(Func&& func): func(kj::fwd(func)), canceled(false) {} inline ~Deferred() noexcept(false) { if (!canceled) func(); } KJ_DISALLOW_COPY(Deferred); // This move constructor is usually optimized away by the compiler. inline Deferred(Deferred&& other): func(kj::mv(other.func)), canceled(false) { other.canceled = true; } private: Func func; bool canceled; }; } // namespace _ (private) template _::Deferred defer(Func&& func) { // Returns an object which will invoke the given functor in its destructor. The object is not // copyable but is movable with the semantics you'd expect. Since the return type is private, // you need to assign to an `auto` variable. // // The KJ_DEFER macro provides slightly more convenient syntax for the common case where you // want some code to run at current scope exit. return _::Deferred(kj::fwd(func)); } #define KJ_DEFER(code) auto KJ_UNIQUE_NAME(_kjDefer) = ::kj::defer([&](){code;}) // Run the given code when the function exits, whether by return or exception. } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/units.h0000644000175000017500000013171613650101756017473 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains types which are intended to help detect incorrect usage at compile // time, but should then be optimized down to basic primitives (usually, integers) by the // compiler. #pragma once #include "common.h" #include KJ_BEGIN_HEADER namespace kj { // ======================================================================================= // IDs template struct Id { // A type-safe numeric ID. `UnderlyingType` is the underlying integer representation. `Label` // distinguishes this Id from other Id types. Sample usage: // // class Foo; // typedef Id FooId; // // class Bar; // typedef Id BarId; // // You can now use the FooId and BarId types without any possibility of accidentally using a // FooId when you really wanted a BarId or vice-versa. UnderlyingType value; inline constexpr Id(): value(0) {} inline constexpr explicit Id(int value): value(value) {} inline constexpr bool operator==(const Id& other) const { return value == other.value; } inline constexpr bool operator!=(const Id& other) const { return value != other.value; } inline constexpr bool operator<=(const Id& other) const { return value <= other.value; } inline constexpr bool operator>=(const Id& other) const { return value >= other.value; } inline constexpr bool operator< (const Id& other) const { return value < other.value; } inline constexpr bool operator> (const Id& other) const { return value > other.value; } }; // ======================================================================================= // Quantity and UnitRatio -- implement unit analysis via the type system struct Unsafe_ {}; constexpr Unsafe_ unsafe = Unsafe_(); // Use as a parameter to constructors that are unsafe to indicate that you really do mean it. template class Bounded; template class BoundedConst; template constexpr bool isIntegral() { return false; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template <> constexpr bool isIntegral() { return true; } template struct IsIntegralOrBounded_ { static constexpr bool value = isIntegral(); }; template struct IsIntegralOrBounded_> { static constexpr bool value = true; }; template struct IsIntegralOrBounded_> { static constexpr bool value = true; }; template inline constexpr bool isIntegralOrBounded() { return IsIntegralOrBounded_::value; } template class UnitRatio { // A multiplier used to convert Quantities of one unit to Quantities of another unit. See // Quantity, below. // // Construct this type by dividing one Quantity by another of a different unit. Use this type // by multiplying it by a Quantity, or dividing a Quantity by it. static_assert(isIntegralOrBounded(), "Underlying type for UnitRatio must be integer."); public: inline UnitRatio() {} constexpr UnitRatio(Number unit1PerUnit2, decltype(unsafe)): unit1PerUnit2(unit1PerUnit2) {} // This constructor was intended to be private, but GCC complains about it being private in a // bunch of places that don't appear to even call it, so I made it public. Oh well. template inline constexpr UnitRatio(const UnitRatio& other) : unit1PerUnit2(other.unit1PerUnit2) {} template inline constexpr UnitRatio operator+(UnitRatio other) const { return UnitRatio( unit1PerUnit2 + other.unit1PerUnit2, unsafe); } template inline constexpr UnitRatio operator-(UnitRatio other) const { return UnitRatio( unit1PerUnit2 - other.unit1PerUnit2, unsafe); } template inline constexpr UnitRatio operator*(UnitRatio other) const { // U1 / U2 * U3 / U1 = U3 / U2 return UnitRatio( unit1PerUnit2 * other.unit1PerUnit2, unsafe); } template inline constexpr UnitRatio operator*(UnitRatio other) const { // U1 / U2 * U2 / U3 = U1 / U3 return UnitRatio( unit1PerUnit2 * other.unit1PerUnit2, unsafe); } template inline constexpr UnitRatio operator/(UnitRatio other) const { // (U1 / U2) / (U1 / U3) = U3 / U2 return UnitRatio( unit1PerUnit2 / other.unit1PerUnit2, unsafe); } template inline constexpr UnitRatio operator/(UnitRatio other) const { // (U1 / U2) / (U3 / U2) = U1 / U3 return UnitRatio( unit1PerUnit2 / other.unit1PerUnit2, unsafe); } template inline decltype(Number() / OtherNumber()) operator/(UnitRatio other) const { return unit1PerUnit2 / other.unit1PerUnit2; } inline bool operator==(UnitRatio other) const { return unit1PerUnit2 == other.unit1PerUnit2; } inline bool operator!=(UnitRatio other) const { return unit1PerUnit2 != other.unit1PerUnit2; } private: Number unit1PerUnit2; template friend class Quantity; template friend class UnitRatio; template friend inline constexpr UnitRatio operator*(N1, UnitRatio); }; template () && isIntegralOrBounded()>> inline constexpr UnitRatio operator*(N1 n, UnitRatio r) { return UnitRatio(n * r.unit1PerUnit2, unsafe); } template class Quantity { // A type-safe numeric quantity, specified in terms of some unit. Two Quantities cannot be used // in arithmetic unless they use the same unit. The `Unit` type parameter is only used to prevent // accidental mixing of units; this type is never instantiated and can very well be incomplete. // `Number` is the underlying primitive numeric type. // // Quantities support most basic arithmetic operators, intelligently handling units, and // automatically casting the underlying type in the same way that the compiler would. // // To convert a primitive number to a Quantity, multiply it by unit>(). // To convert a Quantity to a primitive number, divide it by unit>(). // To convert a Quantity of one unit to another unit, multiply or divide by a UnitRatio. // // The Quantity class is not well-suited to hardcore physics as it does not allow multiplying // one quantity by another. For example, multiplying meters by meters won't get you square // meters; it will get you a compiler error. It would be interesting to see if template // metaprogramming could properly deal with such things but this isn't needed for the present // use case. // // Sample usage: // // class SecondsLabel; // typedef Quantity Seconds; // constexpr Seconds SECONDS = unit(); // // class MinutesLabel; // typedef Quantity Minutes; // constexpr Minutes MINUTES = unit(); // // constexpr UnitRatio SECONDS_PER_MINUTE = // 60 * SECONDS / MINUTES; // // void waitFor(Seconds seconds) { // sleep(seconds / SECONDS); // } // void waitFor(Minutes minutes) { // waitFor(minutes * SECONDS_PER_MINUTE); // } // // void waitThreeMinutes() { // waitFor(3 * MINUTES); // } static_assert(isIntegralOrBounded(), "Underlying type for Quantity must be integer."); public: inline constexpr Quantity() = default; inline constexpr Quantity(MaxValue_): value(maxValue) {} inline constexpr Quantity(MinValue_): value(minValue) {} // Allow initialization from maxValue and minValue. // TODO(msvc): decltype(maxValue) and decltype(minValue) deduce unknown-type for these function // parameters, causing the compiler to complain of a duplicate constructor definition, so we // specify MaxValue_ and MinValue_ types explicitly. inline constexpr Quantity(Number value, decltype(unsafe)): value(value) {} // This constructor was intended to be private, but GCC complains about it being private in a // bunch of places that don't appear to even call it, so I made it public. Oh well. template inline constexpr Quantity(const Quantity& other) : value(other.value) {} template inline Quantity& operator=(const Quantity& other) { value = other.value; return *this; } template inline constexpr Quantity operator+(const Quantity& other) const { return Quantity(value + other.value, unsafe); } template inline constexpr Quantity operator-(const Quantity& other) const { return Quantity(value - other.value, unsafe); } template ()>> inline constexpr Quantity operator*(OtherNumber other) const { return Quantity(value * other, unsafe); } template ()>> inline constexpr Quantity operator/(OtherNumber other) const { return Quantity(value / other, unsafe); } template inline constexpr decltype(Number() / OtherNumber()) operator/(const Quantity& other) const { return value / other.value; } template inline constexpr Quantity operator%(const Quantity& other) const { return Quantity(value % other.value, unsafe); } template inline constexpr Quantity operator*(UnitRatio ratio) const { return Quantity( value * ratio.unit1PerUnit2, unsafe); } template inline constexpr Quantity operator/(UnitRatio ratio) const { return Quantity( value / ratio.unit1PerUnit2, unsafe); } template inline constexpr Quantity operator%(UnitRatio ratio) const { return Quantity( value % ratio.unit1PerUnit2, unsafe); } template inline constexpr UnitRatio operator/(Quantity other) const { return UnitRatio( value / other.value, unsafe); } template inline constexpr bool operator==(const Quantity& other) const { return value == other.value; } template inline constexpr bool operator!=(const Quantity& other) const { return value != other.value; } template inline constexpr bool operator<=(const Quantity& other) const { return value <= other.value; } template inline constexpr bool operator>=(const Quantity& other) const { return value >= other.value; } template inline constexpr bool operator<(const Quantity& other) const { return value < other.value; } template inline constexpr bool operator>(const Quantity& other) const { return value > other.value; } template inline Quantity& operator+=(const Quantity& other) { value += other.value; return *this; } template inline Quantity& operator-=(const Quantity& other) { value -= other.value; return *this; } template inline Quantity& operator*=(OtherNumber other) { value *= other; return *this; } template inline Quantity& operator/=(OtherNumber other) { value /= other.value; return *this; } private: Number value; template friend class Quantity; template friend inline constexpr auto operator*(Number1 a, Quantity b) -> Quantity; }; template struct Unit_ { static inline constexpr T get() { return T(1); } }; template struct Unit_> { static inline constexpr Quantity::get()), U> get() { return Quantity::get()), U>(Unit_::get(), unsafe); } }; template inline constexpr auto unit() -> decltype(Unit_::get()) { return Unit_::get(); } // unit>() returns a Quantity of value 1. It also, intentionally, works on basic // numeric types. template ()>> inline constexpr auto operator*(Number1 a, Quantity b) -> Quantity { return Quantity(a * b.value, unsafe); } template inline constexpr auto operator*(UnitRatio ratio, Quantity measure) -> decltype(measure * ratio) { return measure * ratio; } // ======================================================================================= // Absolute measures template class Absolute { // Wraps some other value -- typically a Quantity -- but represents a value measured based on // some absolute origin. For example, if `Duration` is a type representing a time duration, // Absolute might be a calendar date. // // Since Absolute represents measurements relative to some arbitrary origin, the only sensible // arithmetic to perform on them is addition and subtraction. // TODO(someday): Do the same automatic expansion of integer width that Quantity does? Doesn't // matter for our time use case, where we always use 64-bit anyway. Note that fixing this // would implicitly allow things like multiplying an Absolute by a UnitRatio to change its // units, which is actually totally logical and kind of neat. public: inline constexpr Absolute(MaxValue_): value(maxValue) {} inline constexpr Absolute(MinValue_): value(minValue) {} // Allow initialization from maxValue and minValue. // TODO(msvc): decltype(maxValue) and decltype(minValue) deduce unknown-type for these function // parameters, causing the compiler to complain of a duplicate constructor definition, so we // specify MaxValue_ and MinValue_ types explicitly. inline constexpr Absolute operator+(const T& other) const { return Absolute(value + other); } inline constexpr Absolute operator-(const T& other) const { return Absolute(value - other); } inline constexpr T operator-(const Absolute& other) const { return value - other.value; } inline Absolute& operator+=(const T& other) { value += other; return *this; } inline Absolute& operator-=(const T& other) { value -= other; return *this; } inline constexpr bool operator==(const Absolute& other) const { return value == other.value; } inline constexpr bool operator!=(const Absolute& other) const { return value != other.value; } inline constexpr bool operator<=(const Absolute& other) const { return value <= other.value; } inline constexpr bool operator>=(const Absolute& other) const { return value >= other.value; } inline constexpr bool operator< (const Absolute& other) const { return value < other.value; } inline constexpr bool operator> (const Absolute& other) const { return value > other.value; } private: T value; explicit constexpr Absolute(T value): value(value) {} template friend inline constexpr U origin(); }; template inline constexpr Absolute operator+(const T& a, const Absolute& b) { return b + a; } template struct UnitOf_ { typedef T Type; }; template struct UnitOf_> { typedef T Type; }; template using UnitOf = typename UnitOf_::Type; // UnitOf> is T. UnitOf is AnythingElse. template inline constexpr T origin() { return T(0 * unit>()); } // origin>() returns an Absolute of value 0. It also, intentionally, works on basic // numeric types. // ======================================================================================= // Overflow avoidance template struct BitCount_ { static constexpr uint value = BitCount_<(n >> 1), accum + 1>::value; }; template struct BitCount_<0, accum> { static constexpr uint value = accum; }; template inline constexpr uint bitCount() { return BitCount_::value; } // Number of bits required to represent the number `n`. template struct AtLeastUInt_ { static_assert(bitCountBitCount < 7, "don't know how to represent integers over 64 bits"); }; template <> struct AtLeastUInt_<0> { typedef uint8_t Type; }; template <> struct AtLeastUInt_<1> { typedef uint8_t Type; }; template <> struct AtLeastUInt_<2> { typedef uint8_t Type; }; template <> struct AtLeastUInt_<3> { typedef uint8_t Type; }; template <> struct AtLeastUInt_<4> { typedef uint16_t Type; }; template <> struct AtLeastUInt_<5> { typedef uint32_t Type; }; template <> struct AtLeastUInt_<6> { typedef uint64_t Type; }; template using AtLeastUInt = typename AtLeastUInt_()>::Type; // AtLeastUInt is an unsigned integer of at least n bits. E.g. AtLeastUInt<12> is uint16_t. // ------------------------------------------------------------------- template class BoundedConst { // A constant integer value on which we can do bit size analysis. public: BoundedConst() = default; inline constexpr uint unwrap() const { return value; } #define OP(op, check) \ template \ inline constexpr BoundedConst<(value op other)> \ operator op(BoundedConst) const { \ static_assert(check, "overflow in BoundedConst arithmetic"); \ return BoundedConst<(value op other)>(); \ } #define COMPARE_OP(op) \ template \ inline constexpr bool operator op(BoundedConst) const { \ return value op other; \ } OP(+, value + other >= value) OP(-, value - other <= value) OP(*, value * other / other == value) OP(/, true) // div by zero already errors out; no other division ever overflows OP(%, true) // mod by zero already errors out; no other modulus ever overflows OP(<<, value << other >= value) OP(>>, true) // right shift can't overflow OP(&, true) // bitwise ops can't overflow OP(|, true) // bitwise ops can't overflow COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(< ) COMPARE_OP(> ) COMPARE_OP(<=) COMPARE_OP(>=) #undef OP #undef COMPARE_OP }; template struct Unit_> { static inline constexpr BoundedConst<1> get() { return BoundedConst<1>(); } }; template struct Unit_> { static inline constexpr BoundedConst<1> get() { return BoundedConst<1>(); } }; template inline constexpr BoundedConst bounded() { return BoundedConst(); } template static constexpr uint64_t boundedAdd() { static_assert(a + b >= a, "possible overflow detected"); return a + b; } template static constexpr uint64_t boundedSub() { static_assert(a - b <= a, "possible underflow detected"); return a - b; } template static constexpr uint64_t boundedMul() { static_assert(a * b / b == a, "possible overflow detected"); return a * b; } template static constexpr uint64_t boundedLShift() { static_assert(a << b >= a, "possible overflow detected"); return a << b; } template inline constexpr BoundedConst min(BoundedConst, BoundedConst) { return bounded(); } template inline constexpr BoundedConst max(BoundedConst, BoundedConst) { return bounded(); } // We need to override min() and max() between constants because the ternary operator in the // default implementation would complain. // ------------------------------------------------------------------- template class Bounded { public: static_assert(maxN <= T(kj::maxValue), "possible overflow detected"); Bounded() = default; Bounded(const Bounded& other) = default; template ()>> inline constexpr Bounded(OtherInt value): value(value) { static_assert(OtherInt(maxValue) <= maxN, "possible overflow detected"); } template inline constexpr Bounded(const Bounded& other) : value(other.value) { static_assert(otherMax <= maxN, "possible overflow detected"); } template inline constexpr Bounded(BoundedConst) : value(otherValue) { static_assert(otherValue <= maxN, "overflow detected"); } Bounded& operator=(const Bounded& other) = default; template ()>> Bounded& operator=(OtherInt other) { static_assert(OtherInt(maxValue) <= maxN, "possible overflow detected"); value = other; return *this; } template inline Bounded& operator=(const Bounded& other) { static_assert(otherMax <= maxN, "possible overflow detected"); value = other.value; return *this; } template inline Bounded& operator=(BoundedConst) { static_assert(otherValue <= maxN, "overflow detected"); value = otherValue; return *this; } inline constexpr T unwrap() const { return value; } #define OP(op, newMax) \ template \ inline constexpr Bounded \ operator op(const Bounded& other) const { \ return Bounded(value op other.value, unsafe); \ } #define COMPARE_OP(op) \ template \ inline constexpr bool operator op(const Bounded& other) const { \ return value op other.value; \ } OP(+, (boundedAdd())) OP(*, (boundedMul())) OP(/, maxN) OP(%, otherMax - 1) // operator- is intentionally omitted because we mostly use this with unsigned types, and // subtraction requires proof that subtrahend is not greater than the minuend. COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(< ) COMPARE_OP(> ) COMPARE_OP(<=) COMPARE_OP(>=) #undef OP #undef COMPARE_OP template inline Bounded assertMax(ErrorFunc&& func) const { // Assert that the number is no more than `newMax`. Otherwise, call `func`. static_assert(newMax < maxN, "this bounded size assertion is redundant"); if (KJ_UNLIKELY(value > newMax)) func(); return Bounded(value, unsafe); } template inline Bounded subtractChecked( const Bounded& other, ErrorFunc&& func) const { // Subtract a number, calling func() if the result would underflow. if (KJ_UNLIKELY(value < other.value)) func(); return Bounded(value - other.value, unsafe); } template inline Bounded subtractChecked( BoundedConst, ErrorFunc&& func) const { // Subtract a number, calling func() if the result would underflow. static_assert(otherValue <= maxN, "underflow detected"); if (KJ_UNLIKELY(value < otherValue)) func(); return Bounded(value - otherValue, unsafe); } template inline Maybe> trySubtract( const Bounded& other) const { // Subtract a number, calling func() if the result would underflow. if (value < other.value) { return nullptr; } else { return Bounded(value - other.value, unsafe); } } template inline Maybe> trySubtract(BoundedConst) const { // Subtract a number, calling func() if the result would underflow. if (value < otherValue) { return nullptr; } else { return Bounded(value - otherValue, unsafe); } } inline constexpr Bounded(T value, decltype(unsafe)): value(value) {} template inline constexpr Bounded(Bounded value, decltype(unsafe)) : value(value.value) {} // Mainly for internal use. // // Only use these as a last resort, with ample commentary on why you think it's safe. private: T value; template friend class Bounded; }; template inline constexpr Bounded bounded(Number value) { return Bounded(value, unsafe); } inline constexpr Bounded<1, uint8_t> bounded(bool value) { return Bounded<1, uint8_t>(value, unsafe); } template inline constexpr Bounded(), Number> assumeBits(Number value) { return Bounded(), Number>(value, unsafe); } template inline constexpr Bounded(), T> assumeBits(Bounded value) { return Bounded(), T>(value, unsafe); } template inline constexpr auto assumeBits(Quantity value) -> Quantity(value / unit>())), Unit> { return Quantity(value / unit>())), Unit>( assumeBits(value / unit>()), unsafe); } template inline constexpr Bounded assumeMax(Number value) { return Bounded(value, unsafe); } template inline constexpr Bounded assumeMax(Bounded value) { return Bounded(value, unsafe); } template inline constexpr auto assumeMax(Quantity value) -> Quantity(value / unit>())), Unit> { return Quantity(value / unit>())), Unit>( assumeMax(value / unit>()), unsafe); } template inline constexpr Bounded assumeMax(BoundedConst, Number value) { return assumeMax(value); } template inline constexpr Bounded assumeMax(BoundedConst, Bounded value) { return assumeMax(value); } template inline constexpr auto assumeMax(Quantity, Unit>, Quantity value) -> decltype(assumeMax(value)) { return assumeMax(value); } template inline Bounded assertMax(Bounded value, ErrorFunc&& errorFunc) { // Assert that the bounded value is less than or equal to the given maximum, calling errorFunc() // if not. static_assert(newMax < maxN, "this bounded size assertion is redundant"); return value.template assertMax(kj::fwd(errorFunc)); } template inline Quantity, Unit> assertMax( Quantity, Unit> value, ErrorFunc&& errorFunc) { // Assert that the bounded value is less than or equal to the given maximum, calling errorFunc() // if not. static_assert(newMax < maxN, "this bounded size assertion is redundant"); return (value / unit()).template assertMax( kj::fwd(errorFunc)) * unit(); } template inline Bounded assertMax( BoundedConst, Bounded value, ErrorFunc&& errorFunc) { return assertMax(value, kj::mv(errorFunc)); } template inline Quantity, Unit> assertMax( Quantity, Unit>, Quantity, Unit> value, ErrorFunc&& errorFunc) { return assertMax(value, kj::mv(errorFunc)); } template inline Bounded(), T> assertMaxBits( Bounded value, ErrorFunc&& errorFunc = ErrorFunc()) { // Assert that the bounded value requires no more than the given number of bits, calling // errorFunc() if not. return assertMax()>(value, kj::fwd(errorFunc)); } template inline Quantity(), T>, Unit> assertMaxBits( Quantity, Unit> value, ErrorFunc&& errorFunc = ErrorFunc()) { // Assert that the bounded value requires no more than the given number of bits, calling // errorFunc() if not. return assertMax()>(value, kj::fwd(errorFunc)); } template inline constexpr Bounded upgradeBound(Bounded value) { return value; } template inline constexpr Quantity, Unit> upgradeBound( Quantity, Unit> value) { return value; } template inline auto subtractChecked(Bounded value, Other other, ErrorFunc&& errorFunc) -> decltype(value.subtractChecked(other, kj::fwd(errorFunc))) { return value.subtractChecked(other, kj::fwd(errorFunc)); } template inline auto subtractChecked(Quantity value, Quantity other, ErrorFunc&& errorFunc) -> Quantity(errorFunc))), Unit> { return subtractChecked(value / unit>(), other / unit>(), kj::fwd(errorFunc)) * unit>(); } template inline auto trySubtract(Bounded value, Other other) -> decltype(value.trySubtract(other)) { return value.trySubtract(other); } template inline auto trySubtract(Quantity value, Quantity other) -> Maybe> { return trySubtract(value / unit>(), other / unit>()) .map([](decltype(subtractChecked(T(), U(), int())) x) { return x * unit>(); }); } template inline constexpr Bounded> min(Bounded a, Bounded b) { return Bounded>(kj::min(a.unwrap(), b.unwrap()), unsafe); } template inline constexpr Bounded> max(Bounded a, Bounded b) { return Bounded>(kj::max(a.unwrap(), b.unwrap()), unsafe); } // We need to override min() and max() because: // 1) WiderType<> might not choose the correct bounds. // 2) One of the two sides of the ternary operator in the default implementation would fail to // typecheck even though it is OK in practice. // ------------------------------------------------------------------- // Operators between Bounded and BoundedConst #define OP(op, newMax) \ template \ inline constexpr Bounded<(newMax), decltype(T() op uint())> operator op( \ Bounded value, BoundedConst) { \ return Bounded<(newMax), decltype(T() op uint())>(value.unwrap() op cvalue, unsafe); \ } #define REVERSE_OP(op, newMax) \ template \ inline constexpr Bounded<(newMax), decltype(uint() op T())> operator op( \ BoundedConst, Bounded value) { \ return Bounded<(newMax), decltype(uint() op T())>(cvalue op value.unwrap(), unsafe); \ } #define COMPARE_OP(op) \ template \ inline constexpr bool operator op(Bounded value, BoundedConst) { \ return value.unwrap() op cvalue; \ } \ template \ inline constexpr bool operator op(BoundedConst, Bounded value) { \ return cvalue op value.unwrap(); \ } OP(+, (boundedAdd())) REVERSE_OP(+, (boundedAdd())) OP(*, (boundedMul())) REVERSE_OP(*, (boundedAdd())) OP(/, maxN / cvalue) REVERSE_OP(/, cvalue) // denominator could be 1 OP(%, cvalue - 1) REVERSE_OP(%, maxN - 1) OP(<<, (boundedLShift())) REVERSE_OP(<<, (boundedLShift())) OP(>>, maxN >> cvalue) REVERSE_OP(>>, cvalue >> maxN) OP(&, maxValueForBits()>() & cvalue) REVERSE_OP(&, maxValueForBits()>() & cvalue) OP(|, maxN | cvalue) REVERSE_OP(|, maxN | cvalue) COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(< ) COMPARE_OP(> ) COMPARE_OP(<=) COMPARE_OP(>=) #undef OP #undef REVERSE_OP #undef COMPARE_OP template inline constexpr Bounded operator-(BoundedConst, Bounded value) { // We allow subtraction of a variable from a constant only if the constant is greater than or // equal to the maximum possible value of the variable. Since the variable could be zero, the // result can be as large as the constant. // // We do not allow subtraction of a constant from a variable because there's never a guarantee it // won't underflow (unless the constant is zero, which is silly). static_assert(cvalue >= maxN, "possible underflow detected"); return Bounded(cvalue - value.unwrap(), unsafe); } template inline constexpr Bounded min(Bounded a, BoundedConst) { return Bounded(kj::min(b, a.unwrap()), unsafe); } template inline constexpr Bounded min(BoundedConst, Bounded a) { return Bounded(kj::min(a.unwrap(), b), unsafe); } template inline constexpr Bounded max(Bounded a, BoundedConst) { return Bounded(kj::max(b, a.unwrap()), unsafe); } template inline constexpr Bounded max(BoundedConst, Bounded a) { return Bounded(kj::max(a.unwrap(), b), unsafe); } // We need to override min() between a Bounded and a constant since: // 1) WiderType<> might choose BoundedConst over a 1-byte Bounded, which is wrong. // 2) To clamp the bounds of the output type. // 3) Same ternary operator typechecking issues. // ------------------------------------------------------------------- template class SafeUnwrapper { public: inline explicit constexpr SafeUnwrapper(Bounded value): value(value.unwrap()) {} template ()>> inline constexpr operator U() const { static_assert(maxN <= U(maxValue), "possible truncation detected"); return value; } inline constexpr operator bool() const { static_assert(maxN <= 1, "possible truncation detected"); return value; } private: T value; }; template inline constexpr SafeUnwrapper unbound(Bounded bounded) { // Unwraps the bounded value, returning a value that can be implicitly cast to any integer type. // If this implicit cast could truncate, a compile-time error will be raised. return SafeUnwrapper(bounded); } template class SafeConstUnwrapper { public: template ()>> inline constexpr operator T() const { static_assert(value <= T(maxValue), "this operation will truncate"); return value; } inline constexpr operator bool() const { static_assert(value <= 1, "this operation will truncate"); return value; } }; template inline constexpr SafeConstUnwrapper unbound(BoundedConst) { return SafeConstUnwrapper(); } template inline constexpr T unboundAs(U value) { return unbound(value); } template inline constexpr T unboundMax(Bounded value) { // Explicitly ungaurd expecting a value that is at most `maxN`. static_assert(maxN <= requestedMax, "possible overflow detected"); return value.unwrap(); } template inline constexpr uint unboundMax(BoundedConst) { // Explicitly ungaurd expecting a value that is at most `maxN`. static_assert(value <= requestedMax, "overflow detected"); return value; } template inline constexpr auto unboundMaxBits(T value) -> decltype(unboundMax()>(value)) { // Explicitly ungaurd expecting a value that fits into `bits` bits. return unboundMax()>(value); } #define OP(op) \ template \ inline constexpr auto operator op(T a, SafeUnwrapper b) -> decltype(a op (T)b) { \ return a op (AtLeastUInt)b; \ } \ template \ inline constexpr auto operator op(SafeUnwrapper b, T a) -> decltype((T)b op a) { \ return (AtLeastUInt)b op a; \ } \ template \ inline constexpr auto operator op(T a, SafeConstUnwrapper b) -> decltype(a op (T)b) { \ return a op (AtLeastUInt)b; \ } \ template \ inline constexpr auto operator op(SafeConstUnwrapper b, T a) -> decltype((T)b op a) { \ return (AtLeastUInt)b op a; \ } OP(+) OP(-) OP(*) OP(/) OP(%) OP(<<) OP(>>) OP(&) OP(|) OP(==) OP(!=) OP(<=) OP(>=) OP(<) OP(>) #undef OP // ------------------------------------------------------------------- template class Range> { public: inline constexpr Range(Bounded begin, Bounded end) : inner(unbound(begin), unbound(end)) {} inline explicit constexpr Range(Bounded end) : inner(unbound(end)) {} class Iterator { public: Iterator() = default; inline explicit Iterator(typename Range::Iterator inner): inner(inner) {} inline Bounded operator* () const { return Bounded(*inner, unsafe); } inline Iterator& operator++() { ++inner; return *this; } inline bool operator==(const Iterator& other) const { return inner == other.inner; } inline bool operator!=(const Iterator& other) const { return inner != other.inner; } private: typename Range::Iterator inner; }; inline Iterator begin() const { return Iterator(inner.begin()); } inline Iterator end() const { return Iterator(inner.end()); } private: Range inner; }; template class Range> { public: inline constexpr Range(Quantity begin, Quantity end) : inner(begin / unit>(), end / unit>()) {} inline explicit constexpr Range(Quantity end) : inner(end / unit>()) {} class Iterator { public: Iterator() = default; inline explicit Iterator(typename Range::Iterator inner): inner(inner) {} inline Quantity operator* () const { return *inner * unit>(); } inline Iterator& operator++() { ++inner; return *this; } inline bool operator==(const Iterator& other) const { return inner == other.inner; } inline bool operator!=(const Iterator& other) const { return inner != other.inner; } private: typename Range::Iterator inner; }; inline Iterator begin() const { return Iterator(inner.begin()); } inline Iterator end() const { return Iterator(inner.end()); } private: Range inner; }; template inline constexpr Range> zeroTo(BoundedConst end) { return Range>(end); } template inline constexpr Range, Unit>> zeroTo(Quantity, Unit> end) { return Range, Unit>>(end); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/memory.h0000644000175000017500000004765213650101756017646 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "common.h" KJ_BEGIN_HEADER namespace kj { template inline constexpr bool _kj_internal_isPolymorphic(T*) { // If you get a compiler error here complaining that T is incomplete, it's because you are trying // to use kj::Own with a type that has only been forward-declared. Since KJ doesn't know if // the type might be involved in inheritance (especially multiple inheritance), it doesn't know // how to correctly call the disposer to destroy the type, since the object's true memory address // may differ from the address used to point to a superclass. // // However, if you know for sure that T is NOT polymorphic (i.e. it doesn't have a vtable and // isn't involved in inheritance), then you can use KJ_DECLARE_NON_POLYMORPHIC(T) to declare this // to KJ without actually completing the type. Place this macro invocation either in the global // scope, or in the same namespace as T is defined. return __is_polymorphic(T); } #define KJ_DECLARE_NON_POLYMORPHIC(...) \ inline constexpr bool _kj_internal_isPolymorphic(__VA_ARGS__*) { \ return false; \ } // If you want to use kj::Own for an incomplete type T that you know is not polymorphic, then // write `KJ_DECLARE_NON_POLYMORPHIC(T)` either at the global scope or in the same namespace as // T is declared. // // This also works for templates, e.g.: // // template // struct MyType; // template // KJ_DECLARE_NON_POLYMORPHIC(MyType) namespace _ { // private template struct RefOrVoid_ { typedef T& Type; }; template <> struct RefOrVoid_ { typedef void Type; }; template <> struct RefOrVoid_ { typedef void Type; }; template using RefOrVoid = typename RefOrVoid_::Type; // Evaluates to T&, unless T is `void`, in which case evaluates to `void`. // // This is a hack needed to avoid defining Own as a totally separate class. template struct CastToVoid_; template struct CastToVoid_ { static void* apply(T* ptr) { return static_cast(ptr); } static const void* applyConst(T* ptr) { const T* cptr = ptr; return static_cast(cptr); } }; template struct CastToVoid_ { static void* apply(T* ptr) { return dynamic_cast(ptr); } static const void* applyConst(T* ptr) { const T* cptr = ptr; return dynamic_cast(cptr); } }; template void* castToVoid(T* ptr) { return CastToVoid_::apply(ptr); } template const void* castToConstVoid(T* ptr) { return CastToVoid_::applyConst(ptr); } } // namespace _ (private) // ======================================================================================= // Disposer -- Implementation details. class Disposer { // Abstract interface for a thing that "disposes" of objects, where "disposing" usually means // calling the destructor followed by freeing the underlying memory. `Own` encapsulates an // object pointer with corresponding Disposer. // // Few developers will ever touch this interface. It is primarily useful for those implementing // custom memory allocators. protected: // Do not declare a destructor, as doing so will force a global initializer for each HeapDisposer // instance. Eww! virtual void disposeImpl(void* pointer) const = 0; // Disposes of the object, given a pointer to the beginning of the object. If the object is // polymorphic, this pointer is determined by dynamic_cast(). For non-polymorphic types, // Own does not allow any casting, so the pointer exactly matches the original one given to // Own. public: template void dispose(T* object) const; // Helper wrapper around disposeImpl(). // // If T is polymorphic, calls `disposeImpl(dynamic_cast(object))`, otherwise calls // `disposeImpl(implicitCast(object))`. // // Callers must not call dispose() on the same pointer twice, even if the first call throws // an exception. private: template struct Dispose_; }; template class DestructorOnlyDisposer: public Disposer { // A disposer that merely calls the type's destructor and nothing else. public: static const DestructorOnlyDisposer instance; void disposeImpl(void* pointer) const override { reinterpret_cast(pointer)->~T(); } }; template const DestructorOnlyDisposer DestructorOnlyDisposer::instance = DestructorOnlyDisposer(); class NullDisposer: public Disposer { // A disposer that does nothing. public: static const NullDisposer instance; void disposeImpl(void* pointer) const override {} }; // ======================================================================================= // Own -- An owned pointer. template class Own { // A transferrable title to a T. When an Own goes out of scope, the object's Disposer is // called to dispose of it. An Own can be efficiently passed by move, without relocating the // underlying object; this transfers ownership. // // This is much like std::unique_ptr, except: // - You cannot release(). An owned object is not necessarily allocated with new (see next // point), so it would be hard to use release() correctly. // - The deleter is made polymorphic by virtual call rather than by template. This is much // more powerful -- it allows the use of custom allocators, freelists, etc. This could // _almost_ be accomplished with unique_ptr by forcing everyone to use something like // std::unique_ptr, except that things get hairy in the presence of multiple // inheritance and upcasting, and anyway if you force everyone to use a custom deleter // then you've lost any benefit to interoperating with the "standard" unique_ptr. public: KJ_DISALLOW_COPY(Own); inline Own(): disposer(nullptr), ptr(nullptr) {} inline Own(Own&& other) noexcept : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; } inline Own(Own>&& other) noexcept : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; } template ()>> inline Own(Own&& other) noexcept : disposer(other.disposer), ptr(cast(other.ptr)) { other.ptr = nullptr; } inline Own(T* ptr, const Disposer& disposer) noexcept: disposer(&disposer), ptr(ptr) {} ~Own() noexcept(false) { dispose(); } inline Own& operator=(Own&& other) { // Move-assignnment operator. // Careful, this might own `other`. Therefore we have to transfer the pointers first, then // dispose. const Disposer* disposerCopy = disposer; T* ptrCopy = ptr; disposer = other.disposer; ptr = other.ptr; other.ptr = nullptr; if (ptrCopy != nullptr) { disposerCopy->dispose(const_cast*>(ptrCopy)); } return *this; } inline Own& operator=(decltype(nullptr)) { dispose(); return *this; } template Own attach(Attachments&&... attachments) KJ_WARN_UNUSED_RESULT; // Returns an Own which points to the same object but which also ensures that all values // passed to `attachments` remain alive until after this object is destroyed. Normally // `attachments` are other Owns pointing to objects that this one depends on. // // Note that attachments will eventually be destroyed in the order they are listed. Hence, // foo.attach(bar, baz) is equivalent to (but more efficient than) foo.attach(bar).attach(baz). template Own downcast() { // Downcast the pointer to Own, destroying the original pointer. If this pointer does not // actually point at an instance of U, the results are undefined (throws an exception in debug // mode if RTTI is enabled, otherwise you're on your own). Own result; if (ptr != nullptr) { result.ptr = &kj::downcast(*ptr); result.disposer = disposer; ptr = nullptr; } return result; } #define NULLCHECK KJ_IREQUIRE(ptr != nullptr, "null Own<> dereference") inline T* operator->() { NULLCHECK; return ptr; } inline const T* operator->() const { NULLCHECK; return ptr; } inline _::RefOrVoid operator*() { NULLCHECK; return *ptr; } inline _::RefOrVoid operator*() const { NULLCHECK; return *ptr; } #undef NULLCHECK inline T* get() { return ptr; } inline const T* get() const { return ptr; } inline operator T*() { return ptr; } inline operator const T*() const { return ptr; } private: const Disposer* disposer; // Only valid if ptr != nullptr. T* ptr; inline explicit Own(decltype(nullptr)): disposer(nullptr), ptr(nullptr) {} inline bool operator==(decltype(nullptr)) { return ptr == nullptr; } inline bool operator!=(decltype(nullptr)) { return ptr != nullptr; } // Only called by Maybe>. inline void dispose() { // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly // dispose again. T* ptrCopy = ptr; if (ptrCopy != nullptr) { ptr = nullptr; disposer->dispose(const_cast*>(ptrCopy)); } } template static inline T* cast(U* ptr) { static_assert(_kj_internal_isPolymorphic((T*)nullptr), "Casting owned pointers requires that the target type is polymorphic."); return ptr; } template friend class Own; friend class Maybe>; }; template <> template inline void* Own::cast(U* ptr) { return _::castToVoid(ptr); } template <> template inline const void* Own::cast(U* ptr) { return _::castToConstVoid(ptr); } namespace _ { // private template class OwnOwn { public: inline OwnOwn(Own&& value) noexcept: value(kj::mv(value)) {} inline Own& operator*() & { return value; } inline const Own& operator*() const & { return value; } inline Own&& operator*() && { return kj::mv(value); } inline const Own&& operator*() const && { return kj::mv(value); } inline Own* operator->() { return &value; } inline const Own* operator->() const { return &value; } inline operator Own*() { return value ? &value : nullptr; } inline operator const Own*() const { return value ? &value : nullptr; } private: Own value; }; template OwnOwn readMaybe(Maybe>&& maybe) { return OwnOwn(kj::mv(maybe.ptr)); } template Own* readMaybe(Maybe>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; } template const Own* readMaybe(const Maybe>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; } } // namespace _ (private) template class Maybe> { public: inline Maybe(): ptr(nullptr) {} inline Maybe(Own&& t) noexcept: ptr(kj::mv(t)) {} inline Maybe(Maybe&& other) noexcept: ptr(kj::mv(other.ptr)) {} template inline Maybe(Maybe>&& other): ptr(mv(other.ptr)) {} template inline Maybe(Own&& other): ptr(mv(other)) {} inline Maybe(decltype(nullptr)) noexcept: ptr(nullptr) {} inline Own& emplace(Own value) { // Assign the Maybe to the given value and return the content. This avoids the need to do a // KJ_ASSERT_NONNULL() immediately after setting the Maybe just to read it back again. ptr = kj::mv(value); return ptr; } inline operator Maybe() { return ptr.get(); } inline operator Maybe() const { return ptr.get(); } inline Maybe& operator=(Maybe&& other) { ptr = kj::mv(other.ptr); return *this; } inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; } inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; } Own& orDefault(Own& defaultValue) { if (ptr == nullptr) { return defaultValue; } else { return ptr; } } const Own& orDefault(const Own& defaultValue) const { if (ptr == nullptr) { return defaultValue; } else { return ptr; } } template auto map(Func&& f) & -> Maybe&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(ptr); } } template auto map(Func&& f) const & -> Maybe&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(ptr); } } template auto map(Func&& f) && -> Maybe&&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(kj::mv(ptr)); } } template auto map(Func&& f) const && -> Maybe&&>()))> { if (ptr == nullptr) { return nullptr; } else { return f(kj::mv(ptr)); } } private: Own ptr; template friend class Maybe; template friend _::OwnOwn _::readMaybe(Maybe>&& maybe); template friend Own* _::readMaybe(Maybe>& maybe); template friend const Own* _::readMaybe(const Maybe>& maybe); }; namespace _ { // private template class HeapDisposer final: public Disposer { public: virtual void disposeImpl(void* pointer) const override { delete reinterpret_cast(pointer); } static const HeapDisposer instance; }; template const HeapDisposer HeapDisposer::instance = HeapDisposer(); } // namespace _ (private) template Own heap(Params&&... params) { // heap(...) allocates a T on the heap, forwarding the parameters to its constructor. The // exact heap implementation is unspecified -- for now it is operator new, but you should not // assume this. (Since we know the object size at delete time, we could actually implement an // allocator that is more efficient than operator new.) return Own(new T(kj::fwd(params)...), _::HeapDisposer::instance); } template Own> heap(T&& orig) { // Allocate a copy (or move) of the argument on the heap. // // The purpose of this overload is to allow you to omit the template parameter as there is only // one argument and the purpose is to copy it. typedef Decay T2; return Own(new T2(kj::fwd(orig)), _::HeapDisposer::instance); } template Own> attachVal(T&& value, Attachments&&... attachments); // Returns an Own that takes ownership of `value` and `attachments`, and points to `value`. // // This is equivalent to heap(value).attach(attachments), but only does one allocation rather than // two. template Own attachRef(T& value, Attachments&&... attachments); // Like attach() but `value` is not moved; the resulting Own points to its existing location. // This is preferred if `value` is already owned by one of `attachments`. // // This is equivalent to Own(&value, kj::NullDisposer::instance).attach(attachments), but // is easier to write and allocates slightly less memory. // ======================================================================================= // SpaceFor -- assists in manual allocation template class SpaceFor { // A class which has the same size and alignment as T but does not call its constructor or // destructor automatically. Instead, call construct() to construct a T in the space, which // returns an Own which will take care of calling T's destructor later. public: inline SpaceFor() {} inline ~SpaceFor() {} template Own construct(Params&&... params) { ctor(value, kj::fwd(params)...); return Own(&value, DestructorOnlyDisposer::instance); } private: union { T value; }; }; // ======================================================================================= // Inline implementation details template struct Disposer::Dispose_ { static void dispose(T* object, const Disposer& disposer) { // Note that dynamic_cast does not require RTTI to be enabled, because the offset to // the top of the object is in the vtable -- as it obviously needs to be to correctly implement // operator delete. disposer.disposeImpl(dynamic_cast(object)); } }; template struct Disposer::Dispose_ { static void dispose(T* object, const Disposer& disposer) { disposer.disposeImpl(static_cast(object)); } }; template void Disposer::dispose(T* object) const { Dispose_::dispose(object, *this); } namespace _ { // private template struct OwnedBundle; template <> struct OwnedBundle<> {}; template struct OwnedBundle: public OwnedBundle { OwnedBundle(First&& first, Rest&&... rest) : OwnedBundle(kj::fwd(rest)...), first(kj::fwd(first)) {} // Note that it's intentional that `first` is destroyed before `rest`. This way, doing // ptr.attach(foo, bar, baz) is equivalent to ptr.attach(foo).attach(bar).attach(baz) in terms // of destruction order (although the former does fewer allocations). Decay first; }; template struct DisposableOwnedBundle final: public Disposer, public OwnedBundle { DisposableOwnedBundle(T&&... values): OwnedBundle(kj::fwd(values)...) {} void disposeImpl(void* pointer) const override { delete this; } }; } // namespace _ (private) template template Own Own::attach(Attachments&&... attachments) { T* ptrCopy = ptr; KJ_IREQUIRE(ptrCopy != nullptr, "cannot attach to null pointer"); // HACK: If someone accidentally calls .attach() on a null pointer in opt mode, try our best to // accomplish reasonable behavior: We turn the pointer non-null but still invalid, so that the // disposer will still be called when the pointer goes out of scope. if (ptrCopy == nullptr) ptrCopy = reinterpret_cast(1); auto bundle = new _::DisposableOwnedBundle, Attachments...>( kj::mv(*this), kj::fwd(attachments)...); return Own(ptrCopy, *bundle); } template Own attachRef(T& value, Attachments&&... attachments) { auto bundle = new _::DisposableOwnedBundle(kj::fwd(attachments)...); return Own(&value, *bundle); } template Own> attachVal(T&& value, Attachments&&... attachments) { auto bundle = new _::DisposableOwnedBundle( kj::fwd(value), kj::fwd(attachments)...); return Own>(&bundle->first, *bundle); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/refcount.h0000644000175000017500000002012313650101756020143 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" #if _MSC_VER #if _MSC_VER < 1910 #include #else #include #endif #endif KJ_BEGIN_HEADER namespace kj { // ======================================================================================= // Non-atomic (thread-unsafe) refcounting class Refcounted: private Disposer { // Subclass this to create a class that contains a reference count. Then, use // `kj::refcounted()` to allocate a new refcounted pointer. // // Do NOT use this lightly. Refcounting is a crutch. Good designs should strive to make object // ownership clear, so that refcounting is not necessary. All that said, reference counting can // sometimes simplify code that would otherwise become convoluted with explicit ownership, even // when ownership relationships are clear at an abstract level. // // NOT THREADSAFE: This refcounting implementation assumes that an object's references are // manipulated only in one thread, because atomic (thread-safe) refcounting is surprisingly slow. // // In general, abstract classes should _not_ subclass this. The concrete class at the bottom // of the hierarchy should be the one to decide how it implements refcounting. Interfaces should // expose only an `addRef()` method that returns `Own`. There are two reasons for // this rule: // 1. Interfaces would need to virtually inherit Refcounted, otherwise two refcounted interfaces // could not be inherited by the same subclass. Virtual inheritance is awkward and // inefficient. // 2. An implementation may decide that it would rather return a copy than a refcount, or use // some other strategy. // // TODO(cleanup): Rethink above. Virtual inheritance is not necessarily that bad. OTOH, a // virtual function call for every refcount is sad in its own way. A Ref type to replace // Own could also be nice. public: Refcounted() = default; virtual ~Refcounted() noexcept(false); KJ_DISALLOW_COPY(Refcounted); inline bool isShared() const { return refcount > 1; } // Check if there are multiple references to this object. This is sometimes useful for deciding // whether it's safe to modify the object vs. make a copy. private: mutable uint refcount = 0; // "mutable" because disposeImpl() is const. Bleh. void disposeImpl(void* pointer) const override; template static Own addRefInternal(T* object); template friend Own addRef(T& object); template friend Own refcounted(Params&&... params); }; template inline Own refcounted(Params&&... params) { // Allocate a new refcounted instance of T, passing `params` to its constructor. Returns an // initial reference to the object. More references can be created with `kj::addRef()`. return Refcounted::addRefInternal(new T(kj::fwd(params)...)); } template Own addRef(T& object) { // Return a new reference to `object`, which must subclass Refcounted and have been allocated // using `kj::refcounted<>()`. It is suggested that subclasses implement a non-static addRef() // method which wraps this and returns the appropriate type. KJ_IREQUIRE(object.Refcounted::refcount > 0, "Object not allocated with kj::refcounted()."); return Refcounted::addRefInternal(&object); } template Own Refcounted::addRefInternal(T* object) { Refcounted* refcounted = object; ++refcounted->refcount; return Own(object, *refcounted); } // ======================================================================================= // Atomic (thread-safe) refcounting // // Warning: Atomic ops are SLOW. #if _MSC_VER #if _M_ARM #define KJ_MSVC_INTERLOCKED(OP, MEM) _Interlocked##OP##_##MEM #else #define KJ_MSVC_INTERLOCKED(OP, MEM) _Interlocked##OP #endif #endif class AtomicRefcounted: private kj::Disposer { public: AtomicRefcounted() = default; virtual ~AtomicRefcounted() noexcept(false); KJ_DISALLOW_COPY(AtomicRefcounted); inline bool isShared() const { #if _MSC_VER return KJ_MSVC_INTERLOCKED(Or, acq)(&refcount, 0) > 1; #else return __atomic_load_n(&refcount, __ATOMIC_ACQUIRE) > 1; #endif } private: #if _MSC_VER mutable volatile long refcount = 0; #else mutable volatile uint refcount = 0; #endif bool addRefWeakInternal() const; void disposeImpl(void* pointer) const override; template static kj::Own addRefInternal(T* object); template static kj::Own addRefInternal(const T* object); template friend kj::Own atomicAddRef(T& object); template friend kj::Own atomicAddRef(const T& object); template friend kj::Maybe> atomicAddRefWeak(const T& object); template friend kj::Own atomicRefcounted(Params&&... params); }; template inline kj::Own atomicRefcounted(Params&&... params) { return AtomicRefcounted::addRefInternal(new T(kj::fwd(params)...)); } template kj::Own atomicAddRef(T& object) { KJ_IREQUIRE(object.AtomicRefcounted::refcount > 0, "Object not allocated with kj::atomicRefcounted()."); return AtomicRefcounted::addRefInternal(&object); } template kj::Own atomicAddRef(const T& object) { KJ_IREQUIRE(object.AtomicRefcounted::refcount > 0, "Object not allocated with kj::atomicRefcounted()."); return AtomicRefcounted::addRefInternal(&object); } template kj::Maybe> atomicAddRefWeak(const T& object) { // Try to addref an object whose refcount could have already reached zero in another thread, and // whose destructor could therefore already have started executing. The destructor must contain // some synchronization that guarantees that said destructor has not yet completed when // attomicAddRefWeak() is called (so that the object is still valid). Since the destructor cannot // be canceled once it has started, in the case that it has already started, this function // returns nullptr. const AtomicRefcounted* refcounted = &object; if (refcounted->addRefWeakInternal()) { return kj::Own(&object, *refcounted); } else { return nullptr; } } template kj::Own AtomicRefcounted::addRefInternal(T* object) { AtomicRefcounted* refcounted = object; #if _MSC_VER KJ_MSVC_INTERLOCKED(Increment, nf)(&refcounted->refcount); #else __atomic_add_fetch(&refcounted->refcount, 1, __ATOMIC_RELAXED); #endif return kj::Own(object, *refcounted); } template kj::Own AtomicRefcounted::addRefInternal(const T* object) { const AtomicRefcounted* refcounted = object; #if _MSC_VER KJ_MSVC_INTERLOCKED(Increment, nf)(&refcounted->refcount); #else __atomic_add_fetch(&refcounted->refcount, 1, __ATOMIC_RELAXED); #endif return kj::Own(object, *refcounted); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/array.h0000644000175000017500000007355413650101756017454 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" #include #include KJ_BEGIN_HEADER namespace kj { // ======================================================================================= // ArrayDisposer -- Implementation details. class ArrayDisposer { // Much like Disposer from memory.h. protected: // Do not declare a destructor, as doing so will force a global initializer for // HeapArrayDisposer::instance. virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const = 0; // Disposes of the array. `destroyElement` invokes the destructor of each element, or is nullptr // if the elements have trivial destructors. `capacity` is the amount of space that was // allocated while `elementCount` is the number of elements that were actually constructed; // these are always the same number for Array but may be different when using ArrayBuilder. public: template void dispose(T* firstElement, size_t elementCount, size_t capacity) const; // Helper wrapper around disposeImpl(). // // Callers must not call dispose() on the same array twice, even if the first call throws // an exception. private: template struct Dispose_; }; class ExceptionSafeArrayUtil { // Utility class that assists in constructing or destroying elements of an array, where the // constructor or destructor could throw exceptions. In case of an exception, // ExceptionSafeArrayUtil's destructor will call destructors on all elements that have been // constructed but not destroyed. Remember that destructors that throw exceptions are required // to use UnwindDetector to detect unwind and avoid exceptions in this case. Therefore, no more // than one exception will be thrown (and the program will not terminate). public: inline ExceptionSafeArrayUtil(void* ptr, size_t elementSize, size_t constructedElementCount, void (*destroyElement)(void*)) : pos(reinterpret_cast(ptr) + elementSize * constructedElementCount), elementSize(elementSize), constructedElementCount(constructedElementCount), destroyElement(destroyElement) {} KJ_DISALLOW_COPY(ExceptionSafeArrayUtil); inline ~ExceptionSafeArrayUtil() noexcept(false) { if (constructedElementCount > 0) destroyAll(); } void construct(size_t count, void (*constructElement)(void*)); // Construct the given number of elements. void destroyAll(); // Destroy all elements. Call this immediately before ExceptionSafeArrayUtil goes out-of-scope // to ensure that one element throwing an exception does not prevent the others from being // destroyed. void release() { constructedElementCount = 0; } // Prevent ExceptionSafeArrayUtil's destructor from destroying the constructed elements. // Call this after you've successfully finished constructing. private: byte* pos; size_t elementSize; size_t constructedElementCount; void (*destroyElement)(void*); }; class DestructorOnlyArrayDisposer: public ArrayDisposer { public: static const DestructorOnlyArrayDisposer instance; void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const override; }; class NullArrayDisposer: public ArrayDisposer { // An ArrayDisposer that does nothing. Can be used to construct a fake Arrays that doesn't // actually own its content. public: static const NullArrayDisposer instance; void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const override; }; // ======================================================================================= // Array template class Array { // An owned array which will automatically be disposed of (using an ArrayDisposer) in the // destructor. Can be moved, but not copied. Much like Own, but for arrays rather than // single objects. public: inline Array(): ptr(nullptr), size_(0), disposer(nullptr) {} inline Array(decltype(nullptr)): ptr(nullptr), size_(0), disposer(nullptr) {} inline Array(Array&& other) noexcept : ptr(other.ptr), size_(other.size_), disposer(other.disposer) { other.ptr = nullptr; other.size_ = 0; } inline Array(Array>&& other) noexcept : ptr(other.ptr), size_(other.size_), disposer(other.disposer) { other.ptr = nullptr; other.size_ = 0; } inline Array(T* firstElement, size_t size, const ArrayDisposer& disposer) : ptr(firstElement), size_(size), disposer(&disposer) {} KJ_DISALLOW_COPY(Array); inline ~Array() noexcept { dispose(); } inline operator ArrayPtr() { return ArrayPtr(ptr, size_); } inline operator ArrayPtr() const { return ArrayPtr(ptr, size_); } inline ArrayPtr asPtr() { return ArrayPtr(ptr, size_); } inline ArrayPtr asPtr() const { return ArrayPtr(ptr, size_); } inline size_t size() const { return size_; } inline T& operator[](size_t index) { KJ_IREQUIRE(index < size_, "Out-of-bounds Array access."); return ptr[index]; } inline const T& operator[](size_t index) const { KJ_IREQUIRE(index < size_, "Out-of-bounds Array access."); return ptr[index]; } inline const T* begin() const { return ptr; } inline const T* end() const { return ptr + size_; } inline const T& front() const { return *ptr; } inline const T& back() const { return *(ptr + size_ - 1); } inline T* begin() { return ptr; } inline T* end() { return ptr + size_; } inline T& front() { return *ptr; } inline T& back() { return *(ptr + size_ - 1); } template inline bool operator==(const U& other) const { return asPtr() == other; } template inline bool operator!=(const U& other) const { return asPtr() != other; } inline ArrayPtr slice(size_t start, size_t end) { KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice()."); return ArrayPtr(ptr + start, end - start); } inline ArrayPtr slice(size_t start, size_t end) const { KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice()."); return ArrayPtr(ptr + start, end - start); } inline ArrayPtr asBytes() const { return asPtr().asBytes(); } inline ArrayPtr> asBytes() { return asPtr().asBytes(); } inline ArrayPtr asChars() const { return asPtr().asChars(); } inline ArrayPtr> asChars() { return asPtr().asChars(); } inline Array> releaseAsBytes() { // Like asBytes() but transfers ownership. static_assert(sizeof(T) == sizeof(byte), "releaseAsBytes() only possible on arrays with byte-size elements (e.g. chars)."); Array> result( reinterpret_cast*>(ptr), size_, *disposer); ptr = nullptr; size_ = 0; return result; } inline Array> releaseAsChars() { // Like asChars() but transfers ownership. static_assert(sizeof(T) == sizeof(PropagateConst), "releaseAsChars() only possible on arrays with char-size elements (e.g. bytes)."); Array> result( reinterpret_cast*>(ptr), size_, *disposer); ptr = nullptr; size_ = 0; return result; } inline bool operator==(decltype(nullptr)) const { return size_ == 0; } inline bool operator!=(decltype(nullptr)) const { return size_ != 0; } inline Array& operator=(decltype(nullptr)) { dispose(); return *this; } inline Array& operator=(Array&& other) { dispose(); ptr = other.ptr; size_ = other.size_; disposer = other.disposer; other.ptr = nullptr; other.size_ = 0; return *this; } template Array attach(Attachments&&... attachments) KJ_WARN_UNUSED_RESULT; // Like Own::attach(), but attaches to an Array. private: T* ptr; size_t size_; const ArrayDisposer* disposer; inline void dispose() { // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly // dispose again. T* ptrCopy = ptr; size_t sizeCopy = size_; if (ptrCopy != nullptr) { ptr = nullptr; size_ = 0; disposer->dispose(ptrCopy, sizeCopy, sizeCopy); } } template friend class Array; template friend class ArrayBuilder; }; static_assert(!canMemcpy>(), "canMemcpy<>() is broken"); namespace _ { // private class HeapArrayDisposer final: public ArrayDisposer { public: template static T* allocate(size_t count); template static T* allocateUninitialized(size_t count); static const HeapArrayDisposer instance; private: static void* allocateImpl(size_t elementSize, size_t elementCount, size_t capacity, void (*constructElement)(void*), void (*destroyElement)(void*)); // Allocates and constructs the array. Both function pointers are null if the constructor is // trivial, otherwise destroyElement is null if the constructor doesn't throw. virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const override; template struct Allocate_; }; } // namespace _ (private) template inline Array heapArray(size_t size) { // Much like `heap()` from memory.h, allocates a new array on the heap. return Array(_::HeapArrayDisposer::allocate(size), size, _::HeapArrayDisposer::instance); } template Array heapArray(const T* content, size_t size); template Array heapArray(ArrayPtr content); template Array heapArray(ArrayPtr content); template Array heapArray(Iterator begin, Iterator end); template Array heapArray(std::initializer_list init); // Allocate a heap array containing a copy of the given content. template Array heapArrayFromIterable(Container&& a) { return heapArray(a.begin(), a.end()); } template Array heapArrayFromIterable(Array&& a) { return mv(a); } // ======================================================================================= // ArrayBuilder template class ArrayBuilder { // Class which lets you build an Array specifying the exact constructor arguments for each // element, rather than starting by default-constructing them. public: ArrayBuilder(): ptr(nullptr), pos(nullptr), endPtr(nullptr) {} ArrayBuilder(decltype(nullptr)): ptr(nullptr), pos(nullptr), endPtr(nullptr) {} explicit ArrayBuilder(RemoveConst* firstElement, size_t capacity, const ArrayDisposer& disposer) : ptr(firstElement), pos(firstElement), endPtr(firstElement + capacity), disposer(&disposer) {} ArrayBuilder(ArrayBuilder&& other) : ptr(other.ptr), pos(other.pos), endPtr(other.endPtr), disposer(other.disposer) { other.ptr = nullptr; other.pos = nullptr; other.endPtr = nullptr; } ArrayBuilder(Array&& other) : ptr(other.ptr), pos(other.ptr + other.size_), endPtr(pos), disposer(other.disposer) { // Create an already-full ArrayBuilder from an Array of the same type. This constructor // primarily exists to enable Vector to be constructed from Array. other.ptr = nullptr; other.size_ = 0; } KJ_DISALLOW_COPY(ArrayBuilder); inline ~ArrayBuilder() noexcept(false) { dispose(); } inline operator ArrayPtr() { return arrayPtr(ptr, pos); } inline operator ArrayPtr() const { return arrayPtr(ptr, pos); } inline ArrayPtr asPtr() { return arrayPtr(ptr, pos); } inline ArrayPtr asPtr() const { return arrayPtr(ptr, pos); } inline size_t size() const { return pos - ptr; } inline size_t capacity() const { return endPtr - ptr; } inline T& operator[](size_t index) { KJ_IREQUIRE(index < implicitCast(pos - ptr), "Out-of-bounds Array access."); return ptr[index]; } inline const T& operator[](size_t index) const { KJ_IREQUIRE(index < implicitCast(pos - ptr), "Out-of-bounds Array access."); return ptr[index]; } inline const T* begin() const { return ptr; } inline const T* end() const { return pos; } inline const T& front() const { return *ptr; } inline const T& back() const { return *(pos - 1); } inline T* begin() { return ptr; } inline T* end() { return pos; } inline T& front() { return *ptr; } inline T& back() { return *(pos - 1); } ArrayBuilder& operator=(ArrayBuilder&& other) { dispose(); ptr = other.ptr; pos = other.pos; endPtr = other.endPtr; disposer = other.disposer; other.ptr = nullptr; other.pos = nullptr; other.endPtr = nullptr; return *this; } ArrayBuilder& operator=(decltype(nullptr)) { dispose(); return *this; } template T& add(Params&&... params) { KJ_IREQUIRE(pos < endPtr, "Added too many elements to ArrayBuilder."); ctor(*pos, kj::fwd(params)...); return *pos++; } template void addAll(Container&& container) { addAll()>( container.begin(), container.end()); } template void addAll(Iterator start, Iterator end); void removeLast() { KJ_IREQUIRE(pos > ptr, "No elements present to remove."); kj::dtor(*--pos); } void truncate(size_t size) { KJ_IREQUIRE(size <= this->size(), "can't use truncate() to expand"); T* target = ptr + size; if (__has_trivial_destructor(T)) { pos = target; } else { while (pos > target) { kj::dtor(*--pos); } } } void clear() { if (__has_trivial_destructor(T)) { pos = ptr; } else { while (pos > ptr) { kj::dtor(*--pos); } } } void resize(size_t size) { KJ_IREQUIRE(size <= capacity(), "can't resize past capacity"); T* target = ptr + size; if (target > pos) { // expand if (__has_trivial_constructor(T)) { pos = target; } else { while (pos < target) { kj::ctor(*pos++); } } } else { // truncate if (__has_trivial_destructor(T)) { pos = target; } else { while (pos > target) { kj::dtor(*--pos); } } } } Array finish() { // We could safely remove this check if we assume that the disposer implementation doesn't // need to know the original capacity, as is the case with HeapArrayDisposer since it uses // operator new() or if we created a custom disposer for ArrayBuilder which stores the capacity // in a prefix. But that would make it hard to write cleverer heap allocators, and anyway this // check might catch bugs. Probably people should use Vector if they want to build arrays // without knowing the final size in advance. KJ_IREQUIRE(pos == endPtr, "ArrayBuilder::finish() called prematurely."); Array result(reinterpret_cast(ptr), pos - ptr, *disposer); ptr = nullptr; pos = nullptr; endPtr = nullptr; return result; } inline bool isFull() const { return pos == endPtr; } private: T* ptr; RemoveConst* pos; T* endPtr; const ArrayDisposer* disposer; inline void dispose() { // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly // dispose again. T* ptrCopy = ptr; T* posCopy = pos; T* endCopy = endPtr; if (ptrCopy != nullptr) { ptr = nullptr; pos = nullptr; endPtr = nullptr; disposer->dispose(ptrCopy, posCopy - ptrCopy, endCopy - ptrCopy); } } }; template inline ArrayBuilder heapArrayBuilder(size_t size) { // Like `heapArray()` but does not default-construct the elements. You must construct them // manually by calling `add()`. return ArrayBuilder(_::HeapArrayDisposer::allocateUninitialized>(size), size, _::HeapArrayDisposer::instance); } // ======================================================================================= // Inline Arrays template class FixedArray { // A fixed-width array whose storage is allocated inline rather than on the heap. public: inline constexpr size_t size() const { return fixedSize; } inline constexpr T* begin() { return content; } inline constexpr T* end() { return content + fixedSize; } inline constexpr const T* begin() const { return content; } inline constexpr const T* end() const { return content + fixedSize; } inline constexpr operator ArrayPtr() { return arrayPtr(content, fixedSize); } inline constexpr operator ArrayPtr() const { return arrayPtr(content, fixedSize); } inline constexpr T& operator[](size_t index) { return content[index]; } inline constexpr const T& operator[](size_t index) const { return content[index]; } private: T content[fixedSize]; }; template class CappedArray { // Like `FixedArray` but can be dynamically resized as long as the size does not exceed the limit // specified by the template parameter. // // TODO(someday): Don't construct elements past currentSize? public: inline KJ_CONSTEXPR() CappedArray(): currentSize(fixedSize) {} inline explicit constexpr CappedArray(size_t s): currentSize(s) {} inline size_t size() const { return currentSize; } inline void setSize(size_t s) { KJ_IREQUIRE(s <= fixedSize); currentSize = s; } inline T* begin() { return content; } inline T* end() { return content + currentSize; } inline const T* begin() const { return content; } inline const T* end() const { return content + currentSize; } inline operator ArrayPtr() { return arrayPtr(content, currentSize); } inline operator ArrayPtr() const { return arrayPtr(content, currentSize); } inline T& operator[](size_t index) { return content[index]; } inline const T& operator[](size_t index) const { return content[index]; } private: size_t currentSize; T content[fixedSize]; }; // ======================================================================================= // KJ_MAP #define KJ_MAP(elementName, array) \ ::kj::_::Mapper(array) * \ [&](typename ::kj::_::Mapper::Element elementName) // Applies some function to every element of an array, returning an Array of the results, with // nice syntax. Example: // // StringPtr foo = "abcd"; // Array bar = KJ_MAP(c, foo) -> char { return c + 1; }; // KJ_ASSERT(str(bar) == "bcde"); namespace _ { // private template struct Mapper { T array; Mapper(T&& array): array(kj::fwd(array)) {} template auto operator*(Func&& func) -> Array { auto builder = heapArrayBuilder(array.size()); for (auto iter = array.begin(); iter != array.end(); ++iter) { builder.add(func(*iter)); } return builder.finish(); } typedef decltype(*kj::instance().begin()) Element; }; template struct Mapper { T* array; Mapper(T* array): array(array) {} template auto operator*(Func&& func) -> Array { auto builder = heapArrayBuilder(s); for (size_t i = 0; i < s; i++) { builder.add(func(array[i])); } return builder.finish(); } typedef decltype(*array)& Element; }; } // namespace _ (private) // ======================================================================================= // Inline implementation details template struct ArrayDisposer::Dispose_ { static void dispose(T* firstElement, size_t elementCount, size_t capacity, const ArrayDisposer& disposer) { disposer.disposeImpl(const_cast*>(firstElement), sizeof(T), elementCount, capacity, nullptr); } }; template struct ArrayDisposer::Dispose_ { static void destruct(void* ptr) { kj::dtor(*reinterpret_cast(ptr)); } static void dispose(T* firstElement, size_t elementCount, size_t capacity, const ArrayDisposer& disposer) { disposer.disposeImpl(const_cast*>(firstElement), sizeof(T), elementCount, capacity, &destruct); } }; template void ArrayDisposer::dispose(T* firstElement, size_t elementCount, size_t capacity) const { Dispose_::dispose(firstElement, elementCount, capacity, *this); } namespace _ { // private template struct HeapArrayDisposer::Allocate_ { static T* allocate(size_t elementCount, size_t capacity) { return reinterpret_cast(allocateImpl( sizeof(T), elementCount, capacity, nullptr, nullptr)); } }; template struct HeapArrayDisposer::Allocate_ { static void construct(void* ptr) { kj::ctor(*reinterpret_cast(ptr)); } static T* allocate(size_t elementCount, size_t capacity) { return reinterpret_cast(allocateImpl( sizeof(T), elementCount, capacity, &construct, nullptr)); } }; template struct HeapArrayDisposer::Allocate_ { static void construct(void* ptr) { kj::ctor(*reinterpret_cast(ptr)); } static void destruct(void* ptr) { kj::dtor(*reinterpret_cast(ptr)); } static T* allocate(size_t elementCount, size_t capacity) { return reinterpret_cast(allocateImpl( sizeof(T), elementCount, capacity, &construct, &destruct)); } }; template T* HeapArrayDisposer::allocate(size_t count) { return Allocate_::allocate(count, count); } template T* HeapArrayDisposer::allocateUninitialized(size_t count) { return Allocate_::allocate(0, count); } template ()> struct CopyConstructArray_; template struct CopyConstructArray_ { static inline T* apply(T* __restrict__ pos, T* start, T* end) { if (end != start) { memcpy(pos, start, reinterpret_cast(end) - reinterpret_cast(start)); } return pos + (end - start); } }; template struct CopyConstructArray_ { static inline T* apply(T* __restrict__ pos, const T* start, const T* end) { if (end != start) { memcpy(pos, start, reinterpret_cast(end) - reinterpret_cast(start)); } return pos + (end - start); } }; template struct CopyConstructArray_ { static inline T* apply(T* __restrict__ pos, Iterator start, Iterator end) { // Since both the copy constructor and assignment operator are trivial, we know that assignment // is equivalent to copy-constructing. So we can make this case somewhat easier for the // compiler to optimize. while (start != end) { *pos++ = *start++; } return pos; } }; template struct CopyConstructArray_ { struct ExceptionGuard { T* start; T* pos; inline explicit ExceptionGuard(T* pos): start(pos), pos(pos) {} ~ExceptionGuard() noexcept(false) { while (pos > start) { dtor(*--pos); } } }; static T* apply(T* __restrict__ pos, Iterator start, Iterator end) { // Verify that T can be *implicitly* constructed from the source values. if (false) implicitCast(*start); if (noexcept(T(*start))) { while (start != end) { ctor(*pos++, *start++); } return pos; } else { // Crap. This is complicated. ExceptionGuard guard(pos); while (start != end) { ctor(*guard.pos, *start++); ++guard.pos; } guard.start = guard.pos; return guard.pos; } } }; template struct CopyConstructArray_ { // Actually move-construct. struct ExceptionGuard { T* start; T* pos; inline explicit ExceptionGuard(T* pos): start(pos), pos(pos) {} ~ExceptionGuard() noexcept(false) { while (pos > start) { dtor(*--pos); } } }; static T* apply(T* __restrict__ pos, Iterator start, Iterator end) { // Verify that T can be *implicitly* constructed from the source values. if (false) implicitCast(kj::mv(*start)); if (noexcept(T(kj::mv(*start)))) { while (start != end) { ctor(*pos++, kj::mv(*start++)); } return pos; } else { // Crap. This is complicated. ExceptionGuard guard(pos); while (start != end) { ctor(*guard.pos, kj::mv(*start++)); ++guard.pos; } guard.start = guard.pos; return guard.pos; } } }; } // namespace _ (private) template template void ArrayBuilder::addAll(Iterator start, Iterator end) { pos = _::CopyConstructArray_, Decay, move>::apply(pos, start, end); } template Array heapArray(const T* content, size_t size) { ArrayBuilder builder = heapArrayBuilder(size); builder.addAll(content, content + size); return builder.finish(); } template Array heapArray(T* content, size_t size) { ArrayBuilder builder = heapArrayBuilder(size); builder.addAll(content, content + size); return builder.finish(); } template Array heapArray(ArrayPtr content) { ArrayBuilder builder = heapArrayBuilder(content.size()); builder.addAll(content); return builder.finish(); } template Array heapArray(ArrayPtr content) { ArrayBuilder builder = heapArrayBuilder(content.size()); builder.addAll(content); return builder.finish(); } template Array heapArray(Iterator begin, Iterator end) { ArrayBuilder builder = heapArrayBuilder(end - begin); builder.addAll(begin, end); return builder.finish(); } template inline Array heapArray(std::initializer_list init) { return heapArray(init.begin(), init.end()); } #if __cplusplus > 201402L template inline Array> arr(T&& param1, Params&&... params) { ArrayBuilder> builder = heapArrayBuilder>(sizeof...(params) + 1); (builder.add(kj::fwd(param1)), ... , builder.add(kj::fwd(params))); return builder.finish(); } #endif namespace _ { // private template struct ArrayDisposableOwnedBundle final: public ArrayDisposer, public OwnedBundle { ArrayDisposableOwnedBundle(T&&... values): OwnedBundle(kj::fwd(values)...) {} void disposeImpl(void*, size_t, size_t, size_t, void (*)(void*)) const override { delete this; } }; } // namespace _ (private) template template Array Array::attach(Attachments&&... attachments) { T* ptrCopy = ptr; auto sizeCopy = size_; KJ_IREQUIRE(ptrCopy != nullptr, "cannot attach to null pointer"); // HACK: If someone accidentally calls .attach() on a null pointer in opt mode, try our best to // accomplish reasonable behavior: We turn the pointer non-null but still invalid, so that the // disposer will still be called when the pointer goes out of scope. if (ptrCopy == nullptr) ptrCopy = reinterpret_cast(1); auto bundle = new _::ArrayDisposableOwnedBundle, Attachments...>( kj::mv(*this), kj::fwd(attachments)...); return Array(ptrCopy, sizeCopy, *bundle); } template template Array ArrayPtr::attach(Attachments&&... attachments) const { T* ptrCopy = ptr; KJ_IREQUIRE(ptrCopy != nullptr, "cannot attach to null pointer"); // HACK: If someone accidentally calls .attach() on a null pointer in opt mode, try our best to // accomplish reasonable behavior: We turn the pointer non-null but still invalid, so that the // disposer will still be called when the pointer goes out of scope. if (ptrCopy == nullptr) ptrCopy = reinterpret_cast(1); auto bundle = new _::ArrayDisposableOwnedBundle( kj::fwd(attachments)...); return Array(ptrCopy, size_, *bundle); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/vector.h0000644000175000017500000001173013650101756017624 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "array.h" KJ_BEGIN_HEADER namespace kj { template class Vector { // Similar to std::vector, but based on KJ framework. // // This implementation always uses move constructors when growing the backing array. If the // move constructor throws, the Vector is left in an inconsistent state. This is acceptable // under KJ exception theory which assumes that exceptions leave things in inconsistent states. // TODO(someday): Allow specifying a custom allocator. public: inline Vector() = default; inline explicit Vector(size_t capacity): builder(heapArrayBuilder(capacity)) {} inline Vector(Array&& array): builder(kj::mv(array)) {} inline operator ArrayPtr() { return builder; } inline operator ArrayPtr() const { return builder; } inline ArrayPtr asPtr() { return builder.asPtr(); } inline ArrayPtr asPtr() const { return builder.asPtr(); } inline size_t size() const { return builder.size(); } inline bool empty() const { return size() == 0; } inline size_t capacity() const { return builder.capacity(); } inline T& operator[](size_t index) { return builder[index]; } inline const T& operator[](size_t index) const { return builder[index]; } inline const T* begin() const { return builder.begin(); } inline const T* end() const { return builder.end(); } inline const T& front() const { return builder.front(); } inline const T& back() const { return builder.back(); } inline T* begin() { return builder.begin(); } inline T* end() { return builder.end(); } inline T& front() { return builder.front(); } inline T& back() { return builder.back(); } inline Array releaseAsArray() { // TODO(perf): Avoid a copy/move by allowing Array to point to incomplete space? if (!builder.isFull()) { setCapacity(size()); } return builder.finish(); } template inline bool operator==(const U& other) const { return asPtr() == other; } template inline bool operator!=(const U& other) const { return asPtr() != other; } inline ArrayPtr slice(size_t start, size_t end) { return asPtr().slice(start, end); } inline ArrayPtr slice(size_t start, size_t end) const { return asPtr().slice(start, end); } template inline T& add(Params&&... params) { if (builder.isFull()) grow(); return builder.add(kj::fwd(params)...); } template inline void addAll(Iterator begin, Iterator end) { size_t needed = builder.size() + (end - begin); if (needed > builder.capacity()) grow(needed); builder.addAll(begin, end); } template inline void addAll(Container&& container) { addAll(container.begin(), container.end()); } inline void removeLast() { builder.removeLast(); } inline void resize(size_t size) { if (size > builder.capacity()) grow(size); builder.resize(size); } inline void operator=(decltype(nullptr)) { builder = nullptr; } inline void clear() { builder.clear(); } inline void truncate(size_t size) { builder.truncate(size); } inline void reserve(size_t size) { if (size > builder.capacity()) { setCapacity(size); } } private: ArrayBuilder builder; void grow(size_t minCapacity = 0) { setCapacity(kj::max(minCapacity, capacity() == 0 ? 4 : capacity() * 2)); } void setCapacity(size_t newSize) { if (builder.size() > newSize) { builder.truncate(newSize); } ArrayBuilder newBuilder = heapArrayBuilder(newSize); newBuilder.addAll(kj::mv(builder)); builder = kj::mv(newBuilder); } }; template inline auto KJ_STRINGIFY(const Vector& v) -> decltype(toCharSequence(v.asPtr())) { return toCharSequence(v.asPtr()); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/string.h0000644000175000017500000006731513650101756017642 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include "array.h" #include KJ_BEGIN_HEADER namespace kj { class StringPtr; class String; class StringTree; // string-tree.h } constexpr kj::StringPtr operator "" _kj(const char* str, size_t n); // You can append _kj to a string literal to make its type be StringPtr. There are a few cases // where you must do this for correctness: // - When you want to declare a constexpr StringPtr. Without _kj, this is a compile error. // - When you want to initialize a static/global StringPtr from a string literal without forcing // global constructor code to run at dynamic initialization time. // - When you have a string literal that contains NUL characters. Without _kj, the string will // be considered to end at the first NUL. // - When you want to initialize an ArrayPtr from a string literal, without including // the NUL terminator in the data. (Initializing an ArrayPtr from a regular string literal is // a compile error specifically due to this ambiguity.) // // In other cases, there should be no difference between initializing a StringPtr from a regular // string literal vs. one with _kj (assuming the compiler is able to optimize away strlen() on a // string literal). namespace kj { // Our STL string SFINAE trick does not work with GCC 4.7, but it works with Clang and GCC 4.8, so // we'll just preprocess it out if not supported. #if __clang__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || _MSC_VER #define KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP 1 #endif // ======================================================================================= // StringPtr -- A NUL-terminated ArrayPtr containing UTF-8 text. // // NUL bytes are allowed to appear before the end of the string. The only requirement is that // a NUL byte appear immediately after the last byte of the content. This terminator byte is not // counted in the string's size. class StringPtr { public: inline StringPtr(): content("", 1) {} inline StringPtr(decltype(nullptr)): content("", 1) {} inline StringPtr(const char* value): content(value, strlen(value) + 1) {} inline StringPtr(const char* value, size_t size): content(value, size + 1) { KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated."); } inline StringPtr(const char* begin, const char* end): StringPtr(begin, end - begin) {} inline StringPtr(const String& value); #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP template ().c_str())> inline StringPtr(const T& t): StringPtr(t.c_str()) {} // Allow implicit conversion from any class that has a c_str() method (namely, std::string). // We use a template trick to detect std::string in order to avoid including the header for // those who don't want it. template ().c_str())> inline operator T() const { return cStr(); } // Allow implicit conversion to any class that has a c_str() method (namely, std::string). // We use a template trick to detect std::string in order to avoid including the header for // those who don't want it. #endif inline constexpr operator ArrayPtr() const; inline constexpr ArrayPtr asArray() const; inline ArrayPtr asBytes() const { return asArray().asBytes(); } // Result does not include NUL terminator. inline const char* cStr() const { return content.begin(); } // Returns NUL-terminated string. inline size_t size() const { return content.size() - 1; } // Result does not include NUL terminator. inline char operator[](size_t index) const { return content[index]; } inline const char* begin() const { return content.begin(); } inline const char* end() const { return content.end() - 1; } inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } inline bool operator==(const StringPtr& other) const; inline bool operator!=(const StringPtr& other) const { return !(*this == other); } inline bool operator< (const StringPtr& other) const; inline bool operator> (const StringPtr& other) const { return other < *this; } inline bool operator<=(const StringPtr& other) const { return !(other < *this); } inline bool operator>=(const StringPtr& other) const { return !(*this < other); } inline StringPtr slice(size_t start) const; inline ArrayPtr slice(size_t start, size_t end) const; // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter // version that assumes end = size(). inline bool startsWith(const StringPtr& other) const; inline bool endsWith(const StringPtr& other) const; inline Maybe findFirst(char c) const; inline Maybe findLast(char c) const; template T parseAs() const; // Parse string as template number type. // Integer numbers prefixed by "0x" and "0X" are parsed in base 16 (like strtoi with base 0). // Integer numbers prefixed by "0" are parsed in base 10 (unlike strtoi with base 0). // Overflowed integer numbers throw exception. // Overflowed floating numbers return inf. private: inline constexpr StringPtr(ArrayPtr content): content(content) {} ArrayPtr content; friend constexpr kj::StringPtr (::operator "" _kj)(const char* str, size_t n); }; inline bool operator==(const char* a, const StringPtr& b) { return b == a; } inline bool operator!=(const char* a, const StringPtr& b) { return b != a; } template <> char StringPtr::parseAs() const; template <> signed char StringPtr::parseAs() const; template <> unsigned char StringPtr::parseAs() const; template <> short StringPtr::parseAs() const; template <> unsigned short StringPtr::parseAs() const; template <> int StringPtr::parseAs() const; template <> unsigned StringPtr::parseAs() const; template <> long StringPtr::parseAs() const; template <> unsigned long StringPtr::parseAs() const; template <> long long StringPtr::parseAs() const; template <> unsigned long long StringPtr::parseAs() const; template <> float StringPtr::parseAs() const; template <> double StringPtr::parseAs() const; // ======================================================================================= // String -- A NUL-terminated Array containing UTF-8 text. // // NUL bytes are allowed to appear before the end of the string. The only requirement is that // a NUL byte appear immediately after the last byte of the content. This terminator byte is not // counted in the string's size. // // To allocate a String, you must call kj::heapString(). We do not implement implicit copying to // the heap because this hides potential inefficiency from the developer. class String { public: String() = default; inline String(decltype(nullptr)): content(nullptr) {} inline String(char* value, size_t size, const ArrayDisposer& disposer); // Does not copy. `size` does not include NUL terminator, but `value` must be NUL-terminated. inline explicit String(Array buffer); // Does not copy. Requires `buffer` ends with `\0`. inline operator ArrayPtr(); inline operator ArrayPtr() const; inline ArrayPtr asArray(); inline ArrayPtr asArray() const; inline ArrayPtr asBytes() { return asArray().asBytes(); } inline ArrayPtr asBytes() const { return asArray().asBytes(); } // Result does not include NUL terminator. inline Array releaseArray() { return kj::mv(content); } // Disowns the backing array (which includes the NUL terminator) and returns it. The String value // is clobbered (as if moved away). inline const char* cStr() const; inline size_t size() const; // Result does not include NUL terminator. inline char operator[](size_t index) const; inline char& operator[](size_t index); inline char* begin(); inline char* end(); inline const char* begin() const; inline const char* end() const; inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } inline bool operator==(const StringPtr& other) const { return StringPtr(*this) == other; } inline bool operator!=(const StringPtr& other) const { return StringPtr(*this) != other; } inline bool operator< (const StringPtr& other) const { return StringPtr(*this) < other; } inline bool operator> (const StringPtr& other) const { return StringPtr(*this) > other; } inline bool operator<=(const StringPtr& other) const { return StringPtr(*this) <= other; } inline bool operator>=(const StringPtr& other) const { return StringPtr(*this) >= other; } inline bool startsWith(const StringPtr& other) const { return StringPtr(*this).startsWith(other);} inline bool endsWith(const StringPtr& other) const { return StringPtr(*this).endsWith(other); } inline StringPtr slice(size_t start) const { return StringPtr(*this).slice(start); } inline ArrayPtr slice(size_t start, size_t end) const { return StringPtr(*this).slice(start, end); } inline Maybe findFirst(char c) const { return StringPtr(*this).findFirst(c); } inline Maybe findLast(char c) const { return StringPtr(*this).findLast(c); } template T parseAs() const { return StringPtr(*this).parseAs(); } // Parse as number private: Array content; }; inline bool operator==(const char* a, const String& b) { return b == a; } inline bool operator!=(const char* a, const String& b) { return b != a; } String heapString(size_t size); // Allocate a String of the given size on the heap, not including NUL terminator. The NUL // terminator will be initialized automatically but the rest of the content is not initialized. String heapString(const char* value); String heapString(const char* value, size_t size); String heapString(StringPtr value); String heapString(const String& value); String heapString(ArrayPtr value); // Allocates a copy of the given value on the heap. // ======================================================================================= // Magic str() function which transforms parameters to text and concatenates them into one big // String. namespace _ { // private inline size_t sum(std::initializer_list nums) { size_t result = 0; for (auto num: nums) { result += num; } return result; } inline char* fill(char* ptr) { return ptr; } inline char* fillLimited(char* ptr, char* limit) { return ptr; } template char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest); template char* fillLimited(char* __restrict__ target, char* limit, const StringTree& first, Rest&&... rest); // Make str() work with stringifiers that return StringTree by patching fill(). // // Defined in string-tree.h. template char* fill(char* __restrict__ target, const First& first, Rest&&... rest) { auto i = first.begin(); auto end = first.end(); while (i != end) { *target++ = *i++; } return fill(target, kj::fwd(rest)...); } template String concat(Params&&... params) { // Concatenate a bunch of containers into a single Array. The containers can be anything that // is iterable and whose elements can be converted to `char`. String result = heapString(sum({params.size()...})); fill(result.begin(), kj::fwd(params)...); return result; } inline String concat(String&& arr) { return kj::mv(arr); } template char* fillLimited(char* __restrict__ target, char* limit, const First& first, Rest&&... rest) { auto i = first.begin(); auto end = first.end(); while (i != end) { if (target == limit) return target; *target++ = *i++; } return fillLimited(target, limit, kj::fwd(rest)...); } template class Delimited; // Delimits a sequence of type T with a string delimiter. Implements kj::delimited(). template char* fill(char* __restrict__ target, Delimited first, Rest&&... rest); template char* fillLimited(char* __restrict__ target, char* limit, Delimited first,Rest&&... rest); // As with StringTree, we special-case Delimited. struct Stringifier { // This is a dummy type with only one instance: STR (below). To make an arbitrary type // stringifiable, define `operator*(Stringifier, T)` to return an iterable container of `char`. // The container type must have a `size()` method. Be sure to declare the operator in the same // namespace as `T` **or** in the global scope. // // A more usual way to accomplish what we're doing here would be to require that you define // a function like `toString(T)` and then rely on argument-dependent lookup. However, this has // the problem that it pollutes other people's namespaces and even the global namespace. For // example, some other project may already have functions called `toString` which do something // different. Declaring `operator*` with `Stringifier` as the left operand cannot conflict with // anything. inline ArrayPtr operator*(ArrayPtr s) const { return s; } inline ArrayPtr operator*(ArrayPtr s) const { return s; } inline ArrayPtr operator*(const Array& s) const { return s; } inline ArrayPtr operator*(const Array& s) const { return s; } template inline ArrayPtr operator*(const CappedArray& s) const { return s; } template inline ArrayPtr operator*(const FixedArray& s) const { return s; } inline ArrayPtr operator*(const char* s) const { return arrayPtr(s, strlen(s)); } inline ArrayPtr operator*(const String& s) const { return s.asArray(); } inline ArrayPtr operator*(const StringPtr& s) const { return s.asArray(); } inline Range operator*(const Range& r) const { return r; } inline Repeat operator*(const Repeat& r) const { return r; } inline FixedArray operator*(char c) const { FixedArray result; result[0] = c; return result; } StringPtr operator*(decltype(nullptr)) const; StringPtr operator*(bool b) const; CappedArray operator*(signed char i) const; CappedArray operator*(unsigned char i) const; CappedArray operator*(short i) const; CappedArray operator*(unsigned short i) const; CappedArray operator*(int i) const; CappedArray operator*(unsigned int i) const; CappedArray operator*(long i) const; CappedArray operator*(unsigned long i) const; CappedArray operator*(long long i) const; CappedArray operator*(unsigned long long i) const; CappedArray operator*(float f) const; CappedArray operator*(double f) const; CappedArray operator*(const void* s) const; template _::Delimited> operator*(ArrayPtr arr) const; template _::Delimited> operator*(const Array& arr) const; #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP // supports expression SFINAE? template ().toString())> inline Result operator*(T&& value) const { return kj::fwd(value).toString(); } #endif }; static KJ_CONSTEXPR(const) Stringifier STR = Stringifier(); } // namespace _ (private) template auto toCharSequence(T&& value) -> decltype(_::STR * kj::fwd(value)) { // Returns an iterable of chars that represent a textual representation of the value, suitable // for debugging. // // Most users should use str() instead, but toCharSequence() may occasionally be useful to avoid // heap allocation overhead that str() implies. // // To specialize this function for your type, see KJ_STRINGIFY. return _::STR * kj::fwd(value); } CappedArray hex(unsigned char i); CappedArray hex(unsigned short i); CappedArray hex(unsigned int i); CappedArray hex(unsigned long i); CappedArray hex(unsigned long long i); template String str(Params&&... params) { // Magic function which builds a string from a bunch of arbitrary values. Example: // str(1, " / ", 2, " = ", 0.5) // returns: // "1 / 2 = 0.5" // To teach `str` how to stringify a type, see `Stringifier`. return _::concat(toCharSequence(kj::fwd(params))...); } inline String str(String&& s) { return mv(s); } // Overload to prevent redundant allocation. template _::Delimited delimited(T&& arr, kj::StringPtr delim); // Use to stringify an array. template String strArray(T&& arr, const char* delim) { size_t delimLen = strlen(delim); KJ_STACK_ARRAY(decltype(_::STR * arr[0]), pieces, kj::size(arr), 8, 32); size_t size = 0; for (size_t i = 0; i < kj::size(arr); i++) { if (i > 0) size += delimLen; pieces[i] = _::STR * arr[i]; size += pieces[i].size(); } String result = heapString(size); char* pos = result.begin(); for (size_t i = 0; i < kj::size(arr); i++) { if (i > 0) { memcpy(pos, delim, delimLen); pos += delimLen; } pos = _::fill(pos, pieces[i]); } return result; } template StringPtr strPreallocated(ArrayPtr buffer, Params&&... params) { // Like str() but writes into a preallocated buffer. If the buffer is not long enough, the result // is truncated (but still NUL-terminated). // // This can be used like: // // char buffer[256]; // StringPtr text = strPreallocated(buffer, params...); // // This is useful for optimization. It can also potentially be used safely in async signal // handlers. HOWEVER, to use in an async signal handler, all of the stringifiers for the inputs // must also be signal-safe. KJ guarantees signal safety when stringifying any built-in integer // type (but NOT floating-points), basic char/byte sequences (ArrayPtr, String, etc.), as // well as Array as long as T can also be stringified safely. To safely stringify a delimited // array, you must use kj::delimited(arr, delim) rather than the deprecated // kj::strArray(arr, delim). char* end = _::fillLimited(buffer.begin(), buffer.end() - 1, toCharSequence(kj::fwd(params))...); *end = '\0'; return StringPtr(buffer.begin(), end); } namespace _ { // private template inline _::Delimited> Stringifier::operator*(ArrayPtr arr) const { return _::Delimited>(arr, ", "); } template inline _::Delimited> Stringifier::operator*(const Array& arr) const { return _::Delimited>(arr, ", "); } } // namespace _ (private) #define KJ_STRINGIFY(...) operator*(::kj::_::Stringifier, __VA_ARGS__) // Defines a stringifier for a custom type. Example: // // class Foo {...}; // inline StringPtr KJ_STRINGIFY(const Foo& foo) { return foo.name(); } // // or perhaps // inline String KJ_STRINGIFY(const Foo& foo) { return kj::str(foo.fld1(), ",", foo.fld2()); } // // This allows Foo to be passed to str(). // // The function should be declared either in the same namespace as the target type or in the global // namespace. It can return any type which is an iterable container of chars. // ======================================================================================= // Inline implementation details. inline StringPtr::StringPtr(const String& value): content(value.cStr(), value.size() + 1) {} inline constexpr StringPtr::operator ArrayPtr() const { return ArrayPtr(content.begin(), content.size() - 1); } inline constexpr ArrayPtr StringPtr::asArray() const { return ArrayPtr(content.begin(), content.size() - 1); } inline bool StringPtr::operator==(const StringPtr& other) const { return content.size() == other.content.size() && memcmp(content.begin(), other.content.begin(), content.size() - 1) == 0; } inline bool StringPtr::operator<(const StringPtr& other) const { bool shorter = content.size() < other.content.size(); int cmp = memcmp(content.begin(), other.content.begin(), shorter ? content.size() : other.content.size()); return cmp < 0 || (cmp == 0 && shorter); } inline StringPtr StringPtr::slice(size_t start) const { return StringPtr(content.slice(start, content.size())); } inline ArrayPtr StringPtr::slice(size_t start, size_t end) const { return content.slice(start, end); } inline bool StringPtr::startsWith(const StringPtr& other) const { return other.content.size() <= content.size() && memcmp(content.begin(), other.content.begin(), other.size()) == 0; } inline bool StringPtr::endsWith(const StringPtr& other) const { return other.content.size() <= content.size() && memcmp(end() - other.size(), other.content.begin(), other.size()) == 0; } inline Maybe StringPtr::findFirst(char c) const { const char* pos = reinterpret_cast(memchr(content.begin(), c, size())); if (pos == nullptr) { return nullptr; } else { return pos - content.begin(); } } inline Maybe StringPtr::findLast(char c) const { for (size_t i = size(); i > 0; --i) { if (content[i-1] == c) { return i-1; } } return nullptr; } inline String::operator ArrayPtr() { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline String::operator ArrayPtr() const { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline ArrayPtr String::asArray() { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline ArrayPtr String::asArray() const { return content == nullptr ? ArrayPtr(nullptr) : content.slice(0, content.size() - 1); } inline const char* String::cStr() const { return content == nullptr ? "" : content.begin(); } inline size_t String::size() const { return content == nullptr ? 0 : content.size() - 1; } inline char String::operator[](size_t index) const { return content[index]; } inline char& String::operator[](size_t index) { return content[index]; } inline char* String::begin() { return content == nullptr ? nullptr : content.begin(); } inline char* String::end() { return content == nullptr ? nullptr : content.end() - 1; } inline const char* String::begin() const { return content == nullptr ? nullptr : content.begin(); } inline const char* String::end() const { return content == nullptr ? nullptr : content.end() - 1; } inline String::String(char* value, size_t size, const ArrayDisposer& disposer) : content(value, size + 1, disposer) { KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated."); } inline String::String(Array buffer): content(kj::mv(buffer)) { KJ_IREQUIRE(content.size() > 0 && content.back() == '\0', "String must be NUL-terminated."); } inline String heapString(const char* value) { return heapString(value, strlen(value)); } inline String heapString(StringPtr value) { return heapString(value.begin(), value.size()); } inline String heapString(const String& value) { return heapString(value.begin(), value.size()); } inline String heapString(ArrayPtr value) { return heapString(value.begin(), value.size()); } namespace _ { // private template class Delimited { public: Delimited(T array, kj::StringPtr delimiter) : array(kj::fwd(array)), delimiter(delimiter) {} // TODO(someday): In theory we should support iteration as a character sequence, but the iterator // will be pretty complicated. size_t size() { ensureStringifiedInitialized(); size_t result = 0; bool first = true; for (auto& e: stringified) { if (first) { first = false; } else { result += delimiter.size(); } result += e.size(); } return result; } char* flattenTo(char* __restrict__ target) { ensureStringifiedInitialized(); bool first = true; for (auto& elem: stringified) { if (first) { first = false; } else { target = fill(target, delimiter); } target = fill(target, elem); } return target; } char* flattenTo(char* __restrict__ target, char* limit) { // This is called in the strPreallocated(). We want to avoid allocation. size() will not have // been called in this case, so hopefully `stringified` is still uninitialized. We will // stringify each item and immediately use it. bool first = true; for (auto&& elem: array) { if (target == limit) return target; if (first) { first = false; } else { target = fillLimited(target, limit, delimiter); } target = fillLimited(target, limit, kj::toCharSequence(elem)); } return target; } private: typedef decltype(toCharSequence(*instance().begin())) StringifiedItem; T array; kj::StringPtr delimiter; Array stringified; void ensureStringifiedInitialized() { if (array.size() > 0 && stringified.size() == 0) { stringified = KJ_MAP(e, array) { return toCharSequence(e); }; } } }; template char* fill(char* __restrict__ target, Delimited first, Rest&&... rest) { target = first.flattenTo(target); return fill(target, kj::fwd(rest)...); } template char* fillLimited(char* __restrict__ target, char* limit, Delimited first, Rest&&... rest) { target = first.flattenTo(target, limit); return fillLimited(target, limit, kj::fwd(rest)...); } template inline Delimited&& KJ_STRINGIFY(Delimited&& delimited) { return kj::mv(delimited); } template inline const Delimited& KJ_STRINGIFY(const Delimited& delimited) { return delimited; } } // namespace _ (private) template _::Delimited delimited(T&& arr, kj::StringPtr delim) { return _::Delimited(kj::fwd(arr), delim); } } // namespace kj constexpr kj::StringPtr operator "" _kj(const char* str, size_t n) { return kj::StringPtr(kj::ArrayPtr(str, n + 1)); }; KJ_END_HEADER capnproto-c++-0.8.0/src/kj/string-tree.h0000644000175000017500000002046613650101756020573 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "string.h" KJ_BEGIN_HEADER namespace kj { class StringTree { // A long string, represented internally as a tree of strings. This data structure is like a // String, but optimized for concatenation and iteration at the expense of seek time. The // structure is intended to be used for building large text blobs from many small pieces, where // repeatedly concatenating smaller strings into larger ones would waste copies. This structure // is NOT intended for use cases requiring random access or computing substrings. For those, // you should use a Rope, which is a much more complicated data structure. // // The proper way to construct a StringTree is via kj::strTree(...), which works just like // kj::str(...) but returns a StringTree rather than a String. // // KJ_STRINGIFY() functions that construct large strings from many smaller strings are encouraged // to return StringTree rather than a flat char container. public: inline StringTree(): size_(0) {} inline StringTree(String&& text): size_(text.size()), text(kj::mv(text)) {} StringTree(Array&& pieces, StringPtr delim); // Build a StringTree by concatenating the given pieces, delimited by the given delimiter // (e.g. ", "). inline size_t size() const { return size_; } template void visit(Func&& func) const; String flatten() const; // Return the contents as a string. // TODO(someday): flatten() when *this is an rvalue and when branches.size() == 0 could simply // return `kj::mv(text)`. Requires reference qualifiers (Clang 3.3 / GCC 4.8). char* flattenTo(char* __restrict__ target) const; char* flattenTo(char* __restrict__ target, char* limit) const; // Copy the contents to the given character array. Does not add a NUL terminator. Returns a // pointer just past the end of what was filled. private: size_t size_; String text; struct Branch; Array branches; // In order. inline void fill(char* pos, size_t branchIndex); template void fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest); template void fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest); template void fill(char* pos, size_t branchIndex, Array&& first, Rest&&... rest); template void fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest); template static StringTree concat(Params&&... params); static StringTree&& concat(StringTree&& param) { return kj::mv(param); } template static inline size_t flatSize(const T& t) { return t.size(); } static inline size_t flatSize(String&& s) { return 0; } static inline size_t flatSize(StringTree&& s) { return 0; } template static inline size_t branchCount(const T& t) { return 0; } static inline size_t branchCount(String&& s) { return 1; } static inline size_t branchCount(StringTree&& s) { return 1; } template friend StringTree strTree(Params&&... params); }; inline StringTree&& KJ_STRINGIFY(StringTree&& tree) { return kj::mv(tree); } inline const StringTree& KJ_STRINGIFY(const StringTree& tree) { return tree; } inline StringTree KJ_STRINGIFY(Array&& trees) { return StringTree(kj::mv(trees), ""); } template StringTree strTree(Params&&... params); // Build a StringTree by stringifying the given parameters and concatenating the results. // If any of the parameters stringify to StringTree rvalues, they will be incorporated as // branches to avoid a copy. // ======================================================================================= // Inline implementation details namespace _ { // private template char* fill(char* __restrict__ target, const StringTree& first, Rest&&... rest) { // Make str() work with stringifiers that return StringTree by patching fill(). first.flattenTo(target); return fill(target + first.size(), kj::fwd(rest)...); } template char* fillLimited(char* __restrict__ target, char* limit, const StringTree& first, Rest&&... rest) { // Make str() work with stringifiers that return StringTree by patching fill(). target = first.flattenTo(target, limit); return fillLimited(target + first.size(), limit, kj::fwd(rest)...); } template constexpr bool isStringTree() { return false; } template <> constexpr bool isStringTree() { return true; } inline StringTree&& toStringTreeOrCharSequence(StringTree&& tree) { return kj::mv(tree); } inline StringTree toStringTreeOrCharSequence(String&& str) { return StringTree(kj::mv(str)); } template inline auto toStringTreeOrCharSequence(T&& value) -> decltype(toCharSequence(kj::fwd(value))) { static_assert(!isStringTree>(), "When passing a StringTree into kj::strTree(), either pass it by rvalue " "(use kj::mv(value)) or explicitly call value.flatten() to make a copy."); return toCharSequence(kj::fwd(value)); } } // namespace _ (private) struct StringTree::Branch { size_t index; // Index in `text` where this branch should be inserted. StringTree content; }; template void StringTree::visit(Func&& func) const { size_t pos = 0; for (auto& branch: branches) { if (branch.index > pos) { func(text.slice(pos, branch.index)); pos = branch.index; } branch.content.visit(func); } if (text.size() > pos) { func(text.slice(pos, text.size())); } } inline void StringTree::fill(char* pos, size_t branchIndex) { KJ_IREQUIRE(pos == text.end() && branchIndex == branches.size(), kj::str(text.end() - pos, ' ', branches.size() - branchIndex).cStr()); } template void StringTree::fill(char* pos, size_t branchIndex, First&& first, Rest&&... rest) { pos = _::fill(pos, kj::fwd(first)); fill(pos, branchIndex, kj::fwd(rest)...); } template void StringTree::fill(char* pos, size_t branchIndex, StringTree&& first, Rest&&... rest) { branches[branchIndex].index = pos - text.begin(); branches[branchIndex].content = kj::mv(first); fill(pos, branchIndex + 1, kj::fwd(rest)...); } template void StringTree::fill(char* pos, size_t branchIndex, String&& first, Rest&&... rest) { branches[branchIndex].index = pos - text.begin(); branches[branchIndex].content = StringTree(kj::mv(first)); fill(pos, branchIndex + 1, kj::fwd(rest)...); } template StringTree StringTree::concat(Params&&... params) { StringTree result; result.size_ = _::sum({params.size()...}); result.text = heapString( _::sum({StringTree::flatSize(kj::fwd(params))...})); result.branches = heapArray( _::sum({StringTree::branchCount(kj::fwd(params))...})); result.fill(result.text.begin(), 0, kj::fwd(params)...); return result; } template StringTree strTree(Params&&... params) { return StringTree::concat(_::toStringTreeOrCharSequence(kj::fwd(params))...); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/hash.h0000644000175000017500000001611413650101756017246 0ustar00kentonkenton00000000000000// Copyright (c) 2018 Kenton Varda and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "string.h" KJ_BEGIN_HEADER namespace kj { namespace _ { // private struct HashCoder { // This is a dummy type with only one instance: HASHCODER (below). To make an arbitrary type // hashable, define `operator*(HashCoder, T)` to return any other type that is already hashable. // Be sure to declare the operator in the same namespace as `T` **or** in the global scope. // You can use the KJ_HASHCODE() macro as syntax sugar for this. // // A more usual way to accomplish what we're doing here would be to require that you define // a function like `hashCode(T)` and then rely on argument-dependent lookup. However, this has // the problem that it pollutes other people's namespaces and even the global namespace. For // example, some other project may already have functions called `hashCode` which do something // different. Declaring `operator*` with `HashCoder` as the left operand cannot conflict with // anything. uint operator*(ArrayPtr s) const; inline uint operator*(ArrayPtr s) const { return operator*(s.asConst()); } inline uint operator*(ArrayPtr s) const { return operator*(s.asBytes()); } inline uint operator*(ArrayPtr s) const { return operator*(s.asBytes()); } inline uint operator*(const Array& s) const { return operator*(s.asBytes()); } inline uint operator*(const Array& s) const { return operator*(s.asBytes()); } inline uint operator*(const String& s) const { return operator*(s.asBytes()); } inline uint operator*(const StringPtr& s) const { return operator*(s.asBytes()); } inline uint operator*(decltype(nullptr)) const { return 0; } inline uint operator*(bool b) const { return b; } inline uint operator*(char i) const { return i; } inline uint operator*(signed char i) const { return i; } inline uint operator*(unsigned char i) const { return i; } inline uint operator*(signed short i) const { return i; } inline uint operator*(unsigned short i) const { return i; } inline uint operator*(signed int i) const { return i; } inline uint operator*(unsigned int i) const { return i; } inline uint operator*(signed long i) const { if (sizeof(i) == sizeof(uint)) { return operator*(static_cast(i)); } else { return operator*(static_cast(i)); } } inline uint operator*(unsigned long i) const { if (sizeof(i) == sizeof(uint)) { return operator*(static_cast(i)); } else { return operator*(static_cast(i)); } } inline uint operator*(signed long long i) const { return operator*(static_cast(i)); } inline uint operator*(unsigned long long i) const { // Mix 64 bits to 32 bits in such a way that if our input values differ primarily in the upper // 32 bits, we still get good diffusion. (I.e. we cannot just truncate!) // // 49123 is an arbitrarily-chosen prime that is vaguely close to 2^16. // // TODO(perf): I just made this up. Is it OK? return static_cast(i) + static_cast(i >> 32) * 49123; } template uint operator*(T* ptr) const { if (sizeof(ptr) == sizeof(uint)) { // TODO(cleanup): In C++17, make the if() above be `if constexpr ()`, then change this to // reinterpret_cast(ptr). return reinterpret_cast(ptr); } else { return operator*(reinterpret_cast(ptr)); } } template () * instance())> uint operator*(ArrayPtr arr) const; template () * instance())> uint operator*(const Array& arr) const; template ().hashCode())> inline Result operator*(T&& value) const { return kj::fwd(value).hashCode(); } }; static KJ_CONSTEXPR(const) HashCoder HASHCODER = HashCoder(); } // namespace _ (private) #define KJ_HASHCODE(...) operator*(::kj::_::HashCoder, __VA_ARGS__) // Defines a hash function for a custom type. Example: // // class Foo {...}; // inline uint KJ_HASHCODE(const Foo& foo) { return kj::hashCode(foo.x, foo.y); } // // This allows Foo to be passed to hashCode(). // // The function should be declared either in the same namespace as the target type or in the global // namespace. It can return any type which itself is hashable -- that value will be hashed in turn // until a `uint` comes out. inline uint hashCode(uint value) { return value; } template inline uint hashCode(T&& value) { return hashCode(_::HASHCODER * kj::fwd(value)); } template inline uint hashCode(T&&... values) { uint hashes[] = { hashCode(kj::fwd(values))... }; return hashCode(kj::ArrayPtr(hashes).asBytes()); } // kj::hashCode() is a universal hashing function, like kj::str() is a universal stringification // function. Throw stuff in, get a hash code. // // Hash codes may differ between different processes, even running exactly the same code. // // NOT SUITABLE FOR CRYPTOGRAPHY. This is for hash tables, not crypto. // ======================================================================================= // inline implementation details namespace _ { // private template inline uint HashCoder::operator*(ArrayPtr arr) const { // Hash each array element to create a string of hashes, then murmur2 over those. // // TODO(perf): Choose a more-modern hash. (See hash.c++.) constexpr uint m = 0x5bd1e995; constexpr uint r = 24; uint h = arr.size() * sizeof(uint); for (auto& e: arr) { uint k = kj::hashCode(e); k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } h ^= h >> 13; h *= m; h ^= h >> 15; return h; } template inline uint HashCoder::operator*(const Array& arr) const { return operator*(arr.asPtr()); } } // namespace _ (private) } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/table.h0000644000175000017500000016114013650101756017412 0ustar00kentonkenton00000000000000// Copyright (c) 2018 Kenton Varda and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "common.h" #include "tuple.h" #include "vector.h" #include "function.h" #if _MSC_VER // Need _ReadWriteBarrier #if _MSC_VER < 1910 #include #else #include #endif #endif KJ_BEGIN_HEADER namespace kj { namespace _ { // private template class MappedIterable; template class TableMapping; template using TableIterable = MappedIterable>; } // namespace _ (private) template class Table { // A table with one or more indexes. This is the KJ alternative to map, set, unordered_map, and // unordered_set. // // Unlike a traditional map, which explicitly stores key/value pairs, a Table simply stores // "rows" of arbitrary type, and then lets the application specify how these should be indexed. // Rows could be indexed on a specific struct field, or they could be indexed based on a computed // property. An index could be hash-based or tree-based. Multiple indexes are supported, making // it easy to construct a "bimap". // // The table has deterministic iteration order based on the sequence of insertions and deletions. // In the case of only insertions, the iteration order is the order of insertion. If deletions // occur, then the current last row is moved to occupy the deleted slot. This determinism is // intended to be reliable for the purpose of testing, etc. // // Each index is a class that looks like: // // class Index { // public: // void reserve(size_t size); // // Called when Table::reserve() is called. // // SearchParam& keyForRow(const Row& row) const; // // Given a row, return a value appropriate to pass as SearchParams to the other functions. // // // In all function calls below, `SearchPrams` refers to whatever parameters the index // // supports for looking up a row in the table. // // template // kj::Maybe insert(kj::ArrayPtr table, size_t pos, SearchParams&&...); // // Called to indicate that we're about to insert a new row which will match the given // // search parameters, and will be located at the given position. If this index disallows // // duplicates and some other matching row already exists, then insert() returns the index // // of that row without modifying the index. If the row does not exist, then insert() // // updates the index to note that the new row is located at `pos`. Note that `table[pos]` // // may not be valid yet at the time of this call; the index must go on the search params // // alone. // // // // Insert may throw an exception, in which case the table will roll back insertion. // // template // void erase(kj::ArrayPtr table, size_t pos, SearchParams&&...); // // Called to indicate that the index must remove references to row number `pos`. The // // index must not attempt to access table[pos] directly -- in fact, `pos` may be equal to // // `table.size()`, i.e., may be out-of-bounds (this happens when rolling back a failed // // insertion). Instead, the index can use the search params to search for the row -- they // // will either be the same as the params passed to insert(), or will be a single value of // // type `Row&`. // // // // erase() called immediately after a successful insert() must not throw an exception, as // // it may be called during unwind. // // template // void move(kj::ArrayPtr table, size_t oldPos, size_t newPos, SearchParams&&...); // // Called when a row is about to be moved from `oldPos` to `newPos` in the table. The // // index should update it to the new location. Neither `table[oldPos]` nor `table[newPos]` // // is valid during the call -- use the search params to find the row. Before this call // // `oldPos` is indexed and `newPos` is not -- after the call, the opposite is true. // // // // This should never throw; if it does the table may be corrupted. // // class Iterator; // Behaves like a C++ iterator over size_t values. // class Iterable; // Has begin() and end() methods returning iterators. // // template // Maybe find(kj::ArrayPtr table, SearchParams&&...) const; // // Optional. Implements Table::find(...). // // template // Iterable range(kj::ArrayPtr table, SearchParams&&...) const; // // Optional. Implements Table::range(...). // // Iterator begin() const; // Iterator end() const; // // Optional. Implements Table::ordered(). // }; public: Table(); Table(Indexes&&... indexes); void reserve(size_t size); // Pre-allocates space for a table of the given size. Normally a Table grows by re-allocating // its backing array whenever more space is needed. Reserving in advance avoids redundantly // re-allocating as the table grows. size_t size() const; size_t capacity() const; void clear(); Row* begin(); Row* end(); const Row* begin() const; const Row* end() const; Row& insert(Row&& row); Row& insert(const Row& row); // Inserts a new row. Throws an exception if this would violate the uniqueness constraints of any // of the indexes. template void insertAll(Collection&& collection); template void insertAll(Collection& collection); // Given an iterable collection of Rows, inserts all of them into this table. If the input is // an rvalue, the rows will be moved rather than copied. // // If an insertion throws (e.g. because it violates a uniqueness constraint of some index), // subsequent insertions do not occur, but previous insertions remain inserted. template Row& upsert(Row&& row, UpdateFunc&& update); template Row& upsert(const Row& row, UpdateFunc&& update); // Tries to insert a new row. However, if a duplicate already exists (according to some index), // then update(Row& existingRow, Row&& newRow) is called to modify the existing row. template kj::Maybe find(Params&&... params); template kj::Maybe find(Params&&... params) const; // Using the given index, search for a matching row. What parameters are accepted depends on the // index. Not all indexes support this method -- "multimap" indexes may support only range(). template Row& findOrCreate(Params&&... params, Func&& createFunc); // Like find(), but if the row doesn't exist, call a function to create it. createFunc() must // return `Row` or something that implicitly converts to `Row`. // // NOTE: C++ doesn't actually properly support inferring types of a parameter pack at the // beginning of an argument list, but we define a hack to support it below. Don't worry about // it. template auto range(Params&&... params); template auto range(Params&&... params) const; // Using the given index, look up a range of values, returning an iterable. What parameters are // accepted depends on the index. Not all indexes support this method (in particular, unique // indexes normally don't). template _::TableIterable ordered(); template _::TableIterable ordered() const; // Returns an iterable over the whole table ordered using the given index. Not all indexes // support this method. template bool eraseMatch(Params&&... params); // Erase the row that would be matched by `find(params)`. Returns true if there was a // match. template size_t eraseRange(Params&&... params); // Erase the row that would be matched by `range(params)`. Returns the number of // elements erased. void erase(Row& row); // Erase the given row. // // WARNING: This invalidates all iterators, so you can't iterate over rows and erase them this // way. Use `eraseAll()` for that. Row release(Row& row); // Remove the given row from the table and return it in one operation. // // WARNING: This invalidates all iterators, so you can't iterate over rows and release them this // way. template ()(instance()))> size_t eraseAll(Predicate&& predicate); // Erase all rows for which predicate(row) returns true. This scans over the entire table. template ().begin()), bool = true> size_t eraseAll(Collection&& collection); // Erase all rows in the given iterable collection of rows. This carefully marks rows for // deletion in a first pass then deletes them in a second. template kj::Maybe find(Params&&... params); template kj::Maybe find(Params&&... params) const; template Row& findOrCreate(Params&&... params, Func&& createFunc); template auto range(Params&&... params); template auto range(Params&&... params) const; template _::TableIterable>&> ordered(); template _::TableIterable>&> ordered() const; template bool eraseMatch(Params&&... params); template size_t eraseRange(Params&&... params); // Methods which take an index type as a template parameter can also take an index number. This // is useful particularly when you have multiple indexes of the same type but different runtime // properties. Additionally, you can omit the template parameter altogether to use the first // index. template void verify(); // Checks the integrity of indexes, throwing an exception if there are any problems. This is // intended to be called within the unit test for an index. template Row& findOrCreate(First&& first, Rest&&... rest); template Row& findOrCreate(First&& first, Rest&&... rest); // HACK: A parameter pack can only be inferred if it lives at the end of the argument list, so // the findOrCreate() definitions from earlier won't actually work. These ones will, but we // have to do some annoying things inside to regroup the arguments. private: Vector rows; Tuple indexes; template = sizeof...(Indexes))> class Impl; template class FindOrCreateImpl; template struct FindOrCreateHack; void eraseImpl(size_t pos); template size_t eraseAllImpl(Collection&& collection); }; template class HashIndex; // A Table index based on a hash table. // // This implementation: // * Is based on linear probing, not chaining. It is important to use a high-quality hash function. // Use the KJ hashing library if possible. // * Is limited to tables of 2^30 rows or less, mainly to allow for tighter packing with 32-bit // integers instead of 64-bit. // * Caches hash codes so that each table row need only be hashed once, and never checks equality // unless hash codes have already been determined to be equal. // // The `Callbacks` type defines how to compute hash codes and equality. It should be defined like: // // class Callbacks { // public: // // In this interface, `SearchParams...` means whatever parameters you want to support in // // a call to table.find(...). By overloading the calls to support various inputs, you can // // affect what table.find(...) accepts. // // SearchParam& keyForRow(const Row& row); // // Given a row of the table, return the SearchParams that might be passed to the other // // methods to match this row. // // bool matches(const Row&, SearchParams&&...) const; // // Returns true if the row on the left matches thes search params on the right. // // uint hashCode(SearchParams&&...) const; // // Computes the hash code of the given search params. Matching rows (as determined by // // matches()) must have the same hash code. Non-matching rows should have different hash // // codes, to the maximum extent possible. Non-matching rows with the same hash code hurt // // performance. // }; // // If your `Callbacks` type has dynamic state, you may pass its constructor parameters as the // constructor parameters to `HashIndex`. template class TreeIndex; // A Table index based on a B-tree. // // This allows sorted iteration over rows. // // The `Callbacks` type defines how to compare rows. It should be defined like: // // class Callbacks { // public: // // In this interface, `SearchParams...` means whatever parameters you want to support in // // a call to table.find(...). By overloading the calls to support various inputs, you can // // affect what table.find(...) accepts. // // SearchParam& keyForRow(const Row& row); // // Given a row of the table, return the SearchParams that might be passed to the other // // methods to match this row. // // bool isBefore(const Row&, SearchParams&&...) const; // // Returns true if the row on the left comes before the search params on the right. // // bool matches(const Row&, SearchParams&&...) const; // // Returns true if the row "matches" the search params. // }; // ======================================================================================= // inline implementation details namespace _ { // private KJ_NORETURN(void throwDuplicateTableRow()); template ().size())> inline void tryReserveSize(Dst& dst, Src&& src) { dst.reserve(dst.size() + src.size()); } template inline void tryReserveSize(Params&&...) {} // If `src` has a `.size()` method, call dst.reserve(dst.size() + src.size()). // Otherwise, do nothing. template class MappedIterator: private Mapping { // An iterator that wraps some other iterator and maps the values through a mapping function. // The type `Mapping` must define a method `map()` which performs this mapping. // // TODO(cleanup): This seems generally useful. Should we put it somewhere resuable? public: template MappedIterator(Inner inner, Params&&... params) : Mapping(kj::fwd(params)...), inner(inner) {} inline auto operator->() const { return &Mapping::map(*inner); } inline decltype(auto) operator* () const { return Mapping::map(*inner); } inline decltype(auto) operator[](size_t index) const { return Mapping::map(inner[index]); } inline MappedIterator& operator++() { ++inner; return *this; } inline MappedIterator operator++(int) { return MappedIterator(inner++, *this); } inline MappedIterator& operator--() { --inner; return *this; } inline MappedIterator operator--(int) { return MappedIterator(inner--, *this); } inline MappedIterator& operator+=(ptrdiff_t amount) { inner += amount; return *this; } inline MappedIterator& operator-=(ptrdiff_t amount) { inner -= amount; return *this; } inline MappedIterator operator+ (ptrdiff_t amount) const { return MappedIterator(inner + amount, *this); } inline MappedIterator operator- (ptrdiff_t amount) const { return MappedIterator(inner - amount, *this); } inline ptrdiff_t operator- (const MappedIterator& other) const { return inner - other.inner; } inline bool operator==(const MappedIterator& other) const { return inner == other.inner; } inline bool operator!=(const MappedIterator& other) const { return inner != other.inner; } inline bool operator<=(const MappedIterator& other) const { return inner <= other.inner; } inline bool operator>=(const MappedIterator& other) const { return inner >= other.inner; } inline bool operator< (const MappedIterator& other) const { return inner < other.inner; } inline bool operator> (const MappedIterator& other) const { return inner > other.inner; } private: Inner inner; }; template class MappedIterable: private Mapping { // An iterable that wraps some other iterable and maps the values through a mapping function. // The type `Mapping` must define a method `map()` which performs this mapping. // // TODO(cleanup): This seems generally useful. Should we put it somewhere resuable? public: template MappedIterable(Inner inner, Params&&... params) : Mapping(kj::fwd(params)...), inner(inner) {} typedef Decay().begin())> InnerIterator; typedef MappedIterator Iterator; typedef Decay().begin())> InnerConstIterator; typedef MappedIterator ConstIterator; inline Iterator begin() { return { inner.begin(), (Mapping&)*this }; } inline Iterator end() { return { inner.end(), (Mapping&)*this }; } inline ConstIterator begin() const { return { inner.begin(), (const Mapping&)*this }; } inline ConstIterator end() const { return { inner.end(), (const Mapping&)*this }; } private: Inner inner; }; template class TableMapping { public: TableMapping(Row* table): table(table) {} Row& map(size_t i) const { return table[i]; } private: Row* table; }; template class TableUnmapping { public: TableUnmapping(Row* table): table(table) {} size_t map(Row& row) const { return &row - table; } size_t map(Row* row) const { return row - table; } private: Row* table; }; } // namespace _ (private) template template class Table::Impl { public: static void reserve(Table& table, size_t size) { get(table.indexes).reserve(size); Impl::reserve(table, size); } static void clear(Table& table) { get(table.indexes).clear(); Impl::clear(table); } static kj::Maybe insert(Table& table, size_t pos, Row& row, uint skip) { if (skip == index) { return Impl::insert(table, pos, row, skip); } auto& indexObj = get(table.indexes); KJ_IF_MAYBE(existing, indexObj.insert(table.rows.asPtr(), pos, indexObj.keyForRow(row))) { return *existing; } bool success = false; KJ_DEFER(if (!success) { indexObj.erase(table.rows.asPtr(), pos, indexObj.keyForRow(row)); }); auto result = Impl::insert(table, pos, row, skip); success = result == nullptr; return result; } static void erase(Table& table, size_t pos, Row& row) { auto& indexObj = get(table.indexes); indexObj.erase(table.rows.asPtr(), pos, indexObj.keyForRow(row)); Impl::erase(table, pos, row); } static void move(Table& table, size_t oldPos, size_t newPos, Row& row) { auto& indexObj = get(table.indexes); indexObj.move(table.rows.asPtr(), oldPos, newPos, indexObj.keyForRow(row)); Impl::move(table, oldPos, newPos, row); } }; template template class Table::Impl { public: static void reserve(Table& table, size_t size) {} static void clear(Table& table) {} static kj::Maybe insert(Table& table, size_t pos, Row& row, uint skip) { return nullptr; } static void erase(Table& table, size_t pos, Row& row) {} static void move(Table& table, size_t oldPos, size_t newPos, Row& row) {} }; template Table::Table() {} template Table::Table(Indexes&&... indexes) : indexes(tuple(kj::fwd(indexes)...)) {} template void Table::reserve(size_t size) { rows.reserve(size); Impl<>::reserve(*this, size); } template size_t Table::size() const { return rows.size(); } template void Table::clear() { Impl<>::clear(*this); rows.clear(); } template size_t Table::capacity() const { return rows.capacity(); } template Row* Table::begin() { return rows.begin(); } template Row* Table::end() { return rows.end(); } template const Row* Table::begin() const { return rows.begin(); } template const Row* Table::end() const { return rows.end(); } template Row& Table::insert(Row&& row) { KJ_IF_MAYBE(existing, Impl<>::insert(*this, rows.size(), row, kj::maxValue)) { _::throwDuplicateTableRow(); } else { return rows.add(kj::mv(row)); } } template Row& Table::insert(const Row& row) { return insert(kj::cp(row)); } template template void Table::insertAll(Collection&& collection) { _::tryReserveSize(*this, collection); for (auto& row: collection) { insert(kj::mv(row)); } } template template void Table::insertAll(Collection& collection) { _::tryReserveSize(*this, collection); for (auto& row: collection) { insert(row); } } template template Row& Table::upsert(Row&& row, UpdateFunc&& update) { KJ_IF_MAYBE(existing, Impl<>::insert(*this, rows.size(), row, kj::maxValue)) { update(rows[*existing], kj::mv(row)); return rows[*existing]; } else { return rows.add(kj::mv(row)); } } template template Row& Table::upsert(const Row& row, UpdateFunc&& update) { return upsert(kj::cp(row), kj::fwd(update)); } template template kj::Maybe Table::find(Params&&... params) { return find>()>(kj::fwd(params)...); } template template kj::Maybe Table::find(Params&&... params) { KJ_IF_MAYBE(pos, get(indexes).find(rows.asPtr(), kj::fwd(params)...)) { return rows[*pos]; } else { return nullptr; } } template template kj::Maybe Table::find(Params&&... params) const { return find>()>(kj::fwd(params)...); } template template kj::Maybe Table::find(Params&&... params) const { KJ_IF_MAYBE(pos, get(indexes).find(rows.asPtr(), kj::fwd(params)...)) { return rows[*pos]; } else { return nullptr; } } template template class Table::FindOrCreateImpl { public: template static Row& apply(Table& table, Params&&... params, Func&& createFunc) { auto pos = table.rows.size(); KJ_IF_MAYBE(existing, get(table.indexes).insert(table.rows.asPtr(), pos, params...)) { return table.rows[*existing]; } else { bool success = false; KJ_DEFER({ if (!success) { get(table.indexes).erase(table.rows.asPtr(), pos, params...); } }); auto& newRow = table.rows.add(createFunc()); KJ_DEFER({ if (!success) { table.rows.removeLast(); } }); if (Table::template Impl<>::insert(table, pos, newRow, index) == nullptr) { success = true; } else { _::throwDuplicateTableRow(); } return newRow; } } }; template template struct Table::FindOrCreateHack<_::Tuple, U, V, W...> : public FindOrCreateHack<_::Tuple, V, W...> {}; template template struct Table::FindOrCreateHack<_::Tuple, U> : public FindOrCreateImpl {}; // This awful hack works around C++'s lack of support for parameter packs anywhere other than at // the end of an argument list. We accumulate all of the types except for the last one into a // Tuple, then forward to FindOrCreateImpl with the last parameter as the Func. template template Row& Table::findOrCreate(First&& first, Rest&&... rest) { return findOrCreate>()>( kj::fwd(first), kj::fwd(rest)...); } template template Row& Table::findOrCreate(First&& first, Rest&&... rest) { return FindOrCreateHack<_::Tuple<>, First, Rest...>::template apply( *this, kj::fwd(first), kj::fwd(rest)...); } template template auto Table::range(Params&&... params) { return range>()>(kj::fwd(params)...); } template template auto Table::range(Params&&... params) { auto inner = get(indexes).range(rows.asPtr(), kj::fwd(params)...); return _::TableIterable(kj::mv(inner), rows.begin()); } template template auto Table::range(Params&&... params) const { return range>()>(kj::fwd(params)...); } template template auto Table::range(Params&&... params) const { auto inner = get(indexes).range(rows.asPtr(), kj::fwd(params)...); return _::TableIterable(kj::mv(inner), rows.begin()); } template template _::TableIterable Table::ordered() { return ordered>()>(); } template template _::TableIterable>&> Table::ordered() { return { get(indexes), rows.begin() }; } template template _::TableIterable Table::ordered() const { return ordered>()>(); } template template _::TableIterable>&> Table::ordered() const { return { get(indexes), rows.begin() }; } template template bool Table::eraseMatch(Params&&... params) { return eraseMatch>()>(kj::fwd(params)...); } template template bool Table::eraseMatch(Params&&... params) { KJ_IF_MAYBE(pos, get(indexes).find(rows.asPtr(), kj::fwd(params)...)) { eraseImpl(*pos); return true; } else { return false; } } template template size_t Table::eraseRange(Params&&... params) { return eraseRange>()>(kj::fwd(params)...); } template template size_t Table::eraseRange(Params&&... params) { return eraseAllImpl(get(indexes).range(rows.asPtr(), kj::fwd(params)...)); } template template void Table::verify() { get(indexes).verify(rows.asPtr()); } template void Table::erase(Row& row) { KJ_IREQUIRE(&row >= rows.begin() && &row < rows.end(), "row is not a member of this table"); eraseImpl(&row - rows.begin()); } template void Table::eraseImpl(size_t pos) { Impl<>::erase(*this, pos, rows[pos]); size_t back = rows.size() - 1; if (pos != back) { Impl<>::move(*this, back, pos, rows[back]); rows[pos] = kj::mv(rows[back]); } rows.removeLast(); } template Row Table::release(Row& row) { KJ_IREQUIRE(&row >= rows.begin() && &row < rows.end(), "row is not a member of this table"); size_t pos = &row - rows.begin(); Impl<>::erase(*this, pos, row); Row result = kj::mv(row); size_t back = rows.size() - 1; if (pos != back) { Impl<>::move(*this, back, pos, rows[back]); row = kj::mv(rows[back]); } rows.removeLast(); return result; } template template size_t Table::eraseAll(Predicate&& predicate) { size_t count = 0; for (size_t i = 0; i < rows.size();) { if (predicate(rows[i])) { eraseImpl(i); ++count; // eraseImpl() replaces the erased row with the last row, so don't increment i here; repeat // with the same i. } else { ++i; } } return count; } template template size_t Table::eraseAll(Collection&& collection) { return eraseAllImpl(_::MappedIterable>( collection, rows.begin())); } template template size_t Table::eraseAllImpl(Collection&& collection) { // We need to transform the collection of row numbers into a sequence of erasures, accounting // for the fact that each erasure re-positions the last row into its slot. Vector erased; _::tryReserveSize(erased, collection); for (size_t pos: collection) { while (pos >= rows.size() - erased.size()) { // Oops, the next item to be erased is already scheduled to be moved to a different location // due to a previous erasure. Figure out where it will be at this point. size_t erasureNumber = rows.size() - pos - 1; pos = erased[erasureNumber]; } erased.add(pos); } // Now we can execute the sequence of erasures. for (size_t pos: erased) { eraseImpl(pos); } return erased.size(); } // ----------------------------------------------------------------------------- // Hash table index namespace _ { // private void logHashTableInconsistency(); struct HashBucket { uint hash; uint value; HashBucket() = default; HashBucket(uint hash, uint pos) : hash(hash), value(pos + 2) {} inline bool isEmpty() const { return value == 0; } inline bool isErased() const { return value == 1; } inline bool isOccupied() const { return value >= 2; } template inline Row& getRow(ArrayPtr table) const { return table[getPos()]; } template inline const Row& getRow(ArrayPtr table) const { return table[getPos()]; } inline bool isPos(uint pos) const { return pos + 2 == value; } inline uint getPos() const { KJ_IASSERT(value >= 2); return value - 2; } inline void setEmpty() { value = 0; } inline void setErased() { value = 1; } inline void setPos(uint pos) { value = pos + 2; } }; inline size_t probeHash(const kj::Array& buckets, size_t i) { // TODO(perf): Is linear probing OK or should we do something fancier? if (++i == buckets.size()) { return 0; } else { return i; } } kj::Array rehash(kj::ArrayPtr oldBuckets, size_t targetSize); uint chooseBucket(uint hash, uint count); } // namespace _ (private) template class HashIndex { public: HashIndex() KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY template HashIndex(Params&&... params): cb(kj::fwd(params)...) {} void reserve(size_t size) { if (buckets.size() < size * 2) { rehash(size); } } void clear() { erasedCount = 0; memset(buckets.begin(), 0, buckets.asBytes().size()); } template decltype(auto) keyForRow(Row&& row) const { return cb.keyForRow(kj::fwd(row)); } template kj::Maybe insert(kj::ArrayPtr table, size_t pos, Params&&... params) { if (buckets.size() * 2 < (table.size() + 1 + erasedCount) * 3) { // Load factor is more than 2/3, let's rehash. rehash(kj::max(buckets.size() * 2, (table.size() + 1) * 2)); } uint hashCode = cb.hashCode(params...); Maybe<_::HashBucket&> erasedSlot; for (uint i = _::chooseBucket(hashCode, buckets.size());; i = _::probeHash(buckets, i)) { auto& bucket = buckets[i]; if (bucket.isEmpty()) { // no duplicates found KJ_IF_MAYBE(s, erasedSlot) { --erasedCount; *s = { hashCode, uint(pos) }; } else { bucket = { hashCode, uint(pos) }; } return nullptr; } else if (bucket.isErased()) { // We can fill in the erased slot. However, we have to keep searching to make sure there // are no duplicates before we do that. if (erasedSlot == nullptr) { erasedSlot = bucket; } } else if (bucket.hash == hashCode && cb.matches(bucket.getRow(table), params...)) { // duplicate row return size_t(bucket.getPos()); } } } template void erase(kj::ArrayPtr table, size_t pos, Params&&... params) { uint hashCode = cb.hashCode(params...); for (uint i = _::chooseBucket(hashCode, buckets.size());; i = _::probeHash(buckets, i)) { auto& bucket = buckets[i]; if (bucket.isPos(pos)) { // found it ++erasedCount; bucket.setErased(); return; } else if (bucket.isEmpty()) { // can't find the bucket, something is very wrong _::logHashTableInconsistency(); return; } } } template void move(kj::ArrayPtr table, size_t oldPos, size_t newPos, Params&&... params) { uint hashCode = cb.hashCode(params...); for (uint i = _::chooseBucket(hashCode, buckets.size());; i = _::probeHash(buckets, i)) { auto& bucket = buckets[i]; if (bucket.isPos(oldPos)) { // found it bucket.setPos(newPos); return; } else if (bucket.isEmpty()) { // can't find the bucket, something is very wrong _::logHashTableInconsistency(); return; } } } template Maybe find(kj::ArrayPtr table, Params&&... params) const { if (buckets.size() == 0) return nullptr; uint hashCode = cb.hashCode(params...); for (uint i = _::chooseBucket(hashCode, buckets.size());; i = _::probeHash(buckets, i)) { auto& bucket = buckets[i]; if (bucket.isEmpty()) { // not found. return nullptr; } else if (bucket.isErased()) { // skip, keep searching } else if (bucket.hash == hashCode && cb.matches(bucket.getRow(table), params...)) { // found return size_t(bucket.getPos()); } } } // No begin() nor end() because hash tables are not usefully ordered. private: Callbacks cb; size_t erasedCount = 0; Array<_::HashBucket> buckets; void rehash(size_t targetSize) { buckets = _::rehash(buckets, targetSize); } }; // ----------------------------------------------------------------------------- // BTree index namespace _ { // private KJ_ALWAYS_INLINE(void compilerBarrier()); void compilerBarrier() { // Make sure that reads occurring before this call cannot be re-ordered to happen after // writes that occur after this call. We need this in a couple places below to prevent C++ // strict aliasing rules from breaking things. #if _MSC_VER _ReadWriteBarrier(); #else __asm__ __volatile__("": : :"memory"); #endif } template inline void acopy(T* to, T* from, size_t size) { memcpy(to, from, size * sizeof(T)); } template inline void amove(T* to, T* from, size_t size) { memmove(to, from, size * sizeof(T)); } template inline void azero(T* ptr, size_t size) { memset(ptr, 0, size * sizeof(T)); } // memcpy/memmove/memset variants that count size in elements, not bytes. // // TODO(cleanup): These are generally useful, put them somewhere. class BTreeImpl { public: class Iterator; class MaybeUint; struct NodeUnion; struct Leaf; struct Parent; struct Freelisted; class SearchKey { // Passed to methods that need to search the tree. This class allows most of the B-tree // implementation to be kept out of templates, avoiding code bloat, at the cost of some // performance trade-off. In order to lessen the performance cost of virtual calls, we design // this interface so that it only needs to be called once per tree node, rather than once per // comparison. public: virtual uint search(const Parent& parent) const = 0; virtual uint search(const Leaf& leaf) const = 0; // Binary search for the first key/row in the parent/leaf that is equal to or comes after the // search key. virtual bool isAfter(uint rowIndex) const = 0; // Returns true if the key comes after the value in the given row. }; BTreeImpl(); ~BTreeImpl() noexcept(false); void logInconsistency() const; void reserve(size_t size); void clear(); Iterator begin() const; Iterator end() const; Iterator search(const SearchKey& searchKey) const; // Find the "first" row (in sorted order) for which searchKey.isAfter(rowNumber) returns true. Iterator insert(const SearchKey& searchKey); // Like search() but ensures that there is room in the leaf node to insert a new row. void erase(uint row, const SearchKey& searchKey); // Erase the given row number from the tree. searchKey.isAfter() returns true for the given row // and all rows after it. void renumber(uint oldRow, uint newRow, const SearchKey& searchKey); // Renumber the given row from oldRow to newRow. searchKey.isAfter() returns true for oldRow and // all rows after it. (It will not be called on newRow.) void verify(size_t size, FunctionParam); private: NodeUnion* tree; // allocated with aligned_alloc aligned to cache lines uint treeCapacity; uint height; // height of *parent* tree -- does not include the leaf level uint freelistHead; uint freelistSize; uint beginLeaf; uint endLeaf; void growTree(uint minCapacity = 0); template struct AllocResult; template inline AllocResult alloc(); inline void free(uint pos); inline uint split(Parent& src, uint srcPos, Parent& dst, uint dstPos); inline uint split(Leaf& dst, uint dstPos, Leaf& src, uint srcPos); inline void merge(Parent& dst, uint dstPos, uint pivot, Parent& src); inline void merge(Leaf& dst, uint dstPos, uint pivot, Leaf& src); inline void move(Parent& dst, uint dstPos, Parent& src); inline void move(Leaf& dst, uint dstPos, Leaf& src); inline void rotateLeft( Parent& left, Parent& right, Parent& parent, uint indexInParent, MaybeUint*& fixup); inline void rotateLeft( Leaf& left, Leaf& right, Parent& parent, uint indexInParent, MaybeUint*& fixup); inline void rotateRight(Parent& left, Parent& right, Parent& parent, uint indexInParent); inline void rotateRight(Leaf& left, Leaf& right, Parent& parent, uint indexInParent); template inline Node& insertHelper(const SearchKey& searchKey, Node& node, Parent* parent, uint indexInParent, uint pos); template inline Node& eraseHelper( Node& node, Parent* parent, uint indexInParent, uint pos, MaybeUint*& fixup); size_t verifyNode(size_t size, FunctionParam&, uint pos, uint height, MaybeUint maxRow); static const NodeUnion EMPTY_NODE; }; class BTreeImpl::MaybeUint { // A nullable uint, using the value zero to mean null and shifting all other values up by 1. public: MaybeUint() = default; inline MaybeUint(uint i): i(i - 1) {} inline MaybeUint(decltype(nullptr)): i(0) {} inline bool operator==(decltype(nullptr)) const { return i == 0; } inline bool operator==(uint j) const { return i == j + 1; } inline bool operator==(const MaybeUint& other) const { return i == other.i; } inline bool operator!=(decltype(nullptr)) const { return i != 0; } inline bool operator!=(uint j) const { return i != j + 1; } inline bool operator!=(const MaybeUint& other) const { return i != other.i; } inline MaybeUint& operator=(decltype(nullptr)) { i = 0; return *this; } inline MaybeUint& operator=(uint j) { i = j + 1; return *this; } inline uint operator*() const { KJ_IREQUIRE(i != 0); return i - 1; } template inline bool check(Func& func) const { return i != 0 && func(i - 1); } // Equivalent to *this != nullptr && func(**this) private: uint i; }; struct BTreeImpl::Leaf { uint next; uint prev; // Pointers to next and previous nodes at the same level, used for fast iteration. static constexpr size_t NROWS = 14; MaybeUint rows[NROWS]; // Pointers to table rows, offset by 1 so that 0 is an empty value. inline bool isFull() const; inline bool isMostlyFull() const; inline bool isHalfFull() const; inline void insert(uint i, uint newRow) { KJ_IREQUIRE(rows[Leaf::NROWS - 1] == nullptr); // check not full amove(rows + i + 1, rows + i, Leaf::NROWS - (i + 1)); rows[i] = newRow; } inline void erase(uint i) { KJ_IREQUIRE(rows[0] != nullptr); // check not empty amove(rows + i, rows + i + 1, Leaf::NROWS - (i + 1)); rows[Leaf::NROWS - 1] = nullptr; } inline uint size() const { static_assert(Leaf::NROWS == 14, "logic here needs updating"); // Binary search for first empty element in `rows`, or return 14 if no empty elements. We do // this in a branch-free manner. Since there are 15 possible results (0 through 14, inclusive), // this isn't a perfectly balanced binary search. We carefully choose the split points so that // there's no way we'll try to dereference row[14] or later (which would be a buffer overflow). uint i = (rows[6] != nullptr) * 7; i += (rows[i + 3] != nullptr) * 4; i += (rows[i + 1] != nullptr) * 2; i += (rows[i ] != nullptr); return i; } template inline uint binarySearch(Func& predicate) const { // Binary search to find first row for which predicate(row) is false. static_assert(Leaf::NROWS == 14, "logic here needs updating"); // See comments in size(). uint i = (rows[6].check(predicate)) * 7; i += (rows[i + 3].check(predicate)) * 4; i += (rows[i + 1].check(predicate)) * 2; if (i != 6) { // don't redundantly check row 6 i += (rows[i ].check(predicate)); } return i; } }; struct BTreeImpl::Parent { uint unused; // Not used. May be arbitrarily non-zero due to overlap with Freelisted::nextOffset. static constexpr size_t NKEYS = 7; MaybeUint keys[NKEYS]; // Pointers to table rows, offset by 1 so that 0 is an empty value. static constexpr size_t NCHILDREN = NKEYS + 1; uint children[NCHILDREN]; // Pointers to children. Not offset because the root is always at position 0, and a pointer // to the root would be nonsensical. inline bool isFull() const; inline bool isMostlyFull() const; inline bool isHalfFull() const; inline void initRoot(uint key, uint leftChild, uint rightChild); inline void insertAfter(uint i, uint splitKey, uint child); inline void eraseAfter(uint i); inline uint keyCount() const { static_assert(Parent::NKEYS == 7, "logic here needs updating"); // Binary search for first empty element in `keys`, or return 7 if no empty elements. We do // this in a branch-free manner. Since there are 8 possible results (0 through 7, inclusive), // this is a perfectly balanced binary search. uint i = (keys[3] != nullptr) * 4; i += (keys[i + 1] != nullptr) * 2; i += (keys[i ] != nullptr); return i; } template inline uint binarySearch(Func& predicate) const { // Binary search to find first key for which predicate(key) is false. static_assert(Parent::NKEYS == 7, "logic here needs updating"); // See comments in size(). uint i = (keys[3].check(predicate)) * 4; i += (keys[i + 1].check(predicate)) * 2; i += (keys[i ].check(predicate)); return i; } }; struct BTreeImpl::Freelisted { int nextOffset; // The next node in the freelist is at: this + 1 + nextOffset // // Hence, newly-allocated space can initialize this to zero. uint zero[15]; // Freelisted entries are always zero'd. }; struct BTreeImpl::NodeUnion { union { Freelisted freelist; // If this node is in the freelist. Leaf leaf; // If this node is a leaf. Parent parent; // If this node is not a leaf. }; inline operator Leaf&() { return leaf; } inline operator Parent&() { return parent; } inline operator const Leaf&() const { return leaf; } inline operator const Parent&() const { return parent; } }; static_assert(sizeof(BTreeImpl::Parent) == 64, "BTreeImpl::Parent should be optimized to fit a cache line"); static_assert(sizeof(BTreeImpl::Leaf) == 64, "BTreeImpl::Leaf should be optimized to fit a cache line"); static_assert(sizeof(BTreeImpl::Freelisted) == 64, "BTreeImpl::Freelisted should be optimized to fit a cache line"); static_assert(sizeof(BTreeImpl::NodeUnion) == 64, "BTreeImpl::NodeUnion should be optimized to fit a cache line"); bool BTreeImpl::Leaf::isFull() const { return rows[Leaf::NROWS - 1] != nullptr; } bool BTreeImpl::Leaf::isMostlyFull() const { return rows[Leaf::NROWS / 2] != nullptr; } bool BTreeImpl::Leaf::isHalfFull() const { KJ_IASSERT(rows[Leaf::NROWS / 2 - 1] != nullptr); return rows[Leaf::NROWS / 2] == nullptr; } bool BTreeImpl::Parent::isFull() const { return keys[Parent::NKEYS - 1] != nullptr; } bool BTreeImpl::Parent::isMostlyFull() const { return keys[Parent::NKEYS / 2] != nullptr; } bool BTreeImpl::Parent::isHalfFull() const { KJ_IASSERT(keys[Parent::NKEYS / 2 - 1] != nullptr); return keys[Parent::NKEYS / 2] == nullptr; } class BTreeImpl::Iterator { public: Iterator(const NodeUnion* tree, const Leaf* leaf, uint row) : tree(tree), leaf(leaf), row(row) {} size_t operator*() const { KJ_IREQUIRE(row < Leaf::NROWS && leaf->rows[row] != nullptr, "tried to dereference end() iterator"); return *leaf->rows[row]; } inline Iterator& operator++() { KJ_IREQUIRE(leaf->rows[row] != nullptr, "B-tree iterator overflow"); ++row; if (row >= Leaf::NROWS || leaf->rows[row] == nullptr) { if (leaf->next == 0) { // at end; stay on current leaf } else { leaf = &tree[leaf->next].leaf; row = 0; } } return *this; } inline Iterator operator++(int) { Iterator other = *this; ++*this; return other; } inline Iterator& operator--() { if (row == 0) { KJ_IREQUIRE(leaf->prev != 0, "B-tree iterator underflow"); leaf = &tree[leaf->prev].leaf; row = leaf->size() - 1; } else { --row; } return *this; } inline Iterator operator--(int) { Iterator other = *this; --*this; return other; } inline bool operator==(const Iterator& other) const { return leaf == other.leaf && row == other.row; } inline bool operator!=(const Iterator& other) const { return leaf != other.leaf || row != other.row; } bool isEnd() { return row == Leaf::NROWS || leaf->rows[row] == nullptr; } void insert(BTreeImpl& impl, uint newRow) { KJ_IASSERT(impl.tree == tree); const_cast(leaf)->insert(row, newRow); } void erase(BTreeImpl& impl) { KJ_IASSERT(impl.tree == tree); const_cast(leaf)->erase(row); } void replace(BTreeImpl& impl, uint newRow) { KJ_IASSERT(impl.tree == tree); const_cast(leaf)->rows[row] = newRow; } private: const NodeUnion* tree; const Leaf* leaf; uint row; }; template class IterRange { public: inline IterRange(Iterator b, Iterator e): b(b), e(e) {} inline Iterator begin() const { return b; } inline Iterator end() const { return e; } private: Iterator b; Iterator e; }; template inline IterRange> iterRange(Iterator b, Iterator e) { return { b, e }; } inline BTreeImpl::Iterator BTreeImpl::begin() const { return { tree, &tree[beginLeaf].leaf, 0 }; } inline BTreeImpl::Iterator BTreeImpl::end() const { auto& leaf = tree[endLeaf].leaf; return { tree, &leaf, leaf.size() }; } } // namespace _ (private) template class TreeIndex { public: TreeIndex() KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY template TreeIndex(Params&&... params): cb(kj::fwd(params)...) {} template void verify(kj::ArrayPtr table) { impl.verify(table.size(), [&](uint i, uint j) { return cb.isBefore(table[i], table[j]); }); } inline void reserve(size_t size) { impl.reserve(size); } inline void clear() { impl.clear(); } inline auto begin() const { return impl.begin(); } inline auto end() const { return impl.end(); } template decltype(auto) keyForRow(Row&& row) const { return cb.keyForRow(kj::fwd(row)); } template kj::Maybe insert(kj::ArrayPtr table, size_t pos, Params&&... params) { auto iter = impl.insert(searchKey(table, params...)); if (!iter.isEnd() && cb.matches(table[*iter], params...)) { return *iter; } else { iter.insert(impl, pos); return nullptr; } } template void erase(kj::ArrayPtr table, size_t pos, Params&&... params) { impl.erase(pos, searchKey(table, params...)); } template void move(kj::ArrayPtr table, size_t oldPos, size_t newPos, Params&&... params) { impl.renumber(oldPos, newPos, searchKey(table, params...)); } template Maybe find(kj::ArrayPtr table, Params&&... params) const { auto iter = impl.search(searchKey(table, params...)); if (!iter.isEnd() && cb.matches(table[*iter], params...)) { return size_t(*iter); } else { return nullptr; } } template _::IterRange<_::BTreeImpl::Iterator> range( kj::ArrayPtr table, Begin&& begin, End&& end) const { return { impl.search(searchKey(table, begin)), impl.search(searchKey(table, end )) }; } private: Callbacks cb; _::BTreeImpl impl; template class SearchKeyImpl: public _::BTreeImpl::SearchKey { public: SearchKeyImpl(Predicate&& predicate) : predicate(kj::mv(predicate)) {} uint search(const _::BTreeImpl::Parent& parent) const override { return parent.binarySearch(predicate); } uint search(const _::BTreeImpl::Leaf& leaf) const override { return leaf.binarySearch(predicate); } bool isAfter(uint rowIndex) const override { return predicate(rowIndex); } private: Predicate predicate; }; template inline auto searchKey(kj::ArrayPtr& table, Params&... params) const { auto predicate = [&](uint i) { return cb.isBefore(table[i], params...); }; return SearchKeyImpl(kj::mv(predicate)); } }; // ----------------------------------------------------------------------------- // Insertion order index class InsertionOrderIndex { // Table index which allows iterating over elements in order of insertion. This index cannot // be used for Table::find(), but can be used for Table::ordered(). struct Link; public: InsertionOrderIndex(); InsertionOrderIndex(const InsertionOrderIndex&) = delete; InsertionOrderIndex& operator=(const InsertionOrderIndex&) = delete; InsertionOrderIndex(InsertionOrderIndex&& other); InsertionOrderIndex& operator=(InsertionOrderIndex&& other); ~InsertionOrderIndex() noexcept(false); class Iterator { public: Iterator(const Link* links, uint pos) : links(links), pos(pos) {} inline size_t operator*() const { KJ_IREQUIRE(pos != 0, "can't derefrence end() iterator"); return pos - 1; }; inline Iterator& operator++() { pos = links[pos].next; return *this; } inline Iterator operator++(int) { Iterator result = *this; ++*this; return result; } inline Iterator& operator--() { pos = links[pos].prev; return *this; } inline Iterator operator--(int) { Iterator result = *this; --*this; return result; } inline bool operator==(const Iterator& other) const { return pos == other.pos; } inline bool operator!=(const Iterator& other) const { return pos != other.pos; } private: const Link* links; uint pos; }; template Row& keyForRow(Row& row) const { return row; } void reserve(size_t size); void clear(); inline Iterator begin() const { return Iterator(links, links[0].next); } inline Iterator end() const { return Iterator(links, 0); } template kj::Maybe insert(kj::ArrayPtr table, size_t pos, const Row& row) { return insertImpl(pos); } template void erase(kj::ArrayPtr table, size_t pos, const Row& row) { eraseImpl(pos); } template void move(kj::ArrayPtr table, size_t oldPos, size_t newPos, const Row& row) { return moveImpl(oldPos, newPos); } private: struct Link { uint next; uint prev; }; uint capacity; Link* links; // links[0] is special: links[0].next points to the first link, links[0].prev points to the last. // links[n+1] corresponds to row n. kj::Maybe insertImpl(size_t pos); void eraseImpl(size_t pos); void moveImpl(size_t oldPos, size_t newPos); static const Link EMPTY_LINK; }; } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/map.h0000644000175000017500000004354113650101756017104 0ustar00kentonkenton00000000000000// Copyright (c) 2018 Kenton Varda and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "table.h" #include "hash.h" KJ_BEGIN_HEADER namespace kj { template class HashMap { // A key/value mapping backed by hashing. // // `Key` must be hashable (via a `.hashCode()` method or `KJ_HASHCODE()`; see `hash.h`) and must // implement `operator==()`. Additionally, when performing lookups, you can use key types other // than `Key` as long as the other type is also hashable (producing the same hash codes) and // there is an `operator==` implementation with `Key` on the left and that other type on the // right. For example, if the key type is `String`, you can pass `StringPtr` to `find()`. public: void reserve(size_t size); // Pre-allocates space for a map of the given size. size_t size() const; size_t capacity() const; void clear(); struct Entry { Key key; Value value; }; Entry* begin(); Entry* end(); const Entry* begin() const; const Entry* end() const; // Deterministic iteration. If you only ever insert(), iteration order will be insertion order. // If you erase(), the erased element is swapped with the last element in the ordering. Entry& insert(Key key, Value value); // Inserts a new entry. Throws if the key already exists. template void insertAll(Collection&& collection); // Given an iterable collection of `Entry`s, inserts all of them into this map. If the // input is an rvalue, the entries will be moved rather than copied. template Entry& upsert(Key key, Value value, UpdateFunc&& update); // Tries to insert a new entry. However, if a duplicate already exists (according to some index), // then update(Value& existingValue, Value&& newValue) is called to modify the existing value. template kj::Maybe find(KeyLike&& key); template kj::Maybe find(KeyLike&& key) const; // Search for a matching key. The input does not have to be of type `Key`; it merely has to // be something that the Hasher accepts. // // Note that the default hasher for String accepts StringPtr. template Value& findOrCreate(KeyLike&& key, Func&& createEntry); // Like find() but if the key isn't present then call createEntry() to create the corresponding // entry and insert it. createEntry() must return type `Entry`. template kj::Maybe findEntry(KeyLike&& key); template kj::Maybe findEntry(KeyLike&& key) const; template Entry& findOrCreateEntry(KeyLike&& key, Func&& createEntry); // Sometimes you need to see the whole matching Entry, not just the Value. template bool erase(KeyLike&& key); // Erase the entry with the matching key. // // WARNING: This invalidates all pointers and interators into the map. Use eraseAll() if you need // to iterate and erase multiple entries. void erase(Entry& entry); // Erase an entry by reference. template ()(instance(), instance()))> size_t eraseAll(Predicate&& predicate); // Erase all values for which predicate(key, value) returns true. This scans over the entire map. private: class Callbacks { public: inline const Key& keyForRow(const Entry& entry) const { return entry.key; } inline Key& keyForRow(Entry& entry) const { return entry.key; } template inline bool matches(Entry& e, KeyLike&& key) const { return e.key == key; } template inline bool matches(const Entry& e, KeyLike&& key) const { return e.key == key; } template inline auto hashCode(KeyLike&& key) const { return kj::hashCode(key); } }; kj::Table> table; }; template class TreeMap { // A key/value mapping backed by a B-tree. // // `Key` must support `operator<` and `operator==` against other Keys, and against any type // which you might want to pass to find() (with `Key` always on the left of the comparison). public: void reserve(size_t size); // Pre-allocates space for a map of the given size. size_t size() const; size_t capacity() const; void clear(); struct Entry { Key key; Value value; }; auto begin(); auto end(); auto begin() const; auto end() const; // Iteration is in sorted order by key. Entry& insert(Key key, Value value); // Inserts a new entry. Throws if the key already exists. template void insertAll(Collection&& collection); // Given an iterable collection of `Entry`s, inserts all of them into this map. If the // input is an rvalue, the entries will be moved rather than copied. template Entry& upsert(Key key, Value value, UpdateFunc&& update); // Tries to insert a new entry. However, if a duplicate already exists (according to some index), // then update(Value& existingValue, Value&& newValue) is called to modify the existing value. template kj::Maybe find(KeyLike&& key); template kj::Maybe find(KeyLike&& key) const; // Search for a matching key. The input does not have to be of type `Key`; it merely has to // be something that can be compared against `Key`. template Value& findOrCreate(KeyLike&& key, Func&& createEntry); // Like find() but if the key isn't present then call createEntry() to create the corresponding // entry and insert it. createEntry() must return type `Entry`. template kj::Maybe findEntry(KeyLike&& key); template kj::Maybe findEntry(KeyLike&& key) const; template Entry& findOrCreateEntry(KeyLike&& key, Func&& createEntry); // Sometimes you need to see the whole matching Entry, not just the Value. template auto range(K1&& k1, K2&& k2); template auto range(K1&& k1, K2&& k2) const; // Returns an iterable range of entries with keys between k1 (inclusive) and k2 (exclusive). template bool erase(KeyLike&& key); // Erase the entry with the matching key. // // WARNING: This invalidates all pointers and interators into the map. Use eraseAll() if you need // to iterate and erase multiple entries. void erase(Entry& entry); // Erase an entry by reference. template ()(instance(), instance()))> size_t eraseAll(Predicate&& predicate); // Erase all values for which predicate(key, value) returns true. This scans over the entire map. template size_t eraseRange(K1&& k1, K2&& k2); // Erases all entries with keys between k1 (inclusive) and k2 (exclusive). private: class Callbacks { public: inline const Key& keyForRow(const Entry& entry) const { return entry.key; } inline Key& keyForRow(Entry& entry) const { return entry.key; } template inline bool matches(Entry& e, KeyLike&& key) const { return e.key == key; } template inline bool matches(const Entry& e, KeyLike&& key) const { return e.key == key; } template inline bool isBefore(Entry& e, KeyLike&& key) const { return e.key < key; } template inline bool isBefore(const Entry& e, KeyLike&& key) const { return e.key < key; } }; kj::Table> table; }; namespace _ { // private class HashSetCallbacks { public: template inline Row& keyForRow(Row& row) const { return row; } template inline bool matches(T& a, U& b) const { return a == b; } template inline auto hashCode(KeyLike&& key) const { return kj::hashCode(key); } }; class TreeSetCallbacks { public: template inline Row& keyForRow(Row& row) const { return row; } template inline bool matches(T& a, U& b) const { return a == b; } template inline bool isBefore(T& a, U& b) const { return a < b; } }; } // namespace _ (private) template class HashSet: public Table> { // A simple hashtable-based set, using kj::hashCode() and operator==(). public: // Everything is inherited. template inline bool contains(Params&&... params) const { return this->find(kj::fwd(params)...) != nullptr; } }; template class TreeSet: public Table> { // A simple b-tree-based set, using operator<() and operator==(). public: // Everything is inherited. }; // ======================================================================================= // inline implementation details template void HashMap::reserve(size_t size) { table.reserve(size); } template size_t HashMap::size() const { return table.size(); } template size_t HashMap::capacity() const { return table.capacity(); } template void HashMap::clear() { return table.clear(); } template typename HashMap::Entry* HashMap::begin() { return table.begin(); } template typename HashMap::Entry* HashMap::end() { return table.end(); } template const typename HashMap::Entry* HashMap::begin() const { return table.begin(); } template const typename HashMap::Entry* HashMap::end() const { return table.end(); } template typename HashMap::Entry& HashMap::insert(Key key, Value value) { return table.insert(Entry { kj::mv(key), kj::mv(value) }); } template template void HashMap::insertAll(Collection&& collection) { return table.insertAll(kj::fwd(collection)); } template template typename HashMap::Entry& HashMap::upsert( Key key, Value value, UpdateFunc&& update) { return table.upsert(Entry { kj::mv(key), kj::mv(value) }, [&](Entry& existingEntry, Entry&& newEntry) { update(existingEntry.value, kj::mv(newEntry.value)); }); } template template kj::Maybe HashMap::find(KeyLike&& key) { return table.find(key).map([](Entry& e) -> Value& { return e.value; }); } template template kj::Maybe HashMap::find(KeyLike&& key) const { return table.find(key).map([](const Entry& e) -> const Value& { return e.value; }); } template template Value& HashMap::findOrCreate(KeyLike&& key, Func&& createEntry) { return table.findOrCreate(key, kj::fwd(createEntry)).value; } template template kj::Maybe::Entry&> HashMap::findEntry(KeyLike&& key) { return table.find(kj::fwd(key)); } template template kj::Maybe::Entry&> HashMap::findEntry(KeyLike&& key) const { return table.find(kj::fwd(key)); } template template typename HashMap::Entry& HashMap::findOrCreateEntry(KeyLike&& key, Func&& createEntry) { return table.findOrCreate(kj::fwd(key), kj::fwd(createEntry)); } template template bool HashMap::erase(KeyLike&& key) { return table.eraseMatch(key); } template void HashMap::erase(Entry& entry) { table.erase(entry); } template template size_t HashMap::eraseAll(Predicate&& predicate) { return table.eraseAll([&](Entry& entry) { return predicate(entry.key, entry.value); }); } // ----------------------------------------------------------------------------- template void TreeMap::reserve(size_t size) { table.reserve(size); } template size_t TreeMap::size() const { return table.size(); } template size_t TreeMap::capacity() const { return table.capacity(); } template void TreeMap::clear() { return table.clear(); } template auto TreeMap::begin() { return table.ordered().begin(); } template auto TreeMap::end() { return table.ordered().end(); } template auto TreeMap::begin() const { return table.ordered().begin(); } template auto TreeMap::end() const { return table.ordered().end(); } template typename TreeMap::Entry& TreeMap::insert(Key key, Value value) { return table.insert(Entry { kj::mv(key), kj::mv(value) }); } template template void TreeMap::insertAll(Collection&& collection) { return table.insertAll(kj::fwd(collection)); } template template typename TreeMap::Entry& TreeMap::upsert( Key key, Value value, UpdateFunc&& update) { return table.upsert(Entry { kj::mv(key), kj::mv(value) }, [&](Entry& existingEntry, Entry&& newEntry) { update(existingEntry.value, kj::mv(newEntry.value)); }); } template template kj::Maybe TreeMap::find(KeyLike&& key) { return table.find(key).map([](Entry& e) -> Value& { return e.value; }); } template template kj::Maybe TreeMap::find(KeyLike&& key) const { return table.find(key).map([](const Entry& e) -> const Value& { return e.value; }); } template template Value& TreeMap::findOrCreate(KeyLike&& key, Func&& createEntry) { return table.findOrCreate(key, kj::fwd(createEntry)).value; } template template kj::Maybe::Entry&> TreeMap::findEntry(KeyLike&& key) { return table.find(kj::fwd(key)); } template template kj::Maybe::Entry&> TreeMap::findEntry(KeyLike&& key) const { return table.find(kj::fwd(key)); } template template typename TreeMap::Entry& TreeMap::findOrCreateEntry(KeyLike&& key, Func&& createEntry) { return table.findOrCreate(kj::fwd(key), kj::fwd(createEntry)); } template template auto TreeMap::range(K1&& k1, K2&& k2) { return table.range(kj::fwd(k1), kj::fwd(k2)); } template template auto TreeMap::range(K1&& k1, K2&& k2) const { return table.range(kj::fwd(k1), kj::fwd(k2)); } template template bool TreeMap::erase(KeyLike&& key) { return table.eraseMatch(key); } template void TreeMap::erase(Entry& entry) { table.erase(entry); } template template size_t TreeMap::eraseAll(Predicate&& predicate) { return table.eraseAll([&](Entry& entry) { return predicate(entry.key, entry.value); }); } template template size_t TreeMap::eraseRange(K1&& k1, K2&& k2) { return table.eraseRange(kj::fwd(k1), kj::fwd(k2)); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/encoding.h0000644000175000017500000004025513650101756020114 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // Functions for encoding/decoding bytes and text in common formats, including: // - UTF-{8,16,32} // - Hex // - URI encoding // - Base64 #include "string.h" KJ_BEGIN_HEADER namespace kj { template struct EncodingResult: public ResultType { // Equivalent to ResultType (a String or wide-char array) for all intents and purposes, except // that the bool `hadErrors` can be inspected to see if any errors were encountered in the input. // Each encoding/decoding function that returns this type will "work around" errors in some way, // so an application doesn't strictly have to check for errors. E.g. the Unicode functions // replace errors with U+FFFD in the output. // // Through magic, KJ_IF_MAYBE() and KJ_{REQUIRE,ASSERT}_NONNULL() work on EncodingResult // exactly if it were a Maybe that is null in case of errors. inline EncodingResult(ResultType&& result, bool hadErrors) : ResultType(kj::mv(result)), hadErrors(hadErrors) {} const bool hadErrors; }; template inline auto KJ_STRINGIFY(const EncodingResult& value) -> decltype(toCharSequence(implicitCast(value))) { return toCharSequence(implicitCast(value)); } EncodingResult> encodeUtf16(ArrayPtr text, bool nulTerminate = false); EncodingResult> encodeUtf32(ArrayPtr text, bool nulTerminate = false); // Convert UTF-8 text (which KJ strings use) to UTF-16 or UTF-32. // // If `nulTerminate` is true, an extra NUL character will be added to the end of the output. // // The returned arrays are in platform-native endianness (otherwise they wouldn't really be // char16_t / char32_t). // // Note that the KJ Unicode encoding and decoding functions actually implement // [WTF-8 encoding](http://simonsapin.github.io/wtf-8/), which affects how invalid input is // handled. See comments on decodeUtf16() for more info. EncodingResult decodeUtf16(ArrayPtr utf16); EncodingResult decodeUtf32(ArrayPtr utf32); // Convert UTF-16 or UTF-32 to UTF-8 (which KJ strings use). // // The input should NOT include a NUL terminator; any NUL characters in the input array will be // preserved in the output. // // The input must be in platform-native endianness. BOMs are NOT recognized by these functions. // // Note that the KJ Unicode encoding and decoding functions actually implement // [WTF-8 encoding](http://simonsapin.github.io/wtf-8/). This means that if you start with an array // of char16_t and you pass it through any number of conversions to other Unicode encodings, // eventually returning it to UTF-16, all the while ignoring `hadErrors`, you will end up with // exactly the same char16_t array you started with, *even if* the array is not valid UTF-16. This // is useful because many real-world systems that were designed for UCS-2 (plain 16-bit Unicode) // and later "upgraded" to UTF-16 do not enforce that their UTF-16 is well-formed. For example, // file names on Windows NT are encoded using 16-bit characters, without enforcing that the // character sequence is valid UTF-16. It is important that programs on Windows be able to handle // such filenames, even if they choose to convert the name to UTF-8 for internal processing. // // Specifically, KJ's Unicode handling allows unpaired surrogate code points to round-trip through // UTF-8 and UTF-32. Unpaired surrogates will be flagged as an error (setting `hadErrors` in the // result), but will NOT be replaced with the Unicode replacement character as other erroneous // sequences would be, but rather encoded as an invalid surrogate codepoint in the target encoding. // // KJ makes the following guarantees about invalid input: // - A round trip from UTF-16 to other encodings and back will produce exactly the original input, // with every leg of the trip raising the `hadErrors` flag if the original input was not valid. // - A round trip from UTF-8 or UTF-32 to other encodings and back will either produce exactly // the original input, or will have replaced some invalid sequences with the Unicode replacement // character, U+FFFD. No code units will ever be removed unless they are replaced with U+FFFD, // and no code units will ever be added except to encode U+FFFD. If the original input was not // valid, the `hadErrors` flag will be raised on the first leg of the trip, and will also be // raised on subsequent legs unless all invalid sequences were replaced with U+FFFD (which, after // all, is a valid code point). EncodingResult> encodeWideString( ArrayPtr text, bool nulTerminate = false); EncodingResult decodeWideString(ArrayPtr wide); // Encode / decode strings of wchar_t, aka "wide strings". Unfortunately, different platforms have // different definitions for wchar_t. For example, on Windows they are 16-bit and encode UTF-16, // but on Linux they are 32-bit and encode UTF-32. Some platforms even define wchar_t as 8-bit, // encoding UTF-8 (e.g. BeOS did this). // // KJ assumes that wide strings use the UTF encoding that corresponds to the size of wchar_t on // the target platform. So, these functions are simple aliases for encodeUtf*/decodeUtf*, above // (or simply make a copy if wchar_t is 8 bits). String encodeHex(ArrayPtr bytes); EncodingResult> decodeHex(ArrayPtr text); // Encode/decode bytes as hex strings. String encodeUriComponent(ArrayPtr bytes); String encodeUriComponent(ArrayPtr bytes); EncodingResult decodeUriComponent(ArrayPtr text); // Encode/decode URI components using % escapes for characters listed as "reserved" in RFC 2396. // This is the same behavior as JavaScript's `encodeURIComponent()`. // // See https://tools.ietf.org/html/rfc2396#section-2.3 String encodeUriFragment(ArrayPtr bytes); String encodeUriFragment(ArrayPtr bytes); // Encode URL fragment components using the fragment percent encode set defined by the WHATWG URL // specification. Use decodeUriComponent() to decode. // // Quirk: We also percent-encode the '%' sign itself, because we expect to be called on percent- // decoded data. In other words, this function is not idempotent, in contrast to the URL spec. // // See https://url.spec.whatwg.org/#fragment-percent-encode-set String encodeUriPath(ArrayPtr bytes); String encodeUriPath(ArrayPtr bytes); // Encode URL path components (not entire paths!) using the path percent encode set defined by the // WHATWG URL specification. Use decodeUriComponent() to decode. // // Quirk: We also percent-encode the '%' sign itself, because we expect to be called on percent- // decoded data. In other words, this function is not idempotent, in contrast to the URL spec. // // Quirk: This percent-encodes '/' and '\' characters as well, which are not actually in the set // defined by the WHATWG URL spec. Since a conforming URL implementation will only ever call this // function on individual path components, and never entire paths, augmenting the character set to // include these separators allows this function to be used to implement a URL class that stores // its path components in percent-decoded form. // // See https://url.spec.whatwg.org/#path-percent-encode-set String encodeUriUserInfo(ArrayPtr bytes); String encodeUriUserInfo(ArrayPtr bytes); // Encode URL userinfo components using the userinfo percent encode set defined by the WHATWG URL // specification. Use decodeUriComponent() to decode. // // Quirk: We also percent-encode the '%' sign itself, because we expect to be called on percent- // decoded data. In other words, this function is not idempotent, in contrast to the URL spec. // // See https://url.spec.whatwg.org/#userinfo-percent-encode-set String encodeWwwForm(ArrayPtr bytes); String encodeWwwForm(ArrayPtr bytes); EncodingResult decodeWwwForm(ArrayPtr text); // Encode/decode URI components using % escapes and '+' (for spaces) according to the // application/x-www-form-urlencoded format defined by the WHATWG URL specification. // // Note: Like the fragment, path, and userinfo percent-encoding functions above, this function is // not idempotent: we percent-encode '%' signs. However, in this particular case the spec happens // to agree with us! // // See https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer struct DecodeUriOptions { // Parameter to `decodeBinaryUriComponent()`. // This struct is intentionally convertible from bool, in order to maintain backwards // compatibility with code written when `decodeBinaryUriComponent()` took a boolean second // parameter. DecodeUriOptions(bool nulTerminate = false, bool plusToSpace = false) : nulTerminate(nulTerminate), plusToSpace(plusToSpace) {} bool nulTerminate; // Append a terminal NUL byte. bool plusToSpace; // Convert '+' to ' ' characters before percent decoding. Used to decode // application/x-www-form-urlencoded text, such as query strings. }; EncodingResult> decodeBinaryUriComponent( ArrayPtr text, DecodeUriOptions options = DecodeUriOptions()); // Decode URI components using % escapes. This is a lower-level interface used to implement both // `decodeUriComponent()` and `decodeWwwForm()` String encodeCEscape(ArrayPtr bytes); String encodeCEscape(ArrayPtr bytes); EncodingResult> decodeBinaryCEscape( ArrayPtr text, bool nulTerminate = false); EncodingResult decodeCEscape(ArrayPtr text); String encodeBase64(ArrayPtr bytes, bool breakLines = false); // Encode the given bytes as base64 text. If `breakLines` is true, line breaks will be inserted // into the output every 72 characters (e.g. for encoding e-mail bodies). EncodingResult> decodeBase64(ArrayPtr text); // Decode base64 text. This function reports errors required by the WHATWG HTML/Infra specs: see // https://html.spec.whatwg.org/multipage/webappapis.html#atob for details. String encodeBase64Url(ArrayPtr bytes); // Encode the given bytes as URL-safe base64 text. (RFC 4648, section 5) // ======================================================================================= // inline implementation details namespace _ { // private template NullableValue readMaybe(EncodingResult&& value) { if (value.hadErrors) { return nullptr; } else { return kj::mv(value); } } template T* readMaybe(EncodingResult& value) { if (value.hadErrors) { return nullptr; } else { return &value; } } template const T* readMaybe(const EncodingResult& value) { if (value.hadErrors) { return nullptr; } else { return &value; } } } // namespace _ (private) inline String encodeUriComponent(ArrayPtr text) { return encodeUriComponent(text.asBytes()); } inline EncodingResult decodeUriComponent(ArrayPtr text) { auto result = decodeBinaryUriComponent(text, DecodeUriOptions { /*.nulTerminate=*/true }); return { String(result.releaseAsChars()), result.hadErrors }; } inline String encodeUriFragment(ArrayPtr text) { return encodeUriFragment(text.asBytes()); } inline String encodeUriPath(ArrayPtr text) { return encodeUriPath(text.asBytes()); } inline String encodeUriUserInfo(ArrayPtr text) { return encodeUriUserInfo(text.asBytes()); } inline String encodeWwwForm(ArrayPtr text) { return encodeWwwForm(text.asBytes()); } inline EncodingResult decodeWwwForm(ArrayPtr text) { auto result = decodeBinaryUriComponent(text, DecodeUriOptions { /*.nulTerminate=*/true, /*.plusToSpace=*/true }); return { String(result.releaseAsChars()), result.hadErrors }; } inline String encodeCEscape(ArrayPtr text) { return encodeCEscape(text.asBytes()); } inline EncodingResult decodeCEscape(ArrayPtr text) { auto result = decodeBinaryCEscape(text, true); return { String(result.releaseAsChars()), result.hadErrors }; } // If you pass a string literal to a function taking ArrayPtr, it'll include the NUL // termintator, which is surprising. Let's add overloads that avoid that. In practice this probably // only even matters for encoding-test.c++. template inline EncodingResult> encodeUtf16(const char (&text)[s], bool nulTerminate=false) { return encodeUtf16(arrayPtr(text, s - 1), nulTerminate); } template inline EncodingResult> encodeUtf32(const char (&text)[s], bool nulTerminate=false) { return encodeUtf32(arrayPtr(text, s - 1), nulTerminate); } template inline EncodingResult> encodeWideString( const char (&text)[s], bool nulTerminate=false) { return encodeWideString(arrayPtr(text, s - 1), nulTerminate); } template inline EncodingResult decodeUtf16(const char16_t (&utf16)[s]) { return decodeUtf16(arrayPtr(utf16, s - 1)); } template inline EncodingResult decodeUtf32(const char32_t (&utf32)[s]) { return decodeUtf32(arrayPtr(utf32, s - 1)); } template inline EncodingResult decodeWideString(const wchar_t (&utf32)[s]) { return decodeWideString(arrayPtr(utf32, s - 1)); } template inline EncodingResult> decodeHex(const char (&text)[s]) { return decodeHex(arrayPtr(text, s - 1)); } template inline String encodeUriComponent(const char (&text)[s]) { return encodeUriComponent(arrayPtr(text, s - 1)); } template inline Array decodeBinaryUriComponent(const char (&text)[s]) { return decodeBinaryUriComponent(arrayPtr(text, s - 1)); } template inline EncodingResult decodeUriComponent(const char (&text)[s]) { return decodeUriComponent(arrayPtr(text, s-1)); } template inline String encodeUriFragment(const char (&text)[s]) { return encodeUriFragment(arrayPtr(text, s - 1)); } template inline String encodeUriPath(const char (&text)[s]) { return encodeUriPath(arrayPtr(text, s - 1)); } template inline String encodeUriUserInfo(const char (&text)[s]) { return encodeUriUserInfo(arrayPtr(text, s - 1)); } template inline String encodeWwwForm(const char (&text)[s]) { return encodeWwwForm(arrayPtr(text, s - 1)); } template inline EncodingResult decodeWwwForm(const char (&text)[s]) { return decodeWwwForm(arrayPtr(text, s-1)); } template inline String encodeCEscape(const char (&text)[s]) { return encodeCEscape(arrayPtr(text, s - 1)); } template inline EncodingResult> decodeBinaryCEscape(const char (&text)[s]) { return decodeBinaryCEscape(arrayPtr(text, s - 1)); } template inline EncodingResult decodeCEscape(const char (&text)[s]) { return decodeCEscape(arrayPtr(text, s-1)); } template EncodingResult> decodeBase64(const char (&text)[s]) { return decodeBase64(arrayPtr(text, s - 1)); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/exception.h0000644000175000017500000003746113650101756020331 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" #include "array.h" #include "string.h" #include "windows-sanity.h" // work-around macro conflict with `ERROR` KJ_BEGIN_HEADER namespace kj { class ExceptionImpl; template class Function; class Exception { // Exception thrown in case of fatal errors. // // Actually, a subclass of this which also implements std::exception will be thrown, but we hide // that fact from the interface to avoid #including . public: enum class Type { // What kind of failure? FAILED = 0, // Something went wrong. This is the usual error type. KJ_ASSERT and KJ_REQUIRE throw this // error type. OVERLOADED = 1, // The call failed because of a temporary lack of resources. This could be space resources // (out of memory, out of disk space) or time resources (request queue overflow, operation // timed out). // // The operation might work if tried again, but it should NOT be repeated immediately as this // may simply exacerbate the problem. DISCONNECTED = 2, // The call required communication over a connection that has been lost. The callee will need // to re-establish connections and try again. UNIMPLEMENTED = 3 // The requested method is not implemented. The caller may wish to revert to a fallback // approach based on other methods. // IF YOU ADD A NEW VALUE: // - Update the stringifier. // - Update Cap'n Proto's RPC protocol's Exception.Type enum. }; Exception(Type type, const char* file, int line, String description = nullptr) noexcept; Exception(Type type, String file, int line, String description = nullptr) noexcept; Exception(const Exception& other) noexcept; Exception(Exception&& other) = default; ~Exception() noexcept; const char* getFile() const { return file; } int getLine() const { return line; } Type getType() const { return type; } StringPtr getDescription() const { return description; } ArrayPtr getStackTrace() const { return arrayPtr(trace, traceCount); } struct Context { // Describes a bit about what was going on when the exception was thrown. const char* file; int line; String description; Maybe> next; Context(const char* file, int line, String&& description, Maybe>&& next) : file(file), line(line), description(mv(description)), next(mv(next)) {} Context(const Context& other) noexcept; }; inline Maybe getContext() const { KJ_IF_MAYBE(c, context) { return **c; } else { return nullptr; } } void wrapContext(const char* file, int line, String&& description); // Wraps the context in a new node. This becomes the head node returned by getContext() -- it // is expected that contexts will be added in reverse order as the exception passes up the // callback stack. KJ_NOINLINE void extendTrace(uint ignoreCount); // Append the current stack trace to the exception's trace, ignoring the first `ignoreCount` // frames (see `getStackTrace()` for discussion of `ignoreCount`). KJ_NOINLINE void truncateCommonTrace(); // Remove the part of the stack trace which the exception shares with the caller of this method. // This is used by the async library to remove the async infrastructure from the stack trace // before replacing it with the async trace. void addTrace(void* ptr); // Append the given pointer to the backtrace, if it is not already full. This is used by the // async library to trace through the promise chain that led to the exception. private: String ownFile; const char* file; int line; Type type; String description; Maybe> context; void* trace[32]; uint traceCount; friend class ExceptionImpl; }; StringPtr KJ_STRINGIFY(Exception::Type type); String KJ_STRINGIFY(const Exception& e); // ======================================================================================= enum class LogSeverity { INFO, // Information describing what the code is up to, which users may request to see // with a flag like `--verbose`. Does not indicate a problem. Not printed by // default; you must call setLogLevel(INFO) to enable. WARNING, // A problem was detected but execution can continue with correct output. ERROR, // Something is wrong, but execution can continue with garbage output. FATAL, // Something went wrong, and execution cannot continue. DBG // Temporary debug logging. See KJ_DBG. // Make sure to update the stringifier if you add a new severity level. }; StringPtr KJ_STRINGIFY(LogSeverity severity); class ExceptionCallback { // If you don't like C++ exceptions, you may implement and register an ExceptionCallback in order // to perform your own exception handling. For example, a reasonable thing to do is to have // onRecoverableException() set a flag indicating that an error occurred, and then check for that // flag just before writing to storage and/or returning results to the user. If the flag is set, // discard whatever you have and return an error instead. // // ExceptionCallbacks must always be allocated on the stack. When an exception is thrown, the // newest ExceptionCallback on the calling thread's stack is called. The default implementation // of each method calls the next-oldest ExceptionCallback for that thread. Thus the callbacks // behave a lot like try/catch blocks, except that they are called before any stack unwinding // occurs. public: ExceptionCallback(); KJ_DISALLOW_COPY(ExceptionCallback); virtual ~ExceptionCallback() noexcept(false); virtual void onRecoverableException(Exception&& exception); // Called when an exception has been raised, but the calling code has the ability to continue by // producing garbage output. This method _should_ throw the exception, but is allowed to simply // return if garbage output is acceptable. // // The global default implementation throws an exception unless the library was compiled with // -fno-exceptions, in which case it logs an error and returns. virtual void onFatalException(Exception&& exception); // Called when an exception has been raised and the calling code cannot continue. If this method // returns normally, abort() will be called. The method must throw the exception to avoid // aborting. // // The global default implementation throws an exception unless the library was compiled with // -fno-exceptions, in which case it logs an error and returns. virtual void logMessage(LogSeverity severity, const char* file, int line, int contextDepth, String&& text); // Called when something wants to log some debug text. `contextDepth` indicates how many levels // of context the message passed through; it may make sense to indent the message accordingly. // // The global default implementation writes the text to stderr. enum class StackTraceMode { FULL, // Stringifying a stack trace will attempt to determine source file and line numbers. This may // be expensive. For example, on Linux, this shells out to `addr2line`. // // This is the default in debug builds. ADDRESS_ONLY, // Stringifying a stack trace will only generate a list of code addresses. // // This is the default in release builds. NONE // Generating a stack trace will always return an empty array. // // This avoids ever unwinding the stack. On Windows in particular, the stack unwinding library // has been observed to be pretty slow, so exception-heavy code might benefit significantly // from this setting. (But exceptions should be rare...) }; virtual StackTraceMode stackTraceMode(); // Returns the current preferred stack trace mode. virtual Function)> getThreadInitializer(); // Called just before a new thread is spawned using kj::Thread. Returns a function which should // be invoked inside the new thread to initialize the thread's ExceptionCallback. The initializer // function itself receives, as its parameter, the thread's main function, which it must call. protected: ExceptionCallback& next; private: ExceptionCallback(ExceptionCallback& next); class RootExceptionCallback; friend ExceptionCallback& getExceptionCallback(); friend class Thread; }; ExceptionCallback& getExceptionCallback(); // Returns the current exception callback. KJ_NOINLINE KJ_NORETURN(void throwFatalException(kj::Exception&& exception, uint ignoreCount = 0)); // Invoke the exception callback to throw the given fatal exception. If the exception callback // returns, abort. KJ_NOINLINE void throwRecoverableException(kj::Exception&& exception, uint ignoreCount = 0); // Invoke the exception callback to throw the given recoverable exception. If the exception // callback returns, return normally. // ======================================================================================= namespace _ { class Runnable; } template Maybe runCatchingExceptions(Func&& func) noexcept; // Executes the given function (usually, a lambda returning nothing) catching any exceptions that // are thrown. Returns the Exception if there was one, or null if the operation completed normally. // Non-KJ exceptions will be wrapped. // // If exception are disabled (e.g. with -fno-exceptions), this will still detect whether any // recoverable exceptions occurred while running the function and will return those. class UnwindDetector { // Utility for detecting when a destructor is called due to unwind. Useful for: // - Avoiding throwing exceptions in this case, which would terminate the program. // - Detecting whether to commit or roll back a transaction. // // To use this class, either inherit privately from it or declare it as a member. The detector // works by comparing the exception state against that when the constructor was called, so for // an object that was actually constructed during exception unwind, it will behave as if no // unwind is taking place. This is usually the desired behavior. public: UnwindDetector(); bool isUnwinding() const; // Returns true if the current thread is in a stack unwind that it wasn't in at the time the // object was constructed. template void catchExceptionsIfUnwinding(Func&& func) const; // Runs the given function (e.g., a lambda). If isUnwinding() is true, any exceptions are // caught and treated as secondary faults, meaning they are considered to be side-effects of the // exception that is unwinding the stack. Otherwise, exceptions are passed through normally. private: uint uncaughtCount; void catchExceptionsAsSecondaryFaults(_::Runnable& runnable) const; }; namespace _ { // private class Runnable { public: virtual void run() = 0; }; template class RunnableImpl: public Runnable { public: RunnableImpl(Func&& func): func(kj::mv(func)) {} void run() override { func(); } private: Func func; }; Maybe runCatchingExceptions(Runnable& runnable) noexcept; } // namespace _ (private) template Maybe runCatchingExceptions(Func&& func) noexcept { _::RunnableImpl> runnable(kj::fwd(func)); return _::runCatchingExceptions(runnable); } template void UnwindDetector::catchExceptionsIfUnwinding(Func&& func) const { if (isUnwinding()) { _::RunnableImpl> runnable(kj::fwd(func)); catchExceptionsAsSecondaryFaults(runnable); } else { func(); } } #define KJ_ON_SCOPE_SUCCESS(code) \ ::kj::UnwindDetector KJ_UNIQUE_NAME(_kjUnwindDetector); \ KJ_DEFER(if (!KJ_UNIQUE_NAME(_kjUnwindDetector).isUnwinding()) { code; }) // Runs `code` if the current scope is exited normally (not due to an exception). #define KJ_ON_SCOPE_FAILURE(code) \ ::kj::UnwindDetector KJ_UNIQUE_NAME(_kjUnwindDetector); \ KJ_DEFER(if (KJ_UNIQUE_NAME(_kjUnwindDetector).isUnwinding()) { code; }) // Runs `code` if the current scope is exited due to an exception. // ======================================================================================= KJ_NOINLINE ArrayPtr getStackTrace(ArrayPtr space, uint ignoreCount); // Attempt to get the current stack trace, returning a list of pointers to instructions. The // returned array is a slice of `space`. Provide a larger `space` to get a deeper stack trace. // If the platform doesn't support stack traces, returns an empty array. // // `ignoreCount` items will be truncated from the front of the trace. This is useful for chopping // off a prefix of the trace that is uninteresting to the developer because it's just locations // inside the debug infrastructure that is requesting the trace. Be careful to mark functions as // KJ_NOINLINE if you intend to count them in `ignoreCount`. Note that, unfortunately, the // ignored entries will still waste space in the `space` array (and the returned array's `begin()` // is never exactly equal to `space.begin()` due to this effect, even if `ignoreCount` is zero // since `getStackTrace()` needs to ignore its own internal frames). String stringifyStackTrace(ArrayPtr); // Convert the stack trace to a string with file names and line numbers. This may involve executing // suprocesses. String stringifyStackTraceAddresses(ArrayPtr trace); StringPtr stringifyStackTraceAddresses(ArrayPtr trace, ArrayPtr scratch); // Construct a string containing just enough information about a stack trace to be able to convert // it to file and line numbers later using offline tools. This produces a sequence of // space-separated code location identifiers. Each identifier may be an absolute address // (hex number starting with 0x) or may be a module-relative address "@0x". The // latter case is preferred when ASLR is in effect and has loaded different modules at different // addresses. String getStackTrace(); // Get a stack trace right now and stringify it. Useful for debugging. void printStackTraceOnCrash(); // Registers signal handlers on common "crash" signals like SIGSEGV that will (attempt to) print // a stack trace. You should call this as early as possible on program startup. Programs using // KJ_MAIN get this automatically. kj::StringPtr trimSourceFilename(kj::StringPtr filename); // Given a source code file name, trim off noisy prefixes like "src/" or // "/ekam-provider/canonical/". kj::String getCaughtExceptionType(); // Utility function which attempts to return the human-readable type name of the exception // currently being thrown. This can be called inside a catch block, including a catch (...) block, // for the purpose of error logging. This function is best-effort; on some platforms it may simply // return "(unknown)". } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/debug.h0000644000175000017500000006002713650101756017413 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file declares convenient macros for debug logging and error handling. The macros make // it excessively easy to extract useful context information from code. Example: // // KJ_ASSERT(a == b, a, b, "a and b must be the same."); // // On failure, this will throw an exception whose description looks like: // // myfile.c++:43: bug in code: expected a == b; a = 14; b = 72; a and b must be the same. // // As you can see, all arguments after the first provide additional context. // // The macros available are: // // * `KJ_LOG(severity, ...)`: Just writes a log message, to stderr by default (but you can // intercept messages by implementing an ExceptionCallback). `severity` is `INFO`, `WARNING`, // `ERROR`, or `FATAL`. By default, `INFO` logs are not written, but for command-line apps the // user should be able to pass a flag like `--verbose` to enable them. Other log levels are // enabled by default. Log messages -- like exceptions -- can be intercepted by registering an // ExceptionCallback. // // * `KJ_DBG(...)`: Like `KJ_LOG`, but intended specifically for temporary log lines added while // debugging a particular problem. Calls to `KJ_DBG` should always be deleted before committing // code. It is suggested that you set up a pre-commit hook that checks for this. // // * `KJ_ASSERT(condition, ...)`: Throws an exception if `condition` is false, or aborts if // exceptions are disabled. This macro should be used to check for bugs in the surrounding code // and its dependencies, but NOT to check for invalid input. The macro may be followed by a // brace-delimited code block; if so, the block will be executed in the case where the assertion // fails, before throwing the exception. If control jumps out of the block (e.g. with "break", // "return", or "goto"), then the error is considered "recoverable" -- in this case, if // exceptions are disabled, execution will continue normally rather than aborting (but if // exceptions are enabled, an exception will still be thrown on exiting the block). A "break" // statement in particular will jump to the code immediately after the block (it does not break // any surrounding loop or switch). Example: // // KJ_ASSERT(value >= 0, "Value cannot be negative.", value) { // // Assertion failed. Set value to zero to "recover". // value = 0; // // Don't abort if exceptions are disabled. Continue normally. // // (Still throw an exception if they are enabled, though.) // break; // } // // When exceptions are disabled, we'll get here even if the assertion fails. // // Otherwise, we get here only if the assertion passes. // // * `KJ_REQUIRE(condition, ...)`: Like `KJ_ASSERT` but used to check preconditions -- e.g. to // validate parameters passed from a caller. A failure indicates that the caller is buggy. // // * `KJ_SYSCALL(code, ...)`: Executes `code` assuming it makes a system call. A negative result // is considered an error, with error code reported via `errno`. EINTR is handled by retrying. // Other errors are handled by throwing an exception. If you need to examine the return code, // assign it to a variable like so: // // int fd; // KJ_SYSCALL(fd = open(filename, O_RDONLY), filename); // // `KJ_SYSCALL` can be followed by a recovery block, just like `KJ_ASSERT`. // // * `KJ_NONBLOCKING_SYSCALL(code, ...)`: Like KJ_SYSCALL, but will not throw an exception on // EAGAIN/EWOULDBLOCK. The calling code should check the syscall's return value to see if it // indicates an error; in this case, it can assume the error was EAGAIN because any other error // would have caused an exception to be thrown. // // * `KJ_CONTEXT(...)`: Notes additional contextual information relevant to any exceptions thrown // from within the current scope. That is, until control exits the block in which KJ_CONTEXT() // is used, if any exception is generated, it will contain the given information in its context // chain. This is helpful because it can otherwise be very difficult to come up with error // messages that make sense within low-level helper code. Note that the parameters to // KJ_CONTEXT() are only evaluated if an exception is thrown. This implies that any variables // used must remain valid until the end of the scope. // // Notes: // * Do not write expressions with side-effects in the message content part of the macro, as the // message will not necessarily be evaluated. // * For every macro `FOO` above except `LOG`, there is also a `FAIL_FOO` macro used to report // failures that already happened. For the macros that check a boolean condition, `FAIL_FOO` // omits the first parameter and behaves like it was `false`. `FAIL_SYSCALL` and // `FAIL_RECOVERABLE_SYSCALL` take a string and an OS error number as the first two parameters. // The string should be the name of the failed system call. // * For every macro `FOO` above, there is a `DFOO` version (or `RECOVERABLE_DFOO`) which is only // executed in debug mode, i.e. when KJ_DEBUG is defined. KJ_DEBUG is defined automatically // by common.h when compiling without optimization (unless NDEBUG is defined), but you can also // define it explicitly (e.g. -DKJ_DEBUG). Generally, production builds should NOT use KJ_DEBUG // as it may enable expensive checks that are unlikely to fail. #pragma once #include "string.h" #include "exception.h" #include "windows-sanity.h" // work-around macro conflict with `ERROR` KJ_BEGIN_HEADER namespace kj { #if _MSC_VER // MSVC does __VA_ARGS__ differently from GCC: // - A trailing comma before an empty __VA_ARGS__ is removed automatically, whereas GCC wants // you to request this behavior with "##__VA_ARGS__". // - If __VA_ARGS__ is passed directly as an argument to another macro, it will be treated as a // *single* argument rather than an argument list. This can be worked around by wrapping the // outer macro call in KJ_EXPAND(), which apparently forces __VA_ARGS__ to be expanded before // the macro is evaluated. I don't understand the C preprocessor. // - Using "#__VA_ARGS__" to stringify __VA_ARGS__ expands to zero tokens when __VA_ARGS__ is // empty, rather than expanding to an empty string literal. We can work around by concatenating // with an empty string literal. #define KJ_EXPAND(X) X #define KJ_LOG(severity, ...) \ for (bool _kj_shouldLog = ::kj::_::Debug::shouldLog(::kj::LogSeverity::severity); \ _kj_shouldLog; _kj_shouldLog = false) \ ::kj::_::Debug::log(__FILE__, __LINE__, ::kj::LogSeverity::severity, \ "" #__VA_ARGS__, __VA_ARGS__) #define KJ_DBG(...) KJ_EXPAND(KJ_LOG(DBG, __VA_ARGS__)) #define KJ_REQUIRE(cond, ...) \ if (KJ_LIKELY(cond)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \ #cond, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #define KJ_FAIL_REQUIRE(...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \ nullptr, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #define KJ_SYSCALL(call, ...) \ if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, false)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjSyscallResult.getErrorNumber(), #call, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #define KJ_NONBLOCKING_SYSCALL(call, ...) \ if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, true)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjSyscallResult.getErrorNumber(), #call, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #define KJ_FAIL_SYSCALL(code, errorNumber, ...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ errorNumber, code, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #if _WIN32 || __CYGWIN__ #define KJ_WIN32(call, ...) \ if (auto _kjWin32Result = ::kj::_::Debug::win32Call(call)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjWin32Result, #call, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #define KJ_WINSOCK(call, ...) \ if (auto _kjWin32Result = ::kj::_::Debug::winsockCall(call)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjWin32Result, #call, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #define KJ_FAIL_WIN32(code, errorNumber, ...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ ::kj::_::Debug::Win32Result(errorNumber), code, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) #endif #define KJ_UNIMPLEMENTED(...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::UNIMPLEMENTED, \ nullptr, "" #__VA_ARGS__, __VA_ARGS__);; f.fatal()) // TODO(msvc): MSVC mis-deduces `ContextImpl` as `ContextImpl` in some edge // cases, such as inside nested lambdas inside member functions. Wrapping the type in // `decltype(instance<...>())` helps it deduce the context function's type correctly. #define KJ_CONTEXT(...) \ auto KJ_UNIQUE_NAME(_kjContextFunc) = [&]() -> ::kj::_::Debug::Context::Value { \ return ::kj::_::Debug::Context::Value(__FILE__, __LINE__, \ ::kj::_::Debug::makeDescription("" #__VA_ARGS__, __VA_ARGS__)); \ }; \ decltype(::kj::instance<::kj::_::Debug::ContextImpl>()) \ KJ_UNIQUE_NAME(_kjContext)(KJ_UNIQUE_NAME(_kjContextFunc)) #define KJ_REQUIRE_NONNULL(value, ...) \ (*[&] { \ auto _kj_result = ::kj::_::readMaybe(value); \ if (KJ_UNLIKELY(!_kj_result)) { \ ::kj::_::Debug::Fault(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \ #value " != nullptr", "" #__VA_ARGS__, __VA_ARGS__).fatal(); \ } \ return _kj_result; \ }()) #define KJ_EXCEPTION(type, ...) \ ::kj::Exception(::kj::Exception::Type::type, __FILE__, __LINE__, \ ::kj::_::Debug::makeDescription("" #__VA_ARGS__, __VA_ARGS__)) #else #define KJ_LOG(severity, ...) \ for (bool _kj_shouldLog = ::kj::_::Debug::shouldLog(::kj::LogSeverity::severity); \ _kj_shouldLog; _kj_shouldLog = false) \ ::kj::_::Debug::log(__FILE__, __LINE__, ::kj::LogSeverity::severity, \ #__VA_ARGS__, ##__VA_ARGS__) #define KJ_DBG(...) KJ_LOG(DBG, ##__VA_ARGS__) #define KJ_REQUIRE(cond, ...) \ if (KJ_LIKELY(cond)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \ #cond, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #define KJ_FAIL_REQUIRE(...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \ nullptr, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #define KJ_SYSCALL(call, ...) \ if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, false)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #define KJ_NONBLOCKING_SYSCALL(call, ...) \ if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);}, true)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #define KJ_FAIL_SYSCALL(code, errorNumber, ...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ errorNumber, code, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #if _WIN32 || __CYGWIN__ #define KJ_WIN32(call, ...) \ if (auto _kjWin32Result = ::kj::_::Debug::win32Call(call)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjWin32Result, #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) // Invoke a Win32 syscall that returns either BOOL or HANDLE, and throw an exception if it fails. #define KJ_WINSOCK(call, ...) \ if (auto _kjWin32Result = ::kj::_::Debug::winsockCall(call)) {} else \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ _kjWin32Result, #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) // Like KJ_WIN32 but for winsock calls which return `int` with SOCKET_ERROR indicating failure. // // Unfortunately, it's impossible to distinguish these from BOOL-returning Win32 calls by type, // since BOOL is in fact an alias for `int`. :( #define KJ_FAIL_WIN32(code, errorNumber, ...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, \ ::kj::_::Debug::Win32Result(errorNumber), code, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #endif #define KJ_UNIMPLEMENTED(...) \ for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Type::UNIMPLEMENTED, \ nullptr, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal()) #define KJ_CONTEXT(...) \ auto KJ_UNIQUE_NAME(_kjContextFunc) = [&]() -> ::kj::_::Debug::Context::Value { \ return ::kj::_::Debug::Context::Value(__FILE__, __LINE__, \ ::kj::_::Debug::makeDescription(#__VA_ARGS__, ##__VA_ARGS__)); \ }; \ ::kj::_::Debug::ContextImpl \ KJ_UNIQUE_NAME(_kjContext)(KJ_UNIQUE_NAME(_kjContextFunc)) #define KJ_REQUIRE_NONNULL(value, ...) \ (*({ \ auto _kj_result = ::kj::_::readMaybe(value); \ if (KJ_UNLIKELY(!_kj_result)) { \ ::kj::_::Debug::Fault(__FILE__, __LINE__, ::kj::Exception::Type::FAILED, \ #value " != nullptr", #__VA_ARGS__, ##__VA_ARGS__).fatal(); \ } \ kj::mv(_kj_result); \ })) #define KJ_EXCEPTION(type, ...) \ ::kj::Exception(::kj::Exception::Type::type, __FILE__, __LINE__, \ ::kj::_::Debug::makeDescription(#__VA_ARGS__, ##__VA_ARGS__)) #endif #define KJ_SYSCALL_HANDLE_ERRORS(call) \ if (int _kjSyscallError = ::kj::_::Debug::syscallError([&](){return (call);}, false)) \ switch (int error KJ_UNUSED = _kjSyscallError) // Like KJ_SYSCALL, but doesn't throw. Instead, the block after the macro is a switch block on the // error. Additionally, the int value `error` is defined within the block. So you can do: // // KJ_SYSCALL_HANDLE_ERRORS(foo()) { // case ENOENT: // handleNoSuchFile(); // break; // case EEXIST: // handleExists(); // break; // default: // KJ_FAIL_SYSCALL("foo()", error); // } else { // handleSuccessCase(); // } #if _WIN32 || __CYGWIN__ #define KJ_WIN32_HANDLE_ERRORS(call) \ if (uint _kjWin32Error = ::kj::_::Debug::win32Call(call).number) \ switch (uint error KJ_UNUSED = _kjWin32Error) // Like KJ_WIN32, but doesn't throw. Instead, the block after the macro is a switch block on the // error. Additionally, the int value `error` is defined within the block. So you can do: // // KJ_SYSCALL_HANDLE_ERRORS(foo()) { // case ERROR_FILE_NOT_FOUND: // handleNoSuchFile(); // break; // case ERROR_FILE_EXISTS: // handleExists(); // break; // default: // KJ_FAIL_WIN32("foo()", error); // } else { // handleSuccessCase(); // } #endif #define KJ_ASSERT KJ_REQUIRE #define KJ_FAIL_ASSERT KJ_FAIL_REQUIRE #define KJ_ASSERT_NONNULL KJ_REQUIRE_NONNULL // Use "ASSERT" in place of "REQUIRE" when the problem is local to the immediate surrounding code. // That is, if the assert ever fails, it indicates that the immediate surrounding code is broken. #ifdef KJ_DEBUG #define KJ_DLOG KJ_LOG #define KJ_DASSERT KJ_ASSERT #define KJ_DREQUIRE KJ_REQUIRE #else #define KJ_DLOG(...) do {} while (false) #define KJ_DASSERT(...) do {} while (false) #define KJ_DREQUIRE(...) do {} while (false) #endif namespace _ { // private class Debug { public: Debug() = delete; typedef LogSeverity Severity; // backwards-compatibility #if _WIN32 || __CYGWIN__ struct Win32Result { uint number; inline explicit Win32Result(uint number): number(number) {} operator bool() const { return number == 0; } }; #endif static inline bool shouldLog(LogSeverity severity) { return severity >= minSeverity; } // Returns whether messages of the given severity should be logged. static inline void setLogLevel(LogSeverity severity) { minSeverity = severity; } // Set the minimum message severity which will be logged. // // TODO(someday): Expose publicly. template static void log(const char* file, int line, LogSeverity severity, const char* macroArgs, Params&&... params); class Fault { public: template Fault(const char* file, int line, Code code, const char* condition, const char* macroArgs, Params&&... params); Fault(const char* file, int line, Exception::Type type, const char* condition, const char* macroArgs); Fault(const char* file, int line, int osErrorNumber, const char* condition, const char* macroArgs); #if _WIN32 || __CYGWIN__ Fault(const char* file, int line, Win32Result osErrorNumber, const char* condition, const char* macroArgs); #endif ~Fault() noexcept(false); KJ_NOINLINE KJ_NORETURN(void fatal()); // Throw the exception. private: void init(const char* file, int line, Exception::Type type, const char* condition, const char* macroArgs, ArrayPtr argValues); void init(const char* file, int line, int osErrorNumber, const char* condition, const char* macroArgs, ArrayPtr argValues); #if _WIN32 || __CYGWIN__ void init(const char* file, int line, Win32Result osErrorNumber, const char* condition, const char* macroArgs, ArrayPtr argValues); #endif Exception* exception; }; class SyscallResult { public: inline SyscallResult(int errorNumber): errorNumber(errorNumber) {} inline operator void*() { return errorNumber == 0 ? this : nullptr; } inline int getErrorNumber() { return errorNumber; } private: int errorNumber; }; template static SyscallResult syscall(Call&& call, bool nonblocking); template static int syscallError(Call&& call, bool nonblocking); #if _WIN32 || __CYGWIN__ static Win32Result win32Call(int boolean); static Win32Result win32Call(void* handle); static Win32Result winsockCall(int result); static uint getWin32ErrorCode(); #endif class Context: public ExceptionCallback { public: Context(); KJ_DISALLOW_COPY(Context); virtual ~Context() noexcept(false); struct Value { const char* file; int line; String description; inline Value(const char* file, int line, String&& description) : file(file), line(line), description(mv(description)) {} }; virtual Value evaluate() = 0; virtual void onRecoverableException(Exception&& exception) override; virtual void onFatalException(Exception&& exception) override; virtual void logMessage(LogSeverity severity, const char* file, int line, int contextDepth, String&& text) override; private: bool logged; Maybe value; Value ensureInitialized(); }; template class ContextImpl: public Context { public: inline ContextImpl(Func& func): func(func) {} KJ_DISALLOW_COPY(ContextImpl); Value evaluate() override { return func(); } private: Func& func; }; template static String makeDescription(const char* macroArgs, Params&&... params); private: static LogSeverity minSeverity; static void logInternal(const char* file, int line, LogSeverity severity, const char* macroArgs, ArrayPtr argValues); static String makeDescriptionInternal(const char* macroArgs, ArrayPtr argValues); static int getOsErrorNumber(bool nonblocking); // Get the error code of the last error (e.g. from errno). Returns -1 on EINTR. }; template void Debug::log(const char* file, int line, LogSeverity severity, const char* macroArgs, Params&&... params) { String argValues[sizeof...(Params)] = {str(params)...}; logInternal(file, line, severity, macroArgs, arrayPtr(argValues, sizeof...(Params))); } template <> inline void Debug::log<>(const char* file, int line, LogSeverity severity, const char* macroArgs) { logInternal(file, line, severity, macroArgs, nullptr); } template Debug::Fault::Fault(const char* file, int line, Code code, const char* condition, const char* macroArgs, Params&&... params) : exception(nullptr) { String argValues[sizeof...(Params)] = {str(params)...}; init(file, line, code, condition, macroArgs, arrayPtr(argValues, sizeof...(Params))); } inline Debug::Fault::Fault(const char* file, int line, int osErrorNumber, const char* condition, const char* macroArgs) : exception(nullptr) { init(file, line, osErrorNumber, condition, macroArgs, nullptr); } inline Debug::Fault::Fault(const char* file, int line, kj::Exception::Type type, const char* condition, const char* macroArgs) : exception(nullptr) { init(file, line, type, condition, macroArgs, nullptr); } #if _WIN32 || __CYGWIN__ inline Debug::Fault::Fault(const char* file, int line, Win32Result osErrorNumber, const char* condition, const char* macroArgs) : exception(nullptr) { init(file, line, osErrorNumber, condition, macroArgs, nullptr); } inline Debug::Win32Result Debug::win32Call(int boolean) { return boolean ? Win32Result(0) : Win32Result(getWin32ErrorCode()); } inline Debug::Win32Result Debug::win32Call(void* handle) { // Assume null and INVALID_HANDLE_VALUE mean failure. return win32Call(handle != nullptr && handle != (void*)-1); } inline Debug::Win32Result Debug::winsockCall(int result) { // Expect a return value of SOCKET_ERROR means failure. return win32Call(result != -1); } #endif template Debug::SyscallResult Debug::syscall(Call&& call, bool nonblocking) { while (call() < 0) { int errorNum = getOsErrorNumber(nonblocking); // getOsErrorNumber() returns -1 to indicate EINTR. // Also, if nonblocking is true, then it returns 0 on EAGAIN, which will then be treated as a // non-error. if (errorNum != -1) { return SyscallResult(errorNum); } } return SyscallResult(0); } template int Debug::syscallError(Call&& call, bool nonblocking) { while (call() < 0) { int errorNum = getOsErrorNumber(nonblocking); // getOsErrorNumber() returns -1 to indicate EINTR. // Also, if nonblocking is true, then it returns 0 on EAGAIN, which will then be treated as a // non-error. if (errorNum != -1) { return errorNum; } } return 0; } template String Debug::makeDescription(const char* macroArgs, Params&&... params) { String argValues[sizeof...(Params)] = {str(params)...}; return makeDescriptionInternal(macroArgs, arrayPtr(argValues, sizeof...(Params))); } template <> inline String Debug::makeDescription<>(const char* macroArgs) { return makeDescriptionInternal(macroArgs, nullptr); } } // namespace _ (private) } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/arena.h0000644000175000017500000002001213650101756017401 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" #include "array.h" #include "string.h" KJ_BEGIN_HEADER namespace kj { class Arena { // A class which allows several objects to be allocated in contiguous chunks of memory, then // frees them all at once. // // Allocating from the same Arena in multiple threads concurrently is NOT safe, because making // it safe would require atomic operations that would slow down allocation even when // single-threaded. If you need to use arena allocation in a multithreaded context, consider // allocating thread-local arenas. public: explicit Arena(size_t chunkSizeHint = 1024); // Create an Arena. `chunkSizeHint` hints at where to start when allocating chunks, but is only // a hint -- the Arena will, for example, allocate progressively larger chunks as time goes on, // in order to reduce overall allocation overhead. explicit Arena(ArrayPtr scratch); // Allocates from the given scratch space first, only resorting to the heap when it runs out. KJ_DISALLOW_COPY(Arena); ~Arena() noexcept(false); template T& allocate(Params&&... params); template ArrayPtr allocateArray(size_t size); // Allocate an object or array of type T. If T has a non-trivial destructor, that destructor // will be run during the Arena's destructor. Such destructors are run in opposite order of // allocation. Note that these methods must maintain a list of destructors to call, which has // overhead, but this overhead only applies if T has a non-trivial destructor. template Own allocateOwn(Params&&... params); template Array allocateOwnArray(size_t size); template ArrayBuilder allocateOwnArrayBuilder(size_t capacity); // Allocate an object or array of type T. Destructors are executed when the returned Own // or Array goes out-of-scope, which must happen before the Arena is destroyed. This variant // is useful when you need to control when the destructor is called. This variant also avoids // the need for the Arena itself to keep track of destructors to call later, which may make it // slightly more efficient. template inline T& copy(T&& value) { return allocate>(kj::fwd(value)); } // Allocate a copy of the given value in the arena. This is just a shortcut for calling the // type's copy (or move) constructor. StringPtr copyString(StringPtr content); // Make a copy of the given string inside the arena, and return a pointer to the copy. private: struct ChunkHeader { ChunkHeader* next; byte* pos; // first unallocated byte in this chunk byte* end; // end of this chunk }; struct ObjectHeader { void (*destructor)(void*); ObjectHeader* next; }; size_t nextChunkSize; ChunkHeader* chunkList = nullptr; ObjectHeader* objectList = nullptr; ChunkHeader* currentChunk = nullptr; void cleanup(); // Run all destructors, leaving the above pointers null. If a destructor throws, the State is // left in a consistent state, such that if cleanup() is called again, it will pick up where // it left off. void* allocateBytes(size_t amount, uint alignment, bool hasDisposer); // Allocate the given number of bytes. `hasDisposer` must be true if `setDisposer()` may be // called on this pointer later. void* allocateBytesInternal(size_t amount, uint alignment); // Try to allocate the given number of bytes without taking a lock. Fails if and only if there // is no space left in the current chunk. void setDestructor(void* ptr, void (*destructor)(void*)); // Schedule the given destructor to be executed when the Arena is destroyed. `ptr` must be a // pointer previously returned by an `allocateBytes()` call for which `hasDisposer` was true. template static void destroyArray(void* pointer) { size_t elementCount = *reinterpret_cast(pointer); constexpr size_t prefixSize = kj::max(alignof(T), sizeof(size_t)); DestructorOnlyArrayDisposer::instance.disposeImpl( reinterpret_cast(pointer) + prefixSize, sizeof(T), elementCount, elementCount, &destroyObject); } template static void destroyObject(void* pointer) { dtor(*reinterpret_cast(pointer)); } }; // ======================================================================================= // Inline implementation details template T& Arena::allocate(Params&&... params) { T& result = *reinterpret_cast(allocateBytes( sizeof(T), alignof(T), !__has_trivial_destructor(T))); if (!__has_trivial_constructor(T) || sizeof...(Params) > 0) { ctor(result, kj::fwd(params)...); } if (!__has_trivial_destructor(T)) { setDestructor(&result, &destroyObject); } return result; } template ArrayPtr Arena::allocateArray(size_t size) { if (__has_trivial_destructor(T)) { ArrayPtr result = arrayPtr(reinterpret_cast(allocateBytes( sizeof(T) * size, alignof(T), false)), size); if (!__has_trivial_constructor(T)) { for (size_t i = 0; i < size; i++) { ctor(result[i]); } } return result; } else { // Allocate with a 64-bit prefix in which we store the array size. constexpr size_t prefixSize = kj::max(alignof(T), sizeof(size_t)); void* base = allocateBytes(sizeof(T) * size + prefixSize, alignof(T), true); size_t& tag = *reinterpret_cast(base); ArrayPtr result = arrayPtr(reinterpret_cast(reinterpret_cast(base) + prefixSize), size); setDestructor(base, &destroyArray); if (__has_trivial_constructor(T)) { tag = size; } else { // In case of constructor exceptions, we need the tag to end up storing the number of objects // that were successfully constructed, so that they'll be properly destroyed. tag = 0; for (size_t i = 0; i < size; i++) { ctor(result[i]); tag = i + 1; } } return result; } } template Own Arena::allocateOwn(Params&&... params) { T& result = *reinterpret_cast(allocateBytes(sizeof(T), alignof(T), false)); if (!__has_trivial_constructor(T) || sizeof...(Params) > 0) { ctor(result, kj::fwd(params)...); } return Own(&result, DestructorOnlyDisposer::instance); } template Array Arena::allocateOwnArray(size_t size) { ArrayBuilder result = allocateOwnArrayBuilder(size); for (size_t i = 0; i < size; i++) { result.add(); } return result.finish(); } template ArrayBuilder Arena::allocateOwnArrayBuilder(size_t capacity) { return ArrayBuilder( reinterpret_cast(allocateBytes(sizeof(T) * capacity, alignof(T), false)), capacity, DestructorOnlyArrayDisposer::instance); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/io.h0000644000175000017500000003744113650101756016740 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include #include "common.h" #include "array.h" #include "exception.h" #include KJ_BEGIN_HEADER namespace kj { // ======================================================================================= // Abstract interfaces class InputStream { public: virtual ~InputStream() noexcept(false); size_t read(void* buffer, size_t minBytes, size_t maxBytes); // Reads at least minBytes and at most maxBytes, copying them into the given buffer. Returns // the size read. Throws an exception on errors. Implemented in terms of tryRead(). // // maxBytes is the number of bytes the caller really wants, but minBytes is the minimum amount // needed by the caller before it can start doing useful processing. If the stream returns less // than maxBytes, the caller will usually call read() again later to get the rest. Returning // less than maxBytes is useful when it makes sense for the caller to parallelize processing // with I/O. // // Never blocks if minBytes is zero. If minBytes is zero and maxBytes is non-zero, this may // attempt a non-blocking read or may just return zero. To force a read, use a non-zero minBytes. // To detect EOF without throwing an exception, use tryRead(). // // If the InputStream can't produce minBytes, it MUST throw an exception, as the caller is not // expected to understand how to deal with partial reads. virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0; // Like read(), but may return fewer than minBytes on EOF. inline void read(void* buffer, size_t bytes) { read(buffer, bytes, bytes); } // Convenience method for reading an exact number of bytes. virtual void skip(size_t bytes); // Skips past the given number of bytes, discarding them. The default implementation read()s // into a scratch buffer. String readAllText(uint64_t limit = kj::maxValue); Array readAllBytes(uint64_t limit = kj::maxValue); // Read until EOF and return as one big byte array or string. Throw an exception if EOF is not // seen before reading `limit` bytes. // // To prevent runaway memory allocation, consider using a more conservative value for `limit` than // the default, particularly on untrusted data streams which may never see EOF. }; class OutputStream { public: virtual ~OutputStream() noexcept(false); virtual void write(const void* buffer, size_t size) = 0; // Always writes the full size. Throws exception on error. virtual void write(ArrayPtr> pieces); // Equivalent to write()ing each byte array in sequence, which is what the default implementation // does. Override if you can do something better, e.g. use writev() to do the write in a single // syscall. }; class BufferedInputStream: public InputStream { // An input stream which buffers some bytes in memory to reduce system call overhead. // - OR - // An input stream that actually reads from some in-memory data structure and wants to give its // caller a direct pointer to that memory to potentially avoid a copy. public: virtual ~BufferedInputStream() noexcept(false); ArrayPtr getReadBuffer(); // Get a direct pointer into the read buffer, which contains the next bytes in the input. If the // caller consumes any bytes, it should then call skip() to indicate this. This always returns a // non-empty buffer or throws an exception. Implemented in terms of tryGetReadBuffer(). virtual ArrayPtr tryGetReadBuffer() = 0; // Like getReadBuffer() but may return an empty buffer on EOF. }; class BufferedOutputStream: public OutputStream { // An output stream which buffers some bytes in memory to reduce system call overhead. // - OR - // An output stream that actually writes into some in-memory data structure and wants to give its // caller a direct pointer to that memory to potentially avoid a copy. public: virtual ~BufferedOutputStream() noexcept(false); virtual ArrayPtr getWriteBuffer() = 0; // Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of // this buffer and then pass it to write(), in which case write() may avoid a copy. It is // incorrect to pass to write any slice of this buffer which is not a prefix. }; // ======================================================================================= // Buffered streams implemented as wrappers around regular streams class BufferedInputStreamWrapper: public BufferedInputStream { // Implements BufferedInputStream in terms of an InputStream. // // Note that the underlying stream's position is unpredictable once the wrapper is destroyed, // unless the entire stream was consumed. To read a predictable number of bytes in a buffered // way without going over, you'd need this wrapper to wrap some other wrapper which itself // implements an artificial EOF at the desired point. Such a stream should be trivial to write // but is not provided by the library at this time. public: explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr buffer = nullptr); // Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about // the position of the inner stream after a buffered wrapper has been created unless the entire // input is read. // // If the second parameter is non-null, the stream uses the given buffer instead of allocating // its own. This may improve performance if the buffer can be reused. KJ_DISALLOW_COPY(BufferedInputStreamWrapper); ~BufferedInputStreamWrapper() noexcept(false); // implements BufferedInputStream ---------------------------------- ArrayPtr tryGetReadBuffer() override; size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; void skip(size_t bytes) override; private: InputStream& inner; Array ownedBuffer; ArrayPtr buffer; ArrayPtr bufferAvailable; }; class BufferedOutputStreamWrapper: public BufferedOutputStream { // Implements BufferedOutputStream in terms of an OutputStream. Note that writes to the // underlying stream may be delayed until flush() is called or the wrapper is destroyed. public: explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr buffer = nullptr); // Creates a buffered stream wrapping the given non-buffered stream. // // If the second parameter is non-null, the stream uses the given buffer instead of allocating // its own. This may improve performance if the buffer can be reused. KJ_DISALLOW_COPY(BufferedOutputStreamWrapper); ~BufferedOutputStreamWrapper() noexcept(false); void flush(); // Force the wrapper to write any remaining bytes in its buffer to the inner stream. Note that // this only flushes this object's buffer; this object has no idea how to flush any other buffers // that may be present in the underlying stream. // implements BufferedOutputStream --------------------------------- ArrayPtr getWriteBuffer() override; void write(const void* buffer, size_t size) override; private: OutputStream& inner; Array ownedBuffer; ArrayPtr buffer; byte* bufferPos; UnwindDetector unwindDetector; }; // ======================================================================================= // Array I/O class ArrayInputStream: public BufferedInputStream { public: explicit ArrayInputStream(ArrayPtr array); KJ_DISALLOW_COPY(ArrayInputStream); ~ArrayInputStream() noexcept(false); // implements BufferedInputStream ---------------------------------- ArrayPtr tryGetReadBuffer() override; size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; void skip(size_t bytes) override; private: ArrayPtr array; }; class ArrayOutputStream: public BufferedOutputStream { public: explicit ArrayOutputStream(ArrayPtr array); KJ_DISALLOW_COPY(ArrayOutputStream); ~ArrayOutputStream() noexcept(false); ArrayPtr getArray() { // Get the portion of the array which has been filled in. return arrayPtr(array.begin(), fillPos); } // implements BufferedInputStream ---------------------------------- ArrayPtr getWriteBuffer() override; void write(const void* buffer, size_t size) override; private: ArrayPtr array; byte* fillPos; }; class VectorOutputStream: public BufferedOutputStream { public: explicit VectorOutputStream(size_t initialCapacity = 4096); KJ_DISALLOW_COPY(VectorOutputStream); ~VectorOutputStream() noexcept(false); ArrayPtr getArray() { // Get the portion of the array which has been filled in. return arrayPtr(vector.begin(), fillPos); } // implements BufferedInputStream ---------------------------------- ArrayPtr getWriteBuffer() override; void write(const void* buffer, size_t size) override; private: Array vector; byte* fillPos; void grow(size_t minSize); }; // ======================================================================================= // File descriptor I/O class AutoCloseFd { // A wrapper around a file descriptor which automatically closes the descriptor when destroyed. // The wrapper supports move construction for transferring ownership of the descriptor. If // close() returns an error, the destructor throws an exception, UNLESS the destructor is being // called during unwind from another exception, in which case the close error is ignored. // // If your code is not exception-safe, you should not use AutoCloseFd. In this case you will // have to call close() yourself and handle errors appropriately. public: inline AutoCloseFd(): fd(-1) {} inline AutoCloseFd(decltype(nullptr)): fd(-1) {} inline explicit AutoCloseFd(int fd): fd(fd) {} inline AutoCloseFd(AutoCloseFd&& other) noexcept: fd(other.fd) { other.fd = -1; } KJ_DISALLOW_COPY(AutoCloseFd); ~AutoCloseFd() noexcept(false); inline AutoCloseFd& operator=(AutoCloseFd&& other) { AutoCloseFd old(kj::mv(*this)); fd = other.fd; other.fd = -1; return *this; } inline AutoCloseFd& operator=(decltype(nullptr)) { AutoCloseFd old(kj::mv(*this)); return *this; } inline operator int() const { return fd; } inline int get() const { return fd; } operator bool() const = delete; // Deleting this operator prevents accidental use in boolean contexts, which // the int conversion operator above would otherwise allow. inline bool operator==(decltype(nullptr)) { return fd < 0; } inline bool operator!=(decltype(nullptr)) { return fd >= 0; } inline int release() { // Release ownership of an FD. Not recommended. int result = fd; fd = -1; return result; } private: int fd; }; inline auto KJ_STRINGIFY(const AutoCloseFd& fd) -> decltype(kj::toCharSequence(implicitCast(fd))) { return kj::toCharSequence(implicitCast(fd)); } class FdInputStream: public InputStream { // An InputStream wrapping a file descriptor. public: explicit FdInputStream(int fd): fd(fd) {} explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {} KJ_DISALLOW_COPY(FdInputStream); ~FdInputStream() noexcept(false); size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; inline int getFd() const { return fd; } private: int fd; AutoCloseFd autoclose; }; class FdOutputStream: public OutputStream { // An OutputStream wrapping a file descriptor. public: explicit FdOutputStream(int fd): fd(fd) {} explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {} KJ_DISALLOW_COPY(FdOutputStream); ~FdOutputStream() noexcept(false); void write(const void* buffer, size_t size) override; void write(ArrayPtr> pieces) override; inline int getFd() const { return fd; } private: int fd; AutoCloseFd autoclose; }; // ======================================================================================= // Win32 Handle I/O #ifdef _WIN32 class AutoCloseHandle { // A wrapper around a Win32 HANDLE which automatically closes the handle when destroyed. // The wrapper supports move construction for transferring ownership of the handle. If // CloseHandle() returns an error, the destructor throws an exception, UNLESS the destructor is // being called during unwind from another exception, in which case the close error is ignored. // // If your code is not exception-safe, you should not use AutoCloseHandle. In this case you will // have to call close() yourself and handle errors appropriately. public: inline AutoCloseHandle(): handle((void*)-1) {} inline AutoCloseHandle(decltype(nullptr)): handle((void*)-1) {} inline explicit AutoCloseHandle(void* handle): handle(handle) {} inline AutoCloseHandle(AutoCloseHandle&& other) noexcept: handle(other.handle) { other.handle = (void*)-1; } KJ_DISALLOW_COPY(AutoCloseHandle); ~AutoCloseHandle() noexcept(false); inline AutoCloseHandle& operator=(AutoCloseHandle&& other) { AutoCloseHandle old(kj::mv(*this)); handle = other.handle; other.handle = (void*)-1; return *this; } inline AutoCloseHandle& operator=(decltype(nullptr)) { AutoCloseHandle old(kj::mv(*this)); return *this; } inline operator void*() const { return handle; } inline void* get() const { return handle; } operator bool() const = delete; // Deleting this operator prevents accidental use in boolean contexts, which // the void* conversion operator above would otherwise allow. inline bool operator==(decltype(nullptr)) { return handle != (void*)-1; } inline bool operator!=(decltype(nullptr)) { return handle == (void*)-1; } inline void* release() { // Release ownership of an FD. Not recommended. void* result = handle; handle = (void*)-1; return result; } private: void* handle; // -1 (aka INVALID_HANDLE_VALUE) if not valid. }; class HandleInputStream: public InputStream { // An InputStream wrapping a Win32 HANDLE. public: explicit HandleInputStream(void* handle): handle(handle) {} explicit HandleInputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {} KJ_DISALLOW_COPY(HandleInputStream); ~HandleInputStream() noexcept(false); size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; private: void* handle; AutoCloseHandle autoclose; }; class HandleOutputStream: public OutputStream { // An OutputStream wrapping a Win32 HANDLE. public: explicit HandleOutputStream(void* handle): handle(handle) {} explicit HandleOutputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {} KJ_DISALLOW_COPY(HandleOutputStream); ~HandleOutputStream() noexcept(false); void write(const void* buffer, size_t size) override; private: void* handle; AutoCloseHandle autoclose; }; #endif // _WIN32 } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/tuple.h0000644000175000017500000004274413650101756017464 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file defines a notion of tuples that is simpler that `std::tuple`. It works as follows: // - `kj::Tuple is the type of a tuple of an A, a B, and a C. // - `kj::tuple(a, b, c)` returns a tuple containing a, b, and c. If any of these are themselves // tuples, they are flattened, so `tuple(a, tuple(b, c), d)` is equivalent to `tuple(a, b, c, d)`. // - `kj::get(myTuple)` returns the element of `myTuple` at index n. // - `kj::apply(func, ...)` calls func on the following arguments after first expanding any tuples // in the argument list. So `kj::apply(foo, a, tuple(b, c), d)` would call `foo(a, b, c, d)`. // // Note that: // - The type `Tuple` is a synonym for T. This is why `get` and `apply` are not members of the // type. // - It is illegal for an element of `Tuple` to itself be a tuple, as tuples are meant to be // flattened. // - It is illegal for an element of `Tuple` to be a reference, due to problems this would cause // with type inference and `tuple()`. #pragma once #include "common.h" KJ_BEGIN_HEADER namespace kj { namespace _ { // private template struct TypeByIndex_; template struct TypeByIndex_<0, First, Rest...> { typedef First Type; }; template struct TypeByIndex_ : public TypeByIndex_ {}; template struct TypeByIndex_ { static_assert(index != index, "Index out-of-range."); }; template using TypeByIndex = typename TypeByIndex_::Type; // Chose a particular type out of a list of types, by index. template struct Indexes {}; // Dummy helper type that just encapsulates a sequential list of indexes, so that we can match // templates against them and unpack them with '...'. template struct MakeIndexes_: public MakeIndexes_ {}; template struct MakeIndexes_<0, prefix...> { typedef Indexes Type; }; template using MakeIndexes = typename MakeIndexes_::Type; // Equivalent to Indexes<0, 1, 2, ..., end>. template class Tuple; template inline TypeByIndex& getImpl(Tuple& tuple); template inline TypeByIndex&& getImpl(Tuple&& tuple); template inline const TypeByIndex& getImpl(const Tuple& tuple); template struct TupleElement { // Encapsulates one element of a tuple. The actual tuple implementation multiply-inherits // from a TupleElement for each element, which is more efficient than a recursive definition. T value; TupleElement() KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY constexpr inline TupleElement(const T& value): value(value) {} constexpr inline TupleElement(T&& value): value(kj::mv(value)) {} }; template struct TupleElement { // A tuple containing references can be constucted using refTuple(). T& value; constexpr inline TupleElement(T& value): value(value) {} }; template struct TupleElement> { static_assert(sizeof(Tuple*) == 0, "Tuples cannot contain other tuples -- they should be flattened."); }; template struct TupleImpl; template struct TupleImpl, Types...> : public TupleElement... { // Implementation of Tuple. The only reason we need this rather than rolling this into class // Tuple (below) is so that we can get "indexes" as an unpackable list. static_assert(sizeof...(indexes) == sizeof...(Types), "Incorrect use of TupleImpl."); TupleImpl() KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY template inline TupleImpl(Params&&... params) : TupleElement(kj::fwd(params))... { // Work around Clang 3.2 bug 16303 where this is not detected. (Unfortunately, Clang sometimes // segfaults instead.) static_assert(sizeof...(params) == sizeof...(indexes), "Wrong number of parameters to Tuple constructor."); } template constexpr inline TupleImpl(Tuple&& other) : TupleElement(kj::fwd(getImpl(other)))... {} template constexpr inline TupleImpl(Tuple& other) : TupleElement(getImpl(other))... {} template constexpr inline TupleImpl(const Tuple& other) : TupleElement(getImpl(other))... {} }; struct MakeTupleFunc; struct MakeRefTupleFunc; template class Tuple { // The actual Tuple class (used for tuples of size other than 1). public: Tuple() KJ_DEFAULT_CONSTRUCTOR_VS2015_BUGGY template constexpr inline Tuple(Tuple&& other): impl(kj::mv(other)) {} template constexpr inline Tuple(Tuple& other): impl(other) {} template constexpr inline Tuple(const Tuple& other): impl(other) {} private: template constexpr Tuple(Params&&... params): impl(kj::fwd(params)...) {} TupleImpl, T...> impl; template friend inline TypeByIndex& getImpl(Tuple& tuple); template friend inline TypeByIndex&& getImpl(Tuple&& tuple); template friend inline const TypeByIndex& getImpl(const Tuple& tuple); friend struct MakeTupleFunc; friend struct MakeRefTupleFunc; }; template <> class Tuple<> { // Simplified zero-member version of Tuple. In particular this is important to make sure that // Tuple<>() is constexpr. }; template class Tuple; // Single-element tuple should never be used. The public API should ensure this. template inline TypeByIndex& getImpl(Tuple& tuple) { // Get member of a Tuple by index, e.g. `get<2>(myTuple)`. static_assert(index < sizeof...(T), "Tuple element index out-of-bounds."); return implicitCast>&>(tuple.impl).value; } template inline TypeByIndex&& getImpl(Tuple&& tuple) { // Get member of a Tuple by index, e.g. `get<2>(myTuple)`. static_assert(index < sizeof...(T), "Tuple element index out-of-bounds."); return kj::mv(implicitCast>&>(tuple.impl).value); } template inline const TypeByIndex& getImpl(const Tuple& tuple) { // Get member of a Tuple by index, e.g. `get<2>(myTuple)`. static_assert(index < sizeof...(T), "Tuple element index out-of-bounds."); return implicitCast>&>(tuple.impl).value; } template inline T&& getImpl(T&& value) { // Get member of a Tuple by index, e.g. `getImpl<2>(myTuple)`. // Non-tuples are equivalent to one-element tuples. static_assert(index == 0, "Tuple element index out-of-bounds."); return kj::fwd(value); } template struct ExpandAndApplyResult_; // Template which computes the return type of applying Func to T... after flattening tuples. // SoFar starts as Tuple<> and accumulates the flattened parameter types -- so after this template // is recursively expanded, T... is empty and SoFar is a Tuple containing all the parameters. template struct ExpandAndApplyResult_, First, Rest...> : public ExpandAndApplyResult_, Rest...> {}; template struct ExpandAndApplyResult_, Tuple, Rest...> : public ExpandAndApplyResult_, FirstTypes&&..., Rest...> {}; template struct ExpandAndApplyResult_, Tuple&, Rest...> : public ExpandAndApplyResult_, FirstTypes&..., Rest...> {}; template struct ExpandAndApplyResult_, const Tuple&, Rest...> : public ExpandAndApplyResult_, const FirstTypes&..., Rest...> {}; template struct ExpandAndApplyResult_> { typedef decltype(instance()(instance()...)) Type; }; template using ExpandAndApplyResult = typename ExpandAndApplyResult_, T...>::Type; // Computes the expected return type of `expandAndApply()`. template inline auto expandAndApply(Func&& func) -> ExpandAndApplyResult { return func(); } template struct ExpandAndApplyFunc { Func&& func; First&& first; ExpandAndApplyFunc(Func&& func, First&& first) : func(kj::fwd(func)), first(kj::fwd(first)) {} template auto operator()(T&&... params) -> decltype(this->func(kj::fwd(first), kj::fwd(params)...)) { return this->func(kj::fwd(first), kj::fwd(params)...); } }; template inline auto expandAndApply(Func&& func, First&& first, Rest&&... rest) -> ExpandAndApplyResult { return expandAndApply( ExpandAndApplyFunc(kj::fwd(func), kj::fwd(first)), kj::fwd(rest)...); } template inline auto expandAndApply(Func&& func, Tuple&& first, Rest&&... rest) -> ExpandAndApplyResult { return expandAndApplyWithIndexes(MakeIndexes(), kj::fwd(func), kj::mv(first), kj::fwd(rest)...); } template inline auto expandAndApply(Func&& func, Tuple& first, Rest&&... rest) -> ExpandAndApplyResult { return expandAndApplyWithIndexes(MakeIndexes(), kj::fwd(func), first, kj::fwd(rest)...); } template inline auto expandAndApply(Func&& func, const Tuple& first, Rest&&... rest) -> ExpandAndApplyResult { return expandAndApplyWithIndexes(MakeIndexes(), kj::fwd(func), first, kj::fwd(rest)...); } template inline auto expandAndApplyWithIndexes( Indexes, Func&& func, Tuple&& first, Rest&&... rest) -> ExpandAndApplyResult { return expandAndApply(kj::fwd(func), kj::mv(getImpl(first))..., kj::fwd(rest)...); } template inline auto expandAndApplyWithIndexes( Indexes, Func&& func, const Tuple& first, Rest&&... rest) -> ExpandAndApplyResult { return expandAndApply(kj::fwd(func), getImpl(first)..., kj::fwd(rest)...); } struct MakeTupleFunc { template Tuple...> operator()(Params&&... params) { return Tuple...>(kj::fwd(params)...); } template Decay operator()(Param&& param) { return kj::fwd(param); } }; struct MakeRefTupleFunc { template Tuple operator()(Params&&... params) { return Tuple(kj::fwd(params)...); } template Param operator()(Param&& param) { return kj::fwd(param); } }; } // namespace _ (private) template struct Tuple_ { typedef _::Tuple Type; }; template struct Tuple_ { typedef T Type; }; template using Tuple = typename Tuple_::Type; // Tuple type. `Tuple` (i.e. a single-element tuple) is a synonym for `T`. Tuples of size // other than 1 expand to an internal type. Either way, you can construct a Tuple using // `kj::tuple(...)`, get an element by index `i` using `kj::get(myTuple)`, and expand the tuple // as arguments to a function using `kj::apply(func, myTuple)`. // // Tuples are always flat -- that is, no element of a Tuple is ever itself a Tuple. If you // construct a tuple from other tuples, the elements are flattened and concatenated. template inline auto tuple(Params&&... params) -> decltype(_::expandAndApply(_::MakeTupleFunc(), kj::fwd(params)...)) { // Construct a new tuple from the given values. Any tuples in the argument list will be // flattened into the result. return _::expandAndApply(_::MakeTupleFunc(), kj::fwd(params)...); } template inline auto refTuple(Params&&... params) -> decltype(_::expandAndApply(_::MakeRefTupleFunc(), kj::fwd(params)...)) { // Like tuple(), but if the params include lvalue references, they will be captured as // references. rvalue references will still be captured as whole values (moved). return _::expandAndApply(_::MakeRefTupleFunc(), kj::fwd(params)...); } template inline auto get(Tuple&& tuple) -> decltype(_::getImpl(kj::fwd(tuple))) { // Unpack and return the tuple element at the given index. The index is specified as a template // parameter, e.g. `kj::get<3>(myTuple)`. return _::getImpl(kj::fwd(tuple)); } template inline auto apply(Func&& func, Params&&... params) -> decltype(_::expandAndApply(kj::fwd(func), kj::fwd(params)...)) { // Apply a function to some arguments, expanding tuples into separate arguments. return _::expandAndApply(kj::fwd(func), kj::fwd(params)...); } template struct TupleSize_ { static constexpr size_t size = 1; }; template struct TupleSize_<_::Tuple> { static constexpr size_t size = sizeof...(T); }; template constexpr size_t tupleSize() { return TupleSize_::size; } // Returns size of the tuple T. template struct IndexOfType_; template struct HasType_ { static constexpr bool value = false; }; template struct IndexOfType_ { static constexpr size_t value = 0; }; template struct HasType_ { static constexpr bool value = true; }; template struct IndexOfType_> { static constexpr size_t value = 0; static_assert(!HasType_>::value, "requested type appears multiple times in tuple"); }; template struct HasType_> { static constexpr bool value = true; }; template struct IndexOfType_> { static constexpr size_t value = IndexOfType_>::value + 1; }; template struct HasType_> { static constexpr bool value = HasType_>::value; }; template inline constexpr size_t indexOfType() { static_assert(HasType_::value, "type not present"); return IndexOfType_::value; } template struct TypeOfIndex_; template struct TypeOfIndex_<0, T> { typedef T Type; }; template struct TypeOfIndex_> : public TypeOfIndex_> {}; template struct TypeOfIndex_<0, _::Tuple> { typedef T Type; }; template using TypeOfIndex = typename TypeOfIndex_::Type; } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/one-of.h0000644000175000017500000003333413650101756017511 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "common.h" KJ_BEGIN_HEADER namespace kj { namespace _ { // private template class Fail, typename Key, typename... Variants> struct TypeIndex_; template class Fail, typename Key, typename First, typename... Rest> struct TypeIndex_ { static constexpr uint value = TypeIndex_::value; }; template class Fail, typename Key, typename... Rest> struct TypeIndex_ { static constexpr uint value = i; }; template class Fail, typename Key> struct TypeIndex_: public Fail {}; template struct OneOfFailError_ { static_assert(i == -1, "type does not match any in OneOf"); }; template struct OneOfFailZero_ { static constexpr int value = 0; }; template struct SuccessIfNotZero { typedef int Success; }; template <> struct SuccessIfNotZero<0> {}; enum class Variants0 {}; enum class Variants1 { _variant0 }; enum class Variants2 { _variant0, _variant1 }; enum class Variants3 { _variant0, _variant1, _variant2 }; enum class Variants4 { _variant0, _variant1, _variant2, _variant3 }; enum class Variants5 { _variant0, _variant1, _variant2, _variant3, _variant4 }; enum class Variants6 { _variant0, _variant1, _variant2, _variant3, _variant4, _variant5 }; enum class Variants7 { _variant0, _variant1, _variant2, _variant3, _variant4, _variant5, _variant6 }; enum class Variants8 { _variant0, _variant1, _variant2, _variant3, _variant4, _variant5, _variant6, _variant7 }; enum class Variants9 { _variant0, _variant1, _variant2, _variant3, _variant4, _variant5, _variant6, _variant7, _variant8 }; template struct Variants_; template <> struct Variants_<0> { typedef Variants0 Type; }; template <> struct Variants_<1> { typedef Variants1 Type; }; template <> struct Variants_<2> { typedef Variants2 Type; }; template <> struct Variants_<3> { typedef Variants3 Type; }; template <> struct Variants_<4> { typedef Variants4 Type; }; template <> struct Variants_<5> { typedef Variants5 Type; }; template <> struct Variants_<6> { typedef Variants6 Type; }; template <> struct Variants_<7> { typedef Variants7 Type; }; template <> struct Variants_<8> { typedef Variants8 Type; }; template <> struct Variants_<9> { typedef Variants9 Type; }; template using Variants = typename Variants_::Type; } // namespace _ (private) template class OneOf { template static inline constexpr uint typeIndex() { return _::TypeIndex_<1, _::OneOfFailError_, Key, Variants...>::value; } // Get the 1-based index of Key within the type list Types, or static_assert with a nice error. template static inline constexpr uint typeIndexOrZero() { return _::TypeIndex_<1, _::OneOfFailZero_, Key, Variants...>::value; } template struct HasAll; // Has a member type called "Success" if and only if all of `OtherVariants` are types that // appear in `Variants`. Used with SFINAE to enable subset constructors. public: inline OneOf(): tag(0) {} OneOf(const OneOf& other) { copyFrom(other); } OneOf(OneOf& other) { copyFrom(other); } OneOf(OneOf&& other) { moveFrom(other); } // Copy/move from same OneOf type. template ::Success> OneOf(const OneOf& other) { copyFromSubset(other); } template ::Success> OneOf(OneOf& other) { copyFromSubset(other); } template ::Success> OneOf(OneOf&& other) { moveFromSubset(other); } // Copy/move from OneOf that contains a subset of the types we do. template >::Success> OneOf(T&& other): tag(typeIndex>()) { ctor(*reinterpret_cast*>(space), kj::fwd(other)); } // Copy/move from a value that matches one of the individual types in the OneOf. ~OneOf() { destroy(); } OneOf& operator=(const OneOf& other) { if (tag != 0) destroy(); copyFrom(other); return *this; } OneOf& operator=(OneOf&& other) { if (tag != 0) destroy(); moveFrom(other); return *this; } inline bool operator==(decltype(nullptr)) const { return tag == 0; } inline bool operator!=(decltype(nullptr)) const { return tag != 0; } template bool is() const { return tag == typeIndex(); } template T& get() { KJ_IREQUIRE(is(), "Must check OneOf::is() before calling get()."); return *reinterpret_cast(space); } template const T& get() const { KJ_IREQUIRE(is(), "Must check OneOf::is() before calling get()."); return *reinterpret_cast(space); } template T& init(Params&&... params) { if (tag != 0) destroy(); ctor(*reinterpret_cast(space), kj::fwd(params)...); tag = typeIndex(); return *reinterpret_cast(space); } template Maybe tryGet() { if (is()) { return *reinterpret_cast(space); } else { return nullptr; } } template Maybe tryGet() const { if (is()) { return *reinterpret_cast(space); } else { return nullptr; } } template KJ_NORETURN(void allHandled()); // After a series of if/else blocks handling each variant of the OneOf, have the final else // block call allHandled() where n is the number of variants. This will fail to compile // if new variants are added in the future. typedef _::Variants Tag; Tag which() const { KJ_IREQUIRE(tag != 0, "Can't KJ_SWITCH_ONEOF() on uninitialized value."); return static_cast(tag - 1); } template static constexpr Tag tagFor() { return static_cast(typeIndex() - 1); } OneOf* _switchSubject() & { return this; } const OneOf* _switchSubject() const& { return this; } _::NullableValue _switchSubject() && { return kj::mv(*this); } private: uint tag; static inline constexpr size_t maxSize(size_t a) { return a; } template static inline constexpr size_t maxSize(size_t a, size_t b, Rest... rest) { return maxSize(kj::max(a, b), rest...); } // Returns the maximum of all the parameters. // TODO(someday): Generalize the above template and make it common. I tried, but C++ decided to // be difficult so I cut my losses. static constexpr auto spaceSize = maxSize(sizeof(Variants)...); // TODO(msvc): This constant could just as well go directly inside space's bracket's, where it's // used, but MSVC suffers a parse error on `...`. union { byte space[spaceSize]; void* forceAligned; // TODO(someday): Use C++11 alignas() once we require GCC 4.8 / Clang 3.3. }; template inline void doAll(T... t) {} template inline bool destroyVariant() { if (tag == typeIndex()) { tag = 0; dtor(*reinterpret_cast(space)); } return false; } void destroy() { doAll(destroyVariant()...); } template inline bool copyVariantFrom(const OneOf& other) { if (other.is()) { ctor(*reinterpret_cast(space), other.get()); } return false; } void copyFrom(const OneOf& other) { // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag // is invalid. tag = other.tag; doAll(copyVariantFrom(other)...); } template inline bool copyVariantFrom(OneOf& other) { if (other.is()) { ctor(*reinterpret_cast(space), other.get()); } return false; } void copyFrom(OneOf& other) { // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag // is invalid. tag = other.tag; doAll(copyVariantFrom(other)...); } template inline bool moveVariantFrom(OneOf& other) { if (other.is()) { ctor(*reinterpret_cast(space), kj::mv(other.get())); } return false; } void moveFrom(OneOf& other) { // Initialize as a copy of `other`. Expects that `this` starts out uninitialized, so the tag // is invalid. tag = other.tag; doAll(moveVariantFrom(other)...); } template inline bool copySubsetVariantFrom(const OneOf& other) { if (other.template is()) { tag = typeIndex>(); ctor(*reinterpret_cast(space), other.template get()); } return false; } template void copyFromSubset(const OneOf& other) { doAll(copySubsetVariantFrom(other)...); } template inline bool copySubsetVariantFrom(OneOf& other) { if (other.template is()) { tag = typeIndex>(); ctor(*reinterpret_cast(space), other.template get()); } return false; } template void copyFromSubset(OneOf& other) { doAll(copySubsetVariantFrom(other)...); } template inline bool moveSubsetVariantFrom(OneOf& other) { if (other.template is()) { tag = typeIndex>(); ctor(*reinterpret_cast(space), kj::mv(other.template get())); } return false; } template void moveFromSubset(OneOf& other) { doAll(moveSubsetVariantFrom(other)...); } }; template template struct OneOf::HasAll : public HasAll(), Rest...> {}; template template struct OneOf::HasAll: public _::SuccessIfNotZero {}; template template void OneOf::allHandled() { // After a series of if/else blocks handling each variant of the OneOf, have the final else // block call allHandled() where n is the number of variants. This will fail to compile // if new variants are added in the future. static_assert(i == sizeof...(Variants), "new OneOf variants need to be handled here"); KJ_UNREACHABLE; } #if __cplusplus > 201402L #define KJ_SWITCH_ONEOF(value) \ switch (auto _kj_switch_subject = (value)._switchSubject(); _kj_switch_subject->which()) #else #define KJ_SWITCH_ONEOF(value) \ /* Without C++17, we can only support one switch per containing block. Deal with it. */ \ auto _kj_switch_subject = (value)._switchSubject(); \ switch (_kj_switch_subject->which()) #endif #define KJ_CASE_ONEOF(name, ...) \ break; \ case ::kj::Decay::template tagFor<__VA_ARGS__>(): \ for (auto& name = _kj_switch_subject->template get<__VA_ARGS__>(), *_kj_switch_done = &name; \ _kj_switch_done; _kj_switch_done = nullptr) #define KJ_CASE_ONEOF_DEFAULT break; default: // Allows switching over a OneOf. // // Example: // // kj::OneOf variant; // KJ_SWITCH_ONEOF(variant) { // KJ_CASE_ONEOF(i, int) { // doSomethingWithInt(i); // } // KJ_CASE_ONEOF(s, const char*) { // doSomethingWithString(s); // } // KJ_CASE_ONEOF_DEFAULT { // doSomethingElse(); // } // } // // Notes: // - If you don't handle all possible types and don't include a default branch, you'll get a // compiler warning, just like a regular switch() over an enum where one of the enum values is // missing. // - There's no need for a `break` statement in a KJ_CASE_ONEOF; it is implied. // - Under C++11 and C++14, only one KJ_SWITCH_ONEOF() can appear in a block. Wrap the switch in // a pair of braces if you need a second switch in the same block. If C++17 is enabled, this is // not an issue. // // Implementation notes: // - The use of __VA_ARGS__ is to account for template types that have commas separating type // parameters, since macros don't recognize <> as grouping. // - _kj_switch_done is really used as a boolean flag to prevent the for() loop from actually // looping, but it's defined as a pointer since that's all we can define in this context. } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/function.h0000644000175000017500000002351313650101756020151 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" KJ_BEGIN_HEADER namespace kj { template class Function; // Function wrapper using virtual-based polymorphism. Use this when template polymorphism is // not possible. You can, for example, accept a Function as a parameter: // // void setFilter(Function filter); // // The caller of `setFilter()` may then pass any callable object as the parameter. The callable // object does not have to have the exact signature specified, just one that is "compatible" -- // i.e. the return type is covariant and the parameters are contravariant. // // Unlike `std::function`, `kj::Function`s are movable but not copyable, just like `kj::Own`. This // is to avoid unexpected heap allocation or slow atomic reference counting. // // When a `Function` is constructed from an lvalue, it captures only a reference to the value. // When constructed from an rvalue, it invokes the value's move constructor. So, for example: // // struct AddN { // int n; // int operator(int i) { return i + n; } // } // // Function f1 = AddN{2}; // // f1 owns an instance of AddN. It may safely be moved out // // of the local scope. // // AddN adder(2); // Function f2 = adder; // // f2 contains a reference to `adder`. Thus, it becomes invalid // // when `adder` goes out-of-scope. // // AddN adder2(2); // Function f3 = kj::mv(adder2); // // f3 owns an insatnce of AddN moved from `adder2`. f3 may safely // // be moved out of the local scope. // // Additionally, a Function may be bound to a class method using KJ_BIND_METHOD(object, methodName). // For example: // // class Printer { // public: // void print(int i); // void print(kj::StringPtr s); // }; // // Printer p; // // Function intPrinter = KJ_BIND_METHOD(p, print); // // Will call Printer::print(int). // // Function strPrinter = KJ_BIND_METHOD(p, print); // // Will call Printer::print(kj::StringPtr). // // Notice how KJ_BIND_METHOD is able to figure out which overload to use depending on the kind of // Function it is binding to. template class ConstFunction; // Like Function, but wraps a "const" (i.e. thread-safe) call. template class FunctionParam; // Like Function, but used specifically as a call parameter type. Does not do any heap allocation. // // This type MUST NOT be used for anything other than a parameter type to a function or method. // This is because if FunctionParam binds to a temporary, it assumes that the temporary will // outlive the FunctionParam instance. This is true when FunctionParam is used as a parameter type, // but not if it is used as a local variable nor a class member variable. template class Function { public: template inline Function(F&& f): impl(heap>(kj::fwd(f))) {} Function() = default; // Make sure people don't accidentally end up wrapping a reference when they meant to return // a function. KJ_DISALLOW_COPY(Function); Function(Function&) = delete; Function& operator=(Function&) = delete; template Function(const Function&) = delete; template Function& operator=(const Function&) = delete; template Function(const ConstFunction&) = delete; template Function& operator=(const ConstFunction&) = delete; Function(Function&&) = default; Function& operator=(Function&&) = default; inline Return operator()(Params... params) { return (*impl)(kj::fwd(params)...); } Function reference() { // Forms a new Function of the same type that delegates to this Function by reference. // Therefore, this Function must outlive the returned Function, but otherwise they behave // exactly the same. return *impl; } private: class Iface { public: virtual Return operator()(Params... params) = 0; }; template class Impl final: public Iface { public: explicit Impl(F&& f): f(kj::fwd(f)) {} Return operator()(Params... params) override { return f(kj::fwd(params)...); } private: F f; }; Own impl; }; template class ConstFunction { public: template inline ConstFunction(F&& f): impl(heap>(kj::fwd(f))) {} ConstFunction() = default; // Make sure people don't accidentally end up wrapping a reference when they meant to return // a function. KJ_DISALLOW_COPY(ConstFunction); ConstFunction(ConstFunction&) = delete; ConstFunction& operator=(ConstFunction&) = delete; template ConstFunction(const ConstFunction&) = delete; template ConstFunction& operator=(const ConstFunction&) = delete; template ConstFunction(const Function&) = delete; template ConstFunction& operator=(const Function&) = delete; ConstFunction(ConstFunction&&) = default; ConstFunction& operator=(ConstFunction&&) = default; inline Return operator()(Params... params) const { return (*impl)(kj::fwd(params)...); } ConstFunction reference() const { // Forms a new ConstFunction of the same type that delegates to this ConstFunction by reference. // Therefore, this ConstFunction must outlive the returned ConstFunction, but otherwise they // behave exactly the same. return *impl; } private: class Iface { public: virtual Return operator()(Params... params) const = 0; }; template class Impl final: public Iface { public: explicit Impl(F&& f): f(kj::fwd(f)) {} Return operator()(Params... params) const override { return f(kj::fwd(params)...); } private: F f; }; Own impl; }; template class FunctionParam { public: template FunctionParam(Func&& func) { typedef Wrapper> WrapperType; // All instances of Wrapper are two pointers in size: a vtable, and a Func&. So if we // allocate space for two pointers, we can construct a Wrapper in it! static_assert(sizeof(WrapperType) == sizeof(space), "expected WrapperType to be two pointers"); // Even if `func` is an rvalue reference, it's OK to use it as an lvalue here, because // FunctionParam is used strictly for parameters. If we captured a temporary, we know that // temporary will not be destroyed until after the function call completes. ctor(*reinterpret_cast(space), func); } FunctionParam(const FunctionParam& other) = default; FunctionParam(FunctionParam&& other) = default; // Magically, a plain copy works. inline Return operator()(Params... params) { return (*reinterpret_cast(space))(kj::fwd(params)...); } private: alignas(void*) char space[2 * sizeof(void*)]; class WrapperBase { public: virtual Return operator()(Params... params) = 0; }; template class Wrapper: public WrapperBase { public: Wrapper(Func& func): func(func) {} inline Return operator()(Params... params) override { return func(kj::fwd(params)...); } private: Func& func; }; }; namespace _ { // private template class BoundMethod { public: BoundMethod(T&& t, Func&& func, ConstFunc&& constFunc) : t(kj::fwd(t)), func(kj::mv(func)), constFunc(kj::mv(constFunc)) {} template auto operator()(Params&&... params) { return func(t, kj::fwd(params)...); } template auto operator()(Params&&... params) const { return constFunc(t, kj::fwd(params)...); } private: T t; Func func; ConstFunc constFunc; }; template BoundMethod boundMethod(T&& t, Func&& func, ConstFunc&& constFunc) { return { kj::fwd(t), kj::fwd(func), kj::fwd(constFunc) }; } } // namespace _ (private) #define KJ_BIND_METHOD(obj, method) \ ::kj::_::boundMethod(obj, \ [](auto& s, auto&&... p) mutable { return s.method(kj::fwd(p)...); }, \ [](auto& s, auto&&... p) { return s.method(kj::fwd(p)...); }) // Macro that produces a functor object which forwards to the method `obj.name`. If `obj` is an // lvalue, the functor will hold a reference to it. If `obj` is an rvalue, the functor will // contain a copy (by move) of it. The method is allowed to be overloaded. } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/mutex.h0000644000175000017500000004500013650101756017461 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" #include #include "time.h" KJ_BEGIN_HEADER #if __linux__ && !defined(KJ_USE_FUTEX) #define KJ_USE_FUTEX 1 #endif #if !KJ_USE_FUTEX && !_WIN32 && !__CYGWIN__ // We fall back to pthreads when we don't have a better platform-specific primitive. pthreads // mutexes are bloated, though, so we like to avoid them. Hence on Linux we use futex(), and on // Windows we use SRW locks and friends. On Cygwin we prefer the Win32 primitives both because they // are more efficient and because I ran into problems with Cygwin's implementation of RW locks // seeming to allow multiple threads to lock the same mutex (but I didn't investigate very // closely). // // TODO(someday): Write efficient low-level locking primitives for other platforms. #include #endif namespace kj { class Exception; // ======================================================================================= // Private details -- public interfaces follow below. namespace _ { // private class Mutex { // Internal implementation details. See `MutexGuarded`. struct Waiter; public: Mutex(); ~Mutex(); KJ_DISALLOW_COPY(Mutex); enum Exclusivity { EXCLUSIVE, SHARED }; void lock(Exclusivity exclusivity); void unlock(Exclusivity exclusivity, Waiter* waiterToSkip = nullptr); void assertLockedByCaller(Exclusivity exclusivity); // In debug mode, assert that the mutex is locked by the calling thread, or if that is // non-trivial, assert that the mutex is locked (which should be good enough to catch problems // in unit tests). In non-debug builds, do nothing. class Predicate { public: virtual bool check() = 0; }; void wait(Predicate& predicate, Maybe timeout = nullptr); // If predicate.check() returns false, unlock the mutex until predicate.check() returns true, or // when the timeout (if any) expires. The mutex is always re-locked when this returns regardless // of whether the timeout expired, and including if it throws. // // Requires that the mutex is already exclusively locked before calling. void induceSpuriousWakeupForTest(); // Utility method for mutex-test.c++ which causes a spurious thread wakeup on all threads that // are waiting for a wait() condition. Assuming correct implementation, all those threads // should immediately go back to sleep. private: #if KJ_USE_FUTEX uint futex; // bit 31 (msb) = set if exclusive lock held // bit 30 (msb) = set if threads are waiting for exclusive lock // bits 0-29 = count of readers; If an exclusive lock is held, this is the count of threads // waiting for a read lock, otherwise it is the count of threads that currently hold a read // lock. static constexpr uint EXCLUSIVE_HELD = 1u << 31; static constexpr uint EXCLUSIVE_REQUESTED = 1u << 30; static constexpr uint SHARED_COUNT_MASK = EXCLUSIVE_REQUESTED - 1; #elif _WIN32 || __CYGWIN__ uintptr_t srwLock; // Actually an SRWLOCK, but don't want to #include in header. #else mutable pthread_rwlock_t mutex; #endif struct Waiter { kj::Maybe next; kj::Maybe* prev; Predicate& predicate; Maybe> exception; #if KJ_USE_FUTEX uint futex; bool hasTimeout; #elif _WIN32 || __CYGWIN__ uintptr_t condvar; // Actually CONDITION_VARIABLE, but don't want to #include in header. #else pthread_cond_t condvar; pthread_mutex_t stupidMutex; // pthread condvars are only compatible with basic pthread mutexes, not rwlocks, for no // particularly good reason. To work around this, we need an extra mutex per condvar. #endif }; kj::Maybe waitersHead = nullptr; kj::Maybe* waitersTail = &waitersHead; // linked list of waitUntil()s; can only modify under lock inline void addWaiter(Waiter& waiter); inline void removeWaiter(Waiter& waiter); bool checkPredicate(Waiter& waiter); #if _WIN32 || __CYGWIN__ void wakeReadyWaiter(Waiter* waiterToSkip); #endif }; class Once { // Internal implementation details. See `Lazy`. public: #if KJ_USE_FUTEX inline Once(bool startInitialized = false) : futex(startInitialized ? INITIALIZED : UNINITIALIZED) {} #else Once(bool startInitialized = false); ~Once(); #endif KJ_DISALLOW_COPY(Once); class Initializer { public: virtual void run() = 0; }; void runOnce(Initializer& init); #if _WIN32 || __CYGWIN__ // TODO(perf): Can we make this inline on win32 somehow? bool isInitialized() noexcept; #else inline bool isInitialized() noexcept { // Fast path check to see if runOnce() would simply return immediately. #if KJ_USE_FUTEX return __atomic_load_n(&futex, __ATOMIC_ACQUIRE) == INITIALIZED; #else return __atomic_load_n(&state, __ATOMIC_ACQUIRE) == INITIALIZED; #endif } #endif void reset(); // Returns the state from initialized to uninitialized. It is an error to call this when // not already initialized, or when runOnce() or isInitialized() might be called concurrently in // another thread. private: #if KJ_USE_FUTEX uint futex; enum State { UNINITIALIZED, INITIALIZING, INITIALIZING_WITH_WAITERS, INITIALIZED }; #elif _WIN32 || __CYGWIN__ uintptr_t initOnce; // Actually an INIT_ONCE, but don't want to #include in header. #else enum State { UNINITIALIZED, INITIALIZED }; State state; pthread_mutex_t mutex; #endif }; } // namespace _ (private) // ======================================================================================= // Public interface template class Locked { // Return type for `MutexGuarded::lock()`. `Locked` provides access to the bounded object // and unlocks the mutex when it goes out of scope. public: KJ_DISALLOW_COPY(Locked); inline Locked(): mutex(nullptr), ptr(nullptr) {} inline Locked(Locked&& other): mutex(other.mutex), ptr(other.ptr) { other.mutex = nullptr; other.ptr = nullptr; } inline ~Locked() { if (mutex != nullptr) mutex->unlock(isConst() ? _::Mutex::SHARED : _::Mutex::EXCLUSIVE); } inline Locked& operator=(Locked&& other) { if (mutex != nullptr) mutex->unlock(isConst() ? _::Mutex::SHARED : _::Mutex::EXCLUSIVE); mutex = other.mutex; ptr = other.ptr; other.mutex = nullptr; other.ptr = nullptr; return *this; } inline void release() { if (mutex != nullptr) mutex->unlock(isConst() ? _::Mutex::SHARED : _::Mutex::EXCLUSIVE); mutex = nullptr; ptr = nullptr; } inline T* operator->() { return ptr; } inline const T* operator->() const { return ptr; } inline T& operator*() { return *ptr; } inline const T& operator*() const { return *ptr; } inline T* get() { return ptr; } inline const T* get() const { return ptr; } inline operator T*() { return ptr; } inline operator const T*() const { return ptr; } template void wait(Cond&& condition, Maybe timeout = nullptr) { // Unlocks the lock until `condition(state)` evaluates true (where `state` is type `const T&` // referencing the object protected by the lock). // We can't wait on a shared lock because the internal bookkeeping needed for a wait requires // the protection of an exclusive lock. static_assert(!isConst(), "cannot wait() on shared lock"); struct PredicateImpl final: public _::Mutex::Predicate { bool check() override { return condition(value); } Cond&& condition; const T& value; PredicateImpl(Cond&& condition, const T& value) : condition(kj::fwd(condition)), value(value) {} }; PredicateImpl impl(kj::fwd(condition), *ptr); mutex->wait(impl, timeout); } private: _::Mutex* mutex; T* ptr; inline Locked(_::Mutex& mutex, T& value): mutex(&mutex), ptr(&value) {} template friend class MutexGuarded; template friend class ExternalMutexGuarded; #if KJ_MUTEX_TEST public: #endif void induceSpuriousWakeupForTest() { mutex->induceSpuriousWakeupForTest(); } // Utility method for mutex-test.c++ which causes a spurious thread wakeup on all threads that // are waiting for a when() condition. Assuming correct implementation, all those threads should // immediately go back to sleep. }; template class MutexGuarded { // An object of type T, bounded by a mutex. In order to access the object, you must lock it. // // Write locks are not "recursive" -- trying to lock again in a thread that already holds a lock // will deadlock. Recursive write locks are usually a sign of bad design. // // Unfortunately, **READ LOCKS ARE NOT RECURSIVE** either. Common sense says they should be. // But on many operating systems (BSD, OSX), recursively read-locking a pthread_rwlock is // actually unsafe. The problem is that writers are "prioritized" over readers, so a read lock // request will block if any write lock requests are outstanding. So, if thread A takes a read // lock, thread B requests a write lock (and starts waiting), and then thread A tries to take // another read lock recursively, the result is deadlock. public: template explicit MutexGuarded(Params&&... params); // Initialize the mutex-bounded object by passing the given parameters to its constructor. Locked lockExclusive() const; // Exclusively locks the object and returns it. The returned `Locked` can be passed by // move, similar to `Own`. // // This method is declared `const` in accordance with KJ style rules which say that constness // should be used to indicate thread-safety. It is safe to share a const pointer between threads, // but it is not safe to share a mutable pointer. Since the whole point of MutexGuarded is to // be shared between threads, its methods should be const, even though locking it produces a // non-const pointer to the contained object. Locked lockShared() const; // Lock the value for shared access. Multiple shared locks can be taken concurrently, but cannot // be held at the same time as a non-shared lock. inline const T& getWithoutLock() const { return value; } inline T& getWithoutLock() { return value; } // Escape hatch for cases where some external factor guarantees that it's safe to get the // value. You should treat these like const_cast -- be highly suspicious of any use. inline const T& getAlreadyLockedShared() const; inline T& getAlreadyLockedShared(); inline T& getAlreadyLockedExclusive() const; // Like `getWithoutLock()`, but asserts that the lock is already held by the calling thread. template auto when(Cond&& condition, Func&& callback, Maybe timeout = nullptr) const -> decltype(callback(instance())) { // Waits until condition(state) returns true, then calls callback(state) under lock. // // `condition`, when called, receives as its parameter a const reference to the state, which is // locked (either shared or exclusive). `callback` receives a mutable reference, which is // exclusively locked. // // `condition()` may be called multiple times, from multiple threads, while waiting for the // condition to become true. It may even return true once, but then be called more times. // It is guaranteed, though, that at the time `callback()` is finally called, `condition()` // would currently return true (assuming it is a pure function of the guarded data). // // If `timeout` is specified, then after the given amount of time, the callback will be called // regardless of whether the condition is true. In this case, when `callback()` is called, // `condition()` may in fact evaluate false, but *only* if the timeout was reached. // // TODO(cleanup): lock->wait() is a better interface. Can we deprecate this one? auto lock = lockExclusive(); lock.wait(kj::fwd(condition), timeout); return callback(value); } private: mutable _::Mutex mutex; mutable T value; }; template class MutexGuarded { // MutexGuarded cannot guard a const type. This would be pointless anyway, and would complicate // the implementation of Locked, which uses constness to decide what kind of lock it holds. static_assert(sizeof(T) < 0, "MutexGuarded's type cannot be const."); }; template class ExternalMutexGuarded { // Holds a value that can only be manipulated while some other mutex is locked. // // The ExternalMutexGuarded lives *outside* the scope of any lock on the mutex, but ensures // that the value it holds can only be accessed under lock by forcing the caller to present a // lock before accessing the value. // // Additionally, ExternalMutexGuarded's destructor will take an exclusive lock on the mutex // while destroying the held value, unless the value has been release()ed before hand. // // The type T must have the following properties (which probably all movable types satisfy): // - T is movable. // - Immediately after any of the following has happened, T's destructor is effectively a no-op // (hence certainly not requiring locks): // - The value has been default-constructed. // - The value has been initialized by-move from a default-constructed T. // - The value has been moved away. // - If ExternalMutexGuarded is ever moved, then T must have a move constructor and move // assignment operator that do not follow any pointers, therefore do not need to take a lock. public: ExternalMutexGuarded() = default; ~ExternalMutexGuarded() noexcept(false) { if (mutex != nullptr) { mutex->lock(_::Mutex::EXCLUSIVE); KJ_DEFER(mutex->unlock(_::Mutex::EXCLUSIVE)); value = T(); } } ExternalMutexGuarded(ExternalMutexGuarded&& other) : mutex(other.mutex), value(kj::mv(other.value)) { other.mutex = nullptr; } ExternalMutexGuarded& operator=(ExternalMutexGuarded&& other) { mutex = other.mutex; value = kj::mv(other.value); other.mutex = nullptr; return *this; } template void set(Locked& lock, T&& newValue) { KJ_IREQUIRE(mutex == nullptr); mutex = lock.mutex; value = kj::mv(newValue); } template T& get(Locked& lock) { KJ_IREQUIRE(lock.mutex == mutex); return value; } template const T& get(Locked& lock) const { KJ_IREQUIRE(lock.mutex == mutex); return value; } template T release(Locked& lock) { // Release (move away) the value. This allows the destructor to skip locking the mutex. KJ_IREQUIRE(lock.mutex == mutex); T result = kj::mv(value); mutex = nullptr; return result; } private: _::Mutex* mutex = nullptr; T value; }; template class Lazy { // A lazily-initialized value. public: template T& get(Func&& init); template const T& get(Func&& init) const; // The first thread to call get() will invoke the given init function to construct the value. // Other threads will block until construction completes, then return the same value. // // `init` is a functor(typically a lambda) which takes `SpaceFor&` as its parameter and returns // `Own`. If `init` throws an exception, the exception is propagated out of that thread's // call to `get()`, and subsequent calls behave as if `get()` hadn't been called at all yet -- // in other words, subsequent calls retry initialization until it succeeds. private: mutable _::Once once; mutable SpaceFor space; mutable Own value; template class InitImpl; }; // ======================================================================================= // Inline implementation details template template inline MutexGuarded::MutexGuarded(Params&&... params) : value(kj::fwd(params)...) {} template inline Locked MutexGuarded::lockExclusive() const { mutex.lock(_::Mutex::EXCLUSIVE); return Locked(mutex, value); } template inline Locked MutexGuarded::lockShared() const { mutex.lock(_::Mutex::SHARED); return Locked(mutex, value); } template inline const T& MutexGuarded::getAlreadyLockedShared() const { #ifdef KJ_DEBUG mutex.assertLockedByCaller(_::Mutex::SHARED); #endif return value; } template inline T& MutexGuarded::getAlreadyLockedShared() { #ifdef KJ_DEBUG mutex.assertLockedByCaller(_::Mutex::SHARED); #endif return value; } template inline T& MutexGuarded::getAlreadyLockedExclusive() const { #ifdef KJ_DEBUG mutex.assertLockedByCaller(_::Mutex::EXCLUSIVE); #endif return const_cast(value); } template template class Lazy::InitImpl: public _::Once::Initializer { public: inline InitImpl(const Lazy& lazy, Func&& func): lazy(lazy), func(kj::fwd(func)) {} void run() override { lazy.value = func(lazy.space); } private: const Lazy& lazy; Func func; }; template template inline T& Lazy::get(Func&& init) { if (!once.isInitialized()) { InitImpl initImpl(*this, kj::fwd(init)); once.runOnce(initImpl); } return *value; } template template inline const T& Lazy::get(Func&& init) const { if (!once.isInitialized()) { InitImpl initImpl(*this, kj::fwd(init)); once.runOnce(initImpl); } return *value; } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/thread.h0000644000175000017500000000464113650101756017574 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "common.h" #include "function.h" #include "exception.h" KJ_BEGIN_HEADER namespace kj { class Thread { // A thread! Pass a lambda to the constructor, and it runs in the thread. The destructor joins // the thread. If the function throws an exception, it is rethrown from the thread's destructor // (if not unwinding from another exception). public: explicit Thread(Function func); KJ_DISALLOW_COPY(Thread); ~Thread() noexcept(false); #if !_WIN32 void sendSignal(int signo); // Send a Unix signal to the given thread, using pthread_kill or an equivalent. #endif void detach(); // Don't join the thread in ~Thread(). private: struct ThreadState { ThreadState(Function func); Function func; Function)> initializer; kj::Maybe exception; unsigned int refcount; // Owned by the parent thread and the child thread. void unref(); }; ThreadState* state; #if _WIN32 void* threadHandle; #else unsigned long long threadId; // actually pthread_t #endif bool detached = false; #if _WIN32 static unsigned long __stdcall runThread(void* ptr); #else static void* runThread(void* ptr); #endif }; } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/threadlocal.h0000644000175000017500000000545713650101756020615 0ustar00kentonkenton00000000000000// Copyright (c) 2014, Jason Choy // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // This file declares a macro `KJ_THREADLOCAL_PTR` for declaring thread-local pointer-typed // variables. Use like: // KJ_THREADLOCAL_PTR(MyType) foo = nullptr; // This is equivalent to: // thread_local MyType* foo = nullptr; // This can only be used at the global scope. // // AVOID USING THIS. Use of thread-locals is discouraged because they often have many of the same // properties as singletons: http://www.object-oriented-security.org/lets-argue/singletons // // Also, thread-locals tend to be hostile to event-driven code, which can be particularly // surprising when using fibers (all fibers in the same thread will share the same threadlocals, // even though they do not share a stack). // // That said, thread-locals are sometimes needed for runtime logistics in the KJ framework. For // example, the current exception callback and current EventLoop are stored as thread-local // pointers. Since KJ only ever needs to store pointers, not values, we avoid the question of // whether these values' destructors need to be run, and we avoid the need for heap allocation. #include "common.h" KJ_BEGIN_HEADER namespace kj { #if __GNUC__ #define KJ_THREADLOCAL_PTR(type) static __thread type* // GCC's __thread is lighter-weight than thread_local and is good enough for our purposes. // // TODO(cleanup): The above comment was written many years ago. Is it still true? Shouldn't the // compiler be smart enough to optimize a thread_local of POD type? #else #define KJ_THREADLOCAL_PTR(type) static thread_local type* #endif // KJ_USE_PTHREAD_TLS } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/filesystem.h0000644000175000017500000014574513340402540020513 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "memory.h" #include "io.h" #include #include "time.h" #include "function.h" #include "hash.h" namespace kj { template class Vector; class PathPtr; class Path { // A Path identifies a file in a directory tree. // // In KJ, we avoid representing paths as plain strings because this can lead to path injection // bugs as well as numerous kinds of bugs relating to path parsing edge cases. The Path class's // interface is designed to "make it hard to screw up". // // A "Path" is in fact a list of strings, each string being one component of the path (as would // normally be separated by '/'s). Path components are not allowed to contain '/' nor '\0', nor // are they allowed to be the special names "", ".", nor "..". // // If you explicitly want to parse a path that contains '/'s, ".", and "..", you must use // parse() and/or eval(). However, users of this interface are encouraged to avoid parsing // paths at all, and instead express paths as string arrays. // // Note that when using the Path class, ".." is always canonicalized in path space without // consulting the actual filesystem. This means that "foo/some-symlink/../bar" is exactly // equivalent to "foo/bar". This differs from the kernel's behavior when resolving paths passed // to system calls: the kernel would have resolved "some-symlink" to its target physical path, // and then would have interpreted ".." relative to that. In practice, the kernel's behavior is // rarely what the user or programmer intended, hence canonicalizing in path space produces a // better result. // // Path objects are "immutable": functions that "modify" the path return a new path. However, // if the path being operated on is an rvalue, copying can be avoided. Hence it makes sense to // write code like: // // Path p = ...; // p = kj::mv(p).append("bar"); // in-place update, avoids string copying public: Path(decltype(nullptr)); // empty path explicit Path(StringPtr name); explicit Path(String&& name); // Create a Path containing only one component. `name` is a single filename; it cannot contain // '/' nor '\0' nor can it be exactly "" nor "." nor "..". // // If you want to allow '/'s and such, you must call Path::parse(). We force you to do this to // prevent path injection bugs where you didn't consider what would happen if the path contained // a '/'. explicit Path(std::initializer_list parts); explicit Path(ArrayPtr parts); explicit Path(Array parts); // Construct a path from an array. Note that this means you can do: // // Path{"foo", "bar", "baz"} // equivalent to Path::parse("foo/bar/baz") KJ_DISALLOW_COPY(Path); Path(Path&&) = default; Path& operator=(Path&&) = default; Path clone() const; static Path parse(StringPtr path); // Parses a path in traditional format. Components are separated by '/'. Any use of "." or // ".." will be canonicalized (if they can't be canonicalized, e.g. because the path starts with // "..", an exception is thrown). Multiple consecutive '/'s will be collapsed. A leading '/' // is NOT accepted -- if that is a problem, you probably want `eval()`. Trailing '/'s are // ignored. Path append(Path&& suffix) const&; Path append(Path&& suffix) &&; Path append(PathPtr suffix) const&; Path append(PathPtr suffix) &&; Path append(StringPtr suffix) const&; Path append(StringPtr suffix) &&; Path append(String&& suffix) const&; Path append(String&& suffix) &&; // Create a new path by appending the given path to this path. // // `suffix` cannot contain '/' characters. Instead, you can append an array: // // path.append({"foo", "bar"}) // // Or, use Path::parse(): // // path.append(Path::parse("foo//baz/../bar")) Path eval(StringPtr pathText) const&; Path eval(StringPtr pathText) &&; // Evaluates a traditional path relative to this one. `pathText` is parsed like `parse()` would, // except that: // - It can contain leading ".." components that traverse up the tree. // - It can have a leading '/' which completely replaces the current path. // // THE NAME OF THIS METHOD WAS CHOSEN TO INSPIRE FEAR. // // Instead of using `path.eval(str)`, always consider whether you really want // `path.append(Path::parse(str))`. The former is much riskier than the latter in terms of path // injection vulnerabilities. PathPtr basename() const&; Path basename() &&; // Get the last component of the path. (Use `basename()[0]` to get just the string.) PathPtr parent() const&; Path parent() &&; // Get the parent path. String toString(bool absolute = false) const; // Converts the path to a traditional path string, appropriate to pass to a unix system call. // Never throws. const String& operator[](size_t i) const&; String operator[](size_t i) &&; size_t size() const; const String* begin() const; const String* end() const; PathPtr slice(size_t start, size_t end) const&; Path slice(size_t start, size_t end) &&; // A Path can be accessed as an array of strings. bool operator==(PathPtr other) const; bool operator!=(PathPtr other) const; bool operator< (PathPtr other) const; bool operator> (PathPtr other) const; bool operator<=(PathPtr other) const; bool operator>=(PathPtr other) const; // Compare path components lexically. uint hashCode() const; // Can use in HashMap. bool startsWith(PathPtr prefix) const; bool endsWith(PathPtr suffix) const; // Compare prefix / suffix. Path evalWin32(StringPtr pathText) const&; Path evalWin32(StringPtr pathText) &&; // Evaluates a Win32-style path, as might be written by a user. Differences from `eval()` // include: // // - Backslashes can be used as path separators. // - Absolute paths begin with a drive letter followed by a colon. The drive letter, including // the colon, will become the first component of the path, e.g. "c:\foo" becomes {"c:", "foo"}. // - A network path like "\\host\share\path" is parsed as {"host", "share", "path"}. Path evalNative(StringPtr pathText) const&; Path evalNative(StringPtr pathText) &&; // Alias for either eval() or evalWin32() depending on the target platform. Use this when you are // parsing a path provided by a user and you want the user to be able to use the "natural" format // for their platform. String toWin32String(bool absolute = false) const; // Converts the path to a Win32 path string, as you might display to a user. // // This is meant for display. For making Win32 system calls, consider `toWin32Api()` instead. // // If `absolute` is true, the path is expected to be an absolute path, meaning the first // component is a drive letter, namespace, or network host name. These are converted to their // regular Win32 format -- i.e. this method does the reverse of `evalWin32()`. // // This throws if the path would have unexpected special meaning or is otherwise invalid on // Windows, such as if it contains backslashes (within a path component), colons, or special // names like "con". String toNativeString(bool absolute = false) const; // Alias for either toString() or toWin32String() depending on the target platform. Use this when // you are formatting a path to display to a user and you want to present it in the "natural" // format for the user's platform. Array forWin32Api(bool absolute) const; // Like toWin32String, but additionally: // - Converts the path to UTF-16, with a NUL terminator included. // - For absolute paths, adds the "\\?\" prefix which opts into permitting paths longer than // MAX_PATH, and turns off relative path processing (which KJ paths already handle in userspace // anyway). // // This method is good to use when making a Win32 API call, e.g.: // // DeleteFileW(path.forWin32Api(true).begin()); static Path parseWin32Api(ArrayPtr text); // Parses an absolute path as returned by a Win32 API call like GetFinalPathNameByHandle() or // GetCurrentDirectory(). A "\\?\" prefix is optional but understood if present. // // Since such Win32 API calls generally return a length, this function inputs an array slice. // The slice should not include any NUL terminator. private: Array parts; // TODO(perf): Consider unrolling one element from `parts`, so that a one-element path doesn't // require allocation of an array. enum { ALREADY_CHECKED }; Path(Array parts, decltype(ALREADY_CHECKED)); friend class PathPtr; static String stripNul(String input); static void validatePart(StringPtr part); static void evalPart(Vector& parts, ArrayPtr part); static Path evalImpl(Vector&& parts, StringPtr path); static Path evalWin32Impl(Vector&& parts, StringPtr path, bool fromApi = false); static size_t countParts(StringPtr path); static size_t countPartsWin32(StringPtr path); static bool isWin32Drive(ArrayPtr part); static bool isNetbiosName(ArrayPtr part); static bool isWin32Special(StringPtr part); }; class PathPtr { // Points to a Path or a slice of a Path, but doesn't own it. // // PathPtr is to Path as ArrayPtr is to Array and StringPtr is to String. public: PathPtr(decltype(nullptr)); PathPtr(const Path& path); Path clone(); Path append(Path&& suffix) const; Path append(PathPtr suffix) const; Path append(StringPtr suffix) const; Path append(String&& suffix) const; Path eval(StringPtr pathText) const; PathPtr basename() const; PathPtr parent() const; String toString(bool absolute = false) const; const String& operator[](size_t i) const; size_t size() const; const String* begin() const; const String* end() const; PathPtr slice(size_t start, size_t end) const; bool operator==(PathPtr other) const; bool operator!=(PathPtr other) const; bool operator< (PathPtr other) const; bool operator> (PathPtr other) const; bool operator<=(PathPtr other) const; bool operator>=(PathPtr other) const; uint hashCode() const; bool startsWith(PathPtr prefix) const; bool endsWith(PathPtr suffix) const; Path evalWin32(StringPtr pathText) const; Path evalNative(StringPtr pathText) const; String toWin32String(bool absolute = false) const; String toNativeString(bool absolute = false) const; Array forWin32Api(bool absolute) const; // Equivalent to the corresponding methods of `Path`. private: ArrayPtr parts; explicit PathPtr(ArrayPtr parts); String toWin32StringImpl(bool absolute, bool forApi) const; friend class Path; }; // ======================================================================================= // The filesystem API // // This API is strictly synchronous because, unfortunately, there's no such thing as asynchronous // filesystem access in practice. The filesystem drivers on Linux are written to assume they can // block. The AIO API is only actually asynchronous for reading/writing the raw file blocks, but if // the filesystem needs to be involved (to allocate blocks, update metadata, etc.) that will block. // It's best to imagine that the filesystem is just another tier of memory that happens to be // slower than RAM (which is slower than L3 cache, which is slower than L2, which is slower than // L1). You can't do asynchronous RAM access so why asynchronous filesystem? The only way to // parallelize these is using threads. // // All KJ filesystem objects are thread-safe, and so all methods are marked "const" (even write // methods). Of course, if you concurrently write the same bytes of a file from multiple threads, // it's unspecified which write will "win". class FsNode { // Base class for filesystem node types. public: Own clone() const; // Creates a new object of exactly the same type as this one, pointing at exactly the same // external object. // // Under the hood, this will call dup(), so the FD number will not be the same. virtual Maybe getFd() const { return nullptr; } // Get the underlying Unix file descriptor, if any. Returns nullptr if this object actually isn't // wrapping a file descriptor. virtual Maybe getWin32Handle() const { return nullptr; } // Get the underlying Win32 HANDLE, if any. Returns nullptr if this object actually isn't // wrapping a handle. enum class Type { FILE, DIRECTORY, SYMLINK, BLOCK_DEVICE, CHARACTER_DEVICE, NAMED_PIPE, SOCKET, OTHER, }; struct Metadata { Type type = Type::FILE; uint64_t size = 0; // Logical size of the file. uint64_t spaceUsed = 0; // Physical size of the file on disk. May be smaller for sparse files, or larger for // pre-allocated files. Date lastModified = UNIX_EPOCH; // Last modification time of the file. uint linkCount = 1; // Number of hard links pointing to this node. uint64_t hashCode = 0; // Hint which can be used to determine if two FsNode instances point to the same underlying // file object. If two FsNodes report different hashCodes, then they are not the same object. // If they report the same hashCode, then they may or may not be teh same object. // // The Unix filesystem implementation builds the hashCode based on st_dev and st_ino of // `struct stat`. However, note that some filesystems -- especially FUSE-based -- may not fill // in st_ino. // // The Windows filesystem implementation builds the hashCode based on dwVolumeSerialNumber and // dwFileIndex{Low,High} of the BY_HANDLE_FILE_INFORMATION structure. However, these are again // not guaranteed to be unique on all filesystems. In particular the documentation says that // ReFS uses 128-bit identifiers which can't be represented here, and again virtual filesystems // may often not report real identifiers. // // Of course, the process of hashing values into a single hash code can also cause collisions // even if the filesystem reports reliable information. // // Additionally note that this value is not reliable when returned by `lstat()`. You should // actually open the object, then call `stat()` on the opened object. // Not currently included: // - Access control info: Differs wildly across platforms, and KJ prefers capabilities anyway. // - Other timestamps: Differs across platforms. // - Device number: If you care, you're probably doing platform-specific stuff anyway. Metadata() = default; Metadata(Type type, uint64_t size, uint64_t spaceUsed, Date lastModified, uint linkCount, uint64_t hashCode) : type(type), size(size), spaceUsed(spaceUsed), lastModified(lastModified), linkCount(linkCount), hashCode(hashCode) {} // TODO(cleanup): This constructor is redundant in C++14, but needed in C++11. }; virtual Metadata stat() const = 0; virtual void sync() const = 0; virtual void datasync() const = 0; // Maps to fsync() and fdatasync() system calls. // // Also, when creating or overwriting a file, the first call to sync() atomically links the file // into the filesystem (*after* syncing the data), so than incomplete data is never visible to // other processes. (In practice this works by writing into a temporary file and then rename()ing // it.) protected: virtual Own cloneFsNode() const = 0; // Implements clone(). Required to return an object with exactly the same type as this one. // Hence, every subclass must implement this. }; class ReadableFile: public FsNode { public: Own clone() const; String readAllText() const; // Read all text in the file and return as a big string. Array readAllBytes() const; // Read all bytes in the file and return as a big byte array. // // This differs from mmap() in that the read is performed all at once. Future changes to the file // do not affect the returned copy. Consider using mmap() instead, particularly for large files. virtual size_t read(uint64_t offset, ArrayPtr buffer) const = 0; // Fills `buffer` with data starting at `offset`. Returns the number of bytes actually read -- // the only time this is less than `buffer.size()` is when EOF occurs mid-buffer. virtual Array mmap(uint64_t offset, uint64_t size) const = 0; // Maps the file to memory read-only. The returned array always has exactly the requested size. // Depending on the capabilities of the OS and filesystem, the mapping may or may not reflect // changes that happen to the file after mmap() returns. // // Multiple calls to mmap() on the same file may or may not return the same mapping (it is // immutable, so there's no possibility of interference). // // If the file cannot be mmap()ed, an implementation may choose to allocate a buffer on the heap, // read into it, and return that. This should only happen if a real mmap() is impossible. // // The returned array is always exactly the size requested. However, accessing bytes beyond the // current end of the file may raise SIGBUS, or may simply return zero. virtual Array mmapPrivate(uint64_t offset, uint64_t size) const = 0; // Like mmap() but returns a view that the caller can modify. Modifications will not be written // to the underlying file. Every call to this method returns a unique mapping. Changes made to // the underlying file by other clients may or may not be reflected in the mapping -- in fact, // some changes may be reflected while others aren't, even within the same mapping. // // In practice this is often implemented using copy-on-write pages. When you first write to a // page, a copy is made. Hence, changes to the underlying file within that page stop being // reflected in the mapping. }; class AppendableFile: public FsNode, public OutputStream { public: Own clone() const; // All methods are inherited. }; class WritableFileMapping { public: virtual ArrayPtr get() const = 0; // Gets the mapped bytes. The returned array can be modified, and those changes may be written to // the underlying file, but there is no guarantee that they are written unless you subsequently // call changed(). virtual void changed(ArrayPtr slice) const = 0; // Notifies the implementation that the given bytes have changed. For some implementations this // may be a no-op while for others it may be necessary in order for the changes to be written // back at all. // // `slice` must be a slice of `bytes()`. virtual void sync(ArrayPtr slice) const = 0; // Implies `changed()`, and then waits until the range has actually been written to disk before // returning. // // `slice` must be a slice of `bytes()`. // // On Windows, this calls FlushViewOfFile(). The documentation for this function implies that in // some circumstances, to fully sync to physical disk, you may need to call FlushFileBuffers() on // the file HANDLE as well. The documentation is not very clear on when and why this is needed. // If you believe your program needs this, you can accomplish it by calling `.sync()` on the File // object after calling `.sync()` on the WritableFileMapping. }; class File: public ReadableFile { public: Own clone() const; void writeAll(ArrayPtr bytes) const; void writeAll(StringPtr text) const; // Completely replace the file with the given bytes or text. virtual void write(uint64_t offset, ArrayPtr data) const = 0; // Write the given data starting at the given offset in the file. virtual void zero(uint64_t offset, uint64_t size) const = 0; // Write zeros to the file, starting at `offset` and continuing for `size` bytes. If the platform // supports it, this will "punch a hole" in the file, such that blocks that are entirely zeros // do not take space on disk. virtual void truncate(uint64_t size) const = 0; // Set the file end pointer to `size`. If `size` is less than the current size, data past the end // is truncated. If `size` is larger than the current size, zeros are added to the end of the // file. If the platform supports it, blocks containing all-zeros will not be stored to disk. virtual Own mmapWritable(uint64_t offset, uint64_t size) const = 0; // Like ReadableFile::mmap() but returns a mapping for which any changes will be immediately // visible in other mappings of the file on the same system and will eventually be written back // to the file. virtual size_t copy(uint64_t offset, const ReadableFile& from, uint64_t fromOffset, uint64_t size) const; // Copies bytes from one file to another. // // Copies `size` bytes or to EOF, whichever comes first. Returns the number of bytes actually // copied. Hint: Pass kj::maxValue for `size` to always copy to EOF. // // The copy is not atomic. Concurrent writes may lead to garbage results. // // The default implementation performs a series of reads and writes. Subclasses can often provide // superior implementations that offload the work to the OS or even implement copy-on-write. }; class ReadableDirectory: public FsNode { // Read-only subset of `Directory`. public: Own clone() const; virtual Array listNames() const = 0; // List the contents of this directory. Does NOT include "." nor "..". struct Entry { FsNode::Type type; String name; inline bool operator< (const Entry& other) const { return name < other.name; } inline bool operator> (const Entry& other) const { return name > other.name; } inline bool operator<=(const Entry& other) const { return name <= other.name; } inline bool operator>=(const Entry& other) const { return name >= other.name; } // Convenience comparison operators to sort entries by name. }; virtual Array listEntries() const = 0; // List the contents of the directory including the type of each file. On some platforms and // filesystems, this is just as fast as listNames(), but on others it may require stat()ing each // file. virtual bool exists(PathPtr path) const = 0; // Does the specified path exist? // // If the path is a symlink, the symlink is followed and the return value indicates if the target // exists. If you want to know if the symlink exists, use lstat(). (This implies that listNames() // may return names for which exists() reports false.) FsNode::Metadata lstat(PathPtr path) const; virtual Maybe tryLstat(PathPtr path) const = 0; // Gets metadata about the path. If the path is a symlink, it is not followed -- the metadata // describes the symlink itself. `tryLstat()` returns null if the path doesn't exist. Own openFile(PathPtr path) const; virtual Maybe> tryOpenFile(PathPtr path) const = 0; // Open a file for reading. // // `tryOpenFile()` returns null if the path doesn't exist. Other errors still throw exceptions. Own openSubdir(PathPtr path) const; virtual Maybe> tryOpenSubdir(PathPtr path) const = 0; // Opens a subdirectory. // // `tryOpenSubdir()` returns null if the path doesn't exist. Other errors still throw exceptions. String readlink(PathPtr path) const; virtual Maybe tryReadlink(PathPtr path) const = 0; // If `path` is a symlink, reads and returns the link contents. // // Note that tryReadlink() differs subtly from tryOpen*(). For example, tryOpenFile() throws if // the path is not a file (e.g. if it's a directory); it only returns null if the path doesn't // exist at all. tryReadlink() returns null if either the path doesn't exist, or if it does exist // but isn't a symlink. This is because if it were to throw instead, then almost every real-world // use case of tryReadlink() would be forced to perform an lstat() first for the sole purpose of // checking if it is a link, wasting a syscall and a path traversal. // // See Directory::symlink() for warnings about symlinks. }; enum class WriteMode { // Mode for opening a file (or directory) for write. // // (To open a file or directory read-only, do not specify a mode.) // // WriteMode is a bitfield. Hence, it overloads the bitwise logic operators. To check if a // particular bit is set in a bitfield, use kj::has(), like: // // if (kj::has(mode, WriteMode::MUST_EXIST)) { // requireExists(path); // } // // (`if (mode & WriteMode::MUST_EXIST)` doesn't work because WriteMode is an enum class, which // cannot be converted to bool. Alas, C++ does not allow you to define a conversion operator // on an enum type, so we can't define a conversion to bool.) // ----------------------------------------- // Core flags // // At least one of CREATE or MODIFY must be specified. Optionally, the two flags can be combined // with a bitwise-OR. CREATE = 1, // Create a new empty file. // // When not combined with MODIFY, if the file already exists (including as a broken symlink), // tryOpenFile() returns null (and openFile() throws). // // When combined with MODIFY, if the path already exists, it will be opened as if CREATE hadn't // been specified at all. If the path refers to a broken symlink, the file at the target of the // link will be created (if its parent directory exists). MODIFY = 2, // Modify an existing file. // // When not combined with CREATE, if the file doesn't exist (including if it is a broken symlink), // tryOpenFile() returns null (and openFile() throws). // // When combined with CREATE, if the path doesn't exist, it will be created as if MODIFY hadn't // been specified at all. If the path refers to a broken symlink, the file at the target of the // link will be created (if its parent directory exists). // ----------------------------------------- // Additional flags // // Any number of these may be OR'd with the core flags. CREATE_PARENT = 4, // Indicates that if the target node's parent directory doesn't exist, it should be created // automatically, along with its parent, and so on. This creation is NOT atomic. // // This bit only makes sense with CREATE or REPLACE. EXECUTABLE = 8, // Mark this file executable, if this is a meaningful designation on the host platform. PRIVATE = 16, // Indicates that this file is sensitive and should have permissions masked so that it is only // accessible by the current user. // // When this is not used, the platform's default access control settings are used. On Unix, // that usually means the umask is applied. On Windows, it means permissions are inherited from // the parent. }; inline constexpr WriteMode operator|(WriteMode a, WriteMode b) { return static_cast(static_cast(a) | static_cast(b)); } inline constexpr WriteMode operator&(WriteMode a, WriteMode b) { return static_cast(static_cast(a) & static_cast(b)); } inline constexpr WriteMode operator+(WriteMode a, WriteMode b) { return static_cast(static_cast(a) | static_cast(b)); } inline constexpr WriteMode operator-(WriteMode a, WriteMode b) { return static_cast(static_cast(a) & ~static_cast(b)); } template > bool has(T haystack, T needle) { return (static_cast<__underlying_type(T)>(haystack) & static_cast<__underlying_type(T)>(needle)) == static_cast<__underlying_type(T)>(needle); } enum class TransferMode { // Specifies desired behavior for Directory::transfer(). MOVE, // The node is moved to the new location, i.e. the old location is deleted. If possible, this // move is performed without copying, otherwise it is performed as a copy followed by a delete. LINK, // The new location becomes a synonym for the old location (a "hard link"). Filesystems have // varying support for this -- typically, it is not supported on directories. COPY // The new location becomes a copy of the old. // // Some filesystems may implement this in terms of copy-on-write. // // If the filesystem supports sparse files, COPY takes sparseness into account -- it will punch // holes in the target file where holes exist in the source file. }; class Directory: public ReadableDirectory { // Refers to a specific directory on disk. // // A `Directory` object *only* provides access to children of the directory, not parents. That // is, you cannot open the file "..", nor jump to the root directory with "/". // // On OSs that support it, a `Directory` is backed by an open handle to the directory node. This // means: // - If the directory is renamed on-disk, the `Directory` object still points at it. // - Opening files in the directory only requires the OS to traverse the path from the directory // to the file; it doesn't have to re-traverse all the way from the filesystem root. // // On Windows, a `Directory` object holds a lock on the underlying directory such that it cannot // be renamed nor deleted while the object exists. This is necessary because Windows does not // fully support traversing paths relative to file handles (it does for some operations but not // all), so the KJ filesystem implementation is forced to remember the full path and needs to // ensure that the path is not invalidated. If, in the future, Windows fully supports // handle-relative paths, KJ may stop locking directories in this way, so do not rely on this // behavior. public: Own clone() const; template class Replacer { // Implements an atomic replacement of a file or directory, allowing changes to be made to // storage in a way that avoids losing data in a power outage and prevents other processes // from observing content in an inconsistent state. // // `T` may be `File` or `Directory`. For readability, the text below describes replacing a // file, but the logic is the same for directories. // // When you call `Directory::replaceFile()`, a temporary file is created, but the specified // path is not yet touched. You may call `get()` to obtain the temporary file object, through // which you may initialize its content, knowing that no other process can see it yet. The file // is atomically moved to its final path when you call `commit()`. If you destroy the Replacer // without calling commit(), the temporary file is deleted. // // Note that most operating systems sadly do not support creating a truly unnamed temporary file // and then linking it in later. Moreover, the file cannot necessarily be created in the system // temporary directory because it might not be on the same filesystem as the target. Therefore, // the replacement file may initially be created in the same directory as its eventual target. // The implementation of Directory will choose a name that is unique and "hidden" according to // the conventions of the filesystem. Additionally, the implementation of Directory will avoid // returning these temporary files from its list*() methods, in order to avoid observable // inconsistencies across platforms. public: explicit Replacer(WriteMode mode); virtual const T& get() = 0; // Gets the File or Directory representing the replacement data. Fill in this object before // calling commit(). void commit(); virtual bool tryCommit() = 0; // Commit the replacement. // // `tryCommit()` may return false based on the CREATE/MODIFY bits passed as the WriteMode when // the replacement was initiated. (If CREATE but not MODIFY was used, tryCommit() returns // false to indicate that the target file already existed. If MODIFY but not CREATE was used, // tryCommit() returns false to indicate that the file didn't exist.) // // `commit()` is atomic, meaning that there is no point in time at which other processes // observing the file will see it in an intermediate state -- they will either see the old // content or the complete new content. This includes in the case of a power outage or machine // failure: on recovery, the file will either be in the old state or the new state, but not in // some intermediate state. // // It's important to note that a power failure *after commit() returns* can still revert the // file to its previous state. That is, `commit()` does NOT guarantee that, upon return, the // new content is durable. In order to guarantee this, you must call `sync()` on the immediate // parent directory of the replaced file. // // Note that, sadly, not all filesystems / platforms are capable of supporting all of the // guarantees documented above. In such cases, commit() will make a best-effort attempt to do // what it claims. Some examples of possible problems include: // - Any guarantees about durability through a power outage probably require a journaling // filesystem. // - Many platforms do not support atomically replacing a non-empty directory. Linux does as // of kernel 3.15 (via the renameat2() syscall using RENAME_EXCHANGE). Where not supported, // the old directory will be moved away just before the replacement is moved into place. // - Many platforms do not support atomically requiring the existence or non-existence of a // file before replacing it. In these cases, commit() may have to perform the check as a // separate step, with a small window for a race condition. // - Many platforms do not support "unlinking" a non-empty directory, meaning that a replaced // directory will need to be deconstructed by deleting all contents. If another process has // the directory open when it is replaced, that process will observe the contents // disappearing after the replacement (actually, a swap) has taken place. This differs from // files, where a process that has opened a file before it is replaced will continue see the // file's old content unchanged after the replacement. // - On Windows, there are multiple ways to replace one file with another in a single system // call, but none are documented as being atomic. KJ always uses `MoveFileEx()` with // MOVEFILE_REPLACE_EXISTING. While the alternative `ReplaceFile()` is attractive for many // reasons, it has the critical problem that it cannot be used when the source file has open // file handles, which is generally the case when using Replacer. protected: const WriteMode mode; }; using ReadableDirectory::openFile; using ReadableDirectory::openSubdir; using ReadableDirectory::tryOpenFile; using ReadableDirectory::tryOpenSubdir; Own openFile(PathPtr path, WriteMode mode) const; virtual Maybe> tryOpenFile(PathPtr path, WriteMode mode) const = 0; // Open a file for writing. // // `tryOpenFile()` returns null if the path is required to exist but doesn't (MODIFY or REPLACE) // or if the path is required not to exist but does (CREATE or RACE). virtual Own> replaceFile(PathPtr path, WriteMode mode) const = 0; // Construct a file which, when ready, will be atomically moved to `path`, replacing whatever // is there already. See `Replacer` for detalis. // // The `CREATE` and `MODIFY` bits of `mode` are not enforced until commit time, hence // `replaceFile()` has no "try" variant. virtual Own createTemporary() const = 0; // Create a temporary file backed by this directory's filesystem, but which isn't linked into // the directory tree. The file is deleted from disk when all references to it have been dropped. Own appendFile(PathPtr path, WriteMode mode) const; virtual Maybe> tryAppendFile(PathPtr path, WriteMode mode) const = 0; // Opens the file for appending only. Useful for log files. // // If the underlying filesystem supports it, writes to the file will always be appended even if // other writers are writing to the same file at the same time -- however, some implementations // may instead assume that no other process is changing the file size between writes. Own openSubdir(PathPtr path, WriteMode mode) const; virtual Maybe> tryOpenSubdir(PathPtr path, WriteMode mode) const = 0; // Opens a subdirectory for writing. virtual Own> replaceSubdir(PathPtr path, WriteMode mode) const = 0; // Construct a directory which, when ready, will be atomically moved to `path`, replacing // whatever is there already. See `Replacer` for detalis. // // The `CREATE` and `MODIFY` bits of `mode` are not enforced until commit time, hence // `replaceSubdir()` has no "try" variant. void symlink(PathPtr linkpath, StringPtr content, WriteMode mode) const; virtual bool trySymlink(PathPtr linkpath, StringPtr content, WriteMode mode) const = 0; // Create a symlink. `content` is the raw text which will be written into the symlink node. // How this text is interpreted is entirely dependent on the filesystem. Note in particular that: // - Windows will require a path that uses backslashes as the separator. // - InMemoryDirectory does not support symlinks containing "..". // // Unfortunately under many implementations symlink() can be used to break out of the directory // by writing an absolute path or utilizing "..". Do not call this method with a value for // `target` that you don't trust. // // `mode` must be CREATE or REPLACE, not MODIFY. CREATE_PARENT is honored but EXECUTABLE and // PRIVATE have no effect. `trySymlink()` returns false in CREATE mode when the target already // exists. void transfer(PathPtr toPath, WriteMode toMode, PathPtr fromPath, TransferMode mode) const; void transfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const; virtual bool tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const; virtual Maybe tryTransferTo(const Directory& toDirectory, PathPtr toPath, WriteMode toMode, PathPtr fromPath, TransferMode mode) const; // Move, link, or copy a file/directory tree from one location to another. // // Filesystems vary in what kinds of transfers are allowed, especially for TransferMode::LINK, // and whether TransferMode::MOVE is implemented as an actual move vs. copy+delete. // // tryTransfer() returns false if the source location didn't exist, or when `toMode` is CREATE // and the target already exists. The default implementation implements only TransferMode::COPY. // // tryTransferTo() exists to implement double-dispatch. It should be called as a fallback by // implementations of tryTransfer() in cases where the target directory would otherwise fail or // perform a pessimal transfer. The default implementation returns nullptr, which the caller // should interpret as: "I don't have any special optimizations; do the obvious thing." // // `toMode` controls how the target path is created. CREATE_PARENT is honored but EXECUTABLE and // PRIVATE have no effect. void remove(PathPtr path) const; virtual bool tryRemove(PathPtr path) const = 0; // Deletes/unlinks the given path. If the path names a directory, it is recursively deleted. // // tryRemove() returns false if the path doesn't exist; remove() throws in this case. // TODO(someday): // - Support sockets? There's no openat()-like interface for sockets, so it's hard to support // them currently. Also you'd probably want to use them with the async library. // - Support named pipes? Unclear if there's a use case that isn't better-served by sockets. // Then again, they can be openat()ed. // - Support watching for changes (inotify). Probably also requires the async library. Also // lacks openat()-like semantics. // - xattrs -- linux-specific // - chown/chmod/etc. -- unix-specific, ACLs, eww // - set timestamps -- only needed by archiving programs/ // - advisory locks // - sendfile? // - fadvise and such private: static void commitFailed(WriteMode mode); }; class Filesystem { public: virtual const Directory& getRoot() const = 0; // Get the filesystem's root directory, as of the time the Filesystem object was created. virtual const Directory& getCurrent() const = 0; // Get the filesystem's current directory, as of the time the Filesystem object was created. virtual PathPtr getCurrentPath() const = 0; // Get the path from the root to the current directory, as of the time the Filesystem object was // created. Note that because a `Directory` does not provide access to its parent, if you want to // follow `..` from the current directory, you must use `getCurrentPath().eval("..")` or // `getCurrentPath().parent()`. // // This function attempts to determine the path as it appeared in the user's shell before this // program was started. That means, if the user had `cd`ed into a symlink, the path through that // symlink is returned, *not* the canonical path. // // Because of this, there is an important difference between how the operating system interprets // "../foo" and what you get when you write `getCurrentPath().eval("../foo")`: The former // will interpret ".." relative to the directory's canonical path, whereas the latter will // interpret it relative to the path shown in the user's shell. In practice, the latter is // almost always what the user wants! But the former behavior is what almost all commands do // in practice, and it leads to confusion. KJ commands should implement the behavior the user // expects. }; // ======================================================================================= Own newInMemoryFile(const Clock& clock); Own newInMemoryDirectory(const Clock& clock); // Construct file and directory objects which reside in-memory. // // InMemoryFile has the following special properties: // - The backing store is not sparse and never gets smaller even if you truncate the file. // - While a non-private memory mapping exists, the backing store cannot get larger. Any operation // which would expand it will throw. // // InMemoryDirectory has the following special properties: // - Symlinks are processed using Path::parse(). This implies tha a symlink cannot point to a // parent directory -- InMemoryDirectory does not know its parent. // - link() can link directory nodes in addition to files. // - link() and rename() accept any kind of Directory as `fromDirectory` -- it doesn't need to be // another InMemoryDirectory. However, for rename(), the from path must be a directory. Own newFileAppender(Own inner); // Creates an AppendableFile by wrapping a File. Note that this implementation assumes it is the // only writer. A correct implementation should always append to the file even if other writes // are happening simultaneously, as is achieved with the O_APPEND flag to open(2), but that // behavior is not possible to emulate on top of `File`. #if _WIN32 typedef AutoCloseHandle OsFileHandle; #else typedef AutoCloseFd OsFileHandle; #endif Own newDiskReadableFile(OsFileHandle fd); Own newDiskAppendableFile(OsFileHandle fd); Own newDiskFile(OsFileHandle fd); Own newDiskReadableDirectory(OsFileHandle fd); Own newDiskDirectory(OsFileHandle fd); // Wrap a file descriptor (or Windows HANDLE) as various filesystem types. Own newDiskFilesystem(); // Get at implementation of `Filesystem` representing the real filesystem. // // DO NOT CALL THIS except at the top level of your program, e.g. in main(). Anywhere else, you // should instead have your caller pass in a Filesystem object, or a specific Directory object, // or whatever it is that your code needs. This ensures that your code supports dependency // injection, which makes it more reusable and testable. // // newDiskFilesystem() reads the current working directory at the time it is called. The returned // object is not affected by subsequent calls to chdir(). // ======================================================================================= // inline implementation details inline Path::Path(decltype(nullptr)): parts(nullptr) {} inline Path::Path(std::initializer_list parts) : Path(arrayPtr(parts.begin(), parts.end())) {} inline Path::Path(Array parts, decltype(ALREADY_CHECKED)) : parts(kj::mv(parts)) {} inline Path Path::clone() const { return PathPtr(*this).clone(); } inline Path Path::append(Path&& suffix) const& { return PathPtr(*this).append(kj::mv(suffix)); } inline Path Path::append(PathPtr suffix) const& { return PathPtr(*this).append(suffix); } inline Path Path::append(StringPtr suffix) const& { return append(Path(suffix)); } inline Path Path::append(StringPtr suffix) && { return kj::mv(*this).append(Path(suffix)); } inline Path Path::append(String&& suffix) const& { return append(Path(kj::mv(suffix))); } inline Path Path::append(String&& suffix) && { return kj::mv(*this).append(Path(kj::mv(suffix))); } inline Path Path::eval(StringPtr pathText) const& { return PathPtr(*this).eval(pathText); } inline PathPtr Path::basename() const& { return PathPtr(*this).basename(); } inline PathPtr Path::parent() const& { return PathPtr(*this).parent(); } inline const String& Path::operator[](size_t i) const& { return parts[i]; } inline String Path::operator[](size_t i) && { return kj::mv(parts[i]); } inline size_t Path::size() const { return parts.size(); } inline const String* Path::begin() const { return parts.begin(); } inline const String* Path::end() const { return parts.end(); } inline PathPtr Path::slice(size_t start, size_t end) const& { return PathPtr(*this).slice(start, end); } inline bool Path::operator==(PathPtr other) const { return PathPtr(*this) == other; } inline bool Path::operator!=(PathPtr other) const { return PathPtr(*this) != other; } inline bool Path::operator< (PathPtr other) const { return PathPtr(*this) < other; } inline bool Path::operator> (PathPtr other) const { return PathPtr(*this) > other; } inline bool Path::operator<=(PathPtr other) const { return PathPtr(*this) <= other; } inline bool Path::operator>=(PathPtr other) const { return PathPtr(*this) >= other; } inline uint Path::hashCode() const { return kj::hashCode(parts); } inline bool Path::startsWith(PathPtr prefix) const { return PathPtr(*this).startsWith(prefix); } inline bool Path::endsWith (PathPtr suffix) const { return PathPtr(*this).endsWith (suffix); } inline String Path::toString(bool absolute) const { return PathPtr(*this).toString(absolute); } inline Path Path::evalWin32(StringPtr pathText) const& { return PathPtr(*this).evalWin32(pathText); } inline String Path::toWin32String(bool absolute) const { return PathPtr(*this).toWin32String(absolute); } inline Array Path::forWin32Api(bool absolute) const { return PathPtr(*this).forWin32Api(absolute); } inline PathPtr::PathPtr(decltype(nullptr)): parts(nullptr) {} inline PathPtr::PathPtr(const Path& path): parts(path.parts) {} inline PathPtr::PathPtr(ArrayPtr parts): parts(parts) {} inline Path PathPtr::append(StringPtr suffix) const { return append(Path(suffix)); } inline Path PathPtr::append(String&& suffix) const { return append(Path(kj::mv(suffix))); } inline const String& PathPtr::operator[](size_t i) const { return parts[i]; } inline size_t PathPtr::size() const { return parts.size(); } inline const String* PathPtr::begin() const { return parts.begin(); } inline const String* PathPtr::end() const { return parts.end(); } inline PathPtr PathPtr::slice(size_t start, size_t end) const { return PathPtr(parts.slice(start, end)); } inline bool PathPtr::operator!=(PathPtr other) const { return !(*this == other); } inline bool PathPtr::operator> (PathPtr other) const { return other < *this; } inline bool PathPtr::operator<=(PathPtr other) const { return !(other < *this); } inline bool PathPtr::operator>=(PathPtr other) const { return !(*this < other); } inline uint PathPtr::hashCode() const { return kj::hashCode(parts); } inline String PathPtr::toWin32String(bool absolute) const { return toWin32StringImpl(absolute, false); } #if _WIN32 inline Path Path::evalNative(StringPtr pathText) const& { return evalWin32(pathText); } inline Path Path::evalNative(StringPtr pathText) && { return kj::mv(*this).evalWin32(pathText); } inline String Path::toNativeString(bool absolute) const { return toWin32String(absolute); } inline Path PathPtr::evalNative(StringPtr pathText) const { return evalWin32(pathText); } inline String PathPtr::toNativeString(bool absolute) const { return toWin32String(absolute); } #else inline Path Path::evalNative(StringPtr pathText) const& { return eval(pathText); } inline Path Path::evalNative(StringPtr pathText) && { return kj::mv(*this).eval(pathText); } inline String Path::toNativeString(bool absolute) const { return toString(absolute); } inline Path PathPtr::evalNative(StringPtr pathText) const { return eval(pathText); } inline String PathPtr::toNativeString(bool absolute) const { return toString(absolute); } #endif // _WIN32, else inline Own FsNode::clone() const { return cloneFsNode(); } inline Own ReadableFile::clone() const { return cloneFsNode().downcast(); } inline Own AppendableFile::clone() const { return cloneFsNode().downcast(); } inline Own File::clone() const { return cloneFsNode().downcast(); } inline Own ReadableDirectory::clone() const { return cloneFsNode().downcast(); } inline Own Directory::clone() const { return cloneFsNode().downcast(); } inline void Directory::transfer( PathPtr toPath, WriteMode toMode, PathPtr fromPath, TransferMode mode) const { return transfer(toPath, toMode, *this, fromPath, mode); } template inline Directory::Replacer::Replacer(WriteMode mode): mode(mode) {} template void Directory::Replacer::commit() { if (!tryCommit()) commitFailed(mode); } } // namespace kj capnproto-c++-0.8.0/src/kj/async-prelude.h0000644000175000017500000001562213650101756021101 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains a bunch of internal declarations that must appear before async.h can start. // We don't define these directly in async.h because it makes the file hard to read. #pragma once #include "exception.h" #include "tuple.h" KJ_BEGIN_HEADER namespace kj { class EventLoop; template class Promise; class WaitScope; class TaskSet; template Promise> joinPromises(Array>&& promises); Promise joinPromises(Array>&& promises); namespace _ { // private template Promise chainPromiseType(T*); template Promise chainPromiseType(Promise*); template using ChainPromises = decltype(chainPromiseType((T*)nullptr)); // Constructs a promise for T, reducing double-promises. That is, if T is Promise, resolves to // Promise, otherwise resolves to Promise. template Promise reducePromiseType(T*, ...); template Promise reducePromiseType(Promise*, ...); template >()))> Reduced reducePromiseType(T*, bool); template using ReducePromises = decltype(reducePromiseType((T*)nullptr, false)); // Like ChainPromises, but also takes into account whether T has a method `reducePromise` that // reduces Promise to something else. In particular this allows Promise> // to reduce to capnp::RemotePromise. template struct UnwrapPromise_; template struct UnwrapPromise_> { typedef T Type; }; template using UnwrapPromise = typename UnwrapPromise_::Type; class PropagateException { // A functor which accepts a kj::Exception as a parameter and returns a broken promise of // arbitrary type which simply propagates the exception. public: class Bottom { public: Bottom(Exception&& exception): exception(kj::mv(exception)) {} Exception asException() { return kj::mv(exception); } private: Exception exception; }; Bottom operator()(Exception&& e) { return Bottom(kj::mv(e)); } Bottom operator()(const Exception& e) { return Bottom(kj::cp(e)); } }; template struct ReturnType_ { typedef decltype(instance()(instance())) Type; }; template struct ReturnType_ { typedef decltype(instance()()) Type; }; template using ReturnType = typename ReturnType_::Type; // The return type of functor Func given a parameter of type T, with the special exception that if // T is void, this is the return type of Func called with no arguments. template struct SplitTuplePromise_ { typedef Promise Type; }; template struct SplitTuplePromise_> { typedef kj::Tuple...> Type; }; template using SplitTuplePromise = typename SplitTuplePromise_::Type; // T -> Promise // Tuple -> Tuple> struct Void {}; // Application code should NOT refer to this! See `kj::READY_NOW` instead. template struct FixVoid_ { typedef T Type; }; template <> struct FixVoid_ { typedef Void Type; }; template using FixVoid = typename FixVoid_::Type; // FixVoid is just T unless T is void in which case it is _::Void (an empty struct). template struct UnfixVoid_ { typedef T Type; }; template <> struct UnfixVoid_ { typedef void Type; }; template using UnfixVoid = typename UnfixVoid_::Type; // UnfixVoid is the opposite of FixVoid. template struct MaybeVoidCaller { // Calls the function converting a Void input to an empty parameter list and a void return // value to a Void output. template static inline Out apply(Func& func, In&& in) { return func(kj::mv(in)); } }; template struct MaybeVoidCaller { template static inline Out apply(Func& func, In& in) { return func(in); } }; template struct MaybeVoidCaller { template static inline Out apply(Func& func, Void&& in) { return func(); } }; template struct MaybeVoidCaller { template static inline Void apply(Func& func, In&& in) { func(kj::mv(in)); return Void(); } }; template struct MaybeVoidCaller { template static inline Void apply(Func& func, In& in) { func(in); return Void(); } }; template <> struct MaybeVoidCaller { template static inline Void apply(Func& func, Void&& in) { func(); return Void(); } }; template inline T&& returnMaybeVoid(T&& t) { return kj::fwd(t); } inline void returnMaybeVoid(Void&& v) {} class ExceptionOrValue; class PromiseNode; class ChainPromiseNode; template class ForkHub; class FiberBase; class Event; class XThreadEvent; class PromiseBase { public: kj::String trace(); // Dump debug info about this promise. private: Own node; PromiseBase() = default; PromiseBase(Own&& node): node(kj::mv(node)) {} template friend class kj::Promise; friend class PromiseNode; }; void detach(kj::Promise&& promise); void waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope); bool pollImpl(_::PromiseNode& node, WaitScope& waitScope); Promise yield(); Promise yieldHarder(); Own neverDone(); class NeverDone { public: template operator Promise() const; KJ_NORETURN(void wait(WaitScope& waitScope) const); }; } // namespace _ (private) } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/async.h0000644000175000017500000013552013650101756017443 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "async-prelude.h" #include "exception.h" #include "refcount.h" KJ_BEGIN_HEADER namespace kj { class EventLoop; class WaitScope; template class Promise; template class ForkedPromise; template class PromiseFulfiller; template struct PromiseFulfillerPair; template using PromiseForResult = _::ReducePromises<_::ReturnType>; // Evaluates to the type of Promise for the result of calling functor type Func with parameter type // T. If T is void, then the promise is for the result of calling Func with no arguments. If // Func itself returns a promise, the promises are joined, so you never get Promise>. // ======================================================================================= // Promises template class Promise: protected _::PromiseBase { // The basic primitive of asynchronous computation in KJ. Similar to "futures", but designed // specifically for event loop concurrency. Similar to E promises and JavaScript Promises/A. // // A Promise represents a promise to produce a value of type T some time in the future. Once // that value has been produced, the promise is "fulfilled". Alternatively, a promise can be // "broken", with an Exception describing what went wrong. You may implicitly convert a value of // type T to an already-fulfilled Promise. You may implicitly convert the constant // `kj::READY_NOW` to an already-fulfilled Promise. You may also implicitly convert a // `kj::Exception` to an already-broken promise of any type. // // Promises are linear types -- they are moveable but not copyable. If a Promise is destroyed // or goes out of scope (without being moved elsewhere), any ongoing asynchronous operations // meant to fulfill the promise will be canceled if possible. All methods of `Promise` (unless // otherwise noted) actually consume the promise in the sense of move semantics. (Arguably they // should be rvalue-qualified, but at the time this interface was created compilers didn't widely // support that yet and anyway it would be pretty ugly typing kj::mv(promise).whatever().) If // you want to use one Promise in two different places, you must fork it with `fork()`. // // To use the result of a Promise, you must call `then()` and supply a callback function to // call with the result. `then()` returns another promise, for the result of the callback. // Any time that this would result in Promise>, the promises are collapsed into a // simple Promise that first waits for the outer promise, then the inner. Example: // // // Open a remote file, read the content, and then count the // // number of lines of text. // // Note that none of the calls here block. `file`, `content` // // and `lineCount` are all initialized immediately before any // // asynchronous operations occur. The lambda callbacks are // // called later. // Promise> file = openFtp("ftp://host/foo/bar"); // Promise content = file.then( // [](Own file) -> Promise { // return file.readAll(); // }); // Promise lineCount = content.then( // [](String text) -> int { // uint count = 0; // for (char c: text) count += (c == '\n'); // return count; // }); // // For `then()` to work, the current thread must have an active `EventLoop`. Each callback // is scheduled to execute in that loop. Since `then()` schedules callbacks only on the current // thread's event loop, you do not need to worry about two callbacks running at the same time. // You will need to set up at least one `EventLoop` at the top level of your program before you // can use promises. // // To adapt a non-Promise-based asynchronous API to promises, use `newAdaptedPromise()`. // // Systems using promises should consider supporting the concept of "pipelining". Pipelining // means allowing a caller to start issuing method calls against a promised object before the // promise has actually been fulfilled. This is particularly useful if the promise is for a // remote object living across a network, as this can avoid round trips when chaining a series // of calls. It is suggested that any class T which supports pipelining implement a subclass of // Promise which adds "eventual send" methods -- methods which, when called, say "please // invoke the corresponding method on the promised value once it is available". These methods // should in turn return promises for the eventual results of said invocations. Cap'n Proto, // for example, implements the type `RemotePromise` which supports pipelining RPC requests -- see // `capnp/capability.h`. // // KJ Promises are based on E promises: // http://wiki.erights.org/wiki/Walnut/Distributed_Computing#Promises // // KJ Promises are also inspired in part by the evolving standards for JavaScript/ECMAScript // promises, which are themselves influenced by E promises: // http://promisesaplus.com/ // https://github.com/domenic/promises-unwrapping public: Promise(_::FixVoid value); // Construct an already-fulfilled Promise from a value of type T. For non-void promises, the // parameter type is simply T. So, e.g., in a function that returns `Promise`, you can // say `return 123;` to return a promise that is already fulfilled to 123. // // For void promises, use `kj::READY_NOW` as the value, e.g. `return kj::READY_NOW`. Promise(kj::Exception&& e); // Construct an already-broken Promise. inline Promise(decltype(nullptr)) {} template PromiseForResult then(Func&& func, ErrorFunc&& errorHandler = _::PropagateException()) KJ_WARN_UNUSED_RESULT; // Register a continuation function to be executed when the promise completes. The continuation // (`func`) takes the promised value (an rvalue of type `T`) as its parameter. The continuation // may return a new value; `then()` itself returns a promise for the continuation's eventual // result. If the continuation itself returns a `Promise`, then `then()` shall also return // a `Promise` which first waits for the original promise, then executes the continuation, // then waits for the inner promise (i.e. it automatically "unwraps" the promise). // // In all cases, `then()` returns immediately. The continuation is executed later. The // continuation is always executed on the same EventLoop (and, therefore, the same thread) which // called `then()`, therefore no synchronization is necessary on state shared by the continuation // and the surrounding scope. If no EventLoop is running on the current thread, `then()` throws // an exception. // // You may also specify an error handler continuation as the second parameter. `errorHandler` // must be a functor taking a parameter of type `kj::Exception&&`. It must return the same // type as `func` returns (except when `func` returns `Promise`, in which case `errorHandler` // may return either `Promise` or just `U`). The default error handler simply propagates the // exception to the returned promise. // // Either `func` or `errorHandler` may, of course, throw an exception, in which case the promise // is broken. When compiled with -fno-exceptions, the framework will still detect when a // recoverable exception was thrown inside of a continuation and will consider the promise // broken even though a (presumably garbage) result was returned. // // If the returned promise is destroyed before the callback runs, the callback will be canceled // (it will never run). // // Note that `then()` -- like all other Promise methods -- consumes the promise on which it is // called, in the sense of move semantics. After returning, the original promise is no longer // valid, but `then()` returns a new promise. // // *Advanced implementation tips:* Most users will never need to worry about the below, but // it is good to be aware of. // // As an optimization, if the callback function `func` does _not_ return another promise, then // execution of `func` itself may be delayed until its result is known to be needed. The // expectation here is that `func` is just doing some transformation on the results, not // scheduling any other actions, therefore the system doesn't need to be proactive about // evaluating it. This way, a chain of trivial then() transformations can be executed all at // once without repeatedly re-scheduling through the event loop. Use the `eagerlyEvaluate()` // method to suppress this behavior. // // On the other hand, if `func` _does_ return another promise, then the system evaluates `func` // as soon as possible, because the promise it returns might be for a newly-scheduled // long-running asynchronous task. // // As another optimization, when a callback function registered with `then()` is actually // scheduled, it is scheduled to occur immediately, preempting other work in the event queue. // This allows a long chain of `then`s to execute all at once, improving cache locality by // clustering operations on the same data. However, this implies that starvation can occur // if a chain of `then()`s takes a very long time to execute without ever stopping to wait for // actual I/O. To solve this, use `kj::evalLater()` to yield control; this way, all other events // in the queue will get a chance to run before your callback is executed. Promise ignoreResult() KJ_WARN_UNUSED_RESULT { return then([](T&&) {}); } // Convenience method to convert the promise to a void promise by ignoring the return value. // // You must still wait on the returned promise if you want the task to execute. template Promise catch_(ErrorFunc&& errorHandler) KJ_WARN_UNUSED_RESULT; // Equivalent to `.then(identityFunc, errorHandler)`, where `identifyFunc` is a function that // just returns its input. T wait(WaitScope& waitScope); // Run the event loop until the promise is fulfilled, then return its result. If the promise // is rejected, throw an exception. // // wait() is primarily useful at the top level of a program -- typically, within the function // that allocated the EventLoop. For example, a program that performs one or two RPCs and then // exits would likely use wait() in its main() function to wait on each RPC. On the other hand, // server-side code generally cannot use wait(), because it has to be able to accept multiple // requests at once. // // If the promise is rejected, `wait()` throws an exception. If the program was compiled without // exceptions (-fno-exceptions), this will usually abort. In this case you really should first // use `then()` to set an appropriate handler for the exception case, so that the promise you // actually wait on never throws. // // `waitScope` is an object proving that the caller is in a scope where wait() is allowed. By // convention, any function which might call wait(), or which might call another function which // might call wait(), must take `WaitScope&` as one of its parameters. This is needed for two // reasons: // * `wait()` is not allowed during an event callback, because event callbacks are themselves // called during some other `wait()`, and such recursive `wait()`s would only be able to // complete in LIFO order, which might mean that the outer `wait()` ends up waiting longer // than it is supposed to. To prevent this, a `WaitScope` cannot be constructed or used during // an event callback. // * Since `wait()` runs the event loop, unrelated event callbacks may execute before `wait()` // returns. This means that anyone calling `wait()` must be reentrant -- state may change // around them in arbitrary ways. Therefore, callers really need to know if a function they // are calling might wait(), and the `WaitScope&` parameter makes this clear. // // Usually, there is only one `WaitScope` for each `EventLoop`, and it can only be used at the // top level of the thread owning the loop. Calling `wait()` with this `WaitScope` is what // actually causes the event loop to run at all. This top-level `WaitScope` cannot be used // recursively, so cannot be used within an event callback. // // However, it is possible to obtain a `WaitScope` in lower-level code by using fibers. Use // kj::startFiber() to start some code executing on an alternate call stack. That code will get // its own `WaitScope` allowing it to operate in a synchronous style. In this case, `wait()` // switches back to the main stack in order to run the event loop, returning to the fiber's stack // once the awaited promise resolves. bool poll(WaitScope& waitScope); // Returns true if a call to wait() would complete without blocking, false if it would block. // // If the promise is not yet resolved, poll() will pump the event loop and poll for I/O in an // attempt to resolve it. Only when there is nothing left to do will it return false. // // Generally, poll() is most useful in tests. Often, you may want to verify that a promise does // not resolve until some specific event occurs. To do so, poll() the promise before the event to // verify it isn't resolved, then trigger the event, then poll() again to verify that it resolves. // The first poll() verifies that the promise doesn't resolve early, which would otherwise be // hard to do deterministically. The second poll() allows you to check that the promise has // resolved and avoid a wait() that might deadlock in the case that it hasn't. // // poll() is not supported in fibers; it will throw an exception. ForkedPromise fork() KJ_WARN_UNUSED_RESULT; // Forks the promise, so that multiple different clients can independently wait on the result. // `T` must be copy-constructable for this to work. Or, in the special case where `T` is // `Own`, `U` must have a method `Own addRef()` which returns a new reference to the same // (or an equivalent) object (probably implemented via reference counting). _::SplitTuplePromise split(); // Split a promise for a tuple into a tuple of promises. // // E.g. if you have `Promise>`, `split()` returns // `kj::Tuple, Promise>`. Promise exclusiveJoin(Promise&& other) KJ_WARN_UNUSED_RESULT; // Return a new promise that resolves when either the original promise resolves or `other` // resolves (whichever comes first). The promise that didn't resolve first is canceled. // TODO(someday): inclusiveJoin(), or perhaps just join(), which waits for both completions // and produces a tuple? template Promise attach(Attachments&&... attachments) KJ_WARN_UNUSED_RESULT; // "Attaches" one or more movable objects (often, Owns) to the promise, such that they will // be destroyed when the promise resolves. This is useful when a promise's callback contains // pointers into some object and you want to make sure the object still exists when the callback // runs -- after calling then(), use attach() to add necessary objects to the result. template Promise eagerlyEvaluate(ErrorFunc&& errorHandler) KJ_WARN_UNUSED_RESULT; Promise eagerlyEvaluate(decltype(nullptr)) KJ_WARN_UNUSED_RESULT; // Force eager evaluation of this promise. Use this if you are going to hold on to the promise // for awhile without consuming the result, but you want to make sure that the system actually // processes it. // // `errorHandler` is a function that takes `kj::Exception&&`, like the second parameter to // `then()`, or the parameter to `catch_()`. We make you specify this because otherwise it's // easy to forget to handle errors in a promise that you never use. You may specify nullptr for // the error handler if you are sure that ignoring errors is fine, or if you know that you'll // eventually wait on the promise somewhere. template void detach(ErrorFunc&& errorHandler); // Allows the promise to continue running in the background until it completes or the // `EventLoop` is destroyed. Be careful when using this: since you can no longer cancel this // promise, you need to make sure that the promise owns all the objects it touches or make sure // those objects outlive the EventLoop. // // `errorHandler` is a function that takes `kj::Exception&&`, like the second parameter to // `then()`, except that it must return void. // // This function exists mainly to implement the Cap'n Proto requirement that RPC calls cannot be // canceled unless the callee explicitly permits it. kj::String trace(); // Returns a dump of debug info about this promise. Not for production use. Requires RTTI. // This method does NOT consume the promise as other methods do. private: Promise(bool, Own<_::PromiseNode>&& node): PromiseBase(kj::mv(node)) {} // Second parameter prevent ambiguity with immediate-value constructor. friend class _::PromiseNode; }; template class ForkedPromise { // The result of `Promise::fork()` and `EventLoop::fork()`. Allows branches to be created. // Like `Promise`, this is a pass-by-move type. public: inline ForkedPromise(decltype(nullptr)) {} Promise addBranch(); // Add a new branch to the fork. The branch is equivalent to the original promise. private: Own<_::ForkHub<_::FixVoid>> hub; inline ForkedPromise(bool, Own<_::ForkHub<_::FixVoid>>&& hub): hub(kj::mv(hub)) {} friend class Promise; friend class EventLoop; }; constexpr _::Void READY_NOW = _::Void(); // Use this when you need a Promise that is already fulfilled -- this value can be implicitly // cast to `Promise`. constexpr _::NeverDone NEVER_DONE = _::NeverDone(); // The opposite of `READY_NOW`, return this when the promise should never resolve. This can be // implicitly converted to any promise type. You may also call `NEVER_DONE.wait()` to wait // forever (useful for servers). template PromiseForResult evalLater(Func&& func) KJ_WARN_UNUSED_RESULT; // Schedule for the given zero-parameter function to be executed in the event loop at some // point in the near future. Returns a Promise for its result -- or, if `func()` itself returns // a promise, `evalLater()` returns a Promise for the result of resolving that promise. // // Example usage: // Promise x = evalLater([]() { return 123; }); // // The above is exactly equivalent to: // Promise x = Promise(READY_NOW).then([]() { return 123; }); // // If the returned promise is destroyed before the callback runs, the callback will be canceled // (never called). // // If you schedule several evaluations with `evalLater` during the same callback, they are // guaranteed to be executed in order. template PromiseForResult evalNow(Func&& func) KJ_WARN_UNUSED_RESULT; // Run `func()` and return a promise for its result. `func()` executes before `evalNow()` returns. // If `func()` throws an exception, the exception is caught and wrapped in a promise -- this is the // main reason why `evalNow()` is useful. template PromiseForResult evalLast(Func&& func) KJ_WARN_UNUSED_RESULT; // Like `evalLater()`, except that the function doesn't run until the event queue is otherwise // completely empty and the thread is about to suspend waiting for I/O. // // This is useful when you need to perform some disruptive action and you want to make sure that // you don't interrupt some other task between two .then() continuations. For example, say you want // to cancel a read() operation on a socket and know for sure that if any bytes were read, you saw // them. It could be that a read() has completed and bytes have been transferred to the target // buffer, but the .then() callback that handles the read result hasn't executed yet. If you // cancel the promise at this inopportune moment, the bytes in the buffer are lost. If you do // evalLast(), then you can be sure that any pending .then() callbacks had a chance to finish out // and if you didn't receive the read result yet, then you know nothing has been read, and you can // simply drop the promise. // // If evalLast() is called multiple times, functions are executed in LIFO order. If the first // callback enqueues new events, then latter callbacks will not execute until those events are // drained. template PromiseForResult startFiber(size_t stackSize, Func&& func) KJ_WARN_UNUSED_RESULT; // Executes `func()` in a fiber, returning a promise for the eventual reseult. `func()` will be // passed a `WaitScope&` as its parameter, allowing it to call `.wait()` on promises. Thus, `func()` // can be written in a synchronous, blocking style, instead of using `.then()`. This is often much // easier to write and read, and may even be significantly faster if it allows the use of stack // allocation rather than heap allocation. // // However, fibers have a major disadvantage: memory must be allocated for the fiber's call stack. // The entire stack must be allocated at once, making it necessary to choose a stack size upfront // that is big enough for whatever the fiber needs to do. Estimating this is often difficult. That // said, over-estimating is not too terrible since pages of the stack will actually be allocated // lazily when first accessed; actual memory usage will correspond to the "high watermark" of the // actual stack usage. That said, this lazy allocation forces page faults, which can be quite slow. // Worse, freeing a stack forces a TLB flush and shootdown -- all currently-executing threads will // have to be interrupted to flush their CPU cores' TLB caches. // // In short, when performance matters, you should try to avoid creating fibers very frequently. // // TODO(perf): We should add a mechanism for freelisting stacks. However, this improves CPU usage // at the expense of memory usage: stacks on the freelist will consume however many pages they // used at their high watermark, forever. template Promise> joinPromises(Array>&& promises); // Join an array of promises into a promise for an array. // ======================================================================================= // Hack for creating a lambda that holds an owned pointer. template class CaptureByMove { public: inline CaptureByMove(Func&& func, MovedParam&& param) : func(kj::mv(func)), param(kj::mv(param)) {} template inline auto operator()(Params&&... params) -> decltype(kj::instance()(kj::instance(), kj::fwd(params)...)) { return func(kj::mv(param), kj::fwd(params)...); } private: Func func; MovedParam param; }; template inline CaptureByMove> mvCapture(MovedParam&& param, Func&& func) { // Hack to create a "lambda" which captures a variable by moving it rather than copying or // referencing. C++14 generalized captures should make this obsolete, but for now in C++11 this // is commonly needed for Promise continuations that own their state. Example usage: // // Own ptr = makeFoo(); // Promise promise = callRpc(); // promise.then(mvCapture(ptr, [](Own&& ptr, int result) { // return ptr->finish(result); // })); return CaptureByMove>(kj::fwd(func), kj::mv(param)); } // ======================================================================================= // Advanced promise construction template class PromiseFulfiller { // A callback which can be used to fulfill a promise. Only the first call to fulfill() or // reject() matters; subsequent calls are ignored. public: virtual void fulfill(T&& value) = 0; // Fulfill the promise with the given value. virtual void reject(Exception&& exception) = 0; // Reject the promise with an error. virtual bool isWaiting() = 0; // Returns true if the promise is still unfulfilled and someone is potentially waiting for it. // Returns false if fulfill()/reject() has already been called *or* if the promise to be // fulfilled has been discarded and therefore the result will never be used anyway. template bool rejectIfThrows(Func&& func); // Call the function (with no arguments) and return true. If an exception is thrown, call // `fulfiller.reject()` and then return false. When compiled with exceptions disabled, // non-fatal exceptions are still detected and handled correctly. }; template <> class PromiseFulfiller { // Specialization of PromiseFulfiller for void promises. See PromiseFulfiller. public: virtual void fulfill(_::Void&& value = _::Void()) = 0; // Call with zero parameters. The parameter is a dummy that only exists so that subclasses don't // have to specialize for . virtual void reject(Exception&& exception) = 0; virtual bool isWaiting() = 0; template bool rejectIfThrows(Func&& func); }; template _::ReducePromises newAdaptedPromise(Params&&... adapterConstructorParams); // Creates a new promise which owns an instance of `Adapter` which encapsulates the operation // that will eventually fulfill the promise. This is primarily useful for adapting non-KJ // asynchronous APIs to use promises. // // An instance of `Adapter` will be allocated and owned by the returned `Promise`. A // `PromiseFulfiller&` will be passed as the first parameter to the adapter's constructor, // and `adapterConstructorParams` will be forwarded as the subsequent parameters. The adapter // is expected to perform some asynchronous operation and call the `PromiseFulfiller` once // it is finished. // // The adapter is destroyed when its owning Promise is destroyed. This may occur before the // Promise has been fulfilled. In this case, the adapter's destructor should cancel the // asynchronous operation. Once the adapter is destroyed, the fulfillment callback cannot be // called. // // An adapter implementation should be carefully written to ensure that it cannot accidentally // be left unfulfilled permanently because of an exception. Consider making liberal use of // `PromiseFulfiller::rejectIfThrows()`. template struct PromiseFulfillerPair { _::ReducePromises promise; Own> fulfiller; }; template PromiseFulfillerPair newPromiseAndFulfiller(); // Construct a Promise and a separate PromiseFulfiller which can be used to fulfill the promise. // If the PromiseFulfiller is destroyed before either of its methods are called, the Promise is // implicitly rejected. // // Although this function is easier to use than `newAdaptedPromise()`, it has the serious drawback // that there is no way to handle cancellation (i.e. detect when the Promise is discarded). // // You can arrange to fulfill a promise with another promise by using a promise type for T. E.g. // `newPromiseAndFulfiller>()` will produce a promise of type `Promise` but the // fulfiller will be of type `PromiseFulfiller>`. Thus you pass a `Promise` to the // `fulfill()` callback, and the promises are chained. // ======================================================================================= // Canceler class Canceler { // A Canceler can wrap some set of Promises and then forcefully cancel them on-demand, or // implicitly when the Canceler is destroyed. // // The cancellation is done in such a way that once cancel() (or the Canceler's destructor) // returns, it's guaranteed that the promise has already been canceled and destroyed. This // guarantee is important for enforcing ownership constraints. For example, imagine that Alice // calls a method on Bob that returns a Promise. That Promise encapsulates a task that uses Bob's // internal state. But, imagine that Alice does not own Bob, and indeed Bob might be destroyed // at random without Alice having canceled the promise. In this case, it is necessary for Bob to // ensure that the promise will be forcefully canceled. Bob can do this by constructing a // Canceler and using it to wrap promises before returning them to callers. When Bob is // destroyed, the Canceler is destroyed too, and all promises Bob wrapped with it throw errors. // // Note that another common strategy for cancelation is to use exclusiveJoin() to join a promise // with some "cancellation promise" which only resolves if the operation should be canceled. The // cancellation promise could itself be created by newPromiseAndFulfiller(), and thus // calling the PromiseFulfiller cancels the operation. There is a major problem with this // approach: upon invoking the fulfiller, an arbitrary amount of time may pass before the // exclusive-joined promise actually resolves and cancels its other fork. During that time, the // task might continue to execute. If it holds pointers to objects that have been destroyed, this // might cause segfaults. Thus, it is safer to use a Canceler. public: inline Canceler() {} ~Canceler() noexcept(false); KJ_DISALLOW_COPY(Canceler); template Promise wrap(Promise promise) { return newAdaptedPromise>(*this, kj::mv(promise)); } void cancel(StringPtr cancelReason); void cancel(const Exception& exception); // Cancel all previously-wrapped promises that have not already completed, causing them to throw // the given exception. If you provide just a description message instead of an exception, then // an exception object will be constructed from it -- but only if there are requests to cancel. void release(); // Releases previously-wrapped promises, so that they will not be canceled regardless of what // happens to this Canceler. bool isEmpty() const { return list == nullptr; } // Indicates if any previously-wrapped promises are still executing. (If this returns false, then // cancel() would be a no-op.) private: class AdapterBase { public: AdapterBase(Canceler& canceler); ~AdapterBase() noexcept(false); virtual void cancel(Exception&& e) = 0; void unlink(); private: Maybe&> prev; Maybe next; friend class Canceler; }; template class AdapterImpl: public AdapterBase { public: AdapterImpl(PromiseFulfiller& fulfiller, Canceler& canceler, Promise inner) : AdapterBase(canceler), fulfiller(fulfiller), inner(inner.then( [&fulfiller](T&& value) { fulfiller.fulfill(kj::mv(value)); }, [&fulfiller](Exception&& e) { fulfiller.reject(kj::mv(e)); }) .eagerlyEvaluate(nullptr)) {} void cancel(Exception&& e) override { fulfiller.reject(kj::mv(e)); inner = nullptr; } private: PromiseFulfiller& fulfiller; Promise inner; }; Maybe list; }; template <> class Canceler::AdapterImpl: public AdapterBase { public: AdapterImpl(kj::PromiseFulfiller& fulfiller, Canceler& canceler, kj::Promise inner); void cancel(kj::Exception&& e) override; // These must be defined in async.c++ to prevent translation units compiled by MSVC from trying to // link with symbols defined in async.c++ merely because they included async.h. private: kj::PromiseFulfiller& fulfiller; kj::Promise inner; }; // ======================================================================================= // TaskSet class TaskSet { // Holds a collection of Promises and ensures that each executes to completion. Memory // associated with each promise is automatically freed when the promise completes. Destroying // the TaskSet itself automatically cancels all unfinished promises. // // This is useful for "daemon" objects that perform background tasks which aren't intended to // fulfill any particular external promise, but which may need to be canceled (and thus can't // use `Promise::detach()`). The daemon object holds a TaskSet to collect these tasks it is // working on. This way, if the daemon itself is destroyed, the TaskSet is detroyed as well, // and everything the daemon is doing is canceled. public: class ErrorHandler { public: virtual void taskFailed(kj::Exception&& exception) = 0; }; TaskSet(ErrorHandler& errorHandler); // `errorHandler` will be executed any time a task throws an exception, and will execute within // the given EventLoop. ~TaskSet() noexcept(false); void add(Promise&& promise); kj::String trace(); // Return debug info about all promises currently in the TaskSet. bool isEmpty() { return tasks == nullptr; } // Check if any tasks are running. Promise onEmpty(); // Returns a promise that fulfills the next time the TaskSet is empty. Only one such promise can // exist at a time. private: class Task; TaskSet::ErrorHandler& errorHandler; Maybe> tasks; Maybe>> emptyFulfiller; }; // ======================================================================================= // Cross-thread execution. class Executor { // Executes code on another thread's event loop. // // Use `kj::getCurrentThreadExecutor()` to get an executor that schedules calls on the current // thread's event loop. You may then pass the reference to other threads to enable them to call // back to this one. public: Executor(EventLoop& loop, Badge); ~Executor() noexcept(false); template PromiseForResult executeAsync(Func&& func) const; // Call from any thread to request that the given function be executed on the executor's thread, // returning a promise for the result. // // The Promise returned by executeAsync() belongs to the requesting thread, not the executor // thread. Hence, for example, continuations added to this promise with .then() will execute in // the requesting thread. // // If func() itself returns a Promise, that Promise is *not* returned verbatim to the requesting // thread -- after all, Promise objects cannot be used cross-thread. Instead, the executor thread // awaits the promise. Once it resolves to a final result, that result is transferred to the // requesting thread, resolving the promise that executeAsync() returned earlier. // // `func` will be destroyed in the requesting thread, after the final result has been returned // from the executor thread. This means that it is safe for `func` to capture objects that cannot // safely be destroyed from another thread. It is also safe for `func` to be an lvalue reference, // so long as the functor remains live until the promise completes or is canceled, and the // function is thread-safe. // // Of course, the body of `func` must be careful that any access it makes on these objects is // safe cross-thread. For example, it must not attempt to access Promise-related objects // cross-thread; you cannot create a `PromiseFulfiller` in one thread and then `fulfill()` it // from another. Unfortunately, the usual convention of using const-correctness to enforce // thread-safety does not work here, because applications can often ensure that `func` has // exclusive access to captured objects, and thus can safely mutate them even in non-thread-safe // ways; the const qualifier is not sufficient to express this. // // The final return value of `func` is transferred between threads, and hence is constructed and // destroyed in separate threads. It is the app's responsibility to make sure this is OK. // Alternatively, the app can perhaps arrange to send the return value back to the original // thread for destruction, if needed. // // If the requesting thread destroys the returned Promise, the destructor will block waiting for // the executor thread to acknowledge cancellation. This ensures that `func` can be destroyed // before the Promise's destructor returns. // // Multiple calls to executeAsync() from the same requesting thread to the same target thread // will be delivered in the same order in which they were requested. (However, if func() returns // a promise, delivery of subsequent calls is not blocked on that promise. In other words, this // call provides E-Order in the same way as Cap'n Proto.) template _::UnwrapPromise> executeSync(Func&& func) const; // Schedules `func()` to execute on the executor thread, and then blocks the requesting thread // until `func()` completes. If `func()` returns a Promise, then the wait will continue until // that promise resolves, and the final result will be returned to the requesting thread. // // The requesting thread does not need to have an EventLoop. If it does have an EventLoop, that // loop will *not* execute while the thread is blocked. This method is particularly useful to // allow non-event-loop threads to perform I/O via a separate event-loop thread. // // As with `executeAsync()`, `func` is always destroyed on the requesting thread, after the // executor thread has signaled completion. The return value is transferred between threads. private: EventLoop& loop; struct Impl; Own impl; // To avoid including mutex.h... friend class EventLoop; friend class _::XThreadEvent; void send(_::XThreadEvent& event, bool sync) const; void wait(); bool poll(); }; const Executor& getCurrentThreadExecutor(); // Get the executor for the current thread's event loop. This reference can then be passed to other // threads. // ======================================================================================= // The EventLoop class class EventPort { // Interfaces between an `EventLoop` and events originating from outside of the loop's thread. // All such events come in through the `EventPort` implementation. // // An `EventPort` implementation may interface with low-level operating system APIs and/or other // threads. You can also write an `EventPort` which wraps some other (non-KJ) event loop // framework, allowing the two to coexist in a single thread. public: virtual bool wait() = 0; // Wait for an external event to arrive, sleeping if necessary. Once at least one event has // arrived, queue it to the event loop (e.g. by fulfilling a promise) and return. // // This is called during `Promise::wait()` whenever the event queue becomes empty, in order to // wait for new events to populate the queue. // // It is safe to return even if nothing has actually been queued, so long as calling `wait()` in // a loop will eventually sleep. (That is to say, false positives are fine.) // // Returns true if wake() has been called from another thread. (Precisely, returns true if // no previous call to wait `wait()` nor `poll()` has returned true since `wake()` was last // called.) virtual bool poll() = 0; // Check if any external events have arrived, but do not sleep. If any events have arrived, // add them to the event queue (e.g. by fulfilling promises) before returning. // // This may be called during `Promise::wait()` when the EventLoop has been executing for a while // without a break but is still non-empty. // // Returns true if wake() has been called from another thread. (Precisely, returns true if // no previous call to wait `wait()` nor `poll()` has returned true since `wake()` was last // called.) virtual void setRunnable(bool runnable); // Called to notify the `EventPort` when the `EventLoop` has work to do; specifically when it // transitions from empty -> runnable or runnable -> empty. This is typically useful when // integrating with an external event loop; if the loop is currently runnable then you should // arrange to call run() on it soon. The default implementation does nothing. virtual void wake() const; // Wake up the EventPort's thread from another thread. // // Unlike all other methods on this interface, `wake()` may be called from another thread, hence // it is `const`. // // Technically speaking, `wake()` causes the target thread to cease sleeping and not to sleep // again until `wait()` or `poll()` has returned true at least once. // // The default implementation throws an UNIMPLEMENTED exception. }; class EventLoop { // Represents a queue of events being executed in a loop. Most code won't interact with // EventLoop directly, but instead use `Promise`s to interact with it indirectly. See the // documentation for `Promise`. // // Each thread can have at most one current EventLoop. To make an `EventLoop` current for // the thread, create a `WaitScope`. Async APIs require that the thread has a current EventLoop, // or they will throw exceptions. APIs that use `Promise::wait()` additionally must explicitly // be passed a reference to the `WaitScope` to make the caller aware that they might block. // // Generally, you will want to construct an `EventLoop` at the top level of your program, e.g. // in the main() function, or in the start function of a thread. You can then use it to // construct some promises and wait on the result. Example: // // int main() { // // `loop` becomes the official EventLoop for the thread. // MyEventPort eventPort; // EventLoop loop(eventPort); // // // Now we can call an async function. // Promise textPromise = getHttp("http://example.com"); // // // And we can wait for the promise to complete. Note that you can only use `wait()` // // from the top level, not from inside a promise callback. // String text = textPromise.wait(); // print(text); // return 0; // } // // Most applications that do I/O will prefer to use `setupAsyncIo()` from `async-io.h` rather // than allocate an `EventLoop` directly. public: EventLoop(); // Construct an `EventLoop` which does not receive external events at all. explicit EventLoop(EventPort& port); // Construct an `EventLoop` which receives external events through the given `EventPort`. ~EventLoop() noexcept(false); void run(uint maxTurnCount = maxValue); // Run the event loop for `maxTurnCount` turns or until there is nothing left to be done, // whichever comes first. This never calls the `EventPort`'s `sleep()` or `poll()`. It will // call the `EventPort`'s `setRunnable(false)` if the queue becomes empty. bool isRunnable(); // Returns true if run() would currently do anything, or false if the queue is empty. const Executor& getExecutor(); // Returns an Executor that can be used to schedule events on this EventLoop from another thread. // // Use the global function kj::getCurrentThreadExecutor() to get the current thread's EventLoop's // Executor. // // Note that this is only needed for cross-thread scheduling. To schedule code to run later in // the current thread, use `kj::evalLater()`, which will be more efficient. private: kj::Maybe port; // If null, this thread doesn't receive I/O events from the OS. It can potentially receive // events from other threads via the Executor. bool running = false; // True while looping -- wait() is then not allowed. bool lastRunnableState = false; // What did we last pass to port.setRunnable()? _::Event* head = nullptr; _::Event** tail = &head; _::Event** depthFirstInsertPoint = &head; _::Event** breadthFirstInsertPoint = &head; kj::Maybe executor; // Allocated the first time getExecutor() is requested, making cross-thread request possible. Own daemons; #if _WIN32 || __CYGWIN__ void* mainFiber = nullptr; #endif bool turn(); void setRunnable(bool runnable); void enterScope(); void leaveScope(); void wait(); void poll(); friend void _::detach(kj::Promise&& promise); friend void _::waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope); friend bool _::pollImpl(_::PromiseNode& node, WaitScope& waitScope); friend class _::Event; friend class WaitScope; friend class Executor; friend class _::XThreadEvent; friend class _::FiberBase; }; class WaitScope { // Represents a scope in which asynchronous programming can occur. A `WaitScope` should usually // be allocated on the stack and serves two purposes: // * While the `WaitScope` exists, its `EventLoop` is registered as the current loop for the // thread. Most operations dealing with `Promise` (including all of its methods) do not work // unless the thread has a current `EventLoop`. // * `WaitScope` may be passed to `Promise::wait()` to synchronously wait for a particular // promise to complete. See `Promise::wait()` for an extended discussion. public: inline explicit WaitScope(EventLoop& loop): loop(loop) { loop.enterScope(); } inline ~WaitScope() { if (fiber == nullptr) loop.leaveScope(); } KJ_DISALLOW_COPY(WaitScope); void poll(); // Pumps the event queue and polls for I/O until there's nothing left to do (without blocking). // // Not supported in fibers. void setBusyPollInterval(uint count) { busyPollInterval = count; } // Set the maximum number of events to run in a row before calling poll() on the EventPort to // check for new I/O. // // This has no effect when used in a fiber. private: EventLoop& loop; uint busyPollInterval = kj::maxValue; kj::Maybe<_::FiberBase&> fiber; explicit WaitScope(EventLoop& loop, _::FiberBase& fiber) : loop(loop), fiber(fiber) {} friend class EventLoop; friend class _::FiberBase; friend void _::waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope); friend bool _::pollImpl(_::PromiseNode& node, WaitScope& waitScope); }; } // namespace kj #define KJ_ASYNC_H_INCLUDED #include "async-inl.h" KJ_END_HEADER capnproto-c++-0.8.0/src/kj/async-inl.h0000644000175000017500000013160113650101756020217 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file contains extended inline implementation details that are required along with async.h. // We move this all into a separate file to make async.h more readable. // // Non-inline declarations here are defined in async.c++. #pragma once #ifndef KJ_ASYNC_H_INCLUDED #error "Do not include this directly; include kj/async.h." #include "async.h" // help IDE parse this file #endif KJ_BEGIN_HEADER namespace kj { namespace _ { // private template class ExceptionOr; class ExceptionOrValue { public: ExceptionOrValue(bool, Exception&& exception): exception(kj::mv(exception)) {} KJ_DISALLOW_COPY(ExceptionOrValue); void addException(Exception&& exception) { if (this->exception == nullptr) { this->exception = kj::mv(exception); } } template ExceptionOr& as() { return *static_cast*>(this); } template const ExceptionOr& as() const { return *static_cast*>(this); } Maybe exception; protected: // Allow subclasses to have move constructor / assignment. ExceptionOrValue() = default; ExceptionOrValue(ExceptionOrValue&& other) = default; ExceptionOrValue& operator=(ExceptionOrValue&& other) = default; }; template class ExceptionOr: public ExceptionOrValue { public: ExceptionOr() = default; ExceptionOr(T&& value): value(kj::mv(value)) {} ExceptionOr(bool, Exception&& exception): ExceptionOrValue(false, kj::mv(exception)) {} ExceptionOr(ExceptionOr&&) = default; ExceptionOr& operator=(ExceptionOr&&) = default; Maybe value; }; template inline T convertToReturn(ExceptionOr&& result) { KJ_IF_MAYBE(value, result.value) { KJ_IF_MAYBE(exception, result.exception) { throwRecoverableException(kj::mv(*exception)); } return _::returnMaybeVoid(kj::mv(*value)); } else KJ_IF_MAYBE(exception, result.exception) { throwFatalException(kj::mv(*exception)); } else { // Result contained neither a value nor an exception? KJ_UNREACHABLE; } } inline void convertToReturn(ExceptionOr&& result) { // Override case to use throwRecoverableException(). if (result.value != nullptr) { KJ_IF_MAYBE(exception, result.exception) { throwRecoverableException(kj::mv(*exception)); } } else KJ_IF_MAYBE(exception, result.exception) { throwRecoverableException(kj::mv(*exception)); } else { // Result contained neither a value nor an exception? KJ_UNREACHABLE; } } class Event { // An event waiting to be executed. Not for direct use by applications -- promises use this // internally. public: Event(); Event(kj::EventLoop& loop); ~Event() noexcept(false); KJ_DISALLOW_COPY(Event); void armDepthFirst(); // Enqueue this event so that `fire()` will be called from the event loop soon. // // Events scheduled in this way are executed in depth-first order: if an event callback arms // more events, those events are placed at the front of the queue (in the order in which they // were armed), so that they run immediately after the first event's callback returns. // // Depth-first event scheduling is appropriate for events that represent simple continuations // of a previous event that should be globbed together for performance. Depth-first scheduling // can lead to starvation, so any long-running task must occasionally yield with // `armBreadthFirst()`. (Promise::then() uses depth-first whereas evalLater() uses // breadth-first.) // // To use breadth-first scheduling instead, use `armBreadthFirst()`. void armBreadthFirst(); // Like `armDepthFirst()` except that the event is placed at the end of the queue. void armLast(); // Enqueues this event to happen after all other events have run to completion and there is // really nothing left to do except wait for I/O. void disarm(); // If the event is armed but hasn't fired, cancel it. (Destroying the event does this // implicitly.) kj::String trace(); // Dump debug info about this event. virtual _::PromiseNode* getInnerForTrace(); // If this event wraps a PromiseNode, get that node. Used for debug tracing. // Default implementation returns nullptr. protected: virtual Maybe> fire() = 0; // Fire the event. Possibly returns a pointer to itself, which will be discarded by the // caller. This is the only way that an event can delete itself as a result of firing, as // doing so from within fire() will throw an exception. private: friend class kj::EventLoop; EventLoop& loop; Event* next; Event** prev; bool firing = false; }; class PromiseNode { // A Promise contains a chain of PromiseNodes tracking the pending transformations. // // To reduce generated code bloat, PromiseNode is not a template. Instead, it makes very hacky // use of pointers to ExceptionOrValue which actually point to ExceptionOr, but are only // so down-cast in the few places that really need to be templated. Luckily this is all // internal implementation details. public: virtual void onReady(Event* event) noexcept = 0; // Arms the given event when ready. // // May be called multiple times. If called again before the event was armed, the old event will // never be armed, only the new one. If called again after the event was armed, the new event // will be armed immediately. Can be called with nullptr to un-register the existing event. virtual void setSelfPointer(Own* selfPtr) noexcept; // Tells the node that `selfPtr` is the pointer that owns this node, and will continue to own // this node until it is destroyed or setSelfPointer() is called again. ChainPromiseNode uses // this to shorten redundant chains. The default implementation does nothing; only // ChainPromiseNode should implement this. virtual void get(ExceptionOrValue& output) noexcept = 0; // Get the result. `output` points to an ExceptionOr into which the result will be written. // Can only be called once, and only after the node is ready. Must be called directly from the // event loop, with no application code on the stack. virtual PromiseNode* getInnerForTrace(); // If this node wraps some other PromiseNode, get the wrapped node. Used for debug tracing. // Default implementation returns nullptr. template static Own from(T&& promise) { // Given a Promise, extract the PromiseNode. return kj::mv(promise.node); } template static PromiseNode& from(T& promise) { // Given a Promise, extract the PromiseNode. return *promise.node; } template static T to(Own&& node) { // Construct a Promise from a PromiseNode. (T should be a Promise type.) return T(false, kj::mv(node)); } protected: class OnReadyEvent { // Helper class for implementing onReady(). public: void init(Event* newEvent); void arm(); void armBreadthFirst(); // Arms the event if init() has already been called and makes future calls to init() // automatically arm the event. private: Event* event = nullptr; }; }; // ------------------------------------------------------------------- template inline NeverDone::operator Promise() const { return PromiseNode::to>(neverDone()); } // ------------------------------------------------------------------- class ImmediatePromiseNodeBase: public PromiseNode { public: ImmediatePromiseNodeBase(); ~ImmediatePromiseNodeBase() noexcept(false); void onReady(Event* event) noexcept override; }; template class ImmediatePromiseNode final: public ImmediatePromiseNodeBase { // A promise that has already been resolved to an immediate value or exception. public: ImmediatePromiseNode(ExceptionOr&& result): result(kj::mv(result)) {} void get(ExceptionOrValue& output) noexcept override { output.as() = kj::mv(result); } private: ExceptionOr result; }; class ImmediateBrokenPromiseNode final: public ImmediatePromiseNodeBase { public: ImmediateBrokenPromiseNode(Exception&& exception); void get(ExceptionOrValue& output) noexcept override; private: Exception exception; }; // ------------------------------------------------------------------- class AttachmentPromiseNodeBase: public PromiseNode { public: AttachmentPromiseNodeBase(Own&& dependency); void onReady(Event* event) noexcept override; void get(ExceptionOrValue& output) noexcept override; PromiseNode* getInnerForTrace() override; private: Own dependency; void dropDependency(); template friend class AttachmentPromiseNode; }; template class AttachmentPromiseNode final: public AttachmentPromiseNodeBase { // A PromiseNode that holds on to some object (usually, an Own, but could be any movable // object) until the promise resolves. public: AttachmentPromiseNode(Own&& dependency, Attachment&& attachment) : AttachmentPromiseNodeBase(kj::mv(dependency)), attachment(kj::mv(attachment)) {} ~AttachmentPromiseNode() noexcept(false) { // We need to make sure the dependency is deleted before we delete the attachment because the // dependency may be using the attachment. dropDependency(); } private: Attachment attachment; }; // ------------------------------------------------------------------- #if __GNUC__ >= 8 && !__clang__ // GCC 8's class-memaccess warning rightly does not like the memcpy()'s below, but there's no // "legal" way for us to extract the contetn of a PTMF so too bad. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif class PtmfHelper { // This class is a private helper for GetFunctorStartAddress. The class represents the internal // representation of a pointer-to-member-function. template friend struct GetFunctorStartAddress; #if __GNUG__ void* ptr; ptrdiff_t adj; // Layout of a pointer-to-member-function used by GCC and compatible compilers. void* apply(void* obj) { #if defined(__arm__) || defined(__mips__) || defined(__aarch64__) if (adj & 1) { ptrdiff_t voff = (ptrdiff_t)ptr; #else ptrdiff_t voff = (ptrdiff_t)ptr; if (voff & 1) { voff &= ~1; #endif return *(void**)(*(char**)obj + voff); } else { return ptr; } } #define BODY \ PtmfHelper result; \ static_assert(sizeof(p) == sizeof(result), "unknown ptmf layout"); \ memcpy(&result, &p, sizeof(result)); \ return result #else // __GNUG__ void* apply(void* obj) { return nullptr; } // TODO(port): PTMF instruction address extraction #define BODY return PtmfHelper{} #endif // __GNUG__, else template static PtmfHelper from(F p) { BODY; } // Create a PtmfHelper from some arbitrary pointer-to-member-function which is not // overloaded nor a template. In this case the compiler is able to deduce the full function // signature directly given the name since there is only one function with that name. template static PtmfHelper from(R (C::*p)(NoInfer

...)) { BODY; } template static PtmfHelper from(R (C::*p)(NoInfer

...) const) { BODY; } // Create a PtmfHelper from some poniter-to-member-function which is a template. In this case // the function must match exactly the containing type C, return type R, and parameter types P... // GetFunctorStartAddress normally specifies exactly the correct C and R, but can only make a // guess at P. Luckily, if the function parameters are template parameters then it's not // necessary to be precise about P. #undef BODY }; #if __GNUC__ >= 8 && !__clang__ #pragma GCC diagnostic pop #endif template struct GetFunctorStartAddress { // Given a functor (any object defining operator()), return the start address of the function, // suitable for passing to addr2line to obtain a source file/line for debugging purposes. // // This turns out to be incredibly hard to implement in the presence of overloaded or templated // functors. Therefore, we impose these specific restrictions, specific to our use case: // - Overloading is not allowed, but templating is. (Generally we only intend to support lambdas // anyway.) // - The template parameters to GetFunctorStartAddress specify a hint as to the expected // parameter types. If the functor is templated, its parameters must match exactly these types. // (If it's not templated, ParamTypes are ignored.) template static void* apply(Func&& func) { typedef decltype(func(instance()...)) ReturnType; return PtmfHelper::from, ParamTypes...>( &Decay::operator()).apply(&func); } }; template <> struct GetFunctorStartAddress: public GetFunctorStartAddress<> {}; // Hack for TransformPromiseNode use case: an input type of `Void` indicates that the function // actually has no parameters. class TransformPromiseNodeBase: public PromiseNode { public: TransformPromiseNodeBase(Own&& dependency, void* continuationTracePtr); void onReady(Event* event) noexcept override; void get(ExceptionOrValue& output) noexcept override; PromiseNode* getInnerForTrace() override; private: Own dependency; void* continuationTracePtr; void dropDependency(); void getDepResult(ExceptionOrValue& output); virtual void getImpl(ExceptionOrValue& output) = 0; template friend class TransformPromiseNode; }; template class TransformPromiseNode final: public TransformPromiseNodeBase { // A PromiseNode that transforms the result of another PromiseNode through an application-provided // function (implements `then()`). public: TransformPromiseNode(Own&& dependency, Func&& func, ErrorFunc&& errorHandler) : TransformPromiseNodeBase(kj::mv(dependency), GetFunctorStartAddress::apply(func)), func(kj::fwd(func)), errorHandler(kj::fwd(errorHandler)) {} ~TransformPromiseNode() noexcept(false) { // We need to make sure the dependency is deleted before we delete the continuations because it // is a common pattern for the continuations to hold ownership of objects that might be in-use // by the dependency. dropDependency(); } private: Func func; ErrorFunc errorHandler; void getImpl(ExceptionOrValue& output) override { ExceptionOr depResult; getDepResult(depResult); KJ_IF_MAYBE(depException, depResult.exception) { output.as() = handle( MaybeVoidCaller>>::apply( errorHandler, kj::mv(*depException))); } else KJ_IF_MAYBE(depValue, depResult.value) { output.as() = handle(MaybeVoidCaller::apply(func, kj::mv(*depValue))); } } ExceptionOr handle(T&& value) { return kj::mv(value); } ExceptionOr handle(PropagateException::Bottom&& value) { return ExceptionOr(false, value.asException()); } }; // ------------------------------------------------------------------- class ForkHubBase; class ForkBranchBase: public PromiseNode { public: ForkBranchBase(Own&& hub); ~ForkBranchBase() noexcept(false); void hubReady() noexcept; // Called by the hub to indicate that it is ready. // implements PromiseNode ------------------------------------------ void onReady(Event* event) noexcept override; PromiseNode* getInnerForTrace() override; protected: inline ExceptionOrValue& getHubResultRef(); void releaseHub(ExceptionOrValue& output); // Release the hub. If an exception is thrown, add it to `output`. private: OnReadyEvent onReadyEvent; Own hub; ForkBranchBase* next = nullptr; ForkBranchBase** prevPtr = nullptr; friend class ForkHubBase; }; template T copyOrAddRef(T& t) { return t; } template Own copyOrAddRef(Own& t) { return t->addRef(); } template class ForkBranch final: public ForkBranchBase { // A PromiseNode that implements one branch of a fork -- i.e. one of the branches that receives // a const reference. public: ForkBranch(Own&& hub): ForkBranchBase(kj::mv(hub)) {} void get(ExceptionOrValue& output) noexcept override { ExceptionOr& hubResult = getHubResultRef().template as(); KJ_IF_MAYBE(value, hubResult.value) { output.as().value = copyOrAddRef(*value); } else { output.as().value = nullptr; } output.exception = hubResult.exception; releaseHub(output); } }; template class SplitBranch final: public ForkBranchBase { // A PromiseNode that implements one branch of a fork -- i.e. one of the branches that receives // a const reference. public: SplitBranch(Own&& hub): ForkBranchBase(kj::mv(hub)) {} typedef kj::Decay(kj::instance()))> Element; void get(ExceptionOrValue& output) noexcept override { ExceptionOr& hubResult = getHubResultRef().template as(); KJ_IF_MAYBE(value, hubResult.value) { output.as().value = kj::mv(kj::get(*value)); } else { output.as().value = nullptr; } output.exception = hubResult.exception; releaseHub(output); } }; // ------------------------------------------------------------------- class ForkHubBase: public Refcounted, protected Event { public: ForkHubBase(Own&& inner, ExceptionOrValue& resultRef); inline ExceptionOrValue& getResultRef() { return resultRef; } private: Own inner; ExceptionOrValue& resultRef; ForkBranchBase* headBranch = nullptr; ForkBranchBase** tailBranch = &headBranch; // Tail becomes null once the inner promise is ready and all branches have been notified. Maybe> fire() override; _::PromiseNode* getInnerForTrace() override; friend class ForkBranchBase; }; template class ForkHub final: public ForkHubBase { // A PromiseNode that implements the hub of a fork. The first call to Promise::fork() replaces // the promise's outer node with a ForkHub, and subsequent calls add branches to that hub (if // possible). public: ForkHub(Own&& inner): ForkHubBase(kj::mv(inner), result) {} Promise<_::UnfixVoid> addBranch() { return _::PromiseNode::to>>(kj::heap>(addRef(*this))); } _::SplitTuplePromise split() { return splitImpl(MakeIndexes()>()); } private: ExceptionOr result; template _::SplitTuplePromise splitImpl(Indexes) { return kj::tuple(addSplit()...); } template ReducePromises::Element> addSplit() { return _::PromiseNode::to::Element>>( maybeChain(kj::heap>(addRef(*this)), implicitCast::Element*>(nullptr))); } }; inline ExceptionOrValue& ForkBranchBase::getHubResultRef() { return hub->getResultRef(); } // ------------------------------------------------------------------- class ChainPromiseNode final: public PromiseNode, public Event { // Promise node which reduces Promise> to Promise. // // `Event` is only a public base class because otherwise we can't cast Own to // Own. Ugh, templates and private... public: explicit ChainPromiseNode(Own inner); ~ChainPromiseNode() noexcept(false); void onReady(Event* event) noexcept override; void setSelfPointer(Own* selfPtr) noexcept override; void get(ExceptionOrValue& output) noexcept override; PromiseNode* getInnerForTrace() override; private: enum State { STEP1, STEP2 }; State state; Own inner; // In STEP1, a PromiseNode for a Promise. // In STEP2, a PromiseNode for a T. Event* onReadyEvent = nullptr; Own* selfPtr = nullptr; Maybe> fire() override; }; template Own maybeChain(Own&& node, Promise*) { return heap(kj::mv(node)); } template Own&& maybeChain(Own&& node, T*) { return kj::mv(node); } template >()))> inline Result maybeReduce(Promise&& promise, bool) { return T::reducePromise(kj::mv(promise)); } template inline Promise maybeReduce(Promise&& promise, ...) { return kj::mv(promise); } // ------------------------------------------------------------------- class ExclusiveJoinPromiseNode final: public PromiseNode { public: ExclusiveJoinPromiseNode(Own left, Own right); ~ExclusiveJoinPromiseNode() noexcept(false); void onReady(Event* event) noexcept override; void get(ExceptionOrValue& output) noexcept override; PromiseNode* getInnerForTrace() override; private: class Branch: public Event { public: Branch(ExclusiveJoinPromiseNode& joinNode, Own dependency); ~Branch() noexcept(false); bool get(ExceptionOrValue& output); // Returns true if this is the side that finished. Maybe> fire() override; _::PromiseNode* getInnerForTrace() override; private: ExclusiveJoinPromiseNode& joinNode; Own dependency; }; Branch left; Branch right; OnReadyEvent onReadyEvent; }; // ------------------------------------------------------------------- class ArrayJoinPromiseNodeBase: public PromiseNode { public: ArrayJoinPromiseNodeBase(Array> promises, ExceptionOrValue* resultParts, size_t partSize); ~ArrayJoinPromiseNodeBase() noexcept(false); void onReady(Event* event) noexcept override final; void get(ExceptionOrValue& output) noexcept override final; PromiseNode* getInnerForTrace() override final; protected: virtual void getNoError(ExceptionOrValue& output) noexcept = 0; // Called to compile the result only in the case where there were no errors. private: uint countLeft; OnReadyEvent onReadyEvent; class Branch final: public Event { public: Branch(ArrayJoinPromiseNodeBase& joinNode, Own dependency, ExceptionOrValue& output); ~Branch() noexcept(false); Maybe> fire() override; _::PromiseNode* getInnerForTrace() override; Maybe getPart(); // Calls dependency->get(output). If there was an exception, return it. private: ArrayJoinPromiseNodeBase& joinNode; Own dependency; ExceptionOrValue& output; }; Array branches; }; template class ArrayJoinPromiseNode final: public ArrayJoinPromiseNodeBase { public: ArrayJoinPromiseNode(Array> promises, Array> resultParts) : ArrayJoinPromiseNodeBase(kj::mv(promises), resultParts.begin(), sizeof(ExceptionOr)), resultParts(kj::mv(resultParts)) {} protected: void getNoError(ExceptionOrValue& output) noexcept override { auto builder = heapArrayBuilder(resultParts.size()); for (auto& part: resultParts) { KJ_IASSERT(part.value != nullptr, "Bug in KJ promise framework: Promise result had neither value no exception."); builder.add(kj::mv(*_::readMaybe(part.value))); } output.as>() = builder.finish(); } private: Array> resultParts; }; template <> class ArrayJoinPromiseNode final: public ArrayJoinPromiseNodeBase { public: ArrayJoinPromiseNode(Array> promises, Array> resultParts); ~ArrayJoinPromiseNode(); protected: void getNoError(ExceptionOrValue& output) noexcept override; private: Array> resultParts; }; // ------------------------------------------------------------------- class EagerPromiseNodeBase: public PromiseNode, protected Event { // A PromiseNode that eagerly evaluates its dependency even if its dependent does not eagerly // evaluate it. public: EagerPromiseNodeBase(Own&& dependency, ExceptionOrValue& resultRef); void onReady(Event* event) noexcept override; PromiseNode* getInnerForTrace() override; private: Own dependency; OnReadyEvent onReadyEvent; ExceptionOrValue& resultRef; Maybe> fire() override; }; template class EagerPromiseNode final: public EagerPromiseNodeBase { public: EagerPromiseNode(Own&& dependency) : EagerPromiseNodeBase(kj::mv(dependency), result) {} void get(ExceptionOrValue& output) noexcept override { output.as() = kj::mv(result); } private: ExceptionOr result; }; template Own spark(Own&& node) { // Forces evaluation of the given node to begin as soon as possible, even if no one is waiting // on it. return heap>(kj::mv(node)); } // ------------------------------------------------------------------- class AdapterPromiseNodeBase: public PromiseNode { public: void onReady(Event* event) noexcept override; protected: inline void setReady() { onReadyEvent.arm(); } private: OnReadyEvent onReadyEvent; }; template class AdapterPromiseNode final: public AdapterPromiseNodeBase, private PromiseFulfiller> { // A PromiseNode that wraps a PromiseAdapter. public: template AdapterPromiseNode(Params&&... params) : adapter(static_cast>&>(*this), kj::fwd(params)...) {} void get(ExceptionOrValue& output) noexcept override { KJ_IREQUIRE(!isWaiting()); output.as() = kj::mv(result); } private: ExceptionOr result; bool waiting = true; Adapter adapter; void fulfill(T&& value) override { if (waiting) { waiting = false; result = ExceptionOr(kj::mv(value)); setReady(); } } void reject(Exception&& exception) override { if (waiting) { waiting = false; result = ExceptionOr(false, kj::mv(exception)); setReady(); } } bool isWaiting() override { return waiting; } }; // ------------------------------------------------------------------- class FiberBase: public PromiseNode, private Event { // Base class for the outer PromiseNode representing a fiber. public: FiberBase(size_t stackSize, _::ExceptionOrValue& result); ~FiberBase() noexcept(false); void start() { armDepthFirst(); } // Call immediately after construction to begin executing the fiber. class WaitDoneEvent; void onReady(_::Event* event) noexcept override; PromiseNode* getInnerForTrace() override; protected: bool isFinished() { return state == FINISHED; } void destroy(); private: enum { WAITING, RUNNING, CANCELED, FINISHED } state; size_t stackSize; #if _WIN32 || __CYGWIN__ void* osFiber; #elif !__BIONIC__ struct Impl; Impl& impl; #endif _::PromiseNode* currentInner = nullptr; OnReadyEvent onReadyEvent; _::ExceptionOrValue& result; void run(); virtual void runImpl(WaitScope& waitScope) = 0; struct StartRoutine; void switchToFiber(); void switchToMain(); Maybe> fire() override; // Implements Event. Each time the event is fired, switchToFiber() is called. friend class WaitScope; friend void _::waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope); friend bool _::pollImpl(_::PromiseNode& node, WaitScope& waitScope); }; template class Fiber final: public FiberBase { public: Fiber(size_t stackSize, Func&& func): FiberBase(stackSize, result), func(kj::fwd(func)) {} ~Fiber() noexcept(false) { destroy(); } typedef FixVoid()(kj::instance()))> ResultType; void get(ExceptionOrValue& output) noexcept override { KJ_IREQUIRE(isFinished()); output.as() = kj::mv(result); } private: Func func; ExceptionOr result; void runImpl(WaitScope& waitScope) override { result.template as() = MaybeVoidCaller::apply(func, waitScope); } }; } // namespace _ (private) // ======================================================================================= template Promise::Promise(_::FixVoid value) : PromiseBase(heap<_::ImmediatePromiseNode<_::FixVoid>>(kj::mv(value))) {} template Promise::Promise(kj::Exception&& exception) : PromiseBase(heap<_::ImmediateBrokenPromiseNode>(kj::mv(exception))) {} template template PromiseForResult Promise::then(Func&& func, ErrorFunc&& errorHandler) { typedef _::FixVoid<_::ReturnType> ResultT; Own<_::PromiseNode> intermediate = heap<_::TransformPromiseNode, Func, ErrorFunc>>( kj::mv(node), kj::fwd(func), kj::fwd(errorHandler)); auto result = _::PromiseNode::to<_::ChainPromises<_::ReturnType>>( _::maybeChain(kj::mv(intermediate), implicitCast(nullptr))); return _::maybeReduce(kj::mv(result), false); } namespace _ { // private template struct IdentityFunc { inline T operator()(T&& value) const { return kj::mv(value); } }; template struct IdentityFunc> { inline Promise operator()(T&& value) const { return kj::mv(value); } }; template <> struct IdentityFunc { inline void operator()() const {} }; template <> struct IdentityFunc> { Promise operator()() const; // This can't be inline because it will make the translation unit depend on kj-async. Awkwardly, // Cap'n Proto relies on being able to include this header without creating such a link-time // dependency. }; } // namespace _ (private) template template Promise Promise::catch_(ErrorFunc&& errorHandler) { // then()'s ErrorFunc can only return a Promise if Func also returns a Promise. In this case, // Func is being filled in automatically. We want to make sure ErrorFunc can return a Promise, // but we don't want the extra overhead of promise chaining if ErrorFunc doesn't actually // return a promise. So we make our Func return match ErrorFunc. return then(_::IdentityFunc()))>(), kj::fwd(errorHandler)); } template T Promise::wait(WaitScope& waitScope) { _::ExceptionOr<_::FixVoid> result; _::waitImpl(kj::mv(node), result, waitScope); return convertToReturn(kj::mv(result)); } template bool Promise::poll(WaitScope& waitScope) { return _::pollImpl(*node, waitScope); } template ForkedPromise Promise::fork() { return ForkedPromise(false, refcounted<_::ForkHub<_::FixVoid>>(kj::mv(node))); } template Promise ForkedPromise::addBranch() { return hub->addBranch(); } template _::SplitTuplePromise Promise::split() { return refcounted<_::ForkHub<_::FixVoid>>(kj::mv(node))->split(); } template Promise Promise::exclusiveJoin(Promise&& other) { return Promise(false, heap<_::ExclusiveJoinPromiseNode>(kj::mv(node), kj::mv(other.node))); } template template Promise Promise::attach(Attachments&&... attachments) { return Promise(false, kj::heap<_::AttachmentPromiseNode>>( kj::mv(node), kj::tuple(kj::fwd(attachments)...))); } template template Promise Promise::eagerlyEvaluate(ErrorFunc&& errorHandler) { // See catch_() for commentary. return Promise(false, _::spark<_::FixVoid>(then( _::IdentityFunc()))>(), kj::fwd(errorHandler)).node)); } template Promise Promise::eagerlyEvaluate(decltype(nullptr)) { return Promise(false, _::spark<_::FixVoid>(kj::mv(node))); } template kj::String Promise::trace() { return PromiseBase::trace(); } template inline PromiseForResult evalLater(Func&& func) { return _::yield().then(kj::fwd(func), _::PropagateException()); } template inline PromiseForResult evalLast(Func&& func) { return _::yieldHarder().then(kj::fwd(func), _::PropagateException()); } template inline PromiseForResult evalNow(Func&& func) { PromiseForResult result = nullptr; KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { result = func(); })) { result = kj::mv(*e); } return result; } template inline PromiseForResult startFiber(size_t stackSize, Func&& func) { typedef _::FixVoid<_::ReturnType> ResultT; Own<_::FiberBase> intermediate = kj::heap<_::Fiber>(stackSize, kj::fwd(func)); intermediate->start(); auto result = _::PromiseNode::to<_::ChainPromises<_::ReturnType>>( _::maybeChain(kj::mv(intermediate), implicitCast(nullptr))); return _::maybeReduce(kj::mv(result), false); } template template void Promise::detach(ErrorFunc&& errorHandler) { return _::detach(then([](T&&) {}, kj::fwd(errorHandler))); } template <> template void Promise::detach(ErrorFunc&& errorHandler) { return _::detach(then([]() {}, kj::fwd(errorHandler))); } template Promise> joinPromises(Array>&& promises) { return _::PromiseNode::to>>(kj::heap<_::ArrayJoinPromiseNode>( KJ_MAP(p, promises) { return _::PromiseNode::from(kj::mv(p)); }, heapArray<_::ExceptionOr>(promises.size()))); } // ======================================================================================= namespace _ { // private template class WeakFulfiller final: public PromiseFulfiller, private kj::Disposer { // A wrapper around PromiseFulfiller which can be detached. // // There are a couple non-trivialities here: // - If the WeakFulfiller is discarded, we want the promise it fulfills to be implicitly // rejected. // - We cannot destroy the WeakFulfiller until the application has discarded it *and* it has been // detached from the underlying fulfiller, because otherwise the later detach() call will go // to a dangling pointer. Essentially, WeakFulfiller is reference counted, although the // refcount never goes over 2 and we manually implement the refcounting because we need to do // other special things when each side detaches anyway. To this end, WeakFulfiller is its own // Disposer -- dispose() is called when the application discards its owned pointer to the // fulfiller and detach() is called when the promise is destroyed. public: KJ_DISALLOW_COPY(WeakFulfiller); static kj::Own make() { WeakFulfiller* ptr = new WeakFulfiller; return Own(ptr, *ptr); } void fulfill(FixVoid&& value) override { if (inner != nullptr) { inner->fulfill(kj::mv(value)); } } void reject(Exception&& exception) override { if (inner != nullptr) { inner->reject(kj::mv(exception)); } } bool isWaiting() override { return inner != nullptr && inner->isWaiting(); } void attach(PromiseFulfiller& newInner) { inner = &newInner; } void detach(PromiseFulfiller& from) { if (inner == nullptr) { // Already disposed. delete this; } else { KJ_IREQUIRE(inner == &from); inner = nullptr; } } private: mutable PromiseFulfiller* inner; WeakFulfiller(): inner(nullptr) {} void disposeImpl(void* pointer) const override { // TODO(perf): Factor some of this out so it isn't regenerated for every fulfiller type? if (inner == nullptr) { // Already detached. delete this; } else { if (inner->isWaiting()) { inner->reject(kj::Exception(kj::Exception::Type::FAILED, __FILE__, __LINE__, kj::heapString("PromiseFulfiller was destroyed without fulfilling the promise."))); } inner = nullptr; } } }; template class PromiseAndFulfillerAdapter { public: PromiseAndFulfillerAdapter(PromiseFulfiller& fulfiller, WeakFulfiller& wrapper) : fulfiller(fulfiller), wrapper(wrapper) { wrapper.attach(fulfiller); } ~PromiseAndFulfillerAdapter() noexcept(false) { wrapper.detach(fulfiller); } private: PromiseFulfiller& fulfiller; WeakFulfiller& wrapper; }; } // namespace _ (private) template template bool PromiseFulfiller::rejectIfThrows(Func&& func) { KJ_IF_MAYBE(exception, kj::runCatchingExceptions(kj::mv(func))) { reject(kj::mv(*exception)); return false; } else { return true; } } template bool PromiseFulfiller::rejectIfThrows(Func&& func) { KJ_IF_MAYBE(exception, kj::runCatchingExceptions(kj::mv(func))) { reject(kj::mv(*exception)); return false; } else { return true; } } template _::ReducePromises newAdaptedPromise(Params&&... adapterConstructorParams) { Own<_::PromiseNode> intermediate( heap<_::AdapterPromiseNode<_::FixVoid, Adapter>>( kj::fwd(adapterConstructorParams)...)); return _::PromiseNode::to<_::ReducePromises>( _::maybeChain(kj::mv(intermediate), implicitCast(nullptr))); } template PromiseFulfillerPair newPromiseAndFulfiller() { auto wrapper = _::WeakFulfiller::make(); Own<_::PromiseNode> intermediate( heap<_::AdapterPromiseNode<_::FixVoid, _::PromiseAndFulfillerAdapter>>(*wrapper)); auto promise = _::PromiseNode::to<_::ReducePromises>( _::maybeChain(kj::mv(intermediate), implicitCast(nullptr))); return PromiseFulfillerPair { kj::mv(promise), kj::mv(wrapper) }; } // ======================================================================================= // cross-thread stuff namespace _ { // (private) class XThreadEvent: private Event, // it's an event in the target thread public PromiseNode { // it's a PromiseNode in the requesting thread public: XThreadEvent(ExceptionOrValue& result, const Executor& targetExecutor) : Event(targetExecutor.loop), result(result), targetExecutor(targetExecutor) {} protected: void ensureDoneOrCanceled(); // MUST be called in destructor of subclasses to make sure the object is not destroyed while // still being accessed by the other thread. (This can't be placed in ~XThreadEvent() because // that destructor doesn't run until the subclass has already been destroyed.) virtual kj::Maybe> execute() = 0; // Run the function. If the function returns a promise, returns the inner PromiseNode, otherwise // returns null. // implements PromiseNode ---------------------------------------------------- void onReady(Event* event) noexcept override; private: ExceptionOrValue& result; const Executor& targetExecutor; Maybe replyExecutor; // If executeAsync() was used. kj::Maybe> promiseNode; // Accessed only in target thread. Maybe targetNext; Maybe* targetPrev = nullptr; // Membership in one of the linked lists in the target Executor's work list or cancel list. These // fields are protected by the target Executor's mutex. enum { UNUSED, // Object was never queued on another thread. QUEUED, // Target thread has not yet dequeued the event from crossThreadRequests.events. The requesting // thread can cancel execution by removing the event from the list. EXECUTING, // Target thread has dequeued the event and is executing it. To cancel, the requesting thread // must add the event to the crossThreadRequests.cancel list. DONE // Target thread has completed handling this event and will not touch it again. The requesting // thread can safely delete the object. The `state` is updated to `DONE` using an atomic // release operation after ensuring that the event will not be touched again, so that the // requesting can safely skip locking if it observes the state is already DONE. } state = UNUSED; // State, which is also protected by `targetExecutor`'s mutex. Maybe replyNext; Maybe* replyPrev = nullptr; // Membership in `replyExecutor`'s reply list. Protected by `replyExecutor`'s mutex. The // executing thread places the event in the reply list near the end of the `EXECUTING` state. // Because the thread cannot lock two mutexes at once, it's possible that the reply executor // will receive the reply while the event is still listed in the EXECUTING state, but it can // ignore the state and proceed with the result. OnReadyEvent onReadyEvent; // Accessed only in requesting thread. friend class kj::Executor; void done(); class DelayedDoneHack; // implements Event ---------------------------------------------------------- Maybe> fire() override; // If called with promiseNode == nullptr, it's time to call execute(). If promiseNode != nullptr, // then it just indicated readiness and we need to get its result. }; template >> class XThreadEventImpl final: public XThreadEvent { // Implementation for a function that does not return a Promise. public: XThreadEventImpl(Func&& func, const Executor& target) : XThreadEvent(result, target), func(kj::fwd(func)) {} ~XThreadEventImpl() noexcept(false) { ensureDoneOrCanceled(); } typedef _::FixVoid<_::ReturnType> ResultT; kj::Maybe> execute() override { result.value = MaybeVoidCaller>::apply(func, Void()); return nullptr; } // implements PromiseNode ---------------------------------------------------- void get(ExceptionOrValue& output) noexcept override { output.as() = kj::mv(result); } private: Func func; ExceptionOr result; friend Executor; }; template class XThreadEventImpl> final: public XThreadEvent { // Implementation for a function that DOES return a Promise. public: XThreadEventImpl(Func&& func, const Executor& target) : XThreadEvent(result, target), func(kj::fwd(func)) {} ~XThreadEventImpl() noexcept(false) { ensureDoneOrCanceled(); } typedef _::FixVoid<_::UnwrapPromise>> ResultT; kj::Maybe> execute() override { auto result = _::PromiseNode::from(func()); KJ_IREQUIRE(result.get() != nullptr); return kj::mv(result); } // implements PromiseNode ---------------------------------------------------- void get(ExceptionOrValue& output) noexcept override { output.as() = kj::mv(result); } private: Func func; ExceptionOr result; friend Executor; }; } // namespace _ (private) template _::UnwrapPromise> Executor::executeSync(Func&& func) const { _::XThreadEventImpl event(kj::fwd(func), *this); send(event, true); return convertToReturn(kj::mv(event.result)); } template PromiseForResult Executor::executeAsync(Func&& func) const { auto event = kj::heap<_::XThreadEventImpl>(kj::fwd(func), *this); send(*event, false); return _::PromiseNode::to>(kj::mv(event)); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/time.h0000644000175000017500000001143513650101756017262 0ustar00kentonkenton00000000000000// Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "units.h" #include #include "string.h" KJ_BEGIN_HEADER namespace kj { namespace _ { // private class NanosecondLabel; class TimeLabel; class DateLabel; } // namespace _ (private) using Duration = Quantity; // A time value, in nanoseconds. constexpr Duration NANOSECONDS = unit(); constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; constexpr Duration SECONDS = 1000 * MILLISECONDS; constexpr Duration MINUTES = 60 * SECONDS; constexpr Duration HOURS = 60 * MINUTES; constexpr Duration DAYS = 24 * HOURS; using TimePoint = Absolute; // An absolute time measured by some particular instance of `Timer` or `MonotonicClock`. `Time`s // from two different `Timer`s or `MonotonicClock`s may be measured from different origins and so // are not necessarily compatible. using Date = Absolute; // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). kj::String KJ_STRINGIFY(TimePoint); kj::String KJ_STRINGIFY(Date); kj::String KJ_STRINGIFY(Duration); constexpr Date UNIX_EPOCH = origin(); // The `Date` representing Jan 1, 1970 00:00:00 UTC. class Clock { // Interface to read the current date and time. public: virtual Date now() const = 0; }; class MonotonicClock { // Interface to read time in a way that increases as real-world time increases, independent of // any manual changes to the calendar date/time. Such a clock never "goes backwards" even if the // system administrator changes the calendar time or suspends the system. However, this clock's // time points are only meaningful in comparison to other time points from the same clock, and // cannot be used to determine the current calendar date. public: virtual TimePoint now() const = 0; }; const Clock& nullClock(); // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about // time. const Clock& systemCoarseCalendarClock(); const Clock& systemPreciseCalendarClock(); // A clock that reads the real system time. // // In well-designed code, this should only be called by the top-level dependency injector. All // other modules should request that the caller provide a Clock so that alternate clock // implementations can be injected for testing, simulation, reproducibility, and other purposes. // // The "coarse" version has precision around 1-10ms, while the "precise" version has precision // better than 1us. The "precise" version may be slightly slower, though on modern hardware and // a reasonable operating system the difference is usually negligible. // // Note: On Windows prior to Windows 8, there is no precise calendar clock; the "precise" clock // will be no more precise than the "coarse" clock in this case. const MonotonicClock& systemCoarseMonotonicClock(); const MonotonicClock& systemPreciseMonotonicClock(); // A MonotonicClock that reads the real system time. // // In well-designed code, this should only be called by the top-level dependency injector. All // other modules should request that the caller provide a Clock so that alternate clock // implementations can be injected for testing, simulation, reproducibility, and other purposes. // // The "coarse" version has precision around 1-10ms, while the "precise" version has precision // better than 1us. The "precise" version may be slightly slower, though on modern hardware and // a reasonable operating system the difference is usually negligible. } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/timer.h0000644000175000017500000001277213650101756017451 0ustar00kentonkenton00000000000000// Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "time.h" #include "async.h" KJ_BEGIN_HEADER namespace kj { class Timer: public MonotonicClock { // Interface to time and timer functionality. // // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread). However, // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar // date as tracked by the system is manually modified. // // That said, the `Timer` returned by `kj::setupAsyncIo().provider->getTimer()` in particular is // guaranteed to be synchronized with the `MonotonicClock` returned by // `systemPreciseMonotonicClock()` (or, more precisely, is updated to match that clock whenever // the loop waits). public: virtual TimePoint now() const = 0; // Returns the current value of a clock that moves steadily forward, independent of any // changes in the wall clock. The value is updated every time the event loop waits, // and is constant in-between waits. virtual Promise atTime(TimePoint time) = 0; // Returns a promise that returns as soon as now() >= time. virtual Promise afterDelay(Duration delay) = 0; // Equivalent to atTime(now() + delay). template Promise timeoutAt(TimePoint time, Promise&& promise) KJ_WARN_UNUSED_RESULT; // Return a promise equivalent to `promise` but which throws an exception (and cancels the // original promise) if it hasn't completed by `time`. The thrown exception is of type // "OVERLOADED". template Promise timeoutAfter(Duration delay, Promise&& promise) KJ_WARN_UNUSED_RESULT; // Return a promise equivalent to `promise` but which throws an exception (and cancels the // original promise) if it hasn't completed after `delay` from now. The thrown exception is of // type "OVERLOADED". private: static kj::Exception makeTimeoutException(); }; class TimerImpl final: public Timer { // Implementation of Timer that expects an external caller -- usually, the EventPort // implementation -- to tell it when time has advanced. public: TimerImpl(TimePoint startTime); ~TimerImpl() noexcept(false); Maybe nextEvent(); // Returns the time at which the next scheduled timer event will occur, or null if no timer // events are scheduled. Maybe timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max); // Convenience method which computes a timeout value to pass to an event-waiting system call to // cause it to time out when the next timer event occurs. // // `start` is the time at which the timeout starts counting. This is typically not the same as // now() since some time may have passed since the last time advanceTo() was called. // // `unit` is the time unit in which the timeout is measured. This is often MILLISECONDS. Note // that this method will fractional values *up*, to guarantee that the returned timeout waits // until just *after* the time the event is scheduled. // // The timeout will be clamped to `max`. Use this to avoid an overflow if e.g. the OS wants a // 32-bit value or a signed value. // // Returns nullptr if there are no future events. void advanceTo(TimePoint newTime); // Set the time to `time` and fire any at() events that have been passed. // implements Timer ---------------------------------------------------------- TimePoint now() const override; Promise atTime(TimePoint time) override; Promise afterDelay(Duration delay) override; private: struct Impl; class TimerPromiseAdapter; TimePoint time; Own impl; }; // ======================================================================================= // inline implementation details template Promise Timer::timeoutAt(TimePoint time, Promise&& promise) { return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise { return makeTimeoutException(); })); } template Promise Timer::timeoutAfter(Duration delay, Promise&& promise) { return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise { return makeTimeoutException(); })); } inline TimePoint TimerImpl::now() const { return time; } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/async-unix.h0000644000175000017500000003523713650101756020430 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #if _WIN32 #error "This file is Unix-specific. On Windows, include async-win32.h instead." #endif #include "async.h" #include "timer.h" #include "vector.h" #include "io.h" #include KJ_BEGIN_HEADER #if __linux__ && !__BIONIC__ && !defined(KJ_USE_EPOLL) // Default to epoll on Linux, except on Bionic (Android) which doesn't have signalfd.h. #define KJ_USE_EPOLL 1 #endif #if __CYGWIN__ && !defined(KJ_USE_PIPE_FOR_WAKEUP) // Cygwin has serious issues with the intersection of signals and threads, reported here: // https://cygwin.com/ml/cygwin/2019-07/msg00052.html // On Cygwin, therefore, we do not use signals to wake threads. Instead, each thread allocates a // pipe, and we write a byte to the pipe to wake the thread... ick. #define KJ_USE_PIPE_FOR_WAKEUP 1 #endif namespace kj { class UnixEventPort: public EventPort { // An EventPort implementation which can wait for events on file descriptors as well as signals. // This API only makes sense on Unix. // // The implementation uses `poll()` or possibly a platform-specific API (e.g. epoll, kqueue). // To also wait on signals without race conditions, the implementation may block signals until // just before `poll()` while using a signal handler which `siglongjmp()`s back to just before // the signal was unblocked, or it may use a nicer platform-specific API like signalfd. // // The implementation reserves a signal for internal use. By default, it uses SIGUSR1. If you // need to use SIGUSR1 for something else, you must offer a different signal by calling // setReservedSignal() at startup. // // WARNING: A UnixEventPort can only be used in the thread and process that created it. In // particular, note that after a fork(), a UnixEventPort created in the parent process will // not work correctly in the child, even if the parent ceases to use its copy. In particular // note that this means that server processes which daemonize themselves at startup must wait // until after daemonization to create a UnixEventPort. public: UnixEventPort(); ~UnixEventPort() noexcept(false); class FdObserver; // Class that watches an fd for readability or writability. See definition below. Promise onSignal(int signum); // When the given signal is delivered to this thread, return the corresponding siginfo_t. // The signal must have been captured using `captureSignal()`. // // If `onSignal()` has not been called, the signal will remain blocked in this thread. // Therefore, a signal which arrives before `onSignal()` was called will not be "missed" -- the // next call to 'onSignal()' will receive it. Also, you can control which thread receives a // process-wide signal by only calling `onSignal()` on that thread's event loop. // // The result of waiting on the same signal twice at once is undefined. static void captureSignal(int signum); // Arranges for the given signal to be captured and handled via UnixEventPort, so that you may // then pass it to `onSignal()`. This method is static because it registers a signal handler // which applies process-wide. If any other threads exist in the process when `captureSignal()` // is called, you *must* set the signal mask in those threads to block this signal, otherwise // terrible things will happen if the signal happens to be delivered to those threads. If at // all possible, call `captureSignal()` *before* creating threads, so that threads you create in // the future will inherit the proper signal mask. // // To un-capture a signal, simply install a different signal handler and then un-block it from // the signal mask. static void setReservedSignal(int signum); // Sets the signal number which `UnixEventPort` reserves for internal use. If your application // needs to use SIGUSR1, call this at startup (before any calls to `captureSignal()` and before // constructing an `UnixEventPort`) to offer a different signal. Timer& getTimer() { return timerImpl; } Promise onChildExit(Maybe& pid); // When the given child process exits, resolves to its wait status, as returned by wait(2). You // will need to use the WIFEXITED() etc. macros to interpret the status code. // // You must call onChildExit() immediately after the child is created, before returning to the // event loop. Otherwise, you may miss the child exit event. // // `pid` is a reference to a Maybe which must be non-null at the time of the call. When // wait() is invoked (and indicates this pid has finished), `pid` will be nulled out. This is // necessary to avoid a race condition: as soon as the child has been wait()ed, the PID table // entry is freed and can then be reused. So, if you ever want safely to call `kill()` on the // PID, it's necessary to know whether it has been wait()ed already. Since the promise's // .then() continuation may not run immediately, we need a more precise way, hence we null out // the Maybe. // // You must call `kj::UnixEventPort::captureChildExit()` early in your program if you want to use // `onChildExit()`. // // WARNING: Only one UnixEventPort per process is allowed to use onChildExit(). This is because // child exit is signaled to the process via SIGCHLD, and Unix does not allow the program to // control which thread receives the signal. (We may fix this in the future by automatically // coordinating between threads when multiple threads are expecting child exits.) // WARNING 2: If any UnixEventPort in the process is currently waiting for onChildExit(), then // *only* that port's thread can safely wait on child processes, even synchronously. This is // because the thread which used onChildExit() uses wait() to reap children, without specifying // which child, and therefore it may inadvertently reap children created by other threads. static void captureChildExit(); // Arranges for child process exit to be captured and handled via UnixEventPort, so that you may // call `onChildExit()`. Much like `captureSignal()`, this static method must be called early on // in program startup. // // This method may capture the `SIGCHLD` signal. You must not use `captureSignal(SIGCHLD)` nor // `onSignal(SIGCHLD)` in your own code if you use `captureChildExit()`. // implements EventPort ------------------------------------------------------ bool wait() override; bool poll() override; void wake() const override; private: class SignalPromiseAdapter; class ChildExitPromiseAdapter; const MonotonicClock& clock; TimerImpl timerImpl; SignalPromiseAdapter* signalHead = nullptr; SignalPromiseAdapter** signalTail = &signalHead; void gotSignal(const siginfo_t& siginfo); friend class TimerPromiseAdapter; #if KJ_USE_EPOLL AutoCloseFd epollFd; AutoCloseFd signalFd; AutoCloseFd eventFd; // Used for cross-thread wakeups. sigset_t signalFdSigset; // Signal mask as currently set on the signalFd. Tracked so we can detect whether or not it // needs updating. bool doEpollWait(int timeout); #else class PollContext; FdObserver* observersHead = nullptr; FdObserver** observersTail = &observersHead; #if KJ_USE_PIPE_FOR_WAKEUP AutoCloseFd wakePipeIn; AutoCloseFd wakePipeOut; #else unsigned long long threadId; // actually pthread_t #endif #endif struct ChildSet; Maybe> childSet; }; class UnixEventPort::FdObserver { // Object which watches a file descriptor to determine when it is readable or writable. // // For listen sockets, "readable" means that there is a connection to accept(). For everything // else, it means that read() (or recv()) will return data. // // The presence of out-of-band data should NOT fire this event. However, the event may // occasionally fire spuriously (when there is actually no data to read), and one thing that can // cause such spurious events is the arrival of OOB data on certain platforms whose event // interfaces fail to distinguish between regular and OOB data (e.g. Mac OSX). // // WARNING: The exact behavior of this class differs across systems, since event interfaces // vary wildly. Be sure to read the documentation carefully and avoid depending on unspecified // behavior. If at all possible, use the higher-level AsyncInputStream interface instead. public: enum Flags { OBSERVE_READ = 1, OBSERVE_WRITE = 2, OBSERVE_URGENT = 4, OBSERVE_READ_WRITE = OBSERVE_READ | OBSERVE_WRITE }; FdObserver(UnixEventPort& eventPort, int fd, uint flags); // Begin watching the given file descriptor for readability. Only one ReadObserver may exist // for a given file descriptor at a time. ~FdObserver() noexcept(false); KJ_DISALLOW_COPY(FdObserver); Promise whenBecomesReadable(); // Resolves the next time the file descriptor transitions from having no data to read to having // some data to read. // // KJ uses "edge-triggered" event notification whenever possible. As a result, it is an error // to call this method when there is already data in the read buffer which has been there since // prior to the last turn of the event loop or prior to creation FdWatcher. In this case, it is // unspecified whether the promise will ever resolve -- it depends on the underlying event // mechanism being used. // // In order to avoid this problem, make sure that you only call `whenBecomesReadable()` // only at times when you know the buffer is empty. You know this for sure when one of the // following happens: // * read() or recv() fails with EAGAIN or EWOULDBLOCK. (You MUST have non-blocking mode // enabled on the fd!) // * The file descriptor is a regular byte-oriented object (like a socket or pipe), // read() or recv() returns fewer than the number of bytes requested, and `atEndHint()` // returns false. This can only happen if the buffer is empty but EOF is not reached. (Note, // though, that for record-oriented file descriptors like Linux's inotify interface, this // rule does not hold, because it could simply be that the next record did not fit into the // space available.) // // It is an error to call `whenBecomesReadable()` again when the promise returned previously // has not yet resolved. If you do this, the previous promise may throw an exception. inline Maybe atEndHint() { return atEnd; } // Returns true if the event system has indicated that EOF has been received. There may still // be data in the read buffer, but once that is gone, there's nothing left. // // Returns false if the event system has indicated that EOF had NOT been received as of the // last turn of the event loop. // // Returns nullptr if the event system does not know whether EOF has been reached. In this // case, the only way to know for sure is to call read() or recv() and check if it returns // zero. // // This hint may be useful as an optimization to avoid an unnecessary system call. Promise whenBecomesWritable(); // Resolves the next time the file descriptor transitions from having no space available in the // write buffer to having some space available. // // KJ uses "edge-triggered" event notification whenever possible. As a result, it is an error // to call this method when there is already space in the write buffer which has been there // since prior to the last turn of the event loop or prior to creation FdWatcher. In this case, // it is unspecified whether the promise will ever resolve -- it depends on the underlying // event mechanism being used. // // In order to avoid this problem, make sure that you only call `whenBecomesWritable()` // only at times when you know the buffer is full. You know this for sure when one of the // following happens: // * write() or send() fails with EAGAIN or EWOULDBLOCK. (You MUST have non-blocking mode // enabled on the fd!) // * write() or send() succeeds but accepts fewer than the number of bytes provided. This can // only happen if the buffer is full. // // It is an error to call `whenBecomesWritable()` again when the promise returned previously // has not yet resolved. If you do this, the previous promise may throw an exception. Promise whenUrgentDataAvailable(); // Resolves the next time the file descriptor's read buffer contains "urgent" data. // // The conditions for availability of urgent data are specific to the file descriptor's // underlying implementation. // // It is an error to call `whenUrgentDataAvailable()` again when the promise returned previously // has not yet resolved. If you do this, the previous promise may throw an exception. // // WARNING: This has some known weird behavior on macOS. See // https://github.com/sandstorm-io/capnproto/issues/374. Promise whenWriteDisconnected(); // Resolves when poll() on the file descriptor reports POLLHUP or POLLERR. private: UnixEventPort& eventPort; int fd; uint flags; kj::Maybe>> readFulfiller; kj::Maybe>> writeFulfiller; kj::Maybe>> urgentFulfiller; kj::Maybe>> hupFulfiller; // Replaced each time `whenBecomesReadable()` or `whenBecomesWritable()` is called. Reverted to // null every time an event is fired. Maybe atEnd; void fire(short events); #if !KJ_USE_EPOLL FdObserver* next; FdObserver** prev; // Linked list of observers which currently have a non-null readFulfiller or writeFulfiller. // If `prev` is null then the observer is not currently in the list. short getEventMask(); #endif friend class UnixEventPort; }; } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/async-win32.h0000644000175000017500000002347213650101756020405 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #if !_WIN32 #error "This file is Windows-specific. On Unix, include async-unix.h instead." #endif #include "async.h" #include "timer.h" #include "io.h" #include #include // Include windows.h as lean as possible. (If you need more of the Windows API for your app, // #include windows.h yourself before including this header.) #define WIN32_LEAN_AND_MEAN 1 #define NOSERVICE 1 #define NOMCX 1 #define NOIME 1 #include #include "windows-sanity.h" namespace kj { class Win32EventPort: public EventPort { // Abstract base interface for EventPorts that can listen on Win32 event types. Due to the // absurd complexity of the Win32 API, it's not possible to standardize on a single // implementation of EventPort. In particular, there is no way for a single thread to use I/O // completion ports (the most efficient way of handling I/O) while at the same time waiting for // signalable handles or UI messages. // // Note that UI messages are not supported at all by this interface because the message queue // is implemented by user32.dll and we want libkj to depend only on kernel32.dll. A separate // compat library could provide a Win32EventPort implementation that works with the UI message // queue. public: // --------------------------------------------------------------------------- // overlapped I/O struct IoResult { DWORD errorCode; DWORD bytesTransferred; }; class IoOperation { public: virtual LPOVERLAPPED getOverlapped() = 0; // Gets the OVERLAPPED structure to pass to the Win32 I/O call. Do NOT modify it; just pass it // on. virtual Promise onComplete() = 0; // After making the Win32 call, if the return value indicates that the operation was // successfully queued (i.e. the completion event will definitely occur), call this to wait // for completion. // // You MUST call this if the operation was successfully queued, and you MUST NOT call this // otherwise. If the Win32 call failed (without queuing any operation or event) then you should // simply drop the IoOperation object. // // Dropping the returned Promise cancels the operation via Win32's CancelIoEx(). The destructor // will wait for the cancellation to complete, such that after dropping the proimse it is safe // to free the buffer that the operation was reading from / writing to. // // You may safely drop the `IoOperation` while still waiting for this promise. You may not, // however, drop the `IoObserver`. }; class IoObserver { public: virtual Own newOperation(uint64_t offset) = 0; // Begin an I/O operation. For file operations, `offset` is the offset within the file at // which the operation will start. For stream operations, `offset` is ignored. }; virtual Own observeIo(HANDLE handle) = 0; // Given a handle which supports overlapped I/O, arrange to receive I/O completion events via // this EventPort. // // Different Win32EventPort implementations may handle this in different ways, such as by using // completion routines (APCs) or by using I/O completion ports. The caller should not assume // any particular technique. // // WARNING: It is only safe to call observeIo() on a particular handle once during its lifetime. // You cannot observe the same handle from multiple Win32EventPorts, even if not at the same // time. This is because the Win32 API provides no way to disassociate a handle from an I/O // completion port once it is associated. // --------------------------------------------------------------------------- // signalable handles // // Warning: Due to limitations in the Win32 API, implementations of EventPort may be forced to // spawn additional threads to wait for signaled objects. This is necessary if the EventPort // implementation is based on I/O completion ports, or if you need to wait on more than 64 // handles at once. class SignalObserver { public: virtual Promise onSignaled() = 0; // Returns a promise that completes the next time the handle enters the signaled state. // // Depending on the type of handle, the handle may automatically be reset to a non-signaled // state before the promise resolves. The underlying implementaiton uses WaitForSingleObject() // or an equivalent wait call, so check the documentation for that to understand the semantics. // // If the handle is a mutex and it is abandoned without being unlocked, the promise breaks with // an exception. virtual Promise onSignaledOrAbandoned() = 0; // Like onSignaled(), but instead of throwing when a mutex is abandoned, resolves to `true`. // Resolves to `false` for non-abandoned signals. }; virtual Own observeSignalState(HANDLE handle) = 0; // Given a handle that supports waiting for it to become "signaled" via WaitForSingleObject(), // return an object that can wait for this state using the EventPort. // --------------------------------------------------------------------------- // APCs virtual void allowApc() = 0; // If this is ever called, the Win32EventPort will switch modes so that APCs can be scheduled // on the thread, e.g. through the Win32 QueueUserAPC() call. In the future, this may be enabled // by default. However, as of this writing, Wine does not support the necessary // GetQueuedCompletionStatusEx() call, thus allowApc() breaks Wine support. (Tested on Wine // 1.8.7.) // // If the event port implementation can't support APCs for some reason, this throws. // --------------------------------------------------------------------------- // time virtual Timer& getTimer() = 0; }; class Win32WaitObjectThreadPool { // Helper class that implements Win32EventPort::observeSignalState() by spawning additional // threads as needed to perform the actual waiting. // // This class is intended to be used to assist in building Win32EventPort implementations. public: Win32WaitObjectThreadPool(uint mainThreadCount = 0); // `mainThreadCount` indicates the number of objects the main thread is able to listen on // directly. Typically this would be zero (e.g. if the main thread watches an I/O completion // port) or MAXIMUM_WAIT_OBJECTS (e.g. if the main thread is a UI thread but can use // MsgWaitForMultipleObjectsEx() to wait on some handles at the same time as messages). Own observeSignalState(HANDLE handle); // Implemetns Win32EventPort::observeSignalState(). uint prepareMainThreadWait(HANDLE* handles[]); // Call immediately before invoking WaitForMultipleObjects() or similar in the main thread. // Fills in `handles` with the handle pointers to wait on, and returns the number of handles // in this array. (The array should be allocated to be at least the size passed to the // constructor). // // There's no need to call this if `mainThreadCount` as passed to the constructor was zero. bool finishedMainThreadWait(DWORD returnCode); // Call immediately after invoking WaitForMultipleObjects() or similar in the main thread, // passing the value returend by that call. Returns true if the event indicated by `returnCode` // has been handled (i.e. it was WAIT_OBJECT_n or WAIT_ABANDONED_n where n is in-range for the // last call to prepareMainThreadWait()). }; class Win32IocpEventPort final: public Win32EventPort { // An EventPort implementation which uses Windows I/O completion ports to listen for events. // // With this implementation, observeSignalState() requires spawning a separate thread. public: Win32IocpEventPort(); ~Win32IocpEventPort() noexcept(false); // implements EventPort ------------------------------------------------------ bool wait() override; bool poll() override; void wake() const override; // implements Win32IocpEventPort --------------------------------------------- Own observeIo(HANDLE handle) override; Own observeSignalState(HANDLE handle) override; Timer& getTimer() override { return timerImpl; } void allowApc() override { isAllowApc = true; } private: class IoPromiseAdapter; class IoOperationImpl; class IoObserverImpl; const MonotonicClock& clock; AutoCloseHandle iocp; AutoCloseHandle thread; Win32WaitObjectThreadPool waitThreads; TimerImpl timerImpl; mutable std::atomic sentWake {false}; bool isAllowApc = false; void waitIocp(DWORD timeoutMs); // Wait on the I/O completion port for up to timeoutMs and pump events. Does not advance the // timer; caller must do that. bool receivedWake(); static AutoCloseHandle newIocpHandle(); static AutoCloseHandle openCurrentThread(); }; } // namespace kj capnproto-c++-0.8.0/src/kj/async-io.h0000644000175000017500000011732313650101756020051 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "async.h" #include "function.h" #include "thread.h" #include "timer.h" KJ_BEGIN_HEADER struct sockaddr; namespace kj { #if _WIN32 class Win32EventPort; class AutoCloseHandle; #else class UnixEventPort; #endif class AutoCloseFd; class NetworkAddress; class AsyncOutputStream; class AsyncIoStream; // ======================================================================================= // Streaming I/O class AsyncInputStream { // Asynchronous equivalent of InputStream (from io.h). public: virtual Promise read(void* buffer, size_t minBytes, size_t maxBytes); virtual Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0; Promise read(void* buffer, size_t bytes); virtual Maybe tryGetLength(); // Get the remaining number of bytes that will be produced by this stream, if known. // // This is used e.g. to fill in the Content-Length header of an HTTP message. If unknown, the // HTTP implementation may need to fall back to Transfer-Encoding: chunked. // // The default implementation always returns null. virtual Promise pumpTo( AsyncOutputStream& output, uint64_t amount = kj::maxValue); // Read `amount` bytes from this stream (or to EOF) and write them to `output`, returning the // total bytes actually pumped (which is only less than `amount` if EOF was reached). // // Override this if your stream type knows how to pump itself to certain kinds of output // streams more efficiently than via the naive approach. You can use // kj::dynamicDowncastIfAvailable() to test for stream types you recognize, and if none match, // delegate to the default implementation. // // The default implementation first tries calling output.tryPumpFrom(), but if that fails, it // performs a naive pump by allocating a buffer and reading to it / writing from it in a loop. Promise> readAllBytes(uint64_t limit = kj::maxValue); Promise readAllText(uint64_t limit = kj::maxValue); // Read until EOF and return as one big byte array or string. Throw an exception if EOF is not // seen before reading `limit` bytes. // // To prevent runaway memory allocation, consider using a more conservative value for `limit` than // the default, particularly on untrusted data streams which may never see EOF. }; class AsyncOutputStream { // Asynchronous equivalent of OutputStream (from io.h). public: virtual Promise write(const void* buffer, size_t size) KJ_WARN_UNUSED_RESULT = 0; virtual Promise write(ArrayPtr> pieces) KJ_WARN_UNUSED_RESULT = 0; virtual Maybe> tryPumpFrom( AsyncInputStream& input, uint64_t amount = kj::maxValue); // Implements double-dispatch for AsyncInputStream::pumpTo(). // // This method should only be called from within an implementation of pumpTo(). // // This method examines the type of `input` to find optimized ways to pump data from it to this // output stream. If it finds one, it performs the pump. Otherwise, it returns null. // // The default implementation always returns null. virtual Promise whenWriteDisconnected() = 0; // Returns a promise that resolves when the stream has become disconnected such that new write()s // will fail with a DISCONNECTED exception. This is particularly useful, for example, to cancel // work early when it is detected that no one will receive the result. // // Note that not all streams are able to detect this condition without actually performing a // write(); such stream implementations may return a promise that never resolves. (In particular, // as of this writing, whenWriteDisconnected() is not implemented on Windows. Also, for TCP // streams, not all disconnects are detectable -- a power or network failure may lead the // connection to hang forever, or until configured socket options lead to a timeout.) // // Unlike most other asynchronous stream methods, it is safe to call whenWriteDisconnected() // multiple times without canceling the previous promises. }; class AsyncIoStream: public AsyncInputStream, public AsyncOutputStream { // A combination input and output stream. public: virtual void shutdownWrite() = 0; // Cleanly shut down just the write end of the stream, while keeping the read end open. virtual void abortRead() {} // Similar to shutdownWrite, but this will shut down the read end of the stream, and should only // be called when an error has occurred. virtual void getsockopt(int level, int option, void* value, uint* length); virtual void setsockopt(int level, int option, const void* value, uint length); // Corresponds to getsockopt() and setsockopt() syscalls. Will throw an "unimplemented" exception // if the stream is not a socket or the option is not appropriate for the socket type. The // default implementations always throw "unimplemented". virtual void getsockname(struct sockaddr* addr, uint* length); virtual void getpeername(struct sockaddr* addr, uint* length); // Corresponds to getsockname() and getpeername() syscalls. Will throw an "unimplemented" // exception if the stream is not a socket. The default implementations always throw // "unimplemented". // // Note that we don't provide methods that return NetworkAddress because it usually wouldn't // be useful. You can't connect() to or listen() on these addresses, obviously, because they are // ephemeral addresses for a single connection. }; class AsyncCapabilityStream: public AsyncIoStream { // An AsyncIoStream that also allows transmitting new stream objects and file descirptors // (capabilities, in the object-capability model sense), in addition to bytes. // // Capabilities can be attached to bytes when they are written. On the receiving end, the read() // that receives the first byte of such a message will also receive the capabilities. // // Note that AsyncIoStream's regular byte-oriented methods can be used on AsyncCapabilityStream, // with the effect of silently dropping any capabilities attached to the respective bytes. E.g. // using `AsyncIoStream::tryRead()` to read bytes that had been sent with `writeWithFds()` will // silently drop the FDs (closing them if appropriate). Also note that puming a stream with // `pumpTo()` always drops all capabilities attached to the pumped data. (TODO(someday): Do we // want a version of pumpTo() that preserves capabilities?) // // On Unix, KJ provides an implementation based on Unix domain sockets and file descriptor // passing via SCM_RIGHTS. Due to the nature of SCM_RIGHTS, if the application accidentally // read()s when it should have called receiveStream(), it will observe a NUL byte in the data // and the capability will be discarded. Of course, an application should not depend on this // behavior; it should avoid read()ing through a capability. // // KJ does not provide any inter-process implementation of this type on Windows, as there's no // obvious implementation there. Handle passing on Windows requires at least one of the processes // involved to have permission to modify the other's handle table, which is effectively full // control. Handle passing between mutually non-trusting processes would require a trusted // broker process to facilitate. One could possibly implement this type in terms of such a // broker, or in terms of direct handle passing if at least one process trusts the other. public: virtual Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) = 0; Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds); // Write some data to the stream with some file descriptors attached to it. // // The maximum number of FDs that can be sent at a time is usually subject to an OS-imposed // limit. On Linux, this is 253. In practice, sending more than a handful of FDs at once is // probably a bad idea. struct ReadResult { size_t byteCount; size_t capCount; }; virtual Promise tryReadWithFds(void* buffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) = 0; // Read data from the stream that may have file descriptors attached. Any attached descriptors // will be placed in `fdBuffer`. If multiple bundles of FDs are encountered in the course of // reading the amount of data requested by minBytes/maxBytes, then they will be concatenated. If // more FDs are received than fit in the buffer, then the excess will be discarded and closed -- // this behavior, while ugly, is important to defend against denial-of-service attacks that may // fill up the FD table with garbage. Applications must think carefully about how many FDs they // really need to receive at once and set a well-defined limit. virtual Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) = 0; virtual Promise tryReadWithStreams( void* buffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) = 0; // Like above, but passes AsyncCapabilityStream objects. The stream implementations must be from // the same AsyncIoProvider. // --------------------------------------------------------------------------- // Helpers for sending individual capabilities. // // These are equivalent to the above methods with the constraint that only one FD is // sent/received at a time and the corresponding data is a single zero-valued byte. Promise> receiveStream(); Promise>> tryReceiveStream(); Promise sendStream(Own stream); // Transfer a single stream. Promise receiveFd(); Promise> tryReceiveFd(); Promise sendFd(int fd); // Transfer a single raw file descriptor. }; struct OneWayPipe { // A data pipe with an input end and an output end. (Typically backed by pipe() system call.) Own in; Own out; }; OneWayPipe newOneWayPipe(kj::Maybe expectedLength = nullptr); // Constructs a OneWayPipe that operates in-process. The pipe does not do any buffering -- it waits // until both a read() and a write() call are pending, then resolves both. // // If `expectedLength` is non-null, then the pipe will be expected to transmit exactly that many // bytes. The input end's `tryGetLength()` will return the number of bytes left. struct TwoWayPipe { // A data pipe that supports sending in both directions. Each end's output sends data to the // other end's input. (Typically backed by socketpair() system call.) Own ends[2]; }; TwoWayPipe newTwoWayPipe(); // Constructs a TwoWayPipe that operates in-process. The pipe does not do any buffering -- it waits // until both a read() and a write() call are pending, then resolves both. struct CapabilityPipe { // Like TwoWayPipe but allowing capability-passing. Own ends[2]; }; CapabilityPipe newCapabilityPipe(); // Like newTwoWayPipe() but creates a capability pipe. // // The requirement of `writeWithStreams()` that "The stream implementations must be from the same // AsyncIoProvider." does not apply to this pipe; any kind of AsyncCapabilityStream implementation // is supported. // // This implementation does not know how to convert streams to FDs or vice versa; if you write FDs // you must read FDs, and if you write streams you must read streams. struct Tee { // Two AsyncInputStreams which each read the same data from some wrapped inner AsyncInputStream. Own branches[2]; }; Tee newTee(Own input, uint64_t limit = kj::maxValue); // Constructs a Tee that operates in-process. The tee buffers data if any read or pump operations is // called on one of the two input ends. If a read or pump operation is subsequently called on the // other input end, the buffered data is consumed. // // `pumpTo()` operations on the input ends will proactively read from the inner stream and block // while writing to the output stream. While one branch has an active `pumpTo()` operation, any // `tryRead()` operation on the other branch will not be allowed to read faster than allowed by the // pump's backpressure. (In other words, it will never cause buffering on the pump.) Similarly, if // there are `pumpTo()` operations active on both branches, the greater of the two backpressures is // respected -- the two pumps progress in lockstep, and there is no buffering. // // At no point will a branch's buffer be allowed to grow beyond `limit` bytes. If the buffer would // grow beyond the limit, an exception is generated, which both branches see once they have // exhausted their buffers. // // It is recommended that you use a more conservative value for `limit` than the default. Own newPromisedStream(Promise> promise); Own newPromisedStream(Promise> promise); // Constructs an Async*Stream which waits for a promise to resolve, then forwards all calls to the // promised stream. class ConnectionReceiver { // Represents a server socket listening on a port. public: virtual Promise> accept() = 0; // Accept the next incoming connection. virtual uint getPort() = 0; // Gets the port number, if applicable (i.e. if listening on IP). This is useful if you didn't // specify a port when constructing the NetworkAddress -- one will have been assigned // automatically. virtual void getsockopt(int level, int option, void* value, uint* length); virtual void setsockopt(int level, int option, const void* value, uint length); virtual void getsockname(struct sockaddr* addr, uint* length); // Same as the methods of AsyncIoStream. }; // ======================================================================================= // Datagram I/O class AncillaryMessage { // Represents an ancillary message (aka control message) received using the recvmsg() system // call (or equivalent). Most apps will not use this. public: inline AncillaryMessage(int level, int type, ArrayPtr data); AncillaryMessage() = default; inline int getLevel() const; // Originating protocol / socket level. inline int getType() const; // Protocol-specific message type. template inline Maybe as(); // Interpret the ancillary message as the given struct type. Most ancillary messages are some // sort of struct, so this is a convenient way to access it. Returns nullptr if the message // is smaller than the struct -- this can happen if the message was truncated due to // insufficient ancillary buffer space. template inline ArrayPtr asArray(); // Interpret the ancillary message as an array of items. If the message size does not evenly // divide into elements of type T, the remainder is discarded -- this can happen if the message // was truncated due to insufficient ancillary buffer space. private: int level; int type; ArrayPtr data; // Message data. In most cases you should use `as()` or `asArray()`. }; class DatagramReceiver { // Class encapsulating the recvmsg() system call. You must specify the DatagramReceiver's // capacity in advance; if a received packet is larger than the capacity, it will be truncated. public: virtual Promise receive() = 0; // Receive a new message, overwriting this object's content. // // receive() may reuse the same buffers for content and ancillary data with each call. template struct MaybeTruncated { T value; bool isTruncated; // True if the Receiver's capacity was insufficient to receive the value and therefore the // value is truncated. }; virtual MaybeTruncated> getContent() = 0; // Get the content of the datagram. virtual MaybeTruncated> getAncillary() = 0; // Ancilarry messages received with the datagram. See the recvmsg() system call and the cmsghdr // struct. Most apps don't need this. // // If the returned value is truncated, then the last message in the array may itself be // truncated, meaning its as() method will return nullptr or its asArray() method will // return fewer elements than expected. Truncation can also mean that additional messages were // available but discarded. virtual NetworkAddress& getSource() = 0; // Get the datagram sender's address. struct Capacity { size_t content = 8192; // How much space to allocate for the datagram content. If a datagram is received that is // larger than this, it will be truncated, with no way to recover the tail. size_t ancillary = 0; // How much space to allocate for ancillary messages. As with content, if the ancillary data // is larger than this, it will be truncated. }; }; class DatagramPort { public: virtual Promise send(const void* buffer, size_t size, NetworkAddress& destination) = 0; virtual Promise send(ArrayPtr> pieces, NetworkAddress& destination) = 0; virtual Own makeReceiver( DatagramReceiver::Capacity capacity = DatagramReceiver::Capacity()) = 0; // Create a new `Receiver` that can be used to receive datagrams. `capacity` specifies how much // space to allocate for the received message. The `DatagramPort` must outlive the `Receiver`. virtual uint getPort() = 0; // Gets the port number, if applicable (i.e. if listening on IP). This is useful if you didn't // specify a port when constructing the NetworkAddress -- one will have been assigned // automatically. virtual void getsockopt(int level, int option, void* value, uint* length); virtual void setsockopt(int level, int option, const void* value, uint length); // Same as the methods of AsyncIoStream. }; // ======================================================================================= // Networks class NetworkAddress { // Represents a remote address to which the application can connect. public: virtual Promise> connect() = 0; // Make a new connection to this address. // // The address must not be a wildcard ("*"). If it is an IP address, it must have a port number. virtual Own listen() = 0; // Listen for incoming connections on this address. // // The address must be local. virtual Own bindDatagramPort(); // Open this address as a datagram (e.g. UDP) port. // // The address must be local. virtual Own clone() = 0; // Returns an equivalent copy of this NetworkAddress. virtual String toString() = 0; // Produce a human-readable string which hopefully can be passed to Network::parseAddress() // to reproduce this address, although whether or not that works of course depends on the Network // implementation. This should be called only to display the address to human users, who will // hopefully know what they are able to do with it. }; class Network { // Factory for NetworkAddress instances, representing the network services offered by the // operating system. // // This interface typically represents broad authority, and well-designed code should limit its // use to high-level startup code and user interaction. Low-level APIs should accept // NetworkAddress instances directly and work from there, if at all possible. public: virtual Promise> parseAddress(StringPtr addr, uint portHint = 0) = 0; // Construct a network address from a user-provided string. The format of the address // strings is not specified at the API level, and application code should make no assumptions // about them. These strings should always be provided by humans, and said humans will know // what format to use in their particular context. // // `portHint`, if provided, specifies the "standard" IP port number for the application-level // service in play. If the address turns out to be an IP address (v4 or v6), and it lacks a // port number, this port will be used. If `addr` lacks a port number *and* `portHint` is // omitted, then the returned address will only support listen() and bindDatagramPort() // (not connect()), and an unused port will be chosen each time one of those methods is called. virtual Own getSockaddr(const void* sockaddr, uint len) = 0; // Construct a network address from a legacy struct sockaddr. virtual Own restrictPeers( kj::ArrayPtr allow, kj::ArrayPtr deny = nullptr) KJ_WARN_UNUSED_RESULT = 0; // Constructs a new Network instance wrapping this one which restricts which peer addresses are // permitted (both for outgoing and incoming connections). // // Communication will be allowed only with peers whose addresses match one of the patterns // specified in the `allow` array. If a `deny` array is specified, then any address which matches // a pattern in `deny` and *does not* match any more-specific pattern in `allow` will also be // denied. // // The syntax of address patterns depends on the network, except that three special patterns are // defined for all networks: // - "private": Matches network addresses that are reserved by standards for private networks, // such as "10.0.0.0/8" or "192.168.0.0/16". This is a superset of "local". // - "public": Opposite of "private". // - "local": Matches network addresses that are defined by standards to only be accessible from // the local machine, such as "127.0.0.0/8" or Unix domain addresses. // - "network": Opposite of "local". // // For the standard KJ network implementation, the following patterns are also recognized: // - Network blocks specified in CIDR notation (ipv4 and ipv6), such as "192.0.2.0/24" or // "2001:db8::/32". // - "unix" to match all Unix domain addresses. (In the future, we may support specifying a // glob.) // - "unix-abstract" to match Linux's "abstract unix domain" addresses. (In the future, we may // support specifying a glob.) // // Network restrictions apply *after* DNS resolution (otherwise they'd be useless). // // It is legal to parseAddress() a restricted address. An exception won't be thrown until // connect() is called. // // It's possible to listen() on a restricted address. However, connections will only be accepted // from non-restricted addresses; others will be dropped. If a particular listen address has no // valid peers (e.g. because it's a unix socket address and unix sockets are not allowed) then // listen() may throw (or may simply never receive any connections). // // Examples: // // auto restricted = network->restrictPeers({"public"}); // // Allows connections only to/from public internet addresses. Use this when connecting to an // address specified by a third party that is not trusted and is not themselves already on your // private network. // // auto restricted = network->restrictPeers({"private"}); // // Allows connections only to/from the private network. Use this on the server side to reject // connections from the public internet. // // auto restricted = network->restrictPeers({"192.0.2.0/24"}, {"192.0.2.3/32"}); // // Allows connections only to/from 192.0.2.*, except 192.0.2.3 which is blocked. // // auto restricted = network->restrictPeers({"10.0.0.0/8", "10.1.2.3/32"}, {"10.1.2.0/24"}); // // Allows connections to/from 10.*.*.*, with the exception of 10.1.2.* (which is denied), with an // exception to the exception of 10.1.2.3 (which is allowed, because it is matched by an allow // rule that is more specific than the deny rule). }; // ======================================================================================= // I/O Provider class AsyncIoProvider { // Class which constructs asynchronous wrappers around the operating system's I/O facilities. // // Generally, the implementation of this interface must integrate closely with a particular // `EventLoop` implementation. Typically, the EventLoop implementation itself will provide // an AsyncIoProvider. public: virtual OneWayPipe newOneWayPipe() = 0; // Creates an input/output stream pair representing the ends of a one-way pipe (e.g. created with // the pipe(2) system call). virtual TwoWayPipe newTwoWayPipe() = 0; // Creates two AsyncIoStreams representing the two ends of a two-way pipe (e.g. created with // socketpair(2) system call). Data written to one end can be read from the other. virtual CapabilityPipe newCapabilityPipe(); // Creates two AsyncCapabilityStreams representing the two ends of a two-way capability pipe. // // The default implementation throws an unimplemented exception. In particular this is not // implemented by the default AsyncIoProvider on Windows, since Windows lacks any sane way to // pass handles over a stream. virtual Network& getNetwork() = 0; // Creates a new `Network` instance representing the networks exposed by the operating system. // // DO NOT CALL THIS except at the highest levels of your code, ideally in the main() function. If // you call this from low-level code, then you are preventing higher-level code from injecting an // alternative implementation. Instead, if your code needs to use network functionality, it // should ask for a `Network` as a constructor or method parameter, so that higher-level code can // chose what implementation to use. The system network is essentially a singleton. See: // http://www.object-oriented-security.org/lets-argue/singletons // // Code that uses the system network should not make any assumptions about what kinds of // addresses it will parse, as this could differ across platforms. String addresses should come // strictly from the user, who will know how to write them correctly for their system. // // With that said, KJ currently supports the following string address formats: // - IPv4: "1.2.3.4", "1.2.3.4:80" // - IPv6: "1234:5678::abcd", "[1234:5678::abcd]:80" // - Local IP wildcard (covers both v4 and v6): "*", "*:80" // - Symbolic names: "example.com", "example.com:80", "example.com:http", "1.2.3.4:http" // - Unix domain: "unix:/path/to/socket" struct PipeThread { // A combination of a thread and a two-way pipe that communicates with that thread. // // The fields are intentionally ordered so that the pipe will be destroyed (and therefore // disconnected) before the thread is destroyed (and therefore joined). Thus if the thread // arranges to exit when it detects disconnect, destruction should be clean. Own thread; Own pipe; }; virtual PipeThread newPipeThread( Function startFunc) = 0; // Create a new thread and set up a two-way pipe (socketpair) which can be used to communicate // with it. One end of the pipe is passed to the thread's start function and the other end of // the pipe is returned. The new thread also gets its own `AsyncIoProvider` instance and will // already have an active `EventLoop` when `startFunc` is called. // // TODO(someday): I'm not entirely comfortable with this interface. It seems to be doing too // much at once but I'm not sure how to cleanly break it down. virtual Timer& getTimer() = 0; // Returns a `Timer` based on real time. Time does not pass while event handlers are running -- // it only updates when the event loop polls for system events. This means that calling `now()` // on this timer does not require a system call. // // This timer is not affected by changes to the system date. It is unspecified whether the timer // continues to count while the system is suspended. }; class LowLevelAsyncIoProvider { // Similar to `AsyncIoProvider`, but represents a lower-level interface that may differ on // different operating systems. You should prefer to use `AsyncIoProvider` over this interface // whenever possible, as `AsyncIoProvider` is portable and friendlier to dependency-injection. // // On Unix, this interface can be used to import native file descriptors into the async framework. // Different implementations of this interface might work on top of different event handling // primitives, such as poll vs. epoll vs. kqueue vs. some higher-level event library. // // On Windows, this interface can be used to import native SOCKETs into the async framework. // Different implementations of this interface might work on top of different event handling // primitives, such as I/O completion ports vs. completion routines. public: enum Flags { // Flags controlling how to wrap a file descriptor. TAKE_OWNERSHIP = 1 << 0, // The returned object should own the file descriptor, automatically closing it when destroyed. // The close-on-exec flag will be set on the descriptor if it is not already. // // If this flag is not used, then the file descriptor is not automatically closed and the // close-on-exec flag is not modified. #if !_WIN32 ALREADY_CLOEXEC = 1 << 1, // Indicates that the close-on-exec flag is known already to be set, so need not be set again. // Only relevant when combined with TAKE_OWNERSHIP. // // On Linux, all system calls which yield new file descriptors have flags or variants which // set the close-on-exec flag immediately. Unfortunately, other OS's do not. ALREADY_NONBLOCK = 1 << 2 // Indicates that the file descriptor is known already to be in non-blocking mode, so the flag // need not be set again. Otherwise, all wrap*Fd() methods will enable non-blocking mode // automatically. // // On Linux, all system calls which yield new file descriptors have flags or variants which // enable non-blocking mode immediately. Unfortunately, other OS's do not. #endif }; #if _WIN32 typedef uintptr_t Fd; typedef AutoCloseHandle OwnFd; // On Windows, the `fd` parameter to each of these methods must be a SOCKET, and must have the // flag WSA_FLAG_OVERLAPPED (which socket() uses by default, but WSASocket() wants you to specify // explicitly). #else typedef int Fd; typedef AutoCloseFd OwnFd; // On Unix, any arbitrary file descriptor is supported. #endif virtual Own wrapInputFd(Fd fd, uint flags = 0) = 0; // Create an AsyncInputStream wrapping a file descriptor. // // `flags` is a bitwise-OR of the values of the `Flags` enum. virtual Own wrapOutputFd(Fd fd, uint flags = 0) = 0; // Create an AsyncOutputStream wrapping a file descriptor. // // `flags` is a bitwise-OR of the values of the `Flags` enum. virtual Own wrapSocketFd(Fd fd, uint flags = 0) = 0; // Create an AsyncIoStream wrapping a socket file descriptor. // // `flags` is a bitwise-OR of the values of the `Flags` enum. #if !_WIN32 virtual Own wrapUnixSocketFd(Fd fd, uint flags = 0); // Like wrapSocketFd() but also support capability passing via SCM_RIGHTS. The socket must be // a Unix domain socket. // // The default implementation throws UNIMPLEMENTED, for backwards-compatibility with // LowLevelAsyncIoProvider implementations written before this method was added. #endif virtual Promise> wrapConnectingSocketFd( Fd fd, const struct sockaddr* addr, uint addrlen, uint flags = 0) = 0; // Create an AsyncIoStream wrapping a socket and initiate a connection to the given address. // The returned promise does not resolve until connection has completed. // // `flags` is a bitwise-OR of the values of the `Flags` enum. class NetworkFilter { public: virtual bool shouldAllow(const struct sockaddr* addr, uint addrlen) = 0; // Returns true if incoming connections or datagrams from the given peer should be accepted. // If false, they will be dropped. This is used to implement kj::Network::restrictPeers(). static NetworkFilter& getAllAllowed(); }; virtual Own wrapListenSocketFd( Fd fd, NetworkFilter& filter, uint flags = 0) = 0; inline Own wrapListenSocketFd(Fd fd, uint flags = 0) { return wrapListenSocketFd(fd, NetworkFilter::getAllAllowed(), flags); } // Create an AsyncIoStream wrapping a listen socket file descriptor. This socket should already // have had `bind()` and `listen()` called on it, so it's ready for `accept()`. // // `flags` is a bitwise-OR of the values of the `Flags` enum. virtual Own wrapDatagramSocketFd(Fd fd, NetworkFilter& filter, uint flags = 0); inline Own wrapDatagramSocketFd(Fd fd, uint flags = 0) { return wrapDatagramSocketFd(fd, NetworkFilter::getAllAllowed(), flags); } virtual Timer& getTimer() = 0; // Returns a `Timer` based on real time. Time does not pass while event handlers are running -- // it only updates when the event loop polls for system events. This means that calling `now()` // on this timer does not require a system call. // // This timer is not affected by changes to the system date. It is unspecified whether the timer // continues to count while the system is suspended. Own wrapInputFd(OwnFd&& fd, uint flags = 0); Own wrapOutputFd(OwnFd&& fd, uint flags = 0); Own wrapSocketFd(OwnFd&& fd, uint flags = 0); #if !_WIN32 Own wrapUnixSocketFd(OwnFd&& fd, uint flags = 0); #endif Promise> wrapConnectingSocketFd( OwnFd&& fd, const struct sockaddr* addr, uint addrlen, uint flags = 0); Own wrapListenSocketFd( OwnFd&& fd, NetworkFilter& filter, uint flags = 0); Own wrapListenSocketFd(OwnFd&& fd, uint flags = 0); Own wrapDatagramSocketFd(OwnFd&& fd, NetworkFilter& filter, uint flags = 0); Own wrapDatagramSocketFd(OwnFd&& fd, uint flags = 0); // Convenience wrappers which transfer ownership via AutoCloseFd (Unix) or AutoCloseHandle // (Windows). TAKE_OWNERSHIP will be implicitly added to `flags`. }; Own newAsyncIoProvider(LowLevelAsyncIoProvider& lowLevel); // Make a new AsyncIoProvider wrapping a `LowLevelAsyncIoProvider`. struct AsyncIoContext { Own lowLevelProvider; Own provider; WaitScope& waitScope; #if _WIN32 Win32EventPort& win32EventPort; #else UnixEventPort& unixEventPort; // TEMPORARY: Direct access to underlying UnixEventPort, mainly for waiting on signals. This // field will go away at some point when we have a chance to improve these interfaces. #endif }; AsyncIoContext setupAsyncIo(); // Convenience method which sets up the current thread with everything it needs to do async I/O. // The returned objects contain an `EventLoop` which is wrapping an appropriate `EventPort` for // doing I/O on the host system, so everything is ready for the thread to start making async calls // and waiting on promises. // // You would typically call this in your main() loop or in the start function of a thread. // Example: // // int main() { // auto ioContext = kj::setupAsyncIo(); // // // Now we can call an async function. // Promise textPromise = getHttp(*ioContext.provider, "http://example.com"); // // // And we can wait for the promise to complete. Note that you can only use `wait()` // // from the top level, not from inside a promise callback. // String text = textPromise.wait(ioContext.waitScope); // print(text); // return 0; // } // // WARNING: An AsyncIoContext can only be used in the thread and process that created it. In // particular, note that after a fork(), an AsyncIoContext created in the parent process will // not work correctly in the child, even if the parent ceases to use its copy. In particular // note that this means that server processes which daemonize themselves at startup must wait // until after daemonization to create an AsyncIoContext. // ======================================================================================= // Convenience adapters. class CapabilityStreamConnectionReceiver final: public ConnectionReceiver { // Trivial wrapper which allows an AsyncCapabilityStream to act as a ConnectionReceiver. accept() // calls receiveStream(). public: CapabilityStreamConnectionReceiver(AsyncCapabilityStream& inner) : inner(inner) {} Promise> accept() override; uint getPort() override; private: AsyncCapabilityStream& inner; }; class CapabilityStreamNetworkAddress final: public NetworkAddress { // Trivial wrapper which allows an AsyncCapabilityStream to act as a NetworkAddress. // // connect() is implemented by calling provider.newCapabilityPipe(), sending one end over the // original capability stream, and returning the other end. If `provider` is null, then the // global kj::newCapabilityPipe() will be used, but this ONLY works if `inner` itself is agnostic // to the type of streams it receives, e.g. because it was also created using // kj::NewCapabilityPipe(). // // listen().accept() is implemented by receiving new streams over the original stream. // // Note that clone() doesn't work (due to ownership issues) and toString() returns a static // string. public: CapabilityStreamNetworkAddress(kj::Maybe provider, AsyncCapabilityStream& inner) : provider(provider), inner(inner) {} Promise> connect() override; Own listen() override; Own clone() override; String toString() override; private: kj::Maybe provider; AsyncCapabilityStream& inner; }; // ======================================================================================= // inline implementation details inline AncillaryMessage::AncillaryMessage( int level, int type, ArrayPtr data) : level(level), type(type), data(data) {} inline int AncillaryMessage::getLevel() const { return level; } inline int AncillaryMessage::getType() const { return type; } template inline Maybe AncillaryMessage::as() { if (data.size() >= sizeof(T)) { return *reinterpret_cast(data.begin()); } else { return nullptr; } } template inline ArrayPtr AncillaryMessage::asArray() { return arrayPtr(reinterpret_cast(data.begin()), data.size() / sizeof(T)); } } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/main.h0000644000175000017500000004633013650101756017252 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "array.h" #include "string.h" #include "vector.h" #include "function.h" KJ_BEGIN_HEADER namespace kj { class ProcessContext { // Context for command-line programs. public: virtual StringPtr getProgramName() = 0; // Get argv[0] as passed to main(). KJ_NORETURN(virtual void exit()) = 0; // Indicates program completion. The program is considered successful unless `error()` was // called. Typically this exits with _Exit(), meaning that the stack is not unwound, buffers // are not flushed, etc. -- it is the responsibility of the caller to flush any buffers that // matter. However, an alternate context implementation e.g. for unit testing purposes could // choose to throw an exception instead. // // At first this approach may sound crazy. Isn't it much better to shut down cleanly? What if // you lose data? However, it turns out that if you look at each common class of program, _Exit() // is almost always preferable. Let's break it down: // // * Commands: A typical program you might run from the command line is single-threaded and // exits quickly and deterministically. Commands often use buffered I/O and need to flush // those buffers before exit. However, most of the work performed by destructors is not // flushing buffers, but rather freeing up memory, placing objects into freelists, and closing // file descriptors. All of this is irrelevant if the process is about to exit anyway, and // for a command that runs quickly, time wasted freeing heap space may make a real difference // in the overall runtime of a script. Meanwhile, it is usually easy to determine exactly what // resources need to be flushed before exit, and easy to tell if they are not being flushed // (because the command fails to produce the expected output). Therefore, it is reasonably // easy for commands to explicitly ensure all output is flushed before exiting, and it is // probably a good idea for them to do so anyway, because write failures should be detected // and handled. For commands, a good strategy is to allocate any objects that require clean // destruction on the stack, and allow them to go out of scope before the command exits. // Meanwhile, any resources which do not need to be cleaned up should be allocated as members // of the command's main class, whose destructor normally will not be called. // // * Interactive apps: Programs that interact with the user (whether they be graphical apps // with windows or console-based apps like emacs) generally exit only when the user asks them // to. Such applications may store large data structures in memory which need to be synced // to disk, such as documents or user preferences. However, relying on stack unwind or global // destructors as the mechanism for ensuring such syncing occurs is probably wrong. First of // all, it's 2013, and applications ought to be actively syncing changes to non-volatile // storage the moment those changes are made. Applications can crash at any time and a crash // should never lose data that is more than half a second old. Meanwhile, if a user actually // does try to close an application while unsaved changes exist, the application UI should // prompt the user to decide what to do. Such a UI mechanism is obviously too high level to // be implemented via destructors, so KJ's use of _Exit() shouldn't make a difference here. // // * Servers: A good server is fault-tolerant, prepared for the possibility that at any time // it could crash, the OS could decide to kill it off, or the machine it is running on could // just die. So, using _Exit() should be no problem. In fact, servers generally never even // call exit anyway; they are killed externally. // // * Batch jobs: A long-running batch job is something between a command and a server. It // probably knows exactly what needs to be flushed before exiting, and it probably should be // fault-tolerant. // // Meanwhile, regardless of program type, if you are adhering to KJ style, then the use of // _Exit() shouldn't be a problem anyway: // // * KJ style forbids global mutable state (singletons) in general and global constructors and // destructors in particular. Therefore, everything that could possibly need cleanup either // lives on the stack or is transitively owned by something living on the stack. // // * Calling exit() simply means "Don't clean up anything older than this stack frame.". If you // have resources that require cleanup before exit, make sure they are owned by stack frames // beyond the one that eventually calls exit(). To be as safe as possible, don't place any // state in your program's main class, and don't call exit() yourself. Then, runMainAndExit() // will do it, and the only thing on the stack at that time will be your main class, which // has no state anyway. // // TODO(someday): Perhaps we should use the new std::quick_exit(), so that at_quick_exit() is // available for those who really think they need it. Unfortunately, it is not yet available // on many platforms. virtual void warning(StringPtr message) = 0; // Print the given message to standard error. A newline is printed after the message if it // doesn't already have one. virtual void error(StringPtr message) = 0; // Like `warning()`, but also sets a flag indicating that the process has failed, and that when // it eventually exits it should indicate an error status. KJ_NORETURN(virtual void exitError(StringPtr message)) = 0; // Equivalent to `error(message)` followed by `exit()`. KJ_NORETURN(virtual void exitInfo(StringPtr message)) = 0; // Displays the given non-error message to the user and then calls `exit()`. This is used to // implement things like --help. virtual void increaseLoggingVerbosity() = 0; // Increase the level of detail produced by the debug logging system. `MainBuilder` invokes // this if the caller uses the -v flag. // TODO(someday): Add interfaces representing standard OS resources like the filesystem, so that // these things can be mocked out. }; class TopLevelProcessContext final: public ProcessContext { // A ProcessContext implementation appropriate for use at the actual entry point of a process // (as opposed to when you are trying to call a program's main function from within some other // program). This implementation writes errors to stderr, and its `exit()` method actually // calls the C `quick_exit()` function. public: explicit TopLevelProcessContext(StringPtr programName); struct CleanShutdownException { int exitCode; }; // If the environment variable KJ_CLEAN_SHUTDOWN is set, then exit() will actually throw this // exception rather than exiting. `kj::runMain()` catches this exception and returns normally. // This is useful primarily for testing purposes, to assist tools like memory leak checkers that // are easily confused by quick_exit(). StringPtr getProgramName() override; KJ_NORETURN(void exit() override); void warning(StringPtr message) override; void error(StringPtr message) override; KJ_NORETURN(void exitError(StringPtr message) override); KJ_NORETURN(void exitInfo(StringPtr message) override); void increaseLoggingVerbosity() override; private: StringPtr programName; bool cleanShutdown; bool hadErrors = false; }; typedef Function params)> MainFunc; int runMainAndExit(ProcessContext& context, MainFunc&& func, int argc, char* argv[]); // Runs the given main function and then exits using the given context. If an exception is thrown, // this will catch it, report it via the context and exit with an error code. // // Normally this function does not return, because returning would probably lead to wasting time // on cleanup when the process is just going to exit anyway. However, to facilitate memory leak // checkers and other tools that require a clean shutdown to do their job, if the environment // variable KJ_CLEAN_SHUTDOWN is set, the function will in fact return an exit code, which should // then be returned from main(). // // Most users will use the KJ_MAIN() macro rather than call this function directly. #define KJ_MAIN(MainClass) \ int main(int argc, char* argv[]) { \ ::kj::TopLevelProcessContext context(argv[0]); \ MainClass mainObject(context); \ return ::kj::runMainAndExit(context, mainObject.getMain(), argc, argv); \ } // Convenience macro for declaring a main function based on the given class. The class must have // a constructor that accepts a ProcessContext& and a method getMain() which returns // kj::MainFunc (probably building it using a MainBuilder). class MainBuilder { // Builds a main() function with nice argument parsing. As options and arguments are parsed, // corresponding callbacks are called, so that you never have to write a massive switch() // statement to interpret arguments. Additionally, this approach encourages you to write // main classes that have a reasonable API that can be used as an alternative to their // command-line interface. // // All StringPtrs passed to MainBuilder must remain valid until option parsing completes. The // assumption is that these strings will all be literals, making this an easy requirement. If // not, consider allocating them in an Arena. // // Some flags are automatically recognized by the main functions built by this class: // --help: Prints help text and exits. The help text is constructed based on the // information you provide to the builder as you define each flag. // --verbose: Increase logging verbosity. // --version: Print version information and exit. // // Example usage: // // class FooMain { // public: // FooMain(kj::ProcessContext& context): context(context) {} // // bool setAll() { all = true; return true; } // // Enable the --all flag. // // kj::MainBuilder::Validity setOutput(kj::StringPtr name) { // // Set the output file. // // if (name.endsWith(".foo")) { // outputFile = name; // return true; // } else { // return "Output file must have extension .foo."; // } // } // // kj::MainBuilder::Validity processInput(kj::StringPtr name) { // // Process an input file. // // if (!exists(name)) { // return kj::str(name, ": file not found"); // } // // ... process the input file ... // return true; // } // // kj::MainFunc getMain() { // return MainBuilder(context, "Foo Builder v1.5", "Reads s and builds a Foo.") // .addOption({'a', "all"}, KJ_BIND_METHOD(*this, setAll), // "Frob all the widgets. Otherwise, only some widgets are frobbed.") // .addOptionWithArg({'o', "output"}, KJ_BIND_METHOD(*this, setOutput), // "", "Output to . Must be a .foo file.") // .expectOneOrMoreArgs("", KJ_BIND_METHOD(*this, processInput)) // .build(); // } // // private: // bool all = false; // kj::StringPtr outputFile; // kj::ProcessContext& context; // }; public: MainBuilder(ProcessContext& context, StringPtr version, StringPtr briefDescription, StringPtr extendedDescription = nullptr); ~MainBuilder() noexcept(false); class OptionName { public: OptionName() = default; inline OptionName(char shortName): isLong(false), shortName(shortName) {} inline OptionName(const char* longName): isLong(true), longName(longName) {} private: bool isLong; union { char shortName; const char* longName; }; friend class MainBuilder; }; class Validity { public: inline Validity(bool valid) { if (!valid) errorMessage = heapString("invalid argument"); } inline Validity(const char* errorMessage) : errorMessage(heapString(errorMessage)) {} inline Validity(String&& errorMessage) : errorMessage(kj::mv(errorMessage)) {} inline const Maybe& getError() const { return errorMessage; } inline Maybe releaseError() { return kj::mv(errorMessage); } private: Maybe errorMessage; friend class MainBuilder; }; MainBuilder& addOption(std::initializer_list names, Function callback, StringPtr helpText); // Defines a new option (flag). `names` is a list of characters and strings that can be used to // specify the option on the command line. Single-character names are used with "-" while string // names are used with "--". `helpText` is a natural-language description of the flag. // // `callback` is called when the option is seen. Its return value indicates whether the option // was accepted. If not, further option processing stops, and error is written, and the process // exits. // // Example: // // builder.addOption({'a', "all"}, KJ_BIND_METHOD(*this, showAll), "Show all files."); // // This option could be specified in the following ways: // // -a // --all // // Note that single-character option names can be combined into a single argument. For example, // `-abcd` is equivalent to `-a -b -c -d`. // // The help text for this option would look like: // // -a, --all // Show all files. // // Note that help text is automatically word-wrapped. MainBuilder& addOptionWithArg(std::initializer_list names, Function callback, StringPtr argumentTitle, StringPtr helpText); // Like `addOption()`, but adds an option which accepts an argument. `argumentTitle` is used in // the help text. The argument text is passed to the callback. // // Example: // // builder.addOptionWithArg({'o', "output"}, KJ_BIND_METHOD(*this, setOutput), // "", "Output to ."); // // This option could be specified with an argument of "foo" in the following ways: // // -ofoo // -o foo // --output=foo // --output foo // // Note that single-character option names can be combined, but only the last option can have an // argument, since the characters after the option letter are interpreted as the argument. E.g. // `-abofoo` would be equivalent to `-a -b -o foo`. // // The help text for this option would look like: // // -o FILENAME, --output=FILENAME // Output to FILENAME. MainBuilder& addSubCommand(StringPtr name, Function getSubParser, StringPtr briefHelpText); // If exactly the given name is seen as an argument, invoke getSubParser() and then pass all // remaining arguments to the parser it returns. This is useful for implementing commands which // have lots of sub-commands, like "git" (which has sub-commands "checkout", "branch", "pull", // etc.). // // `getSubParser` is only called if the command is seen. This avoids building main functions // for commands that aren't used. // // `briefHelpText` should be brief enough to show immediately after the command name on a single // line. It will not be wrapped. Users can use the built-in "help" command to get extended // help on a particular command. MainBuilder& expectArg(StringPtr title, Function callback); MainBuilder& expectOptionalArg(StringPtr title, Function callback); MainBuilder& expectZeroOrMoreArgs(StringPtr title, Function callback); MainBuilder& expectOneOrMoreArgs(StringPtr title, Function callback); // Set callbacks to handle arguments. `expectArg()` and `expectOptionalArg()` specify positional // arguments with special handling, while `expect{Zero,One}OrMoreArgs()` specifies a handler for // an argument list (the handler is called once for each argument in the list). `title` // specifies how the argument should be represented in the usage text. // // All options callbacks are called before argument callbacks, regardless of their ordering on // the command line. This matches GNU getopt's behavior of permuting non-flag arguments to the // end of the argument list. Also matching getopt, the special option "--" indicates that the // rest of the command line is all arguments, not options, even if they start with '-'. // // The interpretation of positional arguments is fairly flexible. The non-optional arguments can // be expected at the beginning, end, or in the middle. If more arguments are specified than // the number of non-optional args, they are assigned to the optional argument handlers in the // order of registration. // // For example, say you called: // builder.expectArg("", ...); // builder.expectOptionalArg("", ...); // builder.expectArg("", ...); // builder.expectZeroOrMoreArgs("", ...); // builder.expectArg("", ...); // // This command requires at least three arguments: foo, baz, and corge. If four arguments are // given, the second is assigned to bar. If five or more arguments are specified, then the // arguments between the third and last are assigned to qux. Note that it never makes sense // to call `expect*OrMoreArgs()` more than once since only the first call would ever be used. // // In practice, you probably shouldn't create such complicated commands as in the above example. // But, this flexibility seems necessary to support commands where the first argument is special // as well as commands (like `cp`) where the last argument is special. MainBuilder& callAfterParsing(Function callback); // Call the given function after all arguments have been parsed. MainFunc build(); // Build the "main" function, which simply parses the arguments. Once this returns, the // `MainBuilder` is no longer valid. private: struct Impl; Own impl; class MainImpl; }; } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/test.h0000644000175000017500000001263513650101756017306 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "debug.h" #include "vector.h" #include "function.h" #include "windows-sanity.h" // work-around macro conflict with `ERROR` KJ_BEGIN_HEADER namespace kj { class TestRunner; class TestCase { public: TestCase(const char* file, uint line, const char* description); ~TestCase(); virtual void run() = 0; private: const char* file; uint line; const char* description; TestCase* next; TestCase** prev; bool matchedFilter; friend class TestRunner; }; #define KJ_TEST(description) \ /* Make sure the linker fails if tests are not in anonymous namespaces. */ \ extern int KJ_CONCAT(YouMustWrapTestsInAnonymousNamespace, __COUNTER__) KJ_UNUSED; \ class KJ_UNIQUE_NAME(TestCase): public ::kj::TestCase { \ public: \ KJ_UNIQUE_NAME(TestCase)(): ::kj::TestCase(__FILE__, __LINE__, description) {} \ void run() override; \ } KJ_UNIQUE_NAME(testCase); \ void KJ_UNIQUE_NAME(TestCase)::run() #if _MSC_VER #define KJ_INDIRECT_EXPAND(m, vargs) m vargs #define KJ_FAIL_EXPECT(...) \ KJ_INDIRECT_EXPAND(KJ_LOG, (ERROR , __VA_ARGS__)); #define KJ_EXPECT(cond, ...) \ if (cond); else KJ_INDIRECT_EXPAND(KJ_FAIL_EXPECT, ("failed: expected " #cond , __VA_ARGS__)) #else #define KJ_FAIL_EXPECT(...) \ KJ_LOG(ERROR, ##__VA_ARGS__); #define KJ_EXPECT(cond, ...) \ if (cond); else KJ_FAIL_EXPECT("failed: expected " #cond, ##__VA_ARGS__) #endif #define KJ_EXPECT_THROW_RECOVERABLE(type, code) \ do { \ KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \ KJ_EXPECT(e->getType() == ::kj::Exception::Type::type, \ "code threw wrong exception type: " #code, e->getType()); \ } else { \ KJ_FAIL_EXPECT("code did not throw: " #code); \ } \ } while (false) #define KJ_EXPECT_THROW_RECOVERABLE_MESSAGE(message, code) \ do { \ KJ_IF_MAYBE(e, ::kj::runCatchingExceptions([&]() { code; })) { \ KJ_EXPECT(::kj::_::hasSubstring(e->getDescription(), message), \ "exception description didn't contain expected substring", e->getDescription()); \ } else { \ KJ_FAIL_EXPECT("code did not throw: " #code); \ } \ } while (false) #if KJ_NO_EXCEPTIONS #define KJ_EXPECT_THROW(type, code) \ do { \ KJ_EXPECT(::kj::_::expectFatalThrow(::kj::Exception::Type::type, nullptr, [&]() { code; })); \ } while (false) #define KJ_EXPECT_THROW_MESSAGE(message, code) \ do { \ KJ_EXPECT(::kj::_::expectFatalThrow(nullptr, kj::StringPtr(message), [&]() { code; })); \ } while (false) #else #define KJ_EXPECT_THROW KJ_EXPECT_THROW_RECOVERABLE #define KJ_EXPECT_THROW_MESSAGE KJ_EXPECT_THROW_RECOVERABLE_MESSAGE #endif #define KJ_EXPECT_LOG(level, substring) \ ::kj::_::LogExpectation KJ_UNIQUE_NAME(_kjLogExpectation)(::kj::LogSeverity::level, substring) // Expects that a log message with the given level and substring text will be printed within // the current scope. This message will not cause the test to fail, even if it is an error. // ======================================================================================= namespace _ { // private bool hasSubstring(kj::StringPtr haystack, kj::StringPtr needle); #if KJ_NO_EXCEPTIONS bool expectFatalThrow(Maybe type, Maybe message, Function code); // Expects that the given code will throw a fatal exception matching the given type and/or message. // Since exceptions are disabled, the test will fork() and run in a subprocess. On Windows, where // fork() is not available, this always returns true. #endif class LogExpectation: public ExceptionCallback { public: LogExpectation(LogSeverity severity, StringPtr substring); ~LogExpectation(); void logMessage(LogSeverity severity, const char* file, int line, int contextDepth, String&& text) override; private: LogSeverity severity; StringPtr substring; bool seen; UnwindDetector unwindDetector; }; class GlobFilter { // Implements glob filters for the --filter flag. // // Exposed in header only for testing. public: explicit GlobFilter(const char* pattern); explicit GlobFilter(ArrayPtr pattern); bool matches(StringPtr name); private: String pattern; Vector states; void applyState(char c, int state); }; } // namespace _ (private) } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/windows-sanity.h0000644000175000017500000000600013650101756021313 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2018 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. // This file replaces poorly-named #defines from windows.h with properly-namespaced versions, so // that they no longer conflict with similarly-named identifiers in other namespaces. // // This file must be #included some time after windows.h has been #included but before any attempt // to use the names for other purposes. However, this can be difficult to determine in header // files. Typically KJ / Cap'n Proto headers avoid including windows.h at all, but may use // conflicting identifiers. In order to relieve application developers from the need to include // windows-sanity.h themselves, we would like these headers to conditionally apply the fixes if // and only if windows.h was already included. Therefore, this header checks if windows.h has been // included and only applies fixups if this is the case. Furthermore, this header is designed such // that it can be included multiple times, and the fixups will be applied the first time it is // included *after* windows.h. // // Now, as long as any headers which need to use conflicting identifier names be sure to #include // windows-sanity.h, we can be sure that no conflicts will occur regardless of in what order the // application chooses to include these headers vs. windows.h. #if !_WIN32 && !__CYGWIN__ // Not on Windows. Tell the compiler never to try to include this again. #pragma once #elif defined(_INC_WINDOWS) // We're on Windows and windows.h has been included. We need to fixup the namespace. We only need // to do this once, but we can't do it until windows.h has been included. Since that has happened // now, we use `#pragma once` to tell the compiler never to include this file again. #pragma once namespace win32 { const auto ERROR_ = ERROR; #undef ERROR const auto ERROR = ERROR_; typedef VOID VOID_; #undef VOID typedef VOID_ VOID; } using win32::ERROR; using win32::VOID; #endif capnproto-c++-0.8.0/src/kj/miniposix.h0000644000175000017500000000756613650101756020355 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once // This header provides a small subset of the POSIX API which also happens to be available on // Windows under slightly-different names. #if _WIN32 #include #include #include // _O_BINARY #else #include #include #endif #if !_WIN32 || __MINGW32__ #include #include #include #endif #if !_WIN32 #include #endif KJ_BEGIN_HEADER namespace kj { namespace miniposix { #if _WIN32 && !__MINGW32__ // We're on Windows and not MinGW. So, we need to define wrappers for the POSIX API. typedef int ssize_t; inline ssize_t read(int fd, void* buffer, size_t size) { return ::_read(fd, buffer, size); } inline ssize_t write(int fd, const void* buffer, size_t size) { return ::_write(fd, buffer, size); } inline int close(int fd) { return ::_close(fd); } #ifndef F_OK #define F_OK 0 // access() existence test #endif #ifndef S_ISREG #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) // stat() regular file test #endif #ifndef S_ISDIR #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) // stat() directory test #endif #ifndef STDIN_FILENO #define STDIN_FILENO 0 #endif #ifndef STDOUT_FILENO #define STDOUT_FILENO 1 #endif #ifndef STDERR_FILENO #define STDERR_FILENO 2 #endif #else // We're on a POSIX system or MinGW which already defines the wrappers for us. using ::ssize_t; using ::read; using ::write; using ::close; #endif #if _WIN32 // We're on Windows, including MinGW. pipe() and mkdir() are non-standard even on MinGW. inline int pipe(int fds[2]) { return ::_pipe(fds, 8192, _O_BINARY | _O_NOINHERIT); } inline int mkdir(const char* path, int mode) { return ::_mkdir(path); } #else // We're on real POSIX. using ::pipe; using ::mkdir; inline size_t iovMax(size_t count) { // Apparently, there is a maximum number of iovecs allowed per call. I don't understand why. // Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd, // define neither. // // On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX), // then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails. // // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01 #if defined(IOV_MAX) // Solaris (and others?) return IOV_MAX; #elif defined(UIO_MAXIOV) // Linux return UIO_MAXIOV; #else // POSIX mystery meat long iovmax; errno = 0; if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) { // assume iovmax == -1 && errno == 0 means "unbounded" return errno ? _XOPEN_IOV_MAX : count; } else { return (size_t) iovmax; } #endif } #endif } // namespace miniposix } // namespace kj KJ_END_HEADER capnproto-c++-0.8.0/src/kj/async-io-internal.h0000644000175000017500000000564413340402540021654 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #pragma once #include "string.h" #include "vector.h" #include "async-io.h" #include struct sockaddr; struct sockaddr_un; namespace kj { namespace _ { // private // ======================================================================================= #if !_WIN32 kj::ArrayPtr safeUnixPath(const struct sockaddr_un* addr, uint addrlen); // sockaddr_un::sun_path is not required to have a NUL terminator! Thus to be safe unix address // paths MUST be read using this function. #endif class CidrRange { public: CidrRange(StringPtr pattern); static CidrRange inet4(ArrayPtr bits, uint bitCount); static CidrRange inet6(ArrayPtr prefix, ArrayPtr suffix, uint bitCount); // Zeros are inserted between `prefix` and `suffix` to extend the address to 128 bits. uint getSpecificity() const { return bitCount; } bool matches(const struct sockaddr* addr) const; bool matchesFamily(int family) const; String toString() const; private: int family; byte bits[16]; uint bitCount; // how many bits in `bits` need to match CidrRange(int family, ArrayPtr bits, uint bitCount); void zeroIrrelevantBits(); }; class NetworkFilter: public LowLevelAsyncIoProvider::NetworkFilter { public: NetworkFilter(); NetworkFilter(ArrayPtr allow, ArrayPtr deny, NetworkFilter& next); bool shouldAllow(const struct sockaddr* addr, uint addrlen) override; bool shouldAllowParse(const struct sockaddr* addr, uint addrlen); private: Vector allowCidrs; Vector denyCidrs; bool allowUnix; bool allowAbstractUnix; kj::Maybe next; }; } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/async.c++0000644000175000017500000015646113650101756017573 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 || __CYGWIN__ #define WIN32_LEAN_AND_MEAN 1 // lolz #elif __APPLE__ // getcontext() and friends are marked deprecated on MacOS but seemingly no replacement is // provided. It appears as if they deprecated it solely because the standards bodies deprecated it, // which they seemingly did mainly because the proper sematics are too difficult for them to // define. I doubt MacOS would actually remove these functions as they are widely used. But if they // do, then I guess we'll need to fall back to using setjmp()/longjmp(), and some sort of hack // involving sigaltstack() (and generating a fake signal I guess) in order to initialize the fiber // in the first place. Or we could use assembly, I suppose. Either way, ick. #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #define _XOPEN_SOURCE // Must be defined to see getcontext() on MacOS. #endif #include "async.h" #include "debug.h" #include "vector.h" #include "threadlocal.h" #include "mutex.h" #if _WIN32 || __CYGWIN__ #include // for Sleep(0) and fibers #include "windows-sanity.h" #else #include #include // mmap(), for allocating new stacks #include // sysconf() #include #endif #if !_WIN32 #include // just for sched_yield() #endif #if !KJ_NO_RTTI #include #if __GNUC__ #include #endif #endif #include namespace kj { namespace { KJ_THREADLOCAL_PTR(EventLoop) threadLocalEventLoop = nullptr; #define _kJ_ALREADY_READY reinterpret_cast< ::kj::_::Event*>(1) EventLoop& currentEventLoop() { EventLoop* loop = threadLocalEventLoop; KJ_REQUIRE(loop != nullptr, "No event loop is running on this thread."); return *loop; } class BoolEvent: public _::Event { public: bool fired = false; Maybe> fire() override { fired = true; return nullptr; } }; class YieldPromiseNode final: public _::PromiseNode { public: void onReady(_::Event* event) noexcept override { if (event) event->armBreadthFirst(); } void get(_::ExceptionOrValue& output) noexcept override { output.as<_::Void>() = _::Void(); } }; class YieldHarderPromiseNode final: public _::PromiseNode { public: void onReady(_::Event* event) noexcept override { if (event) event->armLast(); } void get(_::ExceptionOrValue& output) noexcept override { output.as<_::Void>() = _::Void(); } }; class NeverDonePromiseNode final: public _::PromiseNode { public: void onReady(_::Event* event) noexcept override { // ignore } void get(_::ExceptionOrValue& output) noexcept override { KJ_FAIL_REQUIRE("Not ready."); } }; } // namespace // ======================================================================================= Canceler::~Canceler() noexcept(false) { cancel("operation canceled"); } void Canceler::cancel(StringPtr cancelReason) { if (isEmpty()) return; cancel(Exception(Exception::Type::FAILED, __FILE__, __LINE__, kj::str(cancelReason))); } void Canceler::cancel(const Exception& exception) { for (;;) { KJ_IF_MAYBE(a, list) { a->unlink(); a->cancel(kj::cp(exception)); } else { break; } } } void Canceler::release() { for (;;) { KJ_IF_MAYBE(a, list) { a->unlink(); } else { break; } } } Canceler::AdapterBase::AdapterBase(Canceler& canceler) : prev(canceler.list), next(canceler.list) { canceler.list = *this; KJ_IF_MAYBE(n, next) { n->prev = next; } } Canceler::AdapterBase::~AdapterBase() noexcept(false) { unlink(); } void Canceler::AdapterBase::unlink() { KJ_IF_MAYBE(p, prev) { *p = next; } KJ_IF_MAYBE(n, next) { n->prev = prev; } next = nullptr; prev = nullptr; } Canceler::AdapterImpl::AdapterImpl(kj::PromiseFulfiller& fulfiller, Canceler& canceler, kj::Promise inner) : AdapterBase(canceler), fulfiller(fulfiller), inner(inner.then( [&fulfiller]() { fulfiller.fulfill(); }, [&fulfiller](kj::Exception&& e) { fulfiller.reject(kj::mv(e)); }) .eagerlyEvaluate(nullptr)) {} void Canceler::AdapterImpl::cancel(kj::Exception&& e) { fulfiller.reject(kj::mv(e)); inner = nullptr; } // ======================================================================================= TaskSet::TaskSet(TaskSet::ErrorHandler& errorHandler) : errorHandler(errorHandler) {} TaskSet::~TaskSet() noexcept(false) {} class TaskSet::Task final: public _::Event { public: Task(TaskSet& taskSet, Own<_::PromiseNode>&& nodeParam) : taskSet(taskSet), node(kj::mv(nodeParam)) { node->setSelfPointer(&node); node->onReady(this); } Maybe> next; Maybe>* prev = nullptr; protected: Maybe> fire() override { // Get the result. _::ExceptionOr<_::Void> result; node->get(result); // Delete the node, catching any exceptions. KJ_IF_MAYBE(exception, kj::runCatchingExceptions([this]() { node = nullptr; })) { result.addException(kj::mv(*exception)); } // Call the error handler if there was an exception. KJ_IF_MAYBE(e, result.exception) { taskSet.errorHandler.taskFailed(kj::mv(*e)); } // Remove from the task list. KJ_IF_MAYBE(n, next) { n->get()->prev = prev; } Own self = kj::mv(KJ_ASSERT_NONNULL(*prev)); KJ_ASSERT(self.get() == this); *prev = kj::mv(next); next = nullptr; prev = nullptr; KJ_IF_MAYBE(f, taskSet.emptyFulfiller) { if (taskSet.tasks == nullptr) { f->get()->fulfill(); taskSet.emptyFulfiller = nullptr; } } return mv(self); } _::PromiseNode* getInnerForTrace() override { return node; } private: TaskSet& taskSet; Own<_::PromiseNode> node; }; void TaskSet::add(Promise&& promise) { auto task = heap(*this, _::PromiseNode::from(kj::mv(promise))); KJ_IF_MAYBE(head, tasks) { head->get()->prev = &task->next; task->next = kj::mv(tasks); } task->prev = &tasks; tasks = kj::mv(task); } kj::String TaskSet::trace() { kj::Vector traces; Maybe>* ptr = &tasks; for (;;) { KJ_IF_MAYBE(task, *ptr) { traces.add(task->get()->trace()); ptr = &task->get()->next; } else { break; } } return kj::strArray(traces, "\n============================================\n"); } Promise TaskSet::onEmpty() { KJ_REQUIRE(emptyFulfiller == nullptr, "onEmpty() can only be called once at a time"); if (tasks == nullptr) { return READY_NOW; } else { auto paf = newPromiseAndFulfiller(); emptyFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } } namespace _ { // private class LoggingErrorHandler: public TaskSet::ErrorHandler { public: static LoggingErrorHandler instance; void taskFailed(kj::Exception&& exception) override { KJ_LOG(ERROR, "Uncaught exception in daemonized task.", exception); } }; LoggingErrorHandler LoggingErrorHandler::instance = LoggingErrorHandler(); } // namespace _ (private) // ======================================================================================= struct Executor::Impl { typedef Maybe<_::XThreadEvent&> _::XThreadEvent::*NextMember; typedef Maybe<_::XThreadEvent&>* _::XThreadEvent::*PrevMember; template struct List { kj::Maybe<_::XThreadEvent&> head; kj::Maybe<_::XThreadEvent&>* tail = &head; bool empty() const { return head == nullptr; } void insert(_::XThreadEvent& event) { KJ_REQUIRE(event.*prev == nullptr); *tail = event; event.*prev = tail; tail = &(event.*next); } void erase(_::XThreadEvent& event) { KJ_REQUIRE(event.*prev != nullptr); *(event.*prev) = event.*next; KJ_IF_MAYBE(n, event.*next) { n->*prev = event.*prev; } else { KJ_DASSERT(tail == &(event.*next)); tail = event.*prev; } event.*next = nullptr; event.*prev = nullptr; } template void forEach(Func&& func) { kj::Maybe<_::XThreadEvent&> current = head; for (;;) { KJ_IF_MAYBE(c, current) { auto nextItem = c->*next; func(*c); current = nextItem; } else { break; } } } }; struct State { // Queues of notifications from other threads that need this thread's attention. List<&_::XThreadEvent::targetNext, &_::XThreadEvent::targetPrev> run; List<&_::XThreadEvent::targetNext, &_::XThreadEvent::targetPrev> cancel; List<&_::XThreadEvent::replyNext, &_::XThreadEvent::replyPrev> replies; bool waitingForCancel = false; // True if this thread is currently blocked waiting for some other thread to pump its // cancellation queue. If that other thread tries to block on *this* thread, then it could // deadlock -- it must take precautions against this. bool empty() const { return run.empty() && cancel.empty() && replies.empty(); } void dispatchAll(Vector<_::XThreadEvent*>& eventsToCancelOutsideLock) { run.forEach([&](_::XThreadEvent& event) { run.erase(event); event.state = _::XThreadEvent::EXECUTING; event.armBreadthFirst(); }); dispatchCancels(eventsToCancelOutsideLock); replies.forEach([&](_::XThreadEvent& event) { replies.erase(event); event.onReadyEvent.armBreadthFirst(); }); } void dispatchCancels(Vector<_::XThreadEvent*>& eventsToCancelOutsideLock) { cancel.forEach([&](_::XThreadEvent& event) { cancel.erase(event); if (event.promiseNode == nullptr) { event.state = _::XThreadEvent::DONE; } else { // We can't destroy the promiseNode while the mutex is locked, because we don't know // what the destructor might do. But, we *must* destroy it before acknowledging // cancellation. So we have to add it to a list to destroy later. eventsToCancelOutsideLock.add(&event); } }); } }; kj::MutexGuarded state; // After modifying state from another thread, the loop's port.wake() must be called. void processAsyncCancellations(Vector<_::XThreadEvent*>& eventsToCancelOutsideLock) { // After calling dispatchAll() or dispatchCancels() with the lock held, it may be that some // cancellations require dropping the lock before destroying the promiseNode. In that case // those cancellations will be added to the eventsToCancelOutsideLock Vector passed to the // method. That vector must then be passed to processAsyncCancellations() as soon as the lock // is released. for (auto& event: eventsToCancelOutsideLock) { event->promiseNode = nullptr; event->disarm(); } // Now we need to mark all the events "done" under lock. auto lock = state.lockExclusive(); for (auto& event: eventsToCancelOutsideLock) { event->state = _::XThreadEvent::DONE; } } }; namespace _ { // (private) void XThreadEvent::ensureDoneOrCanceled() { #if _MSC_VER { // TODO(perf): TODO(msvc): Implement the double-checked lock optimization on MSVC. #else if (__atomic_load_n(&state, __ATOMIC_ACQUIRE) != DONE) { #endif auto lock = targetExecutor.impl->state.lockExclusive(); switch (state) { case UNUSED: // Nothing to do. break; case QUEUED: lock->run.erase(*this); // No wake needed since we removed work rather than adding it. state = DONE; break; case EXECUTING: { lock->cancel.insert(*this); KJ_IF_MAYBE(p, targetExecutor.loop.port) { p->wake(); } Maybe maybeSelfExecutor = nullptr; if (threadLocalEventLoop != nullptr) { KJ_IF_MAYBE(e, threadLocalEventLoop->executor) { maybeSelfExecutor = *e; } } KJ_IF_MAYBE(selfExecutor, maybeSelfExecutor) { // If, while waiting for other threads to process our cancellation request, we have // cancellation requests queued back to this thread, we must process them. Otherwise, // we could deadlock with two threads waiting on each other to process cancellations. // // We don't have a terribly good way to detect this, except to check if the remote // thread is itself waiting for cancellations and, if so, wake ourselves up to check for // cancellations to process. This will busy-loop but at least it should eventually // resolve assuming fair scheduling. // // To make things extra-annoying, in order to update our waitingForCancel flag, we have // to lock our own executor state, but we can't take both locks at once, so we have to // release the other lock in the meantime. // Make sure we unset waitingForCancel on the way out. KJ_DEFER({ lock = {}; Vector<_::XThreadEvent*> eventsToCancelOutsideLock; KJ_DEFER(selfExecutor->impl->processAsyncCancellations(eventsToCancelOutsideLock)); auto selfLock = selfExecutor->impl->state.lockExclusive(); selfLock->waitingForCancel = false; selfLock->dispatchCancels(eventsToCancelOutsideLock); // We don't need to re-take the lock on the other executor here; it's not used again // after this scope. }); while (state != DONE) { bool otherThreadIsWaiting = lock->waitingForCancel; // Make sure our waitingForCancel is on and dispatch any pending cancellations on this // thread. lock = {}; { Vector<_::XThreadEvent*> eventsToCancelOutsideLock; KJ_DEFER(selfExecutor->impl->processAsyncCancellations(eventsToCancelOutsideLock)); auto selfLock = selfExecutor->impl->state.lockExclusive(); selfLock->waitingForCancel = true; // Note that we don't have to proactively delete the PromiseNodes extracted from // the canceled events because those nodes belong to this thread and can't possibly // continue executing while we're blocked here. selfLock->dispatchCancels(eventsToCancelOutsideLock); } if (otherThreadIsWaiting) { // We know the other thread was waiting for cancellations to complete a moment ago. // We may have just processed the necessary cancellations in this thread, in which // case the other thread needs a chance to receive control and notice this. Or, it // may be that the other thread is waiting for some third thread to take action. // Either way, we should yield control here to give things a chance to settle. // Otherwise we could end up in a tight busy loop. #if _WIN32 Sleep(0); #else sched_yield(); #endif } // OK now we can take the original lock again. lock = targetExecutor.impl->state.lockExclusive(); // OK, now we can wait for the other thread to either process our cancellation or // indicate that it is waiting for remote cancellation. lock.wait([&](const Executor::Impl::State& executorState) { return state == DONE || executorState.waitingForCancel; }); } } else { // We have no executor of our own so we don't have to worry about cancellation cycles // causing deadlock. // // NOTE: I don't think we can actually get here, because it implies that this is a // synchronous execution, which means there's no way to cancel it. lock.wait([&](auto&) { return state == DONE; }); } KJ_DASSERT(targetPrev == nullptr); break; } case DONE: // Became done while we waited for lock. Nothing to do. break; } } KJ_IF_MAYBE(e, replyExecutor) { // Since we know we reached the DONE state (or never left UNUSED), we know that the remote // thread is all done playing with our `replyPrev` pointer. Only the current thread could // possibly modify it after this point. So we can skip the lock if it's already null. if (replyPrev != nullptr) { auto lock = e->impl->state.lockExclusive(); lock->replies.erase(*this); } } } void XThreadEvent::done() { KJ_IF_MAYBE(e, replyExecutor) { // Queue the reply. { auto lock = e->impl->state.lockExclusive(); lock->replies.insert(*this); } KJ_IF_MAYBE(p, e->loop.port) { p->wake(); } } { auto lock = targetExecutor.impl->state.lockExclusive(); KJ_DASSERT(state == EXECUTING); if (targetPrev != nullptr) { // We must be in the cancel list, because we can't be in the run list during EXECUTING state. // We can remove ourselves from the cancel list because at this point we're done anyway, so // whatever. lock->cancel.erase(*this); } #if _MSC_VER // TODO(perf): TODO(msvc): Implement the double-checked lock optimization on MSVC. state = DONE; #else __atomic_store_n(&state, DONE, __ATOMIC_RELEASE); #endif } } class XThreadEvent::DelayedDoneHack: public Disposer { // Crazy hack: In fire(), we want to call done() if the event is finished. But done() signals // the requesting thread to wake up and possibly delete the XThreadEvent. But the caller (the // EventLoop) still has to set `event->firing = false` after `fire()` returns, so this would be // a race condition use-after-free. // // It just so happens, though, that fire() is allowed to return an optional `Own` to drop, // and the caller drops that pointer immediately after setting event->firing = false. So we // return a pointer whose disposer calls done(). // // It's not quite as much of a hack as it seems: The whole reason fire() returns an Own is // so that the event can delete itself, but do so after the caller sets event->firing = false. // It just happens to be that in this case, the event isn't deleting itself, but rather releasing // itself back to the other thread. protected: void disposeImpl(void* pointer) const override { reinterpret_cast(pointer)->done(); } }; Maybe> XThreadEvent::fire() { static constexpr DelayedDoneHack DISPOSER {}; KJ_IF_MAYBE(n, promiseNode) { n->get()->get(result); promiseNode = nullptr; // make sure to destroy in the thread that created it return Own(this, DISPOSER); } else { KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { promiseNode = execute(); })) { result.addException(kj::mv(*exception)); }; KJ_IF_MAYBE(n, promiseNode) { n->get()->onReady(this); } else { return Own(this, DISPOSER); } } return nullptr; } void XThreadEvent::onReady(Event* event) noexcept { onReadyEvent.init(event); } } // namespace _ Executor::Executor(EventLoop& loop, Badge): loop(loop), impl(kj::heap()) {} Executor::~Executor() noexcept(false) {} void Executor::send(_::XThreadEvent& event, bool sync) const { KJ_ASSERT(event.state == _::XThreadEvent::UNUSED); if (sync) { if (threadLocalEventLoop == &loop) { // Invoking a sync request on our own thread. Just execute it directly; if we try to queue // it to the loop, we'll deadlock. auto promiseNode = event.execute(); // If the function returns a promise, we have no way to pump the event loop to wait for it, // because the event loop may already be pumping somewhere up the stack. KJ_ASSERT(promiseNode == nullptr, "can't call executeSync() on own thread's executor with a promise-returning function"); return; } } else { event.replyExecutor = getCurrentThreadExecutor(); // Note that async requests will "just work" even if the target executor is our own thread's // executor. In theory we could detect this case to avoid some locking and signals but that // would be extra code complexity for probably little benefit. } auto lock = impl->state.lockExclusive(); event.state = _::XThreadEvent::QUEUED; lock->run.insert(event); KJ_IF_MAYBE(p, loop.port) { p->wake(); } else { // Event loop will be waiting on executor.wait(), which will be woken when we unlock the mutex. } if (sync) { lock.wait([&](auto&) { return event.state == _::XThreadEvent::DONE; }); } } void Executor::wait() { Vector<_::XThreadEvent*> eventsToCancelOutsideLock; KJ_DEFER(impl->processAsyncCancellations(eventsToCancelOutsideLock)); auto lock = impl->state.lockExclusive(); lock.wait([](const Impl::State& state) { return !state.empty(); }); lock->dispatchAll(eventsToCancelOutsideLock); } bool Executor::poll() { Vector<_::XThreadEvent*> eventsToCancelOutsideLock; KJ_DEFER(impl->processAsyncCancellations(eventsToCancelOutsideLock)); auto lock = impl->state.lockExclusive(); if (lock->empty()) { return false; } else { lock->dispatchAll(eventsToCancelOutsideLock); return true; } } const Executor& getCurrentThreadExecutor() { return currentEventLoop().getExecutor(); } // ======================================================================================= // Fiber implementation. namespace _ { // private #if !(_WIN32 || __CYGWIN__ || __BIONIC__) struct FiberBase::Impl { // This struct serves two purposes: // - It contains OS-specific state that we don't want to declare in the header. // - It is allocated at the top of the fiber's stack area, so the Impl pointer also serves to // track where the stack was allocated. ucontext_t fiberContext; ucontext_t originalContext; static Impl& alloc(size_t stackSize) { // TODO(perf): Freelist stacks to avoid TLB flushes. #ifndef MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON #endif #ifndef MAP_STACK #define MAP_STACK 0 #endif size_t pageSize = getPageSize(); size_t allocSize = stackSize + pageSize; // size plus guard page // Allocate virtual address space for the stack but make it inaccessible initially. // TODO(someday): Does it make sense to use MAP_GROWSDOWN on Linux? It's a kind of bizarre flag // that causes the mapping to automatically allocate extra pages (beyond the range specified) // until it hits something... void* stack = mmap(nullptr, allocSize, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK, -1, 0); if (stack == MAP_FAILED) { KJ_FAIL_SYSCALL("mmap(new stack)", errno); } KJ_ON_SCOPE_FAILURE({ KJ_SYSCALL(munmap(stack, allocSize)) { break; } }); // Now mark everything except the guard page as read-write. We assume the stack grows down, so // the guard page is at the beginning. No modern architecture uses stacks that grow up. KJ_SYSCALL(mprotect(reinterpret_cast(stack) + pageSize, stackSize, PROT_READ | PROT_WRITE)); // Stick `Impl` at the top of the stack. Impl& impl = *(reinterpret_cast(reinterpret_cast(stack) + allocSize) - 1); // Note: mmap() allocates zero'd pages so we don't have to memset() anything here. KJ_SYSCALL(getcontext(&impl.fiberContext)); impl.fiberContext.uc_stack.ss_size = allocSize - sizeof(Impl); impl.fiberContext.uc_stack.ss_sp = reinterpret_cast(stack); impl.fiberContext.uc_stack.ss_flags = 0; impl.fiberContext.uc_link = &impl.originalContext; return impl; } static void free(Impl& impl, size_t stackSize) { size_t allocSize = stackSize + getPageSize(); void* stack = reinterpret_cast(&impl + 1) - allocSize; KJ_SYSCALL(munmap(stack, allocSize)) { break; } } static size_t getPageSize() { #ifndef _SC_PAGESIZE #define _SC_PAGESIZE _SC_PAGE_SIZE #endif static size_t result = sysconf(_SC_PAGE_SIZE); return result; } }; #endif struct FiberBase::StartRoutine { #if _WIN32 || __CYGWIN__ static void WINAPI run(LPVOID ptr) { // This is the static C-style function we pass to CreateFiber(). auto& fiber = *reinterpret_cast(ptr); fiber.run(); // On Windows, if the fiber main function returns, the thread exits. We need to explicitly switch // back to the main stack. fiber.switchToMain(); } #else static void run(int arg1, int arg2) { // This is the static C-style function we pass to makeContext(). // POSIX says the arguments are ints, not pointers. So we split our pointer in half in order to // work correctly on 64-bit machines. Gross. uintptr_t ptr = static_cast(arg1); ptr |= static_cast(static_cast(arg2)) << (sizeof(ptr) * 4); reinterpret_cast(ptr)->run(); } #endif }; FiberBase::FiberBase(size_t stackSizeParam, _::ExceptionOrValue& result) : state(WAITING), // Force stackSize to a reasonable minimum. stackSize(kj::max(stackSizeParam, 65536)), #if !(_WIN32 || __CYGWIN__ || __BIONIC__) impl(Impl::alloc(stackSize)), #endif result(result) { #if _WIN32 || __CYGWIN__ auto& eventLoop = currentEventLoop(); if (eventLoop.mainFiber == nullptr) { // First time we've created a fiber. We need to convert the main stack into a fiber as well // before we can start switching. eventLoop.mainFiber = ConvertThreadToFiber(nullptr); } KJ_WIN32(osFiber = CreateFiber(stackSize, &StartRoutine::run, this)); #elif !__BIONIC__ // Note: Nothing below here can throw. If that changes then we need to call Impl::free(impl) // on exceptions... // POSIX says the arguments are ints, not pointers. So we split our pointer in half in order to // work correctly on 64-bit machines. Gross. uintptr_t ptr = reinterpret_cast(this); int arg1 = ptr & ((uintptr_t(1) << (sizeof(ptr) * 4)) - 1); int arg2 = ptr >> (sizeof(ptr) * 4); makecontext(&impl.fiberContext, reinterpret_cast(&StartRoutine::run), 2, arg1, arg2); #else KJ_UNIMPLEMENTED( "Fibers are not implemented on this platform because its C library lacks setcontext() " "and friends. If you'd like to see fiber support added, file a bug to let us know. " "We can likely make it happen using assembly, but didn't want to try unless it was " "actually needed."); #endif } FiberBase::~FiberBase() noexcept(false) { #if _WIN32 || __CYGWIN__ DeleteFiber(osFiber); #elif !__BIONIC__ Impl::free(impl, stackSize); #endif } void FiberBase::destroy() { // Called by `~Fiber()` to begin teardown. We can't do this work in `~FiberBase()` because the // `Fiber` subclass contains members that may still be in-use until the fiber stops. switch (state) { case WAITING: // We can't just free the stack while the fiber is running. We need to force it to execute // until finished, so we cause it to throw an exception. state = CANCELED; switchToFiber(); // The fiber should only switch back to the main stack on completion, because any further // calls to wait() would throw before trying to switch. KJ_ASSERT(state == FINISHED); break; case RUNNING: case CANCELED: // Bad news. KJ_LOG(FATAL, "fiber tried to destroy itself"); ::abort(); break; case FINISHED: // Normal completion, yay. break; } } Maybe> FiberBase::fire() { KJ_ASSERT(state == WAITING); state = RUNNING; switchToFiber(); return nullptr; } void FiberBase::switchToFiber() { // Switch from the main stack to the fiber. Returns once the fiber either calls switchToMain() // or returns from its main function. #if _WIN32 || __CYGWIN__ SwitchToFiber(osFiber); #elif !__BIONIC__ KJ_SYSCALL(swapcontext(&impl.originalContext, &impl.fiberContext)); #endif } void FiberBase::switchToMain() { // Switch from the fiber to the main stack. Returns the next time the main stack calls // switchToFiber(). #if _WIN32 || __CYGWIN__ SwitchToFiber(currentEventLoop().mainFiber); #elif !__BIONIC__ KJ_SYSCALL(swapcontext(&impl.fiberContext, &impl.originalContext)); #endif } void FiberBase::run() { state = RUNNING; KJ_DEFER(state = FINISHED); WaitScope waitScope(currentEventLoop(), *this); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { runImpl(waitScope); })) { result.addException(kj::mv(*exception)); } onReadyEvent.arm(); } void FiberBase::onReady(_::Event* event) noexcept { onReadyEvent.init(event); } PromiseNode* FiberBase::getInnerForTrace() { return currentInner; } } // namespace _ (private) // ======================================================================================= void EventPort::setRunnable(bool runnable) {} void EventPort::wake() const { kj::throwRecoverableException(KJ_EXCEPTION(UNIMPLEMENTED, "cross-thread wake() not implemented by this EventPort implementation")); } EventLoop::EventLoop() : daemons(kj::heap(_::LoggingErrorHandler::instance)) {} EventLoop::EventLoop(EventPort& port) : port(port), daemons(kj::heap(_::LoggingErrorHandler::instance)) {} EventLoop::~EventLoop() noexcept(false) { #if _WIN32 || __CYGWIN__ KJ_DEFER({ if (mainFiber != nullptr) { // We converted the thread to a fiber, need to convert it back. KJ_WIN32(ConvertFiberToThread()); } }); #endif // Destroy all "daemon" tasks, noting that their destructors might try to access the EventLoop // some more. daemons = nullptr; // The application _should_ destroy everything using the EventLoop before destroying the // EventLoop itself, so if there are events on the loop, this indicates a memory leak. KJ_REQUIRE(head == nullptr, "EventLoop destroyed with events still in the queue. Memory leak?", head->trace()) { // Unlink all the events and hope that no one ever fires them... _::Event* event = head; while (event != nullptr) { _::Event* next = event->next; event->next = nullptr; event->prev = nullptr; event = next; } break; } KJ_REQUIRE(threadLocalEventLoop != this, "EventLoop destroyed while still current for the thread.") { threadLocalEventLoop = nullptr; break; } } void EventLoop::run(uint maxTurnCount) { running = true; KJ_DEFER(running = false); for (uint i = 0; i < maxTurnCount; i++) { if (!turn()) { break; } } setRunnable(isRunnable()); } bool EventLoop::turn() { _::Event* event = head; if (event == nullptr) { // No events in the queue. return false; } else { head = event->next; if (head != nullptr) { head->prev = &head; } depthFirstInsertPoint = &head; if (breadthFirstInsertPoint == &event->next) { breadthFirstInsertPoint = &head; } if (tail == &event->next) { tail = &head; } event->next = nullptr; event->prev = nullptr; Maybe> eventToDestroy; { event->firing = true; KJ_DEFER(event->firing = false); eventToDestroy = event->fire(); } depthFirstInsertPoint = &head; return true; } } bool EventLoop::isRunnable() { return head != nullptr; } const Executor& EventLoop::getExecutor() { KJ_IF_MAYBE(e, executor) { return *e; } else { return executor.emplace(*this, Badge()); } } void EventLoop::setRunnable(bool runnable) { if (runnable != lastRunnableState) { KJ_IF_MAYBE(p, port) { p->setRunnable(runnable); } lastRunnableState = runnable; } } void EventLoop::enterScope() { KJ_REQUIRE(threadLocalEventLoop == nullptr, "This thread already has an EventLoop."); threadLocalEventLoop = this; } void EventLoop::leaveScope() { KJ_REQUIRE(threadLocalEventLoop == this, "WaitScope destroyed in a different thread than it was created in.") { break; } threadLocalEventLoop = nullptr; } void EventLoop::wait() { KJ_IF_MAYBE(p, port) { if (p->wait()) { // Another thread called wake(). Check for cross-thread events. KJ_IF_MAYBE(e, executor) { e->poll(); } } } else KJ_IF_MAYBE(e, executor) { e->wait(); } else { KJ_FAIL_REQUIRE("Nothing to wait for; this thread would hang forever."); } } void EventLoop::poll() { KJ_IF_MAYBE(p, port) { if (p->poll()) { // Another thread called wake(). Check for cross-thread events. KJ_IF_MAYBE(e, executor) { e->poll(); } } } else KJ_IF_MAYBE(e, executor) { e->poll(); } } void WaitScope::poll() { KJ_REQUIRE(&loop == threadLocalEventLoop, "WaitScope not valid for this thread."); KJ_REQUIRE(!loop.running, "poll() is not allowed from within event callbacks."); loop.running = true; KJ_DEFER(loop.running = false); for (;;) { if (!loop.turn()) { // No events in the queue. Poll for I/O. loop.poll(); if (!loop.isRunnable()) { // Still no events in the queue. We're done. return; } } } } namespace _ { // private static kj::Exception fiberCanceledException() { // Construct the exception to throw from wait() when the fiber has been canceled (because the // promise returned by startFiber() was dropped before completion). // // TODO(someday): Should we have a dedicated exception type for cancellation? Do we even want // to build stack traces and such for these? return KJ_EXCEPTION(FAILED, "This fiber is being canceled."); }; void waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result, WaitScope& waitScope) { EventLoop& loop = waitScope.loop; KJ_REQUIRE(&loop == threadLocalEventLoop, "WaitScope not valid for this thread."); KJ_IF_MAYBE(fiber, waitScope.fiber) { if (fiber->state == FiberBase::CANCELED) { result.addException(fiberCanceledException()); return; } KJ_REQUIRE(fiber->state == FiberBase::RUNNING, "This WaitScope can only be used within the fiber that created it."); node->setSelfPointer(&node); node->onReady(fiber); fiber->currentInner = node; KJ_DEFER(fiber->currentInner = nullptr); // Switch to the main stack to run the event loop. fiber->state = FiberBase::WAITING; fiber->switchToMain(); // The main stack switched back to us, meaning either the event we registered with // node->onReady() fired, or we are being canceled by FiberBase's destructor. if (fiber->state == FiberBase::CANCELED) { result.addException(fiberCanceledException()); return; } KJ_ASSERT(fiber->state == FiberBase::RUNNING); } else { KJ_REQUIRE(!loop.running, "wait() is not allowed from within event callbacks."); BoolEvent doneEvent; node->setSelfPointer(&node); node->onReady(&doneEvent); loop.running = true; KJ_DEFER(loop.running = false); uint counter = 0; while (!doneEvent.fired) { if (!loop.turn()) { // No events in the queue. Wait for callback. counter = 0; loop.wait(); } else if (++counter > waitScope.busyPollInterval) { // Note: It's intentional that if busyPollInterval is kj::maxValue, we never poll. counter = 0; loop.poll(); } } loop.setRunnable(loop.isRunnable()); } node->get(result); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { node = nullptr; })) { result.addException(kj::mv(*exception)); } } bool pollImpl(_::PromiseNode& node, WaitScope& waitScope) { EventLoop& loop = waitScope.loop; KJ_REQUIRE(&loop == threadLocalEventLoop, "WaitScope not valid for this thread."); KJ_REQUIRE(waitScope.fiber == nullptr, "poll() is not supported in fibers."); KJ_REQUIRE(!loop.running, "poll() is not allowed from within event callbacks."); BoolEvent doneEvent; node.onReady(&doneEvent); loop.running = true; KJ_DEFER(loop.running = false); while (!doneEvent.fired) { if (!loop.turn()) { // No events in the queue. Poll for I/O. loop.poll(); if (!doneEvent.fired && !loop.isRunnable()) { // No progress. Give up. node.onReady(nullptr); loop.setRunnable(false); return false; } } } loop.setRunnable(loop.isRunnable()); return true; } Promise yield() { return _::PromiseNode::to>(kj::heap()); } Promise yieldHarder() { return _::PromiseNode::to>(kj::heap()); } Own neverDone() { return kj::heap(); } void NeverDone::wait(WaitScope& waitScope) const { ExceptionOr dummy; waitImpl(neverDone(), dummy, waitScope); KJ_UNREACHABLE; } void detach(kj::Promise&& promise) { EventLoop& loop = currentEventLoop(); KJ_REQUIRE(loop.daemons.get() != nullptr, "EventLoop is shutting down.") { return; } loop.daemons->add(kj::mv(promise)); } Event::Event() : loop(currentEventLoop()), next(nullptr), prev(nullptr) {} Event::Event(kj::EventLoop& loop) : loop(loop), next(nullptr), prev(nullptr) {} Event::~Event() noexcept(false) { disarm(); KJ_REQUIRE(!firing, "Promise callback destroyed itself."); } void Event::armDepthFirst() { KJ_REQUIRE(threadLocalEventLoop == &loop || threadLocalEventLoop == nullptr, "Event armed from different thread than it was created in. You must use " "Executor to queue events cross-thread."); if (prev == nullptr) { next = *loop.depthFirstInsertPoint; prev = loop.depthFirstInsertPoint; *prev = this; if (next != nullptr) { next->prev = &next; } loop.depthFirstInsertPoint = &next; if (loop.breadthFirstInsertPoint == prev) { loop.breadthFirstInsertPoint = &next; } if (loop.tail == prev) { loop.tail = &next; } loop.setRunnable(true); } } void Event::armBreadthFirst() { KJ_REQUIRE(threadLocalEventLoop == &loop || threadLocalEventLoop == nullptr, "Event armed from different thread than it was created in. You must use " "Executor to queue events cross-thread."); if (prev == nullptr) { next = *loop.breadthFirstInsertPoint; prev = loop.breadthFirstInsertPoint; *prev = this; if (next != nullptr) { next->prev = &next; } loop.breadthFirstInsertPoint = &next; if (loop.tail == prev) { loop.tail = &next; } loop.setRunnable(true); } } void Event::armLast() { KJ_REQUIRE(threadLocalEventLoop == &loop || threadLocalEventLoop == nullptr, "Event armed from different thread than it was created in. You must use " "Executor to queue events cross-thread."); if (prev == nullptr) { next = *loop.breadthFirstInsertPoint; prev = loop.breadthFirstInsertPoint; *prev = this; if (next != nullptr) { next->prev = &next; } // We don't update loop.breadthFirstInsertPoint because we want further inserts to go *before* // this event. if (loop.tail == prev) { loop.tail = &next; } loop.setRunnable(true); } } void Event::disarm() { if (prev != nullptr) { if (threadLocalEventLoop != &loop && threadLocalEventLoop != nullptr) { KJ_LOG(FATAL, "Promise destroyed from a different thread than it was created in."); // There's no way out of this place without UB, so abort now. abort(); } if (loop.tail == &next) { loop.tail = prev; } if (loop.depthFirstInsertPoint == &next) { loop.depthFirstInsertPoint = prev; } if (loop.breadthFirstInsertPoint == &next) { loop.breadthFirstInsertPoint = prev; } *prev = next; if (next != nullptr) { next->prev = prev; } prev = nullptr; next = nullptr; } } _::PromiseNode* Event::getInnerForTrace() { return nullptr; } #if !KJ_NO_RTTI #if __GNUC__ static kj::String demangleTypeName(const char* name) { int status; char* buf = abi::__cxa_demangle(name, nullptr, nullptr, &status); kj::String result = kj::heapString(buf == nullptr ? name : buf); free(buf); return kj::mv(result); } #else static kj::String demangleTypeName(const char* name) { return kj::heapString(name); } #endif #endif static kj::String traceImpl(Event* event, _::PromiseNode* node) { #if KJ_NO_RTTI return heapString("Trace not available because RTTI is disabled."); #else kj::Vector trace; if (event != nullptr) { trace.add(demangleTypeName(typeid(*event).name())); } while (node != nullptr) { trace.add(demangleTypeName(typeid(*node).name())); node = node->getInnerForTrace(); } return strArray(trace, "\n"); #endif } kj::String Event::trace() { return traceImpl(this, getInnerForTrace()); } } // namespace _ (private) // ======================================================================================= namespace _ { // private kj::String PromiseBase::trace() { return traceImpl(nullptr, node); } void PromiseNode::setSelfPointer(Own* selfPtr) noexcept {} PromiseNode* PromiseNode::getInnerForTrace() { return nullptr; } void PromiseNode::OnReadyEvent::init(Event* newEvent) { if (event == _kJ_ALREADY_READY) { // A new continuation was added to a promise that was already ready. In this case, we schedule // breadth-first, to make it difficult for applications to accidentally starve the event loop // by repeatedly waiting on immediate promises. if (newEvent) newEvent->armBreadthFirst(); } else { event = newEvent; } } void PromiseNode::OnReadyEvent::arm() { KJ_ASSERT(event != _kJ_ALREADY_READY, "arm() should only be called once"); if (event != nullptr) { // A promise resolved and an event is already waiting on it. In this case, arm in depth-first // order so that the event runs immediately after the current one. This way, chained promises // execute together for better cache locality and lower latency. event->armDepthFirst(); } event = _kJ_ALREADY_READY; } void PromiseNode::OnReadyEvent::armBreadthFirst() { KJ_ASSERT(event != _kJ_ALREADY_READY, "armBreadthFirst() should only be called once"); if (event != nullptr) { // A promise resolved and an event is already waiting on it. event->armBreadthFirst(); } event = _kJ_ALREADY_READY; } // ------------------------------------------------------------------- ImmediatePromiseNodeBase::ImmediatePromiseNodeBase() {} ImmediatePromiseNodeBase::~ImmediatePromiseNodeBase() noexcept(false) {} void ImmediatePromiseNodeBase::onReady(Event* event) noexcept { if (event) event->armBreadthFirst(); } ImmediateBrokenPromiseNode::ImmediateBrokenPromiseNode(Exception&& exception) : exception(kj::mv(exception)) {} void ImmediateBrokenPromiseNode::get(ExceptionOrValue& output) noexcept { output.exception = kj::mv(exception); } // ------------------------------------------------------------------- AttachmentPromiseNodeBase::AttachmentPromiseNodeBase(Own&& dependencyParam) : dependency(kj::mv(dependencyParam)) { dependency->setSelfPointer(&dependency); } void AttachmentPromiseNodeBase::onReady(Event* event) noexcept { dependency->onReady(event); } void AttachmentPromiseNodeBase::get(ExceptionOrValue& output) noexcept { dependency->get(output); } PromiseNode* AttachmentPromiseNodeBase::getInnerForTrace() { return dependency; } void AttachmentPromiseNodeBase::dropDependency() { dependency = nullptr; } // ------------------------------------------------------------------- TransformPromiseNodeBase::TransformPromiseNodeBase( Own&& dependencyParam, void* continuationTracePtr) : dependency(kj::mv(dependencyParam)), continuationTracePtr(continuationTracePtr) { dependency->setSelfPointer(&dependency); } void TransformPromiseNodeBase::onReady(Event* event) noexcept { dependency->onReady(event); } void TransformPromiseNodeBase::get(ExceptionOrValue& output) noexcept { KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { getImpl(output); dropDependency(); })) { output.addException(kj::mv(*exception)); } } PromiseNode* TransformPromiseNodeBase::getInnerForTrace() { return dependency; } void TransformPromiseNodeBase::dropDependency() { dependency = nullptr; } void TransformPromiseNodeBase::getDepResult(ExceptionOrValue& output) { dependency->get(output); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { dependency = nullptr; })) { output.addException(kj::mv(*exception)); } KJ_IF_MAYBE(e, output.exception) { e->addTrace(continuationTracePtr); } } // ------------------------------------------------------------------- ForkBranchBase::ForkBranchBase(Own&& hubParam): hub(kj::mv(hubParam)) { if (hub->tailBranch == nullptr) { onReadyEvent.arm(); } else { // Insert into hub's linked list of branches. prevPtr = hub->tailBranch; *prevPtr = this; next = nullptr; hub->tailBranch = &next; } } ForkBranchBase::~ForkBranchBase() noexcept(false) { if (prevPtr != nullptr) { // Remove from hub's linked list of branches. *prevPtr = next; (next == nullptr ? hub->tailBranch : next->prevPtr) = prevPtr; } } void ForkBranchBase::hubReady() noexcept { onReadyEvent.arm(); } void ForkBranchBase::releaseHub(ExceptionOrValue& output) { KJ_IF_MAYBE(exception, kj::runCatchingExceptions([this]() { hub = nullptr; })) { output.addException(kj::mv(*exception)); } } void ForkBranchBase::onReady(Event* event) noexcept { onReadyEvent.init(event); } PromiseNode* ForkBranchBase::getInnerForTrace() { return hub->getInnerForTrace(); } // ------------------------------------------------------------------- ForkHubBase::ForkHubBase(Own&& innerParam, ExceptionOrValue& resultRef) : inner(kj::mv(innerParam)), resultRef(resultRef) { inner->setSelfPointer(&inner); inner->onReady(this); } Maybe> ForkHubBase::fire() { // Dependency is ready. Fetch its result and then delete the node. inner->get(resultRef); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([this]() { inner = nullptr; })) { resultRef.addException(kj::mv(*exception)); } for (auto branch = headBranch; branch != nullptr; branch = branch->next) { branch->hubReady(); *branch->prevPtr = nullptr; branch->prevPtr = nullptr; } *tailBranch = nullptr; // Indicate that the list is no longer active. tailBranch = nullptr; return nullptr; } _::PromiseNode* ForkHubBase::getInnerForTrace() { return inner; } // ------------------------------------------------------------------- ChainPromiseNode::ChainPromiseNode(Own innerParam) : state(STEP1), inner(kj::mv(innerParam)) { inner->setSelfPointer(&inner); inner->onReady(this); } ChainPromiseNode::~ChainPromiseNode() noexcept(false) {} void ChainPromiseNode::onReady(Event* event) noexcept { switch (state) { case STEP1: onReadyEvent = event; return; case STEP2: inner->onReady(event); return; } KJ_UNREACHABLE; } void ChainPromiseNode::setSelfPointer(Own* selfPtr) noexcept { if (state == STEP2) { *selfPtr = kj::mv(inner); // deletes this! selfPtr->get()->setSelfPointer(selfPtr); } else { this->selfPtr = selfPtr; } } void ChainPromiseNode::get(ExceptionOrValue& output) noexcept { KJ_REQUIRE(state == STEP2); return inner->get(output); } PromiseNode* ChainPromiseNode::getInnerForTrace() { return inner; } Maybe> ChainPromiseNode::fire() { KJ_REQUIRE(state != STEP2); static_assert(sizeof(Promise) == sizeof(PromiseBase), "This code assumes Promise does not add any new members to PromiseBase."); ExceptionOr intermediate; inner->get(intermediate); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([this]() { inner = nullptr; })) { intermediate.addException(kj::mv(*exception)); } KJ_IF_MAYBE(exception, intermediate.exception) { // There is an exception. If there is also a value, delete it. kj::runCatchingExceptions([&]() { intermediate.value = nullptr; }); // Now set step2 to a rejected promise. inner = heap(kj::mv(*exception)); } else KJ_IF_MAYBE(value, intermediate.value) { // There is a value and no exception. The value is itself a promise. Adopt it as our // step2. inner = _::PromiseNode::from(kj::mv(*value)); } else { // We can only get here if inner->get() returned neither an exception nor a // value, which never actually happens. KJ_FAIL_ASSERT("Inner node returned empty value."); } state = STEP2; if (selfPtr != nullptr) { // Hey, we can shorten the chain here. auto chain = selfPtr->downcast(); *selfPtr = kj::mv(inner); selfPtr->get()->setSelfPointer(selfPtr); if (onReadyEvent != nullptr) { selfPtr->get()->onReady(onReadyEvent); } // Return our self-pointer so that the caller takes care of deleting it. return Own(kj::mv(chain)); } else { inner->setSelfPointer(&inner); if (onReadyEvent != nullptr) { inner->onReady(onReadyEvent); } return nullptr; } } // ------------------------------------------------------------------- ExclusiveJoinPromiseNode::ExclusiveJoinPromiseNode(Own left, Own right) : left(*this, kj::mv(left)), right(*this, kj::mv(right)) {} ExclusiveJoinPromiseNode::~ExclusiveJoinPromiseNode() noexcept(false) {} void ExclusiveJoinPromiseNode::onReady(Event* event) noexcept { onReadyEvent.init(event); } void ExclusiveJoinPromiseNode::get(ExceptionOrValue& output) noexcept { KJ_REQUIRE(left.get(output) || right.get(output), "get() called before ready."); } PromiseNode* ExclusiveJoinPromiseNode::getInnerForTrace() { auto result = left.getInnerForTrace(); if (result == nullptr) { result = right.getInnerForTrace(); } return result; } ExclusiveJoinPromiseNode::Branch::Branch( ExclusiveJoinPromiseNode& joinNode, Own dependencyParam) : joinNode(joinNode), dependency(kj::mv(dependencyParam)) { dependency->setSelfPointer(&dependency); dependency->onReady(this); } ExclusiveJoinPromiseNode::Branch::~Branch() noexcept(false) {} bool ExclusiveJoinPromiseNode::Branch::get(ExceptionOrValue& output) { if (dependency) { dependency->get(output); return true; } else { return false; } } Maybe> ExclusiveJoinPromiseNode::Branch::fire() { if (dependency) { // Cancel the branch that didn't return first. Ignore exceptions caused by cancellation. if (this == &joinNode.left) { kj::runCatchingExceptions([&]() { joinNode.right.dependency = nullptr; }); } else { kj::runCatchingExceptions([&]() { joinNode.left.dependency = nullptr; }); } joinNode.onReadyEvent.arm(); } else { // The other branch already fired, and this branch was canceled. It's possible for both // branches to fire if both were armed simultaneously. } return nullptr; } PromiseNode* ExclusiveJoinPromiseNode::Branch::getInnerForTrace() { return dependency; } // ------------------------------------------------------------------- ArrayJoinPromiseNodeBase::ArrayJoinPromiseNodeBase( Array> promises, ExceptionOrValue* resultParts, size_t partSize) : countLeft(promises.size()) { // Make the branches. auto builder = heapArrayBuilder(promises.size()); for (uint i: indices(promises)) { ExceptionOrValue& output = *reinterpret_cast( reinterpret_cast(resultParts) + i * partSize); builder.add(*this, kj::mv(promises[i]), output); } branches = builder.finish(); if (branches.size() == 0) { onReadyEvent.arm(); } } ArrayJoinPromiseNodeBase::~ArrayJoinPromiseNodeBase() noexcept(false) {} void ArrayJoinPromiseNodeBase::onReady(Event* event) noexcept { onReadyEvent.init(event); } void ArrayJoinPromiseNodeBase::get(ExceptionOrValue& output) noexcept { // If any of the elements threw exceptions, propagate them. for (auto& branch: branches) { KJ_IF_MAYBE(exception, branch.getPart()) { output.addException(kj::mv(*exception)); } } if (output.exception == nullptr) { // No errors. The template subclass will need to fill in the result. getNoError(output); } } PromiseNode* ArrayJoinPromiseNodeBase::getInnerForTrace() { return branches.size() == 0 ? nullptr : branches[0].getInnerForTrace(); } ArrayJoinPromiseNodeBase::Branch::Branch( ArrayJoinPromiseNodeBase& joinNode, Own dependencyParam, ExceptionOrValue& output) : joinNode(joinNode), dependency(kj::mv(dependencyParam)), output(output) { dependency->setSelfPointer(&dependency); dependency->onReady(this); } ArrayJoinPromiseNodeBase::Branch::~Branch() noexcept(false) {} Maybe> ArrayJoinPromiseNodeBase::Branch::fire() { if (--joinNode.countLeft == 0) { joinNode.onReadyEvent.arm(); } return nullptr; } _::PromiseNode* ArrayJoinPromiseNodeBase::Branch::getInnerForTrace() { return dependency->getInnerForTrace(); } Maybe ArrayJoinPromiseNodeBase::Branch::getPart() { dependency->get(output); return kj::mv(output.exception); } ArrayJoinPromiseNode::ArrayJoinPromiseNode( Array> promises, Array> resultParts) : ArrayJoinPromiseNodeBase(kj::mv(promises), resultParts.begin(), sizeof(ExceptionOr<_::Void>)), resultParts(kj::mv(resultParts)) {} ArrayJoinPromiseNode::~ArrayJoinPromiseNode() {} void ArrayJoinPromiseNode::getNoError(ExceptionOrValue& output) noexcept { output.as<_::Void>() = _::Void(); } } // namespace _ (private) Promise joinPromises(Array>&& promises) { return _::PromiseNode::to>(kj::heap<_::ArrayJoinPromiseNode>( KJ_MAP(p, promises) { return _::PromiseNode::from(kj::mv(p)); }, heapArray<_::ExceptionOr<_::Void>>(promises.size()))); } namespace _ { // (private) // ------------------------------------------------------------------- EagerPromiseNodeBase::EagerPromiseNodeBase( Own&& dependencyParam, ExceptionOrValue& resultRef) : dependency(kj::mv(dependencyParam)), resultRef(resultRef) { dependency->setSelfPointer(&dependency); dependency->onReady(this); } void EagerPromiseNodeBase::onReady(Event* event) noexcept { onReadyEvent.init(event); } PromiseNode* EagerPromiseNodeBase::getInnerForTrace() { return dependency; } Maybe> EagerPromiseNodeBase::fire() { dependency->get(resultRef); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([this]() { dependency = nullptr; })) { resultRef.addException(kj::mv(*exception)); } onReadyEvent.arm(); return nullptr; } // ------------------------------------------------------------------- void AdapterPromiseNodeBase::onReady(Event* event) noexcept { onReadyEvent.init(event); } // ------------------------------------------------------------------- Promise IdentityFunc>::operator()() const { return READY_NOW; } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/async-unix.c++0000644000175000017500000010317613650101756020547 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if !_WIN32 #include "async-unix.h" #include "debug.h" #include "threadlocal.h" #include #include #include #include #include #include #include #include #if KJ_USE_EPOLL #include #include #include #else #include #include #endif namespace kj { // ======================================================================================= // Signal code common to multiple implementations namespace { int reservedSignal = SIGUSR1; bool tooLateToSetReserved = false; bool capturedChildExit = false; bool threadClaimedChildExits = false; struct SignalCapture { sigjmp_buf jumpTo; siginfo_t siginfo; #if __APPLE__ sigset_t originalMask; // The signal mask to be restored when jumping out of the signal handler. // // "But wait!" you say, "Isn't the whole point of siglongjmp() that it does this for you?" Well, // yes, that is supposed to be the point. However, Apple implemented in wrong. On macOS, // siglongjmp() uses sigprocmask() -- not pthread_sigmask() -- to restore the signal mask. // Unfortunately, sigprocmask() on macOS affects threads other than the current thread. Arguably // this is conformant: sigprocmask() is documented as having unspecified behavior in the presence // of threads, and pthread_sigmask() must be used instead. However, this means siglongjmp() // cannot be used in the presence of threads. // // We'll just have to restore the signal mask ourselves, rather than rely on siglongjmp()... // // ... but we ONLY do that on Apple systems, because it turns out, ironically, on Android, this // hack breaks signal delivery. pthread_sigmask() vs. sigprocmask() is not the issue; we // apparently MUST let siglongjmp() itself deal with the signal mask, otherwise various tests in // async-unix-test.c++ end up hanging (I haven't gotten to the bottom of why). Note that on stock // Linux, _either_ strategy works fine; this appears to be a problem with Android's Bionic libc. // Since letting siglongjmp() do the work _seeems_ more "correct", we'll make it the default and // only do something different on Apple platforms. #define KJ_BROKEN_SIGLONGJMP 1 #endif }; #if !KJ_USE_EPOLL // on Linux we'll use signalfd KJ_THREADLOCAL_PTR(SignalCapture) threadCapture = nullptr; void signalHandler(int, siginfo_t* siginfo, void*) { SignalCapture* capture = threadCapture; if (capture != nullptr) { capture->siginfo = *siginfo; #if KJ_BROKEN_SIGLONGJMP // See comments on SignalCapture::originalMask, above: We can't rely on siglongjmp() to restore // the signal mask; we must do it ourselves using pthread_sigmask(). We pass false as the // second parameter to siglongjmp() so that it skips changing the signal mask. This makes it // equivalent to `longjmp()` on Linux or `_longjmp()` on BSD/macOS. See comments on // SignalCapture::originalMask for explanation. pthread_sigmask(SIG_SETMASK, &capture->originalMask, nullptr); siglongjmp(capture->jumpTo, false); #else siglongjmp(capture->jumpTo, true); #endif } } #endif void registerSignalHandler(int signum) { tooLateToSetReserved = true; sigset_t mask; KJ_SYSCALL(sigemptyset(&mask)); KJ_SYSCALL(sigaddset(&mask, signum)); KJ_SYSCALL(pthread_sigmask(SIG_BLOCK, &mask, nullptr)); #if !KJ_USE_EPOLL // on Linux we'll use signalfd struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_sigaction = &signalHandler; KJ_SYSCALL(sigfillset(&action.sa_mask)); action.sa_flags = SA_SIGINFO; KJ_SYSCALL(sigaction(signum, &action, nullptr)); #endif } #if !KJ_USE_EPOLL && !KJ_USE_PIPE_FOR_WAKEUP void registerReservedSignal() { registerSignalHandler(reservedSignal); } #endif void ignoreSigpipe() { // We disable SIGPIPE because users of UnixEventPort almost certainly don't want it. while (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { int error = errno; if (error != EINTR) { KJ_FAIL_SYSCALL("signal(SIGPIPE, SIG_IGN)", error); } } } } // namespace struct UnixEventPort::ChildSet { std::map waiters; void checkExits(); }; class UnixEventPort::ChildExitPromiseAdapter { public: inline ChildExitPromiseAdapter(PromiseFulfiller& fulfiller, ChildSet& childSet, Maybe& pidRef) : childSet(childSet), pid(KJ_REQUIRE_NONNULL(pidRef, "`pid` must be non-null at the time `onChildExit()` is called")), pidRef(pidRef), fulfiller(fulfiller) { KJ_REQUIRE(childSet.waiters.insert(std::make_pair(pid, this)).second, "already called onChildExit() for this pid"); } ~ChildExitPromiseAdapter() noexcept(false) { childSet.waiters.erase(pid); } ChildSet& childSet; pid_t pid; Maybe& pidRef; PromiseFulfiller& fulfiller; }; void UnixEventPort::ChildSet::checkExits() { for (;;) { int status; pid_t pid; KJ_SYSCALL_HANDLE_ERRORS(pid = waitpid(-1, &status, WNOHANG)) { case ECHILD: return; default: KJ_FAIL_SYSCALL("waitpid()", error); } if (pid == 0) break; auto iter = waiters.find(pid); if (iter != waiters.end()) { iter->second->pidRef = nullptr; iter->second->fulfiller.fulfill(kj::cp(status)); } } } Promise UnixEventPort::onChildExit(Maybe& pid) { KJ_REQUIRE(capturedChildExit, "must call UnixEventPort::captureChildExit() to use onChildExit()."); ChildSet* cs; KJ_IF_MAYBE(c, childSet) { cs = *c; } else { // In theory we should do an atomic compare-and-swap on threadClaimedChildExits, but this is // for debug purposes only so it's not a big deal. KJ_REQUIRE(!threadClaimedChildExits, "only one UnixEvertPort per process may listen for child exits"); threadClaimedChildExits = true; auto newChildSet = kj::heap(); cs = newChildSet; childSet = kj::mv(newChildSet); } return kj::newAdaptedPromise(*cs, pid); } void UnixEventPort::captureChildExit() { captureSignal(SIGCHLD); capturedChildExit = true; } class UnixEventPort::SignalPromiseAdapter { public: inline SignalPromiseAdapter(PromiseFulfiller& fulfiller, UnixEventPort& loop, int signum) : loop(loop), signum(signum), fulfiller(fulfiller) { prev = loop.signalTail; *loop.signalTail = this; loop.signalTail = &next; } ~SignalPromiseAdapter() noexcept(false) { if (prev != nullptr) { if (next == nullptr) { loop.signalTail = prev; } else { next->prev = prev; } *prev = next; } } SignalPromiseAdapter* removeFromList() { auto result = next; if (next == nullptr) { loop.signalTail = prev; } else { next->prev = prev; } *prev = next; next = nullptr; prev = nullptr; return result; } UnixEventPort& loop; int signum; PromiseFulfiller& fulfiller; SignalPromiseAdapter* next = nullptr; SignalPromiseAdapter** prev = nullptr; }; Promise UnixEventPort::onSignal(int signum) { KJ_REQUIRE(signum != SIGCHLD || !capturedChildExit, "can't call onSigal(SIGCHLD) when kj::UnixEventPort::captureChildExit() has been called"); return newAdaptedPromise(*this, signum); } void UnixEventPort::captureSignal(int signum) { if (reservedSignal == SIGUSR1) { KJ_REQUIRE(signum != SIGUSR1, "Sorry, SIGUSR1 is reserved by the UnixEventPort implementation. You may call " "UnixEventPort::setReservedSignal() to reserve a different signal."); } else { KJ_REQUIRE(signum != reservedSignal, "Can't capture signal reserved using setReservedSignal().", signum); } registerSignalHandler(signum); } void UnixEventPort::setReservedSignal(int signum) { KJ_REQUIRE(!tooLateToSetReserved, "setReservedSignal() must be called before any calls to `captureSignal()` and " "before any `UnixEventPort` is constructed."); if (reservedSignal != SIGUSR1 && reservedSignal != signum) { KJ_FAIL_REQUIRE("Detected multiple conflicting calls to setReservedSignal(). Please only " "call this once, or always call it with the same signal number."); } reservedSignal = signum; } void UnixEventPort::gotSignal(const siginfo_t& siginfo) { // If onChildExit() has been called and this is SIGCHLD, check for child exits. KJ_IF_MAYBE(cs, childSet) { if (siginfo.si_signo == SIGCHLD) { cs->get()->checkExits(); return; } } // Fire any events waiting on this signal. auto ptr = signalHead; while (ptr != nullptr) { if (ptr->signum == siginfo.si_signo) { ptr->fulfiller.fulfill(kj::cp(siginfo)); ptr = ptr->removeFromList(); } else { ptr = ptr->next; } } } #if KJ_USE_EPOLL // ======================================================================================= // epoll FdObserver implementation UnixEventPort::UnixEventPort() : clock(systemPreciseMonotonicClock()), timerImpl(clock.now()), epollFd(-1), signalFd(-1), eventFd(-1) { ignoreSigpipe(); int fd; KJ_SYSCALL(fd = epoll_create1(EPOLL_CLOEXEC)); epollFd = AutoCloseFd(fd); KJ_SYSCALL(sigemptyset(&signalFdSigset)); KJ_SYSCALL(fd = signalfd(-1, &signalFdSigset, SFD_NONBLOCK | SFD_CLOEXEC)); signalFd = AutoCloseFd(fd); KJ_SYSCALL(fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)); eventFd = AutoCloseFd(fd); struct epoll_event event; memset(&event, 0, sizeof(event)); event.events = EPOLLIN; event.data.u64 = 0; KJ_SYSCALL(epoll_ctl(epollFd, EPOLL_CTL_ADD, signalFd, &event)); event.data.u64 = 1; KJ_SYSCALL(epoll_ctl(epollFd, EPOLL_CTL_ADD, eventFd, &event)); } UnixEventPort::~UnixEventPort() noexcept(false) { if (childSet != nullptr) { // We had claimed the exclusive right to call onChildExit(). Release that right. threadClaimedChildExits = false; } } UnixEventPort::FdObserver::FdObserver(UnixEventPort& eventPort, int fd, uint flags) : eventPort(eventPort), fd(fd), flags(flags) { struct epoll_event event; memset(&event, 0, sizeof(event)); if (flags & OBSERVE_READ) { event.events |= EPOLLIN | EPOLLRDHUP; } if (flags & OBSERVE_WRITE) { event.events |= EPOLLOUT; } if (flags & OBSERVE_URGENT) { event.events |= EPOLLPRI; } event.events |= EPOLLET; // Set edge-triggered mode. event.data.ptr = this; KJ_SYSCALL(epoll_ctl(eventPort.epollFd, EPOLL_CTL_ADD, fd, &event)); } UnixEventPort::FdObserver::~FdObserver() noexcept(false) { KJ_SYSCALL(epoll_ctl(eventPort.epollFd, EPOLL_CTL_DEL, fd, nullptr)) { break; } } void UnixEventPort::FdObserver::fire(short events) { if (events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR)) { if (events & (EPOLLHUP | EPOLLRDHUP)) { atEnd = true; } else { // Since we didn't receive EPOLLRDHUP, we know that we're not at the end. atEnd = false; } KJ_IF_MAYBE(f, readFulfiller) { f->get()->fulfill(); readFulfiller = nullptr; } } if (events & (EPOLLOUT | EPOLLHUP | EPOLLERR)) { KJ_IF_MAYBE(f, writeFulfiller) { f->get()->fulfill(); writeFulfiller = nullptr; } } if (events & (EPOLLHUP | EPOLLERR)) { KJ_IF_MAYBE(f, hupFulfiller) { f->get()->fulfill(); hupFulfiller = nullptr; } } if (events & EPOLLPRI) { KJ_IF_MAYBE(f, urgentFulfiller) { f->get()->fulfill(); urgentFulfiller = nullptr; } } } Promise UnixEventPort::FdObserver::whenBecomesReadable() { KJ_REQUIRE(flags & OBSERVE_READ, "FdObserver was not set to observe reads."); auto paf = newPromiseAndFulfiller(); readFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Promise UnixEventPort::FdObserver::whenBecomesWritable() { KJ_REQUIRE(flags & OBSERVE_WRITE, "FdObserver was not set to observe writes."); auto paf = newPromiseAndFulfiller(); writeFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Promise UnixEventPort::FdObserver::whenUrgentDataAvailable() { KJ_REQUIRE(flags & OBSERVE_URGENT, "FdObserver was not set to observe availability of urgent data."); auto paf = newPromiseAndFulfiller(); urgentFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Promise UnixEventPort::FdObserver::whenWriteDisconnected() { auto paf = newPromiseAndFulfiller(); hupFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } bool UnixEventPort::wait() { return doEpollWait( timerImpl.timeoutToNextEvent(clock.now(), MILLISECONDS, int(maxValue)) .map([](uint64_t t) -> int { return t; }) .orDefault(-1)); } bool UnixEventPort::poll() { return doEpollWait(0); } void UnixEventPort::wake() const { uint64_t one = 1; ssize_t n; KJ_NONBLOCKING_SYSCALL(n = write(eventFd, &one, sizeof(one))); KJ_ASSERT(n < 0 || n == sizeof(one)); } static siginfo_t toRegularSiginfo(const struct signalfd_siginfo& siginfo) { // Unfortunately, siginfo_t is mostly a big union and the correct set of fields to fill in // depends on the type of signal. OTOH, signalfd_siginfo is a flat struct that expands all // siginfo_t's union fields out to be non-overlapping. We can't just copy all the fields over // because of the unions; we have to carefully figure out which fields are appropriate to fill // in for this signal. Ick. siginfo_t result; memset(&result, 0, sizeof(result)); result.si_signo = siginfo.ssi_signo; result.si_errno = siginfo.ssi_errno; result.si_code = siginfo.ssi_code; if (siginfo.ssi_code > 0) { // Signal originated from the kernel. The structure of the siginfo depends primarily on the // signal number. switch (siginfo.ssi_signo) { case SIGCHLD: result.si_pid = siginfo.ssi_pid; result.si_uid = siginfo.ssi_uid; result.si_status = siginfo.ssi_status; result.si_utime = siginfo.ssi_utime; result.si_stime = siginfo.ssi_stime; break; case SIGILL: case SIGFPE: case SIGSEGV: case SIGBUS: case SIGTRAP: result.si_addr = reinterpret_cast(static_cast(siginfo.ssi_addr)); #ifdef si_trapno result.si_trapno = siginfo.ssi_trapno; #endif #ifdef si_addr_lsb // ssi_addr_lsb is defined as coming immediately after ssi_addr in the kernel headers but // apparently the userspace headers were never updated. So we do a pointer hack. :( result.si_addr_lsb = *reinterpret_cast(&siginfo.ssi_addr + 1); #endif break; case SIGIO: static_assert(SIGIO == SIGPOLL, "SIGIO != SIGPOLL?"); // Note: Technically, code can arrange for SIGIO signals to be delivered with a signal number // other than SIGIO. AFAICT there is no way for us to detect this in the siginfo. Luckily // SIGIO is totally obsoleted by epoll so it shouldn't come up. result.si_band = siginfo.ssi_band; result.si_fd = siginfo.ssi_fd; break; case SIGSYS: // Apparently SIGSYS's fields are not available in signalfd_siginfo? break; } } else { // Signal originated from userspace. The sender could specify whatever signal number they // wanted. The structure of the signal is determined by the API they used, which is identified // by SI_CODE. switch (siginfo.ssi_code) { case SI_USER: case SI_TKILL: // kill(), tkill(), or tgkill(). result.si_pid = siginfo.ssi_pid; result.si_uid = siginfo.ssi_uid; break; case SI_QUEUE: case SI_MESGQ: case SI_ASYNCIO: default: result.si_pid = siginfo.ssi_pid; result.si_uid = siginfo.ssi_uid; // This is awkward. In siginfo_t, si_ptr and si_int are in a union together. In // signalfd_siginfo, they are not. We don't really know whether the app intended to send // an int or a pointer. Presumably since the pointer is always larger than the int, if // we write the pointer, we'll end up with the right value for the int? Presumably the // two fields of signalfd_siginfo are actually extracted from one of these unions // originally, so actually contain redundant data? Better write some tests... // // Making matters even stranger, siginfo.ssi_ptr is 64-bit even on 32-bit systems, and // it appears that instead of doing the obvious thing by casting the pointer value to // 64 bits, the kernel actually memcpy()s the 32-bit value into the 64-bit space. As // a result, on big-endian 32-bit systems, the original pointer value ends up in the // *upper* 32 bits of siginfo.ssi_ptr, which is totally weird. We play along and use // a memcpy() on our end too, to get the right result on all platforms. memcpy(&result.si_ptr, &siginfo.ssi_ptr, sizeof(result.si_ptr)); break; case SI_TIMER: result.si_timerid = siginfo.ssi_tid; result.si_overrun = siginfo.ssi_overrun; // Again with this weirdness... result.si_ptr = reinterpret_cast(static_cast(siginfo.ssi_ptr)); break; } } return result; } bool UnixEventPort::doEpollWait(int timeout) { sigset_t newMask; sigemptyset(&newMask); { auto ptr = signalHead; while (ptr != nullptr) { sigaddset(&newMask, ptr->signum); ptr = ptr->next; } if (childSet != nullptr) { sigaddset(&newMask, SIGCHLD); } } if (memcmp(&newMask, &signalFdSigset, sizeof(newMask)) != 0) { // Apparently we're not waiting on the same signals as last time. Need to update the signal // FD's mask. signalFdSigset = newMask; KJ_SYSCALL(signalfd(signalFd, &signalFdSigset, SFD_NONBLOCK | SFD_CLOEXEC)); } struct epoll_event events[16]; int n = epoll_wait(epollFd, events, kj::size(events), timeout); if (n < 0) { int error = errno; if (error == EINTR) { // We can't simply restart the epoll call because we need to recompute the timeout. Instead, // we pretend epoll_wait() returned zero events. This will cause the event loop to spin once, // decide it has nothing to do, recompute timeouts, then return to waiting. n = 0; } else { KJ_FAIL_SYSCALL("epoll_wait()", error); } } bool woken = false; for (int i = 0; i < n; i++) { if (events[i].data.u64 == 0) { for (;;) { struct signalfd_siginfo siginfo; ssize_t n; KJ_NONBLOCKING_SYSCALL(n = read(signalFd, &siginfo, sizeof(siginfo))); if (n < 0) break; // no more signals KJ_ASSERT(n == sizeof(siginfo)); gotSignal(toRegularSiginfo(siginfo)); #ifdef SIGRTMIN if (siginfo.ssi_signo >= SIGRTMIN) { // This is an RT signal. There could be multiple copies queued. We need to remove it from // the signalfd's signal mask before we continue, to avoid accidentally reading and // discarding the extra copies. // TODO(perf): If high throughput of RT signals is desired then perhaps we should read // them all into userspace and queue them here. Maybe we even need a better interface // than onSignal() for receiving high-volume RT signals. KJ_SYSCALL(sigdelset(&signalFdSigset, siginfo.ssi_signo)); KJ_SYSCALL(signalfd(signalFd, &signalFdSigset, SFD_NONBLOCK | SFD_CLOEXEC)); } #endif } } else if (events[i].data.u64 == 1) { // Someone called wake() from another thread. Consume the event. uint64_t value; ssize_t n; KJ_NONBLOCKING_SYSCALL(n = read(eventFd, &value, sizeof(value))); KJ_ASSERT(n < 0 || n == sizeof(value)); // We were woken. Need to return true. woken = true; } else { FdObserver* observer = reinterpret_cast(events[i].data.ptr); observer->fire(events[i].events); } } timerImpl.advanceTo(clock.now()); return woken; } #else // KJ_USE_EPOLL // ======================================================================================= // Traditional poll() FdObserver implementation. #ifndef POLLRDHUP #define POLLRDHUP 0 #endif UnixEventPort::UnixEventPort() : clock(systemPreciseMonotonicClock()), timerImpl(clock.now()) { #if KJ_USE_PIPE_FOR_WAKEUP // Allocate a pipe to which we'll write a byte in order to wake this thread. int fds[2]; KJ_SYSCALL(pipe(fds)); wakePipeIn = kj::AutoCloseFd(fds[0]); wakePipeOut = kj::AutoCloseFd(fds[1]); KJ_SYSCALL(fcntl(wakePipeIn, F_SETFD, FD_CLOEXEC)); KJ_SYSCALL(fcntl(wakePipeOut, F_SETFD, FD_CLOEXEC)); #else static_assert(sizeof(threadId) >= sizeof(pthread_t), "pthread_t is larger than a long long on your platform. Please port."); *reinterpret_cast(&threadId) = pthread_self(); // Note: We used to use a pthread_once to call registerReservedSignal() only once per process. // This didn't work correctly because registerReservedSignal() not only registers the // (process-wide) signal handler, but also sets the (per-thread) signal mask to block the // signal. Thus, if threads were spawned before the first UnixEventPort was created, and then // multiple threads created UnixEventPorts, only one of them would have the signal properly // blocked. We could have changed things so that only the handler registration was protected // by the pthread_once and the mask update happened in every thread, but registering a signal // handler is not an expensive operation, so whatever... we'll do it in every thread. registerReservedSignal(); #endif ignoreSigpipe(); } UnixEventPort::~UnixEventPort() noexcept(false) {} UnixEventPort::FdObserver::FdObserver(UnixEventPort& eventPort, int fd, uint flags) : eventPort(eventPort), fd(fd), flags(flags), next(nullptr), prev(nullptr) {} UnixEventPort::FdObserver::~FdObserver() noexcept(false) { if (prev != nullptr) { if (next == nullptr) { eventPort.observersTail = prev; } else { next->prev = prev; } *prev = next; } } void UnixEventPort::FdObserver::fire(short events) { if (events & (POLLIN | POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)) { if (events & (POLLHUP | POLLRDHUP)) { atEnd = true; #if POLLRDHUP } else { // Since POLLRDHUP exists on this platform, and we didn't receive it, we know that we're not // at the end. atEnd = false; #endif } KJ_IF_MAYBE(f, readFulfiller) { f->get()->fulfill(); readFulfiller = nullptr; } } if (events & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) { KJ_IF_MAYBE(f, writeFulfiller) { f->get()->fulfill(); writeFulfiller = nullptr; } } if (events & (POLLHUP | POLLERR | POLLNVAL)) { KJ_IF_MAYBE(f, hupFulfiller) { f->get()->fulfill(); hupFulfiller = nullptr; } } if (events & POLLPRI) { KJ_IF_MAYBE(f, urgentFulfiller) { f->get()->fulfill(); urgentFulfiller = nullptr; } } if (readFulfiller == nullptr && writeFulfiller == nullptr && urgentFulfiller == nullptr && hupFulfiller == nullptr) { // Remove from list. if (next == nullptr) { eventPort.observersTail = prev; } else { next->prev = prev; } *prev = next; next = nullptr; prev = nullptr; } } short UnixEventPort::FdObserver::getEventMask() { return (readFulfiller == nullptr ? 0 : (POLLIN | POLLRDHUP)) | (writeFulfiller == nullptr ? 0 : POLLOUT) | (urgentFulfiller == nullptr ? 0 : POLLPRI) | // The POSIX standard says POLLHUP and POLLERR will be reported even if not requested. // But on MacOS, if `events` is 0, then POLLHUP apparently will not be reported: // https://openradar.appspot.com/37537852 // It seems that by settingc any non-zero value -- even one documented as ignored -- we // cause POLLHUP to be reported. Both POLLHUP and POLLERR are documented as being ignored. // So, we'll go ahead and set them. This has no effect on non-broken OSs, causes MacOS to // do the right thing, and sort of looks as if we're explicitly requesting notification of // these two conditions, which we do after all want to know about. POLLHUP | POLLERR; } Promise UnixEventPort::FdObserver::whenBecomesReadable() { KJ_REQUIRE(flags & OBSERVE_READ, "FdObserver was not set to observe reads."); if (prev == nullptr) { KJ_DASSERT(next == nullptr); prev = eventPort.observersTail; *prev = this; eventPort.observersTail = &next; } auto paf = newPromiseAndFulfiller(); readFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Promise UnixEventPort::FdObserver::whenBecomesWritable() { KJ_REQUIRE(flags & OBSERVE_WRITE, "FdObserver was not set to observe writes."); if (prev == nullptr) { KJ_DASSERT(next == nullptr); prev = eventPort.observersTail; *prev = this; eventPort.observersTail = &next; } auto paf = newPromiseAndFulfiller(); writeFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Promise UnixEventPort::FdObserver::whenUrgentDataAvailable() { KJ_REQUIRE(flags & OBSERVE_URGENT, "FdObserver was not set to observe availability of urgent data."); if (prev == nullptr) { KJ_DASSERT(next == nullptr); prev = eventPort.observersTail; *prev = this; eventPort.observersTail = &next; } auto paf = newPromiseAndFulfiller(); urgentFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } Promise UnixEventPort::FdObserver::whenWriteDisconnected() { if (prev == nullptr) { KJ_DASSERT(next == nullptr); prev = eventPort.observersTail; *prev = this; eventPort.observersTail = &next; } auto paf = newPromiseAndFulfiller(); hupFulfiller = kj::mv(paf.fulfiller); return kj::mv(paf.promise); } class UnixEventPort::PollContext { public: PollContext(UnixEventPort& port) { for (FdObserver* ptr = port.observersHead; ptr != nullptr; ptr = ptr->next) { struct pollfd pollfd; memset(&pollfd, 0, sizeof(pollfd)); pollfd.fd = ptr->fd; pollfd.events = ptr->getEventMask(); pollfds.add(pollfd); pollEvents.add(ptr); } #if KJ_USE_PIPE_FOR_WAKEUP { struct pollfd pollfd; memset(&pollfd, 0, sizeof(pollfd)); pollfd.fd = port.wakePipeIn; pollfd.events = POLLIN; pollfds.add(pollfd); } #endif } void run(int timeout) { pollResult = ::poll(pollfds.begin(), pollfds.size(), timeout); pollError = pollResult < 0 ? errno : 0; if (pollError == EINTR) { // We can't simply restart the poll call because we need to recompute the timeout. Instead, // we pretend poll() returned zero events. This will cause the event loop to spin once, // decide it has nothing to do, recompute timeouts, then return to waiting. pollResult = 0; pollError = 0; } } bool processResults() { if (pollResult < 0) { KJ_FAIL_SYSCALL("poll()", pollError); } bool woken = false; for (auto i: indices(pollfds)) { if (pollfds[i].revents != 0) { #if KJ_USE_PIPE_FOR_WAKEUP if (i == pollEvents.size()) { // The last pollfd is our cross-thread wake pipe. woken = true; // Discard junk in the wake pipe. char junk[256]; ssize_t n; do { KJ_NONBLOCKING_SYSCALL(n = read(pollfds[i].fd, junk, sizeof(junk))); } while (n >= 256); } else { #endif pollEvents[i]->fire(pollfds[i].revents); #if KJ_USE_PIPE_FOR_WAKEUP } #endif if (--pollResult <= 0) { break; } } } return woken; } private: kj::Vector pollfds; kj::Vector pollEvents; int pollResult = 0; int pollError = 0; }; bool UnixEventPort::wait() { sigset_t newMask; sigemptyset(&newMask); #if !KJ_USE_PIPE_FOR_WAKEUP sigaddset(&newMask, reservedSignal); #endif { auto ptr = signalHead; while (ptr != nullptr) { sigaddset(&newMask, ptr->signum); ptr = ptr->next; } if (childSet != nullptr) { sigaddset(&newMask, SIGCHLD); } } PollContext pollContext(*this); // Capture signals. SignalCapture capture; #if KJ_BROKEN_SIGLONGJMP if (sigsetjmp(capture.jumpTo, false)) { #else if (sigsetjmp(capture.jumpTo, true)) { #endif // We received a signal and longjmp'd back out of the signal handler. threadCapture = nullptr; #if !KJ_USE_PIPE_FOR_WAKEUP if (capture.siginfo.si_signo == reservedSignal) { return true; } else { #endif gotSignal(capture.siginfo); return false; #if !KJ_USE_PIPE_FOR_WAKEUP } #endif } // Enable signals, run the poll, then mask them again. #if KJ_BROKEN_SIGLONGJMP auto& originalMask = capture.originalMask; #else sigset_t originalMask; #endif threadCapture = &capture; pthread_sigmask(SIG_UNBLOCK, &newMask, &originalMask); pollContext.run( timerImpl.timeoutToNextEvent(clock.now(), MILLISECONDS, int(maxValue)) .map([](uint64_t t) -> int { return t; }) .orDefault(-1)); pthread_sigmask(SIG_SETMASK, &originalMask, nullptr); threadCapture = nullptr; // Queue events. bool result = pollContext.processResults(); timerImpl.advanceTo(clock.now()); return result; } bool UnixEventPort::poll() { // volatile so that longjmp() doesn't clobber it. volatile bool woken = false; sigset_t pending; sigset_t waitMask; sigemptyset(&pending); sigfillset(&waitMask); // Count how many signals that we care about are pending. KJ_SYSCALL(sigpending(&pending)); uint signalCount = 0; #if !KJ_USE_PIPE_FOR_WAKEUP if (sigismember(&pending, reservedSignal)) { ++signalCount; sigdelset(&pending, reservedSignal); sigdelset(&waitMask, reservedSignal); } #endif { auto ptr = signalHead; while (ptr != nullptr) { if (sigismember(&pending, ptr->signum)) { ++signalCount; sigdelset(&pending, ptr->signum); sigdelset(&waitMask, ptr->signum); } ptr = ptr->next; } } // Wait for each pending signal. It would be nice to use sigtimedwait() here but it is not // available on OSX. :( Instead, we call sigsuspend() once per expected signal. { SignalCapture capture; #if KJ_BROKEN_SIGLONGJMP pthread_sigmask(SIG_SETMASK, nullptr, &capture.originalMask); #endif threadCapture = &capture; KJ_DEFER(threadCapture = nullptr); while (signalCount-- > 0) { #if KJ_BROKEN_SIGLONGJMP if (sigsetjmp(capture.jumpTo, false)) { #else if (sigsetjmp(capture.jumpTo, true)) { #endif // We received a signal and longjmp'd back out of the signal handler. sigdelset(&waitMask, capture.siginfo.si_signo); #if !KJ_USE_PIPE_FOR_WAKEUP if (capture.siginfo.si_signo == reservedSignal) { woken = true; } else { #endif gotSignal(capture.siginfo); #if !KJ_USE_PIPE_FOR_WAKEUP } #endif } else { #if __CYGWIN__ // Cygwin's sigpending() incorrectly reports signals pending for any thread, not just our // own thread. As a work-around, instead of using sigsuspend() (which would block forever // if the signal is not pending on *this* thread), we un-mask the signals and immediately // mask them again. If any signals are pending, they *should* be delivered before the first // sigprocmask() returns, and the handler will then longjmp() to the block above. If it // turns out no signal is pending, we'll block the signals again and break out of the // loop. // // Bug reported here: https://cygwin.com/ml/cygwin/2019-07/msg00051.html sigset_t origMask; sigprocmask(SIG_SETMASK, &waitMask, &origMask); sigprocmask(SIG_SETMASK, &origMask, nullptr); break; #else sigsuspend(&waitMask); KJ_FAIL_ASSERT("sigsuspend() shouldn't return because the signal handler should " "have siglongjmp()ed."); #endif } } } { PollContext pollContext(*this); pollContext.run(0); if (pollContext.processResults()) { woken = true; } } timerImpl.advanceTo(clock.now()); return woken; } void UnixEventPort::wake() const { #if KJ_USE_PIPE_FOR_WAKEUP // We're going to write() a single byte to our wake pipe in order to cause poll() to complete in // the target thread. // // If this write() fails with EWOULDBLOCK, we don't care, because the target thread is already // scheduled to wake up. char c = 0; KJ_NONBLOCKING_SYSCALL(write(wakePipeOut, &c, 1)); #else int error = pthread_kill(*reinterpret_cast(&threadId), reservedSignal); if (error != 0) { KJ_FAIL_SYSCALL("pthread_kill", error); } #endif } #endif // KJ_USE_EPOLL, else } // namespace kj #endif // !_WIN32 capnproto-c++-0.8.0/src/kj/async-win32.c++0000644000175000017500000002264513650101756020527 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 // Request Vista-level APIs. #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #include "async-win32.h" #include "debug.h" #include #include "time.h" #include "refcount.h" #include // NTSTATUS #include // STATUS_SUCCESS #undef ERROR // dammit windows.h namespace kj { Win32IocpEventPort::Win32IocpEventPort() : clock(systemPreciseMonotonicClock()), iocp(newIocpHandle()), thread(openCurrentThread()), timerImpl(clock.now()) {} Win32IocpEventPort::~Win32IocpEventPort() noexcept(false) {} class Win32IocpEventPort::IoPromiseAdapter final: public OVERLAPPED { public: IoPromiseAdapter(PromiseFulfiller& fulfiller, Win32IocpEventPort& port, uint64_t offset, IoPromiseAdapter** selfPtr) : fulfiller(fulfiller), port(port) { *selfPtr = this; memset(implicitCast(this), 0, sizeof(OVERLAPPED)); this->Offset = offset & 0x00000000FFFFFFFFull; this->OffsetHigh = offset >> 32; } ~IoPromiseAdapter() { if (handle != INVALID_HANDLE_VALUE) { // Need to cancel the I/O. // // Note: Even if HasOverlappedIoCompleted(this) is true, CancelIoEx() still seems needed to // force the completion event. if (!CancelIoEx(handle, this)) { DWORD error = GetLastError(); // ERROR_NOT_FOUND probably means the operation already completed and is enqueued on the // IOCP. // // ERROR_INVALID_HANDLE probably means that, amid a mass of destructors, the HANDLE was // closed before all of the I/O promises were destroyed. We tolerate this so long as the // I/O promises are also destroyed before returning to the event loop, hence the I/O // tasks won't actually continue on a dead handle. // // TODO(cleanup): ERROR_INVALID_HANDLE really shouldn't be allowed. Unfortunately, the // refcounted nature of capabilities and the RPC system seems to mean that objects // are unwound in the wrong order in several of Cap'n Proto's tests. So we live with this // for now. Note that even if a new handle is opened with the same numeric value, it // should be hardless to call CancelIoEx() on it because it couldn't possibly be using // the same OVERLAPPED structure. if (error != ERROR_NOT_FOUND && error != ERROR_INVALID_HANDLE) { KJ_FAIL_WIN32("CancelIoEx()", error, handle); } } // We have to wait for the IOCP to poop out the event, so that we can safely destroy the // OVERLAPPED. while (handle != INVALID_HANDLE_VALUE) { port.waitIocp(INFINITE); } } } void start(HANDLE handle) { KJ_ASSERT(this->handle == INVALID_HANDLE_VALUE); this->handle = handle; } void done(IoResult result) { KJ_ASSERT(handle != INVALID_HANDLE_VALUE); handle = INVALID_HANDLE_VALUE; fulfiller.fulfill(kj::mv(result)); } private: PromiseFulfiller& fulfiller; Win32IocpEventPort& port; HANDLE handle = INVALID_HANDLE_VALUE; // If an I/O operation is currently enqueued, the handle on which it is enqueued. }; class Win32IocpEventPort::IoOperationImpl final: public Win32EventPort::IoOperation { public: explicit IoOperationImpl(Win32IocpEventPort& port, HANDLE handle, uint64_t offset) : handle(handle), promise(newAdaptedPromise(port, offset, &promiseAdapter)) {} LPOVERLAPPED getOverlapped() override { KJ_REQUIRE(promiseAdapter != nullptr, "already called onComplete()"); return promiseAdapter; } Promise onComplete() override { KJ_REQUIRE(promiseAdapter != nullptr, "can only call onComplete() once"); promiseAdapter->start(handle); promiseAdapter = nullptr; return kj::mv(promise); } private: HANDLE handle; IoPromiseAdapter* promiseAdapter; Promise promise; }; class Win32IocpEventPort::IoObserverImpl final: public Win32EventPort::IoObserver { public: IoObserverImpl(Win32IocpEventPort& port, HANDLE handle) : port(port), handle(handle) { KJ_WIN32(CreateIoCompletionPort(handle, port.iocp, 0, 1), handle, port.iocp.get()); } Own newOperation(uint64_t offset) { return heap(port, handle, offset); } private: Win32IocpEventPort& port; HANDLE handle; }; Own Win32IocpEventPort::observeIo(HANDLE handle) { return heap(*this, handle); } Own Win32IocpEventPort::observeSignalState(HANDLE handle) { return waitThreads.observeSignalState(handle); } bool Win32IocpEventPort::wait() { waitIocp(timerImpl.timeoutToNextEvent(clock.now(), MILLISECONDS, INFINITE - 1) .map([](uint64_t t) -> DWORD { return t; }) .orDefault(INFINITE)); timerImpl.advanceTo(clock.now()); return receivedWake(); } bool Win32IocpEventPort::poll() { waitIocp(0); return receivedWake(); } void Win32IocpEventPort::wake() const { if (!sentWake.load(std::memory_order_acquire)) { sentWake.store(true, std::memory_order_release); KJ_WIN32(PostQueuedCompletionStatus(iocp, 0, 0, nullptr)); } } void Win32IocpEventPort::waitIocp(DWORD timeoutMs) { if (isAllowApc) { ULONG countReceived = 0; OVERLAPPED_ENTRY entry; memset(&entry, 0, sizeof(entry)); if (GetQueuedCompletionStatusEx(iocp, &entry, 1, &countReceived, timeoutMs, TRUE)) { KJ_ASSERT(countReceived == 1); if (entry.lpOverlapped == nullptr) { // wake() called in another thread, or APC queued. } else { DWORD error = ERROR_SUCCESS; if (entry.lpOverlapped->Internal != STATUS_SUCCESS) { error = LsaNtStatusToWinError(entry.lpOverlapped->Internal); } static_cast(entry.lpOverlapped) ->done(IoResult { error, entry.dwNumberOfBytesTransferred }); } } else { // Call failed. DWORD error = GetLastError(); if (error == WAIT_TIMEOUT || error == WAIT_IO_COMPLETION) { // WAIT_TIMEOUT = timed out (dunno why this isn't ERROR_TIMEOUT??) // WAIT_IO_COMPLETION = APC queued // Either way, nothing to do. return; } else { KJ_FAIL_WIN32("GetQueuedCompletionStatusEx()", error, error, entry.lpOverlapped); } } } else { DWORD bytesTransferred; ULONG_PTR completionKey; LPOVERLAPPED overlapped = nullptr; BOOL success = GetQueuedCompletionStatus( iocp, &bytesTransferred, &completionKey, &overlapped, timeoutMs); if (overlapped == nullptr) { if (success) { // wake() called in another thread. } else { DWORD error = GetLastError(); if (error == WAIT_TIMEOUT) { // Great, nothing to do. (Why this is WAIT_TIMEOUT and not ERROR_TIMEOUT I'm not sure.) } else { KJ_FAIL_WIN32("GetQueuedCompletionStatus()", error, error, overlapped); } } } else { DWORD error = success ? ERROR_SUCCESS : GetLastError(); static_cast(overlapped)->done(IoResult { error, bytesTransferred }); } } } bool Win32IocpEventPort::receivedWake() { if (sentWake.load(std::memory_order_acquire)) { sentWake.store(false, std::memory_order_release); return true; } else { return false; } } AutoCloseHandle Win32IocpEventPort::newIocpHandle() { HANDLE h; KJ_WIN32(h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1)); return AutoCloseHandle(h); } AutoCloseHandle Win32IocpEventPort::openCurrentThread() { HANDLE process = GetCurrentProcess(); HANDLE result; KJ_WIN32(DuplicateHandle(process, GetCurrentThread(), process, &result, 0, FALSE, DUPLICATE_SAME_ACCESS)); return AutoCloseHandle(result); } // ======================================================================================= Win32WaitObjectThreadPool::Win32WaitObjectThreadPool(uint mainThreadCount) {} Own Win32WaitObjectThreadPool::observeSignalState(HANDLE handle) { KJ_UNIMPLEMENTED("wait for win32 handles"); } uint Win32WaitObjectThreadPool::prepareMainThreadWait(HANDLE* handles[]) { KJ_UNIMPLEMENTED("wait for win32 handles"); } bool Win32WaitObjectThreadPool::finishedMainThreadWait(DWORD returnCode) { KJ_UNIMPLEMENTED("wait for win32 handles"); } } // namespace kj #endif // _WIN32 capnproto-c++-0.8.0/src/kj/async-io.c++0000644000175000017500000032024213650101756020166 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2017 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 // Request Vista-level APIs. #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #endif #include "async-io.h" #include "async-io-internal.h" #include "debug.h" #include "vector.h" #include "io.h" #include "one-of.h" #include #if _WIN32 #include #include #include #include "windows-sanity.h" #define inet_pton InetPtonA #define inet_ntop InetNtopA #include #define dup _dup #else #include #include #include #include #include #endif namespace kj { Promise AsyncInputStream::read(void* buffer, size_t bytes) { return read(buffer, bytes, bytes).then([](size_t) {}); } Promise AsyncInputStream::read(void* buffer, size_t minBytes, size_t maxBytes) { return tryRead(buffer, minBytes, maxBytes).then([=](size_t result) { if (result >= minBytes) { return result; } else { kj::throwRecoverableException(KJ_EXCEPTION(DISCONNECTED, "stream disconnected prematurely")); // Pretend we read zeros from the input. memset(reinterpret_cast(buffer) + result, 0, minBytes - result); return minBytes; } }); } Maybe AsyncInputStream::tryGetLength() { return nullptr; } namespace { class AsyncPump { public: AsyncPump(AsyncInputStream& input, AsyncOutputStream& output, uint64_t limit) : input(input), output(output), limit(limit) {} Promise pump() { // TODO(perf): This could be more efficient by reading half a buffer at a time and then // starting the next read concurrent with writing the data from the previous read. uint64_t n = kj::min(limit - doneSoFar, sizeof(buffer)); if (n == 0) return doneSoFar; return input.tryRead(buffer, 1, n) .then([this](size_t amount) -> Promise { if (amount == 0) return doneSoFar; // EOF doneSoFar += amount; return output.write(buffer, amount) .then([this]() { return pump(); }); }); } private: AsyncInputStream& input; AsyncOutputStream& output; uint64_t limit; uint64_t doneSoFar = 0; byte buffer[4096]; }; } // namespace Promise AsyncInputStream::pumpTo( AsyncOutputStream& output, uint64_t amount) { // See if output wants to dispatch on us. KJ_IF_MAYBE(result, output.tryPumpFrom(*this, amount)) { return kj::mv(*result); } // OK, fall back to naive approach. auto pump = heap(*this, output, amount); auto promise = pump->pump(); return promise.attach(kj::mv(pump)); } namespace { class AllReader { public: AllReader(AsyncInputStream& input): input(input) {} Promise> readAllBytes(uint64_t limit) { return loop(limit).then([this, limit](uint64_t headroom) { auto out = heapArray(limit - headroom); copyInto(out); return out; }); } Promise readAllText(uint64_t limit) { return loop(limit).then([this, limit](uint64_t headroom) { auto out = heapArray(limit - headroom + 1); copyInto(out.slice(0, out.size() - 1).asBytes()); out.back() = '\0'; return String(kj::mv(out)); }); } private: AsyncInputStream& input; Vector> parts; Promise loop(uint64_t limit) { KJ_REQUIRE(limit > 0, "Reached limit before EOF."); auto part = heapArray(kj::min(4096, limit)); auto partPtr = part.asPtr(); parts.add(kj::mv(part)); return input.tryRead(partPtr.begin(), partPtr.size(), partPtr.size()) .then([this,KJ_CPCAP(partPtr),limit](size_t amount) mutable -> Promise { limit -= amount; if (amount < partPtr.size()) { return limit; } else { return loop(limit); } }); } void copyInto(ArrayPtr out) { size_t pos = 0; for (auto& part: parts) { size_t n = kj::min(part.size(), out.size() - pos); memcpy(out.begin() + pos, part.begin(), n); pos += n; } } }; } // namespace Promise> AsyncInputStream::readAllBytes(uint64_t limit) { auto reader = kj::heap(*this); auto promise = reader->readAllBytes(limit); return promise.attach(kj::mv(reader)); } Promise AsyncInputStream::readAllText(uint64_t limit) { auto reader = kj::heap(*this); auto promise = reader->readAllText(limit); return promise.attach(kj::mv(reader)); } Maybe> AsyncOutputStream::tryPumpFrom( AsyncInputStream& input, uint64_t amount) { return nullptr; } namespace { class AsyncPipe final: public AsyncCapabilityStream, public Refcounted { public: ~AsyncPipe() noexcept(false) { KJ_REQUIRE(state == nullptr || ownState.get() != nullptr, "destroying AsyncPipe with operation still in-progress; probably going to segfault") { // Don't std::terminate(). break; } } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { if (minBytes == 0) { return size_t(0); } else KJ_IF_MAYBE(s, state) { return s->tryRead(buffer, minBytes, maxBytes); } else { return newAdaptedPromise( *this, arrayPtr(reinterpret_cast(buffer), maxBytes), minBytes) .then([](ReadResult r) { return r.byteCount; }); } } Promise tryReadWithFds(void* buffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { if (minBytes == 0) { return ReadResult { 0, 0 }; } else KJ_IF_MAYBE(s, state) { return s->tryReadWithFds(buffer, minBytes, maxBytes, fdBuffer, maxFds); } else { return newAdaptedPromise( *this, arrayPtr(reinterpret_cast(buffer), maxBytes), minBytes, kj::arrayPtr(fdBuffer, maxFds)); } } Promise tryReadWithStreams( void* buffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { if (minBytes == 0) { return ReadResult { 0, 0 }; } else KJ_IF_MAYBE(s, state) { return s->tryReadWithStreams(buffer, minBytes, maxBytes, streamBuffer, maxStreams); } else { return newAdaptedPromise( *this, arrayPtr(reinterpret_cast(buffer), maxBytes), minBytes, kj::arrayPtr(streamBuffer, maxStreams)); } } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { if (amount == 0) { return uint64_t(0); } else KJ_IF_MAYBE(s, state) { return s->pumpTo(output, amount); } else { return newAdaptedPromise(*this, output, amount); } } void abortRead() override { KJ_IF_MAYBE(s, state) { s->abortRead(); } else { ownState = kj::heap(); state = *ownState; readAborted = true; KJ_IF_MAYBE(f, readAbortFulfiller) { f->get()->fulfill(); readAbortFulfiller = nullptr; } } } Promise write(const void* buffer, size_t size) override { if (size == 0) { return READY_NOW; } else KJ_IF_MAYBE(s, state) { return s->write(buffer, size); } else { return newAdaptedPromise( *this, arrayPtr(reinterpret_cast(buffer), size), nullptr); } } Promise write(ArrayPtr> pieces) override { while (pieces.size() > 0 && pieces[0].size() == 0) { pieces = pieces.slice(1, pieces.size()); } if (pieces.size() == 0) { return kj::READY_NOW; } else KJ_IF_MAYBE(s, state) { return s->write(pieces); } else { return newAdaptedPromise( *this, pieces[0], pieces.slice(1, pieces.size())); } } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { while (data.size() == 0 && moreData.size() > 0) { data = moreData.front(); moreData = moreData.slice(1, moreData.size()); } if (data.size() == 0) { KJ_REQUIRE(fds.size() == 0, "can't attach FDs to empty message"); return READY_NOW; } else KJ_IF_MAYBE(s, state) { return s->writeWithFds(data, moreData, fds); } else { return newAdaptedPromise(*this, data, moreData, fds); } } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { while (data.size() == 0 && moreData.size() > 0) { data = moreData.front(); moreData = moreData.slice(1, moreData.size()); } if (data.size() == 0) { KJ_REQUIRE(streams.size() == 0, "can't attach capabilities to empty message"); return READY_NOW; } else KJ_IF_MAYBE(s, state) { return s->writeWithStreams(data, moreData, kj::mv(streams)); } else { return newAdaptedPromise(*this, data, moreData, kj::mv(streams)); } } Maybe> tryPumpFrom( AsyncInputStream& input, uint64_t amount) override { if (amount == 0) { return Promise(uint64_t(0)); } else KJ_IF_MAYBE(s, state) { return s->tryPumpFrom(input, amount); } else { return newAdaptedPromise(*this, input, amount); } } Promise whenWriteDisconnected() override { if (readAborted) { return kj::READY_NOW; } else KJ_IF_MAYBE(p, readAbortPromise) { return p->addBranch(); } else { auto paf = newPromiseAndFulfiller(); readAbortFulfiller = kj::mv(paf.fulfiller); auto fork = paf.promise.fork(); auto result = fork.addBranch(); readAbortPromise = kj::mv(fork); return result; } } void shutdownWrite() override { KJ_IF_MAYBE(s, state) { s->shutdownWrite(); } else { ownState = kj::heap(); state = *ownState; } } private: Maybe state; // Object-oriented state! If any method call is blocked waiting on activity from the other end, // then `state` is non-null and method calls should be forwarded to it. If no calls are // outstanding, `state` is null. kj::Own ownState; bool readAborted = false; Maybe>> readAbortFulfiller = nullptr; Maybe> readAbortPromise = nullptr; void endState(AsyncIoStream& obj) { KJ_IF_MAYBE(s, state) { if (s == &obj) { state = nullptr; } } } class BlockedWrite final: public AsyncCapabilityStream { // AsyncPipe state when a write() is currently waiting for a corresponding read(). public: BlockedWrite(PromiseFulfiller& fulfiller, AsyncPipe& pipe, ArrayPtr writeBuffer, ArrayPtr> morePieces, kj::OneOf, Array>> capBuffer = {}) : fulfiller(fulfiller), pipe(pipe), writeBuffer(writeBuffer), morePieces(morePieces), capBuffer(kj::mv(capBuffer)) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedWrite() noexcept(false) { pipe.endState(*this); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { KJ_SWITCH_ONEOF(tryReadImpl(buffer, minBytes, maxBytes)) { KJ_CASE_ONEOF(done, Done) { return done.result; } KJ_CASE_ONEOF(retry, Retry) { return pipe.tryRead(retry.buffer, retry.minBytes, retry.maxBytes) .then([n = retry.alreadyRead](size_t amount) { return amount + n; }); } } KJ_UNREACHABLE; } Promise tryReadWithFds(void* buffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { size_t capCount = 0; { // TODO(cleanup): Remove redundant braces when we update to C++17. KJ_SWITCH_ONEOF(capBuffer) { KJ_CASE_ONEOF(fds, ArrayPtr) { capCount = kj::max(fds.size(), maxFds); // Unfortunately, we have to dup() each FD, because the writer doesn't release ownership // by default. // TODO(perf): Should we add an ownership-releasing version of writeWithFds()? for (auto i: kj::zeroTo(capCount)) { int duped; KJ_SYSCALL(duped = dup(fds[i])); fdBuffer[i] = kj::AutoCloseFd(fds[i]); } fdBuffer += capCount; maxFds -= capCount; } KJ_CASE_ONEOF(streams, Array>) { if (streams.size() > 0 && maxFds > 0) { // TODO(someday): We could let people pass a LowLevelAsyncIoProvider to // newTwoWayPipe() if we wanted to auto-wrap FDs, but does anyone care? KJ_FAIL_REQUIRE( "async pipe message was written with streams attached, but corresponding read " "asked for FDs, and we don't know how to convert here"); } } } } // Drop any unclaimed caps. This mirrors the behavior of unix sockets, where if we didn't // provide enough buffer space for all the written FDs, the remaining ones are lost. capBuffer = {}; KJ_SWITCH_ONEOF(tryReadImpl(buffer, minBytes, maxBytes)) { KJ_CASE_ONEOF(done, Done) { return ReadResult { done.result, capCount }; } KJ_CASE_ONEOF(retry, Retry) { return pipe.tryReadWithFds( retry.buffer, retry.minBytes, retry.maxBytes, fdBuffer, maxFds) .then([byteCount = retry.alreadyRead, capCount](ReadResult result) { result.byteCount += byteCount; result.capCount += capCount; return result; }); } } KJ_UNREACHABLE; } Promise tryReadWithStreams( void* buffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { size_t capCount = 0; { // TODO(cleanup): Remove redundant braces when we update to C++17. KJ_SWITCH_ONEOF(capBuffer) { KJ_CASE_ONEOF(fds, ArrayPtr) { if (fds.size() > 0 && maxStreams > 0) { // TODO(someday): Maybe AsyncIoStream should have a `Maybe getFd()` method? KJ_FAIL_REQUIRE( "async pipe message was written with FDs attached, but corresponding read " "asked for streams, and we don't know how to convert here"); } } KJ_CASE_ONEOF(streams, Array>) { capCount = kj::max(streams.size(), maxStreams); for (auto i: kj::zeroTo(capCount)) { streamBuffer[i] = kj::mv(streams[i]); } streamBuffer += capCount; maxStreams -= capCount; } } } // Drop any unclaimed caps. This mirrors the behavior of unix sockets, where if we didn't // provide enough buffer space for all the written FDs, the remaining ones are lost. capBuffer = {}; KJ_SWITCH_ONEOF(tryReadImpl(buffer, minBytes, maxBytes)) { KJ_CASE_ONEOF(done, Done) { return ReadResult { done.result, capCount }; } KJ_CASE_ONEOF(retry, Retry) { return pipe.tryReadWithStreams( retry.buffer, retry.minBytes, retry.maxBytes, streamBuffer, maxStreams) .then([byteCount = retry.alreadyRead, capCount](ReadResult result) { result.byteCount += byteCount; result.capCount += capCount; return result; }); } } KJ_UNREACHABLE; } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { // Note: Pumps drop all capabilities. KJ_REQUIRE(canceler.isEmpty(), "already pumping"); if (amount < writeBuffer.size()) { // Consume a portion of the write buffer. return canceler.wrap(output.write(writeBuffer.begin(), amount) .then([this,amount]() { writeBuffer = writeBuffer.slice(amount, writeBuffer.size()); // We pumped the full amount, so we're done pumping. return amount; })); } // First piece doesn't cover the whole pump. Figure out how many more pieces to add. uint64_t actual = writeBuffer.size(); size_t i = 0; while (i < morePieces.size() && amount >= actual + morePieces[i].size()) { actual += morePieces[i++].size(); } // Write the first piece. auto promise = output.write(writeBuffer.begin(), writeBuffer.size()); // Write full pieces as a single gather-write. if (i > 0) { auto more = morePieces.slice(0, i); promise = promise.then([&output,more]() { return output.write(more); }); } if (i == morePieces.size()) { // This will complete the write. return canceler.wrap(promise.then([this,&output,amount,actual]() -> Promise { canceler.release(); fulfiller.fulfill(); pipe.endState(*this); if (actual == amount) { // Oh, we had exactly enough. return actual; } else { return pipe.pumpTo(output, amount - actual) .then([actual](uint64_t actual2) { return actual + actual2; }); } })); } else { // Pump ends mid-piece. Write the last, partial piece. auto n = amount - actual; auto splitPiece = morePieces[i]; KJ_ASSERT(n <= splitPiece.size()); auto newWriteBuffer = splitPiece.slice(n, splitPiece.size()); auto newMorePieces = morePieces.slice(i + 1, morePieces.size()); auto prefix = splitPiece.slice(0, n); if (prefix.size() > 0) { promise = promise.then([&output,prefix]() { return output.write(prefix.begin(), prefix.size()); }); } return canceler.wrap(promise.then([this,newWriteBuffer,newMorePieces,amount]() { writeBuffer = newWriteBuffer; morePieces = newMorePieces; canceler.release(); return amount; })); } } void abortRead() override { canceler.cancel("abortRead() was called"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "read end of pipe was aborted")); pipe.endState(*this); pipe.abortRead(); } Promise write(const void* buffer, size_t size) override { KJ_FAIL_REQUIRE("can't write() again until previous write() completes"); } Promise write(ArrayPtr> pieces) override { KJ_FAIL_REQUIRE("can't write() again until previous write() completes"); } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { KJ_FAIL_REQUIRE("can't write() again until previous write() completes"); } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { KJ_FAIL_REQUIRE("can't write() again until previous write() completes"); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { KJ_FAIL_REQUIRE("can't tryPumpFrom() again until previous write() completes"); } void shutdownWrite() override { KJ_FAIL_REQUIRE("can't shutdownWrite() until previous write() completes"); } Promise whenWriteDisconnected() override { KJ_FAIL_ASSERT("can't get here -- implemented by AsyncPipe"); } private: PromiseFulfiller& fulfiller; AsyncPipe& pipe; ArrayPtr writeBuffer; ArrayPtr> morePieces; kj::OneOf, Array>> capBuffer; Canceler canceler; struct Done { size_t result; }; struct Retry { void* buffer; size_t minBytes; size_t maxBytes; size_t alreadyRead; }; OneOf tryReadImpl(void* readBufferPtr, size_t minBytes, size_t maxBytes) { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto readBuffer = arrayPtr(reinterpret_cast(readBufferPtr), maxBytes); size_t totalRead = 0; while (readBuffer.size() >= writeBuffer.size()) { // The whole current write buffer can be copied into the read buffer. { auto n = writeBuffer.size(); memcpy(readBuffer.begin(), writeBuffer.begin(), n); totalRead += n; readBuffer = readBuffer.slice(n, readBuffer.size()); } if (morePieces.size() == 0) { // All done writing. fulfiller.fulfill(); pipe.endState(*this); if (totalRead >= minBytes) { // Also all done reading. return Done { totalRead }; } else { return Retry { readBuffer.begin(), minBytes - totalRead, readBuffer.size(), totalRead }; } } writeBuffer = morePieces[0]; morePieces = morePieces.slice(1, morePieces.size()); } // At this point, the read buffer is smaller than the current write buffer, so we can fill // it completely. { auto n = readBuffer.size(); memcpy(readBuffer.begin(), writeBuffer.begin(), n); writeBuffer = writeBuffer.slice(n, writeBuffer.size()); totalRead += n; } return Done { totalRead }; } }; class BlockedPumpFrom final: public AsyncCapabilityStream { // AsyncPipe state when a tryPumpFrom() is currently waiting for a corresponding read(). public: BlockedPumpFrom(PromiseFulfiller& fulfiller, AsyncPipe& pipe, AsyncInputStream& input, uint64_t amount) : fulfiller(fulfiller), pipe(pipe), input(input), amount(amount) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedPumpFrom() noexcept(false) { pipe.endState(*this); } Promise tryRead(void* readBuffer, size_t minBytes, size_t maxBytes) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto pumpLeft = amount - pumpedSoFar; auto min = kj::min(pumpLeft, minBytes); auto max = kj::min(pumpLeft, maxBytes); return canceler.wrap(input.tryRead(readBuffer, min, max) .then([this,readBuffer,minBytes,maxBytes,min](size_t actual) -> kj::Promise { canceler.release(); pumpedSoFar += actual; KJ_ASSERT(pumpedSoFar <= amount); if (pumpedSoFar == amount || actual < min) { // Either we pumped all we wanted or we hit EOF. fulfiller.fulfill(kj::cp(pumpedSoFar)); pipe.endState(*this); } if (actual >= minBytes) { return actual; } else { return pipe.tryRead(reinterpret_cast(readBuffer) + actual, minBytes - actual, maxBytes - actual) .then([actual](size_t actual2) { return actual + actual2; }); } })); } Promise tryReadWithFds(void* readBuffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { // Pumps drop all capabilities, so fall back to regular read. (We don't even know if the // destination is an AsyncCapabilityStream...) return tryRead(readBuffer, minBytes, maxBytes) .then([](size_t n) { return ReadResult { n, 0 }; }); } Promise tryReadWithStreams( void* readBuffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { // Pumps drop all capabilities, so fall back to regular read. (We don't even know if the // destination is an AsyncCapabilityStream...) return tryRead(readBuffer, minBytes, maxBytes) .then([](size_t n) { return ReadResult { n, 0 }; }); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount2) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto n = kj::min(amount2, amount - pumpedSoFar); return canceler.wrap(input.pumpTo(output, n) .then([this,&output,amount2,n](uint64_t actual) -> Promise { canceler.release(); pumpedSoFar += actual; KJ_ASSERT(pumpedSoFar <= amount); if (pumpedSoFar == amount || actual < n) { // Either we pumped all we wanted or we hit EOF. fulfiller.fulfill(kj::cp(pumpedSoFar)); pipe.endState(*this); return pipe.pumpTo(output, amount2 - actual) .then([actual](uint64_t actual2) { return actual + actual2; }); } // Completed entire pumpTo amount. KJ_ASSERT(actual == amount2); return amount2; })); } void abortRead() override { canceler.cancel("abortRead() was called"); // The input might have reached EOF, but we haven't detected it yet because we haven't tried // to read that far. If we had not optimized tryPumpFrom() and instead used the default // pumpTo() implementation, then the input would not have called write() again once it // reached EOF, and therefore the abortRead() on the other end would *not* propagate an // exception! We need the same behavior here. To that end, we need to detect if we're at EOF // by reading one last byte. checkEofTask = kj::evalNow([&]() { static char junk; return input.tryRead(&junk, 1, 1).then([this](uint64_t n) { if (n == 0) { fulfiller.fulfill(kj::cp(pumpedSoFar)); } else { fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "read end of pipe was aborted")); } }).eagerlyEvaluate([this](kj::Exception&& e) { fulfiller.reject(kj::mv(e)); }); }); pipe.endState(*this); pipe.abortRead(); } Promise write(const void* buffer, size_t size) override { KJ_FAIL_REQUIRE("can't write() again until previous tryPumpFrom() completes"); } Promise write(ArrayPtr> pieces) override { KJ_FAIL_REQUIRE("can't write() again until previous tryPumpFrom() completes"); } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { KJ_FAIL_REQUIRE("can't write() again until previous tryPumpFrom() completes"); } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { KJ_FAIL_REQUIRE("can't write() again until previous tryPumpFrom() completes"); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { KJ_FAIL_REQUIRE("can't tryPumpFrom() again until previous tryPumpFrom() completes"); } void shutdownWrite() override { KJ_FAIL_REQUIRE("can't shutdownWrite() until previous tryPumpFrom() completes"); } Promise whenWriteDisconnected() override { KJ_FAIL_ASSERT("can't get here -- implemented by AsyncPipe"); } private: PromiseFulfiller& fulfiller; AsyncPipe& pipe; AsyncInputStream& input; uint64_t amount; uint64_t pumpedSoFar = 0; Canceler canceler; kj::Promise checkEofTask = nullptr; }; class BlockedRead final: public AsyncCapabilityStream { // AsyncPipe state when a tryRead() is currently waiting for a corresponding write(). public: BlockedRead( PromiseFulfiller& fulfiller, AsyncPipe& pipe, ArrayPtr readBuffer, size_t minBytes, kj::OneOf, ArrayPtr>> capBuffer = {}) : fulfiller(fulfiller), pipe(pipe), readBuffer(readBuffer), minBytes(minBytes), capBuffer(capBuffer) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedRead() noexcept(false) { pipe.endState(*this); } Promise tryRead(void* readBuffer, size_t minBytes, size_t maxBytes) override { KJ_FAIL_REQUIRE("can't read() again until previous read() completes"); } Promise tryReadWithFds(void* readBuffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { KJ_FAIL_REQUIRE("can't read() again until previous read() completes"); } Promise tryReadWithStreams( void* readBuffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { KJ_FAIL_REQUIRE("can't read() again until previous read() completes"); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { KJ_FAIL_REQUIRE("can't read() again until previous read() completes"); } void abortRead() override { canceler.cancel("abortRead() was called"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "read end of pipe was aborted")); pipe.endState(*this); pipe.abortRead(); } Promise write(const void* writeBuffer, size_t size) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto data = arrayPtr(reinterpret_cast(writeBuffer), size); KJ_SWITCH_ONEOF(writeImpl(data, nullptr)) { KJ_CASE_ONEOF(done, Done) { return READY_NOW; } KJ_CASE_ONEOF(retry, Retry) { KJ_ASSERT(retry.moreData == nullptr); return pipe.write(retry.data.begin(), retry.data.size()); } } KJ_UNREACHABLE; } Promise write(ArrayPtr> pieces) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); KJ_SWITCH_ONEOF(writeImpl(pieces[0], pieces.slice(1, pieces.size()))) { KJ_CASE_ONEOF(done, Done) { return READY_NOW; } KJ_CASE_ONEOF(retry, Retry) { if (retry.data.size() == 0) { // We exactly finished the current piece, so just issue a write for the remaining // pieces. if (retry.moreData.size() == 0) { // Nothing left. return READY_NOW; } else { // Write remaining pieces. return pipe.write(retry.moreData); } } else { // Unfortunately we have to execute a separate write() for the remaining part of this // piece, because we can't modify the pieces array. auto promise = pipe.write(retry.data.begin(), retry.data.size()); if (retry.moreData.size() == 0) { // No more pieces so that's it. return kj::mv(promise); } else { // Also need to write the remaining pieces. auto& pipeRef = pipe; return promise.then([pieces=retry.moreData,&pipeRef]() { return pipeRef.write(pieces); }); } } } } KJ_UNREACHABLE; } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { #if __GNUC__ && !__clang__ && __GNUC__ >= 7 // GCC 7 decides the open-brace below is "misleadingly indented" as if it were guarded by the `for` // that appears in the implementation of KJ_REQUIRE(). Shut up shut up shut up. #pragma GCC diagnostic ignored "-Wmisleading-indentation" #endif KJ_REQUIRE(canceler.isEmpty(), "already pumping"); { // TODO(cleanup): Remove redundant braces when we update to C++17. KJ_SWITCH_ONEOF(capBuffer) { KJ_CASE_ONEOF(fdBuffer, ArrayPtr) { size_t count = kj::max(fdBuffer.size(), fds.size()); // Unfortunately, we have to dup() each FD, because the writer doesn't release ownership // by default. // TODO(perf): Should we add an ownership-releasing version of writeWithFds()? for (auto i: kj::zeroTo(count)) { int duped; KJ_SYSCALL(duped = dup(fds[i])); fdBuffer[i] = kj::AutoCloseFd(duped); } capBuffer = fdBuffer.slice(count, fdBuffer.size()); readSoFar.capCount += count; } KJ_CASE_ONEOF(streamBuffer, ArrayPtr>) { if (streamBuffer.size() > 0 && fds.size() > 0) { // TODO(someday): Maybe AsyncIoStream should have a `Maybe getFd()` method? KJ_FAIL_REQUIRE( "async pipe message was written with FDs attached, but corresponding read " "asked for streams, and we don't know how to convert here"); } } } } KJ_SWITCH_ONEOF(writeImpl(data, moreData)) { KJ_CASE_ONEOF(done, Done) { return READY_NOW; } KJ_CASE_ONEOF(retry, Retry) { // Any leftover fds in `fds` are dropped on the floor, per contract. // TODO(cleanup): We use another writeWithFds() call here only because it accepts `data` // and `moreData` directly. After the stream API refactor, we should be able to avoid // this. return pipe.writeWithFds(retry.data, retry.moreData, nullptr); } } KJ_UNREACHABLE; } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); { // TODO(cleanup): Remove redundant braces when we update to C++17. KJ_SWITCH_ONEOF(capBuffer) { KJ_CASE_ONEOF(fdBuffer, ArrayPtr) { if (fdBuffer.size() > 0 && streams.size() > 0) { // TODO(someday): We could let people pass a LowLevelAsyncIoProvider to newTwoWayPipe() // if we wanted to auto-wrap FDs, but does anyone care? KJ_FAIL_REQUIRE( "async pipe message was written with streams attached, but corresponding read " "asked for FDs, and we don't know how to convert here"); } } KJ_CASE_ONEOF(streamBuffer, ArrayPtr>) { size_t count = kj::max(streamBuffer.size(), streams.size()); for (auto i: kj::zeroTo(count)) { streamBuffer[i] = kj::mv(streams[i]); } capBuffer = streamBuffer.slice(count, streamBuffer.size()); readSoFar.capCount += count; } } } KJ_SWITCH_ONEOF(writeImpl(data, moreData)) { KJ_CASE_ONEOF(done, Done) { return READY_NOW; } KJ_CASE_ONEOF(retry, Retry) { // Any leftover fds in `fds` are dropped on the floor, per contract. // TODO(cleanup): We use another writeWithStreams() call here only because it accepts // `data` and `moreData` directly. After the stream API refactor, we should be able to // avoid this. return pipe.writeWithStreams(retry.data, retry.moreData, nullptr); } } KJ_UNREACHABLE; } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { // Note: Pumps drop all capabilities. KJ_REQUIRE(canceler.isEmpty(), "already pumping"); KJ_ASSERT(minBytes > readSoFar.byteCount); auto minToRead = kj::min(amount, minBytes - readSoFar.byteCount); auto maxToRead = kj::min(amount, readBuffer.size()); return canceler.wrap(input.tryRead(readBuffer.begin(), minToRead, maxToRead) .then([this,&input,amount](size_t actual) -> Promise { readBuffer = readBuffer.slice(actual, readBuffer.size()); readSoFar.byteCount += actual; if (readSoFar.byteCount >= minBytes) { // We've read enough to close out this read (readSoFar >= minBytes). canceler.release(); fulfiller.fulfill(kj::cp(readSoFar)); pipe.endState(*this); if (actual < amount) { // We didn't read as much data as the pump requested, but we did fulfill the read, so // we don't know whether we reached EOF on the input. We need to continue the pump, // replacing the BlockedRead state. return input.pumpTo(pipe, amount - actual) .then([actual](uint64_t actual2) -> uint64_t { return actual + actual2; }); } else { // We pumped as much data as was requested, so we can return that now. return actual; } } else { // The pump completed without fulfilling the read. This either means that the pump // reached EOF or the `amount` requested was not enough to satisfy the read in the first // place. Pumps do not propagate EOF, so either way we want to leave the BlockedRead in // place waiting for more data. return actual; } })); } void shutdownWrite() override { canceler.cancel("shutdownWrite() was called"); fulfiller.fulfill(kj::cp(readSoFar)); pipe.endState(*this); pipe.shutdownWrite(); } Promise whenWriteDisconnected() override { KJ_FAIL_ASSERT("can't get here -- implemented by AsyncPipe"); } private: PromiseFulfiller& fulfiller; AsyncPipe& pipe; ArrayPtr readBuffer; size_t minBytes; kj::OneOf, ArrayPtr>> capBuffer; ReadResult readSoFar = {0, 0}; Canceler canceler; struct Done {}; struct Retry { ArrayPtr data; ArrayPtr> moreData; }; OneOf writeImpl(ArrayPtr data, ArrayPtr> moreData) { for (;;) { if (data.size() < readBuffer.size()) { // First write segment consumes a portion of the read buffer but not all of it. auto n = data.size(); memcpy(readBuffer.begin(), data.begin(), n); readSoFar.byteCount += n; readBuffer = readBuffer.slice(n, readBuffer.size()); if (moreData.size() == 0) { // Consumed all written pieces. if (readSoFar.byteCount >= minBytes) { // We've read enough to close out this read. fulfiller.fulfill(kj::cp(readSoFar)); pipe.endState(*this); } return Done(); } data = moreData[0]; moreData = moreData.slice(1, moreData.size()); // loop } else { // First write segment consumes entire read buffer. auto n = readBuffer.size(); readSoFar.byteCount += n; fulfiller.fulfill(kj::cp(readSoFar)); pipe.endState(*this); memcpy(readBuffer.begin(), data.begin(), n); data = data.slice(n, data.size()); if (data.size() == 0 && moreData.size() == 0) { return Done(); } else { // Note: Even if `data` is empty, we don't replace it with moreData[0], because the // retry might need to use write(ArrayPtr>) which doesn't allow // passing a separate first segment. return Retry { data, moreData }; } } } } }; class BlockedPumpTo final: public AsyncCapabilityStream { // AsyncPipe state when a pumpTo() is currently waiting for a corresponding write(). public: BlockedPumpTo(PromiseFulfiller& fulfiller, AsyncPipe& pipe, AsyncOutputStream& output, uint64_t amount) : fulfiller(fulfiller), pipe(pipe), output(output), amount(amount) { KJ_REQUIRE(pipe.state == nullptr); pipe.state = *this; } ~BlockedPumpTo() noexcept(false) { pipe.endState(*this); } Promise tryRead(void* readBuffer, size_t minBytes, size_t maxBytes) override { KJ_FAIL_REQUIRE("can't read() again until previous pumpTo() completes"); } Promise tryReadWithFds(void* readBuffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { KJ_FAIL_REQUIRE("can't read() again until previous pumpTo() completes"); } Promise tryReadWithStreams( void* readBuffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { KJ_FAIL_REQUIRE("can't read() again until previous pumpTo() completes"); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { KJ_FAIL_REQUIRE("can't read() again until previous pumpTo() completes"); } void abortRead() override { canceler.cancel("abortRead() was called"); fulfiller.reject(KJ_EXCEPTION(DISCONNECTED, "read end of pipe was aborted")); pipe.endState(*this); pipe.abortRead(); } Promise write(const void* writeBuffer, size_t size) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto actual = kj::min(amount - pumpedSoFar, size); return canceler.wrap(output.write(writeBuffer, actual) .then([this,size,actual,writeBuffer]() -> kj::Promise { canceler.release(); pumpedSoFar += actual; KJ_ASSERT(pumpedSoFar <= amount); KJ_ASSERT(actual <= size); if (pumpedSoFar == amount) { // Done with pump. fulfiller.fulfill(kj::cp(pumpedSoFar)); pipe.endState(*this); } if (actual == size) { return kj::READY_NOW; } else { KJ_ASSERT(pumpedSoFar == amount); return pipe.write(reinterpret_cast(writeBuffer) + actual, size - actual); } })); } Promise write(ArrayPtr> pieces) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); size_t size = 0; size_t needed = amount - pumpedSoFar; for (auto i: kj::indices(pieces)) { if (pieces[i].size() > needed) { // The pump ends in the middle of this write. auto promise = output.write(pieces.slice(0, i)); if (needed > 0) { // The pump includes part of this piece, but not all. Unfortunately we need to split // writes. auto partial = pieces[i].slice(0, needed); promise = promise.then([this,partial]() { return output.write(partial.begin(), partial.size()); }); auto partial2 = pieces[i].slice(needed, pieces[i].size()); promise = canceler.wrap(promise.then([this,partial2]() { canceler.release(); fulfiller.fulfill(kj::cp(amount)); pipe.endState(*this); return pipe.write(partial2.begin(), partial2.size()); })); ++i; } else { // The pump ends exactly at the end of a piece, how nice. promise = canceler.wrap(promise.then([this]() { canceler.release(); fulfiller.fulfill(kj::cp(amount)); pipe.endState(*this); })); } auto remainder = pieces.slice(i, pieces.size()); if (remainder.size() > 0) { auto& pipeRef = pipe; promise = promise.then([&pipeRef,remainder]() { return pipeRef.write(remainder); }); } return promise; } else { size += pieces[i].size(); needed -= pieces[i].size(); } } // Turns out we can forward this whole write. KJ_ASSERT(size <= amount - pumpedSoFar); return canceler.wrap(output.write(pieces).then([this,size]() { pumpedSoFar += size; KJ_ASSERT(pumpedSoFar <= amount); if (pumpedSoFar == amount) { // Done pumping. canceler.release(); fulfiller.fulfill(kj::cp(amount)); pipe.endState(*this); } })); } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { // Pumps drop all capabilities, so fall back to regular write(). // TODO(cleaunp): After stream API refactor, regular write() methods will take // (data, moreData) and we can clean this up. if (moreData.size() == 0) { return write(data.begin(), data.size()); } else { auto pieces = kj::heapArrayBuilder>(moreData.size() + 1); pieces.add(data); pieces.addAll(moreData); return write(pieces.finish()); } } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { // Pumps drop all capabilities, so fall back to regular write(). // TODO(cleaunp): After stream API refactor, regular write() methods will take // (data, moreData) and we can clean this up. if (moreData.size() == 0) { return write(data.begin(), data.size()); } else { auto pieces = kj::heapArrayBuilder>(moreData.size() + 1); pieces.add(data); pieces.addAll(moreData); return write(pieces.finish()); } } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount2) override { KJ_REQUIRE(canceler.isEmpty(), "already pumping"); auto n = kj::min(amount2, amount - pumpedSoFar); return output.tryPumpFrom(input, n) .map([&](Promise subPump) { return canceler.wrap(subPump .then([this,&input,amount2,n](uint64_t actual) -> Promise { canceler.release(); pumpedSoFar += actual; KJ_ASSERT(pumpedSoFar <= amount); if (pumpedSoFar == amount) { fulfiller.fulfill(kj::cp(amount)); pipe.endState(*this); } KJ_ASSERT(actual <= amount2); if (actual == amount2) { // Completed entire tryPumpFrom amount. return amount2; } else if (actual < n) { // Received less than requested, presumably because EOF. return actual; } else { // We received all the bytes that were requested but it didn't complete the pump. KJ_ASSERT(pumpedSoFar == amount); return input.pumpTo(pipe, amount2 - actual); } })); }); } void shutdownWrite() override { canceler.cancel("shutdownWrite() was called"); fulfiller.fulfill(kj::cp(pumpedSoFar)); pipe.endState(*this); pipe.shutdownWrite(); } Promise whenWriteDisconnected() override { KJ_FAIL_ASSERT("can't get here -- implemented by AsyncPipe"); } private: PromiseFulfiller& fulfiller; AsyncPipe& pipe; AsyncOutputStream& output; uint64_t amount; size_t pumpedSoFar = 0; Canceler canceler; }; class AbortedRead final: public AsyncCapabilityStream { // AsyncPipe state when abortRead() has been called. public: Promise tryRead(void* readBufferPtr, size_t minBytes, size_t maxBytes) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Promise tryReadWithFds(void* readBuffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Promise tryReadWithStreams( void* readBuffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } void abortRead() override { // ignore repeated abort } Promise write(const void* buffer, size_t size) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Promise write(ArrayPtr> pieces) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { return KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called"); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { // There might not actually be any data in `input`, in which case a pump wouldn't actually // write anything and wouldn't fail. if (input.tryGetLength().orDefault(1) == 0) { // Yeah a pump would pump nothing. return Promise(uint64_t(0)); } else { // While we *could* just return nullptr here, it would probably then fall back to a normal // buffered pump, which would allocate a big old buffer just to find there's nothing to // read. Let's try reading 1 byte to avoid that allocation. static char c; return input.tryRead(&c, 1, 1).then([](size_t n) { if (n == 0) { // Yay, we're at EOF as hoped. return uint64_t(0); } else { // There was data in the input. The pump would have thrown. kj::throwRecoverableException( KJ_EXCEPTION(DISCONNECTED, "abortRead() has been called")); return uint64_t(0); } }); } } void shutdownWrite() override { // ignore -- currently shutdownWrite() actually means that the PipeWriteEnd was dropped, // which is not an error even if reads have been aborted. } Promise whenWriteDisconnected() override { KJ_FAIL_ASSERT("can't get here -- implemented by AsyncPipe"); } }; class ShutdownedWrite final: public AsyncCapabilityStream { // AsyncPipe state when shutdownWrite() has been called. public: Promise tryRead(void* readBufferPtr, size_t minBytes, size_t maxBytes) override { return size_t(0); } Promise tryReadWithFds(void* readBuffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { return ReadResult { 0, 0 }; } Promise tryReadWithStreams( void* readBuffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { return ReadResult { 0, 0 }; } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return uint64_t(0); } void abortRead() override { // ignore } Promise write(const void* buffer, size_t size) override { KJ_FAIL_REQUIRE("shutdownWrite() has been called"); } Promise write(ArrayPtr> pieces) override { KJ_FAIL_REQUIRE("shutdownWrite() has been called"); } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { KJ_FAIL_REQUIRE("shutdownWrite() has been called"); } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { KJ_FAIL_REQUIRE("shutdownWrite() has been called"); } Maybe> tryPumpFrom(AsyncInputStream& input, uint64_t amount) override { KJ_FAIL_REQUIRE("shutdownWrite() has been called"); } void shutdownWrite() override { // ignore -- currently shutdownWrite() actually means that the PipeWriteEnd was dropped, // so it will only be called once anyhow. } Promise whenWriteDisconnected() override { KJ_FAIL_ASSERT("can't get here -- implemented by AsyncPipe"); } }; }; class PipeReadEnd final: public AsyncInputStream { public: PipeReadEnd(kj::Own pipe): pipe(kj::mv(pipe)) {} ~PipeReadEnd() noexcept(false) { unwind.catchExceptionsIfUnwinding([&]() { pipe->abortRead(); }); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return pipe->tryRead(buffer, minBytes, maxBytes); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return pipe->pumpTo(output, amount); } private: Own pipe; UnwindDetector unwind; }; class PipeWriteEnd final: public AsyncOutputStream { public: PipeWriteEnd(kj::Own pipe): pipe(kj::mv(pipe)) {} ~PipeWriteEnd() noexcept(false) { unwind.catchExceptionsIfUnwinding([&]() { pipe->shutdownWrite(); }); } Promise write(const void* buffer, size_t size) override { return pipe->write(buffer, size); } Promise write(ArrayPtr> pieces) override { return pipe->write(pieces); } Maybe> tryPumpFrom( AsyncInputStream& input, uint64_t amount) override { return pipe->tryPumpFrom(input, amount); } Promise whenWriteDisconnected() override { return pipe->whenWriteDisconnected(); } private: Own pipe; UnwindDetector unwind; }; class TwoWayPipeEnd final: public AsyncCapabilityStream { public: TwoWayPipeEnd(kj::Own in, kj::Own out) : in(kj::mv(in)), out(kj::mv(out)) {} ~TwoWayPipeEnd() noexcept(false) { unwind.catchExceptionsIfUnwinding([&]() { out->shutdownWrite(); in->abortRead(); }); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return in->tryRead(buffer, minBytes, maxBytes); } Promise tryReadWithFds(void* buffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { return in->tryReadWithFds(buffer, minBytes, maxBytes, fdBuffer, maxFds); } Promise tryReadWithStreams( void* buffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { return in->tryReadWithStreams(buffer, minBytes, maxBytes, streamBuffer, maxStreams); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return in->pumpTo(output, amount); } void abortRead() override { in->abortRead(); } Promise write(const void* buffer, size_t size) override { return out->write(buffer, size); } Promise write(ArrayPtr> pieces) override { return out->write(pieces); } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { return out->writeWithFds(data, moreData, fds); } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { return out->writeWithStreams(data, moreData, kj::mv(streams)); } Maybe> tryPumpFrom( AsyncInputStream& input, uint64_t amount) override { return out->tryPumpFrom(input, amount); } Promise whenWriteDisconnected() override { return out->whenWriteDisconnected(); } void shutdownWrite() override { out->shutdownWrite(); } private: kj::Own in; kj::Own out; UnwindDetector unwind; }; class LimitedInputStream final: public AsyncInputStream { public: LimitedInputStream(kj::Own inner, uint64_t limit) : inner(kj::mv(inner)), limit(limit) { if (limit == 0) { this->inner = nullptr; } } Maybe tryGetLength() override { return limit; } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { if (limit == 0) return size_t(0); return inner->tryRead(buffer, kj::min(minBytes, limit), kj::min(maxBytes, limit)) .then([this,minBytes](size_t actual) { decreaseLimit(actual, minBytes); return actual; }); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { if (limit == 0) return uint64_t(0); auto requested = kj::min(amount, limit); return inner->pumpTo(output, requested) .then([this,requested](uint64_t actual) { decreaseLimit(actual, requested); return actual; }); } private: Own inner; uint64_t limit; void decreaseLimit(uint64_t amount, uint64_t requested) { KJ_ASSERT(limit >= amount); limit -= amount; if (limit == 0) { inner = nullptr; } else if (amount < requested) { KJ_FAIL_REQUIRE("pipe ended prematurely") { break; } } } }; } // namespace OneWayPipe newOneWayPipe(kj::Maybe expectedLength) { auto impl = kj::refcounted(); Own readEnd = kj::heap(kj::addRef(*impl)); KJ_IF_MAYBE(l, expectedLength) { readEnd = kj::heap(kj::mv(readEnd), *l); } Own writeEnd = kj::heap(kj::mv(impl)); return { kj::mv(readEnd), kj::mv(writeEnd) }; } TwoWayPipe newTwoWayPipe() { auto pipe1 = kj::refcounted(); auto pipe2 = kj::refcounted(); auto end1 = kj::heap(kj::addRef(*pipe1), kj::addRef(*pipe2)); auto end2 = kj::heap(kj::mv(pipe2), kj::mv(pipe1)); return { { kj::mv(end1), kj::mv(end2) } }; } CapabilityPipe newCapabilityPipe() { auto pipe1 = kj::refcounted(); auto pipe2 = kj::refcounted(); auto end1 = kj::heap(kj::addRef(*pipe1), kj::addRef(*pipe2)); auto end2 = kj::heap(kj::mv(pipe2), kj::mv(pipe1)); return { { kj::mv(end1), kj::mv(end2) } }; } namespace { class AsyncTee final: public Refcounted { public: using BranchId = uint; explicit AsyncTee(Own inner, uint64_t bufferSizeLimit) : inner(mv(inner)), bufferSizeLimit(bufferSizeLimit), length(this->inner->tryGetLength()) {} ~AsyncTee() noexcept(false) { bool hasBranches = false; for (auto& branch: branches) { hasBranches = hasBranches || branch != nullptr; } KJ_ASSERT(!hasBranches, "destroying AsyncTee with branch still alive") { // Don't std::terminate(). break; } } void addBranch(BranchId branch) { KJ_REQUIRE(branches[branch] == nullptr, "branch already exists"); branches[branch] = Branch(); } void removeBranch(BranchId branch) { auto& state = KJ_REQUIRE_NONNULL(branches[branch], "branch was already destroyed"); KJ_REQUIRE(state.sink == nullptr, "destroying tee branch with operation still in-progress; probably going to segfault") { // Don't std::terminate(). break; } branches[branch] = nullptr; } Promise tryRead(BranchId branch, void* buffer, size_t minBytes, size_t maxBytes) { auto& state = KJ_ASSERT_NONNULL(branches[branch]); KJ_ASSERT(state.sink == nullptr); // If there is excess data in the buffer for us, slurp that up. auto readBuffer = arrayPtr(reinterpret_cast(buffer), maxBytes); auto readSoFar = state.buffer.consume(readBuffer, minBytes); if (minBytes == 0) { return readSoFar; } if (state.buffer.empty()) { KJ_IF_MAYBE(reason, stoppage) { // Prefer a short read to an exception. The exception prevents the pull loop from adding any // data to the buffer, so `readSoFar` will be zero the next time someone calls `tryRead()`, // and the caller will see the exception. if (reason->is() || readSoFar > 0) { return readSoFar; } return cp(reason->get()); } } auto promise = newAdaptedPromise(state.sink, readBuffer, minBytes, readSoFar); ensurePulling(); return mv(promise); } Maybe tryGetLength(BranchId branch) { auto& state = KJ_ASSERT_NONNULL(branches[branch]); return length.map([&state](uint64_t amount) { return amount + state.buffer.size(); }); } Promise pumpTo(BranchId branch, AsyncOutputStream& output, uint64_t amount) { auto& state = KJ_ASSERT_NONNULL(branches[branch]); KJ_ASSERT(state.sink == nullptr); if (amount == 0) { return amount; } if (state.buffer.empty()) { KJ_IF_MAYBE(reason, stoppage) { if (reason->is()) { return uint64_t(0); } return cp(reason->get()); } } auto promise = newAdaptedPromise(state.sink, output, amount); ensurePulling(); return mv(promise); } private: struct Eof {}; using Stoppage = OneOf; class Buffer { public: uint64_t consume(ArrayPtr& readBuffer, size_t& minBytes); // Consume as many bytes as possible, copying them into `readBuffer`. Return the number of bytes // consumed. // // `readBuffer` and `minBytes` are both assigned appropriate new values, such that after any // call to `consume()`, `readBuffer` will point to the remaining slice of unwritten space, and // `minBytes` will have been decremented (clamped to zero) by the amount of bytes read. That is, // the read can be considered fulfilled if `minBytes` is zero after a call to `consume()`. Array> asArray(uint64_t minBytes, uint64_t& amount); // Consume the first `minBytes` of the buffer (or the entire buffer) and return it in an Array // of ArrayPtrs, suitable for passing to AsyncOutputStream.write(). The outer Array // owns the underlying data. void produce(Array bytes); // Enqueue a byte array to the end of the buffer list. bool empty() const; uint64_t size() const; private: std::deque> bufferList; }; class Sink { public: struct Need { // We use uint64_t here because: // - pumpTo() accepts it as the `amount` parameter. // - all practical values of tryRead()'s `maxBytes` parameter (a size_t) should also fit into // a uint64_t, unless we're on a machine with multiple exabytes of memory ... uint64_t minBytes = 0; uint64_t maxBytes = kj::maxValue; }; virtual Promise fill(Buffer& inBuffer, const Maybe& stoppage) = 0; // Attempt to fill the sink with bytes andreturn a promise which must resolve before any inner // read may be attempted. If a sink requires backpressure to be respected, this is how it should // be communicated. // // If the sink is full, it must detach from the tee before the returned promise is resolved. // // The returned promise must not result in an exception. virtual Need need() = 0; virtual void reject(Exception&& exception) = 0; // Inform this sink of a catastrophic exception and detach it. Regular read exceptions should be // propagated through `fill()`'s stoppage parameter instead. }; template class SinkBase: public Sink { // Registers itself with the tee as a sink on construction, detaches from the tee on // fulfillment, rejection, or destruction. // // A bit of a Frankenstein, avert your eyes. For one thing, it's more of a mixin than a base... public: explicit SinkBase(PromiseFulfiller& fulfiller, Maybe& sinkLink) : fulfiller(fulfiller), sinkLink(sinkLink) { KJ_ASSERT(sinkLink == nullptr, "sink initiated with sink already in flight"); sinkLink = *this; } KJ_DISALLOW_COPY(SinkBase); ~SinkBase() noexcept(false) { detach(); } void reject(Exception&& exception) override { // The tee is allowed to reject this sink if it needs to, e.g. to propagate a non-inner read // exception from the pull loop. Only the derived class is allowed to fulfill() directly, // though -- the tee must keep calling fill(). fulfiller.reject(mv(exception)); detach(); } protected: template void fulfill(U value) { fulfiller.fulfill(fwd(value)); detach(); } private: void detach() { KJ_IF_MAYBE(sink, sinkLink) { if (sink == this) { sinkLink = nullptr; } } } PromiseFulfiller& fulfiller; Maybe& sinkLink; }; struct Branch { Buffer buffer; Maybe sink; }; class ReadSink final: public SinkBase { public: explicit ReadSink(PromiseFulfiller& fulfiller, Maybe& registration, ArrayPtr buffer, size_t minBytes, size_t readSoFar) : SinkBase(fulfiller, registration), buffer(buffer), minBytes(minBytes), readSoFar(readSoFar) {} Promise fill(Buffer& inBuffer, const Maybe& stoppage) override { auto amount = inBuffer.consume(buffer, minBytes); readSoFar += amount; if (minBytes == 0) { // We satisfied the read request. fulfill(readSoFar); return READY_NOW; } if (amount == 0 && inBuffer.empty()) { // We made no progress on the read request and the buffer is tapped out. KJ_IF_MAYBE(reason, stoppage) { if (reason->is() || readSoFar > 0) { // Prefer short read to exception. fulfill(readSoFar); } else { reject(cp(reason->get())); } return READY_NOW; } } return READY_NOW; } Need need() override { return Need { minBytes, buffer.size() }; } private: ArrayPtr buffer; size_t minBytes; // Arguments to the outer tryRead() call, sliced/decremented after every buffer consumption. size_t readSoFar; // End result of the outer tryRead(). }; class PumpSink final: public SinkBase { public: explicit PumpSink(PromiseFulfiller& fulfiller, Maybe& registration, AsyncOutputStream& output, uint64_t limit) : SinkBase(fulfiller, registration), output(output), limit(limit) {} ~PumpSink() noexcept(false) { canceler.cancel("This pump has been canceled."); } Promise fill(Buffer& inBuffer, const Maybe& stoppage) override { KJ_ASSERT(limit > 0); uint64_t amount = 0; // TODO(someday): This consumes data from the buffer, but we cannot know if the stream to // which we're pumping will accept it until after the write() promise completes. If the // write() promise rejects, we lose this data. We should consume the data from the buffer // only after successful writes. auto writeBuffer = inBuffer.asArray(limit, amount); KJ_ASSERT(limit >= amount); if (amount > 0) { Promise promise = kj::evalNow([&]() { return output.write(writeBuffer).attach(mv(writeBuffer)); }).then([this, amount]() { limit -= amount; pumpedSoFar += amount; if (limit == 0) { fulfill(pumpedSoFar); } }).eagerlyEvaluate([this](Exception&& exception) { reject(mv(exception)); }); return canceler.wrap(mv(promise)).catch_([](kj::Exception&&) {}); } else KJ_IF_MAYBE(reason, stoppage) { if (reason->is()) { // Unlike in the read case, it makes more sense to immediately propagate exceptions to the // pump promise rather than show it a "short pump". fulfill(pumpedSoFar); } else { reject(cp(reason->get())); } } return READY_NOW; } Need need() override { return Need { 1, limit }; } private: AsyncOutputStream& output; uint64_t limit; // Arguments to the outer pumpTo() call, decremented after every buffer consumption. // // Equal to zero once fulfiller has been fulfilled/rejected. uint64_t pumpedSoFar = 0; // End result of the outer pumpTo(). Canceler canceler; // When the pump is canceled, we also need to cancel any write operations in flight. }; // ===================================================================================== Maybe analyzeSinks() { // Return nullptr if there are no sinks at all. Otherwise, return the largest `minBytes` and the // smallest `maxBytes` requested by any sink. The pull loop will use these values to calculate // the optimal buffer size for the next inner read, so that a minimum amount of data is buffered // at any given time. uint64_t minBytes = 0; uint64_t maxBytes = kj::maxValue; uint nBranches = 0; uint nSinks = 0; for (auto& state: branches) { KJ_IF_MAYBE(s, state) { ++nBranches; KJ_IF_MAYBE(sink, s->sink) { ++nSinks; auto need = sink->need(); minBytes = kj::max(minBytes, need.minBytes); maxBytes = kj::min(maxBytes, need.maxBytes); } } } if (nSinks > 0) { KJ_ASSERT(minBytes > 0); KJ_ASSERT(maxBytes > 0, "sink was filled but did not detach"); // Sinks may report non-overlapping needs. maxBytes = kj::max(minBytes, maxBytes); return Sink::Need { minBytes, maxBytes }; } // No active sinks. return nullptr; } void ensurePulling() { if (!pulling) { pulling = true; UnwindDetector unwind; KJ_DEFER(if (unwind.isUnwinding()) pulling = false); pullPromise = pull(); } } Promise pull() { // Use evalLater() so that two pump sinks added on the same turn of the event loop will not // cause buffering. return evalLater([this] { // Attempt to fill any sinks that exist. Vector> promises; for (auto& state: branches) { KJ_IF_MAYBE(s, state) { KJ_IF_MAYBE(sink, s->sink) { promises.add(sink->fill(s->buffer, stoppage)); } } } // Respect the greatest of the sinks' backpressures. return joinPromises(promises.releaseAsArray()); }).then([this]() -> Promise { // Check to see whether we need to perform an inner read. auto need = analyzeSinks(); if (need == nullptr) { // No more sinks, stop pulling. pulling = false; return READY_NOW; } if (stoppage != nullptr) { // We're eof or errored, don't read, but loop so we can fill the sink(s). return pull(); } auto& n = KJ_ASSERT_NONNULL(need); KJ_ASSERT(n.minBytes > 0); // We must perform an inner read. // We'd prefer not to explode our buffer, if that's cool. We cap `maxBytes` to the buffer size // limit or our builtin MAX_BLOCK_SIZE, whichever is smaller. But, we make sure `maxBytes` is // still >= `minBytes`. n.maxBytes = kj::min(n.maxBytes, MAX_BLOCK_SIZE); n.maxBytes = kj::min(n.maxBytes, bufferSizeLimit); n.maxBytes = kj::max(n.minBytes, n.maxBytes); for (auto& state: branches) { KJ_IF_MAYBE(s, state) { // TODO(perf): buffer.size() is O(n) where n = # of individual heap-allocated byte arrays. if (s->buffer.size() + n.maxBytes > bufferSizeLimit) { stoppage = Stoppage(KJ_EXCEPTION(FAILED, "tee buffer size limit exceeded")); return pull(); } } } auto heapBuffer = heapArray(n.maxBytes); // gcc 4.9 quirk: If I don't hoist this into a separate variable and instead call // // inner->tryRead(heapBuffer.begin(), n.minBytes, heapBuffer.size()) // // `heapBuffer` seems to get moved into the lambda capture before the arguments to `tryRead()` // are evaluated, meaning `inner` sees a nullptr destination. Bizarrely, `inner` sees the // correct value for `heapBuffer.size()`... I dunno, man. auto destination = heapBuffer.begin(); return kj::evalNow([&]() { return inner->tryRead(destination, n.minBytes, n.maxBytes); }) .then([this, heapBuffer = mv(heapBuffer), minBytes = n.minBytes](size_t amount) mutable -> Promise { length = length.map([amount](uint64_t n) { KJ_ASSERT(n >= amount); return n - amount; }); if (amount < heapBuffer.size()) { heapBuffer = heapBuffer.slice(0, amount).attach(mv(heapBuffer)); } KJ_ASSERT(stoppage == nullptr); Maybe> bufferPtr = nullptr; for (auto& state: branches) { KJ_IF_MAYBE(s, state) { // Prefer to move the buffer into the receiving branch's deque, rather than memcpy. // // TODO(perf): For the 2-branch case, this is fine, since the majority of the time // only one buffer will be in use. If we generalize to the n-branch case, this would // become memcpy-heavy. KJ_IF_MAYBE(ptr, bufferPtr) { s->buffer.produce(heapArray(*ptr)); } else { bufferPtr = ArrayPtr(heapBuffer); s->buffer.produce(mv(heapBuffer)); } } } if (amount < minBytes) { // Short read, EOF. stoppage = Stoppage(Eof()); } return pull(); }, [this](Exception&& exception) { // Exception from the inner tryRead(). Propagate. stoppage = Stoppage(mv(exception)); return pull(); }); }).eagerlyEvaluate([this](Exception&& exception) { // Exception from our loop, not from inner tryRead(). Something is broken; tell everybody! pulling = false; for (auto& state: branches) { KJ_IF_MAYBE(s, state) { KJ_IF_MAYBE(sink, s->sink) { sink->reject(KJ_EXCEPTION(FAILED, "Exception in tee loop", exception)); } } } }); } constexpr static size_t MAX_BLOCK_SIZE = 1 << 14; // 16k Own inner; const uint64_t bufferSizeLimit = kj::maxValue; Maybe length; Maybe branches[2]; Maybe stoppage; Promise pullPromise = READY_NOW; bool pulling = false; }; constexpr size_t AsyncTee::MAX_BLOCK_SIZE; uint64_t AsyncTee::Buffer::consume(ArrayPtr& readBuffer, size_t& minBytes) { uint64_t totalAmount = 0; while (readBuffer.size() > 0 && !bufferList.empty()) { auto& bytes = bufferList.front(); auto amount = kj::min(bytes.size(), readBuffer.size()); memcpy(readBuffer.begin(), bytes.begin(), amount); totalAmount += amount; readBuffer = readBuffer.slice(amount, readBuffer.size()); minBytes -= kj::min(amount, minBytes); if (amount == bytes.size()) { bufferList.pop_front(); } else { bytes = heapArray(bytes.slice(amount, bytes.size())); return totalAmount; } } return totalAmount; } void AsyncTee::Buffer::produce(Array bytes) { bufferList.push_back(mv(bytes)); } Array> AsyncTee::Buffer::asArray( uint64_t maxBytes, uint64_t& amount) { amount = 0; Vector> buffers; Vector> ownBuffers; while (maxBytes > 0 && !bufferList.empty()) { auto& bytes = bufferList.front(); if (bytes.size() <= maxBytes) { amount += bytes.size(); maxBytes -= bytes.size(); buffers.add(bytes); ownBuffers.add(mv(bytes)); bufferList.pop_front(); } else { auto ownBytes = heapArray(bytes.slice(0, maxBytes)); buffers.add(ownBytes); ownBuffers.add(mv(ownBytes)); bytes = heapArray(bytes.slice(maxBytes, bytes.size())); amount += maxBytes; maxBytes = 0; } } if (buffers.size() > 0) { return buffers.releaseAsArray().attach(mv(ownBuffers)); } return {}; } bool AsyncTee::Buffer::empty() const { return bufferList.empty(); } uint64_t AsyncTee::Buffer::size() const { uint64_t result = 0; for (auto& bytes: bufferList) { result += bytes.size(); } return result; } class TeeBranch final: public AsyncInputStream { public: TeeBranch(Own tee, uint8_t branch): tee(mv(tee)), branch(branch) { this->tee->addBranch(branch); } ~TeeBranch() noexcept(false) { unwind.catchExceptionsIfUnwinding([&]() { tee->removeBranch(branch); }); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return tee->tryRead(branch, buffer, minBytes, maxBytes); } Promise pumpTo(AsyncOutputStream& output, uint64_t amount) override { return tee->pumpTo(branch, output, amount); } Maybe tryGetLength() override { return tee->tryGetLength(branch); } private: Own tee; const uint8_t branch; UnwindDetector unwind; }; } // namespace Tee newTee(Own input, uint64_t limit) { auto impl = refcounted(mv(input), limit); Own branch1 = heap(addRef(*impl), 0); Own branch2 = heap(mv(impl), 1); return { { mv(branch1), mv(branch2) } }; } namespace { class PromisedAsyncIoStream final: public kj::AsyncIoStream, private kj::TaskSet::ErrorHandler { // An AsyncIoStream which waits for a promise to resolve then forwards all calls to the promised // stream. public: PromisedAsyncIoStream(kj::Promise> promise) : promise(promise.then([this](kj::Own result) { stream = kj::mv(result); }).fork()), tasks(*this) {} kj::Promise read(void* buffer, size_t minBytes, size_t maxBytes) override { KJ_IF_MAYBE(s, stream) { return s->get()->read(buffer, minBytes, maxBytes); } else { return promise.addBranch().then([this,buffer,minBytes,maxBytes]() { return KJ_ASSERT_NONNULL(stream)->read(buffer, minBytes, maxBytes); }); } } kj::Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { KJ_IF_MAYBE(s, stream) { return s->get()->tryRead(buffer, minBytes, maxBytes); } else { return promise.addBranch().then([this,buffer,minBytes,maxBytes]() { return KJ_ASSERT_NONNULL(stream)->tryRead(buffer, minBytes, maxBytes); }); } } kj::Maybe tryGetLength() override { KJ_IF_MAYBE(s, stream) { return s->get()->tryGetLength(); } else { return nullptr; } } kj::Promise pumpTo(kj::AsyncOutputStream& output, uint64_t amount) override { KJ_IF_MAYBE(s, stream) { return s->get()->pumpTo(output, amount); } else { return promise.addBranch().then([this,&output,amount]() { return KJ_ASSERT_NONNULL(stream)->pumpTo(output, amount); }); } } kj::Promise write(const void* buffer, size_t size) override { KJ_IF_MAYBE(s, stream) { return s->get()->write(buffer, size); } else { return promise.addBranch().then([this,buffer,size]() { return KJ_ASSERT_NONNULL(stream)->write(buffer, size); }); } } kj::Promise write(kj::ArrayPtr> pieces) override { KJ_IF_MAYBE(s, stream) { return s->get()->write(pieces); } else { return promise.addBranch().then([this,pieces]() { return KJ_ASSERT_NONNULL(stream)->write(pieces); }); } } kj::Maybe> tryPumpFrom( kj::AsyncInputStream& input, uint64_t amount = kj::maxValue) override { KJ_IF_MAYBE(s, stream) { // Call input.pumpTo() on the resolved stream instead, so that if it does some dynamic_casts // or whatnot to detect stream types it can retry those on the inner stream. return input.pumpTo(**s, amount); } else { return promise.addBranch().then([this,&input,amount]() { // Here we actually have no choice but to call input.pumpTo() because if we called // tryPumpFrom(input, amount) and it returned nullptr, what would we do? It's too late for // us to return nullptr. But the thing about dynamic_cast also applies. return input.pumpTo(*KJ_ASSERT_NONNULL(stream), amount); }); } } Promise whenWriteDisconnected() override { KJ_IF_MAYBE(s, stream) { return s->get()->whenWriteDisconnected(); } else { return promise.addBranch().then([this]() { return KJ_ASSERT_NONNULL(stream)->whenWriteDisconnected(); }, [](kj::Exception&& e) -> kj::Promise { if (e.getType() == kj::Exception::Type::DISCONNECTED) { return kj::READY_NOW; } else { return kj::mv(e); } }); } } void shutdownWrite() override { KJ_IF_MAYBE(s, stream) { return s->get()->shutdownWrite(); } else { tasks.add(promise.addBranch().then([this]() { return KJ_ASSERT_NONNULL(stream)->shutdownWrite(); })); } } void abortRead() override { KJ_IF_MAYBE(s, stream) { return s->get()->abortRead(); } else { tasks.add(promise.addBranch().then([this]() { return KJ_ASSERT_NONNULL(stream)->abortRead(); })); } } private: kj::ForkedPromise promise; kj::Maybe> stream; kj::TaskSet tasks; void taskFailed(kj::Exception&& exception) override { KJ_LOG(ERROR, exception); } }; class PromisedAsyncOutputStream final: public kj::AsyncOutputStream { // An AsyncOutputStream which waits for a promise to resolve then forwards all calls to the // promised stream. // // TODO(cleanup): Can this share implementation with PromiseIoStream? Seems hard. public: PromisedAsyncOutputStream(kj::Promise> promise) : promise(promise.then([this](kj::Own result) { stream = kj::mv(result); }).fork()) {} kj::Promise write(const void* buffer, size_t size) override { KJ_IF_MAYBE(s, stream) { return s->get()->write(buffer, size); } else { return promise.addBranch().then([this,buffer,size]() { return KJ_ASSERT_NONNULL(stream)->write(buffer, size); }); } } kj::Promise write(kj::ArrayPtr> pieces) override { KJ_IF_MAYBE(s, stream) { return s->get()->write(pieces); } else { return promise.addBranch().then([this,pieces]() { return KJ_ASSERT_NONNULL(stream)->write(pieces); }); } } kj::Maybe> tryPumpFrom( kj::AsyncInputStream& input, uint64_t amount = kj::maxValue) override { KJ_IF_MAYBE(s, stream) { return s->get()->tryPumpFrom(input, amount); } else { return promise.addBranch().then([this,&input,amount]() { // Call input.pumpTo() on the resolved stream instead. return input.pumpTo(*KJ_ASSERT_NONNULL(stream), amount); }); } } Promise whenWriteDisconnected() override { KJ_IF_MAYBE(s, stream) { return s->get()->whenWriteDisconnected(); } else { return promise.addBranch().then([this]() { return KJ_ASSERT_NONNULL(stream)->whenWriteDisconnected(); }, [](kj::Exception&& e) -> kj::Promise { if (e.getType() == kj::Exception::Type::DISCONNECTED) { return kj::READY_NOW; } else { return kj::mv(e); } }); } } private: kj::ForkedPromise promise; kj::Maybe> stream; }; } // namespace Own newPromisedStream(Promise> promise) { return heap(kj::mv(promise)); } Own newPromisedStream(Promise> promise) { return heap(kj::mv(promise)); } Promise AsyncCapabilityStream::writeWithFds( ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) { // HACK: AutoCloseFd actually contains an `int` under the hood. We can reinterpret_cast to avoid // unnecessary memory allocation. static_assert(sizeof(AutoCloseFd) == sizeof(int), "this optimization won't work"); auto intArray = arrayPtr(reinterpret_cast(fds.begin()), fds.size()); // Be extra-paranoid about aliasing rules by injecting a compiler barrier here. Probably // not necessary but also probably doesn't hurt. #if _MSC_VER _ReadWriteBarrier(); #else __asm__ __volatile__("": : :"memory"); #endif return writeWithFds(data, moreData, intArray); } Promise> AsyncCapabilityStream::receiveStream() { return tryReceiveStream() .then([](Maybe>&& result) -> Promise> { KJ_IF_MAYBE(r, result) { return kj::mv(*r); } else { return KJ_EXCEPTION(FAILED, "EOF when expecting to receive capability"); } }); } kj::Promise>> AsyncCapabilityStream::tryReceiveStream() { struct ResultHolder { byte b; Own stream; }; auto result = kj::heap(); auto promise = tryReadWithStreams(&result->b, 1, 1, &result->stream, 1); return promise.then([result = kj::mv(result)](ReadResult actual) mutable -> Maybe> { if (actual.byteCount == 0) { return nullptr; } KJ_REQUIRE(actual.capCount == 1, "expected to receive a capability (e.g. file descirptor via SCM_RIGHTS), but didn't") { return nullptr; } return kj::mv(result->stream); }); } Promise AsyncCapabilityStream::sendStream(Own stream) { static constexpr byte b = 0; auto streams = kj::heapArray>(1); streams[0] = kj::mv(stream); return writeWithStreams(arrayPtr(&b, 1), nullptr, kj::mv(streams)); } Promise AsyncCapabilityStream::receiveFd() { return tryReceiveFd().then([](Maybe&& result) -> Promise { KJ_IF_MAYBE(r, result) { return kj::mv(*r); } else { return KJ_EXCEPTION(FAILED, "EOF when expecting to receive capability"); } }); } kj::Promise> AsyncCapabilityStream::tryReceiveFd() { struct ResultHolder { byte b; AutoCloseFd fd; }; auto result = kj::heap(); auto promise = tryReadWithFds(&result->b, 1, 1, &result->fd, 1); return promise.then([result = kj::mv(result)](ReadResult actual) mutable -> Maybe { if (actual.byteCount == 0) { return nullptr; } KJ_REQUIRE(actual.capCount == 1, "expected to receive a file descriptor (e.g. via SCM_RIGHTS), but didn't") { return nullptr; } return kj::mv(result->fd); }); } Promise AsyncCapabilityStream::sendFd(int fd) { static constexpr byte b = 0; auto fds = kj::heapArray(1); fds[0] = fd; auto promise = writeWithFds(arrayPtr(&b, 1), nullptr, fds); return promise.attach(kj::mv(fds)); } void AsyncIoStream::getsockopt(int level, int option, void* value, uint* length) { KJ_UNIMPLEMENTED("Not a socket.") { *length = 0; break; } } void AsyncIoStream::setsockopt(int level, int option, const void* value, uint length) { KJ_UNIMPLEMENTED("Not a socket.") { break; } } void AsyncIoStream::getsockname(struct sockaddr* addr, uint* length) { KJ_UNIMPLEMENTED("Not a socket.") { *length = 0; break; } } void AsyncIoStream::getpeername(struct sockaddr* addr, uint* length) { KJ_UNIMPLEMENTED("Not a socket.") { *length = 0; break; } } void ConnectionReceiver::getsockopt(int level, int option, void* value, uint* length) { KJ_UNIMPLEMENTED("Not a socket.") { *length = 0; break; } } void ConnectionReceiver::setsockopt(int level, int option, const void* value, uint length) { KJ_UNIMPLEMENTED("Not a socket.") { break; } } void ConnectionReceiver::getsockname(struct sockaddr* addr, uint* length) { KJ_UNIMPLEMENTED("Not a socket.") { *length = 0; break; } } void DatagramPort::getsockopt(int level, int option, void* value, uint* length) { KJ_UNIMPLEMENTED("Not a socket.") { *length = 0; break; } } void DatagramPort::setsockopt(int level, int option, const void* value, uint length) { KJ_UNIMPLEMENTED("Not a socket.") { break; } } Own NetworkAddress::bindDatagramPort() { KJ_UNIMPLEMENTED("Datagram sockets not implemented."); } Own LowLevelAsyncIoProvider::wrapDatagramSocketFd( Fd fd, LowLevelAsyncIoProvider::NetworkFilter& filter, uint flags) { KJ_UNIMPLEMENTED("Datagram sockets not implemented."); } #if !_WIN32 Own LowLevelAsyncIoProvider::wrapUnixSocketFd(Fd fd, uint flags) { KJ_UNIMPLEMENTED("Unix socket with FD passing not implemented."); } #endif CapabilityPipe AsyncIoProvider::newCapabilityPipe() { KJ_UNIMPLEMENTED("Capability pipes not implemented."); } Own LowLevelAsyncIoProvider::wrapInputFd(OwnFd&& fd, uint flags) { return wrapInputFd(reinterpret_cast(fd.release()), flags | TAKE_OWNERSHIP); } Own LowLevelAsyncIoProvider::wrapOutputFd(OwnFd&& fd, uint flags) { return wrapOutputFd(reinterpret_cast(fd.release()), flags | TAKE_OWNERSHIP); } Own LowLevelAsyncIoProvider::wrapSocketFd(OwnFd&& fd, uint flags) { return wrapSocketFd(reinterpret_cast(fd.release()), flags | TAKE_OWNERSHIP); } #if !_WIN32 Own LowLevelAsyncIoProvider::wrapUnixSocketFd(OwnFd&& fd, uint flags) { return wrapUnixSocketFd(reinterpret_cast(fd.release()), flags | TAKE_OWNERSHIP); } #endif Promise> LowLevelAsyncIoProvider::wrapConnectingSocketFd( OwnFd&& fd, const struct sockaddr* addr, uint addrlen, uint flags) { return wrapConnectingSocketFd(reinterpret_cast(fd.release()), addr, addrlen, flags | TAKE_OWNERSHIP); } Own LowLevelAsyncIoProvider::wrapListenSocketFd( OwnFd&& fd, NetworkFilter& filter, uint flags) { return wrapListenSocketFd(reinterpret_cast(fd.release()), filter, flags | TAKE_OWNERSHIP); } Own LowLevelAsyncIoProvider::wrapListenSocketFd(OwnFd&& fd, uint flags) { return wrapListenSocketFd(reinterpret_cast(fd.release()), flags | TAKE_OWNERSHIP); } Own LowLevelAsyncIoProvider::wrapDatagramSocketFd( OwnFd&& fd, NetworkFilter& filter, uint flags) { return wrapDatagramSocketFd(reinterpret_cast(fd.release()), filter, flags | TAKE_OWNERSHIP); } Own LowLevelAsyncIoProvider::wrapDatagramSocketFd(OwnFd&& fd, uint flags) { return wrapDatagramSocketFd(reinterpret_cast(fd.release()), flags | TAKE_OWNERSHIP); } namespace { class DummyNetworkFilter: public kj::LowLevelAsyncIoProvider::NetworkFilter { public: bool shouldAllow(const struct sockaddr* addr, uint addrlen) override { return true; } }; } // namespace LowLevelAsyncIoProvider::NetworkFilter& LowLevelAsyncIoProvider::NetworkFilter::getAllAllowed() { static DummyNetworkFilter result; return result; } // ======================================================================================= // Convenience adapters. Promise> CapabilityStreamConnectionReceiver::accept() { return inner.receiveStream() .then([](Own&& stream) -> Own { return kj::mv(stream); }); } uint CapabilityStreamConnectionReceiver::getPort() { return 0; } Promise> CapabilityStreamNetworkAddress::connect() { CapabilityPipe pipe; KJ_IF_MAYBE(p, provider) { pipe = p->newCapabilityPipe(); } else { pipe = kj::newCapabilityPipe(); } auto result = kj::mv(pipe.ends[0]); return inner.sendStream(kj::mv(pipe.ends[1])) .then(kj::mvCapture(result, [](Own&& result) { return kj::mv(result); })); } Own CapabilityStreamNetworkAddress::listen() { return kj::heap(inner); } Own CapabilityStreamNetworkAddress::clone() { KJ_UNIMPLEMENTED("can't clone CapabilityStreamNetworkAddress"); } String CapabilityStreamNetworkAddress::toString() { return kj::str(""); } // ======================================================================================= namespace _ { // private #if !_WIN32 kj::ArrayPtr safeUnixPath(const struct sockaddr_un* addr, uint addrlen) { KJ_REQUIRE(addr->sun_family == AF_UNIX, "not a unix address"); KJ_REQUIRE(addrlen >= offsetof(sockaddr_un, sun_path), "invalid unix address"); size_t maxPathlen = addrlen - offsetof(sockaddr_un, sun_path); size_t pathlen; if (maxPathlen > 0 && addr->sun_path[0] == '\0') { // Linux "abstract" unix address pathlen = strnlen(addr->sun_path + 1, maxPathlen - 1) + 1; } else { pathlen = strnlen(addr->sun_path, maxPathlen); } return kj::arrayPtr(addr->sun_path, pathlen); } #endif // !_WIN32 CidrRange::CidrRange(StringPtr pattern) { size_t slashPos = KJ_REQUIRE_NONNULL(pattern.findFirst('/'), "invalid CIDR", pattern); bitCount = pattern.slice(slashPos + 1).parseAs(); KJ_STACK_ARRAY(char, addr, slashPos + 1, 128, 128); memcpy(addr.begin(), pattern.begin(), slashPos); addr[slashPos] = '\0'; if (pattern.findFirst(':') == nullptr) { family = AF_INET; KJ_REQUIRE(bitCount <= 32, "invalid CIDR", pattern); } else { family = AF_INET6; KJ_REQUIRE(bitCount <= 128, "invalid CIDR", pattern); } KJ_ASSERT(inet_pton(family, addr.begin(), bits) > 0, "invalid CIDR", pattern); zeroIrrelevantBits(); } CidrRange::CidrRange(int family, ArrayPtr bits, uint bitCount) : family(family), bitCount(bitCount) { if (family == AF_INET) { KJ_REQUIRE(bitCount <= 32); } else { KJ_REQUIRE(bitCount <= 128); } KJ_REQUIRE(bits.size() * 8 >= bitCount); size_t byteCount = (bitCount + 7) / 8; memcpy(this->bits, bits.begin(), byteCount); memset(this->bits + byteCount, 0, sizeof(this->bits) - byteCount); zeroIrrelevantBits(); } CidrRange CidrRange::inet4(ArrayPtr bits, uint bitCount) { return CidrRange(AF_INET, bits, bitCount); } CidrRange CidrRange::inet6( ArrayPtr prefix, ArrayPtr suffix, uint bitCount) { KJ_REQUIRE(prefix.size() + suffix.size() <= 8); byte bits[16] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, }; for (size_t i: kj::indices(prefix)) { bits[i * 2] = prefix[i] >> 8; bits[i * 2 + 1] = prefix[i] & 0xff; } byte* suffixBits = bits + (16 - suffix.size() * 2); for (size_t i: kj::indices(suffix)) { suffixBits[i * 2] = suffix[i] >> 8; suffixBits[i * 2 + 1] = suffix[i] & 0xff; } return CidrRange(AF_INET6, bits, bitCount); } bool CidrRange::matches(const struct sockaddr* addr) const { const byte* otherBits; switch (family) { case AF_INET: if (addr->sa_family == AF_INET6) { otherBits = reinterpret_cast(addr)->sin6_addr.s6_addr; static constexpr byte V6MAPPED[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; if (memcmp(otherBits, V6MAPPED, sizeof(V6MAPPED)) == 0) { // We're an ipv4 range and the address is ipv6, but it's a "v6 mapped" address, meaning // it's equivalent to an ipv4 address. Try to match against the ipv4 part. otherBits = otherBits + sizeof(V6MAPPED); } else { return false; } } else if (addr->sa_family == AF_INET) { otherBits = reinterpret_cast( &reinterpret_cast(addr)->sin_addr.s_addr); } else { return false; } break; case AF_INET6: if (addr->sa_family != AF_INET6) return false; otherBits = reinterpret_cast(addr)->sin6_addr.s6_addr; break; default: KJ_UNREACHABLE; } if (memcmp(bits, otherBits, bitCount / 8) != 0) return false; return bitCount == 128 || bits[bitCount / 8] == (otherBits[bitCount / 8] & (0xff00 >> (bitCount % 8))); } bool CidrRange::matchesFamily(int family) const { switch (family) { case AF_INET: return this->family == AF_INET; case AF_INET6: // Even if we're a v4 CIDR, we can match v6 addresses in the v4-mapped range. return true; default: return false; } } String CidrRange::toString() const { char result[128]; KJ_ASSERT(inet_ntop(family, (void*)bits, result, sizeof(result)) == result); return kj::str(result, '/', bitCount); } void CidrRange::zeroIrrelevantBits() { // Mask out insignificant bits of partial byte. if (bitCount < 128) { bits[bitCount / 8] &= 0xff00 >> (bitCount % 8); // Zero the remaining bytes. size_t n = bitCount / 8 + 1; memset(bits + n, 0, sizeof(bits) - n); } } // ----------------------------------------------------------------------------- ArrayPtr localCidrs() { static const CidrRange result[] = { // localhost "127.0.0.0/8"_kj, "::1/128"_kj, // Trying to *connect* to 0.0.0.0 on many systems is equivalent to connecting to localhost. // (wat) "0.0.0.0/32"_kj, "::/128"_kj, }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents result from implicitly // casting to our return type. return kj::arrayPtr(result, kj::size(result)); } ArrayPtr privateCidrs() { static const CidrRange result[] = { "10.0.0.0/8"_kj, // RFC1918 reserved for internal network "100.64.0.0/10"_kj, // RFC6598 "shared address space" for carrier-grade NAT "169.254.0.0/16"_kj, // RFC3927 "link local" (auto-configured LAN in absence of DHCP) "172.16.0.0/12"_kj, // RFC1918 reserved for internal network "192.168.0.0/16"_kj, // RFC1918 reserved for internal network "fc00::/7"_kj, // RFC4193 unique private network "fe80::/10"_kj, // RFC4291 "link local" (auto-configured LAN in absence of DHCP) }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents result from implicitly // casting to our return type. return kj::arrayPtr(result, kj::size(result)); } ArrayPtr reservedCidrs() { static const CidrRange result[] = { "192.0.0.0/24"_kj, // RFC6890 reserved for special protocols "224.0.0.0/4"_kj, // RFC1112 multicast "240.0.0.0/4"_kj, // RFC1112 multicast / reserved for future use "255.255.255.255/32"_kj, // RFC0919 broadcast address "2001::/23"_kj, // RFC2928 reserved for special protocols "ff00::/8"_kj, // RFC4291 multicast }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents result from implicitly // casting to our return type. return kj::arrayPtr(result, kj::size(result)); } ArrayPtr exampleAddresses() { static const CidrRange result[] = { "192.0.2.0/24"_kj, // RFC5737 "example address" block 1 -- like example.com for IPs "198.51.100.0/24"_kj, // RFC5737 "example address" block 2 -- like example.com for IPs "203.0.113.0/24"_kj, // RFC5737 "example address" block 3 -- like example.com for IPs "2001:db8::/32"_kj, // RFC3849 "example address" block -- like example.com for IPs }; // TODO(cleanup): A bug in GCC 4.8, fixed in 4.9, prevents result from implicitly // casting to our return type. return kj::arrayPtr(result, kj::size(result)); } NetworkFilter::NetworkFilter() : allowUnix(true), allowAbstractUnix(true) { allowCidrs.add(CidrRange::inet4({0,0,0,0}, 0)); allowCidrs.add(CidrRange::inet6({}, {}, 0)); denyCidrs.addAll(reservedCidrs()); } NetworkFilter::NetworkFilter(ArrayPtr allow, ArrayPtr deny, NetworkFilter& next) : allowUnix(false), allowAbstractUnix(false), next(next) { for (auto rule: allow) { if (rule == "local") { allowCidrs.addAll(localCidrs()); } else if (rule == "network") { allowCidrs.add(CidrRange::inet4({0,0,0,0}, 0)); allowCidrs.add(CidrRange::inet6({}, {}, 0)); denyCidrs.addAll(localCidrs()); } else if (rule == "private") { allowCidrs.addAll(privateCidrs()); allowCidrs.addAll(localCidrs()); } else if (rule == "public") { allowCidrs.add(CidrRange::inet4({0,0,0,0}, 0)); allowCidrs.add(CidrRange::inet6({}, {}, 0)); denyCidrs.addAll(privateCidrs()); denyCidrs.addAll(localCidrs()); } else if (rule == "unix") { allowUnix = true; } else if (rule == "unix-abstract") { allowAbstractUnix = true; } else { allowCidrs.add(CidrRange(rule)); } } for (auto rule: deny) { if (rule == "local") { denyCidrs.addAll(localCidrs()); } else if (rule == "network") { KJ_FAIL_REQUIRE("don't deny 'network', allow 'local' instead"); } else if (rule == "private") { denyCidrs.addAll(privateCidrs()); } else if (rule == "public") { // Tricky: What if we allow 'network' and deny 'public'? KJ_FAIL_REQUIRE("don't deny 'public', allow 'private' instead"); } else if (rule == "unix") { allowUnix = false; } else if (rule == "unix-abstract") { allowAbstractUnix = false; } else { denyCidrs.add(CidrRange(rule)); } } } bool NetworkFilter::shouldAllow(const struct sockaddr* addr, uint addrlen) { KJ_REQUIRE(addrlen >= sizeof(addr->sa_family)); #if !_WIN32 if (addr->sa_family == AF_UNIX) { auto path = safeUnixPath(reinterpret_cast(addr), addrlen); if (path.size() > 0 && path[0] == '\0') { return allowAbstractUnix; } else { return allowUnix; } } #endif bool allowed = false; uint allowSpecificity = 0; for (auto& cidr: allowCidrs) { if (cidr.matches(addr)) { allowSpecificity = kj::max(allowSpecificity, cidr.getSpecificity()); allowed = true; } } if (!allowed) return false; for (auto& cidr: denyCidrs) { if (cidr.matches(addr)) { if (cidr.getSpecificity() >= allowSpecificity) return false; } } KJ_IF_MAYBE(n, next) { return n->shouldAllow(addr, addrlen); } else { return true; } } bool NetworkFilter::shouldAllowParse(const struct sockaddr* addr, uint addrlen) { bool matched = false; #if !_WIN32 if (addr->sa_family == AF_UNIX) { auto path = safeUnixPath(reinterpret_cast(addr), addrlen); if (path.size() > 0 && path[0] == '\0') { if (allowAbstractUnix) matched = true; } else { if (allowUnix) matched = true; } } else { #endif for (auto& cidr: allowCidrs) { if (cidr.matchesFamily(addr->sa_family)) { matched = true; } } #if !_WIN32 } #endif if (matched) { KJ_IF_MAYBE(n, next) { return n->shouldAllowParse(addr, addrlen); } else { return true; } } else { // No allow rule matches this address family, so don't even allow parsing it. return false; } } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/async-io-unix.c++0000644000175000017500000017033113650101756021151 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if !_WIN32 // For Win32 implementation, see async-io-win32.c++. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "async-io.h" #include "async-io-internal.h" #include "async-unix.h" #include "debug.h" #include "thread.h" #include "io.h" #include "miniposix.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace kj { namespace { void setNonblocking(int fd) { #ifdef FIONBIO int opt = 1; KJ_SYSCALL(ioctl(fd, FIONBIO, &opt)); #else int flags; KJ_SYSCALL(flags = fcntl(fd, F_GETFL)); if ((flags & O_NONBLOCK) == 0) { KJ_SYSCALL(fcntl(fd, F_SETFL, flags | O_NONBLOCK)); } #endif } void setCloseOnExec(int fd) { #ifdef FIOCLEX KJ_SYSCALL(ioctl(fd, FIOCLEX)); #else int flags; KJ_SYSCALL(flags = fcntl(fd, F_GETFD)); if ((flags & FD_CLOEXEC) == 0) { KJ_SYSCALL(fcntl(fd, F_SETFD, flags | FD_CLOEXEC)); } #endif } static constexpr uint NEW_FD_FLAGS = #if __linux__ && !__BIONIC__ LowLevelAsyncIoProvider::ALREADY_CLOEXEC | LowLevelAsyncIoProvider::ALREADY_NONBLOCK | #endif LowLevelAsyncIoProvider::TAKE_OWNERSHIP; // We always try to open FDs with CLOEXEC and NONBLOCK already set on Linux, but on other platforms // this is not possible. class OwnedFileDescriptor { public: OwnedFileDescriptor(int fd, uint flags): fd(fd), flags(flags) { if (flags & LowLevelAsyncIoProvider::ALREADY_NONBLOCK) { KJ_DREQUIRE(fcntl(fd, F_GETFL) & O_NONBLOCK, "You claimed you set NONBLOCK, but you didn't."); } else { setNonblocking(fd); } if (flags & LowLevelAsyncIoProvider::TAKE_OWNERSHIP) { if (flags & LowLevelAsyncIoProvider::ALREADY_CLOEXEC) { KJ_DREQUIRE(fcntl(fd, F_GETFD) & FD_CLOEXEC, "You claimed you set CLOEXEC, but you didn't."); } else { setCloseOnExec(fd); } } } ~OwnedFileDescriptor() noexcept(false) { // Don't use SYSCALL() here because close() should not be repeated on EINTR. if ((flags & LowLevelAsyncIoProvider::TAKE_OWNERSHIP) && close(fd) < 0) { KJ_FAIL_SYSCALL("close", errno, fd) { // Recoverable exceptions are safe in destructors. break; } } } protected: const int fd; private: uint flags; }; // ======================================================================================= class AsyncStreamFd: public OwnedFileDescriptor, public AsyncCapabilityStream { public: AsyncStreamFd(UnixEventPort& eventPort, int fd, uint flags) : OwnedFileDescriptor(fd, flags), eventPort(eventPort), observer(eventPort, fd, UnixEventPort::FdObserver::OBSERVE_READ_WRITE) {} virtual ~AsyncStreamFd() noexcept(false) {} Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { return tryReadInternal(buffer, minBytes, maxBytes, nullptr, 0, {0,0}) .then([](ReadResult r) { return r.byteCount; }); } Promise tryReadWithFds(void* buffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds) override { return tryReadInternal(buffer, minBytes, maxBytes, fdBuffer, maxFds, {0,0}); } Promise tryReadWithStreams( void* buffer, size_t minBytes, size_t maxBytes, Own* streamBuffer, size_t maxStreams) override { auto fdBuffer = kj::heapArray(maxStreams); auto promise = tryReadInternal(buffer, minBytes, maxBytes, fdBuffer.begin(), maxStreams, {0,0}); return promise.then([this, fdBuffer = kj::mv(fdBuffer), streamBuffer] (ReadResult result) mutable { for (auto i: kj::zeroTo(result.capCount)) { streamBuffer[i] = kj::heap(eventPort, fdBuffer[i].release(), LowLevelAsyncIoProvider::TAKE_OWNERSHIP | LowLevelAsyncIoProvider::ALREADY_CLOEXEC); } return result; }); } Promise write(const void* buffer, size_t size) override { ssize_t n; KJ_NONBLOCKING_SYSCALL(n = ::write(fd, buffer, size)) { // Error. // We can't "return kj::READY_NOW;" inside this block because it causes a memory leak due to // a bug that exists in both Clang and GCC: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799 // http://llvm.org/bugs/show_bug.cgi?id=12286 goto error; } if (false) { error: return kj::READY_NOW; } if (n < 0) { // EAGAIN -- need to wait for writability and try again. return observer.whenBecomesWritable().then([=]() { return write(buffer, size); }); } else if (n == size) { // All done. return READY_NOW; } else { // Fewer than `size` bytes were written, but we CANNOT assume we're out of buffer space, as // Linux is known to return partial reads/writes when interrupted by a signal -- yes, even // for non-blocking operations. So, we'll need to write() again now, even though it will // almost certainly fail with EAGAIN. See comments in the read path for more info. buffer = reinterpret_cast(buffer) + n; size -= n; return write(buffer, size); } } Promise write(ArrayPtr> pieces) override { if (pieces.size() == 0) { return writeInternal(nullptr, nullptr, nullptr); } else { return writeInternal(pieces[0], pieces.slice(1, pieces.size()), nullptr); } } Promise writeWithFds(ArrayPtr data, ArrayPtr> moreData, ArrayPtr fds) override { return writeInternal(data, moreData, fds); } Promise writeWithStreams(ArrayPtr data, ArrayPtr> moreData, Array> streams) override { auto fds = KJ_MAP(stream, streams) { return downcast(*stream).fd; }; auto promise = writeInternal(data, moreData, fds); return promise.attach(kj::mv(fds), kj::mv(streams)); } Promise whenWriteDisconnected() override { KJ_IF_MAYBE(p, writeDisconnectedPromise) { return p->addBranch(); } else { auto fork = observer.whenWriteDisconnected().fork(); auto result = fork.addBranch(); writeDisconnectedPromise = kj::mv(fork); return kj::mv(result); } } void shutdownWrite() override { // There's no legitimate way to get an AsyncStreamFd that isn't a socket through the // UnixAsyncIoProvider interface. KJ_SYSCALL(shutdown(fd, SHUT_WR)); } void abortRead() override { // There's no legitimate way to get an AsyncStreamFd that isn't a socket through the // UnixAsyncIoProvider interface. KJ_SYSCALL(shutdown(fd, SHUT_RD)); } void getsockopt(int level, int option, void* value, uint* length) override { socklen_t socklen = *length; KJ_SYSCALL(::getsockopt(fd, level, option, value, &socklen)); *length = socklen; } void setsockopt(int level, int option, const void* value, uint length) override { KJ_SYSCALL(::setsockopt(fd, level, option, value, length)); } void getsockname(struct sockaddr* addr, uint* length) override { socklen_t socklen = *length; KJ_SYSCALL(::getsockname(fd, addr, &socklen)); *length = socklen; } void getpeername(struct sockaddr* addr, uint* length) override { socklen_t socklen = *length; KJ_SYSCALL(::getpeername(fd, addr, &socklen)); *length = socklen; } Promise waitConnected() { // Wait until initial connection has completed. This actually just waits until it is writable. // Can't just go directly to writeObserver.whenBecomesWritable() because of edge triggering. We // need to explicitly check if the socket is already connected. struct pollfd pollfd; memset(&pollfd, 0, sizeof(pollfd)); pollfd.fd = fd; pollfd.events = POLLOUT; int pollResult; KJ_SYSCALL(pollResult = poll(&pollfd, 1, 0)); if (pollResult == 0) { // Not ready yet. We can safely use the edge-triggered observer. return observer.whenBecomesWritable(); } else { // Ready now. return kj::READY_NOW; } } private: UnixEventPort& eventPort; UnixEventPort::FdObserver observer; Maybe> writeDisconnectedPromise; Promise tryReadInternal(void* buffer, size_t minBytes, size_t maxBytes, AutoCloseFd* fdBuffer, size_t maxFds, ReadResult alreadyRead) { // `alreadyRead` is the number of bytes we have already received via previous reads -- minBytes, // maxBytes, and buffer have already been adjusted to account for them, but this count must // be included in the final return value. ssize_t n; if (maxFds == 0) { KJ_NONBLOCKING_SYSCALL(n = ::read(fd, buffer, maxBytes)) { // Error. // We can't "return kj::READY_NOW;" inside this block because it causes a memory leak due to // a bug that exists in both Clang and GCC: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799 // http://llvm.org/bugs/show_bug.cgi?id=12286 goto error; } } else { struct msghdr msg; memset(&msg, 0, sizeof(msg)); struct iovec iov; memset(&iov, 0, sizeof(iov)); iov.iov_base = buffer; iov.iov_len = maxBytes; msg.msg_iov = &iov; msg.msg_iovlen = 1; // Allocate space to receive a cmsg. #if __APPLE__ || __FreeBSD__ // Until very recently (late 2018 / early 2019), FreeBSD suffered from a bug in which when // an SCM_RIGHTS message was truncated on delivery, it would not close the FDs that weren't // delivered -- they would simply leak: https://bugs.freebsd.org/131876 // // My testing indicates that MacOS has this same bug as of today (April 2019). I don't know // if they plan to fix it or are even aware of it. // // To handle both cases, we will always provide space to receive 512 FDs. Hopefully, this is // greater than the maximum number of FDs that these kernels will transmit in one message // PLUS enough space for any other ancillary messages that could be sent before the // SCM_RIGHTS message to push it back in the buffer. I couldn't find any firm documentation // on these limits, though -- I only know that Linux is limited to 253, and I saw a hint in // a comment in someone else's application that suggested FreeBSD is the same. Hopefully, // then, this is sufficient to prevent attacks. But if not, there's nothing more we can do; // it's really up to the kernel to fix this. size_t msgBytes = CMSG_SPACE(sizeof(int) * 512); #else size_t msgBytes = CMSG_SPACE(sizeof(int) * maxFds); #endif // On Linux, CMSG_SPACE will align to a word-size boundary, but on Mac it always aligns to a // 32-bit boundary. I guess aligning to 32 bits helps avoid the problem where you // surprisingly end up with space for two file descriptors when you only wanted one. However, // cmsghdr's preferred alignment is word-size (it contains a size_t). If we stack-allocate // the buffer, we need to make sure it is aligned properly (maybe not on x64, but maybe on // other platforms), so we want to allocate an array of words (we use void*). So... we use // CMSG_SPACE() and then additionally round up to deal with Mac. size_t msgWords = (msgBytes + sizeof(void*) - 1) / sizeof(void*); KJ_STACK_ARRAY(void*, cmsgSpace, msgWords, 16, 256); auto cmsgBytes = cmsgSpace.asBytes(); memset(cmsgBytes.begin(), 0, cmsgBytes.size()); msg.msg_control = cmsgBytes.begin(); msg.msg_controllen = msgBytes; #ifdef MSG_CMSG_CLOEXEC static constexpr int RECVMSG_FLAGS = MSG_CMSG_CLOEXEC; #else static constexpr int RECVMSG_FLAGS = 0; #endif KJ_NONBLOCKING_SYSCALL(n = ::recvmsg(fd, &msg, RECVMSG_FLAGS)) { // Error. // We can't "return kj::READY_NOW;" inside this block because it causes a memory leak due to // a bug that exists in both Clang and GCC: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799 // http://llvm.org/bugs/show_bug.cgi?id=12286 goto error; } if (n >= 0) { // Process all messages. // // WARNING DANGER: We have to be VERY careful not to miss a file descriptor here, because // if we do, then that FD will never be closed, and a malicious peer could exploit this to // fill up our FD table, creating a DoS attack. Some things to keep in mind: // - CMSG_SPACE() could have rounded up the space for alignment purposes, and this could // mean we permitted the kernel to deliver more file descriptors than `maxFds`. We need // to close the extras. // - We can receive multiple ancillary messages at once. In particular, there is also // SCM_CREDENTIALS. The sender decides what to send. They could send SCM_CREDENTIALS // first followed by SCM_RIGHTS. We need to make sure we see both. size_t nfds = 0; size_t spaceLeft = msg.msg_controllen; for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (spaceLeft >= CMSG_LEN(0) && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { // Some operating systems (like MacOS) do not adjust csmg_len when the message is // truncated. We must do so ourselves or risk overrunning the buffer. auto len = kj::min(cmsg->cmsg_len, spaceLeft); auto data = arrayPtr(reinterpret_cast(CMSG_DATA(cmsg)), (len - CMSG_LEN(0)) / sizeof(int)); kj::Vector trashFds; for (auto fd: data) { kj::AutoCloseFd ownFd(fd); if (nfds < maxFds) { fdBuffer[nfds++] = kj::mv(ownFd); } else { trashFds.add(kj::mv(ownFd)); } } } if (spaceLeft >= CMSG_LEN(0) && spaceLeft >= cmsg->cmsg_len) { spaceLeft -= cmsg->cmsg_len; } else { spaceLeft = 0; } } #ifndef MSG_CMSG_CLOEXEC for (size_t i = 0; i < nfds; i++) { setCloseOnExec(fdBuffer[i]); } #endif alreadyRead.capCount += nfds; fdBuffer += nfds; maxFds -= nfds; } } if (false) { error: return alreadyRead; } if (n < 0) { // Read would block. return observer.whenBecomesReadable().then([=]() { return tryReadInternal(buffer, minBytes, maxBytes, fdBuffer, maxFds, alreadyRead); }); } else if (n == 0) { // EOF -OR- maxBytes == 0. return alreadyRead; } else if (implicitCast(n) >= minBytes) { // We read enough to stop here. alreadyRead.byteCount += n; return alreadyRead; } else { // The kernel returned fewer bytes than we asked for (and fewer than we need). buffer = reinterpret_cast(buffer) + n; minBytes -= n; maxBytes -= n; alreadyRead.byteCount += n; // According to David Klempner, who works on Stubby at Google, we sadly CANNOT assume that // we've consumed the whole read buffer here. If a signal is delivered in the middle of a // read() -- yes, even a non-blocking read -- it can cause the kernel to return a partial // result, with data still in the buffer. // https://bugzilla.kernel.org/show_bug.cgi?id=199131 // https://twitter.com/CaptainSegfault/status/1112622245531144194 // // Unfortunately, we have no choice but to issue more read()s until it either tells us EOF // or EAGAIN. We used to have an optimization here using observer.atEndHint() (when it is // non-null) to avoid a redundant call to read(). Alas... return tryReadInternal(buffer, minBytes, maxBytes, fdBuffer, maxFds, alreadyRead); } } Promise writeInternal(ArrayPtr firstPiece, ArrayPtr> morePieces, ArrayPtr fds) { const size_t iovmax = kj::miniposix::iovMax(1 + morePieces.size()); // If there are more than IOV_MAX pieces, we'll only write the first IOV_MAX for now, and // then we'll loop later. KJ_STACK_ARRAY(struct iovec, iov, kj::min(1 + morePieces.size(), iovmax), 16, 128); size_t iovTotal = 0; // writev() interface is not const-correct. :( iov[0].iov_base = const_cast(firstPiece.begin()); iov[0].iov_len = firstPiece.size(); iovTotal += iov[0].iov_len; for (uint i = 1; i < iov.size(); i++) { iov[i].iov_base = const_cast(morePieces[i - 1].begin()); iov[i].iov_len = morePieces[i - 1].size(); iovTotal += iov[i].iov_len; } if (iovTotal == 0) { KJ_REQUIRE(fds.size() == 0, "can't write FDs without bytes"); return kj::READY_NOW; } ssize_t n; if (fds.size() == 0) { KJ_NONBLOCKING_SYSCALL(n = ::writev(fd, iov.begin(), iov.size())) { // Error. // We can't "return kj::READY_NOW;" inside this block because it causes a memory leak due to // a bug that exists in both Clang and GCC: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799 // http://llvm.org/bugs/show_bug.cgi?id=12286 goto error; } } else { struct msghdr msg; memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov.begin(); msg.msg_iovlen = iov.size(); // Allocate space to receive a cmsg. size_t msgBytes = CMSG_SPACE(sizeof(int) * fds.size()); // On Linux, CMSG_SPACE will align to a word-size boundary, but on Mac it always aligns to a // 32-bit boundary. I guess aligning to 32 bits helps avoid the problem where you // surprisingly end up with space for two file descriptors when you only wanted one. However, // cmsghdr's preferred alignment is word-size (it contains a size_t). If we stack-allocate // the buffer, we need to make sure it is aligned properly (maybe not on x64, but maybe on // other platforms), so we want to allocate an array of words (we use void*). So... we use // CMSG_SPACE() and then additionally round up to deal with Mac. size_t msgWords = (msgBytes + sizeof(void*) - 1) / sizeof(void*); KJ_STACK_ARRAY(void*, cmsgSpace, msgWords, 16, 256); auto cmsgBytes = cmsgSpace.asBytes(); memset(cmsgBytes.begin(), 0, cmsgBytes.size()); msg.msg_control = cmsgBytes.begin(); msg.msg_controllen = msgBytes; struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fds.size()); memcpy(CMSG_DATA(cmsg), fds.begin(), fds.asBytes().size()); KJ_NONBLOCKING_SYSCALL(n = ::sendmsg(fd, &msg, 0)) { // Error. // We can't "return kj::READY_NOW;" inside this block because it causes a memory leak due to // a bug that exists in both Clang and GCC: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799 // http://llvm.org/bugs/show_bug.cgi?id=12286 goto error; } } if (false) { error: return kj::READY_NOW; } if (n < 0) { // Got EAGAIN. Nothing was written. return observer.whenBecomesWritable().then([=]() { return writeInternal(firstPiece, morePieces, fds); }); } else if (n == 0) { // Why would a sendmsg() with a non-empty message ever return 0 when writing to a stream // socket? If there's no room in the send buffer, it should fail with EAGAIN. If the // connection is closed, it should fail with EPIPE. Various documents and forum posts around // the internet claim this can happen but no one seems to know when. My guess is it can only // happen if we try to send an empty message -- which we didn't. So I think this is // impossible. If it is possible, we need to figure out how to correctly handle it, which // depends on what caused it. // // Note in particular that if 0 is a valid return here, and we sent an SCM_RIGHTS message, // we need to know whether the message was sent or not, in order to decide whether to retry // sending it! KJ_FAIL_ASSERT("non-empty sendmsg() returned 0"); } // Non-zero bytes were written. This also implies that *all* FDs were written. // Discard all data that was written, then issue a new write for what's left (if any). for (;;) { if (n < firstPiece.size()) { // Only part of the first piece was consumed. Wait for buffer space and then write again. firstPiece = firstPiece.slice(n, firstPiece.size()); iovTotal -= n; if (iovTotal == 0) { // Oops, what actually happened is that we hit the IOV_MAX limit. Don't wait. return writeInternal(firstPiece, morePieces, nullptr); } // As with read(), we cannot assume that a short write() really means the write buffer is // full (see comments in the read path above). We have to write again. return writeInternal(firstPiece, morePieces, nullptr); } else if (morePieces.size() == 0) { // First piece was fully-consumed and there are no more pieces, so we're done. KJ_DASSERT(n == firstPiece.size(), n); return READY_NOW; } else { // First piece was fully consumed, so move on to the next piece. n -= firstPiece.size(); iovTotal -= firstPiece.size(); firstPiece = morePieces[0]; morePieces = morePieces.slice(1, morePieces.size()); } } } }; // ======================================================================================= class SocketAddress { public: SocketAddress(const void* sockaddr, uint len): addrlen(len) { KJ_REQUIRE(len <= sizeof(addr), "Sorry, your sockaddr is too big for me."); memcpy(&addr.generic, sockaddr, len); } bool operator<(const SocketAddress& other) const { // So we can use std::set... see DNS lookup code. if (wildcard < other.wildcard) return true; if (wildcard > other.wildcard) return false; if (addrlen < other.addrlen) return true; if (addrlen > other.addrlen) return false; return memcmp(&addr.generic, &other.addr.generic, addrlen) < 0; } const struct sockaddr* getRaw() const { return &addr.generic; } socklen_t getRawSize() const { return addrlen; } int socket(int type) const { bool isStream = type == SOCK_STREAM; int result; #if __linux__ && !__BIONIC__ type |= SOCK_NONBLOCK | SOCK_CLOEXEC; #endif KJ_SYSCALL(result = ::socket(addr.generic.sa_family, type, 0)); if (isStream && (addr.generic.sa_family == AF_INET || addr.generic.sa_family == AF_INET6)) { // TODO(perf): As a hack for the 0.4 release we are always setting // TCP_NODELAY because Nagle's algorithm pretty much kills Cap'n Proto's // RPC protocol. Later, we should extend the interface to provide more // control over this. Perhaps write() should have a flag which // specifies whether to pass MSG_MORE. int one = 1; KJ_SYSCALL(setsockopt( result, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one))); } return result; } void bind(int sockfd) const { #if !defined(__OpenBSD__) if (wildcard) { // Disable IPV6_V6ONLY because we want to handle both ipv4 and ipv6 on this socket. (The // default value of this option varies across platforms.) int value = 0; KJ_SYSCALL(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &value, sizeof(value))); } #endif KJ_SYSCALL(::bind(sockfd, &addr.generic, addrlen), toString()); } uint getPort() const { switch (addr.generic.sa_family) { case AF_INET: return ntohs(addr.inet4.sin_port); case AF_INET6: return ntohs(addr.inet6.sin6_port); default: return 0; } } String toString() const { if (wildcard) { return str("*:", getPort()); } switch (addr.generic.sa_family) { case AF_INET: { char buffer[INET6_ADDRSTRLEN]; if (inet_ntop(addr.inet4.sin_family, &addr.inet4.sin_addr, buffer, sizeof(buffer)) == nullptr) { KJ_FAIL_SYSCALL("inet_ntop", errno) { break; } return heapString("(inet_ntop error)"); } return str(buffer, ':', ntohs(addr.inet4.sin_port)); } case AF_INET6: { char buffer[INET6_ADDRSTRLEN]; if (inet_ntop(addr.inet6.sin6_family, &addr.inet6.sin6_addr, buffer, sizeof(buffer)) == nullptr) { KJ_FAIL_SYSCALL("inet_ntop", errno) { break; } return heapString("(inet_ntop error)"); } return str('[', buffer, "]:", ntohs(addr.inet6.sin6_port)); } case AF_UNIX: { auto path = _::safeUnixPath(&addr.unixDomain, addrlen); if (path.size() > 0 && path[0] == '\0') { return str("unix-abstract:", path.slice(1, path.size())); } else { return str("unix:", path); } } default: return str("(unknown address family ", addr.generic.sa_family, ")"); } } static Promise> lookupHost( LowLevelAsyncIoProvider& lowLevel, kj::String host, kj::String service, uint portHint, _::NetworkFilter& filter); // Perform a DNS lookup. static Promise> parse( LowLevelAsyncIoProvider& lowLevel, StringPtr str, uint portHint, _::NetworkFilter& filter) { // TODO(someday): Allow commas in `str`. SocketAddress result; if (str.startsWith("unix:")) { StringPtr path = str.slice(strlen("unix:")); KJ_REQUIRE(path.size() < sizeof(addr.unixDomain.sun_path), "Unix domain socket address is too long.", str); KJ_REQUIRE(path.size() == strlen(path.cStr()), "Unix domain socket address contains NULL. Use" " 'unix-abstract:' for the abstract namespace."); result.addr.unixDomain.sun_family = AF_UNIX; strcpy(result.addr.unixDomain.sun_path, path.cStr()); result.addrlen = offsetof(struct sockaddr_un, sun_path) + path.size() + 1; if (!result.parseAllowedBy(filter)) { KJ_FAIL_REQUIRE("unix sockets blocked by restrictPeers()"); return Array(); } auto array = kj::heapArrayBuilder(1); array.add(result); return array.finish(); } if (str.startsWith("unix-abstract:")) { StringPtr path = str.slice(strlen("unix-abstract:")); KJ_REQUIRE(path.size() + 1 < sizeof(addr.unixDomain.sun_path), "Unix domain socket address is too long.", str); result.addr.unixDomain.sun_family = AF_UNIX; result.addr.unixDomain.sun_path[0] = '\0'; // although not strictly required by Linux, also copy the trailing // NULL terminator so that we can safely read it back in toString memcpy(result.addr.unixDomain.sun_path + 1, path.cStr(), path.size() + 1); result.addrlen = offsetof(struct sockaddr_un, sun_path) + path.size() + 1; if (!result.parseAllowedBy(filter)) { KJ_FAIL_REQUIRE("abstract unix sockets blocked by restrictPeers()"); return Array(); } auto array = kj::heapArrayBuilder(1); array.add(result); return array.finish(); } // Try to separate the address and port. ArrayPtr addrPart; Maybe portPart; int af; if (str.startsWith("[")) { // Address starts with a bracket, which is a common way to write an ip6 address with a port, // since without brackets around the address part, the port looks like another segment of // the address. af = AF_INET6; size_t closeBracket = KJ_ASSERT_NONNULL(str.findLast(']'), "Unclosed '[' in address string.", str); addrPart = str.slice(1, closeBracket); if (str.size() > closeBracket + 1) { KJ_REQUIRE(str.slice(closeBracket + 1).startsWith(":"), "Expected port suffix after ']'.", str); portPart = str.slice(closeBracket + 2); } } else { KJ_IF_MAYBE(colon, str.findFirst(':')) { if (str.slice(*colon + 1).findFirst(':') == nullptr) { // There is exactly one colon and no brackets, so it must be an ip4 address with port. af = AF_INET; addrPart = str.slice(0, *colon); portPart = str.slice(*colon + 1); } else { // There are two or more colons and no brackets, so the whole thing must be an ip6 // address with no port. af = AF_INET6; addrPart = str; } } else { // No colons, so it must be an ip4 address without port. af = AF_INET; addrPart = str; } } // Parse the port. unsigned long port; KJ_IF_MAYBE(portText, portPart) { char* endptr; port = strtoul(portText->cStr(), &endptr, 0); if (portText->size() == 0 || *endptr != '\0') { // Not a number. Maybe it's a service name. Fall back to DNS. return lookupHost(lowLevel, kj::heapString(addrPart), kj::heapString(*portText), portHint, filter); } KJ_REQUIRE(port < 65536, "Port number too large."); } else { port = portHint; } // Check for wildcard. if (addrPart.size() == 1 && addrPart[0] == '*') { result.wildcard = true; #if defined(__OpenBSD__) // On OpenBSD, all sockets are either v4-only or v6-only, so use v4 as a // temporary workaround for wildcards. result.addrlen = sizeof(addr.inet4); result.addr.inet4.sin_family = AF_INET; result.addr.inet4.sin_port = htons(port); #else // Create an ip6 socket and set IPV6_V6ONLY to 0 later. result.addrlen = sizeof(addr.inet6); result.addr.inet6.sin6_family = AF_INET6; result.addr.inet6.sin6_port = htons(port); #endif auto array = kj::heapArrayBuilder(1); array.add(result); return array.finish(); } void* addrTarget; if (af == AF_INET6) { result.addrlen = sizeof(addr.inet6); result.addr.inet6.sin6_family = AF_INET6; result.addr.inet6.sin6_port = htons(port); addrTarget = &result.addr.inet6.sin6_addr; } else { result.addrlen = sizeof(addr.inet4); result.addr.inet4.sin_family = AF_INET; result.addr.inet4.sin_port = htons(port); addrTarget = &result.addr.inet4.sin_addr; } if (addrPart.size() < INET6_ADDRSTRLEN - 1) { // addrPart is not necessarily NUL-terminated so we have to make a copy. :( char buffer[INET6_ADDRSTRLEN]; memcpy(buffer, addrPart.begin(), addrPart.size()); buffer[addrPart.size()] = '\0'; // OK, parse it! switch (inet_pton(af, buffer, addrTarget)) { case 1: { // success. if (!result.parseAllowedBy(filter)) { KJ_FAIL_REQUIRE("address family blocked by restrictPeers()"); return Array(); } auto array = kj::heapArrayBuilder(1); array.add(result); return array.finish(); } case 0: // It's apparently not a simple address... fall back to DNS. break; default: KJ_FAIL_SYSCALL("inet_pton", errno, af, addrPart); } } return lookupHost(lowLevel, kj::heapString(addrPart), nullptr, port, filter); } static SocketAddress getLocalAddress(int sockfd) { SocketAddress result; result.addrlen = sizeof(addr); KJ_SYSCALL(getsockname(sockfd, &result.addr.generic, &result.addrlen)); return result; } bool allowedBy(LowLevelAsyncIoProvider::NetworkFilter& filter) { return filter.shouldAllow(&addr.generic, addrlen); } bool parseAllowedBy(_::NetworkFilter& filter) { return filter.shouldAllowParse(&addr.generic, addrlen); } private: SocketAddress() { // We need to memset the whole object 0 otherwise Valgrind gets unhappy when we write it to a // pipe, due to the padding bytes being uninitialized. memset(this, 0, sizeof(*this)); } socklen_t addrlen; bool wildcard = false; union { struct sockaddr generic; struct sockaddr_in inet4; struct sockaddr_in6 inet6; struct sockaddr_un unixDomain; struct sockaddr_storage storage; } addr; struct LookupParams; class LookupReader; }; class SocketAddress::LookupReader { // Reads SocketAddresses off of a pipe coming from another thread that is performing // getaddrinfo. public: LookupReader(kj::Own&& thread, kj::Own&& input, _::NetworkFilter& filter) : thread(kj::mv(thread)), input(kj::mv(input)), filter(filter) {} ~LookupReader() { if (thread) thread->detach(); } Promise> read() { return input->tryRead(¤t, sizeof(current), sizeof(current)).then( [this](size_t n) -> Promise> { if (n < sizeof(current)) { thread = nullptr; // getaddrinfo()'s docs seem to say it will never return an empty list, but let's check // anyway. KJ_REQUIRE(addresses.size() > 0, "DNS lookup returned no permitted addresses.") { break; } return addresses.releaseAsArray(); } else { // getaddrinfo() can return multiple copies of the same address for several reasons. // A major one is that we don't give it a socket type (SOCK_STREAM vs. SOCK_DGRAM), so // it may return two copies of the same address, one for each type, unless it explicitly // knows that the service name given is specific to one type. But we can't tell it a type, // because we don't actually know which one the user wants, and if we specify SOCK_STREAM // while the user specified a UDP service name then they'll get a resolution error which // is lame. (At least, I think that's how it works.) // // So we instead resort to de-duping results. if (alreadySeen.insert(current).second) { if (current.parseAllowedBy(filter)) { addresses.add(current); } } return read(); } }); } private: kj::Own thread; kj::Own input; _::NetworkFilter& filter; SocketAddress current; kj::Vector addresses; std::set alreadySeen; }; struct SocketAddress::LookupParams { kj::String host; kj::String service; }; Promise> SocketAddress::lookupHost( LowLevelAsyncIoProvider& lowLevel, kj::String host, kj::String service, uint portHint, _::NetworkFilter& filter) { // This shitty function spawns a thread to run getaddrinfo(). Unfortunately, getaddrinfo() is // the only cross-platform DNS API and it is blocking. // // TODO(perf): Use a thread pool? Maybe kj::Thread should use a thread pool automatically? // Maybe use the various platform-specific asynchronous DNS libraries? Please do not implement // a custom DNS resolver... int fds[2]; #if __linux__ && !__BIONIC__ KJ_SYSCALL(pipe2(fds, O_NONBLOCK | O_CLOEXEC)); #else KJ_SYSCALL(pipe(fds)); #endif auto input = lowLevel.wrapInputFd(fds[0], NEW_FD_FLAGS); int outFd = fds[1]; LookupParams params = { kj::mv(host), kj::mv(service) }; auto thread = heap(kj::mvCapture(params, [outFd,portHint](LookupParams&& params) { FdOutputStream output((AutoCloseFd(outFd))); struct addrinfo* list; int status = getaddrinfo( params.host == "*" ? nullptr : params.host.cStr(), params.service == nullptr ? nullptr : params.service.cStr(), nullptr, &list); if (status == 0) { KJ_DEFER(freeaddrinfo(list)); struct addrinfo* cur = list; while (cur != nullptr) { if (params.service == nullptr) { switch (cur->ai_addr->sa_family) { case AF_INET: ((struct sockaddr_in*)cur->ai_addr)->sin_port = htons(portHint); break; case AF_INET6: ((struct sockaddr_in6*)cur->ai_addr)->sin6_port = htons(portHint); break; default: break; } } SocketAddress addr; if (params.host == "*") { // Set up a wildcard SocketAddress. Only use the port number returned by getaddrinfo(). addr.wildcard = true; addr.addrlen = sizeof(addr.addr.inet6); addr.addr.inet6.sin6_family = AF_INET6; switch (cur->ai_addr->sa_family) { case AF_INET: addr.addr.inet6.sin6_port = ((struct sockaddr_in*)cur->ai_addr)->sin_port; break; case AF_INET6: addr.addr.inet6.sin6_port = ((struct sockaddr_in6*)cur->ai_addr)->sin6_port; break; default: addr.addr.inet6.sin6_port = portHint; break; } } else { addr.addrlen = cur->ai_addrlen; memcpy(&addr.addr.generic, cur->ai_addr, cur->ai_addrlen); } KJ_ASSERT_CAN_MEMCPY(SocketAddress); output.write(&addr, sizeof(addr)); cur = cur->ai_next; } } else if (status == EAI_SYSTEM) { KJ_FAIL_SYSCALL("getaddrinfo", errno, params.host, params.service) { return; } } else { KJ_FAIL_REQUIRE("DNS lookup failed.", params.host, params.service, gai_strerror(status)) { return; } } })); auto reader = heap(kj::mv(thread), kj::mv(input), filter); return reader->read().attach(kj::mv(reader)); } // ======================================================================================= class FdConnectionReceiver final: public ConnectionReceiver, public OwnedFileDescriptor { public: FdConnectionReceiver(UnixEventPort& eventPort, int fd, LowLevelAsyncIoProvider::NetworkFilter& filter, uint flags) : OwnedFileDescriptor(fd, flags), eventPort(eventPort), filter(filter), observer(eventPort, fd, UnixEventPort::FdObserver::OBSERVE_READ) {} Promise> accept() override { int newFd; struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); retry: #if __linux__ && !__BIONIC__ newFd = ::accept4(fd, reinterpret_cast(&addr), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); #else newFd = ::accept(fd, reinterpret_cast(&addr), &addrlen); #endif if (newFd >= 0) { if (!filter.shouldAllow(reinterpret_cast(&addr), addrlen)) { // Drop disallowed address. close(newFd); return accept(); } else { return Own(heap(eventPort, newFd, NEW_FD_FLAGS)); } } else { int error = errno; switch (error) { case EAGAIN: #if EAGAIN != EWOULDBLOCK case EWOULDBLOCK: #endif // Not ready yet. return observer.whenBecomesReadable().then([this]() { return accept(); }); case EINTR: case ENETDOWN: #ifdef EPROTO // EPROTO is not defined on OpenBSD. case EPROTO: #endif case EHOSTDOWN: case EHOSTUNREACH: case ENETUNREACH: case ECONNABORTED: case ETIMEDOUT: // According to the Linux man page, accept() may report an error if the accepted // connection is already broken. In this case, we really ought to just ignore it and // keep waiting. But it's hard to say exactly what errors are such network errors and // which ones are permanent errors. We've made a guess here. goto retry; default: KJ_FAIL_SYSCALL("accept", error); } } } uint getPort() override { return SocketAddress::getLocalAddress(fd).getPort(); } void getsockopt(int level, int option, void* value, uint* length) override { socklen_t socklen = *length; KJ_SYSCALL(::getsockopt(fd, level, option, value, &socklen)); *length = socklen; } void setsockopt(int level, int option, const void* value, uint length) override { KJ_SYSCALL(::setsockopt(fd, level, option, value, length)); } void getsockname(struct sockaddr* addr, uint* length) override { socklen_t socklen = *length; KJ_SYSCALL(::getsockname(fd, addr, &socklen)); *length = socklen; } public: UnixEventPort& eventPort; LowLevelAsyncIoProvider::NetworkFilter& filter; UnixEventPort::FdObserver observer; }; class DatagramPortImpl final: public DatagramPort, public OwnedFileDescriptor { public: DatagramPortImpl(LowLevelAsyncIoProvider& lowLevel, UnixEventPort& eventPort, int fd, LowLevelAsyncIoProvider::NetworkFilter& filter, uint flags) : OwnedFileDescriptor(fd, flags), lowLevel(lowLevel), eventPort(eventPort), filter(filter), observer(eventPort, fd, UnixEventPort::FdObserver::OBSERVE_READ | UnixEventPort::FdObserver::OBSERVE_WRITE) {} Promise send(const void* buffer, size_t size, NetworkAddress& destination) override; Promise send( ArrayPtr> pieces, NetworkAddress& destination) override; class ReceiverImpl; Own makeReceiver(DatagramReceiver::Capacity capacity) override; uint getPort() override { return SocketAddress::getLocalAddress(fd).getPort(); } void getsockopt(int level, int option, void* value, uint* length) override { socklen_t socklen = *length; KJ_SYSCALL(::getsockopt(fd, level, option, value, &socklen)); *length = socklen; } void setsockopt(int level, int option, const void* value, uint length) override { KJ_SYSCALL(::setsockopt(fd, level, option, value, length)); } public: LowLevelAsyncIoProvider& lowLevel; UnixEventPort& eventPort; LowLevelAsyncIoProvider::NetworkFilter& filter; UnixEventPort::FdObserver observer; }; class LowLevelAsyncIoProviderImpl final: public LowLevelAsyncIoProvider { public: LowLevelAsyncIoProviderImpl() : eventLoop(eventPort), waitScope(eventLoop) {} inline WaitScope& getWaitScope() { return waitScope; } Own wrapInputFd(int fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Own wrapOutputFd(int fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Own wrapSocketFd(int fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Own wrapUnixSocketFd(Fd fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Promise> wrapConnectingSocketFd( int fd, const struct sockaddr* addr, uint addrlen, uint flags = 0) override { // It's important that we construct the AsyncStreamFd first, so that `flags` are honored, // especially setting nonblocking mode and taking ownership. auto result = heap(eventPort, fd, flags); // Unfortunately connect() doesn't fit the mold of KJ_NONBLOCKING_SYSCALL, since it indicates // non-blocking using EINPROGRESS. for (;;) { if (::connect(fd, addr, addrlen) < 0) { int error = errno; if (error == EINPROGRESS) { // Fine. break; } else if (error != EINTR) { KJ_FAIL_SYSCALL("connect()", error) { break; } return Own(); } } else { // no error break; } } auto connected = result->waitConnected(); return connected.then(kj::mvCapture(result, [fd](Own&& stream) { int err; socklen_t errlen = sizeof(err); KJ_SYSCALL(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen)); if (err != 0) { KJ_FAIL_SYSCALL("connect()", err) { break; } } return kj::mv(stream); })); } Own wrapListenSocketFd( int fd, NetworkFilter& filter, uint flags = 0) override { return heap(eventPort, fd, filter, flags); } Own wrapDatagramSocketFd( int fd, NetworkFilter& filter, uint flags = 0) override { return heap(*this, eventPort, fd, filter, flags); } Timer& getTimer() override { return eventPort.getTimer(); } UnixEventPort& getEventPort() { return eventPort; } private: UnixEventPort eventPort; EventLoop eventLoop; WaitScope waitScope; }; // ======================================================================================= class NetworkAddressImpl final: public NetworkAddress { public: NetworkAddressImpl(LowLevelAsyncIoProvider& lowLevel, LowLevelAsyncIoProvider::NetworkFilter& filter, Array addrs) : lowLevel(lowLevel), filter(filter), addrs(kj::mv(addrs)) {} Promise> connect() override { auto addrsCopy = heapArray(addrs.asPtr()); auto promise = connectImpl(lowLevel, filter, addrsCopy); return promise.attach(kj::mv(addrsCopy)); } Own listen() override { if (addrs.size() > 1) { KJ_LOG(WARNING, "Bind address resolved to multiple addresses. Only the first address will " "be used. If this is incorrect, specify the address numerically. This may be fixed " "in the future.", addrs[0].toString()); } int fd = addrs[0].socket(SOCK_STREAM); { KJ_ON_SCOPE_FAILURE(close(fd)); // We always enable SO_REUSEADDR because having to take your server down for five minutes // before it can restart really sucks. int optval = 1; KJ_SYSCALL(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); addrs[0].bind(fd); // TODO(someday): Let queue size be specified explicitly in string addresses. KJ_SYSCALL(::listen(fd, SOMAXCONN)); } return lowLevel.wrapListenSocketFd(fd, filter, NEW_FD_FLAGS); } Own bindDatagramPort() override { if (addrs.size() > 1) { KJ_LOG(WARNING, "Bind address resolved to multiple addresses. Only the first address will " "be used. If this is incorrect, specify the address numerically. This may be fixed " "in the future.", addrs[0].toString()); } int fd = addrs[0].socket(SOCK_DGRAM); { KJ_ON_SCOPE_FAILURE(close(fd)); // We always enable SO_REUSEADDR because having to take your server down for five minutes // before it can restart really sucks. int optval = 1; KJ_SYSCALL(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); addrs[0].bind(fd); } return lowLevel.wrapDatagramSocketFd(fd, filter, NEW_FD_FLAGS); } Own clone() override { return kj::heap(lowLevel, filter, kj::heapArray(addrs.asPtr())); } String toString() override { return strArray(KJ_MAP(addr, addrs) { return addr.toString(); }, ","); } const SocketAddress& chooseOneAddress() { KJ_REQUIRE(addrs.size() > 0, "No addresses available."); return addrs[counter++ % addrs.size()]; } private: LowLevelAsyncIoProvider& lowLevel; LowLevelAsyncIoProvider::NetworkFilter& filter; Array addrs; uint counter = 0; static Promise> connectImpl( LowLevelAsyncIoProvider& lowLevel, LowLevelAsyncIoProvider::NetworkFilter& filter, ArrayPtr addrs) { KJ_ASSERT(addrs.size() > 0); return kj::evalNow([&]() -> Promise> { if (!addrs[0].allowedBy(filter)) { return KJ_EXCEPTION(FAILED, "connect() blocked by restrictPeers()"); } else { int fd = addrs[0].socket(SOCK_STREAM); return lowLevel.wrapConnectingSocketFd( fd, addrs[0].getRaw(), addrs[0].getRawSize(), NEW_FD_FLAGS); } }).then([](Own&& stream) -> Promise> { // Success, pass along. return kj::mv(stream); }, [&lowLevel,&filter,addrs](Exception&& exception) mutable -> Promise> { // Connect failed. if (addrs.size() > 1) { // Try the next address instead. return connectImpl(lowLevel, filter, addrs.slice(1, addrs.size())); } else { // No more addresses to try, so propagate the exception. return kj::mv(exception); } }); } }; class SocketNetwork final: public Network { public: explicit SocketNetwork(LowLevelAsyncIoProvider& lowLevel): lowLevel(lowLevel) {} explicit SocketNetwork(SocketNetwork& parent, kj::ArrayPtr allow, kj::ArrayPtr deny) : lowLevel(parent.lowLevel), filter(allow, deny, parent.filter) {} Promise> parseAddress(StringPtr addr, uint portHint = 0) override { return evalLater(mvCapture(heapString(addr), [this,portHint](String&& addr) { return SocketAddress::parse(lowLevel, addr, portHint, filter); })).then([this](Array addresses) -> Own { return heap(lowLevel, filter, kj::mv(addresses)); }); } Own getSockaddr(const void* sockaddr, uint len) override { auto array = kj::heapArrayBuilder(1); array.add(SocketAddress(sockaddr, len)); KJ_REQUIRE(array[0].allowedBy(filter), "address blocked by restrictPeers()") { break; } return Own(heap(lowLevel, filter, array.finish())); } Own restrictPeers( kj::ArrayPtr allow, kj::ArrayPtr deny = nullptr) override { return heap(*this, allow, deny); } private: LowLevelAsyncIoProvider& lowLevel; _::NetworkFilter filter; }; // ======================================================================================= Promise DatagramPortImpl::send( const void* buffer, size_t size, NetworkAddress& destination) { auto& addr = downcast(destination).chooseOneAddress(); ssize_t n; KJ_NONBLOCKING_SYSCALL(n = sendto(fd, buffer, size, 0, addr.getRaw(), addr.getRawSize())); if (n < 0) { // Write buffer full. return observer.whenBecomesWritable().then([this, buffer, size, &destination]() { return send(buffer, size, destination); }); } else { // If less than the whole message was sent, then it got truncated, and there's nothing we can // do about it. return n; } } Promise DatagramPortImpl::send( ArrayPtr> pieces, NetworkAddress& destination) { struct msghdr msg; memset(&msg, 0, sizeof(msg)); auto& addr = downcast(destination).chooseOneAddress(); msg.msg_name = const_cast(implicitCast(addr.getRaw())); msg.msg_namelen = addr.getRawSize(); const size_t iovmax = kj::miniposix::iovMax(pieces.size()); KJ_STACK_ARRAY(struct iovec, iov, kj::min(pieces.size(), iovmax), 16, 64); for (size_t i: kj::indices(pieces)) { iov[i].iov_base = const_cast(implicitCast(pieces[i].begin())); iov[i].iov_len = pieces[i].size(); } Array extra; if (pieces.size() > iovmax) { // Too many pieces, but we can't use multiple syscalls because they'd send separate // datagrams. We'll have to copy the trailing pieces into a temporary array. // // TODO(perf): On Linux we could use multiple syscalls via MSG_MORE. size_t extraSize = 0; for (size_t i = iovmax - 1; i < pieces.size(); i++) { extraSize += pieces[i].size(); } extra = kj::heapArray(extraSize); extraSize = 0; for (size_t i = iovmax - 1; i < pieces.size(); i++) { memcpy(extra.begin() + extraSize, pieces[i].begin(), pieces[i].size()); extraSize += pieces[i].size(); } iov[iovmax - 1].iov_base = extra.begin(); iov[iovmax - 1].iov_len = extra.size(); } msg.msg_iov = iov.begin(); msg.msg_iovlen = iov.size(); ssize_t n; KJ_NONBLOCKING_SYSCALL(n = sendmsg(fd, &msg, 0)); if (n < 0) { // Write buffer full. return observer.whenBecomesWritable().then([this, pieces, &destination]() { return send(pieces, destination); }); } else { // If less than the whole message was sent, then it was truncated, and there's nothing we can // do about that now. return n; } } class DatagramPortImpl::ReceiverImpl final: public DatagramReceiver { public: explicit ReceiverImpl(DatagramPortImpl& port, Capacity capacity) : port(port), contentBuffer(heapArray(capacity.content)), ancillaryBuffer(capacity.ancillary > 0 ? heapArray(capacity.ancillary) : Array(nullptr)) {} Promise receive() override { struct msghdr msg; memset(&msg, 0, sizeof(msg)); struct sockaddr_storage addr; memset(&addr, 0, sizeof(addr)); msg.msg_name = &addr; msg.msg_namelen = sizeof(addr); struct iovec iov; iov.iov_base = contentBuffer.begin(); iov.iov_len = contentBuffer.size(); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = ancillaryBuffer.begin(); msg.msg_controllen = ancillaryBuffer.size(); ssize_t n; KJ_NONBLOCKING_SYSCALL(n = recvmsg(port.fd, &msg, 0)); if (n < 0) { // No data available. Wait. return port.observer.whenBecomesReadable().then([this]() { return receive(); }); } else { if (!port.filter.shouldAllow(reinterpret_cast(msg.msg_name), msg.msg_namelen)) { // Ignore message from disallowed source. return receive(); } receivedSize = n; contentTruncated = msg.msg_flags & MSG_TRUNC; source.emplace(port.lowLevel, port.filter, msg.msg_name, msg.msg_namelen); ancillaryList.resize(0); ancillaryTruncated = msg.msg_flags & MSG_CTRUNC; for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) { // On some platforms (OSX), a cmsghdr's length may cross the end of the ancillary buffer // when truncated. On other platforms (Linux) the length in cmsghdr will itself be // truncated to fit within the buffer. #if __APPLE__ // On MacOS, `CMSG_SPACE(0)` triggers a bogus warning. #pragma GCC diagnostic ignored "-Wnull-pointer-arithmetic" #endif const byte* pos = reinterpret_cast(cmsg); size_t available = ancillaryBuffer.end() - pos; if (available < CMSG_SPACE(0)) { // The buffer ends in the middle of the header. We can't use this message. // (On Linux, this never happens, because the message is not included if there isn't // space for a header. I'm not sure how other systems behave, though, so let's be safe.) break; } // OK, we know the cmsghdr is valid, at least. // Find the start of the message payload. const byte* begin = (const byte *)CMSG_DATA(cmsg); // Cap the message length to the available space. const byte* end = pos + kj::min(available, cmsg->cmsg_len); ancillaryList.add(AncillaryMessage( cmsg->cmsg_level, cmsg->cmsg_type, arrayPtr(begin, end))); } return READY_NOW; } } MaybeTruncated> getContent() override { return { contentBuffer.slice(0, receivedSize), contentTruncated }; } MaybeTruncated> getAncillary() override { return { ancillaryList.asPtr(), ancillaryTruncated }; } NetworkAddress& getSource() override { return KJ_REQUIRE_NONNULL(source, "Haven't sent a message yet.").abstract; } private: DatagramPortImpl& port; Array contentBuffer; Array ancillaryBuffer; Vector ancillaryList; size_t receivedSize = 0; bool contentTruncated = false; bool ancillaryTruncated = false; struct StoredAddress { StoredAddress(LowLevelAsyncIoProvider& lowLevel, LowLevelAsyncIoProvider::NetworkFilter& filter, const void* sockaddr, uint length) : raw(sockaddr, length), abstract(lowLevel, filter, Array(&raw, 1, NullArrayDisposer::instance)) {} SocketAddress raw; NetworkAddressImpl abstract; }; kj::Maybe source; }; Own DatagramPortImpl::makeReceiver(DatagramReceiver::Capacity capacity) { return kj::heap(*this, capacity); } // ======================================================================================= class AsyncIoProviderImpl final: public AsyncIoProvider { public: AsyncIoProviderImpl(LowLevelAsyncIoProvider& lowLevel) : lowLevel(lowLevel), network(lowLevel) {} OneWayPipe newOneWayPipe() override { int fds[2]; #if __linux__ && !__BIONIC__ KJ_SYSCALL(pipe2(fds, O_NONBLOCK | O_CLOEXEC)); #else KJ_SYSCALL(pipe(fds)); #endif return OneWayPipe { lowLevel.wrapInputFd(fds[0], NEW_FD_FLAGS), lowLevel.wrapOutputFd(fds[1], NEW_FD_FLAGS) }; } TwoWayPipe newTwoWayPipe() override { int fds[2]; int type = SOCK_STREAM; #if __linux__ && !__BIONIC__ type |= SOCK_NONBLOCK | SOCK_CLOEXEC; #endif KJ_SYSCALL(socketpair(AF_UNIX, type, 0, fds)); return TwoWayPipe { { lowLevel.wrapSocketFd(fds[0], NEW_FD_FLAGS), lowLevel.wrapSocketFd(fds[1], NEW_FD_FLAGS) } }; } CapabilityPipe newCapabilityPipe() override { int fds[2]; int type = SOCK_STREAM; #if __linux__ && !__BIONIC__ type |= SOCK_NONBLOCK | SOCK_CLOEXEC; #endif KJ_SYSCALL(socketpair(AF_UNIX, type, 0, fds)); return CapabilityPipe { { lowLevel.wrapUnixSocketFd(fds[0], NEW_FD_FLAGS), lowLevel.wrapUnixSocketFd(fds[1], NEW_FD_FLAGS) } }; } Network& getNetwork() override { return network; } PipeThread newPipeThread( Function startFunc) override { int fds[2]; int type = SOCK_STREAM; #if __linux__ && !__BIONIC__ type |= SOCK_NONBLOCK | SOCK_CLOEXEC; #endif KJ_SYSCALL(socketpair(AF_UNIX, type, 0, fds)); int threadFd = fds[1]; KJ_ON_SCOPE_FAILURE(close(threadFd)); auto pipe = lowLevel.wrapSocketFd(fds[0], NEW_FD_FLAGS); auto thread = heap(kj::mvCapture(startFunc, [threadFd](Function&& startFunc) { LowLevelAsyncIoProviderImpl lowLevel; auto stream = lowLevel.wrapSocketFd(threadFd, NEW_FD_FLAGS); AsyncIoProviderImpl ioProvider(lowLevel); startFunc(ioProvider, *stream, lowLevel.getWaitScope()); })); return { kj::mv(thread), kj::mv(pipe) }; } Timer& getTimer() override { return lowLevel.getTimer(); } private: LowLevelAsyncIoProvider& lowLevel; SocketNetwork network; }; } // namespace Own newAsyncIoProvider(LowLevelAsyncIoProvider& lowLevel) { return kj::heap(lowLevel); } AsyncIoContext setupAsyncIo() { auto lowLevel = heap(); auto ioProvider = kj::heap(*lowLevel); auto& waitScope = lowLevel->getWaitScope(); auto& eventPort = lowLevel->getEventPort(); return { kj::mv(lowLevel), kj::mv(ioProvider), waitScope, eventPort }; } } // namespace kj #endif // !_WIN32 capnproto-c++-0.8.0/src/kj/async-io-win32.c++0000644000175000017500000012506513650101756021134 0ustar00kentonkenton00000000000000// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 // For Unix implementation, see async-io-unix.c++. // Request Vista-level APIs. #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #include "async-io.h" #include "async-io-internal.h" #include "async-win32.h" #include "debug.h" #include "thread.h" #include "io.h" #include "vector.h" #include #include #include #include #include #include #ifndef IPV6_V6ONLY // MinGW's headers are missing this. #define IPV6_V6ONLY 27 #endif namespace kj { namespace _ { // private struct WinsockInitializer { WinsockInitializer() { WSADATA dontcare; int result = WSAStartup(MAKEWORD(2, 2), &dontcare); if (result != 0) { KJ_FAIL_WIN32("WSAStartup()", result); } } }; void initWinsockOnce() { static WinsockInitializer initializer; } int win32Socketpair(SOCKET socks[2]) { // This function from: https://github.com/ncm/selectable-socketpair/blob/master/socketpair.c // // Copyright notice: // // Copyright 2007, 2010 by Nathan C. Myers // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // The name of the author must not be used to endorse or promote products // derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Note: This function is called from some Cap'n Proto unit tests, despite not having a public // header declaration. // TODO(cleanup): Consider putting this somewhere public? Note that since it depends on Winsock, // it needs to be in the kj-async library. initWinsockOnce(); union { struct sockaddr_in inaddr; struct sockaddr addr; } a; SOCKET listener; int e; socklen_t addrlen = sizeof(a.inaddr); int reuse = 1; if (socks == 0) { WSASetLastError(WSAEINVAL); return SOCKET_ERROR; } socks[0] = socks[1] = -1; listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listener == -1) return SOCKET_ERROR; memset(&a, 0, sizeof(a)); a.inaddr.sin_family = AF_INET; a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); a.inaddr.sin_port = 0; for (;;) { if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse, (socklen_t) sizeof(reuse)) == -1) break; if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR) break; memset(&a, 0, sizeof(a)); if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR) break; // win32 getsockname may only set the port number, p=0.0005. // ( http://msdn.microsoft.com/library/ms738543.aspx ): a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); a.inaddr.sin_family = AF_INET; if (listen(listener, 1) == SOCKET_ERROR) break; socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); if (socks[0] == -1) break; if (connect(socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR) break; retryAccept: socks[1] = accept(listener, NULL, NULL); if (socks[1] == -1) break; // Verify that the client is actually us and not someone else who raced to connect first. // (This check added by Kenton for security.) union { struct sockaddr_in inaddr; struct sockaddr addr; } b, c; socklen_t bAddrlen = sizeof(b.inaddr); socklen_t cAddrlen = sizeof(b.inaddr); if (getpeername(socks[1], &b.addr, &bAddrlen) == SOCKET_ERROR) break; if (getsockname(socks[0], &c.addr, &cAddrlen) == SOCKET_ERROR) break; if (bAddrlen != cAddrlen || memcmp(&b.addr, &c.addr, bAddrlen) != 0) { // Someone raced to connect first. Ignore. closesocket(socks[1]); goto retryAccept; } closesocket(listener); return 0; } e = WSAGetLastError(); closesocket(listener); closesocket(socks[0]); closesocket(socks[1]); WSASetLastError(e); socks[0] = socks[1] = -1; return SOCKET_ERROR; } } // namespace _ namespace { // ======================================================================================= static constexpr uint NEW_FD_FLAGS = LowLevelAsyncIoProvider::TAKE_OWNERSHIP; class OwnedFd { public: OwnedFd(SOCKET fd, uint flags): fd(fd), flags(flags) { // TODO(perf): Maybe use SetFileCompletionNotificationModes() to tell Windows not to bother // delivering an event when the operation completes inline. Not currently implemented on // Wine, though. } ~OwnedFd() noexcept(false) { if (flags & LowLevelAsyncIoProvider::TAKE_OWNERSHIP) { KJ_WINSOCK(closesocket(fd)) { break; } } } protected: SOCKET fd; private: uint flags; }; // ======================================================================================= class AsyncStreamFd: public OwnedFd, public AsyncIoStream { public: AsyncStreamFd(Win32EventPort& eventPort, SOCKET fd, uint flags) : OwnedFd(fd, flags), observer(eventPort.observeIo(reinterpret_cast(fd))) {} virtual ~AsyncStreamFd() noexcept(false) {} Promise read(void* buffer, size_t minBytes, size_t maxBytes) override { return tryRead(buffer, minBytes, maxBytes).then([=](size_t result) { KJ_REQUIRE(result >= minBytes, "Premature EOF") { // Pretend we read zeros from the input. memset(reinterpret_cast(buffer) + result, 0, minBytes - result); return minBytes; } return result; }); } Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { auto bufs = heapArray(1); bufs[0].buf = reinterpret_cast(buffer); bufs[0].len = maxBytes; ArrayPtr ref = bufs; return tryReadInternal(ref, minBytes, 0).attach(kj::mv(bufs)); } Promise write(const void* buffer, size_t size) override { auto bufs = heapArray(1); bufs[0].buf = const_cast(reinterpret_cast(buffer)); bufs[0].len = size; ArrayPtr ref = bufs; return writeInternal(ref).attach(kj::mv(bufs)); } Promise write(ArrayPtr> pieces) override { auto bufs = heapArray(pieces.size()); for (auto i: kj::indices(pieces)) { bufs[i].buf = const_cast(pieces[i].asChars().begin()); bufs[i].len = pieces[i].size(); } ArrayPtr ref = bufs; return writeInternal(ref).attach(kj::mv(bufs)); } kj::Promise connect(const struct sockaddr* addr, uint addrlen) { // In order to connect asynchronously, we need the ConnectEx() function. Apparently, we have // to query the socket for it dynamically, I guess because of the insanity in which winsock // can be implemented in userspace and old implementations may not support it. GUID guid = WSAID_CONNECTEX; LPFN_CONNECTEX connectEx = nullptr; DWORD n = 0; KJ_WINSOCK(WSAIoctl(fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), &connectEx, sizeof(connectEx), &n, NULL, NULL)) { goto fail; // avoid memory leak due to compiler bugs } if (false) { fail: return kj::READY_NOW; } // OK, phew, we now have our ConnectEx function pointer. Call it. auto op = observer->newOperation(0); if (!connectEx(fd, addr, addrlen, NULL, 0, NULL, op->getOverlapped())) { DWORD error = WSAGetLastError(); if (error != ERROR_IO_PENDING) { KJ_FAIL_WIN32("ConnectEx()", error) { break; } return kj::READY_NOW; } } return op->onComplete().then([this](Win32EventPort::IoResult result) { if (result.errorCode != ERROR_SUCCESS) { KJ_FAIL_WIN32("ConnectEx()", result.errorCode) { return; } } // Enable shutdown() to work. setsockopt(SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0); }); } Promise whenWriteDisconnected() override { // Windows IOCP does not provide a direct, documented way to detect when the socket disconnects // without actually doing a read or write. However, there is an undocoumented-but-stable // ioctl called IOCTL_AFD_POLL which can be used for this purpose. In fact, select() is // implemented in terms of this ioctl -- performed synchronously -- but it's entirely possible // to put only one socket into the list and perform the ioctl asynchronously. Here's the // source code for select() in Windows 2000 (not sure how this became public...): // // https://github.com/pustladi/Windows-2000/blob/661d000d50637ed6fab2329d30e31775046588a9/private/net/sockets/winsock2/wsp/msafd/select.c#L59-L655 // // And here's an interesting discussion: https://github.com/python-trio/trio/issues/52 // // TODO(someday): Implement this with IOCTL_AFD_POLL. For now I'm leaving it unimplemented // because I added this method for a Linux-only use case. return NEVER_DONE; } void shutdownWrite() override { // There's no legitimate way to get an AsyncStreamFd that isn't a socket through the // Win32AsyncIoProvider interface. KJ_WINSOCK(shutdown(fd, SD_SEND)); } void abortRead() override { // There's no legitimate way to get an AsyncStreamFd that isn't a socket through the // Win32AsyncIoProvider interface. KJ_WINSOCK(shutdown(fd, SD_RECEIVE)); } void getsockopt(int level, int option, void* value, uint* length) override { socklen_t socklen = *length; KJ_WINSOCK(::getsockopt(fd, level, option, reinterpret_cast(value), &socklen)); *length = socklen; } void setsockopt(int level, int option, const void* value, uint length) override { KJ_WINSOCK(::setsockopt(fd, level, option, reinterpret_cast(value), length)); } void getsockname(struct sockaddr* addr, uint* length) override { socklen_t socklen = *length; KJ_WINSOCK(::getsockname(fd, addr, &socklen)); *length = socklen; } void getpeername(struct sockaddr* addr, uint* length) override { socklen_t socklen = *length; KJ_WINSOCK(::getpeername(fd, addr, &socklen)); *length = socklen; } private: Own observer; Promise tryReadInternal(ArrayPtr bufs, size_t minBytes, size_t alreadyRead) { // `bufs` will remain valid until the promise completes and may be freely modified. // // `alreadyRead` is the number of bytes we have already received via previous reads -- minBytes // and buffer have already been adjusted to account for them, but this count must be included // in the final return value. auto op = observer->newOperation(0); DWORD flags = 0; if (WSARecv(fd, bufs.begin(), bufs.size(), NULL, &flags, op->getOverlapped(), NULL) == SOCKET_ERROR) { DWORD error = WSAGetLastError(); if (error != WSA_IO_PENDING) { KJ_FAIL_WIN32("WSARecv()", error) { break; } return alreadyRead; } } return op->onComplete() .then([this,KJ_CPCAP(bufs),minBytes,alreadyRead](Win32IocpEventPort::IoResult result) mutable -> Promise { if (result.errorCode != ERROR_SUCCESS) { if (alreadyRead > 0) { // Report what we already read. return alreadyRead; } else { KJ_FAIL_WIN32("WSARecv()", result.errorCode) { break; } return size_t(0); } } if (result.bytesTransferred == 0) { return alreadyRead; } alreadyRead += result.bytesTransferred; if (result.bytesTransferred >= minBytes) { // We can stop here. return alreadyRead; } minBytes -= result.bytesTransferred; while (result.bytesTransferred >= bufs[0].len) { result.bytesTransferred -= bufs[0].len; bufs = bufs.slice(1, bufs.size()); } if (result.bytesTransferred > 0) { bufs[0].buf += result.bytesTransferred; bufs[0].len -= result.bytesTransferred; } return tryReadInternal(bufs, minBytes, alreadyRead); }).attach(kj::mv(bufs)); } Promise writeInternal(ArrayPtr bufs) { // `bufs` will remain valid until the promise completes and may be freely modified. auto op = observer->newOperation(0); if (WSASend(fd, bufs.begin(), bufs.size(), NULL, 0, op->getOverlapped(), NULL) == SOCKET_ERROR) { DWORD error = WSAGetLastError(); if (error != WSA_IO_PENDING) { KJ_FAIL_WIN32("WSASend()", error) { break; } return kj::READY_NOW; } } return op->onComplete() .then([this,KJ_CPCAP(bufs)](Win32IocpEventPort::IoResult result) mutable -> Promise { if (result.errorCode != ERROR_SUCCESS) { KJ_FAIL_WIN32("WSASend()", result.errorCode) { break; } return kj::READY_NOW; } while (bufs.size() > 0 && result.bytesTransferred >= bufs[0].len) { result.bytesTransferred -= bufs[0].len; bufs = bufs.slice(1, bufs.size()); } if (result.bytesTransferred > 0) { bufs[0].buf += result.bytesTransferred; bufs[0].len -= result.bytesTransferred; } if (bufs.size() > 0) { return writeInternal(bufs); } else { return kj::READY_NOW; } }).attach(kj::mv(bufs)); } }; // ======================================================================================= class SocketAddress { public: SocketAddress(const void* sockaddr, uint len): addrlen(len) { KJ_REQUIRE(len <= sizeof(addr), "Sorry, your sockaddr is too big for me."); memcpy(&addr.generic, sockaddr, len); } bool operator<(const SocketAddress& other) const { // So we can use std::set... see DNS lookup code. if (wildcard < other.wildcard) return true; if (wildcard > other.wildcard) return false; if (addrlen < other.addrlen) return true; if (addrlen > other.addrlen) return false; return memcmp(&addr.generic, &other.addr.generic, addrlen) < 0; } const struct sockaddr* getRaw() const { return &addr.generic; } int getRawSize() const { return addrlen; } SOCKET socket(int type) const { bool isStream = type == SOCK_STREAM; SOCKET result = ::socket(addr.generic.sa_family, type, 0); if (result == INVALID_SOCKET) { KJ_FAIL_WIN32("WSASocket()", WSAGetLastError()) { return INVALID_SOCKET; } } if (isStream && (addr.generic.sa_family == AF_INET || addr.generic.sa_family == AF_INET6)) { // TODO(perf): As a hack for the 0.4 release we are always setting // TCP_NODELAY because Nagle's algorithm pretty much kills Cap'n Proto's // RPC protocol. Later, we should extend the interface to provide more // control over this. Perhaps write() should have a flag which // specifies whether to pass MSG_MORE. BOOL one = TRUE; KJ_WINSOCK(setsockopt(result, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one))); } return result; } void bind(SOCKET sockfd) const { if (wildcard) { // Disable IPV6_V6ONLY because we want to handle both ipv4 and ipv6 on this socket. (The // default value of this option varies across platforms.) DWORD value = 0; KJ_WINSOCK(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&value), sizeof(value))); } KJ_WINSOCK(::bind(sockfd, &addr.generic, addrlen), toString()); } uint getPort() const { switch (addr.generic.sa_family) { case AF_INET: return ntohs(addr.inet4.sin_port); case AF_INET6: return ntohs(addr.inet6.sin6_port); default: return 0; } } String toString() const { if (wildcard) { return str("*:", getPort()); } switch (addr.generic.sa_family) { case AF_INET: { char buffer[16]; if (InetNtopA(addr.inet4.sin_family, const_cast(&addr.inet4.sin_addr), buffer, sizeof(buffer)) == nullptr) { KJ_FAIL_WIN32("InetNtop", WSAGetLastError()) { break; } return heapString("(inet_ntop error)"); } return str(buffer, ':', ntohs(addr.inet4.sin_port)); } case AF_INET6: { char buffer[46]; if (InetNtopA(addr.inet6.sin6_family, const_cast(&addr.inet6.sin6_addr), buffer, sizeof(buffer)) == nullptr) { KJ_FAIL_WIN32("InetNtop", WSAGetLastError()) { break; } return heapString("(inet_ntop error)"); } return str('[', buffer, "]:", ntohs(addr.inet6.sin6_port)); } default: return str("(unknown address family ", addr.generic.sa_family, ")"); } } static Promise> lookupHost( LowLevelAsyncIoProvider& lowLevel, kj::String host, kj::String service, uint portHint, _::NetworkFilter& filter); // Perform a DNS lookup. static Promise> parse( LowLevelAsyncIoProvider& lowLevel, StringPtr str, uint portHint, _::NetworkFilter& filter) { // TODO(someday): Allow commas in `str`. SocketAddress result; // Try to separate the address and port. ArrayPtr addrPart; Maybe portPart; int af; if (str.startsWith("[")) { // Address starts with a bracket, which is a common way to write an ip6 address with a port, // since without brackets around the address part, the port looks like another segment of // the address. af = AF_INET6; size_t closeBracket = KJ_ASSERT_NONNULL(str.findLast(']'), "Unclosed '[' in address string.", str); addrPart = str.slice(1, closeBracket); if (str.size() > closeBracket + 1) { KJ_REQUIRE(str.slice(closeBracket + 1).startsWith(":"), "Expected port suffix after ']'.", str); portPart = str.slice(closeBracket + 2); } } else { KJ_IF_MAYBE(colon, str.findFirst(':')) { if (str.slice(*colon + 1).findFirst(':') == nullptr) { // There is exactly one colon and no brackets, so it must be an ip4 address with port. af = AF_INET; addrPart = str.slice(0, *colon); portPart = str.slice(*colon + 1); } else { // There are two or more colons and no brackets, so the whole thing must be an ip6 // address with no port. af = AF_INET6; addrPart = str; } } else { // No colons, so it must be an ip4 address without port. af = AF_INET; addrPart = str; } } // Parse the port. unsigned long port; KJ_IF_MAYBE(portText, portPart) { char* endptr; port = strtoul(portText->cStr(), &endptr, 0); if (portText->size() == 0 || *endptr != '\0') { // Not a number. Maybe it's a service name. Fall back to DNS. return lookupHost(lowLevel, kj::heapString(addrPart), kj::heapString(*portText), portHint, filter); } KJ_REQUIRE(port < 65536, "Port number too large."); } else { port = portHint; } // Check for wildcard. if (addrPart.size() == 1 && addrPart[0] == '*') { result.wildcard = true; // Create an ip6 socket and set IPV6_V6ONLY to 0 later. result.addrlen = sizeof(addr.inet6); result.addr.inet6.sin6_family = AF_INET6; result.addr.inet6.sin6_port = htons(port); auto array = kj::heapArrayBuilder(1); array.add(result); return array.finish(); } void* addrTarget; if (af == AF_INET6) { result.addrlen = sizeof(addr.inet6); result.addr.inet6.sin6_family = AF_INET6; result.addr.inet6.sin6_port = htons(port); addrTarget = &result.addr.inet6.sin6_addr; } else { result.addrlen = sizeof(addr.inet4); result.addr.inet4.sin_family = AF_INET; result.addr.inet4.sin_port = htons(port); addrTarget = &result.addr.inet4.sin_addr; } char buffer[64]; if (addrPart.size() < sizeof(buffer) - 1) { // addrPart is not necessarily NUL-terminated so we have to make a copy. :( memcpy(buffer, addrPart.begin(), addrPart.size()); buffer[addrPart.size()] = '\0'; // OK, parse it! switch (InetPtonA(af, buffer, addrTarget)) { case 1: { // success. if (!result.parseAllowedBy(filter)) { KJ_FAIL_REQUIRE("address family blocked by restrictPeers()"); return Array(); } auto array = kj::heapArrayBuilder(1); array.add(result); return array.finish(); } case 0: // It's apparently not a simple address... fall back to DNS. break; default: KJ_FAIL_WIN32("InetPton", WSAGetLastError(), af, addrPart); } } return lookupHost(lowLevel, kj::heapString(addrPart), nullptr, port, filter); } static SocketAddress getLocalAddress(SOCKET sockfd) { SocketAddress result; result.addrlen = sizeof(addr); KJ_WINSOCK(getsockname(sockfd, &result.addr.generic, &result.addrlen)); return result; } static SocketAddress getPeerAddress(SOCKET sockfd) { SocketAddress result; result.addrlen = sizeof(addr); KJ_WINSOCK(getpeername(sockfd, &result.addr.generic, &result.addrlen)); return result; } bool allowedBy(LowLevelAsyncIoProvider::NetworkFilter& filter) { return filter.shouldAllow(&addr.generic, addrlen); } bool parseAllowedBy(_::NetworkFilter& filter) { return filter.shouldAllowParse(&addr.generic, addrlen); } static SocketAddress getWildcardForFamily(int family) { SocketAddress result; switch (family) { case AF_INET: result.addrlen = sizeof(addr.inet4); result.addr.inet4.sin_family = AF_INET; return result; case AF_INET6: result.addrlen = sizeof(addr.inet6); result.addr.inet6.sin6_family = AF_INET6; return result; default: KJ_FAIL_REQUIRE("unknown address family", family); } } private: SocketAddress(): addrlen(0) { memset(&addr, 0, sizeof(addr)); } socklen_t addrlen; bool wildcard = false; union { struct sockaddr generic; struct sockaddr_in inet4; struct sockaddr_in6 inet6; struct sockaddr_storage storage; } addr; struct LookupParams; class LookupReader; }; class SocketAddress::LookupReader { // Reads SocketAddresses off of a pipe coming from another thread that is performing // getaddrinfo. public: LookupReader(kj::Own&& thread, kj::Own&& input, _::NetworkFilter& filter) : thread(kj::mv(thread)), input(kj::mv(input)), filter(filter) {} ~LookupReader() { if (thread) thread->detach(); } Promise> read() { return input->tryRead(¤t, sizeof(current), sizeof(current)).then( [this](size_t n) -> Promise> { if (n < sizeof(current)) { thread = nullptr; // getaddrinfo()'s docs seem to say it will never return an empty list, but let's check // anyway. KJ_REQUIRE(addresses.size() > 0, "DNS lookup returned no permitted addresses.") { break; } return addresses.releaseAsArray(); } else { // getaddrinfo() can return multiple copies of the same address for several reasons. // A major one is that we don't give it a socket type (SOCK_STREAM vs. SOCK_DGRAM), so // it may return two copies of the same address, one for each type, unless it explicitly // knows that the service name given is specific to one type. But we can't tell it a type, // because we don't actually know which one the user wants, and if we specify SOCK_STREAM // while the user specified a UDP service name then they'll get a resolution error which // is lame. (At least, I think that's how it works.) // // So we instead resort to de-duping results. if (alreadySeen.insert(current).second) { if (current.parseAllowedBy(filter)) { addresses.add(current); } } return read(); } }); } private: kj::Own thread; kj::Own input; _::NetworkFilter& filter; SocketAddress current; kj::Vector addresses; std::set alreadySeen; }; struct SocketAddress::LookupParams { kj::String host; kj::String service; }; Promise> SocketAddress::lookupHost( LowLevelAsyncIoProvider& lowLevel, kj::String host, kj::String service, uint portHint, _::NetworkFilter& filter) { // This shitty function spawns a thread to run getaddrinfo(). Unfortunately, getaddrinfo() is // the only cross-platform DNS API and it is blocking. // // TODO(perf): Use GetAddrInfoEx(). But there are problems: // - Not implemented in Wine. // - Doesn't seem compatible with I/O completion ports, in particular because it's not associated // with a handle. Could signal completion as an APC instead, but that requires the IOCP code // to use GetQueuedCompletionStatusEx() which it doesn't right now because it's not available // in Wine. // - Requires Unicode, for some reason. Only GetAddrInfoExW() supports async, according to the // docs. Never mind that DNS itself is ASCII... SOCKET fds[2]; KJ_WINSOCK(_::win32Socketpair(fds)); auto input = lowLevel.wrapInputFd(fds[0], NEW_FD_FLAGS); int outFd = fds[1]; LookupParams params = { kj::mv(host), kj::mv(service) }; auto thread = heap(kj::mvCapture(params, [outFd,portHint](LookupParams&& params) { KJ_DEFER(closesocket(outFd)); struct addrinfo* list; int status = getaddrinfo( params.host == "*" ? nullptr : params.host.cStr(), params.service == nullptr ? nullptr : params.service.cStr(), nullptr, &list); if (status == 0) { KJ_DEFER(freeaddrinfo(list)); struct addrinfo* cur = list; while (cur != nullptr) { if (params.service == nullptr) { switch (cur->ai_addr->sa_family) { case AF_INET: ((struct sockaddr_in*)cur->ai_addr)->sin_port = htons(portHint); break; case AF_INET6: ((struct sockaddr_in6*)cur->ai_addr)->sin6_port = htons(portHint); break; default: break; } } SocketAddress addr; memset(&addr, 0, sizeof(addr)); // mollify valgrind if (params.host == "*") { // Set up a wildcard SocketAddress. Only use the port number returned by getaddrinfo(). addr.wildcard = true; addr.addrlen = sizeof(addr.addr.inet6); addr.addr.inet6.sin6_family = AF_INET6; switch (cur->ai_addr->sa_family) { case AF_INET: addr.addr.inet6.sin6_port = ((struct sockaddr_in*)cur->ai_addr)->sin_port; break; case AF_INET6: addr.addr.inet6.sin6_port = ((struct sockaddr_in6*)cur->ai_addr)->sin6_port; break; default: addr.addr.inet6.sin6_port = portHint; break; } } else { addr.addrlen = cur->ai_addrlen; memcpy(&addr.addr.generic, cur->ai_addr, cur->ai_addrlen); } KJ_ASSERT_CAN_MEMCPY(SocketAddress); const char* data = reinterpret_cast(&addr); size_t size = sizeof(addr); while (size > 0) { int n; KJ_WINSOCK(n = send(outFd, data, size, 0)); data += n; size -= n; } cur = cur->ai_next; } } else { KJ_FAIL_WIN32("getaddrinfo()", status, params.host, params.service) { return; } } })); auto reader = heap(kj::mv(thread), kj::mv(input), filter); return reader->read().attach(kj::mv(reader)); } // ======================================================================================= class FdConnectionReceiver final: public ConnectionReceiver, public OwnedFd { public: FdConnectionReceiver(Win32EventPort& eventPort, SOCKET fd, LowLevelAsyncIoProvider::NetworkFilter& filter, uint flags) : OwnedFd(fd, flags), eventPort(eventPort), filter(filter), observer(eventPort.observeIo(reinterpret_cast(fd))), address(SocketAddress::getLocalAddress(fd)) { // In order to accept asynchronously, we need the AcceptEx() function. Apparently, we have // to query the socket for it dynamically, I guess because of the insanity in which winsock // can be implemented in userspace and old implementations may not support it. GUID guid = WSAID_ACCEPTEX; DWORD n = 0; KJ_WINSOCK(WSAIoctl(fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), &acceptEx, sizeof(acceptEx), &n, NULL, NULL)) { acceptEx = nullptr; return; } } Promise> accept() override { SOCKET newFd = address.socket(SOCK_STREAM); KJ_ASSERT(newFd != INVALID_SOCKET); auto result = heap(eventPort, newFd, NEW_FD_FLAGS); auto scratch = heapArray(256); DWORD dummy; auto op = observer->newOperation(0); if (!acceptEx(fd, newFd, scratch.begin(), 0, 128, 128, &dummy, op->getOverlapped())) { DWORD error = WSAGetLastError(); if (error != ERROR_IO_PENDING) { KJ_FAIL_WIN32("AcceptEx()", error) { break; } return Own(kj::mv(result)); // dummy, won't be used } } return op->onComplete().then(mvCapture(result, mvCapture(scratch, [this,newFd] (Array scratch, Own stream, Win32EventPort::IoResult ioResult) -> Promise> { if (ioResult.errorCode != ERROR_SUCCESS) { KJ_FAIL_WIN32("AcceptEx()", ioResult.errorCode) { break; } } else { SOCKET me = fd; stream->setsockopt(SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, reinterpret_cast(&me), sizeof(me)); } // Supposedly, AcceptEx() places the local and peer addresses into the buffer (which we've // named `scratch`). However, the format in which it writes these is undocumented, and // doesn't even match between native Windows and WINE. Apparently it is useless. I don't know // why they require the buffer to have space for it in the first place. We'll need to call // getpeername() to get the address. auto addr = SocketAddress::getPeerAddress(newFd); if (addr.allowedBy(filter)) { return kj::mv(stream); } else { return accept(); } }))); } uint getPort() override { return address.getPort(); } void getsockopt(int level, int option, void* value, uint* length) override { socklen_t socklen = *length; KJ_WINSOCK(::getsockopt(fd, level, option, reinterpret_cast(value), &socklen)); *length = socklen; } void setsockopt(int level, int option, const void* value, uint length) override { KJ_WINSOCK(::setsockopt(fd, level, option, reinterpret_cast(value), length)); } void getsockname(struct sockaddr* addr, uint* length) override { socklen_t socklen = *length; KJ_WINSOCK(::getsockname(fd, addr, &socklen)); *length = socklen; } public: Win32EventPort& eventPort; LowLevelAsyncIoProvider::NetworkFilter& filter; Own observer; LPFN_ACCEPTEX acceptEx = nullptr; SocketAddress address; }; // TODO(someday): DatagramPortImpl class LowLevelAsyncIoProviderImpl final: public LowLevelAsyncIoProvider { public: LowLevelAsyncIoProviderImpl() : eventLoop(eventPort), waitScope(eventLoop) {} inline WaitScope& getWaitScope() { return waitScope; } Own wrapInputFd(SOCKET fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Own wrapOutputFd(SOCKET fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Own wrapSocketFd(SOCKET fd, uint flags = 0) override { return heap(eventPort, fd, flags); } Promise> wrapConnectingSocketFd( SOCKET fd, const struct sockaddr* addr, uint addrlen, uint flags = 0) override { auto result = heap(eventPort, fd, flags); // ConnectEx requires that the socket be bound, for some reason. Bind to an arbitrary port. SocketAddress::getWildcardForFamily(addr->sa_family).bind(fd); auto connected = result->connect(addr, addrlen); return connected.then(kj::mvCapture(result, [](Own&& result) { return kj::mv(result); })); } Own wrapListenSocketFd( SOCKET fd, NetworkFilter& filter, uint flags = 0) override { return heap(eventPort, fd, filter, flags); } Timer& getTimer() override { return eventPort.getTimer(); } Win32EventPort& getEventPort() { return eventPort; } private: Win32IocpEventPort eventPort; EventLoop eventLoop; WaitScope waitScope; }; // ======================================================================================= class NetworkAddressImpl final: public NetworkAddress { public: NetworkAddressImpl(LowLevelAsyncIoProvider& lowLevel, LowLevelAsyncIoProvider::NetworkFilter& filter, Array addrs) : lowLevel(lowLevel), filter(filter), addrs(kj::mv(addrs)) {} Promise> connect() override { auto addrsCopy = heapArray(addrs.asPtr()); auto promise = connectImpl(lowLevel, filter, addrsCopy); return promise.attach(kj::mv(addrsCopy)); } Own listen() override { if (addrs.size() > 1) { KJ_LOG(WARNING, "Bind address resolved to multiple addresses. Only the first address will " "be used. If this is incorrect, specify the address numerically. This may be fixed " "in the future.", addrs[0].toString()); } int fd = addrs[0].socket(SOCK_STREAM); { KJ_ON_SCOPE_FAILURE(closesocket(fd)); // We always enable SO_REUSEADDR because having to take your server down for five minutes // before it can restart really sucks. int optval = 1; KJ_WINSOCK(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&optval), sizeof(optval))); addrs[0].bind(fd); // TODO(someday): Let queue size be specified explicitly in string addresses. KJ_WINSOCK(::listen(fd, SOMAXCONN)); } return lowLevel.wrapListenSocketFd(fd, filter, NEW_FD_FLAGS); } Own bindDatagramPort() override { if (addrs.size() > 1) { KJ_LOG(WARNING, "Bind address resolved to multiple addresses. Only the first address will " "be used. If this is incorrect, specify the address numerically. This may be fixed " "in the future.", addrs[0].toString()); } int fd = addrs[0].socket(SOCK_DGRAM); { KJ_ON_SCOPE_FAILURE(closesocket(fd)); // We always enable SO_REUSEADDR because having to take your server down for five minutes // before it can restart really sucks. int optval = 1; KJ_WINSOCK(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&optval), sizeof(optval))); addrs[0].bind(fd); } return lowLevel.wrapDatagramSocketFd(fd, filter, NEW_FD_FLAGS); } Own clone() override { return kj::heap(lowLevel, filter, kj::heapArray(addrs.asPtr())); } String toString() override { return strArray(KJ_MAP(addr, addrs) { return addr.toString(); }, ","); } const SocketAddress& chooseOneAddress() { KJ_REQUIRE(addrs.size() > 0, "No addresses available."); return addrs[counter++ % addrs.size()]; } private: LowLevelAsyncIoProvider& lowLevel; LowLevelAsyncIoProvider::NetworkFilter& filter; Array addrs; uint counter = 0; static Promise> connectImpl( LowLevelAsyncIoProvider& lowLevel, LowLevelAsyncIoProvider::NetworkFilter& filter, ArrayPtr addrs) { KJ_ASSERT(addrs.size() > 0); int fd = addrs[0].socket(SOCK_STREAM); return kj::evalNow([&]() -> Promise> { if (!addrs[0].allowedBy(filter)) { return KJ_EXCEPTION(FAILED, "connect() blocked by restrictPeers()"); } else { return lowLevel.wrapConnectingSocketFd( fd, addrs[0].getRaw(), addrs[0].getRawSize(), NEW_FD_FLAGS); } }).then([](Own&& stream) -> Promise> { // Success, pass along. return kj::mv(stream); }, [&lowLevel,&filter,KJ_CPCAP(addrs)](Exception&& exception) mutable -> Promise> { // Connect failed. if (addrs.size() > 1) { // Try the next address instead. return connectImpl(lowLevel, filter, addrs.slice(1, addrs.size())); } else { // No more addresses to try, so propagate the exception. return kj::mv(exception); } }); } }; class SocketNetwork final: public Network { public: explicit SocketNetwork(LowLevelAsyncIoProvider& lowLevel): lowLevel(lowLevel) {} explicit SocketNetwork(SocketNetwork& parent, kj::ArrayPtr allow, kj::ArrayPtr deny) : lowLevel(parent.lowLevel), filter(allow, deny, parent.filter) {} Promise> parseAddress(StringPtr addr, uint portHint = 0) override { return evalLater(mvCapture(heapString(addr), [this,portHint](String&& addr) { return SocketAddress::parse(lowLevel, addr, portHint, filter); })).then([this](Array addresses) -> Own { return heap(lowLevel, filter, kj::mv(addresses)); }); } Own getSockaddr(const void* sockaddr, uint len) override { auto array = kj::heapArrayBuilder(1); array.add(SocketAddress(sockaddr, len)); KJ_REQUIRE(array[0].allowedBy(filter), "address blocked by restrictPeers()") { break; } return Own(heap(lowLevel, filter, array.finish())); } Own restrictPeers( kj::ArrayPtr allow, kj::ArrayPtr deny = nullptr) override { return heap(*this, allow, deny); } private: LowLevelAsyncIoProvider& lowLevel; _::NetworkFilter filter; }; // ======================================================================================= class AsyncIoProviderImpl final: public AsyncIoProvider { public: AsyncIoProviderImpl(LowLevelAsyncIoProvider& lowLevel) : lowLevel(lowLevel), network(lowLevel) {} OneWayPipe newOneWayPipe() override { SOCKET fds[2]; KJ_WINSOCK(_::win32Socketpair(fds)); auto in = lowLevel.wrapSocketFd(fds[0], NEW_FD_FLAGS); auto out = lowLevel.wrapOutputFd(fds[1], NEW_FD_FLAGS); in->shutdownWrite(); return { kj::mv(in), kj::mv(out) }; } TwoWayPipe newTwoWayPipe() override { SOCKET fds[2]; KJ_WINSOCK(_::win32Socketpair(fds)); return TwoWayPipe { { lowLevel.wrapSocketFd(fds[0], NEW_FD_FLAGS), lowLevel.wrapSocketFd(fds[1], NEW_FD_FLAGS) } }; } Network& getNetwork() override { return network; } PipeThread newPipeThread( Function startFunc) override { SOCKET fds[2]; KJ_WINSOCK(_::win32Socketpair(fds)); int threadFd = fds[1]; KJ_ON_SCOPE_FAILURE(closesocket(threadFd)); auto pipe = lowLevel.wrapSocketFd(fds[0], NEW_FD_FLAGS); auto thread = heap(kj::mvCapture(startFunc, [threadFd](Function&& startFunc) { LowLevelAsyncIoProviderImpl lowLevel; auto stream = lowLevel.wrapSocketFd(threadFd, NEW_FD_FLAGS); AsyncIoProviderImpl ioProvider(lowLevel); startFunc(ioProvider, *stream, lowLevel.getWaitScope()); })); return { kj::mv(thread), kj::mv(pipe) }; } Timer& getTimer() override { return lowLevel.getTimer(); } private: LowLevelAsyncIoProvider& lowLevel; SocketNetwork network; }; } // namespace Own newAsyncIoProvider(LowLevelAsyncIoProvider& lowLevel) { return kj::heap(lowLevel); } AsyncIoContext setupAsyncIo() { _::initWinsockOnce(); auto lowLevel = heap(); auto ioProvider = kj::heap(*lowLevel); auto& waitScope = lowLevel->getWaitScope(); auto& eventPort = lowLevel->getEventPort(); return { kj::mv(lowLevel), kj::mv(ioProvider), waitScope, eventPort }; } } // namespace kj #endif // _WIN32 capnproto-c++-0.8.0/src/kj/timer.c++0000644000175000017500000000721613340402540017556 0ustar00kentonkenton00000000000000// Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "timer.h" #include "debug.h" #include namespace kj { kj::Exception Timer::makeTimeoutException() { return KJ_EXCEPTION(OVERLOADED, "operation timed out"); } struct TimerImpl::Impl { struct TimerBefore { bool operator()(TimerPromiseAdapter* lhs, TimerPromiseAdapter* rhs) const; }; using Timers = std::multiset; Timers timers; }; class TimerImpl::TimerPromiseAdapter { public: TimerPromiseAdapter(PromiseFulfiller& fulfiller, TimerImpl::Impl& impl, TimePoint time) : time(time), fulfiller(fulfiller), impl(impl) { pos = impl.timers.insert(this); } ~TimerPromiseAdapter() { if (pos != impl.timers.end()) { impl.timers.erase(pos); } } void fulfill() { fulfiller.fulfill(); impl.timers.erase(pos); pos = impl.timers.end(); } const TimePoint time; private: PromiseFulfiller& fulfiller; TimerImpl::Impl& impl; Impl::Timers::const_iterator pos; }; inline bool TimerImpl::Impl::TimerBefore::operator()( TimerPromiseAdapter* lhs, TimerPromiseAdapter* rhs) const { return lhs->time < rhs->time; } Promise TimerImpl::atTime(TimePoint time) { return newAdaptedPromise(*impl, time); } Promise TimerImpl::afterDelay(Duration delay) { return newAdaptedPromise(*impl, time + delay); } TimerImpl::TimerImpl(TimePoint startTime) : time(startTime), impl(heap()) {} TimerImpl::~TimerImpl() noexcept(false) {} Maybe TimerImpl::nextEvent() { auto iter = impl->timers.begin(); if (iter == impl->timers.end()) { return nullptr; } else { return (*iter)->time; } } Maybe TimerImpl::timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max) { return nextEvent().map([&](TimePoint nextTime) -> uint64_t { if (nextTime <= start) return 0; Duration timeout = nextTime - start; uint64_t result = timeout / unit; bool roundUp = timeout % unit > 0 * SECONDS; if (result >= max) { return max; } else { return result + roundUp; } }); } void TimerImpl::advanceTo(TimePoint newTime) { KJ_REQUIRE(newTime >= time, "can't advance backwards in time") { return; } time = newTime; for (;;) { auto front = impl->timers.begin(); if (front == impl->timers.end() || (*front)->time > time) { break; } (*front)->fulfill(); } } } // namespace kj capnproto-c++-0.8.0/src/kj/test.c++0000644000175000017500000002577713650101756017442 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "test.h" #include "main.h" #include "io.h" #include "miniposix.h" #include #include #include #include "time.h" #ifndef _WIN32 #include #endif namespace kj { namespace { TestCase* testCasesHead = nullptr; TestCase** testCasesTail = &testCasesHead; } // namespace TestCase::TestCase(const char* file, uint line, const char* description) : file(file), line(line), description(description), next(nullptr), prev(testCasesTail), matchedFilter(false) { *prev = this; testCasesTail = &next; } TestCase::~TestCase() { *prev = next; if (next == nullptr) { testCasesTail = prev; } else { next->prev = prev; } } // ======================================================================================= namespace _ { // private GlobFilter::GlobFilter(const char* pattern): pattern(heapString(pattern)) {} GlobFilter::GlobFilter(ArrayPtr pattern): pattern(heapString(pattern)) {} bool GlobFilter::matches(StringPtr name) { // Get out your computer science books. We're implementing a non-deterministic finite automaton. // // Our NDFA has one "state" corresponding to each character in the pattern. // // As you may recall, an NDFA can be transformed into a DFA where every state in the DFA // represents some combination of states in the NDFA. Therefore, we actually have to store a // list of states here. (Actually, what we really want is a set of states, but because our // patterns are mostly non-cyclic a list of states should work fine and be a bit more efficient.) // Our state list starts out pointing only at the start of the pattern. states.resize(0); states.add(0); Vector scratch; // Iterate through each character in the name. for (char c: name) { // Pull the current set of states off to the side, so that we can populate `states` with the // new set of states. Vector oldStates = kj::mv(states); states = kj::mv(scratch); states.resize(0); // The pattern can omit a leading path. So if we're at a '/' then enter the state machine at // the beginning on the next char. if (c == '/' || c == '\\') { states.add(0); } // Process each state. for (uint state: oldStates) { applyState(c, state); } // Store the previous state vector for reuse. scratch = kj::mv(oldStates); } // If any one state is at the end of the pattern (or at a wildcard just before the end of the // pattern), we have a match. for (uint state: states) { while (state < pattern.size() && pattern[state] == '*') { ++state; } if (state == pattern.size()) { return true; } } return false; } void GlobFilter::applyState(char c, int state) { if (state < pattern.size()) { switch (pattern[state]) { case '*': // At a '*', we both re-add the current state and attempt to match the *next* state. if (c != '/' && c != '\\') { // '*' doesn't match '/'. states.add(state); } applyState(c, state + 1); break; case '?': // A '?' matches one character (never a '/'). if (c != '/' && c != '\\') { states.add(state + 1); } break; default: // Any other character matches only itself. if (c == pattern[state]) { states.add(state + 1); } break; } } } } // namespace _ (private) // ======================================================================================= namespace { class TestExceptionCallback: public ExceptionCallback { public: TestExceptionCallback(ProcessContext& context): context(context) {} bool failed() { return sawError; } void logMessage(LogSeverity severity, const char* file, int line, int contextDepth, String&& text) override { void* traceSpace[32]; auto trace = getStackTrace(traceSpace, 2); if (text.size() == 0) { text = kj::heapString("expectation failed"); } text = kj::str(kj::repeat('_', contextDepth), file, ':', line, ": ", kj::mv(text)); if (severity == LogSeverity::ERROR || severity == LogSeverity::FATAL) { sawError = true; context.error(kj::str(text, "\nstack: ", strArray(trace, " "), stringifyStackTrace(trace))); } else { context.warning(text); } } private: ProcessContext& context; bool sawError = false; }; TimePoint readClock() { return systemPreciseMonotonicClock().now(); } } // namespace class TestRunner { public: explicit TestRunner(ProcessContext& context) : context(context), useColor(isatty(STDOUT_FILENO)) {} MainFunc getMain() { return MainBuilder(context, "KJ Test Runner (version not applicable)", "Run all tests that have been linked into the binary with this test runner.") .addOptionWithArg({'f', "filter"}, KJ_BIND_METHOD(*this, setFilter), "[:]", "Run only the specified test case(s). You may use a '*' wildcard in . You may " "also omit any prefix of 's path; test from all matching files will run. " "You may specify multiple filters; any test matching at least one filter will run. " " may be a range, e.g. \"100-500\".") .addOption({'l', "list"}, KJ_BIND_METHOD(*this, setList), "List all test cases that would run, but don't run them. If --filter is specified " "then only the match tests will be listed.") .callAfterParsing(KJ_BIND_METHOD(*this, run)) .build(); } MainBuilder::Validity setFilter(StringPtr pattern) { hasFilter = true; ArrayPtr filePattern = pattern; uint minLine = kj::minValue; uint maxLine = kj::maxValue; KJ_IF_MAYBE(colonPos, pattern.findLast(':')) { char* end; StringPtr lineStr = pattern.slice(*colonPos + 1); bool parsedRange = false; minLine = strtoul(lineStr.cStr(), &end, 0); if (end != lineStr.begin()) { if (*end == '-') { // A range. const char* part2 = end + 1; maxLine = strtoul(part2, &end, 0); if (end > part2 && *end == '\0') { parsedRange = true; } } else if (*end == '\0') { parsedRange = true; maxLine = minLine; } } if (parsedRange) { // We have an exact line number. filePattern = pattern.slice(0, *colonPos); } else { // Can't parse as a number. Maybe the colon is part of a Windows path name or something. // Let's just keep it as part of the file pattern. minLine = kj::minValue; maxLine = kj::maxValue; } } _::GlobFilter filter(filePattern); for (TestCase* testCase = testCasesHead; testCase != nullptr; testCase = testCase->next) { if (!testCase->matchedFilter && filter.matches(testCase->file) && testCase->line >= minLine && testCase->line <= maxLine) { testCase->matchedFilter = true; } } return true; } MainBuilder::Validity setList() { listOnly = true; return true; } MainBuilder::Validity run() { if (testCasesHead == nullptr) { return "no tests were declared"; } // Find the common path prefix of all filenames, so we can strip it off. ArrayPtr commonPrefix = StringPtr(testCasesHead->file); for (TestCase* testCase = testCasesHead; testCase != nullptr; testCase = testCase->next) { for (size_t i: kj::indices(commonPrefix)) { if (testCase->file[i] != commonPrefix[i]) { commonPrefix = commonPrefix.slice(0, i); break; } } } // Back off the prefix to the last '/'. while (commonPrefix.size() > 0 && commonPrefix.back() != '/' && commonPrefix.back() != '\\') { commonPrefix = commonPrefix.slice(0, commonPrefix.size() - 1); } // Run the testts. uint passCount = 0; uint failCount = 0; for (TestCase* testCase = testCasesHead; testCase != nullptr; testCase = testCase->next) { if (!hasFilter || testCase->matchedFilter) { auto name = kj::str(testCase->file + commonPrefix.size(), ':', testCase->line, ": ", testCase->description); write(BLUE, "[ TEST ]", name); if (!listOnly) { bool currentFailed = true; auto start = readClock(); KJ_IF_MAYBE(exception, runCatchingExceptions([&]() { TestExceptionCallback exceptionCallback(context); testCase->run(); currentFailed = exceptionCallback.failed(); })) { context.error(kj::str(*exception)); } auto end = readClock(); auto message = kj::str(name, " (", (end - start) / kj::MICROSECONDS, " μs)"); if (currentFailed) { write(RED, "[ FAIL ]", message); ++failCount; } else { write(GREEN, "[ PASS ]", message); ++passCount; } } } } if (passCount > 0) write(GREEN, kj::str(passCount, " test(s) passed"), ""); if (failCount > 0) write(RED, kj::str(failCount, " test(s) failed"), ""); context.exit(); KJ_UNREACHABLE; } private: ProcessContext& context; bool useColor; bool hasFilter = false; bool listOnly = false; enum Color { RED, GREEN, BLUE }; void write(StringPtr text) { FdOutputStream(STDOUT_FILENO).write(text.begin(), text.size()); } void write(Color color, StringPtr prefix, StringPtr message) { StringPtr startColor, endColor; if (useColor) { switch (color) { case RED: startColor = "\033[0;1;31m"; break; case GREEN: startColor = "\033[0;1;32m"; break; case BLUE: startColor = "\033[0;1;34m"; break; } endColor = "\033[0m"; } String text = kj::str(startColor, prefix, endColor, ' ', message, '\n'); write(text); } }; } // namespace kj KJ_MAIN(kj::TestRunner); capnproto-c++-0.8.0/src/kj/common.c++0000644000175000017500000000374013340402540017724 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "common.h" #include "debug.h" #include #ifdef _MSC_VER #include #endif namespace kj { namespace _ { // private void inlineRequireFailure(const char* file, int line, const char* expectation, const char* macroArgs, const char* message) { if (message == nullptr) { Debug::Fault f(file, line, kj::Exception::Type::FAILED, expectation, macroArgs); f.fatal(); } else { Debug::Fault f(file, line, kj::Exception::Type::FAILED, expectation, macroArgs, message); f.fatal(); } } void unreachable() { KJ_FAIL_ASSERT("Supposedly-unreachable branch executed."); // Really make sure we abort. abort(); } } // namespace _ (private) #if _MSC_VER && !__clang__ float nan() { return std::numeric_limits::quiet_NaN(); } #endif } // namespace kj capnproto-c++-0.8.0/src/kj/units.c++0000644000175000017500000000250213340402540017571 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "units.h" #include "debug.h" namespace kj { void ThrowOverflow::operator()() const { KJ_FAIL_REQUIRE("integer overflow"); } } // namespace kj capnproto-c++-0.8.0/src/kj/memory.c++0000644000175000017500000000243213340402540017741 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "memory.h" namespace kj { const NullDisposer NullDisposer::instance = NullDisposer(); } // namespace kj capnproto-c++-0.8.0/src/kj/refcount.c++0000644000175000017500000000650013340402540020256 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "refcount.h" #include "debug.h" #if _MSC_VER // Annoyingly, MSVC only implements the C++ atomic libs, not the C libs, so the only useful // thing we can get from seems to be atomic_thread_fence... but that one function is // indeed not implemented by the intrinsics, so... #include #endif namespace kj { // ======================================================================================= // Non-atomic (thread-unsafe) refcounting Refcounted::~Refcounted() noexcept(false) { KJ_ASSERT(refcount == 0, "Refcounted object deleted with non-zero refcount."); } void Refcounted::disposeImpl(void* pointer) const { if (--refcount == 0) { delete this; } } // ======================================================================================= // Atomic (thread-safe) refcounting AtomicRefcounted::~AtomicRefcounted() noexcept(false) { KJ_ASSERT(refcount == 0, "Refcounted object deleted with non-zero refcount."); } void AtomicRefcounted::disposeImpl(void* pointer) const { #if _MSC_VER if (KJ_MSVC_INTERLOCKED(Decrement, rel)(&refcount) == 0) { std::atomic_thread_fence(std::memory_order_acquire); delete this; } #else if (__atomic_sub_fetch(&refcount, 1, __ATOMIC_RELEASE) == 0) { __atomic_thread_fence(__ATOMIC_ACQUIRE); delete this; } #endif } bool AtomicRefcounted::addRefWeakInternal() const { #if _MSC_VER long orig = refcount; for (;;) { if (orig == 0) { // Refcount already hit zero. Destructor is already running so we can't revive the object. return false; } unsigned long old = KJ_MSVC_INTERLOCKED(CompareExchange, nf)(&refcount, orig + 1, orig); if (old == orig) { return true; } orig = old; } #else uint orig = __atomic_load_n(&refcount, __ATOMIC_RELAXED); for (;;) { if (orig == 0) { // Refcount already hit zero. Destructor is already running so we can't revive the object. return false; } if (__atomic_compare_exchange_n(&refcount, &orig, orig + 1, true, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { // Successfully incremented refcount without letting it hit zero. return true; } } #endif } } // namespace kj capnproto-c++-0.8.0/src/kj/array.c++0000644000175000017500000000744413340402540017557 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "array.h" #include "exception.h" namespace kj { void ExceptionSafeArrayUtil::construct(size_t count, void (*constructElement)(void*)) { while (count > 0) { constructElement(pos); pos += elementSize; ++constructedElementCount; --count; } } void ExceptionSafeArrayUtil::destroyAll() { while (constructedElementCount > 0) { pos -= elementSize; --constructedElementCount; destroyElement(pos); } } const DestructorOnlyArrayDisposer DestructorOnlyArrayDisposer::instance = DestructorOnlyArrayDisposer(); void DestructorOnlyArrayDisposer::disposeImpl( void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const { if (destroyElement != nullptr) { ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement); guard.destroyAll(); } } const NullArrayDisposer NullArrayDisposer::instance = NullArrayDisposer(); void NullArrayDisposer::disposeImpl( void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const {} namespace _ { // private struct AutoDeleter { void* ptr; inline void* release() { void* result = ptr; ptr = nullptr; return result; } inline AutoDeleter(void* ptr): ptr(ptr) {} inline ~AutoDeleter() { operator delete(ptr); } }; void* HeapArrayDisposer::allocateImpl(size_t elementSize, size_t elementCount, size_t capacity, void (*constructElement)(void*), void (*destroyElement)(void*)) { AutoDeleter result(operator new(elementSize * capacity)); if (constructElement == nullptr) { // Nothing to do. } else if (destroyElement == nullptr) { byte* pos = reinterpret_cast(result.ptr); while (elementCount > 0) { constructElement(pos); pos += elementSize; --elementCount; } } else { ExceptionSafeArrayUtil guard(result.ptr, elementSize, 0, destroyElement); guard.construct(elementCount, constructElement); guard.release(); } return result.release(); } void HeapArrayDisposer::disposeImpl( void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const { // Note that capacity is ignored since operator delete() doesn't care about it. AutoDeleter deleter(firstElement); if (destroyElement != nullptr) { ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement); guard.destroyAll(); } } const HeapArrayDisposer HeapArrayDisposer::instance = HeapArrayDisposer(); } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/string.c++0000644000175000017500000004151713340402540017746 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "string.h" #include "debug.h" #include #include #include #include #include namespace kj { #if _MSC_VER #pragma warning(disable: 4996) // Warns that sprintf() is buffer-overrunny. We know that, it's cool. #endif namespace { bool isHex(const char *s) { if (*s == '-') s++; return s[0] == '0' && (s[1] == 'x' || s[1] == 'X'); } long long parseSigned(const StringPtr& s, long long min, long long max) { KJ_REQUIRE(s != nullptr, "String does not contain valid number", s) { return 0; } char *endPtr; errno = 0; auto value = strtoll(s.begin(), &endPtr, isHex(s.cStr()) ? 16 : 10); KJ_REQUIRE(endPtr == s.end(), "String does not contain valid number", s) { return 0; } KJ_REQUIRE(errno != ERANGE, "Value out-of-range", s) { return 0; } KJ_REQUIRE(value >= min && value <= max, "Value out-of-range", value, min, max) { return 0; } return value; } unsigned long long parseUnsigned(const StringPtr& s, unsigned long long max) { KJ_REQUIRE(s != nullptr, "String does not contain valid number", s) { return 0; } char *endPtr; errno = 0; auto value = strtoull(s.begin(), &endPtr, isHex(s.cStr()) ? 16 : 10); KJ_REQUIRE(endPtr == s.end(), "String does not contain valid number", s) { return 0; } KJ_REQUIRE(errno != ERANGE, "Value out-of-range", s) { return 0; } KJ_REQUIRE(value <= max, "Value out-of-range", value, max) { return 0; } //strtoull("-1") does not fail with ERANGE KJ_REQUIRE(s[0] != '-', "Value out-of-range", s) { return 0; } return value; } template T parseInteger(const StringPtr& s) { if (static_cast(minValue) < 0) { long long min = static_cast(minValue); long long max = static_cast(maxValue); return static_cast(parseSigned(s, min, max)); } else { unsigned long long max = static_cast(maxValue); return static_cast(parseUnsigned(s, max)); } } double parseDouble(const StringPtr& s) { KJ_REQUIRE(s != nullptr, "String does not contain valid number", s) { return 0; } char *endPtr; errno = 0; auto value = strtod(s.begin(), &endPtr); KJ_REQUIRE(endPtr == s.end(), "String does not contain valid floating number", s) { return 0; } #if _WIN32 || __CYGWIN__ || __BIONIC__ // When Windows' strtod() parses "nan", it returns a value with the sign bit set. But, our // preferred canonical value for NaN does not have the sign bit set, and all other platforms // return one without the sign bit set. So, on Windows, detect NaN and return our preferred // version. // // Cygwin seemingly does not try to emulate Linux behavior here, but rather allows Windows' // behavior to leak through. (Conversely, WINE actually produces the Linux behavior despite // trying to behave like Win32...) // // Bionic (Android) failed the unit test and so I added it to the list without investigating // further. if (isNaN(value)) { // NaN return kj::nan(); } #endif return value; } } // namespace #define PARSE_AS_INTEGER(T) \ template <> T StringPtr::parseAs() const { return parseInteger(*this); } PARSE_AS_INTEGER(char); PARSE_AS_INTEGER(signed char); PARSE_AS_INTEGER(unsigned char); PARSE_AS_INTEGER(short); PARSE_AS_INTEGER(unsigned short); PARSE_AS_INTEGER(int); PARSE_AS_INTEGER(unsigned int); PARSE_AS_INTEGER(long); PARSE_AS_INTEGER(unsigned long); PARSE_AS_INTEGER(long long); PARSE_AS_INTEGER(unsigned long long); #undef PARSE_AS_INTEGER template <> double StringPtr::parseAs() const { return parseDouble(*this); } template <> float StringPtr::parseAs() const { return parseDouble(*this); } String heapString(size_t size) { char* buffer = _::HeapArrayDisposer::allocate(size + 1); buffer[size] = '\0'; return String(buffer, size, _::HeapArrayDisposer::instance); } String heapString(const char* value, size_t size) { char* buffer = _::HeapArrayDisposer::allocate(size + 1); if (size != 0u) { memcpy(buffer, value, size); } buffer[size] = '\0'; return String(buffer, size, _::HeapArrayDisposer::instance); } template static CappedArray hexImpl(T i) { // We don't use sprintf() because it's not async-signal-safe (for strPreallocated()). CappedArray result; uint8_t reverse[sizeof(T) * 2]; uint8_t* p = reverse; if (i == 0) { *p++ = 0; } else { while (i > 0) { *p++ = i % 16; i /= 16; } } char* p2 = result.begin(); while (p > reverse) { *p2++ = "0123456789abcdef"[*--p]; } result.setSize(p2 - result.begin()); return result; } #define HEXIFY_INT(type) \ CappedArray hex(type i) { \ return hexImpl(i); \ } HEXIFY_INT(unsigned char); HEXIFY_INT(unsigned short); HEXIFY_INT(unsigned int); HEXIFY_INT(unsigned long); HEXIFY_INT(unsigned long long); #undef HEXIFY_INT namespace _ { // private StringPtr Stringifier::operator*(decltype(nullptr)) const { return "nullptr"; } StringPtr Stringifier::operator*(bool b) const { return b ? StringPtr("true") : StringPtr("false"); } template static CappedArray stringifyImpl(T i) { // We don't use sprintf() because it's not async-signal-safe (for strPreallocated()). CappedArray result; bool negative = i < 0; Unsigned u = negative ? -i : i; uint8_t reverse[sizeof(T) * 3 + 1]; uint8_t* p = reverse; if (u == 0) { *p++ = 0; } else { while (u > 0) { *p++ = u % 10; u /= 10; } } char* p2 = result.begin(); if (negative) *p2++ = '-'; while (p > reverse) { *p2++ = '0' + *--p; } result.setSize(p2 - result.begin()); return result; } #define STRINGIFY_INT(type, unsigned) \ CappedArray Stringifier::operator*(type i) const { \ return stringifyImpl(i); \ } STRINGIFY_INT(signed char, uint); STRINGIFY_INT(unsigned char, uint); STRINGIFY_INT(short, uint); STRINGIFY_INT(unsigned short, uint); STRINGIFY_INT(int, uint); STRINGIFY_INT(unsigned int, uint); STRINGIFY_INT(long, unsigned long); STRINGIFY_INT(unsigned long, unsigned long); STRINGIFY_INT(long long, unsigned long long); STRINGIFY_INT(unsigned long long, unsigned long long); #undef STRINGIFY_INT CappedArray Stringifier::operator*(const void* i) const { \ return hexImpl(reinterpret_cast(i)); } namespace { // ---------------------------------------------------------------------- // DoubleToBuffer() // FloatToBuffer() // Copied from Protocol Buffers, (C) Google, BSD license. // Kenton wrote this code originally. The following commentary is // from the original. // // Description: converts a double or float to a string which, if // passed to NoLocaleStrtod(), will produce the exact same original double // (except in case of NaN; all NaNs are considered the same value). // We try to keep the string short but it's not guaranteed to be as // short as possible. // // DoubleToBuffer() and FloatToBuffer() write the text to the given // buffer and return it. The buffer must be at least // kDoubleToBufferSize bytes for doubles and kFloatToBufferSize // bytes for floats. kFastToBufferSize is also guaranteed to be large // enough to hold either. // // We want to print the value without losing precision, but we also do // not want to print more digits than necessary. This turns out to be // trickier than it sounds. Numbers like 0.2 cannot be represented // exactly in binary. If we print 0.2 with a very large precision, // e.g. "%.50g", we get "0.2000000000000000111022302462515654042363167". // On the other hand, if we set the precision too low, we lose // significant digits when printing numbers that actually need them. // It turns out there is no precision value that does the right thing // for all numbers. // // Our strategy is to first try printing with a precision that is never // over-precise, then parse the result with strtod() to see if it // matches. If not, we print again with a precision that will always // give a precise result, but may use more digits than necessary. // // An arguably better strategy would be to use the algorithm described // in "How to Print Floating-Point Numbers Accurately" by Steele & // White, e.g. as implemented by David M. Gay's dtoa(). It turns out, // however, that the following implementation is about as fast as // DMG's code. Furthermore, DMG's code locks mutexes, which means it // will not scale well on multi-core machines. DMG's code is slightly // more accurate (in that it will never use more digits than // necessary), but this is probably irrelevant for most users. // // Rob Pike and Ken Thompson also have an implementation of dtoa() in // third_party/fmt/fltfmt.cc. Their implementation is similar to this // one in that it makes guesses and then uses strtod() to check them. // Their implementation is faster because they use their own code to // generate the digits in the first place rather than use snprintf(), // thus avoiding format string parsing overhead. However, this makes // it considerably more complicated than the following implementation, // and it is embedded in a larger library. If speed turns out to be // an issue, we could re-implement this in terms of their // implementation. // ---------------------------------------------------------------------- #ifdef _WIN32 // MSVC has only _snprintf, not snprintf. // // MinGW has both snprintf and _snprintf, but they appear to be different // functions. The former is buggy. When invoked like so: // char buffer[32]; // snprintf(buffer, 32, "%.*g\n", FLT_DIG, 1.23e10f); // it prints "1.23000e+10". This is plainly wrong: %g should never print // trailing zeros after the decimal point. For some reason this bug only // occurs with some input values, not all. In any case, _snprintf does the // right thing, so we use it. #define snprintf _snprintf #endif inline bool IsNaN(double value) { // NaN is never equal to anything, even itself. return value != value; } // In practice, doubles should never need more than 24 bytes and floats // should never need more than 14 (including null terminators), but we // overestimate to be safe. static const int kDoubleToBufferSize = 32; static const int kFloatToBufferSize = 24; static inline bool IsValidFloatChar(char c) { return ('0' <= c && c <= '9') || c == 'e' || c == 'E' || c == '+' || c == '-'; } void DelocalizeRadix(char* buffer) { // Fast check: if the buffer has a normal decimal point, assume no // translation is needed. if (strchr(buffer, '.') != NULL) return; // Find the first unknown character. while (IsValidFloatChar(*buffer)) ++buffer; if (*buffer == '\0') { // No radix character found. return; } // We are now pointing at the locale-specific radix character. Replace it // with '.'. *buffer = '.'; ++buffer; if (!IsValidFloatChar(*buffer) && *buffer != '\0') { // It appears the radix was a multi-byte character. We need to remove the // extra bytes. char* target = buffer; do { ++buffer; } while (!IsValidFloatChar(*buffer) && *buffer != '\0'); memmove(target, buffer, strlen(buffer) + 1); } } void RemovePlus(char* buffer) { // Remove any + characters because they are redundant and ugly. for (;;) { buffer = strchr(buffer, '+'); if (buffer == NULL) { return; } memmove(buffer, buffer + 1, strlen(buffer + 1) + 1); } } #if _WIN32 void RemoveE0(char* buffer) { // Remove redundant leading 0's after an e, e.g. 1e012. Seems to appear on // Windows. // Find and skip 'e'. char* ptr = strchr(buffer, 'e'); if (ptr == nullptr) return; ++ptr; // Skip '-'. if (*ptr == '-') ++ptr; // Skip '0's. char* ptr2 = ptr; while (*ptr2 == '0') ++ptr2; // If we went past the last digit, back up one. if (*ptr2 < '0' || *ptr2 > '9') --ptr2; // Move bytes backwards. if (ptr2 > ptr) { memmove(ptr, ptr2, strlen(ptr2) + 1); } } #endif char* DoubleToBuffer(double value, char* buffer) { // DBL_DIG is 15 for IEEE-754 doubles, which are used on almost all // platforms these days. Just in case some system exists where DBL_DIG // is significantly larger -- and risks overflowing our buffer -- we have // this assert. static_assert(DBL_DIG < 20, "DBL_DIG is too big."); if (value == inf()) { strcpy(buffer, "inf"); return buffer; } else if (value == -inf()) { strcpy(buffer, "-inf"); return buffer; } else if (IsNaN(value)) { strcpy(buffer, "nan"); return buffer; } int snprintf_result KJ_UNUSED = snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG, value); // The snprintf should never overflow because the buffer is significantly // larger than the precision we asked for. KJ_DASSERT(snprintf_result > 0 && snprintf_result < kDoubleToBufferSize); // We need to make parsed_value volatile in order to force the compiler to // write it out to the stack. Otherwise, it may keep the value in a // register, and if it does that, it may keep it as a long double instead // of a double. This long double may have extra bits that make it compare // unequal to "value" even though it would be exactly equal if it were // truncated to a double. volatile double parsed_value = strtod(buffer, NULL); if (parsed_value != value) { int snprintf_result2 KJ_UNUSED = snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG+2, value); // Should never overflow; see above. KJ_DASSERT(snprintf_result2 > 0 && snprintf_result2 < kDoubleToBufferSize); } DelocalizeRadix(buffer); RemovePlus(buffer); #if _WIN32 RemoveE0(buffer); #endif // _WIN32 return buffer; } bool safe_strtof(const char* str, float* value) { char* endptr; errno = 0; // errno only gets set on errors #if defined(_WIN32) || defined (__hpux) // has no strtof() *value = static_cast(strtod(str, &endptr)); #else *value = strtof(str, &endptr); #endif return *str != 0 && *endptr == 0 && errno == 0; } char* FloatToBuffer(float value, char* buffer) { // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all // platforms these days. Just in case some system exists where FLT_DIG // is significantly larger -- and risks overflowing our buffer -- we have // this assert. static_assert(FLT_DIG < 10, "FLT_DIG is too big"); if (value == inf()) { strcpy(buffer, "inf"); return buffer; } else if (value == -inf()) { strcpy(buffer, "-inf"); return buffer; } else if (IsNaN(value)) { strcpy(buffer, "nan"); return buffer; } int snprintf_result KJ_UNUSED = snprintf(buffer, kFloatToBufferSize, "%.*g", FLT_DIG, value); // The snprintf should never overflow because the buffer is significantly // larger than the precision we asked for. KJ_DASSERT(snprintf_result > 0 && snprintf_result < kFloatToBufferSize); float parsed_value; if (!safe_strtof(buffer, &parsed_value) || parsed_value != value) { int snprintf_result2 KJ_UNUSED = snprintf(buffer, kFloatToBufferSize, "%.*g", FLT_DIG+2, value); // Should never overflow; see above. KJ_DASSERT(snprintf_result2 > 0 && snprintf_result2 < kFloatToBufferSize); } DelocalizeRadix(buffer); RemovePlus(buffer); #if _WIN32 RemoveE0(buffer); #endif // _WIN32 return buffer; } } // namespace CappedArray Stringifier::operator*(float f) const { CappedArray result; result.setSize(strlen(FloatToBuffer(f, result.begin()))); return result; } CappedArray Stringifier::operator*(double f) const { CappedArray result; result.setSize(strlen(DoubleToBuffer(f, result.begin()))); return result; } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/string-tree.c++0000644000175000017500000000501313340402540020672 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "string-tree.h" namespace kj { StringTree::StringTree(Array&& pieces, StringPtr delim) : size_(0), branches(heapArray(pieces.size())) { if (pieces.size() > 0) { if (pieces.size() > 1 && delim.size() > 0) { text = heapString((pieces.size() - 1) * delim.size()); size_ = text.size(); } branches[0].index = 0; branches[0].content = kj::mv(pieces[0]); size_ += pieces[0].size(); for (uint i = 1; i < pieces.size(); i++) { if (delim.size() > 0) { memcpy(text.begin() + (i - 1) * delim.size(), delim.begin(), delim.size()); } branches[i].index = i * delim.size(); branches[i].content = kj::mv(pieces[i]); size_ += pieces[i].size(); } } } String StringTree::flatten() const { String result = heapString(size()); flattenTo(result.begin()); return result; } char* StringTree::flattenTo(char* __restrict__ target) const { visit([&target](ArrayPtr text) { memcpy(target, text.begin(), text.size()); target += text.size(); }); return target; } char* StringTree::flattenTo(char* __restrict__ target, char* limit) const { visit([&target,limit](ArrayPtr text) { size_t size = kj::min(text.size(), limit - target); memcpy(target, text.begin(), size); target += size; }); return target; } } // namespace kj capnproto-c++-0.8.0/src/kj/hash.c++0000644000175000017500000000375713340402540017367 0ustar00kentonkenton00000000000000// Copyright (c) 2018 Kenton Varda and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "hash.h" namespace kj { namespace _ { // private uint HashCoder::operator*(ArrayPtr s) const { // murmur2 adapted from libc++ source code. // // TODO(perf): Use CityHash or FarmHash on 64-bit machines? They seem optimized for x86-64; what // about ARM? Ask Vlad for advice. constexpr uint m = 0x5bd1e995; constexpr uint r = 24; uint h = s.size(); const byte* data = s.begin(); uint len = s.size(); for (; len >= 4; data += 4, len -= 4) { uint k; memcpy(&k, data, sizeof(k)); k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } switch (len) { case 3: h ^= data[2] << 16; // fallthrough case 2: h ^= data[1] << 8; // fallthrough case 1: h ^= data[0]; h *= m; } h ^= h >> 13; h *= m; h ^= h >> 15; return h; } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/table.c++0000644000175000017500000007452113650101756017541 0ustar00kentonkenton00000000000000// Copyright (c) 2018 Kenton Varda and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "table.h" #include "debug.h" #include namespace kj { namespace _ { static inline uint lg(uint value) { // Compute floor(log2(value)). // // Undefined for value = 0. #if _MSC_VER unsigned long i; auto found = _BitScanReverse(&i, value); KJ_DASSERT(found); // !found means value = 0 return i; #else return sizeof(uint) * 8 - 1 - __builtin_clz(value); #endif } void throwDuplicateTableRow() { KJ_FAIL_REQUIRE("inserted row already exists in table"); } void logHashTableInconsistency() { KJ_LOG(ERROR, "HashIndex detected hash table inconsistency. This can happen if you create a kj::Table " "with a hash index and you modify the rows in the table post-indexing in a way that would " "change their hash. This is a serious bug which will lead to undefined behavior." "\nstack: ", kj::getStackTrace()); } // List of primes where each element is roughly double the previous. Obtained // from: // http://planetmath.org/goodhashtableprimes // Primes < 53 were added to ensure that small tables don't allocate excessive memory. static const size_t PRIMES[] = { 1, // 2^ 0 = 1 3, // 2^ 1 = 2 5, // 2^ 2 = 4 11, // 2^ 3 = 8 23, // 2^ 4 = 16 53, // 2^ 5 = 32 97, // 2^ 6 = 64 193, // 2^ 7 = 128 389, // 2^ 8 = 256 769, // 2^ 9 = 512 1543, // 2^10 = 1024 3079, // 2^11 = 2048 6151, // 2^12 = 4096 12289, // 2^13 = 8192 24593, // 2^14 = 16384 49157, // 2^15 = 32768 98317, // 2^16 = 65536 196613, // 2^17 = 131072 393241, // 2^18 = 262144 786433, // 2^19 = 524288 1572869, // 2^20 = 1048576 3145739, // 2^21 = 2097152 6291469, // 2^22 = 4194304 12582917, // 2^23 = 8388608 25165843, // 2^24 = 16777216 50331653, // 2^25 = 33554432 100663319, // 2^26 = 67108864 201326611, // 2^27 = 134217728 402653189, // 2^28 = 268435456 805306457, // 2^29 = 536870912 1610612741, // 2^30 = 1073741824 }; uint chooseBucket(uint hash, uint count) { // Integer modulus is really, really slow. It turns out that the compiler can generate much // faster code if the denominator is a constant. Since we have a fixed set of possible // denominators, a big old switch() statement is a win. // TODO(perf): Consider using power-of-two bucket sizes. We can safely do so as long as we demand // high-quality hash functions -- kj::hashCode() needs good diffusion even for integers, can't // just be a cast. Also be sure to implement Robin Hood hashing to avoid extremely bad negative // lookup time when elements have sequential hashes (otherwise, it could be necessary to scan // the entire list to determine that an element isn't present). switch (count) { #define HANDLE(i) case i##u: return hash % i##u HANDLE( 1); HANDLE( 3); HANDLE( 5); HANDLE( 11); HANDLE( 23); HANDLE( 53); HANDLE( 97); HANDLE( 193); HANDLE( 389); HANDLE( 769); HANDLE( 1543); HANDLE( 3079); HANDLE( 6151); HANDLE( 12289); HANDLE( 24593); HANDLE( 49157); HANDLE( 98317); HANDLE( 196613); HANDLE( 393241); HANDLE( 786433); HANDLE( 1572869); HANDLE( 3145739); HANDLE( 6291469); HANDLE( 12582917); HANDLE( 25165843); HANDLE( 50331653); HANDLE( 100663319); HANDLE( 201326611); HANDLE( 402653189); HANDLE( 805306457); HANDLE(1610612741); #undef HANDLE default: return hash % count; } } size_t chooseHashTableSize(uint size) { if (size == 0) return 0; // Add 1 to compensate for the floor() above, then look up the best prime bucket size for that // target size. return PRIMES[lg(size) + 1]; } kj::Array rehash(kj::ArrayPtr oldBuckets, size_t targetSize) { // Rehash the whole table. KJ_REQUIRE(targetSize < (1 << 30), "hash table has reached maximum size"); size_t size = chooseHashTableSize(targetSize); if (size < oldBuckets.size()) { size = oldBuckets.size(); } auto newBuckets = kj::heapArray(size); memset(newBuckets.begin(), 0, sizeof(HashBucket) * size); uint entryCount = 0; uint collisionCount = 0; for (auto& oldBucket: oldBuckets) { if (oldBucket.isOccupied()) { ++entryCount; for (uint i = oldBucket.hash % newBuckets.size();; i = probeHash(newBuckets, i)) { auto& newBucket = newBuckets[i]; if (newBucket.isEmpty()) { newBucket = oldBucket; break; } ++collisionCount; } } } if (collisionCount > 16 + entryCount * 4) { static bool warned = false; if (!warned) { KJ_LOG(WARNING, "detected excessive collisions in hash table; is your hash function OK?", entryCount, collisionCount, kj::getStackTrace()); warned = true; } } return newBuckets; } // ======================================================================================= // BTree #if _WIN32 #define aligned_free _aligned_free #else #define aligned_free ::free #endif BTreeImpl::BTreeImpl() : tree(const_cast(&EMPTY_NODE)), treeCapacity(1), height(0), freelistHead(1), freelistSize(0), beginLeaf(0), endLeaf(0) {} BTreeImpl::~BTreeImpl() noexcept(false) { if (tree != &EMPTY_NODE) { aligned_free(tree); } } const BTreeImpl::NodeUnion BTreeImpl::EMPTY_NODE = {{{0, {0}}}}; void BTreeImpl::verify(size_t size, FunctionParam f) { KJ_ASSERT(verifyNode(size, f, 0, height, nullptr) == size); } size_t BTreeImpl::verifyNode(size_t size, FunctionParam& f, uint pos, uint height, MaybeUint maxRow) { if (height > 0) { auto& parent = tree[pos].parent; auto n = parent.keyCount(); size_t total = 0; for (auto i: kj::zeroTo(n)) { KJ_ASSERT(*parent.keys[i] < size); total += verifyNode(size, f, parent.children[i], height - 1, parent.keys[i]); KJ_ASSERT(i + 1 == n || f(*parent.keys[i], *parent.keys[i + 1])); } total += verifyNode(size, f, parent.children[n], height - 1, maxRow); KJ_ASSERT(maxRow == nullptr || f(*parent.keys[n-1], *maxRow)); return total; } else { auto& leaf = tree[pos].leaf; auto n = leaf.size(); for (auto i: kj::zeroTo(n)) { KJ_ASSERT(*leaf.rows[i] < size); if (i + 1 < n) { KJ_ASSERT(f(*leaf.rows[i], *leaf.rows[i + 1])); } else { KJ_ASSERT(maxRow == nullptr || leaf.rows[n-1] == maxRow); } } return n; } } void BTreeImpl::logInconsistency() const { KJ_LOG(ERROR, "BTreeIndex detected tree state inconsistency. This can happen if you create a kj::Table " "with a b-tree index and you modify the rows in the table post-indexing in a way that would " "change their ordering. This is a serious bug which will lead to undefined behavior." "\nstack: ", kj::getStackTrace()); } void BTreeImpl::reserve(size_t size) { KJ_REQUIRE(size < (1u << 31), "b-tree has reached maximum size"); // Calculate the worst-case number of leaves to cover the size, given that a leaf is always at // least half-full. (Note that it's correct for this calculation to round down, not up: The // remainder will necessarily be distributed among the non-full leaves, rather than creating a // new leaf, because if it went into a new leaf, that leaf would be less than half-full.) uint leaves = size / (Leaf::NROWS / 2); // Calculate the worst-case number of parents to cover the leaves, given that a parent is always // at least half-full. Since the parents form a tree with branching factor B, the size of the // tree is N/B + N/B^2 + N/B^3 + N/B^4 + ... = N / (B - 1). Math. constexpr uint branchingFactor = Parent::NCHILDREN / 2; uint parents = leaves / (branchingFactor - 1); // Height is log-base-branching-factor of leaves, plus 1 for the root node. uint height = lg(leaves | 1) / lg(branchingFactor) + 1; size_t newSize = leaves + parents + 1 + // + 1 for the root height + 2; // minimum freelist size needed by insert() if (treeCapacity < newSize) { growTree(newSize); } } void BTreeImpl::clear() { if (tree != &EMPTY_NODE) { azero(tree, treeCapacity); height = 0; freelistHead = 1; freelistSize = treeCapacity; beginLeaf = 0; endLeaf = 0; } } void BTreeImpl::growTree(uint minCapacity) { uint newCapacity = kj::max(kj::max(minCapacity, treeCapacity * 2), 4); freelistSize += newCapacity - treeCapacity; // Allocate some aligned memory! In theory this should be as simple as calling the C11 standard // aligned_alloc() function. Unfortunately, many platforms don't implement it. Luckily, there // are usually alternatives. #if _WIN32 // Windows lacks aligned_alloc() but has its own _aligned_malloc() (which requires freeing using // _aligned_free()). // WATCH OUT: The argument order for _aligned_malloc() is opposite of aligned_alloc()! NodeUnion* newTree = reinterpret_cast( _aligned_malloc(newCapacity * sizeof(BTreeImpl::NodeUnion), sizeof(BTreeImpl::NodeUnion))); KJ_ASSERT(newTree != nullptr, "memory allocation failed", newCapacity); #else // macOS, OpenBSD, and Android lack aligned_alloc(), but have posix_memalign(). Fine. void* allocPtr; int error = posix_memalign(&allocPtr, sizeof(BTreeImpl::NodeUnion), newCapacity * sizeof(BTreeImpl::NodeUnion)); if (error != 0) { KJ_FAIL_SYSCALL("posix_memalign", error); } NodeUnion* newTree = reinterpret_cast(allocPtr); #endif // Note: C11 introduces aligned_alloc() as a standard, but it's still missing on many platforms, // so we don't use it. But if you wanted to use it, you'd do this: // NodeUnion* newTree = reinterpret_cast( // aligned_alloc(sizeof(BTreeImpl::NodeUnion), newCapacity * sizeof(BTreeImpl::NodeUnion))); // KJ_ASSERT(newTree != nullptr, "memory allocation failed", newCapacity); acopy(newTree, tree, treeCapacity); azero(newTree + treeCapacity, newCapacity - treeCapacity); if (tree != &EMPTY_NODE) aligned_free(tree); tree = newTree; treeCapacity = newCapacity; } BTreeImpl::Iterator BTreeImpl::search(const SearchKey& searchKey) const { // Find the "first" row number (in sorted order) for which searchKey.isAfter(rowNumber) returns // false. uint pos = 0; for (auto i KJ_UNUSED: zeroTo(height)) { auto& parent = tree[pos].parent; pos = parent.children[searchKey.search(parent)]; } auto& leaf = tree[pos].leaf; return { tree, &leaf, searchKey.search(leaf) }; } template struct BTreeImpl::AllocResult { uint index; T& node; }; template inline BTreeImpl::AllocResult BTreeImpl::alloc() { // Allocate a new item from the freelist. Guaranteed to be zero'd except for the first member. uint i = freelistHead; NodeUnion* ptr = &tree[i]; freelistHead = i + 1 + ptr->freelist.nextOffset; --freelistSize; return { i, *ptr }; } inline void BTreeImpl::free(uint pos) { // Add the given node to the freelist. // HACK: This is typically called on a node immediately after copying its contents away, but the // pointer used to copy it away may be a different pointer pointing to a different union member // which the compiler may not recgonize as aliasing with this object. Just to be extra-safe, // insert a compiler barrier. compilerBarrier(); auto& node = tree[pos]; node.freelist.nextOffset = freelistHead - pos - 1; azero(node.freelist.zero, kj::size(node.freelist.zero)); freelistHead = pos; ++freelistSize; } BTreeImpl::Iterator BTreeImpl::insert(const SearchKey& searchKey) { // Like search() but ensures that there is room in the leaf node to insert a new row. // If we split the root node it will generate two new nodes. If we split any other node in the // path it will generate one new node. `height` doesn't count leaf nodes, but we can equivalently // think of it as not counting the root node, so in the worst case we may allocate height + 2 // new nodes. // // (Also note that if the tree is currently empty, then `tree` points to a dummy root node in // read-only memory. We definitely need to allocate a real tree node array in this case, and // we'll start out allocating space for four nodes, which will be all we need up to 28 rows.) if (freelistSize < height + 2) { if (height > 0 && !tree[0].parent.isFull() && freelistSize >= height) { // Slight optimization: The root node is not full, so we're definitely not going to split it. // That means that the maximum allocations we might do is equal to `height`, not // `height + 2`, and we have that much space, so no need to grow yet. // // This optimization is particularly important for small trees, e.g. when treeCapacity is 4 // and the tree so far consists of a root and two children, we definitely don't need to grow // the tree yet. } else { growTree(); if (freelistHead == 0) { // We have no root yet. Allocate one. KJ_ASSERT(alloc().index == 0); } } } uint pos = 0; // Track grandparent node and child index within grandparent. Parent* parent = nullptr; uint indexInParent = 0; for (auto i KJ_UNUSED: zeroTo(height)) { Parent& node = insertHelper(searchKey, tree[pos].parent, parent, indexInParent, pos); parent = &node; indexInParent = searchKey.search(node); pos = node.children[indexInParent]; } Leaf& leaf = insertHelper(searchKey, tree[pos].leaf, parent, indexInParent, pos); // Fun fact: Unlike erase(), there's no need to climb back up the tree modifying keys, because // either the newly-inserted node will not be the last in the leaf (and thus parent keys aren't // modified), or the leaf is the last leaf in the tree (and thus there's no parent key to // modify). return { tree, &leaf, searchKey.search(leaf) }; } template Node& BTreeImpl::insertHelper(const SearchKey& searchKey, Node& node, Parent* parent, uint indexInParent, uint pos) { if (node.isFull()) { // This node is full. Need to split. if (parent == nullptr) { // This is the root node. We need to split into two nodes and create a new root. auto n1 = alloc(); auto n2 = alloc(); uint pivot = split(n2.node, n2.index, node, pos); move(n1.node, n1.index, node); // Rewrite root to have the two children. tree[0].parent.initRoot(pivot, n1.index, n2.index); // Increased height. ++height; // Decide which new branch has our search key. if (searchKey.isAfter(pivot)) { // the right one return n2.node; } else { // the left one return n1.node; } } else { // This is a non-root parent node. We need to split it into two and insert the new node // into the grandparent. auto n = alloc(); uint pivot = split(n.node, n.index, node, pos); // Insert new child into grandparent. parent->insertAfter(indexInParent, pivot, n.index); // Decide which new branch has our search key. if (searchKey.isAfter(pivot)) { // the new one, which is right of the original return n.node; } else { // the original one, which is left of the new one return node; } } } else { // No split needed. return node; } } void BTreeImpl::erase(uint row, const SearchKey& searchKey) { // Erase the given row number from the tree. predicate() returns true for the given row and all // rows after it. uint pos = 0; // Track grandparent node and child index within grandparent. Parent* parent = nullptr; uint indexInParent = 0; MaybeUint* fixup = nullptr; for (auto i KJ_UNUSED: zeroTo(height)) { Parent& node = eraseHelper(tree[pos].parent, parent, indexInParent, pos, fixup); parent = &node; indexInParent = searchKey.search(node); pos = node.children[indexInParent]; if (indexInParent < kj::size(node.keys) && node.keys[indexInParent] == row) { // Oh look, the row is a key in this node! We'll need to come back and fix this up later. // Note that any particular row can only appear as *one* key value anywhere in the tree, so // we only need one fixup pointer, which is nice. MaybeUint* newFixup = &node.keys[indexInParent]; if (fixup == newFixup) { // The fixup pointer was already set while processing a parent node, and then a merge or // rotate caused it to be moved, but the fixup pointer was updated... so it's already set // to point at the slot we wanted it to point to, so nothing to see here. } else { KJ_DASSERT(fixup == nullptr); fixup = newFixup; } } } Leaf& leaf = eraseHelper(tree[pos].leaf, parent, indexInParent, pos, fixup); uint r = searchKey.search(leaf); if (leaf.rows[r] == row) { leaf.erase(r); if (fixup != nullptr) { // There's a key in a parent node that needs fixup. This is only possible if the removed // node is the last in its leaf. KJ_DASSERT(leaf.rows[r] == nullptr); KJ_DASSERT(r > 0); // non-root nodes must be at least half full so this can't be item 0 KJ_DASSERT(*fixup == row); *fixup = leaf.rows[r - 1]; } } else { logInconsistency(); } } template Node& BTreeImpl::eraseHelper( Node& node, Parent* parent, uint indexInParent, uint pos, MaybeUint*& fixup) { if (parent != nullptr && !node.isMostlyFull()) { // This is not the root, but it's only half-full. Rebalance. KJ_DASSERT(node.isHalfFull()); if (indexInParent > 0) { // There's a sibling to the left. uint sibPos = parent->children[indexInParent - 1]; Node& sib = tree[sibPos]; if (sib.isMostlyFull()) { // Left sibling is more than half full. Steal one member. rotateRight(sib, node, *parent, indexInParent - 1); return node; } else { // Left sibling is half full, too. Merge. KJ_ASSERT(sib.isHalfFull()); merge(sib, sibPos, *parent->keys[indexInParent - 1], node); parent->eraseAfter(indexInParent - 1); free(pos); if (fixup == &parent->keys[indexInParent]) --fixup; if (parent->keys[0] == nullptr) { // Oh hah, the parent has no keys left. It must be the root. We can eliminate it. KJ_DASSERT(parent == &tree->parent); compilerBarrier(); // don't reorder any writes to parent below here move(tree[0], 0, sib); free(sibPos); --height; return tree[0]; } else { return sib; } } } else if (indexInParent < Parent::NKEYS && parent->keys[indexInParent] != nullptr) { // There's a sibling to the right. uint sibPos = parent->children[indexInParent + 1]; Node& sib = tree[sibPos]; if (sib.isMostlyFull()) { // Right sibling is more than half full. Steal one member. rotateLeft(node, sib, *parent, indexInParent, fixup); return node; } else { // Right sibling is half full, too. Merge. KJ_ASSERT(sib.isHalfFull()); merge(node, pos, *parent->keys[indexInParent], sib); parent->eraseAfter(indexInParent); free(sibPos); if (fixup == &parent->keys[indexInParent]) fixup = nullptr; if (parent->keys[0] == nullptr) { // Oh hah, the parent has no keys left. It must be the root. We can eliminate it. KJ_DASSERT(parent == &tree->parent); compilerBarrier(); // don't reorder any writes to parent below here move(tree[0], 0, node); free(pos); --height; return tree[0]; } else { return node; } } } else { KJ_FAIL_ASSERT("inconsistent b-tree"); } } return node; } void BTreeImpl::renumber(uint oldRow, uint newRow, const SearchKey& searchKey) { // Renumber the given row from oldRow to newRow. predicate() returns true for oldRow and all // rows after it. (It will not be called on newRow.) uint pos = 0; for (auto i KJ_UNUSED: zeroTo(height)) { auto& node = tree[pos].parent; uint indexInParent = searchKey.search(node); pos = node.children[indexInParent]; if (node.keys[indexInParent] == oldRow) { node.keys[indexInParent] = newRow; } KJ_DASSERT(pos != 0); } auto& leaf = tree[pos].leaf; uint r = searchKey.search(leaf); if (leaf.rows[r] == oldRow) { leaf.rows[r] = newRow; } else { logInconsistency(); } } uint BTreeImpl::split(Parent& dst, uint dstPos, Parent& src, uint srcPos) { constexpr size_t mid = Parent::NKEYS / 2; uint pivot = *src.keys[mid]; acopy(dst.keys, src.keys + mid + 1, Parent::NKEYS - mid - 1); azero(src.keys + mid, Parent::NKEYS - mid); acopy(dst.children, src.children + mid + 1, Parent::NCHILDREN - mid - 1); azero(src.children + mid + 1, Parent::NCHILDREN - mid - 1); return pivot; } uint BTreeImpl::split(Leaf& dst, uint dstPos, Leaf& src, uint srcPos) { constexpr size_t mid = Leaf::NROWS / 2; uint pivot = *src.rows[mid - 1]; acopy(dst.rows, src.rows + mid, Leaf::NROWS - mid); azero(src.rows + mid, Leaf::NROWS - mid); if (src.next == 0) { endLeaf = dstPos; } else { tree[src.next].leaf.prev = dstPos; } dst.next = src.next; dst.prev = srcPos; src.next = dstPos; return pivot; } void BTreeImpl::merge(Parent& dst, uint dstPos, uint pivot, Parent& src) { // merge() is only legal if both nodes are half-empty. Meanwhile, B-tree invariants // guarantee that the node can't be more than half-empty, or we would have merged it sooner. // (The root can be more than half-empty, but it is never merged with anything.) KJ_DASSERT(src.isHalfFull()); KJ_DASSERT(dst.isHalfFull()); constexpr size_t mid = Parent::NKEYS/2; dst.keys[mid] = pivot; acopy(dst.keys + mid + 1, src.keys, mid); acopy(dst.children + mid + 1, src.children, mid + 1); } void BTreeImpl::merge(Leaf& dst, uint dstPos, uint pivot, Leaf& src) { // merge() is only legal if both nodes are half-empty. Meanwhile, B-tree invariants // guarantee that the node can't be more than half-empty, or we would have merged it sooner. // (The root can be more than half-empty, but it is never merged with anything.) KJ_DASSERT(src.isHalfFull()); KJ_DASSERT(dst.isHalfFull()); constexpr size_t mid = Leaf::NROWS/2; dst.rows[mid] = pivot; acopy(dst.rows + mid, src.rows, mid); dst.next = src.next; if (dst.next == 0) { endLeaf = dstPos; } else { tree[dst.next].leaf.prev = dstPos; } } void BTreeImpl::move(Parent& dst, uint dstPos, Parent& src) { dst = src; } void BTreeImpl::move(Leaf& dst, uint dstPos, Leaf& src) { dst = src; if (src.next == 0) { endLeaf = dstPos; } else { tree[src.next].leaf.prev = dstPos; } if (src.prev == 0) { beginLeaf = dstPos; } else { tree[src.prev].leaf.next = dstPos; } } void BTreeImpl::rotateLeft( Parent& left, Parent& right, Parent& parent, uint indexInParent, MaybeUint*& fixup) { // Steal one item from the right node and move it to the left node. // Like merge(), this is only called on an exactly-half-empty node. KJ_DASSERT(left.isHalfFull()); KJ_DASSERT(right.isMostlyFull()); constexpr size_t mid = Parent::NKEYS/2; left.keys[mid] = parent.keys[indexInParent]; if (fixup == &parent.keys[indexInParent]) fixup = &left.keys[mid]; parent.keys[indexInParent] = right.keys[0]; left.children[mid + 1] = right.children[0]; amove(right.keys, right.keys + 1, Parent::NKEYS - 1); right.keys[Parent::NKEYS - 1] = nullptr; amove(right.children, right.children + 1, Parent::NCHILDREN - 1); right.children[Parent::NCHILDREN - 1] = 0; } void BTreeImpl::rotateLeft( Leaf& left, Leaf& right, Parent& parent, uint indexInParent, MaybeUint*& fixup) { // Steal one item from the right node and move it to the left node. // Like merge(), this is only called on an exactly-half-empty node. KJ_DASSERT(left.isHalfFull()); KJ_DASSERT(right.isMostlyFull()); constexpr size_t mid = Leaf::NROWS/2; parent.keys[indexInParent] = left.rows[mid] = right.rows[0]; if (fixup == &parent.keys[indexInParent]) fixup = nullptr; amove(right.rows, right.rows + 1, Leaf::NROWS - 1); right.rows[Leaf::NROWS - 1] = nullptr; } void BTreeImpl::rotateRight(Parent& left, Parent& right, Parent& parent, uint indexInParent) { // Steal one item from the left node and move it to the right node. // Like merge(), this is only called on an exactly-half-empty node. KJ_DASSERT(right.isHalfFull()); KJ_DASSERT(left.isMostlyFull()); constexpr size_t mid = Parent::NKEYS/2; amove(right.keys + 1, right.keys, mid); amove(right.children + 1, right.children, mid + 1); uint back = left.keyCount() - 1; right.keys[0] = parent.keys[indexInParent]; parent.keys[indexInParent] = left.keys[back]; right.children[0] = left.children[back + 1]; left.keys[back] = nullptr; left.children[back + 1] = 0; } void BTreeImpl::rotateRight(Leaf& left, Leaf& right, Parent& parent, uint indexInParent) { // Steal one item from the left node and move it to the right node. // Like mergeFrom(), this is only called on an exactly-half-empty node. KJ_DASSERT(right.isHalfFull()); KJ_DASSERT(left.isMostlyFull()); constexpr size_t mid = Leaf::NROWS/2; amove(right.rows + 1, right.rows, mid); uint back = left.size() - 1; right.rows[0] = left.rows[back]; parent.keys[indexInParent] = left.rows[back - 1]; left.rows[back] = nullptr; } void BTreeImpl::Parent::initRoot(uint key, uint leftChild, uint rightChild) { // HACK: This is typically called on the root node immediately after copying its contents away, // but the pointer used to copy it away may be a different pointer pointing to a different // union member which the compiler may not recgonize as aliasing with this object. Just to // be extra-safe, insert a compiler barrier. compilerBarrier(); keys[0] = key; children[0] = leftChild; children[1] = rightChild; azero(keys + 1, Parent::NKEYS - 1); azero(children + 2, Parent::NCHILDREN - 2); } void BTreeImpl::Parent::insertAfter(uint i, uint splitKey, uint child) { KJ_IREQUIRE(children[Parent::NCHILDREN - 1] == 0); // check not full amove(keys + i + 1, keys + i, Parent::NKEYS - (i + 1)); keys[i] = splitKey; amove(children + i + 2, children + i + 1, Parent::NCHILDREN - (i + 2)); children[i + 1] = child; } void BTreeImpl::Parent::eraseAfter(uint i) { amove(keys + i, keys + i + 1, Parent::NKEYS - (i + 1)); keys[Parent::NKEYS - 1] = nullptr; amove(children + i + 1, children + i + 2, Parent::NCHILDREN - (i + 2)); children[Parent::NCHILDREN - 1] = 0; } } // namespace _ // ======================================================================================= // Insertion order const InsertionOrderIndex::Link InsertionOrderIndex::EMPTY_LINK = { 0, 0 }; InsertionOrderIndex::InsertionOrderIndex(): capacity(0), links(const_cast(&EMPTY_LINK)) {} InsertionOrderIndex::InsertionOrderIndex(InsertionOrderIndex&& other) : capacity(other.capacity), links(other.links) { other.capacity = 0; other.links = const_cast(&EMPTY_LINK); } InsertionOrderIndex& InsertionOrderIndex::operator=(InsertionOrderIndex&& other) { KJ_DASSERT(&other != this); capacity = other.capacity; links = other.links; other.capacity = 0; other.links = const_cast(&EMPTY_LINK); return *this; } InsertionOrderIndex::~InsertionOrderIndex() noexcept(false) { if (links != &EMPTY_LINK) delete[] links; } void InsertionOrderIndex::reserve(size_t size) { KJ_ASSERT(size < (1u << 31), "Table too big for InsertionOrderIndex"); if (size > capacity) { // Need to grow. // Note that `size` and `capacity` do not include the special link[0]. // Round up to the next power of 2. size_t allocation = 1u << (_::lg(size) + 1); KJ_DASSERT(allocation > size); KJ_DASSERT(allocation <= size * 2); // Round first allocation up to 8. allocation = kj::max(allocation, 8); Link* newLinks = new Link[allocation]; #ifdef KJ_DEBUG // To catch bugs, fill unused links with 0xff. memset(newLinks, 0xff, allocation * sizeof(Link)); #endif _::acopy(newLinks, links, capacity + 1); if (links != &EMPTY_LINK) delete[] links; links = newLinks; capacity = allocation - 1; } } void InsertionOrderIndex::clear() { links[0] = Link { 0, 0 }; #ifdef KJ_DEBUG // To catch bugs, fill unused links with 0xff. memset(links + 1, 0xff, capacity * sizeof(Link)); #endif } kj::Maybe InsertionOrderIndex::insertImpl(size_t pos) { if (pos >= capacity) { reserve(pos + 1); } links[pos + 1].prev = links[0].prev; links[pos + 1].next = 0; links[links[0].prev].next = pos + 1; links[0].prev = pos + 1; return nullptr; } void InsertionOrderIndex::eraseImpl(size_t pos) { Link& link = links[pos + 1]; links[link.next].prev = link.prev; links[link.prev].next = link.next; #ifdef KJ_DEBUG memset(&link, 0xff, sizeof(Link)); #endif } void InsertionOrderIndex::moveImpl(size_t oldPos, size_t newPos) { Link& link = links[oldPos + 1]; Link& newLink = links[newPos + 1]; newLink = link; KJ_DASSERT(links[link.next].prev == oldPos + 1); KJ_DASSERT(links[link.prev].next == oldPos + 1); links[link.next].prev = newPos + 1; links[link.prev].next = newPos + 1; #ifdef KJ_DEBUG memset(&link, 0xff, sizeof(Link)); #endif } } // namespace kj capnproto-c++-0.8.0/src/kj/encoding.c++0000644000175000017500000007600613650101756020240 0ustar00kentonkenton00000000000000// Copyright (c) 2017 Cloudflare, Inc.; Sandstorm Development Group, Inc.; and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "encoding.h" #include "vector.h" #include "debug.h" namespace kj { namespace { #define GOTO_ERROR_IF(cond) if (KJ_UNLIKELY(cond)) goto error inline void addChar32(Vector& vec, char32_t u) { // Encode as surrogate pair. u -= 0x10000; vec.add(0xd800 | (u >> 10)); vec.add(0xdc00 | (u & 0x03ff)); } inline void addChar32(Vector& vec, char32_t u) { vec.add(u); } template EncodingResult> encodeUtf(ArrayPtr text, bool nulTerminate) { Vector result(text.size() + nulTerminate); bool hadErrors = false; size_t i = 0; while (i < text.size()) { byte c = text[i++]; if (c < 0x80) { // 0xxxxxxx -- ASCII result.add(c); continue; } else if (KJ_UNLIKELY(c < 0xc0)) { // 10xxxxxx -- malformed continuation byte goto error; } else if (c < 0xe0) { // 110xxxxx -- 2-byte byte c2; GOTO_ERROR_IF(i == text.size() || ((c2 = text[i]) & 0xc0) != 0x80); ++i; char16_t u = (static_cast(c & 0x1f) << 6) | (static_cast(c2 & 0x3f) ); // Disallow overlong sequence. GOTO_ERROR_IF(u < 0x80); result.add(u); continue; } else if (c < 0xf0) { // 1110xxxx -- 3-byte byte c2, c3; GOTO_ERROR_IF(i == text.size() || ((c2 = text[i]) & 0xc0) != 0x80); ++i; GOTO_ERROR_IF(i == text.size() || ((c3 = text[i]) & 0xc0) != 0x80); ++i; char16_t u = (static_cast(c & 0x0f) << 12) | (static_cast(c2 & 0x3f) << 6) | (static_cast(c3 & 0x3f) ); // Disallow overlong sequence. GOTO_ERROR_IF(u < 0x0800); // Flag surrogate pair code points as errors, but allow them through. if (KJ_UNLIKELY((u & 0xf800) == 0xd800)) { if (result.size() > 0 && (u & 0xfc00) == 0xdc00 && (result.back() & 0xfc00) == 0xd800) { // Whoops, the *previous* character was also an invalid surrogate, and if we add this // one too, they'll form a valid surrogate pair. If we allowed this, then it would mean // invalid UTF-8 round-tripped to UTF-16 and back could actually change meaning entirely. // OTOH, the reason we allow dangling surrogates is to allow invalid UTF-16 to round-trip // to UTF-8 without loss, but if the original UTF-16 had a valid surrogate pair, it would // have been encoded as a valid single UTF-8 codepoint, not as separate UTF-8 codepoints // for each surrogate. goto error; } hadErrors = true; } result.add(u); continue; } else if (c < 0xf8) { // 11110xxx -- 4-byte byte c2, c3, c4; GOTO_ERROR_IF(i == text.size() || ((c2 = text[i]) & 0xc0) != 0x80); ++i; GOTO_ERROR_IF(i == text.size() || ((c3 = text[i]) & 0xc0) != 0x80); ++i; GOTO_ERROR_IF(i == text.size() || ((c4 = text[i]) & 0xc0) != 0x80); ++i; char32_t u = (static_cast(c & 0x07) << 18) | (static_cast(c2 & 0x3f) << 12) | (static_cast(c3 & 0x3f) << 6) | (static_cast(c4 & 0x3f) ); // Disallow overlong sequence. GOTO_ERROR_IF(u < 0x10000); // Unicode ends at U+10FFFF GOTO_ERROR_IF(u >= 0x110000); addChar32(result, u); continue; } else { // 5-byte and 6-byte sequences are not legal as they'd result in codepoints outside the // range of Unicode. goto error; } error: result.add(0xfffd); hadErrors = true; // Ignore all continuation bytes. while (i < text.size() && (text[i] & 0xc0) == 0x80) { ++i; } } if (nulTerminate) result.add(0); return { result.releaseAsArray(), hadErrors }; } } // namespace EncodingResult> encodeUtf16(ArrayPtr text, bool nulTerminate) { return encodeUtf(text, nulTerminate); } EncodingResult> encodeUtf32(ArrayPtr text, bool nulTerminate) { return encodeUtf(text, nulTerminate); } EncodingResult decodeUtf16(ArrayPtr utf16) { Vector result(utf16.size() + 1); bool hadErrors = false; size_t i = 0; while (i < utf16.size()) { char16_t u = utf16[i++]; if (u < 0x80) { result.add(u); continue; } else if (u < 0x0800) { result.addAll>({ static_cast(((u >> 6) ) | 0xc0), static_cast(((u ) & 0x3f) | 0x80) }); continue; } else if ((u & 0xf800) == 0xd800) { // surrogate pair char16_t u2; if (KJ_UNLIKELY(i == utf16.size() // missing second half || (u & 0x0400) != 0 // first half in wrong range || ((u2 = utf16[i]) & 0xfc00) != 0xdc00)) { // second half in wrong range hadErrors = true; goto threeByte; } ++i; char32_t u32 = (((u & 0x03ff) << 10) | (u2 & 0x03ff)) + 0x10000; result.addAll>({ static_cast(((u32 >> 18) ) | 0xf0), static_cast(((u32 >> 12) & 0x3f) | 0x80), static_cast(((u32 >> 6) & 0x3f) | 0x80), static_cast(((u32 ) & 0x3f) | 0x80) }); continue; } else { threeByte: result.addAll>({ static_cast(((u >> 12) ) | 0xe0), static_cast(((u >> 6) & 0x3f) | 0x80), static_cast(((u ) & 0x3f) | 0x80) }); continue; } } result.add(0); return { String(result.releaseAsArray()), hadErrors }; } EncodingResult decodeUtf32(ArrayPtr utf16) { Vector result(utf16.size() + 1); bool hadErrors = false; size_t i = 0; while (i < utf16.size()) { char32_t u = utf16[i++]; if (u < 0x80) { result.add(u); continue; } else if (u < 0x0800) { result.addAll>({ static_cast(((u >> 6) ) | 0xc0), static_cast(((u ) & 0x3f) | 0x80) }); continue; } else if (u < 0x10000) { if (KJ_UNLIKELY((u & 0xfffff800) == 0xd800)) { // no surrogates allowed in utf-32 hadErrors = true; } result.addAll>({ static_cast(((u >> 12) ) | 0xe0), static_cast(((u >> 6) & 0x3f) | 0x80), static_cast(((u ) & 0x3f) | 0x80) }); continue; } else { GOTO_ERROR_IF(u >= 0x110000); // outside Unicode range result.addAll>({ static_cast(((u >> 18) ) | 0xf0), static_cast(((u >> 12) & 0x3f) | 0x80), static_cast(((u >> 6) & 0x3f) | 0x80), static_cast(((u ) & 0x3f) | 0x80) }); continue; } error: result.addAll(StringPtr(u8"\ufffd")); hadErrors = true; } result.add(0); return { String(result.releaseAsArray()), hadErrors }; } namespace { #if __GNUC__ >= 8 && !__clang__ // GCC 8's new class-memaccess warning rightly dislikes the following hacks, but we're really sure // we want to allow them so disable the warning. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif template Array coerceTo(Array&& array) { static_assert(sizeof(To) == sizeof(From), "incompatible coercion"); Array result; memcpy(&result, &array, sizeof(array)); memset(&array, 0, sizeof(array)); return result; } template ArrayPtr coerceTo(ArrayPtr array) { static_assert(sizeof(To) == sizeof(From), "incompatible coercion"); return arrayPtr(reinterpret_cast(array.begin()), array.size()); } template EncodingResult> coerceTo(EncodingResult>&& result) { return { coerceTo(Array(kj::mv(result))), result.hadErrors }; } #if __GNUC__ >= 8 && !__clang__ #pragma GCC diagnostic pop #endif template struct WideConverter; template <> struct WideConverter { typedef char Type; static EncodingResult> encode(ArrayPtr text, bool nulTerminate) { auto result = heapArray(text.size() + nulTerminate); memcpy(result.begin(), text.begin(), text.size()); if (nulTerminate) result.back() = 0; return { kj::mv(result), false }; } static EncodingResult decode(ArrayPtr text) { return { kj::heapString(text), false }; } }; template <> struct WideConverter { typedef char16_t Type; static inline EncodingResult> encode( ArrayPtr text, bool nulTerminate) { return encodeUtf16(text, nulTerminate); } static inline EncodingResult decode(ArrayPtr text) { return decodeUtf16(text); } }; template <> struct WideConverter { typedef char32_t Type; static inline EncodingResult> encode( ArrayPtr text, bool nulTerminate) { return encodeUtf32(text, nulTerminate); } static inline EncodingResult decode(ArrayPtr text) { return decodeUtf32(text); } }; } // namespace EncodingResult> encodeWideString(ArrayPtr text, bool nulTerminate) { return coerceTo(WideConverter::encode(text, nulTerminate)); } EncodingResult decodeWideString(ArrayPtr wide) { using Converter = WideConverter; return Converter::decode(coerceTo(wide)); } // ======================================================================================= namespace { const char HEX_DIGITS[] = "0123456789abcdef"; // Maps integer in the range [0,16) to a hex digit. const char HEX_DIGITS_URI[] = "0123456789ABCDEF"; // RFC 3986 section 2.1 says "For consistency, URI producers and normalizers should use uppercase // hexadecimal digits for all percent-encodings. static Maybe tryFromHexDigit(char c) { if ('0' <= c && c <= '9') { return c - '0'; } else if ('a' <= c && c <= 'f') { return c - ('a' - 10); } else if ('A' <= c && c <= 'F') { return c - ('A' - 10); } else { return nullptr; } } static Maybe tryFromOctDigit(char c) { if ('0' <= c && c <= '7') { return c - '0'; } else { return nullptr; } } } // namespace String encodeHex(ArrayPtr input) { return strArray(KJ_MAP(b, input) { return heapArray({HEX_DIGITS[b/16], HEX_DIGITS[b%16]}); }, ""); } EncodingResult> decodeHex(ArrayPtr text) { auto result = heapArray(text.size() / 2); bool hadErrors = text.size() % 2; for (auto i: kj::indices(result)) { byte b = 0; KJ_IF_MAYBE(d1, tryFromHexDigit(text[i*2])) { b = *d1 << 4; } else { hadErrors = true; } KJ_IF_MAYBE(d2, tryFromHexDigit(text[i*2+1])) { b |= *d2; } else { hadErrors = true; } result[i] = b; } return { kj::mv(result), hadErrors }; } String encodeUriComponent(ArrayPtr bytes) { Vector result(bytes.size() + 1); for (byte b: bytes) { if (('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') || ('0' <= b && b <= '9') || b == '-' || b == '_' || b == '.' || b == '!' || b == '~' || b == '*' || b == '\'' || b == '(' || b == ')') { result.add(b); } else { result.add('%'); result.add(HEX_DIGITS_URI[b/16]); result.add(HEX_DIGITS_URI[b%16]); } } result.add('\0'); return String(result.releaseAsArray()); } String encodeUriFragment(ArrayPtr bytes) { Vector result(bytes.size() + 1); for (byte b: bytes) { if (('?' <= b && b <= '_') || // covers A-Z ('a' <= b && b <= '~') || // covers a-z ('&' <= b && b <= ';') || // covers 0-9 b == '!' || b == '=' || b == '#' || b == '$') { result.add(b); } else { result.add('%'); result.add(HEX_DIGITS_URI[b/16]); result.add(HEX_DIGITS_URI[b%16]); } } result.add('\0'); return String(result.releaseAsArray()); } String encodeUriPath(ArrayPtr bytes) { Vector result(bytes.size() + 1); for (byte b: bytes) { if (('@' <= b && b <= '[') || // covers A-Z ('a' <= b && b <= 'z') || ('0' <= b && b <= ';') || // covers 0-9 ('&' <= b && b <= '.') || b == '_' || b == '!' || b == '=' || b == ']' || b == '^' || b == '|' || b == '~' || b == '$') { result.add(b); } else { result.add('%'); result.add(HEX_DIGITS_URI[b/16]); result.add(HEX_DIGITS_URI[b%16]); } } result.add('\0'); return String(result.releaseAsArray()); } String encodeUriUserInfo(ArrayPtr bytes) { Vector result(bytes.size() + 1); for (byte b: bytes) { if (('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') || ('0' <= b && b <= '9') || ('&' <= b && b <= '.') || b == '_' || b == '!' || b == '~' || b == '$') { result.add(b); } else { result.add('%'); result.add(HEX_DIGITS_URI[b/16]); result.add(HEX_DIGITS_URI[b%16]); } } result.add('\0'); return String(result.releaseAsArray()); } String encodeWwwForm(ArrayPtr bytes) { Vector result(bytes.size() + 1); for (byte b: bytes) { if (('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z') || ('0' <= b && b <= '9') || b == '-' || b == '_' || b == '.' || b == '*') { result.add(b); } else if (b == ' ') { result.add('+'); } else { result.add('%'); result.add(HEX_DIGITS_URI[b/16]); result.add(HEX_DIGITS_URI[b%16]); } } result.add('\0'); return String(result.releaseAsArray()); } EncodingResult> decodeBinaryUriComponent( ArrayPtr text, DecodeUriOptions options) { Vector result(text.size() + options.nulTerminate); bool hadErrors = false; const char* ptr = text.begin(); const char* end = text.end(); while (ptr < end) { if (*ptr == '%') { ++ptr; if (ptr == end) { hadErrors = true; } else KJ_IF_MAYBE(d1, tryFromHexDigit(*ptr)) { byte b = *d1; ++ptr; if (ptr == end) { hadErrors = true; } else KJ_IF_MAYBE(d2, tryFromHexDigit(*ptr)) { b = (b << 4) | *d2; ++ptr; } else { hadErrors = true; } result.add(b); } else { hadErrors = true; } } else if (options.plusToSpace && *ptr == '+') { ++ptr; result.add(' '); } else { result.add(*ptr++); } } if (options.nulTerminate) result.add(0); return { result.releaseAsArray(), hadErrors }; } // ======================================================================================= String encodeCEscape(ArrayPtr bytes) { Vector escaped(bytes.size()); for (byte b: bytes) { switch (b) { case '\a': escaped.addAll(StringPtr("\\a")); break; case '\b': escaped.addAll(StringPtr("\\b")); break; case '\f': escaped.addAll(StringPtr("\\f")); break; case '\n': escaped.addAll(StringPtr("\\n")); break; case '\r': escaped.addAll(StringPtr("\\r")); break; case '\t': escaped.addAll(StringPtr("\\t")); break; case '\v': escaped.addAll(StringPtr("\\v")); break; case '\'': escaped.addAll(StringPtr("\\\'")); break; case '\"': escaped.addAll(StringPtr("\\\"")); break; case '\\': escaped.addAll(StringPtr("\\\\")); break; default: if (b < 0x20 || b == 0x7f) { // Use octal escape, not hex, because hex escapes technically have no length limit and // so can create ambiguity with subsequent characters. escaped.add('\\'); escaped.add(HEX_DIGITS[b / 64]); escaped.add(HEX_DIGITS[(b / 8) % 8]); escaped.add(HEX_DIGITS[b % 8]); } else { escaped.add(b); } break; } } escaped.add(0); return String(escaped.releaseAsArray()); } EncodingResult> decodeBinaryCEscape(ArrayPtr text, bool nulTerminate) { Vector result(text.size() + nulTerminate); bool hadErrors = false; size_t i = 0; while (i < text.size()) { char c = text[i++]; if (c == '\\') { if (i == text.size()) { hadErrors = true; continue; } char c2 = text[i++]; switch (c2) { case 'a' : result.add('\a'); break; case 'b' : result.add('\b'); break; case 'f' : result.add('\f'); break; case 'n' : result.add('\n'); break; case 'r' : result.add('\r'); break; case 't' : result.add('\t'); break; case 'v' : result.add('\v'); break; case '\'': result.add('\''); break; case '\"': result.add('\"'); break; case '\\': result.add('\\'); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': { uint value = c2 - '0'; for (uint j = 0; j < 2 && i < text.size(); j++) { KJ_IF_MAYBE(d, tryFromOctDigit(text[i])) { ++i; value = (value << 3) | *d; } else { break; } } if (value >= 0x100) hadErrors = true; result.add(value); break; } case 'x': { uint value = 0; while (i < text.size()) { KJ_IF_MAYBE(d, tryFromHexDigit(text[i])) { ++i; value = (value << 4) | *d; } else { break; } } if (value >= 0x100) hadErrors = true; result.add(value); break; } case 'u': { char16_t value = 0; for (uint j = 0; j < 4; j++) { if (i == text.size()) { hadErrors = true; break; } else KJ_IF_MAYBE(d, tryFromHexDigit(text[i])) { ++i; value = (value << 4) | *d; } else { hadErrors = true; break; } } auto utf = decodeUtf16(arrayPtr(&value, 1)); if (utf.hadErrors) hadErrors = true; result.addAll(utf.asBytes()); break; } case 'U': { char32_t value = 0; for (uint j = 0; j < 8; j++) { if (i == text.size()) { hadErrors = true; break; } else KJ_IF_MAYBE(d, tryFromHexDigit(text[i])) { ++i; value = (value << 4) | *d; } else { hadErrors = true; break; } } auto utf = decodeUtf32(arrayPtr(&value, 1)); if (utf.hadErrors) hadErrors = true; result.addAll(utf.asBytes()); break; } default: result.add(c2); } } else { result.add(c); } } if (nulTerminate) result.add(0); return { result.releaseAsArray(), hadErrors }; } // ======================================================================================= // This code is derived from libb64 which has been placed in the public domain. // For details, see http://sourceforge.net/projects/libb64 // ------------------------------------------------------------------- // Encoder namespace { typedef enum { step_A, step_B, step_C } base64_encodestep; typedef struct { base64_encodestep step; char result; int stepcount; } base64_encodestate; const int CHARS_PER_LINE = 72; void base64_init_encodestate(base64_encodestate* state_in) { state_in->step = step_A; state_in->result = 0; state_in->stepcount = 0; } char base64_encode_value(char value_in) { static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; if (value_in > 63) return '='; return encoding[(int)value_in]; } int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in, bool breakLines) { const char* plainchar = plaintext_in; const char* const plaintextend = plaintext_in + length_in; char* codechar = code_out; char result; char fragment; result = state_in->result; switch (state_in->step) { while (1) { case step_A: if (plainchar == plaintextend) { state_in->result = result; state_in->step = step_A; return codechar - code_out; } fragment = *plainchar++; result = (fragment & 0x0fc) >> 2; *codechar++ = base64_encode_value(result); result = (fragment & 0x003) << 4; // fallthrough case step_B: if (plainchar == plaintextend) { state_in->result = result; state_in->step = step_B; return codechar - code_out; } fragment = *plainchar++; result |= (fragment & 0x0f0) >> 4; *codechar++ = base64_encode_value(result); result = (fragment & 0x00f) << 2; // fallthrough case step_C: if (plainchar == plaintextend) { state_in->result = result; state_in->step = step_C; return codechar - code_out; } fragment = *plainchar++; result |= (fragment & 0x0c0) >> 6; *codechar++ = base64_encode_value(result); result = (fragment & 0x03f) >> 0; *codechar++ = base64_encode_value(result); ++(state_in->stepcount); if (breakLines && state_in->stepcount == CHARS_PER_LINE/4) { *codechar++ = '\n'; state_in->stepcount = 0; } } } /* control should not reach here */ return codechar - code_out; } int base64_encode_blockend(char* code_out, base64_encodestate* state_in, bool breakLines) { char* codechar = code_out; switch (state_in->step) { case step_B: *codechar++ = base64_encode_value(state_in->result); *codechar++ = '='; *codechar++ = '='; ++state_in->stepcount; break; case step_C: *codechar++ = base64_encode_value(state_in->result); *codechar++ = '='; ++state_in->stepcount; break; case step_A: break; } if (breakLines && state_in->stepcount > 0) { *codechar++ = '\n'; } return codechar - code_out; } } // namespace String encodeBase64(ArrayPtr input, bool breakLines) { /* set up a destination buffer large enough to hold the encoded data */ // equivalent to ceil(input.size() / 3) * 4 auto numChars = (input.size() + 2) / 3 * 4; if (breakLines) { // Add space for newline characters. uint lineCount = numChars / CHARS_PER_LINE; if (numChars % CHARS_PER_LINE > 0) { // Partial line. ++lineCount; } numChars = numChars + lineCount; } auto output = heapString(numChars); /* keep track of our encoded position */ char* c = output.begin(); /* store the number of bytes encoded by a single call */ int cnt = 0; size_t total = 0; /* we need an encoder state */ base64_encodestate s; /*---------- START ENCODING ----------*/ /* initialise the encoder state */ base64_init_encodestate(&s); /* gather data from the input and send it to the output */ cnt = base64_encode_block((const char *)input.begin(), input.size(), c, &s, breakLines); c += cnt; total += cnt; /* since we have encoded the entire input string, we know that there is no more input data; finalise the encoding */ cnt = base64_encode_blockend(c, &s, breakLines); c += cnt; total += cnt; /*---------- STOP ENCODING ----------*/ KJ_ASSERT(total == output.size(), total, output.size()); return output; } // ------------------------------------------------------------------- // Decoder namespace { typedef enum { step_a, step_b, step_c, step_d } base64_decodestep; typedef struct { bool hadErrors = false; size_t nPaddingBytesSeen = 0; // Output state. `nPaddingBytesSeen` is not guaranteed to be correct if `hadErrors` is true. It is // included in the state purely to preserve the streaming capability of the algorithm while still // checking for errors correctly (consider chunk 1 = "abc=", chunk 2 = "d"). base64_decodestep step = step_a; char plainchar = 0; } base64_decodestate; int base64_decode_value(char value_in) { // Returns either the fragment value or: -1 on whitespace, -2 on padding, -3 on invalid input. // // Note that the original libb64 implementation used -1 for invalid input, -2 on padding -- this // new scheme allows for some simpler error checks in steps A and B. static const signed char decoding[] = { -3,-3,-3,-3,-3,-3,-3,-3, -3,-1,-1,-3,-1,-1,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -1,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,62,-3,-3,-3,63, 52,53,54,55,56,57,58,59, 60,61,-3,-3,-3,-2,-3,-3, -3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, 15,16,17,18,19,20,21,22, 23,24,25,-3,-3,-3,-3,-3, -3,26,27,28,29,30,31,32, 33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48, 49,50,51,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, -3,-3,-3,-3,-3,-3,-3,-3, }; static_assert(sizeof(decoding) == 256, "base64 decoding table size error"); return decoding[(unsigned char)value_in]; } int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in) { const char* codechar = code_in; char* plainchar = plaintext_out; signed char fragment; if (state_in->step != step_a) { *plainchar = state_in->plainchar; } #define ERROR_IF(predicate) state_in->hadErrors = state_in->hadErrors || (predicate) switch (state_in->step) { while (1) { case step_a: do { if (codechar == code_in+length_in) { state_in->step = step_a; state_in->plainchar = '\0'; return plainchar - plaintext_out; } fragment = (signed char)base64_decode_value(*codechar++); // It is an error to see invalid or padding bytes in step A. ERROR_IF(fragment < -1); } while (fragment < 0); *plainchar = (fragment & 0x03f) << 2; // fallthrough case step_b: do { if (codechar == code_in+length_in) { state_in->step = step_b; state_in->plainchar = *plainchar; // It is always an error to suspend from step B, because we don't have enough bits yet. // TODO(someday): This actually breaks the streaming use case, if base64_decode_block() is // to be called multiple times. We'll fix it if we ever care to support streaming. state_in->hadErrors = true; return plainchar - plaintext_out; } fragment = (signed char)base64_decode_value(*codechar++); // It is an error to see invalid or padding bytes in step B. ERROR_IF(fragment < -1); } while (fragment < 0); *plainchar++ |= (fragment & 0x030) >> 4; *plainchar = (fragment & 0x00f) << 4; // fallthrough case step_c: do { if (codechar == code_in+length_in) { state_in->step = step_c; state_in->plainchar = *plainchar; // It is an error to complete from step C if we have seen incomplete padding. // TODO(someday): This actually breaks the streaming use case, if base64_decode_block() is // to be called multiple times. We'll fix it if we ever care to support streaming. ERROR_IF(state_in->nPaddingBytesSeen == 1); return plainchar - plaintext_out; } fragment = (signed char)base64_decode_value(*codechar++); // It is an error to see invalid bytes or more than two padding bytes in step C. ERROR_IF(fragment < -2 || (fragment == -2 && ++state_in->nPaddingBytesSeen > 2)); } while (fragment < 0); // It is an error to continue from step C after having seen any padding. ERROR_IF(state_in->nPaddingBytesSeen > 0); *plainchar++ |= (fragment & 0x03c) >> 2; *plainchar = (fragment & 0x003) << 6; // fallthrough case step_d: do { if (codechar == code_in+length_in) { state_in->step = step_d; state_in->plainchar = *plainchar; return plainchar - plaintext_out; } fragment = (signed char)base64_decode_value(*codechar++); // It is an error to see invalid bytes or more than one padding byte in step D. ERROR_IF(fragment < -2 || (fragment == -2 && ++state_in->nPaddingBytesSeen > 1)); } while (fragment < 0); // It is an error to continue from step D after having seen padding bytes. ERROR_IF(state_in->nPaddingBytesSeen > 0); *plainchar++ |= (fragment & 0x03f); } } #undef ERROR_IF /* control should not reach here */ return plainchar - plaintext_out; } } // namespace EncodingResult> decodeBase64(ArrayPtr input) { base64_decodestate state; auto output = heapArray((input.size() * 6 + 7) / 8); size_t n = base64_decode_block(input.begin(), input.size(), reinterpret_cast(output.begin()), &state); if (n < output.size()) { auto copy = heapArray(n); memcpy(copy.begin(), output.begin(), n); output = kj::mv(copy); } return EncodingResult>(kj::mv(output), state.hadErrors); } String encodeBase64Url(ArrayPtr bytes) { // TODO(perf): Rewrite as single pass? // TODO(someday): Write decoder? auto base64 = kj::encodeBase64(bytes); for (char& c: base64) { if (c == '+') c = '-'; if (c == '/') c = '_'; } // Remove trailing '='s. kj::ArrayPtr slice = base64; while (slice.size() > 0 && slice.back() == '=') { slice = slice.slice(0, slice.size() - 1); } return kj::str(slice); } } // namespace kj capnproto-c++-0.8.0/src/kj/exception.c++0000644000175000017500000011670113650101756020445 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #if (_WIN32 && _M_X64) || (__CYGWIN__ && __x86_64__) // Currently the Win32 stack-trace code only supports x86_64. We could easily extend it to support // i386 as well but it requires some code changes around how we read the context to start the // trace. #define KJ_USE_WIN32_DBGHELP 1 #endif #include "exception.h" #include "string.h" #include "debug.h" #include "threadlocal.h" #include "miniposix.h" #include "function.h" #include #include #include #include #include #ifndef _WIN32 #include #endif #include "io.h" #if !KJ_NO_RTTI #include #endif #if __GNUC__ #include #endif #if (__linux__ && __GLIBC__ && !__UCLIBC__) || __APPLE__ #define KJ_HAS_BACKTRACE 1 #include #endif #if _WIN32 || __CYGWIN__ #define WIN32_LEAN_AND_MEAN #include #include "windows-sanity.h" #include #endif #if (__linux__ || __APPLE__ || __CYGWIN__) #include #include #endif #if __CYGWIN__ #include #include #endif #if KJ_HAS_LIBDL #include "dlfcn.h" #endif namespace kj { StringPtr KJ_STRINGIFY(LogSeverity severity) { static const char* SEVERITY_STRINGS[] = { "info", "warning", "error", "fatal", "debug" }; return SEVERITY_STRINGS[static_cast(severity)]; } #if KJ_USE_WIN32_DBGHELP namespace { struct Dbghelp { // Load dbghelp.dll dynamically since we don't really need it, it's just for debugging. HINSTANCE lib; BOOL (WINAPI *symInitialize)(HANDLE hProcess,PCSTR UserSearchPath,BOOL fInvadeProcess); BOOL (WINAPI *stackWalk64)( DWORD MachineType,HANDLE hProcess,HANDLE hThread, LPSTACKFRAME64 StackFrame,PVOID ContextRecord, PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine, PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine, PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine, PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress); PVOID (WINAPI *symFunctionTableAccess64)(HANDLE hProcess,DWORD64 AddrBase); DWORD64 (WINAPI *symGetModuleBase64)(HANDLE hProcess,DWORD64 qwAddr); BOOL (WINAPI *symGetLineFromAddr64)( HANDLE hProcess,DWORD64 qwAddr,PDWORD pdwDisplacement,PIMAGEHLP_LINE64 Line64); #if __GNUC__ && !__clang__ && __GNUC__ >= 8 // GCC 8 warns that our reinterpret_casts of function pointers below are casting between // incompatible types. Yes, GCC, we know that. This is the nature of GetProcAddress(); it returns // everything as `long long int (*)()` and we have to cast to the actual type. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-function-type" #endif Dbghelp() : lib(LoadLibraryA("dbghelp.dll")), symInitialize(lib == nullptr ? nullptr : reinterpret_cast( GetProcAddress(lib, "SymInitialize"))), stackWalk64(symInitialize == nullptr ? nullptr : reinterpret_cast( GetProcAddress(lib, "StackWalk64"))), symFunctionTableAccess64(symInitialize == nullptr ? nullptr : reinterpret_cast( GetProcAddress(lib, "SymFunctionTableAccess64"))), symGetModuleBase64(symInitialize == nullptr ? nullptr : reinterpret_cast( GetProcAddress(lib, "SymGetModuleBase64"))), symGetLineFromAddr64(symInitialize == nullptr ? nullptr : reinterpret_cast( GetProcAddress(lib, "SymGetLineFromAddr64"))) { if (symInitialize != nullptr) { symInitialize(GetCurrentProcess(), NULL, TRUE); } } #if __GNUC__ && !__clang__ && __GNUC__ >= 9 #pragma GCC diagnostic pop #endif }; const Dbghelp& getDbghelp() { static Dbghelp dbghelp; return dbghelp; } ArrayPtr getStackTrace(ArrayPtr space, uint ignoreCount, HANDLE thread, CONTEXT& context) { // NOTE: Apparently there is a function CaptureStackBackTrace() that is equivalent to glibc's // backtrace(). Somehow I missed that when I originally wrote this. However, // CaptureStackBackTrace() does not accept a CONTEXT parameter; it can only trace the caller. // That's more problematic on Windows where breakHandler(), sehHandler(), and Cygwin signal // handlers all depend on the ability to pass a CONTEXT. So we'll keep this code, which works // after all. const Dbghelp& dbghelp = getDbghelp(); if (dbghelp.stackWalk64 == nullptr || dbghelp.symFunctionTableAccess64 == nullptr || dbghelp.symGetModuleBase64 == nullptr) { return nullptr; } STACKFRAME64 frame; memset(&frame, 0, sizeof(frame)); frame.AddrPC.Offset = context.Rip; frame.AddrPC.Mode = AddrModeFlat; frame.AddrStack.Offset = context.Rsp; frame.AddrStack.Mode = AddrModeFlat; frame.AddrFrame.Offset = context.Rbp; frame.AddrFrame.Mode = AddrModeFlat; HANDLE process = GetCurrentProcess(); uint count = 0; for (; count < space.size(); count++) { if (!dbghelp.stackWalk64(IMAGE_FILE_MACHINE_AMD64, process, thread, &frame, &context, NULL, dbghelp.symFunctionTableAccess64, dbghelp.symGetModuleBase64, NULL)){ break; } // Subtract 1 from each address so that we identify the calling instructions, rather than the // return addresses (which are typically the instruction after the call). space[count] = reinterpret_cast(frame.AddrPC.Offset - 1); } return space.slice(kj::min(ignoreCount, count), count); } } // namespace #endif ArrayPtr getStackTrace(ArrayPtr space, uint ignoreCount) { if (getExceptionCallback().stackTraceMode() == ExceptionCallback::StackTraceMode::NONE) { return nullptr; } #if KJ_USE_WIN32_DBGHELP CONTEXT context; RtlCaptureContext(&context); return getStackTrace(space, ignoreCount, GetCurrentThread(), context); #elif KJ_HAS_BACKTRACE size_t size = backtrace(space.begin(), space.size()); for (auto& addr: space.slice(0, size)) { // The addresses produced by backtrace() are return addresses, which means they point to the // instruction immediately after the call. Invoking addr2line on these can be confusing because // it often points to the next line. If the next instruction is inlined from another function, // the trace can be extra-confusing, since now it claims to be in a function that was not // actually on the call stack. If we subtract 1 from each address, though, we get a much more // reasonable trace. This may cause the addresses to be invalid instruction pointers if the // instructions were multi-byte, but it appears addr2line is able to cope with this. addr = reinterpret_cast(reinterpret_cast(addr) - 1); } return space.slice(kj::min(ignoreCount + 1, size), size); #else return nullptr; #endif } String stringifyStackTrace(ArrayPtr trace) { if (trace.size() == 0) return nullptr; if (getExceptionCallback().stackTraceMode() != ExceptionCallback::StackTraceMode::FULL) { return nullptr; } #if KJ_USE_WIN32_DBGHELP && _MSC_VER // Try to get file/line using SymGetLineFromAddr64(). We don't bother if we aren't on MSVC since // this requires MSVC debug info. // // TODO(someday): We could perhaps shell out to addr2line on MinGW. const Dbghelp& dbghelp = getDbghelp(); if (dbghelp.symGetLineFromAddr64 == nullptr) return nullptr; HANDLE process = GetCurrentProcess(); KJ_STACK_ARRAY(String, lines, trace.size(), 32, 32); for (auto i: kj::indices(trace)) { IMAGEHLP_LINE64 lineInfo; memset(&lineInfo, 0, sizeof(lineInfo)); lineInfo.SizeOfStruct = sizeof(lineInfo); if (dbghelp.symGetLineFromAddr64(process, reinterpret_cast(trace[i]), NULL, &lineInfo)) { lines[i] = kj::str('\n', lineInfo.FileName, ':', lineInfo.LineNumber); } } return strArray(lines, ""); #elif (__linux__ || __APPLE__ || __CYGWIN__) && !__ANDROID__ // We want to generate a human-readable stack trace. // TODO(someday): It would be really great if we could avoid farming out to another process // and do this all in-process, but that may involve onerous requirements like large library // dependencies or using -rdynamic. // The environment manipulation is not thread-safe, so lock a mutex. This could still be // problematic if another thread is manipulating the environment in unrelated code, but there's // not much we can do about that. This is debug-only anyway and only an issue when LD_PRELOAD // is in use. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex); KJ_DEFER(pthread_mutex_unlock(&mutex)); // Don't heapcheck / intercept syscalls. const char* preload = getenv("LD_PRELOAD"); String oldPreload; if (preload != nullptr) { oldPreload = heapString(preload); unsetenv("LD_PRELOAD"); } KJ_DEFER(if (oldPreload != nullptr) { setenv("LD_PRELOAD", oldPreload.cStr(), true); }); String lines[32]; FILE* p = nullptr; auto strTrace = strArray(trace, " "); #if __linux__ if (access("/proc/self/exe", R_OK) < 0) { // Apparently /proc is not available? return nullptr; } // Obtain symbolic stack trace using addr2line. // TODO(cleanup): Use fork() and exec() or maybe our own Subprocess API (once it exists), to // avoid depending on a shell. p = popen(str("addr2line -e /proc/", getpid(), "/exe ", strTrace).cStr(), "r"); #elif __APPLE__ // The Mac OS X equivalent of addr2line is atos. // (Internally, it uses the private CoreSymbolication.framework library.) p = popen(str("xcrun atos -p ", getpid(), ' ', strTrace).cStr(), "r"); #elif __CYGWIN__ wchar_t exeWinPath[MAX_PATH]; if (GetModuleFileNameW(nullptr, exeWinPath, sizeof(exeWinPath)) == 0) { return nullptr; } char exePosixPath[MAX_PATH * 2]; if (cygwin_conv_path(CCP_WIN_W_TO_POSIX, exeWinPath, exePosixPath, sizeof(exePosixPath)) < 0) { return nullptr; } p = popen(str("addr2line -e '", exePosixPath, "' ", strTrace).cStr(), "r"); #endif if (p == nullptr) { return nullptr; } char line[512]; size_t i = 0; while (i < kj::size(lines) && fgets(line, sizeof(line), p) != nullptr) { // Don't include exception-handling infrastructure or promise infrastructure in stack trace. // addr2line output matches file names; atos output matches symbol names. if (strstr(line, "kj/common.c++") != nullptr || strstr(line, "kj/exception.") != nullptr || strstr(line, "kj/debug.") != nullptr || strstr(line, "kj/async.") != nullptr || strstr(line, "kj/async-prelude.h") != nullptr || strstr(line, "kj/async-inl.h") != nullptr || strstr(line, "kj::Exception") != nullptr || strstr(line, "kj::_::Debug") != nullptr) { continue; } size_t len = strlen(line); if (len > 0 && line[len-1] == '\n') line[len-1] = '\0'; lines[i++] = str("\n ", trimSourceFilename(line), ": returning here"); } // Skip remaining input. while (fgets(line, sizeof(line), p) != nullptr) {} pclose(p); return strArray(arrayPtr(lines, i), ""); #else return nullptr; #endif } String stringifyStackTraceAddresses(ArrayPtr trace) { #if KJ_HAS_LIBDL return strArray(KJ_MAP(addr, trace) { Dl_info info; // Shared libraries are mapped near the end of the address space while the executable is mapped // near the beginning. We want to print addresses in the executable as raw addresses, not // offsets, since that's what addr2line expects for executables. For shared libraries it // expects offsets. In any case, most frames are likely to be in the main executable so it // makes the output cleaner if we don't repeatedly write its name. if (reinterpret_cast(addr) >= 0x400000000000ull && dladdr(addr, &info)) { uintptr_t offset = reinterpret_cast(addr) - reinterpret_cast(info.dli_fbase); return kj::str(info.dli_fname, '@', reinterpret_cast(offset)); } else { return kj::str(addr); } }, " "); #else // TODO(someday): Support other platforms. return kj::strArray(trace, " "); #endif } StringPtr stringifyStackTraceAddresses(ArrayPtr trace, ArrayPtr scratch) { // Version which writes into a pre-allocated buffer. This is safe for signal handlers to the // extent that dladdr() is safe. // // TODO(cleanup): We should improve the KJ stringification framework so that there's a way to // write this string directly into a larger message buffer with strPreallocated(). #if KJ_HAS_LIBDL char* ptr = scratch.begin(); char* limit = scratch.end() - 1; for (auto addr: trace) { Dl_info info; // Shared libraries are mapped near the end of the address space while the executable is mapped // near the beginning. We want to print addresses in the executable as raw addresses, not // offsets, since that's what addr2line expects for executables. For shared libraries it // expects offsets. In any case, most frames are likely to be in the main executable so it // makes the output cleaner if we don't repeatedly write its name. if (reinterpret_cast(addr) >= 0x400000000000ull && dladdr(addr, &info)) { uintptr_t offset = reinterpret_cast(addr) - reinterpret_cast(info.dli_fbase); ptr = _::fillLimited(ptr, limit, kj::StringPtr(info.dli_fname), "@0x"_kj, hex(offset)); } else { ptr = _::fillLimited(ptr, limit, toCharSequence(addr)); } ptr = _::fillLimited(ptr, limit, " "_kj); } *ptr = '\0'; return StringPtr(scratch.begin(), ptr); #else // TODO(someday): Support other platforms. return kj::strPreallocated(scratch, kj::delimited(trace, " ")); #endif } String getStackTrace() { void* space[32]; auto trace = getStackTrace(space, 2); return kj::str(stringifyStackTraceAddresses(trace), stringifyStackTrace(trace)); } namespace { #if !KJ_NO_EXCEPTIONS void terminateHandler() { void* traceSpace[32]; // ignoreCount = 3 to ignore std::terminate entry. auto trace = kj::getStackTrace(traceSpace, 3); kj::String message; auto eptr = std::current_exception(); if (eptr != nullptr) { try { std::rethrow_exception(eptr); } catch (const kj::Exception& exception) { message = kj::str("*** Fatal uncaught kj::Exception: ", exception, '\n'); } catch (const std::exception& exception) { message = kj::str("*** Fatal uncaught std::exception: ", exception.what(), "\nstack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace), '\n'); } catch (...) { message = kj::str("*** Fatal uncaught exception of type: ", kj::getCaughtExceptionType(), "\nstack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace), '\n'); } } else { message = kj::str("*** std::terminate() called with no exception" "\nstack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace), '\n'); } kj::FdOutputStream(STDERR_FILENO).write(message.begin(), message.size()); _exit(1); } #endif } // namespace #if KJ_USE_WIN32_DBGHELP && !__CYGWIN__ namespace { DWORD mainThreadId = 0; BOOL WINAPI breakHandler(DWORD type) { switch (type) { case CTRL_C_EVENT: case CTRL_BREAK_EVENT: { HANDLE thread = OpenThread(THREAD_ALL_ACCESS, FALSE, mainThreadId); if (thread != NULL) { if (SuspendThread(thread) != (DWORD)-1) { CONTEXT context; memset(&context, 0, sizeof(context)); context.ContextFlags = CONTEXT_FULL; if (GetThreadContext(thread, &context)) { void* traceSpace[32]; auto trace = getStackTrace(traceSpace, 0, thread, context); ResumeThread(thread); auto message = kj::str("*** Received CTRL+C. stack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace), '\n'); FdOutputStream(STDERR_FILENO).write(message.begin(), message.size()); } else { ResumeThread(thread); } } CloseHandle(thread); } break; } default: break; } return FALSE; // still crash } kj::StringPtr exceptionDescription(DWORD code) { switch (code) { case EXCEPTION_ACCESS_VIOLATION: return "access violation"; case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: return "array bounds exceeded"; case EXCEPTION_BREAKPOINT: return "breakpoint"; case EXCEPTION_DATATYPE_MISALIGNMENT: return "datatype misalignment"; case EXCEPTION_FLT_DENORMAL_OPERAND: return "denormal floating point operand"; case EXCEPTION_FLT_DIVIDE_BY_ZERO: return "floating point division by zero"; case EXCEPTION_FLT_INEXACT_RESULT: return "inexact floating point result"; case EXCEPTION_FLT_INVALID_OPERATION: return "invalid floating point operation"; case EXCEPTION_FLT_OVERFLOW: return "floating point overflow"; case EXCEPTION_FLT_STACK_CHECK: return "floating point stack overflow"; case EXCEPTION_FLT_UNDERFLOW: return "floating point underflow"; case EXCEPTION_ILLEGAL_INSTRUCTION: return "illegal instruction"; case EXCEPTION_IN_PAGE_ERROR: return "page error"; case EXCEPTION_INT_DIVIDE_BY_ZERO: return "integer divided by zero"; case EXCEPTION_INT_OVERFLOW: return "integer overflow"; case EXCEPTION_INVALID_DISPOSITION: return "invalid disposition"; case EXCEPTION_NONCONTINUABLE_EXCEPTION: return "noncontinuable exception"; case EXCEPTION_PRIV_INSTRUCTION: return "privileged instruction"; case EXCEPTION_SINGLE_STEP: return "single step"; case EXCEPTION_STACK_OVERFLOW: return "stack overflow"; default: return "(unknown exception code)"; } } LONG WINAPI sehHandler(EXCEPTION_POINTERS* info) { void* traceSpace[32]; auto trace = getStackTrace(traceSpace, 0, GetCurrentThread(), *info->ContextRecord); auto message = kj::str("*** Received structured exception #0x", hex(info->ExceptionRecord->ExceptionCode), ": ", exceptionDescription(info->ExceptionRecord->ExceptionCode), "; stack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace), '\n'); FdOutputStream(STDERR_FILENO).write(message.begin(), message.size()); return EXCEPTION_EXECUTE_HANDLER; // still crash } } // namespace void printStackTraceOnCrash() { mainThreadId = GetCurrentThreadId(); KJ_WIN32(SetConsoleCtrlHandler(breakHandler, TRUE)); SetUnhandledExceptionFilter(&sehHandler); #if !KJ_NO_EXCEPTIONS // Also override std::terminate() handler with something nicer for KJ. std::set_terminate(&terminateHandler); #endif } #elif _WIN32 // Windows, but KJ_USE_WIN32_DBGHELP is not enabled. We can't print useful stack traces, so don't // try to catch SEH nor ctrl+C. void printStackTraceOnCrash() { #if !KJ_NO_EXCEPTIONS std::set_terminate(&terminateHandler); #endif } #else namespace { void crashHandler(int signo, siginfo_t* info, void* context) { void* traceSpace[32]; #if KJ_USE_WIN32_DBGHELP // Win32 backtracing can't trace its way out of a Cygwin signal handler. However, Cygwin gives // us direct access to the CONTEXT, which we can pass to the Win32 tracing functions. ucontext_t* ucontext = reinterpret_cast(context); // Cygwin's mcontext_t has the same layout as CONTEXT. // TODO(someday): Figure out why this produces garbage for SIGINT from ctrl+C. It seems to work // correctly for SIGSEGV. CONTEXT win32Context; static_assert(sizeof(ucontext->uc_mcontext) >= sizeof(win32Context), "mcontext_t should be an extension of CONTEXT"); memcpy(&win32Context, &ucontext->uc_mcontext, sizeof(win32Context)); auto trace = getStackTrace(traceSpace, 0, GetCurrentThread(), win32Context); #else // ignoreCount = 2 to ignore crashHandler() and signal trampoline. auto trace = getStackTrace(traceSpace, 2); #endif auto message = kj::str("*** Received signal #", signo, ": ", strsignal(signo), "\nstack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace), '\n'); FdOutputStream(STDERR_FILENO).write(message.begin(), message.size()); _exit(1); } } // namespace void printStackTraceOnCrash() { // Set up alternate signal stack so that stack overflows can be handled. stack_t stack; memset(&stack, 0, sizeof(stack)); #ifndef MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON #endif #ifndef MAP_GROWSDOWN #define MAP_GROWSDOWN 0 #endif stack.ss_size = 65536; // Note: ss_sp is char* on FreeBSD, void* on Linux and OSX. stack.ss_sp = reinterpret_cast(mmap( nullptr, stack.ss_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN, -1, 0)); KJ_SYSCALL(sigaltstack(&stack, nullptr)); // Catch all relevant signals. struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER | SA_RESETHAND; action.sa_sigaction = &crashHandler; // Dump stack on common "crash" signals. KJ_SYSCALL(sigaction(SIGSEGV, &action, nullptr)); KJ_SYSCALL(sigaction(SIGBUS, &action, nullptr)); KJ_SYSCALL(sigaction(SIGFPE, &action, nullptr)); KJ_SYSCALL(sigaction(SIGABRT, &action, nullptr)); KJ_SYSCALL(sigaction(SIGILL, &action, nullptr)); // Dump stack on unimplemented syscalls -- useful in seccomp sandboxes. KJ_SYSCALL(sigaction(SIGSYS, &action, nullptr)); #ifdef KJ_DEBUG // Dump stack on keyboard interrupt -- useful for infinite loops. Only in debug mode, though, // because stack traces on ctrl+c can be obnoxious for, say, command-line tools. KJ_SYSCALL(sigaction(SIGINT, &action, nullptr)); #endif #if !KJ_NO_EXCEPTIONS // Also override std::terminate() handler with something nicer for KJ. std::set_terminate(&terminateHandler); #endif } #endif kj::StringPtr trimSourceFilename(kj::StringPtr filename) { // Removes noisy prefixes from source code file name. // // The goal here is to produce the "canonical" filename given the filename returned by e.g. // addr2line. addr2line gives us the full path of the file as passed on the compiler // command-line, which in turn is affected by build system and by whether and where we're // performing an out-of-tree build. // // To deal with all this, we look for directory names in the path which we recognize to be // locations that represent roots of the source tree. We strip said root and everything before // it. // // On Windows, we often get filenames containing backslashes. Since we aren't allowed to allocate // a new string here, we can't do much about this, so our returned "canonical" name will // unfortunately end up with backslashes. static constexpr const char* ROOTS[] = { "ekam-provider/canonical/", // Ekam source file. "ekam-provider/c++header/", // Ekam include file. "src/", // Non-Ekam source root. "tmp/", // Non-Ekam generated code. #if _WIN32 "src\\", // Win32 source root. "tmp\\", // Win32 generated code. #endif }; retry: for (size_t i: kj::indices(filename)) { if (i == 0 || filename[i-1] == '/' #if _WIN32 || filename[i-1] == '\\' #endif ) { // We're at the start of a directory name. Check for valid prefixes. for (kj::StringPtr root: ROOTS) { if (filename.slice(i).startsWith(root)) { filename = filename.slice(i + root.size()); // We should keep searching to find the last instance of a root name. `i` is no longer // a valid index for `filename` so start the loop over. goto retry; } } } } return filename; } StringPtr KJ_STRINGIFY(Exception::Type type) { static const char* TYPE_STRINGS[] = { "failed", "overloaded", "disconnected", "unimplemented" }; return TYPE_STRINGS[static_cast(type)]; } String KJ_STRINGIFY(const Exception& e) { uint contextDepth = 0; Maybe contextPtr = e.getContext(); for (;;) { KJ_IF_MAYBE(c, contextPtr) { ++contextDepth; contextPtr = c->next; } else { break; } } Array contextText = heapArray(contextDepth); contextDepth = 0; contextPtr = e.getContext(); for (;;) { KJ_IF_MAYBE(c, contextPtr) { contextText[contextDepth++] = str(c->file, ":", c->line, ": context: ", c->description, "\n"); contextPtr = c->next; } else { break; } } return str(strArray(contextText, ""), e.getFile(), ":", e.getLine(), ": ", e.getType(), e.getDescription() == nullptr ? "" : ": ", e.getDescription(), e.getStackTrace().size() > 0 ? "\nstack: " : "", stringifyStackTraceAddresses(e.getStackTrace()), stringifyStackTrace(e.getStackTrace())); } Exception::Exception(Type type, const char* file, int line, String description) noexcept : file(trimSourceFilename(file).cStr()), line(line), type(type), description(mv(description)), traceCount(0) {} Exception::Exception(Type type, String file, int line, String description) noexcept : ownFile(kj::mv(file)), file(trimSourceFilename(ownFile).cStr()), line(line), type(type), description(mv(description)), traceCount(0) {} Exception::Exception(const Exception& other) noexcept : file(other.file), line(other.line), type(other.type), description(heapString(other.description)), traceCount(other.traceCount) { if (file == other.ownFile.cStr()) { ownFile = heapString(other.ownFile); file = ownFile.cStr(); } memcpy(trace, other.trace, sizeof(trace[0]) * traceCount); KJ_IF_MAYBE(c, other.context) { context = heap(**c); } } Exception::~Exception() noexcept {} Exception::Context::Context(const Context& other) noexcept : file(other.file), line(other.line), description(str(other.description)) { KJ_IF_MAYBE(n, other.next) { next = heap(**n); } } void Exception::wrapContext(const char* file, int line, String&& description) { context = heap(file, line, mv(description), mv(context)); } void Exception::extendTrace(uint ignoreCount) { KJ_STACK_ARRAY(void*, newTraceSpace, kj::size(trace) + ignoreCount + 1, sizeof(trace)/sizeof(trace[0]) + 8, 128); auto newTrace = kj::getStackTrace(newTraceSpace, ignoreCount + 1); if (newTrace.size() > ignoreCount + 2) { // Remove suffix that won't fit into our static-sized trace. newTrace = newTrace.slice(0, kj::min(kj::size(trace) - traceCount, newTrace.size())); // Copy the rest into our trace. memcpy(trace + traceCount, newTrace.begin(), newTrace.asBytes().size()); traceCount += newTrace.size(); } } void Exception::truncateCommonTrace() { if (traceCount > 0) { // Create a "reference" stack trace that is a little bit deeper than the one in the exception. void* refTraceSpace[sizeof(this->trace) / sizeof(this->trace[0]) + 4]; auto refTrace = kj::getStackTrace(refTraceSpace, 0); // We expect that the deepest frame in the exception's stack trace should be somewhere in our // own trace, since our own trace has a deeper limit. Search for it. for (uint i = refTrace.size(); i > 0; i--) { if (refTrace[i-1] == trace[traceCount-1]) { // See how many frames match. for (uint j = 0; j < i; j++) { if (j >= traceCount) { // We matched the whole trace, apparently? traceCount = 0; return; } else if (refTrace[i-j-1] != trace[traceCount-j-1]) { // Found mismatching entry. // If we matched more than half of the reference trace, guess that this is in fact // the prefix we're looking for. if (j > refTrace.size() / 2) { // Delete the matching suffix. Also delete one non-matched entry on the assumption // that both traces contain that stack frame but are simply at different points in // the function. traceCount -= j + 1; return; } } } } } // No match. Ignore. } } void Exception::addTrace(void* ptr) { if (traceCount < kj::size(trace)) { trace[traceCount++] = ptr; } } class ExceptionImpl: public Exception, public std::exception { public: inline ExceptionImpl(Exception&& other): Exception(mv(other)) {} ExceptionImpl(const ExceptionImpl& other): Exception(other) { // No need to copy whatBuffer since it's just to hold the return value of what(). } const char* what() const noexcept override; private: mutable String whatBuffer; }; const char* ExceptionImpl::what() const noexcept { whatBuffer = str(*this); return whatBuffer.begin(); } // ======================================================================================= namespace { KJ_THREADLOCAL_PTR(ExceptionCallback) threadLocalCallback = nullptr; } // namespace ExceptionCallback::ExceptionCallback(): next(getExceptionCallback()) { char stackVar; ptrdiff_t offset = reinterpret_cast(this) - &stackVar; KJ_ASSERT(offset < 65536 && offset > -65536, "ExceptionCallback must be allocated on the stack."); threadLocalCallback = this; } ExceptionCallback::ExceptionCallback(ExceptionCallback& next): next(next) {} ExceptionCallback::~ExceptionCallback() noexcept(false) { if (&next != this) { threadLocalCallback = &next; } } void ExceptionCallback::onRecoverableException(Exception&& exception) { next.onRecoverableException(mv(exception)); } void ExceptionCallback::onFatalException(Exception&& exception) { next.onFatalException(mv(exception)); } void ExceptionCallback::logMessage( LogSeverity severity, const char* file, int line, int contextDepth, String&& text) { next.logMessage(severity, file, line, contextDepth, mv(text)); } ExceptionCallback::StackTraceMode ExceptionCallback::stackTraceMode() { return next.stackTraceMode(); } Function)> ExceptionCallback::getThreadInitializer() { return next.getThreadInitializer(); } class ExceptionCallback::RootExceptionCallback: public ExceptionCallback { public: RootExceptionCallback(): ExceptionCallback(*this) {} void onRecoverableException(Exception&& exception) override { #if KJ_NO_EXCEPTIONS logException(LogSeverity::ERROR, mv(exception)); #else if (std::uncaught_exception()) { // Bad time to throw an exception. Just log instead. // // TODO(someday): We should really compare uncaughtExceptionCount() against the count at // the innermost runCatchingExceptions() frame in this thread to tell if exceptions are // being caught correctly. logException(LogSeverity::ERROR, mv(exception)); } else { throw ExceptionImpl(mv(exception)); } #endif } void onFatalException(Exception&& exception) override { #if KJ_NO_EXCEPTIONS logException(LogSeverity::FATAL, mv(exception)); #else throw ExceptionImpl(mv(exception)); #endif } void logMessage(LogSeverity severity, const char* file, int line, int contextDepth, String&& text) override { text = str(kj::repeat('_', contextDepth), file, ":", line, ": ", severity, ": ", mv(text), '\n'); StringPtr textPtr = text; while (textPtr != nullptr) { miniposix::ssize_t n = miniposix::write(STDERR_FILENO, textPtr.begin(), textPtr.size()); if (n <= 0) { // stderr is broken. Give up. return; } textPtr = textPtr.slice(n); } } StackTraceMode stackTraceMode() override { #ifdef KJ_DEBUG return StackTraceMode::FULL; #else return StackTraceMode::ADDRESS_ONLY; #endif } Function)> getThreadInitializer() override { return [](Function func) { // No initialization needed since RootExceptionCallback is automatically the root callback // for new threads. func(); }; } private: void logException(LogSeverity severity, Exception&& e) { // We intentionally go back to the top exception callback on the stack because we don't want to // bypass whatever log processing is in effect. // // We intentionally don't log the context since it should get re-added by the exception callback // anyway. getExceptionCallback().logMessage(severity, e.getFile(), e.getLine(), 0, str( e.getType(), e.getDescription() == nullptr ? "" : ": ", e.getDescription(), e.getStackTrace().size() > 0 ? "\nstack: " : "", stringifyStackTraceAddresses(e.getStackTrace()), stringifyStackTrace(e.getStackTrace()), "\n")); } }; ExceptionCallback& getExceptionCallback() { static ExceptionCallback::RootExceptionCallback defaultCallback; ExceptionCallback* scoped = threadLocalCallback; return scoped != nullptr ? *scoped : defaultCallback; } void throwFatalException(kj::Exception&& exception, uint ignoreCount) { exception.extendTrace(ignoreCount + 1); getExceptionCallback().onFatalException(kj::mv(exception)); abort(); } void throwRecoverableException(kj::Exception&& exception, uint ignoreCount) { exception.extendTrace(ignoreCount + 1); getExceptionCallback().onRecoverableException(kj::mv(exception)); } // ======================================================================================= namespace _ { // private #if __cplusplus >= 201703L uint uncaughtExceptionCount() { return std::uncaught_exceptions(); } #elif __GNUC__ // Horrible -- but working -- hack: We can dig into __cxa_get_globals() in order to extract the // count of uncaught exceptions. This function is part of the C++ ABI implementation used on Linux, // OSX, and probably other platforms that use GCC. Unfortunately, __cxa_get_globals() is only // actually defined in cxxabi.h on some platforms (e.g. Linux, but not OSX), and even where it is // defined, it returns an incomplete type. Here we use the same hack used by Evgeny Panasyuk: // https://github.com/panaseleus/stack_unwinding/blob/master/boost/exception/uncaught_exception_count.hpp // // Notice that a similar hack is possible on MSVC -- if its C++11 support ever gets to the point of // supporting KJ in the first place. // // It appears likely that a future version of the C++ standard may include an // uncaught_exception_count() function in the standard library, or an equivalent language feature. // Some discussion: // https://groups.google.com/a/isocpp.org/d/msg/std-proposals/HglEslyZFYs/kKdu5jJw5AgJ struct FakeEhGlobals { // Fake void* caughtExceptions; uint uncaughtExceptions; }; // LLVM's libstdc++ doesn't declare __cxa_get_globals in its cxxabi.h. GNU does. Because it is // extern "C", the compiler wills get upset if we re-declare it even in a different namespace. #if _LIBCPPABI_VERSION extern "C" void* __cxa_get_globals(); #else using abi::__cxa_get_globals; #endif uint uncaughtExceptionCount() { return reinterpret_cast(__cxa_get_globals())->uncaughtExceptions; } #elif _MSC_VER #if _MSC_VER >= 1900 // MSVC14 has a refactored CRT which now provides a direct accessor for this value. // See https://svn.boost.org/trac/boost/ticket/10158 for a brief discussion. extern "C" int *__cdecl __processing_throw(); uint uncaughtExceptionCount() { return static_cast(*__processing_throw()); } #elif _MSC_VER >= 1400 // The below was copied from: // https://github.com/panaseleus/stack_unwinding/blob/master/boost/exception/uncaught_exception_count.hpp extern "C" char *__cdecl _getptd(); uint uncaughtExceptionCount() { return *reinterpret_cast(_getptd() + (sizeof(void*) == 8 ? 0x100 : 0x90)); } #else uint uncaughtExceptionCount() { // Since the above doesn't work, fall back to uncaught_exception(). This will produce incorrect // results in very obscure cases that Cap'n Proto doesn't really rely on anyway. return std::uncaught_exception(); } #endif #else #error "This needs to be ported to your compiler / C++ ABI." #endif } // namespace _ (private) UnwindDetector::UnwindDetector(): uncaughtCount(_::uncaughtExceptionCount()) {} bool UnwindDetector::isUnwinding() const { return _::uncaughtExceptionCount() > uncaughtCount; } void UnwindDetector::catchExceptionsAsSecondaryFaults(_::Runnable& runnable) const { // TODO(someday): Attach the secondary exception to whatever primary exception is causing // the unwind. For now we just drop it on the floor as this is probably fine most of the // time. runCatchingExceptions(runnable); } #if __GNUC__ && !KJ_NO_RTTI static kj::String demangleTypeName(const char* name) { if (name == nullptr) return kj::heapString("(nil)"); int status; char* buf = abi::__cxa_demangle(name, nullptr, nullptr, &status); kj::String result = kj::heapString(buf == nullptr ? name : buf); free(buf); return kj::mv(result); } kj::String getCaughtExceptionType() { return demangleTypeName(abi::__cxa_current_exception_type()->name()); } #else kj::String getCaughtExceptionType() { return kj::heapString("(unknown)"); } #endif namespace _ { // private class RecoverableExceptionCatcher: public ExceptionCallback { // Catches a recoverable exception without using try/catch. Used when compiled with // -fno-exceptions. public: virtual ~RecoverableExceptionCatcher() noexcept(false) {} void onRecoverableException(Exception&& exception) override { if (caught == nullptr) { caught = mv(exception); } else { // TODO(someday): Consider it a secondary fault? } } Maybe caught; }; Maybe runCatchingExceptions(Runnable& runnable) noexcept { #if KJ_NO_EXCEPTIONS RecoverableExceptionCatcher catcher; runnable.run(); KJ_IF_MAYBE(e, catcher.caught) { e->truncateCommonTrace(); } return mv(catcher.caught); #else try { runnable.run(); return nullptr; } catch (Exception& e) { e.truncateCommonTrace(); return kj::mv(e); } catch (std::bad_alloc& e) { return Exception(Exception::Type::OVERLOADED, "(unknown)", -1, str("std::bad_alloc: ", e.what())); } catch (std::exception& e) { return Exception(Exception::Type::FAILED, "(unknown)", -1, str("std::exception: ", e.what())); } catch (...) { #if __GNUC__ && !KJ_NO_RTTI return Exception(Exception::Type::FAILED, "(unknown)", -1, str( "unknown non-KJ exception of type: ", getCaughtExceptionType())); #else return Exception(Exception::Type::FAILED, "(unknown)", -1, str("unknown non-KJ exception")); #endif } #endif } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/debug.c++0000644000175000017500000003027313650101756017534 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "debug.h" #include #include #include #include #if _WIN32 || __CYGWIN__ #if !__CYGWIN__ #define strerror_r(errno,buf,len) strerror_s(buf,len,errno) #endif #define NOMINMAX 1 #define WIN32_LEAN_AND_MEAN 1 #define NOSERVICE 1 #define NOMCX 1 #define NOIME 1 #include #include "windows-sanity.h" #include "encoding.h" #include #endif namespace kj { namespace _ { // private LogSeverity Debug::minSeverity = LogSeverity::WARNING; namespace { Exception::Type typeOfErrno(int error) { switch (error) { #ifdef EDQUOT case EDQUOT: #endif #ifdef EMFILE case EMFILE: #endif #ifdef ENFILE case ENFILE: #endif #ifdef ENOBUFS case ENOBUFS: #endif #ifdef ENOLCK case ENOLCK: #endif #ifdef ENOMEM case ENOMEM: #endif #ifdef ENOSPC case ENOSPC: #endif #ifdef ETIMEDOUT case ETIMEDOUT: #endif #ifdef EUSERS case EUSERS: #endif return Exception::Type::OVERLOADED; #ifdef ENOTCONN case ENOTCONN: #endif #ifdef ECONNABORTED case ECONNABORTED: #endif #ifdef ECONNREFUSED case ECONNREFUSED: #endif #ifdef ECONNRESET case ECONNRESET: #endif #ifdef EHOSTDOWN case EHOSTDOWN: #endif #ifdef EHOSTUNREACH case EHOSTUNREACH: #endif #ifdef ENETDOWN case ENETDOWN: #endif #ifdef ENETRESET case ENETRESET: #endif #ifdef ENETUNREACH case ENETUNREACH: #endif #ifdef ENONET case ENONET: #endif #ifdef EPIPE case EPIPE: #endif return Exception::Type::DISCONNECTED; #ifdef ENOSYS case ENOSYS: #endif #ifdef ENOTSUP case ENOTSUP: #endif #if defined(EOPNOTSUPP) && EOPNOTSUPP != ENOTSUP case EOPNOTSUPP: #endif #ifdef ENOPROTOOPT case ENOPROTOOPT: #endif #ifdef ENOTSOCK // This is really saying "syscall not implemented for non-sockets". case ENOTSOCK: #endif return Exception::Type::UNIMPLEMENTED; default: return Exception::Type::FAILED; } } #if _WIN32 || __CYGWIN__ Exception::Type typeOfWin32Error(DWORD error) { switch (error) { // TODO(someday): This needs more work. case WSAETIMEDOUT: return Exception::Type::OVERLOADED; case WSAENOTCONN: case WSAECONNABORTED: case WSAECONNREFUSED: case WSAECONNRESET: case WSAEHOSTDOWN: case WSAEHOSTUNREACH: case WSAENETDOWN: case WSAENETRESET: case WSAENETUNREACH: case WSAESHUTDOWN: return Exception::Type::DISCONNECTED; case WSAEOPNOTSUPP: case WSAENOPROTOOPT: case WSAENOTSOCK: // This is really saying "syscall not implemented for non-sockets". return Exception::Type::UNIMPLEMENTED; default: return Exception::Type::FAILED; } } #endif // _WIN32 enum DescriptionStyle { LOG, ASSERTION, SYSCALL }; static String makeDescriptionImpl(DescriptionStyle style, const char* code, int errorNumber, const char* sysErrorString, const char* macroArgs, ArrayPtr argValues) { KJ_STACK_ARRAY(ArrayPtr, argNames, argValues.size(), 8, 64); if (argValues.size() > 0) { size_t index = 0; const char* start = macroArgs; while (isspace(*start)) ++start; const char* pos = start; uint depth = 0; bool quoted = false; while (char c = *pos++) { if (quoted) { if (c == '\\' && *pos != '\0') { ++pos; } else if (c == '\"') { quoted = false; } } else { if (c == '(') { ++depth; } else if (c == ')') { --depth; } else if (c == '\"') { quoted = true; } else if (c == ',' && depth == 0) { if (index < argValues.size()) { argNames[index] = arrayPtr(start, pos - 1); } ++index; while (isspace(*pos)) ++pos; start = pos; } } } if (index < argValues.size()) { argNames[index] = arrayPtr(start, pos - 1); } ++index; if (index != argValues.size()) { getExceptionCallback().logMessage(LogSeverity::ERROR, __FILE__, __LINE__, 0, str("Failed to parse logging macro args into ", argValues.size(), " names: ", macroArgs, '\n')); } } if (style == SYSCALL) { // Strip off leading "foo = " from code, since callers will sometimes write things like: // ssize_t n; // RECOVERABLE_SYSCALL(n = read(fd, buffer, sizeof(buffer))) { return ""; } // return std::string(buffer, n); const char* equalsPos = strchr(code, '='); if (equalsPos != nullptr && equalsPos[1] != '=') { code = equalsPos + 1; while (isspace(*code)) ++code; } } if (style == ASSERTION && code == nullptr) { style = LOG; } { StringPtr expected = "expected "; StringPtr codeArray = style == LOG ? nullptr : StringPtr(code); StringPtr sep = " = "; StringPtr delim = "; "; StringPtr colon = ": "; StringPtr sysErrorArray; // On android before marshmallow only the posix version of stderror_r was // available, even with __USE_GNU. #if __USE_GNU && !(defined(__ANDROID_API__) && __ANDROID_API__ < 23) char buffer[256]; if (style == SYSCALL) { if (sysErrorString == nullptr) { sysErrorArray = strerror_r(errorNumber, buffer, sizeof(buffer)); } else { sysErrorArray = sysErrorString; } } #else char buffer[256]; if (style == SYSCALL) { if (sysErrorString == nullptr) { strerror_r(errorNumber, buffer, sizeof(buffer)); sysErrorArray = buffer; } else { sysErrorArray = sysErrorString; } } #endif size_t totalSize = 0; switch (style) { case LOG: break; case ASSERTION: totalSize += expected.size() + codeArray.size(); break; case SYSCALL: totalSize += codeArray.size() + colon.size() + sysErrorArray.size(); break; } for (size_t i = 0; i < argValues.size(); i++) { if (i > 0 || style != LOG) { totalSize += delim.size(); } if (argNames[i].size() > 0 && argNames[i][0] != '\"') { totalSize += argNames[i].size() + sep.size(); } totalSize += argValues[i].size(); } String result = heapString(totalSize); char* pos = result.begin(); switch (style) { case LOG: break; case ASSERTION: pos = _::fill(pos, expected, codeArray); break; case SYSCALL: pos = _::fill(pos, codeArray, colon, sysErrorArray); break; } for (size_t i = 0; i < argValues.size(); i++) { if (i > 0 || style != LOG) { pos = _::fill(pos, delim); } if (argNames[i].size() > 0 && argNames[i][0] != '\"') { pos = _::fill(pos, argNames[i], sep); } pos = _::fill(pos, argValues[i]); } return result; } } } // namespace void Debug::logInternal(const char* file, int line, LogSeverity severity, const char* macroArgs, ArrayPtr argValues) { getExceptionCallback().logMessage(severity, trimSourceFilename(file).cStr(), line, 0, makeDescriptionImpl(LOG, nullptr, 0, nullptr, macroArgs, argValues)); } Debug::Fault::~Fault() noexcept(false) { if (exception != nullptr) { Exception copy = mv(*exception); delete exception; throwRecoverableException(mv(copy), 2); } } void Debug::Fault::fatal() { Exception copy = mv(*exception); delete exception; exception = nullptr; throwFatalException(mv(copy), 2); abort(); } void Debug::Fault::init( const char* file, int line, Exception::Type type, const char* condition, const char* macroArgs, ArrayPtr argValues) { exception = new Exception(type, file, line, makeDescriptionImpl(ASSERTION, condition, 0, nullptr, macroArgs, argValues)); } void Debug::Fault::init( const char* file, int line, int osErrorNumber, const char* condition, const char* macroArgs, ArrayPtr argValues) { exception = new Exception(typeOfErrno(osErrorNumber), file, line, makeDescriptionImpl(SYSCALL, condition, osErrorNumber, nullptr, macroArgs, argValues)); } #if _WIN32 || __CYGWIN__ void Debug::Fault::init( const char* file, int line, Win32Result osErrorNumber, const char* condition, const char* macroArgs, ArrayPtr argValues) { LPVOID ptr; // TODO(someday): Why doesn't this work for winsock errors? DWORD result = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, osErrorNumber.number, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR) &ptr, 0, NULL); String message; if (result > 0) { KJ_DEFER(LocalFree(ptr)); const wchar_t* desc = reinterpret_cast(ptr); size_t len = wcslen(desc); if (len > 0 && desc[len-1] == '\n') --len; if (len > 0 && desc[len-1] == '\r') --len; message = kj::str('#', osErrorNumber.number, ' ', decodeWideString(arrayPtr(desc, len))); } else { message = kj::str("win32 error code: ", osErrorNumber.number); } exception = new Exception(typeOfWin32Error(osErrorNumber.number), file, line, makeDescriptionImpl(SYSCALL, condition, 0, message.cStr(), macroArgs, argValues)); } #endif String Debug::makeDescriptionInternal(const char* macroArgs, ArrayPtr argValues) { return makeDescriptionImpl(LOG, nullptr, 0, nullptr, macroArgs, argValues); } int Debug::getOsErrorNumber(bool nonblocking) { int result = errno; // On many systems, EAGAIN and EWOULDBLOCK have the same value, but this is not strictly required // by POSIX, so we need to check both. return result == EINTR ? -1 : nonblocking && (result == EAGAIN || result == EWOULDBLOCK) ? 0 : result; } #if _WIN32 || __CYGWIN__ uint Debug::getWin32ErrorCode() { return ::GetLastError(); } #endif Debug::Context::Context(): logged(false) {} Debug::Context::~Context() noexcept(false) {} Debug::Context::Value Debug::Context::ensureInitialized() { KJ_IF_MAYBE(v, value) { return Value(v->file, v->line, heapString(v->description)); } else { Value result = evaluate(); value = Value(result.file, result.line, heapString(result.description)); return result; } } void Debug::Context::onRecoverableException(Exception&& exception) { Value v = ensureInitialized(); exception.wrapContext(v.file, v.line, mv(v.description)); next.onRecoverableException(kj::mv(exception)); } void Debug::Context::onFatalException(Exception&& exception) { Value v = ensureInitialized(); exception.wrapContext(v.file, v.line, mv(v.description)); next.onFatalException(kj::mv(exception)); } void Debug::Context::logMessage(LogSeverity severity, const char* file, int line, int contextDepth, String&& text) { if (!logged) { Value v = ensureInitialized(); next.logMessage(LogSeverity::INFO, v.file, v.line, 0, str("context: ", mv(v.description), '\n')); logged = true; } next.logMessage(severity, file, line, contextDepth + 1, mv(text)); } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/arena.c++0000644000175000017500000001332613340402540017523 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "arena.h" #include "debug.h" #include namespace kj { Arena::Arena(size_t chunkSizeHint): nextChunkSize(kj::max(sizeof(ChunkHeader), chunkSizeHint)) {} Arena::Arena(ArrayPtr scratch) : nextChunkSize(kj::max(sizeof(ChunkHeader), scratch.size())) { if (scratch.size() > sizeof(ChunkHeader)) { ChunkHeader* chunk = reinterpret_cast(scratch.begin()); chunk->end = scratch.end(); chunk->pos = reinterpret_cast(chunk + 1); chunk->next = nullptr; // Never actually observed. // Don't place the chunk in the chunk list because it's not ours to delete. Just make it the // current chunk so that we'll allocate from it until it is empty. currentChunk = chunk; } } Arena::~Arena() noexcept(false) { // Run cleanup() explicitly, but if it throws an exception, make sure to run it again as part of // unwind. The second call will not throw because destructors are required to guard against // exceptions when already unwinding. KJ_ON_SCOPE_FAILURE(cleanup()); cleanup(); } void Arena::cleanup() { while (objectList != nullptr) { void* ptr = objectList + 1; auto destructor = objectList->destructor; objectList = objectList->next; destructor(ptr); } while (chunkList != nullptr) { void* ptr = chunkList; chunkList = chunkList->next; operator delete(ptr); } } namespace { constexpr bool KJ_UNUSED isPowerOfTwo(size_t value) { return (value & (value - 1)) == 0; } inline byte* alignTo(byte* p, uint alignment) { // Round the pointer up to the next aligned value. KJ_DASSERT(isPowerOfTwo(alignment), alignment); uintptr_t mask = alignment - 1; uintptr_t i = reinterpret_cast(p); return reinterpret_cast((i + mask) & ~mask); } inline size_t alignTo(size_t s, uint alignment) { // Round the pointer up to the next aligned value. KJ_DASSERT(isPowerOfTwo(alignment), alignment); size_t mask = alignment - 1; return (s + mask) & ~mask; } } // namespace void* Arena::allocateBytes(size_t amount, uint alignment, bool hasDisposer) { if (hasDisposer) { alignment = kj::max(alignment, alignof(ObjectHeader)); amount += alignTo(sizeof(ObjectHeader), alignment); } void* result = allocateBytesInternal(amount, alignment); if (hasDisposer) { // Reserve space for the ObjectHeader, but don't add it to the object list yet. result = alignTo(reinterpret_cast(result) + sizeof(ObjectHeader), alignment); } KJ_DASSERT(reinterpret_cast(result) % alignment == 0); return result; } void* Arena::allocateBytesInternal(size_t amount, uint alignment) { if (currentChunk != nullptr) { ChunkHeader* chunk = currentChunk; byte* alignedPos = alignTo(chunk->pos, alignment); // Careful about overflow here. if (amount + (alignedPos - chunk->pos) <= chunk->end - chunk->pos) { // There's enough space in this chunk. chunk->pos = alignedPos + amount; return alignedPos; } } // Not enough space in the current chunk. Allocate a new one. // We need to allocate at least enough space for the ChunkHeader and the requested allocation. // If the alignment is less than that of the chunk header, we'll need to increase it. alignment = kj::max(alignment, alignof(ChunkHeader)); // If the ChunkHeader size does not match the alignment, we'll need to pad it up. amount += alignTo(sizeof(ChunkHeader), alignment); // Make sure we're going to allocate enough space. while (nextChunkSize < amount) { nextChunkSize *= 2; } // Allocate. byte* bytes = reinterpret_cast(operator new(nextChunkSize)); // Set up the ChunkHeader at the beginning of the allocation. ChunkHeader* newChunk = reinterpret_cast(bytes); newChunk->next = chunkList; newChunk->pos = bytes + amount; newChunk->end = bytes + nextChunkSize; currentChunk = newChunk; chunkList = newChunk; nextChunkSize *= 2; // Move past the ChunkHeader to find the position of the allocated object. return alignTo(bytes + sizeof(ChunkHeader), alignment); } StringPtr Arena::copyString(StringPtr content) { char* data = reinterpret_cast(allocateBytes(content.size() + 1, 1, false)); memcpy(data, content.cStr(), content.size() + 1); return StringPtr(data, content.size()); } void Arena::setDestructor(void* ptr, void (*destructor)(void*)) { ObjectHeader* header = reinterpret_cast(ptr) - 1; KJ_DASSERT(reinterpret_cast(header) % alignof(ObjectHeader) == 0); header->destructor = destructor; header->next = objectList; objectList = header; } } // namespace kj capnproto-c++-0.8.0/src/kj/io.c++0000644000175000017500000003452113650101756017055 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "io.h" #include "debug.h" #include "miniposix.h" #include #include #include "vector.h" #if _WIN32 #ifndef NOMINMAX #define NOMINMAX 1 #endif #define WIN32_LEAN_AND_MEAN #include #include "windows-sanity.h" #else #include #endif namespace kj { InputStream::~InputStream() noexcept(false) {} OutputStream::~OutputStream() noexcept(false) {} BufferedInputStream::~BufferedInputStream() noexcept(false) {} BufferedOutputStream::~BufferedOutputStream() noexcept(false) {} size_t InputStream::read(void* buffer, size_t minBytes, size_t maxBytes) { size_t n = tryRead(buffer, minBytes, maxBytes); KJ_REQUIRE(n >= minBytes, "Premature EOF") { // Pretend we read zeros from the input. memset(reinterpret_cast(buffer) + n, 0, minBytes - n); return minBytes; } return n; } void InputStream::skip(size_t bytes) { char scratch[8192]; while (bytes > 0) { size_t amount = std::min(bytes, sizeof(scratch)); read(scratch, amount); bytes -= amount; } } namespace { Array readAll(InputStream& input, uint64_t limit, bool nulTerminate) { Vector> parts; constexpr size_t BLOCK_SIZE = 4096; for (;;) { KJ_REQUIRE(limit > 0, "Reached limit before EOF."); auto part = heapArray(kj::min(BLOCK_SIZE, limit)); size_t n = input.tryRead(part.begin(), part.size(), part.size()); limit -= n; if (n < part.size()) { auto result = heapArray(parts.size() * BLOCK_SIZE + n + nulTerminate); byte* pos = result.begin(); for (auto& p: parts) { memcpy(pos, p.begin(), BLOCK_SIZE); pos += BLOCK_SIZE; } memcpy(pos, part.begin(), n); pos += n; if (nulTerminate) *pos++ = '\0'; KJ_ASSERT(pos == result.end()); return result; } else { parts.add(kj::mv(part)); } } } } // namespace String InputStream::readAllText(uint64_t limit) { return String(readAll(*this, limit, true).releaseAsChars()); } Array InputStream::readAllBytes(uint64_t limit) { return readAll(*this, limit, false); } void OutputStream::write(ArrayPtr> pieces) { for (auto piece: pieces) { write(piece.begin(), piece.size()); } } ArrayPtr BufferedInputStream::getReadBuffer() { auto result = tryGetReadBuffer(); KJ_REQUIRE(result.size() > 0, "Premature EOF"); return result; } // ======================================================================================= BufferedInputStreamWrapper::BufferedInputStreamWrapper(InputStream& inner, ArrayPtr buffer) : inner(inner), ownedBuffer(buffer == nullptr ? heapArray(8192) : nullptr), buffer(buffer == nullptr ? ownedBuffer : buffer) {} BufferedInputStreamWrapper::~BufferedInputStreamWrapper() noexcept(false) {} ArrayPtr BufferedInputStreamWrapper::tryGetReadBuffer() { if (bufferAvailable.size() == 0) { size_t n = inner.tryRead(buffer.begin(), 1, buffer.size()); bufferAvailable = buffer.slice(0, n); } return bufferAvailable; } size_t BufferedInputStreamWrapper::tryRead(void* dst, size_t minBytes, size_t maxBytes) { if (minBytes <= bufferAvailable.size()) { // Serve from current buffer. size_t n = std::min(bufferAvailable.size(), maxBytes); memcpy(dst, bufferAvailable.begin(), n); bufferAvailable = bufferAvailable.slice(n, bufferAvailable.size()); return n; } else { // Copy current available into destination. memcpy(dst, bufferAvailable.begin(), bufferAvailable.size()); size_t fromFirstBuffer = bufferAvailable.size(); dst = reinterpret_cast(dst) + fromFirstBuffer; minBytes -= fromFirstBuffer; maxBytes -= fromFirstBuffer; if (maxBytes <= buffer.size()) { // Read the next buffer-full. size_t n = inner.read(buffer.begin(), minBytes, buffer.size()); size_t fromSecondBuffer = std::min(n, maxBytes); memcpy(dst, buffer.begin(), fromSecondBuffer); bufferAvailable = buffer.slice(fromSecondBuffer, n); return fromFirstBuffer + fromSecondBuffer; } else { // Forward large read to the underlying stream. bufferAvailable = nullptr; return fromFirstBuffer + inner.read(dst, minBytes, maxBytes); } } } void BufferedInputStreamWrapper::skip(size_t bytes) { if (bytes <= bufferAvailable.size()) { bufferAvailable = bufferAvailable.slice(bytes, bufferAvailable.size()); } else { bytes -= bufferAvailable.size(); if (bytes <= buffer.size()) { // Read the next buffer-full. size_t n = inner.read(buffer.begin(), bytes, buffer.size()); bufferAvailable = buffer.slice(bytes, n); } else { // Forward large skip to the underlying stream. bufferAvailable = nullptr; inner.skip(bytes); } } } // ------------------------------------------------------------------- BufferedOutputStreamWrapper::BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr buffer) : inner(inner), ownedBuffer(buffer == nullptr ? heapArray(8192) : nullptr), buffer(buffer == nullptr ? ownedBuffer : buffer), bufferPos(this->buffer.begin()) {} BufferedOutputStreamWrapper::~BufferedOutputStreamWrapper() noexcept(false) { unwindDetector.catchExceptionsIfUnwinding([&]() { flush(); }); } void BufferedOutputStreamWrapper::flush() { if (bufferPos > buffer.begin()) { inner.write(buffer.begin(), bufferPos - buffer.begin()); bufferPos = buffer.begin(); } } ArrayPtr BufferedOutputStreamWrapper::getWriteBuffer() { return arrayPtr(bufferPos, buffer.end()); } void BufferedOutputStreamWrapper::write(const void* src, size_t size) { if (src == bufferPos) { // Oh goody, the caller wrote directly into our buffer. bufferPos += size; } else { size_t available = buffer.end() - bufferPos; if (size <= available) { memcpy(bufferPos, src, size); bufferPos += size; } else if (size <= buffer.size()) { // Too much for this buffer, but not a full buffer's worth, so we'll go ahead and copy. memcpy(bufferPos, src, available); inner.write(buffer.begin(), buffer.size()); size -= available; src = reinterpret_cast(src) + available; memcpy(buffer.begin(), src, size); bufferPos = buffer.begin() + size; } else { // Writing so much data that we might as well write directly to avoid a copy. inner.write(buffer.begin(), bufferPos - buffer.begin()); bufferPos = buffer.begin(); inner.write(src, size); } } } // ======================================================================================= ArrayInputStream::ArrayInputStream(ArrayPtr array): array(array) {} ArrayInputStream::~ArrayInputStream() noexcept(false) {} ArrayPtr ArrayInputStream::tryGetReadBuffer() { return array; } size_t ArrayInputStream::tryRead(void* dst, size_t minBytes, size_t maxBytes) { size_t n = std::min(maxBytes, array.size()); memcpy(dst, array.begin(), n); array = array.slice(n, array.size()); return n; } void ArrayInputStream::skip(size_t bytes) { KJ_REQUIRE(array.size() >= bytes, "ArrayInputStream ended prematurely.") { bytes = array.size(); break; } array = array.slice(bytes, array.size()); } // ------------------------------------------------------------------- ArrayOutputStream::ArrayOutputStream(ArrayPtr array): array(array), fillPos(array.begin()) {} ArrayOutputStream::~ArrayOutputStream() noexcept(false) {} ArrayPtr ArrayOutputStream::getWriteBuffer() { return arrayPtr(fillPos, array.end()); } void ArrayOutputStream::write(const void* src, size_t size) { if (src == fillPos && fillPos != array.end()) { // Oh goody, the caller wrote directly into our buffer. KJ_REQUIRE(size <= array.end() - fillPos, size, fillPos, array.end() - fillPos); fillPos += size; } else { KJ_REQUIRE(size <= (size_t)(array.end() - fillPos), "ArrayOutputStream's backing array was not large enough for the data written."); memcpy(fillPos, src, size); fillPos += size; } } // ------------------------------------------------------------------- VectorOutputStream::VectorOutputStream(size_t initialCapacity) : vector(heapArray(initialCapacity)), fillPos(vector.begin()) {} VectorOutputStream::~VectorOutputStream() noexcept(false) {} ArrayPtr VectorOutputStream::getWriteBuffer() { // Grow if needed. if (fillPos == vector.end()) { grow(vector.size() + 1); } return arrayPtr(fillPos, vector.end()); } void VectorOutputStream::write(const void* src, size_t size) { if (src == fillPos && fillPos != vector.end()) { // Oh goody, the caller wrote directly into our buffer. KJ_REQUIRE(size <= vector.end() - fillPos, size, fillPos, vector.end() - fillPos); fillPos += size; } else { if (vector.end() - fillPos < size) { grow(fillPos - vector.begin() + size); } memcpy(fillPos, src, size); fillPos += size; } } void VectorOutputStream::grow(size_t minSize) { size_t newSize = vector.size() * 2; while (newSize < minSize) newSize *= 2; auto newVector = heapArray(newSize); memcpy(newVector.begin(), vector.begin(), fillPos - vector.begin()); fillPos = fillPos - vector.begin() + newVector.begin(); vector = kj::mv(newVector); } // ======================================================================================= AutoCloseFd::~AutoCloseFd() noexcept(false) { if (fd >= 0) { // Don't use SYSCALL() here because close() should not be repeated on EINTR. if (miniposix::close(fd) < 0) { KJ_FAIL_SYSCALL("close", errno, fd) { // This ensures we don't throw an exception if unwinding. break; } } } } FdInputStream::~FdInputStream() noexcept(false) {} size_t FdInputStream::tryRead(void* buffer, size_t minBytes, size_t maxBytes) { byte* pos = reinterpret_cast(buffer); byte* min = pos + minBytes; byte* max = pos + maxBytes; while (pos < min) { miniposix::ssize_t n; KJ_SYSCALL(n = miniposix::read(fd, pos, max - pos), fd); if (n == 0) { break; } pos += n; } return pos - reinterpret_cast(buffer); } FdOutputStream::~FdOutputStream() noexcept(false) {} void FdOutputStream::write(const void* buffer, size_t size) { const char* pos = reinterpret_cast(buffer); while (size > 0) { miniposix::ssize_t n; KJ_SYSCALL(n = miniposix::write(fd, pos, size), fd); KJ_ASSERT(n > 0, "write() returned zero."); pos += n; size -= n; } } void FdOutputStream::write(ArrayPtr> pieces) { #if _WIN32 // Windows has no reasonable writev(). It has WriteFileGather, but this call has the unreasonable // restriction that each segment must be page-aligned. So, fall back to the default implementation OutputStream::write(pieces); #else const size_t iovmax = miniposix::iovMax(pieces.size()); while (pieces.size() > iovmax) { write(pieces.slice(0, iovmax)); pieces = pieces.slice(iovmax, pieces.size()); } KJ_STACK_ARRAY(struct iovec, iov, pieces.size(), 16, 128); for (uint i = 0; i < pieces.size(); i++) { // writev() interface is not const-correct. :( iov[i].iov_base = const_cast(pieces[i].begin()); iov[i].iov_len = pieces[i].size(); } struct iovec* current = iov.begin(); // Advance past any leading empty buffers so that a write full of only empty buffers does not // cause a syscall at all. while (current < iov.end() && current->iov_len == 0) { ++current; } while (current < iov.end()) { // Issue the write. ssize_t n = 0; KJ_SYSCALL(n = ::writev(fd, current, iov.end() - current), fd); KJ_ASSERT(n > 0, "writev() returned zero."); // Advance past all buffers that were fully-written. while (current < iov.end() && static_cast(n) >= current->iov_len) { n -= current->iov_len; ++current; } // If we only partially-wrote one of the buffers, adjust the pointer and size to include only // the unwritten part. if (n > 0) { current->iov_base = reinterpret_cast(current->iov_base) + n; current->iov_len -= n; } } #endif } // ======================================================================================= #if _WIN32 AutoCloseHandle::~AutoCloseHandle() noexcept(false) { if (handle != (void*)-1) { KJ_WIN32(CloseHandle(handle)); } } HandleInputStream::~HandleInputStream() noexcept(false) {} size_t HandleInputStream::tryRead(void* buffer, size_t minBytes, size_t maxBytes) { byte* pos = reinterpret_cast(buffer); byte* min = pos + minBytes; byte* max = pos + maxBytes; while (pos < min) { DWORD n; KJ_WIN32(ReadFile(handle, pos, kj::min(max - pos, DWORD(kj::maxValue)), &n, nullptr)); if (n == 0) { break; } pos += n; } return pos - reinterpret_cast(buffer); } HandleOutputStream::~HandleOutputStream() noexcept(false) {} void HandleOutputStream::write(const void* buffer, size_t size) { const char* pos = reinterpret_cast(buffer); while (size > 0) { DWORD n; KJ_WIN32(WriteFile(handle, pos, kj::min(size, DWORD(kj::maxValue)), &n, nullptr)); KJ_ASSERT(n > 0, "write() returned zero."); pos += n; size -= n; } } #endif // _WIN32 } // namespace kj capnproto-c++-0.8.0/src/kj/mutex.c++0000644000175000017500000007504713650101756017620 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 || __CYGWIN__ #define WIN32_LEAN_AND_MEAN 1 // lolz #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #endif #include "mutex.h" #include "debug.h" #if !_WIN32 && !__CYGWIN__ #include #include #endif #if KJ_USE_FUTEX #include #include #include #include #ifndef SYS_futex // Missing on Android/Bionic. #define SYS_futex __NR_futex #endif #ifndef FUTEX_WAIT_PRIVATE // Missing on Android/Bionic. #define FUTEX_WAIT_PRIVATE FUTEX_WAIT #define FUTEX_WAKE_PRIVATE FUTEX_WAKE #endif #elif _WIN32 || __CYGWIN__ #include #endif namespace kj { namespace _ { // private inline void Mutex::addWaiter(Waiter& waiter) { #ifdef KJ_DEBUG assertLockedByCaller(EXCLUSIVE); #endif *waitersTail = waiter; waitersTail = &waiter.next; } inline void Mutex::removeWaiter(Waiter& waiter) { #ifdef KJ_DEBUG assertLockedByCaller(EXCLUSIVE); #endif *waiter.prev = waiter.next; KJ_IF_MAYBE(next, waiter.next) { next->prev = waiter.prev; } else { KJ_DASSERT(waitersTail == &waiter.next); waitersTail = waiter.prev; } } bool Mutex::checkPredicate(Waiter& waiter) { // Run the predicate from a thread other than the waiting thread, returning true if it's time to // signal the waiting thread. This is not only when the predicate passes, but also when it // throws, in which case we want to propagate the exception to the waiting thread. if (waiter.exception != nullptr) return true; // don't run again after an exception bool result = false; KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { result = waiter.predicate.check(); })) { // Exception thown. result = true; waiter.exception = kj::heap(kj::mv(*exception)); }; return result; } #if !_WIN32 && !__CYGWIN__ namespace { TimePoint toTimePoint(struct timespec ts) { return kj::origin() + ts.tv_sec * kj::SECONDS + ts.tv_nsec * kj::NANOSECONDS; } TimePoint now() { struct timespec now; KJ_SYSCALL(clock_gettime(CLOCK_MONOTONIC, &now)); return toTimePoint(now); } struct timespec toRelativeTimespec(Duration timeout) { struct timespec ts; ts.tv_sec = timeout / kj::SECONDS; ts.tv_nsec = timeout % kj::SECONDS / kj::NANOSECONDS; return ts; } struct timespec toAbsoluteTimespec(TimePoint time) { return toRelativeTimespec(time - kj::origin()); } } // namespace #endif #if KJ_USE_FUTEX // ======================================================================================= // Futex-based implementation (Linux-only) Mutex::Mutex(): futex(0) {} Mutex::~Mutex() { // This will crash anyway, might as well crash with a nice error message. KJ_ASSERT(futex == 0, "Mutex destroyed while locked.") { break; } } void Mutex::lock(Exclusivity exclusivity) { switch (exclusivity) { case EXCLUSIVE: for (;;) { uint state = 0; if (KJ_LIKELY(__atomic_compare_exchange_n(&futex, &state, EXCLUSIVE_HELD, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))) { // Acquired. break; } // The mutex is contended. Set the exclusive-requested bit and wait. if ((state & EXCLUSIVE_REQUESTED) == 0) { if (!__atomic_compare_exchange_n(&futex, &state, state | EXCLUSIVE_REQUESTED, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { // Oops, the state changed before we could set the request bit. Start over. continue; } state |= EXCLUSIVE_REQUESTED; } syscall(SYS_futex, &futex, FUTEX_WAIT_PRIVATE, state, nullptr, nullptr, 0); } break; case SHARED: { uint state = __atomic_add_fetch(&futex, 1, __ATOMIC_ACQUIRE); for (;;) { if (KJ_LIKELY((state & EXCLUSIVE_HELD) == 0)) { // Acquired. break; } // The mutex is exclusively locked by another thread. Since we incremented the counter // already, we just have to wait for it to be unlocked. syscall(SYS_futex, &futex, FUTEX_WAIT_PRIVATE, state, nullptr, nullptr, 0); state = __atomic_load_n(&futex, __ATOMIC_ACQUIRE); } break; } } } void Mutex::unlock(Exclusivity exclusivity, Waiter* waiterToSkip) { switch (exclusivity) { case EXCLUSIVE: { KJ_DASSERT(futex & EXCLUSIVE_HELD, "Unlocked a mutex that wasn't locked."); // First check if there are any conditional waiters. Note we only do this when unlocking an // exclusive lock since under a shared lock the state couldn't have changed. auto nextWaiter = waitersHead; for (;;) { KJ_IF_MAYBE(waiter, nextWaiter) { nextWaiter = waiter->next; if (waiter != waiterToSkip && checkPredicate(*waiter)) { // This waiter's predicate now evaluates true, so wake it up. if (waiter->hasTimeout) { // In this case we need to be careful to make sure the target thread isn't already // processing a timeout, so we need to do an atomic CAS rather than just a store. uint expected = 0; if (__atomic_compare_exchange_n(&waiter->futex, &expected, 1, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) { // Good, we set it to 1, transferring ownership of the mutex. Continue on below. } else { // Looks like the thread already timed out and set its own futex to 1. In that // case it is going to try to lock the mutex itself, so we should NOT attempt an // ownership transfer as this will deadlock. // // We have two options here: We can continue along the waiter list looking for // another waiter that's ready to be signaled, or we could drop out of the list // immediately since we know that another thread is already waiting for the lock // and will re-evaluate the waiter queue itself when it is done. It feels cleaner // to me to continue. continue; } } else { __atomic_store_n(&waiter->futex, 1, __ATOMIC_RELEASE); } syscall(SYS_futex, &waiter->futex, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0); // We transferred ownership of the lock to this waiter, so we're done now. return; } } else { // No more waiters. break; } } // Didn't wake any waiters, so wake normally. uint oldState = __atomic_fetch_and( &futex, ~(EXCLUSIVE_HELD | EXCLUSIVE_REQUESTED), __ATOMIC_RELEASE); if (KJ_UNLIKELY(oldState & ~EXCLUSIVE_HELD)) { // Other threads are waiting. If there are any shared waiters, they now collectively hold // the lock, and we must wake them up. If there are any exclusive waiters, we must wake // them up even if readers are waiting so that at the very least they may re-establish the // EXCLUSIVE_REQUESTED bit that we just removed. syscall(SYS_futex, &futex, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0); } break; } case SHARED: { KJ_DASSERT(futex & SHARED_COUNT_MASK, "Unshared a mutex that wasn't shared."); uint state = __atomic_sub_fetch(&futex, 1, __ATOMIC_RELEASE); // The only case where anyone is waiting is if EXCLUSIVE_REQUESTED is set, and the only time // it makes sense to wake up that waiter is if the shared count has reached zero. if (KJ_UNLIKELY(state == EXCLUSIVE_REQUESTED)) { if (__atomic_compare_exchange_n( &futex, &state, 0, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { // Wake all exclusive waiters. We have to wake all of them because one of them will // grab the lock while the others will re-establish the exclusive-requested bit. syscall(SYS_futex, &futex, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0); } } break; } } } void Mutex::assertLockedByCaller(Exclusivity exclusivity) { switch (exclusivity) { case EXCLUSIVE: KJ_ASSERT(futex & EXCLUSIVE_HELD, "Tried to call getAlreadyLocked*() but lock is not held."); break; case SHARED: KJ_ASSERT(futex & SHARED_COUNT_MASK, "Tried to call getAlreadyLocked*() but lock is not held."); break; } } void Mutex::wait(Predicate& predicate, Maybe timeout) { // Add waiter to list. Waiter waiter { nullptr, waitersTail, predicate, nullptr, 0, timeout != nullptr }; addWaiter(waiter); // To guarantee that we've re-locked the mutex before scope exit, keep track of whether it is // currently. bool currentlyLocked = true; KJ_DEFER({ if (!currentlyLocked) lock(EXCLUSIVE); removeWaiter(waiter); }); if (!predicate.check()) { unlock(EXCLUSIVE, &waiter); currentlyLocked = false; struct timespec ts; struct timespec* tsp = nullptr; KJ_IF_MAYBE(t, timeout) { ts = toAbsoluteTimespec(now() + *t); tsp = &ts; } // Wait for someone to set our futex to 1. for (;;) { // Note we use FUTEX_WAIT_BITSET_PRIVATE + FUTEX_BITSET_MATCH_ANY to get the same effect as // FUTEX_WAIT_PRIVATE except that the timeout is specified as an absolute time based on // CLOCK_MONOTONIC. Otherwise, FUTEX_WAIT_PRIVATE interprets it as a relative time, forcing // us to recompute the time after every iteration. KJ_SYSCALL_HANDLE_ERRORS(syscall(SYS_futex, &waiter.futex, FUTEX_WAIT_BITSET_PRIVATE, 0, tsp, nullptr, FUTEX_BITSET_MATCH_ANY)) { case EAGAIN: // Indicates that the futex was already non-zero by the time the kernal looked at it. // Not an error. break; case ETIMEDOUT: { // Wait timed out. This leaves us in a bit of a pickle: Ownership of the mutex was not // transferred to us from another thread. So, we need to lock it ourselves. But, another // thread might be in the process of signaling us and transferring ownership. So, we // first must atomically take control of our destiny. KJ_ASSERT(timeout != nullptr); uint expected = 0; if (__atomic_compare_exchange_n(&waiter.futex, &expected, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) { // OK, we set our own futex to 1. That means no other thread will, and so we won't be // receiving a mutex ownership transfer. We have to lock the mutex ourselves. lock(EXCLUSIVE); currentlyLocked = true; return; } else { // Oh, someone else actually did signal us, apparently. Let's move on as if the futex // call told us so. break; } } default: KJ_FAIL_SYSCALL("futex(FUTEX_WAIT_PRIVATE)", error); } if (__atomic_load_n(&waiter.futex, __ATOMIC_ACQUIRE)) { // We received a lock ownership transfer from another thread. currentlyLocked = true; // The other thread checked the predicate before the transfer. #ifdef KJ_DEBUG assertLockedByCaller(EXCLUSIVE); #endif KJ_IF_MAYBE(exception, waiter.exception) { // The predicate threw an exception, apparently. Propagate it. // TODO(someday): Could we somehow have this be a recoverable exception? Presumably we'd // then want MutexGuarded::when() to skip calling the callback, but then what should it // return, since it normally returns the callback's result? Or maybe people who disable // exceptions just really should not write predicates that can throw. kj::throwFatalException(kj::mv(**exception)); } return; } } } } void Mutex::induceSpuriousWakeupForTest() { auto nextWaiter = waitersHead; for (;;) { KJ_IF_MAYBE(waiter, nextWaiter) { nextWaiter = waiter->next; syscall(SYS_futex, &waiter->futex, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0); } else { // No more waiters. break; } } } void Once::runOnce(Initializer& init) { startOver: uint state = UNINITIALIZED; if (__atomic_compare_exchange_n(&futex, &state, INITIALIZING, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { // It's our job to initialize! { KJ_ON_SCOPE_FAILURE({ // An exception was thrown by the initializer. We have to revert. if (__atomic_exchange_n(&futex, UNINITIALIZED, __ATOMIC_RELEASE) == INITIALIZING_WITH_WAITERS) { // Someone was waiting for us to finish. syscall(SYS_futex, &futex, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0); } }); init.run(); } if (__atomic_exchange_n(&futex, INITIALIZED, __ATOMIC_RELEASE) == INITIALIZING_WITH_WAITERS) { // Someone was waiting for us to finish. syscall(SYS_futex, &futex, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0); } } else { for (;;) { if (state == INITIALIZED) { break; } else if (state == INITIALIZING) { // Initialization is taking place in another thread. Indicate that we're waiting. if (!__atomic_compare_exchange_n(&futex, &state, INITIALIZING_WITH_WAITERS, true, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) { // State changed, retry. continue; } } else { KJ_DASSERT(state == INITIALIZING_WITH_WAITERS); } // Wait for initialization. syscall(SYS_futex, &futex, FUTEX_WAIT_PRIVATE, INITIALIZING_WITH_WAITERS, nullptr, nullptr, 0); state = __atomic_load_n(&futex, __ATOMIC_ACQUIRE); if (state == UNINITIALIZED) { // Oh hey, apparently whoever was trying to initialize gave up. Let's take it from the // top. goto startOver; } } } } void Once::reset() { uint state = INITIALIZED; if (!__atomic_compare_exchange_n(&futex, &state, UNINITIALIZED, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) { KJ_FAIL_REQUIRE("reset() called while not initialized."); } } #elif _WIN32 || __CYGWIN__ // ======================================================================================= // Win32 implementation #define coercedSrwLock (*reinterpret_cast(&srwLock)) #define coercedInitOnce (*reinterpret_cast(&initOnce)) #define coercedCondvar(var) (*reinterpret_cast(&var)) Mutex::Mutex() { static_assert(sizeof(SRWLOCK) == sizeof(srwLock), "SRWLOCK is not a pointer?"); InitializeSRWLock(&coercedSrwLock); } Mutex::~Mutex() {} void Mutex::lock(Exclusivity exclusivity) { switch (exclusivity) { case EXCLUSIVE: AcquireSRWLockExclusive(&coercedSrwLock); break; case SHARED: AcquireSRWLockShared(&coercedSrwLock); break; } } void Mutex::wakeReadyWaiter(Waiter* waiterToSkip) { // Look for a waiter whose predicate is now evaluating true, and wake it. We wake no more than // one waiter because only one waiter could get the lock anyway, and once it releases that lock // it will awake the next waiter if necessary. auto nextWaiter = waitersHead; for (;;) { KJ_IF_MAYBE(waiter, nextWaiter) { nextWaiter = waiter->next; if (waiter != waiterToSkip && checkPredicate(*waiter)) { // This waiter's predicate now evaluates true, so wake it up. It doesn't matter if we // use Wake vs. WakeAll here since there's always only one thread waiting. WakeConditionVariable(&coercedCondvar(waiter->condvar)); // We only need to wake one waiter. Note that unlike the futex-based implementation, we // cannot "transfer ownership" of the lock to the waiter, therefore we cannot guarantee // that the condition is still true when that waiter finally awakes. However, if the // condition is no longer true at that point, the waiter will re-check all other // waiters' conditions and possibly wake up any other waiter who is now ready, hence we // still only need to wake one waiter here. return; } } else { // No more waiters. break; } } } void Mutex::unlock(Exclusivity exclusivity, Waiter* waiterToSkip) { switch (exclusivity) { case EXCLUSIVE: { KJ_DEFER(ReleaseSRWLockExclusive(&coercedSrwLock)); // Check if there are any conditional waiters. Note we only do this when unlocking an // exclusive lock since under a shared lock the state couldn't have changed. wakeReadyWaiter(waiterToSkip); break; } case SHARED: ReleaseSRWLockShared(&coercedSrwLock); break; } } void Mutex::assertLockedByCaller(Exclusivity exclusivity) { // We could use TryAcquireSRWLock*() here like we do with the pthread version. However, as of // this writing, my version of Wine (1.6.2) doesn't implement these functions and will abort if // they are called. Since we were only going to use them as a hacky way to check if the lock is // held for debug purposes anyway, we just don't bother. } void Mutex::wait(Predicate& predicate, Maybe timeout) { // Add waiter to list. Waiter waiter { nullptr, waitersTail, predicate, nullptr, 0 }; static_assert(sizeof(waiter.condvar) == sizeof(CONDITION_VARIABLE), "CONDITION_VARIABLE is not a pointer?"); InitializeConditionVariable(&coercedCondvar(waiter.condvar)); addWaiter(waiter); KJ_DEFER(removeWaiter(waiter)); DWORD sleepMs; // Only initialized if `timeout` is non-null. const MonotonicClock* clock = nullptr; kj::Maybe endTime; KJ_IF_MAYBE(t, timeout) { // Windows sleeps are inaccurate -- they can be longer *or shorter* than the requested amount. // For many use cases of our API, a too-short sleep would be unacceptable. Experimentally, it // seems like sleeps can be up to half a millisecond short, so we'll add half a millisecond // (and then we round up, below). *t += 500 * kj::MICROSECONDS; // Compute initial sleep time. sleepMs = *t / kj::MILLISECONDS; if (*t % kj::MILLISECONDS > 0 * kj::SECONDS) { // We guarantee we won't wake up too early. ++sleepMs; } clock = &systemPreciseMonotonicClock(); endTime = clock->now() + *t; } else { sleepMs = INFINITE; } while (!predicate.check()) { // SleepConditionVariableSRW() will temporarily release the lock, so we need to signal other // waiters that are now ready. wakeReadyWaiter(&waiter); if (SleepConditionVariableSRW(&coercedCondvar(waiter.condvar), &coercedSrwLock, sleepMs, 0)) { // Normal result. Continue loop to check predicate. } else { DWORD error = GetLastError(); if (error == ERROR_TIMEOUT) { // Windows may have woken us up too early, so don't return yet. Instead, proceed through the // loop and rely on our sleep time recalculation to detect if we timed out. } else { KJ_FAIL_WIN32("SleepConditionVariableSRW()", error); } } KJ_IF_MAYBE(exception, waiter.exception) { // The predicate threw an exception, apparently. Propagate it. // TODO(someday): Could we somehow have this be a recoverable exception? Presumably we'd // then want MutexGuarded::when() to skip calling the callback, but then what should it // return, since it normally returns the callback's result? Or maybe people who disable // exceptions just really should not write predicates that can throw. kj::throwFatalException(kj::mv(**exception)); } // Recompute sleep time. KJ_IF_MAYBE(e, endTime) { auto now = clock->now(); if (*e > now) { auto sleepTime = *e - now; sleepMs = sleepTime / kj::MILLISECONDS; if (sleepTime % kj::MILLISECONDS > 0 * kj::SECONDS) { // We guarantee we won't wake up too early. ++sleepMs; } } else { // Oops, already timed out. return; } } } } void Mutex::induceSpuriousWakeupForTest() { auto nextWaiter = waitersHead; for (;;) { KJ_IF_MAYBE(waiter, nextWaiter) { nextWaiter = waiter->next; WakeConditionVariable(&coercedCondvar(waiter->condvar)); } else { // No more waiters. break; } } } static BOOL WINAPI nullInitializer(PINIT_ONCE initOnce, PVOID parameter, PVOID* context) { return true; } Once::Once(bool startInitialized) { static_assert(sizeof(INIT_ONCE) == sizeof(initOnce), "INIT_ONCE is not a pointer?"); InitOnceInitialize(&coercedInitOnce); if (startInitialized) { InitOnceExecuteOnce(&coercedInitOnce, &nullInitializer, nullptr, nullptr); } } Once::~Once() {} void Once::runOnce(Initializer& init) { BOOL needInit; while (!InitOnceBeginInitialize(&coercedInitOnce, 0, &needInit, nullptr)) { // Init was occurring in another thread, but then failed with an exception. Retry. } if (needInit) { { KJ_ON_SCOPE_FAILURE(InitOnceComplete(&coercedInitOnce, INIT_ONCE_INIT_FAILED, nullptr)); init.run(); } KJ_ASSERT(InitOnceComplete(&coercedInitOnce, 0, nullptr)); } } bool Once::isInitialized() noexcept { BOOL junk; return InitOnceBeginInitialize(&coercedInitOnce, INIT_ONCE_CHECK_ONLY, &junk, nullptr); } void Once::reset() { InitOnceInitialize(&coercedInitOnce); } #else // ======================================================================================= // Generic pthreads-based implementation #define KJ_PTHREAD_CALL(code) \ { \ int pthreadError = code; \ if (pthreadError != 0) { \ KJ_FAIL_SYSCALL(#code, pthreadError); \ } \ } #define KJ_PTHREAD_CLEANUP(code) \ { \ int pthreadError = code; \ if (pthreadError != 0) { \ KJ_LOG(ERROR, #code, strerror(pthreadError)); \ } \ } Mutex::Mutex(): mutex(PTHREAD_RWLOCK_INITIALIZER) {} Mutex::~Mutex() { KJ_PTHREAD_CLEANUP(pthread_rwlock_destroy(&mutex)); } void Mutex::lock(Exclusivity exclusivity) { switch (exclusivity) { case EXCLUSIVE: KJ_PTHREAD_CALL(pthread_rwlock_wrlock(&mutex)); break; case SHARED: KJ_PTHREAD_CALL(pthread_rwlock_rdlock(&mutex)); break; } } void Mutex::unlock(Exclusivity exclusivity, Waiter* waiterToSkip) { KJ_DEFER(KJ_PTHREAD_CALL(pthread_rwlock_unlock(&mutex))); if (exclusivity == EXCLUSIVE) { // Check if there are any conditional waiters. Note we only do this when unlocking an // exclusive lock since under a shared lock the state couldn't have changed. auto nextWaiter = waitersHead; for (;;) { KJ_IF_MAYBE(waiter, nextWaiter) { nextWaiter = waiter->next; if (waiter != waiterToSkip && checkPredicate(*waiter)) { // This waiter's predicate now evaluates true, so wake it up. It doesn't matter if we // use _signal() vs. _broadcast() here since there's always only one thread waiting. KJ_PTHREAD_CALL(pthread_mutex_lock(&waiter->stupidMutex)); KJ_PTHREAD_CALL(pthread_cond_signal(&waiter->condvar)); KJ_PTHREAD_CALL(pthread_mutex_unlock(&waiter->stupidMutex)); // We only need to wake one waiter. Note that unlike the futex-based implementation, we // cannot "transfer ownership" of the lock to the waiter, therefore we cannot guarantee // that the condition is still true when that waiter finally awakes. However, if the // condition is no longer true at that point, the waiter will re-check all other waiters' // conditions and possibly wake up any other waiter who is now ready, hence we still only // need to wake one waiter here. break; } } else { // No more waiters. break; } } } } void Mutex::assertLockedByCaller(Exclusivity exclusivity) { switch (exclusivity) { case EXCLUSIVE: // A read lock should fail if the mutex is already held for writing. if (pthread_rwlock_tryrdlock(&mutex) == 0) { pthread_rwlock_unlock(&mutex); KJ_FAIL_ASSERT("Tried to call getAlreadyLocked*() but lock is not held."); } break; case SHARED: // A write lock should fail if the mutex is already held for reading or writing. We don't // have any way to prove that the lock is held only for reading. if (pthread_rwlock_trywrlock(&mutex) == 0) { pthread_rwlock_unlock(&mutex); KJ_FAIL_ASSERT("Tried to call getAlreadyLocked*() but lock is not held."); } break; } } void Mutex::wait(Predicate& predicate, Maybe timeout) { // Add waiter to list. Waiter waiter { nullptr, waitersTail, predicate, nullptr, PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER }; addWaiter(waiter); // To guarantee that we've re-locked the mutex before scope exit, keep track of whether it is // currently. bool currentlyLocked = true; KJ_DEFER({ if (!currentlyLocked) lock(EXCLUSIVE); removeWaiter(waiter); // Destroy pthread objects. KJ_PTHREAD_CLEANUP(pthread_mutex_destroy(&waiter.stupidMutex)); KJ_PTHREAD_CLEANUP(pthread_cond_destroy(&waiter.condvar)); }); #if !__APPLE__ if (timeout != nullptr) { // Oops, the default condvar uses the wall clock, which is dumb... fix it to use the monotonic // clock. (Except not on macOS, where pthread_condattr_setclock() is unimplemented, but there's // a bizarre pthread_cond_timedwait_relative_np() method we can use instead...) pthread_condattr_t attr; KJ_PTHREAD_CALL(pthread_condattr_init(&attr)); KJ_PTHREAD_CALL(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); pthread_cond_init(&waiter.condvar, &attr); KJ_PTHREAD_CALL(pthread_condattr_destroy(&attr)); } #endif Maybe endTime = timeout.map([](Duration d) { return toAbsoluteTimespec(now() + d); }); while (!predicate.check()) { // pthread condvars only work with basic mutexes, not rwlocks. So, we need to lock a basic // mutex before we unlock the real mutex, and the signaling thread also needs to lock this // mutex, in order to ensure that this thread is actually waiting on the condvar before it is // signaled. KJ_PTHREAD_CALL(pthread_mutex_lock(&waiter.stupidMutex)); // OK, now we can unlock the main mutex. unlock(EXCLUSIVE, &waiter); currentlyLocked = false; bool timedOut = false; // Wait for someone to signal the condvar. KJ_IF_MAYBE(t, endTime) { #if __APPLE__ // On macOS, the absolute timeout can only be specified in wall time, not monotonic time, // which means modifying the system clock will break the wait. However, macOS happens to // provide an alternative relative-time wait function, so I guess we'll use that. It does // require recomputing the time every iteration... struct timespec ts = toRelativeTimespec(kj::max(toTimePoint(*t) - now(), 0 * kj::SECONDS)); int error = pthread_cond_timedwait_relative_np(&waiter.condvar, &waiter.stupidMutex, &ts); #else int error = pthread_cond_timedwait(&waiter.condvar, &waiter.stupidMutex, t); #endif if (error != 0) { if (error == ETIMEDOUT) { timedOut = true; } else { KJ_FAIL_SYSCALL("pthread_cond_timedwait", error); } } } else { KJ_PTHREAD_CALL(pthread_cond_wait(&waiter.condvar, &waiter.stupidMutex)); } // We have to be very careful about lock ordering here. We need to unlock stupidMutex before // re-locking the main mutex, because another thread may have a lock on the main mutex already // and be waiting for a lock on stupidMutex. Note that other thread may signal the condvar // right after we unlock stupidMutex but before we re-lock the main mutex. That is fine, // because we've already been signaled. KJ_PTHREAD_CALL(pthread_mutex_unlock(&waiter.stupidMutex)); lock(EXCLUSIVE); currentlyLocked = true; KJ_IF_MAYBE(exception, waiter.exception) { // The predicate threw an exception, apparently. Propagate it. // TODO(someday): Could we somehow have this be a recoverable exception? Presumably we'd // then want MutexGuarded::when() to skip calling the callback, but then what should it // return, since it normally returns the callback's result? Or maybe people who disable // exceptions just really should not write predicates that can throw. kj::throwFatalException(kj::mv(**exception)); } if (timedOut) { return; } } } void Mutex::induceSpuriousWakeupForTest() { auto nextWaiter = waitersHead; for (;;) { KJ_IF_MAYBE(waiter, nextWaiter) { nextWaiter = waiter->next; KJ_PTHREAD_CALL(pthread_mutex_lock(&waiter->stupidMutex)); KJ_PTHREAD_CALL(pthread_cond_signal(&waiter->condvar)); KJ_PTHREAD_CALL(pthread_mutex_unlock(&waiter->stupidMutex)); } else { // No more waiters. break; } } } Once::Once(bool startInitialized) : state(startInitialized ? INITIALIZED : UNINITIALIZED), mutex(PTHREAD_MUTEX_INITIALIZER) {} Once::~Once() { KJ_PTHREAD_CLEANUP(pthread_mutex_destroy(&mutex)); } void Once::runOnce(Initializer& init) { KJ_PTHREAD_CALL(pthread_mutex_lock(&mutex)); KJ_DEFER(KJ_PTHREAD_CALL(pthread_mutex_unlock(&mutex))); if (state != UNINITIALIZED) { return; } init.run(); __atomic_store_n(&state, INITIALIZED, __ATOMIC_RELEASE); } void Once::reset() { State oldState = INITIALIZED; if (!__atomic_compare_exchange_n(&state, &oldState, UNINITIALIZED, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) { KJ_FAIL_REQUIRE("reset() called while not initialized."); } } #endif } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/thread.c++0000644000175000017500000001063513340402540017704 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "thread.h" #include "debug.h" #if _WIN32 #include #include "windows-sanity.h" #else #include #include #endif namespace kj { #if _WIN32 Thread::Thread(Function func): state(new ThreadState(kj::mv(func))) { threadHandle = CreateThread(nullptr, 0, &runThread, state, 0, nullptr); if (threadHandle == nullptr) { state->unref(); KJ_FAIL_ASSERT("CreateThread failed."); } } Thread::~Thread() noexcept(false) { if (!detached) { KJ_DEFER(state->unref()); KJ_ASSERT(WaitForSingleObject(threadHandle, INFINITE) != WAIT_FAILED); KJ_IF_MAYBE(e, state->exception) { Exception ecopy = kj::mv(*e); state->exception = nullptr; // don't complain of uncaught exception when deleting kj::throwRecoverableException(kj::mv(ecopy)); } } } void Thread::detach() { KJ_ASSERT(CloseHandle(threadHandle)); detached = true; } #else // _WIN32 Thread::Thread(Function func): state(new ThreadState(kj::mv(func))) { static_assert(sizeof(threadId) >= sizeof(pthread_t), "pthread_t is larger than a long long on your platform. Please port."); int pthreadResult = pthread_create(reinterpret_cast(&threadId), nullptr, &runThread, state); if (pthreadResult != 0) { state->unref(); KJ_FAIL_SYSCALL("pthread_create", pthreadResult); } } Thread::~Thread() noexcept(false) { if (!detached) { KJ_DEFER(state->unref()); int pthreadResult = pthread_join(*reinterpret_cast(&threadId), nullptr); if (pthreadResult != 0) { KJ_FAIL_SYSCALL("pthread_join", pthreadResult) { break; } } KJ_IF_MAYBE(e, state->exception) { Exception ecopy = kj::mv(*e); state->exception = nullptr; // don't complain of uncaught exception when deleting kj::throwRecoverableException(kj::mv(ecopy)); } } } void Thread::sendSignal(int signo) { int pthreadResult = pthread_kill(*reinterpret_cast(&threadId), signo); if (pthreadResult != 0) { KJ_FAIL_SYSCALL("pthread_kill", pthreadResult) { break; } } } void Thread::detach() { int pthreadResult = pthread_detach(*reinterpret_cast(&threadId)); if (pthreadResult != 0) { KJ_FAIL_SYSCALL("pthread_detach", pthreadResult) { break; } } detached = true; state->unref(); } #endif // _WIN32, else Thread::ThreadState::ThreadState(Function func) : func(kj::mv(func)), initializer(getExceptionCallback().getThreadInitializer()), exception(nullptr), refcount(2) {} void Thread::ThreadState::unref() { #if _MSC_VER if (_InterlockedDecrement(&refcount) == 0) { #else if (__atomic_sub_fetch(&refcount, 1, __ATOMIC_RELEASE) == 0) { __atomic_thread_fence(__ATOMIC_ACQUIRE); #endif KJ_IF_MAYBE(e, exception) { KJ_LOG(ERROR, "uncaught exception thrown by detached thread", *e); } delete this; } } #if _WIN32 DWORD Thread::runThread(void* ptr) { #else void* Thread::runThread(void* ptr) { #endif ThreadState* state = reinterpret_cast(ptr); KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() { state->initializer(kj::mv(state->func)); })) { state->exception = kj::mv(*exception); } state->unref(); return 0; } } // namespace kj capnproto-c++-0.8.0/src/kj/time.c++0000644000175000017500000002220413650101756017377 0ustar00kentonkenton00000000000000// Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 #define WIN32_LEAN_AND_MEAN 1 // lolz #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #endif #include "time.h" #include "debug.h" #include #if _WIN32 #include #else #include #endif namespace kj { const Clock& nullClock() { class NullClock final: public Clock { public: Date now() const override { return UNIX_EPOCH; } }; static KJ_CONSTEXPR(const) NullClock NULL_CLOCK = NullClock(); return NULL_CLOCK; } #if _WIN32 namespace { static constexpr int64_t WIN32_EPOCH_OFFSET = 116444736000000000ull; // Number of 100ns intervals from Jan 1, 1601 to Jan 1, 1970. static Date toKjDate(FILETIME t) { int64_t value = (static_cast(t.dwHighDateTime) << 32) | t.dwLowDateTime; return (value - WIN32_EPOCH_OFFSET) * (100 * kj::NANOSECONDS) + UNIX_EPOCH; } class Win32CoarseClock: public Clock { public: Date now() const override { FILETIME ft; GetSystemTimeAsFileTime(&ft); return toKjDate(ft); } }; class Win32PreciseClock: public Clock { typedef VOID WINAPI GetSystemTimePreciseAsFileTimeFunc(LPFILETIME); public: Date now() const override { static const GetSystemTimePreciseAsFileTimeFunc* getSystemTimePreciseAsFileTimePtr = getGetSystemTimePreciseAsFileTime(); FILETIME ft; if (getSystemTimePreciseAsFileTimePtr == nullptr) { // We can't use QueryPerformanceCounter() to get any more precision because we have no way // of knowing when the calendar clock jumps. So I guess we're stuck. GetSystemTimeAsFileTime(&ft); } else { getSystemTimePreciseAsFileTimePtr(&ft); } return toKjDate(ft); } private: static GetSystemTimePreciseAsFileTimeFunc* getGetSystemTimePreciseAsFileTime() { // Dynamically look up the function GetSystemTimePreciseAsFileTimeFunc(). This was only // introduced as of Windows 8, so it might be missing. #if __GNUC__ && !__clang__ && __GNUC__ >= 8 // GCC 8 warns that our reinterpret_cast of a function pointer below is casting between // incompatible types. Yes, GCC, we know that. This is the nature of GetProcAddress(); it returns // everything as `long long int (*)()` and we have to cast to the actual type. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-function-type" #endif return reinterpret_cast(GetProcAddress( GetModuleHandleA("kernel32.dll"), "GetSystemTimePreciseAsFileTime")); } }; class Win32CoarseMonotonicClock: public MonotonicClock { public: TimePoint now() const override { return kj::origin() + GetTickCount64() * kj::MILLISECONDS; } }; class Win32PreciseMonotonicClock: public MonotonicClock { // Precise clock implemented using QueryPerformanceCounter(). // // TODO(someday): Windows 10 has QueryUnbiasedInterruptTime() and // QueryUnbiasedInterruptTimePrecise(), a new API for monotonic timing that isn't as difficult. // Is there any benefit to dynamically checking for these and using them if available? public: TimePoint now() const override { static const QpcProperties props; LARGE_INTEGER now; QueryPerformanceCounter(&now); uint64_t adjusted = now.QuadPart - props.origin; uint64_t ns = mulDiv64(adjusted, 1'000'000'000, props.frequency); return kj::origin() + ns * kj::NANOSECONDS; } private: struct QpcProperties { uint64_t origin; // What QueryPerformanceCounter() would have returned at the time when GetTickCount64() returned // zero. Used to ensure that the coarse and precise timers return similar values. uint64_t frequency; // From QueryPerformanceFrequency(). QpcProperties() { LARGE_INTEGER now, freqLi; uint64_t ticks = GetTickCount64(); QueryPerformanceCounter(&now); QueryPerformanceFrequency(&freqLi); frequency = freqLi.QuadPart; // Convert the millisecond tick count into performance counter ticks. uint64_t ticksAsQpc = mulDiv64(ticks, freqLi.QuadPart, 1000); origin = now.QuadPart - ticksAsQpc; } }; static inline uint64_t mulDiv64(uint64_t value, uint64_t numer, uint64_t denom) { // Inspired by: // https://github.com/rust-lang/rust/pull/22788/files#diff-24f054cd23f65af3b574c6ce8aa5a837R54 // Computes (value*numer)/denom without overflow, as long as both // (numer*denom) and the overall result fit into 64 bits. uint64_t q = value / denom; uint64_t r = value % denom; return q * numer + r * numer / denom; } }; } // namespace const Clock& systemCoarseCalendarClock() { static constexpr Win32CoarseClock clock; return clock; } const Clock& systemPreciseCalendarClock() { static constexpr Win32PreciseClock clock; return clock; } const MonotonicClock& systemCoarseMonotonicClock() { static constexpr Win32CoarseMonotonicClock clock; return clock; } const MonotonicClock& systemPreciseMonotonicClock() { static constexpr Win32PreciseMonotonicClock clock; return clock; } #else namespace { class PosixClock: public Clock { public: constexpr PosixClock(clockid_t clockId): clockId(clockId) {} Date now() const override { struct timespec ts; KJ_SYSCALL(clock_gettime(clockId, &ts)); return UNIX_EPOCH + ts.tv_sec * kj::SECONDS + ts.tv_nsec * kj::NANOSECONDS; } private: clockid_t clockId; }; class PosixMonotonicClock: public MonotonicClock { public: constexpr PosixMonotonicClock(clockid_t clockId): clockId(clockId) {} TimePoint now() const override { struct timespec ts; KJ_SYSCALL(clock_gettime(clockId, &ts)); return kj::origin() + ts.tv_sec * kj::SECONDS + ts.tv_nsec * kj::NANOSECONDS; } private: clockid_t clockId; }; } // namespace // FreeBSD has "_PRECISE", but Linux just defaults to precise. #ifndef CLOCK_REALTIME_PRECISE #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME #endif #ifndef CLOCK_MONOTONIC_PRECISE #define CLOCK_MONOTONIC_PRECISE CLOCK_MONOTONIC #endif // FreeBSD has "_FAST", Linux has "_COARSE". // MacOS has an "_APPROX" but only for CLOCK_MONOTONIC_RAW, which isn't helpful. #ifndef CLOCK_REALTIME_COARSE #ifdef CLOCK_REALTIME_FAST #define CLOCK_REALTIME_COARSE CLOCK_REALTIME_FAST #else #define CLOCK_REALTIME_COARSE CLOCK_REALTIME #endif #endif #ifndef CLOCK_MONOTONIC_COARSE #ifdef CLOCK_MONOTONIC_FAST #define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC_FAST #else #define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC #endif #endif const Clock& systemCoarseCalendarClock() { static constexpr PosixClock clock(CLOCK_REALTIME_COARSE); return clock; } const Clock& systemPreciseCalendarClock() { static constexpr PosixClock clock(CLOCK_REALTIME_PRECISE); return clock; } const MonotonicClock& systemCoarseMonotonicClock() { static constexpr PosixMonotonicClock clock(CLOCK_MONOTONIC_COARSE); return clock; } const MonotonicClock& systemPreciseMonotonicClock() { static constexpr PosixMonotonicClock clock(CLOCK_MONOTONIC_PRECISE); return clock; } #endif kj::String KJ_STRINGIFY(TimePoint t) { return kj::toCharSequence(t - kj::origin()); } kj::String KJ_STRINGIFY(Date d) { return kj::toCharSequence(d - UNIX_EPOCH); } kj::String KJ_STRINGIFY(Duration d) { auto digits = kj::toCharSequence(d / kj::NANOSECONDS); ArrayPtr arr = digits; size_t point; kj::StringPtr suffix; kj::Duration unit; if (digits.size() > 9) { point = arr.size() - 9; suffix = "s"; unit = kj::SECONDS; } else if (digits.size() > 6) { point = arr.size() - 6; suffix = "ms"; unit = kj::MILLISECONDS; } else if (digits.size() > 3) { point = arr.size() - 3; suffix = "μs"; unit = kj::MICROSECONDS; } else { point = arr.size(); suffix = "ns"; unit = kj::NANOSECONDS; } if (d % unit == 0 * kj::NANOSECONDS) { return kj::str(arr.slice(0, point), suffix); } else { while (arr.back() == '0') { arr = arr.slice(0, arr.size() - 1); } KJ_ASSERT(arr.size() > point); return kj::str(arr.slice(0, point), ".", arr.slice(point, arr.size()), suffix); } } } // namespace kj capnproto-c++-0.8.0/src/kj/filesystem.c++0000644000175000017500000016142013650101756020631 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "filesystem.h" #include "vector.h" #include "debug.h" #include "one-of.h" #include "encoding.h" #include "refcount.h" #include "mutex.h" #include namespace kj { Path::Path(StringPtr name): Path(heapString(name)) {} Path::Path(String&& name): parts(heapArray(1)) { parts[0] = kj::mv(name); validatePart(parts[0]); } Path::Path(ArrayPtr parts) : Path(KJ_MAP(p, parts) { return heapString(p); }) {} Path::Path(Array partsParam) : Path(kj::mv(partsParam), ALREADY_CHECKED) { for (auto& p: parts) { validatePart(p); } } Path PathPtr::clone() { return Path(KJ_MAP(p, parts) { return heapString(p); }, Path::ALREADY_CHECKED); } Path Path::parse(StringPtr path) { KJ_REQUIRE(!path.startsWith("/"), "expected a relative path, got absolute", path) { // When exceptions are disabled, go on -- the leading '/' will end up ignored. break; } return evalImpl(Vector(countParts(path)), path); } Path Path::parseWin32Api(ArrayPtr text) { auto utf8 = decodeWideString(text); return evalWin32Impl(Vector(countPartsWin32(utf8)), utf8, true); } Path PathPtr::append(Path&& suffix) const { auto newParts = kj::heapArrayBuilder(parts.size() + suffix.parts.size()); for (auto& p: parts) newParts.add(heapString(p)); for (auto& p: suffix.parts) newParts.add(kj::mv(p)); return Path(newParts.finish(), Path::ALREADY_CHECKED); } Path Path::append(Path&& suffix) && { auto newParts = kj::heapArrayBuilder(parts.size() + suffix.parts.size()); for (auto& p: parts) newParts.add(kj::mv(p)); for (auto& p: suffix.parts) newParts.add(kj::mv(p)); return Path(newParts.finish(), ALREADY_CHECKED); } Path PathPtr::append(PathPtr suffix) const { auto newParts = kj::heapArrayBuilder(parts.size() + suffix.parts.size()); for (auto& p: parts) newParts.add(heapString(p)); for (auto& p: suffix.parts) newParts.add(heapString(p)); return Path(newParts.finish(), Path::ALREADY_CHECKED); } Path Path::append(PathPtr suffix) && { auto newParts = kj::heapArrayBuilder(parts.size() + suffix.parts.size()); for (auto& p: parts) newParts.add(kj::mv(p)); for (auto& p: suffix.parts) newParts.add(heapString(p)); return Path(newParts.finish(), ALREADY_CHECKED); } Path PathPtr::eval(StringPtr pathText) const { if (pathText.startsWith("/")) { // Optimization: avoid copying parts that will just be dropped. return Path::evalImpl(Vector(Path::countParts(pathText)), pathText); } else { Vector newParts(parts.size() + Path::countParts(pathText)); for (auto& p: parts) newParts.add(heapString(p)); return Path::evalImpl(kj::mv(newParts), pathText); } } Path Path::eval(StringPtr pathText) && { if (pathText.startsWith("/")) { // Optimization: avoid copying parts that will just be dropped. return evalImpl(Vector(countParts(pathText)), pathText); } else { Vector newParts(parts.size() + countParts(pathText)); for (auto& p: parts) newParts.add(kj::mv(p)); return evalImpl(kj::mv(newParts), pathText); } } PathPtr PathPtr::basename() const { KJ_REQUIRE(parts.size() > 0, "root path has no basename"); return PathPtr(parts.slice(parts.size() - 1, parts.size())); } Path Path::basename() && { KJ_REQUIRE(parts.size() > 0, "root path has no basename"); auto newParts = kj::heapArrayBuilder(1); newParts.add(kj::mv(parts[parts.size() - 1])); return Path(newParts.finish(), ALREADY_CHECKED); } PathPtr PathPtr::parent() const { KJ_REQUIRE(parts.size() > 0, "root path has no parent"); return PathPtr(parts.slice(0, parts.size() - 1)); } Path Path::parent() && { KJ_REQUIRE(parts.size() > 0, "root path has no parent"); return Path(KJ_MAP(p, parts.slice(0, parts.size() - 1)) { return kj::mv(p); }, ALREADY_CHECKED); } String PathPtr::toString(bool absolute) const { if (parts.size() == 0) { // Special-case empty path. return absolute ? kj::str("/") : kj::str("."); } size_t size = absolute + (parts.size() - 1); for (auto& p: parts) size += p.size(); String result = kj::heapString(size); char* ptr = result.begin(); bool leadingSlash = absolute; for (auto& p: parts) { if (leadingSlash) *ptr++ = '/'; leadingSlash = true; memcpy(ptr, p.begin(), p.size()); ptr += p.size(); } KJ_ASSERT(ptr == result.end()); return result; } Path Path::slice(size_t start, size_t end) && { return Path(KJ_MAP(p, parts.slice(start, end)) { return kj::mv(p); }); } bool PathPtr::operator==(PathPtr other) const { return parts == other.parts; } bool PathPtr::operator< (PathPtr other) const { for (size_t i = 0; i < kj::min(parts.size(), other.parts.size()); i++) { int comp = strcmp(parts[i].cStr(), other.parts[i].cStr()); if (comp < 0) return true; if (comp > 0) return false; } return parts.size() < other.parts.size(); } bool PathPtr::startsWith(PathPtr prefix) const { return parts.size() >= prefix.parts.size() && parts.slice(0, prefix.parts.size()) == prefix.parts; } bool PathPtr::endsWith(PathPtr suffix) const { return parts.size() >= suffix.parts.size() && parts.slice(parts.size() - suffix.parts.size(), parts.size()) == suffix.parts; } Path PathPtr::evalWin32(StringPtr pathText) const { Vector newParts(parts.size() + Path::countPartsWin32(pathText)); for (auto& p: parts) newParts.add(heapString(p)); return Path::evalWin32Impl(kj::mv(newParts), pathText); } Path Path::evalWin32(StringPtr pathText) && { Vector newParts(parts.size() + countPartsWin32(pathText)); for (auto& p: parts) newParts.add(kj::mv(p)); return evalWin32Impl(kj::mv(newParts), pathText); } String PathPtr::toWin32StringImpl(bool absolute, bool forApi) const { if (parts.size() == 0) { // Special-case empty path. KJ_REQUIRE(!absolute, "absolute path is missing disk designator") { break; } return absolute ? kj::str("\\\\") : kj::str("."); } bool isUncPath = false; if (absolute) { if (Path::isWin32Drive(parts[0])) { // It's a win32 drive } else if (Path::isNetbiosName(parts[0])) { isUncPath = true; } else { KJ_FAIL_REQUIRE("absolute win32 path must start with drive letter or netbios host name", parts[0]); } } else { // Currently we do nothing differently in the forApi case for relative paths. forApi = false; } size_t size = forApi ? (isUncPath ? 8 : 4) + (parts.size() - 1) : (isUncPath ? 2 : 0) + (parts.size() - 1); for (auto& p: parts) size += p.size(); String result = heapString(size); char* ptr = result.begin(); if (forApi) { *ptr++ = '\\'; *ptr++ = '\\'; *ptr++ = '?'; *ptr++ = '\\'; if (isUncPath) { *ptr++ = 'U'; *ptr++ = 'N'; *ptr++ = 'C'; *ptr++ = '\\'; } } else { if (isUncPath) { *ptr++ = '\\'; *ptr++ = '\\'; } } bool leadingSlash = false; for (auto& p: parts) { if (leadingSlash) *ptr++ = '\\'; leadingSlash = true; KJ_REQUIRE(!Path::isWin32Special(p), "path cannot contain DOS reserved name", p) { // Recover by blotting out the name with invalid characters which Win32 syscalls will reject. for (size_t i = 0; i < p.size(); i++) { *ptr++ = '|'; } goto skip; } memcpy(ptr, p.begin(), p.size()); ptr += p.size(); skip:; } KJ_ASSERT(ptr == result.end()); // Check for colons (other than in drive letter), which on NTFS would be interpreted as an // "alternate data stream", which can lead to surprising results. If we want to support ADS, we // should do so using an explicit API. Note that this check also prevents a relative path from // appearing to start with a drive letter. for (size_t i: kj::indices(result)) { if (result[i] == ':') { if (absolute && i == (forApi ? 5 : 1)) { // False alarm: this is the drive letter. } else { KJ_FAIL_REQUIRE( "colons are prohibited in win32 paths to avoid triggering alterante data streams", result) { // Recover by using a different character which we know Win32 syscalls will reject. result[i] = '|'; break; } } } } return result; } Array PathPtr::forWin32Api(bool absolute) const { return encodeWideString(toWin32StringImpl(absolute, true), true); } // ----------------------------------------------------------------------------- String Path::stripNul(String input) { kj::Vector output(input.size()); for (char c: input) { if (c != '\0') output.add(c); } output.add('\0'); return String(output.releaseAsArray()); } void Path::validatePart(StringPtr part) { KJ_REQUIRE(part != "" && part != "." && part != "..", "invalid path component", part); KJ_REQUIRE(strlen(part.begin()) == part.size(), "NUL character in path component", part); KJ_REQUIRE(part.findFirst('/') == nullptr, "'/' character in path component; did you mean to use Path::parse()?", part); } void Path::evalPart(Vector& parts, ArrayPtr part) { if (part.size() == 0) { // Ignore consecutive or trailing '/'s. } else if (part.size() == 1 && part[0] == '.') { // Refers to current directory; ignore. } else if (part.size() == 2 && part[0] == '.' && part [1] == '.') { KJ_REQUIRE(parts.size() > 0, "can't use \"..\" to break out of starting directory") { // When exceptions are disabled, ignore. return; } parts.removeLast(); } else { auto str = heapString(part); KJ_REQUIRE(strlen(str.begin()) == str.size(), "NUL character in path component", str) { // When exceptions are disabled, strip out '\0' chars. str = stripNul(kj::mv(str)); break; } parts.add(kj::mv(str)); } } Path Path::evalImpl(Vector&& parts, StringPtr path) { if (path.startsWith("/")) { parts.clear(); } size_t partStart = 0; for (auto i: kj::indices(path)) { if (path[i] == '/') { evalPart(parts, path.slice(partStart, i)); partStart = i + 1; } } evalPart(parts, path.slice(partStart)); return Path(parts.releaseAsArray(), Path::ALREADY_CHECKED); } Path Path::evalWin32Impl(Vector&& parts, StringPtr path, bool fromApi) { // Convert all forward slashes to backslashes. String ownPath; if (!fromApi && path.findFirst('/') != nullptr) { ownPath = heapString(path); for (char& c: ownPath) { if (c == '/') c = '\\'; } path = ownPath; } // Interpret various forms of absolute paths. if (fromApi && path.startsWith("\\\\?\\")) { path = path.slice(4); if (path.startsWith("UNC\\")) { path = path.slice(4); } // The path is absolute. parts.clear(); } else if (path.startsWith("\\\\")) { // UNC path. path = path.slice(2); // This path is absolute. The first component is a server name. parts.clear(); } else if (path.startsWith("\\")) { KJ_REQUIRE(!fromApi, "parseWin32Api() requires absolute path"); // Path is relative to the current drive / network share. if (parts.size() >= 1 && isWin32Drive(parts[0])) { // Leading \ interpreted as root of current drive. parts.truncate(1); } else if (parts.size() >= 2) { // Leading \ interpreted as root of current network share (which is indicated by the first // *two* components of the path). parts.truncate(2); } else { KJ_FAIL_REQUIRE("must specify drive letter", path) { // Recover by assuming C drive. parts.clear(); parts.add(kj::str("c:")); break; } } } else if ((path.size() == 2 || (path.size() > 2 && path[2] == '\\')) && isWin32Drive(path.slice(0, 2))) { // Starts with a drive letter. parts.clear(); } else { KJ_REQUIRE(!fromApi, "parseWin32Api() requires absolute path"); } size_t partStart = 0; for (auto i: kj::indices(path)) { if (path[i] == '\\') { evalPart(parts, path.slice(partStart, i)); partStart = i + 1; } } evalPart(parts, path.slice(partStart)); return Path(parts.releaseAsArray(), Path::ALREADY_CHECKED); } size_t Path::countParts(StringPtr path) { size_t result = 1; for (char c: path) { result += (c == '/'); } return result; } size_t Path::countPartsWin32(StringPtr path) { size_t result = 1; for (char c: path) { result += (c == '/' || c == '\\'); } return result; } bool Path::isWin32Drive(ArrayPtr part) { return part.size() == 2 && part[1] == ':' && (('a' <= part[0] && part[0] <= 'z') || ('A' <= part[0] && part[0] <= 'Z')); } bool Path::isNetbiosName(ArrayPtr part) { // Characters must be alphanumeric or '.' or '-'. for (char c: part) { if (c != '.' && c != '-' && (c < 'a' || 'z' < c) && (c < 'A' || 'Z' < c) && (c < '0' || '9' < c)) { return false; } } // Can't be empty nor start or end with a '.' or a '-'. return part.size() > 0 && part[0] != '.' && part[0] != '-' && part[part.size() - 1] != '.' && part[part.size() - 1] != '-'; } bool Path::isWin32Special(StringPtr part) { bool isNumbered; if (part.size() == 3 || (part.size() > 3 && part[3] == '.')) { // Filename is three characters or three characters followed by an extension. isNumbered = false; } else if ((part.size() == 4 || (part.size() > 4 && part[4] == '.')) && '1' <= part[3] && part[3] <= '9') { // Filename is four characters or four characters followed by an extension, and the fourth // character is a nonzero digit. isNumbered = true; } else { return false; } // OK, this could be a Win32 special filename. We need to match the first three letters against // the list of specials, case-insensitively. char tmp[4]; memcpy(tmp, part.begin(), 3); tmp[3] = '\0'; for (char& c: tmp) { if ('A' <= c && c <= 'Z') { c += 'a' - 'A'; } } StringPtr str(tmp, 3); if (isNumbered) { // Specials that are followed by a digit. return str == "com" || str == "lpt"; } else { // Specials that are not followed by a digit. return str == "con" || str == "prn" || str == "aux" || str == "nul"; } } // ======================================================================================= String ReadableFile::readAllText() const { String result = heapString(stat().size); size_t n = read(0, result.asBytes()); if (n < result.size()) { // Apparently file was truncated concurrently. Reduce to new size to match. result = heapString(result.slice(0, n)); } return result; } Array ReadableFile::readAllBytes() const { Array result = heapArray(stat().size); size_t n = read(0, result.asBytes()); if (n < result.size()) { // Apparently file was truncated concurrently. Reduce to new size to match. result = heapArray(result.slice(0, n)); } return result; } void File::writeAll(ArrayPtr bytes) const { truncate(0); write(0, bytes); } void File::writeAll(StringPtr text) const { writeAll(text.asBytes()); } size_t File::copy(uint64_t offset, const ReadableFile& from, uint64_t fromOffset, uint64_t size) const { byte buffer[8192]; size_t result = 0; while (size > 0) { size_t n = from.read(fromOffset, kj::arrayPtr(buffer, kj::min(sizeof(buffer), size))); write(offset, arrayPtr(buffer, n)); result += n; if (n < sizeof(buffer)) { // Either we copied the amount requested or we hit EOF. break; } fromOffset += n; offset += n; size -= n; } return result; } FsNode::Metadata ReadableDirectory::lstat(PathPtr path) const { KJ_IF_MAYBE(meta, tryLstat(path)) { return *meta; } else { KJ_FAIL_REQUIRE("no such file", path) { break; } return FsNode::Metadata(); } } Own ReadableDirectory::openFile(PathPtr path) const { KJ_IF_MAYBE(file, tryOpenFile(path)) { return kj::mv(*file); } else { KJ_FAIL_REQUIRE("no such directory", path) { break; } return newInMemoryFile(nullClock()); } } Own ReadableDirectory::openSubdir(PathPtr path) const { KJ_IF_MAYBE(dir, tryOpenSubdir(path)) { return kj::mv(*dir); } else { KJ_FAIL_REQUIRE("no such file or directory", path) { break; } return newInMemoryDirectory(nullClock()); } } String ReadableDirectory::readlink(PathPtr path) const { KJ_IF_MAYBE(p, tryReadlink(path)) { return kj::mv(*p); } else { KJ_FAIL_REQUIRE("not a symlink", path) { break; } return kj::str("."); } } Own Directory::openFile(PathPtr path, WriteMode mode) const { KJ_IF_MAYBE(f, tryOpenFile(path, mode)) { return kj::mv(*f); } else if (has(mode, WriteMode::CREATE) && !has(mode, WriteMode::MODIFY)) { KJ_FAIL_REQUIRE("file already exists", path) { break; } } else if (has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_REQUIRE("file does not exist", path) { break; } } else if (!has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_ASSERT("neither WriteMode::CREATE nor WriteMode::MODIFY was given", path) { break; } } else { // Shouldn't happen. KJ_FAIL_ASSERT("tryOpenFile() returned null despite no preconditions", path) { break; } } return newInMemoryFile(nullClock()); } Own Directory::appendFile(PathPtr path, WriteMode mode) const { KJ_IF_MAYBE(f, tryAppendFile(path, mode)) { return kj::mv(*f); } else if (has(mode, WriteMode::CREATE) && !has(mode, WriteMode::MODIFY)) { KJ_FAIL_REQUIRE("file already exists", path) { break; } } else if (has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_REQUIRE("file does not exist", path) { break; } } else if (!has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_ASSERT("neither WriteMode::CREATE nor WriteMode::MODIFY was given", path) { break; } } else { // Shouldn't happen. KJ_FAIL_ASSERT("tryAppendFile() returned null despite no preconditions", path) { break; } } return newFileAppender(newInMemoryFile(nullClock())); } Own Directory::openSubdir(PathPtr path, WriteMode mode) const { KJ_IF_MAYBE(f, tryOpenSubdir(path, mode)) { return kj::mv(*f); } else if (has(mode, WriteMode::CREATE) && !has(mode, WriteMode::MODIFY)) { KJ_FAIL_REQUIRE("directory already exists", path) { break; } } else if (has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_REQUIRE("directory does not exist", path) { break; } } else if (!has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_ASSERT("neither WriteMode::CREATE nor WriteMode::MODIFY was given", path) { break; } } else { // Shouldn't happen. KJ_FAIL_ASSERT("tryOpenSubdir() returned null despite no preconditions", path) { break; } } return newInMemoryDirectory(nullClock()); } void Directory::symlink(PathPtr linkpath, StringPtr content, WriteMode mode) const { if (!trySymlink(linkpath, content, mode)) { if (has(mode, WriteMode::CREATE)) { KJ_FAIL_REQUIRE("path already exists", linkpath) { break; } } else { // Shouldn't happen. KJ_FAIL_ASSERT("symlink() returned null despite no preconditions", linkpath) { break; } } } } void Directory::transfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const { if (!tryTransfer(toPath, toMode, fromDirectory, fromPath, mode)) { if (has(toMode, WriteMode::CREATE)) { KJ_FAIL_REQUIRE("toPath already exists or fromPath doesn't exist", toPath, fromPath) { break; } } else { KJ_FAIL_ASSERT("fromPath doesn't exist", fromPath) { break; } } } } static void copyContents(const Directory& to, const ReadableDirectory& from); static bool tryCopyDirectoryEntry(const Directory& to, PathPtr toPath, WriteMode toMode, const ReadableDirectory& from, PathPtr fromPath, FsNode::Type type, bool atomic) { // TODO(cleanup): Make this reusable? switch (type) { case FsNode::Type::FILE: { KJ_IF_MAYBE(fromFile, from.tryOpenFile(fromPath)) { if (atomic) { auto replacer = to.replaceFile(toPath, toMode); replacer->get().copy(0, **fromFile, 0, kj::maxValue); return replacer->tryCommit(); } else KJ_IF_MAYBE(toFile, to.tryOpenFile(toPath, toMode)) { toFile->get()->copy(0, **fromFile, 0, kj::maxValue); return true; } else { return false; } } else { // Apparently disappeared. Treat as source-doesn't-exist. return false; } } case FsNode::Type::DIRECTORY: KJ_IF_MAYBE(fromSubdir, from.tryOpenSubdir(fromPath)) { if (atomic) { auto replacer = to.replaceSubdir(toPath, toMode); copyContents(replacer->get(), **fromSubdir); return replacer->tryCommit(); } else KJ_IF_MAYBE(toSubdir, to.tryOpenSubdir(toPath, toMode)) { copyContents(**toSubdir, **fromSubdir); return true; } else { return false; } } else { // Apparently disappeared. Treat as source-doesn't-exist. return false; } case FsNode::Type::SYMLINK: KJ_IF_MAYBE(content, from.tryReadlink(fromPath)) { return to.trySymlink(toPath, *content, toMode); } else { // Apparently disappeared. Treat as source-doesn't-exist. return false; } break; default: // Note: Unclear whether it's better to throw an error here or just ignore it / log a // warning. Can reconsider when we see an actual use case. KJ_FAIL_REQUIRE("can only copy files, directories, and symlinks", fromPath) { return false; } } } static void copyContents(const Directory& to, const ReadableDirectory& from) { for (auto& entry: from.listEntries()) { Path subPath(kj::mv(entry.name)); tryCopyDirectoryEntry(to, subPath, WriteMode::CREATE, from, subPath, entry.type, false); } } bool Directory::tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const { KJ_REQUIRE(toPath.size() > 0, "can't replace self") { return false; } // First try reversing. KJ_IF_MAYBE(result, fromDirectory.tryTransferTo(*this, toPath, toMode, fromPath, mode)) { return *result; } switch (mode) { case TransferMode::COPY: KJ_IF_MAYBE(meta, fromDirectory.tryLstat(fromPath)) { return tryCopyDirectoryEntry(*this, toPath, toMode, fromDirectory, fromPath, meta->type, true); } else { // Source doesn't exist. return false; } case TransferMode::MOVE: // Implement move as copy-then-delete. if (!tryTransfer(toPath, toMode, fromDirectory, fromPath, TransferMode::COPY)) { return false; } fromDirectory.remove(fromPath); return true; case TransferMode::LINK: KJ_FAIL_REQUIRE("can't link across different Directory implementations") { return false; } } KJ_UNREACHABLE; } Maybe Directory::tryTransferTo(const Directory& toDirectory, PathPtr toPath, WriteMode toMode, PathPtr fromPath, TransferMode mode) const { return nullptr; } void Directory::remove(PathPtr path) const { if (!tryRemove(path)) { KJ_FAIL_REQUIRE("path to remove doesn't exist", path) { break; } } } void Directory::commitFailed(WriteMode mode) { if (has(mode, WriteMode::CREATE) && !has(mode, WriteMode::MODIFY)) { KJ_FAIL_REQUIRE("replace target already exists") { break; } } else if (has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_REQUIRE("replace target does not exist") { break; } } else if (!has(mode, WriteMode::MODIFY) && !has(mode, WriteMode::CREATE)) { KJ_FAIL_ASSERT("neither WriteMode::CREATE nor WriteMode::MODIFY was given") { break; } } else { KJ_FAIL_ASSERT("tryCommit() returned null despite no preconditions") { break; } } } // ======================================================================================= namespace { class InMemoryFile final: public File, public AtomicRefcounted { public: InMemoryFile(const Clock& clock): impl(clock) {} Own cloneFsNode() const override { return atomicAddRef(*this); } Maybe getFd() const override { return nullptr; } Metadata stat() const override { auto lock = impl.lockShared(); uint64_t hash = reinterpret_cast(this); return Metadata { Type::FILE, lock->size, lock->size, lock->lastModified, 1, hash }; } void sync() const override {} void datasync() const override {} // no-ops size_t read(uint64_t offset, ArrayPtr buffer) const override { auto lock = impl.lockShared(); if (offset >= lock->size) { // Entirely out-of-range. return 0; } size_t readSize = kj::min(buffer.size(), lock->size - offset); memcpy(buffer.begin(), lock->bytes.begin() + offset, readSize); return readSize; } Array mmap(uint64_t offset, uint64_t size) const override { KJ_REQUIRE(offset + size >= offset, "mmap() request overflows uint64"); auto lock = impl.lockExclusive(); lock->ensureCapacity(offset + size); ArrayDisposer* disposer = new MmapDisposer(atomicAddRef(*this)); return Array(lock->bytes.begin() + offset, size, *disposer); } Array mmapPrivate(uint64_t offset, uint64_t size) const override { // Return a copy. // Allocate exactly the size requested. auto result = heapArray(size); // Use read() to fill it. size_t actual = read(offset, result); // Ignore the rest. if (actual < size) { memset(result.begin() + actual, 0, size - actual); } return result; } void write(uint64_t offset, ArrayPtr data) const override { if (data.size() == 0) return; auto lock = impl.lockExclusive(); lock->modified(); uint64_t end = offset + data.size(); KJ_REQUIRE(end >= offset, "write() request overflows uint64"); lock->ensureCapacity(end); lock->size = kj::max(lock->size, end); memcpy(lock->bytes.begin() + offset, data.begin(), data.size()); } void zero(uint64_t offset, uint64_t zeroSize) const override { if (zeroSize == 0) return; auto lock = impl.lockExclusive(); lock->modified(); uint64_t end = offset + zeroSize; KJ_REQUIRE(end >= offset, "zero() request overflows uint64"); lock->ensureCapacity(end); lock->size = kj::max(lock->size, end); memset(lock->bytes.begin() + offset, 0, zeroSize); } void truncate(uint64_t newSize) const override { auto lock = impl.lockExclusive(); if (newSize < lock->size) { lock->modified(); memset(lock->bytes.begin() + newSize, 0, lock->size - newSize); lock->size = newSize; } else if (newSize > lock->size) { lock->modified(); lock->ensureCapacity(newSize); lock->size = newSize; } } Own mmapWritable(uint64_t offset, uint64_t size) const override { uint64_t end = offset + size; KJ_REQUIRE(end >= offset, "mmapWritable() request overflows uint64"); auto lock = impl.lockExclusive(); lock->ensureCapacity(end); return heap(atomicAddRef(*this), lock->bytes.slice(offset, end)); } size_t copy(uint64_t offset, const ReadableFile& from, uint64_t fromOffset, uint64_t copySize) const override { size_t fromFileSize = from.stat().size; if (fromFileSize <= fromOffset) return 0; // Clamp size to EOF. copySize = kj::min(copySize, fromFileSize - fromOffset); if (copySize == 0) return 0; auto lock = impl.lockExclusive(); // Allocate space for the copy. uint64_t end = offset + copySize; lock->ensureCapacity(end); // Read directly into our backing store. size_t n = from.read(fromOffset, lock->bytes.slice(offset, end)); lock->size = kj::max(lock->size, offset + n); lock->modified(); return n; } private: struct Impl { const Clock& clock; Array bytes; size_t size = 0; // bytes may be larger than this to accommodate mmaps Date lastModified; uint mmapCount = 0; // number of mappings outstanding Impl(const Clock& clock): clock(clock), lastModified(clock.now()) {} void ensureCapacity(size_t capacity) { if (bytes.size() < capacity) { KJ_ASSERT(mmapCount == 0, "InMemoryFile cannot resize the file backing store while memory mappings exist."); auto newBytes = heapArray(kj::max(capacity, bytes.size() * 2)); memcpy(newBytes.begin(), bytes.begin(), size); memset(newBytes.begin() + size, 0, newBytes.size() - size); bytes = kj::mv(newBytes); } } void modified() { lastModified = clock.now(); } }; kj::MutexGuarded impl; class MmapDisposer final: public ArrayDisposer { public: MmapDisposer(Own&& refParam): ref(kj::mv(refParam)) { ++ref->impl.getAlreadyLockedExclusive().mmapCount; } ~MmapDisposer() noexcept(false) { --ref->impl.lockExclusive()->mmapCount; } void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const override { delete this; } private: Own ref; }; class WritableFileMappingImpl final: public WritableFileMapping { public: WritableFileMappingImpl(Own&& refParam, ArrayPtr range) : ref(kj::mv(refParam)), range(range) { ++ref->impl.getAlreadyLockedExclusive().mmapCount; } ~WritableFileMappingImpl() noexcept(false) { --ref->impl.lockExclusive()->mmapCount; } ArrayPtr get() const override { // const_cast OK because WritableFileMapping does indeed provide a writable view despite // being const itself. return arrayPtr(const_cast(range.begin()), range.size()); } void changed(ArrayPtr slice) const override { ref->impl.lockExclusive()->modified(); } void sync(ArrayPtr slice) const override { ref->impl.lockExclusive()->modified(); } private: Own ref; ArrayPtr range; }; }; // ----------------------------------------------------------------------------- class InMemoryDirectory final: public Directory, public AtomicRefcounted { public: InMemoryDirectory(const Clock& clock): impl(clock) {} Own cloneFsNode() const override { return atomicAddRef(*this); } Maybe getFd() const override { return nullptr; } Metadata stat() const override { auto lock = impl.lockShared(); uint64_t hash = reinterpret_cast(this); return Metadata { Type::DIRECTORY, 0, 0, lock->lastModified, 1, hash }; } void sync() const override {} void datasync() const override {} // no-ops Array listNames() const override { auto lock = impl.lockShared(); return KJ_MAP(e, lock->entries) { return heapString(e.first); }; } Array listEntries() const override { auto lock = impl.lockShared(); return KJ_MAP(e, lock->entries) { FsNode::Type type; if (e.second.node.is()) { type = FsNode::Type::SYMLINK; } else if (e.second.node.is()) { type = FsNode::Type::FILE; } else { KJ_ASSERT(e.second.node.is()); type = FsNode::Type::DIRECTORY; } return Entry { type, heapString(e.first) }; }; } bool exists(PathPtr path) const override { if (path.size() == 0) { return true; } else if (path.size() == 1) { auto lock = impl.lockShared(); KJ_IF_MAYBE(entry, lock->tryGetEntry(path[0])) { return exists(lock, *entry); } else { return false; } } else { KJ_IF_MAYBE(subdir, tryGetParent(path[0])) { return subdir->get()->exists(path.slice(1, path.size())); } else { return false; } } } Maybe tryLstat(PathPtr path) const override { if (path.size() == 0) { return stat(); } else if (path.size() == 1) { auto lock = impl.lockShared(); KJ_IF_MAYBE(entry, lock->tryGetEntry(path[0])) { if (entry->node.is()) { return entry->node.get().file->stat(); } else if (entry->node.is()) { return entry->node.get().directory->stat(); } else if (entry->node.is()) { auto& link = entry->node.get(); uint64_t hash = reinterpret_cast(link.content.begin()); return FsNode::Metadata { FsNode::Type::SYMLINK, 0, 0, link.lastModified, 1, hash }; } else { KJ_FAIL_ASSERT("unknown node type") { return nullptr; } } } else { return nullptr; } } else { KJ_IF_MAYBE(subdir, tryGetParent(path[0])) { return subdir->get()->tryLstat(path.slice(1, path.size())); } else { return nullptr; } } } Maybe> tryOpenFile(PathPtr path) const override { if (path.size() == 0) { KJ_FAIL_REQUIRE("not a file") { return nullptr; } } else if (path.size() == 1) { auto lock = impl.lockShared(); KJ_IF_MAYBE(entry, lock->tryGetEntry(path[0])) { return asFile(lock, *entry); } else { return nullptr; } } else { KJ_IF_MAYBE(subdir, tryGetParent(path[0])) { return subdir->get()->tryOpenFile(path.slice(1, path.size())); } else { return nullptr; } } } Maybe> tryOpenSubdir(PathPtr path) const override { if (path.size() == 0) { return clone(); } else if (path.size() == 1) { auto lock = impl.lockShared(); KJ_IF_MAYBE(entry, lock->tryGetEntry(path[0])) { return asDirectory(lock, *entry); } else { return nullptr; } } else { KJ_IF_MAYBE(subdir, tryGetParent(path[0])) { return subdir->get()->tryOpenSubdir(path.slice(1, path.size())); } else { return nullptr; } } } Maybe tryReadlink(PathPtr path) const override { if (path.size() == 0) { KJ_FAIL_REQUIRE("not a symlink") { return nullptr; } } else if (path.size() == 1) { auto lock = impl.lockShared(); KJ_IF_MAYBE(entry, lock->tryGetEntry(path[0])) { return asSymlink(lock, *entry); } else { return nullptr; } } else { KJ_IF_MAYBE(subdir, tryGetParent(path[0])) { return subdir->get()->tryReadlink(path.slice(1, path.size())); } else { return nullptr; } } } Maybe> tryOpenFile(PathPtr path, WriteMode mode) const override { if (path.size() == 0) { if (has(mode, WriteMode::MODIFY)) { KJ_FAIL_REQUIRE("not a file") { return nullptr; } } else if (has(mode, WriteMode::CREATE)) { return nullptr; // already exists (as a directory) } else { KJ_FAIL_REQUIRE("can't replace self") { return nullptr; } } } else if (path.size() == 1) { auto lock = impl.lockExclusive(); KJ_IF_MAYBE(entry, lock->openEntry(path[0], mode)) { return asFile(lock, *entry, mode); } else { return nullptr; } } else { KJ_IF_MAYBE(child, tryGetParent(path[0], mode)) { return child->get()->tryOpenFile(path.slice(1, path.size()), mode); } else { return nullptr; } } } Own> replaceFile(PathPtr path, WriteMode mode) const override { if (path.size() == 0) { KJ_FAIL_REQUIRE("can't replace self") { break; } } else if (path.size() == 1) { // don't need lock just to read the clock ref return heap>(*this, path[0], newInMemoryFile(impl.getWithoutLock().clock), mode); } else { KJ_IF_MAYBE(child, tryGetParent(path[0], mode)) { return child->get()->replaceFile(path.slice(1, path.size()), mode); } } return heap>(newInMemoryFile(impl.getWithoutLock().clock)); } Maybe> tryOpenSubdir(PathPtr path, WriteMode mode) const override { if (path.size() == 0) { if (has(mode, WriteMode::MODIFY)) { return atomicAddRef(*this); } else if (has(mode, WriteMode::CREATE)) { return nullptr; // already exists } else { KJ_FAIL_REQUIRE("can't replace self") { return nullptr; } } } else if (path.size() == 1) { auto lock = impl.lockExclusive(); KJ_IF_MAYBE(entry, lock->openEntry(path[0], mode)) { return asDirectory(lock, *entry, mode); } else { return nullptr; } } else { KJ_IF_MAYBE(child, tryGetParent(path[0], mode)) { return child->get()->tryOpenSubdir(path.slice(1, path.size()), mode); } else { return nullptr; } } } Own> replaceSubdir(PathPtr path, WriteMode mode) const override { if (path.size() == 0) { KJ_FAIL_REQUIRE("can't replace self") { break; } } else if (path.size() == 1) { // don't need lock just to read the clock ref return heap>(*this, path[0], newInMemoryDirectory(impl.getWithoutLock().clock), mode); } else { KJ_IF_MAYBE(child, tryGetParent(path[0], mode)) { return child->get()->replaceSubdir(path.slice(1, path.size()), mode); } } return heap>(newInMemoryDirectory(impl.getWithoutLock().clock)); } Maybe> tryAppendFile(PathPtr path, WriteMode mode) const override { if (path.size() == 0) { if (has(mode, WriteMode::MODIFY)) { KJ_FAIL_REQUIRE("not a file") { return nullptr; } } else if (has(mode, WriteMode::CREATE)) { return nullptr; // already exists (as a directory) } else { KJ_FAIL_REQUIRE("can't replace self") { return nullptr; } } } else if (path.size() == 1) { auto lock = impl.lockExclusive(); KJ_IF_MAYBE(entry, lock->openEntry(path[0], mode)) { return asFile(lock, *entry, mode).map(newFileAppender); } else { return nullptr; } } else { KJ_IF_MAYBE(child, tryGetParent(path[0], mode)) { return child->get()->tryAppendFile(path.slice(1, path.size()), mode); } else { return nullptr; } } } bool trySymlink(PathPtr path, StringPtr content, WriteMode mode) const override { if (path.size() == 0) { if (has(mode, WriteMode::CREATE)) { return false; } else { KJ_FAIL_REQUIRE("can't replace self") { return false; } } } else if (path.size() == 1) { auto lock = impl.lockExclusive(); KJ_IF_MAYBE(entry, lock->openEntry(path[0], mode)) { entry->init(SymlinkNode { lock->clock.now(), heapString(content) }); lock->modified(); return true; } else { return false; } } else { KJ_IF_MAYBE(child, tryGetParent(path[0], mode)) { return child->get()->trySymlink(path.slice(1, path.size()), content, mode); } else { KJ_FAIL_REQUIRE("couldn't create parent directory") { return false; } } } } Own createTemporary() const override { // Don't need lock just to read the clock ref. return newInMemoryFile(impl.getWithoutLock().clock); } bool tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const override { if (toPath.size() == 0) { if (has(toMode, WriteMode::CREATE)) { return false; } else { KJ_FAIL_REQUIRE("can't replace self") { return false; } } } else if (toPath.size() == 1) { // tryTransferChild() needs to at least know the node type, so do an lstat. KJ_IF_MAYBE(meta, fromDirectory.tryLstat(fromPath)) { auto lock = impl.lockExclusive(); KJ_IF_MAYBE(entry, lock->openEntry(toPath[0], toMode)) { // Make sure if we just cerated a new entry, and we don't successfully transfer to it, we // remove the entry before returning. bool needRollback = entry->node == nullptr; KJ_DEFER(if (needRollback) { lock->entries.erase(toPath[0]); }); if (lock->tryTransferChild(*entry, meta->type, meta->lastModified, meta->size, fromDirectory, fromPath, mode)) { lock->modified(); needRollback = false; return true; } else { KJ_FAIL_REQUIRE("InMemoryDirectory can't link an inode of this type", fromPath) { return false; } } } else { return false; } } else { return false; } } else { // TODO(someday): Ideally we wouldn't create parent directories if fromPath doesn't exist. // This requires a different approach to the code here, though. KJ_IF_MAYBE(child, tryGetParent(toPath[0], toMode)) { return child->get()->tryTransfer( toPath.slice(1, toPath.size()), toMode, fromDirectory, fromPath, mode); } else { return false; } } } Maybe tryTransferTo(const Directory& toDirectory, PathPtr toPath, WriteMode toMode, PathPtr fromPath, TransferMode mode) const override { if (fromPath.size() <= 1) { // If `fromPath` is in this directory (or *is* this directory) then we don't have any // optimizations. return nullptr; } // `fromPath` is in a subdirectory. It could turn out that that subdirectory is not an // InMemoryDirectory and is instead something `toDirectory` is friendly with. So let's follow // the path. KJ_IF_MAYBE(child, tryGetParent(fromPath[0], WriteMode::MODIFY)) { // OK, switch back to tryTransfer() but use the subdirectory. return toDirectory.tryTransfer(toPath, toMode, **child, fromPath.slice(1, fromPath.size()), mode); } else { // Hmm, doesn't exist. Fall back to standard path. return nullptr; } } bool tryRemove(PathPtr path) const override { if (path.size() == 0) { KJ_FAIL_REQUIRE("can't remove self from self") { return false; } } else if (path.size() == 1) { auto lock = impl.lockExclusive(); auto iter = lock->entries.find(path[0]); if (iter == lock->entries.end()) { return false; } else { lock->entries.erase(iter); lock->modified(); return true; } } else { KJ_IF_MAYBE(child, tryGetParent(path[0], WriteMode::MODIFY)) { return child->get()->tryRemove(path.slice(1, path.size())); } else { return false; } } } private: struct FileNode { Own file; }; struct DirectoryNode { Own directory; }; struct SymlinkNode { Date lastModified; String content; Path parse() const { KJ_CONTEXT("parsing symlink", content); return Path::parse(content); } }; struct EntryImpl { String name; OneOf node; EntryImpl(String&& name): name(kj::mv(name)) {} Own init(FileNode&& value) { return node.init(kj::mv(value)).file->clone(); } Own init(DirectoryNode&& value) { return node.init(kj::mv(value)).directory->clone(); } void init(SymlinkNode&& value) { node.init(kj::mv(value)); } bool init(OneOf&& value) { node = kj::mv(value); return node != nullptr; } void set(Own&& value) { node.init(FileNode { kj::mv(value) }); } void set(Own&& value) { node.init(DirectoryNode { kj::mv(value) }); } }; template class ReplacerImpl final: public Replacer { public: ReplacerImpl(const InMemoryDirectory& directory, kj::StringPtr name, Own inner, WriteMode mode) : Replacer(mode), directory(atomicAddRef(directory)), name(heapString(name)), inner(kj::mv(inner)) {} const T& get() override { return *inner; } bool tryCommit() override { KJ_REQUIRE(!committed, "commit() already called") { return true; } auto lock = directory->impl.lockExclusive(); KJ_IF_MAYBE(entry, lock->openEntry(name, Replacer::mode)) { entry->set(inner->clone()); lock->modified(); return true; } else { return false; } } private: bool committed = false; Own directory; kj::String name; Own inner; }; template class BrokenReplacer final: public Replacer { // For recovery path when exceptions are disabled. public: BrokenReplacer(Own inner) : Replacer(WriteMode::CREATE | WriteMode::MODIFY), inner(kj::mv(inner)) {} const T& get() override { return *inner; } bool tryCommit() override { return false; } private: Own inner; }; struct Impl { const Clock& clock; std::map entries; // Note: If this changes to a non-sorted map, listNames() and listEntries() must be updated to // sort their results. Date lastModified; Impl(const Clock& clock): clock(clock), lastModified(clock.now()) {} Maybe openEntry(kj::StringPtr name, WriteMode mode) { // TODO(perf): We could avoid a copy if the entry exists, at the expense of a double-lookup // if it doesn't. Maybe a better map implementation will solve everything? return openEntry(heapString(name), mode); } Maybe openEntry(String&& name, WriteMode mode) { if (has(mode, WriteMode::CREATE)) { EntryImpl entry(kj::mv(name)); StringPtr nameRef = entry.name; auto insertResult = entries.insert(std::make_pair(nameRef, kj::mv(entry))); if (!insertResult.second && !has(mode, WriteMode::MODIFY)) { // Entry already existed and MODIFY not specified. return nullptr; } return insertResult.first->second; } else if (has(mode, WriteMode::MODIFY)) { return tryGetEntry(name); } else { // Neither CREATE nor MODIFY specified: precondition always fails. return nullptr; } } kj::Maybe tryGetEntry(kj::StringPtr name) const { auto iter = entries.find(name); if (iter == entries.end()) { return nullptr; } else { return iter->second; } } kj::Maybe tryGetEntry(kj::StringPtr name) { auto iter = entries.find(name); if (iter == entries.end()) { return nullptr; } else { return iter->second; } } void modified() { lastModified = clock.now(); } bool tryTransferChild(EntryImpl& entry, const FsNode::Type type, kj::Maybe lastModified, kj::Maybe size, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) { switch (type) { case FsNode::Type::FILE: KJ_IF_MAYBE(file, fromDirectory.tryOpenFile(fromPath, WriteMode::MODIFY)) { if (mode == TransferMode::COPY) { auto copy = newInMemoryFile(clock); copy->copy(0, **file, 0, size.orDefault(kj::maxValue)); entry.set(kj::mv(copy)); } else { if (mode == TransferMode::MOVE) { KJ_ASSERT(fromDirectory.tryRemove(fromPath), "couldn't move node", fromPath) { return false; } } entry.set(kj::mv(*file)); } return true; } else { KJ_FAIL_ASSERT("source node deleted concurrently during transfer", fromPath) { return false; } } case FsNode::Type::DIRECTORY: KJ_IF_MAYBE(subdir, fromDirectory.tryOpenSubdir(fromPath, WriteMode::MODIFY)) { if (mode == TransferMode::COPY) { auto copy = atomicRefcounted(clock); auto& cpim = copy->impl.getWithoutLock(); // safe because just-created for (auto& subEntry: subdir->get()->listEntries()) { EntryImpl newEntry(kj::mv(subEntry.name)); Path filename(newEntry.name); if (!cpim.tryTransferChild(newEntry, subEntry.type, nullptr, nullptr, **subdir, filename, TransferMode::COPY)) { KJ_LOG(ERROR, "couldn't copy node of type not supported by InMemoryDirectory", filename); } else { StringPtr nameRef = newEntry.name; cpim.entries.insert(std::make_pair(nameRef, kj::mv(newEntry))); } } entry.set(kj::mv(copy)); } else { if (mode == TransferMode::MOVE) { KJ_ASSERT(fromDirectory.tryRemove(fromPath), "couldn't move node", fromPath) { return false; } } entry.set(kj::mv(*subdir)); } return true; } else { KJ_FAIL_ASSERT("source node deleted concurrently during transfer", fromPath) { return false; } } case FsNode::Type::SYMLINK: KJ_IF_MAYBE(content, fromDirectory.tryReadlink(fromPath)) { // Since symlinks are immutable, we can implement LINK the same as COPY. entry.init(SymlinkNode { lastModified.orDefault(clock.now()), kj::mv(*content) }); if (mode == TransferMode::MOVE) { KJ_ASSERT(fromDirectory.tryRemove(fromPath), "couldn't move node", fromPath) { return false; } } return true; } else { KJ_FAIL_ASSERT("source node deleted concurrently during transfer", fromPath) { return false; } } default: return false; } } }; kj::MutexGuarded impl; bool exists(kj::Locked& lock, const EntryImpl& entry) const { if (entry.node.is()) { auto newPath = entry.node.get().parse(); lock.release(); return exists(newPath); } else { return true; } } Maybe> asFile( kj::Locked& lock, const EntryImpl& entry) const { if (entry.node.is()) { return entry.node.get().file->clone(); } else if (entry.node.is()) { auto newPath = entry.node.get().parse(); lock.release(); return tryOpenFile(newPath); } else { KJ_FAIL_REQUIRE("not a file") { return nullptr; } } } Maybe> asDirectory( kj::Locked& lock, const EntryImpl& entry) const { if (entry.node.is()) { return entry.node.get().directory->clone(); } else if (entry.node.is()) { auto newPath = entry.node.get().parse(); lock.release(); return tryOpenSubdir(newPath); } else { KJ_FAIL_REQUIRE("not a directory") { return nullptr; } } } Maybe asSymlink(kj::Locked& lock, const EntryImpl& entry) const { if (entry.node.is()) { return heapString(entry.node.get().content); } else { KJ_FAIL_REQUIRE("not a symlink") { return nullptr; } } } Maybe> asFile(kj::Locked& lock, EntryImpl& entry, WriteMode mode) const { if (entry.node.is()) { return entry.node.get().file->clone(); } else if (entry.node.is()) { // CREATE_PARENT doesn't apply to creating the parents of a symlink target. However, the // target itself can still be created. auto newPath = entry.node.get().parse(); lock.release(); return tryOpenFile(newPath, mode - WriteMode::CREATE_PARENT); } else if (entry.node == nullptr) { KJ_ASSERT(has(mode, WriteMode::CREATE)); lock->modified(); return entry.init(FileNode { newInMemoryFile(lock->clock) }); } else { KJ_FAIL_REQUIRE("not a file") { return nullptr; } } } Maybe> asDirectory( kj::Locked& lock, EntryImpl& entry, WriteMode mode) const { if (entry.node.is()) { return entry.node.get().directory->clone(); } else if (entry.node.is()) { // CREATE_PARENT doesn't apply to creating the parents of a symlink target. However, the // target itself can still be created. auto newPath = entry.node.get().parse(); lock.release(); return tryOpenSubdir(newPath, mode - WriteMode::CREATE_PARENT); } else if (entry.node == nullptr) { KJ_ASSERT(has(mode, WriteMode::CREATE)); lock->modified(); return entry.init(DirectoryNode { newInMemoryDirectory(lock->clock) }); } else { KJ_FAIL_REQUIRE("not a directory") { return nullptr; } } } kj::Maybe> tryGetParent(kj::StringPtr name) const { auto lock = impl.lockShared(); KJ_IF_MAYBE(entry, impl.lockShared()->tryGetEntry(name)) { return asDirectory(lock, *entry); } else { return nullptr; } } kj::Maybe> tryGetParent(kj::StringPtr name, WriteMode mode) const { // Get a directory which is a parent of the eventual target. If `mode` includes // WriteMode::CREATE_PARENTS, possibly create the parent directory. auto lock = impl.lockExclusive(); WriteMode parentMode = has(mode, WriteMode::CREATE) && has(mode, WriteMode::CREATE_PARENT) ? WriteMode::CREATE | WriteMode::MODIFY // create parent : WriteMode::MODIFY; // don't create parent // Possibly create parent. KJ_IF_MAYBE(entry, lock->openEntry(name, parentMode)) { if (entry->node.is()) { return entry->node.get().directory->clone(); } else if (entry->node == nullptr) { lock->modified(); return entry->init(DirectoryNode { newInMemoryDirectory(lock->clock) }); } // Continue on. } if (has(mode, WriteMode::CREATE)) { // CREATE is documented as returning null when the file already exists. In this case, the // file does NOT exist because the parent directory does not exist or is not a directory. KJ_FAIL_REQUIRE("parent is not a directory") { return nullptr; } } else { return nullptr; } } }; // ----------------------------------------------------------------------------- class AppendableFileImpl final: public AppendableFile { public: AppendableFileImpl(Own&& fileParam): file(kj::mv(fileParam)) {} Own cloneFsNode() const override { return heap(file->clone()); } Maybe getFd() const override { return nullptr; } Metadata stat() const override { return file->stat(); } void sync() const override { file->sync(); } void datasync() const override { file->datasync(); } void write(const void* buffer, size_t size) override { file->write(file->stat().size, arrayPtr(reinterpret_cast(buffer), size)); } private: Own file; }; } // namespace // ----------------------------------------------------------------------------- Own newInMemoryFile(const Clock& clock) { return atomicRefcounted(clock); } Own newInMemoryDirectory(const Clock& clock) { return atomicRefcounted(clock); } Own newFileAppender(Own inner) { return heap(kj::mv(inner)); } } // namespace kj capnproto-c++-0.8.0/src/kj/filesystem-disk-unix.c++0000644000175000017500000016456613650101756022560 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if !_WIN32 #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "filesystem.h" #include "debug.h" #include #include #include #include #include #include #include #include #include #include #include "vector.h" #include "miniposix.h" #include #if __linux__ #include #include #include #endif namespace kj { namespace { #define HIDDEN_PREFIX ".kj-tmp." // Prefix for temp files which should be hidden when listing a directory. // // If you change this, make sure to update the unit test. #ifdef O_CLOEXEC #define MAYBE_O_CLOEXEC O_CLOEXEC #else #define MAYBE_O_CLOEXEC 0 #endif #ifdef O_DIRECTORY #define MAYBE_O_DIRECTORY O_DIRECTORY #else #define MAYBE_O_DIRECTORY 0 #endif #if __APPLE__ // Mac OSX defines SEEK_HOLE, but it doesn't work. ("Inappropriate ioctl for device", it says.) #undef SEEK_HOLE #endif #if __BIONIC__ // No no DTTOIF function #undef DT_UNKNOWN #endif static void setCloexec(int fd) KJ_UNUSED; static void setCloexec(int fd) { // Set the O_CLOEXEC flag on the given fd. // // We try to avoid the need to call this by taking advantage of syscall flags that set it // atomically on new file descriptors. Unfortunately some platforms do not support such syscalls. #ifdef FIOCLEX // Yay, we can set the flag in one call. KJ_SYSCALL_HANDLE_ERRORS(ioctl(fd, FIOCLEX)) { case EINVAL: case EOPNOTSUPP: break; default: KJ_FAIL_SYSCALL("ioctl(fd, FIOCLEX)", error) { break; } break; } else { // success return; } #endif // Sadness, we must resort to read/modify/write. // // (On many platforms, FD_CLOEXEC is the only flag modifiable via F_SETFD and therefore we could // skip the read... but it seems dangerous to assume that's true of all platforms, and anyway // most platforms support FIOCLEX.) int flags; KJ_SYSCALL(flags = fcntl(fd, F_GETFD)); if (!(flags & FD_CLOEXEC)) { KJ_SYSCALL(fcntl(fd, F_SETFD, flags | FD_CLOEXEC)); } } static Date toKjDate(struct timespec tv) { return tv.tv_sec * SECONDS + tv.tv_nsec * NANOSECONDS + UNIX_EPOCH; } static FsNode::Type modeToType(mode_t mode) { switch (mode & S_IFMT) { case S_IFREG : return FsNode::Type::FILE; case S_IFDIR : return FsNode::Type::DIRECTORY; case S_IFLNK : return FsNode::Type::SYMLINK; case S_IFBLK : return FsNode::Type::BLOCK_DEVICE; case S_IFCHR : return FsNode::Type::CHARACTER_DEVICE; case S_IFIFO : return FsNode::Type::NAMED_PIPE; case S_IFSOCK: return FsNode::Type::SOCKET; default: return FsNode::Type::OTHER; } } static FsNode::Metadata statToMetadata(struct stat& stats) { // Probably st_ino and st_dev are usually under 32 bits, so mix by rotating st_dev left 32 bits // and XOR. uint64_t d = stats.st_dev; uint64_t hash = ((d << 32) | (d >> 32)) ^ stats.st_ino; return FsNode::Metadata { modeToType(stats.st_mode), implicitCast(stats.st_size), implicitCast(stats.st_blocks * 512u), #if __APPLE__ toKjDate(stats.st_mtimespec), #else toKjDate(stats.st_mtim), #endif implicitCast(stats.st_nlink), hash }; } static bool rmrf(int fd, StringPtr path); static void rmrfChildrenAndClose(int fd) { // Assumes fd is seeked to beginning. DIR* dir = fdopendir(fd); if (dir == nullptr) { close(fd); KJ_FAIL_SYSCALL("fdopendir", errno); }; KJ_DEFER(closedir(dir)); for (;;) { errno = 0; struct dirent* entry = readdir(dir); if (entry == nullptr) { int error = errno; if (error == 0) { break; } else { KJ_FAIL_SYSCALL("readdir", error); } } if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) { // ignore . and .. } else { #ifdef DT_UNKNOWN // d_type is not available on all platforms. if (entry->d_type == DT_DIR) { int subdirFd; KJ_SYSCALL(subdirFd = openat( fd, entry->d_name, O_RDONLY | MAYBE_O_DIRECTORY | MAYBE_O_CLOEXEC)); rmrfChildrenAndClose(subdirFd); KJ_SYSCALL(unlinkat(fd, entry->d_name, AT_REMOVEDIR)); } else if (entry->d_type != DT_UNKNOWN) { KJ_SYSCALL(unlinkat(fd, entry->d_name, 0)); } else { #endif KJ_ASSERT(rmrf(fd, entry->d_name)); #ifdef DT_UNKNOWN } #endif } } } static bool rmrf(int fd, StringPtr path) { struct stat stats; KJ_SYSCALL_HANDLE_ERRORS(fstatat(fd, path.cStr(), &stats, AT_SYMLINK_NOFOLLOW)) { case ENOENT: case ENOTDIR: // Doesn't exist. return false; default: KJ_FAIL_SYSCALL("lstat(path)", error, path) { return false; } } if (S_ISDIR(stats.st_mode)) { int subdirFd; KJ_SYSCALL(subdirFd = openat( fd, path.cStr(), O_RDONLY | MAYBE_O_DIRECTORY | MAYBE_O_CLOEXEC)) { return false; } rmrfChildrenAndClose(subdirFd); KJ_SYSCALL(unlinkat(fd, path.cStr(), AT_REMOVEDIR)) { return false; } } else { KJ_SYSCALL(unlinkat(fd, path.cStr(), 0)) { return false; } } return true; } struct MmapRange { uint64_t offset; uint64_t size; }; static MmapRange getMmapRange(uint64_t offset, uint64_t size) { // Comes up with an offset and size to pass to mmap(), given an offset and size requested by // the caller, and considering the fact that mappings must start at a page boundary. // // The offset is rounded down to the nearest page boundary, and the size is increased to // compensate. Note that the endpoint of the mapping is *not* rounded up to a page boundary, as // mmap() does not actually require this, and it causes trouble on some systems (notably Cygwin). #ifndef _SC_PAGESIZE #define _SC_PAGESIZE _SC_PAGE_SIZE #endif static const uint64_t pageSize = sysconf(_SC_PAGESIZE); uint64_t pageMask = pageSize - 1; uint64_t realOffset = offset & ~pageMask; return { realOffset, offset + size - realOffset }; } class MmapDisposer: public ArrayDisposer { protected: void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const { auto range = getMmapRange(reinterpret_cast(firstElement), elementSize * elementCount); KJ_SYSCALL(munmap(reinterpret_cast(range.offset), range.size)) { break; } } }; constexpr MmapDisposer mmapDisposer = MmapDisposer(); class DiskHandle { // We need to implement each of ReadableFile, AppendableFile, File, ReadableDirectory, and // Directory for disk handles. There is a lot of implementation overlap between these, especially // stat(), sync(), etc. We can't have everything inherit from a common DiskFsNode that implements // these because then we get diamond inheritance which means we need to make all our inheritance // virtual which means downcasting requires RTTI which violates our goal of supporting compiling // with no RTTI. So instead we have the DiskHandle class which implements all the methods without // inheriting anything, and then we have DiskFile, DiskDirectory, etc. hold this and delegate to // it. Ugly, but works. public: DiskHandle(AutoCloseFd&& fd): fd(kj::mv(fd)) {} // OsHandle ------------------------------------------------------------------ AutoCloseFd clone() const { int fd2; #ifdef F_DUPFD_CLOEXEC KJ_SYSCALL_HANDLE_ERRORS(fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 3)) { case EINVAL: case EOPNOTSUPP: // fall back break; default: KJ_FAIL_SYSCALL("fnctl(fd, F_DUPFD_CLOEXEC, 3)", error) { break; } break; } else { return AutoCloseFd(fd2); } #endif KJ_SYSCALL(fd2 = ::dup(fd)); AutoCloseFd result(fd2); setCloexec(result); return result; } int getFd() const { return fd.get(); } // FsNode -------------------------------------------------------------------- FsNode::Metadata stat() const { struct stat stats; KJ_SYSCALL(::fstat(fd, &stats)); return statToMetadata(stats); } void sync() const { #if __APPLE__ // For whatever reason, fsync() on OSX only flushes kernel buffers. It does not flush hardware // disk buffers. This makes it not very useful. But OSX documents fcntl F_FULLFSYNC which does // the right thing. Why they don't just make fsync() do the right thing, I do not know. KJ_SYSCALL(fcntl(fd, F_FULLFSYNC)); #else KJ_SYSCALL(fsync(fd)); #endif } void datasync() const { // The presence of the _POSIX_SYNCHRONIZED_IO define is supposed to tell us that fdatasync() // exists. But Apple defines this yet doesn't offer fdatasync(). Thanks, Apple. #if _POSIX_SYNCHRONIZED_IO && !__APPLE__ KJ_SYSCALL(fdatasync(fd)); #else this->sync(); #endif } // ReadableFile -------------------------------------------------------------- size_t read(uint64_t offset, ArrayPtr buffer) const { // pread() probably never returns short reads unless it hits EOF. Unfortunately, though, per // spec we are not allowed to assume this. size_t total = 0; while (buffer.size() > 0) { ssize_t n; KJ_SYSCALL(n = pread(fd, buffer.begin(), buffer.size(), offset)); if (n == 0) break; total += n; offset += n; buffer = buffer.slice(n, buffer.size()); } return total; } Array mmap(uint64_t offset, uint64_t size) const { if (size == 0) return nullptr; // zero-length mmap() returns EINVAL, so avoid it auto range = getMmapRange(offset, size); const void* mapping = ::mmap(NULL, range.size, PROT_READ, MAP_SHARED, fd, range.offset); if (mapping == MAP_FAILED) { KJ_FAIL_SYSCALL("mmap", errno); } return Array(reinterpret_cast(mapping) + (offset - range.offset), size, mmapDisposer); } Array mmapPrivate(uint64_t offset, uint64_t size) const { if (size == 0) return nullptr; // zero-length mmap() returns EINVAL, so avoid it auto range = getMmapRange(offset, size); void* mapping = ::mmap(NULL, range.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, range.offset); if (mapping == MAP_FAILED) { KJ_FAIL_SYSCALL("mmap", errno); } return Array(reinterpret_cast(mapping) + (offset - range.offset), size, mmapDisposer); } // File ---------------------------------------------------------------------- void write(uint64_t offset, ArrayPtr data) const { // pwrite() probably never returns short writes unless there's no space left on disk. // Unfortunately, though, per spec we are not allowed to assume this. while (data.size() > 0) { ssize_t n; KJ_SYSCALL(n = pwrite(fd, data.begin(), data.size(), offset)); KJ_ASSERT(n > 0, "pwrite() returned zero?"); offset += n; data = data.slice(n, data.size()); } } void zero(uint64_t offset, uint64_t size) const { #ifdef FALLOC_FL_PUNCH_HOLE KJ_SYSCALL_HANDLE_ERRORS( fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, size)) { case EOPNOTSUPP: // fall back to below break; default: KJ_FAIL_SYSCALL("fallocate(FALLOC_FL_PUNCH_HOLE)", error) { return; } } else { return; } #endif static const byte ZEROS[4096] = { 0 }; #if __APPLE__ || __CYGWIN__ || (defined(__ANDROID__) && __ANDROID_API__ < 24) // Mac & Cygwin & Android API levels 23 and lower doesn't have pwritev(). while (size > sizeof(ZEROS)) { write(offset, ZEROS); size -= sizeof(ZEROS); offset += sizeof(ZEROS); } write(offset, kj::arrayPtr(ZEROS, size)); #else // Use a 4k buffer of zeros amplified by iov to write zeros with as few syscalls as possible. size_t count = (size + sizeof(ZEROS) - 1) / sizeof(ZEROS); const size_t iovmax = miniposix::iovMax(count); KJ_STACK_ARRAY(struct iovec, iov, kj::min(iovmax, count), 16, 256); for (auto& item: iov) { item.iov_base = const_cast(ZEROS); item.iov_len = sizeof(ZEROS); } while (size > 0) { size_t iovCount; if (size >= iov.size() * sizeof(ZEROS)) { iovCount = iov.size(); } else { iovCount = size / sizeof(ZEROS); size_t rem = size % sizeof(ZEROS); if (rem > 0) { iov[iovCount++].iov_len = rem; } } ssize_t n; KJ_SYSCALL(n = pwritev(fd, iov.begin(), count, offset)); KJ_ASSERT(n > 0, "pwrite() returned zero?"); offset += n; size -= n; } #endif } void truncate(uint64_t size) const { KJ_SYSCALL(ftruncate(fd, size)); } class WritableFileMappingImpl final: public WritableFileMapping { public: WritableFileMappingImpl(Array bytes): bytes(kj::mv(bytes)) {} ArrayPtr get() const override { // const_cast OK because WritableFileMapping does indeed provide a writable view despite // being const itself. return arrayPtr(const_cast(bytes.begin()), bytes.size()); } void changed(ArrayPtr slice) const override { KJ_REQUIRE(slice.begin() >= bytes.begin() && slice.end() <= bytes.end(), "byte range is not part of this mapping"); if (slice.size() == 0) return; // msync() requires page-alignment, apparently, so use getMmapRange() to accomplish that. auto range = getMmapRange(reinterpret_cast(slice.begin()), slice.size()); KJ_SYSCALL(msync(reinterpret_cast(range.offset), range.size, MS_ASYNC)); } void sync(ArrayPtr slice) const override { KJ_REQUIRE(slice.begin() >= bytes.begin() && slice.end() <= bytes.end(), "byte range is not part of this mapping"); if (slice.size() == 0) return; // msync() requires page-alignment, apparently, so use getMmapRange() to accomplish that. auto range = getMmapRange(reinterpret_cast(slice.begin()), slice.size()); KJ_SYSCALL(msync(reinterpret_cast(range.offset), range.size, MS_SYNC)); } private: Array bytes; }; Own mmapWritable(uint64_t offset, uint64_t size) const { if (size == 0) { // zero-length mmap() returns EINVAL, so avoid it return heap(nullptr); } auto range = getMmapRange(offset, size); void* mapping = ::mmap(NULL, range.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, range.offset); if (mapping == MAP_FAILED) { KJ_FAIL_SYSCALL("mmap", errno); } auto array = Array(reinterpret_cast(mapping) + (offset - range.offset), size, mmapDisposer); return heap(kj::mv(array)); } size_t copyChunk(uint64_t offset, int fromFd, uint64_t fromOffset, uint64_t size) const { // Copies a range of bytes from `fromFd` to this file in the most efficient way possible for // the OS. Only returns less than `size` if EOF. Does not account for holes. #if __linux__ { KJ_SYSCALL(lseek(fd, offset, SEEK_SET)); off_t fromPos = fromOffset; off_t end = fromOffset + size; while (fromPos < end) { ssize_t n; KJ_SYSCALL_HANDLE_ERRORS(n = sendfile(fd, fromFd, &fromPos, end - fromPos)) { case EINVAL: case ENOSYS: goto sendfileNotAvailable; default: KJ_FAIL_SYSCALL("sendfile", error) { return fromPos - fromOffset; } } if (n == 0) break; } return fromPos - fromOffset; } sendfileNotAvailable: #endif uint64_t total = 0; while (size > 0) { byte buffer[4096]; ssize_t n; KJ_SYSCALL(n = pread(fromFd, buffer, kj::min(sizeof(buffer), size), fromOffset)); if (n == 0) break; write(offset, arrayPtr(buffer, n)); fromOffset += n; offset += n; total += n; size -= n; } return total; } kj::Maybe copy(uint64_t offset, const ReadableFile& from, uint64_t fromOffset, uint64_t size) const { KJ_IF_MAYBE(otherFd, from.getFd()) { #ifdef FICLONE if (offset == 0 && fromOffset == 0 && size == kj::maxValue && stat().size == 0) { if (ioctl(fd, FICLONE, *otherFd) >= 0) { return stat().size; } } else if (size > 0) { // src_length = 0 has special meaning for the syscall, so avoid. struct file_clone_range range; memset(&range, 0, sizeof(range)); range.src_fd = *otherFd; range.dest_offset = offset; range.src_offset = fromOffset; range.src_length = size == kj::maxValue ? 0 : size; if (ioctl(fd, FICLONERANGE, &range) >= 0) { // TODO(someday): What does FICLONERANGE actually do if the range goes past EOF? The docs // don't say. Maybe it only copies the parts that exist. Maybe it punches holes for the // rest. Where does the destination file's EOF marker end up? Who knows? return kj::min(from.stat().size - fromOffset, size); } } else { // size == 0 return size_t(0); } // ioctl failed. Almost all failures documented for these are of the form "the operation is // not supported for the filesystem(s) specified", so fall back to other approaches. #endif off_t toPos = offset; off_t fromPos = fromOffset; off_t end = size == kj::maxValue ? off_t(kj::maxValue) : off_t(fromOffset + size); for (;;) { // Handle data. { // Find out how much data there is before the next hole. off_t nextHole; #ifdef SEEK_HOLE KJ_SYSCALL_HANDLE_ERRORS(nextHole = lseek(*otherFd, fromPos, SEEK_HOLE)) { case EINVAL: // SEEK_HOLE probably not supported. Assume no holes. nextHole = end; break; case ENXIO: // Past EOF. Stop here. return fromPos - fromOffset; default: KJ_FAIL_SYSCALL("lseek(fd, pos, SEEK_HOLE)", error) { return fromPos - fromOffset; } } #else // SEEK_HOLE not supported. Assume no holes. nextHole = end; #endif // Copy the next chunk of data. off_t copyTo = kj::min(end, nextHole); size_t amount = copyTo - fromPos; if (amount > 0) { size_t n = copyChunk(toPos, *otherFd, fromPos, amount); fromPos += n; toPos += n; if (n < amount) { return fromPos - fromOffset; } } if (fromPos == end) { return fromPos - fromOffset; } } #ifdef SEEK_HOLE // Handle hole. { // Find out how much hole there is before the next data. off_t nextData; KJ_SYSCALL_HANDLE_ERRORS(nextData = lseek(*otherFd, fromPos, SEEK_DATA)) { case EINVAL: // SEEK_DATA probably not supported. But we should only have gotten here if we // were expecting a hole. KJ_FAIL_ASSERT("can't determine hole size; SEEK_DATA not supported"); break; case ENXIO: // No more data. Set to EOF. KJ_SYSCALL(nextData = lseek(*otherFd, 0, SEEK_END)); if (nextData > end) { end = nextData; } break; default: KJ_FAIL_SYSCALL("lseek(fd, pos, SEEK_HOLE)", error) { return fromPos - fromOffset; } } // Write zeros. off_t zeroTo = kj::min(end, nextData); off_t amount = zeroTo - fromPos; if (amount > 0) { zero(toPos, amount); toPos += amount; fromPos = zeroTo; } if (fromPos == end) { return fromPos - fromOffset; } } #endif } } // Indicates caller should call File::copy() default implementation. return nullptr; } // ReadableDirectory --------------------------------------------------------- template auto list(bool needTypes, Func&& func) const -> Array(), instance()))>> { // Seek to start of directory. KJ_SYSCALL(lseek(fd, 0, SEEK_SET)); // Unfortunately, fdopendir() takes ownership of the file descriptor. Therefore we need to // make a duplicate. int duped; KJ_SYSCALL(duped = dup(fd)); DIR* dir = fdopendir(duped); if (dir == nullptr) { close(duped); KJ_FAIL_SYSCALL("fdopendir", errno); } KJ_DEFER(closedir(dir)); typedef Decay(), instance()))> Entry; kj::Vector entries; for (;;) { errno = 0; struct dirent* entry = readdir(dir); if (entry == nullptr) { int error = errno; if (error == 0) { break; } else { KJ_FAIL_SYSCALL("readdir", error); } } kj::StringPtr name = entry->d_name; if (name != "." && name != ".." && !name.startsWith(HIDDEN_PREFIX)) { #ifdef DT_UNKNOWN // d_type is not available on all platforms. if (entry->d_type != DT_UNKNOWN) { entries.add(func(name, modeToType(DTTOIF(entry->d_type)))); } else { #endif if (needTypes) { // Unknown type. Fall back to stat. struct stat stats; KJ_SYSCALL(fstatat(fd, name.cStr(), &stats, AT_SYMLINK_NOFOLLOW)); entries.add(func(name, modeToType(stats.st_mode))); } else { entries.add(func(name, FsNode::Type::OTHER)); } #ifdef DT_UNKNOWN } #endif } } auto result = entries.releaseAsArray(); std::sort(result.begin(), result.end()); return result; } Array listNames() const { return list(false, [](StringPtr name, FsNode::Type type) { return heapString(name); }); } Array listEntries() const { return list(true, [](StringPtr name, FsNode::Type type) { return ReadableDirectory::Entry { type, heapString(name), }; }); } bool exists(PathPtr path) const { KJ_SYSCALL_HANDLE_ERRORS(faccessat(fd, path.toString().cStr(), F_OK, 0)) { case ENOENT: case ENOTDIR: return false; default: KJ_FAIL_SYSCALL("faccessat(fd, path)", error, path) { return false; } } return true; } Maybe tryLstat(PathPtr path) const { struct stat stats; KJ_SYSCALL_HANDLE_ERRORS(fstatat(fd, path.toString().cStr(), &stats, AT_SYMLINK_NOFOLLOW)) { case ENOENT: case ENOTDIR: return nullptr; default: KJ_FAIL_SYSCALL("faccessat(fd, path)", error, path) { return nullptr; } } return statToMetadata(stats); } Maybe> tryOpenFile(PathPtr path) const { int newFd; KJ_SYSCALL_HANDLE_ERRORS(newFd = openat( fd, path.toString().cStr(), O_RDONLY | MAYBE_O_CLOEXEC)) { case ENOENT: case ENOTDIR: return nullptr; default: KJ_FAIL_SYSCALL("openat(fd, path, O_RDONLY)", error, path) { return nullptr; } } kj::AutoCloseFd result(newFd); #ifndef O_CLOEXEC setCloexec(result); #endif return newDiskReadableFile(kj::mv(result)); } Maybe tryOpenSubdirInternal(PathPtr path) const { int newFd; KJ_SYSCALL_HANDLE_ERRORS(newFd = openat( fd, path.toString().cStr(), O_RDONLY | MAYBE_O_CLOEXEC | MAYBE_O_DIRECTORY)) { case ENOENT: return nullptr; case ENOTDIR: // Could mean that a parent is not a directory, which we treat as "doesn't exist". // Could also mean that the specified file is not a directory, which should throw. // Check using exists(). if (!exists(path)) { return nullptr; } // fallthrough default: KJ_FAIL_SYSCALL("openat(fd, path, O_DIRECTORY)", error, path) { return nullptr; } } kj::AutoCloseFd result(newFd); #ifndef O_CLOEXEC setCloexec(result); #endif return kj::mv(result); } Maybe> tryOpenSubdir(PathPtr path) const { return tryOpenSubdirInternal(path).map(newDiskReadableDirectory); } Maybe tryReadlink(PathPtr path) const { size_t trySize = 256; for (;;) { KJ_STACK_ARRAY(char, buf, trySize, 256, 4096); ssize_t n = readlinkat(fd, path.toString().cStr(), buf.begin(), buf.size()); if (n < 0) { int error = errno; switch (error) { case EINTR: continue; case ENOENT: case ENOTDIR: case EINVAL: // not a link return nullptr; default: KJ_FAIL_SYSCALL("readlinkat(fd, path)", error, path) { return nullptr; } } } if (n >= buf.size()) { // Didn't give it enough space. Better retry with a bigger buffer. trySize *= 2; continue; } return heapString(buf.begin(), n); } } // Directory ----------------------------------------------------------------- bool tryMkdir(PathPtr path, WriteMode mode, bool noThrow) const { // Internal function to make a directory. auto filename = path.toString(); mode_t acl = has(mode, WriteMode::PRIVATE) ? 0700 : 0777; KJ_SYSCALL_HANDLE_ERRORS(mkdirat(fd, filename.cStr(), acl)) { case EEXIST: { // Apparently this path exists. if (!has(mode, WriteMode::MODIFY)) { // Require exclusive create. return false; } // MODIFY is allowed, so we just need to check whether the existing entry is a directory. struct stat stats; KJ_SYSCALL_HANDLE_ERRORS(fstatat(fd, filename.cStr(), &stats, 0)) { default: // mkdir() says EEXIST but we can't stat it. Maybe it's a dangling link, or maybe // we can't access it for some reason. Assume failure. // // TODO(someday): Maybe we should be creating the directory at the target of the // link? goto failed; } return (stats.st_mode & S_IFMT) == S_IFDIR; } case ENOENT: if (has(mode, WriteMode::CREATE_PARENT) && path.size() > 0 && tryMkdir(path.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryMkdir(path, mode - WriteMode::CREATE_PARENT, noThrow); } else { goto failed; } default: failed: if (noThrow) { // Caller requested no throwing. return false; } else { KJ_FAIL_SYSCALL("mkdirat(fd, path)", error, path); } } return true; } kj::Maybe createNamedTemporary( PathPtr finalName, WriteMode mode, Function tryCreate) const { // Create a temporary file which will eventually replace `finalName`. // // Calls `tryCreate` to actually create the temporary, passing in the desired path. tryCreate() // is expected to behave like a syscall, returning a negative value and setting `errno` on // error. tryCreate() MUST fail with EEXIST if the path exists -- this is not checked in // advance, since it needs to be checked atomically. In the case of EEXIST, tryCreate() will // be called again with a new path. // // Returns the temporary path that succeeded. Only returns nullptr if there was an exception // but we're compiled with -fno-exceptions. if (finalName.size() == 0) { KJ_FAIL_REQUIRE("can't replace self") { break; } return nullptr; } static uint counter = 0; static const pid_t pid = getpid(); String pathPrefix; if (finalName.size() > 1) { pathPrefix = kj::str(finalName.parent(), '/'); } auto path = kj::str(pathPrefix, HIDDEN_PREFIX, pid, '.', counter++, '.', finalName.basename()[0], ".partial"); KJ_SYSCALL_HANDLE_ERRORS(tryCreate(path)) { case EEXIST: return createNamedTemporary(finalName, mode, kj::mv(tryCreate)); case ENOENT: if (has(mode, WriteMode::CREATE_PARENT) && finalName.size() > 1 && tryMkdir(finalName.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. mode = mode - WriteMode::CREATE_PARENT; return createNamedTemporary(finalName, mode, kj::mv(tryCreate)); } // fallthrough default: KJ_FAIL_SYSCALL("create(path)", error, path) { break; } return nullptr; } return kj::mv(path); } bool tryReplaceNode(PathPtr path, WriteMode mode, Function tryCreate) const { // Replaces the given path with an object created by calling tryCreate(). // // tryCreate() must behave like a syscall which creates the node at the path passed to it, // returning a negative value on error. If the path passed to tryCreate already exists, it // MUST fail with EEXIST. // // When `mode` includes MODIFY, replaceNode() reacts to EEXIST by creating the node in a // temporary location and then rename()ing it into place. if (path.size() == 0) { KJ_FAIL_REQUIRE("can't replace self") { return false; } } auto filename = path.toString(); if (has(mode, WriteMode::CREATE)) { // First try just cerating the node in-place. KJ_SYSCALL_HANDLE_ERRORS(tryCreate(filename)) { case EEXIST: // Target exists. if (has(mode, WriteMode::MODIFY)) { // Fall back to MODIFY path, below. break; } else { return false; } case ENOENT: if (has(mode, WriteMode::CREATE_PARENT) && path.size() > 0 && tryMkdir(path.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryReplaceNode(path, mode - WriteMode::CREATE_PARENT, kj::mv(tryCreate)); } // fallthrough default: KJ_FAIL_SYSCALL("create(path)", error, path) { return false; } } else { // Success. return true; } } // Either we don't have CREATE mode or the target already exists. We need to perform a // replacement instead. KJ_IF_MAYBE(tempPath, createNamedTemporary(path, mode, kj::mv(tryCreate))) { if (tryCommitReplacement(filename, fd, *tempPath, mode)) { return true; } else { KJ_SYSCALL_HANDLE_ERRORS(unlinkat(fd, tempPath->cStr(), 0)) { case ENOENT: // meh break; default: KJ_FAIL_SYSCALL("unlinkat(fd, tempPath, 0)", error, *tempPath); } return false; } } else { // threw, but exceptions are disabled return false; } } Maybe tryOpenFileInternal(PathPtr path, WriteMode mode, bool append) const { uint flags = O_RDWR | MAYBE_O_CLOEXEC; mode_t acl = 0666; if (has(mode, WriteMode::CREATE)) { flags |= O_CREAT; } if (!has(mode, WriteMode::MODIFY)) { if (!has(mode, WriteMode::CREATE)) { // Neither CREATE nor MODIFY -- impossible to satisfy preconditions. return nullptr; } flags |= O_EXCL; } if (append) { flags |= O_APPEND; } if (has(mode, WriteMode::EXECUTABLE)) { acl = 0777; } if (has(mode, WriteMode::PRIVATE)) { acl &= 0700; } auto filename = path.toString(); int newFd; KJ_SYSCALL_HANDLE_ERRORS(newFd = openat(fd, filename.cStr(), flags, acl)) { case ENOENT: if (has(mode, WriteMode::CREATE)) { // Either: // - The file is a broken symlink. // - A parent directory didn't exist. if (has(mode, WriteMode::CREATE_PARENT) && path.size() > 0 && tryMkdir(path.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryOpenFileInternal(path, mode - WriteMode::CREATE_PARENT, append); } // Check for broken link. if (!has(mode, WriteMode::MODIFY) && faccessat(fd, filename.cStr(), F_OK, AT_SYMLINK_NOFOLLOW) >= 0) { // Yep. We treat this as already-exists, which means in CREATE-only mode this is a // simple failure. return nullptr; } KJ_FAIL_REQUIRE("parent is not a directory", path) { return nullptr; } } else { // MODIFY-only mode. ENOENT = doesn't exist = return null. return nullptr; } case ENOTDIR: if (!has(mode, WriteMode::CREATE)) { // MODIFY-only mode. ENOTDIR = parent not a directory = doesn't exist = return null. return nullptr; } goto failed; case EEXIST: if (!has(mode, WriteMode::MODIFY)) { // CREATE-only mode. EEXIST = already exists = return null. return nullptr; } goto failed; default: failed: KJ_FAIL_SYSCALL("openat(fd, path, O_RDWR | ...)", error, path) { return nullptr; } } kj::AutoCloseFd result(newFd); #ifndef O_CLOEXEC setCloexec(result); #endif return kj::mv(result); } bool tryCommitReplacement(StringPtr toPath, int fromDirFd, StringPtr fromPath, WriteMode mode, int* errorReason = nullptr) const { if (has(mode, WriteMode::CREATE) && has(mode, WriteMode::MODIFY)) { // Always clobber. Try it. KJ_SYSCALL_HANDLE_ERRORS(renameat(fromDirFd, fromPath.cStr(), fd.get(), toPath.cStr())) { case EISDIR: case ENOTDIR: case ENOTEMPTY: case EEXIST: // Failed because target exists and due to the various weird quirks of rename(), it // can't remove it for us. On Linux we can try an exchange instead. On others we have // to move the target out of the way. break; default: if (errorReason == nullptr) { KJ_FAIL_SYSCALL("rename(fromPath, toPath)", error, fromPath, toPath) { return false; } } else { *errorReason = error; return false; } } else { return true; } } #if __linux__ && defined(RENAME_EXCHANGE) // Try to use Linux's renameat2() to atomically check preconditions and apply. if (has(mode, WriteMode::MODIFY)) { // Use an exchange to implement modification. // // We reach this branch when performing a MODIFY-only, or when performing a CREATE | MODIFY // in which we determined above that there's a node of a different type blocking the // exchange. KJ_SYSCALL_HANDLE_ERRORS(syscall(SYS_renameat2, fromDirFd, fromPath.cStr(), fd.get(), toPath.cStr(), RENAME_EXCHANGE)) { case ENOSYS: break; // fall back to traditional means case ENOENT: // Presumably because the target path doesn't exist. if (has(mode, WriteMode::CREATE)) { KJ_FAIL_ASSERT("rename(tmp, path) claimed path exists but " "renameat2(fromPath, toPath, EXCAHNGE) said it doest; concurrent modification?", fromPath, toPath) { return false; } } else { // Assume target doesn't exist. return false; } default: if (errorReason == nullptr) { KJ_FAIL_SYSCALL("renameat2(fromPath, toPath, EXCHANGE)", error, fromPath, toPath) { return false; } } else { *errorReason = error; return false; } } else { // Successful swap! Delete swapped-out content. rmrf(fromDirFd, fromPath); return true; } } else if (has(mode, WriteMode::CREATE)) { KJ_SYSCALL_HANDLE_ERRORS(syscall(SYS_renameat2, fromDirFd, fromPath.cStr(), fd.get(), toPath.cStr(), RENAME_NOREPLACE)) { case ENOSYS: break; // fall back to traditional means case EEXIST: return false; default: if (errorReason == nullptr) { KJ_FAIL_SYSCALL("renameat2(fromPath, toPath, NOREPLACE)", error, fromPath, toPath) { return false; } } else { *errorReason = error; return false; } } else { return true; } } #endif // We're unable to do what we wanted atomically. :( if (has(mode, WriteMode::CREATE) && has(mode, WriteMode::MODIFY)) { // We failed to atomically delete the target previously. So now we need to do two calls in // rapid succession to move the old file away then move the new one into place. // Find out what kind of file exists at the target path. struct stat stats; KJ_SYSCALL(fstatat(fd, toPath.cStr(), &stats, AT_SYMLINK_NOFOLLOW)) { return false; } // Create a temporary location to move the existing object to. Note that rename() allows a // non-directory to replace a non-directory, and allows a directory to replace an empty // directory. So we have to create the right type. Path toPathParsed = Path::parse(toPath); String away; KJ_IF_MAYBE(awayPath, createNamedTemporary(toPathParsed, WriteMode::CREATE, [&](StringPtr candidatePath) { if (S_ISDIR(stats.st_mode)) { return mkdirat(fd, candidatePath.cStr(), 0700); } else { #if __APPLE__ // No mknodat() on OSX, gotta open() a file, ugh. int newFd = openat(fd, candidatePath.cStr(), O_RDWR | O_CREAT | O_EXCL | MAYBE_O_CLOEXEC, 0700); if (newFd >= 0) close(newFd); return newFd; #else return mknodat(fd, candidatePath.cStr(), S_IFREG | 0600, dev_t()); #endif } })) { away = kj::mv(*awayPath); } else { // Already threw. return false; } // OK, now move the target object to replace the thing we just created. KJ_SYSCALL(renameat(fd, toPath.cStr(), fd, away.cStr())) { // Something went wrong. Remove the thing we just created. unlinkat(fd, away.cStr(), S_ISDIR(stats.st_mode) ? AT_REMOVEDIR : 0); return false; } // Now move the source object to the target location. KJ_SYSCALL_HANDLE_ERRORS(renameat(fromDirFd, fromPath.cStr(), fd, toPath.cStr())) { default: // Try to put things back where they were. If this fails, though, then we have little // choice but to leave things broken. KJ_SYSCALL_HANDLE_ERRORS(renameat(fd, away.cStr(), fd, toPath.cStr())) { default: break; } if (errorReason == nullptr) { KJ_FAIL_SYSCALL("rename(fromPath, toPath)", error, fromPath, toPath) { return false; } } else { *errorReason = error; return false; } } // OK, success. Delete the old content. rmrf(fd, away); return true; } else { // Only one of CREATE or MODIFY is specified, so we need to verify non-atomically that the // corresponding precondition (must-not-exist or must-exist, respectively) is held. if (has(mode, WriteMode::CREATE)) { struct stat stats; KJ_SYSCALL_HANDLE_ERRORS(fstatat(fd.get(), toPath.cStr(), &stats, AT_SYMLINK_NOFOLLOW)) { case ENOENT: case ENOTDIR: break; // doesn't exist; continue default: KJ_FAIL_SYSCALL("fstatat(fd, toPath)", error, toPath) { return false; } } else { return false; // already exists; fail } } else if (has(mode, WriteMode::MODIFY)) { struct stat stats; KJ_SYSCALL_HANDLE_ERRORS(fstatat(fd.get(), toPath.cStr(), &stats, AT_SYMLINK_NOFOLLOW)) { case ENOENT: case ENOTDIR: return false; // doesn't exist; fail default: KJ_FAIL_SYSCALL("fstatat(fd, toPath)", error, toPath) { return false; } } else { // already exists; continue } } else { // Neither CREATE nor MODIFY. return false; } // Start over in create-and-modify mode. return tryCommitReplacement(toPath, fromDirFd, fromPath, WriteMode::CREATE | WriteMode::MODIFY, errorReason); } } template class ReplacerImpl final: public Directory::Replacer { public: ReplacerImpl(Own&& object, const DiskHandle& handle, String&& tempPath, String&& path, WriteMode mode) : Directory::Replacer(mode), object(kj::mv(object)), handle(handle), tempPath(kj::mv(tempPath)), path(kj::mv(path)) {} ~ReplacerImpl() noexcept(false) { if (!committed) { rmrf(handle.fd, tempPath); } } const T& get() override { return *object; } bool tryCommit() override { KJ_ASSERT(!committed, "already committed") { return false; } return committed = handle.tryCommitReplacement(path, handle.fd, tempPath, Directory::Replacer::mode); } private: Own object; const DiskHandle& handle; String tempPath; String path; bool committed = false; // true if *successfully* committed (in which case tempPath is gone) }; template class BrokenReplacer final: public Directory::Replacer { // For recovery path when exceptions are disabled. public: BrokenReplacer(Own inner) : Directory::Replacer(WriteMode::CREATE | WriteMode::MODIFY), inner(kj::mv(inner)) {} const T& get() override { return *inner; } bool tryCommit() override { return false; } private: Own inner; }; Maybe> tryOpenFile(PathPtr path, WriteMode mode) const { return tryOpenFileInternal(path, mode, false).map(newDiskFile); } Own> replaceFile(PathPtr path, WriteMode mode) const { mode_t acl = 0666; if (has(mode, WriteMode::EXECUTABLE)) { acl = 0777; } if (has(mode, WriteMode::PRIVATE)) { acl &= 0700; } int newFd_; KJ_IF_MAYBE(temp, createNamedTemporary(path, mode, [&](StringPtr candidatePath) { return newFd_ = openat(fd, candidatePath.cStr(), O_RDWR | O_CREAT | O_EXCL | MAYBE_O_CLOEXEC, acl); })) { AutoCloseFd newFd(newFd_); #ifndef O_CLOEXEC setCloexec(newFd); #endif return heap>(newDiskFile(kj::mv(newFd)), *this, kj::mv(*temp), path.toString(), mode); } else { // threw, but exceptions are disabled return heap>(newInMemoryFile(nullClock())); } } Own createTemporary() const { int newFd_; #if __linux__ && defined(O_TMPFILE) // Use syscall() to work around glibc bug with O_TMPFILE: // https://sourceware.org/bugzilla/show_bug.cgi?id=17523 KJ_SYSCALL_HANDLE_ERRORS(newFd_ = syscall( SYS_openat, fd.get(), ".", O_RDWR | O_TMPFILE, 0700)) { case EOPNOTSUPP: case EINVAL: case EISDIR: // Maybe not supported by this kernel / filesystem. Fall back to below. break; default: KJ_FAIL_SYSCALL("open(O_TMPFILE)", error) { break; } break; } else { AutoCloseFd newFd(newFd_); #ifndef O_CLOEXEC setCloexec(newFd); #endif return newDiskFile(kj::mv(newFd)); } #endif KJ_IF_MAYBE(temp, createNamedTemporary(Path("unnamed"), WriteMode::CREATE, [&](StringPtr path) { return newFd_ = openat(fd, path.cStr(), O_RDWR | O_CREAT | O_EXCL | MAYBE_O_CLOEXEC, 0600); })) { AutoCloseFd newFd(newFd_); #ifndef O_CLOEXEC setCloexec(newFd); #endif auto result = newDiskFile(kj::mv(newFd)); KJ_SYSCALL(unlinkat(fd, temp->cStr(), 0)) { break; } return kj::mv(result); } else { // threw, but exceptions are disabled return newInMemoryFile(nullClock()); } } Maybe> tryAppendFile(PathPtr path, WriteMode mode) const { return tryOpenFileInternal(path, mode, true).map(newDiskAppendableFile); } Maybe> tryOpenSubdir(PathPtr path, WriteMode mode) const { // Must create before open. if (has(mode, WriteMode::CREATE)) { if (!tryMkdir(path, mode, false)) return nullptr; } return tryOpenSubdirInternal(path).map(newDiskDirectory); } Own> replaceSubdir(PathPtr path, WriteMode mode) const { mode_t acl = has(mode, WriteMode::PRIVATE) ? 0700 : 0777; KJ_IF_MAYBE(temp, createNamedTemporary(path, mode, [&](StringPtr candidatePath) { return mkdirat(fd, candidatePath.cStr(), acl); })) { int subdirFd_; KJ_SYSCALL_HANDLE_ERRORS(subdirFd_ = openat( fd, temp->cStr(), O_RDONLY | MAYBE_O_CLOEXEC | MAYBE_O_DIRECTORY)) { default: KJ_FAIL_SYSCALL("open(just-created-temporary)", error); return heap>(newInMemoryDirectory(nullClock())); } AutoCloseFd subdirFd(subdirFd_); #ifndef O_CLOEXEC setCloexec(subdirFd); #endif return heap>( newDiskDirectory(kj::mv(subdirFd)), *this, kj::mv(*temp), path.toString(), mode); } else { // threw, but exceptions are disabled return heap>(newInMemoryDirectory(nullClock())); } } bool trySymlink(PathPtr linkpath, StringPtr content, WriteMode mode) const { return tryReplaceNode(linkpath, mode, [&](StringPtr candidatePath) { return symlinkat(content.cStr(), fd, candidatePath.cStr()); }); } bool tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode, const Directory& self) const { KJ_REQUIRE(toPath.size() > 0, "can't replace self") { return false; } if (mode == TransferMode::LINK) { KJ_IF_MAYBE(fromFd, fromDirectory.getFd()) { // Other is a disk directory, so we can hopefully do an efficient move/link. return tryReplaceNode(toPath, toMode, [&](StringPtr candidatePath) { return linkat(*fromFd, fromPath.toString().cStr(), fd, candidatePath.cStr(), 0); }); }; } else if (mode == TransferMode::MOVE) { KJ_IF_MAYBE(fromFd, fromDirectory.getFd()) { KJ_ASSERT(mode == TransferMode::MOVE); int error = 0; if (tryCommitReplacement(toPath.toString(), *fromFd, fromPath.toString(), toMode, &error)) { return true; } else switch (error) { case 0: // Plain old WriteMode precondition failure. return false; case EXDEV: // Can't move between devices. Fall back to default implementation, which does // copy/delete. break; case ENOENT: // Either the destination directory doesn't exist or the source path doesn't exist. // Unfortunately we don't really know. If CREATE_PARENT was provided, try creating // the parent directory. Otherwise, we don't actually need to distinguish between // these two errors; just return false. if (has(toMode, WriteMode::CREATE) && has(toMode, WriteMode::CREATE_PARENT) && toPath.size() > 0 && tryMkdir(toPath.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryTransfer(toPath, toMode - WriteMode::CREATE_PARENT, fromDirectory, fromPath, mode, self); } return false; default: KJ_FAIL_SYSCALL("rename(fromPath, toPath)", error, fromPath, toPath) { return false; } } } } // OK, we can't do anything efficient using the OS. Fall back to default implementation. return self.Directory::tryTransfer(toPath, toMode, fromDirectory, fromPath, mode); } bool tryRemove(PathPtr path) const { return rmrf(fd, path.toString()); } protected: AutoCloseFd fd; }; #define FSNODE_METHODS(classname) \ Maybe getFd() const override { return DiskHandle::getFd(); } \ \ Own cloneFsNode() const override { \ return heap(DiskHandle::clone()); \ } \ \ Metadata stat() const override { return DiskHandle::stat(); } \ void sync() const override { DiskHandle::sync(); } \ void datasync() const override { DiskHandle::datasync(); } class DiskReadableFile final: public ReadableFile, public DiskHandle { public: DiskReadableFile(AutoCloseFd&& fd): DiskHandle(kj::mv(fd)) {} FSNODE_METHODS(DiskReadableFile); size_t read(uint64_t offset, ArrayPtr buffer) const override { return DiskHandle::read(offset, buffer); } Array mmap(uint64_t offset, uint64_t size) const override { return DiskHandle::mmap(offset, size); } Array mmapPrivate(uint64_t offset, uint64_t size) const override { return DiskHandle::mmapPrivate(offset, size); } }; class DiskAppendableFile final: public AppendableFile, public DiskHandle, public FdOutputStream { public: DiskAppendableFile(AutoCloseFd&& fd) : DiskHandle(kj::mv(fd)), FdOutputStream(DiskHandle::fd.get()) {} FSNODE_METHODS(DiskAppendableFile); void write(const void* buffer, size_t size) override { FdOutputStream::write(buffer, size); } void write(ArrayPtr> pieces) override { FdOutputStream::write(pieces); } }; class DiskFile final: public File, public DiskHandle { public: DiskFile(AutoCloseFd&& fd): DiskHandle(kj::mv(fd)) {} FSNODE_METHODS(DiskFile); size_t read(uint64_t offset, ArrayPtr buffer) const override { return DiskHandle::read(offset, buffer); } Array mmap(uint64_t offset, uint64_t size) const override { return DiskHandle::mmap(offset, size); } Array mmapPrivate(uint64_t offset, uint64_t size) const override { return DiskHandle::mmapPrivate(offset, size); } void write(uint64_t offset, ArrayPtr data) const override { DiskHandle::write(offset, data); } void zero(uint64_t offset, uint64_t size) const override { DiskHandle::zero(offset, size); } void truncate(uint64_t size) const override { DiskHandle::truncate(size); } Own mmapWritable(uint64_t offset, uint64_t size) const override { return DiskHandle::mmapWritable(offset, size); } size_t copy(uint64_t offset, const ReadableFile& from, uint64_t fromOffset, uint64_t size) const override { KJ_IF_MAYBE(result, DiskHandle::copy(offset, from, fromOffset, size)) { return *result; } else { return File::copy(offset, from, fromOffset, size); } } }; class DiskReadableDirectory final: public ReadableDirectory, public DiskHandle { public: DiskReadableDirectory(AutoCloseFd&& fd): DiskHandle(kj::mv(fd)) {} FSNODE_METHODS(DiskReadableDirectory); Array listNames() const override { return DiskHandle::listNames(); } Array listEntries() const override { return DiskHandle::listEntries(); } bool exists(PathPtr path) const override { return DiskHandle::exists(path); } Maybe tryLstat(PathPtr path) const override { return DiskHandle::tryLstat(path); } Maybe> tryOpenFile(PathPtr path) const override { return DiskHandle::tryOpenFile(path); } Maybe> tryOpenSubdir(PathPtr path) const override { return DiskHandle::tryOpenSubdir(path); } Maybe tryReadlink(PathPtr path) const override { return DiskHandle::tryReadlink(path); } }; class DiskDirectory final: public Directory, public DiskHandle { public: DiskDirectory(AutoCloseFd&& fd): DiskHandle(kj::mv(fd)) {} FSNODE_METHODS(DiskDirectory); Array listNames() const override { return DiskHandle::listNames(); } Array listEntries() const override { return DiskHandle::listEntries(); } bool exists(PathPtr path) const override { return DiskHandle::exists(path); } Maybe tryLstat(PathPtr path) const override { return DiskHandle::tryLstat(path); } Maybe> tryOpenFile(PathPtr path) const override { return DiskHandle::tryOpenFile(path); } Maybe> tryOpenSubdir(PathPtr path) const override { return DiskHandle::tryOpenSubdir(path); } Maybe tryReadlink(PathPtr path) const override { return DiskHandle::tryReadlink(path); } Maybe> tryOpenFile(PathPtr path, WriteMode mode) const override { return DiskHandle::tryOpenFile(path, mode); } Own> replaceFile(PathPtr path, WriteMode mode) const override { return DiskHandle::replaceFile(path, mode); } Own createTemporary() const override { return DiskHandle::createTemporary(); } Maybe> tryAppendFile(PathPtr path, WriteMode mode) const override { return DiskHandle::tryAppendFile(path, mode); } Maybe> tryOpenSubdir(PathPtr path, WriteMode mode) const override { return DiskHandle::tryOpenSubdir(path, mode); } Own> replaceSubdir(PathPtr path, WriteMode mode) const override { return DiskHandle::replaceSubdir(path, mode); } bool trySymlink(PathPtr linkpath, StringPtr content, WriteMode mode) const override { return DiskHandle::trySymlink(linkpath, content, mode); } bool tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const override { return DiskHandle::tryTransfer(toPath, toMode, fromDirectory, fromPath, mode, *this); } // tryTransferTo() not implemented because we have nothing special we can do. bool tryRemove(PathPtr path) const override { return DiskHandle::tryRemove(path); } }; class DiskFilesystem final: public Filesystem { public: DiskFilesystem() : root(openDir("/")), current(openDir(".")), currentPath(computeCurrentPath()) {} const Directory& getRoot() const override { return root; } const Directory& getCurrent() const override { return current; } PathPtr getCurrentPath() const override { return currentPath; } private: DiskDirectory root; DiskDirectory current; Path currentPath; static AutoCloseFd openDir(const char* dir) { int newFd; KJ_SYSCALL(newFd = open(dir, O_RDONLY | MAYBE_O_CLOEXEC | MAYBE_O_DIRECTORY)); AutoCloseFd result(newFd); #ifndef O_CLOEXEC setCloexec(result); #endif return result; } static Path computeCurrentPath() { // If env var PWD is set and points to the current directory, use it. This captures the current // path according to the user's shell, which may differ from the kernel's idea in the presence // of symlinks. const char* pwd = getenv("PWD"); if (pwd != nullptr) { Path result = nullptr; struct stat pwdStat, dotStat; KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { KJ_ASSERT(pwd[0] == '/') { return; } result = Path::parse(pwd + 1); KJ_SYSCALL(lstat(result.toString(true).cStr(), &pwdStat), result) { return; } KJ_SYSCALL(lstat(".", &dotStat)) { return; } })) { // failed, give up on PWD KJ_LOG(WARNING, "PWD environment variable seems invalid", pwd, *e); } else { if (pwdStat.st_ino == dotStat.st_ino && pwdStat.st_dev == dotStat.st_dev) { return kj::mv(result); } else { KJ_LOG(WARNING, "PWD environment variable doesn't match current directory", pwd); } } } size_t size = 256; retry: KJ_STACK_ARRAY(char, buf, size, 256, 4096); if (getcwd(buf.begin(), size) == nullptr) { int error = errno; if (error == ENAMETOOLONG) { size *= 2; goto retry; } else { KJ_FAIL_SYSCALL("getcwd()", error); } } StringPtr path = buf.begin(); // On Linux, the path will start with "(unreachable)" if the working directory is not a subdir // of the root directory, which is possible via chroot() or mount namespaces. KJ_ASSERT(!path.startsWith("(unreachable)"), "working directory is not reachable from root", path); KJ_ASSERT(path.startsWith("/"), "current directory is not absolute", path); return Path::parse(path.slice(1)); } }; } // namespace Own newDiskReadableFile(kj::AutoCloseFd fd) { return heap(kj::mv(fd)); } Own newDiskAppendableFile(kj::AutoCloseFd fd) { return heap(kj::mv(fd)); } Own newDiskFile(kj::AutoCloseFd fd) { return heap(kj::mv(fd)); } Own newDiskReadableDirectory(kj::AutoCloseFd fd) { return heap(kj::mv(fd)); } Own newDiskDirectory(kj::AutoCloseFd fd) { return heap(kj::mv(fd)); } Own newDiskFilesystem() { return heap(); } } // namespace kj #endif // !_WIN32 capnproto-c++-0.8.0/src/kj/filesystem-disk-win32.c++0000644000175000017500000016357013650101756022531 0ustar00kentonkenton00000000000000// Copyright (c) 2015 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #if _WIN32 // For Unix implementation, see filesystem-disk-unix.c++. // Request Vista-level APIs. #define WINVER 0x0600 #define _WIN32_WINNT 0x0600 #define WIN32_LEAN_AND_MEAN // ::eyeroll:: #include "filesystem.h" #include "debug.h" #include "encoding.h" #include "vector.h" #include #include #include #include #include "windows-sanity.h" namespace kj { static Own newDiskReadableDirectory(AutoCloseHandle fd, Path&& path); static Own newDiskDirectory(AutoCloseHandle fd, Path&& path); static AutoCloseHandle* getHandlePointerHack(File& file) { return nullptr; } static AutoCloseHandle* getHandlePointerHack(Directory& dir); static Path* getPathPointerHack(File& file) { return nullptr; } static Path* getPathPointerHack(Directory& dir); namespace { struct REPARSE_DATA_BUFFER { // From ntifs.h, which is part of the driver development kit so not necessarily available I // guess. ULONG ReparseTag; USHORT ReparseDataLength; USHORT Reserved; union { struct { USHORT SubstituteNameOffset; USHORT SubstituteNameLength; USHORT PrintNameOffset; USHORT PrintNameLength; ULONG Flags; WCHAR PathBuffer[1]; } SymbolicLinkReparseBuffer; struct { USHORT SubstituteNameOffset; USHORT SubstituteNameLength; USHORT PrintNameOffset; USHORT PrintNameLength; WCHAR PathBuffer[1]; } MountPointReparseBuffer; struct { UCHAR DataBuffer[1]; } GenericReparseBuffer; }; }; #define HIDDEN_PREFIX ".kj-tmp." // Prefix for temp files which should be hidden when listing a directory. // // If you change this, make sure to update the unit test. static constexpr int64_t WIN32_EPOCH_OFFSET = 116444736000000000ull; // Number of 100ns intervals from Jan 1, 1601 to Jan 1, 1970. static Date toKjDate(FILETIME t) { int64_t value = (static_cast(t.dwHighDateTime) << 32) | t.dwLowDateTime; return (value - WIN32_EPOCH_OFFSET) * (100 * kj::NANOSECONDS) + UNIX_EPOCH; } static FsNode::Type modeToType(DWORD attrs, DWORD reparseTag) { if ((attrs & FILE_ATTRIBUTE_REPARSE_POINT) && reparseTag == IO_REPARSE_TAG_SYMLINK) { return FsNode::Type::SYMLINK; } if (attrs & FILE_ATTRIBUTE_DIRECTORY) return FsNode::Type::DIRECTORY; return FsNode::Type::FILE; } static FsNode::Metadata statToMetadata(const BY_HANDLE_FILE_INFORMATION& stats) { uint64_t size = (implicitCast(stats.nFileSizeHigh) << 32) | stats.nFileSizeLow; // Assume file index is usually a small number, i.e. nFileIndexHigh is usually 0. So we try to // put the serial number in the upper 32 bits and the index in the lower. uint64_t hash = ((uint64_t(stats.dwVolumeSerialNumber) << 32) ^ (uint64_t(stats.nFileIndexHigh) << 32)) | (uint64_t(stats.nFileIndexLow)); return FsNode::Metadata { modeToType(stats.dwFileAttributes, 0), size, // In theory, spaceUsed should be based on GetCompressedFileSize(), but requiring an extra // syscall for something rarely used would be sad. size, toKjDate(stats.ftLastWriteTime), stats.nNumberOfLinks, hash }; } static FsNode::Metadata statToMetadata(const WIN32_FIND_DATAW& stats) { uint64_t size = (implicitCast(stats.nFileSizeHigh) << 32) | stats.nFileSizeLow; return FsNode::Metadata { modeToType(stats.dwFileAttributes, stats.dwReserved0), size, // In theory, spaceUsed should be based on GetCompressedFileSize(), but requiring an extra // syscall for something rarely used would be sad. size, toKjDate(stats.ftLastWriteTime), // We can't get the number of links without opening the file, apparently. Meh. 1, // We can't produce a reliable hashCode without opening the file. 0 }; } static Array join16(ArrayPtr path, const wchar_t* file) { // Assumes `path` ends with a NUL terminator (and `file` is of course NUL terminated as well). size_t len = wcslen(file) + 1; auto result = kj::heapArray(path.size() + len); memcpy(result.begin(), path.begin(), path.asBytes().size() - sizeof(wchar_t)); result[path.size() - 1] = '\\'; memcpy(result.begin() + path.size(), file, len * sizeof(wchar_t)); return result; } static String dbgStr(ArrayPtr wstr) { if (wstr.size() > 0 && wstr[wstr.size() - 1] == L'\0') { wstr = wstr.slice(0, wstr.size() - 1); } return decodeWideString(wstr); } static void rmrfChildren(ArrayPtr path) { auto glob = join16(path, L"*"); WIN32_FIND_DATAW data; HANDLE handle = FindFirstFileW(glob.begin(), &data); if (handle == INVALID_HANDLE_VALUE) { auto error = GetLastError(); if (error == ERROR_FILE_NOT_FOUND) return; KJ_FAIL_WIN32("FindFirstFile", error, dbgStr(glob)) { return; } } KJ_DEFER(KJ_WIN32(FindClose(handle)) { break; }); do { // Ignore "." and "..", ugh. if (data.cFileName[0] == L'.') { if (data.cFileName[1] == L'\0' || (data.cFileName[1] == L'.' && data.cFileName[2] == L'\0')) { continue; } } auto child = join16(path, data.cFileName); // For rmrf purposes, we assume any "reparse points" are symlink-like, even if they aren't // actually the "symbolic link" reparse type, because we don't want to recursively delete any // shared content. if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) { rmrfChildren(child); uint retryCount = 0; retry: KJ_WIN32_HANDLE_ERRORS(RemoveDirectoryW(child.begin())) { case ERROR_DIR_NOT_EMPTY: // On Windows, deleting a file actually only schedules it for deletion. Under heavy // load it may take a bit for the deletion to go through. Or, if another process has // the file open, it may not be deleted until that process closes it. // // We'll repeatedly retry for up to 100ms, then give up. This is awful but there's no // way to tell for sure if the system is just being slow or if someone has the file // open. if (retryCount++ < 10) { Sleep(10); goto retry; } // fallthrough default: KJ_FAIL_WIN32("RemoveDirectory", error, dbgStr(child)) { break; } } } else { KJ_WIN32(DeleteFileW(child.begin())); } } while (FindNextFileW(handle, &data)); auto error = GetLastError(); if (error != ERROR_NO_MORE_FILES) { KJ_FAIL_WIN32("FindNextFile", error, dbgStr(path)) { return; } } } static bool rmrf(ArrayPtr path) { // Figure out whether this is a file or a directory. // // We use FindFirstFileW() because in the case of symlinks it will return info about the // symlink rather than info about the target. WIN32_FIND_DATAW data; HANDLE handle = FindFirstFileW(path.begin(), &data); if (handle == INVALID_HANDLE_VALUE) { auto error = GetLastError(); if (error == ERROR_FILE_NOT_FOUND) return false; KJ_FAIL_WIN32("FindFirstFile", error, dbgStr(path)); } KJ_WIN32(FindClose(handle)); // For remove purposes, we assume any "reparse points" are symlink-like, even if they aren't // actually the "symbolic link" reparse type, because we don't want to recursively delete any // shared content. if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) { // directory rmrfChildren(path); KJ_WIN32(RemoveDirectoryW(path.begin()), dbgStr(path)); } else { KJ_WIN32(DeleteFileW(path.begin()), dbgStr(path)); } return true; } static Path getPathFromHandle(HANDLE handle) { DWORD tryLen = MAX_PATH; for (;;) { auto temp = kj::heapArray(tryLen + 1); DWORD len = GetFinalPathNameByHandleW(handle, temp.begin(), tryLen, 0); if (len == 0) { KJ_FAIL_WIN32("GetFinalPathNameByHandleW", GetLastError()); } if (len < temp.size()) { return Path::parseWin32Api(temp.slice(0, len)); } // Try again with new length. tryLen = len; } } struct MmapRange { uint64_t offset; uint64_t size; }; static size_t getAllocationGranularity() { SYSTEM_INFO info; GetSystemInfo(&info); return info.dwAllocationGranularity; }; static MmapRange getMmapRange(uint64_t offset, uint64_t size) { // Rounds the given offset down to the nearest page boundary, and adjusts the size up to match. // (This is somewhat different from Unix: we do NOT round the size up to an even multiple of // pages.) static const uint64_t pageSize = getAllocationGranularity(); uint64_t pageMask = pageSize - 1; uint64_t realOffset = offset & ~pageMask; uint64_t end = offset + size; return { realOffset, end - realOffset }; } class MmapDisposer: public ArrayDisposer { protected: void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, void (*destroyElement)(void*)) const { auto range = getMmapRange(reinterpret_cast(firstElement), elementSize * elementCount); void* mapping = reinterpret_cast(range.offset); if (mapping != nullptr) { KJ_ASSERT(UnmapViewOfFile(mapping)) { break; } } } }; #if _MSC_VER && _MSC_VER < 1910 // TODO(msvc): MSVC 2015 can't initialize a constexpr's vtable correctly. const MmapDisposer mmapDisposer = MmapDisposer(); #else constexpr MmapDisposer mmapDisposer = MmapDisposer(); #endif void* win32Mmap(HANDLE handle, MmapRange range, DWORD pageProtect, DWORD access) { HANDLE mappingHandle; KJ_WIN32(mappingHandle = CreateFileMappingW(handle, NULL, pageProtect, 0, 0, NULL)); KJ_DEFER(KJ_WIN32(CloseHandle(mappingHandle)) { break; }); void* mapping = MapViewOfFile(mappingHandle, access, static_cast(range.offset >> 32), static_cast(range.offset), range.size); if (mapping == nullptr) { KJ_FAIL_WIN32("MapViewOfFile", GetLastError()); } // It's unclear from the documentation whether mappings will always start at a multiple of the // allocation granularity, but we depend on that later, so check it... KJ_ASSERT(getMmapRange(reinterpret_cast(mapping), 0).size == 0); return mapping; } class DiskHandle { // We need to implement each of ReadableFile, AppendableFile, File, ReadableDirectory, and // Directory for disk handles. There is a lot of implementation overlap between these, especially // stat(), sync(), etc. We can't have everything inherit from a common DiskFsNode that implements // these because then we get diamond inheritance which means we need to make all our inheritance // virtual which means downcasting requires RTTI which violates our goal of supporting compiling // with no RTTI. So instead we have the DiskHandle class which implements all the methods without // inheriting anything, and then we have DiskFile, DiskDirectory, etc. hold this and delegate to // it. Ugly, but works. public: DiskHandle(AutoCloseHandle&& handle, Maybe dirPath) : handle(kj::mv(handle)), dirPath(kj::mv(dirPath)) {} AutoCloseHandle handle; kj::Maybe dirPath; // needed for directories, empty for files Array nativePath(PathPtr path) const { return KJ_ASSERT_NONNULL(dirPath).append(path).forWin32Api(true); } // OsHandle ------------------------------------------------------------------ AutoCloseHandle clone() const { HANDLE newHandle; KJ_WIN32(DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)); return AutoCloseHandle(newHandle); } HANDLE getWin32Handle() const { return handle.get(); } // FsNode -------------------------------------------------------------------- FsNode::Metadata stat() const { BY_HANDLE_FILE_INFORMATION stats; KJ_WIN32(GetFileInformationByHandle(handle, &stats)); auto metadata = statToMetadata(stats); // Get space usage, e.g. for sparse files. Apparently the correct way to do this is to query // "compression". FILE_COMPRESSION_INFO compInfo; KJ_WIN32_HANDLE_ERRORS(GetFileInformationByHandleEx( handle, FileCompressionInfo, &compInfo, sizeof(compInfo))) { case ERROR_CALL_NOT_IMPLEMENTED: // Probably WINE. break; default: KJ_FAIL_WIN32("GetFileInformationByHandleEx(FileCompressionInfo)", error) { break; } break; } else { metadata.spaceUsed = compInfo.CompressedFileSize.QuadPart; } return metadata; } void sync() const { KJ_WIN32(FlushFileBuffers(handle)); } void datasync() const { KJ_WIN32(FlushFileBuffers(handle)); } // ReadableFile -------------------------------------------------------------- size_t read(uint64_t offset, ArrayPtr buffer) const { // ReadFile() probably never returns short reads unless it hits EOF. Unfortunately, though, // this is not documented, and it's unclear whether we can rely on it. size_t total = 0; while (buffer.size() > 0) { // Apparently, the way to fake pread() on Windows is to provide an OVERLAPPED structure even // though we're not doing overlapped I/O. OVERLAPPED overlapped; memset(&overlapped, 0, sizeof(overlapped)); overlapped.Offset = static_cast(offset); overlapped.OffsetHigh = static_cast(offset >> 32); DWORD n; KJ_WIN32_HANDLE_ERRORS(ReadFile(handle, buffer.begin(), buffer.size(), &n, &overlapped)) { case ERROR_HANDLE_EOF: // The documentation claims this shouldn't happen for synchronous reads, but it seems // to happen for me, at least under WINE. n = 0; break; default: KJ_FAIL_WIN32("ReadFile", offset, buffer.size()) { return total; } } if (n == 0) break; total += n; offset += n; buffer = buffer.slice(n, buffer.size()); } return total; } Array mmap(uint64_t offset, uint64_t size) const { if (size == 0) return nullptr; // Windows won't allow zero-length mappings auto range = getMmapRange(offset, size); const void* mapping = win32Mmap(handle, range, PAGE_READONLY, FILE_MAP_READ); return Array(reinterpret_cast(mapping) + (offset - range.offset), size, mmapDisposer); } Array mmapPrivate(uint64_t offset, uint64_t size) const { if (size == 0) return nullptr; // Windows won't allow zero-length mappings auto range = getMmapRange(offset, size); void* mapping = win32Mmap(handle, range, PAGE_READONLY, FILE_MAP_COPY); return Array(reinterpret_cast(mapping) + (offset - range.offset), size, mmapDisposer); } // File ---------------------------------------------------------------------- void write(uint64_t offset, ArrayPtr data) const { // WriteFile() probably never returns short writes unless there's no space left on disk. // Unfortunately, though, this is not documented, and it's unclear whether we can rely on it. while (data.size() > 0) { // Apparently, the way to fake pwrite() on Windows is to provide an OVERLAPPED structure even // though we're not doing overlapped I/O. OVERLAPPED overlapped; memset(&overlapped, 0, sizeof(overlapped)); overlapped.Offset = static_cast(offset); overlapped.OffsetHigh = static_cast(offset >> 32); DWORD n; KJ_WIN32(WriteFile(handle, data.begin(), data.size(), &n, &overlapped)); KJ_ASSERT(n > 0, "WriteFile() returned zero?"); offset += n; data = data.slice(n, data.size()); } } void zero(uint64_t offset, uint64_t size) const { FILE_ZERO_DATA_INFORMATION info; memset(&info, 0, sizeof(info)); info.FileOffset.QuadPart = offset; info.BeyondFinalZero.QuadPart = offset + size; DWORD dummy; KJ_WIN32_HANDLE_ERRORS(DeviceIoControl(handle, FSCTL_SET_ZERO_DATA, &info, sizeof(info), NULL, 0, &dummy, NULL)) { case ERROR_NOT_SUPPORTED: { // Dang. Let's do it the hard way. static const byte ZEROS[4096] = { 0 }; while (size > sizeof(ZEROS)) { write(offset, ZEROS); size -= sizeof(ZEROS); offset += sizeof(ZEROS); } write(offset, kj::arrayPtr(ZEROS, size)); break; } default: KJ_FAIL_WIN32("DeviceIoControl(FSCTL_SET_ZERO_DATA)", error); break; } } void truncate(uint64_t size) const { // SetEndOfFile() would require seeking the file. It looks like SetFileInformationByHandle() // lets us avoid this! FILE_END_OF_FILE_INFO info; memset(&info, 0, sizeof(info)); info.EndOfFile.QuadPart = size; KJ_WIN32_HANDLE_ERRORS( SetFileInformationByHandle(handle, FileEndOfFileInfo, &info, sizeof(info))) { case ERROR_CALL_NOT_IMPLEMENTED: { // Wine doesn't implement this. :( LONG currentHigh = 0; LONG currentLow = SetFilePointer(handle, 0, ¤tHigh, FILE_CURRENT); if (currentLow == INVALID_SET_FILE_POINTER) { KJ_FAIL_WIN32("SetFilePointer", GetLastError()); } uint64_t current = (uint64_t(currentHigh) << 32) | uint64_t((ULONG)currentLow); LONG endLow = size & 0x00000000ffffffffull; LONG endHigh = size >> 32; if (SetFilePointer(handle, endLow, &endHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { KJ_FAIL_WIN32("SetFilePointer", GetLastError()); } KJ_WIN32(SetEndOfFile(handle)); if (current < size) { if (SetFilePointer(handle, currentLow, ¤tHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { KJ_FAIL_WIN32("SetFilePointer", GetLastError()); } } break; } default: KJ_FAIL_WIN32("SetFileInformationByHandle", error); } } class WritableFileMappingImpl final: public WritableFileMapping { public: WritableFileMappingImpl(Array bytes): bytes(kj::mv(bytes)) {} ArrayPtr get() const override { // const_cast OK because WritableFileMapping does indeed provide a writable view despite // being const itself. return arrayPtr(const_cast(bytes.begin()), bytes.size()); } void changed(ArrayPtr slice) const override { KJ_REQUIRE(slice.begin() >= bytes.begin() && slice.end() <= bytes.end(), "byte range is not part of this mapping"); // Nothing needed here -- NT tracks dirty pages. } void sync(ArrayPtr slice) const override { KJ_REQUIRE(slice.begin() >= bytes.begin() && slice.end() <= bytes.end(), "byte range is not part of this mapping"); // Zero is treated specially by FlushViewOfFile(), so check for it. (This also handles the // case where `bytes` is actually empty and not a real mapping.) if (slice.size() > 0) { KJ_WIN32(FlushViewOfFile(slice.begin(), slice.size())); } } private: Array bytes; }; Own mmapWritable(uint64_t offset, uint64_t size) const { if (size == 0) { // Windows won't allow zero-length mappings return heap(nullptr); } auto range = getMmapRange(offset, size); void* mapping = win32Mmap(handle, range, PAGE_READWRITE, FILE_MAP_ALL_ACCESS); auto array = Array(reinterpret_cast(mapping) + (offset - range.offset), size, mmapDisposer); return heap(kj::mv(array)); } // copy() is not optimized on Windows. // ReadableDirectory --------------------------------------------------------- template auto list(bool needTypes, Func&& func) const -> Array(), instance()))>> { PathPtr path = KJ_ASSERT_NONNULL(dirPath); auto glob = join16(path.forWin32Api(true), L"*"); // TODO(perf): Use FindFileEx() with FindExInfoBasic? Not apparently supported on Vista. // TODO(someday): Use NtQueryDirectoryObject() instead? It's "internal", but so much cleaner. WIN32_FIND_DATAW data; HANDLE handle = FindFirstFileW(glob.begin(), &data); if (handle == INVALID_HANDLE_VALUE) { auto error = GetLastError(); if (error == ERROR_FILE_NOT_FOUND) return nullptr; KJ_FAIL_WIN32("FindFirstFile", error, dbgStr(glob)); } KJ_DEFER(KJ_WIN32(FindClose(handle)) { break; }); typedef Decay(), instance()))> Entry; kj::Vector entries; do { auto name = decodeUtf16( arrayPtr(reinterpret_cast(data.cFileName), wcslen(data.cFileName))); if (name != "." && name != ".." && !name.startsWith(HIDDEN_PREFIX)) { entries.add(func(name, modeToType(data.dwFileAttributes, data.dwReserved0))); } } while (FindNextFileW(handle, &data)); auto error = GetLastError(); if (error != ERROR_NO_MORE_FILES) { KJ_FAIL_WIN32("FindNextFile", error, path); } auto result = entries.releaseAsArray(); std::sort(result.begin(), result.end()); return result; } Array listNames() const { return list(false, [](StringPtr name, FsNode::Type type) { return heapString(name); }); } Array listEntries() const { return list(true, [](StringPtr name, FsNode::Type type) { return ReadableDirectory::Entry { type, heapString(name), }; }); } bool exists(PathPtr path) const { DWORD result = GetFileAttributesW(nativePath(path).begin()); if (result == INVALID_FILE_ATTRIBUTES) { auto error = GetLastError(); switch (error) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: return false; default: KJ_FAIL_WIN32("GetFileAttributesEx(path)", error, path) { return false; } } } else { return true; } } Maybe tryLstat(PathPtr path) const { // We use FindFirstFileW() because in the case of symlinks it will return info about the // symlink rather than info about the target. WIN32_FIND_DATAW data; HANDLE handle = FindFirstFileW(nativePath(path).begin(), &data); if (handle == INVALID_HANDLE_VALUE) { auto error = GetLastError(); if (error == ERROR_FILE_NOT_FOUND) return nullptr; KJ_FAIL_WIN32("FindFirstFile", error, path); } else { KJ_WIN32(FindClose(handle)); return statToMetadata(data); } } Maybe> tryOpenFile(PathPtr path) const { HANDLE newHandle; KJ_WIN32_HANDLE_ERRORS(newHandle = CreateFileW( nativePath(path).begin(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: return nullptr; default: KJ_FAIL_WIN32("CreateFile(path, OPEN_EXISTING)", error, path) { return nullptr; } } return newDiskReadableFile(kj::AutoCloseHandle(newHandle)); } Maybe tryOpenSubdirInternal(PathPtr path) const { HANDLE newHandle; KJ_WIN32_HANDLE_ERRORS(newHandle = CreateFileW( nativePath(path).begin(), GENERIC_READ, // When opening directories, we do NOT use FILE_SHARE_DELETE, because we need the directory // path to remain vaild. // // TODO(someday): Use NtCreateFile() and related "internal" APIs that allow for // openat()-like behavior? FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, // apparently, this flag is required for directories NULL)) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: return nullptr; default: KJ_FAIL_WIN32("CreateFile(directoryPath, OPEN_EXISTING)", error, path) { return nullptr; } } kj::AutoCloseHandle ownHandle(newHandle); BY_HANDLE_FILE_INFORMATION info; KJ_WIN32(GetFileInformationByHandle(ownHandle, &info)); KJ_REQUIRE(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY, "not a directory", path); return kj::mv(ownHandle); } Maybe> tryOpenSubdir(PathPtr path) const { return tryOpenSubdirInternal(path).map([&](AutoCloseHandle&& handle) { return newDiskReadableDirectory(kj::mv(handle), KJ_ASSERT_NONNULL(dirPath).append(path)); }); } Maybe tryReadlink(PathPtr path) const { // Windows symlinks work differently from Unix. Generally they are set up by the system // administrator and apps are expected to treat them transparently. Hence, on Windows, we act // as if nothing is a symlink by always returning null here. // TODO(someday): If we want to treat Windows symlinks more like Unix ones, start by reverting // the comment that added this comment. return nullptr; } // Directory ----------------------------------------------------------------- static LPSECURITY_ATTRIBUTES makeSecAttr(WriteMode mode) { if (has(mode, WriteMode::PRIVATE)) { KJ_UNIMPLEMENTED("WriteMode::PRIVATE on Win32 is not implemented"); } return nullptr; } bool tryMkdir(PathPtr path, WriteMode mode, bool noThrow) const { // Internal function to make a directory. auto filename = nativePath(path); KJ_WIN32_HANDLE_ERRORS(CreateDirectoryW(filename.begin(), makeSecAttr(mode))) { case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: { // Apparently this path exists. if (!has(mode, WriteMode::MODIFY)) { // Require exclusive create. return false; } // MODIFY is allowed, so we just need to check whether the existing entry is a directory. DWORD attr = GetFileAttributesW(filename.begin()); if (attr == INVALID_FILE_ATTRIBUTES) { // CreateDirectory() says it already exists but we can't get attributes. Maybe it's a // dangling link, or maybe we can't access it for some reason. Assume failure. // // TODO(someday): Maybe we should be creating the directory at the target of the // link? goto failed; } return attr & FILE_ATTRIBUTE_DIRECTORY; } case ERROR_PATH_NOT_FOUND: if (has(mode, WriteMode::CREATE_PARENT) && path.size() > 0 && tryMkdir(path.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryMkdir(path, mode - WriteMode::CREATE_PARENT, noThrow); } else { goto failed; } default: failed: if (noThrow) { // Caller requested no throwing. return false; } else { KJ_FAIL_WIN32("CreateDirectory", error, path); } } return true; } kj::Maybe> createNamedTemporary( PathPtr finalName, WriteMode mode, Path& kjTempPath, Function tryCreate) const { // Create a temporary file which will eventually replace `finalName`. // // Calls `tryCreate` to actually create the temporary, passing in the desired path. tryCreate() // is expected to behave like a win32 call, returning a BOOL and setting `GetLastError()` on // error. tryCreate() MUST fail with ERROR_{FILE,ALREADY}_EXISTS if the path exists -- this is // not checked in advance, since it needs to be checked atomically. In the case of // ERROR_*_EXISTS, tryCreate() will be called again with a new path. // // Returns the temporary path that succeeded. Only returns nullptr if there was an exception // but we're compiled with -fno-exceptions. // // The optional parameter `kjTempPath` is filled in with the KJ Path of the temporary. if (finalName.size() == 0) { KJ_FAIL_REQUIRE("can't replace self") { break; } return nullptr; } static uint counter = 0; static const DWORD pid = GetCurrentProcessId(); auto tempName = kj::str(HIDDEN_PREFIX, pid, '.', counter++, '.', finalName.basename()[0], ".partial"); kjTempPath = finalName.parent().append(tempName); auto path = nativePath(kjTempPath); KJ_WIN32_HANDLE_ERRORS(tryCreate(path.begin())) { case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: // Try again with a new counter value. return createNamedTemporary(finalName, mode, kj::mv(tryCreate)); case ERROR_PATH_NOT_FOUND: if (has(mode, WriteMode::CREATE_PARENT) && finalName.size() > 1 && tryMkdir(finalName.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. mode = mode - WriteMode::CREATE_PARENT; return createNamedTemporary(finalName, mode, kj::mv(tryCreate)); } // fallthrough default: KJ_FAIL_WIN32("create(path)", error, path) { break; } return nullptr; } return kj::mv(path); } kj::Maybe> createNamedTemporary( PathPtr finalName, WriteMode mode, Function tryCreate) const { Path dummy = nullptr; return createNamedTemporary(finalName, mode, dummy, kj::mv(tryCreate)); } bool tryReplaceNode(PathPtr path, WriteMode mode, Function tryCreate) const { // Replaces the given path with an object created by calling tryCreate(). // // tryCreate() must behave like a win32 call which creates the node at the path passed to it, // returning FALSE error. If the path passed to tryCreate already exists, it MUST fail with // ERROR_{FILE,ALREADY}_EXISTS. // // When `mode` includes MODIFY, replaceNode() reacts to ERROR_*_EXISTS by creating the // node in a temporary location and then rename()ing it into place. if (path.size() == 0) { KJ_FAIL_REQUIRE("can't replace self") { return false; } } auto filename = nativePath(path); if (has(mode, WriteMode::CREATE)) { // First try just cerating the node in-place. KJ_WIN32_HANDLE_ERRORS(tryCreate(filename.begin())) { case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: // Target exists. if (has(mode, WriteMode::MODIFY)) { // Fall back to MODIFY path, below. break; } else { return false; } case ERROR_PATH_NOT_FOUND: if (has(mode, WriteMode::CREATE_PARENT) && path.size() > 0 && tryMkdir(path.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryReplaceNode(path, mode - WriteMode::CREATE_PARENT, kj::mv(tryCreate)); } // fallthrough default: KJ_FAIL_WIN32("create(path)", error, path) { return false; } } else { // Success. return true; } } // Either we don't have CREATE mode or the target already exists. We need to perform a // replacement instead. KJ_IF_MAYBE(tempPath, createNamedTemporary(path, mode, kj::mv(tryCreate))) { if (tryCommitReplacement(path, *tempPath, mode)) { return true; } else { KJ_WIN32_HANDLE_ERRORS(DeleteFileW(tempPath->begin())) { case ERROR_FILE_NOT_FOUND: // meh break; default: KJ_FAIL_WIN32("DeleteFile(tempPath)", error, dbgStr(*tempPath)); } return false; } } else { // threw, but exceptions are disabled return false; } } Maybe tryOpenFileInternal(PathPtr path, WriteMode mode, bool append) const { DWORD disposition; if (has(mode, WriteMode::MODIFY)) { if (has(mode, WriteMode::CREATE)) { disposition = OPEN_ALWAYS; } else { disposition = OPEN_EXISTING; } } else { if (has(mode, WriteMode::CREATE)) { disposition = CREATE_NEW; } else { // Neither CREATE nor MODIFY -- impossible to satisfy preconditions. return nullptr; } } DWORD access = GENERIC_READ | GENERIC_WRITE; if (append) { // FILE_GENERIC_WRITE includes both FILE_APPEND_DATA and FILE_WRITE_DATA, but we only want // the former. There are also a zillion other bits that we need, annoyingly. access = (FILE_READ_ATTRIBUTES | FILE_GENERIC_WRITE) & ~FILE_WRITE_DATA; } auto filename = path.toString(); HANDLE newHandle; KJ_WIN32_HANDLE_ERRORS(newHandle = CreateFileW( nativePath(path).begin(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, makeSecAttr(mode), disposition, FILE_ATTRIBUTE_NORMAL, NULL)) { case ERROR_PATH_NOT_FOUND: if (has(mode, WriteMode::CREATE)) { // A parent directory didn't exist. Maybe cerate it. if (has(mode, WriteMode::CREATE_PARENT) && path.size() > 0 && tryMkdir(path.parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryOpenFileInternal(path, mode - WriteMode::CREATE_PARENT, append); } KJ_FAIL_REQUIRE("parent is not a directory", path) { return nullptr; } } else { // MODIFY-only mode. ERROR_PATH_NOT_FOUND = parent path doesn't exist = return null. return nullptr; } case ERROR_FILE_NOT_FOUND: if (!has(mode, WriteMode::CREATE)) { // MODIFY-only mode. ERROR_FILE_NOT_FOUND = doesn't exist = return null. return nullptr; } goto failed; case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: if (!has(mode, WriteMode::MODIFY)) { // CREATE-only mode. ERROR_ALREADY_EXISTS = already exists = return null. return nullptr; } goto failed; default: failed: KJ_FAIL_WIN32("CreateFile", error, path) { return nullptr; } } return kj::AutoCloseHandle(newHandle); } bool tryCommitReplacement( PathPtr toPath, ArrayPtr fromPath, WriteMode mode, kj::Maybe pathForCreatingParents = nullptr) const { // Try to use MoveFileEx() to replace `toPath` with `fromPath`. auto wToPath = nativePath(toPath); DWORD flags = has(mode, WriteMode::MODIFY) ? MOVEFILE_REPLACE_EXISTING : 0; if (!has(mode, WriteMode::CREATE)) { // Non-atomically verify that target exists. There's no way to make this atomic. DWORD result = GetFileAttributesW(wToPath.begin()); if (result == INVALID_FILE_ATTRIBUTES) { auto error = GetLastError(); switch (error) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: return false; default: KJ_FAIL_WIN32("GetFileAttributesEx(toPath)", error, toPath) { return false; } } } } KJ_WIN32_HANDLE_ERRORS(MoveFileExW(fromPath.begin(), wToPath.begin(), flags)) { case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: // We must not be in MODIFY mode. return false; case ERROR_PATH_NOT_FOUND: KJ_IF_MAYBE(p, pathForCreatingParents) { if (has(mode, WriteMode::CREATE_PARENT) && p->size() > 0 && tryMkdir(p->parent(), WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT, true)) { // Retry, but make sure we don't try to create the parent again. return tryCommitReplacement(toPath, fromPath, mode - WriteMode::CREATE_PARENT); } } goto default_; case ERROR_ACCESS_DENIED: { // This often means that the target already exists and cannot be replaced, e.g. because // it is a directory. Move it out of the way first, then move our replacement in, then // delete the old thing. if (has(mode, WriteMode::MODIFY)) { KJ_IF_MAYBE(tempName, createNamedTemporary(toPath, WriteMode::CREATE, [&](const wchar_t* tempName2) { return MoveFileW(wToPath.begin(), tempName2); })) { KJ_WIN32_HANDLE_ERRORS(MoveFileW(fromPath.begin(), wToPath.begin())) { default: // Try to move back. MoveFileW(tempName->begin(), wToPath.begin()); KJ_FAIL_WIN32("MoveFile", error, dbgStr(fromPath), dbgStr(wToPath)) { return false; } } // Succeeded, delete temporary. rmrf(*tempName); return true; } else { // createNamedTemporary() threw exception but exceptions are disabled. return false; } } else { // Not MODIFY, so no overwrite allowed. If the file really does exist, we need to return // false. if (GetFileAttributesW(wToPath.begin()) != INVALID_FILE_ATTRIBUTES) { return false; } } goto default_; } default: default_: KJ_FAIL_WIN32("MoveFileEx", error, dbgStr(wToPath), dbgStr(fromPath)) { return false; } } return true; } template class ReplacerImpl final: public Directory::Replacer { public: ReplacerImpl(Own&& object, const DiskHandle& parentDirectory, Array&& tempPath, Path&& path, WriteMode mode) : Directory::Replacer(mode), object(kj::mv(object)), parentDirectory(parentDirectory), tempPath(kj::mv(tempPath)), path(kj::mv(path)) {} ~ReplacerImpl() noexcept(false) { if (!committed) { object = Own(); // Force close of handle before trying to delete. if (kj::isSameType()) { KJ_WIN32(DeleteFileW(tempPath.begin())) { break; } } else { rmrfChildren(tempPath); KJ_WIN32(RemoveDirectoryW(tempPath.begin())) { break; } } } } const T& get() override { return *object; } bool tryCommit() override { KJ_ASSERT(!committed, "already committed") { return false; } // For directories, we intentionally don't use FILE_SHARE_DELETE on our handle because if the // directory name changes our paths would be wrong. But, this means we can't rename the // directory here to commit it. So, we need to close the handle and then re-open it // afterwards. Ick. AutoCloseHandle* objectHandle = getHandlePointerHack(*object); if (kj::isSameType()) { *objectHandle = nullptr; } KJ_DEFER({ if (kj::isSameType()) { HANDLE newHandle = nullptr; KJ_WIN32(newHandle = CreateFileW( committed ? parentDirectory.nativePath(path).begin() : tempPath.begin(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, // apparently, this flag is required for directories NULL)) { return; } *objectHandle = AutoCloseHandle(newHandle); *getPathPointerHack(*object) = KJ_ASSERT_NONNULL(parentDirectory.dirPath).append(path); } }); return committed = parentDirectory.tryCommitReplacement( path, tempPath, Directory::Replacer::mode); } private: Own object; const DiskHandle& parentDirectory; Array tempPath; Path path; bool committed = false; // true if *successfully* committed (in which case tempPath is gone) }; template class BrokenReplacer final: public Directory::Replacer { // For recovery path when exceptions are disabled. public: BrokenReplacer(Own inner) : Directory::Replacer(WriteMode::CREATE | WriteMode::MODIFY), inner(kj::mv(inner)) {} const T& get() override { return *inner; } bool tryCommit() override { return false; } private: Own inner; }; Maybe> tryOpenFile(PathPtr path, WriteMode mode) const { return tryOpenFileInternal(path, mode, false).map(newDiskFile); } Own> replaceFile(PathPtr path, WriteMode mode) const { HANDLE newHandle_; KJ_IF_MAYBE(temp, createNamedTemporary(path, mode, [&](const wchar_t* candidatePath) { newHandle_ = CreateFileW( candidatePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, makeSecAttr(mode), CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); return newHandle_ != INVALID_HANDLE_VALUE; })) { AutoCloseHandle newHandle(newHandle_); return heap>(newDiskFile(kj::mv(newHandle)), *this, kj::mv(*temp), path.clone(), mode); } else { // threw, but exceptions are disabled return heap>(newInMemoryFile(nullClock())); } } Own createTemporary() const { HANDLE newHandle_; KJ_IF_MAYBE(temp, createNamedTemporary(Path("unnamed"), WriteMode::CREATE, [&](const wchar_t* candidatePath) { newHandle_ = CreateFileW( candidatePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, // TODO(someday): makeSecAttr(WriteMode::PRIVATE), when it's implemented CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL); return newHandle_ != INVALID_HANDLE_VALUE; })) { AutoCloseHandle newHandle(newHandle_); return newDiskFile(kj::mv(newHandle)); } else { // threw, but exceptions are disabled return newInMemoryFile(nullClock()); } } Maybe> tryAppendFile(PathPtr path, WriteMode mode) const { return tryOpenFileInternal(path, mode, true).map(newDiskAppendableFile); } Maybe> tryOpenSubdir(PathPtr path, WriteMode mode) const { // Must create before open. if (has(mode, WriteMode::CREATE)) { if (!tryMkdir(path, mode, false)) return nullptr; } return tryOpenSubdirInternal(path).map([&](AutoCloseHandle&& handle) { return newDiskDirectory(kj::mv(handle), KJ_ASSERT_NONNULL(dirPath).append(path)); }); } Own> replaceSubdir(PathPtr path, WriteMode mode) const { Path kjTempPath = nullptr; KJ_IF_MAYBE(temp, createNamedTemporary(path, mode, kjTempPath, [&](const wchar_t* candidatePath) { return CreateDirectoryW(candidatePath, makeSecAttr(mode)); })) { HANDLE subdirHandle_; KJ_WIN32_HANDLE_ERRORS(subdirHandle_ = CreateFileW( temp->begin(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, // apparently, this flag is required for directories NULL)) { default: KJ_FAIL_WIN32("CreateFile(just-created-temporary, OPEN_EXISTING)", error, path) { goto fail; } } AutoCloseHandle subdirHandle(subdirHandle_); return heap>( newDiskDirectory(kj::mv(subdirHandle), KJ_ASSERT_NONNULL(dirPath).append(kj::mv(kjTempPath))), *this, kj::mv(*temp), path.clone(), mode); } else { // threw, but exceptions are disabled fail: return heap>(newInMemoryDirectory(nullClock())); } } bool trySymlink(PathPtr linkpath, StringPtr content, WriteMode mode) const { // We can't really create symlinks on Windows. Reasons: // - We'd need to know whether the target is a file or a directory to pass the correct flags. // That means we'd need to evaluate the link content and track down the target. What if the // taget doesn't exist? It's unclear if this is even allowed on Windows. // - Apparently, creating symlinks is a privileged operation on Windows prior to Windows 10. // The flag SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE is very new. KJ_UNIMPLEMENTED( "Creating symbolic links is not supported on Windows due to semantic differences."); } bool tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode, const Directory& self) const { KJ_REQUIRE(toPath.size() > 0, "can't replace self") { return false; } // Try to get the "from" path. Array rawFromPath; #if !KJ_NO_RTTI // Oops, dynamicDowncastIfAvailable() doesn't work since this isn't a downcast, it's a // side-cast... if (auto dh = dynamic_cast(&fromDirectory)) { rawFromPath = dh->nativePath(fromPath); } else #endif KJ_IF_MAYBE(h, fromDirectory.getWin32Handle()) { // Can't downcast to DiskHandle, but getWin32Handle() returns a handle... maybe RTTI is // disabled? Or maybe this is some kind of wrapper? rawFromPath = getPathFromHandle(*h).append(fromPath).forWin32Api(true); } else { // Not a disk directory, so fall back to default implementation. return self.Directory::tryTransfer(toPath, toMode, fromDirectory, fromPath, mode); } if (mode == TransferMode::LINK) { return tryReplaceNode(toPath, toMode, [&](const wchar_t* candidatePath) { return CreateHardLinkW(candidatePath, rawFromPath.begin(), NULL); }); } else if (mode == TransferMode::MOVE) { return tryCommitReplacement(toPath, rawFromPath, toMode, toPath); } else if (mode == TransferMode::COPY) { // We can accellerate copies on Windows. if (!has(toMode, WriteMode::CREATE)) { // Non-atomically verify that target exists. There's no way to make this atomic. if (!exists(toPath)) return false; } bool failIfExists = !has(toMode, WriteMode::MODIFY); KJ_WIN32_HANDLE_ERRORS( CopyFileW(rawFromPath.begin(), nativePath(toPath).begin(), failIfExists)) { case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: return false; case ERROR_ACCESS_DENIED: // This usually means that fromPath was a directory or toPath was a direcotry. Fall back // to default implementation. break; default: KJ_FAIL_WIN32("CopyFile", error, fromPath, toPath) { return false; } } else { // Copy succeeded. return true; } } // OK, we can't do anything efficient using the OS. Fall back to default implementation. return self.Directory::tryTransfer(toPath, toMode, fromDirectory, fromPath, mode); } bool tryRemove(PathPtr path) const { return rmrf(nativePath(path)); } }; #define FSNODE_METHODS \ Maybe getWin32Handle() const override { return DiskHandle::getWin32Handle(); } \ \ Metadata stat() const override { return DiskHandle::stat(); } \ void sync() const override { DiskHandle::sync(); } \ void datasync() const override { DiskHandle::datasync(); } class DiskReadableFile final: public ReadableFile, public DiskHandle { public: DiskReadableFile(AutoCloseHandle&& handle): DiskHandle(kj::mv(handle), nullptr) {} Own cloneFsNode() const override { return heap(DiskHandle::clone()); } FSNODE_METHODS size_t read(uint64_t offset, ArrayPtr buffer) const override { return DiskHandle::read(offset, buffer); } Array mmap(uint64_t offset, uint64_t size) const override { return DiskHandle::mmap(offset, size); } Array mmapPrivate(uint64_t offset, uint64_t size) const override { return DiskHandle::mmapPrivate(offset, size); } }; class DiskAppendableFile final: public AppendableFile, public DiskHandle { public: DiskAppendableFile(AutoCloseHandle&& handle) : DiskHandle(kj::mv(handle), nullptr), stream(DiskHandle::handle.get()) {} Own cloneFsNode() const override { return heap(DiskHandle::clone()); } FSNODE_METHODS void write(const void* buffer, size_t size) override { stream.write(buffer, size); } void write(ArrayPtr> pieces) override { implicitCast(stream).write(pieces); } private: HandleOutputStream stream; }; class DiskFile final: public File, public DiskHandle { public: DiskFile(AutoCloseHandle&& handle): DiskHandle(kj::mv(handle), nullptr) {} Own cloneFsNode() const override { return heap(DiskHandle::clone()); } FSNODE_METHODS size_t read(uint64_t offset, ArrayPtr buffer) const override { return DiskHandle::read(offset, buffer); } Array mmap(uint64_t offset, uint64_t size) const override { return DiskHandle::mmap(offset, size); } Array mmapPrivate(uint64_t offset, uint64_t size) const override { return DiskHandle::mmapPrivate(offset, size); } void write(uint64_t offset, ArrayPtr data) const override { DiskHandle::write(offset, data); } void zero(uint64_t offset, uint64_t size) const override { DiskHandle::zero(offset, size); } void truncate(uint64_t size) const override { DiskHandle::truncate(size); } Own mmapWritable(uint64_t offset, uint64_t size) const override { return DiskHandle::mmapWritable(offset, size); } // copy() is not optimized on Windows. }; class DiskReadableDirectory final: public ReadableDirectory, public DiskHandle { public: DiskReadableDirectory(AutoCloseHandle&& handle, Path&& path) : DiskHandle(kj::mv(handle), kj::mv(path)) {} Own cloneFsNode() const override { return heap(DiskHandle::clone(), KJ_ASSERT_NONNULL(dirPath).clone()); } FSNODE_METHODS Array listNames() const override { return DiskHandle::listNames(); } Array listEntries() const override { return DiskHandle::listEntries(); } bool exists(PathPtr path) const override { return DiskHandle::exists(path); } Maybe tryLstat(PathPtr path) const override { return DiskHandle::tryLstat(path); } Maybe> tryOpenFile(PathPtr path) const override { return DiskHandle::tryOpenFile(path); } Maybe> tryOpenSubdir(PathPtr path) const override { return DiskHandle::tryOpenSubdir(path); } Maybe tryReadlink(PathPtr path) const override { return DiskHandle::tryReadlink(path); } }; class DiskDirectoryBase: public Directory, public DiskHandle { public: DiskDirectoryBase(AutoCloseHandle&& handle, Path&& path) : DiskHandle(kj::mv(handle), kj::mv(path)) {} bool exists(PathPtr path) const override { return DiskHandle::exists(path); } Maybe tryLstat(PathPtr path) const override { return DiskHandle::tryLstat(path); } Maybe> tryOpenFile(PathPtr path) const override { return DiskHandle::tryOpenFile(path); } Maybe> tryOpenSubdir(PathPtr path) const override { return DiskHandle::tryOpenSubdir(path); } Maybe tryReadlink(PathPtr path) const override { return DiskHandle::tryReadlink(path); } Maybe> tryOpenFile(PathPtr path, WriteMode mode) const override { return DiskHandle::tryOpenFile(path, mode); } Own> replaceFile(PathPtr path, WriteMode mode) const override { return DiskHandle::replaceFile(path, mode); } Maybe> tryAppendFile(PathPtr path, WriteMode mode) const override { return DiskHandle::tryAppendFile(path, mode); } Maybe> tryOpenSubdir(PathPtr path, WriteMode mode) const override { return DiskHandle::tryOpenSubdir(path, mode); } Own> replaceSubdir(PathPtr path, WriteMode mode) const override { return DiskHandle::replaceSubdir(path, mode); } bool trySymlink(PathPtr linkpath, StringPtr content, WriteMode mode) const override { return DiskHandle::trySymlink(linkpath, content, mode); } bool tryTransfer(PathPtr toPath, WriteMode toMode, const Directory& fromDirectory, PathPtr fromPath, TransferMode mode) const override { return DiskHandle::tryTransfer(toPath, toMode, fromDirectory, fromPath, mode, *this); } // tryTransferTo() not implemented because we have nothing special we can do. bool tryRemove(PathPtr path) const override { return DiskHandle::tryRemove(path); } }; class DiskDirectory final: public DiskDirectoryBase { public: DiskDirectory(AutoCloseHandle&& handle, Path&& path) : DiskDirectoryBase(kj::mv(handle), kj::mv(path)) {} Own cloneFsNode() const override { return heap(DiskHandle::clone(), KJ_ASSERT_NONNULL(dirPath).clone()); } FSNODE_METHODS Array listNames() const override { return DiskHandle::listNames(); } Array listEntries() const override { return DiskHandle::listEntries(); } Own createTemporary() const override { return DiskHandle::createTemporary(); } }; class RootDiskDirectory final: public DiskDirectoryBase { // On Windows, the root directory is special. // // HACK: We only override a few functions of DiskDirectory, and we rely on the fact that // Path::forWin32Api(true) throws an exception complaining about missing drive letter if the // path is totally empty. public: RootDiskDirectory(): DiskDirectoryBase(nullptr, Path(nullptr)) {} Own cloneFsNode() const override { return heap(); } Metadata stat() const override { return { Type::DIRECTORY, 0, 0, UNIX_EPOCH, 1, 0 }; } void sync() const override {} void datasync() const override {} Array listNames() const override { return KJ_MAP(e, listEntries()) { return kj::mv(e.name); }; } Array listEntries() const override { DWORD drives = GetLogicalDrives(); if (drives == 0) { KJ_FAIL_WIN32("GetLogicalDrives()", GetLastError()) { return nullptr; } } Vector results; for (uint i = 0; i < 26; i++) { if (drives & (1 << i)) { char name[2] = { static_cast('A' + i), ':' }; results.add(Entry { FsNode::Type::DIRECTORY, kj::heapString(name, 2) }); } } return results.releaseAsArray(); } Own createTemporary() const override { KJ_FAIL_REQUIRE("can't create temporaries in Windows pseudo-root directory (the drive list)"); } }; class DiskFilesystem final: public Filesystem { public: DiskFilesystem() : DiskFilesystem(computeCurrentPath()) {} DiskFilesystem(Path currentPath) : current(KJ_ASSERT_NONNULL(root.tryOpenSubdirInternal(currentPath), "path returned by GetCurrentDirectory() doesn't exist?"), kj::mv(currentPath)) {} const Directory& getRoot() const override { return root; } const Directory& getCurrent() const override { return current; } PathPtr getCurrentPath() const override { return KJ_ASSERT_NONNULL(current.dirPath); } private: RootDiskDirectory root; DiskDirectory current; static Path computeCurrentPath() { DWORD tryLen = MAX_PATH; for (;;) { auto temp = kj::heapArray(tryLen + 1); DWORD len = GetCurrentDirectoryW(temp.size(), temp.begin()); if (len == 0) { KJ_FAIL_WIN32("GetCurrentDirectory", GetLastError()) { break; } return Path("."); } if (len < temp.size()) { return Path::parseWin32Api(temp.slice(0, len)); } // Try again with new length. tryLen = len; } } }; } // namespace Own newDiskReadableFile(AutoCloseHandle fd) { return heap(kj::mv(fd)); } Own newDiskAppendableFile(AutoCloseHandle fd) { return heap(kj::mv(fd)); } Own newDiskFile(AutoCloseHandle fd) { return heap(kj::mv(fd)); } Own newDiskReadableDirectory(AutoCloseHandle fd) { return heap(kj::mv(fd), getPathFromHandle(fd)); } static Own newDiskReadableDirectory(AutoCloseHandle fd, Path&& path) { return heap(kj::mv(fd), kj::mv(path)); } Own newDiskDirectory(AutoCloseHandle fd) { return heap(kj::mv(fd), getPathFromHandle(fd)); } static Own newDiskDirectory(AutoCloseHandle fd, Path&& path) { return heap(kj::mv(fd), kj::mv(path)); } Own newDiskFilesystem() { return heap(); } static AutoCloseHandle* getHandlePointerHack(Directory& dir) { return &static_cast(dir).handle; } static Path* getPathPointerHack(Directory& dir) { return &KJ_ASSERT_NONNULL(static_cast(dir).dirPath); } } // namespace kj #endif // _WIN32 capnproto-c++-0.8.0/src/kj/test-helpers.c++0000644000175000017500000001042013340402540021044 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #include "test.h" #ifndef _WIN32 #include #include #include #else #include #endif namespace kj { namespace _ { // private bool hasSubstring(StringPtr haystack, StringPtr needle) { // TODO(perf): This is not the best algorithm for substring matching. if (needle.size() <= haystack.size()) { for (size_t i = 0; i <= haystack.size() - needle.size(); i++) { if (haystack.slice(i).startsWith(needle)) { return true; } } } return false; } LogExpectation::LogExpectation(LogSeverity severity, StringPtr substring) : severity(severity), substring(substring), seen(false) {} LogExpectation::~LogExpectation() { if (!unwindDetector.isUnwinding()) { KJ_ASSERT(seen, "expected log message not seen", severity, substring); } } void LogExpectation::logMessage( LogSeverity severity, const char* file, int line, int contextDepth, String&& text) { if (!seen && severity == this->severity) { if (hasSubstring(text, substring)) { // Match. Ignore it. seen = true; return; } } // Pass up the chain. ExceptionCallback::logMessage(severity, file, line, contextDepth, kj::mv(text)); } // ======================================================================================= namespace { class FatalThrowExpectation: public ExceptionCallback { public: FatalThrowExpectation(Maybe type, Maybe message) : type(type), message(message) {} virtual void onFatalException(Exception&& exception) { KJ_IF_MAYBE(expectedType, type) { if (exception.getType() != *expectedType) { KJ_LOG(ERROR, "threw exception of wrong type", exception, *expectedType); _exit(1); } } KJ_IF_MAYBE(expectedSubstring, message) { if (!hasSubstring(exception.getDescription(), *expectedSubstring)) { KJ_LOG(ERROR, "threw exception with wrong message", exception, *expectedSubstring); _exit(1); } } _exit(0); } private: Maybe type; Maybe message; }; } // namespace bool expectFatalThrow(kj::Maybe type, kj::Maybe message, Function code) { #if _WIN32 // We don't support death tests on Windows due to lack of efficient fork. return true; #else pid_t child; KJ_SYSCALL(child = fork()); if (child == 0) { KJ_DEFER(_exit(1)); FatalThrowExpectation expectation(type, message); KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { code(); })) { KJ_LOG(ERROR, "a non-fatal exception was thrown, but we expected fatal", *e); } else { KJ_LOG(ERROR, "no fatal exception was thrown"); } } int status; KJ_SYSCALL(waitpid(child, &status, 0)); if (WIFEXITED(status)) { return WEXITSTATUS(status) == 0; } else if (WIFSIGNALED(status)) { KJ_FAIL_EXPECT("subprocess crashed without throwing exception", WTERMSIG(status)); return false; } else { KJ_FAIL_EXPECT("subprocess neiter excited nor crashed?", status); return false; } #endif } } // namespace _ (private) } // namespace kj capnproto-c++-0.8.0/src/kj/main.c++0000644000175000017500000006264513340402540017371 0ustar00kentonkenton00000000000000// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors // Licensed under the MIT License: // // 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: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // 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. #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "main.h" #include "debug.h" #include "arena.h" #include "miniposix.h" #include #include #include #include #include #if _WIN32 #define WIN32_LEAN_AND_MEAN #ifndef NOMINMAX #define NOMINMAX 1 #endif #include #include "windows-sanity.h" #else #include #endif namespace kj { // ======================================================================================= TopLevelProcessContext::TopLevelProcessContext(StringPtr programName) : programName(programName), cleanShutdown(getenv("KJ_CLEAN_SHUTDOWN") != nullptr) { printStackTraceOnCrash(); } StringPtr TopLevelProcessContext::getProgramName() { return programName; } void TopLevelProcessContext::exit() { int exitCode = hadErrors ? 1 : 0; if (cleanShutdown) { #if KJ_NO_EXCEPTIONS // This is the best we can do. warning("warning: KJ_CLEAN_SHUTDOWN may not work correctly when compiled " "with -fno-exceptions."); ::exit(exitCode); #else throw CleanShutdownException { exitCode }; #endif } _exit(exitCode); } #if _WIN32 void setStandardIoMode(int fd) { // Set mode to binary if the fd is not a console. HANDLE handle = reinterpret_cast(_get_osfhandle(fd)); DWORD consoleMode; if (GetConsoleMode(handle, &consoleMode)) { // It's a console. } else { KJ_SYSCALL(_setmode(fd, _O_BINARY)); } } #else void setStandardIoMode(int fd) {} #endif static void writeLineToFd(int fd, StringPtr message) { // Write the given message to the given file descriptor with a trailing newline iff the message // is non-empty and doesn't already have a trailing newline. We use writev() to do this in a // single system call without any copying (OS permitting). if (message.size() == 0) { return; } #if _WIN32 KJ_STACK_ARRAY(char, newlineExpansionBuffer, 2 * (message.size() + 1), 128, 512); char* p = newlineExpansionBuffer.begin(); for(char ch : message) { if(ch == '\n') { *(p++) = '\r'; } *(p++) = ch; } if(!message.endsWith("\n")) { *(p++) = '\r'; *(p++) = '\n'; } size_t newlineExpandedSize = p - newlineExpansionBuffer.begin(); KJ_ASSERT(newlineExpandedSize <= newlineExpansionBuffer.size()); HANDLE handle = reinterpret_cast(_get_osfhandle(fd)); DWORD consoleMode; bool redirectedToFile = !GetConsoleMode(handle, &consoleMode); DWORD writtenSize; if(redirectedToFile) { WriteFile(handle, newlineExpansionBuffer.begin(), newlineExpandedSize, &writtenSize, nullptr); } else { KJ_STACK_ARRAY(wchar_t, buffer, newlineExpandedSize, 128, 512); size_t finalSize = MultiByteToWideChar( CP_UTF8, 0, newlineExpansionBuffer.begin(), newlineExpandedSize, buffer.begin(), buffer.size()); KJ_ASSERT(finalSize <= buffer.size()); WriteConsoleW(handle, buffer.begin(), finalSize, &writtenSize, nullptr); } #else // Unfortunately the writev interface requires non-const pointers even though it won't modify // the data. struct iovec vec[2]; vec[0].iov_base = const_cast(message.begin()); vec[0].iov_len = message.size(); vec[1].iov_base = const_cast("\n"); vec[1].iov_len = 1; struct iovec* pos = vec; // Only use the second item in the vec if the message doesn't already end with \n. uint count = message.endsWith("\n") ? 1 : 2; for (;;) { ssize_t n = writev(fd, pos, count); if (n < 0) { if (errno == EINTR) { continue; } else { // This function is meant for writing to stdout and stderr. If writes fail on those FDs // there's not a whole lot we can reasonably do, so just ignore it. return; } } // Update chunks to discard what was successfully written. for (;;) { if (count == 0) { // Done writing. return; } else if (pos->iov_len <= implicitCast(n)) { // Wrote this entire chunk. n -= pos->iov_len; ++pos; --count; } else { // Wrote only part of this chunk. Adjust the pointer and then retry. pos->iov_base = reinterpret_cast(pos->iov_base) + n; pos->iov_len -= n; break; } } } #endif } void TopLevelProcessContext::warning(StringPtr message) { writeLineToFd(STDERR_FILENO, message); } void TopLevelProcessContext::error(StringPtr message) { hadErrors = true; writeLineToFd(STDERR_FILENO, message); } void TopLevelProcessContext::exitError(StringPtr message) { error(message); exit(); } void TopLevelProcessContext::exitInfo(StringPtr message) { writeLineToFd(STDOUT_FILENO, message); exit(); } void TopLevelProcessContext::increaseLoggingVerbosity() { // At the moment, there is only one log level that isn't enabled by default. _::Debug::setLogLevel(_::Debug::Severity::INFO); } // ======================================================================================= int runMainAndExit(ProcessContext& context, MainFunc&& func, int argc, char* argv[]) { setStandardIoMode(STDIN_FILENO); setStandardIoMode(STDOUT_FILENO); setStandardIoMode(STDERR_FILENO); #if !KJ_NO_EXCEPTIONS try { #endif KJ_ASSERT(argc > 0); KJ_STACK_ARRAY(StringPtr, params, argc - 1, 8, 32); for (int i = 1; i < argc; i++) { params[i - 1] = argv[i]; } KJ_IF_MAYBE(exception, runCatchingExceptions([&]() { func(argv[0], params); })) { context.error(str("*** Uncaught exception ***\n", *exception)); } context.exit(); #if !KJ_NO_EXCEPTIONS } catch (const TopLevelProcessContext::CleanShutdownException& e) { return e.exitCode; } #endif KJ_CLANG_KNOWS_THIS_IS_UNREACHABLE_BUT_GCC_DOESNT } // ======================================================================================= struct MainBuilder::Impl { inline Impl(ProcessContext& context, StringPtr version, StringPtr briefDescription, StringPtr extendedDescription) : context(context), version(version), briefDescription(briefDescription), extendedDescription(extendedDescription) {} ProcessContext& context; StringPtr version; StringPtr briefDescription; StringPtr extendedDescription; Arena arena; struct CharArrayCompare { inline bool operator()(const ArrayPtr& a, const ArrayPtr& b) const { int cmp = memcmp(a.begin(), b.begin(), min(a.size(), b.size())); if (cmp == 0) { return a.size() < b.size(); } else { return cmp < 0; } } }; struct Option { ArrayPtr names; bool hasArg; union { Function* func; Function* funcWithArg; }; StringPtr argTitle; StringPtr helpText; }; class OptionDisplayOrder; std::map shortOptions; std::map, Option*, CharArrayCompare> longOptions; struct SubCommand { Function func; StringPtr helpText; }; std::map subCommands; struct Arg { StringPtr title; Function callback; uint minCount; uint maxCount; }; Vector args; Maybe> finalCallback; Option& addOption(std::initializer_list names, bool hasArg, StringPtr helpText) { KJ_REQUIRE(names.size() > 0, "option must have at least one name"); Option& option = arena.allocate