ncnv.pas 152 KB

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