ncnv.pas 168 KB

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