ncnv.pas 142 KB

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