ncnv.pas 128 KB

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