ncnv.pas 139 KB

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