yql_expr.h 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  1. #pragma once
  2. #include "yql_ast.h"
  3. #include "yql_expr_types.h"
  4. #include "yql_type_string.h"
  5. #include "yql_expr_builder.h"
  6. #include "yql_gc_nodes.h"
  7. #include "yql_constraint.h"
  8. #include "yql_pos_handle.h"
  9. #include <yql/essentials/core/url_lister/interface/url_lister_manager.h>
  10. #include <yql/essentials/utils/yql_panic.h>
  11. #include <yql/essentials/public/issue/yql_issue_manager.h>
  12. #include <yql/essentials/public/udf/udf_data_type.h>
  13. #include <library/cpp/yson/node/node.h>
  14. #include <library/cpp/yson/writer.h>
  15. #include <library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h>
  16. #include <library/cpp/enumbitset/enumbitset.h>
  17. #include <library/cpp/containers/stack_vector/stack_vec.h>
  18. #include <library/cpp/deprecated/enum_codegen/enum_codegen.h>
  19. #include <util/string/ascii.h>
  20. #include <util/string/builder.h>
  21. #include <util/generic/array_ref.h>
  22. #include <util/generic/deque.h>
  23. #include <util/generic/cast.h>
  24. #include <util/generic/hash.h>
  25. #include <util/generic/maybe.h>
  26. #include <util/generic/set.h>
  27. #include <util/generic/bt_exception.h>
  28. #include <util/generic/algorithm.h>
  29. #include <util/digest/murmur.h>
  30. #include <algorithm>
  31. #include <unordered_set>
  32. #include <unordered_map>
  33. #include <span>
  34. #include <stack>
  35. //#define YQL_CHECK_NODES_CONSISTENCY
  36. #ifdef YQL_CHECK_NODES_CONSISTENCY
  37. #define ENSURE_NOT_DELETED \
  38. YQL_ENSURE(!Dead(), "Access to dead node # " << UniqueId_ << ": " << Type_ << " '" << ContentUnchecked() << "'");
  39. #define ENSURE_NOT_FROZEN \
  40. YQL_ENSURE(!Frozen(), "Change in frozen node # " << UniqueId_ << ": " << Type_ << " '" << ContentUnchecked() << "'");
  41. #define ENSURE_NOT_FROZEN_CTX \
  42. YQL_ENSURE(!Frozen, "Change in frozen expr context.");
  43. #else
  44. #define ENSURE_NOT_DELETED Y_DEBUG_ABORT_UNLESS(!Dead(), "Access to dead node # %lu: %d '%s'", UniqueId_, (int)Type_, TString(ContentUnchecked()).data());
  45. #define ENSURE_NOT_FROZEN Y_DEBUG_ABORT_UNLESS(!Frozen());
  46. #define ENSURE_NOT_FROZEN_CTX Y_DEBUG_ABORT_UNLESS(!Frozen);
  47. #endif
  48. namespace NYql {
  49. using NUdf::EDataSlot;
  50. class TUnitExprType;
  51. class TMultiExprType;
  52. class TTupleExprType;
  53. class TStructExprType;
  54. class TItemExprType;
  55. class TListExprType;
  56. class TStreamExprType;
  57. class TDataExprType;
  58. class TPgExprType;
  59. class TWorldExprType;
  60. class TOptionalExprType;
  61. class TCallableExprType;
  62. class TResourceExprType;
  63. class TTypeExprType;
  64. class TDictExprType;
  65. class TVoidExprType;
  66. class TNullExprType;
  67. class TGenericExprType;
  68. class TTaggedExprType;
  69. class TErrorExprType;
  70. class TVariantExprType;
  71. class TStreamExprType;
  72. class TFlowExprType;
  73. class TEmptyListExprType;
  74. class TEmptyDictExprType;
  75. class TBlockExprType;
  76. class TScalarExprType;
  77. const size_t DefaultMistypeDistance = 3;
  78. const TString YqlVirtualPrefix = "_yql_virtual_";
  79. extern const TStringBuf ZeroString;
  80. struct TTypeAnnotationVisitor {
  81. virtual ~TTypeAnnotationVisitor() = default;
  82. virtual void Visit(const TUnitExprType& type) = 0;
  83. virtual void Visit(const TMultiExprType& type) = 0;
  84. virtual void Visit(const TTupleExprType& type) = 0;
  85. virtual void Visit(const TStructExprType& type) = 0;
  86. virtual void Visit(const TItemExprType& type) = 0;
  87. virtual void Visit(const TListExprType& type) = 0;
  88. virtual void Visit(const TStreamExprType& type) = 0;
  89. virtual void Visit(const TFlowExprType& type) = 0;
  90. virtual void Visit(const TDataExprType& type) = 0;
  91. virtual void Visit(const TPgExprType& type) = 0;
  92. virtual void Visit(const TWorldExprType& type) = 0;
  93. virtual void Visit(const TOptionalExprType& type) = 0;
  94. virtual void Visit(const TCallableExprType& type) = 0;
  95. virtual void Visit(const TResourceExprType& type) = 0;
  96. virtual void Visit(const TTypeExprType& type) = 0;
  97. virtual void Visit(const TDictExprType& type) = 0;
  98. virtual void Visit(const TVoidExprType& type) = 0;
  99. virtual void Visit(const TNullExprType& type) = 0;
  100. virtual void Visit(const TGenericExprType& type) = 0;
  101. virtual void Visit(const TTaggedExprType& type) = 0;
  102. virtual void Visit(const TErrorExprType& type) = 0;
  103. virtual void Visit(const TVariantExprType& type) = 0;
  104. virtual void Visit(const TEmptyListExprType& type) = 0;
  105. virtual void Visit(const TEmptyDictExprType& type) = 0;
  106. virtual void Visit(const TBlockExprType& type) = 0;
  107. virtual void Visit(const TScalarExprType& type) = 0;
  108. };
  109. struct TDefaultTypeAnnotationVisitor : public TTypeAnnotationVisitor {
  110. void Visit(const TUnitExprType& type) override;
  111. void Visit(const TMultiExprType& type) override;
  112. void Visit(const TTupleExprType& type) override;
  113. void Visit(const TStructExprType& type) override;
  114. void Visit(const TItemExprType& type) override;
  115. void Visit(const TListExprType& type) override;
  116. void Visit(const TStreamExprType& type) override;
  117. void Visit(const TFlowExprType& type) override;
  118. void Visit(const TDataExprType& type) override;
  119. void Visit(const TPgExprType& type) override;
  120. void Visit(const TWorldExprType& type) override;
  121. void Visit(const TOptionalExprType& type) override;
  122. void Visit(const TCallableExprType& type) override;
  123. void Visit(const TResourceExprType& type) override;
  124. void Visit(const TTypeExprType& type) override;
  125. void Visit(const TDictExprType& type) override;
  126. void Visit(const TVoidExprType& type) override;
  127. void Visit(const TNullExprType& type) override;
  128. void Visit(const TGenericExprType& type) override;
  129. void Visit(const TTaggedExprType& type) override;
  130. void Visit(const TErrorExprType& type) override;
  131. void Visit(const TVariantExprType& type) override;
  132. void Visit(const TEmptyListExprType& type) override;
  133. void Visit(const TEmptyDictExprType& type) override;
  134. void Visit(const TBlockExprType& type) override;
  135. void Visit(const TScalarExprType& type) override;
  136. };
  137. class TErrorTypeVisitor : public TDefaultTypeAnnotationVisitor
  138. {
  139. public:
  140. TErrorTypeVisitor(TExprContext& ctx);
  141. void Visit(const TErrorExprType& type) override;
  142. bool HasErrors() const;
  143. private:
  144. TExprContext& Ctx_;
  145. bool HasErrors_ = false;
  146. };
  147. enum ETypeAnnotationFlags : ui32 {
  148. TypeNonComposable = 0x01,
  149. TypeNonPersistable = 0x02,
  150. TypeNonComputable = 0x04,
  151. TypeNonInspectable = 0x08,
  152. TypeNonHashable = 0x10,
  153. TypeNonEquatable = 0x20,
  154. TypeNonComparable = 0x40,
  155. TypeHasNull = 0x80,
  156. TypeHasOptional = 0x100,
  157. TypeHasManyValues = 0x200,
  158. TypeHasBareYson = 0x400,
  159. TypeHasNestedOptional = 0x800,
  160. TypeNonPresortable = 0x1000,
  161. TypeHasDynamicSize = 0x2000,
  162. TypeNonComparableInternal = 0x4000,
  163. TypeHasError = 0x8000,
  164. };
  165. const ui64 TypeHashMagic = 0x10000;
  166. inline ui64 StreamHash(const void* buffer, size_t size, ui64 seed) {
  167. return MurmurHash(buffer, size, seed);
  168. }
  169. inline ui64 StreamHash(ui64 value, ui64 seed) {
  170. return MurmurHash(&value, sizeof(value), seed);
  171. }
  172. void ReportError(TExprContext& ctx, const TIssue& issue);
  173. class TTypeAnnotationNode {
  174. protected:
  175. TTypeAnnotationNode(ETypeAnnotationKind kind, ui32 flags, ui64 hash, ui64 usedPgExtensions)
  176. : Kind(kind)
  177. , Flags(flags)
  178. , Hash(hash)
  179. , UsedPgExtensions(usedPgExtensions)
  180. {
  181. }
  182. public:
  183. virtual ~TTypeAnnotationNode() = default;
  184. template <typename T>
  185. const T* Cast() const {
  186. static_assert(std::is_base_of<TTypeAnnotationNode, T>::value,
  187. "Should be derived from TTypeAnnotationNode");
  188. const auto ret = dynamic_cast<const T*>(this);
  189. YQL_ENSURE(ret, "Cannot cast type " << *this << " to " << ETypeAnnotationKind(T::KindValue));
  190. return ret;
  191. }
  192. template <typename T>
  193. const T* UserCast(TPosition pos, TExprContext& ctx) const {
  194. static_assert(std::is_base_of<TTypeAnnotationNode, T>::value,
  195. "Should be derived from TTypeAnnotationNode");
  196. const auto ret = dynamic_cast<const T*>(this);
  197. if (!ret) {
  198. ReportError(ctx, TIssue(pos, TStringBuilder() << "Cannot cast type " << *this << " to " << ETypeAnnotationKind(T::KindValue)));
  199. }
  200. return ret;
  201. }
  202. ETypeAnnotationKind GetKind() const {
  203. return Kind;
  204. }
  205. bool ReturnsWorld() const;
  206. bool IsComposable() const {
  207. return (GetFlags() & TypeNonComposable) == 0;
  208. }
  209. bool IsPersistable() const {
  210. return (GetFlags() & TypeNonPersistable) == 0;
  211. }
  212. bool IsComputable() const {
  213. return (GetFlags() & TypeNonComputable) == 0;
  214. }
  215. bool IsInspectable() const {
  216. return (GetFlags() & TypeNonInspectable) == 0;
  217. }
  218. bool IsHashable() const {
  219. return IsPersistable() && (GetFlags() & TypeNonHashable) == 0;
  220. }
  221. bool IsEquatable() const {
  222. return IsPersistable() && (GetFlags() & TypeNonEquatable) == 0;
  223. }
  224. bool IsComparable() const {
  225. return IsPersistable() && (GetFlags() & TypeNonComparable) == 0;
  226. }
  227. bool IsComparableInternal() const {
  228. return IsPersistable() && (GetFlags() & TypeNonComparableInternal) == 0;
  229. }
  230. bool HasNull() const {
  231. return (GetFlags() & TypeHasNull) != 0;
  232. }
  233. bool HasOptional() const {
  234. return (GetFlags() & TypeHasOptional) != 0;
  235. }
  236. bool HasNestedOptional() const {
  237. return (GetFlags() & TypeHasNestedOptional) != 0;
  238. }
  239. bool HasOptionalOrNull() const {
  240. return (GetFlags() & (TypeHasOptional | TypeHasNull)) != 0;
  241. }
  242. bool IsOptionalOrNull() const {
  243. auto kind = GetKind();
  244. return kind == ETypeAnnotationKind::Optional || kind == ETypeAnnotationKind::Null || kind == ETypeAnnotationKind::Pg;
  245. }
  246. bool IsBlockOrScalar() const {
  247. return IsBlock() || IsScalar();
  248. }
  249. bool IsBlock() const {
  250. return GetKind() == ETypeAnnotationKind::Block;
  251. }
  252. bool IsScalar() const {
  253. return GetKind() == ETypeAnnotationKind::Scalar;
  254. }
  255. bool HasFixedSizeRepr() const {
  256. return (GetFlags() & (TypeHasDynamicSize | TypeNonPersistable | TypeNonComputable)) == 0;
  257. }
  258. bool IsSingleton() const {
  259. return (GetFlags() & TypeHasManyValues) == 0;
  260. }
  261. bool HasBareYson() const {
  262. return (GetFlags() & TypeHasBareYson) != 0;
  263. }
  264. bool IsPresortSupported() const {
  265. return (GetFlags() & TypeNonPresortable) == 0;
  266. }
  267. bool HasErrors() const {
  268. return (GetFlags() & TypeHasError) != 0;
  269. }
  270. ui32 GetFlags() const {
  271. return Flags;
  272. }
  273. ui64 GetHash() const {
  274. return Hash;
  275. }
  276. ui64 GetUsedPgExtensions() const {
  277. return UsedPgExtensions;
  278. }
  279. bool Equals(const TTypeAnnotationNode& node) const;
  280. void Accept(TTypeAnnotationVisitor& visitor) const;
  281. void Out(IOutputStream& out) const {
  282. out << FormatType(this);
  283. }
  284. struct THash {
  285. size_t operator()(const TTypeAnnotationNode* node) const {
  286. return node->GetHash();
  287. }
  288. };
  289. struct TEqual {
  290. bool operator()(const TTypeAnnotationNode* one, const TTypeAnnotationNode* two) const {
  291. return one->Equals(*two);
  292. }
  293. };
  294. typedef std::vector<const TTypeAnnotationNode*> TListType;
  295. typedef std::span<const TTypeAnnotationNode*> TSpanType;
  296. protected:
  297. template <typename T>
  298. static ui32 CombineFlags(const T& items) {
  299. ui32 flags = 0;
  300. for (auto& item : items) {
  301. flags |= item->GetFlags();
  302. }
  303. return flags;
  304. }
  305. template <typename T>
  306. static ui64 CombinePgExtensions(const T& items) {
  307. ui64 mask = 0;
  308. for (auto& item : items) {
  309. mask |= item->GetUsedPgExtensions();
  310. }
  311. return mask;
  312. }
  313. private:
  314. const ETypeAnnotationKind Kind;
  315. const ui32 Flags;
  316. const ui64 Hash;
  317. const ui64 UsedPgExtensions;
  318. };
  319. class TUnitExprType : public TTypeAnnotationNode {
  320. public:
  321. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Unit;
  322. TUnitExprType(ui64 hash)
  323. : TTypeAnnotationNode(KindValue,
  324. TypeNonComputable | TypeNonPersistable, hash, 0)
  325. {
  326. }
  327. static ui64 MakeHash() {
  328. return TypeHashMagic | (ui64)ETypeAnnotationKind::Unit;
  329. }
  330. bool operator==(const TUnitExprType& other) const {
  331. Y_UNUSED(other);
  332. return true;
  333. }
  334. };
  335. class TTupleExprType : public TTypeAnnotationNode {
  336. public:
  337. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Tuple;
  338. TTupleExprType(ui64 hash, const TTypeAnnotationNode::TListType& items)
  339. : TTypeAnnotationNode(KindValue, CombineFlags(items), hash, CombinePgExtensions(items))
  340. , Items(items)
  341. {
  342. }
  343. static ui64 MakeHash(const TTypeAnnotationNode::TListType& items) {
  344. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Tuple;
  345. hash = StreamHash(items.size(), hash);
  346. for (const auto& item : items) {
  347. hash = StreamHash(item->GetHash(), hash);
  348. }
  349. return hash;
  350. }
  351. size_t GetSize() const {
  352. return Items.size();
  353. }
  354. const TTypeAnnotationNode::TListType& GetItems() const {
  355. return Items;
  356. }
  357. bool operator==(const TTupleExprType& other) const {
  358. if (GetSize() != other.GetSize()) {
  359. return false;
  360. }
  361. for (ui32 i = 0, e = GetSize(); i < e; ++i) {
  362. if (GetItems()[i] != other.GetItems()[i]) {
  363. return false;
  364. }
  365. }
  366. return true;
  367. }
  368. bool Validate(TPosition position, TExprContext& ctx) const;
  369. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  370. private:
  371. TTypeAnnotationNode::TListType Items;
  372. };
  373. class TMultiExprType : public TTypeAnnotationNode {
  374. public:
  375. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Multi;
  376. TMultiExprType(ui64 hash, const TTypeAnnotationNode::TListType& items)
  377. : TTypeAnnotationNode(KindValue, CombineFlags(items), hash, CombinePgExtensions(items))
  378. , Items(items)
  379. {
  380. }
  381. static ui64 MakeHash(const TTypeAnnotationNode::TListType& items) {
  382. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Multi;
  383. hash = StreamHash(items.size(), hash);
  384. for (const auto& item : items) {
  385. hash = StreamHash(item->GetHash(), hash);
  386. }
  387. return hash;
  388. }
  389. size_t GetSize() const {
  390. return Items.size();
  391. }
  392. const TTypeAnnotationNode::TListType& GetItems() const {
  393. return Items;
  394. }
  395. bool operator==(const TMultiExprType& other) const {
  396. if (GetSize() != other.GetSize()) {
  397. return false;
  398. }
  399. for (ui32 i = 0, e = GetSize(); i < e; ++i) {
  400. if (GetItems()[i] != other.GetItems()[i]) {
  401. return false;
  402. }
  403. }
  404. return true;
  405. }
  406. bool Validate(TPosition position, TExprContext& ctx) const;
  407. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  408. private:
  409. TTypeAnnotationNode::TListType Items;
  410. };
  411. struct TExprContext;
  412. bool ValidateName(TPosition position, TStringBuf name, TStringBuf descr, TExprContext& ctx);
  413. bool ValidateName(TPositionHandle position, TStringBuf name, TStringBuf descr, TExprContext& ctx);
  414. class TItemExprType : public TTypeAnnotationNode {
  415. public:
  416. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Item;
  417. TItemExprType(ui64 hash, const TStringBuf& name, const TTypeAnnotationNode* itemType)
  418. : TTypeAnnotationNode(KindValue, itemType->GetFlags(), hash, itemType->GetUsedPgExtensions())
  419. , Name(name)
  420. , ItemType(itemType)
  421. {
  422. }
  423. static ui64 MakeHash(const TStringBuf& name, const TTypeAnnotationNode* itemType) {
  424. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Item;
  425. hash = StreamHash(name.size(), hash);
  426. hash = StreamHash(name.data(), name.size(), hash);
  427. return StreamHash(itemType->GetHash(), hash);
  428. }
  429. bool Validate(TPosition position, TExprContext& ctx) const;
  430. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  431. const TStringBuf& GetName() const {
  432. return Name;
  433. }
  434. TStringBuf GetCleanName(bool isVirtual) const;
  435. const TTypeAnnotationNode* GetItemType() const {
  436. return ItemType;
  437. }
  438. bool operator==(const TItemExprType& other) const {
  439. return GetName() == other.GetName() && GetItemType() == other.GetItemType();
  440. }
  441. const TItemExprType* GetCleanItem(bool isVirtual, TExprContext& ctx) const;
  442. private:
  443. const TStringBuf Name;
  444. const TTypeAnnotationNode* ItemType;
  445. };
  446. class TStructExprType : public TTypeAnnotationNode {
  447. public:
  448. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Struct;
  449. struct TItemLess {
  450. bool operator()(const TItemExprType* x, const TItemExprType* y) const {
  451. return x->GetName() < y->GetName();
  452. };
  453. bool operator()(const TItemExprType* x, const TStringBuf& y) const {
  454. return x->GetName() < y;
  455. };
  456. bool operator()(const TStringBuf& x, const TItemExprType* y) const {
  457. return x < y->GetName();
  458. };
  459. };
  460. TStructExprType(ui64 hash, const TVector<const TItemExprType*>& items)
  461. : TTypeAnnotationNode(KindValue, TypeNonComparable | CombineFlags(items), hash, CombinePgExtensions(items))
  462. , Items(items)
  463. {
  464. }
  465. static ui64 MakeHash(const TVector<const TItemExprType*>& items) {
  466. Y_DEBUG_ABORT_UNLESS(IsSorted(items.begin(), items.end(), TItemLess()));
  467. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Struct;
  468. hash = StreamHash(items.size(), hash);
  469. for (const auto& item : items) {
  470. hash = StreamHash(item->GetHash(), hash);
  471. }
  472. return hash;
  473. }
  474. bool Validate(TPosition position, TExprContext& ctx) const;
  475. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  476. size_t GetSize() const {
  477. return Items.size();
  478. }
  479. const TVector<const TItemExprType*>& GetItems() const {
  480. return Items;
  481. }
  482. TMaybe<ui32> FindItem(const TStringBuf& name) const {
  483. auto it = LowerBound(Items.begin(), Items.end(), name, TItemLess());
  484. if (it == Items.end() || (*it)->GetName() != name) {
  485. return TMaybe<ui32>();
  486. }
  487. return it - Items.begin();
  488. }
  489. TMaybe<ui32> FindItemI(const TStringBuf& name, bool* isVirtual) const {
  490. for (ui32 v = 0; v < 2; ++v) {
  491. if (isVirtual) {
  492. *isVirtual = v > 0;
  493. }
  494. auto nameToSearch = (v ? YqlVirtualPrefix : "") + name;
  495. auto strict = FindItem(nameToSearch);
  496. if (strict) {
  497. return strict;
  498. }
  499. TMaybe<ui32> ret;
  500. for (ui32 i = 0; i < Items.size(); ++i) {
  501. if (AsciiEqualsIgnoreCase(nameToSearch, Items[i]->GetName())) {
  502. if (ret) {
  503. return Nothing();
  504. }
  505. ret = i;
  506. }
  507. }
  508. if (ret) {
  509. return ret;
  510. }
  511. }
  512. return Nothing();
  513. }
  514. const TTypeAnnotationNode* FindItemType(const TStringBuf& name) const {
  515. const auto it = LowerBound(Items.begin(), Items.end(), name, TItemLess());
  516. if (it == Items.end() || (*it)->GetName() != name) {
  517. return nullptr;
  518. }
  519. return (*it)->GetItemType();
  520. }
  521. TMaybe<TStringBuf> FindMistype(const TStringBuf& name) const {
  522. for (const auto& item: Items) {
  523. if (NLevenshtein::Distance(name, item->GetName()) < DefaultMistypeDistance) {
  524. return item->GetName();
  525. }
  526. }
  527. return TMaybe<TStringBuf>();
  528. }
  529. bool operator==(const TStructExprType& other) const {
  530. if (GetSize() != other.GetSize()) {
  531. return false;
  532. }
  533. for (ui32 i = 0, e = GetSize(); i < e; ++i) {
  534. if (GetItems()[i] != other.GetItems()[i]) {
  535. return false;
  536. }
  537. }
  538. return true;
  539. }
  540. TString ToString() const {
  541. TStringBuilder sb;
  542. for (std::size_t i = 0; i < Items.size(); i++) {
  543. sb << i << ": " << Items[i]->GetName() << "(" << FormatType(Items[i]->GetItemType()) << ")";
  544. if (i != Items.size() - 1) {
  545. sb << ", ";
  546. }
  547. }
  548. return sb;
  549. }
  550. private:
  551. TVector<const TItemExprType*> Items;
  552. };
  553. class TListExprType : public TTypeAnnotationNode {
  554. public:
  555. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::List;
  556. TListExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  557. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeHasDynamicSize, hash, itemType->GetUsedPgExtensions())
  558. , ItemType(itemType)
  559. {
  560. }
  561. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  562. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::List;
  563. return StreamHash(itemType->GetHash(), hash);
  564. }
  565. const TTypeAnnotationNode* GetItemType() const {
  566. return ItemType;
  567. }
  568. bool operator==(const TListExprType& other) const {
  569. return GetItemType() == other.GetItemType();
  570. }
  571. private:
  572. const TTypeAnnotationNode* ItemType;
  573. };
  574. class TStreamExprType : public TTypeAnnotationNode {
  575. public:
  576. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Stream;
  577. TStreamExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  578. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  579. , ItemType(itemType)
  580. {
  581. }
  582. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  583. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Stream;
  584. return StreamHash(itemType->GetHash(), hash);
  585. }
  586. const TTypeAnnotationNode* GetItemType() const {
  587. return ItemType;
  588. }
  589. bool operator==(const TStreamExprType& other) const {
  590. return GetItemType() == other.GetItemType();
  591. }
  592. private:
  593. const TTypeAnnotationNode* ItemType;
  594. };
  595. class TFlowExprType : public TTypeAnnotationNode {
  596. public:
  597. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Flow;
  598. TFlowExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  599. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  600. , ItemType(itemType)
  601. {
  602. }
  603. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  604. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Flow;
  605. return StreamHash(itemType->GetHash(), hash);
  606. }
  607. const TTypeAnnotationNode* GetItemType() const {
  608. return ItemType;
  609. }
  610. bool operator==(const TFlowExprType& other) const {
  611. return GetItemType() == other.GetItemType();
  612. }
  613. private:
  614. const TTypeAnnotationNode* ItemType;
  615. };
  616. class TBlockExprType : public TTypeAnnotationNode {
  617. public:
  618. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Block;
  619. TBlockExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  620. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  621. , ItemType(itemType)
  622. {
  623. }
  624. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  625. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Block;
  626. return StreamHash(itemType->GetHash(), hash);
  627. }
  628. const TTypeAnnotationNode* GetItemType() const {
  629. return ItemType;
  630. }
  631. bool operator==(const TBlockExprType& other) const {
  632. return GetItemType() == other.GetItemType();
  633. }
  634. private:
  635. const TTypeAnnotationNode* ItemType;
  636. };
  637. class TScalarExprType : public TTypeAnnotationNode {
  638. public:
  639. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Scalar;
  640. TScalarExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  641. : TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash, itemType->GetUsedPgExtensions())
  642. , ItemType(itemType)
  643. {
  644. }
  645. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  646. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Scalar;
  647. return StreamHash(itemType->GetHash(), hash);
  648. }
  649. const TTypeAnnotationNode* GetItemType() const {
  650. return ItemType;
  651. }
  652. bool operator==(const TScalarExprType& other) const {
  653. return GetItemType() == other.GetItemType();
  654. }
  655. private:
  656. const TTypeAnnotationNode* ItemType;
  657. };
  658. class TDataExprType : public TTypeAnnotationNode {
  659. public:
  660. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Data;
  661. TDataExprType(ui64 hash, EDataSlot slot)
  662. : TTypeAnnotationNode(KindValue, GetFlags(slot), hash, 0)
  663. , Slot(slot)
  664. {
  665. }
  666. static ui32 GetFlags(EDataSlot slot) {
  667. ui32 ret = TypeHasManyValues;
  668. auto props = NUdf::GetDataTypeInfo(slot).Features;
  669. if (!(props & NUdf::CanHash)) {
  670. ret |= TypeNonHashable;
  671. }
  672. if (!(props & NUdf::CanEquate)) {
  673. ret |= TypeNonEquatable;
  674. }
  675. if (!(props & NUdf::CanCompare)) {
  676. ret |= TypeNonComparable;
  677. ret |= TypeNonComparableInternal;
  678. }
  679. if (slot == NUdf::EDataSlot::Yson) {
  680. ret |= TypeHasBareYson;
  681. }
  682. if (props & NUdf::StringType) {
  683. ret |= TypeHasDynamicSize;
  684. }
  685. return ret;
  686. }
  687. static ui64 MakeHash(EDataSlot slot) {
  688. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Data;
  689. auto dataType = NUdf::GetDataTypeInfo(slot).Name;
  690. hash = StreamHash(dataType.size(), hash);
  691. return StreamHash(dataType.data(), dataType.size(), hash);
  692. }
  693. EDataSlot GetSlot() const {
  694. return Slot;
  695. }
  696. TStringBuf GetName() const {
  697. return NUdf::GetDataTypeInfo(Slot).Name;
  698. }
  699. bool operator==(const TDataExprType& other) const {
  700. return Slot == other.Slot;
  701. }
  702. private:
  703. EDataSlot Slot;
  704. };
  705. class TDataExprParamsType : public TDataExprType {
  706. public:
  707. TDataExprParamsType(ui64 hash, EDataSlot slot, const TStringBuf& one, const TStringBuf& two)
  708. : TDataExprType(hash, slot), One(one), Two(two)
  709. {}
  710. static ui64 MakeHash(EDataSlot slot, const TStringBuf& one, const TStringBuf& two) {
  711. auto hash = TDataExprType::MakeHash(slot);
  712. hash = StreamHash(one.size(), hash);
  713. hash = StreamHash(one.data(), one.size(), hash);
  714. hash = StreamHash(two.size(), hash);
  715. hash = StreamHash(two.data(), two.size(), hash);
  716. return hash;
  717. }
  718. const TStringBuf& GetParamOne() const {
  719. return One;
  720. }
  721. const TStringBuf& GetParamTwo() const {
  722. return Two;
  723. }
  724. bool operator==(const TDataExprParamsType& other) const {
  725. return GetSlot() == other.GetSlot() && GetParamOne() == other.GetParamOne() && GetParamTwo() == other.GetParamTwo();
  726. }
  727. bool Validate(TPosition position, TExprContext& ctx) const;
  728. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  729. private:
  730. const TStringBuf One, Two;
  731. };
  732. class TPgExprType : public TTypeAnnotationNode {
  733. public:
  734. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Pg;
  735. // TODO: TypeHasDynamicSize for Pg types
  736. TPgExprType(ui64 hash, ui32 typeId)
  737. : TTypeAnnotationNode(KindValue, GetFlags(typeId), hash, GetPgExtensionsMask(typeId))
  738. , TypeId(typeId)
  739. {
  740. }
  741. static ui64 MakeHash(ui32 typeId) {
  742. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Pg;
  743. return StreamHash(typeId, hash);
  744. }
  745. const TString& GetName() const;
  746. ui32 GetId() const {
  747. return TypeId;
  748. }
  749. bool operator==(const TPgExprType& other) const {
  750. return TypeId == other.TypeId;
  751. }
  752. private:
  753. ui32 GetFlags(ui32 typeId);
  754. ui64 GetPgExtensionsMask(ui32 typeId);
  755. private:
  756. ui32 TypeId;
  757. };
  758. ui64 MakePgExtensionMask(ui32 extensionIndex);
  759. class TWorldExprType : public TTypeAnnotationNode {
  760. public:
  761. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::World;
  762. TWorldExprType(ui64 hash)
  763. : TTypeAnnotationNode(KindValue,
  764. TypeNonComposable | TypeNonComputable | TypeNonPersistable | TypeNonInspectable, hash, 0)
  765. {
  766. }
  767. static ui64 MakeHash() {
  768. return TypeHashMagic | (ui64)ETypeAnnotationKind::World;
  769. }
  770. bool operator==(const TWorldExprType& other) const {
  771. Y_UNUSED(other);
  772. return true;
  773. }
  774. };
  775. class TOptionalExprType : public TTypeAnnotationNode {
  776. public:
  777. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Optional;
  778. TOptionalExprType(ui64 hash, const TTypeAnnotationNode* itemType)
  779. : TTypeAnnotationNode(KindValue, GetFlags(itemType), hash, itemType->GetUsedPgExtensions())
  780. , ItemType(itemType)
  781. {
  782. }
  783. static ui32 GetFlags(const TTypeAnnotationNode* itemType) {
  784. auto ret = TypeHasOptional | itemType->GetFlags();
  785. if (itemType->GetKind() == ETypeAnnotationKind::Data &&
  786. itemType->Cast<TDataExprType>()->GetSlot() == NUdf::EDataSlot::Yson) {
  787. ret = ret & ~TypeHasBareYson;
  788. }
  789. if (itemType->IsOptionalOrNull()) {
  790. ret |= TypeHasNestedOptional;
  791. }
  792. return ret;
  793. }
  794. static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
  795. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Optional;
  796. return StreamHash(itemType->GetHash(), hash);
  797. }
  798. const TTypeAnnotationNode* GetItemType() const {
  799. return ItemType;
  800. }
  801. bool operator==(const TOptionalExprType& other) const {
  802. return GetItemType() == other.GetItemType();
  803. }
  804. private:
  805. const TTypeAnnotationNode* ItemType;
  806. };
  807. class TVariantExprType : public TTypeAnnotationNode {
  808. public:
  809. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Variant;
  810. TVariantExprType(ui64 hash, const TTypeAnnotationNode* underlyingType)
  811. : TTypeAnnotationNode(KindValue, MakeFlags(underlyingType), hash, underlyingType->GetUsedPgExtensions())
  812. , UnderlyingType(underlyingType)
  813. {
  814. }
  815. static ui64 MakeHash(const TTypeAnnotationNode* underlyingType) {
  816. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Variant;
  817. return StreamHash(underlyingType->GetHash(), hash);
  818. }
  819. const TTypeAnnotationNode* GetUnderlyingType() const {
  820. return UnderlyingType;
  821. }
  822. bool operator==(const TVariantExprType& other) const {
  823. return GetUnderlyingType() == other.GetUnderlyingType();
  824. }
  825. bool Validate(TPosition position, TExprContext& ctx) const;
  826. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  827. static ui32 MakeFlags(const TTypeAnnotationNode* underlyingType);
  828. private:
  829. const TTypeAnnotationNode* UnderlyingType;
  830. };
  831. class TTypeExprType : public TTypeAnnotationNode {
  832. public:
  833. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Type;
  834. TTypeExprType(ui64 hash, const TTypeAnnotationNode* type)
  835. : TTypeAnnotationNode(KindValue, TypeNonPersistable | TypeNonComputable | (type->GetFlags() & TypeHasError), hash, 0)
  836. , Type(type)
  837. {
  838. }
  839. static ui64 MakeHash(const TTypeAnnotationNode* type) {
  840. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Type;
  841. return StreamHash(type->GetHash(), hash);
  842. }
  843. const TTypeAnnotationNode* GetType() const {
  844. return Type;
  845. }
  846. bool operator==(const TTypeExprType& other) const {
  847. return GetType() == other.GetType();
  848. }
  849. private:
  850. const TTypeAnnotationNode* Type;
  851. };
  852. class TDictExprType : public TTypeAnnotationNode {
  853. public:
  854. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Dict;
  855. TDictExprType(ui64 hash, const TTypeAnnotationNode* keyType, const TTypeAnnotationNode* payloadType)
  856. : TTypeAnnotationNode(KindValue, TypeNonComparable | TypeHasDynamicSize |
  857. keyType->GetFlags() | payloadType->GetFlags(), hash,
  858. keyType->GetUsedPgExtensions() | payloadType->GetUsedPgExtensions())
  859. , KeyType(keyType)
  860. , PayloadType(payloadType)
  861. {
  862. }
  863. static ui64 MakeHash(const TTypeAnnotationNode* keyType, const TTypeAnnotationNode* payloadType) {
  864. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Dict;
  865. return StreamHash(StreamHash(keyType->GetHash(), hash), payloadType->GetHash());
  866. }
  867. bool Validate(TPosition position, TExprContext& ctx) const;
  868. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  869. const TTypeAnnotationNode* GetKeyType() const {
  870. return KeyType;
  871. }
  872. const TTypeAnnotationNode* GetPayloadType() const {
  873. return PayloadType;
  874. }
  875. bool operator==(const TDictExprType& other) const {
  876. return GetKeyType() == other.GetKeyType() &&
  877. GetPayloadType() == other.GetPayloadType();
  878. }
  879. private:
  880. const TTypeAnnotationNode* KeyType;
  881. const TTypeAnnotationNode* PayloadType;
  882. };
  883. class TVoidExprType : public TTypeAnnotationNode {
  884. public:
  885. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Void;
  886. TVoidExprType(ui64 hash)
  887. : TTypeAnnotationNode(KindValue, 0, hash, 0)
  888. {
  889. }
  890. static ui64 MakeHash() {
  891. return TypeHashMagic | (ui64)ETypeAnnotationKind::Void;
  892. }
  893. bool operator==(const TVoidExprType& other) const {
  894. Y_UNUSED(other);
  895. return true;
  896. }
  897. };
  898. class TNullExprType : public TTypeAnnotationNode {
  899. public:
  900. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Null;
  901. TNullExprType(ui64 hash)
  902. : TTypeAnnotationNode(KindValue, TypeHasNull, hash, 0)
  903. {
  904. }
  905. static ui64 MakeHash() {
  906. return TypeHashMagic | (ui64)ETypeAnnotationKind::Null;
  907. }
  908. bool operator==(const TNullExprType& other) const {
  909. Y_UNUSED(other);
  910. return true;
  911. }
  912. };
  913. class TCallableExprType : public TTypeAnnotationNode {
  914. public:
  915. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Callable;
  916. struct TArgumentInfo {
  917. const TTypeAnnotationNode* Type = nullptr;
  918. TStringBuf Name;
  919. ui64 Flags = 0;
  920. bool operator==(const TArgumentInfo& other) const {
  921. return Type == other.Type && Name == other.Name && Flags == other.Flags;
  922. }
  923. bool operator!=(const TArgumentInfo& other) const {
  924. return !(*this == other);
  925. }
  926. };
  927. TCallableExprType(ui64 hash, const TTypeAnnotationNode* returnType, const TVector<TArgumentInfo>& arguments
  928. , size_t optionalArgumentsCount, const TStringBuf& payload)
  929. : TTypeAnnotationNode(KindValue, MakeFlags(arguments, returnType), hash, returnType->GetUsedPgExtensions())
  930. , ReturnType(returnType)
  931. , Arguments(arguments)
  932. , OptionalArgumentsCount(optionalArgumentsCount)
  933. , Payload(payload)
  934. {
  935. for (ui32 i = 0; i < Arguments.size(); ++i) {
  936. const auto& arg = Arguments[i];
  937. if (!arg.Name.empty()) {
  938. IndexByName.insert({ arg.Name, i });
  939. }
  940. }
  941. }
  942. static ui64 MakeHash(const TTypeAnnotationNode* returnType, const TVector<TArgumentInfo>& arguments
  943. , size_t optionalArgumentsCount, const TStringBuf& payload) {
  944. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Callable;
  945. hash = StreamHash(returnType->GetHash(), hash);
  946. hash = StreamHash(arguments.size(), hash);
  947. for (const auto& arg : arguments) {
  948. hash = StreamHash(arg.Name.size(), hash);
  949. hash = StreamHash(arg.Name.data(), arg.Name.size(), hash);
  950. hash = StreamHash(arg.Flags, hash);
  951. hash = StreamHash(arg.Type->GetHash(), hash);
  952. }
  953. hash = StreamHash(optionalArgumentsCount, hash);
  954. hash = StreamHash(payload.size(), hash);
  955. hash = StreamHash(payload.data(), payload.size(), hash);
  956. return hash;
  957. }
  958. const TTypeAnnotationNode* GetReturnType() const {
  959. return ReturnType;
  960. }
  961. size_t GetOptionalArgumentsCount() const {
  962. return OptionalArgumentsCount;
  963. }
  964. const TStringBuf& GetPayload() const {
  965. return Payload;
  966. }
  967. size_t GetArgumentsSize() const {
  968. return Arguments.size();
  969. }
  970. const TVector<TArgumentInfo>& GetArguments() const {
  971. return Arguments;
  972. }
  973. bool operator==(const TCallableExprType& other) const {
  974. if (GetArgumentsSize() != other.GetArgumentsSize()) {
  975. return false;
  976. }
  977. if (GetOptionalArgumentsCount() != other.GetOptionalArgumentsCount()) {
  978. return false;
  979. }
  980. if (GetReturnType() != other.GetReturnType()) {
  981. return false;
  982. }
  983. for (ui32 i = 0, e = GetArgumentsSize(); i < e; ++i) {
  984. if (GetArguments()[i] != other.GetArguments()[i]) {
  985. return false;
  986. }
  987. }
  988. return true;
  989. }
  990. bool Validate(TPosition position, TExprContext& ctx) const;
  991. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  992. TMaybe<ui32> ArgumentIndexByName(const TStringBuf& name) const {
  993. auto it = IndexByName.find(name);
  994. if (it == IndexByName.end()) {
  995. return {};
  996. }
  997. return it->second;
  998. }
  999. private:
  1000. static ui32 MakeFlags(const TVector<TArgumentInfo>& arguments, const TTypeAnnotationNode* returnType) {
  1001. ui32 flags = TypeNonPersistable;
  1002. flags |= returnType->GetFlags();
  1003. for (const auto& arg : arguments) {
  1004. flags |= (arg.Type->GetFlags() & TypeHasError);
  1005. }
  1006. return flags;
  1007. }
  1008. private:
  1009. const TTypeAnnotationNode* ReturnType;
  1010. TVector<TArgumentInfo> Arguments;
  1011. const size_t OptionalArgumentsCount;
  1012. const TStringBuf Payload;
  1013. THashMap<TStringBuf, ui32> IndexByName;
  1014. };
  1015. class TGenericExprType : public TTypeAnnotationNode {
  1016. public:
  1017. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Generic;
  1018. TGenericExprType(ui64 hash)
  1019. : TTypeAnnotationNode(KindValue, TypeNonComputable, hash, 0)
  1020. {
  1021. }
  1022. static ui64 MakeHash() {
  1023. return TypeHashMagic | (ui64)ETypeAnnotationKind::Generic;
  1024. }
  1025. bool operator==(const TGenericExprType& other) const {
  1026. Y_UNUSED(other);
  1027. return true;
  1028. }
  1029. };
  1030. class TResourceExprType : public TTypeAnnotationNode {
  1031. public:
  1032. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Resource;
  1033. TResourceExprType(ui64 hash, const TStringBuf& tag)
  1034. : TTypeAnnotationNode(KindValue, TypeNonPersistable | TypeHasManyValues, hash, 0)
  1035. , Tag(tag)
  1036. {}
  1037. static ui64 MakeHash(const TStringBuf& tag) {
  1038. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Resource;
  1039. hash = StreamHash(tag.size(), hash);
  1040. return StreamHash(tag.data(), tag.size(), hash);
  1041. }
  1042. const TStringBuf& GetTag() const {
  1043. return Tag;
  1044. }
  1045. bool operator==(const TResourceExprType& other) const {
  1046. return Tag == other.Tag;
  1047. }
  1048. private:
  1049. const TStringBuf Tag;
  1050. };
  1051. class TTaggedExprType : public TTypeAnnotationNode {
  1052. public:
  1053. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Tagged;
  1054. TTaggedExprType(ui64 hash, const TTypeAnnotationNode* baseType, const TStringBuf& tag)
  1055. : TTypeAnnotationNode(KindValue, baseType->GetFlags(), hash, baseType->GetUsedPgExtensions())
  1056. , BaseType(baseType)
  1057. , Tag(tag)
  1058. {}
  1059. static ui64 MakeHash(const TTypeAnnotationNode* baseType, const TStringBuf& tag) {
  1060. ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Tagged;
  1061. hash = StreamHash(baseType->GetHash(), hash);
  1062. hash = StreamHash(tag.size(), hash);
  1063. return StreamHash(tag.data(), tag.size(), hash);
  1064. }
  1065. const TStringBuf& GetTag() const {
  1066. return Tag;
  1067. }
  1068. const TTypeAnnotationNode* GetBaseType() const {
  1069. return BaseType;
  1070. }
  1071. bool operator==(const TTaggedExprType& other) const {
  1072. return Tag == other.Tag && GetBaseType() == other.GetBaseType();
  1073. }
  1074. bool Validate(TPosition position, TExprContext& ctx) const;
  1075. bool Validate(TPositionHandle position, TExprContext& ctx) const;
  1076. private:
  1077. const TTypeAnnotationNode* BaseType;
  1078. const TStringBuf Tag;
  1079. };
  1080. class TErrorExprType : public TTypeAnnotationNode {
  1081. public:
  1082. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Error;
  1083. TErrorExprType(ui64 hash, const TIssue& error)
  1084. : TTypeAnnotationNode(KindValue, TypeHasError, hash, 0)
  1085. , Error(error)
  1086. {}
  1087. static ui64 MakeHash(const TIssue& error) {
  1088. return error.Hash();
  1089. }
  1090. const TIssue& GetError() const {
  1091. return Error;
  1092. }
  1093. bool operator==(const TErrorExprType& other) const {
  1094. return Error == other.Error;
  1095. }
  1096. private:
  1097. const TIssue Error;
  1098. };
  1099. class TEmptyListExprType : public TTypeAnnotationNode {
  1100. public:
  1101. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::EmptyList;
  1102. TEmptyListExprType(ui64 hash)
  1103. : TTypeAnnotationNode(KindValue, 0, hash, 0)
  1104. {
  1105. }
  1106. static ui64 MakeHash() {
  1107. return TypeHashMagic | (ui64)ETypeAnnotationKind::EmptyList;
  1108. }
  1109. bool operator==(const TEmptyListExprType& other) const {
  1110. Y_UNUSED(other);
  1111. return true;
  1112. }
  1113. };
  1114. class TEmptyDictExprType : public TTypeAnnotationNode {
  1115. public:
  1116. static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::EmptyDict;
  1117. TEmptyDictExprType(ui64 hash)
  1118. : TTypeAnnotationNode(KindValue, 0, hash, 0)
  1119. {
  1120. }
  1121. static ui64 MakeHash() {
  1122. return TypeHashMagic | (ui64)ETypeAnnotationKind::EmptyDict;
  1123. }
  1124. bool operator==(const TEmptyDictExprType& other) const {
  1125. Y_UNUSED(other);
  1126. return true;
  1127. }
  1128. };
  1129. inline bool TTypeAnnotationNode::ReturnsWorld() const {
  1130. if (Kind == ETypeAnnotationKind::World) {
  1131. return true;
  1132. }
  1133. if (Kind == ETypeAnnotationKind::Tuple) {
  1134. auto tuple = static_cast<const TTupleExprType*>(this);
  1135. if (tuple->GetSize() == 2 && tuple->GetItems()[0]->GetKind() == ETypeAnnotationKind::World) {
  1136. return true;
  1137. }
  1138. }
  1139. return false;
  1140. }
  1141. inline bool TTypeAnnotationNode::Equals(const TTypeAnnotationNode& node) const {
  1142. if (this == &node) {
  1143. return true;
  1144. }
  1145. if (Hash != node.GetHash()) {
  1146. return false;
  1147. }
  1148. if (Kind != node.GetKind()) {
  1149. return false;
  1150. }
  1151. switch (Kind) {
  1152. case ETypeAnnotationKind::Unit:
  1153. return static_cast<const TUnitExprType&>(*this) == static_cast<const TUnitExprType&>(node);
  1154. case ETypeAnnotationKind::Tuple:
  1155. return static_cast<const TTupleExprType&>(*this) == static_cast<const TTupleExprType&>(node);
  1156. case ETypeAnnotationKind::Struct:
  1157. return static_cast<const TStructExprType&>(*this) == static_cast<const TStructExprType&>(node);
  1158. case ETypeAnnotationKind::Item:
  1159. return static_cast<const TItemExprType&>(*this) == static_cast<const TItemExprType&>(node);
  1160. case ETypeAnnotationKind::List:
  1161. return static_cast<const TListExprType&>(*this) == static_cast<const TListExprType&>(node);
  1162. case ETypeAnnotationKind::Data:
  1163. return static_cast<const TDataExprType&>(*this) == static_cast<const TDataExprType&>(node);
  1164. case ETypeAnnotationKind::Pg:
  1165. return static_cast<const TPgExprType&>(*this) == static_cast<const TPgExprType&>(node);
  1166. case ETypeAnnotationKind::World:
  1167. return static_cast<const TWorldExprType&>(*this) == static_cast<const TWorldExprType&>(node);
  1168. case ETypeAnnotationKind::Optional:
  1169. return static_cast<const TOptionalExprType&>(*this) == static_cast<const TOptionalExprType&>(node);
  1170. case ETypeAnnotationKind::Type:
  1171. return static_cast<const TTypeExprType&>(*this) == static_cast<const TTypeExprType&>(node);
  1172. case ETypeAnnotationKind::Dict:
  1173. return static_cast<const TDictExprType&>(*this) == static_cast<const TDictExprType&>(node);
  1174. case ETypeAnnotationKind::Void:
  1175. return static_cast<const TVoidExprType&>(*this) == static_cast<const TVoidExprType&>(node);
  1176. case ETypeAnnotationKind::Null:
  1177. return static_cast<const TNullExprType&>(*this) == static_cast<const TNullExprType&>(node);
  1178. case ETypeAnnotationKind::Callable:
  1179. return static_cast<const TCallableExprType&>(*this) == static_cast<const TCallableExprType&>(node);
  1180. case ETypeAnnotationKind::Generic:
  1181. return static_cast<const TGenericExprType&>(*this) == static_cast<const TGenericExprType&>(node);
  1182. case ETypeAnnotationKind::Resource:
  1183. return static_cast<const TResourceExprType&>(*this) == static_cast<const TResourceExprType&>(node);
  1184. case ETypeAnnotationKind::Tagged:
  1185. return static_cast<const TTaggedExprType&>(*this) == static_cast<const TTaggedExprType&>(node);
  1186. case ETypeAnnotationKind::Error:
  1187. return static_cast<const TErrorExprType&>(*this) == static_cast<const TErrorExprType&>(node);
  1188. case ETypeAnnotationKind::Variant:
  1189. return static_cast<const TVariantExprType&>(*this) == static_cast<const TVariantExprType&>(node);
  1190. case ETypeAnnotationKind::Stream:
  1191. return static_cast<const TStreamExprType&>(*this) == static_cast<const TStreamExprType&>(node);
  1192. case ETypeAnnotationKind::Flow:
  1193. return static_cast<const TFlowExprType&>(*this) == static_cast<const TFlowExprType&>(node);
  1194. case ETypeAnnotationKind::EmptyList:
  1195. return static_cast<const TEmptyListExprType&>(*this) == static_cast<const TEmptyListExprType&>(node);
  1196. case ETypeAnnotationKind::EmptyDict:
  1197. return static_cast<const TEmptyDictExprType&>(*this) == static_cast<const TEmptyDictExprType&>(node);
  1198. case ETypeAnnotationKind::Multi:
  1199. return static_cast<const TMultiExprType&>(*this) == static_cast<const TMultiExprType&>(node);
  1200. case ETypeAnnotationKind::Block:
  1201. return static_cast<const TBlockExprType&>(*this) == static_cast<const TBlockExprType&>(node);
  1202. case ETypeAnnotationKind::Scalar:
  1203. return static_cast<const TScalarExprType&>(*this) == static_cast<const TScalarExprType&>(node);
  1204. case ETypeAnnotationKind::LastType:
  1205. YQL_ENSURE(false, "Incorrect type");
  1206. }
  1207. return false;
  1208. }
  1209. inline void TTypeAnnotationNode::Accept(TTypeAnnotationVisitor& visitor) const {
  1210. switch (Kind) {
  1211. case ETypeAnnotationKind::Unit:
  1212. return visitor.Visit(static_cast<const TUnitExprType&>(*this));
  1213. case ETypeAnnotationKind::Tuple:
  1214. return visitor.Visit(static_cast<const TTupleExprType&>(*this));
  1215. case ETypeAnnotationKind::Struct:
  1216. return visitor.Visit(static_cast<const TStructExprType&>(*this));
  1217. case ETypeAnnotationKind::Item:
  1218. return visitor.Visit(static_cast<const TItemExprType&>(*this));
  1219. case ETypeAnnotationKind::List:
  1220. return visitor.Visit(static_cast<const TListExprType&>(*this));
  1221. case ETypeAnnotationKind::Data:
  1222. return visitor.Visit(static_cast<const TDataExprType&>(*this));
  1223. case ETypeAnnotationKind::Pg:
  1224. return visitor.Visit(static_cast<const TPgExprType&>(*this));
  1225. case ETypeAnnotationKind::World:
  1226. return visitor.Visit(static_cast<const TWorldExprType&>(*this));
  1227. case ETypeAnnotationKind::Optional:
  1228. return visitor.Visit(static_cast<const TOptionalExprType&>(*this));
  1229. case ETypeAnnotationKind::Type:
  1230. return visitor.Visit(static_cast<const TTypeExprType&>(*this));
  1231. case ETypeAnnotationKind::Dict:
  1232. return visitor.Visit(static_cast<const TDictExprType&>(*this));
  1233. case ETypeAnnotationKind::Void:
  1234. return visitor.Visit(static_cast<const TVoidExprType&>(*this));
  1235. case ETypeAnnotationKind::Null:
  1236. return visitor.Visit(static_cast<const TNullExprType&>(*this));
  1237. case ETypeAnnotationKind::Callable:
  1238. return visitor.Visit(static_cast<const TCallableExprType&>(*this));
  1239. case ETypeAnnotationKind::Generic:
  1240. return visitor.Visit(static_cast<const TGenericExprType&>(*this));
  1241. case ETypeAnnotationKind::Resource:
  1242. return visitor.Visit(static_cast<const TResourceExprType&>(*this));
  1243. case ETypeAnnotationKind::Tagged:
  1244. return visitor.Visit(static_cast<const TTaggedExprType&>(*this));
  1245. case ETypeAnnotationKind::Error:
  1246. return visitor.Visit(static_cast<const TErrorExprType&>(*this));
  1247. case ETypeAnnotationKind::Variant:
  1248. return visitor.Visit(static_cast<const TVariantExprType&>(*this));
  1249. case ETypeAnnotationKind::Stream:
  1250. return visitor.Visit(static_cast<const TStreamExprType&>(*this));
  1251. case ETypeAnnotationKind::Flow:
  1252. return visitor.Visit(static_cast<const TFlowExprType&>(*this));
  1253. case ETypeAnnotationKind::EmptyList:
  1254. return visitor.Visit(static_cast<const TEmptyListExprType&>(*this));
  1255. case ETypeAnnotationKind::EmptyDict:
  1256. return visitor.Visit(static_cast<const TEmptyDictExprType&>(*this));
  1257. case ETypeAnnotationKind::Multi:
  1258. return visitor.Visit(static_cast<const TMultiExprType&>(*this));
  1259. case ETypeAnnotationKind::Block:
  1260. return visitor.Visit(static_cast<const TBlockExprType&>(*this));
  1261. case ETypeAnnotationKind::Scalar:
  1262. return visitor.Visit(static_cast<const TScalarExprType&>(*this));
  1263. case ETypeAnnotationKind::LastType:
  1264. YQL_ENSURE(false, "Incorrect type");
  1265. }
  1266. }
  1267. class TExprNode {
  1268. friend class TExprNodeBuilder;
  1269. friend class TExprNodeReplaceBuilder;
  1270. friend struct TExprContext;
  1271. private:
  1272. struct TExprFlags {
  1273. enum : ui16 {
  1274. Default = 0,
  1275. Dead = 0x01,
  1276. Frozen = 0x02,
  1277. };
  1278. static constexpr ui32 FlagsMask = 0x03; // all flags should fit here
  1279. };
  1280. public:
  1281. typedef TIntrusivePtr<TExprNode> TPtr;
  1282. typedef std::vector<TPtr> TListType;
  1283. typedef TArrayRef<const TPtr> TChildrenType;
  1284. struct TPtrHash : private std::hash<const TExprNode*> {
  1285. size_t operator()(const TPtr& p) const {
  1286. return std::hash<const TExprNode*>::operator()(p.Get());
  1287. }
  1288. };
  1289. #define YQL_EXPR_NODE_TYPE_MAP(xx) \
  1290. xx(List, 0) \
  1291. xx(Atom, 1) \
  1292. xx(Callable, 2) \
  1293. xx(Lambda, 3) \
  1294. xx(Argument, 4) \
  1295. xx(Arguments, 5) \
  1296. xx(World, 7)
  1297. enum EType : ui8 {
  1298. YQL_EXPR_NODE_TYPE_MAP(ENUM_VALUE_GEN)
  1299. };
  1300. static constexpr ui32 TypeMask = 0x07; // all types should fit here
  1301. #define YQL_EXPR_NODE_STATE_MAP(xx) \
  1302. xx(Initial, 0) \
  1303. xx(TypeInProgress, 1) \
  1304. xx(TypePending, 2) \
  1305. xx(TypeComplete, 3) \
  1306. xx(ConstrInProgress, 4) \
  1307. xx(ConstrPending, 5) \
  1308. xx(ConstrComplete, 6) \
  1309. xx(ExecutionRequired, 7) \
  1310. xx(ExecutionInProgress, 8) \
  1311. xx(ExecutionPending, 9) \
  1312. xx(ExecutionComplete, 10) \
  1313. xx(Error, 11) \
  1314. xx(Last, 12)
  1315. enum class EState : ui8 {
  1316. YQL_EXPR_NODE_STATE_MAP(ENUM_VALUE_GEN)
  1317. };
  1318. static TPtr GetResult(const TPtr& node) {
  1319. return node->Type() == Callable ? node->Result : node;
  1320. }
  1321. const TExprNode& GetResult() const {
  1322. ENSURE_NOT_DELETED
  1323. if (Type() != Callable) {
  1324. return *this;
  1325. }
  1326. YQL_ENSURE(Result);
  1327. return *Result;
  1328. }
  1329. bool HasResult() const {
  1330. ENSURE_NOT_DELETED
  1331. return Type() != Callable || bool(Result);
  1332. }
  1333. void SetResult(TPtr&& result) {
  1334. ENSURE_NOT_DELETED
  1335. ENSURE_NOT_FROZEN
  1336. Result = std::move(result);
  1337. }
  1338. const std::shared_ptr<TListType>& GetWorldLinks() const {
  1339. ENSURE_NOT_DELETED
  1340. ENSURE_NOT_FROZEN
  1341. return WorldLinks;
  1342. }
  1343. std::shared_ptr<TListType>& GetWorldLinks() {
  1344. ENSURE_NOT_DELETED
  1345. ENSURE_NOT_FROZEN
  1346. return WorldLinks;
  1347. }
  1348. void SetWorldLinks(std::shared_ptr<TListType>&& links) {
  1349. ENSURE_NOT_DELETED
  1350. ENSURE_NOT_FROZEN
  1351. WorldLinks = std::move(links);
  1352. }
  1353. bool IsCallable(const std::string_view& name) const {
  1354. ENSURE_NOT_DELETED
  1355. return Type() == TExprNode::Callable && Content() == name;
  1356. }
  1357. bool IsCallable(const std::initializer_list<std::string_view>& names) const {
  1358. ENSURE_NOT_DELETED
  1359. return Type() == TExprNode::Callable && names.end() != std::find(names.begin(), names.end(), Content());
  1360. }
  1361. template <class TKey>
  1362. bool IsCallable(const THashSet<TKey>& names) const {
  1363. ENSURE_NOT_DELETED
  1364. return Type() == TExprNode::Callable && names.contains(Content());
  1365. }
  1366. bool IsCallable() const {
  1367. ENSURE_NOT_DELETED
  1368. return Type() == TExprNode::Callable;
  1369. }
  1370. bool IsAtom() const {
  1371. ENSURE_NOT_DELETED
  1372. return Type() == TExprNode::Atom;
  1373. }
  1374. bool IsWorld() const {
  1375. ENSURE_NOT_DELETED
  1376. return Type() == TExprNode::World;
  1377. }
  1378. bool IsAtom(const std::string_view& content) const {
  1379. ENSURE_NOT_DELETED
  1380. return Type() == TExprNode::Atom && Content() == content;
  1381. }
  1382. bool IsAtom(const std::initializer_list<std::string_view>& names) const {
  1383. ENSURE_NOT_DELETED
  1384. return Type() == TExprNode::Atom && names.end() != std::find(names.begin(), names.end(), Content());
  1385. }
  1386. bool IsList() const {
  1387. ENSURE_NOT_DELETED
  1388. return Type() == TExprNode::List;
  1389. }
  1390. bool IsLambda() const {
  1391. ENSURE_NOT_DELETED
  1392. return Type() == TExprNode::Lambda;
  1393. }
  1394. bool IsArgument() const {
  1395. ENSURE_NOT_DELETED
  1396. return Type() == TExprNode::Argument;
  1397. }
  1398. bool IsArguments() const {
  1399. ENSURE_NOT_DELETED
  1400. return Type() == TExprNode::Arguments;
  1401. }
  1402. bool IsComposable() const {
  1403. ENSURE_NOT_DELETED
  1404. return !IsLambda() && TypeAnnotation_->IsComposable();
  1405. }
  1406. bool IsPersistable() const {
  1407. ENSURE_NOT_DELETED
  1408. return !IsLambda() && TypeAnnotation_->IsPersistable();
  1409. }
  1410. bool IsComputable() const {
  1411. ENSURE_NOT_DELETED
  1412. return !IsLambda() && TypeAnnotation_->IsComputable();
  1413. }
  1414. bool IsInspectable() const {
  1415. ENSURE_NOT_DELETED
  1416. return !IsLambda() && TypeAnnotation_->IsInspectable();
  1417. }
  1418. bool ForDisclosing() const {
  1419. ENSURE_NOT_DELETED
  1420. return Type() == TExprNode::List && ShallBeDisclosed;
  1421. }
  1422. void SetDisclosing() {
  1423. ENSURE_NOT_DELETED
  1424. Y_ENSURE(Type() == TExprNode::List, "Must be list.");
  1425. ShallBeDisclosed = true;
  1426. }
  1427. ui32 GetFlagsToCompare() const {
  1428. ENSURE_NOT_DELETED
  1429. ui32 ret = Flags();
  1430. if ((ret & TNodeFlags::BinaryContent) == 0) {
  1431. ret |= TNodeFlags::ArbitraryContent | TNodeFlags::MultilineContent;
  1432. }
  1433. return ret;
  1434. }
  1435. TString Dump() const;
  1436. bool StartsExecution() const {
  1437. ENSURE_NOT_DELETED
  1438. return State == EState::ExecutionComplete
  1439. || State == EState::ExecutionInProgress
  1440. || State == EState::ExecutionRequired
  1441. || State == EState::ExecutionPending;
  1442. }
  1443. bool IsComplete() const {
  1444. YQL_ENSURE(HasLambdaScope);
  1445. return !OuterLambda;
  1446. }
  1447. bool IsLiteralList() const {
  1448. YQL_ENSURE(IsList());
  1449. return LiteralList;
  1450. }
  1451. void SetLiteralList(bool literal) {
  1452. YQL_ENSURE(IsList());
  1453. LiteralList = literal;
  1454. }
  1455. void Ref() {
  1456. ENSURE_NOT_DELETED
  1457. ENSURE_NOT_FROZEN
  1458. Y_ENSURE(RefCount_ < Max<ui32>());
  1459. ++RefCount_;
  1460. }
  1461. void UnRef() {
  1462. ENSURE_NOT_DELETED
  1463. ENSURE_NOT_FROZEN
  1464. if (!--RefCount_) {
  1465. Result.Reset();
  1466. WorldLinks.reset();
  1467. Children_.clear();
  1468. Constraints_.Clear();
  1469. MarkDead();
  1470. }
  1471. }
  1472. ui32 UseCount() const { return RefCount_; }
  1473. bool Unique() const { return 1U == UseCount(); }
  1474. bool Dead() const {
  1475. return ExprFlags_ & TExprFlags::Dead;
  1476. }
  1477. TPositionHandle Pos() const {
  1478. ENSURE_NOT_DELETED
  1479. return Position_;
  1480. }
  1481. TPosition Pos(const TExprContext& ctx) const;
  1482. EType Type() const {
  1483. ENSURE_NOT_DELETED
  1484. return (EType)Type_;
  1485. }
  1486. TListType::size_type ChildrenSize() const {
  1487. ENSURE_NOT_DELETED
  1488. return Children_.size();
  1489. }
  1490. TExprNode* Child(ui32 index) const {
  1491. ENSURE_NOT_DELETED
  1492. Y_ENSURE(index < Children_.size(), "index out of range");
  1493. return Children_[index].Get();
  1494. }
  1495. TPtr ChildPtr(ui32 index) const {
  1496. ENSURE_NOT_DELETED
  1497. Y_ENSURE(index < Children_.size(), "index out of range");
  1498. return Children_[index];
  1499. }
  1500. TPtr& ChildRef(ui32 index) {
  1501. ENSURE_NOT_DELETED
  1502. ENSURE_NOT_FROZEN
  1503. Y_ENSURE(index < Children_.size(), "index out of range");
  1504. return Children_[index];
  1505. }
  1506. const TExprNode& Head() const {
  1507. ENSURE_NOT_DELETED
  1508. Y_ENSURE(!Children_.empty(), "no children");
  1509. return *Children_.front();
  1510. }
  1511. TExprNode& Head() {
  1512. ENSURE_NOT_DELETED
  1513. Y_ENSURE(!Children_.empty(), "no children");
  1514. return *Children_.front();
  1515. }
  1516. TPtr HeadPtr() const {
  1517. ENSURE_NOT_DELETED
  1518. Y_ENSURE(!Children_.empty(), "no children");
  1519. return Children_.front();
  1520. }
  1521. TPtr& HeadRef() {
  1522. ENSURE_NOT_DELETED
  1523. ENSURE_NOT_FROZEN
  1524. Y_ENSURE(!Children_.empty(), "no children");
  1525. return Children_.front();
  1526. }
  1527. const TExprNode& Tail() const {
  1528. ENSURE_NOT_DELETED
  1529. Y_ENSURE(!Children_.empty(), "no children");
  1530. return *Children_.back();
  1531. }
  1532. TExprNode& Tail() {
  1533. ENSURE_NOT_DELETED
  1534. Y_ENSURE(!Children_.empty(), "no children");
  1535. return *Children_.back();
  1536. }
  1537. TPtr TailPtr() const {
  1538. ENSURE_NOT_DELETED
  1539. Y_ENSURE(!Children_.empty(), "no children");
  1540. return Children_.back();
  1541. }
  1542. TPtr& TailRef() {
  1543. ENSURE_NOT_DELETED
  1544. ENSURE_NOT_FROZEN
  1545. Y_ENSURE(!Children_.empty(), "no children");
  1546. return Children_.back();
  1547. }
  1548. TChildrenType Children() const {
  1549. ENSURE_NOT_DELETED
  1550. return TChildrenType(Children_.data(), Children_.size());
  1551. }
  1552. TListType ChildrenList() const {
  1553. ENSURE_NOT_DELETED
  1554. return Children_;
  1555. }
  1556. void ChangeChildrenInplace(TListType&& newChildren) {
  1557. ENSURE_NOT_DELETED
  1558. Children_ = std::move(newChildren);
  1559. }
  1560. template<class F>
  1561. void ForEachChild(const F& visitor) const {
  1562. for (const auto& child : Children_)
  1563. visitor(*child);
  1564. }
  1565. TStringBuf Content() const {
  1566. ENSURE_NOT_DELETED
  1567. return ContentUnchecked();
  1568. }
  1569. ui32 Flags() const {
  1570. ENSURE_NOT_DELETED
  1571. return Flags_;
  1572. }
  1573. void NormalizeAtomFlags(const TExprNode& otherAtom) {
  1574. ENSURE_NOT_DELETED
  1575. ENSURE_NOT_FROZEN
  1576. Y_ENSURE(Type_ == Atom && otherAtom.Type_ == Atom, "Expected atoms");
  1577. Y_ENSURE((Flags_ & TNodeFlags::BinaryContent) ==
  1578. (otherAtom.Flags_ & TNodeFlags::BinaryContent), "Mismatch binary atom flags");
  1579. if (!(Flags_ & TNodeFlags::BinaryContent)) {
  1580. Flags_ = Min(Flags_, otherAtom.Flags_);
  1581. }
  1582. }
  1583. ui64 UniqueId() const {
  1584. ENSURE_NOT_DELETED
  1585. return UniqueId_;
  1586. }
  1587. const TConstraintNode* GetConstraint(TStringBuf name) const {
  1588. ENSURE_NOT_DELETED
  1589. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1590. return Constraints_.GetConstraint(name);
  1591. }
  1592. template <class TConstraintType>
  1593. const TConstraintType* GetConstraint() const {
  1594. ENSURE_NOT_DELETED
  1595. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1596. return Constraints_.GetConstraint<TConstraintType>();
  1597. }
  1598. const TConstraintNode::TListType& GetAllConstraints() const {
  1599. ENSURE_NOT_DELETED
  1600. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1601. return Constraints_.GetAllConstraints();
  1602. }
  1603. const TConstraintSet& GetConstraintSet() const {
  1604. ENSURE_NOT_DELETED
  1605. Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
  1606. return Constraints_;
  1607. }
  1608. void AddConstraint(const TConstraintNode* node) {
  1609. ENSURE_NOT_DELETED
  1610. ENSURE_NOT_FROZEN
  1611. Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
  1612. Y_ENSURE(!StartsExecution());
  1613. Constraints_.AddConstraint(node);
  1614. State = EState::ConstrComplete;
  1615. }
  1616. void CopyConstraints(const TExprNode& node) {
  1617. ENSURE_NOT_DELETED
  1618. ENSURE_NOT_FROZEN
  1619. Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
  1620. Constraints_ = node.Constraints_;
  1621. State = EState::ConstrComplete;
  1622. }
  1623. void SetConstraints(const TConstraintSet& constraints) {
  1624. ENSURE_NOT_DELETED
  1625. ENSURE_NOT_FROZEN
  1626. Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
  1627. Constraints_ = constraints;
  1628. State = EState::ConstrComplete;
  1629. }
  1630. static TPtr NewAtom(ui64 uniqueId, TPositionHandle pos, const TStringBuf& content, ui32 flags) {
  1631. return Make(pos, Atom, {}, content, flags, uniqueId);
  1632. }
  1633. static TPtr NewArgument(ui64 uniqueId, TPositionHandle pos, const TStringBuf& name) {
  1634. return Make(pos, Argument, {}, name, 0, uniqueId);
  1635. }
  1636. static TPtr NewArguments(ui64 uniqueId, TPositionHandle pos, TListType&& argNodes) {
  1637. return Make(pos, Arguments, std::move(argNodes), ZeroString, 0, uniqueId);
  1638. }
  1639. static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TListType&& lambda) {
  1640. return Make(pos, Lambda, std::move(lambda), ZeroString, 0, uniqueId);
  1641. }
  1642. static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TPtr&& args, TListType&& body) {
  1643. TListType lambda(body.size() + 1U);
  1644. lambda.front() = std::move(args);
  1645. std::move(body.rbegin(), body.rend(), lambda.rbegin());
  1646. return NewLambda(uniqueId, pos, std::move(lambda));
  1647. }
  1648. static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TPtr&& args, TPtr&& body) {
  1649. TListType children(body ? 2 : 1);
  1650. children.front() = std::move(args);
  1651. if (body) {
  1652. children.back() = std::move(body);
  1653. }
  1654. return NewLambda(uniqueId, pos, std::move(children));
  1655. }
  1656. static TPtr NewWorld(ui64 uniqueId, TPositionHandle pos) {
  1657. return Make(pos, World, {}, {}, 0, uniqueId);
  1658. }
  1659. static TPtr NewList(ui64 uniqueId, TPositionHandle pos, TListType&& children) {
  1660. return Make(pos, List, std::move(children), ZeroString, 0, uniqueId);
  1661. }
  1662. static TPtr NewCallable(ui64 uniqueId, TPositionHandle pos, const TStringBuf& name, TListType&& children) {
  1663. return Make(pos, Callable, std::move(children), name, 0, uniqueId);
  1664. }
  1665. TPtr Clone(ui64 newUniqueId) const {
  1666. ENSURE_NOT_DELETED
  1667. return Make(Position_, (EType)Type_, TListType(Children_), Content(), Flags_, newUniqueId);
  1668. }
  1669. TPtr CloneWithPosition(ui64 newUniqueId, TPositionHandle pos) const {
  1670. ENSURE_NOT_DELETED
  1671. return Make(pos, (EType)Type_, TListType(Children_), Content(), Flags_, newUniqueId);
  1672. }
  1673. static TPtr NewNode(TPositionHandle position, EType type, TListType&& children, const TStringBuf& content, ui32 flags, ui64 uniqueId) {
  1674. return Make(position, type, std::move(children), content, flags, uniqueId);
  1675. }
  1676. TPtr ChangeContent(ui64 newUniqueId, const TStringBuf& content) const {
  1677. ENSURE_NOT_DELETED
  1678. return Make(Position_, (EType)Type_, TListType(Children_), content, Flags_, newUniqueId);
  1679. }
  1680. TPtr ChangeChildren(ui64 newUniqueId, TListType&& children) const {
  1681. ENSURE_NOT_DELETED
  1682. return Make(Position_, (EType)Type_, std::move(children), Content(), Flags_, newUniqueId);
  1683. }
  1684. TPtr ChangeChild(ui64 newUniqueId, ui32 index, TPtr&& child) const {
  1685. ENSURE_NOT_DELETED
  1686. Y_ENSURE(index < Children_.size(), "index out of range");
  1687. TListType newChildren(Children_);
  1688. newChildren[index] = std::move(child);
  1689. return Make(Position_, (EType)Type_, std::move(newChildren), Content(), Flags_, newUniqueId);
  1690. }
  1691. void SetTypeAnn(const TTypeAnnotationNode* typeAnn) {
  1692. TypeAnnotation_ = typeAnn;
  1693. State = TypeAnnotation_ ? EState::TypeComplete : EState::Initial;
  1694. }
  1695. const TTypeAnnotationNode* GetTypeAnn() const {
  1696. return TypeAnnotation_;
  1697. }
  1698. EState GetState() const {
  1699. return State;
  1700. }
  1701. void SetState(EState state) {
  1702. State = state;
  1703. }
  1704. ui32 GetArgIndex() const {
  1705. YQL_ENSURE(Type() == EType::Argument);
  1706. return ArgIndex;
  1707. }
  1708. void SetArgIndex(ui32 argIndex) {
  1709. YQL_ENSURE(Type() == EType::Argument);
  1710. YQL_ENSURE(argIndex <= Max<ui16>());
  1711. ArgIndex = (ui16)argIndex;
  1712. }
  1713. ui64 GetHash() const {
  1714. Y_DEBUG_ABORT_UNLESS(HashAbove == HashBelow);
  1715. return HashAbove;
  1716. }
  1717. void SetHash(ui64 hash) {
  1718. HashAbove = HashBelow = hash;
  1719. }
  1720. ui64 GetHashAbove() const {
  1721. return HashAbove;
  1722. }
  1723. void SetHashAbove(ui64 hash) {
  1724. HashAbove = hash;
  1725. }
  1726. ui64 GetHashBelow() const {
  1727. return HashBelow;
  1728. }
  1729. void SetHashBelow(ui64 hash) {
  1730. HashBelow = hash;
  1731. }
  1732. ui64 GetBloom() const {
  1733. return Bloom;
  1734. }
  1735. void SetBloom(ui64 bloom) {
  1736. Bloom = bloom;
  1737. }
  1738. // return pair of outer and inner lambda.
  1739. std::optional<std::pair<const TExprNode*, const TExprNode*>> GetDependencyScope() const {
  1740. if (HasLambdaScope) {
  1741. return std::make_pair(OuterLambda, InnerLambda);
  1742. }
  1743. return std::nullopt;
  1744. }
  1745. void SetDependencyScope(const TExprNode* outerLambda, const TExprNode* innerLambda) {
  1746. Y_DEBUG_ABORT_UNLESS(outerLambda == innerLambda || outerLambda->GetLambdaLevel() < innerLambda->GetLambdaLevel(), "Wrong scope of closures.");
  1747. HasLambdaScope = 1;
  1748. OuterLambda = outerLambda;
  1749. InnerLambda = innerLambda;
  1750. }
  1751. ui16 GetLambdaLevel() const { return LambdaLevel; }
  1752. void SetLambdaLevel(ui16 lambdaLevel) { LambdaLevel = lambdaLevel; }
  1753. bool IsUsedInDependsOn() const {
  1754. YQL_ENSURE(Type() == EType::Argument);
  1755. return UsedInDependsOn;
  1756. }
  1757. void SetUsedInDependsOn() {
  1758. YQL_ENSURE(Type() == EType::Argument);
  1759. UsedInDependsOn = 1;
  1760. }
  1761. void SetUnorderedChildren() {
  1762. YQL_ENSURE(Type() == EType::List || Type() == EType::Callable);
  1763. UnordChildren = 1;
  1764. }
  1765. bool UnorderedChildren() const {
  1766. YQL_ENSURE(Type() == EType::List || Type() == EType::Callable);
  1767. return bool(UnordChildren);
  1768. }
  1769. ~TExprNode() {
  1770. Y_ABORT_UNLESS(Dead(), "Node (id: %lu, type: %s, content: '%s') not dead on destruction.",
  1771. UniqueId_, ToString(Type_).data(), TString(ContentUnchecked()).data());
  1772. Y_ABORT_UNLESS(!UseCount(), "Node (id: %lu, type: %s, content: '%s') has non-zero use count on destruction.",
  1773. UniqueId_, ToString(Type_).data(), TString(ContentUnchecked()).data());
  1774. }
  1775. private:
  1776. static TPtr Make(TPositionHandle position, EType type, TListType&& children, const TStringBuf& content, ui32 flags, ui64 uniqueId) {
  1777. Y_ENSURE(flags <= TNodeFlags::FlagsMask);
  1778. Y_ENSURE(children.size() <= Max<ui32>());
  1779. Y_ENSURE(content.size() <= Max<ui32>());
  1780. for (size_t i = 0; i < children.size(); ++i) {
  1781. Y_ENSURE(children[i], "Unable to create node " << content << ": " << i << "th child is null");
  1782. }
  1783. return TPtr(new TExprNode(position, type, std::move(children), content.data(), ui32(content.size()), flags, uniqueId));
  1784. }
  1785. TExprNode(TPositionHandle position, EType type, TListType&& children,
  1786. const char* content, ui32 contentSize, ui32 flags, ui64 uniqueId)
  1787. : Children_(std::move(children))
  1788. , Content_(content)
  1789. , UniqueId_(uniqueId)
  1790. , Position_(position)
  1791. , ContentSize(contentSize)
  1792. , Type_(type)
  1793. , Flags_(flags)
  1794. , ExprFlags_(TExprFlags::Default)
  1795. , State(EState::Initial)
  1796. , HasLambdaScope(0)
  1797. , UsedInDependsOn(0)
  1798. , UnordChildren(0)
  1799. , ShallBeDisclosed(0)
  1800. , LiteralList(0)
  1801. {}
  1802. TExprNode(const TExprNode&) = delete;
  1803. TExprNode(TExprNode&&) = delete;
  1804. TExprNode& operator=(const TExprNode&) = delete;
  1805. TExprNode& operator=(TExprNode&&) = delete;
  1806. bool Frozen() const {
  1807. return ExprFlags_ & TExprFlags::Frozen;
  1808. }
  1809. void MarkFrozen(bool frozen = true) {
  1810. if (frozen) {
  1811. ExprFlags_ |= TExprFlags::Frozen;
  1812. } else {
  1813. ExprFlags_ &= ~TExprFlags::Frozen;
  1814. }
  1815. }
  1816. void MarkDead() {
  1817. ExprFlags_ |= TExprFlags::Dead;
  1818. }
  1819. TStringBuf ContentUnchecked() const {
  1820. return TStringBuf(Content_, ContentSize);
  1821. }
  1822. TListType Children_;
  1823. TConstraintSet Constraints_;
  1824. const char* Content_ = nullptr;
  1825. const TExprNode* OuterLambda = nullptr;
  1826. const TExprNode* InnerLambda = nullptr;
  1827. TPtr Result;
  1828. std::shared_ptr<TListType> WorldLinks;
  1829. ui64 HashAbove = 0ULL;
  1830. ui64 HashBelow = 0ULL;
  1831. ui64 Bloom = 0ULL;
  1832. const ui64 UniqueId_;
  1833. const TTypeAnnotationNode* TypeAnnotation_ = nullptr;
  1834. const TPositionHandle Position_;
  1835. ui32 RefCount_ = 0U;
  1836. const ui32 ContentSize;
  1837. ui16 ArgIndex = ui16(-1);
  1838. ui16 LambdaLevel = 0; // filled together with OuterLambda
  1839. ui16 IntermediateHashesCount = 0;
  1840. static_assert(TypeMask <= 7, "EType wont fit in 3 bits, increase Type_ bitfield size");
  1841. static_assert(TNodeFlags::FlagsMask <= 7, "TNodeFlags wont fit in 3 bits, increase Flags_ bitfield size");
  1842. static_assert(TExprFlags::FlagsMask <= 3, "TExprFlags wont fit in 2 bits, increase ExprFlags_ bitfield size");
  1843. static_assert(int(EState::Last) <= 16, "EState wont fit in 4 bits, increase State bitfield size");
  1844. struct {
  1845. ui8 Type_ : 3;
  1846. ui8 Flags_ : 3;
  1847. ui8 ExprFlags_ : 2;
  1848. EState State : 4;
  1849. ui8 HasLambdaScope : 1;
  1850. ui8 UsedInDependsOn : 1;
  1851. ui8 UnordChildren : 1;
  1852. ui8 ShallBeDisclosed: 1;
  1853. ui8 LiteralList : 1;
  1854. };
  1855. };
  1856. class TExportTable {
  1857. public:
  1858. using TSymbols = THashMap<TString, TExprNode::TPtr>;
  1859. TExportTable() = default;
  1860. TExportTable(TExprContext& ctx, TSymbols&& symbols)
  1861. : Symbols_(std::move(symbols))
  1862. , Ctx_(&ctx)
  1863. {}
  1864. const TSymbols& Symbols() const {
  1865. return Symbols_;
  1866. }
  1867. TSymbols& Symbols(TExprContext& ctx) {
  1868. if (Ctx_) {
  1869. YQL_ENSURE(Ctx_ == &ctx);
  1870. } else {
  1871. Ctx_ = &ctx;
  1872. }
  1873. return Symbols_;
  1874. }
  1875. TExprContext& ExprCtx() const {
  1876. YQL_ENSURE(Ctx_);
  1877. return *Ctx_;
  1878. }
  1879. private:
  1880. TSymbols Symbols_;
  1881. TExprContext* Ctx_ = nullptr;
  1882. };
  1883. using TModulesTable = THashMap<TString, TExportTable>;
  1884. class IModuleResolver {
  1885. public:
  1886. typedef std::shared_ptr<IModuleResolver> TPtr;
  1887. virtual bool AddFromFile(const std::string_view& file, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos = {}) = 0;
  1888. virtual bool AddFromUrl(const std::string_view& file, const std::string_view& url, const std::string_view& tokenName, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos = {}) = 0;
  1889. virtual bool AddFromMemory(const std::string_view& file, const TString& body, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos = {}) = 0;
  1890. virtual bool AddFromMemory(const std::string_view& file, const TString& body, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TPosition pos, TString& moduleName, std::vector<TString>* exports = nullptr, std::vector<TString>* imports = nullptr) = 0;
  1891. virtual bool Link(TExprContext& ctx) = 0;
  1892. virtual void UpdateNextUniqueId(TExprContext& ctx) const = 0;
  1893. virtual ui64 GetNextUniqueId() const = 0;
  1894. virtual void RegisterPackage(const TString& package) = 0;
  1895. virtual bool SetPackageDefaultVersion(const TString& package, ui32 version) = 0;
  1896. virtual const TExportTable* GetModule(const TString& module) const = 0;
  1897. virtual void WriteStatistics(NYson::TYsonWriter& writer) = 0;
  1898. /*
  1899. Create new resolver which will use already collected modules in readonly manner.
  1900. Parent resolver should be alive while using child due to raw data sharing.
  1901. */
  1902. virtual IModuleResolver::TPtr CreateMutableChild() const = 0;
  1903. virtual void SetFileAliasPrefix(TString&& prefix) = 0;
  1904. virtual TString GetFileAliasPrefix() const = 0;
  1905. virtual ~IModuleResolver() = default;
  1906. };
  1907. struct TExprStep {
  1908. enum ELevel {
  1909. Params,
  1910. ExpandApplyForLambdas,
  1911. ValidateProviders,
  1912. Configure,
  1913. ExprEval,
  1914. DiscoveryIO,
  1915. Epochs,
  1916. Intents,
  1917. LoadTablesMetadata,
  1918. RewriteIO,
  1919. Recapture,
  1920. LastLevel
  1921. };
  1922. TExprStep()
  1923. {
  1924. }
  1925. void Done(ELevel level) {
  1926. Steps_.Set(level);
  1927. }
  1928. void Reset() {
  1929. Steps_.Reset();
  1930. }
  1931. TExprStep& Repeat(ELevel level) {
  1932. Steps_.Reset(level);
  1933. return *this;
  1934. }
  1935. bool IsDone(ELevel level) {
  1936. return Steps_.Test(level);
  1937. }
  1938. private:
  1939. TEnumBitSet<ELevel, Params, LastLevel> Steps_;
  1940. };
  1941. template <typename T>
  1942. struct TMakeTypeImpl;
  1943. template <class T>
  1944. using TNodeMap = std::unordered_map<const TExprNode*, T>;
  1945. using TNodeSet = std::unordered_set<const TExprNode*>;
  1946. using TNodeOnNodeOwnedMap = TNodeMap<TExprNode::TPtr>;
  1947. using TParentsMap = TNodeMap<TNodeSet>;
  1948. using TNodeMultiSet = std::unordered_multiset<const TExprNode*>;
  1949. using TParentsMultiMap = TNodeMap<TNodeMultiSet>;
  1950. template <>
  1951. struct TMakeTypeImpl<TVoidExprType> {
  1952. static const TVoidExprType* Make(TExprContext& ctx);
  1953. };
  1954. template <>
  1955. struct TMakeTypeImpl<TNullExprType> {
  1956. static const TNullExprType* Make(TExprContext& ctx);
  1957. };
  1958. template <>
  1959. struct TMakeTypeImpl<TEmptyListExprType> {
  1960. static const TEmptyListExprType* Make(TExprContext& ctx);
  1961. };
  1962. template <>
  1963. struct TMakeTypeImpl<TEmptyDictExprType> {
  1964. static const TEmptyDictExprType* Make(TExprContext& ctx);
  1965. };
  1966. template <>
  1967. struct TMakeTypeImpl<TUnitExprType> {
  1968. static const TUnitExprType* Make(TExprContext& ctx);
  1969. };
  1970. template <>
  1971. struct TMakeTypeImpl<TWorldExprType> {
  1972. static const TWorldExprType* Make(TExprContext& ctx);
  1973. };
  1974. template <>
  1975. struct TMakeTypeImpl<TGenericExprType> {
  1976. static const TGenericExprType* Make(TExprContext& ctx);
  1977. };
  1978. template <>
  1979. struct TMakeTypeImpl<TItemExprType> {
  1980. static const TItemExprType* Make(TExprContext& ctx, const TStringBuf& name, const TTypeAnnotationNode* itemType);
  1981. };
  1982. template <>
  1983. struct TMakeTypeImpl<TListExprType> {
  1984. static const TListExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  1985. };
  1986. template <>
  1987. struct TMakeTypeImpl<TOptionalExprType> {
  1988. static const TOptionalExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  1989. };
  1990. template <>
  1991. struct TMakeTypeImpl<TVariantExprType> {
  1992. static const TVariantExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* underlyingType);
  1993. };
  1994. template <>
  1995. struct TMakeTypeImpl<TErrorExprType> {
  1996. static const TErrorExprType* Make(TExprContext& ctx, const TIssue& error);
  1997. };
  1998. template <>
  1999. struct TMakeTypeImpl<TDictExprType> {
  2000. static const TDictExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* keyType,
  2001. const TTypeAnnotationNode* payloadType);
  2002. };
  2003. template <>
  2004. struct TMakeTypeImpl<TTypeExprType> {
  2005. static const TTypeExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* baseType);
  2006. };
  2007. template <>
  2008. struct TMakeTypeImpl<TDataExprType> {
  2009. static const TDataExprType* Make(TExprContext& ctx, EDataSlot slot);
  2010. };
  2011. template <>
  2012. struct TMakeTypeImpl<TPgExprType> {
  2013. static const TPgExprType* Make(TExprContext& ctx, ui32 typeId);
  2014. };
  2015. template <>
  2016. struct TMakeTypeImpl<TDataExprParamsType> {
  2017. static const TDataExprParamsType* Make(TExprContext& ctx, EDataSlot slot, const TStringBuf& one, const TStringBuf& two);
  2018. };
  2019. template <>
  2020. struct TMakeTypeImpl<TCallableExprType> {
  2021. static const TCallableExprType* Make(
  2022. TExprContext& ctx, const TTypeAnnotationNode* returnType, const TVector<TCallableExprType::TArgumentInfo>& arguments,
  2023. size_t optionalArgumentsCount, const TStringBuf& payload);
  2024. };
  2025. template <>
  2026. struct TMakeTypeImpl<TResourceExprType> {
  2027. static const TResourceExprType* Make(TExprContext& ctx, const TStringBuf& tag);
  2028. };
  2029. template <>
  2030. struct TMakeTypeImpl<TTaggedExprType> {
  2031. static const TTaggedExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* baseType, const TStringBuf& tag);
  2032. };
  2033. template <>
  2034. struct TMakeTypeImpl<TStructExprType> {
  2035. static const TStructExprType* Make(TExprContext& ctx, const TVector<const TItemExprType*>& items);
  2036. };
  2037. template <>
  2038. struct TMakeTypeImpl<TTupleExprType> {
  2039. static const TTupleExprType* Make(TExprContext& ctx, const TTypeAnnotationNode::TListType& items);
  2040. };
  2041. template <>
  2042. struct TMakeTypeImpl<TMultiExprType> {
  2043. static const TMultiExprType* Make(TExprContext& ctx, const TTypeAnnotationNode::TListType& items);
  2044. };
  2045. template <>
  2046. struct TMakeTypeImpl<TStreamExprType> {
  2047. static const TStreamExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2048. };
  2049. template <>
  2050. struct TMakeTypeImpl<TFlowExprType> {
  2051. static const TFlowExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2052. };
  2053. template <>
  2054. struct TMakeTypeImpl<TBlockExprType> {
  2055. static const TBlockExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2056. };
  2057. template <>
  2058. struct TMakeTypeImpl<TScalarExprType> {
  2059. static const TScalarExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
  2060. };
  2061. using TSingletonTypeCache = std::tuple<
  2062. const TVoidExprType*,
  2063. const TNullExprType*,
  2064. const TUnitExprType*,
  2065. const TEmptyListExprType*,
  2066. const TEmptyDictExprType*,
  2067. const TWorldExprType*,
  2068. const TGenericExprType*,
  2069. const TTupleExprType*,
  2070. const TStructExprType*,
  2071. const TMultiExprType*
  2072. >;
  2073. struct TExprContext : private TNonCopyable {
  2074. class TFreezeGuard {
  2075. public:
  2076. TFreezeGuard(const TFreezeGuard&) = delete;
  2077. TFreezeGuard& operator=(const TFreezeGuard&) = delete;
  2078. TFreezeGuard(TExprContext& ctx)
  2079. : Ctx(ctx)
  2080. {
  2081. Ctx.Freeze();
  2082. }
  2083. ~TFreezeGuard() {
  2084. Ctx.UnFreeze();
  2085. }
  2086. private:
  2087. TExprContext& Ctx;
  2088. };
  2089. TIssueManager IssueManager;
  2090. TNodeMap<TIssues> AssociativeIssues;
  2091. TMemoryPool StringPool;
  2092. std::unordered_set<std::string_view> Strings;
  2093. std::unordered_map<ui32, std::string_view> Indexes;
  2094. std::stack<std::unique_ptr<const TTypeAnnotationNode>> TypeNodes;
  2095. std::stack<std::unique_ptr<const TConstraintNode>> ConstraintNodes;
  2096. std::deque<std::unique_ptr<TExprNode>> ExprNodes;
  2097. TSingletonTypeCache SingletonTypeCache;
  2098. std::unordered_set<const TTypeAnnotationNode*, TTypeAnnotationNode::THash, TTypeAnnotationNode::TEqual> TypeSet;
  2099. std::unordered_set<const TConstraintNode*, TConstraintNode::THash, TConstraintNode::TEqual> ConstraintSet;
  2100. std::unordered_map<const TTypeAnnotationNode*, TExprNode::TPtr> TypeAsNodeCache;
  2101. std::unordered_set<TStringBuf, THash<TStringBuf>> DisabledConstraints;
  2102. ui64 NextUniqueId = 0;
  2103. ui64 NodeAllocationCounter = 0;
  2104. ui64 NodesAllocationLimit = 3000000;
  2105. ui64 StringsAllocationLimit = 100000000;
  2106. ui64 RepeatTransformLimit = 1000000;
  2107. ui64 RepeatTransformCounter = 0;
  2108. ui64 TypeAnnNodeRepeatLimit = 1000;
  2109. TGcNodeConfig GcConfig;
  2110. std::unordered_multimap<ui64, TExprNode*> UniqueNodes;
  2111. TExprStep Step;
  2112. bool Frozen;
  2113. explicit TExprContext(ui64 nextUniqueId = 0ULL);
  2114. ~TExprContext();
  2115. ui64 AllocateNextUniqueId() {
  2116. ENSURE_NOT_FROZEN_CTX
  2117. const auto ret = ++NextUniqueId;
  2118. return ret;
  2119. }
  2120. TStringBuf AppendString(const TStringBuf& buf) {
  2121. ENSURE_NOT_FROZEN_CTX
  2122. if (buf.size() == 0) {
  2123. return ZeroString;
  2124. }
  2125. auto it = Strings.find(buf);
  2126. if (it != Strings.end()) {
  2127. return *it;
  2128. }
  2129. auto newBuf = StringPool.AppendString(buf);
  2130. Strings.insert(it, newBuf);
  2131. return newBuf;
  2132. }
  2133. TPositionHandle AppendPosition(const TPosition& pos);
  2134. TPosition GetPosition(TPositionHandle handle) const;
  2135. TExprNodeBuilder Builder(TPositionHandle pos) {
  2136. return TExprNodeBuilder(pos, *this);
  2137. }
  2138. [[nodiscard]]
  2139. TExprNode::TPtr RenameNode(const TExprNode& node, const TStringBuf& name);
  2140. [[nodiscard]]
  2141. TExprNode::TPtr ShallowCopy(const TExprNode& node);
  2142. [[nodiscard]]
  2143. TExprNode::TPtr ShallowCopyWithPosition(const TExprNode& node, TPositionHandle pos);
  2144. [[nodiscard]]
  2145. TExprNode::TPtr ChangeChildren(const TExprNode& node, TExprNode::TListType&& children);
  2146. [[nodiscard]]
  2147. TExprNode::TPtr ChangeChild(const TExprNode& node, ui32 index, TExprNode::TPtr&& child);
  2148. [[nodiscard]]
  2149. TExprNode::TPtr ExactChangeChildren(const TExprNode& node, TExprNode::TListType&& children);
  2150. [[nodiscard]]
  2151. TExprNode::TPtr ExactShallowCopy(const TExprNode& node);
  2152. [[nodiscard]]
  2153. TExprNode::TPtr DeepCopyLambda(const TExprNode& node, TExprNode::TListType&& body);
  2154. [[nodiscard]]
  2155. TExprNode::TPtr DeepCopyLambda(const TExprNode& node, TExprNode::TPtr&& body = TExprNode::TPtr());
  2156. [[nodiscard]]
  2157. TExprNode::TPtr FuseLambdas(const TExprNode& outer, const TExprNode& inner);
  2158. using TCustomDeepCopier = std::function<bool(const TExprNode& node, TExprNode::TListType& newChildren)>;
  2159. [[nodiscard]]
  2160. TExprNode::TPtr DeepCopy(const TExprNode& node, TExprContext& nodeContext, TNodeOnNodeOwnedMap& deepClones,
  2161. bool internStrings, bool copyTypes, bool copyResult = false, TCustomDeepCopier customCopier = {});
  2162. [[nodiscard]]
  2163. TExprNode::TPtr SwapWithHead(const TExprNode& node);
  2164. TExprNode::TPtr ReplaceNode(TExprNode::TPtr&& start, const TExprNode& src, TExprNode::TPtr dst);
  2165. TExprNode::TPtr ReplaceNodes(TExprNode::TPtr&& start, const TNodeOnNodeOwnedMap& replaces);
  2166. template<bool KeepTypeAnns = false>
  2167. TExprNode::TListType ReplaceNodes(TExprNode::TListType&& start, const TNodeOnNodeOwnedMap& replaces);
  2168. TExprNode::TPtr NewAtom(TPositionHandle pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent) {
  2169. ++NodeAllocationCounter;
  2170. const auto node = TExprNode::NewAtom(AllocateNextUniqueId(), pos, AppendString(content), flags);
  2171. ExprNodes.emplace_back(node.Get());
  2172. return node;
  2173. }
  2174. TExprNode::TPtr NewAtom(TPositionHandle pos, ui32 index) {
  2175. ++NodeAllocationCounter;
  2176. const auto node = TExprNode::NewAtom(AllocateNextUniqueId(), pos, GetIndexAsString(index), TNodeFlags::Default);
  2177. ExprNodes.emplace_back(node.Get());
  2178. return node;
  2179. }
  2180. TExprNode::TPtr NewArgument(TPositionHandle pos, const TStringBuf& name) {
  2181. ++NodeAllocationCounter;
  2182. const auto node = TExprNode::NewArgument(AllocateNextUniqueId(), pos, AppendString(name));
  2183. ExprNodes.emplace_back(node.Get());
  2184. return node;
  2185. }
  2186. TExprNode::TPtr NewArguments(TPositionHandle pos, TExprNode::TListType&& argNodes) {
  2187. ++NodeAllocationCounter;
  2188. const auto node = TExprNode::NewArguments(AllocateNextUniqueId(), pos, std::move(argNodes));
  2189. ExprNodes.emplace_back(node.Get());
  2190. return node;
  2191. }
  2192. TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TListType&& lambda) {
  2193. ++NodeAllocationCounter;
  2194. const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(lambda));
  2195. ExprNodes.emplace_back(node.Get());
  2196. return node;
  2197. }
  2198. TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TPtr&& args, TExprNode::TListType&& body) {
  2199. ++NodeAllocationCounter;
  2200. const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(args), std::move(body));
  2201. ExprNodes.emplace_back(node.Get());
  2202. return node;
  2203. }
  2204. TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TPtr&& args, TExprNode::TPtr&& body) {
  2205. ++NodeAllocationCounter;
  2206. const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(args), std::move(body));
  2207. ExprNodes.emplace_back(node.Get());
  2208. return node;
  2209. }
  2210. TExprNode::TPtr NewWorld(TPositionHandle pos) {
  2211. ++NodeAllocationCounter;
  2212. const auto node = TExprNode::NewWorld(AllocateNextUniqueId(), pos);
  2213. ExprNodes.emplace_back(node.Get());
  2214. return node;
  2215. }
  2216. TExprNode::TPtr NewList(TPositionHandle pos, TExprNode::TListType&& children) {
  2217. ++NodeAllocationCounter;
  2218. const auto node = TExprNode::NewList(AllocateNextUniqueId(), pos, std::move(children));
  2219. ExprNodes.emplace_back(node.Get());
  2220. return node;
  2221. }
  2222. TExprNode::TPtr NewCallable(TPositionHandle pos, const TStringBuf& name, TExprNode::TListType&& children) {
  2223. ++NodeAllocationCounter;
  2224. const auto node = TExprNode::NewCallable(AllocateNextUniqueId(), pos, AppendString(name), std::move(children));
  2225. ExprNodes.emplace_back(node.Get());
  2226. return node;
  2227. }
  2228. TExprNode::TPtr NewAtom(TPosition pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent) {
  2229. return NewAtom(AppendPosition(pos), content, flags);
  2230. }
  2231. TExprNode::TPtr NewAtom(TPosition pos, ui32 index) {
  2232. return NewAtom(AppendPosition(pos), index);
  2233. }
  2234. TExprNode::TPtr NewArgument(TPosition pos, const TStringBuf& name) {
  2235. return NewArgument(AppendPosition(pos), name);
  2236. }
  2237. TExprNode::TPtr NewArguments(TPosition pos, TExprNode::TListType&& argNodes) {
  2238. return NewArguments(AppendPosition(pos), std::move(argNodes));
  2239. }
  2240. TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TListType&& lambda) {
  2241. return NewLambda(AppendPosition(pos), std::move(lambda));
  2242. }
  2243. TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TPtr&& args, TExprNode::TListType&& body) {
  2244. return NewLambda(AppendPosition(pos), std::move(args), std::move(body));
  2245. }
  2246. TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TPtr&& args, TExprNode::TPtr&& body) {
  2247. return NewLambda(AppendPosition(pos), std::move(args), std::move(body));
  2248. }
  2249. TExprNode::TPtr NewWorld(TPosition pos) {
  2250. return NewWorld(AppendPosition(pos));
  2251. }
  2252. TExprNode::TPtr NewList(TPosition pos, TExprNode::TListType&& children) {
  2253. return NewList(AppendPosition(pos), std::move(children));
  2254. }
  2255. TExprNode::TPtr NewCallable(TPosition pos, const TStringBuf& name, TExprNode::TListType&& children) {
  2256. return NewCallable(AppendPosition(pos), name, std::move(children));
  2257. }
  2258. TExprNode::TPtr WrapByCallableIf(bool condition, const TStringBuf& callable, TExprNode::TPtr&& node);
  2259. template <typename T, typename... Args>
  2260. const T* MakeType(Args&&... args);
  2261. template <typename T, typename... Args>
  2262. const T* MakeConstraint(Args&&... args);
  2263. TConstraintSet MakeConstraintSet(const NYT::TNode& serializedConstraints);
  2264. void AddError(const TIssue& error) {
  2265. ENSURE_NOT_FROZEN_CTX
  2266. IssueManager.RaiseIssue(error);
  2267. }
  2268. bool AddWarning(const TIssue& warning) {
  2269. ENSURE_NOT_FROZEN_CTX
  2270. return IssueManager.RaiseWarning(warning);
  2271. }
  2272. void Freeze();
  2273. void UnFreeze();
  2274. void Reset();
  2275. template <class TConstraint>
  2276. bool IsConstraintEnabled() const {
  2277. return DisabledConstraints.find(TConstraint::Name()) == DisabledConstraints.end();
  2278. }
  2279. std::string_view GetIndexAsString(ui32 index);
  2280. private:
  2281. using TPositionHandleEqualPred = std::function<bool(TPositionHandle, TPositionHandle)>;
  2282. using TPositionHandleHasher = std::function<size_t(TPositionHandle)>;
  2283. bool IsEqual(TPositionHandle a, TPositionHandle b) const;
  2284. size_t GetHash(TPositionHandle p) const;
  2285. std::unordered_set<TPositionHandle, TPositionHandleHasher, TPositionHandleEqualPred> PositionSet;
  2286. std::deque<TPosition> Positions;
  2287. };
  2288. template <typename T, typename... Args>
  2289. inline const T* TExprContext::MakeConstraint(Args&&... args) {
  2290. ENSURE_NOT_FROZEN_CTX
  2291. if (!IsConstraintEnabled<T>()) {
  2292. return nullptr;
  2293. }
  2294. T sample(*this, std::forward<Args>(args)...);
  2295. const auto it = ConstraintSet.find(&sample);
  2296. if (ConstraintSet.cend() != it) {
  2297. return static_cast<const T*>(*it);
  2298. }
  2299. ConstraintNodes.emplace(new T(std::move(sample)));
  2300. const auto ins = ConstraintSet.emplace(ConstraintNodes.top().get());
  2301. return static_cast<const T*>(*ins.first);
  2302. }
  2303. #undef ENSURE_NOT_DELETED
  2304. #undef ENSURE_NOT_FROZEN
  2305. #undef ENSURE_NOT_FROZEN_CTX
  2306. inline bool IsSameAnnotation(const TTypeAnnotationNode& left, const TTypeAnnotationNode& right) {
  2307. return &left == &right;
  2308. }
  2309. template <typename T, typename... Args>
  2310. const T* TExprContext::MakeType(Args&&... args) {
  2311. return TMakeTypeImpl<T>::Make(*this, std::forward<Args>(args)...);
  2312. }
  2313. struct TExprAnnotationFlags {
  2314. enum {
  2315. None = 0x00,
  2316. Position = 0x01,
  2317. Types = 0x02
  2318. };
  2319. };
  2320. ///////////////////////////////////////////////////////////////////////////////
  2321. // TNodeException
  2322. ///////////////////////////////////////////////////////////////////////////////
  2323. class TNodeException: public yexception {
  2324. public:
  2325. TNodeException();
  2326. explicit TNodeException(const TExprNode& node);
  2327. explicit TNodeException(const TExprNode* node);
  2328. explicit TNodeException(const TPositionHandle& pos);
  2329. inline const TPositionHandle& Pos() const {
  2330. return Pos_;
  2331. }
  2332. private:
  2333. const TPositionHandle Pos_;
  2334. };
  2335. bool CompileExpr(TAstNode& astRoot, TExprNode::TPtr& exprRoot, TExprContext& ctx,
  2336. IModuleResolver* resolver, IUrlListerManager* urlListerManager,
  2337. bool hasAnnotations = false, ui32 typeAnnotationIndex = Max<ui32>(), ui16 syntaxVersion = 0);
  2338. bool CompileExpr(TAstNode& astRoot, TExprNode::TPtr& exprRoot, TExprContext& ctx,
  2339. IModuleResolver* resolver, IUrlListerManager* urlListerManager,
  2340. ui32 annotationFlags, ui16 syntaxVersion = 0);
  2341. struct TLibraryCohesion {
  2342. TExportTable Exports;
  2343. TNodeMap<std::pair<TString, TString>> Imports;
  2344. };
  2345. bool CompileExpr(TAstNode& astRoot, TLibraryCohesion& cohesion, TExprContext& ctx, ui16 syntaxVersion = 0);
  2346. const TTypeAnnotationNode* CompileTypeAnnotation(const TAstNode& node, TExprContext& ctx);
  2347. // validate consistency of arguments and lambdas
  2348. void CheckArguments(const TExprNode& root);
  2349. void CheckCounts(const TExprNode& root);
  2350. // Compare expression trees and return first diffrent nodes.
  2351. bool CompareExprTrees(const TExprNode*& one, const TExprNode*& two);
  2352. bool CompareExprTreeParts(const TExprNode& one, const TExprNode& two, const TNodeMap<ui32>& argsMap);
  2353. TString MakeCacheKey(const TExprNode& root);
  2354. void GatherParents(const TExprNode& node, TParentsMap& parentsMap);
  2355. struct TConvertToAstSettings {
  2356. ui32 AnnotationFlags = 0;
  2357. bool RefAtoms = false;
  2358. std::function<bool(const TExprNode&)> NoInlineFunc;
  2359. bool PrintArguments = false;
  2360. bool AllowFreeArgs = false;
  2361. bool NormalizeAtomFlags = false;
  2362. IAllocator* Allocator = TDefaultAllocator::Instance();
  2363. };
  2364. TAstParseResult ConvertToAst(const TExprNode& root, TExprContext& ctx, const TConvertToAstSettings& settings);
  2365. // refAtoms allows omit copying of atom bodies - they will be referenced from expr graph
  2366. TAstParseResult ConvertToAst(const TExprNode& root, TExprContext& ctx, ui32 annotationFlags, bool refAtoms);
  2367. TExprNode::TListType GetLambdaBody(const TExprNode& lambda);
  2368. TString SubstParameters(const TString& str, const TMaybe<NYT::TNode>& params, TSet<TString>* usedNames);
  2369. const TTypeAnnotationNode* GetSeqItemType(const TTypeAnnotationNode* seq);
  2370. const TTypeAnnotationNode& GetSeqItemType(const TTypeAnnotationNode& seq);
  2371. const TTypeAnnotationNode& RemoveOptionality(const TTypeAnnotationNode& type);
  2372. TMaybe<TIssue> NormalizeName(TPosition position, TString& name);
  2373. TString NormalizeName(const TStringBuf& name);
  2374. } // namespace NYql
  2375. template<>
  2376. inline void Out<NYql::TTypeAnnotationNode>(
  2377. IOutputStream &out, const NYql::TTypeAnnotationNode& type)
  2378. {
  2379. type.Out(out);
  2380. }
  2381. #include "yql_expr_builder.inl"