ncnv.pas 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totypedef : tdef;
  29. totypedefderef : tderef;
  30. convtype : tconverttype;
  31. warn_pointer_to_signed: boolean;
  32. constructor create(node : tnode;def:tdef);virtual;
  33. constructor create_explicit(node : tnode;def:tdef);
  34. constructor create_internal(node : tnode;def:tdef);
  35. constructor create_proc_to_procvar(node : tnode);
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. function dogetcopy : tnode;override;
  41. function actualtargetnode: tnode;override;
  42. procedure printnodeinfo(var t : text);override;
  43. function pass_1 : tnode;override;
  44. function pass_typecheck:tnode;override;
  45. function simplify:tnode; override;
  46. procedure mark_write;override;
  47. function docompare(p: tnode) : boolean; override;
  48. function assign_allowed:boolean;
  49. procedure second_call_helper(c : tconverttype);
  50. private
  51. function typecheck_int_to_int : tnode;
  52. function typecheck_cord_to_pointer : tnode;
  53. function typecheck_chararray_to_string : tnode;
  54. function typecheck_string_to_chararray : tnode;
  55. function typecheck_string_to_string : tnode;
  56. function typecheck_char_to_string : tnode;
  57. function typecheck_char_to_chararray : tnode;
  58. function typecheck_int_to_real : tnode;
  59. function typecheck_real_to_real : tnode;
  60. function typecheck_real_to_currency : tnode;
  61. function typecheck_cchar_to_pchar : tnode;
  62. function typecheck_cstring_to_pchar : tnode;
  63. function typecheck_cstring_to_int : tnode;
  64. function typecheck_char_to_char : tnode;
  65. function typecheck_arrayconstructor_to_set : tnode;
  66. function typecheck_set_to_set : tnode;
  67. function typecheck_pchar_to_string : tnode;
  68. function typecheck_interface_to_guid : tnode;
  69. function typecheck_dynarray_to_openarray : tnode;
  70. function typecheck_pwchar_to_string : tnode;
  71. function typecheck_variant_to_dynarray : tnode;
  72. function typecheck_dynarray_to_variant : tnode;
  73. function typecheck_call_helper(c : tconverttype) : tnode;
  74. function typecheck_variant_to_enum : tnode;
  75. function typecheck_enum_to_variant : tnode;
  76. function typecheck_proc_to_procvar : tnode;
  77. function typecheck_variant_to_interface : tnode;
  78. function typecheck_interface_to_variant : tnode;
  79. function typecheck_array_2_dynarray : tnode;
  80. protected
  81. function first_int_to_int : tnode;virtual;
  82. function first_cstring_to_pchar : tnode;virtual;
  83. function first_cstring_to_int : tnode;virtual;
  84. function first_string_to_chararray : tnode;virtual;
  85. function first_char_to_string : tnode;virtual;
  86. function first_nothing : tnode;virtual;
  87. function first_array_to_pointer : tnode;virtual;
  88. function first_int_to_real : tnode;virtual;
  89. function first_real_to_real : tnode;virtual;
  90. function first_pointer_to_array : tnode;virtual;
  91. function first_cchar_to_pchar : tnode;virtual;
  92. function first_bool_to_int : tnode;virtual;
  93. function first_int_to_bool : tnode;virtual;
  94. function first_bool_to_bool : tnode;virtual;
  95. function first_proc_to_procvar : tnode;virtual;
  96. function first_nil_to_methodprocvar : tnode;virtual;
  97. function first_set_to_set : tnode;virtual;
  98. function first_cord_to_pointer : tnode;virtual;
  99. function first_ansistring_to_pchar : tnode;virtual;
  100. function first_arrayconstructor_to_set : tnode;virtual;
  101. function first_class_to_intf : tnode;virtual;
  102. function first_char_to_char : tnode;virtual;
  103. function first_call_helper(c : tconverttype) : tnode;
  104. { these wrapper are necessary, because the first_* stuff is called }
  105. { through a table. Without the wrappers override wouldn't have }
  106. { any effect }
  107. function _first_int_to_int : tnode;
  108. function _first_cstring_to_pchar : tnode;
  109. function _first_cstring_to_int : tnode;
  110. function _first_string_to_chararray : tnode;
  111. function _first_char_to_string : tnode;
  112. function _first_nothing : tnode;
  113. function _first_array_to_pointer : tnode;
  114. function _first_int_to_real : tnode;
  115. function _first_real_to_real: tnode;
  116. function _first_pointer_to_array : tnode;
  117. function _first_cchar_to_pchar : tnode;
  118. function _first_bool_to_int : tnode;
  119. function _first_int_to_bool : tnode;
  120. function _first_bool_to_bool : tnode;
  121. function _first_proc_to_procvar : tnode;
  122. function _first_nil_to_methodprocvar : tnode;
  123. function _first_cord_to_pointer : tnode;
  124. function _first_ansistring_to_pchar : tnode;
  125. function _first_arrayconstructor_to_set : tnode;
  126. function _first_class_to_intf : tnode;
  127. function _first_char_to_char : tnode;
  128. function _first_set_to_set : tnode;
  129. procedure _second_int_to_int;virtual;
  130. procedure _second_string_to_string;virtual;
  131. procedure _second_cstring_to_pchar;virtual;
  132. procedure _second_cstring_to_int;virtual;
  133. procedure _second_string_to_chararray;virtual;
  134. procedure _second_array_to_pointer;virtual;
  135. procedure _second_pointer_to_array;virtual;
  136. procedure _second_chararray_to_string;virtual;
  137. procedure _second_char_to_string;virtual;
  138. procedure _second_int_to_real;virtual;
  139. procedure _second_real_to_real;virtual;
  140. procedure _second_cord_to_pointer;virtual;
  141. procedure _second_proc_to_procvar;virtual;
  142. procedure _second_nil_to_methodprocvar;virtual;
  143. procedure _second_bool_to_int;virtual;
  144. procedure _second_int_to_bool;virtual;
  145. procedure _second_bool_to_bool;virtual;
  146. procedure _second_set_to_set;virtual;
  147. procedure _second_ansistring_to_pchar;virtual;
  148. procedure _second_class_to_intf;virtual;
  149. procedure _second_char_to_char;virtual;
  150. procedure _second_nothing; virtual;
  151. procedure second_int_to_int;virtual;abstract;
  152. procedure second_string_to_string;virtual;abstract;
  153. procedure second_cstring_to_pchar;virtual;abstract;
  154. procedure second_cstring_to_int;virtual;abstract;
  155. procedure second_string_to_chararray;virtual;abstract;
  156. procedure second_array_to_pointer;virtual;abstract;
  157. procedure second_pointer_to_array;virtual;abstract;
  158. procedure second_chararray_to_string;virtual;abstract;
  159. procedure second_char_to_string;virtual;abstract;
  160. procedure second_int_to_real;virtual;abstract;
  161. procedure second_real_to_real;virtual;abstract;
  162. procedure second_cord_to_pointer;virtual;abstract;
  163. procedure second_proc_to_procvar;virtual;abstract;
  164. procedure second_nil_to_methodprocvar;virtual;abstract;
  165. procedure second_bool_to_int;virtual;abstract;
  166. procedure second_int_to_bool;virtual;abstract;
  167. procedure second_bool_to_bool;virtual;abstract;
  168. procedure second_set_to_set;virtual;abstract;
  169. procedure second_ansistring_to_pchar;virtual;abstract;
  170. procedure second_class_to_intf;virtual;abstract;
  171. procedure second_char_to_char;virtual;abstract;
  172. procedure second_nothing; virtual;abstract;
  173. end;
  174. ttypeconvnodeclass = class of ttypeconvnode;
  175. tasnode = class(tbinarynode)
  176. constructor create(l,r : tnode);virtual;
  177. function pass_1 : tnode;override;
  178. function pass_typecheck:tnode;override;
  179. function dogetcopy: tnode;override;
  180. destructor destroy; override;
  181. call: tnode;
  182. end;
  183. tasnodeclass = class of tasnode;
  184. tisnode = class(tbinarynode)
  185. constructor create(l,r : tnode);virtual;
  186. function pass_1 : tnode;override;
  187. function pass_typecheck:tnode;override;
  188. procedure pass_generate_code;override;
  189. end;
  190. tisnodeclass = class of tisnode;
  191. var
  192. ctypeconvnode : ttypeconvnodeclass;
  193. casnode : tasnodeclass;
  194. cisnode : tisnodeclass;
  195. procedure inserttypeconv(var p:tnode;def:tdef);
  196. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  197. procedure arrayconstructor_to_set(var p : tnode);
  198. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  199. implementation
  200. uses
  201. globtype,systems,constexp,
  202. cutils,verbose,globals,widestr,
  203. symconst,symdef,symsym,symbase,symtable,
  204. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  205. cgbase,procinfo,
  206. htypechk,pass_1,cpuinfo;
  207. {*****************************************************************************
  208. Helpers
  209. *****************************************************************************}
  210. procedure inserttypeconv(var p:tnode;def:tdef);
  211. begin
  212. if not assigned(p.resultdef) then
  213. begin
  214. typecheckpass(p);
  215. if codegenerror then
  216. exit;
  217. end;
  218. { don't insert obsolete type conversions }
  219. if equal_defs(p.resultdef,def) then
  220. p.resultdef:=def
  221. else
  222. begin
  223. p:=ctypeconvnode.create(p,def);
  224. typecheckpass(p);
  225. end;
  226. end;
  227. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  228. begin
  229. if not assigned(p.resultdef) then
  230. begin
  231. typecheckpass(p);
  232. if codegenerror then
  233. exit;
  234. end;
  235. { don't insert obsolete type conversions }
  236. if equal_defs(p.resultdef,def) then
  237. p.resultdef:=def
  238. else
  239. begin
  240. p:=ctypeconvnode.create_internal(p,def);
  241. typecheckpass(p);
  242. end;
  243. end;
  244. {*****************************************************************************
  245. Array constructor to Set Conversion
  246. *****************************************************************************}
  247. procedure arrayconstructor_to_set(var p : tnode);
  248. var
  249. constp : tsetconstnode;
  250. buildp,
  251. p2,p3,p4 : tnode;
  252. hdef : tdef;
  253. constset : Pconstset;
  254. constsetlo,
  255. constsethi : TConstExprInt;
  256. procedure update_constsethi(def:tdef; maybetruncenumrange: boolean);
  257. begin
  258. if (def.typ=orddef) and
  259. ((torddef(def).high>=constsethi) or
  260. (torddef(def).low <=constsetlo)) then
  261. begin
  262. if torddef(def).ordtype=uwidechar then
  263. begin
  264. constsethi:=255;
  265. constsetlo:=0;
  266. if hdef=nil then
  267. hdef:=def;
  268. end
  269. else
  270. begin
  271. if (torddef(def).high>=constsethi) then
  272. constsethi:=torddef(def).high;
  273. if (torddef(def).low<=constsetlo) then
  274. constsetlo:=torddef(def).low;
  275. if hdef=nil then
  276. begin
  277. if (constsethi>255) or
  278. (torddef(def).low<0) then
  279. hdef:=u8inttype
  280. else
  281. hdef:=def;
  282. end;
  283. if constsethi>255 then
  284. constsethi:=255;
  285. if constsetlo<0 then
  286. constsetlo:=0;
  287. end;
  288. end
  289. else if (def.typ=enumdef) and
  290. ((tenumdef(def).max>=constsethi) or
  291. (tenumdef(def).min<=constsetlo)) then
  292. begin
  293. if hdef=nil then
  294. hdef:=def;
  295. if (tenumdef(def).max>=constsethi) then
  296. constsethi:=tenumdef(def).max;
  297. if (tenumdef(def).min<=constsetlo) then
  298. constsetlo:=tenumdef(def).min;
  299. { for constant set elements, delphi allows the usage of elements of enumerations which
  300. have value>255 if there is no element with a value > 255 used }
  301. if (maybetruncenumrange) and
  302. (m_delphi in current_settings.modeswitches) then
  303. begin
  304. if constsethi>255 then
  305. constsethi:=255;
  306. if constsetlo<0 then
  307. constsetlo:=0;
  308. end;
  309. end;
  310. end;
  311. procedure do_set(pos : longint);
  312. begin
  313. if (pos and not $ff)<>0 then
  314. Message(parser_e_illegal_set_expr);
  315. if pos>constsethi then
  316. constsethi:=pos;
  317. if pos<constsetlo then
  318. constsetlo:=pos;
  319. if pos in constset^ then
  320. Message(parser_e_illegal_set_expr);
  321. include(constset^,pos);
  322. end;
  323. var
  324. l : Longint;
  325. lr,hr : TConstExprInt;
  326. hp : tarrayconstructornode;
  327. begin
  328. if p.nodetype<>arrayconstructorn then
  329. internalerror(200205105);
  330. new(constset);
  331. constset^:=[];
  332. hdef:=nil;
  333. { make sure to set constsetlo correctly for empty sets }
  334. if assigned(tarrayconstructornode(p).left) then
  335. constsetlo:=high(aint)
  336. else
  337. constsetlo:=0;
  338. constsethi:=0;
  339. constp:=csetconstnode.create(nil,hdef);
  340. constp.value_set:=constset;
  341. buildp:=constp;
  342. hp:=tarrayconstructornode(p);
  343. if assigned(hp.left) then
  344. begin
  345. while assigned(hp) do
  346. begin
  347. p4:=nil; { will contain the tree to create the set }
  348. {split a range into p2 and p3 }
  349. if hp.left.nodetype=arrayconstructorrangen then
  350. begin
  351. p2:=tarrayconstructorrangenode(hp.left).left;
  352. p3:=tarrayconstructorrangenode(hp.left).right;
  353. tarrayconstructorrangenode(hp.left).left:=nil;
  354. tarrayconstructorrangenode(hp.left).right:=nil;
  355. end
  356. else
  357. begin
  358. p2:=hp.left;
  359. hp.left:=nil;
  360. p3:=nil;
  361. end;
  362. typecheckpass(p2);
  363. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  364. if assigned(p3) then
  365. begin
  366. typecheckpass(p3);
  367. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  368. end;
  369. if codegenerror then
  370. break;
  371. current_filepos:=p2.fileinfo;
  372. case p2.resultdef.typ of
  373. enumdef,
  374. orddef:
  375. begin
  376. { widechars are not yet supported }
  377. if is_widechar(p2.resultdef) then
  378. begin
  379. inserttypeconv(p2,cchartype);
  380. if (p2.nodetype<>ordconstn) then
  381. incompatibletypes(cwidechartype,cchartype);
  382. end;
  383. getrange(p2.resultdef,lr,hr);
  384. if assigned(p3) then
  385. begin
  386. if is_widechar(p3.resultdef) then
  387. begin
  388. inserttypeconv(p3,cchartype);
  389. if (p3.nodetype<>ordconstn) then
  390. begin
  391. current_filepos:=p3.fileinfo;
  392. incompatibletypes(cwidechartype,cchartype);
  393. end;
  394. end;
  395. { this isn't good, you'll get problems with
  396. type t010 = 0..10;
  397. ts = set of t010;
  398. var s : ts;b : t010
  399. begin s:=[1,2,b]; end.
  400. if is_integer(p3^.resultdef) then
  401. begin
  402. inserttypeconv(p3,u8bitdef);
  403. end;
  404. }
  405. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  406. begin
  407. CGMessagePos(p3.fileinfo,type_e_typeconflict_in_set);
  408. end
  409. else
  410. begin
  411. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  412. begin
  413. if not(is_integer(p3.resultdef)) then
  414. hdef:=p3.resultdef
  415. else
  416. begin
  417. inserttypeconv(p3,u8inttype);
  418. inserttypeconv(p2,u8inttype);
  419. end;
  420. for l:=tordconstnode(p2).value.svalue to tordconstnode(p3).value.svalue do
  421. do_set(l);
  422. p2.free;
  423. p3.free;
  424. end
  425. else
  426. begin
  427. update_constsethi(p2.resultdef,false);
  428. inserttypeconv(p2,hdef);
  429. update_constsethi(p3.resultdef,false);
  430. inserttypeconv(p3,hdef);
  431. if assigned(hdef) then
  432. inserttypeconv(p3,hdef)
  433. else
  434. inserttypeconv(p3,u8inttype);
  435. p4:=csetelementnode.create(p2,p3);
  436. end;
  437. end;
  438. end
  439. else
  440. begin
  441. { Single value }
  442. if p2.nodetype=ordconstn then
  443. begin
  444. if not(is_integer(p2.resultdef)) then
  445. update_constsethi(p2.resultdef,true);
  446. if assigned(hdef) then
  447. inserttypeconv(p2,hdef)
  448. else
  449. inserttypeconv(p2,u8inttype);
  450. do_set(tordconstnode(p2).value.svalue);
  451. p2.free;
  452. end
  453. else
  454. begin
  455. update_constsethi(p2.resultdef,false);
  456. if assigned(hdef) then
  457. inserttypeconv(p2,hdef)
  458. else
  459. inserttypeconv(p2,u8inttype);
  460. p4:=csetelementnode.create(p2,nil);
  461. end;
  462. end;
  463. end;
  464. stringdef :
  465. begin
  466. { if we've already set elements which are constants }
  467. { throw an error }
  468. if ((hdef=nil) and assigned(buildp)) or
  469. not(is_char(hdef)) then
  470. CGMessage(type_e_typeconflict_in_set)
  471. else
  472. for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
  473. do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
  474. if hdef=nil then
  475. hdef:=cchartype;
  476. p2.free;
  477. end;
  478. else
  479. CGMessage(type_e_ordinal_expr_expected);
  480. end;
  481. { insert the set creation tree }
  482. if assigned(p4) then
  483. buildp:=caddnode.create(addn,buildp,p4);
  484. { load next and dispose current node }
  485. p2:=hp;
  486. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  487. tarrayconstructornode(p2).right:=nil;
  488. p2.free;
  489. end;
  490. if (hdef=nil) then
  491. hdef:=u8inttype;
  492. end
  493. else
  494. begin
  495. { empty set [], only remove node }
  496. p.free;
  497. end;
  498. { set the initial set type }
  499. constp.resultdef:=tsetdef.create(hdef,constsetlo.svalue,constsethi.svalue);
  500. { determine the resultdef for the tree }
  501. typecheckpass(buildp);
  502. { set the new tree }
  503. p:=buildp;
  504. end;
  505. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  506. begin
  507. if not(iscvarargs) and
  508. (p.nodetype=stringconstn) then
  509. p:=ctypeconvnode.create_internal(p,cansistringtype)
  510. else
  511. case p.resultdef.typ of
  512. enumdef :
  513. p:=ctypeconvnode.create_internal(p,s32inttype);
  514. arraydef :
  515. begin
  516. if is_chararray(p.resultdef) then
  517. p:=ctypeconvnode.create_internal(p,charpointertype)
  518. else
  519. if is_widechararray(p.resultdef) then
  520. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  521. else
  522. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  523. end;
  524. orddef :
  525. begin
  526. if is_integer(p.resultdef) and
  527. not(is_64bitint(p.resultdef)) then
  528. p:=ctypeconvnode.create(p,s32inttype)
  529. else if is_void(p.resultdef) then
  530. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  531. else if iscvarargs and is_currency(p.resultdef)
  532. and (current_settings.fputype<>fpu_none) then
  533. p:=ctypeconvnode.create(p,s64floattype);
  534. end;
  535. floatdef :
  536. if not(iscvarargs) then
  537. begin
  538. if not(is_currency(p.resultdef)) then
  539. p:=ctypeconvnode.create(p,pbestrealtype^);
  540. end
  541. else
  542. begin
  543. if is_constrealnode(p) and
  544. not(nf_explicit in p.flags) then
  545. MessagePos(p.fileinfo,type_w_double_c_varargs);
  546. if (tfloatdef(p.resultdef).floattype in [s32real,s64currency]) or
  547. (is_constrealnode(p) and
  548. not(nf_explicit in p.flags)) then
  549. p:=ctypeconvnode.create(p,s64floattype);
  550. end;
  551. procvardef :
  552. p:=ctypeconvnode.create(p,voidpointertype);
  553. stringdef:
  554. if iscvarargs then
  555. p:=ctypeconvnode.create(p,charpointertype);
  556. variantdef:
  557. if iscvarargs then
  558. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  559. pointerdef:
  560. ;
  561. classrefdef:
  562. if iscvarargs then
  563. p:=ctypeconvnode.create(p,voidpointertype);
  564. objectdef :
  565. if iscvarargs or
  566. is_object(p.resultdef) then
  567. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  568. else
  569. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  570. end;
  571. typecheckpass(p);
  572. end;
  573. {*****************************************************************************
  574. TTYPECONVNODE
  575. *****************************************************************************}
  576. constructor ttypeconvnode.create(node : tnode;def:tdef);
  577. begin
  578. inherited create(typeconvn,node);
  579. convtype:=tc_none;
  580. totypedef:=def;
  581. if def=nil then
  582. internalerror(200103281);
  583. fileinfo:=node.fileinfo;
  584. {An attempt to convert the result of a floating point division
  585. (with the / operator) to an integer type will fail. Give a hint
  586. to use the div operator.}
  587. if (node.nodetype=slashn) and (def.typ=orddef) then
  588. cgmessage(type_h_use_div_for_int);
  589. {In expressions like int64:=longint+longint, an integer overflow could be avoided
  590. by simply converting the operands to int64 first. Give a hint to do this.}
  591. if (node.nodetype in [addn,subn,muln]) and
  592. (def.typ=orddef) and (node.resultdef<>nil) and (node.resultdef.typ=orddef) and
  593. ((Torddef(node.resultdef).low>=Torddef(def).low) and (Torddef(node.resultdef).high<=Torddef(def).high)) and
  594. ((Torddef(node.resultdef).low>Torddef(def).low) or (Torddef(node.resultdef).high<Torddef(def).high)) then
  595. case node.nodetype of
  596. addn:
  597. cgmessage1(type_h_convert_add_operands_to_prevent_overflow,def.gettypename);
  598. subn:
  599. cgmessage1(type_h_convert_sub_operands_to_prevent_overflow,def.gettypename);
  600. muln:
  601. cgmessage1(type_h_convert_mul_operands_to_prevent_overflow,def.gettypename);
  602. end;
  603. end;
  604. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  605. begin
  606. self.create(node,def);
  607. include(flags,nf_explicit);
  608. end;
  609. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  610. begin
  611. self.create(node,def);
  612. { handle like explicit conversions }
  613. include(flags,nf_explicit);
  614. include(flags,nf_internal);
  615. end;
  616. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  617. begin
  618. self.create(node,voidtype);
  619. convtype:=tc_proc_2_procvar;
  620. end;
  621. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  622. begin
  623. inherited ppuload(t,ppufile);
  624. ppufile.getderef(totypedefderef);
  625. convtype:=tconverttype(ppufile.getbyte);
  626. end;
  627. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  628. begin
  629. inherited ppuwrite(ppufile);
  630. ppufile.putderef(totypedefderef);
  631. ppufile.putbyte(byte(convtype));
  632. end;
  633. procedure ttypeconvnode.buildderefimpl;
  634. begin
  635. inherited buildderefimpl;
  636. totypedefderef.build(totypedef);
  637. end;
  638. procedure ttypeconvnode.derefimpl;
  639. begin
  640. inherited derefimpl;
  641. totypedef:=tdef(totypedefderef.resolve);
  642. end;
  643. function ttypeconvnode.dogetcopy : tnode;
  644. var
  645. n : ttypeconvnode;
  646. begin
  647. n:=ttypeconvnode(inherited dogetcopy);
  648. n.convtype:=convtype;
  649. n.totypedef:=totypedef;
  650. dogetcopy:=n;
  651. end;
  652. procedure ttypeconvnode.printnodeinfo(var t : text);
  653. const
  654. convtyp2str : array[tconverttype] of pchar = (
  655. 'tc_none',
  656. 'tc_equal',
  657. 'tc_not_possible',
  658. 'tc_string_2_string',
  659. 'tc_char_2_string',
  660. 'tc_char_2_chararray',
  661. 'tc_pchar_2_string',
  662. 'tc_cchar_2_pchar',
  663. 'tc_cstring_2_pchar',
  664. 'tc_cstring_2_int',
  665. 'tc_ansistring_2_pchar',
  666. 'tc_string_2_chararray',
  667. 'tc_chararray_2_string',
  668. 'tc_array_2_pointer',
  669. 'tc_pointer_2_array',
  670. 'tc_int_2_int',
  671. 'tc_int_2_bool',
  672. 'tc_bool_2_bool',
  673. 'tc_bool_2_int',
  674. 'tc_real_2_real',
  675. 'tc_int_2_real',
  676. 'tc_real_2_currency',
  677. 'tc_proc_2_procvar',
  678. 'tc_nil_2_methodprocvar',
  679. 'tc_arrayconstructor_2_set',
  680. 'tc_set_2_set',
  681. 'tc_cord_2_pointer',
  682. 'tc_intf_2_string',
  683. 'tc_intf_2_guid',
  684. 'tc_class_2_intf',
  685. 'tc_char_2_char',
  686. 'tc_dynarray_2_openarray',
  687. 'tc_pwchar_2_string',
  688. 'tc_variant_2_dynarray',
  689. 'tc_dynarray_2_variant',
  690. 'tc_variant_2_enum',
  691. 'tc_enum_2_variant',
  692. 'tc_interface_2_variant',
  693. 'tc_variant_2_interface',
  694. 'tc_array_2_dynarray'
  695. );
  696. begin
  697. inherited printnodeinfo(t);
  698. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  699. end;
  700. function ttypeconvnode.typecheck_cord_to_pointer : tnode;
  701. var
  702. t : tnode;
  703. begin
  704. result:=nil;
  705. if left.nodetype=ordconstn then
  706. begin
  707. { check if we have a valid pointer constant (JM) }
  708. {$if sizeof(pointer) > sizeof(TConstPtrUInt)}
  709. {$if sizeof(TConstPtrUInt) = 4}
  710. if (tordconstnode(left).value < int64(low(longint))) or
  711. (tordconstnode(left).value > int64(high(cardinal))) then
  712. CGMessage(parser_e_range_check_error);
  713. {$else} {$if sizeof(TConstPtrUInt) = 8}
  714. if (tordconstnode(left).value < int64(low(int64))) or
  715. (tordconstnode(left).value > int64(high(qword))) then
  716. CGMessage(parser_e_range_check_error);
  717. {$else}
  718. internalerror(2001020801);
  719. {$endif} {$endif}
  720. {$endif}
  721. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  722. result:=t;
  723. end
  724. else
  725. internalerror(200104023);
  726. end;
  727. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  728. var
  729. chartype : string[8];
  730. newblock : tblocknode;
  731. newstat : tstatementnode;
  732. restemp : ttempcreatenode;
  733. begin
  734. if is_widechar(tarraydef(left.resultdef).elementdef) then
  735. chartype:='widechar'
  736. else
  737. chartype:='char';
  738. if tstringdef(resultdef).stringtype=st_shortstring then
  739. begin
  740. newblock:=internalstatements(newstat);
  741. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  742. addstatement(newstat,restemp);
  743. addstatement(newstat,ccallnode.createintern('fpc_'+chartype+'array_to_shortstr',
  744. ccallparanode.create(cordconstnode.create(
  745. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  746. ccallparanode.create(left,ccallparanode.create(
  747. ctemprefnode.create(restemp),nil)))));
  748. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  749. addstatement(newstat,ctemprefnode.create(restemp));
  750. result:=newblock;
  751. end
  752. else
  753. result:=ccallnode.createinternres(
  754. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  755. ccallparanode.create(cordconstnode.create(
  756. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  757. ccallparanode.create(left,nil)),resultdef);
  758. left:=nil;
  759. end;
  760. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  761. var
  762. newblock : tblocknode;
  763. newstat : tstatementnode;
  764. restemp : ttempcreatenode;
  765. pchtemp : pchar;
  766. arrsize : aint;
  767. chartype : string[8];
  768. begin
  769. result := nil;
  770. with tarraydef(resultdef) do
  771. begin
  772. if highrange<lowrange then
  773. internalerror(200501051);
  774. arrsize := highrange-lowrange+1;
  775. end;
  776. if (left.nodetype = stringconstn) and
  777. (tstringconstnode(left).cst_type=cst_conststring) then
  778. begin
  779. { if the array of char is large enough we can use the string
  780. constant directly. This is handled in ncgcnv }
  781. if (arrsize>=tstringconstnode(left).len) and
  782. is_char(tarraydef(resultdef).elementdef) then
  783. begin
  784. { pad the constant string with #0 to the array len }
  785. { (2.0.x compatible) }
  786. if (arrsize>tstringconstnode(left).len) then
  787. begin
  788. pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  789. left.free;
  790. left:=cstringconstnode.createpchar(pchtemp,arrsize);
  791. typecheckpass(left);
  792. end;
  793. exit;
  794. end;
  795. { Convert to wide/short/ansistring and call default helper }
  796. if is_widechar(tarraydef(resultdef).elementdef) then
  797. inserttypeconv(left,cwidestringtype)
  798. else
  799. begin
  800. if tstringconstnode(left).len>255 then
  801. inserttypeconv(left,cansistringtype)
  802. else
  803. inserttypeconv(left,cshortstringtype);
  804. end;
  805. end;
  806. if is_widechar(tarraydef(resultdef).elementdef) then
  807. chartype:='widechar'
  808. else
  809. chartype:='char';
  810. newblock:=internalstatements(newstat);
  811. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  812. addstatement(newstat,restemp);
  813. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  814. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  815. ctemprefnode.create(restemp),nil))));
  816. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  817. addstatement(newstat,ctemprefnode.create(restemp));
  818. result:=newblock;
  819. left:=nil;
  820. end;
  821. function ttypeconvnode.typecheck_string_to_string : tnode;
  822. var
  823. procname: string[31];
  824. newblock : tblocknode;
  825. newstat : tstatementnode;
  826. restemp : ttempcreatenode;
  827. begin
  828. result:=nil;
  829. if (left.nodetype=stringconstn) and
  830. ((not is_widechararray(left.resultdef) and
  831. not is_widestring(left.resultdef)) or
  832. (tstringdef(resultdef).stringtype=st_widestring) or
  833. { non-ascii chars would be replaced with '?' -> loses info }
  834. not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str))) then
  835. begin
  836. tstringconstnode(left).changestringtype(resultdef);
  837. result:=left;
  838. left:=nil;
  839. end
  840. else
  841. begin
  842. { get the correct procedure name }
  843. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  844. '_to_'+tstringdef(resultdef).stringtypname;
  845. if tstringdef(resultdef).stringtype=st_shortstring then
  846. begin
  847. newblock:=internalstatements(newstat);
  848. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  849. addstatement(newstat,restemp);
  850. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  851. ctemprefnode.create(restemp),nil))));
  852. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  853. addstatement(newstat,ctemprefnode.create(restemp));
  854. result:=newblock;
  855. end
  856. else
  857. result := ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  858. left:=nil;
  859. end;
  860. end;
  861. function ttypeconvnode.typecheck_char_to_string : tnode;
  862. var
  863. procname: string[31];
  864. para : tcallparanode;
  865. hp : tstringconstnode;
  866. ws : pcompilerwidestring;
  867. newblock : tblocknode;
  868. newstat : tstatementnode;
  869. restemp : ttempcreatenode;
  870. begin
  871. result:=nil;
  872. { we can't do widechar to ansichar conversions at compile time, since }
  873. { this maps all non-ascii chars to '?' -> loses information }
  874. if (left.nodetype=ordconstn) and
  875. ((tstringdef(resultdef).stringtype=st_widestring) or
  876. (torddef(left.resultdef).ordtype=uchar) or
  877. { >=128 is destroyed }
  878. (tordconstnode(left).value.uvalue<128)) then
  879. begin
  880. if tstringdef(resultdef).stringtype=st_widestring then
  881. begin
  882. initwidestring(ws);
  883. if torddef(left.resultdef).ordtype=uwidechar then
  884. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  885. else
  886. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value.uvalue)));
  887. hp:=cstringconstnode.createwstr(ws);
  888. donewidestring(ws);
  889. end
  890. else
  891. begin
  892. if torddef(left.resultdef).ordtype=uwidechar then
  893. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)))
  894. else
  895. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  896. tstringconstnode(hp).changestringtype(resultdef);
  897. end;
  898. result:=hp;
  899. end
  900. else
  901. { shortstrings are handled 'inline' (except for widechars) }
  902. if (tstringdef(resultdef).stringtype <> st_shortstring) or
  903. (torddef(left.resultdef).ordtype = uwidechar) then
  904. begin
  905. if (tstringdef(resultdef).stringtype <> st_shortstring) then
  906. begin
  907. { create the procname }
  908. if torddef(left.resultdef).ordtype<>uwidechar then
  909. procname := 'fpc_char_to_'
  910. else
  911. procname := 'fpc_wchar_to_';
  912. procname:=procname+tstringdef(resultdef).stringtypname;
  913. { and the parameter }
  914. para := ccallparanode.create(left,nil);
  915. { and finally the call }
  916. result := ccallnode.createinternres(procname,para,resultdef);
  917. end
  918. else
  919. begin
  920. newblock:=internalstatements(newstat);
  921. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  922. addstatement(newstat,restemp);
  923. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  924. ctemprefnode.create(restemp),nil))));
  925. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  926. addstatement(newstat,ctemprefnode.create(restemp));
  927. result:=newblock;
  928. end;
  929. left := nil;
  930. end
  931. else
  932. begin
  933. { create word(byte(char) shl 8 or 1) for litte endian machines }
  934. { and word(byte(char) or 256) for big endian machines }
  935. left := ctypeconvnode.create_internal(left,u8inttype);
  936. if (target_info.endian = endian_little) then
  937. left := caddnode.create(orn,
  938. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  939. cordconstnode.create(1,s32inttype,false))
  940. else
  941. left := caddnode.create(orn,left,
  942. cordconstnode.create(1 shl 8,s32inttype,false));
  943. left := ctypeconvnode.create_internal(left,u16inttype);
  944. typecheckpass(left);
  945. end;
  946. end;
  947. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  948. begin
  949. if resultdef.size <> 1 then
  950. begin
  951. { convert first to string, then to chararray }
  952. inserttypeconv(left,cshortstringtype);
  953. inserttypeconv(left,resultdef);
  954. result:=left;
  955. left := nil;
  956. exit;
  957. end;
  958. result := nil;
  959. end;
  960. function ttypeconvnode.typecheck_char_to_char : tnode;
  961. var
  962. hp : tordconstnode;
  963. begin
  964. result:=nil;
  965. if (left.nodetype=ordconstn) and
  966. ((torddef(resultdef).ordtype<>uchar) or
  967. (torddef(left.resultdef).ordtype<>uwidechar) or
  968. { >= 128 is replaced by '?' currently -> loses information }
  969. (tordconstnode(left).value.uvalue<128)) then
  970. begin
  971. if (torddef(resultdef).ordtype=uchar) and
  972. (torddef(left.resultdef).ordtype=uwidechar) then
  973. begin
  974. hp:=cordconstnode.create(
  975. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  976. cchartype,true);
  977. result:=hp;
  978. end
  979. else if (torddef(resultdef).ordtype=uwidechar) and
  980. (torddef(left.resultdef).ordtype=uchar) then
  981. begin
  982. hp:=cordconstnode.create(
  983. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  984. cwidechartype,true);
  985. result:=hp;
  986. end
  987. else
  988. internalerror(200105131);
  989. exit;
  990. end;
  991. end;
  992. function ttypeconvnode.typecheck_int_to_int : tnode;
  993. var
  994. v : TConstExprInt;
  995. begin
  996. result:=nil;
  997. if left.nodetype=ordconstn then
  998. begin
  999. v:=tordconstnode(left).value;
  1000. if is_currency(resultdef) then
  1001. v:=v*10000;
  1002. if (resultdef.typ=pointerdef) then
  1003. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1004. else
  1005. begin
  1006. if is_currency(left.resultdef) then
  1007. v:=v div 10000;
  1008. result:=cordconstnode.create(v,resultdef,false);
  1009. end;
  1010. end
  1011. else if left.nodetype=pointerconstn then
  1012. begin
  1013. v:=tpointerconstnode(left).value;
  1014. if (resultdef.typ=pointerdef) then
  1015. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1016. else
  1017. begin
  1018. if is_currency(resultdef) then
  1019. v:=v*10000;
  1020. result:=cordconstnode.create(v,resultdef,false);
  1021. end;
  1022. end
  1023. else
  1024. begin
  1025. { multiply by 10000 for currency. We need to use getcopy to pass
  1026. the argument because the current node is always disposed. Only
  1027. inserting the multiply in the left node is not possible because
  1028. it'll get in an infinite loop to convert int->currency }
  1029. if is_currency(resultdef) then
  1030. begin
  1031. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1032. include(result.flags,nf_is_currency);
  1033. end
  1034. else if is_currency(left.resultdef) then
  1035. begin
  1036. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1037. include(result.flags,nf_is_currency);
  1038. end;
  1039. end;
  1040. end;
  1041. function ttypeconvnode.typecheck_int_to_real : tnode;
  1042. var
  1043. rv : bestreal;
  1044. begin
  1045. result:=nil;
  1046. if left.nodetype=ordconstn then
  1047. begin
  1048. rv:=tordconstnode(left).value;
  1049. if is_currency(resultdef) then
  1050. rv:=rv*10000.0
  1051. else if is_currency(left.resultdef) then
  1052. rv:=rv/10000.0;
  1053. result:=crealconstnode.create(rv,resultdef);
  1054. end
  1055. else
  1056. begin
  1057. { multiply by 10000 for currency. We need to use getcopy to pass
  1058. the argument because the current node is always disposed. Only
  1059. inserting the multiply in the left node is not possible because
  1060. it'll get in an infinite loop to convert int->currency }
  1061. if is_currency(resultdef) then
  1062. begin
  1063. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1064. include(result.flags,nf_is_currency);
  1065. end
  1066. else if is_currency(left.resultdef) then
  1067. begin
  1068. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1069. include(result.flags,nf_is_currency);
  1070. end;
  1071. end;
  1072. end;
  1073. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1074. begin
  1075. if not is_currency(resultdef) then
  1076. internalerror(200304221);
  1077. result:=nil;
  1078. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1079. include(left.flags,nf_is_currency);
  1080. typecheckpass(left);
  1081. { Convert constants directly, else call Round() }
  1082. if left.nodetype=realconstn then
  1083. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1084. else
  1085. begin
  1086. result:=ccallnode.createinternres('fpc_round_real',
  1087. ccallparanode.create(left,nil),resultdef);
  1088. left:=nil;
  1089. end;
  1090. end;
  1091. function ttypeconvnode.typecheck_real_to_real : tnode;
  1092. begin
  1093. result:=nil;
  1094. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1095. begin
  1096. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1097. include(left.flags,nf_is_currency);
  1098. typecheckpass(left);
  1099. end
  1100. else
  1101. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1102. begin
  1103. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1104. include(left.flags,nf_is_currency);
  1105. typecheckpass(left);
  1106. end;
  1107. end;
  1108. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1109. begin
  1110. result:=nil;
  1111. if is_pwidechar(resultdef) then
  1112. inserttypeconv(left,cwidestringtype)
  1113. else
  1114. inserttypeconv(left,cshortstringtype);
  1115. { evaluate again, reset resultdef so the convert_typ
  1116. will be calculated again and cstring_to_pchar will
  1117. be used for futher conversion }
  1118. convtype:=tc_none;
  1119. result:=pass_typecheck;
  1120. end;
  1121. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1122. begin
  1123. result:=nil;
  1124. if is_pwidechar(resultdef) then
  1125. inserttypeconv(left,cwidestringtype)
  1126. else
  1127. if is_pchar(resultdef) and
  1128. is_widestring(left.resultdef) then
  1129. begin
  1130. inserttypeconv(left,cansistringtype);
  1131. { the second pass of second_cstring_to_pchar expects a }
  1132. { strinconstn, but this may become a call to the }
  1133. { widestring manager in case left contains "high ascii" }
  1134. if (left.nodetype<>stringconstn) then
  1135. begin
  1136. result:=left;
  1137. left:=nil;
  1138. end;
  1139. end;
  1140. end;
  1141. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1142. var
  1143. fcc : cardinal;
  1144. pb : pbyte;
  1145. begin
  1146. result:=nil;
  1147. if left.nodetype<>stringconstn then
  1148. internalerror(200510012);
  1149. if tstringconstnode(left).len=4 then
  1150. begin
  1151. pb:=pbyte(tstringconstnode(left).value_str);
  1152. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1153. result:=cordconstnode.create(fcc,u32inttype,false);
  1154. end
  1155. else
  1156. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1157. end;
  1158. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1159. var
  1160. hp : tnode;
  1161. begin
  1162. result:=nil;
  1163. if left.nodetype<>arrayconstructorn then
  1164. internalerror(5546);
  1165. { remove typeconv node }
  1166. hp:=left;
  1167. left:=nil;
  1168. { create a set constructor tree }
  1169. arrayconstructor_to_set(hp);
  1170. result:=hp;
  1171. end;
  1172. function ttypeconvnode.typecheck_set_to_set : tnode;
  1173. begin
  1174. result:=nil;
  1175. { constant sets can be converted by changing the type only }
  1176. if (left.nodetype=setconstn) then
  1177. begin
  1178. left.resultdef:=resultdef;
  1179. result:=left;
  1180. left:=nil;
  1181. exit;
  1182. end;
  1183. end;
  1184. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1185. var
  1186. newblock : tblocknode;
  1187. newstat : tstatementnode;
  1188. restemp : ttempcreatenode;
  1189. begin
  1190. if tstringdef(resultdef).stringtype=st_shortstring then
  1191. begin
  1192. newblock:=internalstatements(newstat);
  1193. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1194. addstatement(newstat,restemp);
  1195. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1196. ctemprefnode.create(restemp),nil))));
  1197. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1198. addstatement(newstat,ctemprefnode.create(restemp));
  1199. result:=newblock;
  1200. end
  1201. else
  1202. result := ccallnode.createinternres(
  1203. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1204. ccallparanode.create(left,nil),resultdef);
  1205. left:=nil;
  1206. end;
  1207. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1208. begin
  1209. if assigned(tobjectdef(left.resultdef).iidguid) then
  1210. begin
  1211. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1212. CGMessage1(type_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1213. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1214. end;
  1215. end;
  1216. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1217. begin
  1218. { a dynamic array is a pointer to an array, so to convert it to }
  1219. { an open array, we have to dereference it (JM) }
  1220. result := ctypeconvnode.create_internal(left,voidpointertype);
  1221. typecheckpass(result);
  1222. { left is reused }
  1223. left := nil;
  1224. result := cderefnode.create(result);
  1225. include(result.flags,nf_no_checkpointer);
  1226. result.resultdef := resultdef;
  1227. end;
  1228. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1229. var
  1230. newblock : tblocknode;
  1231. newstat : tstatementnode;
  1232. restemp : ttempcreatenode;
  1233. begin
  1234. if tstringdef(resultdef).stringtype=st_shortstring then
  1235. begin
  1236. newblock:=internalstatements(newstat);
  1237. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1238. addstatement(newstat,restemp);
  1239. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1240. ctemprefnode.create(restemp),nil))));
  1241. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1242. addstatement(newstat,ctemprefnode.create(restemp));
  1243. result:=newblock;
  1244. end
  1245. else
  1246. result := ccallnode.createinternres(
  1247. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1248. ccallparanode.create(left,nil),resultdef);
  1249. left:=nil;
  1250. end;
  1251. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1252. begin
  1253. result := ccallnode.createinternres(
  1254. 'fpc_variant_to_dynarray',
  1255. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1256. ccallparanode.create(left,nil)
  1257. ),resultdef);
  1258. typecheckpass(result);
  1259. left:=nil;
  1260. end;
  1261. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1262. begin
  1263. result := ccallnode.createinternres(
  1264. 'fpc_dynarray_to_variant',
  1265. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1266. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1267. ),resultdef);
  1268. typecheckpass(result);
  1269. left:=nil;
  1270. end;
  1271. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1272. begin
  1273. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1274. result := ccallnode.createinternres(
  1275. 'fpc_variant_to_idispatch',
  1276. ccallparanode.create(left,nil)
  1277. ,resultdef)
  1278. else
  1279. result := ccallnode.createinternres(
  1280. 'fpc_variant_to_interface',
  1281. ccallparanode.create(left,nil)
  1282. ,resultdef);
  1283. typecheckpass(result);
  1284. left:=nil;
  1285. end;
  1286. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1287. begin
  1288. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1289. result := ccallnode.createinternres(
  1290. 'fpc_idispatch_to_variant',
  1291. ccallparanode.create(left,nil)
  1292. ,resultdef)
  1293. else
  1294. result := ccallnode.createinternres(
  1295. 'fpc_interface_to_variant',
  1296. ccallparanode.create(left,nil)
  1297. ,resultdef);
  1298. typecheckpass(result);
  1299. left:=nil;
  1300. end;
  1301. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1302. begin
  1303. result := ctypeconvnode.create_internal(left,sinttype);
  1304. result := ctypeconvnode.create_internal(result,resultdef);
  1305. typecheckpass(result);
  1306. { left is reused }
  1307. left := nil;
  1308. end;
  1309. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1310. begin
  1311. result := ctypeconvnode.create_internal(left,sinttype);
  1312. result := ctypeconvnode.create_internal(result,cvarianttype);
  1313. typecheckpass(result);
  1314. { left is reused }
  1315. left := nil;
  1316. end;
  1317. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1318. var
  1319. newstatement : tstatementnode;
  1320. temp : ttempcreatenode;
  1321. temp2 : ttempcreatenode;
  1322. begin
  1323. { create statements with call to getmem+initialize }
  1324. result:=internalstatements(newstatement);
  1325. { create temp for result }
  1326. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1327. addstatement(newstatement,temp);
  1328. { get temp for array of lengths }
  1329. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1330. addstatement(newstatement,temp2);
  1331. { one dimensional }
  1332. addstatement(newstatement,cassignmentnode.create(
  1333. ctemprefnode.create_offset(temp2,0),
  1334. cordconstnode.create
  1335. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1336. { create call to fpc_dynarr_setlength }
  1337. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1338. ccallparanode.create(caddrnode.create_internal
  1339. (ctemprefnode.create(temp2)),
  1340. ccallparanode.create(cordconstnode.create
  1341. (1,s32inttype,true),
  1342. ccallparanode.create(caddrnode.create_internal
  1343. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1344. ccallparanode.create(
  1345. ctypeconvnode.create_internal(
  1346. ctemprefnode.create(temp),voidpointertype),
  1347. nil))))
  1348. ));
  1349. addstatement(newstatement,ctempdeletenode.create(temp2));
  1350. { copy ... }
  1351. addstatement(newstatement,cassignmentnode.create(
  1352. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1353. left
  1354. ));
  1355. { left is reused }
  1356. left:=nil;
  1357. { the last statement should return the value as
  1358. location and type, this is done be referencing the
  1359. temp and converting it first from a persistent temp to
  1360. normal temp }
  1361. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1362. addstatement(newstatement,ctemprefnode.create(temp));
  1363. end;
  1364. procedure copyparasym(p:TObject;arg:pointer);
  1365. var
  1366. newparast : TSymtable absolute arg;
  1367. vs : tparavarsym;
  1368. begin
  1369. if tsym(p).typ<>paravarsym then
  1370. exit;
  1371. with tparavarsym(p) do
  1372. begin
  1373. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1374. vs.defaultconstsym:=defaultconstsym;
  1375. newparast.insert(vs);
  1376. end;
  1377. end;
  1378. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1379. var
  1380. pd : tabstractprocdef;
  1381. begin
  1382. result:=nil;
  1383. pd:=tabstractprocdef(left.resultdef);
  1384. { create procvardef }
  1385. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1386. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1387. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1388. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1389. tprocvardef(resultdef).returndef:=pd.returndef;
  1390. { method ? then set the methodpointer flag }
  1391. if (pd.owner.symtabletype=ObjectSymtable) then
  1392. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1393. { was it a local procedure? }
  1394. if (pd.owner.symtabletype=localsymtable) then
  1395. include(tprocvardef(resultdef).procoptions,po_local);
  1396. { only need the address of the method? this is needed
  1397. for @tobject.create. In this case there will be a loadn without
  1398. a methodpointer. }
  1399. if (left.nodetype=loadn) and
  1400. not assigned(tloadnode(left).left) then
  1401. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1402. { Add parameters use only references, we don't need to keep the
  1403. parast. We use the parast from the original function to calculate
  1404. our parameter data and reset it afterwards }
  1405. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1406. tprocvardef(resultdef).calcparas;
  1407. end;
  1408. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1409. const
  1410. resultdefconvert : array[tconverttype] of pointer = (
  1411. {none} nil,
  1412. {equal} nil,
  1413. {not_possible} nil,
  1414. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1415. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1416. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1417. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1418. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1419. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1420. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1421. { ansistring_2_pchar } nil,
  1422. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1423. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1424. { array_2_pointer } nil,
  1425. { pointer_2_array } nil,
  1426. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1427. { int_2_bool } nil,
  1428. { bool_2_bool } nil,
  1429. { bool_2_int } nil,
  1430. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1431. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1432. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1433. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1434. { nil_2_methodprocvar } nil,
  1435. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1436. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1437. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1438. { intf_2_string } nil,
  1439. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1440. { class_2_intf } nil,
  1441. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1442. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1443. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1444. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1445. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1446. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1447. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1448. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1449. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1450. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1451. );
  1452. type
  1453. tprocedureofobject = function : tnode of object;
  1454. var
  1455. r : packed record
  1456. proc : pointer;
  1457. obj : pointer;
  1458. end;
  1459. begin
  1460. result:=nil;
  1461. { this is a little bit dirty but it works }
  1462. { and should be quite portable too }
  1463. r.proc:=resultdefconvert[c];
  1464. r.obj:=self;
  1465. if assigned(r.proc) then
  1466. result:=tprocedureofobject(r)();
  1467. end;
  1468. function ttypeconvnode.actualtargetnode: tnode;
  1469. begin
  1470. result:=self;
  1471. while (result.nodetype=typeconvn) and
  1472. (nf_absolute in result.flags) and
  1473. (resultdef.size=left.resultdef.size) do
  1474. result:=ttypeconvnode(result).left;
  1475. end;
  1476. function ttypeconvnode.pass_typecheck:tnode;
  1477. var
  1478. hdef : tdef;
  1479. hp : tnode;
  1480. currprocdef : tabstractprocdef;
  1481. aprocdef : tprocdef;
  1482. eq : tequaltype;
  1483. cdoptions : tcompare_defs_options;
  1484. newblock: tblocknode;
  1485. newstatement: tstatementnode;
  1486. tempnode: ttempcreatenode;
  1487. begin
  1488. result:=nil;
  1489. resultdef:=totypedef;
  1490. typecheckpass(left);
  1491. if codegenerror then
  1492. exit;
  1493. { When absolute force tc_equal }
  1494. if (nf_absolute in flags) then
  1495. begin
  1496. convtype:=tc_equal;
  1497. if not(tstoreddef(resultdef).is_intregable) and
  1498. not(tstoreddef(resultdef).is_fpuregable) then
  1499. make_not_regable(left,[ra_addr_regable]);
  1500. exit;
  1501. end;
  1502. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1503. convert on the procvar value. This is used to access the
  1504. fields of a methodpointer }
  1505. if not(nf_load_procvar in flags) and
  1506. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1507. maybe_call_procvar(left,true);
  1508. { convert array constructors to sets, because there is no conversion
  1509. possible for array constructors }
  1510. if (resultdef.typ<>arraydef) and
  1511. is_array_constructor(left.resultdef) then
  1512. begin
  1513. arrayconstructor_to_set(left);
  1514. typecheckpass(left);
  1515. end;
  1516. if convtype=tc_none then
  1517. begin
  1518. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1519. if nf_explicit in flags then
  1520. include(cdoptions,cdo_explicit);
  1521. if nf_internal in flags then
  1522. include(cdoptions,cdo_internal);
  1523. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1524. case eq of
  1525. te_exact,
  1526. te_equal :
  1527. begin
  1528. result := simplify;
  1529. if assigned(result) then
  1530. exit;
  1531. { Only leave when there is no conversion to do.
  1532. We can still need to call a conversion routine,
  1533. like the routine to convert a stringconstnode }
  1534. if convtype in [tc_equal,tc_not_possible] then
  1535. begin
  1536. left.resultdef:=resultdef;
  1537. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1538. include(left.flags, nf_typedaddr);
  1539. result:=left;
  1540. left:=nil;
  1541. exit;
  1542. end;
  1543. end;
  1544. te_convert_l1,
  1545. te_convert_l2,
  1546. te_convert_l3,
  1547. te_convert_l4,
  1548. te_convert_l5:
  1549. begin
  1550. result := simplify;
  1551. if assigned(result) then
  1552. exit;
  1553. { nothing to do }
  1554. end;
  1555. te_convert_operator :
  1556. begin
  1557. include(current_procinfo.flags,pi_do_call);
  1558. aprocdef.procsym.IncRefCountBy(1);
  1559. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1560. { tell explicitly which def we must use !! (PM) }
  1561. tcallnode(hp).procdefinition:=aprocdef;
  1562. left:=nil;
  1563. result:=hp;
  1564. exit;
  1565. end;
  1566. te_incompatible :
  1567. begin
  1568. { Procedures have a resultdef of voiddef and functions of their
  1569. own resultdef. They will therefore always be incompatible with
  1570. a procvar. Because isconvertable cannot check for procedures we
  1571. use an extra check for them.}
  1572. if (left.nodetype=calln) and
  1573. (tcallnode(left).para_count=0) and
  1574. (resultdef.typ=procvardef) and
  1575. (
  1576. (m_tp_procvar in current_settings.modeswitches) or
  1577. (m_mac_procvar in current_settings.modeswitches)
  1578. ) then
  1579. begin
  1580. if assigned(tcallnode(left).right) then
  1581. begin
  1582. { this is already a procvar, if it is really equal
  1583. is checked below }
  1584. convtype:=tc_equal;
  1585. hp:=tcallnode(left).right.getcopy;
  1586. currprocdef:=tabstractprocdef(hp.resultdef);
  1587. end
  1588. else
  1589. begin
  1590. convtype:=tc_proc_2_procvar;
  1591. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1592. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1593. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1594. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1595. begin
  1596. if assigned(tcallnode(left).methodpointer) then
  1597. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  1598. else
  1599. tloadnode(hp).set_mp(load_self_node);
  1600. end;
  1601. typecheckpass(hp);
  1602. end;
  1603. left.free;
  1604. left:=hp;
  1605. { Now check if the procedure we are going to assign to
  1606. the procvar, is compatible with the procvar's type }
  1607. if not(nf_explicit in flags) and
  1608. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1609. IncompatibleTypes(left.resultdef,resultdef);
  1610. exit;
  1611. end;
  1612. { Handle explicit type conversions }
  1613. if nf_explicit in flags then
  1614. begin
  1615. { do common tc_equal cast }
  1616. convtype:=tc_equal;
  1617. { ordinal constants can be resized to 1,2,4,8 bytes }
  1618. if (left.nodetype=ordconstn) then
  1619. begin
  1620. { Insert typeconv for ordinal to the correct size first on left, after
  1621. that the other conversion can be done }
  1622. hdef:=nil;
  1623. case longint(resultdef.size) of
  1624. 1 :
  1625. hdef:=s8inttype;
  1626. 2 :
  1627. hdef:=s16inttype;
  1628. 4 :
  1629. hdef:=s32inttype;
  1630. 8 :
  1631. hdef:=s64inttype;
  1632. end;
  1633. { we need explicit, because it can also be an enum }
  1634. if assigned(hdef) then
  1635. inserttypeconv_internal(left,hdef)
  1636. else
  1637. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1638. end;
  1639. { check if the result could be in a register }
  1640. if (not(tstoreddef(resultdef).is_intregable) and
  1641. not(tstoreddef(resultdef).is_fpuregable)) or
  1642. ((left.resultdef.typ = floatdef) and
  1643. (resultdef.typ <> floatdef)) then
  1644. make_not_regable(left,[ra_addr_regable]);
  1645. { class/interface to class/interface, with checkobject support }
  1646. if is_class_or_interface(resultdef) and
  1647. is_class_or_interface(left.resultdef) then
  1648. begin
  1649. { check if the types are related }
  1650. if not(nf_internal in flags) and
  1651. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1652. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1653. begin
  1654. { Give an error when typecasting class to interface, this is compatible
  1655. with delphi }
  1656. if is_interface(resultdef) and
  1657. not is_interface(left.resultdef) then
  1658. CGMessage2(type_e_classes_not_related,
  1659. FullTypeName(left.resultdef,resultdef),
  1660. FullTypeName(resultdef,left.resultdef))
  1661. else
  1662. CGMessage2(type_w_classes_not_related,
  1663. FullTypeName(left.resultdef,resultdef),
  1664. FullTypeName(resultdef,left.resultdef))
  1665. end;
  1666. { Add runtime check? }
  1667. if (cs_check_object in current_settings.localswitches) and
  1668. not(nf_internal in flags) then
  1669. begin
  1670. { we can translate the typeconvnode to 'as' when
  1671. typecasting to a class or interface }
  1672. { we need to make sure the result can still be
  1673. passed as a var parameter }
  1674. newblock:=internalstatements(newstatement);
  1675. if (valid_for_var(left,false)) then
  1676. begin
  1677. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1678. addstatement(newstatement,tempnode);
  1679. addstatement(newstatement,cassignmentnode.create(
  1680. ctemprefnode.create(tempnode),
  1681. caddrnode.create_internal(left)));
  1682. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1683. end
  1684. else
  1685. begin
  1686. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1687. addstatement(newstatement,tempnode);
  1688. addstatement(newstatement,cassignmentnode.create(
  1689. ctemprefnode.create(tempnode),
  1690. left));
  1691. left:=ctemprefnode.create(tempnode);
  1692. end;
  1693. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1694. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1695. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1696. left:=nil;
  1697. result:=newblock;
  1698. exit;
  1699. end;
  1700. end
  1701. else
  1702. begin
  1703. { only if the same size or formal def, and }
  1704. { don't allow type casting of constants to }
  1705. { structured types }
  1706. if not(
  1707. (left.resultdef.typ=formaldef) or
  1708. (
  1709. not(is_open_array(left.resultdef)) and
  1710. not(is_array_constructor(left.resultdef)) and
  1711. (left.resultdef.size=resultdef.size) and
  1712. { disallow casts of const nodes }
  1713. (not is_constnode(left) or
  1714. { however, there are some exceptions }
  1715. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1716. filedef,variantdef,objectdef]) or
  1717. is_class_or_interface(resultdef) or
  1718. { the softfloat code generates casts <const. float> to record }
  1719. (nf_internal in flags)
  1720. ))
  1721. ) or
  1722. (
  1723. is_void(left.resultdef) and
  1724. (left.nodetype=derefn)
  1725. )
  1726. ) then
  1727. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1728. end;
  1729. end
  1730. else
  1731. IncompatibleTypes(left.resultdef,resultdef);
  1732. end;
  1733. else
  1734. internalerror(200211231);
  1735. end;
  1736. end;
  1737. { Give hint or warning for unportable code, exceptions are
  1738. - typecasts from constants
  1739. - void }
  1740. if not(nf_internal in flags) and
  1741. (left.nodetype<>ordconstn) and
  1742. not(is_void(left.resultdef)) and
  1743. (((left.resultdef.typ=orddef) and
  1744. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1745. ((resultdef.typ=orddef) and
  1746. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1747. begin
  1748. {Converting pointers to signed integers is a bad idea. Warn.}
  1749. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1750. { Give a warning when sizes don't match, because then info will be lost }
  1751. if left.resultdef.size=resultdef.size then
  1752. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1753. else
  1754. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1755. end;
  1756. result := simplify;
  1757. if assigned(result) then
  1758. exit;
  1759. { now call the resultdef helper to do constant folding }
  1760. result:=typecheck_call_helper(convtype);
  1761. end;
  1762. {$ifndef cpu64bitaddr}
  1763. { checks whether we can safely remove 64 bit typeconversions }
  1764. { in case range and overflow checking are off, and in case }
  1765. { the result of this node tree is downcasted again to a }
  1766. { 8/16/32 bit value afterwards }
  1767. function checkremove64bittypeconvs(n: tnode): boolean;
  1768. var
  1769. gotmuldivmod, gotsint: boolean;
  1770. { checks whether a node is either an u32bit, or originally }
  1771. { was one but was implicitly converted to s64bit }
  1772. function wasoriginallyint32(n: tnode): boolean;
  1773. begin
  1774. if (n.resultdef.typ<>orddef) then
  1775. exit(false);
  1776. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  1777. begin
  1778. if (torddef(n.resultdef).ordtype=s32bit) then
  1779. gotsint:=true;
  1780. exit(true);
  1781. end;
  1782. if (torddef(n.resultdef).ordtype=s64bit) and
  1783. { nf_explicit is also set for explicitly typecasted }
  1784. { ordconstn's }
  1785. ([nf_internal,nf_explicit]*n.flags=[]) and
  1786. { either a typeconversion node coming from u32bit }
  1787. (((n.nodetype=typeconvn) and
  1788. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  1789. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  1790. { or an ordconstnode which was/is a valid cardinal }
  1791. ((n.nodetype=ordconstn) and
  1792. (tordconstnode(n).value>=int64(low(longint))) and
  1793. (tordconstnode(n).value<=high(cardinal)))) then
  1794. begin
  1795. if ((n.nodetype=typeconvn) and
  1796. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  1797. ((n.nodetype=ordconstn) and
  1798. (tordconstnode(n).value<0)) then
  1799. gotsint:=true;
  1800. exit(true);
  1801. end;
  1802. result:=false;
  1803. end;
  1804. function docheckremove64bittypeconvs(n: tnode): boolean;
  1805. begin
  1806. result:=false;
  1807. if wasoriginallyint32(n) then
  1808. exit(true);
  1809. case n.nodetype of
  1810. subn,orn,xorn:
  1811. begin
  1812. { nf_internal is set by taddnode.typecheckpass in }
  1813. { case the arguments of this subn were u32bit, but }
  1814. { upcasted to s64bit for calculation correctness }
  1815. { (normally only needed when range checking, but }
  1816. { also done otherwise so there is no difference }
  1817. { in overload choosing etc between $r+ and $r-) }
  1818. if (nf_internal in n.flags) then
  1819. result:=true
  1820. else
  1821. result:=
  1822. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1823. docheckremove64bittypeconvs(tbinarynode(n).right);
  1824. end;
  1825. addn,muln,divn,modn,andn:
  1826. begin
  1827. if n.nodetype in [muln,divn,modn] then
  1828. gotmuldivmod:=true;
  1829. result:=
  1830. docheckremove64bittypeconvs(tbinarynode(n).left) and
  1831. docheckremove64bittypeconvs(tbinarynode(n).right);
  1832. end;
  1833. end;
  1834. end;
  1835. begin { checkremove64bittypeconvs }
  1836. gotmuldivmod:=false;
  1837. gotsint:=false;
  1838. result:=
  1839. docheckremove64bittypeconvs(n) and
  1840. not(gotmuldivmod and gotsint);
  1841. end;
  1842. procedure doremove64bittypeconvs(var n: tnode; todef: tdef);
  1843. begin
  1844. case n.nodetype of
  1845. subn,addn,muln,divn,modn,xorn,andn,orn:
  1846. begin
  1847. exclude(n.flags,nf_internal);
  1848. if is_signed(n.resultdef) then
  1849. begin
  1850. doremove64bittypeconvs(tbinarynode(n).left,s32inttype);
  1851. doremove64bittypeconvs(tbinarynode(n).right,s32inttype);
  1852. n.resultdef:=s32inttype
  1853. end
  1854. else
  1855. begin
  1856. doremove64bittypeconvs(tbinarynode(n).left,u32inttype);
  1857. doremove64bittypeconvs(tbinarynode(n).right,u32inttype);
  1858. n.resultdef:=u32inttype
  1859. end;
  1860. end;
  1861. ordconstn:
  1862. inserttypeconv_internal(n,todef);
  1863. typeconvn:
  1864. n.resultdef:=todef;
  1865. end;
  1866. end;
  1867. {$endif not cpu64bitaddr}
  1868. function ttypeconvnode.simplify: tnode;
  1869. var
  1870. hp: tnode;
  1871. begin
  1872. result := nil;
  1873. { Constant folding and other node transitions to
  1874. remove the typeconv node }
  1875. case left.nodetype of
  1876. realconstn :
  1877. begin
  1878. if (convtype = tc_real_2_currency) then
  1879. result := typecheck_real_to_currency
  1880. else if (convtype = tc_real_2_real) then
  1881. result := typecheck_real_to_real
  1882. else
  1883. exit;
  1884. if not(assigned(result)) then
  1885. begin
  1886. result := left;
  1887. left := nil;
  1888. end;
  1889. if (result.nodetype = realconstn) then
  1890. begin
  1891. hp:=result;
  1892. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1893. if ([nf_explicit,nf_internal] * flags <> []) then
  1894. include(result.flags, nf_explicit);
  1895. hp.free;
  1896. end;
  1897. end;
  1898. niln :
  1899. begin
  1900. { nil to ordinal node }
  1901. if (resultdef.typ=orddef) then
  1902. begin
  1903. hp:=cordconstnode.create(0,resultdef,true);
  1904. if ([nf_explicit,nf_internal] * flags <> []) then
  1905. include(hp.flags, nf_explicit);
  1906. result:=hp;
  1907. exit;
  1908. end
  1909. else
  1910. { fold nil to any pointer type }
  1911. if (resultdef.typ=pointerdef) then
  1912. begin
  1913. hp:=cnilnode.create;
  1914. hp.resultdef:=resultdef;
  1915. if ([nf_explicit,nf_internal] * flags <> []) then
  1916. include(hp.flags, nf_explicit);
  1917. result:=hp;
  1918. exit;
  1919. end
  1920. else
  1921. { remove typeconv after niln, but not when the result is a
  1922. methodpointer. The typeconv of the methodpointer will then
  1923. take care of updateing size of niln to OS_64 }
  1924. if not((resultdef.typ=procvardef) and
  1925. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1926. begin
  1927. left.resultdef:=resultdef;
  1928. if ([nf_explicit,nf_internal] * flags <> []) then
  1929. include(left.flags, nf_explicit);
  1930. result:=left;
  1931. left:=nil;
  1932. exit;
  1933. end;
  1934. end;
  1935. ordconstn :
  1936. begin
  1937. { ordinal contants can be directly converted }
  1938. { but not char to char because it is a widechar to char or via versa }
  1939. { which needs extra code to do the code page transistion }
  1940. { constant ordinal to pointer }
  1941. if (resultdef.typ=pointerdef) and
  1942. (convtype<>tc_cchar_2_pchar) then
  1943. begin
  1944. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  1945. if ([nf_explicit,nf_internal] * flags <> []) then
  1946. include(hp.flags, nf_explicit);
  1947. result:=hp;
  1948. exit;
  1949. end
  1950. else if is_ordinal(resultdef) and
  1951. not(convtype=tc_char_2_char) then
  1952. begin
  1953. { replace the resultdef and recheck the range }
  1954. if ([nf_explicit,nf_internal] * flags <> []) then
  1955. include(left.flags, nf_explicit)
  1956. else
  1957. { no longer an ordconst with an explicit typecast }
  1958. exclude(left.flags, nf_explicit);
  1959. { when converting from one boolean type to another, force }
  1960. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  1961. { (Delphi-compatibile) }
  1962. if is_boolean(left.resultdef) and
  1963. is_boolean(resultdef) and
  1964. (is_cbool(left.resultdef) or
  1965. is_cbool(resultdef)) then
  1966. begin
  1967. if is_pasbool(resultdef) then
  1968. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  1969. else
  1970. {$ifdef VER2_2}
  1971. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  1972. tordconstnode(left).value:=-tordconstnode(left).value;
  1973. {$else}
  1974. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  1975. {$endif VER2_2}
  1976. end
  1977. else
  1978. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1979. left.resultdef:=resultdef;
  1980. result:=left;
  1981. left:=nil;
  1982. exit;
  1983. end;
  1984. end;
  1985. pointerconstn :
  1986. begin
  1987. { pointerconstn to any pointer is folded too }
  1988. if (resultdef.typ=pointerdef) then
  1989. begin
  1990. left.resultdef:=resultdef;
  1991. if ([nf_explicit,nf_internal] * flags <> []) then
  1992. include(left.flags, nf_explicit)
  1993. else
  1994. { no longer an ordconst with an explicit typecast }
  1995. exclude(left.flags, nf_explicit);
  1996. result:=left;
  1997. left:=nil;
  1998. exit;
  1999. end
  2000. { constant pointer to ordinal }
  2001. else if is_ordinal(resultdef) then
  2002. begin
  2003. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2004. resultdef,not(nf_explicit in flags));
  2005. if ([nf_explicit,nf_internal] * flags <> []) then
  2006. include(hp.flags, nf_explicit);
  2007. result:=hp;
  2008. exit;
  2009. end;
  2010. end;
  2011. end;
  2012. {$ifndef cpu64bitaddr}
  2013. { must be done before code below, because we need the
  2014. typeconversions for ordconstn's as well }
  2015. case convtype of
  2016. tc_int_2_int:
  2017. begin
  2018. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2019. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2020. (resultdef.size <= 4) and
  2021. is_64bitint(left.resultdef) and
  2022. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2023. checkremove64bittypeconvs(left) then
  2024. begin
  2025. { avoid unnecessary widening of intermediary calculations }
  2026. { to 64 bit }
  2027. doremove64bittypeconvs(left,generrordef);
  2028. end;
  2029. end;
  2030. end;
  2031. {$endif not cpu64bitaddr}
  2032. end;
  2033. procedure Ttypeconvnode.mark_write;
  2034. begin
  2035. left.mark_write;
  2036. end;
  2037. function ttypeconvnode.first_cord_to_pointer : tnode;
  2038. begin
  2039. result:=nil;
  2040. internalerror(200104043);
  2041. end;
  2042. function ttypeconvnode.first_int_to_int : tnode;
  2043. begin
  2044. first_int_to_int:=nil;
  2045. expectloc:=left.expectloc;
  2046. if not is_void(left.resultdef) then
  2047. begin
  2048. if (left.expectloc<>LOC_REGISTER) and
  2049. (resultdef.size>left.resultdef.size) then
  2050. expectloc:=LOC_REGISTER
  2051. else
  2052. if (left.expectloc=LOC_CREGISTER) and
  2053. (resultdef.size<left.resultdef.size) then
  2054. expectloc:=LOC_REGISTER;
  2055. end;
  2056. end;
  2057. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2058. begin
  2059. result:=nil;
  2060. expectloc:=LOC_REGISTER;
  2061. end;
  2062. function ttypeconvnode.first_cstring_to_int : tnode;
  2063. begin
  2064. result:=nil;
  2065. internalerror(200510014);
  2066. end;
  2067. function ttypeconvnode.first_string_to_chararray : tnode;
  2068. begin
  2069. first_string_to_chararray:=nil;
  2070. expectloc:=left.expectloc;
  2071. end;
  2072. function ttypeconvnode.first_char_to_string : tnode;
  2073. begin
  2074. first_char_to_string:=nil;
  2075. expectloc:=LOC_REFERENCE;
  2076. end;
  2077. function ttypeconvnode.first_nothing : tnode;
  2078. begin
  2079. first_nothing:=nil;
  2080. end;
  2081. function ttypeconvnode.first_array_to_pointer : tnode;
  2082. begin
  2083. first_array_to_pointer:=nil;
  2084. expectloc:=LOC_REGISTER;
  2085. end;
  2086. function ttypeconvnode.first_int_to_real: tnode;
  2087. var
  2088. fname: string[32];
  2089. begin
  2090. if target_info.system in system_wince then
  2091. begin
  2092. { converting a 64bit integer to a float requires a helper }
  2093. if is_64bitint(left.resultdef) or
  2094. is_currency(left.resultdef) then
  2095. begin
  2096. { hack to avoid double division by 10000, as it's
  2097. already done by typecheckpass.resultdef_int_to_real }
  2098. if is_currency(left.resultdef) then
  2099. left.resultdef := s64inttype;
  2100. if is_signed(left.resultdef) then
  2101. fname:='I64TO'
  2102. else
  2103. fname:='UI64TO';
  2104. end
  2105. else
  2106. { other integers are supposed to be 32 bit }
  2107. begin
  2108. if is_signed(left.resultdef) then
  2109. fname:='ITO'
  2110. else
  2111. fname:='UTO';
  2112. firstpass(left);
  2113. end;
  2114. if tfloatdef(resultdef).floattype=s64real then
  2115. fname:=fname+'D'
  2116. else
  2117. fname:=fname+'S';
  2118. result:=ccallnode.createintern(fname,ccallparanode.create(
  2119. left,nil));
  2120. left:=nil;
  2121. firstpass(result);
  2122. exit;
  2123. end
  2124. else
  2125. begin
  2126. { converting a 64bit integer to a float requires a helper }
  2127. if is_64bitint(left.resultdef) or
  2128. is_currency(left.resultdef) then
  2129. begin
  2130. { hack to avoid double division by 10000, as it's
  2131. already done by typecheckpass.resultdef_int_to_real }
  2132. if is_currency(left.resultdef) then
  2133. left.resultdef := s64inttype;
  2134. if is_signed(left.resultdef) then
  2135. fname:='int64_to_'
  2136. else
  2137. { we can't do better currently }
  2138. fname:='int64_to_';
  2139. end
  2140. else
  2141. { other integers are supposed to be 32 bit }
  2142. begin
  2143. if is_signed(left.resultdef) then
  2144. fname:='int32_to_'
  2145. else
  2146. { we can't do better currently }
  2147. fname:='int32_to_';
  2148. firstpass(left);
  2149. end;
  2150. if tfloatdef(resultdef).floattype=s64real then
  2151. fname:=fname+'float64'
  2152. else
  2153. fname:=fname+'float32';
  2154. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2155. left,nil)),resultdef);
  2156. left:=nil;
  2157. firstpass(result);
  2158. exit;
  2159. end;
  2160. end;
  2161. function ttypeconvnode.first_real_to_real : tnode;
  2162. begin
  2163. {$ifdef cpufpemu}
  2164. if cs_fp_emulation in current_settings.moduleswitches then
  2165. begin
  2166. if target_info.system in system_wince then
  2167. begin
  2168. case tfloatdef(left.resultdef).floattype of
  2169. s32real:
  2170. case tfloatdef(resultdef).floattype of
  2171. s64real:
  2172. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2173. s32real:
  2174. begin
  2175. result:=left;
  2176. left:=nil;
  2177. end;
  2178. else
  2179. internalerror(2005082704);
  2180. end;
  2181. s64real:
  2182. case tfloatdef(resultdef).floattype of
  2183. s32real:
  2184. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2185. s64real:
  2186. begin
  2187. result:=left;
  2188. left:=nil;
  2189. end;
  2190. else
  2191. internalerror(2005082703);
  2192. end;
  2193. else
  2194. internalerror(2005082702);
  2195. end;
  2196. left:=nil;
  2197. firstpass(result);
  2198. exit;
  2199. end
  2200. else
  2201. begin
  2202. case tfloatdef(left.resultdef).floattype of
  2203. s32real:
  2204. case tfloatdef(resultdef).floattype of
  2205. s64real:
  2206. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2207. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2208. s32real:
  2209. begin
  2210. result:=left;
  2211. left:=nil;
  2212. end;
  2213. else
  2214. internalerror(200610151);
  2215. end;
  2216. s64real:
  2217. case tfloatdef(resultdef).floattype of
  2218. s32real:
  2219. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2220. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2221. s64real:
  2222. begin
  2223. result:=left;
  2224. left:=nil;
  2225. end;
  2226. else
  2227. internalerror(200610152);
  2228. end;
  2229. else
  2230. internalerror(200610153);
  2231. end;
  2232. left:=nil;
  2233. firstpass(result);
  2234. exit;
  2235. end;
  2236. end
  2237. else
  2238. {$endif cpufpemu}
  2239. begin
  2240. first_real_to_real:=nil;
  2241. expectloc:=LOC_FPUREGISTER;
  2242. end;
  2243. end;
  2244. function ttypeconvnode.first_pointer_to_array : tnode;
  2245. begin
  2246. first_pointer_to_array:=nil;
  2247. expectloc:=LOC_REFERENCE;
  2248. end;
  2249. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2250. begin
  2251. first_cchar_to_pchar:=nil;
  2252. internalerror(200104021);
  2253. end;
  2254. function ttypeconvnode.first_bool_to_int : tnode;
  2255. begin
  2256. first_bool_to_int:=nil;
  2257. { byte(boolean) or word(wordbool) or longint(longbool) must
  2258. be accepted for var parameters }
  2259. if (nf_explicit in flags) and
  2260. (left.resultdef.size=resultdef.size) and
  2261. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2262. exit;
  2263. { when converting to 64bit, first convert to a 32bit int and then }
  2264. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2265. if resultdef.size > sizeof(aint) then
  2266. begin
  2267. result := ctypeconvnode.create_internal(left,s32inttype);
  2268. result := ctypeconvnode.create(result,resultdef);
  2269. left := nil;
  2270. firstpass(result);
  2271. exit;
  2272. end;
  2273. expectloc:=LOC_REGISTER;
  2274. end;
  2275. function ttypeconvnode.first_int_to_bool : tnode;
  2276. begin
  2277. first_int_to_bool:=nil;
  2278. { byte(boolean) or word(wordbool) or longint(longbool) must
  2279. be accepted for var parameters }
  2280. if (nf_explicit in flags) and
  2281. (left.resultdef.size=resultdef.size) and
  2282. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2283. exit;
  2284. { when converting 64bit int to C-ctyle boolean, first convert to a 32bit int and then }
  2285. { convert to a boolean (only necessary for 32bit processors) }
  2286. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2287. and is_cbool(resultdef) then
  2288. begin
  2289. result := ctypeconvnode.create_internal(left,s32inttype);
  2290. left := nil;
  2291. firstpass(result);
  2292. exit;
  2293. end;
  2294. expectloc:=LOC_REGISTER;
  2295. end;
  2296. function ttypeconvnode.first_bool_to_bool : tnode;
  2297. begin
  2298. first_bool_to_bool:=nil;
  2299. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2300. expectloc := left.expectloc
  2301. else
  2302. expectloc:=LOC_REGISTER;
  2303. end;
  2304. function ttypeconvnode.first_char_to_char : tnode;
  2305. var
  2306. fname: string[18];
  2307. begin
  2308. if (torddef(resultdef).ordtype=uchar) and
  2309. (torddef(left.resultdef).ordtype=uwidechar) then
  2310. fname := 'fpc_wchar_to_char'
  2311. else if (torddef(resultdef).ordtype=uwidechar) and
  2312. (torddef(left.resultdef).ordtype=uchar) then
  2313. fname := 'fpc_char_to_wchar'
  2314. else
  2315. internalerror(2007081201);
  2316. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2317. left:=nil;
  2318. firstpass(result);
  2319. end;
  2320. function ttypeconvnode.first_proc_to_procvar : tnode;
  2321. begin
  2322. first_proc_to_procvar:=nil;
  2323. { if we take the address of a nested function, it'll }
  2324. { probably be used in a foreach() construct and then }
  2325. { the parent needs a stackframe }
  2326. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  2327. include(current_procinfo.flags,pi_needs_stackframe);
  2328. if tabstractprocdef(resultdef).is_addressonly then
  2329. expectloc:=LOC_REGISTER
  2330. else
  2331. begin
  2332. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2333. CGMessage(parser_e_illegal_expression);
  2334. expectloc:=left.expectloc;
  2335. end;
  2336. end;
  2337. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2338. begin
  2339. first_nil_to_methodprocvar:=nil;
  2340. expectloc:=LOC_REFERENCE;
  2341. end;
  2342. function ttypeconvnode.first_set_to_set : tnode;
  2343. var
  2344. newstatement : tstatementnode;
  2345. temp : ttempcreatenode;
  2346. begin
  2347. { in theory, we should do range checking here,
  2348. but Delphi doesn't do it either (FK) }
  2349. if left.nodetype=setconstn then
  2350. begin
  2351. left.resultdef:=resultdef;
  2352. result:=left;
  2353. left:=nil;
  2354. end
  2355. { equal sets for the code generator? }
  2356. else if (left.resultdef.size=resultdef.size) and
  2357. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2358. {$warning This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2359. { e.g., this prints true because of this:
  2360. var
  2361. sa: set of 1..2;
  2362. sb: set of 5..6;
  2363. b: byte;
  2364. begin
  2365. b:=1;
  2366. sa:=[1..2];
  2367. sb:=sa;
  2368. writeln(b in sb);
  2369. end.
  2370. }
  2371. begin
  2372. result:=left;
  2373. left:=nil;
  2374. end
  2375. else
  2376. begin
  2377. result:=internalstatements(newstatement);
  2378. { in case left is a smallset expression, it can be an addn or so. }
  2379. { fpc_varset_load expects a formal const parameter, which doesn't }
  2380. { accept set addn's -> assign to a temp first and pass the temp }
  2381. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2382. begin
  2383. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2384. addstatement(newstatement,temp);
  2385. { temp := left }
  2386. addstatement(newstatement,cassignmentnode.create(
  2387. ctemprefnode.create(temp),left));
  2388. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2389. addstatement(newstatement,ctemprefnode.create(temp));
  2390. left:=result;
  2391. firstpass(left);
  2392. { recreate the result's internalstatements list }
  2393. result:=internalstatements(newstatement);
  2394. end;
  2395. { create temp for result }
  2396. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2397. addstatement(newstatement,temp);
  2398. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2399. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2400. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2401. ccallparanode.create(ctemprefnode.create(temp),
  2402. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2403. ccallparanode.create(left,nil))))))
  2404. );
  2405. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2406. addstatement(newstatement,ctemprefnode.create(temp));
  2407. left:=nil;
  2408. end;
  2409. end;
  2410. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2411. begin
  2412. first_ansistring_to_pchar:=nil;
  2413. expectloc:=LOC_REGISTER;
  2414. end;
  2415. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2416. begin
  2417. first_arrayconstructor_to_set:=nil;
  2418. internalerror(200104022);
  2419. end;
  2420. function ttypeconvnode.first_class_to_intf : tnode;
  2421. var
  2422. hd : tobjectdef;
  2423. ImplIntf : TImplementedInterface;
  2424. begin
  2425. result:=nil;
  2426. expectloc:=LOC_REGISTER;
  2427. hd:=tobjectdef(left.resultdef);
  2428. while assigned(hd) do
  2429. begin
  2430. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2431. if assigned(ImplIntf) then
  2432. begin
  2433. case ImplIntf.IType of
  2434. etStandard:
  2435. { handle in pass 2 }
  2436. ;
  2437. etFieldValue:
  2438. if is_interface(tobjectdef(resultdef)) then
  2439. begin
  2440. result:=left;
  2441. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2442. left:=nil;
  2443. end
  2444. else
  2445. begin
  2446. internalerror(200802213);
  2447. end;
  2448. etStaticMethodResult,
  2449. etVirtualMethodResult:
  2450. if is_interface(tobjectdef(resultdef)) then
  2451. begin
  2452. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2453. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2454. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2455. left,[]);
  2456. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2457. left:=nil;
  2458. end
  2459. else if is_class(tobjectdef(resultdef)) then
  2460. begin
  2461. internalerror(200802211);
  2462. end
  2463. else
  2464. internalerror(200802231);
  2465. else
  2466. internalerror(200802165);
  2467. end;
  2468. break;
  2469. end;
  2470. hd:=hd.childof;
  2471. end;
  2472. if hd=nil then
  2473. internalerror(200802164);
  2474. end;
  2475. function ttypeconvnode._first_int_to_int : tnode;
  2476. begin
  2477. result:=first_int_to_int;
  2478. end;
  2479. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2480. begin
  2481. result:=first_cstring_to_pchar;
  2482. end;
  2483. function ttypeconvnode._first_cstring_to_int : tnode;
  2484. begin
  2485. result:=first_cstring_to_int;
  2486. end;
  2487. function ttypeconvnode._first_string_to_chararray : tnode;
  2488. begin
  2489. result:=first_string_to_chararray;
  2490. end;
  2491. function ttypeconvnode._first_char_to_string : tnode;
  2492. begin
  2493. result:=first_char_to_string;
  2494. end;
  2495. function ttypeconvnode._first_nothing : tnode;
  2496. begin
  2497. result:=first_nothing;
  2498. end;
  2499. function ttypeconvnode._first_array_to_pointer : tnode;
  2500. begin
  2501. result:=first_array_to_pointer;
  2502. end;
  2503. function ttypeconvnode._first_int_to_real : tnode;
  2504. begin
  2505. result:=first_int_to_real;
  2506. end;
  2507. function ttypeconvnode._first_real_to_real : tnode;
  2508. begin
  2509. result:=first_real_to_real;
  2510. end;
  2511. function ttypeconvnode._first_pointer_to_array : tnode;
  2512. begin
  2513. result:=first_pointer_to_array;
  2514. end;
  2515. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2516. begin
  2517. result:=first_cchar_to_pchar;
  2518. end;
  2519. function ttypeconvnode._first_bool_to_int : tnode;
  2520. begin
  2521. result:=first_bool_to_int;
  2522. end;
  2523. function ttypeconvnode._first_int_to_bool : tnode;
  2524. begin
  2525. result:=first_int_to_bool;
  2526. end;
  2527. function ttypeconvnode._first_bool_to_bool : tnode;
  2528. begin
  2529. result:=first_bool_to_bool;
  2530. end;
  2531. function ttypeconvnode._first_proc_to_procvar : tnode;
  2532. begin
  2533. result:=first_proc_to_procvar;
  2534. end;
  2535. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2536. begin
  2537. result:=first_nil_to_methodprocvar;
  2538. end;
  2539. function ttypeconvnode._first_set_to_set : tnode;
  2540. begin
  2541. result:=first_set_to_set;
  2542. end;
  2543. function ttypeconvnode._first_cord_to_pointer : tnode;
  2544. begin
  2545. result:=first_cord_to_pointer;
  2546. end;
  2547. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2548. begin
  2549. result:=first_ansistring_to_pchar;
  2550. end;
  2551. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2552. begin
  2553. result:=first_arrayconstructor_to_set;
  2554. end;
  2555. function ttypeconvnode._first_class_to_intf : tnode;
  2556. begin
  2557. result:=first_class_to_intf;
  2558. end;
  2559. function ttypeconvnode._first_char_to_char : tnode;
  2560. begin
  2561. result:=first_char_to_char;
  2562. end;
  2563. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2564. const
  2565. firstconvert : array[tconverttype] of pointer = (
  2566. nil, { none }
  2567. @ttypeconvnode._first_nothing, {equal}
  2568. @ttypeconvnode._first_nothing, {not_possible}
  2569. nil, { removed in typecheck_string_to_string }
  2570. @ttypeconvnode._first_char_to_string,
  2571. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2572. nil, { removed in typecheck_chararray_to_string }
  2573. @ttypeconvnode._first_cchar_to_pchar,
  2574. @ttypeconvnode._first_cstring_to_pchar,
  2575. @ttypeconvnode._first_cstring_to_int,
  2576. @ttypeconvnode._first_ansistring_to_pchar,
  2577. @ttypeconvnode._first_string_to_chararray,
  2578. nil, { removed in typecheck_chararray_to_string }
  2579. @ttypeconvnode._first_array_to_pointer,
  2580. @ttypeconvnode._first_pointer_to_array,
  2581. @ttypeconvnode._first_int_to_int,
  2582. @ttypeconvnode._first_int_to_bool,
  2583. @ttypeconvnode._first_bool_to_bool,
  2584. @ttypeconvnode._first_bool_to_int,
  2585. @ttypeconvnode._first_real_to_real,
  2586. @ttypeconvnode._first_int_to_real,
  2587. nil, { removed in typecheck_real_to_currency }
  2588. @ttypeconvnode._first_proc_to_procvar,
  2589. @ttypeconvnode._first_nil_to_methodprocvar,
  2590. @ttypeconvnode._first_arrayconstructor_to_set,
  2591. @ttypeconvnode._first_set_to_set,
  2592. @ttypeconvnode._first_cord_to_pointer,
  2593. @ttypeconvnode._first_nothing,
  2594. @ttypeconvnode._first_nothing,
  2595. @ttypeconvnode._first_class_to_intf,
  2596. @ttypeconvnode._first_char_to_char,
  2597. @ttypeconvnode._first_nothing,
  2598. @ttypeconvnode._first_nothing,
  2599. nil,
  2600. nil,
  2601. nil,
  2602. nil,
  2603. nil,
  2604. nil,
  2605. nil
  2606. );
  2607. type
  2608. tprocedureofobject = function : tnode of object;
  2609. var
  2610. r : packed record
  2611. proc : pointer;
  2612. obj : pointer;
  2613. end;
  2614. begin
  2615. { this is a little bit dirty but it works }
  2616. { and should be quite portable too }
  2617. r.proc:=firstconvert[c];
  2618. r.obj:=self;
  2619. if not assigned(r.proc) then
  2620. internalerror(200312081);
  2621. first_call_helper:=tprocedureofobject(r)()
  2622. end;
  2623. function ttypeconvnode.pass_1 : tnode;
  2624. begin
  2625. if warn_pointer_to_signed then
  2626. cgmessage(type_w_pointer_to_signed);
  2627. result:=nil;
  2628. firstpass(left);
  2629. if codegenerror then
  2630. exit;
  2631. expectloc:=left.expectloc;
  2632. result:=first_call_helper(convtype);
  2633. end;
  2634. function ttypeconvnode.assign_allowed:boolean;
  2635. begin
  2636. result:=(convtype=tc_equal) or
  2637. { typecasting from void is always allowed }
  2638. is_void(left.resultdef) or
  2639. (left.resultdef.typ=formaldef) or
  2640. { int 2 int with same size reuses same location, or for
  2641. tp7 mode also allow size < orignal size }
  2642. (
  2643. (convtype=tc_int_2_int) and
  2644. (
  2645. (resultdef.size=left.resultdef.size) or
  2646. ((m_tp7 in current_settings.modeswitches) and
  2647. (resultdef.size<left.resultdef.size))
  2648. )
  2649. ) or
  2650. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2651. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2652. (nf_explicit in flags) and
  2653. (resultdef.size=left.resultdef.size));
  2654. { When using only a part of the value it can't be in a register since
  2655. that will load the value in a new register first }
  2656. { the same goes for changing the sign of equal-sized values which
  2657. are smaller than an entire register }
  2658. if (resultdef.size<left.resultdef.size) or
  2659. ((resultdef.size=left.resultdef.size) and
  2660. (left.resultdef.size<sizeof(aint)) and
  2661. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  2662. make_not_regable(left,[ra_addr_regable]);
  2663. end;
  2664. function ttypeconvnode.docompare(p: tnode) : boolean;
  2665. begin
  2666. docompare :=
  2667. inherited docompare(p) and
  2668. (convtype = ttypeconvnode(p).convtype);
  2669. end;
  2670. procedure ttypeconvnode._second_int_to_int;
  2671. begin
  2672. second_int_to_int;
  2673. end;
  2674. procedure ttypeconvnode._second_string_to_string;
  2675. begin
  2676. second_string_to_string;
  2677. end;
  2678. procedure ttypeconvnode._second_cstring_to_pchar;
  2679. begin
  2680. second_cstring_to_pchar;
  2681. end;
  2682. procedure ttypeconvnode._second_cstring_to_int;
  2683. begin
  2684. second_cstring_to_int;
  2685. end;
  2686. procedure ttypeconvnode._second_string_to_chararray;
  2687. begin
  2688. second_string_to_chararray;
  2689. end;
  2690. procedure ttypeconvnode._second_array_to_pointer;
  2691. begin
  2692. second_array_to_pointer;
  2693. end;
  2694. procedure ttypeconvnode._second_pointer_to_array;
  2695. begin
  2696. second_pointer_to_array;
  2697. end;
  2698. procedure ttypeconvnode._second_chararray_to_string;
  2699. begin
  2700. second_chararray_to_string;
  2701. end;
  2702. procedure ttypeconvnode._second_char_to_string;
  2703. begin
  2704. second_char_to_string;
  2705. end;
  2706. procedure ttypeconvnode._second_int_to_real;
  2707. begin
  2708. second_int_to_real;
  2709. end;
  2710. procedure ttypeconvnode._second_real_to_real;
  2711. begin
  2712. second_real_to_real;
  2713. end;
  2714. procedure ttypeconvnode._second_cord_to_pointer;
  2715. begin
  2716. second_cord_to_pointer;
  2717. end;
  2718. procedure ttypeconvnode._second_proc_to_procvar;
  2719. begin
  2720. second_proc_to_procvar;
  2721. end;
  2722. procedure ttypeconvnode._second_nil_to_methodprocvar;
  2723. begin
  2724. second_nil_to_methodprocvar;
  2725. end;
  2726. procedure ttypeconvnode._second_bool_to_int;
  2727. begin
  2728. second_bool_to_int;
  2729. end;
  2730. procedure ttypeconvnode._second_int_to_bool;
  2731. begin
  2732. second_int_to_bool;
  2733. end;
  2734. procedure ttypeconvnode._second_bool_to_bool;
  2735. begin
  2736. second_bool_to_bool;
  2737. end;
  2738. procedure ttypeconvnode._second_set_to_set;
  2739. begin
  2740. second_set_to_set;
  2741. end;
  2742. procedure ttypeconvnode._second_ansistring_to_pchar;
  2743. begin
  2744. second_ansistring_to_pchar;
  2745. end;
  2746. procedure ttypeconvnode._second_class_to_intf;
  2747. begin
  2748. second_class_to_intf;
  2749. end;
  2750. procedure ttypeconvnode._second_char_to_char;
  2751. begin
  2752. second_char_to_char;
  2753. end;
  2754. procedure ttypeconvnode._second_nothing;
  2755. begin
  2756. second_nothing;
  2757. end;
  2758. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2759. const
  2760. secondconvert : array[tconverttype] of pointer = (
  2761. @ttypeconvnode._second_nothing, {none}
  2762. @ttypeconvnode._second_nothing, {equal}
  2763. @ttypeconvnode._second_nothing, {not_possible}
  2764. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2765. @ttypeconvnode._second_char_to_string,
  2766. @ttypeconvnode._second_nothing, {char_to_charray}
  2767. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2768. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2769. @ttypeconvnode._second_cstring_to_pchar,
  2770. @ttypeconvnode._second_cstring_to_int,
  2771. @ttypeconvnode._second_ansistring_to_pchar,
  2772. @ttypeconvnode._second_string_to_chararray,
  2773. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2774. @ttypeconvnode._second_array_to_pointer,
  2775. @ttypeconvnode._second_pointer_to_array,
  2776. @ttypeconvnode._second_int_to_int,
  2777. @ttypeconvnode._second_int_to_bool,
  2778. @ttypeconvnode._second_bool_to_bool,
  2779. @ttypeconvnode._second_bool_to_int,
  2780. @ttypeconvnode._second_real_to_real,
  2781. @ttypeconvnode._second_int_to_real,
  2782. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2783. @ttypeconvnode._second_proc_to_procvar,
  2784. @ttypeconvnode._second_nil_to_methodprocvar,
  2785. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2786. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  2787. @ttypeconvnode._second_cord_to_pointer,
  2788. @ttypeconvnode._second_nothing, { interface 2 string }
  2789. @ttypeconvnode._second_nothing, { interface 2 guid }
  2790. @ttypeconvnode._second_class_to_intf,
  2791. @ttypeconvnode._second_char_to_char,
  2792. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2793. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2794. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2795. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2796. @ttypeconvnode._second_nothing, { variant_2_enum }
  2797. @ttypeconvnode._second_nothing, { enum_2_variant }
  2798. @ttypeconvnode._second_nothing, { variant_2_interface }
  2799. @ttypeconvnode._second_nothing, { interface_2_variant }
  2800. @ttypeconvnode._second_nothing { array_2_dynarray }
  2801. );
  2802. type
  2803. tprocedureofobject = procedure of object;
  2804. var
  2805. r : packed record
  2806. proc : pointer;
  2807. obj : pointer;
  2808. end;
  2809. begin
  2810. { this is a little bit dirty but it works }
  2811. { and should be quite portable too }
  2812. r.proc:=secondconvert[c];
  2813. r.obj:=self;
  2814. tprocedureofobject(r)();
  2815. end;
  2816. {*****************************************************************************
  2817. TISNODE
  2818. *****************************************************************************}
  2819. constructor tisnode.create(l,r : tnode);
  2820. begin
  2821. inherited create(isn,l,r);
  2822. end;
  2823. function tisnode.pass_typecheck:tnode;
  2824. var
  2825. paras: tcallparanode;
  2826. begin
  2827. result:=nil;
  2828. typecheckpass(left);
  2829. typecheckpass(right);
  2830. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2831. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2832. if codegenerror then
  2833. exit;
  2834. if (right.resultdef.typ=classrefdef) then
  2835. begin
  2836. { left must be a class }
  2837. if is_class(left.resultdef) then
  2838. begin
  2839. { the operands must be related }
  2840. if (not(tobjectdef(left.resultdef).is_related(
  2841. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2842. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2843. tobjectdef(left.resultdef)))) then
  2844. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2845. tclassrefdef(right.resultdef).pointeddef.typename);
  2846. end
  2847. else
  2848. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2849. { call fpc_do_is helper }
  2850. paras := ccallparanode.create(
  2851. left,
  2852. ccallparanode.create(
  2853. right,nil));
  2854. result := ccallnode.createintern('fpc_do_is',paras);
  2855. left := nil;
  2856. right := nil;
  2857. end
  2858. else if is_interface(right.resultdef) then
  2859. begin
  2860. { left is a class }
  2861. if is_class(left.resultdef) then
  2862. begin
  2863. { the class must implement the interface }
  2864. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2865. CGMessage2(type_e_classes_not_related,
  2866. FullTypeName(left.resultdef,right.resultdef),
  2867. FullTypeName(right.resultdef,left.resultdef))
  2868. end
  2869. { left is an interface }
  2870. else if is_interface(left.resultdef) then
  2871. begin
  2872. { the operands must be related }
  2873. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2874. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2875. CGMessage2(type_e_classes_not_related,
  2876. FullTypeName(left.resultdef,right.resultdef),
  2877. FullTypeName(right.resultdef,left.resultdef));
  2878. end
  2879. else
  2880. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2881. { call fpc_do_is helper }
  2882. paras := ccallparanode.create(
  2883. left,
  2884. ccallparanode.create(
  2885. right,nil));
  2886. result := ccallnode.createintern('fpc_do_is',paras);
  2887. left := nil;
  2888. right := nil;
  2889. end
  2890. else
  2891. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2892. resultdef:=booltype;
  2893. end;
  2894. function tisnode.pass_1 : tnode;
  2895. begin
  2896. internalerror(200204254);
  2897. result:=nil;
  2898. end;
  2899. { dummy pass_2, it will never be called, but we need one since }
  2900. { you can't instantiate an abstract class }
  2901. procedure tisnode.pass_generate_code;
  2902. begin
  2903. end;
  2904. {*****************************************************************************
  2905. TASNODE
  2906. *****************************************************************************}
  2907. constructor tasnode.create(l,r : tnode);
  2908. begin
  2909. inherited create(asn,l,r);
  2910. call := nil;
  2911. end;
  2912. destructor tasnode.destroy;
  2913. begin
  2914. call.free;
  2915. inherited destroy;
  2916. end;
  2917. function tasnode.pass_typecheck:tnode;
  2918. var
  2919. hp : tnode;
  2920. begin
  2921. result:=nil;
  2922. typecheckpass(right);
  2923. typecheckpass(left);
  2924. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2925. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2926. if codegenerror then
  2927. exit;
  2928. if (right.resultdef.typ=classrefdef) then
  2929. begin
  2930. { left must be a class }
  2931. if is_class(left.resultdef) then
  2932. begin
  2933. { the operands must be related }
  2934. if (not(tobjectdef(left.resultdef).is_related(
  2935. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2936. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2937. tobjectdef(left.resultdef)))) then
  2938. CGMessage2(type_e_classes_not_related,
  2939. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2940. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2941. end
  2942. else
  2943. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2944. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2945. end
  2946. else if is_interface(right.resultdef) then
  2947. begin
  2948. { left is a class }
  2949. if not(is_class(left.resultdef) or
  2950. is_interfacecom(left.resultdef)) then
  2951. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2952. resultdef:=right.resultdef;
  2953. { load the GUID of the interface }
  2954. if (right.nodetype=typen) then
  2955. begin
  2956. if assigned(tobjectdef(right.resultdef).iidguid) then
  2957. begin
  2958. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  2959. CGMessage1(type_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  2960. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  2961. right.free;
  2962. right:=hp;
  2963. end
  2964. else
  2965. internalerror(200206282);
  2966. typecheckpass(right);
  2967. end;
  2968. end
  2969. else
  2970. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2971. end;
  2972. function tasnode.dogetcopy: tnode;
  2973. begin
  2974. result := inherited dogetcopy;
  2975. if assigned(call) then
  2976. tasnode(result).call := call.getcopy
  2977. else
  2978. tasnode(result).call := nil;
  2979. end;
  2980. function tasnode.pass_1 : tnode;
  2981. var
  2982. procname: string;
  2983. begin
  2984. result:=nil;
  2985. if not assigned(call) then
  2986. begin
  2987. if is_class(left.resultdef) and
  2988. (right.resultdef.typ=classrefdef) then
  2989. call := ccallnode.createinternres('fpc_do_as',
  2990. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2991. resultdef)
  2992. else
  2993. begin
  2994. if is_class(left.resultdef) then
  2995. procname := 'fpc_class_as_intf'
  2996. else
  2997. procname := 'fpc_intf_as';
  2998. call := ccallnode.createintern(procname,
  2999. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3000. call := ctypeconvnode.create_internal(call,resultdef);
  3001. end;
  3002. left := nil;
  3003. right := nil;
  3004. firstpass(call);
  3005. if codegenerror then
  3006. exit;
  3007. expectloc:=call.expectloc;
  3008. end;
  3009. end;
  3010. begin
  3011. ctypeconvnode:=ttypeconvnode;
  3012. casnode:=tasnode;
  3013. cisnode:=tisnode;
  3014. end.