ncnv.pas 139 KB

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