ncnv.pas 138 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 (tstringdef(resultdef).stringtype=st_ansistring) then
  838. begin
  839. result:=ccallnode.createinternres(
  840. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  841. ccallparanode.create(
  842. cordconstnode.create(
  843. ord(tarraydef(left.resultdef).lowrange=0),
  844. pasbool8type,
  845. false
  846. ),
  847. ccallparanode.create(
  848. cordconstnode.create(
  849. tstringdef(resultdef).encoding,
  850. u16inttype,
  851. true
  852. ),
  853. ccallparanode.create(left,nil)
  854. )
  855. ),
  856. resultdef
  857. );
  858. end
  859. else
  860. result:=ccallnode.createinternres(
  861. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  862. ccallparanode.create(cordconstnode.create(
  863. ord(tarraydef(left.resultdef).lowrange=0),pasbool8type,false),
  864. ccallparanode.create(left,nil)),resultdef);
  865. left:=nil;
  866. end;
  867. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  868. var
  869. newblock : tblocknode;
  870. newstat : tstatementnode;
  871. restemp : ttempcreatenode;
  872. pchtemp : pchar;
  873. arrsize : aint;
  874. chartype : string[8];
  875. begin
  876. result := nil;
  877. with tarraydef(resultdef) do
  878. begin
  879. if highrange<lowrange then
  880. internalerror(200501051);
  881. arrsize := highrange-lowrange+1;
  882. end;
  883. if (left.nodetype = stringconstn) and
  884. (tstringconstnode(left).cst_type=cst_conststring) then
  885. begin
  886. { if the array of char is large enough we can use the string
  887. constant directly. This is handled in ncgcnv }
  888. if (arrsize>=tstringconstnode(left).len) and
  889. is_char(tarraydef(resultdef).elementdef) then
  890. begin
  891. { pad the constant string with #0 to the array len }
  892. { (2.0.x compatible) }
  893. if (arrsize>tstringconstnode(left).len) then
  894. begin
  895. pchtemp:=concatansistrings(tstringconstnode(left).value_str,pchar(StringOfChar(#0,arrsize-tstringconstnode(left).len)),tstringconstnode(left).len,arrsize-tstringconstnode(left).len);
  896. left.free;
  897. left:=cstringconstnode.createpchar(pchtemp,arrsize);
  898. typecheckpass(left);
  899. end;
  900. exit;
  901. end;
  902. { Convert to wide/short/ansistring and call default helper }
  903. if is_widechar(tarraydef(resultdef).elementdef) then
  904. inserttypeconv(left,cwidestringtype)
  905. else
  906. begin
  907. if tstringconstnode(left).len>255 then
  908. inserttypeconv(left,cansistringtype)
  909. else
  910. inserttypeconv(left,cshortstringtype);
  911. end;
  912. end;
  913. if is_widechar(tarraydef(resultdef).elementdef) then
  914. chartype:='widechar'
  915. else
  916. chartype:='char';
  917. newblock:=internalstatements(newstat);
  918. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  919. addstatement(newstat,restemp);
  920. addstatement(newstat,ccallnode.createintern('fpc_'+tstringdef(left.resultdef).stringtypname+
  921. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  922. ctemprefnode.create(restemp),nil))));
  923. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  924. addstatement(newstat,ctemprefnode.create(restemp));
  925. result:=newblock;
  926. left:=nil;
  927. end;
  928. function ttypeconvnode.typecheck_char_to_string : tnode;
  929. var
  930. procname: string[31];
  931. para : tcallparanode;
  932. hp : tstringconstnode;
  933. ws : pcompilerwidestring;
  934. newblock : tblocknode;
  935. newstat : tstatementnode;
  936. restemp : ttempcreatenode;
  937. sa : ansistring;
  938. cw : WideChar;
  939. l : SizeUInt;
  940. begin
  941. result:=nil;
  942. if (left.nodetype=ordconstn) and
  943. ((tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring]) or
  944. (torddef(left.resultdef).ordtype=uchar) or
  945. ((torddef(left.resultdef).ordtype=uwidechar) and
  946. (tstringdef(resultdef).stringtype<>st_shortstring)
  947. )
  948. ) then
  949. begin
  950. if (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
  951. begin
  952. initwidestring(ws);
  953. if torddef(left.resultdef).ordtype=uwidechar then
  954. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  955. else
  956. concatwidestringchar(ws,asciichar2unicode(chr(tordconstnode(left).value.uvalue)));
  957. hp:=cstringconstnode.createwstr(ws);
  958. hp.changestringtype(resultdef);
  959. donewidestring(ws);
  960. end
  961. else
  962. begin
  963. if (torddef(left.resultdef).ordtype=uwidechar) then
  964. begin
  965. if (current_settings.sourcecodepage<>CP_UTF8) then
  966. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)))
  967. else
  968. begin
  969. Word(cw):=tcompilerwidechar(tordconstnode(left).value.uvalue);
  970. SetLength(sa,5);
  971. l:=UnicodeToUtf8(@(sa[1]),Length(sa),@cw,1);
  972. SetLength(sa,l-1);
  973. hp:=cstringconstnode.createstr(sa);
  974. end
  975. end
  976. else
  977. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  978. tstringconstnode(hp).changestringtype(resultdef);
  979. end;
  980. result:=hp;
  981. end
  982. else
  983. if (tstringdef(resultdef).stringtype=st_shortstring) and
  984. (torddef(left.resultdef).ordtype=uwidechar) and
  985. (tcompilerwidechar(tordconstnode(left).value.uvalue) <= 127)
  986. then
  987. begin
  988. SetLength(sa,1);
  989. Byte(sa[1]):= tordconstnode(left).value.uvalue;
  990. hp:=cstringconstnode.createstr(sa);
  991. tstringconstnode(hp).changestringtype(resultdef);
  992. result:=hp;
  993. end
  994. else
  995. { shortstrings are handled 'inline' (except for widechars) }
  996. if (tstringdef(resultdef).stringtype<>st_shortstring) or
  997. (torddef(left.resultdef).ordtype=uwidechar) then
  998. begin
  999. if (tstringdef(resultdef).stringtype<>st_shortstring) then
  1000. begin
  1001. { parameter }
  1002. para:=ccallparanode.create(left,nil);
  1003. { encoding required? }
  1004. if tstringdef(resultdef).stringtype=st_ansistring then
  1005. para:=ccallparanode.create(cordconstnode.create(tstringdef(resultdef).encoding,u16inttype,true),para);
  1006. { create the procname }
  1007. if torddef(left.resultdef).ordtype<>uwidechar then
  1008. procname:='fpc_char_to_'
  1009. else
  1010. procname:='fpc_uchar_to_';
  1011. procname:=procname+tstringdef(resultdef).stringtypname;
  1012. { and finally the call }
  1013. result:=ccallnode.createinternres(procname,para,resultdef);
  1014. end
  1015. else
  1016. begin
  1017. newblock:=internalstatements(newstat);
  1018. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1019. addstatement(newstat,restemp);
  1020. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1021. ctemprefnode.create(restemp),nil))));
  1022. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1023. addstatement(newstat,ctemprefnode.create(restemp));
  1024. result:=newblock;
  1025. end;
  1026. left := nil;
  1027. end
  1028. else
  1029. begin
  1030. { create word(byte(char) shl 8 or 1) for litte endian machines }
  1031. { and word(byte(char) or 256) for big endian machines }
  1032. left := ctypeconvnode.create_internal(left,u8inttype);
  1033. if (target_info.endian = endian_little) then
  1034. left := caddnode.create(orn,
  1035. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  1036. cordconstnode.create(1,s32inttype,false))
  1037. else
  1038. left := caddnode.create(orn,left,
  1039. cordconstnode.create(1 shl 8,s32inttype,false));
  1040. left := ctypeconvnode.create_internal(left,u16inttype);
  1041. typecheckpass(left);
  1042. end;
  1043. end;
  1044. function ttypeconvnode.typecheck_string_to_string : tnode;
  1045. begin
  1046. result:=nil;
  1047. if (left.nodetype=stringconstn) and
  1048. (((tstringdef(resultdef).stringtype=st_ansistring) and
  1049. (tstringdef(resultdef).encoding<>CP_NONE)
  1050. )
  1051. ) and
  1052. (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1053. begin
  1054. tstringconstnode(left).changestringtype(resultdef);
  1055. Result:=left;
  1056. left:=nil;
  1057. end
  1058. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  1059. (tstringdef(left.resultdef).stringtype=st_ansistring) and
  1060. (tstringdef(resultdef).encoding<>tstringdef(left.resultdef).encoding) then
  1061. begin
  1062. result:=ccallnode.createinternres(
  1063. 'fpc_ansistr_to_ansistr',
  1064. ccallparanode.create(
  1065. cordconstnode.create(
  1066. tstringdef(resultdef).encoding,
  1067. u16inttype,
  1068. true
  1069. ),
  1070. ccallparanode.create(left,nil)
  1071. ),
  1072. resultdef
  1073. );
  1074. left:=nil;
  1075. end
  1076. else if (left.nodetype=stringconstn) and
  1077. (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1078. (tstringdef(resultdef).stringtype=st_shortstring) then
  1079. begin
  1080. if not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)) then
  1081. begin
  1082. tstringconstnode(left).changestringtype(resultdef);
  1083. Result:=left;
  1084. left:=nil;
  1085. end;
  1086. end
  1087. end;
  1088. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  1089. begin
  1090. if resultdef.size <> 1 then
  1091. begin
  1092. { convert first to string, then to chararray }
  1093. inserttypeconv(left,cshortstringtype);
  1094. inserttypeconv(left,resultdef);
  1095. result:=left;
  1096. left := nil;
  1097. exit;
  1098. end;
  1099. result := nil;
  1100. end;
  1101. function ttypeconvnode.typecheck_char_to_char : tnode;
  1102. var
  1103. hp : tordconstnode;
  1104. begin
  1105. result:=nil;
  1106. if (left.nodetype=ordconstn) and
  1107. ((torddef(resultdef).ordtype<>uchar) or
  1108. (torddef(left.resultdef).ordtype<>uwidechar) or
  1109. (current_settings.sourcecodepage<>CP_UTF8))
  1110. then
  1111. begin
  1112. if (torddef(resultdef).ordtype=uchar) and
  1113. (torddef(left.resultdef).ordtype=uwidechar) and
  1114. (current_settings.sourcecodepage<>CP_UTF8) then
  1115. begin
  1116. hp:=cordconstnode.create(
  1117. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  1118. cchartype,true);
  1119. result:=hp;
  1120. end
  1121. else if (torddef(resultdef).ordtype=uwidechar) and
  1122. (torddef(left.resultdef).ordtype=uchar) then
  1123. begin
  1124. hp:=cordconstnode.create(
  1125. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  1126. cwidechartype,true);
  1127. result:=hp;
  1128. end
  1129. else
  1130. internalerror(200105131);
  1131. exit;
  1132. end;
  1133. end;
  1134. function ttypeconvnode.typecheck_int_to_int : tnode;
  1135. var
  1136. v : TConstExprInt;
  1137. begin
  1138. result:=nil;
  1139. if left.nodetype=ordconstn then
  1140. begin
  1141. v:=tordconstnode(left).value;
  1142. if is_currency(resultdef) then
  1143. v:=v*10000;
  1144. if (resultdef.typ=pointerdef) then
  1145. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1146. else
  1147. begin
  1148. if is_currency(left.resultdef) then
  1149. v:=v div 10000;
  1150. result:=cordconstnode.create(v,resultdef,false);
  1151. end;
  1152. end
  1153. else if left.nodetype=pointerconstn then
  1154. begin
  1155. v:=tpointerconstnode(left).value;
  1156. if (resultdef.typ=pointerdef) then
  1157. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1158. else
  1159. begin
  1160. if is_currency(resultdef) then
  1161. v:=v*10000;
  1162. result:=cordconstnode.create(v,resultdef,false);
  1163. end;
  1164. end
  1165. else
  1166. begin
  1167. { multiply by 10000 for currency. We need to use getcopy to pass
  1168. the argument because the current node is always disposed. Only
  1169. inserting the multiply in the left node is not possible because
  1170. it'll get in an infinite loop to convert int->currency }
  1171. if is_currency(resultdef) then
  1172. begin
  1173. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1174. include(result.flags,nf_is_currency);
  1175. end
  1176. else if is_currency(left.resultdef) then
  1177. begin
  1178. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1179. include(result.flags,nf_is_currency);
  1180. end;
  1181. end;
  1182. end;
  1183. function ttypeconvnode.typecheck_int_to_real : tnode;
  1184. var
  1185. rv : bestreal;
  1186. begin
  1187. result:=nil;
  1188. if left.nodetype=ordconstn then
  1189. begin
  1190. rv:=tordconstnode(left).value;
  1191. if is_currency(resultdef) then
  1192. rv:=rv*10000.0
  1193. else if is_currency(left.resultdef) then
  1194. rv:=rv/10000.0;
  1195. result:=crealconstnode.create(rv,resultdef);
  1196. end
  1197. else
  1198. begin
  1199. { multiply by 10000 for currency. We need to use getcopy to pass
  1200. the argument because the current node is always disposed. Only
  1201. inserting the multiply in the left node is not possible because
  1202. it'll get in an infinite loop to convert int->currency }
  1203. if is_currency(resultdef) then
  1204. begin
  1205. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1206. include(result.flags,nf_is_currency);
  1207. end
  1208. else if is_currency(left.resultdef) then
  1209. begin
  1210. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1211. include(result.flags,nf_is_currency);
  1212. end;
  1213. end;
  1214. end;
  1215. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1216. begin
  1217. if not is_currency(resultdef) then
  1218. internalerror(200304221);
  1219. result:=nil;
  1220. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1221. include(left.flags,nf_is_currency);
  1222. typecheckpass(left);
  1223. { Convert constants directly, else call Round() }
  1224. if left.nodetype=realconstn then
  1225. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1226. else
  1227. begin
  1228. result:=ccallnode.createinternres('fpc_round_real',
  1229. ccallparanode.create(left,nil),resultdef);
  1230. left:=nil;
  1231. end;
  1232. end;
  1233. function ttypeconvnode.typecheck_real_to_real : tnode;
  1234. begin
  1235. result:=nil;
  1236. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1237. begin
  1238. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1239. include(left.flags,nf_is_currency);
  1240. typecheckpass(left);
  1241. end
  1242. else
  1243. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1244. begin
  1245. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1246. include(left.flags,nf_is_currency);
  1247. typecheckpass(left);
  1248. end;
  1249. end;
  1250. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1251. begin
  1252. result:=nil;
  1253. if is_pwidechar(resultdef) then
  1254. inserttypeconv(left,cwidestringtype)
  1255. else
  1256. inserttypeconv(left,cshortstringtype);
  1257. { evaluate again, reset resultdef so the convert_typ
  1258. will be calculated again and cstring_to_pchar will
  1259. be used for futher conversion }
  1260. convtype:=tc_none;
  1261. result:=pass_typecheck;
  1262. end;
  1263. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1264. begin
  1265. result:=nil;
  1266. if is_pwidechar(resultdef) then
  1267. inserttypeconv(left,cwidestringtype)
  1268. else
  1269. if is_pchar(resultdef) and
  1270. (is_widestring(left.resultdef) or
  1271. is_unicodestring(left.resultdef)) then
  1272. begin
  1273. inserttypeconv(left,cansistringtype);
  1274. { the second pass of second_cstring_to_pchar expects a }
  1275. { strinconstn, but this may become a call to the }
  1276. { widestring manager in case left contains "high ascii" }
  1277. if (left.nodetype<>stringconstn) then
  1278. begin
  1279. result:=left;
  1280. left:=nil;
  1281. end;
  1282. end;
  1283. end;
  1284. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1285. var
  1286. fcc : cardinal;
  1287. pb : pbyte;
  1288. begin
  1289. result:=nil;
  1290. if left.nodetype<>stringconstn then
  1291. internalerror(200510012);
  1292. if tstringconstnode(left).len=4 then
  1293. begin
  1294. pb:=pbyte(tstringconstnode(left).value_str);
  1295. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1296. result:=cordconstnode.create(fcc,u32inttype,false);
  1297. end
  1298. else
  1299. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1300. end;
  1301. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1302. var
  1303. hp : tnode;
  1304. begin
  1305. result:=nil;
  1306. if left.nodetype<>arrayconstructorn then
  1307. internalerror(5546);
  1308. { remove typeconv node }
  1309. hp:=left;
  1310. left:=nil;
  1311. { create a set constructor tree }
  1312. arrayconstructor_to_set(hp);
  1313. result:=hp;
  1314. end;
  1315. function ttypeconvnode.typecheck_set_to_set : tnode;
  1316. begin
  1317. result:=nil;
  1318. { constant sets can be converted by changing the type only }
  1319. if (left.nodetype=setconstn) then
  1320. begin
  1321. left.resultdef:=resultdef;
  1322. result:=left;
  1323. left:=nil;
  1324. exit;
  1325. end;
  1326. end;
  1327. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1328. var
  1329. newblock : tblocknode;
  1330. newstat : tstatementnode;
  1331. restemp : ttempcreatenode;
  1332. begin
  1333. if tstringdef(resultdef).stringtype=st_shortstring then
  1334. begin
  1335. newblock:=internalstatements(newstat);
  1336. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1337. addstatement(newstat,restemp);
  1338. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1339. ctemprefnode.create(restemp),nil))));
  1340. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1341. addstatement(newstat,ctemprefnode.create(restemp));
  1342. result:=newblock;
  1343. end
  1344. else if tstringdef(resultdef).stringtype=st_ansistring then
  1345. result := ccallnode.createinternres(
  1346. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1347. ccallparanode.create(
  1348. cordconstnode.create(tstringdef(resultdef).encoding,u16inttype,true),
  1349. ccallparanode.create(left,nil)
  1350. ),
  1351. resultdef
  1352. )
  1353. else
  1354. result := ccallnode.createinternres(
  1355. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1356. ccallparanode.create(left,nil),resultdef);
  1357. left:=nil;
  1358. end;
  1359. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1360. begin
  1361. if assigned(tobjectdef(left.resultdef).iidstr) 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:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1366. tstringconstnode(result).changestringtype(cshortstringtype);
  1367. end;
  1368. end;
  1369. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1370. begin
  1371. if assigned(tobjectdef(left.resultdef).iidguid) then
  1372. begin
  1373. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1374. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1375. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1376. end;
  1377. end;
  1378. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1379. begin
  1380. { a dynamic array is a pointer to an array, so to convert it to }
  1381. { an open array, we have to dereference it (JM) }
  1382. result := ctypeconvnode.create_internal(left,voidpointertype);
  1383. typecheckpass(result);
  1384. { left is reused }
  1385. left := nil;
  1386. result := cderefnode.create(result);
  1387. include(result.flags,nf_no_checkpointer);
  1388. result.resultdef := resultdef;
  1389. end;
  1390. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1391. var
  1392. newblock : tblocknode;
  1393. newstat : tstatementnode;
  1394. restemp : ttempcreatenode;
  1395. begin
  1396. if tstringdef(resultdef).stringtype=st_shortstring then
  1397. begin
  1398. newblock:=internalstatements(newstat);
  1399. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1400. addstatement(newstat,restemp);
  1401. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1402. ctemprefnode.create(restemp),nil))));
  1403. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1404. addstatement(newstat,ctemprefnode.create(restemp));
  1405. result:=newblock;
  1406. end
  1407. else if tstringdef(resultdef).stringtype=st_ansistring then
  1408. begin
  1409. result:=ccallnode.createinternres(
  1410. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1411. ccallparanode.create(
  1412. cordconstnode.create(
  1413. tstringdef(resultdef).encoding,
  1414. u16inttype,
  1415. true
  1416. ),
  1417. ccallparanode.create(left,nil)
  1418. ),
  1419. resultdef
  1420. );
  1421. end
  1422. else
  1423. result := ccallnode.createinternres(
  1424. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1425. ccallparanode.create(left,nil),resultdef);
  1426. left:=nil;
  1427. end;
  1428. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1429. begin
  1430. result := ccallnode.createinternres(
  1431. 'fpc_variant_to_dynarray',
  1432. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1433. ccallparanode.create(left,nil)
  1434. ),resultdef);
  1435. typecheckpass(result);
  1436. left:=nil;
  1437. end;
  1438. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1439. begin
  1440. result := ccallnode.createinternres(
  1441. 'fpc_dynarray_to_variant',
  1442. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1443. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1444. ),resultdef);
  1445. typecheckpass(result);
  1446. left:=nil;
  1447. end;
  1448. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1449. begin
  1450. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1451. result := ccallnode.createinternres(
  1452. 'fpc_variant_to_idispatch',
  1453. ccallparanode.create(left,nil)
  1454. ,resultdef)
  1455. else
  1456. result := ccallnode.createinternres(
  1457. 'fpc_variant_to_interface',
  1458. ccallparanode.create(left,nil)
  1459. ,resultdef);
  1460. typecheckpass(result);
  1461. left:=nil;
  1462. end;
  1463. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1464. begin
  1465. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1466. result := ccallnode.createinternres(
  1467. 'fpc_idispatch_to_variant',
  1468. ccallparanode.create(left,nil)
  1469. ,resultdef)
  1470. else
  1471. result := ccallnode.createinternres(
  1472. 'fpc_interface_to_variant',
  1473. ccallparanode.create(left,nil)
  1474. ,resultdef);
  1475. typecheckpass(result);
  1476. left:=nil;
  1477. end;
  1478. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1479. begin
  1480. result := ctypeconvnode.create_internal(left,sinttype);
  1481. result := ctypeconvnode.create_internal(result,resultdef);
  1482. typecheckpass(result);
  1483. { left is reused }
  1484. left := nil;
  1485. end;
  1486. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1487. begin
  1488. result := ctypeconvnode.create_internal(left,sinttype);
  1489. result := ctypeconvnode.create_internal(result,cvarianttype);
  1490. typecheckpass(result);
  1491. { left is reused }
  1492. left := nil;
  1493. end;
  1494. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1495. var
  1496. newstatement : tstatementnode;
  1497. temp : ttempcreatenode;
  1498. temp2 : ttempcreatenode;
  1499. begin
  1500. { create statements with call to getmem+initialize }
  1501. result:=internalstatements(newstatement);
  1502. { create temp for result }
  1503. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1504. addstatement(newstatement,temp);
  1505. { get temp for array of lengths }
  1506. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1507. addstatement(newstatement,temp2);
  1508. { one dimensional }
  1509. addstatement(newstatement,cassignmentnode.create(
  1510. ctemprefnode.create_offset(temp2,0),
  1511. cordconstnode.create
  1512. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1513. { create call to fpc_dynarr_setlength }
  1514. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1515. ccallparanode.create(caddrnode.create_internal
  1516. (ctemprefnode.create(temp2)),
  1517. ccallparanode.create(cordconstnode.create
  1518. (1,s32inttype,true),
  1519. ccallparanode.create(caddrnode.create_internal
  1520. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1521. ccallparanode.create(
  1522. ctypeconvnode.create_internal(
  1523. ctemprefnode.create(temp),voidpointertype),
  1524. nil))))
  1525. ));
  1526. addstatement(newstatement,ctempdeletenode.create(temp2));
  1527. { copy ... }
  1528. addstatement(newstatement,cassignmentnode.create(
  1529. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1530. left
  1531. ));
  1532. { left is reused }
  1533. left:=nil;
  1534. { the last statement should return the value as
  1535. location and type, this is done be referencing the
  1536. temp and converting it first from a persistent temp to
  1537. normal temp }
  1538. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1539. addstatement(newstatement,ctemprefnode.create(temp));
  1540. end;
  1541. procedure copyparasym(p:TObject;arg:pointer);
  1542. var
  1543. newparast : TSymtable absolute arg;
  1544. vs : tparavarsym;
  1545. begin
  1546. if tsym(p).typ<>paravarsym then
  1547. exit;
  1548. with tparavarsym(p) do
  1549. begin
  1550. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1551. vs.defaultconstsym:=defaultconstsym;
  1552. newparast.insert(vs);
  1553. end;
  1554. end;
  1555. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1556. var
  1557. pd : tabstractprocdef;
  1558. nestinglevel : byte;
  1559. begin
  1560. result:=nil;
  1561. pd:=tabstractprocdef(left.resultdef);
  1562. { create procvardef (default for create_proc_to_procvar is voiddef,
  1563. but if later a regular inserttypeconvnode() is used to insert a type
  1564. conversion to the actual procvardef, totypedef will be set to the
  1565. real procvartype that we are converting to) }
  1566. if assigned(totypedef) and
  1567. (totypedef.typ=procvardef) then
  1568. resultdef:=totypedef
  1569. else
  1570. begin
  1571. nestinglevel:=pd.parast.symtablelevel;
  1572. resultdef:=tprocvardef.create(nestinglevel);
  1573. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1574. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1575. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1576. tprocvardef(resultdef).returndef:=pd.returndef;
  1577. { method ? then set the methodpointer flag }
  1578. if (pd.owner.symtabletype=ObjectSymtable) then
  1579. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1580. { only need the address of the method? this is needed
  1581. for @tobject.create. In this case there will be a loadn without
  1582. a methodpointer. }
  1583. if (left.nodetype=loadn) and
  1584. not assigned(tloadnode(left).left) and
  1585. (not(m_nested_procvars in current_settings.modeswitches) or
  1586. not is_nested_pd(tprocvardef(resultdef))) then
  1587. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1588. { Add parameters use only references, we don't need to keep the
  1589. parast. We use the parast from the original function to calculate
  1590. our parameter data and reset it afterwards }
  1591. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1592. tprocvardef(resultdef).calcparas;
  1593. end;
  1594. end;
  1595. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1596. const
  1597. resultdefconvert : array[tconverttype] of pointer = (
  1598. {none} nil,
  1599. {equal} nil,
  1600. {not_possible} nil,
  1601. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1602. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1603. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1604. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1605. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1606. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1607. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1608. { ansistring_2_pchar } nil,
  1609. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1610. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1611. { array_2_pointer } nil,
  1612. { pointer_2_array } nil,
  1613. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1614. { int_2_bool } nil,
  1615. { bool_2_bool } nil,
  1616. { bool_2_int } nil,
  1617. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1618. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1619. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1620. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1621. { nil_2_methodprocvar } nil,
  1622. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1623. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1624. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1625. { intf_2_string } @ttypeconvnode.typecheck_interface_to_string,
  1626. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1627. { class_2_intf } nil,
  1628. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1629. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1630. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1631. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1632. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1633. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1634. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1635. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1636. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1637. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1638. );
  1639. type
  1640. tprocedureofobject = function : tnode of object;
  1641. var
  1642. r : packed record
  1643. proc : pointer;
  1644. obj : pointer;
  1645. end;
  1646. begin
  1647. result:=nil;
  1648. { this is a little bit dirty but it works }
  1649. { and should be quite portable too }
  1650. r.proc:=resultdefconvert[c];
  1651. r.obj:=self;
  1652. if assigned(r.proc) then
  1653. result:=tprocedureofobject(r)();
  1654. end;
  1655. function ttypeconvnode.actualtargetnode: tnode;
  1656. begin
  1657. result:=self;
  1658. while (result.nodetype=typeconvn) and
  1659. ttypeconvnode(result).retains_value_location do
  1660. result:=ttypeconvnode(result).left;
  1661. end;
  1662. function ttypeconvnode.pass_typecheck:tnode;
  1663. var
  1664. hdef : tdef;
  1665. hp : tnode;
  1666. currprocdef : tabstractprocdef;
  1667. aprocdef : tprocdef;
  1668. eq : tequaltype;
  1669. cdoptions : tcompare_defs_options;
  1670. newblock: tblocknode;
  1671. newstatement: tstatementnode;
  1672. tempnode: ttempcreatenode;
  1673. begin
  1674. result:=nil;
  1675. resultdef:=totypedef;
  1676. typecheckpass(left);
  1677. if codegenerror then
  1678. exit;
  1679. { When absolute force tc_equal }
  1680. if (nf_absolute in flags) then
  1681. begin
  1682. convtype:=tc_equal;
  1683. if not(tstoreddef(resultdef).is_intregable) and
  1684. not(tstoreddef(resultdef).is_fpuregable) then
  1685. make_not_regable(left,[ra_addr_regable]);
  1686. exit;
  1687. end;
  1688. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1689. convert on the procvar value. This is used to access the
  1690. fields of a methodpointer }
  1691. if not(nf_load_procvar in flags) and
  1692. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1693. maybe_call_procvar(left,true);
  1694. { convert array constructors to sets, because there is no conversion
  1695. possible for array constructors }
  1696. if (resultdef.typ<>arraydef) and
  1697. is_array_constructor(left.resultdef) then
  1698. begin
  1699. arrayconstructor_to_set(left);
  1700. typecheckpass(left);
  1701. end;
  1702. if convtype=tc_none then
  1703. begin
  1704. cdoptions:=[cdo_check_operator,cdo_allow_variant,cdo_warn_incompatible_univ];
  1705. if nf_explicit in flags then
  1706. include(cdoptions,cdo_explicit);
  1707. if nf_internal in flags then
  1708. include(cdoptions,cdo_internal);
  1709. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1710. case eq of
  1711. te_exact,
  1712. te_equal :
  1713. begin
  1714. result := simplify(false);
  1715. if assigned(result) then
  1716. exit;
  1717. { in case of bitpacked accesses, the original type must
  1718. remain so that not too many/few bits are laoded }
  1719. if is_bitpacked_access(left) then
  1720. convtype:=tc_int_2_int;
  1721. { Only leave when there is no conversion to do.
  1722. We can still need to call a conversion routine,
  1723. like the routine to convert a stringconstnode }
  1724. if (convtype in [tc_equal,tc_not_possible]) and
  1725. { some conversions, like dynarray to pointer in Delphi
  1726. mode, must not be removed, because then we get memory
  1727. leaks due to missing temp finalization }
  1728. (not is_managed_type(left.resultdef) or
  1729. { different kinds of refcounted types may need calls
  1730. to different kinds of refcounting helpers }
  1731. (resultdef=left.resultdef)) then
  1732. begin
  1733. left.resultdef:=resultdef;
  1734. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1735. include(left.flags, nf_typedaddr);
  1736. result:=left;
  1737. left:=nil;
  1738. exit;
  1739. end;
  1740. end;
  1741. te_convert_l1,
  1742. te_convert_l2,
  1743. te_convert_l3,
  1744. te_convert_l4,
  1745. te_convert_l5:
  1746. { nothing to do }
  1747. ;
  1748. te_convert_operator :
  1749. begin
  1750. include(current_procinfo.flags,pi_do_call);
  1751. addsymref(aprocdef.procsym);
  1752. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1753. { tell explicitly which def we must use !! (PM) }
  1754. tcallnode(hp).procdefinition:=aprocdef;
  1755. left:=nil;
  1756. result:=hp;
  1757. exit;
  1758. end;
  1759. te_incompatible :
  1760. begin
  1761. { Procedures have a resultdef of voiddef and functions of their
  1762. own resultdef. They will therefore always be incompatible with
  1763. a procvar. Because isconvertable cannot check for procedures we
  1764. use an extra check for them.}
  1765. if (left.nodetype=calln) and
  1766. (tcallnode(left).required_para_count=0) and
  1767. (resultdef.typ=procvardef) and
  1768. (
  1769. (m_tp_procvar in current_settings.modeswitches) or
  1770. (m_mac_procvar in current_settings.modeswitches)
  1771. ) then
  1772. begin
  1773. if assigned(tcallnode(left).right) then
  1774. begin
  1775. { this is already a procvar, if it is really equal
  1776. is checked below }
  1777. convtype:=tc_equal;
  1778. hp:=tcallnode(left).right.getcopy;
  1779. currprocdef:=tabstractprocdef(hp.resultdef);
  1780. end
  1781. else
  1782. begin
  1783. convtype:=tc_proc_2_procvar;
  1784. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1785. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1786. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1787. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1788. begin
  1789. if assigned(tcallnode(left).methodpointer) then
  1790. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  1791. else
  1792. tloadnode(hp).set_mp(load_self_node);
  1793. end;
  1794. typecheckpass(hp);
  1795. end;
  1796. left.free;
  1797. left:=hp;
  1798. { Now check if the procedure we are going to assign to
  1799. the procvar, is compatible with the procvar's type }
  1800. if not(nf_explicit in flags) and
  1801. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
  1802. IncompatibleTypes(left.resultdef,resultdef);
  1803. exit;
  1804. end
  1805. else if maybe_global_proc_to_nested(left,resultdef) then
  1806. begin
  1807. result:=left;
  1808. left:=nil;
  1809. exit;
  1810. end;
  1811. { Handle explicit type conversions }
  1812. if nf_explicit in flags then
  1813. begin
  1814. { do common tc_equal cast }
  1815. convtype:=tc_equal;
  1816. { ordinal constants can be resized to 1,2,4,8 bytes }
  1817. if (left.nodetype=ordconstn) then
  1818. begin
  1819. { Insert typeconv for ordinal to the correct size first on left, after
  1820. that the other conversion can be done }
  1821. hdef:=nil;
  1822. case longint(resultdef.size) of
  1823. 1 :
  1824. hdef:=s8inttype;
  1825. 2 :
  1826. hdef:=s16inttype;
  1827. 4 :
  1828. hdef:=s32inttype;
  1829. 8 :
  1830. hdef:=s64inttype;
  1831. end;
  1832. { we need explicit, because it can also be an enum }
  1833. if assigned(hdef) then
  1834. inserttypeconv_internal(left,hdef)
  1835. else
  1836. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1837. end;
  1838. { check if the result could be in a register }
  1839. if (not(tstoreddef(resultdef).is_intregable) and
  1840. not(tstoreddef(resultdef).is_fpuregable)) or
  1841. ((left.resultdef.typ = floatdef) and
  1842. (resultdef.typ <> floatdef)) then
  1843. make_not_regable(left,[ra_addr_regable]);
  1844. { class/interface to class/interface, with checkobject support }
  1845. if is_class_or_interface_or_objc(resultdef) and
  1846. is_class_or_interface_or_objc(left.resultdef) then
  1847. begin
  1848. { check if the types are related }
  1849. if not(nf_internal in flags) and
  1850. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1851. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1852. begin
  1853. { Give an error when typecasting class to interface, this is compatible
  1854. with delphi }
  1855. if is_interface(resultdef) and
  1856. not is_interface(left.resultdef) then
  1857. CGMessage2(type_e_classes_not_related,
  1858. FullTypeName(left.resultdef,resultdef),
  1859. FullTypeName(resultdef,left.resultdef))
  1860. else
  1861. CGMessage2(type_w_classes_not_related,
  1862. FullTypeName(left.resultdef,resultdef),
  1863. FullTypeName(resultdef,left.resultdef))
  1864. end;
  1865. { Add runtime check? }
  1866. if not is_objc_class_or_protocol(resultdef) and
  1867. not is_objc_class_or_protocol(left.resultdef) and
  1868. (cs_check_object in current_settings.localswitches) and
  1869. not(nf_internal in flags) then
  1870. begin
  1871. { we can translate the typeconvnode to 'as' when
  1872. typecasting to a class or interface }
  1873. { we need to make sure the result can still be
  1874. passed as a var parameter }
  1875. newblock:=internalstatements(newstatement);
  1876. if (valid_for_var(left,false)) then
  1877. begin
  1878. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1879. addstatement(newstatement,tempnode);
  1880. addstatement(newstatement,cassignmentnode.create(
  1881. ctemprefnode.create(tempnode),
  1882. caddrnode.create_internal(left)));
  1883. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1884. end
  1885. else
  1886. begin
  1887. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1888. addstatement(newstatement,tempnode);
  1889. addstatement(newstatement,cassignmentnode.create(
  1890. ctemprefnode.create(tempnode),
  1891. left));
  1892. left:=ctemprefnode.create(tempnode);
  1893. end;
  1894. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1895. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1896. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1897. left:=nil;
  1898. result:=newblock;
  1899. exit;
  1900. end;
  1901. end
  1902. else
  1903. begin
  1904. { only if the same size or formal def, and }
  1905. { don't allow type casting of constants to }
  1906. { structured types }
  1907. if not(
  1908. (left.resultdef.typ=formaldef) or
  1909. (
  1910. not(is_open_array(left.resultdef)) and
  1911. not(is_array_constructor(left.resultdef)) and
  1912. not(is_array_of_const(left.resultdef)) and
  1913. (left.resultdef.size=resultdef.size) and
  1914. { disallow casts of const nodes }
  1915. (not is_constnode(left) or
  1916. { however, there are some exceptions }
  1917. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1918. filedef,variantdef,objectdef]) or
  1919. is_class_or_interface_or_objc(resultdef) or
  1920. { the softfloat code generates casts <const. float> to record }
  1921. (nf_internal in flags)
  1922. ))
  1923. ) or
  1924. (
  1925. is_void(left.resultdef) and
  1926. (left.nodetype=derefn)
  1927. )
  1928. ) then
  1929. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1930. end;
  1931. end
  1932. else
  1933. IncompatibleTypes(left.resultdef,resultdef);
  1934. end;
  1935. else
  1936. internalerror(200211231);
  1937. end;
  1938. end;
  1939. { Give hint or warning for unportable code, exceptions are
  1940. - typecasts from constants
  1941. - void }
  1942. if not(nf_internal in flags) and
  1943. (left.nodetype<>ordconstn) and
  1944. not(is_void(left.resultdef)) and
  1945. (((left.resultdef.typ=orddef) and
  1946. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1947. ((resultdef.typ=orddef) and
  1948. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1949. begin
  1950. {Converting pointers to signed integers is a bad idea. Warn.}
  1951. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  1952. { Give a warning when sizes don't match, because then info will be lost }
  1953. if left.resultdef.size=resultdef.size then
  1954. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1955. else
  1956. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1957. end;
  1958. { tc_cord_2_pointer still requires a type check, which
  1959. simplify does not do }
  1960. if (convtype<>tc_cord_2_pointer) then
  1961. begin
  1962. result := simplify(false);
  1963. if assigned(result) then
  1964. exit;
  1965. end;
  1966. { now call the resultdef helper to do constant folding }
  1967. result:=typecheck_call_helper(convtype);
  1968. end;
  1969. {$ifndef cpu64bitalu}
  1970. { checks whether we can safely remove 64 bit typeconversions }
  1971. { in case range and overflow checking are off, and in case }
  1972. { the result of this node tree is downcasted again to a }
  1973. { 8/16/32 bit value afterwards }
  1974. function checkremove64bittypeconvs(n: tnode; out gotsint: boolean): boolean;
  1975. var
  1976. gotmuldivmod: boolean;
  1977. { checks whether a node is either an u32bit, or originally }
  1978. { was one but was implicitly converted to s64bit }
  1979. function wasoriginallyint32(n: tnode): boolean;
  1980. begin
  1981. if (n.resultdef.typ<>orddef) then
  1982. exit(false);
  1983. if (torddef(n.resultdef).ordtype in [s32bit,u32bit]) then
  1984. begin
  1985. if (torddef(n.resultdef).ordtype=s32bit) then
  1986. gotsint:=true;
  1987. exit(true);
  1988. end;
  1989. if (torddef(n.resultdef).ordtype=s64bit) and
  1990. { nf_explicit is also set for explicitly typecasted }
  1991. { ordconstn's }
  1992. ([nf_internal,nf_explicit]*n.flags=[]) and
  1993. { either a typeconversion node coming from u32bit }
  1994. (((n.nodetype=typeconvn) and
  1995. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  1996. (torddef(ttypeconvnode(n).left.resultdef).ordtype in [s32bit,u32bit])) or
  1997. { or an ordconstnode which was/is a valid cardinal }
  1998. ((n.nodetype=ordconstn) and
  1999. (tordconstnode(n).value>=int64(low(longint))) and
  2000. (tordconstnode(n).value<=high(cardinal)))) then
  2001. begin
  2002. if ((n.nodetype=typeconvn) and
  2003. (torddef(ttypeconvnode(n).left.resultdef).ordtype=s32bit)) or
  2004. ((n.nodetype=ordconstn) and
  2005. (tordconstnode(n).value<0)) then
  2006. gotsint:=true;
  2007. exit(true);
  2008. end;
  2009. result:=false;
  2010. end;
  2011. function docheckremove64bittypeconvs(n: tnode): boolean;
  2012. begin
  2013. result:=false;
  2014. if wasoriginallyint32(n) then
  2015. exit(true);
  2016. case n.nodetype of
  2017. subn,orn,xorn:
  2018. begin
  2019. { nf_internal is set by taddnode.typecheckpass in }
  2020. { case the arguments of this subn were u32bit, but }
  2021. { upcasted to s64bit for calculation correctness }
  2022. { (normally only needed when range checking, but }
  2023. { also done otherwise so there is no difference }
  2024. { in overload choosing etc between $r+ and $r-) }
  2025. if (nf_internal in n.flags) then
  2026. result:=true
  2027. else
  2028. result:=
  2029. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2030. docheckremove64bittypeconvs(tbinarynode(n).right);
  2031. end;
  2032. addn,muln,divn,modn,andn:
  2033. begin
  2034. if n.nodetype in [muln,divn,modn] then
  2035. gotmuldivmod:=true;
  2036. result:=
  2037. docheckremove64bittypeconvs(tbinarynode(n).left) and
  2038. docheckremove64bittypeconvs(tbinarynode(n).right);
  2039. end;
  2040. end;
  2041. end;
  2042. begin { checkremove64bittypeconvs }
  2043. gotmuldivmod:=false;
  2044. gotsint:=false;
  2045. result:=
  2046. docheckremove64bittypeconvs(n) and
  2047. not(gotmuldivmod and gotsint);
  2048. end;
  2049. procedure doremove64bittypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean);
  2050. begin
  2051. case n.nodetype of
  2052. subn,addn,muln,divn,modn,xorn,andn,orn:
  2053. begin
  2054. exclude(n.flags,nf_internal);
  2055. if not forceunsigned and
  2056. is_signed(n.resultdef) then
  2057. begin
  2058. doremove64bittypeconvs(tbinarynode(n).left,s32inttype,false);
  2059. doremove64bittypeconvs(tbinarynode(n).right,s32inttype,false);
  2060. n.resultdef:=s32inttype
  2061. end
  2062. else
  2063. begin
  2064. doremove64bittypeconvs(tbinarynode(n).left,u32inttype,forceunsigned);
  2065. doremove64bittypeconvs(tbinarynode(n).right,u32inttype,forceunsigned);
  2066. n.resultdef:=u32inttype
  2067. end;
  2068. end;
  2069. ordconstn:
  2070. inserttypeconv_internal(n,todef);
  2071. typeconvn:
  2072. begin
  2073. n.resultdef:=todef;
  2074. ttypeconvnode(n).totypedef:=todef;
  2075. end;
  2076. end;
  2077. end;
  2078. {$endif not cpu64bitalu}
  2079. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2080. var
  2081. hp: tnode;
  2082. {$ifndef cpu64bitalu}
  2083. foundsint: boolean;
  2084. {$endif not cpu64bitalu}
  2085. begin
  2086. result := nil;
  2087. { Constant folding and other node transitions to
  2088. remove the typeconv node }
  2089. case left.nodetype of
  2090. stringconstn :
  2091. if (convtype=tc_string_2_string) and
  2092. (
  2093. ((not is_widechararray(left.resultdef) and
  2094. not is_wide_or_unicode_string(left.resultdef)) or
  2095. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  2096. )
  2097. ) then
  2098. begin
  2099. tstringconstnode(left).changestringtype(resultdef);
  2100. result:=left;
  2101. left:=nil;
  2102. exit;
  2103. end;
  2104. realconstn :
  2105. begin
  2106. if (convtype = tc_real_2_currency) then
  2107. result := typecheck_real_to_currency
  2108. else if (convtype = tc_real_2_real) then
  2109. result := typecheck_real_to_real
  2110. else
  2111. exit;
  2112. if not(assigned(result)) then
  2113. begin
  2114. result := left;
  2115. left := nil;
  2116. end;
  2117. if (result.nodetype = realconstn) then
  2118. begin
  2119. hp:=result;
  2120. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2121. if ([nf_explicit,nf_internal] * flags <> []) then
  2122. include(result.flags, nf_explicit);
  2123. hp.free;
  2124. end;
  2125. end;
  2126. niln :
  2127. begin
  2128. { nil to ordinal node }
  2129. if (resultdef.typ=orddef) then
  2130. begin
  2131. hp:=cordconstnode.create(0,resultdef,true);
  2132. if ([nf_explicit,nf_internal] * flags <> []) then
  2133. include(hp.flags, nf_explicit);
  2134. result:=hp;
  2135. exit;
  2136. end
  2137. else
  2138. { fold nil to any pointer type }
  2139. if (resultdef.typ=pointerdef) then
  2140. begin
  2141. hp:=cnilnode.create;
  2142. hp.resultdef:=resultdef;
  2143. if ([nf_explicit,nf_internal] * flags <> []) then
  2144. include(hp.flags, nf_explicit);
  2145. result:=hp;
  2146. exit;
  2147. end
  2148. else
  2149. { remove typeconv after niln, but not when the result is a
  2150. methodpointer. The typeconv of the methodpointer will then
  2151. take care of updateing size of niln to OS_64 }
  2152. if not((resultdef.typ=procvardef) and
  2153. not(tprocvardef(resultdef).is_addressonly)) then
  2154. begin
  2155. left.resultdef:=resultdef;
  2156. if ([nf_explicit,nf_internal] * flags <> []) then
  2157. include(left.flags, nf_explicit);
  2158. result:=left;
  2159. left:=nil;
  2160. exit;
  2161. end;
  2162. end;
  2163. ordconstn :
  2164. begin
  2165. { ordinal contants can be directly converted }
  2166. { but not char to char because it is a widechar to char or via versa }
  2167. { which needs extra code to do the code page transistion }
  2168. { constant ordinal to pointer }
  2169. if (resultdef.typ=pointerdef) and
  2170. (convtype<>tc_cchar_2_pchar) then
  2171. begin
  2172. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2173. if ([nf_explicit,nf_internal] * flags <> []) then
  2174. include(hp.flags, nf_explicit);
  2175. result:=hp;
  2176. exit;
  2177. end
  2178. else if is_ordinal(resultdef) and
  2179. not(convtype=tc_char_2_char) then
  2180. begin
  2181. { replace the resultdef and recheck the range }
  2182. if ([nf_explicit,nf_internal] * flags <> []) then
  2183. include(left.flags, nf_explicit)
  2184. else
  2185. { no longer an ordconst with an explicit typecast }
  2186. exclude(left.flags, nf_explicit);
  2187. { when converting from one boolean type to another, force }
  2188. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2189. { (Delphi-compatibile) }
  2190. if is_boolean(left.resultdef) and
  2191. is_boolean(resultdef) and
  2192. (is_cbool(left.resultdef) or
  2193. is_cbool(resultdef)) then
  2194. begin
  2195. if is_pasbool(resultdef) then
  2196. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2197. else
  2198. {$ifdef VER2_2}
  2199. tordconstnode(left).value:=ord(tordconstnode(left).value<>0);
  2200. tordconstnode(left).value:=-tordconstnode(left).value;
  2201. {$else}
  2202. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2203. {$endif VER2_2}
  2204. end
  2205. else
  2206. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags),false);
  2207. left.resultdef:=resultdef;
  2208. tordconstnode(left).typedef:=resultdef;
  2209. result:=left;
  2210. left:=nil;
  2211. exit;
  2212. end;
  2213. end;
  2214. pointerconstn :
  2215. begin
  2216. { pointerconstn to any pointer is folded too }
  2217. if (resultdef.typ=pointerdef) then
  2218. begin
  2219. left.resultdef:=resultdef;
  2220. if ([nf_explicit,nf_internal] * flags <> []) then
  2221. include(left.flags, nf_explicit)
  2222. else
  2223. { no longer an ordconst with an explicit typecast }
  2224. exclude(left.flags, nf_explicit);
  2225. result:=left;
  2226. left:=nil;
  2227. exit;
  2228. end
  2229. { constant pointer to ordinal }
  2230. else if is_ordinal(resultdef) then
  2231. begin
  2232. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2233. resultdef,not(nf_explicit in flags));
  2234. if ([nf_explicit,nf_internal] * flags <> []) then
  2235. include(hp.flags, nf_explicit);
  2236. result:=hp;
  2237. exit;
  2238. end;
  2239. end;
  2240. end;
  2241. {$ifndef cpu64bitalu}
  2242. { must be done before code below, because we need the
  2243. typeconversions for ordconstn's as well }
  2244. case convtype of
  2245. tc_int_2_int:
  2246. begin
  2247. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2248. (resultdef.typ in [pointerdef,orddef,enumdef]) and
  2249. (resultdef.size <= 4) and
  2250. is_64bitint(left.resultdef) and
  2251. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2252. checkremove64bittypeconvs(left,foundsint) then
  2253. begin
  2254. { avoid unnecessary widening of intermediary calculations }
  2255. { to 64 bit }
  2256. doremove64bittypeconvs(left,generrordef,not foundsint);
  2257. end;
  2258. end;
  2259. end;
  2260. {$endif not cpu64bitalu}
  2261. end;
  2262. procedure Ttypeconvnode.mark_write;
  2263. begin
  2264. left.mark_write;
  2265. end;
  2266. function ttypeconvnode.first_cord_to_pointer : tnode;
  2267. begin
  2268. result:=nil;
  2269. internalerror(200104043);
  2270. end;
  2271. function ttypeconvnode.first_int_to_int : tnode;
  2272. begin
  2273. first_int_to_int:=nil;
  2274. expectloc:=left.expectloc;
  2275. if not is_void(left.resultdef) then
  2276. begin
  2277. if (left.expectloc<>LOC_REGISTER) and
  2278. ((resultdef.size>left.resultdef.size) or
  2279. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2280. expectloc:=LOC_REGISTER
  2281. else
  2282. if (left.expectloc=LOC_CREGISTER) and
  2283. (resultdef.size<left.resultdef.size) then
  2284. expectloc:=LOC_REGISTER;
  2285. end;
  2286. end;
  2287. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2288. begin
  2289. result:=nil;
  2290. expectloc:=LOC_REGISTER;
  2291. end;
  2292. function ttypeconvnode.first_cstring_to_int : tnode;
  2293. begin
  2294. result:=nil;
  2295. internalerror(200510014);
  2296. end;
  2297. function ttypeconvnode.first_string_to_chararray : tnode;
  2298. begin
  2299. first_string_to_chararray:=nil;
  2300. expectloc:=left.expectloc;
  2301. end;
  2302. function ttypeconvnode.first_char_to_string : tnode;
  2303. begin
  2304. first_char_to_string:=nil;
  2305. expectloc:=LOC_REFERENCE;
  2306. end;
  2307. function ttypeconvnode.first_nothing : tnode;
  2308. begin
  2309. first_nothing:=nil;
  2310. end;
  2311. function ttypeconvnode.first_array_to_pointer : tnode;
  2312. begin
  2313. first_array_to_pointer:=nil;
  2314. expectloc:=LOC_REGISTER;
  2315. end;
  2316. function ttypeconvnode.first_int_to_real: tnode;
  2317. var
  2318. fname: string[32];
  2319. begin
  2320. if target_info.system in systems_wince then
  2321. begin
  2322. { converting a 64bit integer to a float requires a helper }
  2323. if is_64bitint(left.resultdef) or
  2324. is_currency(left.resultdef) then
  2325. begin
  2326. { hack to avoid double division by 10000, as it's
  2327. already done by typecheckpass.resultdef_int_to_real }
  2328. if is_currency(left.resultdef) then
  2329. left.resultdef := s64inttype;
  2330. if is_signed(left.resultdef) then
  2331. fname:='I64TO'
  2332. else
  2333. fname:='UI64TO';
  2334. end
  2335. else
  2336. { other integers are supposed to be 32 bit }
  2337. begin
  2338. if is_signed(left.resultdef) then
  2339. fname:='ITO'
  2340. else
  2341. fname:='UTO';
  2342. firstpass(left);
  2343. end;
  2344. if tfloatdef(resultdef).floattype=s64real then
  2345. fname:=fname+'D'
  2346. else
  2347. fname:=fname+'S';
  2348. result:=ccallnode.createintern(fname,ccallparanode.create(
  2349. left,nil));
  2350. left:=nil;
  2351. firstpass(result);
  2352. exit;
  2353. end
  2354. else
  2355. begin
  2356. { converting a 64bit integer to a float requires a helper }
  2357. if is_64bitint(left.resultdef) or
  2358. is_currency(left.resultdef) then
  2359. begin
  2360. { hack to avoid double division by 10000, as it's
  2361. already done by typecheckpass.resultdef_int_to_real }
  2362. if is_currency(left.resultdef) then
  2363. left.resultdef := s64inttype;
  2364. if is_signed(left.resultdef) then
  2365. fname:='int64_to_'
  2366. else
  2367. { we can't do better currently }
  2368. fname:='qword_to_';
  2369. end
  2370. else
  2371. { other integers are supposed to be 32 bit }
  2372. begin
  2373. if is_signed(left.resultdef) then
  2374. fname:='int32_to_'
  2375. else
  2376. fname:='int64_to_';
  2377. firstpass(left);
  2378. end;
  2379. if tfloatdef(resultdef).floattype=s64real then
  2380. fname:=fname+'float64'
  2381. else
  2382. fname:=fname+'float32';
  2383. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2384. left,nil)),resultdef);
  2385. left:=nil;
  2386. firstpass(result);
  2387. exit;
  2388. end;
  2389. end;
  2390. function ttypeconvnode.first_real_to_real : tnode;
  2391. begin
  2392. {$ifdef cpufpemu}
  2393. if cs_fp_emulation in current_settings.moduleswitches then
  2394. begin
  2395. if target_info.system in systems_wince then
  2396. begin
  2397. case tfloatdef(left.resultdef).floattype of
  2398. s32real:
  2399. case tfloatdef(resultdef).floattype of
  2400. s64real:
  2401. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2402. s32real:
  2403. begin
  2404. result:=left;
  2405. left:=nil;
  2406. end;
  2407. else
  2408. internalerror(2005082704);
  2409. end;
  2410. s64real:
  2411. case tfloatdef(resultdef).floattype of
  2412. s32real:
  2413. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2414. s64real:
  2415. begin
  2416. result:=left;
  2417. left:=nil;
  2418. end;
  2419. else
  2420. internalerror(2005082703);
  2421. end;
  2422. else
  2423. internalerror(2005082702);
  2424. end;
  2425. left:=nil;
  2426. firstpass(result);
  2427. exit;
  2428. end
  2429. else
  2430. begin
  2431. case tfloatdef(left.resultdef).floattype of
  2432. s32real:
  2433. case tfloatdef(resultdef).floattype of
  2434. s64real:
  2435. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2436. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2437. s32real:
  2438. begin
  2439. result:=left;
  2440. left:=nil;
  2441. end;
  2442. else
  2443. internalerror(200610151);
  2444. end;
  2445. s64real:
  2446. case tfloatdef(resultdef).floattype of
  2447. s32real:
  2448. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2449. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2450. s64real:
  2451. begin
  2452. result:=left;
  2453. left:=nil;
  2454. end;
  2455. else
  2456. internalerror(200610152);
  2457. end;
  2458. else
  2459. internalerror(200610153);
  2460. end;
  2461. left:=nil;
  2462. firstpass(result);
  2463. exit;
  2464. end;
  2465. end
  2466. else
  2467. {$endif cpufpemu}
  2468. begin
  2469. first_real_to_real:=nil;
  2470. if not use_vectorfpu(resultdef) then
  2471. expectloc:=LOC_FPUREGISTER
  2472. else
  2473. expectloc:=LOC_MMREGISTER;
  2474. end;
  2475. end;
  2476. function ttypeconvnode.first_pointer_to_array : tnode;
  2477. begin
  2478. first_pointer_to_array:=nil;
  2479. expectloc:=LOC_REFERENCE;
  2480. end;
  2481. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2482. begin
  2483. first_cchar_to_pchar:=nil;
  2484. internalerror(200104021);
  2485. end;
  2486. function ttypeconvnode.first_bool_to_int : tnode;
  2487. begin
  2488. first_bool_to_int:=nil;
  2489. { byte(boolean) or word(wordbool) or longint(longbool) must
  2490. be accepted for var parameters }
  2491. if (nf_explicit in flags) and
  2492. (left.resultdef.size=resultdef.size) and
  2493. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2494. exit;
  2495. { when converting to 64bit, first convert to a 32bit int and then }
  2496. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2497. if resultdef.size > sizeof(aint) then
  2498. begin
  2499. result := ctypeconvnode.create_internal(left,s32inttype);
  2500. result := ctypeconvnode.create(result,resultdef);
  2501. left := nil;
  2502. firstpass(result);
  2503. exit;
  2504. end;
  2505. expectloc:=LOC_REGISTER;
  2506. end;
  2507. function ttypeconvnode.first_int_to_bool : tnode;
  2508. begin
  2509. first_int_to_bool:=nil;
  2510. { byte(boolean) or word(wordbool) or longint(longbool) must
  2511. be accepted for var parameters }
  2512. if (nf_explicit in flags) and
  2513. (left.resultdef.size=resultdef.size) and
  2514. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2515. exit;
  2516. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  2517. { convert to a boolean (only necessary for 32bit processors) }
  2518. if (left.resultdef.size > sizeof(aint)) and (left.resultdef.size<>resultdef.size)
  2519. and is_cbool(resultdef) then
  2520. begin
  2521. left:=ctypeconvnode.create_internal(left,s32inttype);
  2522. firstpass(left);
  2523. exit;
  2524. end;
  2525. expectloc:=LOC_REGISTER;
  2526. end;
  2527. function ttypeconvnode.first_bool_to_bool : tnode;
  2528. begin
  2529. first_bool_to_bool:=nil;
  2530. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2531. expectloc := left.expectloc
  2532. else
  2533. expectloc:=LOC_REGISTER;
  2534. end;
  2535. function ttypeconvnode.first_char_to_char : tnode;
  2536. var
  2537. fname: string[18];
  2538. begin
  2539. if (torddef(resultdef).ordtype=uchar) and
  2540. (torddef(left.resultdef).ordtype=uwidechar) then
  2541. fname := 'fpc_uchar_to_char'
  2542. else if (torddef(resultdef).ordtype=uwidechar) and
  2543. (torddef(left.resultdef).ordtype=uchar) then
  2544. fname := 'fpc_char_to_uchar'
  2545. else
  2546. internalerror(2007081201);
  2547. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2548. left:=nil;
  2549. firstpass(result);
  2550. end;
  2551. function ttypeconvnode.first_proc_to_procvar : tnode;
  2552. begin
  2553. first_proc_to_procvar:=nil;
  2554. { if we take the address of a nested function, the current function/
  2555. procedure needs a stack frame since it's required to construct
  2556. the nested procvar }
  2557. if is_nested_pd(tprocvardef(resultdef)) then
  2558. include(current_procinfo.flags,pi_needs_stackframe);
  2559. if tabstractprocdef(resultdef).is_addressonly then
  2560. expectloc:=LOC_REGISTER
  2561. else
  2562. begin
  2563. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2564. CGMessage(parser_e_illegal_expression);
  2565. expectloc:=left.expectloc;
  2566. end;
  2567. end;
  2568. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2569. begin
  2570. first_nil_to_methodprocvar:=nil;
  2571. expectloc:=LOC_REFERENCE;
  2572. end;
  2573. function ttypeconvnode.first_set_to_set : tnode;
  2574. var
  2575. newstatement : tstatementnode;
  2576. temp : ttempcreatenode;
  2577. begin
  2578. { in theory, we should do range checking here,
  2579. but Delphi doesn't do it either (FK) }
  2580. if left.nodetype=setconstn then
  2581. begin
  2582. left.resultdef:=resultdef;
  2583. result:=left;
  2584. left:=nil;
  2585. end
  2586. { equal sets for the code generator? }
  2587. else if (left.resultdef.size=resultdef.size) and
  2588. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2589. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2590. { e.g., this prints true because of this:
  2591. var
  2592. sa: set of 1..2;
  2593. sb: set of 5..6;
  2594. b: byte;
  2595. begin
  2596. b:=1;
  2597. sa:=[1..2];
  2598. sb:=sa;
  2599. writeln(b in sb);
  2600. end.
  2601. }
  2602. begin
  2603. result:=left;
  2604. left:=nil;
  2605. end
  2606. else
  2607. begin
  2608. result:=internalstatements(newstatement);
  2609. { in case left is a smallset expression, it can be an addn or so. }
  2610. { fpc_varset_load expects a formal const parameter, which doesn't }
  2611. { accept set addn's -> assign to a temp first and pass the temp }
  2612. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2613. begin
  2614. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2615. addstatement(newstatement,temp);
  2616. { temp := left }
  2617. addstatement(newstatement,cassignmentnode.create(
  2618. ctemprefnode.create(temp),left));
  2619. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2620. addstatement(newstatement,ctemprefnode.create(temp));
  2621. left:=result;
  2622. firstpass(left);
  2623. { recreate the result's internalstatements list }
  2624. result:=internalstatements(newstatement);
  2625. end;
  2626. { create temp for result }
  2627. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2628. addstatement(newstatement,temp);
  2629. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2630. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2631. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2632. ccallparanode.create(ctemprefnode.create(temp),
  2633. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2634. ccallparanode.create(left,nil))))))
  2635. );
  2636. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2637. addstatement(newstatement,ctemprefnode.create(temp));
  2638. left:=nil;
  2639. end;
  2640. end;
  2641. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2642. begin
  2643. first_ansistring_to_pchar:=nil;
  2644. expectloc:=LOC_REGISTER;
  2645. end;
  2646. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2647. begin
  2648. first_arrayconstructor_to_set:=nil;
  2649. internalerror(200104022);
  2650. end;
  2651. function ttypeconvnode.first_class_to_intf : tnode;
  2652. var
  2653. hd : tobjectdef;
  2654. ImplIntf : TImplementedInterface;
  2655. begin
  2656. result:=nil;
  2657. expectloc:=LOC_REGISTER;
  2658. hd:=tobjectdef(left.resultdef);
  2659. while assigned(hd) do
  2660. begin
  2661. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  2662. if assigned(ImplIntf) then
  2663. begin
  2664. case ImplIntf.IType of
  2665. etStandard:
  2666. { handle in pass 2 }
  2667. ;
  2668. etFieldValue, etFieldValueClass:
  2669. if is_interface(tobjectdef(resultdef)) then
  2670. begin
  2671. result:=left;
  2672. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  2673. { this ensures proper refcounting when field is of class type }
  2674. if not is_interface(result.resultdef) then
  2675. inserttypeconv(result, resultdef);
  2676. left:=nil;
  2677. end
  2678. else
  2679. begin
  2680. internalerror(200802213);
  2681. end;
  2682. etStaticMethodResult, etStaticMethodClass,
  2683. etVirtualMethodResult, etVirtualMethodClass:
  2684. if is_interface(tobjectdef(resultdef)) then
  2685. begin
  2686. { TODO: generating a call to TObject.GetInterface instead could yield
  2687. smaller code size. OTOH, refcounting gotchas are possible that way. }
  2688. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  2689. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  2690. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  2691. left,[]);
  2692. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  2693. { if it is a class, process it further in a similar way }
  2694. if not is_interface(result.resultdef) then
  2695. inserttypeconv(result, resultdef);
  2696. left:=nil;
  2697. end
  2698. else if is_class(tobjectdef(resultdef)) then
  2699. begin
  2700. internalerror(200802211);
  2701. end
  2702. else
  2703. internalerror(200802231);
  2704. else
  2705. internalerror(200802165);
  2706. end;
  2707. break;
  2708. end;
  2709. hd:=hd.childof;
  2710. end;
  2711. if hd=nil then
  2712. internalerror(200802164);
  2713. end;
  2714. function ttypeconvnode.first_string_to_string : tnode;
  2715. var
  2716. procname: string[31];
  2717. newblock : tblocknode;
  2718. newstat : tstatementnode;
  2719. restemp : ttempcreatenode;
  2720. begin
  2721. { get the correct procedure name }
  2722. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  2723. '_to_'+tstringdef(resultdef).stringtypname;
  2724. if tstringdef(resultdef).stringtype=st_shortstring then
  2725. begin
  2726. newblock:=internalstatements(newstat);
  2727. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  2728. addstatement(newstat,restemp);
  2729. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  2730. ctemprefnode.create(restemp),nil))));
  2731. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  2732. addstatement(newstat,ctemprefnode.create(restemp));
  2733. result:=newblock;
  2734. end
  2735. { encoding parameter required? }
  2736. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  2737. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  2738. result:=ccallnode.createinternres(procname,
  2739. ccallparanode.create(cordconstnode.create(tstringdef(resultdef).encoding,u16inttype,true),
  2740. ccallparanode.create(left,nil)),resultdef)
  2741. else
  2742. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  2743. left:=nil;
  2744. end;
  2745. function ttypeconvnode._first_int_to_int : tnode;
  2746. begin
  2747. result:=first_int_to_int;
  2748. end;
  2749. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2750. begin
  2751. result:=first_cstring_to_pchar;
  2752. end;
  2753. function ttypeconvnode._first_cstring_to_int : tnode;
  2754. begin
  2755. result:=first_cstring_to_int;
  2756. end;
  2757. function ttypeconvnode._first_string_to_chararray : tnode;
  2758. begin
  2759. result:=first_string_to_chararray;
  2760. end;
  2761. function ttypeconvnode._first_char_to_string : tnode;
  2762. begin
  2763. result:=first_char_to_string;
  2764. end;
  2765. function ttypeconvnode._first_nothing : tnode;
  2766. begin
  2767. result:=first_nothing;
  2768. end;
  2769. function ttypeconvnode._first_array_to_pointer : tnode;
  2770. begin
  2771. result:=first_array_to_pointer;
  2772. end;
  2773. function ttypeconvnode._first_int_to_real : tnode;
  2774. begin
  2775. result:=first_int_to_real;
  2776. end;
  2777. function ttypeconvnode._first_real_to_real : tnode;
  2778. begin
  2779. result:=first_real_to_real;
  2780. end;
  2781. function ttypeconvnode._first_pointer_to_array : tnode;
  2782. begin
  2783. result:=first_pointer_to_array;
  2784. end;
  2785. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2786. begin
  2787. result:=first_cchar_to_pchar;
  2788. end;
  2789. function ttypeconvnode._first_bool_to_int : tnode;
  2790. begin
  2791. result:=first_bool_to_int;
  2792. end;
  2793. function ttypeconvnode._first_int_to_bool : tnode;
  2794. begin
  2795. result:=first_int_to_bool;
  2796. end;
  2797. function ttypeconvnode._first_bool_to_bool : tnode;
  2798. begin
  2799. result:=first_bool_to_bool;
  2800. end;
  2801. function ttypeconvnode._first_proc_to_procvar : tnode;
  2802. begin
  2803. result:=first_proc_to_procvar;
  2804. end;
  2805. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2806. begin
  2807. result:=first_nil_to_methodprocvar;
  2808. end;
  2809. function ttypeconvnode._first_set_to_set : tnode;
  2810. begin
  2811. result:=first_set_to_set;
  2812. end;
  2813. function ttypeconvnode._first_cord_to_pointer : tnode;
  2814. begin
  2815. result:=first_cord_to_pointer;
  2816. end;
  2817. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2818. begin
  2819. result:=first_ansistring_to_pchar;
  2820. end;
  2821. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2822. begin
  2823. result:=first_arrayconstructor_to_set;
  2824. end;
  2825. function ttypeconvnode._first_class_to_intf : tnode;
  2826. begin
  2827. result:=first_class_to_intf;
  2828. end;
  2829. function ttypeconvnode._first_char_to_char : tnode;
  2830. begin
  2831. result:=first_char_to_char;
  2832. end;
  2833. function ttypeconvnode._first_string_to_string : tnode;
  2834. begin
  2835. result:=first_string_to_string;
  2836. end;
  2837. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2838. const
  2839. firstconvert : array[tconverttype] of pointer = (
  2840. nil, { none }
  2841. @ttypeconvnode._first_nothing, {equal}
  2842. @ttypeconvnode._first_nothing, {not_possible}
  2843. @ttypeconvnode._first_string_to_string,
  2844. @ttypeconvnode._first_char_to_string,
  2845. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2846. nil, { removed in typecheck_chararray_to_string }
  2847. @ttypeconvnode._first_cchar_to_pchar,
  2848. @ttypeconvnode._first_cstring_to_pchar,
  2849. @ttypeconvnode._first_cstring_to_int,
  2850. @ttypeconvnode._first_ansistring_to_pchar,
  2851. @ttypeconvnode._first_string_to_chararray,
  2852. nil, { removed in typecheck_chararray_to_string }
  2853. @ttypeconvnode._first_array_to_pointer,
  2854. @ttypeconvnode._first_pointer_to_array,
  2855. @ttypeconvnode._first_int_to_int,
  2856. @ttypeconvnode._first_int_to_bool,
  2857. @ttypeconvnode._first_bool_to_bool,
  2858. @ttypeconvnode._first_bool_to_int,
  2859. @ttypeconvnode._first_real_to_real,
  2860. @ttypeconvnode._first_int_to_real,
  2861. nil, { removed in typecheck_real_to_currency }
  2862. @ttypeconvnode._first_proc_to_procvar,
  2863. @ttypeconvnode._first_nil_to_methodprocvar,
  2864. @ttypeconvnode._first_arrayconstructor_to_set,
  2865. @ttypeconvnode._first_set_to_set,
  2866. @ttypeconvnode._first_cord_to_pointer,
  2867. @ttypeconvnode._first_nothing,
  2868. @ttypeconvnode._first_nothing,
  2869. @ttypeconvnode._first_class_to_intf,
  2870. @ttypeconvnode._first_char_to_char,
  2871. @ttypeconvnode._first_nothing,
  2872. @ttypeconvnode._first_nothing,
  2873. nil,
  2874. nil,
  2875. nil,
  2876. nil,
  2877. nil,
  2878. nil,
  2879. nil
  2880. );
  2881. type
  2882. tprocedureofobject = function : tnode of object;
  2883. var
  2884. r : packed record
  2885. proc : pointer;
  2886. obj : pointer;
  2887. end;
  2888. begin
  2889. { this is a little bit dirty but it works }
  2890. { and should be quite portable too }
  2891. r.proc:=firstconvert[c];
  2892. r.obj:=self;
  2893. if not assigned(r.proc) then
  2894. internalerror(200312081);
  2895. first_call_helper:=tprocedureofobject(r)()
  2896. end;
  2897. function ttypeconvnode.pass_1 : tnode;
  2898. begin
  2899. if warn_pointer_to_signed then
  2900. cgmessage(type_w_pointer_to_signed);
  2901. result:=nil;
  2902. firstpass(left);
  2903. if codegenerror then
  2904. exit;
  2905. expectloc:=left.expectloc;
  2906. result:=first_call_helper(convtype);
  2907. end;
  2908. function ttypeconvnode.retains_value_location:boolean;
  2909. begin
  2910. result:=(convtype=tc_equal) or
  2911. { typecasting from void is always allowed }
  2912. is_void(left.resultdef) or
  2913. (left.resultdef.typ=formaldef) or
  2914. { int 2 int with same size reuses same location, or for
  2915. tp7 mode also allow size < orignal size }
  2916. (
  2917. (convtype=tc_int_2_int) and
  2918. (
  2919. not is_bitpacked_access(left) and
  2920. (resultdef.size=left.resultdef.size) or
  2921. ((m_tp7 in current_settings.modeswitches) and
  2922. (resultdef.size<left.resultdef.size))
  2923. )
  2924. ) or
  2925. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2926. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  2927. (nf_explicit in flags) and
  2928. (resultdef.size=left.resultdef.size));
  2929. end;
  2930. function ttypeconvnode.assign_allowed:boolean;
  2931. begin
  2932. result:=retains_value_location;
  2933. { When using only a part of the value it can't be in a register since
  2934. that will load the value in a new register first }
  2935. { the same goes for changing the sign of equal-sized values which
  2936. are smaller than an entire register }
  2937. if result and
  2938. (resultdef.size<left.resultdef.size) or
  2939. ((resultdef.size=left.resultdef.size) and
  2940. (left.resultdef.size<sizeof(aint)) and
  2941. (is_signed(resultdef) xor is_signed(left.resultdef))) then
  2942. make_not_regable(left,[ra_addr_regable]);
  2943. end;
  2944. function ttypeconvnode.docompare(p: tnode) : boolean;
  2945. begin
  2946. docompare :=
  2947. inherited docompare(p) and
  2948. (convtype = ttypeconvnode(p).convtype) and
  2949. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  2950. end;
  2951. procedure ttypeconvnode._second_int_to_int;
  2952. begin
  2953. second_int_to_int;
  2954. end;
  2955. procedure ttypeconvnode._second_string_to_string;
  2956. begin
  2957. second_string_to_string;
  2958. end;
  2959. procedure ttypeconvnode._second_cstring_to_pchar;
  2960. begin
  2961. second_cstring_to_pchar;
  2962. end;
  2963. procedure ttypeconvnode._second_cstring_to_int;
  2964. begin
  2965. second_cstring_to_int;
  2966. end;
  2967. procedure ttypeconvnode._second_string_to_chararray;
  2968. begin
  2969. second_string_to_chararray;
  2970. end;
  2971. procedure ttypeconvnode._second_array_to_pointer;
  2972. begin
  2973. second_array_to_pointer;
  2974. end;
  2975. procedure ttypeconvnode._second_pointer_to_array;
  2976. begin
  2977. second_pointer_to_array;
  2978. end;
  2979. procedure ttypeconvnode._second_chararray_to_string;
  2980. begin
  2981. second_chararray_to_string;
  2982. end;
  2983. procedure ttypeconvnode._second_char_to_string;
  2984. begin
  2985. second_char_to_string;
  2986. end;
  2987. procedure ttypeconvnode._second_int_to_real;
  2988. begin
  2989. second_int_to_real;
  2990. end;
  2991. procedure ttypeconvnode._second_real_to_real;
  2992. begin
  2993. second_real_to_real;
  2994. end;
  2995. procedure ttypeconvnode._second_cord_to_pointer;
  2996. begin
  2997. second_cord_to_pointer;
  2998. end;
  2999. procedure ttypeconvnode._second_proc_to_procvar;
  3000. begin
  3001. second_proc_to_procvar;
  3002. end;
  3003. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3004. begin
  3005. second_nil_to_methodprocvar;
  3006. end;
  3007. procedure ttypeconvnode._second_bool_to_int;
  3008. begin
  3009. second_bool_to_int;
  3010. end;
  3011. procedure ttypeconvnode._second_int_to_bool;
  3012. begin
  3013. second_int_to_bool;
  3014. end;
  3015. procedure ttypeconvnode._second_bool_to_bool;
  3016. begin
  3017. second_bool_to_bool;
  3018. end;
  3019. procedure ttypeconvnode._second_set_to_set;
  3020. begin
  3021. second_set_to_set;
  3022. end;
  3023. procedure ttypeconvnode._second_ansistring_to_pchar;
  3024. begin
  3025. second_ansistring_to_pchar;
  3026. end;
  3027. procedure ttypeconvnode._second_class_to_intf;
  3028. begin
  3029. second_class_to_intf;
  3030. end;
  3031. procedure ttypeconvnode._second_char_to_char;
  3032. begin
  3033. second_char_to_char;
  3034. end;
  3035. procedure ttypeconvnode._second_nothing;
  3036. begin
  3037. second_nothing;
  3038. end;
  3039. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3040. const
  3041. secondconvert : array[tconverttype] of pointer = (
  3042. @ttypeconvnode._second_nothing, {none}
  3043. @ttypeconvnode._second_nothing, {equal}
  3044. @ttypeconvnode._second_nothing, {not_possible}
  3045. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3046. @ttypeconvnode._second_char_to_string,
  3047. @ttypeconvnode._second_nothing, {char_to_charray}
  3048. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3049. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3050. @ttypeconvnode._second_cstring_to_pchar,
  3051. @ttypeconvnode._second_cstring_to_int,
  3052. @ttypeconvnode._second_ansistring_to_pchar,
  3053. @ttypeconvnode._second_string_to_chararray,
  3054. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3055. @ttypeconvnode._second_array_to_pointer,
  3056. @ttypeconvnode._second_pointer_to_array,
  3057. @ttypeconvnode._second_int_to_int,
  3058. @ttypeconvnode._second_int_to_bool,
  3059. @ttypeconvnode._second_bool_to_bool,
  3060. @ttypeconvnode._second_bool_to_int,
  3061. @ttypeconvnode._second_real_to_real,
  3062. @ttypeconvnode._second_int_to_real,
  3063. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3064. @ttypeconvnode._second_proc_to_procvar,
  3065. @ttypeconvnode._second_nil_to_methodprocvar,
  3066. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3067. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3068. @ttypeconvnode._second_cord_to_pointer,
  3069. @ttypeconvnode._second_nothing, { interface 2 string }
  3070. @ttypeconvnode._second_nothing, { interface 2 guid }
  3071. @ttypeconvnode._second_class_to_intf,
  3072. @ttypeconvnode._second_char_to_char,
  3073. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3074. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3075. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3076. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3077. @ttypeconvnode._second_nothing, { variant_2_enum }
  3078. @ttypeconvnode._second_nothing, { enum_2_variant }
  3079. @ttypeconvnode._second_nothing, { variant_2_interface }
  3080. @ttypeconvnode._second_nothing, { interface_2_variant }
  3081. @ttypeconvnode._second_nothing { array_2_dynarray }
  3082. );
  3083. type
  3084. tprocedureofobject = procedure of object;
  3085. var
  3086. r : packed record
  3087. proc : pointer;
  3088. obj : pointer;
  3089. end;
  3090. begin
  3091. { this is a little bit dirty but it works }
  3092. { and should be quite portable too }
  3093. r.proc:=secondconvert[c];
  3094. r.obj:=self;
  3095. tprocedureofobject(r)();
  3096. end;
  3097. {*****************************************************************************
  3098. TASNODE
  3099. *****************************************************************************}
  3100. function tasisnode.pass_typecheck: tnode;
  3101. var
  3102. hp : tnode;
  3103. begin
  3104. result:=nil;
  3105. typecheckpass(right);
  3106. typecheckpass(left);
  3107. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3108. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3109. if codegenerror then
  3110. exit;
  3111. if (right.resultdef.typ=classrefdef) then
  3112. begin
  3113. { left maybe an interface reference }
  3114. if is_interfacecom(left.resultdef) then
  3115. begin
  3116. { relation checks are not possible }
  3117. end
  3118. { or left must be a class }
  3119. else if is_class(left.resultdef) then
  3120. begin
  3121. { the operands must be related }
  3122. if (not(tobjectdef(left.resultdef).is_related(
  3123. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3124. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  3125. tobjectdef(left.resultdef)))) then
  3126. CGMessage2(type_e_classes_not_related,
  3127. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3128. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3129. end
  3130. else
  3131. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3132. case nodetype of
  3133. isn:
  3134. resultdef:=pasbool8type;
  3135. asn:
  3136. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3137. end;
  3138. end
  3139. else if is_interface(right.resultdef) or is_dispinterface(right.resultdef) then
  3140. begin
  3141. { left is a class }
  3142. if not(is_class(left.resultdef) or
  3143. is_interfacecom(left.resultdef)) then
  3144. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3145. case nodetype of
  3146. isn:
  3147. resultdef:=pasbool8type;
  3148. asn:
  3149. resultdef:=right.resultdef;
  3150. end;
  3151. { load the GUID of the interface }
  3152. if (right.nodetype=typen) then
  3153. begin
  3154. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3155. begin
  3156. if assigned(tobjectdef(right.resultdef).iidstr) then
  3157. begin
  3158. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3159. tstringconstnode(hp).changestringtype(cshortstringtype);
  3160. right.free;
  3161. right:=hp;
  3162. end
  3163. else
  3164. internalerror(201006131);
  3165. end
  3166. else
  3167. begin
  3168. if assigned(tobjectdef(right.resultdef).iidguid) then
  3169. begin
  3170. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3171. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3172. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3173. right.free;
  3174. right:=hp;
  3175. end
  3176. else
  3177. internalerror(201006132);
  3178. end;
  3179. typecheckpass(right);
  3180. end;
  3181. end
  3182. else
  3183. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3184. end;
  3185. {*****************************************************************************
  3186. TISNODE
  3187. *****************************************************************************}
  3188. constructor tisnode.create(l,r : tnode);
  3189. begin
  3190. inherited create(isn,l,r);
  3191. end;
  3192. function tisnode.pass_1 : tnode;
  3193. var
  3194. procname: string;
  3195. begin
  3196. result:=nil;
  3197. { Passing a class type to an "is" expression cannot result in a class
  3198. of that type to be constructed.
  3199. }
  3200. include(right.flags,nf_ignore_for_wpo);
  3201. if is_class(left.resultdef) and
  3202. (right.resultdef.typ=classrefdef) then
  3203. result := ccallnode.createinternres('fpc_do_is',
  3204. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3205. resultdef)
  3206. else
  3207. begin
  3208. if is_class(left.resultdef) then
  3209. if is_shortstring(right.resultdef) then
  3210. procname := 'fpc_class_is_corbaintf'
  3211. else
  3212. procname := 'fpc_class_is_intf'
  3213. else
  3214. if right.resultdef.typ=classrefdef then
  3215. procname := 'fpc_intf_is_class'
  3216. else
  3217. procname := 'fpc_intf_is';
  3218. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3219. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3220. end;
  3221. left := nil;
  3222. right := nil;
  3223. //firstpass(call);
  3224. if codegenerror then
  3225. exit;
  3226. end;
  3227. { dummy pass_2, it will never be called, but we need one since }
  3228. { you can't instantiate an abstract class }
  3229. procedure tisnode.pass_generate_code;
  3230. begin
  3231. end;
  3232. {*****************************************************************************
  3233. TASNODE
  3234. *****************************************************************************}
  3235. constructor tasnode.create(l,r : tnode);
  3236. begin
  3237. inherited create(asn,l,r);
  3238. call := nil;
  3239. end;
  3240. destructor tasnode.destroy;
  3241. begin
  3242. call.free;
  3243. inherited destroy;
  3244. end;
  3245. function tasnode.dogetcopy: tnode;
  3246. begin
  3247. result := inherited dogetcopy;
  3248. if assigned(call) then
  3249. tasnode(result).call := call.getcopy
  3250. else
  3251. tasnode(result).call := nil;
  3252. end;
  3253. function tasnode.docompare(p: tnode): boolean;
  3254. begin
  3255. result:=
  3256. inherited docompare(p) and
  3257. tasnode(p).call.isequal(call);
  3258. end;
  3259. function tasnode.pass_1 : tnode;
  3260. var
  3261. procname: string;
  3262. begin
  3263. result:=nil;
  3264. { Passing a class type to an "as" expression cannot result in a class
  3265. of that type to be constructed.
  3266. }
  3267. include(right.flags,nf_ignore_for_wpo);
  3268. if not assigned(call) then
  3269. begin
  3270. if is_class(left.resultdef) and
  3271. (right.resultdef.typ=classrefdef) then
  3272. call := ccallnode.createinternres('fpc_do_as',
  3273. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3274. resultdef)
  3275. else
  3276. begin
  3277. if is_class(left.resultdef) then
  3278. if is_shortstring(right.resultdef) then
  3279. procname := 'fpc_class_as_corbaintf'
  3280. else
  3281. procname := 'fpc_class_as_intf'
  3282. else
  3283. if right.resultdef.typ=classrefdef then
  3284. procname := 'fpc_intf_as_class'
  3285. else
  3286. procname := 'fpc_intf_as';
  3287. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3288. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3289. end;
  3290. left := nil;
  3291. right := nil;
  3292. firstpass(call);
  3293. if codegenerror then
  3294. exit;
  3295. expectloc:=call.expectloc;
  3296. end;
  3297. end;
  3298. end.