defvalue.cpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401
  1. /*##############################################################################
  2. HPCC SYSTEMS software 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 "jlib.hpp"
  14. #include "jexcept.hpp"
  15. #include "jmisc.hpp"
  16. #include "deftype.ipp"
  17. #include "defvalue.ipp"
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <limits.h>
  21. #include "rtlbcd.hpp"
  22. #include "eclrtl_imp.hpp"
  23. BoolValue *BoolValue::trueconst;
  24. BoolValue *BoolValue::falseconst;
  25. static IAtom * asciiAtom;
  26. static IAtom * ebcdicAtom;
  27. MODULE_INIT(INIT_PRIORITY_DEFVALUE)
  28. {
  29. asciiAtom = createLowerCaseAtom("ascii");
  30. ebcdicAtom = createLowerCaseAtom("ebcdic");
  31. BoolValue::trueconst = new BoolValue(true);
  32. BoolValue::falseconst = new BoolValue(false);
  33. return true;
  34. }
  35. MODULE_EXIT()
  36. {
  37. ::Release(BoolValue::trueconst);
  38. ::Release(BoolValue::falseconst);
  39. }
  40. union RealUnion
  41. {
  42. float r4;
  43. double r8;
  44. };
  45. //===========================================================================
  46. static const double powerOfTen[] = {1e0, 1e1, 1e2, 1e3, 1e4,
  47. 1e5, 1e6, 1e7, 1e8, 1e9,
  48. 1e10, 1e11, 1e12, 1e13, 1e14,
  49. 1e15, 1e16, 1e17, 1e18, 1e19,
  50. 1e20, 1e21, 1e22, 1e23, 1e24,
  51. 1e25, 1e26, 1e27, 1e28, 1e29,
  52. 1e30, 1e31, 1e32 };
  53. static const unsigned __int64 maxUIntValue[] = { I64C(0), I64C(0xFF), I64C(0xFFFF), I64C(0xFFFFFF), I64C(0xFFFFFFFF), I64C(0xFFFFFFFFFF), I64C(0xFFFFFFFFFFFF), I64C(0xFFFFFFFFFFFFFF), U64C(0xFFFFFFFFFFFFFFFF) };
  54. static const __uint64 maxIntValue[] = { I64C(0), I64C(0x7F), I64C(0x7FFF), I64C(0x7FFFFF), I64C(0x7FFFFFFF), I64C(0x7FFFFFFFFF), I64C(0x7FFFFFFFFFFF), I64C(0x7FFFFFFFFFFFFF), I64C(0x7FFFFFFFFFFFFFFF) };
  55. int rangeCompare(double value, ITypeInfo * targetType)
  56. {
  57. if (targetType->isSigned())
  58. {
  59. switch (targetType->getTypeCode())
  60. {
  61. case type_decimal:
  62. if (value <= -powerOfTen[targetType->getDigits()-targetType->getPrecision()])
  63. return -1;
  64. if (value >= powerOfTen[targetType->getDigits()-targetType->getPrecision()])
  65. return +1;
  66. break;
  67. case type_int:
  68. case type_swapint:
  69. if (value < 0)
  70. {
  71. if (-value > (double)maxIntValue[targetType->getSize()]+1)
  72. return -1;
  73. }
  74. if (value > (double)maxIntValue[targetType->getSize()])
  75. return +1;
  76. break;
  77. case type_packedint:
  78. return rangeCompare(value, targetType->queryPromotedType());
  79. }
  80. }
  81. else
  82. {
  83. if (value < 0)
  84. return -1;
  85. switch (targetType->getTypeCode())
  86. {
  87. case type_decimal:
  88. if (value >= powerOfTen[targetType->getDigits()-targetType->getPrecision()])
  89. return +1;
  90. break;
  91. case type_int:
  92. case type_swapint:
  93. if (value > (double)maxUIntValue[targetType->getSize()])
  94. return +1;
  95. break;
  96. case type_packedint:
  97. return rangeCompare(value, targetType->queryPromotedType());
  98. case type_bitfield:
  99. {
  100. unsigned __int64 maxValue = (U64C(1) << targetType->getBitSize()) - 1;
  101. if (value > maxValue)
  102. return +1;
  103. break;
  104. }
  105. }
  106. }
  107. return 0;
  108. }
  109. inline unsigned getTargetLength(ITypeInfo * type, unsigned dft)
  110. {
  111. unsigned length = type->getStringLen();
  112. if (length == UNKNOWN_LENGTH)
  113. length = dft;
  114. return length;
  115. }
  116. //===========================================================================
  117. CValue::CValue(ITypeInfo *_type)
  118. {
  119. type = _type;
  120. assertThrow(type);
  121. }
  122. CValue::~CValue()
  123. {
  124. type->Release();
  125. }
  126. int CValue::compare(const void *mem)
  127. {
  128. type->Link();
  129. IValue *to = createValueFromMem(type, mem);
  130. int ret = compare(to);
  131. to->Release();
  132. return ret;
  133. }
  134. double CValue::getRealValue()
  135. {
  136. return (double)getIntValue();
  137. }
  138. type_t CValue::getTypeCode(void)
  139. {
  140. return type->getTypeCode();
  141. }
  142. size32_t CValue::getSize()
  143. {
  144. return type->getSize();
  145. }
  146. ITypeInfo *CValue::queryType() const
  147. {
  148. return type;
  149. }
  150. ITypeInfo *CValue::getType()
  151. {
  152. return LINK(type);
  153. }
  154. IValue * CValue::doCastTo(unsigned osize, const char * text, ITypeInfo *t)
  155. {
  156. assertex(t == queryUnqualifiedType(t));
  157. switch (t->getTypeCode())
  158. {
  159. case type_string:
  160. return createStringValue(text, LINK(t), osize, type->queryCharset());
  161. case type_varstring:
  162. return createVarStringValue(text, LINK(t), osize, type->queryCharset());
  163. case type_data:
  164. return t->castFrom(osize, text); //NB: Must not go through translation in default case
  165. case type_char:
  166. return createCharValue(text[0], LINK(t));
  167. case type_int:
  168. case type_swapint:
  169. case type_packedint:
  170. break; // treat these using the default action in the type.
  171. case type_boolean:
  172. return createBoolValue(getBoolValue());
  173. case type_unicode:
  174. case type_varunicode:
  175. {
  176. rtlDataAttr buff(osize * sizeof(UChar));
  177. UChar * target = buff.getustr();
  178. rtlCodepageToUnicode(osize, target, osize, text, type->queryCharset()->queryCodepageName());
  179. return t->castFrom(osize, target);
  180. }
  181. case type_utf8:
  182. {
  183. unsigned tLen = getTargetLength(t, osize);
  184. rtlDataAttr buff(tLen * 4);
  185. char * target = buff.getstr();
  186. rtlCodepageToUtf8(tLen, target, osize, text, type->queryCharset()->queryCodepageName());
  187. return createUtf8Value(tLen, target, LINK(t));
  188. }
  189. }
  190. Owned<ICharsetInfo> charset = getCharset(asciiAtom);
  191. ITranslationInfo * translation = queryDefaultTranslation(charset, type->queryCharset());
  192. if (translation)
  193. {
  194. StringBuffer translated;
  195. translation->translate(translated, osize, text);
  196. return t->castFrom(osize, translated.str());
  197. }
  198. return t->castFrom(osize, text);
  199. }
  200. //===========================================================================
  201. VarStringValue::VarStringValue(unsigned len, const char *v, ITypeInfo *_type) : CValue(_type)
  202. {
  203. unsigned typeLen = type->getStringLen();
  204. assertex(typeLen != UNKNOWN_LENGTH);
  205. if (len >= typeLen)
  206. val.set(v, typeLen);
  207. else
  208. {
  209. char * temp = (char *)checked_malloc(typeLen+1, DEFVALUE_MALLOC_FAILED);
  210. memcpy_iflen(temp, v, len);
  211. temp[len] = 0;
  212. val.set(temp, typeLen);
  213. free(temp);
  214. }
  215. }
  216. const char *VarStringValue::generateECL(StringBuffer &out)
  217. {
  218. return appendStringAsQuotedECL(out, val.length(), val).str();
  219. }
  220. const char *VarStringValue::generateCPP(StringBuffer &out, CompilerType compiler)
  221. {
  222. unsigned len = val.length();
  223. return appendStringAsQuotedCPP(out, len, val.get(), len>120).str();
  224. }
  225. void VarStringValue::toMem(void *target)
  226. {
  227. unsigned copyLen = val.length();
  228. unsigned typeLen = type->getSize();
  229. //assertThrow(typeLen);
  230. if(typeLen > 0) {
  231. if (copyLen >= typeLen)
  232. copyLen = typeLen - 1;
  233. memcpy(target, val.get(), copyLen);
  234. memset_iflen((char *)target+copyLen, 0, (typeLen-copyLen));
  235. }
  236. else {
  237. memcpy(target, val.get(), copyLen + 1);
  238. }
  239. }
  240. unsigned VarStringValue::getHash(unsigned initval)
  241. {
  242. return hashc((unsigned char *) val.get(), val.length(), initval);
  243. }
  244. int VarStringValue::compare(IValue *_to)
  245. {
  246. assertThrow(_to->getTypeCode()==type->getTypeCode());
  247. VarStringValue *to = (VarStringValue *) _to;
  248. return rtlCompareVStrVStr(val.get(), to->val.get());
  249. }
  250. int VarStringValue::compare(const void *mem)
  251. {
  252. return rtlCompareVStrVStr(val.get(), (const char *)mem);
  253. }
  254. IValue *VarStringValue::castTo(ITypeInfo *t)
  255. {
  256. t = queryUnqualifiedType(t);
  257. if (t==type)
  258. return LINK(this);
  259. type_t tc = t->getTypeCode();
  260. if (tc == type_any)
  261. return LINK(this);
  262. return doCastTo(val.length(), (const char *)val.get(), t);
  263. }
  264. const void *VarStringValue::queryValue() const
  265. {
  266. return (const void *) val.get();
  267. }
  268. bool VarStringValue::getBoolValue()
  269. {
  270. return val.length() != 0;
  271. }
  272. __int64 VarStringValue::getIntValue()
  273. {
  274. return rtlVStrToInt8(val.get());
  275. }
  276. const char *VarStringValue::getStringValue(StringBuffer &out)
  277. {
  278. out.append(val);
  279. return out.str();
  280. }
  281. void VarStringValue::pushDecimalValue(void)
  282. {
  283. DecPushCString((char*)val.get());
  284. }
  285. void VarStringValue::serialize(MemoryBuffer &tgt)
  286. {
  287. tgt.append(val);
  288. }
  289. void VarStringValue::deserialize(MemoryBuffer &src)
  290. {
  291. src.read(val);
  292. }
  293. IValue * createVarStringValue(unsigned len, const char * value, ITypeInfo *type)
  294. {
  295. if (type->getSize() != UNKNOWN_LENGTH)
  296. return new VarStringValue(len, value, type);
  297. ITypeInfo * newType = getStretchedType(len, type);
  298. type->Release();
  299. return new VarStringValue(len, value, newType);
  300. }
  301. IValue *createVarStringValue(const char *val, ITypeInfo *type, int srcLength, ICharsetInfo * srcCharset)
  302. {
  303. ITranslationInfo * translation = queryDefaultTranslation(type->queryCharset(), srcCharset);
  304. if(translation)
  305. {
  306. StringBuffer translated;
  307. translation->translate(translated, srcLength, val);
  308. return createVarStringValue(srcLength, translated.str(), type);
  309. }
  310. else
  311. return createVarStringValue(srcLength, val, type);
  312. }
  313. int VarStringValue::rangeCompare(ITypeInfo * targetType)
  314. {
  315. //MORE - should probably check for unpadded length, and check the type as well.
  316. return 0;
  317. }
  318. //===========================================================================
  319. MemoryValue::MemoryValue(const void *v, ITypeInfo *_type) : CValue(_type)
  320. {
  321. assertex(_type->getSize() != UNKNOWN_LENGTH);
  322. val.set(_type->getSize(), v);
  323. }
  324. MemoryValue::MemoryValue(ITypeInfo *_type) : CValue(_type)
  325. {
  326. }
  327. void MemoryValue::toMem(void *target)
  328. {
  329. size32_t size = getSize();
  330. assertThrow(val.length()>=size);
  331. memcpy_iflen(target, val.get(), size);
  332. }
  333. unsigned MemoryValue::getHash(unsigned initval)
  334. {
  335. size32_t size = getSize();
  336. assertThrow(val.length()>=size);
  337. return hashc((unsigned char *) val.get(), size, initval);
  338. }
  339. int MemoryValue::compare(IValue * to)
  340. {
  341. ITypeInfo * toType = to->queryType();
  342. assertex(toType->getTypeCode()==type->getTypeCode());
  343. return rtlCompareDataData(type->getSize(), (const char *)val.get(), toType->getSize(), (const char *)to->queryValue());
  344. }
  345. int MemoryValue::compare(const void *mem)
  346. {
  347. return memcmp(val.get(), (const char *) mem, type->getSize());
  348. }
  349. const char *MemoryValue::generateCPP(StringBuffer &out, CompilerType compiler)
  350. {
  351. unsigned size = getSize();
  352. return appendStringAsQuotedCPP(out, size, (const char *)val.get(), size>120).str();
  353. }
  354. bool MemoryValue::getBoolValue()
  355. {
  356. //return true if any non zero character
  357. const char * cur = (const char *)val.get();
  358. unsigned len = getSize();
  359. while (len--)
  360. {
  361. if (*cur++ != 0)
  362. return true;
  363. }
  364. return false;
  365. }
  366. void MemoryValue::pushDecimalValue()
  367. {
  368. DecPushUlong(0);
  369. }
  370. void MemoryValue::serialize(MemoryBuffer &tgt)
  371. {
  372. size32_t serialLen = (size32_t)val.length();
  373. assertex(serialLen == val.length());
  374. tgt.append(serialLen);
  375. tgt.append(serialLen, val.get());
  376. }
  377. void MemoryValue::deserialize(MemoryBuffer &src)
  378. {
  379. size32_t size;
  380. src.read(size);
  381. void *mem = checked_malloc(size, DEFVALUE_MALLOC_FAILED);
  382. assertex(mem);
  383. src.read(size, mem);
  384. val.setOwn(size, mem);
  385. }
  386. int MemoryValue::rangeCompare(ITypeInfo * targetType)
  387. {
  388. //MORE - should probably check for unpadded length, and check the type as well.
  389. return 0;
  390. }
  391. //===========================================================================
  392. StringValue::StringValue(const char *v, ITypeInfo *_type) : MemoryValue(_type)
  393. {
  394. //store a null terminated string for ease of conversion
  395. unsigned len = _type->getSize();
  396. assertex(len != UNKNOWN_LENGTH);
  397. char * temp = (char *)val.allocate(len+1);
  398. memcpy_iflen(temp, v, len);
  399. temp[len] = 0;
  400. }
  401. const char *StringValue::generateECL(StringBuffer &out)
  402. {
  403. return appendStringAsQuotedECL(out, type->getSize(), (const char *)val.get()).str();
  404. }
  405. int StringValue::compare(IValue * to)
  406. {
  407. ITypeInfo * toType = to->queryType();
  408. assertex(toType->getTypeCode()==type->getTypeCode());
  409. return rtlCompareStrStr(type->getSize(), (const char *)val.get(), toType->getSize(), (const char *)to->queryValue());
  410. }
  411. int StringValue::compare(const void *mem)
  412. {
  413. // if (type->isCaseSensitive())
  414. return memcmp(val.get(), (const char *) mem, type->getSize());
  415. // return memicmp(val.get(), (const char *) mem, type->getSize());
  416. }
  417. IValue *StringValue::castTo(ITypeInfo *t)
  418. {
  419. t = queryUnqualifiedType(t);
  420. if (t==type)
  421. return LINK(this);
  422. type_t tc = t->getTypeCode();
  423. if (tc == type_any)
  424. return LINK(this);
  425. const char * str = (const char *)val.get();
  426. if (tc == type_data)
  427. return t->castFrom(type->getSize(), str); //NB: Must not go through translation in default case
  428. ICharsetInfo * srcCharset = type->queryCharset();
  429. Owned<ICharsetInfo> asciiCharset = getCharset(asciiAtom);
  430. if (queryDefaultTranslation(asciiCharset, srcCharset))
  431. {
  432. Owned<ITypeInfo> asciiType = getAsciiType(type);
  433. Owned<IValue> asciiValue = createStringValue(str, LINK(asciiType), type->getStringLen(), srcCharset);
  434. return asciiValue->castTo(t);
  435. }
  436. return doCastTo(type->getStringLen(), str, t);
  437. }
  438. const void *StringValue::queryValue() const
  439. {
  440. return (const void *) val.get();
  441. }
  442. bool StringValue::getBoolValue()
  443. {
  444. //return true if any character is non space
  445. //return true if any non zero character
  446. const char * cur = (const char *)val.get();
  447. unsigned len = type->getSize();
  448. char fill = type->queryCharset()->queryFillChar();
  449. while (len--)
  450. {
  451. if (*cur++ != fill)
  452. return true;
  453. }
  454. return false;
  455. }
  456. __int64 StringValue::getIntValue()
  457. {
  458. return rtlStrToInt8((size32_t)val.length(), (const char *)val.get());
  459. }
  460. const char *StringValue::getStringValue(StringBuffer &out)
  461. {
  462. out.append(type->getSize(), (const char *)val.get());
  463. return out.str();
  464. }
  465. const char *StringValue::getUTF8Value(StringBuffer &out)
  466. {
  467. ICharsetInfo * srcCharset = type->queryCharset();
  468. Owned<ICharsetInfo> asciiCharset = getCharset(asciiAtom);
  469. if (queryDefaultTranslation(asciiCharset, srcCharset))
  470. {
  471. Owned<ITypeInfo> asciiType = getAsciiType(type);
  472. Owned<IValue> asciiValue = castTo(asciiType);
  473. return asciiValue->getUTF8Value(out);
  474. }
  475. rtlDataAttr temp;
  476. unsigned bufflen;
  477. rtlStrToUtf8X(bufflen, temp.refstr(), type->getSize(), (const char *)val.get());
  478. out.append(rtlUtf8Size(bufflen, temp.getstr()), temp.getstr());
  479. return out.str();
  480. }
  481. void StringValue::pushDecimalValue()
  482. {
  483. DecPushString(type->getSize(),(char *)val.get());
  484. }
  485. IValue *createStringValue(const char *val, unsigned size)
  486. {
  487. return createStringValue(val, makeStringType(size, NULL, NULL));
  488. }
  489. IValue *createStringValue(const char *val, ITypeInfo *type)
  490. {
  491. assertex(type->getSize() != UNKNOWN_LENGTH);
  492. if (type->getSize() == UNKNOWN_LENGTH)
  493. {
  494. ITypeInfo * newType = getStretchedType((size32_t)strlen(val), type);
  495. type->Release();
  496. return new StringValue(val, newType);
  497. }
  498. return new StringValue(val, type);
  499. }
  500. IValue *createStringValue(const char *val, ITypeInfo *type, size32_t srcLength, ICharsetInfo *srcCharset)
  501. {
  502. ITranslationInfo * translation = queryDefaultTranslation(type->queryCharset(), srcCharset);
  503. size32_t tgtLength = type->getSize();
  504. if (tgtLength == UNKNOWN_LENGTH)
  505. {
  506. ITypeInfo * newType = getStretchedType(srcLength, type);
  507. type->Release();
  508. type = newType;
  509. tgtLength = srcLength;
  510. }
  511. if(translation)
  512. {
  513. StringBuffer translated;
  514. translation->translate(translated, srcLength, val);
  515. if(tgtLength > srcLength)
  516. translated.appendN(tgtLength-srcLength, type->queryCharset()->queryFillChar());
  517. return new StringValue(translated.str(), type);
  518. }
  519. else if (tgtLength > srcLength)
  520. {
  521. char * extended = (char *)checked_malloc(tgtLength, DEFVALUE_MALLOC_FAILED);
  522. memcpy_iflen(extended, val, srcLength);
  523. memset(extended+srcLength, type->queryCharset()->queryFillChar(), tgtLength-srcLength);
  524. IValue * ret = new StringValue(extended, type);
  525. free(extended);
  526. return ret;
  527. }
  528. else
  529. return new StringValue(val, type);
  530. }
  531. //==========================================================================
  532. UnicodeValue::UnicodeValue(UChar const * _value, ITypeInfo * _type) : MemoryValue(_type)
  533. {
  534. unsigned len = _type->getSize();
  535. assertex(len != UNKNOWN_LENGTH);
  536. val.set(len, _value);
  537. }
  538. const void *UnicodeValue::queryValue() const
  539. {
  540. return (const void *) val.get();
  541. }
  542. const char *UnicodeValue::generateCPP(StringBuffer &out, CompilerType compiler)
  543. {
  544. out.append("((UChar *)");
  545. MemoryValue::generateCPP(out, compiler);
  546. return out.append(")").str();
  547. }
  548. const char *UnicodeValue::generateECL(StringBuffer &out)
  549. {
  550. char * buff;
  551. unsigned bufflen;
  552. rtlUnicodeToUtf8X(bufflen, buff, type->getStringLen(), (UChar const *)val.get());
  553. out.append("U'");
  554. appendUtf8AsECL(out, rtlUtf8Size(bufflen, buff), buff);
  555. out.append('\'');
  556. rtlFree(buff);
  557. return out.str();
  558. }
  559. IValue *UnicodeValue::castTo(ITypeInfo *t)
  560. {
  561. t = queryUnqualifiedType(t);
  562. if (t==type)
  563. return LINK(this);
  564. type_t tc = t->getTypeCode();
  565. if (tc == type_any)
  566. return LINK(this);
  567. size32_t olen = type->getStringLen();
  568. UChar const * uchars = (const UChar *)val.get();
  569. switch (tc)
  570. {
  571. case type_unicode:
  572. if ((t->getSize() == UNKNOWN_LENGTH) && (type->queryLocale() == t->queryLocale()))
  573. return LINK(this);
  574. return createUnicodeValue(uchars, olen, LINK(t));
  575. case type_string:
  576. {
  577. char * buff;
  578. unsigned bufflen;
  579. rtlUnicodeToCodepageX(bufflen, buff, olen, uchars, t->queryCharset()->queryCodepageName());
  580. Owned<IValue> temp = createStringValue(buff, makeStringType(bufflen, LINK(t->queryCharset()), 0));
  581. rtlFree(buff);
  582. return temp->castTo(t);
  583. }
  584. }
  585. return t->castFrom(olen, uchars);
  586. }
  587. int UnicodeValue::compare(IValue * to)
  588. {
  589. ITypeInfo * toType = to->queryType();
  590. assertex(toType->getTypeCode()==type->getTypeCode());
  591. assertex(haveCommonLocale(type, toType));
  592. char const * locale = str(getCommonLocale(type, toType));
  593. return rtlCompareUnicodeUnicode(type->getStringLen(), (const UChar *)val.get(), toType->getStringLen(), (const UChar *)to->queryValue(), locale);
  594. }
  595. int UnicodeValue::compare(const void *mem)
  596. {
  597. size32_t len = type->getStringLen();
  598. return rtlCompareUnicodeUnicode(len, (const UChar *)val.get(), len, (const UChar *)mem, str(type->queryLocale()));
  599. }
  600. bool UnicodeValue::getBoolValue()
  601. {
  602. return rtlUnicodeToBool(type->getStringLen(), (UChar const *)val.get());
  603. }
  604. __int64 UnicodeValue::getIntValue()
  605. {
  606. return rtlUnicodeToInt8(type->getStringLen(), (UChar const *)val.get());
  607. }
  608. void UnicodeValue::pushDecimalValue()
  609. {
  610. rtlDecPushUnicode(type->getStringLen(), (UChar const *)val.get());
  611. }
  612. const char *UnicodeValue::getStringValue(StringBuffer &out)
  613. {
  614. return getCodepageValue(out, "US-ASCII");
  615. }
  616. void * UnicodeValue::getUCharStringValue(unsigned len, void * out)
  617. {
  618. size_t vallen = val.length()/2;
  619. if(vallen > len)
  620. vallen = len;
  621. memcpy_iflen(out, val.get(), vallen*2);
  622. if(len > vallen)
  623. ((UChar *)out)[vallen] = 0x0000;
  624. return out;
  625. }
  626. const char *UnicodeValue::getUTF8Value(StringBuffer &out)
  627. {
  628. return getCodepageValue(out, "UTF-8");
  629. }
  630. const char *UnicodeValue::getCodepageValue(StringBuffer &out, char const * codepage)
  631. {
  632. char * buff;
  633. unsigned bufflen;
  634. rtlUnicodeToCodepageX(bufflen, buff, (size32_t)(val.length()/2), (UChar const *)val.get(), codepage);
  635. out.append(bufflen, buff);
  636. rtlFree(buff);
  637. return out.str();
  638. }
  639. IValue *createUnicodeValue(char const * value, unsigned size, char const * locale, bool utf8, bool unescape)
  640. {
  641. UChar * buff = 0;
  642. unsigned bufflen = 0;
  643. if(unescape)
  644. rtlCodepageToUnicodeXUnescape(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  645. else
  646. rtlCodepageToUnicodeX(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  647. IValue * ret = new UnicodeValue(buff, makeUnicodeType(bufflen, createLowerCaseAtom(locale)));
  648. rtlFree(buff);
  649. return ret;
  650. }
  651. IValue *createUtf8Value(size32_t len, char const * value, char const * locale, bool unescape)
  652. {
  653. if (unescape)
  654. {
  655. rtlDataAttr temp;
  656. size32_t newlen = 0;
  657. size32_t size = rtlUtf8Size(len, value);
  658. rtlCodepageToUtf8XUnescape(newlen, temp.refstr(), size, value, "UTF-8");
  659. ITypeInfo * type = makeUtf8Type(newlen, createLowerCaseAtom(locale));
  660. return createUtf8Value(temp.getstr(), type);
  661. }
  662. else
  663. {
  664. ITypeInfo * type = makeUtf8Type(len, createLowerCaseAtom(locale));
  665. return createUtf8Value(value, type);
  666. }
  667. }
  668. IValue *createUnicodeValue(char const * value, ITypeInfo * type)
  669. {
  670. if(type->getSize() == UNKNOWN_LENGTH)
  671. {
  672. type->Release();
  673. return createUnicodeValue(value, (size32_t)strlen(value), str(type->queryLocale()), false);
  674. }
  675. return createUnicodeValue(value, type, type->getStringLen());
  676. }
  677. IValue *createUnicodeValue(char const * value, ITypeInfo * type, unsigned srclen)
  678. {
  679. if(type->getSize() == UNKNOWN_LENGTH)
  680. {
  681. type->Release();
  682. return createUnicodeValue(value, srclen, str(type->queryLocale()), false);
  683. }
  684. UChar * buff = (UChar *)checked_malloc(type->getSize(), DEFVALUE_MALLOC_FAILED);
  685. rtlCodepageToUnicode(type->getStringLen(), buff, srclen, value, "US-ASCII");
  686. IValue * ret = new UnicodeValue(buff, type);
  687. free(buff);
  688. return ret;
  689. }
  690. IValue * createUnicodeValue(UChar const * text, size32_t len, ITypeInfo * type)
  691. {
  692. size32_t nlen = type->getStringLen();
  693. if(nlen == UNKNOWN_LENGTH)
  694. {
  695. ITypeInfo * newType = getStretchedType(len, type);
  696. type->Release();
  697. type = newType;
  698. nlen = len;
  699. }
  700. if (nlen <= len)
  701. return new UnicodeValue(text, type);
  702. UChar * buff = 0;
  703. unsigned bufflen = 0;
  704. rtlUnicodeSubStrFTX(bufflen, buff, len, text, 1, nlen);
  705. assertex(bufflen == nlen);
  706. IValue * ret = new UnicodeValue(buff, type);
  707. rtlFree(buff);
  708. return ret;
  709. }
  710. IValue * createUnicodeValue(size32_t len, const void * text, ITypeInfo * type)
  711. {
  712. return createUnicodeValue((const UChar *)text, len, type);
  713. }
  714. //===========================================================================
  715. void UnicodeAttr::set(UChar const * _text, unsigned _len)
  716. {
  717. free(text);
  718. text = (UChar *) checked_malloc((_len+1)*2, DEFVALUE_MALLOC_FAILED);
  719. memcpy_iflen(text, _text, _len*2);
  720. text[_len] = 0x0000;
  721. }
  722. void UnicodeAttr::setown(UChar * _text)
  723. {
  724. free(text);
  725. text = _text;
  726. }
  727. VarUnicodeValue::VarUnicodeValue(unsigned len, const UChar * v, ITypeInfo * _type) : CValue(_type)
  728. {
  729. unsigned typeLen = type->getStringLen();
  730. assertex(typeLen != UNKNOWN_LENGTH);
  731. if (len >= typeLen)
  732. val.set(v, typeLen);
  733. else
  734. {
  735. UChar * temp = (UChar *)checked_malloc((typeLen+1)*2, DEFVALUE_MALLOC_FAILED);
  736. memcpy_iflen(temp, v, len*2);
  737. temp[len] = 0;
  738. val.set(temp, typeLen);
  739. free(temp);
  740. }
  741. }
  742. const void * VarUnicodeValue::queryValue() const
  743. {
  744. return val.get();
  745. }
  746. const char * VarUnicodeValue::generateCPP(StringBuffer & out, CompilerType compiler)
  747. {
  748. out.append("(UChar *)");
  749. unsigned len = val.length()*2+1; // not pretty, but adds one null, so that string is double-null-terminated as required
  750. return appendStringAsQuotedCPP(out, len, (char const *)val.get(), len>120).str();
  751. }
  752. const char * VarUnicodeValue::generateECL(StringBuffer & out)
  753. {
  754. char * buff;
  755. unsigned bufflen;
  756. rtlUnicodeToUtf8X(bufflen, buff, val.length(), val.get());
  757. out.append("U'");
  758. appendUtf8AsECL(out, rtlUtf8Size(bufflen, buff), buff);
  759. out.append('\'');
  760. rtlFree(buff);
  761. return out.str();
  762. }
  763. IValue * VarUnicodeValue::castTo(ITypeInfo * t)
  764. {
  765. t = queryUnqualifiedType(t);
  766. if (t==type)
  767. return LINK(this);
  768. type_t tc = t->getTypeCode();
  769. if (tc == type_any)
  770. return LINK(this);
  771. size32_t olen = val.length();
  772. UChar const * uchars = (UChar const *)val.get();
  773. switch (tc)
  774. {
  775. case type_string:
  776. {
  777. char * buff;
  778. unsigned bufflen;
  779. rtlUnicodeToCodepageX(bufflen, buff, olen, uchars, t->queryCharset()->queryCodepageName());
  780. Owned<IValue> temp = createStringValue(buff, makeStringType(bufflen, LINK(t->queryCharset()), 0));
  781. rtlFree(buff);
  782. return temp->castTo(t);
  783. }
  784. }
  785. return t->castFrom(olen, uchars);
  786. }
  787. int VarUnicodeValue::compare(IValue * to)
  788. {
  789. ITypeInfo * toType = to->queryType();
  790. assertex(toType->getTypeCode()==type->getTypeCode());
  791. assertex(haveCommonLocale(type, toType));
  792. char const * locale = str(getCommonLocale(type, toType));
  793. UChar const * rhs = (UChar const *) to->queryValue();
  794. return rtlCompareUnicodeUnicode(val.length(), val.get(), rtlUnicodeStrlen(rhs), rhs, locale);
  795. }
  796. int VarUnicodeValue::compare(const void * mem)
  797. {
  798. UChar const * rhs = (UChar const *) mem;
  799. return rtlCompareUnicodeUnicode(val.length(), val.get(), rtlUnicodeStrlen(rhs), rhs, str(type->queryLocale()));
  800. }
  801. bool VarUnicodeValue::getBoolValue()
  802. {
  803. return rtlUnicodeToBool(val.length(), val.get());
  804. }
  805. __int64 VarUnicodeValue::getIntValue()
  806. {
  807. return rtlUnicodeToInt8(val.length(), val.get());
  808. }
  809. const char * VarUnicodeValue::getStringValue(StringBuffer & out)
  810. {
  811. return getCodepageValue(out, "US-ASCII");
  812. }
  813. void * VarUnicodeValue::getUCharStringValue(unsigned len, void * out)
  814. {
  815. unsigned vallen = val.length();
  816. if(vallen > len)
  817. vallen = len;
  818. memcpy_iflen(out, val.get(), vallen*2);
  819. if(len > vallen)
  820. ((UChar *)out)[vallen] = 0x0000;
  821. return out;
  822. }
  823. const char * VarUnicodeValue::getUTF8Value(StringBuffer & out)
  824. {
  825. return getCodepageValue(out, "UTF-8");
  826. }
  827. const char * VarUnicodeValue::getCodepageValue(StringBuffer & out, char const * codepage)
  828. {
  829. char * buff;
  830. unsigned bufflen;
  831. rtlUnicodeToCodepageX(bufflen, buff, val.length(), val.get(), codepage);
  832. out.append(bufflen, buff);
  833. rtlFree(buff);
  834. return out.str();
  835. }
  836. void VarUnicodeValue::pushDecimalValue()
  837. {
  838. rtlDecPushUnicode(val.length(), val.get());
  839. }
  840. void VarUnicodeValue::toMem(void * target)
  841. {
  842. memcpy(target, val.get(), (val.length()+1)*2);
  843. }
  844. unsigned VarUnicodeValue::getHash(unsigned initval)
  845. {
  846. return hashc((unsigned char *) val.get(), val.length()*2, initval);
  847. }
  848. int VarUnicodeValue::rangeCompare(ITypeInfo * targetType)
  849. {
  850. return 0;
  851. }
  852. void VarUnicodeValue::serialize(MemoryBuffer & tgt)
  853. {
  854. tgt.append(val.length());
  855. tgt.append(val.length()*2, val.get());
  856. }
  857. void VarUnicodeValue::deserialize(MemoryBuffer & src)
  858. {
  859. size32_t len;
  860. src.read(len);
  861. UChar * buff = (UChar *) checked_malloc(len*2, DEFVALUE_MALLOC_FAILED);
  862. src.read(len*2, buff);
  863. val.setown(buff);
  864. }
  865. IValue *createVarUnicodeValue(char const * value, unsigned size, char const * locale, bool utf8, bool unescape)
  866. {
  867. UChar * buff = 0;
  868. unsigned bufflen = 0;
  869. if(unescape)
  870. rtlCodepageToUnicodeXUnescape(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  871. else
  872. rtlCodepageToUnicodeX(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  873. IValue * ret = new VarUnicodeValue(bufflen, buff, makeUnicodeType(bufflen, createLowerCaseAtom(locale)));
  874. rtlFree(buff);
  875. return ret;
  876. }
  877. IValue * createVarUnicodeValue(UChar const * text, size32_t len, ITypeInfo * type)
  878. {
  879. size32_t nlen = type->getStringLen();
  880. if(nlen == UNKNOWN_LENGTH)
  881. {
  882. ITypeInfo * newType = getStretchedType(len, type);
  883. type->Release();
  884. type = newType;
  885. nlen = len;
  886. }
  887. if(nlen > len)
  888. nlen = len;
  889. UChar * buff = 0;
  890. unsigned bufflen = 0;
  891. rtlUnicodeSubStrFTX(bufflen, buff, len, text, 1, nlen);
  892. assertex(bufflen == nlen);
  893. IValue * ret = new VarUnicodeValue(bufflen, buff, type);
  894. rtlFree(buff);
  895. return ret;
  896. }
  897. IValue * createVarUnicodeValue(size32_t len, const void * text, ITypeInfo * type)
  898. {
  899. return createVarUnicodeValue((UChar const *)text, len, type);
  900. }
  901. //==========================================================================
  902. Utf8Value::Utf8Value(const char * _value, ITypeInfo * _type) : MemoryValue(_type)
  903. {
  904. unsigned len = _type->getStringLen();
  905. assertex(len != UNKNOWN_LENGTH);
  906. unsigned size = rtlUtf8Size(len, _value);
  907. val.set(size, _value);
  908. }
  909. size32_t Utf8Value::getSize()
  910. {
  911. #ifdef _DEBUG
  912. unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
  913. assertex(size == val.length());
  914. #endif
  915. return (size32_t)val.length();
  916. }
  917. const void *Utf8Value::queryValue() const
  918. {
  919. return (const void *) val.get();
  920. }
  921. const char *Utf8Value::generateCPP(StringBuffer &out, CompilerType compiler)
  922. {
  923. out.append("((char *)");
  924. MemoryValue::generateCPP(out, compiler);
  925. return out.append(")").str();
  926. }
  927. const char *Utf8Value::generateECL(StringBuffer &out)
  928. {
  929. unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
  930. out.append("U");
  931. appendStringAsQuotedECL(out, size, (const char *)val.get());
  932. return out.str();
  933. }
  934. IValue *Utf8Value::castTo(ITypeInfo *t)
  935. {
  936. t = queryUnqualifiedType(t);
  937. if (t==type)
  938. return LINK(this);
  939. type_t tc = t->getTypeCode();
  940. if (tc == type_any)
  941. return LINK(this);
  942. size32_t olen = type->getStringLen();
  943. const char * data = (const char *)val.get();
  944. switch(tc)
  945. {
  946. case type_unicode:
  947. case type_varunicode:
  948. {
  949. Owned<IValue> temp = createUnicodeValue(data, rtlUtf8Size(olen, data), str(t->queryLocale()), true, false);
  950. return temp->castTo(t);
  951. }
  952. case type_utf8:
  953. {
  954. unsigned targetLength = t->getStringLen();
  955. if (targetLength == UNKNOWN_LENGTH)
  956. {
  957. if (type->queryLocale() == t->queryLocale())
  958. return LINK(this);
  959. }
  960. return createUtf8Value(olen, data, LINK(t));
  961. }
  962. case type_string:
  963. {
  964. rtlDataAttr buff;
  965. unsigned bufflen;
  966. rtlUtf8ToCodepageX(bufflen, buff.refstr(), olen, data, t->queryCharset()->queryCodepageName());
  967. Owned<IValue> temp = createStringValue(buff.getstr(), makeStringType(bufflen, LINK(t->queryCharset()), 0));
  968. return temp->castTo(t);
  969. }
  970. case type_data:
  971. {
  972. unsigned size = rtlUtf8Size(olen, data);
  973. if (size >= t->getSize())
  974. return createDataValue(data, LINK(t));
  975. Owned<IValue> temp = createDataValue(data, size);
  976. return temp->castTo(t);
  977. }
  978. }
  979. Owned<ITypeInfo> stringType = makeStringType(UNKNOWN_LENGTH, LINK(t->queryCharset()), LINK(t->queryCollation()));
  980. Owned<IValue> stringValue = castTo(stringType);
  981. return stringValue->castTo(t);
  982. }
  983. int Utf8Value::compare(IValue * to)
  984. {
  985. ITypeInfo * toType = to->queryType();
  986. assertex(toType->getTypeCode()==type->getTypeCode());
  987. assertex(haveCommonLocale(type, toType));
  988. char const * locale = str(getCommonLocale(type, toType));
  989. return rtlCompareUtf8Utf8(type->getStringLen(), (const char *)val.get(), toType->getStringLen(), (const char *)to->queryValue(), locale);
  990. }
  991. int Utf8Value::compare(const void *mem)
  992. {
  993. size32_t len = type->getStringLen();
  994. return rtlCompareUtf8Utf8(len, (const char *)val.get(), len, (const char *)mem, str(type->queryLocale()));
  995. }
  996. bool Utf8Value::getBoolValue()
  997. {
  998. return rtlUtf8ToBool(type->getStringLen(), (const char *)val.get());
  999. }
  1000. __int64 Utf8Value::getIntValue()
  1001. {
  1002. return rtlUtf8ToInt(type->getStringLen(), (const char *)val.get());
  1003. }
  1004. void Utf8Value::pushDecimalValue()
  1005. {
  1006. rtlDecPushUtf8(type->getStringLen(), (const char *)val.get());
  1007. }
  1008. const char * Utf8Value::getStringValue(StringBuffer &out)
  1009. {
  1010. return getCodepageValue(out, "US-ASCII");
  1011. }
  1012. const char * Utf8Value::getCodepageValue(StringBuffer &out, char const * codepage)
  1013. {
  1014. unsigned bufflen;
  1015. rtlDataAttr buff;
  1016. rtlUtf8ToCodepageX(bufflen, buff.refstr(), type->getStringLen(), (const char *)val.get(), codepage);
  1017. out.append(bufflen, buff.getstr());
  1018. return out.str();
  1019. }
  1020. void * Utf8Value::getUCharStringValue(unsigned len, void * _out)
  1021. {
  1022. unsigned thisLen = type->getStringLen();
  1023. UChar * out = (UChar *)_out;
  1024. rtlUtf8ToUnicode(len, out, thisLen, (const char *)val.get());
  1025. //wierd semantics.
  1026. if (len > thisLen)
  1027. out[thisLen] = 0x0000;
  1028. return out;
  1029. }
  1030. const char *Utf8Value::getUTF8Value(StringBuffer &out)
  1031. {
  1032. unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
  1033. return out.append(size, (const char *)val.get()).str();
  1034. }
  1035. IValue * createUtf8Value(const char * text, ITypeInfo * type)
  1036. {
  1037. return new Utf8Value(text, type);
  1038. }
  1039. IValue * createUtf8Value(size32_t len, const char * text, ITypeInfo * type)
  1040. {
  1041. size32_t nlen = type->getStringLen();
  1042. if(nlen == UNKNOWN_LENGTH)
  1043. {
  1044. ITypeInfo * newType = getStretchedType(len, type);
  1045. type->Release();
  1046. type = newType;
  1047. nlen = len;
  1048. }
  1049. if (nlen <= len)
  1050. return createUtf8Value(text, type);
  1051. rtlDataAttr buff(nlen * 4);
  1052. rtlUtf8SubStrFT(nlen, buff.getstr(), len, text, 1, nlen);
  1053. return new Utf8Value(buff.getstr(), type);
  1054. }
  1055. //===========================================================================
  1056. DataValue::DataValue(const void *v, ITypeInfo *_type) : MemoryValue(v, _type)
  1057. {
  1058. }
  1059. const char *DataValue::generateECL(StringBuffer &out)
  1060. {
  1061. out.append("X'");
  1062. appendDataAsHex(out, (size32_t)val.length(), val.get());
  1063. out.append('\'');
  1064. return out.str();
  1065. }
  1066. IValue *DataValue::castTo(ITypeInfo *t)
  1067. {
  1068. t = queryUnqualifiedType(t);
  1069. if (t==type)
  1070. return LINK(this);
  1071. type_t tc = t->getTypeCode();
  1072. if (tc == type_any)
  1073. return LINK(this);
  1074. size32_t osize = type->getSize();
  1075. size32_t nsize = t->getSize();
  1076. switch (tc)
  1077. {
  1078. case type_data:
  1079. if (nsize == UNKNOWN_LENGTH)
  1080. return LINK(this);
  1081. if (nsize <= osize)
  1082. return new DataValue(val.get(), LINK(t));
  1083. else
  1084. {
  1085. char *newstr = (char *) checked_malloc(nsize, DEFVALUE_MALLOC_FAILED);
  1086. memcpy_iflen(newstr, val.get(), osize);
  1087. memset(newstr+osize, 0, nsize-osize);
  1088. IValue * ret = new DataValue(newstr, LINK(t));
  1089. free(newstr);
  1090. return ret;
  1091. }
  1092. break;
  1093. case type_string:
  1094. if (nsize == UNKNOWN_LENGTH)
  1095. {
  1096. nsize = osize;
  1097. t = getStretchedType(osize, t);
  1098. }
  1099. else
  1100. t->Link();
  1101. if (nsize <= osize)
  1102. return new StringValue((char *)val.get(), t);
  1103. else
  1104. {
  1105. char *newstr = (char *) checked_malloc(nsize, DEFVALUE_MALLOC_FAILED);
  1106. memcpy_iflen(newstr, val.get(), osize);
  1107. memset(newstr+osize, t->queryCharset()->queryFillChar(), nsize-osize);
  1108. IValue * ret = new StringValue(newstr, t);
  1109. free(newstr);
  1110. return ret;
  1111. }
  1112. break;
  1113. case type_decimal:
  1114. return t->castFrom(osize, (const char *)val.get());
  1115. case type_boolean:
  1116. return createBoolValue(getBoolValue());
  1117. }
  1118. ITypeInfo * tempType = makeStringType(getSize(), NULL, NULL);
  1119. IValue * temp = castTo(tempType);
  1120. IValue * ret = temp->castTo(t);
  1121. temp->Release();
  1122. tempType->Release();
  1123. return ret;
  1124. }
  1125. const void *DataValue::queryValue() const
  1126. {
  1127. return val.get();
  1128. }
  1129. bool DataValue::getBoolValue()
  1130. {
  1131. //return true if any character is non space
  1132. //return true if any non zero character
  1133. const char * cur = (const char *)val.get();
  1134. unsigned len = type->getSize();
  1135. char fill = 0;
  1136. while (len--)
  1137. {
  1138. if (*cur++ != fill)
  1139. return true;
  1140. }
  1141. return false;
  1142. }
  1143. const char *DataValue::getStringValue(StringBuffer &out)
  1144. {
  1145. appendDataAsHex(out, (size32_t)val.length(), val.get());
  1146. return out.str();
  1147. }
  1148. IValue *createDataValue(const char *val, unsigned size)
  1149. {
  1150. return createDataValue(val, makeDataType(size));
  1151. }
  1152. IValue *createDataValue(const char *val, ITypeInfo *type)
  1153. {
  1154. assertex(type->getSize() != UNKNOWN_LENGTH);
  1155. return new DataValue(val, type);
  1156. }
  1157. IValue *createDataValue(const char *val, ITypeInfo *type, size32_t srcLen)
  1158. {
  1159. size32_t targetSize = type->getSize();
  1160. assertex(targetSize != UNKNOWN_LENGTH);
  1161. if (srcLen >= targetSize)
  1162. return new DataValue(val, type);
  1163. MemoryAttr stretched(targetSize);
  1164. rtlDataToData(targetSize, stretched.mem(), srcLen, val);
  1165. return new DataValue(stretched.get(), type);
  1166. }
  1167. //===========================================================================
  1168. QStringValue::QStringValue(unsigned len, const void *v, ITypeInfo *_type) : MemoryValue(_type)
  1169. {
  1170. unsigned newSize = _type->getSize();
  1171. char * temp = (char *)val.allocate(newSize);
  1172. rtlStrToQStr(_type->getStringLen(), temp, len, v);
  1173. }
  1174. QStringValue::QStringValue(const char *v, ITypeInfo *_type) : MemoryValue(_type)
  1175. {
  1176. unsigned newSize = _type->getSize();
  1177. char * temp = (char *)val.allocate(newSize);
  1178. memcpy_iflen(temp, v, newSize);
  1179. }
  1180. const char *QStringValue::generateECL(StringBuffer &out)
  1181. {
  1182. unsigned strLen = type->getStringLen();
  1183. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1184. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1185. out.append('Q');
  1186. appendStringAsQuotedECL(out, strLen, strData);
  1187. free(strData);
  1188. return out.str();
  1189. }
  1190. int QStringValue::compare(IValue * to)
  1191. {
  1192. return rtlCompareQStrQStr(type->getStringLen(), val.get(), to->queryType()->getStringLen(), to->queryValue());
  1193. }
  1194. int QStringValue::compare(const void *mem)
  1195. {
  1196. size32_t len = type->getStringLen();
  1197. return rtlCompareQStrQStr(len, val.get(), len, mem);
  1198. }
  1199. IValue *QStringValue::castTo(ITypeInfo *t)
  1200. {
  1201. t = queryUnqualifiedType(t);
  1202. if (t==type)
  1203. return LINK(this);
  1204. type_t tc = t->getTypeCode();
  1205. if (tc == type_any)
  1206. return LINK(this);
  1207. switch (tc)
  1208. {
  1209. case type_boolean:
  1210. return createBoolValue(getBoolValue());
  1211. }
  1212. unsigned strLen = type->getStringLen();
  1213. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1214. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1215. IValue * ret = t->castFrom(strLen, strData);
  1216. free(strData);
  1217. return ret;
  1218. }
  1219. const void *QStringValue::queryValue() const
  1220. {
  1221. return (const void *) val.get();
  1222. }
  1223. bool QStringValue::getBoolValue()
  1224. {
  1225. //return true if any character is non space
  1226. //return true if any non zero character
  1227. const char * cur = (const char *)val.get();
  1228. unsigned len = type->getSize();
  1229. while (len--)
  1230. {
  1231. if (*cur++)
  1232. return true;
  1233. }
  1234. return false;
  1235. }
  1236. __int64 QStringValue::getIntValue()
  1237. {
  1238. unsigned strLen = type->getStringLen();
  1239. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1240. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1241. __int64 ret = rtlStrToInt8(strLen, strData);
  1242. free(strData);
  1243. return ret;
  1244. }
  1245. const char *QStringValue::getStringValue(StringBuffer &out)
  1246. {
  1247. unsigned strLen = type->getStringLen();
  1248. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1249. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1250. out.append(strLen, strData);
  1251. free(strData);
  1252. return out.str();
  1253. }
  1254. void QStringValue::pushDecimalValue()
  1255. {
  1256. unsigned strLen = type->getStringLen();
  1257. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1258. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1259. DecPushString(strLen, strData);
  1260. free(strData);
  1261. }
  1262. IValue *createQStringValue(unsigned len, const char *val, ITypeInfo *type)
  1263. {
  1264. if (type->getSize() == UNKNOWN_LENGTH)
  1265. {
  1266. ITypeInfo * newType = getStretchedType(len, type);
  1267. type->Release();
  1268. return new QStringValue(len, val, newType);
  1269. }
  1270. return new QStringValue(len, val, type);
  1271. }
  1272. //===========================================================================
  1273. CharValue::CharValue(char _val, ITypeInfo * _type) : CValue(_type)
  1274. {
  1275. val = _val;
  1276. }
  1277. const char *CharValue::generateECL(StringBuffer &out)
  1278. {
  1279. appendStringAsQuotedECL(out, 1, &val);
  1280. return out.str();
  1281. }
  1282. const char *CharValue::generateCPP(StringBuffer &out, CompilerType compiler)
  1283. {
  1284. switch (val)
  1285. {
  1286. case '\n': out.append("'\\n'"); break;
  1287. case '\r': out.append("'\\r'"); break;
  1288. case '\t': out.append("'\\t'"); break;
  1289. case '\'': out.append("'\\''"); break;
  1290. default:
  1291. if ((val >= ' ') && (val <= 126))
  1292. out.append('\'').append(val).append('\'');
  1293. else
  1294. out.append((int)val);
  1295. break;
  1296. }
  1297. return out.str();
  1298. }
  1299. void CharValue::toMem(void *target)
  1300. {
  1301. *(char *)target = val;
  1302. }
  1303. unsigned CharValue::getHash(unsigned initval)
  1304. {
  1305. char buf = val;;
  1306. return hashc((unsigned char *)&buf, 1, initval);
  1307. }
  1308. int CharValue::compare(IValue *_to)
  1309. {
  1310. assertThrow(_to->queryType()==type);
  1311. CharValue *to = (CharValue *) _to;
  1312. return memcmp(&val, &to->val, 1);
  1313. }
  1314. int CharValue::compare(const void *mem)
  1315. {
  1316. return memcmp(&val, (const char *) mem, 1);
  1317. }
  1318. IValue *CharValue::castTo(ITypeInfo *t)
  1319. {
  1320. t = queryUnqualifiedType(t);
  1321. if (t==type)
  1322. return LINK(this);
  1323. type_t tc = t->getTypeCode();
  1324. if (tc == type_any)
  1325. return LINK(this);
  1326. switch (tc)
  1327. {
  1328. case type_boolean:
  1329. return createBoolValue(val != 0);
  1330. }
  1331. return t->castFrom(1, &val);
  1332. }
  1333. bool CharValue::getBoolValue()
  1334. {
  1335. return (val != ' ');
  1336. }
  1337. const char *CharValue::getStringValue(StringBuffer &out)
  1338. {
  1339. return out.append(val).str();
  1340. }
  1341. void CharValue::pushDecimalValue()
  1342. {
  1343. DecPushString(1,&val);
  1344. }
  1345. void CharValue::serialize(MemoryBuffer &tgt)
  1346. {
  1347. tgt.append(val);
  1348. }
  1349. void CharValue::deserialize(MemoryBuffer &src)
  1350. {
  1351. src.read(val);
  1352. }
  1353. int CharValue::rangeCompare(ITypeInfo * targetType)
  1354. {
  1355. //MORE: to integegral types?
  1356. return 0;
  1357. }
  1358. IValue *createCharValue(char val, bool caseSensitive)
  1359. {
  1360. return createCharValue(val, makeCharType(caseSensitive));
  1361. }
  1362. IValue *createCharValue(char val, ITypeInfo *type)
  1363. {
  1364. return new CharValue(val, type);
  1365. }
  1366. //===========================================================================
  1367. int IntValue::compare(IValue *_to)
  1368. {
  1369. assertThrow(_to->queryType()==type);
  1370. IntValue *to = (IntValue *) _to;
  1371. if (val == to->val)
  1372. return 0;
  1373. else if (type->isSigned())
  1374. return (__int64) val > (__int64) to->val ? 1 : -1;
  1375. else
  1376. return val > to->val ? 1 : -1;
  1377. }
  1378. IValue * IntValue::castViaString(ITypeInfo * t)
  1379. {
  1380. size32_t len;
  1381. char * text;
  1382. if (type->isSigned())
  1383. rtlInt8ToStrX(len, text, val);
  1384. else
  1385. rtlUInt8ToStrX(len, text, (unsigned __int64)val);
  1386. IValue * ret = t->castFrom(len, text); // Include EBCDIC conversion, and creating correct type
  1387. rtlFree(text);
  1388. return ret;
  1389. }
  1390. IValue *IntValue::castTo(ITypeInfo *t)
  1391. {
  1392. t = queryUnqualifiedType(t);
  1393. if (t==type)
  1394. return LINK(this);
  1395. type_t tc = t->getTypeCode();
  1396. if (tc == type_any)
  1397. return LINK(this);
  1398. unsigned nLen = t->getStringLen();
  1399. switch (tc)
  1400. {
  1401. case type_qstring:
  1402. case type_utf8:
  1403. case type_unicode:
  1404. case type_varstring:
  1405. case type_varunicode:
  1406. return castViaString(t);
  1407. case type_string:
  1408. {
  1409. if (nLen == UNKNOWN_LENGTH)
  1410. return castViaString(t);
  1411. char *newstr = (char *) checked_malloc(nLen, DEFVALUE_MALLOC_FAILED);
  1412. if (type->isSigned())
  1413. rtlInt8ToStr(nLen, newstr, val);
  1414. else
  1415. rtlUInt8ToStr(nLen, newstr, (unsigned __int64)val);
  1416. IValue * ret = t->castFrom(nLen, newstr);
  1417. free(newstr);
  1418. return ret;
  1419. }
  1420. case type_int:
  1421. case type_swapint:
  1422. return createTruncIntValue(val, LINK(t));
  1423. case type_boolean:
  1424. return createBoolValue(val!=0);
  1425. }
  1426. return t->castFrom(type->isSigned(), (__int64)val);
  1427. }
  1428. byte * IntValue::getAddressValue()
  1429. {
  1430. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1431. return (byte *)&val;
  1432. #else
  1433. return (byte *)&val + (8 - type->getSize());
  1434. #endif
  1435. }
  1436. const void * IntValue::queryValue() const
  1437. {
  1438. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1439. return reinterpret_cast<const byte *>(&val);
  1440. #else
  1441. return reinterpret_cast<const byte *>(&val) + (8 - type->getSize());
  1442. #endif
  1443. }
  1444. void IntValue::toMem(void *target)
  1445. {
  1446. size32_t size = type->getSize();
  1447. const byte * data = getAddressValue();
  1448. if (type->isSwappedEndian())
  1449. _cpyrevn(target, data, size);
  1450. else
  1451. memcpy_iflen(target, data, size);
  1452. }
  1453. unsigned IntValue::getHash(unsigned initval)
  1454. {
  1455. return hashc(getAddressValue(), type->getSize(), initval);
  1456. }
  1457. const char *IntValue::generateECL(StringBuffer &s)
  1458. {
  1459. return getStringValue(s);
  1460. }
  1461. static void generateUnsignedCPP(StringBuffer &s, __uint64 val, unsigned size, CompilerType compiler)
  1462. {
  1463. s.append(val);
  1464. switch (compiler)
  1465. {
  1466. case GccCppCompiler:
  1467. if (val && (size > sizeof(unsigned)))
  1468. s.append("LLU");
  1469. else
  1470. s.append("U");
  1471. break;
  1472. case Vs6CppCompiler:
  1473. s.append("U");
  1474. if (val && (size > sizeof(unsigned)))
  1475. s.append("i64");
  1476. break;
  1477. default:
  1478. throwUnexpected();
  1479. }
  1480. }
  1481. static void generateSignedCPP(StringBuffer &s, __int64 val, unsigned size, CompilerType compiler)
  1482. {
  1483. // Special case needed for MININT etc
  1484. if (val && (size > sizeof(unsigned)))
  1485. {
  1486. if (val == LLONG_MIN)
  1487. s.append("LLONG_MIN");
  1488. else
  1489. {
  1490. s.append(val);
  1491. switch (compiler)
  1492. {
  1493. case GccCppCompiler:
  1494. s.append("LL");
  1495. break;
  1496. case Vs6CppCompiler:
  1497. s.append("i64");
  1498. break;
  1499. default:
  1500. throwUnexpected();
  1501. }
  1502. }
  1503. }
  1504. else
  1505. {
  1506. if (val == INT_MIN)
  1507. s.append("INT_MIN");
  1508. else
  1509. s.append(val);
  1510. }
  1511. }
  1512. const char *IntValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1513. {
  1514. unsigned size = type->getSize();
  1515. if (type->isSwappedEndian())
  1516. {
  1517. if (type->isSigned())
  1518. generateSignedCPP(s, rtlReadSwapInt(getAddressValue(), size), size, compiler);
  1519. else
  1520. generateUnsignedCPP(s, rtlReadSwapUInt(getAddressValue(), size), size, compiler);
  1521. }
  1522. else
  1523. {
  1524. if (type->isSigned())
  1525. generateSignedCPP(s, (__int64) val, size, compiler);
  1526. else
  1527. generateUnsignedCPP(s, val, size, compiler);
  1528. }
  1529. return s.str();
  1530. }
  1531. const char *IntValue::getStringValue(StringBuffer &s)
  1532. {
  1533. if (type->isSigned())
  1534. s.append((__int64)val);
  1535. else
  1536. s.append(val);
  1537. return s.str();
  1538. }
  1539. bool IntValue::getBoolValue()
  1540. {
  1541. return val != 0;
  1542. }
  1543. __int64 IntValue::getIntValue()
  1544. {
  1545. return val;
  1546. }
  1547. void IntValue::pushDecimalValue(void)
  1548. {
  1549. if (type->isSigned())
  1550. DecPushInt64(val);
  1551. else
  1552. DecPushUInt64(val);
  1553. }
  1554. int IntValue::rangeCompare(ITypeInfo * targetType)
  1555. {
  1556. if (type->isSigned())
  1557. return ::rangeCompare((double)(__int64)val, targetType);
  1558. return ::rangeCompare((double)(unsigned __int64)val, targetType);
  1559. }
  1560. void IntValue::serialize(MemoryBuffer &tgt)
  1561. {
  1562. tgt.append(val);
  1563. }
  1564. void IntValue::deserialize(MemoryBuffer &src)
  1565. {
  1566. src.read(val);
  1567. }
  1568. //===========================================================================
  1569. int PackedIntValue::compare(IValue *_to)
  1570. {
  1571. assertThrow(_to->queryType()==type);
  1572. unsigned __int64 val = value->getIntValue();
  1573. unsigned __int64 toVal = _to->getIntValue();
  1574. if (val == toVal)
  1575. return 0;
  1576. else if (type->isSigned())
  1577. return (__int64) val > (__int64) toVal ? 1 : -1;
  1578. else
  1579. return val > toVal ? 1 : -1;
  1580. }
  1581. void PackedIntValue::toMem(void *target)
  1582. {
  1583. unsigned __int64 val = value->getIntValue();
  1584. if (type->isSigned())
  1585. rtlSetPackedSigned(target, val);
  1586. else
  1587. rtlSetPackedUnsigned(target, val);
  1588. }
  1589. //---------------------------------------------------------------------------
  1590. bool isInRange(__uint64 value, bool isSigned, unsigned size)
  1591. {
  1592. if (isSigned)
  1593. value += (maxIntValue[size]+1);
  1594. if (value & ~maxUIntValue[size])
  1595. return false;
  1596. return true;
  1597. }
  1598. inline unsigned getMinSize(unsigned __int64 value, bool isSigned)
  1599. {
  1600. if (isSigned)
  1601. {
  1602. if ((value + 0x80) <= 0xff) return 1;
  1603. if ((value + 0x8000) <= 0xffff) return 2;
  1604. if ((value + 0x80000000) <= 0xffffffff) return 4;
  1605. }
  1606. else
  1607. {
  1608. if (value <= 0xff) return 1;
  1609. if (value <= 0xffff) return 2;
  1610. if (value <= 0xffffffff) return 4;
  1611. }
  1612. return 8;
  1613. }
  1614. unsigned getMinimumIntegerSize(unsigned __int64 value, bool isSigned)
  1615. {
  1616. return getMinSize(value, isSigned);
  1617. }
  1618. static unsigned __int64 truncateInt(unsigned __int64 value, unsigned size, bool isSigned)
  1619. {
  1620. if (isSigned)
  1621. {
  1622. unsigned shift = (8-size)*8;
  1623. value <<= shift;
  1624. value = ((__int64)value) >> shift;
  1625. }
  1626. else
  1627. {
  1628. value &= maxUIntValue[size];
  1629. }
  1630. return value;
  1631. }
  1632. IValue *createIntValue(__int64 val, ITypeInfo * type)
  1633. {
  1634. #ifdef _DEBUG
  1635. if (!isInRange(val, type->isSigned(), type->getSize()))
  1636. throw MakeStringException(1, "Out of range value");
  1637. #endif
  1638. return new IntValue(val, type);
  1639. }
  1640. IValue *createIntValue(__int64 val, unsigned size, bool isSigned)
  1641. {
  1642. #ifdef _DEBUG
  1643. if (!isInRange(val, isSigned, size))
  1644. throw MakeStringException(0, "Out of range value");
  1645. #endif
  1646. ITypeInfo * type = makeIntType(size, isSigned);
  1647. return new IntValue(val, type);
  1648. }
  1649. IValue *createTruncIntValue(__int64 val, ITypeInfo * type)
  1650. {
  1651. if (type->getTypeCode() == type_packedint)
  1652. return createPackedIntValue(val, type);
  1653. return new IntValue(truncateInt(val, type->getSize(), type->isSigned()), type);
  1654. }
  1655. IValue *createPackedIntValue(__int64 val, ITypeInfo * type)
  1656. {
  1657. assertex(type->getTypeCode() == type_packedint);
  1658. ITypeInfo * promotedType = type->queryPromotedType();
  1659. IValue * value = createTruncIntValue(val, LINK(promotedType));
  1660. return new PackedIntValue(value, type);
  1661. }
  1662. IValue *createPackedIntValue(IValue * val, ITypeInfo * type)
  1663. {
  1664. assertex(type->getTypeCode() == type_packedint);
  1665. return new PackedIntValue(val, type);
  1666. }
  1667. IValue *createTruncIntValue(__int64 val, unsigned size, bool isSigned)
  1668. {
  1669. val = truncateInt(val, size, isSigned);
  1670. ITypeInfo * type = makeIntType(size, isSigned);
  1671. return new IntValue(val, type);
  1672. }
  1673. IValue * createMinIntValue(__int64 value)
  1674. {
  1675. return createIntValue(value, getMinSize(value, true), true);
  1676. }
  1677. IValue * createMinUIntValue(unsigned __int64 value)
  1678. {
  1679. return createIntValue(value, getMinSize(value, false), false);
  1680. }
  1681. IValue *createBitfieldValue(__int64 val, ITypeInfo * type)
  1682. {
  1683. #ifdef _DEBUG
  1684. size32_t bitsize = type->getBitSize();
  1685. if (bitsize != sizeof(__uint64) * 8)
  1686. val = val & (((__uint64)1 << bitsize)-1);
  1687. #endif
  1688. return new BitfieldValue(val, type);
  1689. }
  1690. //===========================================================================
  1691. IValue *EnumValue::castTo(ITypeInfo * t)
  1692. {
  1693. t = queryUnqualifiedType(t);
  1694. if (t == type)
  1695. return LINK(this);
  1696. type_t tc = t->getTypeCode();
  1697. if (tc == type_any)
  1698. return LINK(this);
  1699. if (t->isInteger())
  1700. return IntValue::castTo(t);
  1701. CEnumeratedTypeInfo * et = (CEnumeratedTypeInfo *) type;
  1702. IValue *ret = et->queryValue((int) val);
  1703. if (ret)
  1704. return ret->castTo(t);
  1705. // cope better with unknown index
  1706. Owned<IValue> temp = createStringValue("", 0U);
  1707. return temp->castTo(t);
  1708. }
  1709. IValue * createEnumValue(int value, ITypeInfo * type)
  1710. {
  1711. return new EnumValue(value, type);
  1712. }
  1713. //===========================================================================
  1714. int RealValue::compare(IValue *_to)
  1715. {
  1716. assertThrow(_to->queryType()==type);
  1717. RealValue *to = (RealValue *) _to;
  1718. if (val == to->val)
  1719. return 0;
  1720. else
  1721. return val > to->val ? 1 : -1;
  1722. }
  1723. IValue *RealValue::castTo(ITypeInfo *t)
  1724. {
  1725. t = queryUnqualifiedType(t);
  1726. if (t==type)
  1727. return LINK(this);
  1728. type_t tc = t->getTypeCode();
  1729. if (tc == type_any)
  1730. return LINK(this);
  1731. switch (tc)
  1732. {
  1733. case type_real:
  1734. {
  1735. double value = val;
  1736. switch (t->getSize())
  1737. {
  1738. case 4: value = (float)value; break;
  1739. case 8: value = (double)value; break;
  1740. }
  1741. return createRealValue(value, LINK(t));
  1742. }
  1743. case type_int:
  1744. case type_swapint:
  1745. case type_packedint:
  1746. return createTruncIntValue((__int64)val, LINK(t));
  1747. case type_boolean:
  1748. return createBoolValue(val!=0);
  1749. }
  1750. return t->castFrom(val);
  1751. }
  1752. void RealValue::toMem(void *target)
  1753. {
  1754. RealUnion u;
  1755. size32_t size = type->getSize();
  1756. switch (size)
  1757. {
  1758. case 4:
  1759. u.r4 = (float)val;
  1760. break;
  1761. case 8:
  1762. u.r8 = val;
  1763. break;
  1764. };
  1765. memcpy(target, &u, size);
  1766. }
  1767. unsigned RealValue::getHash(unsigned initval)
  1768. {
  1769. RealUnion u;
  1770. size32_t size = type->getSize();
  1771. switch (size)
  1772. {
  1773. case 4:
  1774. u.r4 = (float)val;
  1775. break;
  1776. case 8:
  1777. u.r8 = val;
  1778. break;
  1779. };
  1780. return hashc((unsigned char *) &u, size, initval);
  1781. }
  1782. const char *RealValue::generateECL(StringBuffer &s)
  1783. {
  1784. if (isinf(val))
  1785. return s.append("Std.Math.INFINITY");
  1786. else if (isnan(val))
  1787. return s.append("Std.Math.NaN");
  1788. else
  1789. return getStringValue(s);
  1790. }
  1791. const char *RealValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1792. {
  1793. if (isinf(val))
  1794. return s.append("rtlCreateRealInf()");
  1795. else if (isnan(val))
  1796. return s.append("rtlCreateRealNull()");
  1797. else
  1798. return getStringValue(s);
  1799. }
  1800. const char *RealValue::getStringValue(StringBuffer &s)
  1801. {
  1802. size32_t size = type->getSize();
  1803. if (size==4)
  1804. return s.append((float) val);
  1805. else
  1806. return s.append(val);
  1807. }
  1808. bool RealValue::getBoolValue()
  1809. {
  1810. return val != 0;
  1811. }
  1812. __int64 RealValue::getIntValue()
  1813. {
  1814. return (__int64)(unsigned __int64)val;
  1815. }
  1816. double RealValue::getRealValue()
  1817. {
  1818. return val;
  1819. }
  1820. void RealValue::pushDecimalValue()
  1821. {
  1822. DecPushReal(val);
  1823. }
  1824. void RealValue::serialize(MemoryBuffer &tgt)
  1825. {
  1826. tgt.append(val);
  1827. }
  1828. void RealValue::deserialize(MemoryBuffer &src)
  1829. {
  1830. src.read((double &)val);
  1831. }
  1832. int RealValue::rangeCompare(ITypeInfo * targetType)
  1833. {
  1834. return ::rangeCompare(val, targetType);
  1835. }
  1836. IValue *createRealValue(double val, unsigned size)
  1837. {
  1838. return new RealValue(val, size);
  1839. }
  1840. IValue *createRealValue(double val, ITypeInfo * type)
  1841. {
  1842. return new RealValue(val, type);
  1843. }
  1844. //===========================================================================
  1845. DecimalValue::DecimalValue(const void * v, ITypeInfo * _type) : CValue(_type)
  1846. {
  1847. unsigned len = _type->getSize();
  1848. val = (char *)checked_malloc(len, DEFVALUE_MALLOC_FAILED);
  1849. memcpy(val, v, len);
  1850. }
  1851. DecimalValue::~DecimalValue()
  1852. {
  1853. free(val);
  1854. }
  1855. int DecimalValue::compare(IValue *_to)
  1856. {
  1857. assertThrow(_to->getTypeCode()==type_decimal);
  1858. BcdCriticalBlock bcdBlock;
  1859. pushDecimalValue();
  1860. _to->pushDecimalValue();
  1861. return DecDistinct();
  1862. }
  1863. IValue *DecimalValue::castTo(ITypeInfo *t)
  1864. {
  1865. t = queryUnqualifiedType(t);
  1866. if (t==type)
  1867. return LINK(this);
  1868. type_t tc = t->getTypeCode();
  1869. if (tc == type_any)
  1870. return LINK(this);
  1871. BcdCriticalBlock bcdBlock;
  1872. char newstr[400];
  1873. pushDecimalValue();
  1874. switch (tc)
  1875. {
  1876. case type_real:
  1877. {
  1878. double value = DecPopReal();
  1879. switch (t->getSize())
  1880. {
  1881. case 4: value = (float)value; break;
  1882. case 8: value = (double)value; break;
  1883. }
  1884. return createRealValue(value, LINK(t));
  1885. }
  1886. case type_int:
  1887. case type_swapint:
  1888. case type_packedint:
  1889. return createTruncIntValue(DecPopInt64(), LINK(t));
  1890. case type_boolean:
  1891. return createBoolValue(DecCompareNull() != 0);
  1892. case type_decimal:
  1893. return createDecimalValueFromStack(t);
  1894. }
  1895. DecPopCString(sizeof(newstr), newstr);
  1896. return t->castFrom((size32_t)strlen(newstr), newstr);
  1897. }
  1898. void DecimalValue::toMem(void *target)
  1899. {
  1900. size32_t size = type->getSize();
  1901. memcpy(target, val, size);
  1902. }
  1903. unsigned DecimalValue::getHash(unsigned initval)
  1904. {
  1905. size32_t size = type->getSize();
  1906. return hashc((unsigned char *) val, size, initval);
  1907. }
  1908. const char *DecimalValue::generateECL(StringBuffer &s)
  1909. {
  1910. getStringValue(s);
  1911. s.append('D');
  1912. return s;
  1913. }
  1914. const char *DecimalValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1915. {
  1916. size32_t size = type->getSize();
  1917. s.append("\"");
  1918. for (unsigned i=0;i<size;i++)
  1919. {
  1920. unsigned char c = ((unsigned char *)val)[i];
  1921. s.append("\\x");
  1922. s.appendhex(c, false);
  1923. }
  1924. s.append('"');
  1925. return s.str();
  1926. }
  1927. const char *DecimalValue::getStringValue(StringBuffer &s)
  1928. {
  1929. char strval[64];
  1930. BcdCriticalBlock bcdBlock;
  1931. pushDecimalValue();
  1932. DecPopCString(sizeof(strval), strval);
  1933. s.append(strval);
  1934. return s.str();
  1935. }
  1936. bool DecimalValue::getBoolValue()
  1937. {
  1938. BcdCriticalBlock bcdBlock;
  1939. pushDecimalValue();
  1940. return DecCompareNull() != 0;
  1941. }
  1942. __int64 DecimalValue::getIntValue()
  1943. {
  1944. BcdCriticalBlock bcdBlock;
  1945. pushDecimalValue();
  1946. return DecPopInt64();
  1947. }
  1948. double DecimalValue::getRealValue()
  1949. {
  1950. BcdCriticalBlock bcdBlock;
  1951. pushDecimalValue();
  1952. return DecPopReal();
  1953. }
  1954. void DecimalValue::pushDecimalValue()
  1955. {
  1956. if (type->isSigned())
  1957. DecPushDecimal(val, type->getSize(), type->getPrecision());
  1958. else
  1959. DecPushUDecimal(val, type->getSize(), type->getPrecision());
  1960. }
  1961. const void * DecimalValue::queryValue() const
  1962. {
  1963. return (const void *)val;
  1964. }
  1965. void DecimalValue::serialize(MemoryBuffer &tgt)
  1966. {
  1967. tgt.append(type->getSize());
  1968. tgt.append(type->getSize(), val);
  1969. }
  1970. void DecimalValue::deserialize(MemoryBuffer &src)
  1971. {
  1972. size32_t size;
  1973. src.read(size);
  1974. val = checked_malloc(size, DEFVALUE_MALLOC_FAILED);
  1975. assertex(val);
  1976. }
  1977. int DecimalValue::rangeCompare(ITypeInfo * targetType)
  1978. {
  1979. return ::rangeCompare(getRealValue(), targetType);
  1980. }
  1981. IValue *createDecimalValue(void * val, ITypeInfo * type)
  1982. {
  1983. return new DecimalValue(val, type);
  1984. }
  1985. IValue *createDecimalValueFromStack(ITypeInfo * type)
  1986. {
  1987. return static_cast<CDecimalTypeInfo*>(type)->createValueFromStack();
  1988. }
  1989. //===========================================================================
  1990. const char *BoolValue::generateECL(StringBuffer &s)
  1991. {
  1992. s.append(val ? "true" : "false");
  1993. return s.str();
  1994. }
  1995. const char *BoolValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1996. {
  1997. s.append(val ? "true" : "false");
  1998. return s.str();
  1999. }
  2000. const char *BoolValue::getStringValue(StringBuffer &s)
  2001. {
  2002. s.append(val ? "true" : "false");
  2003. return s.str();
  2004. }
  2005. bool BoolValue::getBoolValue()
  2006. {
  2007. return val;
  2008. }
  2009. __int64 BoolValue::getIntValue()
  2010. {
  2011. return val;
  2012. }
  2013. void BoolValue::pushDecimalValue()
  2014. {
  2015. DecPushUlong(val);
  2016. }
  2017. void BoolValue::toMem(void *target)
  2018. {
  2019. memcpy(target, &val, type->getSize());
  2020. }
  2021. unsigned BoolValue::getHash(unsigned initval)
  2022. {
  2023. size32_t size = type->getSize();
  2024. return hashc((unsigned char *) &val, size, initval);
  2025. }
  2026. int BoolValue::compare(IValue *_to)
  2027. {
  2028. assertThrow(_to->queryType()==type);
  2029. BoolValue *to = (BoolValue *) _to;
  2030. return (int) val - (int) to->val;
  2031. }
  2032. int BoolValue::compare(const void *mem)
  2033. {
  2034. type->Link();
  2035. IValue *to = createValueFromMem(type, mem);
  2036. int ret = compare(to);
  2037. to->Release();
  2038. return ret;
  2039. }
  2040. IValue *BoolValue::castTo(ITypeInfo *t)
  2041. {
  2042. t = queryUnqualifiedType(t);
  2043. if (t==type)
  2044. return LINK(this);
  2045. type_t tc = t->getTypeCode();
  2046. if (tc == type_any)
  2047. return LINK(this);
  2048. switch (tc)
  2049. {
  2050. case type_string:
  2051. case type_qstring:
  2052. case type_varstring:
  2053. case type_unicode:
  2054. case type_varunicode:
  2055. case type_utf8:
  2056. if (!val)
  2057. return t->castFrom(0, "");
  2058. break;
  2059. case type_int:
  2060. case type_swapint:
  2061. return createTruncIntValue(val, LINK(t));
  2062. case type_packedint:
  2063. return createPackedIntValue(val, LINK(t));
  2064. }
  2065. return t->castFrom(false, (__int64)val);
  2066. }
  2067. BoolValue *BoolValue::getTrue()
  2068. {
  2069. trueconst->Link();
  2070. return trueconst;
  2071. }
  2072. BoolValue *BoolValue::getFalse()
  2073. {
  2074. falseconst->Link();
  2075. return falseconst;
  2076. }
  2077. void BoolValue::serialize(MemoryBuffer &tgt)
  2078. {
  2079. tgt.append(val);
  2080. }
  2081. void BoolValue::deserialize(MemoryBuffer &src)
  2082. {
  2083. src.read(val);
  2084. }
  2085. IValue *createBoolValue(bool v)
  2086. {
  2087. return v ? BoolValue::getTrue() : BoolValue::getFalse();
  2088. }
  2089. int BoolValue::rangeCompare(ITypeInfo * targetType)
  2090. {
  2091. return 0; // can fit in anything
  2092. }
  2093. /* In type: linked */
  2094. IValue *createValueFromMem(ITypeInfo *type, const void *mem)
  2095. {
  2096. int size = type->getSize();
  2097. type_t code = type->getTypeCode();
  2098. switch (code)
  2099. {
  2100. case type_string:
  2101. return createStringValue((const char *) mem, type);
  2102. case type_unicode:
  2103. return new UnicodeValue((UChar const *) mem, type);
  2104. case type_utf8:
  2105. return new Utf8Value((char const *) mem, type);
  2106. case type_data:
  2107. type->Release();
  2108. return createDataValue((const char *) mem, size);
  2109. case type_varstring:
  2110. return createVarStringValue((size32_t)strlen((const char *) mem), (const char *) mem, type);
  2111. case type_varunicode:
  2112. return new VarUnicodeValue(rtlUnicodeStrlen((UChar const *) mem), (UChar const *) mem, type);
  2113. case type_qstring:
  2114. return new QStringValue((const char *)mem, type);
  2115. case type_decimal:
  2116. return new DecimalValue(mem, type);
  2117. case type_bitfield:
  2118. UNIMPLEMENTED;
  2119. //needs more thought;
  2120. case type_int:
  2121. {
  2122. unsigned __int64 val;
  2123. if (type->isSigned())
  2124. val = rtlReadInt(mem, size);
  2125. else
  2126. val = rtlReadUInt(mem, size);
  2127. return createIntValue(val, type);
  2128. }
  2129. case type_swapint:
  2130. {
  2131. unsigned __int64 val;
  2132. if (type->isSigned())
  2133. val = rtlReadSwapInt(mem, size);
  2134. else
  2135. val = rtlReadSwapUInt(mem, size);
  2136. return createIntValue(val, type);
  2137. }
  2138. case type_packedint:
  2139. {
  2140. unsigned __int64 val = type->isSigned() ? rtlGetPackedSigned(mem) : rtlGetPackedUnsigned(mem);
  2141. return createPackedIntValue(val, type);
  2142. }
  2143. case type_real:
  2144. {
  2145. RealUnion u;
  2146. memcpy(&u, mem, size);
  2147. double val = 0;
  2148. switch (size)
  2149. {
  2150. case 4:
  2151. val = u.r4;
  2152. break;
  2153. case 8:
  2154. val = u.r8;
  2155. break;
  2156. }
  2157. type->Release();
  2158. return createRealValue(val, size);
  2159. }
  2160. case type_boolean:
  2161. type->Release();
  2162. return createBoolValue(*(bool *)mem);
  2163. }
  2164. type->Release();
  2165. return NULL;
  2166. }
  2167. //----------------------------------------------------------------------------
  2168. void appendValueToBuffer(MemoryBuffer & mem, IValue * value)
  2169. {
  2170. ITypeInfo * type = value->queryType();
  2171. unsigned len = type->getSize();
  2172. void * temp = checked_malloc(len, DEFVALUE_MALLOC_FAILED);
  2173. value->toMem(temp);
  2174. if (type->isSwappedEndian() != mem.needSwapEndian())
  2175. mem.appendSwap(len, temp);
  2176. else
  2177. mem.append(len, temp);
  2178. free(temp);
  2179. }
  2180. //============================================================================
  2181. IValue * addValues(IValue * left, IValue * right)
  2182. {
  2183. IValue * ret;
  2184. ITypeInfo * pnt = getPromotedAddSubType(left->queryType(), right->queryType());
  2185. switch(pnt->getTypeCode())
  2186. {
  2187. case type_int:
  2188. case type_swapint:
  2189. case type_packedint:
  2190. ret = createTruncIntValue((__int64)((__uint64)left->getIntValue() + (__uint64)right->getIntValue()), pnt);
  2191. break;
  2192. case type_real:
  2193. ret = createRealValue(left->getRealValue() + right->getRealValue(), pnt);
  2194. break;
  2195. case type_decimal:
  2196. {
  2197. BcdCriticalBlock bcdBlock;
  2198. left->pushDecimalValue();
  2199. right->pushDecimalValue();
  2200. DecAdd();
  2201. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2202. pnt->Release();
  2203. break;
  2204. }
  2205. default:
  2206. throwUnexpected();
  2207. }
  2208. return ret;
  2209. }
  2210. #define CALCULATE_AND_RETURN(op) IValue * res = LINK(*values); \
  2211. while (--num) \
  2212. { \
  2213. values++; \
  2214. IValue * tmp = op(res, *values);\
  2215. res->Release(); \
  2216. res = tmp; \
  2217. } \
  2218. return res;
  2219. IValue * subtractValues(IValue * left, IValue * right)
  2220. {
  2221. IValue * ret;
  2222. ITypeInfo * pnt = getPromotedAddSubType(left->queryType(), right->queryType());
  2223. switch(pnt->getTypeCode())
  2224. {
  2225. case type_int:
  2226. case type_swapint:
  2227. case type_packedint:
  2228. ret = createTruncIntValue((__int64)((__uint64)left->getIntValue() - (__uint64)right->getIntValue()), pnt);
  2229. break;
  2230. case type_real:
  2231. ret = createRealValue(left->getRealValue() - right->getRealValue(), pnt);
  2232. break;
  2233. case type_decimal:
  2234. {
  2235. BcdCriticalBlock bcdBlock;
  2236. left->pushDecimalValue();
  2237. right->pushDecimalValue();
  2238. DecSub();
  2239. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2240. pnt->Release();
  2241. break;
  2242. }
  2243. default:
  2244. throwUnexpected();
  2245. }
  2246. return ret;
  2247. }
  2248. IValue * multiplyValues(IValue * left, IValue * right)
  2249. {
  2250. IValue * ret;
  2251. ITypeInfo * pnt = getPromotedMulDivType(left->queryType(), right->queryType());
  2252. switch(pnt->getTypeCode())
  2253. {
  2254. case type_int:
  2255. case type_swapint:
  2256. case type_packedint:
  2257. ret = createTruncIntValue(left->getIntValue() * right->getIntValue(), pnt);
  2258. break;
  2259. case type_real:
  2260. ret = createRealValue(left->getRealValue() * right->getRealValue(), pnt);
  2261. break;
  2262. case type_decimal:
  2263. {
  2264. BcdCriticalBlock bcdBlock;
  2265. left->pushDecimalValue();
  2266. right->pushDecimalValue();
  2267. DecMul();
  2268. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2269. pnt->Release();
  2270. break;
  2271. }
  2272. default:
  2273. throwUnexpected();
  2274. }
  2275. return ret;
  2276. }
  2277. IValue * divideValues(IValue * left, IValue * right, byte dbz)
  2278. {
  2279. Owned<ITypeInfo> pnt = getPromotedMulDivType(left->queryType(), right->queryType());
  2280. //Use a cast to a boolean as a shortcut for testing against zero
  2281. if (!right->getBoolValue())
  2282. {
  2283. //If no action is selected, return NULL so the expression doesn't get constant folded.
  2284. if (dbz == DBZnone)
  2285. return NULL;
  2286. if (dbz == DBZfail)
  2287. rtlFailDivideByZero();
  2288. }
  2289. switch(pnt->getTypeCode())
  2290. {
  2291. case type_int:
  2292. case type_swapint:
  2293. case type_packedint:
  2294. {
  2295. __int64 lv = left->getIntValue();
  2296. __int64 rv = right->getIntValue();
  2297. __int64 res = 0;
  2298. if (rv)
  2299. {
  2300. if (pnt->isSigned())
  2301. res = lv / rv;
  2302. else
  2303. res = (__int64)((unsigned __int64)lv / (unsigned __int64)rv);
  2304. }
  2305. return createTruncIntValue(res, pnt.getClear());
  2306. }
  2307. case type_real:
  2308. {
  2309. double lv = left->getRealValue();
  2310. double rv = right->getRealValue();
  2311. double res;
  2312. if (rv)
  2313. res = lv / rv;
  2314. else if (dbz == DBZnan)
  2315. res = rtlCreateRealInf();
  2316. else
  2317. res = 0.0;
  2318. return createRealValue(res, pnt.getClear());
  2319. }
  2320. case type_decimal:
  2321. {
  2322. BcdCriticalBlock bcdBlock;
  2323. left->pushDecimalValue();
  2324. right->pushDecimalValue();
  2325. DecDivide(dbz);
  2326. return createDecimalValueFromStack(pnt);
  2327. }
  2328. default:
  2329. throwUnexpected();
  2330. return NULL;
  2331. }
  2332. }
  2333. IValue * modulusValues(IValue * left, IValue * right, byte dbz)
  2334. {
  2335. Owned<ITypeInfo> pnt = getPromotedMulDivType(left->queryType(), right->queryType());
  2336. //Use a cast to a boolean as a shortcut for testing against zero
  2337. if (!right->getBoolValue())
  2338. {
  2339. //If no action is selected, return NULL so the expression doesn't get constant folded.
  2340. if (dbz == DBZnone)
  2341. return NULL;
  2342. if (dbz == DBZfail)
  2343. rtlFailDivideByZero();
  2344. }
  2345. switch(pnt->getTypeCode())
  2346. {
  2347. case type_int:
  2348. case type_swapint:
  2349. case type_packedint:
  2350. {
  2351. __int64 lv = left->getIntValue();
  2352. __int64 rv = right->getIntValue();
  2353. __int64 res = 0;
  2354. if (rv)
  2355. {
  2356. if (pnt->isSigned())
  2357. res = lv % rv;
  2358. else
  2359. res = (__int64)((unsigned __int64)lv % (unsigned __int64)rv);
  2360. }
  2361. return createTruncIntValue(res, pnt.getClear());
  2362. }
  2363. case type_real:
  2364. {
  2365. double rv = right->getRealValue();
  2366. double res;
  2367. if (rv)
  2368. res = fmod(left->getRealValue(), rv);
  2369. else if (dbz == DBZnan)
  2370. res = rtlCreateRealNull();
  2371. else
  2372. res = 0.0;
  2373. return createRealValue(res, pnt.getClear());
  2374. }
  2375. case type_decimal:
  2376. {
  2377. BcdCriticalBlock bcdBlock;
  2378. left->pushDecimalValue();
  2379. right->pushDecimalValue();
  2380. DecModulus(dbz);
  2381. return createDecimalValueFromStack(pnt);
  2382. }
  2383. default:
  2384. throwUnexpected();
  2385. return NULL;
  2386. }
  2387. }
  2388. IValue * powerValues(IValue * left, IValue * right)
  2389. {
  2390. IValue * ret;
  2391. ITypeInfo * pnt = makeRealType(8);
  2392. switch(pnt->getTypeCode())
  2393. {
  2394. case type_int:
  2395. case type_swapint:
  2396. case type_packedint:
  2397. case type_real:
  2398. ret = createRealValue(pow(left->getRealValue(), right->getRealValue()), pnt);
  2399. break;
  2400. /*
  2401. // TBD
  2402. case type_decimal:
  2403. BcdCriticalBlock bcdBlock;
  2404. left->pushDecimalValue();
  2405. DecLongPower(right->getIntValue());
  2406. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2407. pnt->Release();
  2408. break
  2409. */
  2410. default:
  2411. pnt->Release();
  2412. throwUnexpected();
  2413. }
  2414. return ret;
  2415. }
  2416. IValue * negateValue(IValue * v)
  2417. {
  2418. switch(v->getTypeCode())
  2419. {
  2420. case type_int:
  2421. case type_swapint:
  2422. case type_packedint:
  2423. {
  2424. __uint64 value = - (__uint64)v->getIntValue(); // avoid undefined behaviour if value = int_min
  2425. return createTruncIntValue((__int64)value, v->getType());
  2426. }
  2427. case type_real:
  2428. return createRealValue(-(v->getRealValue()), v->getSize());
  2429. case type_decimal:
  2430. {
  2431. BcdCriticalBlock bcdBlock;
  2432. v->pushDecimalValue();
  2433. DecNegate();
  2434. return ((CDecimalTypeInfo*)v->queryType())->createValueFromStack();
  2435. }
  2436. }
  2437. throwUnexpected();
  2438. return NULL;
  2439. }
  2440. IValue * expValue(IValue * v)
  2441. {
  2442. return createRealValue(exp(v->getRealValue()), 8);
  2443. }
  2444. IValue * roundUpValue(IValue * v)
  2445. {
  2446. switch(v->getTypeCode())
  2447. {
  2448. case type_int:
  2449. case type_swapint:
  2450. case type_packedint:
  2451. return LINK(v);
  2452. case type_real:
  2453. return createTruncIntValue(rtlRoundUp(v->getRealValue()), 8, true);
  2454. case type_decimal:
  2455. {
  2456. BcdCriticalBlock bcdBlock;
  2457. v->pushDecimalValue();
  2458. DecRoundUp();
  2459. OwnedITypeInfo resultType = getRoundType(v->queryType());
  2460. return createDecimalValueFromStack(resultType);
  2461. }
  2462. }
  2463. throwUnexpected();
  2464. return NULL;
  2465. }
  2466. IValue * roundValue(IValue * v)
  2467. {
  2468. switch(v->getTypeCode())
  2469. {
  2470. case type_int:
  2471. case type_swapint:
  2472. case type_packedint:
  2473. return LINK(v);
  2474. case type_real:
  2475. return createTruncIntValue(rtlRound(v->getRealValue()), 8, true);
  2476. case type_decimal:
  2477. {
  2478. BcdCriticalBlock bcdBlock;
  2479. v->pushDecimalValue();
  2480. DecRound();
  2481. Owned<ITypeInfo> resultType = getRoundType(v->queryType());
  2482. return createDecimalValueFromStack(resultType);
  2483. }
  2484. }
  2485. throwUnexpected();
  2486. return NULL;
  2487. }
  2488. IValue * roundToValue(IValue * v, int places)
  2489. {
  2490. switch(v->getTypeCode())
  2491. {
  2492. case type_int:
  2493. case type_swapint:
  2494. case type_packedint:
  2495. return createRealValue(rtlRoundTo(v->getRealValue(), places), 8);
  2496. case type_real:
  2497. return createRealValue(rtlRoundTo(v->getRealValue(), places), 8);
  2498. case type_decimal:
  2499. {
  2500. BcdCriticalBlock bcdBlock;
  2501. v->pushDecimalValue();
  2502. DecRoundTo(places);
  2503. OwnedITypeInfo resultType = getRoundToType(v->queryType());
  2504. return createDecimalValueFromStack(resultType);
  2505. }
  2506. }
  2507. throwUnexpected();
  2508. return NULL;
  2509. }
  2510. IValue * truncateValue(IValue * v)
  2511. {
  2512. switch(v->getTypeCode())
  2513. {
  2514. case type_int:
  2515. case type_swapint:
  2516. case type_packedint:
  2517. return LINK(v);
  2518. case type_real:
  2519. return createTruncIntValue(v->getIntValue(), 8, true);
  2520. case type_decimal:
  2521. {
  2522. BcdCriticalBlock bcdBlock;
  2523. v->pushDecimalValue();
  2524. DecTruncate();
  2525. OwnedITypeInfo resultType = getTruncType(v->queryType());
  2526. return createDecimalValueFromStack(resultType);
  2527. }
  2528. }
  2529. throwUnexpected();
  2530. return NULL;
  2531. }
  2532. IValue * lnValue(IValue * v, byte onZero)
  2533. {
  2534. return createRealValue(rtlLog(v->getRealValue(), onZero), 8);
  2535. }
  2536. IValue * sinValue(IValue * v)
  2537. {
  2538. return createRealValue(sin(v->getRealValue()), 8);
  2539. }
  2540. IValue * cosValue(IValue * v)
  2541. {
  2542. return createRealValue(cos(v->getRealValue()), 8);
  2543. }
  2544. IValue * tanValue(IValue * v)
  2545. {
  2546. return createRealValue(tan(v->getRealValue()), 8);
  2547. }
  2548. IValue * sinhValue(IValue * v)
  2549. {
  2550. return createRealValue(sinh(v->getRealValue()), 8);
  2551. }
  2552. IValue * coshValue(IValue * v)
  2553. {
  2554. return createRealValue(cosh(v->getRealValue()), 8);
  2555. }
  2556. IValue * tanhValue(IValue * v)
  2557. {
  2558. return createRealValue(tanh(v->getRealValue()), 8);
  2559. }
  2560. IValue * asinValue(IValue * v, byte onZero)
  2561. {
  2562. return createRealValue(rtlASin(v->getRealValue(), onZero), 8);
  2563. }
  2564. IValue * acosValue(IValue * v, byte onZero)
  2565. {
  2566. return createRealValue(rtlACos(v->getRealValue(), onZero), 8);
  2567. }
  2568. IValue * atanValue(IValue * v)
  2569. {
  2570. return createRealValue(atan(v->getRealValue()), 8);
  2571. }
  2572. IValue * atan2Value(IValue * y, IValue* x)
  2573. {
  2574. return createRealValue(atan2(y->getRealValue(), x->getRealValue()), 8);
  2575. }
  2576. IValue * log10Value(IValue * v, byte onZero)
  2577. {
  2578. return createRealValue(rtlLog10(v->getRealValue(), onZero), 8);
  2579. }
  2580. IValue * sqrtValue(IValue * v, byte onZero)
  2581. {
  2582. switch(v->getTypeCode())
  2583. {
  2584. case type_decimal:
  2585. //MORE: This should probably do this more accurately.
  2586. //fall into
  2587. case type_int:
  2588. case type_swapint:
  2589. case type_packedint:
  2590. case type_real:
  2591. return createRealValue(rtlSqrt(v->getRealValue(), onZero), 8);
  2592. }
  2593. throwUnexpected();
  2594. return NULL;
  2595. }
  2596. IValue * absValue(IValue * v)
  2597. {
  2598. switch(v->getTypeCode())
  2599. {
  2600. case type_int:
  2601. case type_swapint:
  2602. case type_packedint:
  2603. {
  2604. ITypeInfo * type = v->queryType();
  2605. if (type->isSigned())
  2606. {
  2607. __int64 val = v->getIntValue();
  2608. if (val < 0)
  2609. return createIntValue(-val, LINK(type));
  2610. }
  2611. return LINK(v);
  2612. }
  2613. case type_real:
  2614. return createRealValue(fabs(v->getRealValue()), v->getSize());
  2615. case type_decimal:
  2616. {
  2617. BcdCriticalBlock bcdBlock;
  2618. v->pushDecimalValue();
  2619. DecAbs();
  2620. return ((CDecimalTypeInfo*)v->queryType())->createValueFromStack();
  2621. }
  2622. }
  2623. throwUnexpected();
  2624. return NULL;
  2625. }
  2626. IValue * substringValue(IValue * v, IValue * lower, IValue * higher)
  2627. {
  2628. ITypeInfo * type = v->queryType();
  2629. unsigned srcLen = type->getStringLen();
  2630. const void * raw = v->queryValue();
  2631. unsigned low = lower ? (unsigned)lower->getIntValue() : 0;
  2632. unsigned high = higher ? (unsigned)higher->getIntValue() : srcLen;
  2633. unsigned retLen = 0;
  2634. void * retPtr;
  2635. ITypeInfo * retType = NULL;
  2636. switch (type->getTypeCode())
  2637. {
  2638. case type_string:
  2639. rtlSubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2640. break;
  2641. case type_varstring:
  2642. rtlSubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2643. retType = makeStringType(retLen, LINK(type->queryCharset()), LINK(type->queryCollation()));
  2644. break;
  2645. case type_data:
  2646. rtlSubDataFTX(retLen, retPtr, srcLen, raw, low, high);
  2647. break;
  2648. case type_qstring:
  2649. rtlSubQStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2650. break;
  2651. case type_unicode:
  2652. rtlUnicodeSubStrFTX(retLen, *(UChar * *)&retPtr, srcLen, (const UChar *)raw, low, high);
  2653. break;
  2654. case type_varunicode:
  2655. rtlUnicodeSubStrFTX(retLen, *(UChar * *)&retPtr, srcLen, (const UChar *)raw, low, high);
  2656. retType = makeUnicodeType(retLen, type->queryLocale());
  2657. break;
  2658. case type_utf8:
  2659. rtlUtf8SubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2660. break;
  2661. default:
  2662. UNIMPLEMENTED;
  2663. }
  2664. if (retType == NULL)
  2665. retType = getStretchedType(retLen, type);
  2666. IValue * ret = createValueFromMem(retType, retPtr);
  2667. rtlFree(retPtr);
  2668. return ret;
  2669. }
  2670. IValue * trimStringValue(IValue * v, char typecode, bool whitespace)
  2671. {
  2672. ITypeInfo * type = v->queryType();
  2673. type_t tc = type->getTypeCode();
  2674. if(isUnicodeType(type))
  2675. {
  2676. unsigned tlen = 0;
  2677. rtlDataAttr resultstr;
  2678. unsigned len = type->getStringLen();
  2679. if (tc == type_utf8)
  2680. {
  2681. char const * str = (char const *)v->queryValue();
  2682. if (whitespace)
  2683. rtlTrimUtf8WS(tlen, resultstr.refstr(), len, str, typecode=='B'||typecode=='L', typecode=='A', typecode=='B'||typecode=='R');
  2684. else switch(typecode)
  2685. {
  2686. case 'A':
  2687. rtlTrimUtf8All(tlen, resultstr.refstr(), len, str);
  2688. break;
  2689. case 'B':
  2690. rtlTrimUtf8Both(tlen, resultstr.refstr(), len, str);
  2691. break;
  2692. case 'L':
  2693. rtlTrimUtf8Left(tlen, resultstr.refstr(), len, str);
  2694. break;
  2695. default:
  2696. rtlTrimUtf8Right(tlen, resultstr.refstr(), len, str);
  2697. break;
  2698. }
  2699. ITypeInfo * newtype = makeUtf8Type(tlen, type->queryLocale());
  2700. return createUtf8Value(tlen, resultstr.getstr(), newtype);
  2701. }
  2702. else
  2703. {
  2704. UChar const * str = (UChar const *)v->queryValue();
  2705. if (whitespace)
  2706. rtlTrimUnicodeWS(tlen, resultstr.refustr(), len, str, typecode=='B'||typecode=='L', typecode=='A', typecode=='B'||typecode=='R');
  2707. else switch(typecode)
  2708. {
  2709. case 'A':
  2710. rtlTrimUnicodeAll(tlen, resultstr.refustr(), len, str);
  2711. break;
  2712. case 'B':
  2713. rtlTrimUnicodeBoth(tlen, resultstr.refustr(), len, str);
  2714. break;
  2715. case 'L':
  2716. rtlTrimUnicodeLeft(tlen, resultstr.refustr(), len, str);
  2717. break;
  2718. default:
  2719. rtlTrimUnicodeRight(tlen, resultstr.refustr(), len, str);
  2720. break;
  2721. }
  2722. ITypeInfo * newtype = makeUnicodeType(tlen, v->queryType()->queryLocale());
  2723. return createUnicodeValue(resultstr.getustr(), tlen, newtype);
  2724. }
  2725. }
  2726. else
  2727. {
  2728. Owned<ITypeInfo> st = getStringType(type);
  2729. Owned<ITypeInfo> asciiType = getAsciiType(st);
  2730. Owned<IValue> sv = v->castTo(asciiType);
  2731. StringBuffer s;
  2732. sv->getStringValue(s);
  2733. unsigned tlen = 0;
  2734. rtlDataAttr resultstr;
  2735. unsigned len = s.length();
  2736. char const * str = s.str();
  2737. if (whitespace)
  2738. rtlTrimWS(tlen, resultstr.refstr(), len, str, typecode=='B'||typecode=='L', typecode=='A', typecode=='B'||typecode=='R');
  2739. else switch(typecode)
  2740. {
  2741. case 'A':
  2742. rtlTrimAll(tlen, resultstr.refstr(), len, str);
  2743. break;
  2744. case 'B':
  2745. rtlTrimBoth(tlen, resultstr.refstr(), len, str);
  2746. break;
  2747. case 'L':
  2748. rtlTrimLeft(tlen, resultstr.refstr(), len, str);
  2749. break;
  2750. default:
  2751. rtlTrimRight(tlen, resultstr.refstr(), len, str);
  2752. break;
  2753. }
  2754. ITypeInfo * newtype = makeStringType(tlen, LINK(asciiType->queryCharset()), LINK(asciiType->queryCollation()));
  2755. return createStringValue(resultstr.getstr(), newtype);
  2756. }
  2757. }
  2758. //---------------------------------------------------------------------
  2759. void getStringFromIValue(StringBuffer & s, IValue* val)
  2760. {
  2761. Owned<ITypeInfo> tp = getStringType(val->queryType());
  2762. Owned<IValue> sv = val->castTo(tp);
  2763. sv->getStringValue(s);
  2764. }
  2765. void getStringFromIValue(unsigned & len, char* & str, IValue* val)
  2766. {
  2767. StringBuffer s;
  2768. getStringFromIValue(s, val);
  2769. len = s.length();
  2770. str = s.detach();
  2771. }
  2772. //---------------------------------------------------------------------
  2773. IValue * concatValues(IValue * left, IValue * right)
  2774. {
  2775. ITypeInfo * leftType = left->queryType();
  2776. ITypeInfo * rightType = right->queryType();
  2777. type_t ltc = leftType->getTypeCode();
  2778. type_t rtc = rightType->getTypeCode();
  2779. if(isUnicodeType(leftType))
  2780. {
  2781. assertex(isUnicodeType(rightType));
  2782. assertex(leftType->queryLocale() == rightType->queryLocale());
  2783. rtlDataAttr out;
  2784. unsigned outlen;
  2785. if (ltc == type_utf8 && rtc == type_utf8)
  2786. {
  2787. rtlConcatUtf8(outlen, out.addrstr(), leftType->getStringLen(), (char const *)left->queryValue(), rightType->getStringLen(), (char const *)right->queryValue(), -1);
  2788. ITypeInfo * newtype = makeUtf8Type(outlen, leftType->queryLocale());
  2789. return createUtf8Value(outlen, out.getstr(), newtype);
  2790. }
  2791. else
  2792. {
  2793. rtlConcatUnicode(outlen, out.addrustr(), leftType->getStringLen(), (UChar const *)left->queryValue(), rightType->getStringLen(), (UChar const *)right->queryValue(), -1);
  2794. ITypeInfo * newtype = makeUnicodeType(outlen, leftType->queryLocale());
  2795. return createUnicodeValue(out.getustr(), outlen, newtype);
  2796. }
  2797. }
  2798. else
  2799. {
  2800. Owned<ITypeInfo> lt = getStringType(leftType);
  2801. Owned<ITypeInfo> rt = getStringType(rightType);
  2802. Owned<IValue> lv = left->castTo(lt);
  2803. Owned<IValue> rv = right->castTo(rt);
  2804. assertex(!isUnicodeType(rt));
  2805. assertex(lt->queryCharset() == rt->queryCharset());
  2806. assertex(lt->queryCollation() == rt->queryCollation());
  2807. size32_t len = lt->getStringLen() + rt->getStringLen();
  2808. StringBuffer s;
  2809. lv->getStringValue(s);
  2810. rv->getStringValue(s);
  2811. if (ltc == type_varstring || rtc == type_varstring)
  2812. {
  2813. ITypeInfo * newtype = makeVarStringType(len);
  2814. return createVarStringValue(len, s.str(), newtype);
  2815. }
  2816. else if (ltc == type_string || rtc == type_string)
  2817. {
  2818. ITypeInfo * newtype = makeStringType(len, LINK(lt->queryCharset()), LINK(lt->queryCollation()));
  2819. return createStringValue(s.str(), newtype);
  2820. }
  2821. else
  2822. {
  2823. return createDataValue(s.str(), len);
  2824. }
  2825. }
  2826. }
  2827. IValue * binaryAndValues(IValue * left, IValue * right)
  2828. {
  2829. IValue * ret;
  2830. Owned<ITypeInfo> pnt = getBandType(left->queryType(), right->queryType());
  2831. switch(pnt->getTypeCode())
  2832. {
  2833. case type_boolean:
  2834. return createBoolValue(left->getBoolValue() && right->getBoolValue());
  2835. case type_int:
  2836. case type_swapint:
  2837. case type_packedint:
  2838. ret = createTruncIntValue(left->getIntValue() & right->getIntValue(), pnt.getClear());
  2839. break;
  2840. default:
  2841. throwUnexpected();
  2842. }
  2843. return ret;
  2844. }
  2845. IValue * binaryOrValues(IValue * left, IValue * right)
  2846. {
  2847. IValue * ret;
  2848. Owned<ITypeInfo> pnt = getBorType(left->queryType(), right->queryType());
  2849. switch(pnt->getTypeCode())
  2850. {
  2851. case type_boolean:
  2852. return createBoolValue(left->getBoolValue() || right->getBoolValue());
  2853. case type_int:
  2854. case type_swapint:
  2855. case type_packedint:
  2856. ret = createTruncIntValue(left->getIntValue() | right->getIntValue(), pnt.getClear());
  2857. break;
  2858. default:
  2859. throwUnexpected();
  2860. }
  2861. return ret;
  2862. }
  2863. IValue * binaryXorValues(IValue * left, IValue * right)
  2864. {
  2865. IValue * ret;
  2866. ITypeInfo * pnt = getPromotedNumericType(left->queryType(), right->queryType());
  2867. switch(pnt->getTypeCode())
  2868. {
  2869. case type_int:
  2870. case type_swapint:
  2871. case type_packedint:
  2872. ret = createTruncIntValue(left->getIntValue() ^ right->getIntValue(), pnt);
  2873. break;
  2874. default:
  2875. throwUnexpected();
  2876. }
  2877. return ret;
  2878. }
  2879. IValue * binaryNotValues(IValue * v)
  2880. {
  2881. switch(v->getTypeCode())
  2882. {
  2883. case type_int:
  2884. case type_swapint:
  2885. case type_packedint:
  2886. return createTruncIntValue(~v->getIntValue(), v->getType());
  2887. }
  2888. throwUnexpected();
  2889. return NULL;
  2890. }
  2891. IValue * logicalNotValues(IValue * v)
  2892. {
  2893. return createBoolValue(!v->getBoolValue());
  2894. }
  2895. IValue * logicalAndValues(IValue * left, IValue * right)
  2896. {
  2897. return createBoolValue(left->getBoolValue() && right->getBoolValue());
  2898. }
  2899. IValue * logicalOrValues(IValue * left, IValue * right)
  2900. {
  2901. return createBoolValue(left->getBoolValue() || right->getBoolValue());
  2902. }
  2903. int orderValues(IValue * left, IValue * right)
  2904. {
  2905. //The following line can be uncommented to check that the types are consistent everywhere
  2906. //but remains commented out to improve resilience when the types are wrong.
  2907. // return left->compare(right);
  2908. Owned<ITypeInfo> pt = getPromotedCompareType(left->queryType(), right->queryType());
  2909. Owned<IValue> lv = left->castTo(pt);
  2910. Owned<IValue> rv = right->castTo(pt);
  2911. return lv->compare(rv);
  2912. }
  2913. #define COMPARE_AND_RETURN(op) \
  2914. return createBoolValue(orderValues(left, right) op 0);
  2915. IValue * equalValues(IValue * left, IValue * right)
  2916. {
  2917. COMPARE_AND_RETURN(==)
  2918. }
  2919. IValue * notEqualValues(IValue * left, IValue * right)
  2920. {
  2921. COMPARE_AND_RETURN(!=)
  2922. }
  2923. IValue * lessValues(IValue * left, IValue * right)
  2924. {
  2925. COMPARE_AND_RETURN(<)
  2926. }
  2927. IValue * lessEqualValues(IValue * left, IValue * right)
  2928. {
  2929. COMPARE_AND_RETURN(<=)
  2930. }
  2931. IValue * greaterValues(IValue * left, IValue * right)
  2932. {
  2933. COMPARE_AND_RETURN(>)
  2934. }
  2935. IValue * greaterEqualValues(IValue * left, IValue * right)
  2936. {
  2937. COMPARE_AND_RETURN(>=)
  2938. }
  2939. IValue * shiftLeftValues(IValue * left, IValue * right)
  2940. {
  2941. ITypeInfo * retType = left->getType();
  2942. switch(retType->getTypeCode())
  2943. {
  2944. case type_int:
  2945. case type_swapint:
  2946. case type_packedint:
  2947. {
  2948. __uint64 value = (__uint64)left->getIntValue() << (__uint64)right->getIntValue();
  2949. return createTruncIntValue((__int64)value, retType);
  2950. }
  2951. default:
  2952. UNIMPLEMENTED;
  2953. }
  2954. }
  2955. IValue * shiftRightValues(IValue * left, IValue * right)
  2956. {
  2957. ITypeInfo * retType = left->getType();
  2958. switch(retType->getTypeCode())
  2959. {
  2960. case type_int:
  2961. case type_swapint:
  2962. case type_packedint:
  2963. if (retType->isSigned())
  2964. return createTruncIntValue(((__int64)left->getIntValue()) >> right->getIntValue(), retType);
  2965. else
  2966. return createTruncIntValue(((unsigned __int64)left->getIntValue()) >> right->getIntValue(), retType);
  2967. default:
  2968. UNIMPLEMENTED;
  2969. }
  2970. }
  2971. extern DEFTYPE_API void serializeValue(MemoryBuffer & target, IValue * value)
  2972. {
  2973. ITypeInfo * type = value->queryType();
  2974. type->serialize(target);
  2975. void * buffer = target.reserve(type->getSize());
  2976. value->toMem(buffer);
  2977. }
  2978. extern DEFTYPE_API IValue * deserializeValue(MemoryBuffer & source)
  2979. {
  2980. Owned<ITypeInfo> type = deserializeType(source);
  2981. const void * buffer = source.readDirect(type->getSize());
  2982. return createValueFromMem(LINK(type), buffer);
  2983. }