ncnv.pas 195 KB

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