ncnv.pas 134 KB

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