hqlir.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  1. /*##############################################################################
  2. Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jstring.hpp"
  14. #include "jiface.hpp"
  15. #include "hqlir.hpp"
  16. //#define ADD_ACTIVE_SCOPE_AS_COMMENT
  17. namespace EclIR
  18. {
  19. /*
  20. The general format of IR is the following:
  21. <type> := <complex-type>
  22. | <simple-type>
  23. | %typelabel
  24. | <type> { annotation }
  25. | typeof <expr>
  26. complex-type
  27. := set '(' <type> ')'
  28. | dataset '(' record-type ')'
  29. | row '(' record-type ')'
  30. | grouped <type>
  31. ;
  32. <simple-type>
  33. := [u]int<n> // [unsigned] integer[n]
  34. | [u]swap<n> //
  35. | [u]packed
  36. | [e]str<n>
  37. | [u]dec(<n>[,m)
  38. | real<n>
  39. | vstr<n>
  40. | data<n>
  41. | uni<n>
  42. | vuni<n>
  43. | any
  44. | action
  45. | null
  46. ;
  47. %typelabel = 'type' <type>;
  48. <expression> := operator[#name][(sequence(n))][(arg1, arg2, ... argn) : <type>]
  49. | %exprlabel
  50. | <expression> { annotation };
  51. %exprlabel = <expression>;
  52. By convention the labels are generated as follows:
  53. Types
  54. %t<nnnnn> - name of a type
  55. %tr<nnnnn> - type of a record
  56. %tw<nnnnn> - type of a row
  57. %td<nnnnn> - type of a dataset
  58. Expressions
  59. %ds<nnnnn> - a dataset
  60. %rw<nnnnn> - a row
  61. %rc<nnnnn> - a record
  62. %dt<nnnnn> - a dictionary
  63. %e<nnnnn> - a general expression
  64. Where <nnnnn> is an auto incremented number.
  65. There are options on generating to
  66. a) Expand unshared annotations inline
  67. b) Expand simple operands inline (e.g., constants, attributes)
  68. c) Use #name as a synonym for attr:name
  69. d) Expand simple types inline instead of using type labels.
  70. The IR will be printed in logical order, with the operation being
  71. dumped at the end, and all its dependencies (and their dependencies)
  72. printed in dependency order, upwards.
  73. Note that operations do accept multiple types for each argument
  74. (think integerN, stringN, any numerical value, etc), so it's important
  75. to make sure the argument is compatible, but there's no way to
  76. enforce type safety with the current set of operators.
  77. Note that this code is intentionally not throwing exceptions or asserting,
  78. since it will be largely used during debug sessions (step through or
  79. core analysis) and debuggers are not that comfortable with calls throwing
  80. exceptions or asserting.
  81. In case where an invalid tree might be produced (missing information),
  82. the dumper should print "unknown_*", so that you can continue debugging
  83. your program, and open an issue to fix the dumper or the expression tree
  84. in separate.
  85. The test cases at the end of this file give some examples of the output that is expected.
  86. */
  87. // -------------------------------------------------------------------------------------------------------------------
  88. #define EXPAND_CASE(prefix,suffix) case prefix##_##suffix: return #suffix
  89. const char * getOperatorIRText(node_operator op)
  90. {
  91. switch(op)
  92. {
  93. EXPAND_CASE(no,none);
  94. EXPAND_CASE(no,scope);
  95. EXPAND_CASE(no,list);
  96. EXPAND_CASE(no,mul);
  97. EXPAND_CASE(no,div);
  98. EXPAND_CASE(no,modulus);
  99. EXPAND_CASE(no,negate);
  100. EXPAND_CASE(no,add);
  101. EXPAND_CASE(no,sub);
  102. EXPAND_CASE(no,eq);
  103. EXPAND_CASE(no,ne);
  104. EXPAND_CASE(no,lt);
  105. EXPAND_CASE(no,le);
  106. EXPAND_CASE(no,gt);
  107. EXPAND_CASE(no,ge);
  108. EXPAND_CASE(no,not);
  109. EXPAND_CASE(no,notnot);
  110. EXPAND_CASE(no,and);
  111. EXPAND_CASE(no,or);
  112. EXPAND_CASE(no,xor);
  113. EXPAND_CASE(no,concat);
  114. EXPAND_CASE(no,notin);
  115. EXPAND_CASE(no,in);
  116. EXPAND_CASE(no,notbetween);
  117. EXPAND_CASE(no,between);
  118. EXPAND_CASE(no,comma);
  119. EXPAND_CASE(no,count);
  120. EXPAND_CASE(no,countgroup);
  121. EXPAND_CASE(no,selectmap);
  122. EXPAND_CASE(no,exists);
  123. EXPAND_CASE(no,within);
  124. EXPAND_CASE(no,notwithin);
  125. EXPAND_CASE(no,param);
  126. EXPAND_CASE(no,constant);
  127. EXPAND_CASE(no,field);
  128. EXPAND_CASE(no,map);
  129. EXPAND_CASE(no,if);
  130. EXPAND_CASE(no,max);
  131. EXPAND_CASE(no,min);
  132. EXPAND_CASE(no,sum);
  133. EXPAND_CASE(no,ave);
  134. EXPAND_CASE(no,maxgroup);
  135. EXPAND_CASE(no,mingroup);
  136. EXPAND_CASE(no,sumgroup);
  137. EXPAND_CASE(no,avegroup);
  138. EXPAND_CASE(no,exp);
  139. EXPAND_CASE(no,power);
  140. EXPAND_CASE(no,round);
  141. EXPAND_CASE(no,roundup);
  142. EXPAND_CASE(no,range);
  143. EXPAND_CASE(no,rangeto);
  144. EXPAND_CASE(no,rangefrom);
  145. EXPAND_CASE(no,substring);
  146. EXPAND_CASE(no,transform);
  147. EXPAND_CASE(no,rollup);
  148. EXPAND_CASE(no,iterate);
  149. EXPAND_CASE(no,hqlproject);
  150. EXPAND_CASE(no,assign);
  151. EXPAND_CASE(no,assignall);
  152. EXPAND_CASE(no,asstring);
  153. EXPAND_CASE(no,group);
  154. EXPAND_CASE(no,cogroup);
  155. EXPAND_CASE(no,cosort);
  156. EXPAND_CASE(no,truncate);
  157. EXPAND_CASE(no,ln);
  158. EXPAND_CASE(no,log10);
  159. EXPAND_CASE(no,sin);
  160. EXPAND_CASE(no,cos);
  161. EXPAND_CASE(no,tan);
  162. EXPAND_CASE(no,asin);
  163. EXPAND_CASE(no,acos);
  164. EXPAND_CASE(no,atan);
  165. EXPAND_CASE(no,atan2);
  166. EXPAND_CASE(no,sinh);
  167. EXPAND_CASE(no,cosh);
  168. EXPAND_CASE(no,tanh);
  169. EXPAND_CASE(no,sqrt);
  170. EXPAND_CASE(no,evaluate);
  171. EXPAND_CASE(no,choose);
  172. EXPAND_CASE(no,which);
  173. EXPAND_CASE(no,rejected);
  174. EXPAND_CASE(no,mapto);
  175. EXPAND_CASE(no,record);
  176. EXPAND_CASE(no,service);
  177. EXPAND_CASE(no,index);
  178. EXPAND_CASE(no,all);
  179. EXPAND_CASE(no,left);
  180. EXPAND_CASE(no,right);
  181. EXPAND_CASE(no,outofline);
  182. EXPAND_CASE(no,cast);
  183. EXPAND_CASE(no,implicitcast);
  184. EXPAND_CASE(no,once);
  185. EXPAND_CASE(no,csv);
  186. EXPAND_CASE(no,sql);
  187. EXPAND_CASE(no,thor);
  188. EXPAND_CASE(no,flat);
  189. EXPAND_CASE(no,pipe);
  190. EXPAND_CASE(no,mix);
  191. EXPAND_CASE(no,selectnth);
  192. EXPAND_CASE(no,stored);
  193. EXPAND_CASE(no,failure);
  194. EXPAND_CASE(no,success);
  195. EXPAND_CASE(no,recovery);
  196. EXPAND_CASE(no,external);
  197. EXPAND_CASE(no,funcdef);
  198. EXPAND_CASE(no,externalcall);
  199. EXPAND_CASE(no,wait);
  200. EXPAND_CASE(no,event);
  201. EXPAND_CASE(no,persist);
  202. EXPAND_CASE(no,buildindex);
  203. EXPAND_CASE(no,output);
  204. EXPAND_CASE(no,omitted);
  205. EXPAND_CASE(no,when);
  206. EXPAND_CASE(no,setconditioncode);
  207. EXPAND_CASE(no,priority);
  208. EXPAND_CASE(no,intformat);
  209. EXPAND_CASE(no,realformat);
  210. EXPAND_CASE(no,abs);
  211. EXPAND_CASE(no,nofold);
  212. EXPAND_CASE(no,table);
  213. EXPAND_CASE(no,keyindex);
  214. EXPAND_CASE(no,temptable);
  215. EXPAND_CASE(no,usertable);
  216. EXPAND_CASE(no,choosen);
  217. EXPAND_CASE(no,filter);
  218. EXPAND_CASE(no,fetch);
  219. EXPAND_CASE(no,join);
  220. EXPAND_CASE(no,joined);
  221. EXPAND_CASE(no,sort);
  222. EXPAND_CASE(no,sorted);
  223. EXPAND_CASE(no,sortlist);
  224. EXPAND_CASE(no,dedup);
  225. EXPAND_CASE(no,enth);
  226. EXPAND_CASE(no,sample);
  227. EXPAND_CASE(no,selectfields);
  228. EXPAND_CASE(no,persist_check);
  229. EXPAND_CASE(no,create_initializer);
  230. EXPAND_CASE(no,owned_ds);
  231. EXPAND_CASE(no,complex);
  232. EXPAND_CASE(no,assign_addfiles);
  233. EXPAND_CASE(no,debug_option_value);
  234. EXPAND_CASE(no,hash);
  235. EXPAND_CASE(no,hash32);
  236. EXPAND_CASE(no,hash64);
  237. EXPAND_CASE(no,crc);
  238. EXPAND_CASE(no,return_stmt);
  239. EXPAND_CASE(no,update);
  240. EXPAND_CASE(no,subsort);
  241. EXPAND_CASE(no,chooseds);
  242. EXPAND_CASE(no,alias);
  243. EXPAND_CASE(no,datasetfromdictionary);
  244. EXPAND_CASE(no,delayedscope);
  245. EXPAND_CASE(no,assertconcrete);
  246. EXPAND_CASE(no,unboundselect);
  247. EXPAND_CASE(no,id);
  248. EXPAND_CASE(no,orderedactionlist);
  249. EXPAND_CASE(no,dataset_from_transform);
  250. EXPAND_CASE(no,childquery);
  251. EXPAND_CASE(no,unknown);
  252. EXPAND_CASE(no,createdictionary);
  253. EXPAND_CASE(no,indict);
  254. EXPAND_CASE(no,countdict);
  255. EXPAND_CASE(no,any);
  256. EXPAND_CASE(no,existsdict);
  257. EXPAND_CASE(no,unused101);
  258. EXPAND_CASE(no,unused25);
  259. EXPAND_CASE(no,unused28);
  260. EXPAND_CASE(no,unused29);
  261. EXPAND_CASE(no,unused30);
  262. EXPAND_CASE(no,unused31);
  263. EXPAND_CASE(no,unused32);
  264. EXPAND_CASE(no,unused33);
  265. EXPAND_CASE(no,unused34);
  266. EXPAND_CASE(no,unused35);
  267. EXPAND_CASE(no,unused36);
  268. EXPAND_CASE(no,unused37);
  269. EXPAND_CASE(no,unused38);
  270. EXPAND_CASE(no,is_null);
  271. EXPAND_CASE(no,dataset_alias);
  272. EXPAND_CASE(no,unused40);
  273. EXPAND_CASE(no,unused41);
  274. EXPAND_CASE(no,unused52);
  275. EXPAND_CASE(no,trim);
  276. EXPAND_CASE(no,position);
  277. EXPAND_CASE(no,charlen);
  278. EXPAND_CASE(no,unused42);
  279. EXPAND_CASE(no,unused43);
  280. EXPAND_CASE(no,unused44);
  281. EXPAND_CASE(no,unused45);
  282. EXPAND_CASE(no,unused46);
  283. EXPAND_CASE(no,unused47);
  284. EXPAND_CASE(no,unused48);
  285. EXPAND_CASE(no,unused49);
  286. EXPAND_CASE(no,unused50);
  287. EXPAND_CASE(no,nullptr);
  288. EXPAND_CASE(no,sizeof);
  289. EXPAND_CASE(no,offsetof);
  290. EXPAND_CASE(no,current_date);
  291. EXPAND_CASE(no,current_time);
  292. EXPAND_CASE(no,current_timestamp);
  293. EXPAND_CASE(no,variable);
  294. EXPAND_CASE(no,libraryselect);
  295. EXPAND_CASE(no,case);
  296. EXPAND_CASE(no,band);
  297. EXPAND_CASE(no,bor);
  298. EXPAND_CASE(no,bxor);
  299. EXPAND_CASE(no,bnot);
  300. EXPAND_CASE(no,postinc);
  301. EXPAND_CASE(no,postdec);
  302. EXPAND_CASE(no,preinc);
  303. EXPAND_CASE(no,predec);
  304. EXPAND_CASE(no,pselect);
  305. EXPAND_CASE(no,address);
  306. EXPAND_CASE(no,deref);
  307. EXPAND_CASE(no,order);
  308. EXPAND_CASE(no,hint);
  309. EXPAND_CASE(no,attr);
  310. EXPAND_CASE(no,self);
  311. EXPAND_CASE(no,rank);
  312. EXPAND_CASE(no,ranked);
  313. EXPAND_CASE(no,mergedscope);
  314. EXPAND_CASE(no,ordered);
  315. EXPAND_CASE(no,typetransfer);
  316. EXPAND_CASE(no,decimalstack);
  317. EXPAND_CASE(no,type);
  318. EXPAND_CASE(no,apply);
  319. EXPAND_CASE(no,ifblock);
  320. EXPAND_CASE(no,translated);
  321. EXPAND_CASE(no,addfiles);
  322. EXPAND_CASE(no,distribute);
  323. EXPAND_CASE(no,macro);
  324. EXPAND_CASE(no,cloned);
  325. EXPAND_CASE(no,cachealias);
  326. EXPAND_CASE(no,lshift);
  327. EXPAND_CASE(no,rshift);
  328. EXPAND_CASE(no,colon);
  329. EXPAND_CASE(no,setworkflow_cond);
  330. EXPAND_CASE(no,unused102);
  331. EXPAND_CASE(no,unused15);
  332. EXPAND_CASE(no,random);
  333. EXPAND_CASE(no,select);
  334. EXPAND_CASE(no,normalize);
  335. EXPAND_CASE(no,counter);
  336. EXPAND_CASE(no,distributed);
  337. EXPAND_CASE(no,grouped);
  338. EXPAND_CASE(no,denormalize);
  339. EXPAND_CASE(no,transformebcdic);
  340. EXPAND_CASE(no,transformascii);
  341. EXPAND_CASE(no,childdataset);
  342. EXPAND_CASE(no,envsymbol);
  343. EXPAND_CASE(no,null);
  344. EXPAND_CASE(no,quoted);
  345. EXPAND_CASE(no,bound_func);
  346. EXPAND_CASE(no,bound_type);
  347. EXPAND_CASE(no,metaactivity);
  348. EXPAND_CASE(no,fail);
  349. EXPAND_CASE(no,filepos);
  350. EXPAND_CASE(no,aggregate);
  351. EXPAND_CASE(no,distribution);
  352. EXPAND_CASE(no,newusertable);
  353. EXPAND_CASE(no,newaggregate);
  354. EXPAND_CASE(no,newtransform);
  355. EXPAND_CASE(no,fromunicode);
  356. EXPAND_CASE(no,tounicode);
  357. EXPAND_CASE(no,keyunicode);
  358. EXPAND_CASE(no,loadxml);
  359. EXPAND_CASE(no,isomitted);
  360. EXPAND_CASE(no,fieldmap);
  361. EXPAND_CASE(no,template_context);
  362. EXPAND_CASE(no,ensureresult);
  363. EXPAND_CASE(no,getresult);
  364. EXPAND_CASE(no,setresult);
  365. EXPAND_CASE(no,is_valid);
  366. EXPAND_CASE(no,alias_project);
  367. EXPAND_CASE(no,alias_scope);
  368. EXPAND_CASE(no,global);
  369. EXPAND_CASE(no,eventname);
  370. EXPAND_CASE(no,sequential);
  371. EXPAND_CASE(no,parallel);
  372. EXPAND_CASE(no,writespill);
  373. EXPAND_CASE(no,readspill);
  374. EXPAND_CASE(no,nolink);
  375. EXPAND_CASE(no,workflow);
  376. EXPAND_CASE(no,workflow_action);
  377. EXPAND_CASE(no,commonspill);
  378. EXPAND_CASE(no,choosesets);
  379. EXPAND_CASE(no,regex_find);
  380. EXPAND_CASE(no,regex_replace);
  381. EXPAND_CASE(no,workunit_dataset);
  382. EXPAND_CASE(no,failcode);
  383. EXPAND_CASE(no,failmessage);
  384. EXPAND_CASE(no,independent);
  385. EXPAND_CASE(no,keyed);
  386. EXPAND_CASE(no,compound);
  387. EXPAND_CASE(no,checkpoint);
  388. EXPAND_CASE(no,split);
  389. EXPAND_CASE(no,spill);
  390. EXPAND_CASE(no,subgraph);
  391. EXPAND_CASE(no,dependenton);
  392. EXPAND_CASE(no,setmeta);
  393. EXPAND_CASE(no,throughaggregate);
  394. EXPAND_CASE(no,joincount);
  395. EXPAND_CASE(no,merge_nomatch);
  396. EXPAND_CASE(no,countcompare);
  397. EXPAND_CASE(no,limit);
  398. EXPAND_CASE(no,evaluate_stmt);
  399. EXPAND_CASE(no,notify);
  400. EXPAND_CASE(no,parse);
  401. EXPAND_CASE(no,newparse);
  402. EXPAND_CASE(no,skip);
  403. EXPAND_CASE(no,matched);
  404. EXPAND_CASE(no,matchtext);
  405. EXPAND_CASE(no,matchlength);
  406. EXPAND_CASE(no,matchposition);
  407. EXPAND_CASE(no,pat_select);
  408. EXPAND_CASE(no,pat_const);
  409. EXPAND_CASE(no,pat_pattern);
  410. EXPAND_CASE(no,pat_follow);
  411. EXPAND_CASE(no,pat_first);
  412. EXPAND_CASE(no,pat_last);
  413. EXPAND_CASE(no,pat_repeat);
  414. EXPAND_CASE(no,pat_instance);
  415. EXPAND_CASE(no,pat_anychar);
  416. EXPAND_CASE(no,pat_token);
  417. EXPAND_CASE(no,pat_imptoken);
  418. EXPAND_CASE(no,pat_set);
  419. EXPAND_CASE(no,pat_checkin);
  420. EXPAND_CASE(no,pat_x_before_y);
  421. EXPAND_CASE(no,pat_x_after_y);
  422. EXPAND_CASE(no,xml);
  423. EXPAND_CASE(no,compound_fetch);
  424. EXPAND_CASE(no,pat_index);
  425. EXPAND_CASE(no,pat_beginpattern);
  426. EXPAND_CASE(no,pat_endpattern);
  427. EXPAND_CASE(no,pat_checklength);
  428. EXPAND_CASE(no,topn);
  429. EXPAND_CASE(no,outputscalar);
  430. EXPAND_CASE(no,matchunicode);
  431. EXPAND_CASE(no,pat_validate);
  432. EXPAND_CASE(no,unused83);
  433. EXPAND_CASE(no,existsgroup);
  434. EXPAND_CASE(no,pat_use);
  435. EXPAND_CASE(no,unused13);
  436. EXPAND_CASE(no,penalty);
  437. EXPAND_CASE(no,rowdiff);
  438. EXPAND_CASE(no,wuid);
  439. EXPAND_CASE(no,featuretype);
  440. EXPAND_CASE(no,pat_guard);
  441. EXPAND_CASE(no,xmltext);
  442. EXPAND_CASE(no,xmlunicode);
  443. EXPAND_CASE(no,newxmlparse);
  444. EXPAND_CASE(no,xmlparse);
  445. EXPAND_CASE(no,xmldecode);
  446. EXPAND_CASE(no,xmlencode);
  447. EXPAND_CASE(no,pat_featureparam);
  448. EXPAND_CASE(no,pat_featureactual);
  449. EXPAND_CASE(no,pat_featuredef);
  450. EXPAND_CASE(no,evalonce);
  451. EXPAND_CASE(no,unused14);
  452. EXPAND_CASE(no,merge);
  453. EXPAND_CASE(no,keyeddistribute);
  454. EXPAND_CASE(no,distributer);
  455. EXPAND_CASE(no,impure);
  456. EXPAND_CASE(no,attr_link);
  457. EXPAND_CASE(no,attr_expr);
  458. EXPAND_CASE(no,addsets);
  459. EXPAND_CASE(no,rowvalue);
  460. EXPAND_CASE(no,newkeyindex);
  461. EXPAND_CASE(no,pat_case);
  462. EXPAND_CASE(no,pat_nocase);
  463. EXPAND_CASE(no,activetable);
  464. EXPAND_CASE(no,preload);
  465. EXPAND_CASE(no,createset);
  466. EXPAND_CASE(no,assertkeyed);
  467. EXPAND_CASE(no,assertwild);
  468. EXPAND_CASE(no,recordlist);
  469. EXPAND_CASE(no,hashmd5);
  470. EXPAND_CASE(no,soapcall);
  471. EXPAND_CASE(no,soapcall_ds);
  472. EXPAND_CASE(no,newsoapcall);
  473. EXPAND_CASE(no,newsoapcall_ds);
  474. EXPAND_CASE(no,soapaction_ds);
  475. EXPAND_CASE(no,newsoapaction_ds);
  476. EXPAND_CASE(no,temprow);
  477. EXPAND_CASE(no,activerow);
  478. EXPAND_CASE(no,catch);
  479. EXPAND_CASE(no,unused80);
  480. EXPAND_CASE(no,reference);
  481. EXPAND_CASE(no,callback);
  482. EXPAND_CASE(no,keyedlimit);
  483. EXPAND_CASE(no,keydiff);
  484. EXPAND_CASE(no,keypatch);
  485. EXPAND_CASE(no,returnresult);
  486. EXPAND_CASE(no,id2blob);
  487. EXPAND_CASE(no,blob2id);
  488. EXPAND_CASE(no,anon);
  489. EXPAND_CASE(no,projectrow);
  490. EXPAND_CASE(no,embedbody);
  491. EXPAND_CASE(no,sortpartition);
  492. EXPAND_CASE(no,define);
  493. EXPAND_CASE(no,globalscope);
  494. EXPAND_CASE(no,forcelocal);
  495. EXPAND_CASE(no,typedef);
  496. EXPAND_CASE(no,matchattr);
  497. EXPAND_CASE(no,pat_production);
  498. EXPAND_CASE(no,guard);
  499. EXPAND_CASE(no,datasetfromrow);
  500. EXPAND_CASE(no,createrow);
  501. EXPAND_CASE(no,selfref);
  502. EXPAND_CASE(no,unicodeorder);
  503. EXPAND_CASE(no,assertconstant);
  504. EXPAND_CASE(no,compound_selectnew);
  505. EXPAND_CASE(no,nothor);
  506. EXPAND_CASE(no,newrow);
  507. EXPAND_CASE(no,clustersize);
  508. EXPAND_CASE(no,call);
  509. EXPAND_CASE(no,compound_diskread);
  510. EXPAND_CASE(no,compound_disknormalize);
  511. EXPAND_CASE(no,compound_diskaggregate);
  512. EXPAND_CASE(no,compound_diskcount);
  513. EXPAND_CASE(no,compound_diskgroupaggregate);
  514. EXPAND_CASE(no,compound_indexread);
  515. EXPAND_CASE(no,compound_indexnormalize);
  516. EXPAND_CASE(no,compound_indexaggregate);
  517. EXPAND_CASE(no,compound_indexcount);
  518. EXPAND_CASE(no,compound_indexgroupaggregate);
  519. EXPAND_CASE(no,compound_childread);
  520. EXPAND_CASE(no,compound_childnormalize);
  521. EXPAND_CASE(no,compound_childaggregate);
  522. EXPAND_CASE(no,compound_childcount);
  523. EXPAND_CASE(no,compound_childgroupaggregate);
  524. EXPAND_CASE(no,compound_inline);
  525. EXPAND_CASE(no,getgraphresult);
  526. EXPAND_CASE(no,setgraphresult);
  527. EXPAND_CASE(no,assert);
  528. EXPAND_CASE(no,assert_ds);
  529. EXPAND_CASE(no,namedactual);
  530. EXPAND_CASE(no,combine);
  531. EXPAND_CASE(no,rows);
  532. EXPAND_CASE(no,rollupgroup);
  533. EXPAND_CASE(no,regroup);
  534. EXPAND_CASE(no,combinegroup);
  535. EXPAND_CASE(no,inlinetable);
  536. EXPAND_CASE(no,transformlist);
  537. EXPAND_CASE(no,variance);
  538. EXPAND_CASE(no,covariance);
  539. EXPAND_CASE(no,correlation);
  540. EXPAND_CASE(no,vargroup);
  541. EXPAND_CASE(no,covargroup);
  542. EXPAND_CASE(no,corrgroup);
  543. EXPAND_CASE(no,denormalizegroup);
  544. EXPAND_CASE(no,xmlproject);
  545. EXPAND_CASE(no,spillgraphresult);
  546. EXPAND_CASE(no,enum);
  547. EXPAND_CASE(no,pat_or);
  548. EXPAND_CASE(no,loop);
  549. EXPAND_CASE(no,loopbody);
  550. EXPAND_CASE(no,cluster);
  551. EXPAND_CASE(no,forcenolocal);
  552. EXPAND_CASE(no,allnodes);
  553. EXPAND_CASE(no,unused6);
  554. EXPAND_CASE(no,matchrow);
  555. EXPAND_CASE(no,sequence);
  556. EXPAND_CASE(no,selfjoin);
  557. EXPAND_CASE(no,remotescope);
  558. EXPAND_CASE(no,privatescope);
  559. EXPAND_CASE(no,virtualscope);
  560. EXPAND_CASE(no,concretescope);
  561. EXPAND_CASE(no,purevirtual);
  562. EXPAND_CASE(no,internalselect);
  563. EXPAND_CASE(no,delayedselect);
  564. EXPAND_CASE(no,pure);
  565. EXPAND_CASE(no,libraryscope);
  566. EXPAND_CASE(no,libraryscopeinstance);
  567. EXPAND_CASE(no,libraryinput);
  568. EXPAND_CASE(no,pseudods);
  569. EXPAND_CASE(no,process);
  570. EXPAND_CASE(no,matchutf8);
  571. EXPAND_CASE(no,thisnode);
  572. EXPAND_CASE(no,graphloop);
  573. EXPAND_CASE(no,rowset);
  574. EXPAND_CASE(no,loopcounter);
  575. EXPAND_CASE(no,getgraphloopresult);
  576. EXPAND_CASE(no,setgraphloopresult);
  577. EXPAND_CASE(no,rowsetindex);
  578. EXPAND_CASE(no,rowsetrange);
  579. EXPAND_CASE(no,assertstepped);
  580. EXPAND_CASE(no,assertsorted);
  581. EXPAND_CASE(no,assertgrouped);
  582. EXPAND_CASE(no,assertdistributed);
  583. EXPAND_CASE(no,mergejoin);
  584. EXPAND_CASE(no,datasetlist);
  585. EXPAND_CASE(no,nwayjoin);
  586. EXPAND_CASE(no,nwaymerge);
  587. EXPAND_CASE(no,stepped);
  588. EXPAND_CASE(no,existslist);
  589. EXPAND_CASE(no,countlist);
  590. EXPAND_CASE(no,maxlist);
  591. EXPAND_CASE(no,minlist);
  592. EXPAND_CASE(no,sumlist);
  593. EXPAND_CASE(no,getgraphloopresultset);
  594. EXPAND_CASE(no,forwardscope);
  595. EXPAND_CASE(no,pat_before_y);
  596. EXPAND_CASE(no,pat_after_y);
  597. EXPAND_CASE(no,extractresult);
  598. EXPAND_CASE(no,attrname);
  599. EXPAND_CASE(no,nonempty);
  600. EXPAND_CASE(no,processing);
  601. EXPAND_CASE(no,filtergroup);
  602. EXPAND_CASE(no,rangecommon);
  603. EXPAND_CASE(no,section);
  604. EXPAND_CASE(no,nobody);
  605. EXPAND_CASE(no,deserialize);
  606. EXPAND_CASE(no,serialize);
  607. EXPAND_CASE(no,eclcrc);
  608. EXPAND_CASE(no,top);
  609. EXPAND_CASE(no,uncommoned_comma);
  610. EXPAND_CASE(no,nameof);
  611. EXPAND_CASE(no,catchds);
  612. EXPAND_CASE(no,file_logicalname);
  613. EXPAND_CASE(no,toxml);
  614. EXPAND_CASE(no,sectioninput);
  615. EXPAND_CASE(no,forcegraph);
  616. EXPAND_CASE(no,eventextra);
  617. EXPAND_CASE(no,unused81);
  618. EXPAND_CASE(no,related);
  619. EXPAND_CASE(no,executewhen);
  620. EXPAND_CASE(no,definesideeffect);
  621. EXPAND_CASE(no,callsideeffect);
  622. EXPAND_CASE(no,fromxml);
  623. EXPAND_CASE(no,actionlist);
  624. EXPAND_CASE(no,preservemeta);
  625. EXPAND_CASE(no,normalizegroup);
  626. EXPAND_CASE(no,indirect);
  627. EXPAND_CASE(no,selectindirect);
  628. EXPAND_CASE(no,nohoist);
  629. EXPAND_CASE(no,merge_pending);
  630. EXPAND_CASE(no,httpcall);
  631. EXPAND_CASE(no,getenv);
  632. }
  633. return "<unknown>";
  634. }
  635. const char * getTypeIRText(type_t type)
  636. {
  637. switch(type)
  638. {
  639. EXPAND_CASE(type,boolean);
  640. EXPAND_CASE(type,int);
  641. EXPAND_CASE(type,real);
  642. EXPAND_CASE(type,decimal);
  643. EXPAND_CASE(type,string);
  644. EXPAND_CASE(type,date);
  645. EXPAND_CASE(type,bitfield);
  646. EXPAND_CASE(type,char);
  647. EXPAND_CASE(type,enumerated);
  648. EXPAND_CASE(type,record);
  649. EXPAND_CASE(type,varstring);
  650. EXPAND_CASE(type,blob);
  651. EXPAND_CASE(type,data);
  652. EXPAND_CASE(type,pointer);
  653. EXPAND_CASE(type,class);
  654. EXPAND_CASE(type,array);
  655. EXPAND_CASE(type,table);
  656. EXPAND_CASE(type,set);
  657. EXPAND_CASE(type,row);
  658. EXPAND_CASE(type,groupedtable);
  659. EXPAND_CASE(type,void);
  660. EXPAND_CASE(type,alien);
  661. case type_swapint: return "swap";
  662. EXPAND_CASE(type,none);
  663. EXPAND_CASE(type,packedint);
  664. EXPAND_CASE(type,qstring);
  665. EXPAND_CASE(type,unicode);
  666. EXPAND_CASE(type,any);
  667. EXPAND_CASE(type,varunicode);
  668. EXPAND_CASE(type,pattern);
  669. EXPAND_CASE(type,rule);
  670. EXPAND_CASE(type,token);
  671. EXPAND_CASE(type,feature);
  672. EXPAND_CASE(type,event);
  673. EXPAND_CASE(type,null);
  674. EXPAND_CASE(type,scope);
  675. EXPAND_CASE(type,utf8);
  676. EXPAND_CASE(type,transform);
  677. EXPAND_CASE(type,ifblock);
  678. EXPAND_CASE(type,function);
  679. EXPAND_CASE(type,sortlist);
  680. EXPAND_CASE(type,dictionary);
  681. EXPAND_CASE(type,alias);
  682. case type_unused2:
  683. case type_unused3:
  684. case type_unused4:
  685. case type_unused5:
  686. return "unused";
  687. }
  688. return "<unknown>";
  689. }
  690. // -----------------------------------------------------------------
  691. // The class performing the building is responsible for mapping its internal representation to and from the types
  692. // returned by the builder interface
  693. typedef unsigned id_t;
  694. typedef id_t typeid_t;
  695. typedef id_t exprid_t;
  696. typedef UnsignedArray IdArray;
  697. inline type_t getRequiredTypeCode(node_operator op)
  698. {
  699. switch (op)
  700. {
  701. //These always have the same type
  702. case no_assign:
  703. case no_output:
  704. case no_sequential:
  705. case no_parallel:
  706. case no_apply:
  707. case no_actionlist:
  708. case no_orderedactionlist:
  709. return type_void;
  710. case no_attr:
  711. case no_attr_expr:
  712. case no_attr_link:
  713. case no_service:
  714. return type_null;
  715. //These must never have the type
  716. case no_record:
  717. case no_privatescope:
  718. case no_scope:
  719. case no_virtualscope:
  720. case no_concretescope:
  721. case no_remotescope:
  722. case no_libraryscope:
  723. case no_type:
  724. case no_libraryscopeinstance:
  725. return type_alias; // type is an alias if itself.
  726. }
  727. return type_none;
  728. }
  729. struct ConstantBuilderInfo
  730. {
  731. public:
  732. ConstantBuilderInfo() : tc(type_none), type(0) {}
  733. public:
  734. type_t tc;
  735. typeid_t type;
  736. union
  737. {
  738. unsigned __int64 intValue;
  739. double realValue;
  740. struct
  741. {
  742. size32_t size;
  743. const void * data;
  744. } dataValue;
  745. };
  746. };
  747. struct SimpleTypeBuilderInfo
  748. {
  749. public:
  750. SimpleTypeBuilderInfo() : length(0), isSigned(false), precision(0), locale(NULL) {}
  751. public:
  752. size32_t length;
  753. size32_t precision;
  754. bool isSigned;
  755. const char * locale; // locale or code page
  756. };
  757. struct CompoundTypeBuilderInfo
  758. {
  759. public:
  760. CompoundTypeBuilderInfo() : baseType(0) {}
  761. public:
  762. id_t baseType;
  763. };
  764. class TypeAnnotationBuilderInfo
  765. {
  766. public:
  767. TypeAnnotationBuilderInfo() : type(0), otherExpr(0) {}
  768. public:
  769. typeid_t type;
  770. exprid_t otherExpr;
  771. };
  772. class ExprBuilderInfo
  773. {
  774. public:
  775. ExprBuilderInfo() : type(0), name(NULL), id(NULL), sequence(0) {}
  776. inline void addOperand(exprid_t id) { args.append((unsigned)id); }
  777. public:
  778. typeid_t type;
  779. IAtom * name;
  780. IIdAtom * id;
  781. unsigned __int64 sequence;
  782. IdArray args;
  783. IdArray special;
  784. IdArray comment;
  785. };
  786. class ExprAnnotationBuilderInfo
  787. {
  788. public:
  789. ExprAnnotationBuilderInfo() : expr(0), name(NULL), value(0), line(0), col(0), warning(NULL), tree(NULL) {}
  790. public:
  791. exprid_t expr;
  792. const char * name;
  793. IECLError * warning;
  794. IPropertyTree * tree;
  795. unsigned value;
  796. unsigned line;
  797. unsigned col;
  798. IdArray args;
  799. };
  800. interface IEclBuilder
  801. {
  802. public:
  803. virtual typeid_t addSimpleType(type_t tc, const SimpleTypeBuilderInfo & info) = 0;
  804. virtual typeid_t addExprType(type_t tc, exprid_t expr) = 0;
  805. virtual typeid_t addCompoundType(type_t tc, const CompoundTypeBuilderInfo & info) = 0;
  806. virtual typeid_t addUnknownType(type_t tc) = 0;
  807. virtual typeid_t addTypeAnnotation(typemod_t kind, const TypeAnnotationBuilderInfo & info) = 0;
  808. virtual exprid_t addExpr(node_operator op, const ExprBuilderInfo & info) = 0;
  809. virtual exprid_t addConstantExpr(const ConstantBuilderInfo & info) = 0;
  810. virtual exprid_t addExprAnnotation(annotate_kind annot, const ExprAnnotationBuilderInfo & info) = 0;
  811. virtual void addReturn(exprid_t) = 0;
  812. };
  813. //--------------------------------------------------------------------------------------------------------------------
  814. //- Binary
  815. //--------------------------------------------------------------------------------------------------------------------
  816. class IdMapper
  817. {
  818. public:
  819. void addMapping(id_t src, id_t target)
  820. {
  821. while (targetIds.ordinality() < src)
  822. targetIds.append(0);
  823. if (targetIds.ordinality() > src)
  824. {
  825. assertex(targetIds.item(src) == 0);
  826. targetIds.replace(target, (unsigned)src);
  827. }
  828. else
  829. targetIds.append(target);
  830. }
  831. id_t lookup(id_t src) const
  832. {
  833. if (src == 0)
  834. return 0;
  835. assertex(targetIds.isItem(src));
  836. return targetIds.item(src);
  837. }
  838. protected:
  839. UnsignedArray targetIds;
  840. };
  841. //Hardly worth having as a base class at the moment - more for documentation
  842. class CIRPlayer
  843. {
  844. public:
  845. CIRPlayer(IEclBuilder * _target) : target(_target)
  846. {
  847. }
  848. protected:
  849. IEclBuilder * target;
  850. };
  851. class BinaryIRPlayer : public CIRPlayer
  852. {
  853. enum {
  854. IntLengthMask = 0x3f,
  855. SignedMask = 0x80,
  856. };
  857. //Needs to be in a binary format class so shared with writer
  858. enum
  859. {
  860. ElementNone,
  861. ElementType,
  862. ElementExpr,
  863. ElementReturn,
  864. };
  865. public:
  866. BinaryIRPlayer(ISimpleReadStream * _in, IEclBuilder * _target) : CIRPlayer(_target), in(_in), seq(0)
  867. {
  868. }
  869. void process();
  870. protected:
  871. bool processTypeOrExpr();
  872. exprid_t processExpr();
  873. typeid_t processType();
  874. void processReturn();
  875. //Processing for specific types.
  876. typeid_t readType();
  877. typeid_t readSimpleType(type_t tc);
  878. typeid_t readTableType();
  879. exprid_t readExpr();
  880. //Processing for expressions
  881. //Mapping helpers
  882. void addMapping(typeid_t srcId, typeid_t tgtId) { idMapper.addMapping(srcId, tgtId); }
  883. exprid_t mapExprToTarget(exprid_t srcId) const { return idMapper.lookup(srcId); }
  884. typeid_t mapTypeToTarget(typeid_t srcId) const { return idMapper.lookup(srcId); }
  885. protected:
  886. //Candidates for being wrapped in a class together with in
  887. template <class X>
  888. inline void read(X & x) const { in->read(sizeof(x), &x); }
  889. template <class X>
  890. void readPacked(X & value) const { read(value); }
  891. IIdAtom * readSymId();
  892. IAtom * readName();
  893. exprid_t readId() const;
  894. private:
  895. Owned<ISimpleReadStream> in;
  896. IdMapper idMapper;
  897. unsigned seq;
  898. };
  899. void BinaryIRPlayer::process()
  900. {
  901. seq = 1;
  902. loop
  903. {
  904. byte element;
  905. read(element);
  906. switch (element)
  907. {
  908. case ElementNone:
  909. return;
  910. case ElementType:
  911. processType();
  912. break;
  913. case ElementExpr:
  914. processExpr();
  915. break;
  916. case ElementReturn:
  917. processReturn();
  918. break;
  919. }
  920. }
  921. }
  922. typeid_t BinaryIRPlayer::processType()
  923. {
  924. typeid_t id = readType();
  925. addMapping(seq++, id);
  926. return id;
  927. }
  928. exprid_t BinaryIRPlayer::processExpr()
  929. {
  930. exprid_t id = readExpr();
  931. addMapping(seq++, id);
  932. return id;
  933. }
  934. exprid_t BinaryIRPlayer::readId() const
  935. {
  936. exprid_t id;
  937. readPacked(id);
  938. return mapExprToTarget(id);
  939. }
  940. //Functions for reading types and processing them
  941. typeid_t BinaryIRPlayer::readType()
  942. {
  943. type_t tc;
  944. read(tc);
  945. switch (tc)
  946. {
  947. case type_none:
  948. return 0;
  949. case type_alias:
  950. {
  951. unsigned refid;
  952. readPacked(refid);
  953. return mapTypeToTarget(refid);
  954. };
  955. case type_int:
  956. return readSimpleType(tc);
  957. case type_table:
  958. return readTableType();
  959. default:
  960. return target->addUnknownType(tc);
  961. }
  962. throwUnexpected();
  963. }
  964. typeid_t BinaryIRPlayer::readSimpleType(type_t tc)
  965. {
  966. SimpleTypeBuilderInfo info;
  967. switch (tc)
  968. {
  969. case type_int:
  970. {
  971. //MORE: Worry about packing the binary representation later
  972. //(or should it be done by passing through zip)
  973. read(info.length);
  974. read(info.isSigned);
  975. break;
  976. }
  977. case type_data:
  978. readPacked(info.length);
  979. break;
  980. default:
  981. throwUnexpected();
  982. }
  983. return target->addSimpleType(tc, info);
  984. }
  985. typeid_t BinaryIRPlayer::readTableType()
  986. {
  987. UNIMPLEMENTED;
  988. return 0;
  989. }
  990. //Functions for reading expressions and processing them
  991. exprid_t BinaryIRPlayer::readExpr()
  992. {
  993. node_operator op;
  994. read(op);
  995. if (op == no_none)
  996. return 0;
  997. ExprBuilderInfo info;
  998. info.type = readId();
  999. info.id = readSymId();
  1000. info.name = readName();
  1001. loop
  1002. {
  1003. exprid_t id = readId();
  1004. if (!id)
  1005. break;
  1006. info.addOperand(id);
  1007. }
  1008. return target->addExpr(op, info);
  1009. }
  1010. void BinaryIRPlayer::processReturn()
  1011. {
  1012. exprid_t id = readId();
  1013. target->addReturn(id);
  1014. }
  1015. IIdAtom * BinaryIRPlayer::readSymId()
  1016. {
  1017. return NULL;
  1018. }
  1019. IAtom * BinaryIRPlayer::readName()
  1020. {
  1021. return NULL;
  1022. }
  1023. //MORE: Compress the binary format
  1024. //--------------------------------------------------------------------------------------------------------------------
  1025. //- Text
  1026. //--------------------------------------------------------------------------------------------------------------------
  1027. enum
  1028. {
  1029. TIRexpandSimpleTypes = 0x00000001,
  1030. TIRexpandAttributes = 0x00000002,
  1031. TIRstripAnnotatations = 0x00000004,
  1032. };
  1033. class TextIRBuilder : public CInterfaceOf<IEclBuilder>
  1034. {
  1035. class Definition
  1036. {
  1037. public:
  1038. Definition(const char * prefix, id_t _id, bool expandInline) : id(_id)
  1039. {
  1040. if (!expandInline)
  1041. idText.append("%").append(prefix).append(id);
  1042. }
  1043. inline bool expandInline() const { return idText.length() == 0; }
  1044. public:
  1045. StringBuffer idText;
  1046. id_t id;
  1047. };
  1048. public:
  1049. TextIRBuilder(unsigned _options) : options(_options) {}
  1050. virtual typeid_t addSimpleType(type_t tc, const SimpleTypeBuilderInfo & info)
  1051. {
  1052. bool expandInline = (options & TIRexpandSimpleTypes) != 0;
  1053. Definition def("t", nextId(), expandInline);
  1054. startDefinition(def, "type");
  1055. appendTypeText(tc, info);
  1056. finishDefinition(def);
  1057. return def.id;
  1058. }
  1059. virtual typeid_t addExprType(type_t tc, exprid_t expr)
  1060. {
  1061. Definition def("t", nextId(), false);
  1062. startDefinition(def, "type");
  1063. line.append(getTypeIRText(tc)).append("(");
  1064. appendId(expr).append(")");
  1065. finishDefinition(def);
  1066. return def.id;
  1067. }
  1068. virtual typeid_t addCompoundType(type_t tc, const CompoundTypeBuilderInfo & info)
  1069. {
  1070. Definition def("t", nextId(), false);
  1071. startDefinition(def, "type");
  1072. line.append(getTypeIRText(tc)).append("(");
  1073. appendId(info.baseType).append(")");
  1074. finishDefinition(def);
  1075. return def.id;
  1076. }
  1077. virtual typeid_t addUnknownType(type_t tc)
  1078. {
  1079. Definition def("t", nextId(), false);
  1080. startDefinition(def, "type");
  1081. line.append("unknown:").append(getTypeIRText(tc));
  1082. finishDefinition(def);
  1083. return def.id;
  1084. }
  1085. virtual typeid_t addTypeAnnotation(typemod_t kind, const TypeAnnotationBuilderInfo & info)
  1086. {
  1087. if (options & TIRstripAnnotatations)
  1088. return info.type;
  1089. Definition def("t", nextId(), false);
  1090. startDefinition(def, "type");
  1091. appendId(info.type).append(" {");
  1092. switch (kind)
  1093. {
  1094. case typemod_const:
  1095. line.append("const");
  1096. break;
  1097. case typemod_ref:
  1098. line.append("reference");
  1099. break;
  1100. case typemod_wrapper:
  1101. line.append("wrapper");
  1102. break;
  1103. case typemod_builder:
  1104. line.append("builder");
  1105. break;
  1106. case typemod_original:
  1107. line.append("original(");
  1108. appendId(info.otherExpr);
  1109. line.append(")");
  1110. break;
  1111. case typemod_member:
  1112. line.append("member");
  1113. break;
  1114. case typemod_serialized:
  1115. line.append("serialized");
  1116. break;
  1117. case typemod_outofline:
  1118. line.append("outofline");
  1119. break;
  1120. case typemod_attr:
  1121. line.append("attr(");
  1122. appendId(info.otherExpr);
  1123. line.append(")");
  1124. break;
  1125. case typemod_indirect:
  1126. line.append("indirect(");
  1127. appendId(info.otherExpr);
  1128. line.append(")");
  1129. break;
  1130. }
  1131. line.append("}");
  1132. finishDefinition(def);
  1133. return def.id;
  1134. }
  1135. virtual exprid_t addExpr(node_operator op, const ExprBuilderInfo & info)
  1136. {
  1137. bool expandInline = false;
  1138. const char * prefix = "e";
  1139. switch (op)
  1140. {
  1141. case no_attr:
  1142. case no_attr_expr:
  1143. expandInline = (options & TIRexpandAttributes) != 0;
  1144. break;
  1145. case no_assign:
  1146. prefix = "as";
  1147. break;
  1148. }
  1149. Definition def(prefix, nextId(), expandInline);
  1150. startDefinition(def, NULL);
  1151. appendExprText(op, info);
  1152. finishDefinition(def);
  1153. return def.id;
  1154. }
  1155. virtual exprid_t addConstantExpr(const ConstantBuilderInfo & info)
  1156. {
  1157. Definition def("c", nextId(), false);
  1158. startDefinition(def, NULL);
  1159. line.append("constant ");
  1160. appendConstantText(info);
  1161. finishDefinition(def);
  1162. return def.id;
  1163. }
  1164. virtual exprid_t addExprAnnotation(annotate_kind annot, const ExprAnnotationBuilderInfo & info)
  1165. {
  1166. if (options & TIRstripAnnotatations)
  1167. return info.expr;
  1168. Definition def("e", nextId(), false);
  1169. startDefinition(def, NULL);
  1170. appendId(info.expr);
  1171. line.append(" {");
  1172. switch (annot)
  1173. {
  1174. case annotate_symbol:
  1175. line.append("symbol ").append(info.name);
  1176. if (info.value & ob_exported)
  1177. line.append(" exported");
  1178. else if (info.value & ob_shared)
  1179. line.append(" shared");
  1180. if (info.value & ob_virtual)
  1181. line.append(" virtual");
  1182. if (info.line)
  1183. line.append("@").append(info.line);
  1184. break;
  1185. case annotate_location:
  1186. line.append("location '").append(info.name);
  1187. line.append("'");
  1188. if (info.line)
  1189. {
  1190. line.append(",").append(info.line);
  1191. if (info.col)
  1192. line.append(",").append(info.col);
  1193. }
  1194. break;
  1195. case annotate_meta:
  1196. {
  1197. line.append("meta(");
  1198. ForEachItemIn(i, info.args)
  1199. {
  1200. if (i!= 0)
  1201. line.append(",");
  1202. appendId(info.args.item(i));
  1203. }
  1204. line.append(")");
  1205. break;
  1206. }
  1207. case annotate_warning:
  1208. {
  1209. IECLError * warning = info.warning;
  1210. StringBuffer msg;
  1211. warning->errorMessage(msg);
  1212. const char * filename = warning->getFilename();
  1213. line.append("warning ");
  1214. line.append("(");
  1215. if (filename)
  1216. {
  1217. line.append("'");
  1218. appendStringAsCPP(line, strlen(filename), filename, false);
  1219. line.append("'");
  1220. }
  1221. line.append(",").append(warning->getLine());
  1222. line.append(",").append(warning->getColumn());
  1223. line.append(",").append(warning->errorCode());
  1224. line.append(",'");
  1225. appendStringAsCPP(line, msg.length(), msg.str(), false);
  1226. line.append("'");
  1227. line.append(",").append(warning->errorAudience());
  1228. line.append(",").append((unsigned)warning->getSeverity());
  1229. line.append(")");
  1230. break;
  1231. }
  1232. case annotate_parsemeta:
  1233. line.append("parsemeta");
  1234. break;
  1235. case annotate_javadoc:
  1236. line.append("javadoc ");
  1237. toXML(info.tree, line);
  1238. break;
  1239. default:
  1240. line.append("unknown");
  1241. break;
  1242. }
  1243. line.append("}");
  1244. finishDefinition(def);
  1245. return def.id;
  1246. }
  1247. virtual void addReturn(exprid_t id)
  1248. {
  1249. line.append("return ");
  1250. appendId(id);
  1251. finishLine();
  1252. }
  1253. protected:
  1254. inline id_t nextId() const { return ids.ordinality()+1; }
  1255. id_t createId(const char * prefix)
  1256. {
  1257. id_t nextId = ids.ordinality()+1;
  1258. StringBuffer idText;
  1259. idText.append("%").append(prefix).append(nextId);
  1260. ids.append(idText.str());
  1261. return nextId;
  1262. }
  1263. id_t addId(const char * text)
  1264. {
  1265. id_t nextId = ids.ordinality()+1;
  1266. ids.append(text);
  1267. return nextId;
  1268. }
  1269. void appendTypeText(type_t tc, const SimpleTypeBuilderInfo & info)
  1270. {
  1271. const char * irText = getTypeIRText(tc);
  1272. switch (tc)
  1273. {
  1274. case type_boolean:
  1275. case type_date:
  1276. case type_char:
  1277. case type_event:
  1278. case type_null:
  1279. case type_void:
  1280. case type_sortlist:
  1281. case type_any:
  1282. line.append(irText);
  1283. return;
  1284. case type_int:
  1285. case type_swapint:
  1286. {
  1287. if (!info.isSigned)
  1288. line.append("u");
  1289. line.append(irText);
  1290. line.append(info.length);
  1291. return;
  1292. }
  1293. case type_real:
  1294. case type_data:
  1295. case type_qstring:
  1296. line.append(irText);
  1297. if (info.length != UNKNOWN_LENGTH)
  1298. line.append(info.length);
  1299. return;
  1300. case type_decimal:
  1301. case type_packedint:
  1302. case type_bitfield:
  1303. case type_enumerated:
  1304. return;
  1305. case type_string:
  1306. case type_unicode:
  1307. case type_utf8:
  1308. case type_varstring:
  1309. line.append(irText);
  1310. if (info.length != UNKNOWN_LENGTH)
  1311. line.append(info.length);
  1312. line.append("(").append(info.locale).append(")");
  1313. return;
  1314. }
  1315. line.append(irText).append("(").append(info.length).append(",").append(info.precision).append(",").append(info.isSigned).append(")");
  1316. }
  1317. void appendExprText(node_operator op, const ExprBuilderInfo & info)
  1318. {
  1319. line.append(getOperatorIRText(op));
  1320. if (info.id)
  1321. line.append("#").append(info.id->str());
  1322. else if (info.name)
  1323. line.append("#").append(info.name->str());
  1324. if (info.sequence)
  1325. line.append("[seq(").append(info.sequence).append(")]");
  1326. if (info.args.ordinality())
  1327. {
  1328. line.append("(");
  1329. ForEachItemIn(i, info.args)
  1330. {
  1331. if (i)
  1332. line.append(",");
  1333. appendId(info.args.item(i));
  1334. }
  1335. line.append(")");
  1336. }
  1337. if (info.special.ordinality())
  1338. {
  1339. line.append("[");
  1340. ForEachItemIn(i, info.special)
  1341. {
  1342. if (i)
  1343. line.append(",");
  1344. appendId(info.special.item(i));
  1345. }
  1346. line.append("]");
  1347. }
  1348. type_t tc = getRequiredTypeCode(op);
  1349. if (tc == type_none)
  1350. {
  1351. line.append(" : ");
  1352. appendId(info.type);
  1353. }
  1354. if (info.comment.ordinality())
  1355. {
  1356. line.append(" // ");
  1357. ForEachItemIn(i, info.comment)
  1358. {
  1359. if (i)
  1360. line.append(",");
  1361. appendId(info.comment.item(i));
  1362. }
  1363. }
  1364. }
  1365. void appendConstantText(const ConstantBuilderInfo & info)
  1366. {
  1367. switch (info.tc)
  1368. {
  1369. case type_boolean:
  1370. line.append(info.intValue ? "true" : "false");
  1371. break;
  1372. case type_int:
  1373. case type_swapint:
  1374. {
  1375. if (true)//info.isSigned)
  1376. line.append((__int64)info.intValue);
  1377. else
  1378. line.append((unsigned __int64)info.intValue);
  1379. break;
  1380. }
  1381. case type_real:
  1382. {
  1383. line.append(info.realValue);
  1384. break;
  1385. }
  1386. case type_decimal:
  1387. case type_packedint:
  1388. case type_string:
  1389. case type_bitfield:
  1390. case type_enumerated:
  1391. case type_varstring:
  1392. case type_data:
  1393. case type_qstring:
  1394. {
  1395. line.append("D");
  1396. appendStringAsQuotedCPP(line, info.dataValue.size, (const char *)info.dataValue.data, false);
  1397. break;
  1398. }
  1399. }
  1400. line.append(" : ");
  1401. appendId(info.type);
  1402. }
  1403. StringBuffer & appendId(id_t id)
  1404. {
  1405. if (id)
  1406. line.append(ids.item((unsigned)id-1));
  1407. else
  1408. line.append("<null>");
  1409. return line;
  1410. }
  1411. void startDefinition(Definition & def, const char * prefix)
  1412. {
  1413. if (!def.expandInline())
  1414. {
  1415. line.append(def.idText).append(" = ");
  1416. if (prefix)
  1417. line.append(prefix).append(" ");
  1418. }
  1419. }
  1420. void finishDefinition(Definition & def)
  1421. {
  1422. assertex(def.id == ids.ordinality()+1);
  1423. if (!def.expandInline())
  1424. {
  1425. ids.append(def.idText);
  1426. finishLine();
  1427. }
  1428. else
  1429. ids.append(line.str());
  1430. line.clear();
  1431. }
  1432. virtual void finishLine() = 0;
  1433. protected:
  1434. unsigned options;
  1435. StringBuffer line;
  1436. StringArray ids;
  1437. };
  1438. class StringTextIRBuilder : public TextIRBuilder
  1439. {
  1440. public:
  1441. const char * queryText() { return line.str(); }
  1442. protected:
  1443. virtual void finishLine()
  1444. {
  1445. if (line.length())
  1446. {
  1447. line.append(";");
  1448. output.append(line);
  1449. line.clear();
  1450. }
  1451. }
  1452. protected:
  1453. StringBuffer output;
  1454. };
  1455. class DblgLogIRBuilder : public TextIRBuilder
  1456. {
  1457. public:
  1458. DblgLogIRBuilder(unsigned _options) : TextIRBuilder(_options) {}
  1459. protected:
  1460. virtual void finishLine()
  1461. {
  1462. if (line.length())
  1463. {
  1464. DBGLOG("%s;", line.str());
  1465. line.clear();
  1466. }
  1467. }
  1468. };
  1469. class FileIRBuilder : public TextIRBuilder
  1470. {
  1471. public:
  1472. FileIRBuilder(unsigned _options, FILE * _file) : TextIRBuilder(_options), file(_file) {}
  1473. protected:
  1474. virtual void finishLine()
  1475. {
  1476. if (line.length())
  1477. {
  1478. fputs(line.str(), file);
  1479. fputs(";\n", file);
  1480. line.clear();
  1481. }
  1482. }
  1483. protected:
  1484. FILE * file;
  1485. };
  1486. class StringBufferIRBuilder : public TextIRBuilder
  1487. {
  1488. public:
  1489. StringBufferIRBuilder(StringBuffer & _target, unsigned _options) : TextIRBuilder(_options), target(_target) {}
  1490. protected:
  1491. virtual void finishLine()
  1492. {
  1493. if (line.length())
  1494. {
  1495. target.append(line).append(";\n");
  1496. line.clear();
  1497. }
  1498. }
  1499. protected:
  1500. StringBuffer & target;
  1501. };
  1502. class StringArrayIRBuilder : public TextIRBuilder
  1503. {
  1504. public:
  1505. StringArrayIRBuilder(StringArray & _target, unsigned _options) : TextIRBuilder(_options), target(_target) {}
  1506. protected:
  1507. virtual void finishLine()
  1508. {
  1509. if (line.length())
  1510. {
  1511. line.append(";");
  1512. target.append(line.str());
  1513. line.clear();
  1514. }
  1515. }
  1516. protected:
  1517. StringArray & target;
  1518. };
  1519. //--------------------------------------------------------------------------------------------------------------------
  1520. //- XML
  1521. //--------------------------------------------------------------------------------------------------------------------
  1522. //--------------------------------------------------------------------------------------------------------------------
  1523. //--------------------------------------------------------------------------------------------------------------------
  1524. //- Expression trees
  1525. //--------------------------------------------------------------------------------------------------------------------
  1526. class ExpressionIRBuilder : public CInterfaceOf<IEclBuilder>
  1527. {
  1528. public:
  1529. virtual typeid_t addSimpleType(type_t tc, const SimpleTypeBuilderInfo & info)
  1530. {
  1531. return 0;
  1532. }
  1533. virtual typeid_t addExprType(type_t tc, exprid_t expr)
  1534. {
  1535. return 0;
  1536. }
  1537. virtual typeid_t addCompoundType(type_t tc, const CompoundTypeBuilderInfo & info)
  1538. {
  1539. return 0;
  1540. }
  1541. virtual typeid_t addUnknownType(type_t tc)
  1542. {
  1543. return saveItem(makeNullType());
  1544. }
  1545. virtual typeid_t addTypeAnnotation(typemod_t kind, const TypeAnnotationBuilderInfo & info)
  1546. {
  1547. return 0;
  1548. }
  1549. virtual exprid_t addExpr(node_operator op, const ExprBuilderInfo & info)
  1550. {
  1551. return 0;
  1552. }
  1553. virtual exprid_t addConstantExpr(const ConstantBuilderInfo & info)
  1554. {
  1555. UNIMPLEMENTED;
  1556. return 0;
  1557. }
  1558. virtual exprid_t addExprAnnotation(annotate_kind annot, const ExprAnnotationBuilderInfo & info)
  1559. {
  1560. UNIMPLEMENTED;
  1561. return 0;
  1562. }
  1563. virtual void addReturn(exprid_t id)
  1564. {
  1565. IHqlExpression * expr = idToExpr(id);
  1566. results.append(*LINK(expr));
  1567. }
  1568. IHqlExpression * getExpression() { return createCompound(results); }
  1569. protected:
  1570. id_t saveItem(IInterface * next)
  1571. {
  1572. values.append(*next);
  1573. return values.ordinality();
  1574. }
  1575. IHqlExpression * idToExpr(id_t id)
  1576. {
  1577. if (id == 0)
  1578. return NULL;
  1579. IInterface & cur = values.item(id-1);
  1580. return static_cast<IHqlExpression *>(&cur);
  1581. }
  1582. protected:
  1583. Array values;
  1584. HqlExprArray results;
  1585. };
  1586. //--------------------------------------------------------------------------------------------------------------------
  1587. class ExpressionId : public CInterfaceOf<IInterface>
  1588. {
  1589. public:
  1590. ExpressionId(id_t _id) : id(_id) {}
  1591. public:
  1592. const id_t id;
  1593. };
  1594. class ExpressionIRPlayer : public CIRPlayer
  1595. {
  1596. public:
  1597. ExpressionIRPlayer(IEclBuilder * _target) : CIRPlayer(_target), seq(0)
  1598. {
  1599. lockTransformMutex();
  1600. }
  1601. ~ExpressionIRPlayer()
  1602. {
  1603. unlockTransformMutex();
  1604. }
  1605. void play(IHqlExpression * expr);
  1606. void play(ITypeInfo * type);
  1607. protected:
  1608. id_t processType(ITypeInfo * type);
  1609. id_t doProcessType(ITypeInfo * expr);
  1610. id_t processExpr(IHqlExpression * expr);
  1611. id_t doProcessExpr(IHqlExpression * expr);
  1612. id_t doProcessConstant(IHqlExpression * expr);
  1613. id_t doProcessAnnotation(IHqlExpression * expr);
  1614. protected:
  1615. CopyArray types;
  1616. UnsignedArray typeIds;
  1617. unsigned seq;
  1618. };
  1619. void ExpressionIRPlayer::play(IHqlExpression * expr)
  1620. {
  1621. id_t id = processExpr(expr);
  1622. target->addReturn(id);
  1623. }
  1624. void ExpressionIRPlayer::play(ITypeInfo * type)
  1625. {
  1626. id_t id = processType(type);
  1627. target->addReturn(id);
  1628. }
  1629. //----
  1630. id_t ExpressionIRPlayer::processType(ITypeInfo * type)
  1631. {
  1632. if (!type)
  1633. return 0;
  1634. unsigned match = types.find(*type);
  1635. if (match != NotFound)
  1636. return typeIds.item(match);
  1637. id_t nextId = doProcessType(type);
  1638. types.append(*type);
  1639. typeIds.append(nextId);
  1640. return nextId;
  1641. }
  1642. id_t ExpressionIRPlayer::doProcessType(ITypeInfo * type)
  1643. {
  1644. typemod_t mod = type->queryModifier();
  1645. if (mod == typemod_none)
  1646. {
  1647. type_t tc = type->getTypeCode();
  1648. SimpleTypeBuilderInfo info;
  1649. switch (tc)
  1650. {
  1651. case type_boolean:
  1652. case type_date:
  1653. case type_char:
  1654. case type_event:
  1655. case type_null:
  1656. case type_void:
  1657. case type_sortlist:
  1658. case type_any:
  1659. break;
  1660. case type_int:
  1661. case type_swapint:
  1662. case type_real:
  1663. case type_decimal:
  1664. case type_packedint:
  1665. info.length = type->getSize();
  1666. info.isSigned = type->isSigned();
  1667. info.precision = type->getPrecision();
  1668. break;
  1669. case type_unicode:
  1670. case type_varunicode:
  1671. case type_utf8:
  1672. info.length = type->getStringLen();
  1673. info.locale = type->queryLocale()->str();
  1674. break;;
  1675. case type_string:
  1676. case type_varstring:
  1677. info.length = type->getStringLen();
  1678. info.locale = type->queryCharset()->queryName()->str();
  1679. break;
  1680. case type_bitfield:
  1681. return target->addUnknownType(tc);
  1682. case type_record:
  1683. case type_scope:
  1684. case type_alien:
  1685. case type_enumerated:
  1686. return target->addExprType(tc, processExpr(queryExpression(type)));
  1687. case type_data:
  1688. case type_qstring:
  1689. info.length = type->getStringLen();
  1690. break;
  1691. case type_set:
  1692. case type_row:
  1693. case type_pattern:
  1694. case type_rule:
  1695. case type_token:
  1696. case type_transform:
  1697. case type_table: // more??
  1698. case type_groupedtable:
  1699. case type_dictionary:
  1700. case type_function:
  1701. case type_pointer:
  1702. {
  1703. CompoundTypeBuilderInfo info;
  1704. info.baseType = processType(type->queryChildType());
  1705. return target->addCompoundType(tc, info);
  1706. }
  1707. case type_feature:
  1708. return target->addUnknownType(tc);
  1709. case type_none:
  1710. case type_ifblock:
  1711. case type_alias:
  1712. case type_blob:
  1713. case type_class:
  1714. case type_array:
  1715. throwUnexpected();
  1716. break;
  1717. default:
  1718. UNIMPLEMENTED;
  1719. }
  1720. return target->addSimpleType(tc, info);
  1721. }
  1722. else
  1723. {
  1724. TypeAnnotationBuilderInfo info;
  1725. info.type = processType(type->queryTypeBase());
  1726. switch (mod)
  1727. {
  1728. case typemod_original:
  1729. case typemod_attr:
  1730. case typemod_indirect:
  1731. {
  1732. IHqlExpression * expr = static_cast<IHqlExpression *>(type->queryModifierExtra());
  1733. info.otherExpr = processExpr(expr);
  1734. break;
  1735. }
  1736. }
  1737. return target->addTypeAnnotation(mod, info);
  1738. }
  1739. }
  1740. //----
  1741. id_t ExpressionIRPlayer::processExpr(IHqlExpression * expr)
  1742. {
  1743. if (!expr)
  1744. return 0;
  1745. IInterface * match = expr->queryTransformExtra();
  1746. if (match)
  1747. {
  1748. //Check for recursion
  1749. if (match == expr)
  1750. throwUnexpected();
  1751. return static_cast<ExpressionId *>(match)->id;
  1752. }
  1753. expr->setTransformExtraUnlinked(expr);
  1754. id_t nextId = doProcessExpr(expr);
  1755. expr->setTransformExtraOwned(new ExpressionId(nextId));
  1756. return nextId;
  1757. }
  1758. id_t ExpressionIRPlayer::doProcessExpr(IHqlExpression * expr)
  1759. {
  1760. IHqlExpression * body = expr->queryBody(true);
  1761. if (body != expr)
  1762. return doProcessAnnotation(expr);
  1763. node_operator op = expr->getOperator();
  1764. switch (op)
  1765. {
  1766. case no_constant:
  1767. return doProcessConstant(expr);
  1768. }
  1769. ExprBuilderInfo info;
  1770. switch (op)
  1771. {
  1772. case no_externalcall:
  1773. case no_record:
  1774. case no_left:
  1775. case no_self:
  1776. case no_selfref:
  1777. case no_right:
  1778. break;
  1779. default:
  1780. info.name = expr->queryName();
  1781. break;
  1782. }
  1783. ForEachChild(i, expr)
  1784. info.args.append(processExpr(expr->queryChild(i)));
  1785. #ifdef ADD_ACTIVE_SCOPE_AS_COMMENT
  1786. HqlExprCopyArray inScope;
  1787. expr->gatherTablesUsed(NULL, &inScope);
  1788. ForEachItemIn(i, inScope)
  1789. {
  1790. IHqlExpression * cur = &inScope.item(i);
  1791. if (cur == expr) // add 0 to list if is own active selector
  1792. cur = NULL;
  1793. info.comment.append(processExpr(cur));
  1794. }
  1795. #endif
  1796. switch (op)
  1797. {
  1798. case no_externalcall:
  1799. info.special.append(processExpr(expr->queryExternalDefinition()));
  1800. break;
  1801. case no_call:
  1802. info.special.append(processExpr(expr->queryBody()->queryFunctionDefinition()));
  1803. break;
  1804. case no_virtualscope:
  1805. case no_concretescope:
  1806. {
  1807. HqlExprArray scopeSymbols;
  1808. expr->queryScope()->getSymbols(scopeSymbols);
  1809. ForEachItemIn(i, scopeSymbols)
  1810. info.special.append(processExpr(&scopeSymbols.item(i)));
  1811. break;
  1812. }
  1813. case no_type:
  1814. {
  1815. IHqlAlienTypeInfo * alienType = queryAlienType(expr->queryType());
  1816. //MORE: Need to output information about members of the scope, but no functions are avaiable to generate it...
  1817. info.special.append(processExpr(alienType->queryLoadFunction()));
  1818. info.special.append(processExpr(alienType->queryLengthFunction()));
  1819. info.special.append(processExpr(alienType->queryStoreFunction()));
  1820. break;
  1821. }
  1822. }
  1823. if (getRequiredTypeCode(op) == type_none)
  1824. info.type = processType(expr->queryType());
  1825. info.sequence = expr->querySequenceExtra();
  1826. return target->addExpr(op, info);
  1827. }
  1828. id_t ExpressionIRPlayer::doProcessConstant(IHqlExpression * expr)
  1829. {
  1830. IValue * value = expr->queryValue();
  1831. assertex(value);
  1832. ITypeInfo * type = expr->queryType();
  1833. ConstantBuilderInfo info;
  1834. info.tc = type->getTypeCode();
  1835. info.type = processType(type);
  1836. switch (info.tc)
  1837. {
  1838. case type_boolean:
  1839. info.intValue = value->getBoolValue();
  1840. break;
  1841. case type_int:
  1842. case type_swapint:
  1843. info.intValue = value->getIntValue();
  1844. break;
  1845. case type_real:
  1846. info.realValue = value->getRealValue();
  1847. break;
  1848. case type_decimal:
  1849. case type_packedint:
  1850. case type_string:
  1851. case type_bitfield:
  1852. case type_enumerated:
  1853. case type_varstring:
  1854. case type_data:
  1855. case type_qstring:
  1856. info.dataValue.size = value->getSize();
  1857. info.dataValue.data = value->queryValue();
  1858. break;
  1859. }
  1860. return target->addConstantExpr(info);
  1861. }
  1862. id_t ExpressionIRPlayer::doProcessAnnotation(IHqlExpression * expr)
  1863. {
  1864. annotate_kind kind = expr->getAnnotationKind();
  1865. IHqlExpression * body = expr->queryBody(true);
  1866. ExprAnnotationBuilderInfo info;
  1867. Owned<IPropertyTree> javadoc;
  1868. info.expr = processExpr(body);
  1869. switch (kind)
  1870. {
  1871. case annotate_symbol:
  1872. {
  1873. IHqlNamedAnnotation * annotation = static_cast<IHqlNamedAnnotation *>(expr->queryAnnotation());
  1874. info.name = expr->queryId()->str();
  1875. info.value = annotation->isExported() ? ob_exported : annotation->isShared() ? ob_shared : 0;
  1876. if (annotation->isVirtual())
  1877. info.value |= ob_virtual;
  1878. info.line = expr->getStartLine();
  1879. info.col = expr->getStartColumn();
  1880. break;
  1881. }
  1882. case annotate_location:
  1883. info.name = expr->querySourcePath()->str();
  1884. info.line = expr->getStartLine();
  1885. info.col = expr->getStartColumn();
  1886. break;
  1887. case annotate_meta:
  1888. {
  1889. for (unsigned i=0;; i++)
  1890. {
  1891. IHqlExpression * arg = expr->queryAnnotationParameter(i);
  1892. if (!arg)
  1893. break;
  1894. info.args.append(processExpr(arg));
  1895. }
  1896. break;
  1897. }
  1898. case annotate_warning:
  1899. info.warning = queryAnnotatedWarning(expr);
  1900. break;
  1901. case annotate_parsemeta:
  1902. //This isn't even used.
  1903. break;
  1904. case annotate_javadoc:
  1905. javadoc.setown(expr->getDocumentation());
  1906. info.tree = javadoc;
  1907. break;
  1908. }
  1909. return target->addExprAnnotation(kind, info);
  1910. }
  1911. // --------------------------------------------------------------- Exported functions
  1912. unsigned defaultDumpOptions = TIRexpandSimpleTypes|TIRexpandAttributes;
  1913. extern HQL_API void testIR(IHqlExpression * expr)
  1914. {
  1915. ExpressionIRBuilder output;
  1916. ExpressionIRPlayer reader(&output);
  1917. reader.play(expr);
  1918. OwnedHqlExpr result = output.getExpression();
  1919. assertex(expr == result);
  1920. }
  1921. //-- Dump the IR for the expression(s)/type to stdout ----------------------------------------------------------------
  1922. static void playIR(IEclBuilder & output, IHqlExpression * expr, const HqlExprArray * exprs, ITypeInfo * type)
  1923. {
  1924. ExpressionIRPlayer reader(&output);
  1925. if (expr)
  1926. reader.play(expr);
  1927. if (exprs)
  1928. {
  1929. ForEachItemIn(i, *exprs)
  1930. reader.play(&exprs->item(i));
  1931. }
  1932. if (type)
  1933. reader.play(type);
  1934. }
  1935. static void dump_ir(IHqlExpression * expr, const HqlExprArray * exprs, ITypeInfo * type)
  1936. {
  1937. printf("\nIR Expression Dumper\n====================\n");
  1938. FileIRBuilder output(defaultDumpOptions, stdout);
  1939. playIR(output, expr, exprs, type);
  1940. }
  1941. extern HQL_API void dump_ir(IHqlExpression * expr)
  1942. {
  1943. dump_ir(expr, NULL, NULL);
  1944. }
  1945. extern HQL_API void dump_ir(const HqlExprArray & exprs)
  1946. {
  1947. dump_ir(NULL, &exprs, NULL);
  1948. }
  1949. extern HQL_API void dump_ir(ITypeInfo * type)
  1950. {
  1951. dump_ir(NULL, NULL, type);
  1952. }
  1953. extern HQL_API void dump_ir(ITypeInfo * type1, ITypeInfo * type2)
  1954. {
  1955. FileIRBuilder output(defaultDumpOptions, stdout);
  1956. ExpressionIRPlayer reader(&output);
  1957. reader.play(type1);
  1958. reader.play(type2);
  1959. }
  1960. extern HQL_API void dump_ir(IHqlExpression * expr1, IHqlExpression * expr2)
  1961. {
  1962. FileIRBuilder output(defaultDumpOptions, stdout);
  1963. ExpressionIRPlayer reader(&output);
  1964. reader.play(expr1);
  1965. reader.play(expr2);
  1966. }
  1967. extern HQL_API void dump_irn(unsigned n, ...)
  1968. {
  1969. FileIRBuilder output(defaultDumpOptions, stdout);
  1970. ExpressionIRPlayer reader(&output);
  1971. va_list args;
  1972. va_start(args, n);
  1973. for (unsigned i=0; i < n;i++)
  1974. {
  1975. IInterface * next = va_arg(args, IInterface *);
  1976. IHqlExpression * expr = dynamic_cast<IHqlExpression *>(next);
  1977. ITypeInfo * type = dynamic_cast<ITypeInfo *>(next);
  1978. if (expr)
  1979. reader.play(expr);
  1980. else if (type)
  1981. reader.play(type);
  1982. }
  1983. va_end(args);
  1984. }
  1985. //-- Dump the IR for the expression(s)/type to DBGLOG ----------------------------------------------------------------
  1986. extern HQL_API void dbglogIR(IHqlExpression * expr)
  1987. {
  1988. DblgLogIRBuilder output(defaultDumpOptions);
  1989. playIR(output, expr, NULL, NULL);
  1990. }
  1991. extern HQL_API void dbglogIR(const HqlExprArray & exprs)
  1992. {
  1993. DblgLogIRBuilder output(defaultDumpOptions);
  1994. playIR(output, NULL, &exprs, NULL);
  1995. }
  1996. extern HQL_API void dbglogIR(ITypeInfo * type)
  1997. {
  1998. DblgLogIRBuilder output(defaultDumpOptions);
  1999. playIR(output, NULL, NULL, type);
  2000. }
  2001. extern HQL_API void dbglogIR(unsigned n, ...)
  2002. {
  2003. DblgLogIRBuilder output(defaultDumpOptions);
  2004. ExpressionIRPlayer reader(&output);
  2005. va_list args;
  2006. va_start(args, n);
  2007. for (unsigned i=0; i < n;i++)
  2008. {
  2009. IInterface * next = va_arg(args, IInterface *);
  2010. IHqlExpression * expr = dynamic_cast<IHqlExpression *>(next);
  2011. ITypeInfo * type = dynamic_cast<ITypeInfo *>(next);
  2012. if (expr)
  2013. reader.play(expr);
  2014. else if (type)
  2015. reader.play(type);
  2016. }
  2017. va_end(args);
  2018. }
  2019. extern HQL_API void getIRText(StringBuffer & target, unsigned options, IHqlExpression * expr)
  2020. {
  2021. StringBufferIRBuilder output(target, options);
  2022. ExpressionIRPlayer reader(&output);
  2023. reader.play(expr);
  2024. }
  2025. extern HQL_API void getIRText(StringArray & target, unsigned options, IHqlExpression * expr)
  2026. {
  2027. StringArrayIRBuilder output(target, options);
  2028. ExpressionIRPlayer reader(&output);
  2029. reader.play(expr);
  2030. }
  2031. static StringBuffer staticDebuggingStringBuffer;
  2032. extern HQL_API const char * getIRText(IHqlExpression * expr)
  2033. {
  2034. StringBufferIRBuilder output(staticDebuggingStringBuffer.clear(), defaultDumpOptions);
  2035. playIR(output, expr, NULL, NULL);
  2036. return staticDebuggingStringBuffer.str();
  2037. }
  2038. extern HQL_API const char * getIRText(ITypeInfo * type)
  2039. {
  2040. StringBufferIRBuilder output(staticDebuggingStringBuffer.clear(), defaultDumpOptions);
  2041. playIR(output, NULL, NULL, type);
  2042. return staticDebuggingStringBuffer.str();
  2043. }
  2044. } // end namespace
  2045. #ifdef _USE_CPPUNIT
  2046. #include "unittests.hpp"
  2047. namespace EclIR
  2048. {
  2049. // These test queries illustrate the kind of output that is expected from the IR generation
  2050. static const char * const testQuery1 =
  2051. "r := RECORD\n"
  2052. " unsigned id;\n"
  2053. "END;\n"
  2054. "\n"
  2055. "r t(unsigned value) := TRANSFORM\n"
  2056. " SELF.id := value;\n"
  2057. "END;\n"
  2058. "\n"
  2059. "ds := DATASET([t(10), t(1 + 10)]);\n"
  2060. "OUTPUT(ds);\n";
  2061. static const char * const expectedIR1 [] = {
  2062. "%t1 = type uint8;",
  2063. "%e2 = field#id : %t1;",
  2064. "%e3 = record(%e2);",
  2065. "%t4 = type record(%e3);",
  2066. "%t5 = type row(%t4);",
  2067. "%e6 = self(%e3) : %t5;",
  2068. "%e7 = select(%e6,%e2) : %t1;",
  2069. "%c8 = constant 10 : %t1;",
  2070. "%as9 = assign(%e7,%c8);",
  2071. "%e10 = %as9 {location '',6,3};",
  2072. "%e11 = %e3 {symbol r};",
  2073. "%t12 = type %t4 {original(%e11)};",
  2074. "%t13 = type transform(%t12);",
  2075. "%e14 = transform(%e10) : %t13;",
  2076. "%e15 = %e14 {symbol t};",
  2077. "%t16 = type int8;",
  2078. "%c17 = constant 1 : %t16;",
  2079. "%c18 = constant 10 : %t16;",
  2080. "%e19 = add(%c17,%c18) : %t16;",
  2081. "%e20 = implicitcast(%e19) : %t1;",
  2082. "%as21 = assign(%e7,%e20);",
  2083. "%e22 = %as21 {location '',6,3};",
  2084. "%e23 = transform(%e22) : %t13;",
  2085. "%e24 = %e23 {symbol t};",
  2086. "%t25 = type null;",
  2087. "%e26 = transformlist(%e15,%e24) : %t25;",
  2088. "%t27 = type table(%t5);",
  2089. "%e28 = inlinetable(%e26,%e3) : %t27;",
  2090. "%e29 = %e28 {location '',9,7};",
  2091. "%e30 = %e29 {symbol ds};",
  2092. "%e31 = null : <null>;",
  2093. "%e32 = selectfields(%e30,%e31) : %t27;",
  2094. "%t33 = type uint4;",
  2095. "%c34 = constant 3219609901 : %t33;",
  2096. "%e35 = attr#always;",
  2097. "%e36 = attr#update(%c34,%e35);",
  2098. "%e37 = output(%e32,%e36);",
  2099. "%e38 = %e37 {location '',10,1};",
  2100. "%e39 = %e38 {location '',10,1};",
  2101. "return %e39;",
  2102. NULL
  2103. };
  2104. class EclIRTests : public CppUnit::TestFixture
  2105. {
  2106. CPPUNIT_TEST_SUITE( EclIRTests );
  2107. CPPUNIT_TEST(testExprToText);
  2108. CPPUNIT_TEST_SUITE_END();
  2109. public:
  2110. void compareStringArrays(const StringArray & left, const char * const right[], unsigned test)
  2111. {
  2112. for (unsigned i=0;;i++)
  2113. {
  2114. const char * leftLine = left.isItem(i) ? left.item(i) : NULL;
  2115. const char * rightLine = right[i];
  2116. if (!leftLine && !rightLine)
  2117. return;
  2118. if (!rightLine)
  2119. {
  2120. printf("Test %u@%u: Extra item on left: %s", test, i, leftLine);
  2121. ASSERT(false);
  2122. }
  2123. else if (!leftLine)
  2124. {
  2125. printf("Test %u@%u: Extra item on right: %s", test, i, rightLine);
  2126. ASSERT(false);
  2127. }
  2128. else
  2129. {
  2130. if (strcmp(leftLine, rightLine) != 0)
  2131. {
  2132. printf("Test %u@%u: Line mismatch: [%s],[%s]", test, i, leftLine, rightLine);
  2133. ASSERT(false);
  2134. }
  2135. }
  2136. }
  2137. }
  2138. void testExprToText()
  2139. {
  2140. OwnedHqlExpr query = parseQuery(testQuery1, NULL);
  2141. StringArray ir;
  2142. getIRText(ir, 0, query);
  2143. compareStringArrays(ir, expectedIR1, __LINE__);
  2144. }
  2145. };
  2146. CPPUNIT_TEST_SUITE_REGISTRATION( EclIRTests );
  2147. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EclIRTests, "EclIRTests" );
  2148. } // end namespace
  2149. #endif // USE_CPPUNIT