ncnv.pas 131 KB

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