ncnv.pas 143 KB

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