ncnv.pas 129 KB

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