ncnv.pas 134 KB

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