ncnv.pas 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148
  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 not((current_settings.sourcecodepage=CP_UTF8) or
  1181. ((tstringdef(resultdef).stringtype=st_ansistring) and
  1182. (tstringdef(resultdef).encoding=CP_UTF8))) then
  1183. begin
  1184. if tordconstnode(left).value.uvalue>127 then
  1185. begin
  1186. Message(type_w_unicode_data_loss);
  1187. // compiler has different codepage than a system running an application
  1188. // to prevent wrong codepage and data loss we are converting unicode char
  1189. // using a helper routine. This is not delphi compatible behavior.
  1190. // Delphi converts UniocodeChar to ansistring at the compile time
  1191. // old behavior:
  1192. // hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)));
  1193. para:=ccallparanode.create(left,nil);
  1194. if tstringdef(resultdef).stringtype=st_ansistring then
  1195. para:=ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),para);
  1196. result:=ccallnode.createinternres('fpc_uchar_to_'+tstringdef(resultdef).stringtypname,
  1197. para,resultdef);
  1198. left:=nil;
  1199. exit;
  1200. end
  1201. else
  1202. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)));
  1203. end
  1204. else
  1205. begin
  1206. cw:=tcompilerwidechar(tordconstnode(left).value.uvalue);
  1207. SetLength(sa,5);
  1208. l:=UnicodeToUtf8(@(sa[1]),Length(sa),@cw,1);
  1209. SetLength(sa,l-1);
  1210. hp:=cstringconstnode.createstr(sa);
  1211. { explicitly set the type of string constant to avoid unnecessary conversion }
  1212. if (tstringdef(resultdef).stringtype=st_ansistring) and
  1213. (tstringdef(resultdef).encoding=CP_UTF8) then
  1214. begin
  1215. hp.cst_type:=cst_ansistring;
  1216. hp.resultdef:=resultdef;
  1217. end;
  1218. end
  1219. end
  1220. else
  1221. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  1222. { output string consts in local ansistring encoding }
  1223. if is_ansistring(resultdef) and ((tstringdef(resultdef).encoding=0) or (tstringdef(resultdef).encoding=globals.CP_NONE)) then
  1224. tstringconstnode(hp).changestringtype(getansistringdef)
  1225. else
  1226. tstringconstnode(hp).changestringtype(resultdef);
  1227. end;
  1228. result:=hp;
  1229. end
  1230. else
  1231. { shortstrings are handled 'inline' (except for widechars) }
  1232. if (tstringdef(resultdef).stringtype<>st_shortstring) or
  1233. (torddef(left.resultdef).ordtype=uwidechar) or
  1234. (target_info.system in systems_managed_vm) then
  1235. begin
  1236. { parameter }
  1237. para:=ccallparanode.create(left,nil);
  1238. { encoding required? }
  1239. if tstringdef(resultdef).stringtype=st_ansistring then
  1240. para:=ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),para);
  1241. { create the procname }
  1242. if torddef(left.resultdef).ordtype<>uwidechar then
  1243. begin
  1244. procname:='fpc_char_to_';
  1245. if tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring] then
  1246. if nf_explicit in flags then
  1247. Message2(type_w_explicit_string_cast,left.resultdef.typename,resultdef.typename)
  1248. else
  1249. Message2(type_w_implicit_string_cast,left.resultdef.typename,resultdef.typename);
  1250. end
  1251. else
  1252. begin
  1253. procname:='fpc_uchar_to_';
  1254. if not (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring]) then
  1255. if nf_explicit in flags then
  1256. Message2(type_w_explicit_string_cast_loss,left.resultdef.typename,resultdef.typename)
  1257. else
  1258. Message2(type_w_implicit_string_cast_loss,left.resultdef.typename,resultdef.typename);
  1259. end;
  1260. procname:=procname+tstringdef(resultdef).stringtypname;
  1261. { and finally the call }
  1262. result:=ccallnode.createinternres(procname,para,resultdef);
  1263. left := nil;
  1264. end
  1265. else
  1266. begin
  1267. { use at least u16inttype }
  1268. {$ifdef cpu8bitalu}
  1269. exprtype:=u16inttype;
  1270. {$else cpu8bitalu}
  1271. exprtype:=uinttype;
  1272. {$endif cpu8bitalu}
  1273. { create word(byte(char) shl 8 or 1) for litte endian machines }
  1274. { and word(byte(char) or 256) for big endian machines }
  1275. left := ctypeconvnode.create_internal(left,exprtype);
  1276. if (target_info.endian = endian_little) then
  1277. left := caddnode.create(orn,
  1278. cshlshrnode.create(shln,left,cordconstnode.create(8,exprtype,false)),
  1279. cordconstnode.create(1,exprtype,false))
  1280. else
  1281. left := caddnode.create(orn,left,
  1282. cordconstnode.create(1 shl 8,exprtype,false));
  1283. left := ctypeconvnode.create_internal(left,u16inttype);
  1284. typecheckpass(left);
  1285. end;
  1286. end;
  1287. function ttypeconvnode.typecheck_string_to_string : tnode;
  1288. begin
  1289. result:=nil;
  1290. if (left.nodetype=stringconstn) and
  1291. (((tstringdef(resultdef).stringtype=st_ansistring) and
  1292. (tstringdef(resultdef).encoding<>CP_NONE)
  1293. )
  1294. ) and
  1295. (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1296. begin
  1297. tstringconstnode(left).changestringtype(resultdef);
  1298. Result:=left;
  1299. left:=nil;
  1300. end
  1301. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  1302. (tstringdef(left.resultdef).stringtype=st_ansistring) and
  1303. (tstringdef(resultdef).encoding<>tstringdef(left.resultdef).encoding) then
  1304. begin
  1305. result:=ccallnode.createinternres(
  1306. 'fpc_ansistr_to_ansistr',
  1307. ccallparanode.create(
  1308. cordconstnode.create(
  1309. tstringdef(resultdef).encoding,
  1310. u16inttype,
  1311. true
  1312. ),
  1313. ccallparanode.create(left,nil)
  1314. ),
  1315. resultdef
  1316. );
  1317. left:=nil;
  1318. end
  1319. else if (left.nodetype=stringconstn) and
  1320. (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1321. (tstringdef(resultdef).stringtype=st_shortstring) then
  1322. begin
  1323. if not hasnonasciichars(pcompilerwidestring(tstringconstnode(left).value_str)) then
  1324. begin
  1325. tstringconstnode(left).changestringtype(resultdef);
  1326. Result:=left;
  1327. left:=nil;
  1328. end;
  1329. end
  1330. else if (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1331. not (tstringdef(resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1332. begin
  1333. if nf_explicit in flags then
  1334. Message2(type_w_explicit_string_cast_loss,left.resultdef.typename,resultdef.typename)
  1335. else
  1336. Message2(type_w_implicit_string_cast_loss,left.resultdef.typename,resultdef.typename);
  1337. end
  1338. else if not (tstringdef(left.resultdef).stringtype in [st_unicodestring,st_widestring]) and
  1339. (tstringdef(resultdef).stringtype in [st_unicodestring,st_widestring]) then
  1340. begin
  1341. if nf_explicit in flags then
  1342. Message2(type_w_explicit_string_cast,left.resultdef.typename,resultdef.typename)
  1343. else
  1344. Message2(type_w_implicit_string_cast,left.resultdef.typename,resultdef.typename);
  1345. end
  1346. end;
  1347. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  1348. begin
  1349. result:=nil;
  1350. end;
  1351. function ttypeconvnode.typecheck_char_to_char : tnode;
  1352. var
  1353. hp : tordconstnode;
  1354. begin
  1355. result:=nil;
  1356. if (left.nodetype=ordconstn) and
  1357. ((torddef(resultdef).ordtype<>uchar) or
  1358. (torddef(left.resultdef).ordtype<>uwidechar) or
  1359. (current_settings.sourcecodepage<>CP_UTF8))
  1360. then
  1361. begin
  1362. if (torddef(resultdef).ordtype=uchar) and
  1363. (torddef(left.resultdef).ordtype=uwidechar) and
  1364. (current_settings.sourcecodepage<>CP_UTF8) then
  1365. begin
  1366. if tordconstnode(left).value.uvalue>127 then
  1367. Message(type_w_unicode_data_loss);
  1368. hp:=cordconstnode.create(
  1369. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  1370. cansichartype,true);
  1371. result:=hp;
  1372. end
  1373. else if (torddef(resultdef).ordtype=uwidechar) and
  1374. (torddef(left.resultdef).ordtype=uchar) then
  1375. begin
  1376. hp:=cordconstnode.create(
  1377. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  1378. cwidechartype,true);
  1379. result:=hp;
  1380. end
  1381. else
  1382. internalerror(200105131);
  1383. exit;
  1384. end;
  1385. end;
  1386. function ttypeconvnode.typecheck_int_to_int : tnode;
  1387. var
  1388. v : TConstExprInt;
  1389. begin
  1390. result:=nil;
  1391. if left.nodetype=ordconstn then
  1392. begin
  1393. v:=tordconstnode(left).value;
  1394. if is_currency(resultdef) and
  1395. not(nf_internal in flags) then
  1396. v:=v*10000;
  1397. if (resultdef.typ=pointerdef) then
  1398. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1399. else
  1400. begin
  1401. if is_currency(left.resultdef) then
  1402. begin
  1403. if not(nf_internal in flags) then
  1404. v:=v div 10000;
  1405. end
  1406. else if (resultdef.typ in [orddef,enumdef]) then
  1407. adaptrange(resultdef,v,([nf_internal,nf_absolute]*flags)<>[],nf_explicit in flags,cs_check_range in localswitches);
  1408. result:=cordconstnode.create(v,resultdef,false);
  1409. end;
  1410. end
  1411. else if left.nodetype=pointerconstn then
  1412. begin
  1413. v:=tpointerconstnode(left).value;
  1414. if (resultdef.typ=pointerdef) then
  1415. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1416. else
  1417. begin
  1418. if is_currency(resultdef) and
  1419. not(nf_internal in flags) then
  1420. v:=v*10000;
  1421. result:=cordconstnode.create(v,resultdef,false);
  1422. end;
  1423. end
  1424. else
  1425. begin
  1426. if (is_currency(resultdef) or
  1427. is_currency(left.resultdef)) and
  1428. (nf_internal in flags) then
  1429. begin
  1430. include(flags,nf_is_currency)
  1431. end
  1432. { multiply by 10000 for currency. We need to use getcopy to pass
  1433. the argument because the current node is always disposed. Only
  1434. inserting the multiply in the left node is not possible because
  1435. it'll get in an infinite loop to convert int->currency }
  1436. else if is_currency(resultdef) then
  1437. begin
  1438. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1439. include(result.flags,nf_is_currency);
  1440. include(taddnode(result).left.flags,nf_internal);
  1441. end
  1442. else if is_currency(left.resultdef) then
  1443. begin
  1444. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1445. include(result.flags,nf_is_currency);
  1446. include(tmoddivnode(result).left.flags,nf_internal);
  1447. end;
  1448. end;
  1449. end;
  1450. function ttypeconvnode.typecheck_int_to_real : tnode;
  1451. var
  1452. rv : bestreal;
  1453. begin
  1454. result:=nil;
  1455. if left.nodetype=ordconstn then
  1456. begin
  1457. rv:=tordconstnode(left).value;
  1458. if is_currency(resultdef) and
  1459. not(nf_internal in flags) then
  1460. rv:=rv*10000.0
  1461. else if is_currency(left.resultdef) and
  1462. not(nf_internal in flags) then
  1463. rv:=rv/10000.0;
  1464. result:=crealconstnode.create(rv,resultdef);
  1465. end
  1466. else
  1467. begin
  1468. if (is_currency(resultdef) or
  1469. is_currency(left.resultdef)) and
  1470. (nf_internal in flags) then
  1471. begin
  1472. include(flags,nf_is_currency)
  1473. end
  1474. { multiply by 10000 for currency. We need to use getcopy to pass
  1475. the argument because the current node is always disposed. Only
  1476. inserting the multiply in the left node is not possible because
  1477. it'll get in an infinite loop to convert int->currency }
  1478. else if is_currency(resultdef) then
  1479. begin
  1480. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1481. include(result.flags,nf_is_currency);
  1482. end
  1483. else if is_currency(left.resultdef) then
  1484. begin
  1485. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1486. include(result.flags,nf_is_currency);
  1487. end;
  1488. end;
  1489. end;
  1490. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1491. begin
  1492. if not is_currency(resultdef) then
  1493. internalerror(200304221);
  1494. result:=nil;
  1495. if not(nf_internal in flags) then
  1496. begin
  1497. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1498. include(left.flags,nf_is_currency);
  1499. { Convert constants directly, else call Round() }
  1500. if left.nodetype=realconstn then
  1501. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1502. else
  1503. begin
  1504. result:=cinlinenode.create(in_round_real,false,left);
  1505. { Internal type cast to currency }
  1506. result:=ctypeconvnode.create_internal(result,s64currencytype);
  1507. left:=nil;
  1508. end
  1509. end
  1510. else
  1511. begin
  1512. include(left.flags,nf_is_currency);
  1513. result:=left;
  1514. left:=nil;
  1515. end;
  1516. end;
  1517. function ttypeconvnode.typecheck_real_to_real : tnode;
  1518. begin
  1519. result:=nil;
  1520. if not(nf_internal in flags) then
  1521. begin
  1522. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1523. begin
  1524. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1525. include(left.flags,nf_is_currency);
  1526. typecheckpass(left);
  1527. end
  1528. else
  1529. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1530. begin
  1531. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1532. include(left.flags,nf_is_currency);
  1533. include(flags,nf_is_currency);
  1534. typecheckpass(left);
  1535. end;
  1536. { comp is handled by the fpu but not a floating type point }
  1537. if is_fpucomp(resultdef) and not(is_fpucomp(left.resultdef)) and
  1538. not (nf_explicit in flags) then
  1539. Message(type_w_convert_real_2_comp);
  1540. end
  1541. else
  1542. include(flags,nf_is_currency);
  1543. end;
  1544. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1545. begin
  1546. result:=nil;
  1547. { handle any constants via cunicodestringtype because the compiler
  1548. cannot convert arbitrary unicodechar constants at compile time to
  1549. a shortstring (since it doesn't know the code page to use) }
  1550. inserttypeconv(left,cunicodestringtype);
  1551. { evaluate again, reset resultdef so the convert_typ
  1552. will be calculated again and cstring_to_pchar will
  1553. be used for futher conversion }
  1554. convtype:=tc_none;
  1555. result:=pass_typecheck;
  1556. end;
  1557. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1558. begin
  1559. result:=nil;
  1560. if is_pwidechar(resultdef) then
  1561. inserttypeconv(left,cunicodestringtype)
  1562. else
  1563. if is_pchar(resultdef) and
  1564. (is_widestring(left.resultdef) or
  1565. is_unicodestring(left.resultdef)) then
  1566. begin
  1567. inserttypeconv(left,getansistringdef);
  1568. { the second pass of second_cstring_to_pchar expects a }
  1569. { strinconstn, but this may become a call to the }
  1570. { widestring manager in case left contains "high ascii" }
  1571. if (left.nodetype<>stringconstn) then
  1572. begin
  1573. result:=left;
  1574. left:=nil;
  1575. end;
  1576. end;
  1577. end;
  1578. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1579. var
  1580. fcc : cardinal;
  1581. pb : pbyte;
  1582. begin
  1583. result:=nil;
  1584. if left.nodetype<>stringconstn then
  1585. internalerror(200510012);
  1586. if (m_mac in current_settings.modeswitches) and
  1587. is_integer(resultdef) and
  1588. (tstringconstnode(left).cst_type=cst_conststring) and
  1589. (tstringconstnode(left).len=4) then
  1590. begin
  1591. pb:=pbyte(tstringconstnode(left).value_str);
  1592. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1593. result:=cordconstnode.create(fcc,u32inttype,false);
  1594. end
  1595. else
  1596. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  1597. end;
  1598. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1599. var
  1600. hp : tnode;
  1601. begin
  1602. result:=nil;
  1603. if left.nodetype<>arrayconstructorn then
  1604. internalerror(5546);
  1605. { remove typeconv node }
  1606. hp:=left;
  1607. left:=nil;
  1608. { create a set constructor tree }
  1609. arrayconstructor_to_set(hp);
  1610. if is_emptyset(hp) then
  1611. begin
  1612. { enforce the result type for an empty set }
  1613. hp.resultdef:=resultdef;
  1614. result:=hp;
  1615. end
  1616. else if hp.resultdef<>resultdef then
  1617. begin
  1618. { the set might contain a subrange element (e.g. through a variable),
  1619. thus we need to insert another type conversion }
  1620. if nf_explicit in flags then
  1621. result:=ctypeconvnode.create_explicit(hp,totypedef)
  1622. else if nf_internal in flags then
  1623. result:=ctypeconvnode.create_internal(hp,totypedef)
  1624. else
  1625. result:=ctypeconvnode.create(hp,totypedef);
  1626. end
  1627. else
  1628. result:=hp;
  1629. end;
  1630. function ttypeconvnode.typecheck_set_to_set : tnode;
  1631. begin
  1632. result:=nil;
  1633. { constant sets can be converted by changing the type only }
  1634. if (left.nodetype=setconstn) then
  1635. begin
  1636. left.resultdef:=resultdef;
  1637. result:=left;
  1638. left:=nil;
  1639. exit;
  1640. end;
  1641. end;
  1642. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1643. var
  1644. newblock : tblocknode;
  1645. newstat : tstatementnode;
  1646. restemp : ttempcreatenode;
  1647. begin
  1648. if tstringdef(resultdef).stringtype=st_shortstring then
  1649. begin
  1650. newblock:=internalstatements(newstat);
  1651. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1652. addstatement(newstat,restemp);
  1653. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1654. ctemprefnode.create(restemp),nil))));
  1655. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1656. addstatement(newstat,ctemprefnode.create(restemp));
  1657. result:=newblock;
  1658. end
  1659. else if tstringdef(resultdef).stringtype=st_ansistring then
  1660. result := ccallnode.createinternres(
  1661. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1662. ccallparanode.create(
  1663. cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  1664. ccallparanode.create(left,nil)
  1665. ),
  1666. resultdef
  1667. )
  1668. else
  1669. result := ccallnode.createinternres(
  1670. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1671. ccallparanode.create(left,nil),resultdef);
  1672. left:=nil;
  1673. end;
  1674. function ttypeconvnode.typecheck_interface_to_string : tnode;
  1675. begin
  1676. if assigned(tobjectdef(left.resultdef).iidstr) then
  1677. begin
  1678. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1679. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1680. result:=cstringconstnode.createstr(tobjectdef(left.resultdef).iidstr^);
  1681. tstringconstnode(result).changestringtype(cshortstringtype);
  1682. end
  1683. else
  1684. internalerror(2013112913);
  1685. end;
  1686. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1687. begin
  1688. if assigned(tobjectdef(left.resultdef).iidguid) then
  1689. begin
  1690. if not(oo_has_valid_guid in tobjectdef(left.resultdef).objectoptions) then
  1691. CGMessage1(type_e_interface_has_no_guid,tobjectdef(left.resultdef).typename);
  1692. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1693. end
  1694. else
  1695. internalerror(2013112914);
  1696. end;
  1697. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1698. begin
  1699. if (actualtargetnode(@left)^.nodetype in [pointerconstn,niln]) then
  1700. CGMessage(type_e_no_addr_of_constant);
  1701. { a dynamic array is a pointer to an array, so to convert it to }
  1702. { an open array, we have to dereference it (JM) }
  1703. result := ctypeconvnode.create_internal(left,cpointerdef.getreusable(resultdef));
  1704. typecheckpass(result);
  1705. { left is reused }
  1706. left := nil;
  1707. result := cderefnode.create(result);
  1708. include(result.flags,nf_no_checkpointer);
  1709. end;
  1710. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1711. var
  1712. newblock : tblocknode;
  1713. newstat : tstatementnode;
  1714. restemp : ttempcreatenode;
  1715. begin
  1716. if tstringdef(resultdef).stringtype=st_shortstring then
  1717. begin
  1718. newblock:=internalstatements(newstat);
  1719. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1720. addstatement(newstat,restemp);
  1721. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1722. ctemprefnode.create(restemp),nil))));
  1723. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1724. addstatement(newstat,ctemprefnode.create(restemp));
  1725. result:=newblock;
  1726. end
  1727. else if tstringdef(resultdef).stringtype=st_ansistring then
  1728. begin
  1729. result:=ccallnode.createinternres(
  1730. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1731. ccallparanode.create(
  1732. cordconstnode.create(
  1733. getparaencoding(resultdef),
  1734. u16inttype,
  1735. true
  1736. ),
  1737. ccallparanode.create(left,nil)
  1738. ),
  1739. resultdef
  1740. );
  1741. end
  1742. else
  1743. result := ccallnode.createinternres(
  1744. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1745. ccallparanode.create(left,nil),resultdef);
  1746. left:=nil;
  1747. end;
  1748. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1749. begin
  1750. result := ccallnode.createinternres(
  1751. 'fpc_variant_to_dynarray',
  1752. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1753. ccallparanode.create(left,nil)
  1754. ),resultdef);
  1755. typecheckpass(result);
  1756. left:=nil;
  1757. end;
  1758. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1759. begin
  1760. result := ccallnode.createinternres(
  1761. 'fpc_dynarray_to_variant',
  1762. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1763. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1764. ),resultdef);
  1765. typecheckpass(result);
  1766. left:=nil;
  1767. end;
  1768. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1769. begin
  1770. if def_is_related(tobjectdef(resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1771. result := ccallnode.createinternres(
  1772. 'fpc_variant_to_idispatch',
  1773. ccallparanode.create(left,nil)
  1774. ,resultdef)
  1775. else
  1776. result := ccallnode.createinternres(
  1777. 'fpc_variant_to_interface',
  1778. ccallparanode.create(left,nil)
  1779. ,resultdef);
  1780. typecheckpass(result);
  1781. left:=nil;
  1782. end;
  1783. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1784. begin
  1785. if def_is_related(tobjectdef(left.resultdef),tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1786. result := ccallnode.createinternres(
  1787. 'fpc_idispatch_to_variant',
  1788. ccallparanode.create(left,nil)
  1789. ,resultdef)
  1790. else
  1791. result := ccallnode.createinternres(
  1792. 'fpc_interface_to_variant',
  1793. ccallparanode.create(left,nil)
  1794. ,resultdef);
  1795. typecheckpass(result);
  1796. left:=nil;
  1797. end;
  1798. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1799. begin
  1800. result := ctypeconvnode.create_internal(left,sinttype);
  1801. result := ctypeconvnode.create_internal(result,resultdef);
  1802. typecheckpass(result);
  1803. { left is reused }
  1804. left := nil;
  1805. end;
  1806. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1807. begin
  1808. result := ctypeconvnode.create_internal(left,sinttype);
  1809. result := ctypeconvnode.create_internal(result,cvarianttype);
  1810. typecheckpass(result);
  1811. { left is reused }
  1812. left := nil;
  1813. end;
  1814. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1815. var
  1816. newstatement : tstatementnode;
  1817. temp : ttempcreatenode;
  1818. temp2 : ttempcreatenode;
  1819. begin
  1820. { create statements with call to getmem+initialize }
  1821. result:=internalstatements(newstatement);
  1822. { create temp for result }
  1823. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1824. addstatement(newstatement,temp);
  1825. { get temp for array of lengths }
  1826. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1827. addstatement(newstatement,temp2);
  1828. { one dimensional }
  1829. addstatement(newstatement,cassignmentnode.create(
  1830. ctemprefnode.create(temp2),
  1831. cordconstnode.create
  1832. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1833. { create call to fpc_dynarr_setlength }
  1834. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1835. ccallparanode.create(caddrnode.create_internal
  1836. (ctemprefnode.create(temp2)),
  1837. ccallparanode.create(cordconstnode.create
  1838. (1,s32inttype,true),
  1839. ccallparanode.create(caddrnode.create_internal
  1840. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1841. ccallparanode.create(
  1842. ctypeconvnode.create_internal(
  1843. ctemprefnode.create(temp),voidpointertype),
  1844. nil))))
  1845. ));
  1846. addstatement(newstatement,ctempdeletenode.create(temp2));
  1847. { copy ... }
  1848. addstatement(newstatement,cassignmentnode.create(
  1849. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1850. left
  1851. ));
  1852. { left is reused }
  1853. left:=nil;
  1854. { the last statement should return the value as
  1855. location and type, this is done be referencing the
  1856. temp and converting it first from a persistent temp to
  1857. normal temp }
  1858. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1859. addstatement(newstatement,ctemprefnode.create(temp));
  1860. end;
  1861. function ttypeconvnode.typecheck_elem_2_openarray : tnode;
  1862. begin
  1863. result:=nil;
  1864. end;
  1865. function ttypeconvnode.typecheck_arrayconstructor_to_dynarray : tnode;
  1866. var
  1867. newstatement,
  1868. assstatement : tstatementnode;
  1869. arrnode : ttempcreatenode;
  1870. temp2 : ttempcreatenode;
  1871. assnode : tnode;
  1872. paracount : integer;
  1873. elemnode : tarrayconstructornode;
  1874. begin
  1875. { assignment of []? }
  1876. if (
  1877. (left.nodetype=arrayconstructorn) and
  1878. not assigned(tarrayconstructornode(left).left)
  1879. ) or
  1880. is_emptyset(left)
  1881. then
  1882. begin
  1883. result:=cnilnode.create;
  1884. exit;
  1885. end;
  1886. if resultdef.typ<>arraydef then
  1887. internalerror(2017050102);
  1888. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1889. result:=internalstatements(newstatement);
  1890. { create temp for result }
  1891. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1892. addstatement(newstatement,arrnode);
  1893. paracount:=0;
  1894. { create an assignment call for each element }
  1895. assnode:=internalstatements(assstatement);
  1896. if left.nodetype=arrayconstructorrangen then
  1897. internalerror(2016021902);
  1898. elemnode:=tarrayconstructornode(left);
  1899. while assigned(elemnode) do
  1900. begin
  1901. { arr[i] := param_i }
  1902. if not assigned(elemnode.left) then
  1903. internalerror(2017050103);
  1904. addstatement(assstatement,
  1905. cassignmentnode.create(
  1906. cvecnode.create(
  1907. ctemprefnode.create(arrnode),
  1908. cordconstnode.create(paracount,tarraydef(totypedef).rangedef,false)),
  1909. elemnode.left));
  1910. elemnode.left:=nil;
  1911. inc(paracount);
  1912. elemnode:=tarrayconstructornode(elemnode.right);
  1913. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1914. internalerror(2016021903);
  1915. end;
  1916. { get temp for array of lengths }
  1917. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1918. addstatement(newstatement,temp2);
  1919. { create call to fpc_dynarr_setlength }
  1920. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1921. ccallparanode.create(caddrnode.create_internal
  1922. (ctemprefnode.create(temp2)),
  1923. ccallparanode.create(cordconstnode.create
  1924. (1,s32inttype,true),
  1925. ccallparanode.create(caddrnode.create_internal
  1926. (crttinode.create(tstoreddef(totypedef),initrtti,rdt_normal)),
  1927. ccallparanode.create(
  1928. ctypeconvnode.create_internal(
  1929. ctemprefnode.create(arrnode),voidpointertype),
  1930. nil))))
  1931. ));
  1932. { add assignment statememnts }
  1933. addstatement(newstatement,ctempdeletenode.create(temp2));
  1934. addstatement(newstatement,assnode);
  1935. { the last statement should return the value as
  1936. location and type, this is done be referencing the
  1937. temp and converting it first from a persistent temp to
  1938. normal temp }
  1939. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1940. addstatement(newstatement,ctemprefnode.create(arrnode));
  1941. end;
  1942. function ttypeconvnode.typecheck_arrayconstructor_to_array : tnode;
  1943. var
  1944. newstatement,
  1945. assstatement : tstatementnode;
  1946. arrnode : ttempcreatenode;
  1947. temp2 : ttempcreatenode;
  1948. assnode : tnode;
  1949. paracount : integer;
  1950. elemnode : tarrayconstructornode;
  1951. begin
  1952. tarrayconstructornode(left).force_type(tarraydef(resultdef).elementdef);
  1953. result:=internalstatements(newstatement);
  1954. { create temp for result }
  1955. arrnode:=ctempcreatenode.create(totypedef,totypedef.size,tt_persistent,true);
  1956. addstatement(newstatement,arrnode);
  1957. paracount:=0;
  1958. { create an assignment call for each element }
  1959. assnode:=internalstatements(assstatement);
  1960. if left.nodetype=arrayconstructorrangen then
  1961. internalerror(2020041402);
  1962. elemnode:=tarrayconstructornode(left);
  1963. while assigned(elemnode) do
  1964. begin
  1965. { arr[i] := param_i }
  1966. if not assigned(elemnode.left) then
  1967. internalerror(2020041403);
  1968. addstatement(assstatement,
  1969. cassignmentnode.create(
  1970. cvecnode.create(
  1971. ctemprefnode.create(arrnode),
  1972. cordconstnode.create(paracount+tarraydef(totypedef).lowrange,tarraydef(totypedef).rangedef,false)),
  1973. elemnode.left));
  1974. elemnode.left:=nil;
  1975. inc(paracount);
  1976. elemnode:=tarrayconstructornode(elemnode.right);
  1977. if assigned(elemnode) and (elemnode.nodetype<>arrayconstructorn) then
  1978. internalerror(2020041404);
  1979. end;
  1980. { get temp for array of lengths }
  1981. temp2:=ctempcreatenode.create_value(sinttype,sinttype.size,tt_persistent,false,cordconstnode.create(paracount,s32inttype,true));
  1982. addstatement(newstatement,temp2);
  1983. { add assignment statememnts }
  1984. addstatement(newstatement,ctempdeletenode.create(temp2));
  1985. addstatement(newstatement,assnode);
  1986. { the last statement should return the value as
  1987. location and type, this is done be referencing the
  1988. temp and converting it first from a persistent temp to
  1989. normal temp }
  1990. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1991. addstatement(newstatement,ctemprefnode.create(arrnode));
  1992. end;
  1993. function ttypeconvnode._typecheck_int_to_int : tnode;
  1994. begin
  1995. result := typecheck_int_to_int;
  1996. end;
  1997. function ttypeconvnode._typecheck_cord_to_pointer : tnode;
  1998. begin
  1999. result := typecheck_cord_to_pointer;
  2000. end;
  2001. function ttypeconvnode._typecheck_chararray_to_string : tnode;
  2002. begin
  2003. result := typecheck_chararray_to_string;
  2004. end;
  2005. function ttypeconvnode._typecheck_string_to_chararray : tnode;
  2006. begin
  2007. result := typecheck_string_to_chararray;
  2008. end;
  2009. function ttypeconvnode._typecheck_string_to_string: tnode;
  2010. begin
  2011. result := typecheck_string_to_string;
  2012. end;
  2013. function ttypeconvnode._typecheck_char_to_string : tnode;
  2014. begin
  2015. result := typecheck_char_to_string;
  2016. end;
  2017. function ttypeconvnode._typecheck_char_to_chararray : tnode;
  2018. begin
  2019. result := typecheck_char_to_chararray;
  2020. end;
  2021. function ttypeconvnode._typecheck_int_to_real : tnode;
  2022. begin
  2023. result := typecheck_int_to_real;
  2024. end;
  2025. function ttypeconvnode._typecheck_real_to_real : tnode;
  2026. begin
  2027. result := typecheck_real_to_real;
  2028. end;
  2029. function ttypeconvnode._typecheck_real_to_currency : tnode;
  2030. begin
  2031. result := typecheck_real_to_currency;
  2032. end;
  2033. function ttypeconvnode._typecheck_cchar_to_pchar : tnode;
  2034. begin
  2035. result := typecheck_cchar_to_pchar;
  2036. end;
  2037. function ttypeconvnode._typecheck_cstring_to_pchar : tnode;
  2038. begin
  2039. result := typecheck_cstring_to_pchar;
  2040. end;
  2041. function ttypeconvnode._typecheck_cstring_to_int : tnode;
  2042. begin
  2043. result := typecheck_cstring_to_int;
  2044. end;
  2045. function ttypeconvnode._typecheck_char_to_char : tnode;
  2046. begin
  2047. result := typecheck_char_to_char;
  2048. end;
  2049. function ttypeconvnode._typecheck_arrayconstructor_to_set : tnode;
  2050. begin
  2051. result := typecheck_arrayconstructor_to_set;
  2052. end;
  2053. function ttypeconvnode._typecheck_set_to_set : tnode;
  2054. begin
  2055. result := typecheck_set_to_set;
  2056. end;
  2057. function ttypeconvnode._typecheck_pchar_to_string : tnode;
  2058. begin
  2059. result := typecheck_pchar_to_string;
  2060. end;
  2061. function ttypeconvnode._typecheck_interface_to_string : tnode;
  2062. begin
  2063. result := typecheck_interface_to_string;
  2064. end;
  2065. function ttypeconvnode._typecheck_interface_to_guid : tnode;
  2066. begin
  2067. result := typecheck_interface_to_guid;
  2068. end;
  2069. function ttypeconvnode._typecheck_dynarray_to_openarray : tnode;
  2070. begin
  2071. result := typecheck_dynarray_to_openarray;
  2072. end;
  2073. function ttypeconvnode._typecheck_pwchar_to_string : tnode;
  2074. begin
  2075. result := typecheck_pwchar_to_string;
  2076. end;
  2077. function ttypeconvnode._typecheck_variant_to_dynarray : tnode;
  2078. begin
  2079. result := typecheck_variant_to_dynarray;
  2080. end;
  2081. function ttypeconvnode._typecheck_dynarray_to_variant : tnode;
  2082. begin
  2083. result := typecheck_dynarray_to_variant;
  2084. end;
  2085. function ttypeconvnode._typecheck_variant_to_enum : tnode;
  2086. begin
  2087. result := typecheck_variant_to_enum;
  2088. end;
  2089. function ttypeconvnode._typecheck_enum_to_variant : tnode;
  2090. begin
  2091. result := typecheck_enum_to_variant;
  2092. end;
  2093. function ttypeconvnode._typecheck_proc_to_procvar : tnode;
  2094. begin
  2095. result := typecheck_proc_to_procvar;
  2096. end;
  2097. function ttypeconvnode._typecheck_variant_to_interface : tnode;
  2098. begin
  2099. result := typecheck_variant_to_interface;
  2100. end;
  2101. function ttypeconvnode._typecheck_interface_to_variant : tnode;
  2102. begin
  2103. result := typecheck_interface_to_variant;
  2104. end;
  2105. function ttypeconvnode._typecheck_array_2_dynarray : tnode;
  2106. begin
  2107. result := typecheck_array_2_dynarray;
  2108. end;
  2109. function ttypeconvnode._typecheck_elem_2_openarray : tnode;
  2110. begin
  2111. result := typecheck_elem_2_openarray;
  2112. end;
  2113. function ttypeconvnode._typecheck_arrayconstructor_to_dynarray : tnode;
  2114. begin
  2115. result:=typecheck_arrayconstructor_to_dynarray;
  2116. end;
  2117. function ttypeconvnode._typecheck_arrayconstructor_to_array : tnode;
  2118. begin
  2119. result:=typecheck_arrayconstructor_to_array;
  2120. end;
  2121. function ttypeconvnode._typecheck_procvar_to_funcref : tnode;
  2122. begin
  2123. result:=typecheck_procvar_2_funcref;
  2124. end;
  2125. function ttypeconvnode._typecheck_anonproc_to_funcref : tnode;
  2126. begin
  2127. result:=typecheck_anonproc_2_funcref;
  2128. end;
  2129. function ttypeconvnode.target_specific_general_typeconv: boolean;
  2130. begin
  2131. result:=false;
  2132. end;
  2133. function ttypeconvnode.target_specific_explicit_typeconv: boolean;
  2134. begin
  2135. result:=false;
  2136. end;
  2137. class function ttypeconvnode.target_specific_need_equal_typeconv(fromdef, todef: tdef): boolean;
  2138. begin
  2139. result:=false;
  2140. end;
  2141. type
  2142. tsym_mapping = record
  2143. oldsym:tsym;
  2144. newsym:tsym;
  2145. end;
  2146. psym_mapping = ^tsym_mapping;
  2147. function replace_self_sym(var n:tnode;arg:pointer):foreachnoderesult;
  2148. var
  2149. mapping : psym_mapping absolute arg;
  2150. ld : tloadnode;
  2151. begin
  2152. if n.nodetype=loadn then
  2153. begin
  2154. ld:=tloadnode(n);
  2155. if ld.symtableentry=mapping^.oldsym then
  2156. begin
  2157. ld.symtableentry:=mapping^.newsym;
  2158. { make sure that the node is processed again }
  2159. ld.resultdef:=nil;
  2160. if assigned(ld.left) then
  2161. begin
  2162. { no longer loaded through the frame pointer }
  2163. ld.left.free;
  2164. ld.left:=nil;
  2165. end;
  2166. typecheckpass(n);
  2167. end;
  2168. end;
  2169. result:=fen_true;
  2170. end;
  2171. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  2172. function is_self_sym(sym:tsym):boolean;
  2173. begin
  2174. result:=(sym.typ in [localvarsym,paravarsym]) and
  2175. (vo_is_self in tabstractvarsym(sym).varoptions);
  2176. end;
  2177. var
  2178. pd : tabstractprocdef;
  2179. copytype : tproccopytyp;
  2180. source: pnode;
  2181. fpsym,
  2182. selfsym,
  2183. sym : tsym;
  2184. mapping : tsym_mapping;
  2185. pi : tprocinfo;
  2186. i : longint;
  2187. begin
  2188. result:=nil;
  2189. pd:=tabstractprocdef(left.resultdef);
  2190. { create procvardef (default for create_proc_to_procvar is voiddef,
  2191. but if later a regular inserttypeconvnode() is used to insert a type
  2192. conversion to the actual procvardef, totypedef will be set to the
  2193. real procvartype that we are converting to) }
  2194. if assigned(totypedef) and
  2195. (totypedef.typ=procvardef) then
  2196. begin
  2197. { have to do this in typecheckpass so that it's triggered for
  2198. typed constant declarations }
  2199. if po_is_block in tprocvardef(totypedef).procoptions then
  2200. begin
  2201. { can only convert from procdef to procvardef, but in the mean
  2202. time other type conversions may have been inserted (pointers,
  2203. proc2procvar, ...) }
  2204. source:=actualtargetnode(@left);
  2205. while (source^.nodetype=typeconvn) and
  2206. (ttypeconvnode(source^).convtype=tc_proc_2_procvar) and
  2207. (is_void(source^.resultdef) or
  2208. (source^.resultdef.typ=procvardef)) do
  2209. begin
  2210. { won't skip proc2procvar }
  2211. source:=actualtargetnode(@ttypeconvnode(source^).left);
  2212. end;
  2213. if (source^.nodetype=loadn) and
  2214. (source^.resultdef.typ=procdef) and
  2215. not is_nested_pd(tprocdef(source^.resultdef)) and
  2216. not is_objcclass(tdef(source^.resultdef.owner.defowner)) then
  2217. begin
  2218. result:=generate_block_for_procaddr(tloadnode(source^));
  2219. exit;
  2220. end
  2221. else
  2222. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2223. end
  2224. else if (pd.typ=procdef) and
  2225. (po_anonymous in pd.procoptions) then
  2226. begin
  2227. if left.nodetype<>loadn then
  2228. internalerror(2021062402);
  2229. { get rid of any potential framepointer loading; if it's necessary
  2230. (for a nested procvar for example) it will be added again }
  2231. if assigned(tloadnode(left).left) and (tloadnode(left).left.nodetype=loadparentfpn) then
  2232. begin
  2233. tloadnode(left).left.free;
  2234. tloadnode(left).left:=nil;
  2235. tloadnode(left).resultdef:=nil;
  2236. end;
  2237. if tprocvardef(totypedef).is_methodpointer then
  2238. begin
  2239. if assigned(tprocdef(pd).capturedsyms) and
  2240. (
  2241. (tprocdef(pd).capturedsyms.count>1) or
  2242. (
  2243. (tprocdef(pd).capturedsyms.count=1) and
  2244. not is_self_sym(tsym(pcapturedsyminfo(tprocdef(pd).capturedsyms[0])^.sym))
  2245. )
  2246. ) then
  2247. internalerror(2021060801);
  2248. { so that insert_self_and_vmt_para correctly inserts the
  2249. Self, cause it otherwise skips that for anonymous functions }
  2250. include(pd.procoptions,po_methodpointer);
  2251. { we know this only captures Self, so we can move the
  2252. anonymous function to normal function level }
  2253. pd.parast.symtablelevel:=normal_function_level;
  2254. tprocdef(pd).localst.symtablelevel:=normal_function_level;
  2255. selfsym:=nil;
  2256. fpsym:=nil;
  2257. { find the framepointer parameter and an eventual self }
  2258. for i:=0 to tprocdef(pd).parast.symlist.count-1 do
  2259. begin
  2260. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2261. if sym.typ<>paravarsym then
  2262. continue;
  2263. if vo_is_parentfp in tparavarsym(sym).varoptions then
  2264. fpsym:=sym;
  2265. if vo_is_self in tparavarsym(sym).varoptions then
  2266. selfsym:=sym;
  2267. if assigned(fpsym) and assigned(selfsym) then
  2268. break;
  2269. end;
  2270. if assigned(fpsym) then
  2271. tprocdef(pd).parast.symlist.remove(fpsym);
  2272. { if we don't have a self parameter already we need to
  2273. insert a suitable one }
  2274. if not assigned(selfsym) then
  2275. begin
  2276. { replace the self symbol by the new parameter if it was
  2277. captured }
  2278. if assigned(tprocdef(pd).capturedsyms) and
  2279. (tprocdef(pd).capturedsyms.count>0) then
  2280. begin
  2281. if not assigned(tprocdef(pd).struct) then
  2282. { we can't use the captured symbol for the struct as that
  2283. might be the self of a type helper, thus we need to find
  2284. the parent procinfo that provides the Self }
  2285. tprocdef(pd).struct:=current_procinfo.get_normal_proc.procdef.struct;
  2286. if not assigned(tprocdef(pd).struct) then
  2287. internalerror(2021062204);
  2288. insert_self_and_vmt_para(pd);
  2289. mapping.oldsym:=tsym(pcapturedsyminfo(tprocdef(pd).capturedsyms[0])^.sym);
  2290. mapping.newsym:=nil;
  2291. { find the new self parameter }
  2292. for i:=0 to tprocdef(pd).parast.symlist.count-1 do
  2293. begin
  2294. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2295. if (sym.typ=paravarsym) and (vo_is_self in tparavarsym(sym).varoptions) then
  2296. begin
  2297. mapping.newsym:=sym;
  2298. break;
  2299. end;
  2300. end;
  2301. if not assigned(mapping.newsym) then
  2302. internalerror(2021062202);
  2303. { the anonymous function can only be a direct child of the
  2304. current_procinfo }
  2305. pi:=current_procinfo.get_first_nestedproc;
  2306. while assigned(pi) do
  2307. begin
  2308. if pi.procdef=pd then
  2309. break;
  2310. pi:=tprocinfo(pi.next);
  2311. end;
  2312. if not assigned(pi) then
  2313. internalerror(2021062203);
  2314. { replace all uses of the captured Self by the new Self
  2315. parameter }
  2316. foreachnodestatic(pm_preprocess,tcgprocinfo(pi).code,@replace_self_sym,@mapping);
  2317. end
  2318. else
  2319. begin
  2320. { for a nested function of a method struct is already
  2321. set }
  2322. if not assigned(tprocdef(pd).struct) then
  2323. { simply add a TObject as Self parameter }
  2324. tprocdef(pd).struct:=class_tobject;
  2325. insert_self_and_vmt_para(pd);
  2326. { there is no self, so load a nil value }
  2327. tloadnode(left).set_mp(cnilnode.create);
  2328. end;
  2329. end;
  2330. { the anonymous function no longer adheres to the nested
  2331. calling convention }
  2332. exclude(pd.procoptions,po_delphi_nested_cc);
  2333. tprocdef(pd).calcparas;
  2334. if not assigned(tloadnode(left).left) then
  2335. tloadnode(left).set_mp(load_self_node);
  2336. end
  2337. else if tprocvardef(totypedef).is_addressonly then
  2338. begin
  2339. if assigned(tprocdef(pd).capturedsyms) and (tprocdef(pd).capturedsyms.count>0) then
  2340. internalerror(2021060802);
  2341. { remove framepointer and Self parameters }
  2342. for i:=tprocdef(pd).parast.symlist.count-1 downto 0 do
  2343. begin
  2344. sym:=tsym(tprocdef(pd).parast.symlist[i]);
  2345. if (sym.typ=paravarsym) and (tparavarsym(sym).varoptions*[vo_is_parentfp,vo_is_self]<>[]) then
  2346. tprocdef(pd).parast.symlist.delete(i);
  2347. end;
  2348. { the anonymous function no longer adheres to the nested
  2349. calling convention }
  2350. exclude(pd.procoptions,po_delphi_nested_cc);
  2351. { we don't need to look through the existing nodes, cause
  2352. the parameter was never used anyway }
  2353. tprocdef(pd).calcparas;
  2354. end
  2355. else
  2356. begin
  2357. { this is a nested function pointer, so ensure that the
  2358. anonymous function is handled as such }
  2359. if assigned(tprocdef(pd).capturedsyms) and
  2360. (tprocdef(pd).capturedsyms.count>0) and
  2361. (left.nodetype=loadn) then
  2362. begin
  2363. tloadnode(left).left:=cloadparentfpnode.create(tprocdef(tloadnode(left).symtable.defowner),lpf_forload);
  2364. typecheckpass(tloadnode(left).left);
  2365. pi:=current_procinfo.get_first_nestedproc;
  2366. while assigned(pi) do
  2367. begin
  2368. if pi.procdef=pd then
  2369. break;
  2370. pi:=tprocinfo(pi.next);
  2371. end;
  2372. pi.set_needs_parentfp(tprocdef(tloadnode(left).symtable.defowner).parast.symtablelevel);
  2373. end;
  2374. end;
  2375. end;
  2376. resultdef:=totypedef;
  2377. end
  2378. else
  2379. begin
  2380. { only need the address of the method? this is needed
  2381. for @tobject.create. In this case there will be a loadn without
  2382. a methodpointer. }
  2383. if (left.nodetype=loadn) and
  2384. not assigned(tloadnode(left).left) and
  2385. (not(m_nested_procvars in current_settings.modeswitches) or
  2386. not is_nested_pd(tabstractprocdef(tloadnode(left).resultdef))) then
  2387. copytype:=pc_address_only
  2388. else
  2389. copytype:=pc_normal;
  2390. resultdef:=cprocvardef.getreusableprocaddr(pd,copytype);
  2391. end;
  2392. end;
  2393. function ttypeconvnode.typecheck_procvar_2_funcref : tnode;
  2394. var
  2395. capturer : tsym;
  2396. intfdef : tdef;
  2397. ld,blck,hp : tnode;
  2398. stmt : tstatementnode;
  2399. begin
  2400. result:=nil;
  2401. if not(m_tp_procvar in current_settings.modeswitches) and
  2402. is_invokable(resultdef) and
  2403. (left.nodetype=typeconvn) and
  2404. (ttypeconvnode(left).convtype=tc_proc_2_procvar) and
  2405. is_methodpointer(left.resultdef) and
  2406. (po_classmethod in tprocvardef(left.resultdef).procoptions) and
  2407. not(po_staticmethod in tprocvardef(left.resultdef).procoptions) and
  2408. (proc_to_funcref_equal(tprocdef(ttypeconvnode(left).left.resultdef),tobjectdef(resultdef))>=te_convert_l1) then
  2409. begin
  2410. hp:=left;
  2411. left:=ttypeconvnode(left).left;
  2412. if (left.nodetype=loadn) and
  2413. not assigned(tloadnode(left).left) then
  2414. tloadnode(left).set_mp(cloadvmtaddrnode.create(ctypenode.create(tdef(tloadnode(left).symtable.defowner))));
  2415. left:=ctypeconvnode.create_proc_to_procvar(left);
  2416. ttypeconvnode(left).totypedef:=resultdef;
  2417. typecheckpass(left);
  2418. ttypeconvnode(hp).left:=nil;
  2419. hp.free;
  2420. end;
  2421. intfdef:=capturer_add_procvar_or_proc(current_procinfo,left,capturer,hp);
  2422. if assigned(intfdef) then
  2423. begin
  2424. if assigned(capturer) then
  2425. ld:=cloadnode.create(capturer,capturer.owner)
  2426. else
  2427. ld:=cnilnode.create;
  2428. result:=ctypeconvnode.create_internal(
  2429. ctypeconvnode.create_internal(
  2430. ld,
  2431. intfdef),
  2432. totypedef);
  2433. if assigned(hp) then
  2434. begin
  2435. blck:=internalstatements(stmt);
  2436. addstatement(stmt,cassignmentnode.create(hp,left));
  2437. left:=nil;
  2438. addstatement(stmt,result);
  2439. result:=blck;
  2440. end;
  2441. end;
  2442. if not assigned(result) then
  2443. result:=cerrornode.create;
  2444. end;
  2445. function ttypeconvnode.typecheck_anonproc_2_funcref : tnode;
  2446. var
  2447. capturer : tsym;
  2448. intfdef : tdef;
  2449. ldnode : tnode;
  2450. begin
  2451. intfdef:=capturer_add_anonymous_proc(current_procinfo,tprocdef(left.resultdef),capturer);
  2452. if assigned(intfdef) then
  2453. begin
  2454. if assigned(capturer) then
  2455. ldnode:=cloadnode.create(capturer,capturer.owner)
  2456. else
  2457. ldnode:=cnilnode.create;
  2458. result:=ctypeconvnode.create_internal(
  2459. ctypeconvnode.create_internal(
  2460. ldnode,
  2461. intfdef),
  2462. totypedef);
  2463. end
  2464. else
  2465. result:=cerrornode.create;
  2466. end;
  2467. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  2468. const
  2469. resultdefconvert : array[tconverttype] of pointer = (
  2470. {none} nil,
  2471. {equal} nil,
  2472. {not_possible} nil,
  2473. { string_2_string } @ttypeconvnode._typecheck_string_to_string,
  2474. { char_2_string } @ttypeconvnode._typecheck_char_to_string,
  2475. { char_2_chararray } @ttypeconvnode._typecheck_char_to_chararray,
  2476. { pchar_2_string } @ttypeconvnode._typecheck_pchar_to_string,
  2477. { cchar_2_pchar } @ttypeconvnode._typecheck_cchar_to_pchar,
  2478. { cstring_2_pchar } @ttypeconvnode._typecheck_cstring_to_pchar,
  2479. { cstring_2_int } @ttypeconvnode._typecheck_cstring_to_int,
  2480. { ansistring_2_pchar } nil,
  2481. { string_2_chararray } @ttypeconvnode._typecheck_string_to_chararray,
  2482. { chararray_2_string } @ttypeconvnode._typecheck_chararray_to_string,
  2483. { array_2_pointer } nil,
  2484. { pointer_2_array } nil,
  2485. { int_2_int } @ttypeconvnode._typecheck_int_to_int,
  2486. { int_2_bool } nil,
  2487. { bool_2_bool } nil,
  2488. { bool_2_int } nil,
  2489. { real_2_real } @ttypeconvnode._typecheck_real_to_real,
  2490. { int_2_real } @ttypeconvnode._typecheck_int_to_real,
  2491. { real_2_currency } @ttypeconvnode._typecheck_real_to_currency,
  2492. { proc_2_procvar } @ttypeconvnode._typecheck_proc_to_procvar,
  2493. { nil_2_methodprocvar } nil,
  2494. { arrayconstructor_2_set } @ttypeconvnode._typecheck_arrayconstructor_to_set,
  2495. { set_to_set } @ttypeconvnode._typecheck_set_to_set,
  2496. { cord_2_pointer } @ttypeconvnode._typecheck_cord_to_pointer,
  2497. { intf_2_string } @ttypeconvnode._typecheck_interface_to_string,
  2498. { intf_2_guid } @ttypeconvnode._typecheck_interface_to_guid,
  2499. { class_2_intf } nil,
  2500. { char_2_char } @ttypeconvnode._typecheck_char_to_char,
  2501. { dynarray_2_openarray} @ttypeconvnode._typecheck_dynarray_to_openarray,
  2502. { pwchar_2_string} @ttypeconvnode._typecheck_pwchar_to_string,
  2503. { variant_2_dynarray} @ttypeconvnode._typecheck_variant_to_dynarray,
  2504. { dynarray_2_variant} @ttypeconvnode._typecheck_dynarray_to_variant,
  2505. { variant_2_enum} @ttypeconvnode._typecheck_variant_to_enum,
  2506. { enum_2_variant} @ttypeconvnode._typecheck_enum_to_variant,
  2507. { variant_2_interface} @ttypeconvnode._typecheck_interface_to_variant,
  2508. { interface_2_variant} @ttypeconvnode._typecheck_variant_to_interface,
  2509. { array_2_dynarray} @ttypeconvnode._typecheck_array_2_dynarray,
  2510. { elem_2_openarray } @ttypeconvnode._typecheck_elem_2_openarray,
  2511. { arrayconstructor_2_dynarray } @ttypeconvnode._typecheck_arrayconstructor_to_dynarray,
  2512. { arrayconstructor_2_array } @ttypeconvnode._typecheck_arrayconstructor_to_array,
  2513. { anonproc_2_funcref } @ttypeconvnode._typecheck_anonproc_to_funcref,
  2514. { procvar_2_funcref } @ttypeconvnode._typecheck_procvar_to_funcref
  2515. );
  2516. type
  2517. tprocedureofobject = function : tnode of object;
  2518. var
  2519. r : TMethod;
  2520. begin
  2521. result:=nil;
  2522. { this is a little bit dirty but it works }
  2523. { and should be quite portable too }
  2524. r.Code:=resultdefconvert[c];
  2525. r.Data:=self;
  2526. if assigned(r.Code) then
  2527. result:=tprocedureofobject(r)();
  2528. end;
  2529. function ttypeconvnode.pass_typecheck:tnode;
  2530. var
  2531. hdef : tdef;
  2532. hp : tnode;
  2533. currprocdef : tabstractprocdef;
  2534. aprocdef : tprocdef;
  2535. eq : tequaltype;
  2536. cdoptions : tcompare_defs_options;
  2537. selfnode : tnode;
  2538. newblock: tblocknode;
  2539. newstatement: tstatementnode;
  2540. tempnode: ttempcreatenode;
  2541. begin
  2542. result:=nil;
  2543. resultdef:=totypedef;
  2544. typecheckpass(left);
  2545. if codegenerror then
  2546. exit;
  2547. { When absolute force tc_equal }
  2548. if (nf_absolute in flags) then
  2549. begin
  2550. convtype:=tc_equal;
  2551. { we need to check regability only if something is really regable }
  2552. if ((tstoreddef(left.resultdef).is_intregable) or
  2553. (tstoreddef(resultdef).is_fpuregable)) and
  2554. (
  2555. (tstoreddef(resultdef).is_intregable<>tstoreddef(left.resultdef).is_intregable) or
  2556. (tstoreddef(resultdef).is_fpuregable<>tstoreddef(left.resultdef).is_fpuregable) or
  2557. { like in pdecvar.read_absolute(): if the size changes, the
  2558. register size would also have to change (but second_nothing
  2559. does not handle this) }
  2560. (tstoreddef(resultdef).size<>tstoreddef(left.resultdef).size)) then
  2561. make_not_regable(left,[ra_addr_regable]);
  2562. exit;
  2563. end;
  2564. { tp procvar support. Skip typecasts to procvar, record or set. Those
  2565. convert on the procvar value. This is used to access the
  2566. fields of a methodpointer }
  2567. if not(nf_load_procvar in flags) and
  2568. not(resultdef.typ in [procvardef,recorddef,setdef]) and
  2569. not is_invokable(resultdef) and
  2570. { in case of interface assignments of invokables they'll be converted
  2571. to voidpointertype using an internal conversions; we must not call
  2572. the invokable in that case }
  2573. not (
  2574. (nf_internal in flags) and
  2575. is_invokable(left.resultdef)
  2576. ) then
  2577. maybe_call_procvar(left,true);
  2578. if target_specific_general_typeconv then
  2579. exit;
  2580. if convtype=tc_none then
  2581. begin
  2582. cdoptions:=[cdo_allow_variant,cdo_warn_incompatible_univ];
  2583. { overloaded operators require calls, which is not possible inside
  2584. a constant declaration }
  2585. if (block_type<>bt_const) and
  2586. not(nf_internal in flags) then
  2587. include(cdoptions,cdo_check_operator);
  2588. if nf_explicit in flags then
  2589. include(cdoptions,cdo_explicit);
  2590. if nf_internal in flags then
  2591. include(cdoptions,cdo_internal);
  2592. aprocdef:=nil;
  2593. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  2594. case eq of
  2595. te_exact,
  2596. te_equal :
  2597. begin
  2598. result := simplify(false);
  2599. if assigned(result) then
  2600. exit;
  2601. { in case of bitpacked accesses, the original type must
  2602. remain so that not too many/few bits are laoded }
  2603. if is_bitpacked_access(left) then
  2604. convtype:=tc_int_2_int;
  2605. { Only leave when there is no conversion to do.
  2606. We can still need to call a conversion routine,
  2607. like the routine to convert a stringconstnode }
  2608. if (convtype in [tc_equal,tc_not_possible]) and
  2609. { some conversions, like dynarray to pointer in Delphi
  2610. mode, must not be removed, because then we get memory
  2611. leaks due to missing temp finalization }
  2612. (not is_managed_type(left.resultdef) or
  2613. { different kinds of refcounted types may need calls
  2614. to different kinds of refcounting helpers }
  2615. (resultdef=left.resultdef)) then
  2616. begin
  2617. {$ifdef llvm}
  2618. { we still may have to insert a type conversion at the
  2619. llvm level }
  2620. if (blocktype<>bt_const) and
  2621. (left.resultdef<>resultdef) and
  2622. { if unspecialised generic -> we won't generate any code
  2623. for this, and keeping the type conversion node will
  2624. cause valid_for_assign to fail because the typecast will be from/to something of 0
  2625. bytes to/from something with a non-zero size }
  2626. not is_typeparam(left.resultdef) and
  2627. not is_typeparam(resultdef) then
  2628. result:=nil
  2629. else
  2630. {$endif llvm}
  2631. begin
  2632. left.resultdef:=resultdef;
  2633. if (nf_explicit in flags) and (left.nodetype = addrn) then
  2634. include(taddrnode(left).addrnodeflags,anf_typedaddr);
  2635. result:=left;
  2636. left:=nil;
  2637. end;
  2638. exit;
  2639. end;
  2640. end;
  2641. te_convert_l1,
  2642. te_convert_l2,
  2643. te_convert_l3,
  2644. te_convert_l4,
  2645. te_convert_l5,
  2646. te_convert_l6:
  2647. { nothing to do }
  2648. ;
  2649. te_convert_operator :
  2650. begin
  2651. include(current_procinfo.flags,pi_do_call);
  2652. addsymref(aprocdef.procsym,aprocdef);
  2653. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[],nil);
  2654. { tell explicitly which def we must use !! (PM) }
  2655. tcallnode(hp).procdefinition:=aprocdef;
  2656. left:=nil;
  2657. result:=hp;
  2658. exit;
  2659. end;
  2660. te_incompatible :
  2661. begin
  2662. { convert an array constructor to a set so that we still get
  2663. the error "set of Y incompatible to Z" instead of "array of
  2664. X incompatible to Z" }
  2665. if (resultdef.typ<>arraydef) and
  2666. is_array_constructor(left.resultdef) then
  2667. begin
  2668. arrayconstructor_to_set(left);
  2669. typecheckpass(left);
  2670. end;
  2671. { Procedures have a resultdef of voiddef and functions of their
  2672. own resultdef. They will therefore always be incompatible with
  2673. a procvar. Because isconvertable cannot check for procedures we
  2674. use an extra check for them.}
  2675. if (left.nodetype=calln) and
  2676. (tcallnode(left).required_para_count=0) and
  2677. (
  2678. (resultdef.typ=procvardef) or
  2679. is_invokable(resultdef)
  2680. ) and
  2681. (
  2682. (m_tp_procvar in current_settings.modeswitches) or
  2683. (m_mac_procvar in current_settings.modeswitches)
  2684. ) then
  2685. begin
  2686. if assigned(tcallnode(left).right) then
  2687. begin
  2688. { this is already a procvar, if it is really equal
  2689. is checked below }
  2690. convtype:=tc_equal;
  2691. hp:=tcallnode(left).right.getcopy;
  2692. currprocdef:=tabstractprocdef(hp.resultdef);
  2693. end
  2694. else
  2695. begin
  2696. if resultdef.typ=procvardef then
  2697. begin
  2698. convtype:=tc_proc_2_procvar;
  2699. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  2700. end
  2701. else
  2702. begin
  2703. convtype:=tc_procvar_2_funcref;
  2704. currprocdef:=tprocsym(tcallnode(left).symtableprocentry).find_procdef_byfuncrefdef(tobjectdef(resultdef));
  2705. end;
  2706. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  2707. tprocdef(currprocdef),tcallnode(left).symtableproc);
  2708. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  2709. begin
  2710. selfnode:=tcallnode(left).methodpointer;
  2711. if assigned(selfnode) then
  2712. begin
  2713. { in case the nodetype is a typen, avoid the internal error
  2714. in set_mp and instead let the code error out normally }
  2715. if selfnode.nodetype<>typen then
  2716. tloadnode(hp).set_mp(selfnode.getcopy)
  2717. end
  2718. else
  2719. tloadnode(hp).set_mp(load_self_node);
  2720. end;
  2721. typecheckpass(hp);
  2722. end;
  2723. left.free;
  2724. left:=hp;
  2725. { Now check if the procedure we are going to assign to
  2726. the procvar, is compatible with the procvar's type }
  2727. if not(nf_explicit in flags) and
  2728. (
  2729. (
  2730. (resultdef.typ=procvardef) and
  2731. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef),false)=te_incompatible)
  2732. ) or (
  2733. is_invokable(resultdef) and
  2734. (proc_to_funcref_equal(currprocdef,tobjectdef(resultdef))=te_incompatible)
  2735. )
  2736. ) then
  2737. IncompatibleTypes(left.resultdef,resultdef)
  2738. else
  2739. result:=typecheck_call_helper(convtype);
  2740. exit;
  2741. end
  2742. else if maybe_global_proc_to_nested(left,resultdef) or
  2743. maybe_classmethod_to_methodprocvar(left,resultdef) then
  2744. begin
  2745. result:=left;
  2746. left:=nil;
  2747. exit;
  2748. end;
  2749. { Handle explicit type conversions }
  2750. if nf_explicit in flags then
  2751. begin
  2752. { do common tc_equal cast, except when dealing with proc -> procvar
  2753. (may have to get rid of method pointer) }
  2754. if (left.resultdef.typ<>procdef) or
  2755. (resultdef.typ<>procvardef) then
  2756. convtype:=tc_equal
  2757. else
  2758. convtype:=tc_proc_2_procvar;
  2759. { ordinal constants can be resized to 1,2,4,8 bytes }
  2760. if (left.nodetype=ordconstn) then
  2761. begin
  2762. { Insert typeconv for ordinal to the correct size first on left, after
  2763. that the other conversion can be done }
  2764. hdef:=nil;
  2765. case longint(resultdef.size) of
  2766. 1 :
  2767. hdef:=s8inttype;
  2768. 2 :
  2769. hdef:=s16inttype;
  2770. 4 :
  2771. hdef:=s32inttype;
  2772. 8 :
  2773. hdef:=s64inttype;
  2774. end;
  2775. { we need explicit, because it can also be an enum }
  2776. if assigned(hdef) then
  2777. inserttypeconv_internal(left,hdef)
  2778. else
  2779. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename);
  2780. end;
  2781. { class/interface to class/interface, with checkobject support }
  2782. if is_class_or_interface_or_objc(resultdef) and
  2783. is_class_or_interface_or_objc(left.resultdef) then
  2784. begin
  2785. { check if the types are related }
  2786. if not(nf_internal in flags) and
  2787. (not(def_is_related(tobjectdef(left.resultdef),tobjectdef(resultdef)))) and
  2788. (not(def_is_related(tobjectdef(resultdef),tobjectdef(left.resultdef)))) then
  2789. begin
  2790. { Give an error when typecasting class to interface, this is compatible
  2791. with delphi }
  2792. if is_interface(resultdef) and
  2793. not is_interface(left.resultdef) then
  2794. CGMessage2(type_e_classes_not_related,
  2795. FullTypeName(left.resultdef,resultdef),
  2796. FullTypeName(resultdef,left.resultdef))
  2797. else
  2798. CGMessage2(type_w_classes_not_related,
  2799. FullTypeName(left.resultdef,resultdef),
  2800. FullTypeName(resultdef,left.resultdef))
  2801. end;
  2802. { Add runtime check? }
  2803. if not is_objc_class_or_protocol(resultdef) and
  2804. not is_objc_class_or_protocol(left.resultdef) and
  2805. (cs_check_object in current_settings.localswitches) and
  2806. not(nf_internal in flags) then
  2807. begin
  2808. { we can translate the typeconvnode to 'as' when
  2809. typecasting to a class or interface }
  2810. { we need to make sure the result can still be
  2811. passed as a var parameter }
  2812. newblock:=internalstatements(newstatement);
  2813. if (valid_for_var(left,false)) then
  2814. begin
  2815. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2816. addstatement(newstatement,tempnode);
  2817. addstatement(newstatement,cassignmentnode.create(
  2818. ctemprefnode.create(tempnode),
  2819. caddrnode.create_internal(left)));
  2820. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  2821. end
  2822. else
  2823. begin
  2824. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  2825. addstatement(newstatement,tempnode);
  2826. addstatement(newstatement,cassignmentnode.create(
  2827. ctemprefnode.create(tempnode),
  2828. left));
  2829. left:=ctemprefnode.create(tempnode);
  2830. end;
  2831. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  2832. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2833. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  2834. left:=nil;
  2835. result:=newblock;
  2836. exit;
  2837. end;
  2838. end
  2839. else
  2840. begin
  2841. { only if the same size or formal def, and }
  2842. { don't allow type casting of constants to }
  2843. { structured types }
  2844. if not(
  2845. (left.resultdef.typ=formaldef) or
  2846. {$ifdef jvm}
  2847. { enums /are/ class instances on the JVM
  2848. platform }
  2849. (((left.resultdef.typ=enumdef) and
  2850. (resultdef.typ=objectdef)) or
  2851. ((resultdef.typ=enumdef) and
  2852. (left.resultdef.typ=objectdef))) or
  2853. {$endif}
  2854. (
  2855. is_void(left.resultdef) and
  2856. (left.nodetype=derefn)
  2857. ) or
  2858. (
  2859. not(is_open_array(left.resultdef)) and
  2860. not(is_array_constructor(left.resultdef)) and
  2861. not(is_array_of_const(left.resultdef)) and
  2862. (left.resultdef.size=resultdef.size) and
  2863. { disallow casts of const nodes }
  2864. (not is_constnode(left) or
  2865. { however, there are some exceptions }
  2866. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  2867. filedef,variantdef,objectdef]) or
  2868. is_class_or_interface_or_objc(resultdef) or
  2869. { the softfloat code generates casts <const. float> to record }
  2870. (nf_internal in flags)
  2871. ))
  2872. )
  2873. ) then
  2874. CGMessage2(type_e_illegal_type_conversion,left.resultdef.typename,resultdef.typename)
  2875. else
  2876. begin
  2877. { perform target-specific explicit typecast
  2878. checks }
  2879. if target_specific_explicit_typeconv then
  2880. begin
  2881. result:=simplify(false);
  2882. exit;
  2883. end;
  2884. end;
  2885. end;
  2886. end
  2887. else
  2888. IncompatibleTypes(left.resultdef,resultdef);
  2889. end;
  2890. end;
  2891. end;
  2892. { Give hint or warning for unportable code, exceptions are
  2893. - typecasts from constants
  2894. - void }
  2895. if not(nf_internal in flags) and
  2896. (left.nodetype<>ordconstn) and
  2897. not(is_void(left.resultdef)) and
  2898. (((left.resultdef.typ=orddef) and
  2899. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  2900. ((resultdef.typ=orddef) and
  2901. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  2902. begin
  2903. {Converting pointers to signed integers is a bad idea. Warn.}
  2904. warn_pointer_to_signed:=(resultdef.typ=orddef) and (Torddef(resultdef).ordtype in [s8bit,s16bit,s32bit,s64bit]);
  2905. { Give a warning when sizes don't match, because then info will be lost }
  2906. if left.resultdef.size=resultdef.size then
  2907. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  2908. else
  2909. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  2910. end;
  2911. { tc_cord_2_pointer still requires a type check, which
  2912. simplify does not do }
  2913. if (convtype<>tc_cord_2_pointer) then
  2914. begin
  2915. result := simplify(false);
  2916. if assigned(result) then
  2917. exit;
  2918. end;
  2919. { now call the resultdef helper to do constant folding }
  2920. result:=typecheck_call_helper(convtype);
  2921. end;
  2922. { some code generators for 64 bit CPUs might not support 32 bit operations, so we can
  2923. disable the following optimization in fpcdefs.inc. Currently the only CPU for which
  2924. this applies is powerpc64
  2925. }
  2926. {$ifndef CPUNO32BITOPS}
  2927. { checks whether we can safely remove typeconversions to bigger types
  2928. in case range and overflow checking are off, and in case
  2929. the result of this node tree is downcasted again to a
  2930. smaller type value afterwards,
  2931. the smaller types being allowed are described by validints, ordinal constants must fit into l..h
  2932. We do this on 64 bit CPUs as well, they benefit from it as well }
  2933. function checkremovebiginttypeconvs(n: tnode; out gotsint: boolean;validints : tordtypeset;const l,h : Tconstexprint): boolean;
  2934. var
  2935. gotminus1,
  2936. gotsigned,
  2937. gotunsigned,
  2938. gotdivmod: boolean;
  2939. { checks whether a node has an accepted resultdef, or originally
  2940. had one but was implicitly converted to s64bit }
  2941. function wasoriginallysmallerint(n: tnode): boolean;
  2942. begin
  2943. if (n.resultdef.typ<>orddef) then
  2944. exit(false);
  2945. gotsigned:=gotsigned or is_signed(n.resultdef);
  2946. gotunsigned:=gotunsigned or not(is_signed(n.resultdef));
  2947. { actually, we should only check right (denominator) nodes here, but
  2948. setting it always is a safe approximation }
  2949. if ((n.nodetype=ordconstn) and
  2950. (tordconstnode(n).value=-1)) then
  2951. gotminus1:=true;
  2952. if (torddef(n.resultdef).ordtype in validints) then
  2953. begin
  2954. if is_signed(n.resultdef) then
  2955. gotsint:=true;
  2956. exit(true);
  2957. end;
  2958. { type conv to a bigger int, we do not like to use? }
  2959. if (torddef(n.resultdef).ordtype in ([s8bit,u8bit,s16bit,u16bit,s32bit,u32bit,s64bit,u64bit]-validints)) and
  2960. { nf_explicit is also set for explicitly typecasted }
  2961. { ordconstn's }
  2962. ([nf_internal,nf_explicit]*n.flags=[]) and
  2963. { either a typeconversion node coming from a smaller type }
  2964. (((n.nodetype=typeconvn) and
  2965. (ttypeconvnode(n).left.resultdef.typ=orddef) and
  2966. (torddef(ttypeconvnode(n).left.resultdef).ordtype in validints)) or
  2967. { or an ordconstnode which has a smaller type}
  2968. ((n.nodetype=ordconstn) and
  2969. (tordconstnode(n).value>=l) and
  2970. (tordconstnode(n).value<=h))) then
  2971. begin
  2972. if ((n.nodetype=typeconvn) and
  2973. is_signed(ttypeconvnode(n).left.resultdef)) or
  2974. ((n.nodetype=ordconstn) and
  2975. (tordconstnode(n).value<0)) then
  2976. begin
  2977. gotsint:=true;
  2978. gotsigned:=true;
  2979. end
  2980. else
  2981. gotunsigned:=true;
  2982. exit(true);
  2983. end;
  2984. result:=false;
  2985. end;
  2986. function docheckremoveinttypeconvs(n: tnode): boolean;
  2987. begin
  2988. if wasoriginallysmallerint(n) then
  2989. exit(true);
  2990. case n.nodetype of
  2991. subn,orn,xorn:
  2992. begin
  2993. { the result could become negative in this case }
  2994. if n.nodetype=subn then
  2995. gotsint:=true;
  2996. result:=
  2997. docheckremoveinttypeconvs(tbinarynode(n).left) and
  2998. docheckremoveinttypeconvs(tbinarynode(n).right);
  2999. end;
  3000. unaryminusn:
  3001. begin
  3002. gotsint:=true;
  3003. result:=docheckremoveinttypeconvs(tunarynode(n).left);
  3004. end;
  3005. shrn:
  3006. begin
  3007. result:=wasoriginallysmallerint(tbinarynode(n).left) and
  3008. docheckremoveinttypeconvs(tbinarynode(n).right);
  3009. end;
  3010. notn:
  3011. result:=docheckremoveinttypeconvs(tunarynode(n).left);
  3012. addn,muln,divn,modn,andn,shln:
  3013. begin
  3014. if n.nodetype in [divn,modn] then
  3015. gotdivmod:=true;
  3016. result:=
  3017. (docheckremoveinttypeconvs(tbinarynode(n).left) and
  3018. docheckremoveinttypeconvs(tbinarynode(n).right) and
  3019. (not(n.nodetype in [modn,divn]) or (not(gotminus1)))
  3020. ) or
  3021. (
  3022. (n.nodetype=andn) and
  3023. (
  3024. { Right node is more likely to be a constant, so check
  3025. this one first }
  3026. wasoriginallysmallerint(tbinarynode(n).right) or
  3027. wasoriginallysmallerint(tbinarynode(n).left)
  3028. )
  3029. );
  3030. end;
  3031. else
  3032. result:=false;
  3033. end;
  3034. end;
  3035. begin { checkremove64bittypeconvs }
  3036. gotdivmod:=false;
  3037. gotsint:=false;
  3038. gotminus1:=false;
  3039. gotsigned:=false;
  3040. gotunsigned:=false;
  3041. result:=
  3042. docheckremoveinttypeconvs(n) and
  3043. (not(gotdivmod) or (gotsigned xor gotunsigned));
  3044. end;
  3045. { remove int type conversions and set the result to the given type }
  3046. procedure doremoveinttypeconvs(level : dword;var n: tnode; todef: tdef; forceunsigned: boolean; signedtype,unsignedtype : tdef);
  3047. var
  3048. newblock: tblocknode;
  3049. newstatements: tstatementnode;
  3050. originaldivtree: tnode;
  3051. tempnode: ttempcreatenode;
  3052. begin
  3053. { we may not recurse into shr nodes:
  3054. dword1:=dword1+((dword2+dword3) shr 2);
  3055. while we can remove an extension on the outermost addition, we cannot
  3056. remove it from the shr
  3057. }
  3058. { Don't downsize into a division operation either, as the numerator can
  3059. be much larger than the result and non-linear properties prevent
  3060. accurate truncation; fixes #39646 [Kit] }
  3061. if (n.nodetype in [shrn,divn,modn]) and (level<>0) then
  3062. begin
  3063. inserttypeconv_internal(n,todef);
  3064. exit;
  3065. end;
  3066. case n.nodetype of
  3067. subn,addn,muln,divn,modn,xorn,andn,orn,shln,shrn:
  3068. begin
  3069. exclude(n.flags,nf_internal);
  3070. if not forceunsigned and
  3071. is_signed(n.resultdef) then
  3072. begin
  3073. originaldivtree:=nil;
  3074. if n.nodetype in [divn,modn] then
  3075. originaldivtree:=n.getcopy;
  3076. doremoveinttypeconvs(level+1,tbinarynode(n).left,signedtype,false,signedtype,unsignedtype);
  3077. doremoveinttypeconvs(level+1,tbinarynode(n).right,signedtype,false,signedtype,unsignedtype);
  3078. n.resultdef:=signedtype;
  3079. if n.nodetype in [divn,modn] then
  3080. begin
  3081. newblock:=internalstatements(newstatements);
  3082. tempnode:=ctempcreatenode.create(n.resultdef,n.resultdef.size,tt_persistent,true);
  3083. addstatement(newstatements,tempnode);
  3084. addstatement(newstatements,cifnode.create_internal(
  3085. caddnode.create_internal(equaln,tbinarynode(n).right.getcopy,cordconstnode.create(-1,n.resultdef,false)),
  3086. cassignmentnode.create_internal(
  3087. ctemprefnode.create(tempnode),
  3088. cmoddivnode.create(n.nodetype,tbinarynode(originaldivtree).left.getcopy,cordconstnode.create(-1,tbinarynode(originaldivtree).right.resultdef,false))
  3089. ),
  3090. cassignmentnode.create_internal(
  3091. ctemprefnode.create(tempnode),n
  3092. )
  3093. )
  3094. );
  3095. addstatement(newstatements,ctempdeletenode.create_normal_temp(tempnode));
  3096. addstatement(newstatements,ctemprefnode.create(tempnode));
  3097. n:=newblock;
  3098. do_typecheckpass(n);
  3099. originaldivtree.free;
  3100. end;
  3101. end
  3102. else
  3103. begin
  3104. doremoveinttypeconvs(level+1,tbinarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3105. doremoveinttypeconvs(level+1,tbinarynode(n).right,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3106. n.resultdef:=unsignedtype;
  3107. end;
  3108. //if ((n.nodetype=andn) and (tbinarynode(n).left.nodetype=ordconstn) and
  3109. // ((tordconstnode(tbinarynode(n).left).value and $7fffffff)=tordconstnode(tbinarynode(n).left).value)
  3110. // ) then
  3111. // inserttypeconv_internal(tbinarynode(n).right,n.resultdef)
  3112. //else if (n.nodetype=andn) and (tbinarynode(n).right.nodetype=ordconstn) and
  3113. // ((tordconstnode(tbinarynode(n).right).value and $7fffffff)=tordconstnode(tbinarynode(n).right).value) then
  3114. // inserttypeconv_internal(tbinarynode(n).left,n.resultdef);
  3115. end;
  3116. unaryminusn,notn:
  3117. begin
  3118. exclude(n.flags,nf_internal);
  3119. if not forceunsigned and
  3120. is_signed(n.resultdef) then
  3121. begin
  3122. doremoveinttypeconvs(level+1,tunarynode(n).left,signedtype,false,signedtype,unsignedtype);
  3123. n.resultdef:=signedtype;
  3124. end
  3125. else
  3126. begin
  3127. doremoveinttypeconvs(level+1,tunarynode(n).left,unsignedtype,forceunsigned,signedtype,unsignedtype);
  3128. n.resultdef:=unsignedtype;
  3129. end;
  3130. end;
  3131. typeconvn:
  3132. begin
  3133. ttypeconvnode(n).totypedef:=todef;
  3134. { may change the type conversion, e.g. if the old conversion was
  3135. from 64 bit to a 64 bit, and now becomes 64 bit to 32 bit }
  3136. n.resultdef:=nil;
  3137. ttypeconvnode(n).convtype:=tc_none;
  3138. typecheckpass(n);
  3139. end;
  3140. else
  3141. inserttypeconv_internal(n,todef);
  3142. end;
  3143. end;
  3144. {$endif not CPUNO32BITOPS}
  3145. procedure swap_const_value (var val : TConstExprInt; size : longint);
  3146. begin
  3147. case size of
  3148. 1 : {do nothing };
  3149. 2 : if val.signed then
  3150. val.svalue:=swapendian(smallint(val.svalue))
  3151. else
  3152. val.uvalue:=swapendian(word(val.uvalue));
  3153. 4 : if val.signed then
  3154. val.svalue:=swapendian(longint(val.svalue))
  3155. else
  3156. val.uvalue:=swapendian(qword(val.uvalue));
  3157. 8 : if val.signed then
  3158. val.svalue:=swapendian(int64(val.svalue))
  3159. else
  3160. val.uvalue:=swapendian(qword(val.uvalue));
  3161. else
  3162. internalerror(2014111201);
  3163. end;
  3164. end;
  3165. function ttypeconvnode.simplify(forinline : boolean): tnode;
  3166. var
  3167. hp: tnode;
  3168. v: Tconstexprint;
  3169. {$ifndef CPUNO32BITOPS}
  3170. foundsint: boolean;
  3171. {$endif not CPUNO32BITOPS}
  3172. begin
  3173. result := nil;
  3174. { Constant folding and other node transitions to
  3175. remove the typeconv node }
  3176. case left.nodetype of
  3177. stringconstn :
  3178. if (resultdef.typ=stringdef) and
  3179. ((convtype=tc_equal) or
  3180. ((convtype=tc_string_2_string) and
  3181. (
  3182. ((not is_widechararray(left.resultdef) and
  3183. not is_wide_or_unicode_string(left.resultdef)) or
  3184. (tstringdef(resultdef).stringtype in [st_widestring,st_unicodestring,st_ansistring])
  3185. )
  3186. )
  3187. )
  3188. ) then
  3189. begin
  3190. { output string consts in local ansistring encoding }
  3191. if is_ansistring(resultdef) and
  3192. { do not mess with the result type for internally created nodes }
  3193. not(nf_internal in flags) and
  3194. ((tstringdef(resultdef).encoding=0) or (tstringdef(resultdef).encoding=globals.CP_NONE)) then
  3195. tstringconstnode(left).changestringtype(getansistringdef)
  3196. else
  3197. tstringconstnode(left).changestringtype(resultdef);
  3198. result:=left;
  3199. left:=nil;
  3200. exit;
  3201. end
  3202. else if
  3203. (convtype<>tc_cstring_2_pchar) and
  3204. is_dynamicstring(left.resultdef) and
  3205. (tstringconstnode(left).len=0) and
  3206. (resultdef.typ=pointerdef) and
  3207. cstringconstnode.emptydynstrnil then
  3208. begin
  3209. result:=cnilnode.create;
  3210. exit;
  3211. end;
  3212. realconstn :
  3213. begin
  3214. if (convtype = tc_real_2_currency) then
  3215. result := typecheck_real_to_currency
  3216. else if (convtype = tc_real_2_real) then
  3217. result := typecheck_real_to_real
  3218. else
  3219. exit;
  3220. if not(assigned(result)) then
  3221. begin
  3222. result := left;
  3223. left := nil;
  3224. end;
  3225. if (result.nodetype = realconstn) then
  3226. begin
  3227. hp:=result;
  3228. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  3229. if nf_is_currency in hp.flags then
  3230. include(result.flags,nf_is_currency);
  3231. if ([nf_explicit,nf_internal] * flags <> []) then
  3232. include(result.flags, nf_explicit);
  3233. hp.free;
  3234. end;
  3235. end;
  3236. niln :
  3237. begin
  3238. { nil to ordinal node }
  3239. if (resultdef.typ=orddef) then
  3240. begin
  3241. hp:=cordconstnode.create(0,resultdef,true);
  3242. if ([nf_explicit,nf_internal] * flags <> []) then
  3243. include(hp.flags, nf_explicit);
  3244. result:=hp;
  3245. exit;
  3246. end
  3247. else
  3248. { fold nil to any pointer type }
  3249. if (resultdef.typ=pointerdef) then
  3250. begin
  3251. hp:=cnilnode.create;
  3252. hp.resultdef:=resultdef;
  3253. if ([nf_explicit,nf_internal] * flags <> []) then
  3254. include(hp.flags, nf_explicit);
  3255. result:=hp;
  3256. exit;
  3257. end
  3258. else
  3259. { remove typeconv after niln, but not when the result is a
  3260. methodpointer. The typeconv of the methodpointer will then
  3261. take care of updateing size of niln to OS_64 }
  3262. if not((resultdef.typ=procvardef) and
  3263. not(tprocvardef(resultdef).is_addressonly)) and
  3264. { converting (dynamic array) nil to a an open array is not allowed }
  3265. not is_open_array(resultdef) then
  3266. begin
  3267. left.resultdef:=resultdef;
  3268. if ([nf_explicit,nf_internal] * flags <> []) then
  3269. include(left.flags, nf_explicit);
  3270. result:=left;
  3271. left:=nil;
  3272. exit;
  3273. end;
  3274. end;
  3275. ordconstn :
  3276. begin
  3277. { ordinal contants can be directly converted }
  3278. { but not char to char because it is a widechar to char or via versa }
  3279. { which needs extra code to do the code page transistion }
  3280. { constant ordinal to pointer }
  3281. if (resultdef.typ=pointerdef) and
  3282. (convtype<>tc_cchar_2_pchar) then
  3283. begin
  3284. if (target_info.system in systems_managed_vm) and
  3285. (tordconstnode(left).value<>0) then
  3286. message(parser_e_feature_unsupported_for_vm);
  3287. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  3288. if ([nf_explicit,nf_internal] * flags <> []) then
  3289. include(hp.flags, nf_explicit);
  3290. result:=hp;
  3291. exit;
  3292. end
  3293. else if is_ordinal(resultdef) and
  3294. not(convtype=tc_char_2_char) then
  3295. begin
  3296. { replace the resultdef and recheck the range }
  3297. if ([nf_explicit,nf_absolute, nf_internal] * flags <> []) then
  3298. include(left.flags, nf_explicit)
  3299. else
  3300. { no longer an ordconst with an explicit typecast }
  3301. exclude(left.flags, nf_explicit);
  3302. { when converting from one boolean type to another, force }
  3303. { booleans to 0/1, and byte/word/long/qwordbool to 0/-1 }
  3304. { (Delphi-compatibile) }
  3305. if is_boolean(left.resultdef) and
  3306. is_boolean(resultdef) and
  3307. (is_cbool(left.resultdef) or
  3308. is_cbool(resultdef)) then
  3309. begin
  3310. if is_pasbool(resultdef) then
  3311. tordconstnode(left).value:=ord(tordconstnode(left).value<>0)
  3312. else
  3313. tordconstnode(left).value:=-ord(tordconstnode(left).value<>0);
  3314. end
  3315. else
  3316. begin
  3317. { for constant values on absolute variables, swapping is required }
  3318. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  3319. swap_const_value(tordconstnode(left).value,tordconstnode(left).resultdef.size);
  3320. if not(nf_generic_para in flags) then
  3321. adaptrange(
  3322. resultdef,tordconstnode(left).value,
  3323. { when evaluating an explicit typecast during inlining, don't warn about
  3324. lost bits; only warn if someone literally typed e.g. byte($1ff) }
  3325. (([nf_internal,nf_absolute]*flags)<>[]) or (forinline and (nf_explicit in flags)),
  3326. nf_explicit in flags,
  3327. cs_check_range in localswitches);
  3328. { swap value back, but according to new type }
  3329. if (target_info.endian = endian_big) and (nf_absolute in flags) then
  3330. swap_const_value(tordconstnode(left).value,resultdef.size);
  3331. { cut off the new value? }
  3332. if resultdef.size<left.resultdef.size then
  3333. case resultdef.size of
  3334. 1:
  3335. if is_signed(resultdef) then
  3336. tordconstnode(left).value:=tordconstnode(left).value and shortint($ff)
  3337. else
  3338. tordconstnode(left).value:=tordconstnode(left).value and byte($ff);
  3339. 2:
  3340. if is_signed(resultdef) then
  3341. tordconstnode(left).value:=tordconstnode(left).value and smallint($ffff)
  3342. else
  3343. tordconstnode(left).value:=tordconstnode(left).value and word($ffff);
  3344. 4:
  3345. if is_signed(resultdef) then
  3346. tordconstnode(left).value:=tordconstnode(left).value and longint($ffffffff)
  3347. else
  3348. tordconstnode(left).value:=tordconstnode(left).value and dword($ffffffff);
  3349. end;
  3350. end;
  3351. left.resultdef:=resultdef;
  3352. tordconstnode(left).typedef:=resultdef;
  3353. if is_signed(resultdef) then
  3354. tordconstnode(left).value.signed:=true
  3355. else
  3356. tordconstnode(left).value.signed:=false;
  3357. result:=left;
  3358. left:=nil;
  3359. exit;
  3360. end
  3361. else if (convtype=tc_int_2_int) and
  3362. is_currency(resultdef) then
  3363. begin
  3364. v:=tordconstnode(left).value;
  3365. if not(nf_internal in flags) and not(is_currency(left.resultdef)) then
  3366. v:=v*10000;
  3367. result:=cordconstnode.create(v,resultdef,false);
  3368. exit;
  3369. end;
  3370. end;
  3371. pointerconstn :
  3372. begin
  3373. { pointerconstn to any pointer is folded too }
  3374. if (resultdef.typ=pointerdef) then
  3375. begin
  3376. left.resultdef:=resultdef;
  3377. if ([nf_explicit,nf_internal] * flags <> []) then
  3378. include(left.flags, nf_explicit)
  3379. else
  3380. { no longer an ordconst with an explicit typecast }
  3381. exclude(left.flags, nf_explicit);
  3382. result:=left;
  3383. left:=nil;
  3384. exit;
  3385. end
  3386. { constant pointer to ordinal }
  3387. else if is_ordinal(resultdef) then
  3388. begin
  3389. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  3390. resultdef,not(nf_explicit in flags));
  3391. if ([nf_explicit,nf_internal] * flags <> []) then
  3392. include(hp.flags, nf_explicit);
  3393. result:=hp;
  3394. exit;
  3395. end;
  3396. end;
  3397. else
  3398. ;
  3399. end;
  3400. {$ifndef CPUNO32BITOPS}
  3401. { must be done before code below, because we need the
  3402. typeconversions for ordconstn's as well }
  3403. case convtype of
  3404. tc_bool_2_int,
  3405. tc_int_2_bool,
  3406. tc_int_2_int:
  3407. begin
  3408. if (localswitches * [cs_check_range,cs_check_overflow] = []) and
  3409. (resultdef.typ in [pointerdef,orddef,enumdef]) then
  3410. begin
  3411. { avoid unnecessary widening of intermediary calculations
  3412. to 64 bit }
  3413. if (resultdef.size <= 4) and
  3414. is_64bitint(left.resultdef) and
  3415. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3416. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit,s32bit,u32bit],int64(low(longint)),high(cardinal)) then
  3417. doremoveinttypeconvs(0,left,generrordef,not foundsint,s32inttype,u32inttype);
  3418. {$if defined(cpu16bitalu)}
  3419. if (resultdef.size <= 2) and
  3420. (is_32bitint(left.resultdef) or is_64bitint(left.resultdef)) and
  3421. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3422. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit,s16bit,u16bit],int64(low(smallint)),high(word)) then
  3423. doremoveinttypeconvs(0,left,generrordef,not foundsint,s16inttype,u16inttype);
  3424. {$endif defined(cpu16bitalu)}
  3425. {$if defined(cpu8bitalu)}
  3426. if (resultdef.size<left.resultdef.size) and
  3427. is_integer(left.resultdef) and
  3428. (left.nodetype in [subn,addn,muln,divn,modn,xorn,andn,orn,notn,unaryminusn,shln,shrn]) and
  3429. checkremovebiginttypeconvs(left,foundsint,[s8bit,u8bit],int64(low(shortint)),high(byte)) then
  3430. doremoveinttypeconvs(0,left,generrordef,not foundsint,s8inttype,u8inttype);
  3431. {$endif defined(cpu8bitalu)}
  3432. { the above simplification may have left a redundant equal
  3433. typeconv (e.g. int32 to int32). If that's the case, we remove it }
  3434. if equal_defs(left.resultdef,resultdef) then
  3435. begin
  3436. result:=left;
  3437. left:=nil;
  3438. exit;
  3439. end;
  3440. if (convtype=tc_int_2_int) and (left.nodetype=typeconvn) and (ttypeconvnode(left).convtype=tc_bool_2_int) then
  3441. begin
  3442. ttypeconvnode(left).totypedef:=resultdef;
  3443. ttypeconvnode(left).resultdef:=resultdef;
  3444. result:=left;
  3445. left:=nil;
  3446. exit;
  3447. end;
  3448. end;
  3449. end;
  3450. else
  3451. ;
  3452. end;
  3453. {$endif not CPUNO32BITOPS}
  3454. end;
  3455. procedure Ttypeconvnode.mark_write;
  3456. begin
  3457. if not(convtype=tc_pointer_2_array) then
  3458. left.mark_write;
  3459. end;
  3460. function ttypeconvnode.first_cord_to_pointer : tnode;
  3461. begin
  3462. result:=nil;
  3463. internalerror(200104043);
  3464. end;
  3465. function ttypeconvnode.first_int_to_int : tnode;
  3466. begin
  3467. first_int_to_int:=nil;
  3468. expectloc:=left.expectloc;
  3469. if not is_void(left.resultdef) then
  3470. begin
  3471. if (left.expectloc<>LOC_REGISTER) and
  3472. ((resultdef.size>left.resultdef.size) or
  3473. (left.expectloc in [LOC_SUBSETREF,LOC_CSUBSETREF,LOC_SUBSETREG,LOC_CSUBSETREG])) then
  3474. expectloc:=LOC_REGISTER
  3475. else
  3476. if (left.expectloc=LOC_CREGISTER) and
  3477. (resultdef.size<left.resultdef.size) then
  3478. expectloc:=LOC_REGISTER;
  3479. end;
  3480. end;
  3481. function ttypeconvnode.first_cstring_to_pchar : tnode;
  3482. begin
  3483. result:=nil;
  3484. expectloc:=LOC_REGISTER;
  3485. end;
  3486. function ttypeconvnode.first_cstring_to_int : tnode;
  3487. begin
  3488. result:=nil;
  3489. internalerror(200510014);
  3490. end;
  3491. function ttypeconvnode.first_string_to_chararray : tnode;
  3492. begin
  3493. first_string_to_chararray:=nil;
  3494. expectloc:=left.expectloc;
  3495. end;
  3496. function ttypeconvnode.first_char_to_string : tnode;
  3497. begin
  3498. first_char_to_string:=nil;
  3499. if tstringdef(resultdef).stringtype=st_shortstring then
  3500. inc(current_procinfo.estimatedtempsize,256);
  3501. expectloc:=LOC_REFERENCE;
  3502. end;
  3503. function ttypeconvnode.first_char_to_chararray : tnode;
  3504. begin
  3505. if resultdef.size <> 1 then
  3506. begin
  3507. { convert first to string, then to chararray }
  3508. inserttypeconv(left,cshortstringtype);
  3509. inserttypeconv(left,resultdef);
  3510. result:=left;
  3511. left := nil;
  3512. exit;
  3513. end;
  3514. result := nil;
  3515. end;
  3516. function ttypeconvnode.first_nothing : tnode;
  3517. begin
  3518. first_nothing:=nil;
  3519. end;
  3520. function ttypeconvnode.first_array_to_pointer : tnode;
  3521. begin
  3522. first_array_to_pointer:=nil;
  3523. make_not_regable(left,[ra_addr_regable]);
  3524. expectloc:=LOC_REGISTER;
  3525. end;
  3526. function ttypeconvnode.first_int_to_real: tnode;
  3527. var
  3528. fname: string[32];
  3529. begin
  3530. if target_info.system in systems_wince then
  3531. begin
  3532. { converting a 64bit integer to a float requires a helper }
  3533. if is_64bitint(left.resultdef) or
  3534. is_currency(left.resultdef) then
  3535. begin
  3536. { hack to avoid double division by 10000, as it's
  3537. already done by typecheckpass.resultdef_int_to_real }
  3538. if is_currency(left.resultdef) then
  3539. left.resultdef := s64inttype;
  3540. if is_signed(left.resultdef) then
  3541. fname:='i64to'
  3542. else
  3543. fname:='ui64to';
  3544. end
  3545. else
  3546. { other integers are supposed to be 32 bit }
  3547. begin
  3548. if is_signed(left.resultdef) then
  3549. fname:='ito'
  3550. else
  3551. fname:='uto';
  3552. firstpass(left);
  3553. end;
  3554. if tfloatdef(resultdef).floattype=s64real then
  3555. fname:=fname+'d'
  3556. else
  3557. fname:=fname+'s';
  3558. result:=ccallnode.createintern(fname,ccallparanode.create(
  3559. left,nil));
  3560. left:=nil;
  3561. firstpass(result);
  3562. exit;
  3563. end
  3564. else
  3565. begin
  3566. { converting a 64bit integer to a float requires a helper }
  3567. if is_64bitint(left.resultdef) or
  3568. is_currency(left.resultdef) then
  3569. begin
  3570. { hack to avoid double division by 10000, as it's
  3571. already done by typecheckpass.resultdef_int_to_real }
  3572. if is_currency(left.resultdef) then
  3573. left.resultdef := s64inttype;
  3574. if is_signed(left.resultdef) then
  3575. fname:='int64_to_'
  3576. else
  3577. { we can't do better currently }
  3578. fname:='qword_to_';
  3579. end
  3580. else
  3581. { other integers are supposed to be 32 bit }
  3582. begin
  3583. if is_signed(left.resultdef) then
  3584. fname:='int32_to_'
  3585. else
  3586. fname:='int64_to_';
  3587. firstpass(left);
  3588. end;
  3589. if tfloatdef(resultdef).floattype=s64real then
  3590. fname:=fname+'float64'
  3591. else
  3592. fname:=fname+'float32';
  3593. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  3594. left,nil)),resultdef);
  3595. left:=nil;
  3596. firstpass(result);
  3597. exit;
  3598. end;
  3599. end;
  3600. function ttypeconvnode.first_real_to_real : tnode;
  3601. begin
  3602. {$ifdef cpufpemu}
  3603. if cs_fp_emulation in current_settings.moduleswitches then
  3604. begin
  3605. if target_info.system in systems_wince then
  3606. begin
  3607. case tfloatdef(left.resultdef).floattype of
  3608. s32real:
  3609. case tfloatdef(resultdef).floattype of
  3610. s64real:
  3611. result:=ccallnode.createintern('stod',ccallparanode.create(left,nil));
  3612. s32real:
  3613. begin
  3614. result:=left;
  3615. left:=nil;
  3616. end;
  3617. else
  3618. internalerror(2005082704);
  3619. end;
  3620. s64real:
  3621. case tfloatdef(resultdef).floattype of
  3622. s32real:
  3623. result:=ccallnode.createintern('dtos',ccallparanode.create(left,nil));
  3624. s64real:
  3625. begin
  3626. result:=left;
  3627. left:=nil;
  3628. end;
  3629. else
  3630. internalerror(2005082703);
  3631. end;
  3632. else
  3633. internalerror(2005082702);
  3634. end;
  3635. left:=nil;
  3636. firstpass(result);
  3637. exit;
  3638. end
  3639. else
  3640. begin
  3641. case tfloatdef(left.resultdef).floattype of
  3642. s32real:
  3643. case tfloatdef(resultdef).floattype of
  3644. s64real:
  3645. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  3646. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  3647. s32real:
  3648. begin
  3649. result:=left;
  3650. left:=nil;
  3651. end;
  3652. else
  3653. internalerror(200610151);
  3654. end;
  3655. s64real:
  3656. case tfloatdef(resultdef).floattype of
  3657. s32real:
  3658. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  3659. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  3660. s64real:
  3661. begin
  3662. result:=left;
  3663. left:=nil;
  3664. end;
  3665. else
  3666. internalerror(200610152);
  3667. end;
  3668. else
  3669. internalerror(200610153);
  3670. end;
  3671. left:=nil;
  3672. firstpass(result);
  3673. exit;
  3674. end;
  3675. end
  3676. else
  3677. {$endif cpufpemu}
  3678. begin
  3679. first_real_to_real:=nil;
  3680. if not use_vectorfpu(resultdef) then
  3681. expectloc:=LOC_FPUREGISTER
  3682. else
  3683. expectloc:=LOC_MMREGISTER;
  3684. end;
  3685. end;
  3686. function ttypeconvnode.first_pointer_to_array : tnode;
  3687. begin
  3688. first_pointer_to_array:=nil;
  3689. expectloc:=LOC_REFERENCE;
  3690. end;
  3691. function ttypeconvnode.first_cchar_to_pchar : tnode;
  3692. begin
  3693. first_cchar_to_pchar:=nil;
  3694. internalerror(200104021);
  3695. end;
  3696. function ttypeconvnode.first_bool_to_int : tnode;
  3697. begin
  3698. first_bool_to_int:=nil;
  3699. { byte(boolean) or word(wordbool) or longint(longbool) must
  3700. be accepted for var parameters }
  3701. if (nf_explicit in flags) and
  3702. (left.resultdef.size=resultdef.size) and
  3703. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3704. begin
  3705. expectloc:=left.expectloc;
  3706. exit;
  3707. end;
  3708. expectloc:=LOC_REGISTER;
  3709. end;
  3710. function ttypeconvnode.first_int_to_bool : tnode;
  3711. begin
  3712. first_int_to_bool:=nil;
  3713. { byte(boolean) or word(wordbool) or longint(longbool) must
  3714. be accepted for var parameters }
  3715. if (nf_explicit in flags) and
  3716. (left.resultdef.size=resultdef.size) and
  3717. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  3718. begin
  3719. {$ifdef xtensa}
  3720. expectloc:=LOC_REGISTER;
  3721. {$else xtensa}
  3722. expectloc:=left.expectloc;
  3723. {$endif xtensa}
  3724. exit;
  3725. end;
  3726. { when converting 64bit int to C-ctyle boolean, first convert to an int32 and then }
  3727. { convert to a boolean (only necessary for 32bit processors) }
  3728. { note: not if left is already a bool (qwordbool that is true, even if
  3729. only because the highest bit is set, must remain true if it is
  3730. --implicitly, unlike integers-- converted to another type of bool),
  3731. Left can already be a bool because this routine can also be called
  3732. from first_bool_to_bool }
  3733. if not is_boolean(left.resultdef) and
  3734. (left.resultdef.size > sizeof(aint)) and
  3735. (left.resultdef.size<>resultdef.size)
  3736. and is_cbool(resultdef) then
  3737. begin
  3738. left:=ctypeconvnode.create_internal(left,s32inttype);
  3739. firstpass(left);
  3740. exit;
  3741. end;
  3742. expectloc:=LOC_REGISTER;
  3743. end;
  3744. function ttypeconvnode.first_bool_to_bool : tnode;
  3745. begin
  3746. first_bool_to_bool:=nil;
  3747. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  3748. not is_cbool(resultdef) then
  3749. expectloc := left.expectloc
  3750. { the following cases use the code generation for bool_to_int/
  3751. int_to_bool -> also set their expectlocs }
  3752. else if (resultdef.size=left.resultdef.size) and
  3753. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  3754. result:=first_bool_to_int
  3755. else
  3756. result:=first_int_to_bool
  3757. end;
  3758. function ttypeconvnode.first_char_to_char : tnode;
  3759. var
  3760. fname: string[18];
  3761. begin
  3762. if (torddef(resultdef).ordtype=uchar) and
  3763. (torddef(left.resultdef).ordtype=uwidechar) then
  3764. fname := 'fpc_uchar_to_char'
  3765. else if (torddef(resultdef).ordtype=uwidechar) and
  3766. (torddef(left.resultdef).ordtype=uchar) then
  3767. fname := 'fpc_char_to_uchar'
  3768. else
  3769. internalerror(2007081201);
  3770. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  3771. left:=nil;
  3772. firstpass(result);
  3773. end;
  3774. function ttypeconvnode.first_proc_to_procvar : tnode;
  3775. begin
  3776. first_proc_to_procvar:=nil;
  3777. { if we take the address of a nested function, the current function/
  3778. procedure needs a stack frame since it's required to construct
  3779. the nested procvar }
  3780. if is_nested_pd(tprocvardef(resultdef)) and
  3781. (
  3782. not (po_anonymous in tprocdef(left.resultdef).procoptions) or
  3783. (po_delphi_nested_cc in tprocvardef(resultdef).procoptions)
  3784. ) then
  3785. include(current_procinfo.flags,pi_needs_stackframe);
  3786. if tabstractprocdef(resultdef).is_addressonly then
  3787. expectloc:=LOC_REGISTER
  3788. else
  3789. expectloc:=left.expectloc;
  3790. end;
  3791. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  3792. begin
  3793. first_nil_to_methodprocvar:=nil;
  3794. expectloc:=LOC_REGISTER;
  3795. end;
  3796. function ttypeconvnode.first_set_to_set : tnode;
  3797. var
  3798. newstatement : tstatementnode;
  3799. temp : ttempcreatenode;
  3800. begin
  3801. { in theory, we should do range checking here,
  3802. but Delphi doesn't do it either (FK) }
  3803. if left.nodetype=setconstn then
  3804. begin
  3805. left.resultdef:=resultdef;
  3806. result:=left;
  3807. left:=nil;
  3808. end
  3809. { equal sets for the code generator? }
  3810. else if (left.resultdef.size=resultdef.size) and
  3811. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  3812. { TODO: This causes wrong (but Delphi-compatible) results for disjoint subsets}
  3813. { e.g., this prints true because of this:
  3814. var
  3815. sa: set of 1..2;
  3816. sb: set of 5..6;
  3817. b: byte;
  3818. begin
  3819. b:=1;
  3820. sa:=[1..2];
  3821. sb:=sa;
  3822. writeln(b in sb);
  3823. end.
  3824. }
  3825. begin
  3826. result:=left;
  3827. left:=nil;
  3828. end
  3829. else
  3830. begin
  3831. result:=internalstatements(newstatement);
  3832. { in case left is a smallset expression, it can be an addn or so. }
  3833. { fpc_varset_load expects a formal const parameter, which doesn't }
  3834. { accept set addn's -> assign to a temp first and pass the temp }
  3835. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  3836. begin
  3837. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  3838. addstatement(newstatement,temp);
  3839. { temp := left }
  3840. addstatement(newstatement,cassignmentnode.create(
  3841. ctemprefnode.create(temp),left));
  3842. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3843. addstatement(newstatement,ctemprefnode.create(temp));
  3844. left:=result;
  3845. firstpass(left);
  3846. { recreate the result's internalstatements list }
  3847. result:=internalstatements(newstatement);
  3848. end;
  3849. { create temp for result }
  3850. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3851. addstatement(newstatement,temp);
  3852. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  3853. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  3854. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3855. ccallparanode.create(ctemprefnode.create(temp),
  3856. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3857. ccallparanode.create(left,nil))))))
  3858. );
  3859. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3860. addstatement(newstatement,ctemprefnode.create(temp));
  3861. left:=nil;
  3862. end;
  3863. end;
  3864. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  3865. begin
  3866. first_ansistring_to_pchar:=nil;
  3867. expectloc:=LOC_REGISTER;
  3868. end;
  3869. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  3870. begin
  3871. first_arrayconstructor_to_set:=nil;
  3872. internalerror(200104022);
  3873. end;
  3874. function ttypeconvnode.first_class_to_intf : tnode;
  3875. var
  3876. hd : tobjectdef;
  3877. ImplIntf : TImplementedInterface;
  3878. begin
  3879. result:=nil;
  3880. expectloc:=LOC_REGISTER;
  3881. hd:=tobjectdef(left.resultdef);
  3882. while assigned(hd) do
  3883. begin
  3884. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  3885. if assigned(ImplIntf) then
  3886. begin
  3887. case ImplIntf.IType of
  3888. etStandard:
  3889. { handle in pass 2 }
  3890. ;
  3891. etFieldValue, etFieldValueClass:
  3892. if is_interface(tobjectdef(resultdef)) then
  3893. begin
  3894. result:=left;
  3895. propaccesslist_to_node(result,tpropertysym(implintf.implementsgetter).owner,tpropertysym(implintf.implementsgetter).propaccesslist[palt_read]);
  3896. { this ensures proper refcounting when field is of class type }
  3897. if not is_interface(result.resultdef) then
  3898. inserttypeconv(result, resultdef);
  3899. left:=nil;
  3900. end
  3901. else
  3902. begin
  3903. internalerror(200802213);
  3904. end;
  3905. etStaticMethodResult, etStaticMethodClass,
  3906. etVirtualMethodResult, etVirtualMethodClass:
  3907. if is_interface(tobjectdef(resultdef)) then
  3908. begin
  3909. { TODO: generating a call to TObject.GetInterface instead could yield
  3910. smaller code size. OTOH, refcounting gotchas are possible that way. }
  3911. { constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags); }
  3912. result:=ccallnode.create(nil,tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym),
  3913. tprocsym(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym).owner,
  3914. left,[],nil);
  3915. addsymref(tpropertysym(implintf.implementsgetter).propaccesslist[palt_read].firstsym^.sym);
  3916. { if it is a class, process it further in a similar way }
  3917. if not is_interface(result.resultdef) then
  3918. inserttypeconv(result, resultdef);
  3919. left:=nil;
  3920. end
  3921. else if is_class(tobjectdef(resultdef)) then
  3922. begin
  3923. internalerror(200802211);
  3924. end
  3925. else
  3926. internalerror(200802231);
  3927. end;
  3928. break;
  3929. end;
  3930. hd:=hd.childof;
  3931. end;
  3932. if hd=nil then
  3933. internalerror(200802164);
  3934. end;
  3935. function ttypeconvnode.first_string_to_string : tnode;
  3936. var
  3937. procname: string[31];
  3938. newblock : tblocknode;
  3939. newstat : tstatementnode;
  3940. restemp : ttempcreatenode;
  3941. begin
  3942. { get the correct procedure name }
  3943. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  3944. '_to_'+tstringdef(resultdef).stringtypname;
  3945. if tstringdef(resultdef).stringtype=st_shortstring then
  3946. begin
  3947. newblock:=internalstatements(newstat);
  3948. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  3949. addstatement(newstat,restemp);
  3950. addstatement(newstat,ccallnode.createintern(procname,ccallparanode.create(left,ccallparanode.create(
  3951. ctemprefnode.create(restemp),nil))));
  3952. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  3953. addstatement(newstat,ctemprefnode.create(restemp));
  3954. result:=newblock;
  3955. end
  3956. { encoding parameter required? }
  3957. else if (tstringdef(resultdef).stringtype=st_ansistring) and
  3958. (tstringdef(left.resultdef).stringtype in [st_widestring,st_unicodestring,st_shortstring,st_ansistring]) then
  3959. result:=ccallnode.createinternres(procname,
  3960. ccallparanode.create(cordconstnode.create(getparaencoding(resultdef),u16inttype,true),
  3961. ccallparanode.create(left,nil)),resultdef)
  3962. else
  3963. result:=ccallnode.createinternres(procname,ccallparanode.create(left,nil),resultdef);
  3964. left:=nil;
  3965. end;
  3966. function ttypeconvnode._first_int_to_int : tnode;
  3967. begin
  3968. result:=first_int_to_int;
  3969. end;
  3970. function ttypeconvnode._first_cstring_to_pchar : tnode;
  3971. begin
  3972. result:=first_cstring_to_pchar;
  3973. end;
  3974. function ttypeconvnode._first_cstring_to_int : tnode;
  3975. begin
  3976. result:=first_cstring_to_int;
  3977. end;
  3978. function ttypeconvnode._first_string_to_chararray : tnode;
  3979. begin
  3980. result:=first_string_to_chararray;
  3981. end;
  3982. function ttypeconvnode._first_char_to_string : tnode;
  3983. begin
  3984. result:=first_char_to_string;
  3985. end;
  3986. function ttypeconvnode._first_char_to_chararray: tnode;
  3987. begin
  3988. result:=first_char_to_chararray;
  3989. end;
  3990. function ttypeconvnode._first_nothing : tnode;
  3991. begin
  3992. result:=first_nothing;
  3993. end;
  3994. function ttypeconvnode._first_array_to_pointer : tnode;
  3995. begin
  3996. result:=first_array_to_pointer;
  3997. end;
  3998. function ttypeconvnode._first_int_to_real : tnode;
  3999. begin
  4000. result:=first_int_to_real;
  4001. end;
  4002. function ttypeconvnode._first_real_to_real : tnode;
  4003. begin
  4004. result:=first_real_to_real;
  4005. end;
  4006. function ttypeconvnode._first_pointer_to_array : tnode;
  4007. begin
  4008. result:=first_pointer_to_array;
  4009. end;
  4010. function ttypeconvnode._first_cchar_to_pchar : tnode;
  4011. begin
  4012. result:=first_cchar_to_pchar;
  4013. end;
  4014. function ttypeconvnode._first_bool_to_int : tnode;
  4015. begin
  4016. result:=first_bool_to_int;
  4017. end;
  4018. function ttypeconvnode._first_int_to_bool : tnode;
  4019. begin
  4020. result:=first_int_to_bool;
  4021. end;
  4022. function ttypeconvnode._first_bool_to_bool : tnode;
  4023. begin
  4024. result:=first_bool_to_bool;
  4025. end;
  4026. function ttypeconvnode._first_proc_to_procvar : tnode;
  4027. begin
  4028. result:=first_proc_to_procvar;
  4029. end;
  4030. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  4031. begin
  4032. result:=first_nil_to_methodprocvar;
  4033. end;
  4034. function ttypeconvnode._first_set_to_set : tnode;
  4035. begin
  4036. result:=first_set_to_set;
  4037. end;
  4038. function ttypeconvnode._first_cord_to_pointer : tnode;
  4039. begin
  4040. result:=first_cord_to_pointer;
  4041. end;
  4042. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  4043. begin
  4044. result:=first_ansistring_to_pchar;
  4045. end;
  4046. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  4047. begin
  4048. result:=first_arrayconstructor_to_set;
  4049. end;
  4050. function ttypeconvnode._first_class_to_intf : tnode;
  4051. begin
  4052. result:=first_class_to_intf;
  4053. end;
  4054. function ttypeconvnode._first_char_to_char : tnode;
  4055. begin
  4056. result:=first_char_to_char;
  4057. end;
  4058. function ttypeconvnode._first_string_to_string : tnode;
  4059. begin
  4060. result:=first_string_to_string;
  4061. end;
  4062. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  4063. const
  4064. firstconvert : array[tconverttype] of pointer = (
  4065. nil, { none }
  4066. @ttypeconvnode._first_nothing, {equal}
  4067. @ttypeconvnode._first_nothing, {not_possible}
  4068. @ttypeconvnode._first_string_to_string,
  4069. @ttypeconvnode._first_char_to_string,
  4070. @ttypeconvnode._first_char_to_chararray,
  4071. nil, { removed in typecheck_chararray_to_string }
  4072. @ttypeconvnode._first_cchar_to_pchar,
  4073. @ttypeconvnode._first_cstring_to_pchar,
  4074. @ttypeconvnode._first_cstring_to_int,
  4075. @ttypeconvnode._first_ansistring_to_pchar,
  4076. @ttypeconvnode._first_string_to_chararray,
  4077. nil, { removed in typecheck_chararray_to_string }
  4078. @ttypeconvnode._first_array_to_pointer,
  4079. @ttypeconvnode._first_pointer_to_array,
  4080. @ttypeconvnode._first_int_to_int,
  4081. @ttypeconvnode._first_int_to_bool,
  4082. @ttypeconvnode._first_bool_to_bool,
  4083. @ttypeconvnode._first_bool_to_int,
  4084. @ttypeconvnode._first_real_to_real,
  4085. @ttypeconvnode._first_int_to_real,
  4086. nil, { removed in typecheck_real_to_currency }
  4087. @ttypeconvnode._first_proc_to_procvar,
  4088. @ttypeconvnode._first_nil_to_methodprocvar,
  4089. @ttypeconvnode._first_arrayconstructor_to_set,
  4090. @ttypeconvnode._first_set_to_set,
  4091. @ttypeconvnode._first_cord_to_pointer,
  4092. @ttypeconvnode._first_nothing,
  4093. @ttypeconvnode._first_nothing,
  4094. @ttypeconvnode._first_class_to_intf,
  4095. @ttypeconvnode._first_char_to_char,
  4096. @ttypeconvnode._first_nothing,
  4097. @ttypeconvnode._first_nothing,
  4098. nil,
  4099. nil,
  4100. nil,
  4101. nil,
  4102. nil,
  4103. nil,
  4104. nil,
  4105. @ttypeconvnode._first_nothing,
  4106. @ttypeconvnode._first_nothing,
  4107. @ttypeconvnode._first_nothing,
  4108. nil,
  4109. nil
  4110. );
  4111. type
  4112. tprocedureofobject = function : tnode of object;
  4113. var
  4114. r : TMethod;
  4115. begin
  4116. { this is a little bit dirty but it works }
  4117. { and should be quite portable too }
  4118. r.Code:=firstconvert[c];
  4119. r.Data:=self;
  4120. if not assigned(r.Code) then
  4121. internalerror(200312081);
  4122. first_call_helper:=tprocedureofobject(r)()
  4123. end;
  4124. function ttypeconvnode.pass_1 : tnode;
  4125. begin
  4126. if warn_pointer_to_signed then
  4127. cgmessage(type_w_pointer_to_signed);
  4128. result:=nil;
  4129. firstpass(left);
  4130. if codegenerror then
  4131. exit;
  4132. expectloc:=left.expectloc;
  4133. if nf_explicit in flags then
  4134. { check if the result could be in a register }
  4135. if (not(tstoreddef(resultdef).is_intregable) and
  4136. not(tstoreddef(resultdef).is_const_intregable) and
  4137. not(tstoreddef(resultdef).is_fpuregable)) or
  4138. ((left.resultdef.typ = floatdef) and
  4139. (resultdef.typ <> floatdef)) then
  4140. make_not_regable(left,[ra_addr_regable]);
  4141. result:=first_call_helper(convtype);
  4142. end;
  4143. function ttypeconvnode.retains_value_location:boolean;
  4144. begin
  4145. result:=assigned(left.resultdef) and
  4146. (
  4147. (convtype=tc_equal) or
  4148. { typecasting from void is always allowed }
  4149. is_void(left.resultdef) or
  4150. (left.resultdef.typ=formaldef) or
  4151. { int 2 int with same size reuses same location, or for
  4152. tp7 mode also allow size < orignal size }
  4153. (
  4154. (convtype=tc_int_2_int) and
  4155. (
  4156. not is_bitpacked_access(left) and
  4157. (resultdef.size=left.resultdef.size) or
  4158. ((m_tp7 in current_settings.modeswitches) and
  4159. (resultdef.size<left.resultdef.size))
  4160. )
  4161. ) or
  4162. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  4163. ((convtype in [tc_int_2_bool,tc_bool_2_int,tc_bool_2_bool]) and
  4164. (nf_explicit in flags) and
  4165. (resultdef.size=left.resultdef.size)) or
  4166. { on managed platforms, converting an element to an open array
  4167. involves creating an actual array -> value location changes }
  4168. ((convtype=tc_elem_2_openarray) and
  4169. not(target_info.system in systems_managed_vm))
  4170. );
  4171. end;
  4172. function ttypeconvnode.assign_allowed:boolean;
  4173. begin
  4174. result:=retains_value_location;
  4175. { When using only a part of the value it can't be in a register since
  4176. that will load the value in a new register first }
  4177. { the same goes for changing the sign of equal-sized values which
  4178. are smaller than an entire register }
  4179. if result and
  4180. { don't try to check the size of an open array }
  4181. (is_open_array(resultdef) or
  4182. (resultdef.size<left.resultdef.size) or
  4183. ((resultdef.size=left.resultdef.size) and
  4184. (left.resultdef.size<sizeof(aint)) and
  4185. (is_signed(resultdef) xor is_signed(left.resultdef)))) then
  4186. make_not_regable(left,[ra_addr_regable]);
  4187. end;
  4188. function ttypeconvnode.docompare(p: tnode) : boolean;
  4189. begin
  4190. docompare :=
  4191. inherited docompare(p) and
  4192. (convtype = ttypeconvnode(p).convtype) and
  4193. (convnodeflags = ttypeconvnode(p).convnodeflags) and
  4194. equal_defs(totypedef,ttypeconvnode(p).totypedef);
  4195. end;
  4196. procedure ttypeconvnode._second_int_to_int;
  4197. begin
  4198. second_int_to_int;
  4199. end;
  4200. procedure ttypeconvnode._second_string_to_string;
  4201. begin
  4202. second_string_to_string;
  4203. end;
  4204. procedure ttypeconvnode._second_cstring_to_pchar;
  4205. begin
  4206. second_cstring_to_pchar;
  4207. end;
  4208. procedure ttypeconvnode._second_cstring_to_int;
  4209. begin
  4210. second_cstring_to_int;
  4211. end;
  4212. procedure ttypeconvnode._second_string_to_chararray;
  4213. begin
  4214. second_string_to_chararray;
  4215. end;
  4216. procedure ttypeconvnode._second_array_to_pointer;
  4217. begin
  4218. second_array_to_pointer;
  4219. end;
  4220. procedure ttypeconvnode._second_pointer_to_array;
  4221. begin
  4222. second_pointer_to_array;
  4223. end;
  4224. procedure ttypeconvnode._second_chararray_to_string;
  4225. begin
  4226. second_chararray_to_string;
  4227. end;
  4228. procedure ttypeconvnode._second_char_to_string;
  4229. begin
  4230. second_char_to_string;
  4231. end;
  4232. procedure ttypeconvnode._second_int_to_real;
  4233. begin
  4234. second_int_to_real;
  4235. end;
  4236. procedure ttypeconvnode._second_real_to_real;
  4237. begin
  4238. second_real_to_real;
  4239. end;
  4240. procedure ttypeconvnode._second_cord_to_pointer;
  4241. begin
  4242. second_cord_to_pointer;
  4243. end;
  4244. procedure ttypeconvnode._second_proc_to_procvar;
  4245. begin
  4246. second_proc_to_procvar;
  4247. end;
  4248. procedure ttypeconvnode._second_nil_to_methodprocvar;
  4249. begin
  4250. second_nil_to_methodprocvar;
  4251. end;
  4252. procedure ttypeconvnode._second_bool_to_int;
  4253. begin
  4254. second_bool_to_int;
  4255. end;
  4256. procedure ttypeconvnode._second_int_to_bool;
  4257. begin
  4258. second_int_to_bool;
  4259. end;
  4260. procedure ttypeconvnode._second_bool_to_bool;
  4261. begin
  4262. second_bool_to_bool;
  4263. end;
  4264. procedure ttypeconvnode._second_set_to_set;
  4265. begin
  4266. second_set_to_set;
  4267. end;
  4268. procedure ttypeconvnode._second_ansistring_to_pchar;
  4269. begin
  4270. second_ansistring_to_pchar;
  4271. end;
  4272. procedure ttypeconvnode._second_class_to_intf;
  4273. begin
  4274. second_class_to_intf;
  4275. end;
  4276. procedure ttypeconvnode._second_char_to_char;
  4277. begin
  4278. second_char_to_char;
  4279. end;
  4280. procedure ttypeconvnode._second_elem_to_openarray;
  4281. begin
  4282. second_elem_to_openarray;
  4283. end;
  4284. procedure ttypeconvnode._second_nothing;
  4285. begin
  4286. second_nothing;
  4287. end;
  4288. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  4289. const
  4290. secondconvert : array[tconverttype] of pointer = (
  4291. @ttypeconvnode._second_nothing, {none}
  4292. @ttypeconvnode._second_nothing, {equal}
  4293. @ttypeconvnode._second_nothing, {not_possible}
  4294. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  4295. @ttypeconvnode._second_char_to_string,
  4296. @ttypeconvnode._second_nothing, {char_to_charray}
  4297. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  4298. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  4299. @ttypeconvnode._second_cstring_to_pchar,
  4300. @ttypeconvnode._second_cstring_to_int,
  4301. @ttypeconvnode._second_ansistring_to_pchar,
  4302. @ttypeconvnode._second_string_to_chararray,
  4303. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  4304. @ttypeconvnode._second_array_to_pointer,
  4305. @ttypeconvnode._second_pointer_to_array,
  4306. @ttypeconvnode._second_int_to_int,
  4307. @ttypeconvnode._second_int_to_bool,
  4308. @ttypeconvnode._second_bool_to_bool,
  4309. @ttypeconvnode._second_bool_to_int,
  4310. @ttypeconvnode._second_real_to_real,
  4311. @ttypeconvnode._second_int_to_real,
  4312. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  4313. @ttypeconvnode._second_proc_to_procvar,
  4314. @ttypeconvnode._second_nil_to_methodprocvar,
  4315. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  4316. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  4317. @ttypeconvnode._second_cord_to_pointer,
  4318. @ttypeconvnode._second_nothing, { interface 2 string }
  4319. @ttypeconvnode._second_nothing, { interface 2 guid }
  4320. @ttypeconvnode._second_class_to_intf,
  4321. @ttypeconvnode._second_char_to_char,
  4322. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  4323. @ttypeconvnode._second_nothing, { pwchar_2_string }
  4324. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  4325. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  4326. @ttypeconvnode._second_nothing, { variant_2_enum }
  4327. @ttypeconvnode._second_nothing, { enum_2_variant }
  4328. @ttypeconvnode._second_nothing, { variant_2_interface }
  4329. @ttypeconvnode._second_nothing, { interface_2_variant }
  4330. @ttypeconvnode._second_nothing, { array_2_dynarray }
  4331. @ttypeconvnode._second_elem_to_openarray, { elem_2_openarray }
  4332. @ttypeconvnode._second_nothing, { arrayconstructor_2_dynarray }
  4333. @ttypeconvnode._second_nothing, { arrayconstructor_2_array }
  4334. @ttypeconvnode._second_nothing, { anonproc_2_funcref }
  4335. @ttypeconvnode._second_nothing { procvar_2_funcref }
  4336. );
  4337. type
  4338. tprocedureofobject = procedure of object;
  4339. var
  4340. r : TMethod;
  4341. begin
  4342. { this is a little bit dirty but it works }
  4343. { and should be quite portable too }
  4344. r.Code:=secondconvert[c];
  4345. r.Data:=self;
  4346. tprocedureofobject(r)();
  4347. end;
  4348. {*****************************************************************************
  4349. TASNODE
  4350. *****************************************************************************}
  4351. function tasisnode.target_specific_typecheck: boolean;
  4352. begin
  4353. result:=false;
  4354. end;
  4355. function tasisnode.pass_typecheck: tnode;
  4356. var
  4357. hp : tnode;
  4358. begin
  4359. result:=nil;
  4360. typecheckpass(right);
  4361. typecheckpass(left);
  4362. set_varstate(right,vs_read,[vsf_must_be_valid]);
  4363. set_varstate(left,vs_read,[vsf_must_be_valid]);
  4364. if codegenerror then
  4365. exit;
  4366. if target_specific_typecheck then
  4367. begin
  4368. // ok
  4369. end
  4370. else if (right.resultdef.typ=classrefdef) then
  4371. begin
  4372. { left maybe an interface reference }
  4373. if is_interfacecom(left.resultdef) or
  4374. is_javainterface(left.resultdef) then
  4375. begin
  4376. { relation checks are not possible }
  4377. end
  4378. { or left must be a class }
  4379. else if is_class(left.resultdef) or
  4380. is_javaclass(left.resultdef) then
  4381. begin
  4382. { the operands must be related }
  4383. if (not(def_is_related(tobjectdef(left.resultdef),
  4384. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  4385. (not(def_is_related(tobjectdef(tclassrefdef(right.resultdef).pointeddef),
  4386. tobjectdef(left.resultdef)))) then
  4387. CGMessage2(type_e_classes_not_related,
  4388. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  4389. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  4390. end
  4391. else
  4392. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  4393. case nodetype of
  4394. isn:
  4395. resultdef:=pasbool1type;
  4396. asn:
  4397. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  4398. else
  4399. ;
  4400. end;
  4401. end
  4402. else if is_interface(right.resultdef) or
  4403. is_dispinterface(right.resultdef) or
  4404. is_javainterface(right.resultdef) then
  4405. begin
  4406. case nodetype of
  4407. isn:
  4408. resultdef:=pasbool1type;
  4409. asn:
  4410. resultdef:=right.resultdef;
  4411. else
  4412. ;
  4413. end;
  4414. { left is a class or interface }
  4415. if is_javainterface(right.resultdef) then
  4416. begin
  4417. if not is_java_class_or_interface(left.resultdef) then
  4418. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  4419. end
  4420. else if not(is_class(left.resultdef) or
  4421. is_interfacecom(left.resultdef)) then
  4422. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename)
  4423. else
  4424. begin
  4425. { load the GUID of the interface }
  4426. if (right.nodetype=typen) then
  4427. begin
  4428. if tobjectdef(right.resultdef).objecttype=odt_interfacecorba then
  4429. begin
  4430. if assigned(tobjectdef(right.resultdef).iidstr) then
  4431. begin
  4432. hp:=cstringconstnode.createstr(tobjectdef(right.resultdef).iidstr^);
  4433. tstringconstnode(hp).changestringtype(cshortstringtype);
  4434. right.free;
  4435. right:=hp;
  4436. end
  4437. else
  4438. internalerror(201006131);
  4439. end
  4440. else
  4441. begin
  4442. if assigned(tobjectdef(right.resultdef).iidguid) then
  4443. begin
  4444. if not(oo_has_valid_guid in tobjectdef(right.resultdef).objectoptions) then
  4445. CGMessage1(type_e_interface_has_no_guid,tobjectdef(right.resultdef).typename);
  4446. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  4447. right.free;
  4448. right:=hp;
  4449. end
  4450. else
  4451. internalerror(201006132);
  4452. end;
  4453. typecheckpass(right);
  4454. end;
  4455. end;
  4456. end
  4457. else
  4458. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  4459. end;
  4460. {*****************************************************************************
  4461. TISNODE
  4462. *****************************************************************************}
  4463. constructor tisnode.create(l,r : tnode);
  4464. begin
  4465. inherited create(isn,l,r);
  4466. end;
  4467. constructor tisnode.create_internal(l, r: tnode);
  4468. begin
  4469. create(l,r);
  4470. include(flags,nf_internal);
  4471. end;
  4472. function tisnode.pass_1 : tnode;
  4473. var
  4474. procname: string;
  4475. statement : tstatementnode;
  4476. tempnode : ttempcreatenode;
  4477. begin
  4478. result:=nil;
  4479. { Passing a class type to an "is" expression cannot result in a class
  4480. of that type to be constructed.
  4481. }
  4482. include(right.flags,nf_ignore_for_wpo);
  4483. if is_class(left.resultdef) and
  4484. (right.resultdef.typ=classrefdef) then
  4485. begin
  4486. if (right.nodetype=loadvmtaddrn) and
  4487. (tloadvmtaddrnode(right).left.nodetype=typen) and
  4488. (oo_is_sealed in tobjectdef(tloadvmtaddrnode(right).left.resultdef).objectoptions) and
  4489. equal_defs(left.resultdef,tclassrefdef(right.resultdef).pointeddef) then
  4490. begin
  4491. if might_have_sideeffects(left) or
  4492. (node_complexity(left)>2) then
  4493. begin
  4494. result:=internalstatements(statement);
  4495. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  4496. addstatement(statement,tempnode);
  4497. addstatement(statement,cassignmentnode.create_internal(ctemprefnode.create(tempnode),left));
  4498. addstatement(statement,caddnode.create_internal(andn,
  4499. caddnode.create_internal(unequaln,ctemprefnode.create(tempnode),cnilnode.create),
  4500. caddnode.create_internal(equaln,cloadvmtaddrnode.create(ctemprefnode.create(tempnode)),right)
  4501. )
  4502. );
  4503. left:=nil;
  4504. right:=nil;
  4505. end
  4506. else
  4507. begin
  4508. result:=caddnode.create_internal(andn,
  4509. caddnode.create_internal(unequaln,left.getcopy,cnilnode.create),
  4510. caddnode.create_internal(equaln,cloadvmtaddrnode.create(left.getcopy),right)
  4511. );
  4512. right:=nil;
  4513. end;
  4514. end
  4515. else
  4516. result := ccallnode.createinternres('fpc_do_is',
  4517. ccallparanode.create(left,ccallparanode.create(right,nil)),
  4518. resultdef);
  4519. end
  4520. else
  4521. begin
  4522. if is_class(left.resultdef) then
  4523. if is_shortstring(right.resultdef) then
  4524. procname := 'fpc_class_is_corbaintf'
  4525. else
  4526. procname := 'fpc_class_is_intf'
  4527. else
  4528. if right.resultdef.typ=classrefdef then
  4529. procname := 'fpc_intf_is_class'
  4530. else
  4531. procname := 'fpc_intf_is';
  4532. result := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4533. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4534. end;
  4535. left := nil;
  4536. right := nil;
  4537. //firstpass(call);
  4538. if codegenerror then
  4539. exit;
  4540. end;
  4541. { dummy pass_2, it will never be called, but we need one since }
  4542. { you can't instantiate an abstract class }
  4543. procedure tisnode.pass_generate_code;
  4544. begin
  4545. end;
  4546. {*****************************************************************************
  4547. TASNODE
  4548. *****************************************************************************}
  4549. constructor tasnode.create(l,r : tnode);
  4550. begin
  4551. inherited create(asn,l,r);
  4552. call := nil;
  4553. end;
  4554. constructor tasnode.create_internal(l,r : tnode);
  4555. begin
  4556. create(l,r);
  4557. include(flags,nf_internal);
  4558. end;
  4559. destructor tasnode.destroy;
  4560. begin
  4561. call.free;
  4562. inherited destroy;
  4563. end;
  4564. function tasnode.dogetcopy: tnode;
  4565. begin
  4566. result := inherited dogetcopy;
  4567. if assigned(call) then
  4568. tasnode(result).call := call.getcopy
  4569. else
  4570. tasnode(result).call := nil;
  4571. end;
  4572. function tasnode.docompare(p: tnode): boolean;
  4573. begin
  4574. result:=
  4575. inherited docompare(p) and
  4576. tasnode(p).call.isequal(call);
  4577. end;
  4578. function tasnode.pass_1 : tnode;
  4579. var
  4580. procname: string;
  4581. begin
  4582. result:=nil;
  4583. { Passing a class type to an "as" expression cannot result in a class
  4584. of that type to be constructed.
  4585. We could put this inside the if-block below, but this way it is
  4586. safe for sure even if the code below changes
  4587. }
  4588. if assigned(right) then
  4589. include(right.flags,nf_ignore_for_wpo);
  4590. if not assigned(call) then
  4591. begin
  4592. if is_class(left.resultdef) and
  4593. (right.resultdef.typ=classrefdef) then
  4594. call := ccallnode.createinternres('fpc_do_as',
  4595. ccallparanode.create(left,ccallparanode.create(right,nil)),
  4596. resultdef)
  4597. else
  4598. begin
  4599. if is_class(left.resultdef) then
  4600. if is_shortstring(right.resultdef) then
  4601. procname := 'fpc_class_as_corbaintf'
  4602. else
  4603. procname := 'fpc_class_as_intf'
  4604. else
  4605. if right.resultdef.typ=classrefdef then
  4606. procname := 'fpc_intf_as_class'
  4607. else
  4608. procname := 'fpc_intf_as';
  4609. call := ctypeconvnode.create_internal(ccallnode.createintern(procname,
  4610. ccallparanode.create(right,ccallparanode.create(left,nil))),resultdef);
  4611. end;
  4612. left := nil;
  4613. right := nil;
  4614. firstpass(call);
  4615. if codegenerror then
  4616. exit;
  4617. expectloc:=call.expectloc;
  4618. end;
  4619. end;
  4620. end.