ncnv.pas 173 KB

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