ncnv.pas 200 KB

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