ncnv.pas 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488
  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),pasbool1type,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. pasbool1type,
  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),pasbool1type,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
  1513. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1514. end;
  1515. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1516. var
  1517. hp : tnode;
  1518. begin
  1519. result:=nil;
  1520. if left.nodetype<>arrayconstructorn then
  1521. internalerror(5546);
  1522. { remove typeconv node }
  1523. hp:=left;
  1524. left:=nil;
  1525. { create a set constructor tree }
  1526. arrayconstructor_to_set(hp);
  1527. result:=hp;
  1528. end;
  1529. function ttypeconvnode.typecheck_set_to_set : tnode;
  1530. begin
  1531. result:=nil;
  1532. { constant sets can be converted by changing the type only }
  1533. if (left.nodetype=setconstn) then
  1534. begin
  1535. left.resultdef:=resultdef;
  1536. result:=left;
  1537. left:=nil;
  1538. exit;
  1539. end;
  1540. end;
  1541. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1542. var
  1543. newblock : tblocknode;
  1544. newstat : tstatementnode;
  1545. restemp : ttempcreatenode;
  1546. begin
  1547. if tstringdef(resultdef).stringtype=st_shortstring then
  1548. begin
  1549. newblock:=internalstatements(newstat);
  1550. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1551. addstatement(newstat,restemp);
  1552. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1553. ctemprefnode.create(restemp),nil))));
  1554. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1555. addstatement(newstat,ctemprefnode.create(restemp));
  1556. result:=newblock;
  1557. end
  1558. else if tstringdef(resultdef).stringtype=st_ansistring then
  1559. result := ccallnode.createinternres(
  1560. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1561. ccallparanode.create(
  1562. cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  1563. ccallparanode.create(left,nil)
  1564. ),
  1565. resultdef
  1566. )
  1567. else
  1568. result := ccallnode.createinternres(
  1569. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1570. ccallparanode.create(left,nil),resultdef);
  1571. left:=nil;
  1572. end;
  1573. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1574. begin
  1575. if assigned(tobjectdef(left.resultdef).iidstr) then
  1576. begin
  1577. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1578. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1579. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1580. tstringconstnode(result).changestringtype(cshortstringtype);
  1581. end
  1582. else
  1583. internalerror(2013112913);
  1584. end;
  1585. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1586. begin
  1587. if assigned(tobjectdef(left.resultdef).iidguid) then
  1588. begin
  1589. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1590. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1591. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1592. end
  1593. else
  1594. internalerror(2013112914);
  1595. end;
  1596. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1597. begin
  1598. if (actualtargetnode(@left)^.nodetype in [pointerconstn,niln]) then
  1599. CGMessage(type_e_no_addr_of_constant);
  1600. { a dynamic array is a pointer to an array, so to convert it to }
  1601. { an open array, we have to dereference it (JM) }
  1602. result := ctypeconvnode.create_internal(left,cpointerdef.getreusable(resultdef));
  1603. typecheckpass(result);
  1604. { left is reused }
  1605. left := nil;
  1606. result := cderefnode.create(result);
  1607. include(result.flags,nf_no_checkpointer);
  1608. end;
  1609. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1610. var
  1611. newblock : tblocknode;
  1612. newstat : tstatementnode;
  1613. restemp : ttempcreatenode;
  1614. begin
  1615. if tstringdef(resultdef).stringtype=st_shortstring then
  1616. begin
  1617. newblock:=internalstatements(newstat);
  1618. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1619. addstatement(newstat,restemp);
  1620. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1621. ctemprefnode.create(restemp),nil))));
  1622. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1623. addstatement(newstat,ctemprefnode.create(restemp));
  1624. result:=newblock;
  1625. end
  1626. else if tstringdef(resultdef).stringtype=st_ansistring then
  1627. begin
  1628. result:=ccallnode.createinternres(
  1629. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1630. ccallparanode.create(
  1631. cordconstnode.create(
  1632. getparaencoding(resultdef),
  1633. u16inttype,
  1634. true
  1635. ),
  1636. ccallparanode.create(left,nil)
  1637. ),
  1638. resultdef
  1639. );
  1640. end
  1641. else
  1642. result := ccallnode.createinternres(
  1643. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1644. ccallparanode.create(left,nil),resultdef);
  1645. left:=nil;
  1646. end;
  1647. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1648. begin
  1649. result := ccallnode.createinternres(
  1650. 'fpc_variant_to_dynarray',
  1651. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1652. ccallparanode.create(left,nil)
  1653. ),resultdef);
  1654. typecheckpass(result);
  1655. left:=nil;
  1656. end;
  1657. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1658. begin
  1659. result := ccallnode.createinternres(
  1660. 'fpc_dynarray_to_variant',
  1661. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1662. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1663. ),resultdef);
  1664. typecheckpass(result);
  1665. left:=nil;
  1666. end;
  1667. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1668. begin
  1669. if def_is_related(tobjectdef(resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1670. result := ccallnode.createinternres(
  1671. 'fpc_variant_to_idispatch',
  1672. ccallparanode.create(left,nil)
  1673. ,resultdef)
  1674. else
  1675. result := ccallnode.createinternres(
  1676. 'fpc_variant_to_interface',
  1677. ccallparanode.create(left,nil)
  1678. ,resultdef);
  1679. typecheckpass(result);
  1680. left:=nil;
  1681. end;
  1682. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1683. begin
  1684. if def_is_related(tobjectdef(left.resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1685. result := ccallnode.createinternres(
  1686. 'fpc_idispatch_to_variant',
  1687. ccallparanode.create(left,nil)
  1688. ,resultdef)
  1689. else
  1690. result := ccallnode.createinternres(
  1691. 'fpc_interface_to_variant',
  1692. ccallparanode.create(left,nil)
  1693. ,resultdef);
  1694. typecheckpass(result);
  1695. left:=nil;
  1696. end;
  1697. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1698. begin
  1699. result := ctypeconvnode.create_internal(left,sinttype);
  1700. result := ctypeconvnode.create_internal(result,resultdef);
  1701. typecheckpass(result);
  1702. { left is reused }
  1703. left := nil;
  1704. end;
  1705. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1706. begin
  1707. result := ctypeconvnode.create_internal(left,sinttype);
  1708. result := ctypeconvnode.create_internal(result,cvarianttype);
  1709. typecheckpass(result);
  1710. { left is reused }
  1711. left := nil;
  1712. end;
  1713. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1714. var
  1715. newstatement : tstatementnode;
  1716. temp : ttempcreatenode;
  1717. temp2 : ttempcreatenode;
  1718. begin
  1719. { create statements with call to getmem+initialize }
  1720. result:=internalstatements(newstatement);
  1721. { create temp for result }
  1722. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1723. addstatement(newstatement,temp);
  1724. { get temp for array of lengths }
  1725. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1726. addstatement(newstatement,temp2);
  1727. { one dimensional }
  1728. addstatement(newstatement,cassignmentnode.create(
  1729. ctemprefnode.create(temp2),
  1730. cordconstnode.create
  1731. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1732. { create call to fpc_dynarr_setlength }
  1733. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1734. ccallparanode.create(caddrnode.create_internal
  1735. (ctemprefnode.create(temp2)),
  1736. ccallparanode.create(cordconstnode.create
  1737. (1,s32inttype,true),
  1738. ccallparanode.create(caddrnode.create_internal
  1739. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1740. ccallparanode.create(
  1741. ctypeconvnode.create_internal(
  1742. ctemprefnode.create(temp),voidpointertype),
  1743. nil))))
  1744. ));
  1745. addstatement(newstatement,ctempdeletenode.create(temp2));
  1746. { copy ... }
  1747. addstatement(newstatement,cassignmentnode.create(
  1748. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1749. left
  1750. ));
  1751. { left is reused }
  1752. left:=nil;
  1753. { the last statement should return the value as
  1754. location and type, this is done be referencing the
  1755. temp and converting it first from a persistent temp to
  1756. normal temp }
  1757. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1758. addstatement(newstatement,ctemprefnode.create(temp));
  1759. end;
  1760. function ttypeconvnode.typecheck_elem_2_openarray : tnode;
  1761. begin
  1762. result:=nil;
  1763. end;
  1764. function ttypeconvnode.typecheck_arrayconstructor_to_dynarray : tnode;
  1765. var
  1766. newstatement,assstatement:tstatementnode;
  1767. arrnode:ttempcreatenode;
  1768. temp2:ttempcreatenode;
  1769. assnode:tnode;
  1770. paracount:integer;
  1771. elemnode:tarrayconstructornode;
  1772. begin
  1773. { assignment of []? }
  1774. if (left.nodetype=arrayconstructorn) and not assigned(tarrayconstructornode(left).left) then
  1775. begin
  1776. result:=cnilnode.create;
  1777. exit;
  1778. end;
  1779. if resultdef.typ<>arraydef then
  1780. internalerror(2017050102);
  1781. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1782. result:=internalstatements(newstatement);
  1783. { create temp for result }
  1784. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1785. addstatement(newstatement,arrnode);
  1786. paracount:=0;
  1787. { create an assignment call for each element }
  1788. assnode:=internalstatements(assstatement);
  1789. if left.nodetype=arrayconstructorrangen then
  1790. internalerror(2016021902);
  1791. elemnode:=tarrayconstructornode(left);
  1792. while assigned(elemnode) do
  1793. begin
  1794. { arr[i] := param_i }
  1795. if not assigned(elemnode.left) then
  1796. internalerror(2017050101);
  1797. addstatement(assstatement,
  1798. cassignmentnode.create(
  1799. cvecnode.create(
  1800. ctemprefnode.create(arrnode),
  1801. cordconstnode.create(paracount,tarraydef(totypedef).rangedef,false)),
  1802. elemnode.left));
  1803. elemnode.left:=nil;
  1804. inc(paracount);
  1805. elemnode:=tarrayconstructornode(elemnode.right);
  1806. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1807. internalerror(2016021903);
  1808. end;
  1809. { get temp for array of lengths }
  1810. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1811. addstatement(newstatement,temp2);
  1812. { create call to fpc_dynarr_setlength }
  1813. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1814. ccallparanode.create(caddrnode.create_internal
  1815. (ctemprefnode.create(temp2)),
  1816. ccallparanode.create(cordconstnode.create
  1817. (1,s32inttype,true),
  1818. ccallparanode.create(caddrnode.create_internal
  1819. (crttinode.create(tstoreddef(totypedef),initrtti,rdt_normal)),
  1820. ccallparanode.create(
  1821. ctypeconvnode.create_internal(
  1822. ctemprefnode.create(arrnode),voidpointertype),
  1823. nil))))
  1824. ));
  1825. { add assignment statememnts }
  1826. addstatement(newstatement,ctempdeletenode.create(temp2));
  1827. addstatement(newstatement,assnode);
  1828. { the last statement should return the value as
  1829. location and type, this is done be referencing the
  1830. temp and converting it first from a persistent temp to
  1831. normal temp }
  1832. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1833. addstatement(newstatement,ctemprefnode.create(arrnode));
  1834. end;
  1835. function ttypeconvnode._typecheck_int_to_int : tnode;
  1836. begin
  1837. result := typecheck_int_to_int;
  1838. end;
  1839. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  1840. begin
  1841. result := typecheck_cord_to_pointer;
  1842. end;
  1843. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  1844. begin
  1845. result := typecheck_chararray_to_string;
  1846. end;
  1847. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  1848. begin
  1849. result := typecheck_string_to_chararray;
  1850. end;
  1851. function ttypeconvnode._typecheck_string_to_string: tnode;
  1852. begin
  1853. result := typecheck_string_to_string;
  1854. end;
  1855. function ttypeconvnode._typecheck_char_to_string : tnode;
  1856. begin
  1857. result := typecheck_char_to_string;
  1858. end;
  1859. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  1860. begin
  1861. result := typecheck_char_to_chararray;
  1862. end;
  1863. function ttypeconvnode._typecheck_int_to_real : tnode;
  1864. begin
  1865. result := typecheck_int_to_real;
  1866. end;
  1867. function ttypeconvnode._typecheck_real_to_real : tnode;
  1868. begin
  1869. result := typecheck_real_to_real;
  1870. end;
  1871. function ttypeconvnode._typecheck_real_to_currency : tnode;
  1872. begin
  1873. result := typecheck_real_to_currency;
  1874. end;
  1875. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  1876. begin
  1877. result := typecheck_cchar_to_pchar;
  1878. end;
  1879. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  1880. begin
  1881. result := typecheck_cstring_to_pchar;
  1882. end;
  1883. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  1884. begin
  1885. result := typecheck_cstring_to_int;
  1886. end;
  1887. function ttypeconvnode._typecheck_char_to_char : tnode;
  1888. begin
  1889. result := typecheck_char_to_char;
  1890. end;
  1891. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  1892. begin
  1893. result := typecheck_arrayconstructor_to_set;
  1894. end;
  1895. function ttypeconvnode._typecheck_set_to_set : tnode;
  1896. begin
  1897. result := typecheck_set_to_set;
  1898. end;
  1899. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  1900. begin
  1901. result := typecheck_pchar_to_string;
  1902. end;
  1903. function ttypeconvnode._typecheck_interface_to_string : tnode;
  1904. begin
  1905. result := typecheck_interface_to_string;
  1906. end;
  1907. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  1908. begin
  1909. result := typecheck_interface_to_guid;
  1910. end;
  1911. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  1912. begin
  1913. result := typecheck_dynarray_to_openarray;
  1914. end;
  1915. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  1916. begin
  1917. result := typecheck_pwchar_to_string;
  1918. end;
  1919. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  1920. begin
  1921. result := typecheck_variant_to_dynarray;
  1922. end;
  1923. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  1924. begin
  1925. result := typecheck_dynarray_to_variant;
  1926. end;
  1927. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  1928. begin
  1929. result := typecheck_variant_to_enum;
  1930. end;
  1931. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  1932. begin
  1933. result := typecheck_enum_to_variant;
  1934. end;
  1935. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  1936. begin
  1937. result := typecheck_proc_to_procvar;
  1938. end;
  1939. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  1940. begin
  1941. result := typecheck_variant_to_interface;
  1942. end;
  1943. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  1944. begin
  1945. result := typecheck_interface_to_variant;
  1946. end;
  1947. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  1948. begin
  1949. result := typecheck_array_2_dynarray;
  1950. end;
  1951. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  1952. begin
  1953. result := typecheck_elem_2_openarray;
  1954. end;
  1955. function ttypeconvnode._typecheck_arrayconstructor_to_dynarray : tnode;
  1956. begin
  1957. result:=typecheck_arrayconstructor_to_dynarray;
  1958. end;
  1959. function ttypeconvnode.target_specific_general_typeconv: boolean;
  1960. begin
  1961. result:=false;
  1962. end;
  1963. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  1964. begin
  1965. result:=false;
  1966. end;
  1967. class function ttypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  1968. begin
  1969. result:=false;
  1970. end;
  1971. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1972. var
  1973. pd : tabstractprocdef;
  1974. copytype : tproccopytyp;
  1975. source: pnode;
  1976. begin
  1977. result:=nil;
  1978. pd:=tabstractprocdef(left.resultdef);
  1979. { create procvardef (default for create_proc_to_procvar is voiddef,
  1980. but if later a regular inserttypeconvnode() is used to insert a type
  1981. conversion to the actual procvardef, totypedef will be set to the
  1982. real procvartype that we are converting to) }
  1983. if assigned(totypedef) and
  1984. (totypedef.typ=procvardef) then
  1985. begin
  1986. { have to do this in typecheckpass so that it's triggered for
  1987. typed constant declarations }
  1988. if po_is_block in tprocvardef(totypedef).procoptions then
  1989. begin
  1990. { can only convert from procdef to procvardef, but in the mean
  1991. time other type conversions may have been inserted (pointers,
  1992. proc2procvar, ...) }
  1993. source:=actualtargetnode(@left);
  1994. while (source^.nodetype=typeconvn) and
  1995. (ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
  1996. (is_void(source^.resultdef) or
  1997. (source^.resultdef.typ=procvardef)) do
  1998. begin
  1999. { won't skip proc2procvar }
  2000. source:=actualtargetnode(@ttypeconvnode(source^).left);
  2001. end;
  2002. if (source^.nodetype=loadn) and
  2003. (source^.resultdef.typ=procdef) and
  2004. not is_nested_pd(tprocdef(source^.resultdef)) and
  2005. not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
  2006. begin
  2007. result:=generate_block_for_procaddr(tloadnode(source^));
  2008. exit;
  2009. end
  2010. else
  2011. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2012. end;
  2013. resultdef:=totypedef;
  2014. end
  2015. else
  2016. begin
  2017. { only need the address of the method? this is needed
  2018. for @tobject.create. In this case there will be a loadn without
  2019. a methodpointer. }
  2020. if (left.nodetype=loadn) and
  2021. not assigned(tloadnode(left).left) and
  2022. (not(m_nested_procvars in current_settings.modeswitches) or
  2023. not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
  2024. copytype:=pc_address_only
  2025. else
  2026. copytype:=pc_normal;
  2027. resultdef:=pd.getcopyas(procvardef,copytype);
  2028. end;
  2029. end;
  2030. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  2031. const
  2032. resultdefconvert : array[tconverttype] of pointer = (
  2033. {none} nil,
  2034. {equal} nil,
  2035. {not_possible} nil,
  2036. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  2037. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  2038. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  2039. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  2040. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  2041. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  2042. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  2043. { ansistring_2_pchar } nil,
  2044. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  2045. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  2046. { array_2_pointer } nil,
  2047. { pointer_2_array } nil,
  2048. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  2049. { int_2_bool } nil,
  2050. { bool_2_bool } nil,
  2051. { bool_2_int } nil,
  2052. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  2053. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  2054. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  2055. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  2056. { nil_2_methodprocvar } nil,
  2057. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  2058. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  2059. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  2060. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  2061. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  2062. { class_2_intf } nil,
  2063. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  2064. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  2065. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  2066. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  2067. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  2068. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  2069. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  2070. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  2071. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  2072. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  2073. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray,
  2074. { arrayconstructor_2_dynarray } @ttypeconvnode._typecheck_arrayconstructor_to_dynarray
  2075. );
  2076. type
  2077. tprocedureofobject = function : tnode of object;
  2078. var
  2079. r : packed record
  2080. proc : pointer;
  2081. obj : pointer;
  2082. end;
  2083. begin
  2084. result:=nil;
  2085. { this is a little bit dirty but it works }
  2086. { and should be quite portable too }
  2087. r.proc:=resultdefconvert[c];
  2088. r.obj:=self;
  2089. if assigned(r.proc) then
  2090. result:=tprocedureofobject(r)();
  2091. end;
  2092. function ttypeconvnode.pass_typecheck:tnode;
  2093. var
  2094. hdef : tdef;
  2095. hp : tnode;
  2096. currprocdef : tabstractprocdef;
  2097. aprocdef : tprocdef;
  2098. eq : tequaltype;
  2099. cdoptions : tcompare_defs_options;
  2100. newblock: tblocknode;
  2101. newstatement: tstatementnode;
  2102. tempnode: ttempcreatenode;
  2103. begin
  2104. result:=nil;
  2105. resultdef:=totypedef;
  2106. typecheckpass(left);
  2107. if codegenerror then
  2108. exit;
  2109. { When absolute force tc_equal }
  2110. if (nf_absolute in flags) then
  2111. begin
  2112. convtype:=tc_equal;
  2113. { we need to check regability only if something is really regable }
  2114. if ((tstoreddef(left.resultdef).is_intregable) or
  2115. (tstoreddef(resultdef).is_fpuregable)) and
  2116. (
  2117. (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  2118. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) or
  2119. { like in pdecvar.read_absolute(): if the size changes, the
  2120. register size would also have to change (but second_nothing
  2121. does not handle this) }
  2122. (tstoreddef(resultdef).size<>tstoreddef(left.resultdef).size)) then
  2123. make_not_regable(left,[ra_addr_regable]);
  2124. exit;
  2125. end;
  2126. { tp procvar support. Skip typecasts to procvar, record or set. Those
  2127. convert on the procvar value. This is used to access the
  2128. fields of a methodpointer }
  2129. if not(nf_load_procvar in flags) and
  2130. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  2131. maybe_call_procvar(left,true);
  2132. { convert array constructors to sets, because there is no conversion
  2133. possible for array constructors }
  2134. if (resultdef.typ<>arraydef) and
  2135. is_array_constructor(left.resultdef) then
  2136. begin
  2137. arrayconstructor_to_set(left);
  2138. typecheckpass(left);
  2139. end;
  2140. if target_specific_general_typeconv then
  2141. exit;
  2142. if convtype=tc_none then
  2143. begin
  2144. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  2145. { overloaded operators require calls, which is not possible inside
  2146. a constant declaration }
  2147. if (block_type<>bt_const) and
  2148. not(nf_internal in flags) then
  2149. include(cdoptions,cdo_check_operator);
  2150. if nf_explicit in flags then
  2151. include(cdoptions,cdo_explicit);
  2152. if nf_internal in flags then
  2153. include(cdoptions,cdo_internal);
  2154. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  2155. case eq of
  2156. te_exact,
  2157. te_equal :
  2158. begin
  2159. result := simplify(false);
  2160. if assigned(result) then
  2161. exit;
  2162. { in case of bitpacked accesses, the original type must
  2163. remain so that not too many/few bits are laoded }
  2164. if is_bitpacked_access(left) then
  2165. convtype:=tc_int_2_int;
  2166. { Only leave when there is no conversion to do.
  2167. We can still need to call a conversion routine,
  2168. like the routine to convert a stringconstnode }
  2169. if (convtype in [tc_equal,tc_not_possible]) and
  2170. { some conversions, like dynarray to pointer in Delphi
  2171. mode, must not be removed, because then we get memory
  2172. leaks due to missing temp finalization }
  2173. (not is_managed_type(left.resultdef) or
  2174. { different kinds of refcounted types may need calls
  2175. to different kinds of refcounting helpers }
  2176. (resultdef=left.resultdef)) then
  2177. begin
  2178. {$ifdef llvm}
  2179. { we still may have to insert a type conversion at the
  2180. llvm level }
  2181. if (left.resultdef<>resultdef) and
  2182. { if unspecialised generic -> we won't generate any code
  2183. for this, and keeping the type conversion node will
  2184. cause valid_for_assign to fail because the typecast will be from/to something of 0
  2185. bytes to/from something with a non-zero size }
  2186. not is_typeparam(left.resultdef) and
  2187. not is_typeparam(resultdef) then
  2188. result:=nil
  2189. else
  2190. {$endif llvm}
  2191. begin
  2192. left.resultdef:=resultdef;
  2193. if (nf_explicit in flags) and (left.nodetype = addrn) then
  2194. include(taddrnode(left).addrnodeflags,anf_typedaddr);
  2195. result:=left;
  2196. left:=nil;
  2197. end;
  2198. exit;
  2199. end;
  2200. end;
  2201. te_convert_l1,
  2202. te_convert_l2,
  2203. te_convert_l3,
  2204. te_convert_l4,
  2205. te_convert_l5,
  2206. te_convert_l6:
  2207. { nothing to do }
  2208. ;
  2209. te_convert_operator :
  2210. begin
  2211. include(current_procinfo.flags,pi_do_call);
  2212. addsymref(aprocdef.procsym);
  2213. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[],nil);
  2214. { tell explicitly which def we must use !! (PM) }
  2215. tcallnode(hp).procdefinition:=aprocdef;
  2216. left:=nil;
  2217. result:=hp;
  2218. exit;
  2219. end;
  2220. te_incompatible :
  2221. begin
  2222. { Procedures have a resultdef of voiddef and functions of their
  2223. own resultdef. They will therefore always be incompatible with
  2224. a procvar. Because isconvertable cannot check for procedures we
  2225. use an extra check for them.}
  2226. if (left.nodetype=calln) and
  2227. (tcallnode(left).required_para_count=0) and
  2228. (resultdef.typ=procvardef) and
  2229. (
  2230. (m_tp_procvar in current_settings.modeswitches) or
  2231. (m_mac_procvar in current_settings.modeswitches)
  2232. ) then
  2233. begin
  2234. if assigned(tcallnode(left).right) then
  2235. begin
  2236. { this is already a procvar, if it is really equal
  2237. is checked below }
  2238. convtype:=tc_equal;
  2239. hp:=tcallnode(left).right.getcopy;
  2240. currprocdef:=tabstractprocdef(hp.resultdef);
  2241. end
  2242. else
  2243. begin
  2244. convtype:=tc_proc_2_procvar;
  2245. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  2246. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  2247. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2248. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2249. begin
  2250. if assigned(tcallnode(left).methodpointer) then
  2251. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  2252. else
  2253. tloadnode(hp).set_mp(load_self_node);
  2254. end;
  2255. typecheckpass(hp);
  2256. end;
  2257. left.free;
  2258. left:=hp;
  2259. { Now check if the procedure we are going to assign to
  2260. the procvar, is compatible with the procvar's type }
  2261. if not(nf_explicit in flags) and
  2262. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible) then
  2263. IncompatibleTypes(left.resultdef,resultdef)
  2264. else
  2265. result:=typecheck_call_helper(convtype);
  2266. exit;
  2267. end
  2268. else if maybe_global_proc_to_nested(left,resultdef) or
  2269. maybe_classmethod_to_methodprocvar(left,resultdef) then
  2270. begin
  2271. result:=left;
  2272. left:=nil;
  2273. exit;
  2274. end;
  2275. { Handle explicit type conversions }
  2276. if nf_explicit in flags then
  2277. begin
  2278. { do common tc_equal cast, except when dealing with proc -> procvar
  2279. (may have to get rid of method pointer) }
  2280. if (left.resultdef.typ<>procdef) or
  2281. (resultdef.typ<>procvardef) then
  2282. convtype:=tc_equal
  2283. else
  2284. convtype:=tc_proc_2_procvar;
  2285. { ordinal constants can be resized to 1,2,4,8 bytes }
  2286. if (left.nodetype=ordconstn) then
  2287. begin
  2288. { Insert typeconv for ordinal to the correct size first on left, after
  2289. that the other conversion can be done }
  2290. hdef:=nil;
  2291. case longint(resultdef.size) of
  2292. 1 :
  2293. hdef:=s8inttype;
  2294. 2 :
  2295. hdef:=s16inttype;
  2296. 4 :
  2297. hdef:=s32inttype;
  2298. 8 :
  2299. hdef:=s64inttype;
  2300. end;
  2301. { we need explicit, because it can also be an enum }
  2302. if assigned(hdef) then
  2303. inserttypeconv_internal(left,hdef)
  2304. else
  2305. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2306. end;
  2307. { class/interface to class/interface, with checkobject support }
  2308. if is_class_or_interface_or_objc(resultdef) and
  2309. is_class_or_interface_or_objc(left.resultdef) then
  2310. begin
  2311. { check if the types are related }
  2312. if not(nf_internal in flags) and
  2313. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2314. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2315. begin
  2316. { Give an error when typecasting class to interface, this is compatible
  2317. with delphi }
  2318. if is_interface(resultdef) and
  2319. not is_interface(left.resultdef) then
  2320. CGMessage2(type_e_classes_not_related,
  2321. FullTypeName(left.resultdef,resultdef),
  2322. FullTypeName(resultdef,left.resultdef))
  2323. else
  2324. CGMessage2(type_w_classes_not_related,
  2325. FullTypeName(left.resultdef,resultdef),
  2326. FullTypeName(resultdef,left.resultdef))
  2327. end;
  2328. { Add runtime check? }
  2329. if not is_objc_class_or_protocol(resultdef) and
  2330. not is_objc_class_or_protocol(left.resultdef) and
  2331. (cs_check_object in current_settings.localswitches) and
  2332. not(nf_internal in flags) then
  2333. begin
  2334. { we can translate the typeconvnode to 'as' when
  2335. typecasting to a class or interface }
  2336. { we need to make sure the result can still be
  2337. passed as a var parameter }
  2338. newblock:=internalstatements(newstatement);
  2339. if (valid_for_var(left,false)) then
  2340. begin
  2341. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2342. addstatement(newstatement,tempnode);
  2343. addstatement(newstatement,cassignmentnode.create(
  2344. ctemprefnode.create(tempnode),
  2345. caddrnode.create_internal(left)));
  2346. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2347. end
  2348. else
  2349. begin
  2350. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2351. addstatement(newstatement,tempnode);
  2352. addstatement(newstatement,cassignmentnode.create(
  2353. ctemprefnode.create(tempnode),
  2354. left));
  2355. left:=ctemprefnode.create(tempnode);
  2356. end;
  2357. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2358. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2359. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2360. left:=nil;
  2361. result:=newblock;
  2362. exit;
  2363. end;
  2364. end
  2365. else
  2366. begin
  2367. { only if the same size or formal def, and }
  2368. { don't allow type casting of constants to }
  2369. { structured types }
  2370. if not(
  2371. (left.resultdef.typ=formaldef) or
  2372. {$ifdef jvm}
  2373. { enums /are/ class instances on the JVM
  2374. platform }
  2375. (((left.resultdef.typ=enumdef) and
  2376. (resultdef.typ=objectdef)) or
  2377. ((resultdef.typ=enumdef) and
  2378. (left.resultdef.typ=objectdef))) or
  2379. {$endif}
  2380. (
  2381. is_void(left.resultdef) and
  2382. (left.nodetype=derefn)
  2383. ) or
  2384. (
  2385. not(is_open_array(left.resultdef)) and
  2386. not(is_array_constructor(left.resultdef)) and
  2387. not(is_array_of_const(left.resultdef)) and
  2388. (left.resultdef.size=resultdef.size) and
  2389. { disallow casts of const nodes }
  2390. (not is_constnode(left) or
  2391. { however, there are some exceptions }
  2392. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2393. filedef,variantdef,objectdef]) or
  2394. is_class_or_interface_or_objc(resultdef) or
  2395. { the softfloat code generates casts <const. float> to record }
  2396. (nf_internal in flags)
  2397. ))
  2398. )
  2399. ) then
  2400. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2401. else
  2402. begin
  2403. { perform target-specific explicit typecast
  2404. checks }
  2405. if target_specific_explicit_typeconv then
  2406. begin
  2407. result:=simplify(false);
  2408. exit;
  2409. end;
  2410. end;
  2411. end;
  2412. end
  2413. else
  2414. IncompatibleTypes(left.resultdef,resultdef);
  2415. end;
  2416. else
  2417. internalerror(200211231);
  2418. end;
  2419. end;
  2420. { Give hint or warning for unportable code, exceptions are
  2421. - typecasts from constants
  2422. - void }
  2423. if not(nf_internal in flags) and
  2424. (left.nodetype<>ordconstn) and
  2425. not(is_void(left.resultdef)) and
  2426. (((left.resultdef.typ=orddef) and
  2427. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2428. ((resultdef.typ=orddef) and
  2429. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2430. begin
  2431. {Converting pointers to signed integers is a bad idea. Warn.}
  2432. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2433. { Give a warning when sizes don't match, because then info will be lost }
  2434. if left.resultdef.size=resultdef.size then
  2435. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2436. else
  2437. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2438. end;
  2439. { tc_cord_2_pointer still requires a type check, which
  2440. simplify does not do }
  2441. if (convtype<>tc_cord_2_pointer) then
  2442. begin
  2443. result := simplify(false);
  2444. if assigned(result) then
  2445. exit;
  2446. end;
  2447. { now call the resultdef helper to do constant folding }
  2448. result:=typecheck_call_helper(convtype);
  2449. end;
  2450. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2451. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2452. this applies is powerpc64
  2453. }
  2454. {$ifndef CPUNO32BITOPS}
  2455. { checks whether we can safely remove typeconversions to bigger types
  2456. in case range and overflow checking are off, and in case
  2457. the result of this node tree is downcasted again to a
  2458. smaller type value afterwards,
  2459. the smaller types being allowed are described by validints, ordinal constants must fit into l..h
  2460. We do this on 64 bit CPUs as well, they benefit from it as well }
  2461. function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;const l,h : Tconstexprint): boolean;
  2462. var
  2463. gotdivmod: boolean;
  2464. { checks whether a node has an accepted resultdef, or originally
  2465. had one but was implicitly converted to s64bit }
  2466. function wasoriginallysmallerint(n: tnode): boolean;
  2467. begin
  2468. if (n.resultdef.typ<>orddef) then
  2469. exit(false);
  2470. if (torddef(n.resultdef).ordtype in validints) then
  2471. begin
  2472. if is_signed(n.resultdef) then
  2473. gotsint:=true;
  2474. exit(true);
  2475. end;
  2476. { type conv to a bigger int, we do not like to use? }
  2477. if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
  2478. { nf_explicit is also set for explicitly typecasted }
  2479. { ordconstn's }
  2480. ([nf_internal,nf_explicit]*n.flags=[]) and
  2481. { either a typeconversion node coming from a smaller type }
  2482. (((n.nodetype=typeconvn) and
  2483. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2484. (torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
  2485. { or an ordconstnode which has a smaller type}
  2486. ((n.nodetype=ordconstn) and
  2487. (tordconstnode(n).value>=l) and
  2488. (tordconstnode(n).value<=h))) then
  2489. begin
  2490. if ((n.nodetype=typeconvn) and
  2491. is_signed(ttypeconvnode(n).left.resultdef)) or
  2492. ((n.nodetype=ordconstn) and
  2493. (tordconstnode(n).value<0)) then
  2494. gotsint:=true;
  2495. exit(true);
  2496. end;
  2497. result:=false;
  2498. end;
  2499. function docheckremoveinttypeconvs(n: tnode): boolean;
  2500. begin
  2501. result:=false;
  2502. if wasoriginallysmallerint(n) then
  2503. exit(true);
  2504. case n.nodetype of
  2505. subn,orn,xorn:
  2506. begin
  2507. { the result could become negative in this case }
  2508. if n.nodetype=subn then
  2509. gotsint:=true;
  2510. result:=
  2511. docheckremoveinttypeconvs(tbinarynode(n).left) and
  2512. docheckremoveinttypeconvs(tbinarynode(n).right);
  2513. end;
  2514. addn,muln,divn,modn,andn:
  2515. begin
  2516. if n.nodetype in [divn,modn] then
  2517. gotdivmod:=true;
  2518. result:=
  2519. (docheckremoveinttypeconvs(tbinarynode(n).left) and
  2520. docheckremoveinttypeconvs(tbinarynode(n).right)) or
  2521. { in case of div/mod, the result of that division/modulo can
  2522. usually be different in 32 and 64 bit }
  2523. (not gotdivmod and
  2524. (((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).left)) or
  2525. ((n.nodetype=andn) and wasoriginallysmallerint(tbinarynode(n).right))));
  2526. end;
  2527. end;
  2528. end;
  2529. begin { checkremove64bittypeconvs }
  2530. gotdivmod:=false;
  2531. gotsint:=false;
  2532. result:=
  2533. docheckremoveinttypeconvs(n) and
  2534. not(gotdivmod and gotsint);
  2535. end;
  2536. { remove int type conversions and set the result to the given type }
  2537. procedure doremoveinttypeconvs(var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
  2538. begin
  2539. case n.nodetype of
  2540. subn,addn,muln,divn,modn,xorn,andn,orn:
  2541. begin
  2542. exclude(n.flags,nf_internal);
  2543. if not forceunsigned and
  2544. is_signed(n.resultdef) then
  2545. begin
  2546. doremoveinttypeconvs(tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
  2547. doremoveinttypeconvs(tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
  2548. n.resultdef:=signedtype;
  2549. end
  2550. else
  2551. begin
  2552. doremoveinttypeconvs(tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  2553. doremoveinttypeconvs(tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
  2554. n.resultdef:=unsignedtype;
  2555. end;
  2556. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  2557. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  2558. // ) then
  2559. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  2560. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  2561. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  2562. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  2563. end;
  2564. typeconvn:
  2565. begin
  2566. ttypeconvnode(n).totypedef:=todef;
  2567. { may change the type conversion, e.g. if the old conversion was
  2568. from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
  2569. n.resultdef:=nil;
  2570. ttypeconvnode(n).convtype:=tc_none;
  2571. typecheckpass(n);
  2572. end;
  2573. else
  2574. inserttypeconv_internal(n,todef);
  2575. end;
  2576. end;
  2577. {$endif not CPUNO32BITOPS}
  2578. procedure swap_const_value (var val : TConstExprInt; size : longint);
  2579. begin
  2580. case size of
  2581. 1 : {do nothing };
  2582. 2 : if val.signed then
  2583. val.svalue:=swapendian(smallint(val.svalue))
  2584. else
  2585. val.uvalue:=swapendian(word(val.uvalue));
  2586. 4 : if val.signed then
  2587. val.svalue:=swapendian(longint(val.svalue))
  2588. else
  2589. val.uvalue:=swapendian(qword(val.uvalue));
  2590. 8 : if val.signed then
  2591. val.svalue:=swapendian(int64(val.svalue))
  2592. else
  2593. val.uvalue:=swapendian(qword(val.uvalue));
  2594. else
  2595. internalerror(2014111201);
  2596. end;
  2597. end;
  2598. function ttypeconvnode.simplify(forinline : boolean): tnode;
  2599. var
  2600. hp: tnode;
  2601. {$ifndef CPUNO32BITOPS}
  2602. foundsint: boolean;
  2603. {$endif not CPUNO32BITOPS}
  2604. begin
  2605. result := nil;
  2606. { Constant folding and other node transitions to
  2607. remove the typeconv node }
  2608. case left.nodetype of
  2609. stringconstn :
  2610. if (resultdef.typ=stringdef) and
  2611. ((convtype=tc_equal) or
  2612. ((convtype=tc_string_2_string) and
  2613. (
  2614. ((not is_widechararray(left.resultdef) and
  2615. not is_wide_or_unicode_string(left.resultdef)) or
  2616. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  2617. )
  2618. )
  2619. )
  2620. ) then
  2621. begin
  2622. { output string consts in local ansistring encoding }
  2623. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0)or(tstringdef(resultdef).encoding=globals.CP_NONE)) then
  2624. tstringconstnode(left).changestringtype(getansistringdef)
  2625. else
  2626. tstringconstnode(left).changestringtype(resultdef);
  2627. result:=left;
  2628. resultdef:=left.resultdef;
  2629. left:=nil;
  2630. exit;
  2631. end
  2632. else if
  2633. (convtype<>tc_cstring_2_pchar) and
  2634. is_dynamicstring(left.resultdef) and
  2635. (tstringconstnode(left).len=0) and
  2636. (resultdef.typ=pointerdef) and
  2637. cstringconstnode.emptydynstrnil then
  2638. begin
  2639. result:=cnilnode.create;
  2640. exit;
  2641. end;
  2642. realconstn :
  2643. begin
  2644. if (convtype = tc_real_2_currency) then
  2645. result := typecheck_real_to_currency
  2646. else if (convtype = tc_real_2_real) then
  2647. result := typecheck_real_to_real
  2648. else
  2649. exit;
  2650. if not(assigned(result)) then
  2651. begin
  2652. result := left;
  2653. left := nil;
  2654. end;
  2655. if (result.nodetype = realconstn) then
  2656. begin
  2657. hp:=result;
  2658. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  2659. if ([nf_explicit,nf_internal] * flags <> []) then
  2660. include(result.flags, nf_explicit);
  2661. hp.free;
  2662. end;
  2663. end;
  2664. niln :
  2665. begin
  2666. { nil to ordinal node }
  2667. if (resultdef.typ=orddef) then
  2668. begin
  2669. hp:=cordconstnode.create(0,resultdef,true);
  2670. if ([nf_explicit,nf_internal] * flags <> []) then
  2671. include(hp.flags, nf_explicit);
  2672. result:=hp;
  2673. exit;
  2674. end
  2675. else
  2676. { fold nil to any pointer type }
  2677. if (resultdef.typ=pointerdef) then
  2678. begin
  2679. hp:=cnilnode.create;
  2680. hp.resultdef:=resultdef;
  2681. if ([nf_explicit,nf_internal] * flags <> []) then
  2682. include(hp.flags, nf_explicit);
  2683. result:=hp;
  2684. exit;
  2685. end
  2686. else
  2687. { remove typeconv after niln, but not when the result is a
  2688. methodpointer. The typeconv of the methodpointer will then
  2689. take care of updateing size of niln to OS_64 }
  2690. if not((resultdef.typ=procvardef) and
  2691. not(tprocvardef(resultdef).is_addressonly)) and
  2692. { converting (dynamic array) nil to a an open array is not allowed }
  2693. not is_open_array(resultdef) then
  2694. begin
  2695. left.resultdef:=resultdef;
  2696. if ([nf_explicit,nf_internal] * flags <> []) then
  2697. include(left.flags, nf_explicit);
  2698. result:=left;
  2699. left:=nil;
  2700. exit;
  2701. end;
  2702. end;
  2703. ordconstn :
  2704. begin
  2705. { ordinal contants can be directly converted }
  2706. { but not char to char because it is a widechar to char or via versa }
  2707. { which needs extra code to do the code page transistion }
  2708. { constant ordinal to pointer }
  2709. if (resultdef.typ=pointerdef) and
  2710. (convtype<>tc_cchar_2_pchar) then
  2711. begin
  2712. if (target_info.system in systems_managed_vm) and
  2713. (tordconstnode(left).value<>0) then
  2714. message(parser_e_feature_unsupported_for_vm);
  2715. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  2716. if ([nf_explicit,nf_internal] * flags <> []) then
  2717. include(hp.flags, nf_explicit);
  2718. result:=hp;
  2719. exit;
  2720. end
  2721. else if is_ordinal(resultdef) and
  2722. not(convtype=tc_char_2_char) then
  2723. begin
  2724. { replace the resultdef and recheck the range }
  2725. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  2726. include(left.flags, nf_explicit)
  2727. else
  2728. { no longer an ordconst with an explicit typecast }
  2729. exclude(left.flags, nf_explicit);
  2730. { when converting from one boolean type to another, force }
  2731. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  2732. { (Delphi-compatibile) }
  2733. if is_boolean(left.resultdef) and
  2734. is_boolean(resultdef) and
  2735. (is_cbool(left.resultdef) or
  2736. is_cbool(resultdef)) then
  2737. begin
  2738. if is_pasbool(resultdef) then
  2739. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  2740. else
  2741. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  2742. end
  2743. else
  2744. begin
  2745. { for constant values on absolute variables, swaping is required }
  2746. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2747. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  2748. if not(nf_internal in flags) then
  2749. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags)
  2750. or (nf_absolute in flags),false);
  2751. { swap value back, but according to new type }
  2752. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  2753. swap_const_value(tordconstnode(left).value,resultdef.size);
  2754. { cut off the new value? }
  2755. if resultdef.size<left.resultdef.size then
  2756. case resultdef.size of
  2757. 1:
  2758. if is_signed(resultdef) then
  2759. tordconstnode(left).value:=tordconstnode(left).value and shortint($ff)
  2760. else
  2761. tordconstnode(left).value:=tordconstnode(left).value and byte($ff);
  2762. 2:
  2763. if is_signed(resultdef) then
  2764. tordconstnode(left).value:=tordconstnode(left).value and smallint($ffff)
  2765. else
  2766. tordconstnode(left).value:=tordconstnode(left).value and word($ffff);
  2767. 4:
  2768. if is_signed(resultdef) then
  2769. tordconstnode(left).value:=tordconstnode(left).value and longint($ffffffff)
  2770. else
  2771. tordconstnode(left).value:=tordconstnode(left).value and dword($ffffffff);
  2772. end;
  2773. end;
  2774. left.resultdef:=resultdef;
  2775. tordconstnode(left).typedef:=resultdef;
  2776. if is_signed(resultdef) then
  2777. tordconstnode(left).value.signed:=true
  2778. else
  2779. tordconstnode(left).value.signed:=false;
  2780. result:=left;
  2781. left:=nil;
  2782. exit;
  2783. end;
  2784. end;
  2785. pointerconstn :
  2786. begin
  2787. { pointerconstn to any pointer is folded too }
  2788. if (resultdef.typ=pointerdef) then
  2789. begin
  2790. left.resultdef:=resultdef;
  2791. if ([nf_explicit,nf_internal] * flags <> []) then
  2792. include(left.flags, nf_explicit)
  2793. else
  2794. { no longer an ordconst with an explicit typecast }
  2795. exclude(left.flags, nf_explicit);
  2796. result:=left;
  2797. left:=nil;
  2798. exit;
  2799. end
  2800. { constant pointer to ordinal }
  2801. else if is_ordinal(resultdef) then
  2802. begin
  2803. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  2804. resultdef,not(nf_explicit in flags));
  2805. if ([nf_explicit,nf_internal] * flags <> []) then
  2806. include(hp.flags, nf_explicit);
  2807. result:=hp;
  2808. exit;
  2809. end;
  2810. end;
  2811. end;
  2812. {$ifndef CPUNO32BITOPS}
  2813. { must be done before code below, because we need the
  2814. typeconversions for ordconstn's as well }
  2815. case convtype of
  2816. tc_int_2_int:
  2817. begin
  2818. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  2819. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  2820. begin
  2821. { avoid unnecessary widening of intermediary calculations
  2822. to 64 bit }
  2823. if (resultdef.size <= 4) and
  2824. is_64bitint(left.resultdef) and
  2825. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2826. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  2827. doremoveinttypeconvs(left,generrordef,not foundsint,s32inttype,u32inttype);
  2828. {$if defined(cpu16bitalu)}
  2829. if (resultdef.size <= 2) and
  2830. (is_32bitint(left.resultdef) or 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],int64(low(smallint)),high(word)) then
  2833. doremoveinttypeconvs(left,generrordef,not foundsint,s16inttype,u16inttype);
  2834. {$endif defined(cpu16bitalu)}
  2835. {$if defined(cpu8bitalu)}
  2836. if (resultdef.size<left.resultdef.size) and
  2837. is_integer(left.resultdef) and
  2838. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn]) and
  2839. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  2840. doremoveinttypeconvs(left,generrordef,not foundsint,s8inttype,u8inttype);
  2841. {$endif defined(cpu8bitalu)}
  2842. { the above simplification may have left a redundant equal
  2843. typeconv (e.g. int32 to int32). If that's the case, we remove it }
  2844. if equal_defs(left.resultdef,resultdef) then
  2845. begin
  2846. result:=left;
  2847. left:=nil;
  2848. exit;
  2849. end;
  2850. end;
  2851. end;
  2852. end;
  2853. {$endif not CPUNO32BITOPS}
  2854. end;
  2855. procedure Ttypeconvnode.mark_write;
  2856. begin
  2857. left.mark_write;
  2858. end;
  2859. function ttypeconvnode.first_cord_to_pointer : tnode;
  2860. begin
  2861. result:=nil;
  2862. internalerror(200104043);
  2863. end;
  2864. function ttypeconvnode.first_int_to_int : tnode;
  2865. begin
  2866. first_int_to_int:=nil;
  2867. expectloc:=left.expectloc;
  2868. if not is_void(left.resultdef) then
  2869. begin
  2870. if (left.expectloc<>LOC_REGISTER) and
  2871. ((resultdef.size>left.resultdef.size) or
  2872. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  2873. expectloc:=LOC_REGISTER
  2874. else
  2875. if (left.expectloc=LOC_CREGISTER) and
  2876. (resultdef.size<left.resultdef.size) then
  2877. expectloc:=LOC_REGISTER;
  2878. end;
  2879. end;
  2880. function ttypeconvnode.first_cstring_to_pchar : tnode;
  2881. begin
  2882. result:=nil;
  2883. expectloc:=LOC_REGISTER;
  2884. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  2885. if (cs_create_pic in current_settings.moduleswitches) then
  2886. include(current_procinfo.flags,pi_needs_got);
  2887. end;
  2888. function ttypeconvnode.first_cstring_to_int : tnode;
  2889. begin
  2890. result:=nil;
  2891. internalerror(200510014);
  2892. end;
  2893. function ttypeconvnode.first_string_to_chararray : tnode;
  2894. begin
  2895. first_string_to_chararray:=nil;
  2896. expectloc:=left.expectloc;
  2897. end;
  2898. function ttypeconvnode.first_char_to_string : tnode;
  2899. begin
  2900. first_char_to_string:=nil;
  2901. if tstringdef(resultdef).stringtype=st_shortstring then
  2902. inc(current_procinfo.estimatedtempsize,256);
  2903. expectloc:=LOC_REFERENCE;
  2904. end;
  2905. function ttypeconvnode.first_char_to_chararray : tnode;
  2906. begin
  2907. if resultdef.size <> 1 then
  2908. begin
  2909. { convert first to string, then to chararray }
  2910. inserttypeconv(left,cshortstringtype);
  2911. inserttypeconv(left,resultdef);
  2912. result:=left;
  2913. left := nil;
  2914. exit;
  2915. end;
  2916. result := nil;
  2917. end;
  2918. function ttypeconvnode.first_nothing : tnode;
  2919. begin
  2920. first_nothing:=nil;
  2921. end;
  2922. function ttypeconvnode.first_array_to_pointer : tnode;
  2923. begin
  2924. first_array_to_pointer:=nil;
  2925. expectloc:=LOC_REGISTER;
  2926. end;
  2927. function ttypeconvnode.first_int_to_real: tnode;
  2928. var
  2929. fname: string[32];
  2930. begin
  2931. if target_info.system in systems_wince then
  2932. begin
  2933. { converting a 64bit integer to a float requires a helper }
  2934. if is_64bitint(left.resultdef) or
  2935. is_currency(left.resultdef) then
  2936. begin
  2937. { hack to avoid double division by 10000, as it's
  2938. already done by typecheckpass.resultdef_int_to_real }
  2939. if is_currency(left.resultdef) then
  2940. left.resultdef := s64inttype;
  2941. if is_signed(left.resultdef) then
  2942. fname:='i64to'
  2943. else
  2944. fname:='ui64to';
  2945. end
  2946. else
  2947. { other integers are supposed to be 32 bit }
  2948. begin
  2949. if is_signed(left.resultdef) then
  2950. fname:='ito'
  2951. else
  2952. fname:='uto';
  2953. firstpass(left);
  2954. end;
  2955. if tfloatdef(resultdef).floattype=s64real then
  2956. fname:=fname+'d'
  2957. else
  2958. fname:=fname+'s';
  2959. result:=ccallnode.createintern(fname,ccallparanode.create(
  2960. left,nil));
  2961. left:=nil;
  2962. firstpass(result);
  2963. exit;
  2964. end
  2965. else
  2966. begin
  2967. { converting a 64bit integer to a float requires a helper }
  2968. if is_64bitint(left.resultdef) or
  2969. is_currency(left.resultdef) then
  2970. begin
  2971. { hack to avoid double division by 10000, as it's
  2972. already done by typecheckpass.resultdef_int_to_real }
  2973. if is_currency(left.resultdef) then
  2974. left.resultdef := s64inttype;
  2975. if is_signed(left.resultdef) then
  2976. fname:='int64_to_'
  2977. else
  2978. { we can't do better currently }
  2979. fname:='qword_to_';
  2980. end
  2981. else
  2982. { other integers are supposed to be 32 bit }
  2983. begin
  2984. if is_signed(left.resultdef) then
  2985. fname:='int32_to_'
  2986. else
  2987. fname:='int64_to_';
  2988. firstpass(left);
  2989. end;
  2990. if tfloatdef(resultdef).floattype=s64real then
  2991. fname:=fname+'float64'
  2992. else
  2993. fname:=fname+'float32';
  2994. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2995. left,nil)),resultdef);
  2996. left:=nil;
  2997. firstpass(result);
  2998. exit;
  2999. end;
  3000. end;
  3001. function ttypeconvnode.first_real_to_real : tnode;
  3002. begin
  3003. {$ifdef cpufpemu}
  3004. if cs_fp_emulation in current_settings.moduleswitches then
  3005. begin
  3006. if target_info.system in systems_wince then
  3007. begin
  3008. case tfloatdef(left.resultdef).floattype of
  3009. s32real:
  3010. case tfloatdef(resultdef).floattype of
  3011. s64real:
  3012. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  3013. s32real:
  3014. begin
  3015. result:=left;
  3016. left:=nil;
  3017. end;
  3018. else
  3019. internalerror(2005082704);
  3020. end;
  3021. s64real:
  3022. case tfloatdef(resultdef).floattype of
  3023. s32real:
  3024. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  3025. s64real:
  3026. begin
  3027. result:=left;
  3028. left:=nil;
  3029. end;
  3030. else
  3031. internalerror(2005082703);
  3032. end;
  3033. else
  3034. internalerror(2005082702);
  3035. end;
  3036. left:=nil;
  3037. firstpass(result);
  3038. exit;
  3039. end
  3040. else
  3041. begin
  3042. case tfloatdef(left.resultdef).floattype of
  3043. s32real:
  3044. case tfloatdef(resultdef).floattype of
  3045. s64real:
  3046. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  3047. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  3048. s32real:
  3049. begin
  3050. result:=left;
  3051. left:=nil;
  3052. end;
  3053. else
  3054. internalerror(200610151);
  3055. end;
  3056. s64real:
  3057. case tfloatdef(resultdef).floattype of
  3058. s32real:
  3059. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  3060. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  3061. s64real:
  3062. begin
  3063. result:=left;
  3064. left:=nil;
  3065. end;
  3066. else
  3067. internalerror(200610152);
  3068. end;
  3069. else
  3070. internalerror(200610153);
  3071. end;
  3072. left:=nil;
  3073. firstpass(result);
  3074. exit;
  3075. end;
  3076. end
  3077. else
  3078. {$endif cpufpemu}
  3079. begin
  3080. first_real_to_real:=nil;
  3081. if not use_vectorfpu(resultdef) then
  3082. expectloc:=LOC_FPUREGISTER
  3083. else
  3084. expectloc:=LOC_MMREGISTER;
  3085. end;
  3086. end;
  3087. function ttypeconvnode.first_pointer_to_array : tnode;
  3088. begin
  3089. first_pointer_to_array:=nil;
  3090. expectloc:=LOC_REFERENCE;
  3091. end;
  3092. function ttypeconvnode.first_cchar_to_pchar : tnode;
  3093. begin
  3094. first_cchar_to_pchar:=nil;
  3095. internalerror(200104021);
  3096. end;
  3097. function ttypeconvnode.first_bool_to_int : tnode;
  3098. begin
  3099. first_bool_to_int:=nil;
  3100. { byte(boolean) or word(wordbool) or longint(longbool) must
  3101. be accepted for var parameters }
  3102. if (nf_explicit in flags) and
  3103. (left.resultdef.size=resultdef.size) and
  3104. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3105. begin
  3106. expectloc:=left.expectloc;
  3107. exit;
  3108. end;
  3109. expectloc:=LOC_REGISTER;
  3110. end;
  3111. function ttypeconvnode.first_int_to_bool : tnode;
  3112. begin
  3113. first_int_to_bool:=nil;
  3114. { byte(boolean) or word(wordbool) or longint(longbool) must
  3115. be accepted for var parameters }
  3116. if (nf_explicit in flags) and
  3117. (left.resultdef.size=resultdef.size) and
  3118. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3119. begin
  3120. expectloc:=left.expectloc;
  3121. exit;
  3122. end;
  3123. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  3124. { convert to a boolean (only necessary for 32bit processors) }
  3125. { note: not if left is already a bool (qwordbool that is true, even if
  3126. only because the highest bit is set, must remain true if it is
  3127. --implicitly, unlike integers-- converted to another type of bool),
  3128. Left can already be a bool because this routine can also be called
  3129. from first_bool_to_bool }
  3130. if not is_boolean(left.resultdef) and
  3131. (left.resultdef.size > sizeof(aint)) and
  3132. (left.resultdef.size<>resultdef.size)
  3133. and is_cbool(resultdef) then
  3134. begin
  3135. left:=ctypeconvnode.create_internal(left,s32inttype);
  3136. firstpass(left);
  3137. exit;
  3138. end;
  3139. expectloc:=LOC_REGISTER;
  3140. end;
  3141. function ttypeconvnode.first_bool_to_bool : tnode;
  3142. begin
  3143. first_bool_to_bool:=nil;
  3144. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  3145. not is_cbool(resultdef) then
  3146. expectloc := left.expectloc
  3147. { the following cases use the code generation for bool_to_int/
  3148. int_to_bool -> also set their expectlocs }
  3149. else if (resultdef.size=left.resultdef.size) and
  3150. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  3151. result:=first_bool_to_int
  3152. else
  3153. result:=first_int_to_bool
  3154. end;
  3155. function ttypeconvnode.first_char_to_char : tnode;
  3156. var
  3157. fname: string[18];
  3158. begin
  3159. if (torddef(resultdef).ordtype=uchar) and
  3160. (torddef(left.resultdef).ordtype=uwidechar) then
  3161. fname := 'fpc_uchar_to_char'
  3162. else if (torddef(resultdef).ordtype=uwidechar) and
  3163. (torddef(left.resultdef).ordtype=uchar) then
  3164. fname := 'fpc_char_to_uchar'
  3165. else
  3166. internalerror(2007081201);
  3167. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  3168. left:=nil;
  3169. firstpass(result);
  3170. end;
  3171. function ttypeconvnode.first_proc_to_procvar : tnode;
  3172. begin
  3173. first_proc_to_procvar:=nil;
  3174. { if we take the address of a nested function, the current function/
  3175. procedure needs a stack frame since it's required to construct
  3176. the nested procvar }
  3177. if is_nested_pd(tprocvardef(resultdef)) then
  3178. include(current_procinfo.flags,pi_needs_stackframe);
  3179. if tabstractprocdef(resultdef).is_addressonly then
  3180. expectloc:=LOC_REGISTER
  3181. else
  3182. expectloc:=left.expectloc;
  3183. end;
  3184. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  3185. begin
  3186. first_nil_to_methodprocvar:=nil;
  3187. expectloc:=LOC_REGISTER;
  3188. end;
  3189. function ttypeconvnode.first_set_to_set : tnode;
  3190. var
  3191. newstatement : tstatementnode;
  3192. temp : ttempcreatenode;
  3193. begin
  3194. { in theory, we should do range checking here,
  3195. but Delphi doesn't do it either (FK) }
  3196. if left.nodetype=setconstn then
  3197. begin
  3198. left.resultdef:=resultdef;
  3199. result:=left;
  3200. left:=nil;
  3201. end
  3202. { equal sets for the code generator? }
  3203. else if (left.resultdef.size=resultdef.size) and
  3204. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  3205. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  3206. { e.g., this prints true because of this:
  3207. var
  3208. sa: set of 1..2;
  3209. sb: set of 5..6;
  3210. b: byte;
  3211. begin
  3212. b:=1;
  3213. sa:=[1..2];
  3214. sb:=sa;
  3215. writeln(b in sb);
  3216. end.
  3217. }
  3218. begin
  3219. result:=left;
  3220. left:=nil;
  3221. end
  3222. else
  3223. begin
  3224. result:=internalstatements(newstatement);
  3225. { in case left is a smallset expression, it can be an addn or so. }
  3226. { fpc_varset_load expects a formal const parameter, which doesn't }
  3227. { accept set addn's -> assign to a temp first and pass the temp }
  3228. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  3229. begin
  3230. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  3231. addstatement(newstatement,temp);
  3232. { temp := left }
  3233. addstatement(newstatement,cassignmentnode.create(
  3234. ctemprefnode.create(temp),left));
  3235. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3236. addstatement(newstatement,ctemprefnode.create(temp));
  3237. left:=result;
  3238. firstpass(left);
  3239. { recreate the result's internalstatements list }
  3240. result:=internalstatements(newstatement);
  3241. end;
  3242. { create temp for result }
  3243. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3244. addstatement(newstatement,temp);
  3245. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  3246. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  3247. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3248. ccallparanode.create(ctemprefnode.create(temp),
  3249. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3250. ccallparanode.create(left,nil))))))
  3251. );
  3252. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3253. addstatement(newstatement,ctemprefnode.create(temp));
  3254. left:=nil;
  3255. end;
  3256. end;
  3257. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  3258. begin
  3259. first_ansistring_to_pchar:=nil;
  3260. expectloc:=LOC_REGISTER;
  3261. { Use of FPC_EMPTYCHAR label requires setting pi_needs_got flag }
  3262. if (cs_create_pic in current_settings.moduleswitches) then
  3263. include(current_procinfo.flags,pi_needs_got);
  3264. end;
  3265. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  3266. begin
  3267. first_arrayconstructor_to_set:=nil;
  3268. internalerror(200104022);
  3269. end;
  3270. function ttypeconvnode.first_class_to_intf : tnode;
  3271. var
  3272. hd : tobjectdef;
  3273. ImplIntf : TImplementedInterface;
  3274. begin
  3275. result:=nil;
  3276. expectloc:=LOC_REGISTER;
  3277. hd:=tobjectdef(left.resultdef);
  3278. while assigned(hd) do
  3279. begin
  3280. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3281. if assigned(ImplIntf) then
  3282. begin
  3283. case ImplIntf.IType of
  3284. etStandard:
  3285. { handle in pass 2 }
  3286. ;
  3287. etFieldValue, etFieldValueClass:
  3288. if is_interface(tobjectdef(resultdef)) then
  3289. begin
  3290. result:=left;
  3291. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3292. { this ensures proper refcounting when field is of class type }
  3293. if not is_interface(result.resultdef) then
  3294. inserttypeconv(result, resultdef);
  3295. left:=nil;
  3296. end
  3297. else
  3298. begin
  3299. internalerror(200802213);
  3300. end;
  3301. etStaticMethodResult, etStaticMethodClass,
  3302. etVirtualMethodResult, etVirtualMethodClass:
  3303. if is_interface(tobjectdef(resultdef)) then
  3304. begin
  3305. { TODO: generating a call to TObject.GetInterface instead could yield
  3306. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3307. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3308. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3309. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3310. left,[],nil);
  3311. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3312. { if it is a class, process it further in a similar way }
  3313. if not is_interface(result.resultdef) then
  3314. inserttypeconv(result, resultdef);
  3315. left:=nil;
  3316. end
  3317. else if is_class(tobjectdef(resultdef)) then
  3318. begin
  3319. internalerror(200802211);
  3320. end
  3321. else
  3322. internalerror(200802231);
  3323. else
  3324. internalerror(200802165);
  3325. end;
  3326. break;
  3327. end;
  3328. hd:=hd.childof;
  3329. end;
  3330. if hd=nil then
  3331. internalerror(200802164);
  3332. end;
  3333. function ttypeconvnode.first_string_to_string : tnode;
  3334. var
  3335. procname: string[31];
  3336. newblock : tblocknode;
  3337. newstat : tstatementnode;
  3338. restemp : ttempcreatenode;
  3339. begin
  3340. { get the correct procedure name }
  3341. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3342. '_to_'+tstringdef(resultdef).stringtypname;
  3343. if tstringdef(resultdef).stringtype=st_shortstring then
  3344. begin
  3345. newblock:=internalstatements(newstat);
  3346. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3347. addstatement(newstat,restemp);
  3348. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3349. ctemprefnode.create(restemp),nil))));
  3350. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  3351. addstatement(newstat,ctemprefnode.create(restemp));
  3352. result:=newblock;
  3353. end
  3354. { encoding parameter required? }
  3355. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  3356. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  3357. result:=ccallnode.createinternres(procname,
  3358. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  3359. ccallparanode.create(left,nil)),resultdef)
  3360. else
  3361. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  3362. left:=nil;
  3363. end;
  3364. function ttypeconvnode._first_int_to_int : tnode;
  3365. begin
  3366. result:=first_int_to_int;
  3367. end;
  3368. function ttypeconvnode._first_cstring_to_pchar : tnode;
  3369. begin
  3370. result:=first_cstring_to_pchar;
  3371. end;
  3372. function ttypeconvnode._first_cstring_to_int : tnode;
  3373. begin
  3374. result:=first_cstring_to_int;
  3375. end;
  3376. function ttypeconvnode._first_string_to_chararray : tnode;
  3377. begin
  3378. result:=first_string_to_chararray;
  3379. end;
  3380. function ttypeconvnode._first_char_to_string : tnode;
  3381. begin
  3382. result:=first_char_to_string;
  3383. end;
  3384. function ttypeconvnode._first_char_to_chararray: tnode;
  3385. begin
  3386. result:=first_char_to_chararray;
  3387. end;
  3388. function ttypeconvnode._first_nothing : tnode;
  3389. begin
  3390. result:=first_nothing;
  3391. end;
  3392. function ttypeconvnode._first_array_to_pointer : tnode;
  3393. begin
  3394. result:=first_array_to_pointer;
  3395. end;
  3396. function ttypeconvnode._first_int_to_real : tnode;
  3397. begin
  3398. result:=first_int_to_real;
  3399. end;
  3400. function ttypeconvnode._first_real_to_real : tnode;
  3401. begin
  3402. result:=first_real_to_real;
  3403. end;
  3404. function ttypeconvnode._first_pointer_to_array : tnode;
  3405. begin
  3406. result:=first_pointer_to_array;
  3407. end;
  3408. function ttypeconvnode._first_cchar_to_pchar : tnode;
  3409. begin
  3410. result:=first_cchar_to_pchar;
  3411. end;
  3412. function ttypeconvnode._first_bool_to_int : tnode;
  3413. begin
  3414. result:=first_bool_to_int;
  3415. end;
  3416. function ttypeconvnode._first_int_to_bool : tnode;
  3417. begin
  3418. result:=first_int_to_bool;
  3419. end;
  3420. function ttypeconvnode._first_bool_to_bool : tnode;
  3421. begin
  3422. result:=first_bool_to_bool;
  3423. end;
  3424. function ttypeconvnode._first_proc_to_procvar : tnode;
  3425. begin
  3426. result:=first_proc_to_procvar;
  3427. end;
  3428. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  3429. begin
  3430. result:=first_nil_to_methodprocvar;
  3431. end;
  3432. function ttypeconvnode._first_set_to_set : tnode;
  3433. begin
  3434. result:=first_set_to_set;
  3435. end;
  3436. function ttypeconvnode._first_cord_to_pointer : tnode;
  3437. begin
  3438. result:=first_cord_to_pointer;
  3439. end;
  3440. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  3441. begin
  3442. result:=first_ansistring_to_pchar;
  3443. end;
  3444. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  3445. begin
  3446. result:=first_arrayconstructor_to_set;
  3447. end;
  3448. function ttypeconvnode._first_class_to_intf : tnode;
  3449. begin
  3450. result:=first_class_to_intf;
  3451. end;
  3452. function ttypeconvnode._first_char_to_char : tnode;
  3453. begin
  3454. result:=first_char_to_char;
  3455. end;
  3456. function ttypeconvnode._first_string_to_string : tnode;
  3457. begin
  3458. result:=first_string_to_string;
  3459. end;
  3460. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  3461. const
  3462. firstconvert : array[tconverttype] of pointer = (
  3463. nil, { none }
  3464. @ttypeconvnode._first_nothing, {equal}
  3465. @ttypeconvnode._first_nothing, {not_possible}
  3466. @ttypeconvnode._first_string_to_string,
  3467. @ttypeconvnode._first_char_to_string,
  3468. @ttypeconvnode._first_char_to_chararray,
  3469. nil, { removed in typecheck_chararray_to_string }
  3470. @ttypeconvnode._first_cchar_to_pchar,
  3471. @ttypeconvnode._first_cstring_to_pchar,
  3472. @ttypeconvnode._first_cstring_to_int,
  3473. @ttypeconvnode._first_ansistring_to_pchar,
  3474. @ttypeconvnode._first_string_to_chararray,
  3475. nil, { removed in typecheck_chararray_to_string }
  3476. @ttypeconvnode._first_array_to_pointer,
  3477. @ttypeconvnode._first_pointer_to_array,
  3478. @ttypeconvnode._first_int_to_int,
  3479. @ttypeconvnode._first_int_to_bool,
  3480. @ttypeconvnode._first_bool_to_bool,
  3481. @ttypeconvnode._first_bool_to_int,
  3482. @ttypeconvnode._first_real_to_real,
  3483. @ttypeconvnode._first_int_to_real,
  3484. nil, { removed in typecheck_real_to_currency }
  3485. @ttypeconvnode._first_proc_to_procvar,
  3486. @ttypeconvnode._first_nil_to_methodprocvar,
  3487. @ttypeconvnode._first_arrayconstructor_to_set,
  3488. @ttypeconvnode._first_set_to_set,
  3489. @ttypeconvnode._first_cord_to_pointer,
  3490. @ttypeconvnode._first_nothing,
  3491. @ttypeconvnode._first_nothing,
  3492. @ttypeconvnode._first_class_to_intf,
  3493. @ttypeconvnode._first_char_to_char,
  3494. @ttypeconvnode._first_nothing,
  3495. @ttypeconvnode._first_nothing,
  3496. nil,
  3497. nil,
  3498. nil,
  3499. nil,
  3500. nil,
  3501. nil,
  3502. nil,
  3503. @ttypeconvnode._first_nothing,
  3504. @ttypeconvnode._first_nothing
  3505. );
  3506. type
  3507. tprocedureofobject = function : tnode of object;
  3508. var
  3509. r : packed record
  3510. proc : pointer;
  3511. obj : pointer;
  3512. end;
  3513. begin
  3514. { this is a little bit dirty but it works }
  3515. { and should be quite portable too }
  3516. r.proc:=firstconvert[c];
  3517. r.obj:=self;
  3518. if not assigned(r.proc) then
  3519. internalerror(200312081);
  3520. first_call_helper:=tprocedureofobject(r)()
  3521. end;
  3522. function ttypeconvnode.pass_1 : tnode;
  3523. begin
  3524. if warn_pointer_to_signed then
  3525. cgmessage(type_w_pointer_to_signed);
  3526. result:=nil;
  3527. firstpass(left);
  3528. if codegenerror then
  3529. exit;
  3530. expectloc:=left.expectloc;
  3531. if nf_explicit in flags then
  3532. { check if the result could be in a register }
  3533. if (not(tstoreddef(resultdef).is_intregable) and
  3534. not(tstoreddef(resultdef).is_const_intregable) and
  3535. not(tstoreddef(resultdef).is_fpuregable)) or
  3536. ((left.resultdef.typ = floatdef) and
  3537. (resultdef.typ <> floatdef)) then
  3538. make_not_regable(left,[ra_addr_regable]);
  3539. result:=first_call_helper(convtype);
  3540. end;
  3541. function ttypeconvnode.retains_value_location:boolean;
  3542. begin
  3543. result:=(convtype=tc_equal) or
  3544. { typecasting from void is always allowed }
  3545. is_void(left.resultdef) or
  3546. (left.resultdef.typ=formaldef) or
  3547. { int 2 int with same size reuses same location, or for
  3548. tp7 mode also allow size < orignal size }
  3549. (
  3550. (convtype=tc_int_2_int) and
  3551. (
  3552. not is_bitpacked_access(left) and
  3553. (resultdef.size=left.resultdef.size) or
  3554. ((m_tp7 in current_settings.modeswitches) and
  3555. (resultdef.size<left.resultdef.size))
  3556. )
  3557. ) or
  3558. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  3559. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  3560. (nf_explicit in flags) and
  3561. (resultdef.size=left.resultdef.size)) or
  3562. { on managed platforms, converting an element to an open array
  3563. involves creating an actual array -> value location changes }
  3564. ((convtype=tc_elem_2_openarray) and
  3565. not(target_info.system in systems_managed_vm));
  3566. end;
  3567. function ttypeconvnode.assign_allowed:boolean;
  3568. begin
  3569. result:=retains_value_location;
  3570. { When using only a part of the value it can't be in a register since
  3571. that will load the value in a new register first }
  3572. { the same goes for changing the sign of equal-sized values which
  3573. are smaller than an entire register }
  3574. if result and
  3575. { don't try to check the size of an open array }
  3576. (is_open_array(resultdef) or
  3577. (resultdef.size<left.resultdef.size) or
  3578. ((resultdef.size=left.resultdef.size) and
  3579. (left.resultdef.size<sizeof(aint)) and
  3580. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  3581. make_not_regable(left,[ra_addr_regable]);
  3582. end;
  3583. function ttypeconvnode.docompare(p: tnode) : boolean;
  3584. begin
  3585. docompare :=
  3586. inherited docompare(p) and
  3587. (convtype = ttypeconvnode(p).convtype) and
  3588. (convnodeflags = ttypeconvnode(p).convnodeflags) and
  3589. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  3590. end;
  3591. procedure ttypeconvnode._second_int_to_int;
  3592. begin
  3593. second_int_to_int;
  3594. end;
  3595. procedure ttypeconvnode._second_string_to_string;
  3596. begin
  3597. second_string_to_string;
  3598. end;
  3599. procedure ttypeconvnode._second_cstring_to_pchar;
  3600. begin
  3601. second_cstring_to_pchar;
  3602. end;
  3603. procedure ttypeconvnode._second_cstring_to_int;
  3604. begin
  3605. second_cstring_to_int;
  3606. end;
  3607. procedure ttypeconvnode._second_string_to_chararray;
  3608. begin
  3609. second_string_to_chararray;
  3610. end;
  3611. procedure ttypeconvnode._second_array_to_pointer;
  3612. begin
  3613. second_array_to_pointer;
  3614. end;
  3615. procedure ttypeconvnode._second_pointer_to_array;
  3616. begin
  3617. second_pointer_to_array;
  3618. end;
  3619. procedure ttypeconvnode._second_chararray_to_string;
  3620. begin
  3621. second_chararray_to_string;
  3622. end;
  3623. procedure ttypeconvnode._second_char_to_string;
  3624. begin
  3625. second_char_to_string;
  3626. end;
  3627. procedure ttypeconvnode._second_int_to_real;
  3628. begin
  3629. second_int_to_real;
  3630. end;
  3631. procedure ttypeconvnode._second_real_to_real;
  3632. begin
  3633. second_real_to_real;
  3634. end;
  3635. procedure ttypeconvnode._second_cord_to_pointer;
  3636. begin
  3637. second_cord_to_pointer;
  3638. end;
  3639. procedure ttypeconvnode._second_proc_to_procvar;
  3640. begin
  3641. second_proc_to_procvar;
  3642. end;
  3643. procedure ttypeconvnode._second_nil_to_methodprocvar;
  3644. begin
  3645. second_nil_to_methodprocvar;
  3646. end;
  3647. procedure ttypeconvnode._second_bool_to_int;
  3648. begin
  3649. second_bool_to_int;
  3650. end;
  3651. procedure ttypeconvnode._second_int_to_bool;
  3652. begin
  3653. second_int_to_bool;
  3654. end;
  3655. procedure ttypeconvnode._second_bool_to_bool;
  3656. begin
  3657. second_bool_to_bool;
  3658. end;
  3659. procedure ttypeconvnode._second_set_to_set;
  3660. begin
  3661. second_set_to_set;
  3662. end;
  3663. procedure ttypeconvnode._second_ansistring_to_pchar;
  3664. begin
  3665. second_ansistring_to_pchar;
  3666. end;
  3667. procedure ttypeconvnode._second_class_to_intf;
  3668. begin
  3669. second_class_to_intf;
  3670. end;
  3671. procedure ttypeconvnode._second_char_to_char;
  3672. begin
  3673. second_char_to_char;
  3674. end;
  3675. procedure ttypeconvnode._second_elem_to_openarray;
  3676. begin
  3677. second_elem_to_openarray;
  3678. end;
  3679. procedure ttypeconvnode._second_nothing;
  3680. begin
  3681. second_nothing;
  3682. end;
  3683. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  3684. const
  3685. secondconvert : array[tconverttype] of pointer = (
  3686. @ttypeconvnode._second_nothing, {none}
  3687. @ttypeconvnode._second_nothing, {equal}
  3688. @ttypeconvnode._second_nothing, {not_possible}
  3689. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  3690. @ttypeconvnode._second_char_to_string,
  3691. @ttypeconvnode._second_nothing, {char_to_charray}
  3692. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  3693. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  3694. @ttypeconvnode._second_cstring_to_pchar,
  3695. @ttypeconvnode._second_cstring_to_int,
  3696. @ttypeconvnode._second_ansistring_to_pchar,
  3697. @ttypeconvnode._second_string_to_chararray,
  3698. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  3699. @ttypeconvnode._second_array_to_pointer,
  3700. @ttypeconvnode._second_pointer_to_array,
  3701. @ttypeconvnode._second_int_to_int,
  3702. @ttypeconvnode._second_int_to_bool,
  3703. @ttypeconvnode._second_bool_to_bool,
  3704. @ttypeconvnode._second_bool_to_int,
  3705. @ttypeconvnode._second_real_to_real,
  3706. @ttypeconvnode._second_int_to_real,
  3707. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  3708. @ttypeconvnode._second_proc_to_procvar,
  3709. @ttypeconvnode._second_nil_to_methodprocvar,
  3710. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  3711. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  3712. @ttypeconvnode._second_cord_to_pointer,
  3713. @ttypeconvnode._second_nothing, { interface 2 string }
  3714. @ttypeconvnode._second_nothing, { interface 2 guid }
  3715. @ttypeconvnode._second_class_to_intf,
  3716. @ttypeconvnode._second_char_to_char,
  3717. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  3718. @ttypeconvnode._second_nothing, { pwchar_2_string }
  3719. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  3720. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  3721. @ttypeconvnode._second_nothing, { variant_2_enum }
  3722. @ttypeconvnode._second_nothing, { enum_2_variant }
  3723. @ttypeconvnode._second_nothing, { variant_2_interface }
  3724. @ttypeconvnode._second_nothing, { interface_2_variant }
  3725. @ttypeconvnode._second_nothing, { array_2_dynarray }
  3726. @ttypeconvnode._second_elem_to_openarray, { elem_2_openarray }
  3727. @ttypeconvnode._second_nothing { arrayconstructor_2_dynarray }
  3728. );
  3729. type
  3730. tprocedureofobject = procedure of object;
  3731. var
  3732. r : packed record
  3733. proc : pointer;
  3734. obj : pointer;
  3735. end;
  3736. begin
  3737. { this is a little bit dirty but it works }
  3738. { and should be quite portable too }
  3739. r.proc:=secondconvert[c];
  3740. r.obj:=self;
  3741. tprocedureofobject(r)();
  3742. end;
  3743. {*****************************************************************************
  3744. TASNODE
  3745. *****************************************************************************}
  3746. function tasisnode.target_specific_typecheck: boolean;
  3747. begin
  3748. result:=false;
  3749. end;
  3750. function tasisnode.pass_typecheck: tnode;
  3751. var
  3752. hp : tnode;
  3753. begin
  3754. result:=nil;
  3755. typecheckpass(right);
  3756. typecheckpass(left);
  3757. set_varstate(right,vs_read,[vsf_must_be_valid]);
  3758. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3759. if codegenerror then
  3760. exit;
  3761. if target_specific_typecheck then
  3762. begin
  3763. // ok
  3764. end
  3765. else if (right.resultdef.typ=classrefdef) then
  3766. begin
  3767. { left maybe an interface reference }
  3768. if is_interfacecom(left.resultdef) or
  3769. is_javainterface(left.resultdef) then
  3770. begin
  3771. { relation checks are not possible }
  3772. end
  3773. { or left must be a class }
  3774. else if is_class(left.resultdef) or
  3775. is_javaclass(left.resultdef) then
  3776. begin
  3777. { the operands must be related }
  3778. if (not(def_is_related(tobjectdef(left.resultdef),
  3779. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  3780. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  3781. tobjectdef(left.resultdef)))) then
  3782. CGMessage2(type_e_classes_not_related,
  3783. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  3784. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  3785. end
  3786. else
  3787. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3788. case nodetype of
  3789. isn:
  3790. resultdef:=pasbool1type;
  3791. asn:
  3792. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  3793. end;
  3794. end
  3795. else if is_interface(right.resultdef) or
  3796. is_dispinterface(right.resultdef) or
  3797. is_javainterface(right.resultdef) then
  3798. begin
  3799. case nodetype of
  3800. isn:
  3801. resultdef:=pasbool1type;
  3802. asn:
  3803. resultdef:=right.resultdef;
  3804. end;
  3805. { left is a class or interface }
  3806. if is_javainterface(right.resultdef) then
  3807. begin
  3808. if not is_java_class_or_interface(left.resultdef) then
  3809. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  3810. end
  3811. else if not(is_class(left.resultdef) or
  3812. is_interfacecom(left.resultdef)) then
  3813. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  3814. else
  3815. begin
  3816. { load the GUID of the interface }
  3817. if (right.nodetype=typen) then
  3818. begin
  3819. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  3820. begin
  3821. if assigned(tobjectdef(right.resultdef).iidstr) then
  3822. begin
  3823. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  3824. tstringconstnode(hp).changestringtype(cshortstringtype);
  3825. right.free;
  3826. right:=hp;
  3827. end
  3828. else
  3829. internalerror(201006131);
  3830. end
  3831. else
  3832. begin
  3833. if assigned(tobjectdef(right.resultdef).iidguid) then
  3834. begin
  3835. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  3836. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  3837. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  3838. right.free;
  3839. right:=hp;
  3840. end
  3841. else
  3842. internalerror(201006132);
  3843. end;
  3844. typecheckpass(right);
  3845. end;
  3846. end;
  3847. end
  3848. else
  3849. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  3850. end;
  3851. {*****************************************************************************
  3852. TISNODE
  3853. *****************************************************************************}
  3854. constructor tisnode.create(l,r : tnode);
  3855. begin
  3856. inherited create(isn,l,r);
  3857. end;
  3858. constructor tisnode.create_internal(l, r: tnode);
  3859. begin
  3860. create(l,r);
  3861. include(flags,nf_internal);
  3862. end;
  3863. function tisnode.pass_1 : tnode;
  3864. var
  3865. procname: string;
  3866. statement : tstatementnode;
  3867. tempnode : ttempcreatenode;
  3868. begin
  3869. result:=nil;
  3870. { Passing a class type to an "is" expression cannot result in a class
  3871. of that type to be constructed.
  3872. }
  3873. include(right.flags,nf_ignore_for_wpo);
  3874. if is_class(left.resultdef) and
  3875. (right.resultdef.typ=classrefdef) then
  3876. begin
  3877. if (right.nodetype=loadvmtaddrn) and
  3878. (tloadvmtaddrnode(right).left.nodetype=typen) and
  3879. (oo_is_sealed in tobjectdef(tloadvmtaddrnode(right).left.resultdef).objectoptions) and
  3880. equal_defs(left.resultdef,tclassrefdef(right.resultdef).pointeddef) then
  3881. begin
  3882. if might_have_sideeffects(left) or
  3883. (node_complexity(left)>2) then
  3884. begin
  3885. result:=internalstatements(statement);
  3886. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  3887. addstatement(statement,tempnode);
  3888. addstatement(statement,cassignmentnode.create_internal(ctemprefnode.create(tempnode),left));
  3889. addstatement(statement,caddnode.create_internal(andn,
  3890. caddnode.create_internal(unequaln,ctemprefnode.create(tempnode),cnilnode.create),
  3891. caddnode.create_internal(equaln,cloadvmtaddrnode.create(ctemprefnode.create(tempnode)),right)
  3892. )
  3893. );
  3894. left:=nil;
  3895. right:=nil;
  3896. end
  3897. else
  3898. begin
  3899. result:=caddnode.create_internal(andn,
  3900. caddnode.create_internal(unequaln,left.getcopy,cnilnode.create),
  3901. caddnode.create_internal(equaln,cloadvmtaddrnode.create(left.getcopy),right)
  3902. );
  3903. right:=nil;
  3904. end;
  3905. end
  3906. else
  3907. result := ccallnode.createinternres('fpc_do_is',
  3908. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3909. resultdef);
  3910. end
  3911. else
  3912. begin
  3913. if is_class(left.resultdef) then
  3914. if is_shortstring(right.resultdef) then
  3915. procname := 'fpc_class_is_corbaintf'
  3916. else
  3917. procname := 'fpc_class_is_intf'
  3918. else
  3919. if right.resultdef.typ=classrefdef then
  3920. procname := 'fpc_intf_is_class'
  3921. else
  3922. procname := 'fpc_intf_is';
  3923. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  3924. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  3925. end;
  3926. left := nil;
  3927. right := nil;
  3928. //firstpass(call);
  3929. if codegenerror then
  3930. exit;
  3931. end;
  3932. { dummy pass_2, it will never be called, but we need one since }
  3933. { you can't instantiate an abstract class }
  3934. procedure tisnode.pass_generate_code;
  3935. begin
  3936. end;
  3937. {*****************************************************************************
  3938. TASNODE
  3939. *****************************************************************************}
  3940. constructor tasnode.create(l,r : tnode);
  3941. begin
  3942. inherited create(asn,l,r);
  3943. call := nil;
  3944. end;
  3945. constructor tasnode.create_internal(l,r : tnode);
  3946. begin
  3947. create(l,r);
  3948. include(flags,nf_internal);
  3949. end;
  3950. destructor tasnode.destroy;
  3951. begin
  3952. call.free;
  3953. inherited destroy;
  3954. end;
  3955. function tasnode.dogetcopy: tnode;
  3956. begin
  3957. result := inherited dogetcopy;
  3958. if assigned(call) then
  3959. tasnode(result).call := call.getcopy
  3960. else
  3961. tasnode(result).call := nil;
  3962. end;
  3963. function tasnode.docompare(p: tnode): boolean;
  3964. begin
  3965. result:=
  3966. inherited docompare(p) and
  3967. tasnode(p).call.isequal(call);
  3968. end;
  3969. function tasnode.pass_1 : tnode;
  3970. var
  3971. procname: string;
  3972. begin
  3973. result:=nil;
  3974. { Passing a class type to an "as" expression cannot result in a class
  3975. of that type to be constructed.
  3976. We could put this inside the if-block below, but this way it is
  3977. safe for sure even if the code below changes
  3978. }
  3979. if assigned(right) then
  3980. include(right.flags,nf_ignore_for_wpo);
  3981. if not assigned(call) then
  3982. begin
  3983. if is_class(left.resultdef) and
  3984. (right.resultdef.typ=classrefdef) then
  3985. call := ccallnode.createinternres('fpc_do_as',
  3986. ccallparanode.create(left,ccallparanode.create(right,nil)),
  3987. resultdef)
  3988. else
  3989. begin
  3990. if is_class(left.resultdef) then
  3991. if is_shortstring(right.resultdef) then
  3992. procname := 'fpc_class_as_corbaintf'
  3993. else
  3994. procname := 'fpc_class_as_intf'
  3995. else
  3996. if right.resultdef.typ=classrefdef then
  3997. procname := 'fpc_intf_as_class'
  3998. else
  3999. procname := 'fpc_intf_as';
  4000. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4001. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4002. end;
  4003. left := nil;
  4004. right := nil;
  4005. firstpass(call);
  4006. if codegenerror then
  4007. exit;
  4008. expectloc:=call.expectloc;
  4009. end;
  4010. end;
  4011. end.